Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
avg4.c
Go to the documentation of this file.
1 /*
2  * This software is copyrighted as noted below. It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is". Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * avg4.c - Reduce image by half in X and Y, producing alphas even
20  * if they weren't there originally.
21  *
22  * Author: Rod Bogart & John W. Peterson
23  * Computer Science Dept.
24  * University of Utah
25  * Date: Fri Jun 20 1986
26  * Copyright (c) 1986, University of Utah
27  *
28  */
29 #ifndef lint
30 static char rcs_ident[] = "$Header: /l/spencer/src/urt/tools/RCS/avg4.c,v 3.0.1.4 1992/04/30 14:08:43 spencer Exp $";
31 #endif
32 
33 
34 #include <stdio.h>
35 #include "rle.h"
36 
37 static bit_count[16] = {0, 63, 63, 127, 63, 127, 127,
38  192, 63, 127, 127, 192, 127, 192, 192, 255};
39 
40 void
42 int argc;
43 char *argv[];
44 {
45  char *infname = NULL, *outfname = NULL;
46  char *cmd = cmd_name( argv );
47  int oflag = 0;
48  int rle_cnt;
49  FILE *outfile = stdout;
50  int i, j;
51  int new_xlen,
52  new_ylen;
53  int rle_err;
54  rle_hdr in_hdr, out_hdr;
55  rle_pixel **rows0, **rows1, **rowsout;
56  rle_pixel *ptr0, *ptr1, *ptrout, *alphptr;
57  int A, chan;
58 
59  in_hdr = *rle_hdr_init( NULL );
60  out_hdr = *rle_hdr_init( NULL );
61 
62  if ( scanargs( argc, argv, "% o%-outfile!s infile%s",
63  &oflag, &outfname, &infname ) == 0 )
64  exit( 1 );
65 
66  in_hdr.rle_file = rle_open_f( cmd, infname, "r" );
67  rle_names( &in_hdr, cmd, infname, 0 );
68  rle_names( &out_hdr, in_hdr.cmd, outfname, 0 );
69 
70  for ( rle_cnt = 0;
71  (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS;
72  rle_cnt++ )
73  {
74  if ( rle_cnt == 0 )
75  outfile = rle_open_f( cmd, outfname, "w" );
76 
77  (void)rle_hdr_cp( &in_hdr, &out_hdr );
78  rle_addhist( argv, &in_hdr, &out_hdr );
79 
80  /* Force input to an even length line. */
81  if ( (in_hdr.xmax - in_hdr.xmin) % 2 == 0 )
82  in_hdr.xmax++;
83 
84  new_xlen = (in_hdr.xmax - in_hdr.xmin + 1 ) / 2;
85  new_ylen = (in_hdr.ymax - in_hdr.ymin + 2 ) / 2;
86  out_hdr.xmin = in_hdr.xmin / 2;
87  out_hdr.ymin = in_hdr.ymin / 2;
88  out_hdr.xmax = out_hdr.xmin + new_xlen - 1;
89  out_hdr.ymax = out_hdr.ymin + new_ylen - 1;
90 
91  out_hdr.alpha = 1; /* Force alpha in output. */
92  RLE_SET_BIT( out_hdr, RLE_ALPHA );
93 
94  out_hdr.rle_file = outfile;
95  rle_put_setup( &out_hdr );
96 
97  /* Oink. */
98  if ( rle_row_alloc( &in_hdr, &rows0 ) < 0 ||
99  rle_row_alloc( &in_hdr, &rows1 ) < 0 ||
100  rle_row_alloc( &out_hdr, &rowsout ) < 0 )
101  RLE_CHECK_ALLOC( cmd, 0, "image" );
102 
103  for ( j = 0; j < new_ylen*2; j+=2 )
104  {
105  rle_getrow(&in_hdr, rows0 );
106  rle_getrow(&in_hdr, rows1 );
107 
108  for (chan = RLE_ALPHA; chan < in_hdr.ncolors; chan++)
109  {
110  ptr0 = &(rows0[chan][in_hdr.xmin]);
111  ptr1 = &(rows1[chan][in_hdr.xmin]);
112  ptrout = rowsout[chan];
113  alphptr = rowsout[RLE_ALPHA];
114  /*
115  * If we don't start out with an alpha channel in the
116  * original image, then we want to fake one up. This
117  * works by counting the number of non-zero pixels in the
118  * R, G and B channels. We set bits in the alpha channel
119  * for the non-zero pixels found, then use bit_count to
120  * convert this to reasonable coverage values.
121  */
122  if ((chan == RLE_ALPHA) && (!in_hdr.alpha))
123  {
124  bzero(alphptr, new_xlen);
125  }
126  else for( i = 0; i < new_xlen; i++)
127  {
128  if (!in_hdr.alpha)
129  {
130  *alphptr |= (*ptr0 ? 1 : 0) | (ptr0[1] ? 2 : 0) |
131  (*ptr1 ? 4 : 0) | (ptr1[1] ? 8 : 0);
132 
133  /* calc fake alpha from bit count */
134  if (chan == (in_hdr.ncolors - 1))
135  *alphptr = bit_count[*alphptr];
136 
137  alphptr++;
138  }
139  A = (int) *ptr0++ + (int) *ptr1++;
140  A += (int) *ptr0++ + (int) *ptr1++;
141  *ptrout++ = (rle_pixel) (A / 4);
142  }
143  }
144  rle_putrow( rowsout, new_xlen, &out_hdr );
145  }
146  rle_puteof( &out_hdr );
147 
148  /* Skip last row if odd number of rows. */
149  while ( rle_getskip( &in_hdr ) != 32768 )
150  ;
151 
152  rle_row_free( &in_hdr, rows0 );
153  rle_row_free( &in_hdr, rows1 );
154  rle_row_free( &out_hdr, rowsout );
155  }
156 
157  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
158  rle_get_error( rle_err, cmd, infname );
159 
160  exit( 0 );
161 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
#define RLE_SET_BIT(glob, bit)
Definition: rle.h:122
int xmin
Definition: rle.h:100
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
static char rcs_ident[]
Definition: avg4.c:30
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
#define RLE_EMPTY
Definition: rle.h:73
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void main(int argc, char **argv)
Definition: aliastorle.c:121
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
int rle_getrow(rle_hdr *the_hdr, scanline)
Definition: rle_getrow.c:333
#define RLE_SUCCESS
Definition: rle.h:70
int ymin
Definition: rle.h:100
int rle_get_error(int code, const char *pgmname, const char *fname)
Definition: rle_error.c:76
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
const char * cmd
Definition: rle.h:133
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
Definition: rle_putrow.c:96
int xmax
Definition: rle.h:100
static bit_count[16]
Definition: avg4.c:37
#define RLE_EOF
Definition: rle.h:74
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
unsigned int rle_getskip(rle_hdr *the_hdr)
Definition: rle_getskip.c:57
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
int alpha
Definition: rle.h:100
#define RLE_ALPHA
Definition: rle.h:65
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86