Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
unexp.c
Go to the documentation of this file.
1 /*
2  * unexp.c - take an exponential RLE file and make an integer file.
3  *
4  * Author: John W. Peterson
5  * Computer Science Dept.
6  * University of Utah
7  * Date: Wed Aug 21 17:02:45 1985
8  *
9  * Based on code by Spencer Thomas
10  */
11 
12 #include <stdio.h>
13 #include <math.h>
14 #include "rle.h"
15 
16 #define MAX(i,j) ( (i) > (j) ? (i) : (j) )
17 
18 #define RLE_EXPONENT (in_hdr.ncolors-1) /* Assume last channel is exp */
19 
22 
23 int truncations = 0;
24 rle_hdr in_hdr, out_hdr;
26 
27 void
29 int argc;
30 char *argv[];
31 {
32  register int x, chan;
33  int y;
34  int scan_flag = 0;
35  int max_flag = 0;
36  int verbose_flag = 0;
37  int print_flag = 0;
38  int oflag = 0;
39  float maxval, tmp, expnt, tmp_max;
40  char *infilename = NULL, *outfilename = NULL;
41  FILE *outfile = stdout;
42  int rle_cnt, rle_err;
43  long start;
44 
45  if (scanargs(argc, argv, "% v%- p%- s%- m%-maxval!f o%-outfile!s infile!s",
46  &verbose_flag, &print_flag, &scan_flag, &max_flag, &maxval,
47  &oflag, &outfilename, &infilename ) == 0)
48  {
49  exit(-1);
50  }
51 
52  (void)rle_hdr_init( &in_hdr );
53  in_hdr.rle_file = rle_open_f(cmd_name( argv ), infilename, "r");
54  rle_names( &in_hdr, cmd_name( argv ), infilename, 0 );
55  rle_names( &out_hdr, in_hdr.cmd, outfilename, 0 );
56  for ( rle_cnt = 0; ; rle_cnt++ )
57  {
58  start = ftell( in_hdr.rle_file );
59  if ( start < 0 && !max_flag && !scan_flag )
60  {
61  fprintf( stderr,
62  "%s: Can't pipe input unless either -m or -s is given.\n",
63  cmd_name( argv ) );
64  exit( 1 );
65  }
66 
67  if ( (rle_err = rle_get_setup( &in_hdr )) != RLE_SUCCESS )
68  break;
69 
70  if (! rle_getcom( "exponential_data", &in_hdr ))
71  fprintf(stderr, "%s: warning - no exponential_data comment\n",
72  cmd_name( argv ));
73 
74  if (in_hdr.ncolors < 2)
75  {
76  fprintf(stderr,
77  "%s: File does not contain exponent channel.\n",
78  cmd_name( argv ));
79  exit(-4);
80  }
81 
82  if ( rle_row_alloc( &in_hdr, &in_rows ) < 0 )
83  RLE_CHECK_ALLOC( cmd_name( argv ), 0, 0 );
84 
85  /* If maximum value isn't given, slosh through file to find it */
86  if (! max_flag)
87  {
88  maxval = 0.0;
89  for (y = in_hdr.ymin; y <= in_hdr.ymax; y++)
90  {
91  if ((verbose_flag) && (y % 100) == 0)
92  fprintf(stderr, "Scanning row %d...\n", y);
93 
95 
96  for (x = in_hdr.xmin; x <= in_hdr.xmax; x++)
97  {
98  expnt = ldexp( 1/256.0, in_rows[RLE_EXPONENT][x] - 127);
99  tmp = -2000.0;
100  for (chan = 0; chan < RLE_EXPONENT; chan++)
101  tmp = MAX( tmp, (float)in_rows[chan][x] );
102 
103  tmp *= expnt;
104  maxval = MAX( maxval, tmp );
105  }
106  }
107  if (scan_flag || print_flag || verbose_flag)
108  fprintf(stderr, "Maximum value: %1.8g\n", maxval);
109 
110  if (scan_flag) exit(0);
111 
112  fseek( in_hdr.rle_file, start, 0 );
113 
115  }
116 
117  /* Open output file */
118 
119  (void)rle_hdr_cp( &in_hdr, &out_hdr );
120  rle_delcom( "exponential_data", &out_hdr );
122  if ( rle_cnt == 0 )
123  outfile = rle_open_f( cmd_name( argv ), outfilename, "w" );
124  out_hdr.rle_file = outfile;
125 
127 
129 
130  out_hdr.xmin = 0;
132 
133  if (rle_row_alloc( &out_hdr, &out_rows ) < 0)
134  RLE_CHECK_ALLOC( cmd_name( argv ), 0, 0 );
135 
136  /*
137  * Convert byte/exponent form into straight RLE.
138  * Alpha is passed straight through.
139  */
140 
141  if ( maxval == 0.0 )
142  maxval = 1.0;
143  else
144  maxval /= 255; /* Pre-scale to 0-255 */
145 
146  for (y = in_hdr.ymin; y <= in_hdr.ymax; y++)
147  {
148  if (((y % 100) == 0) && verbose_flag)
149  fprintf(stderr, "Processing row %d...\n", y);
150 
152 
153  for (x = in_hdr.xmin; x <= in_hdr.xmax; x++)
154  {
155  /* Only bother with pixels with coverage */
156  if (in_rows[RLE_ALPHA][x])
157  {
158  expnt = ldexp( 1/256.0, in_rows[RLE_EXPONENT][x] - 127 );
159  tmp_max = -1000;
160  for( chan = 0; chan < RLE_EXPONENT; chan++ )
161  tmp_max = MAX( tmp_max, in_rows[chan][x] );
162 
163  tmp = expnt / maxval * tmp_max;
164  if (tmp > 255)
165  {
166  tmp = 255 / tmp;
167  truncations++;
168  }
169  else
170  tmp = 1.0;
171 
172  tmp *= expnt / maxval;
173  for( chan = 0; chan < RLE_EXPONENT; chan++ )
174  out_rows[chan][x-in_hdr.xmin] = (rle_pixel) (int)
175  (in_rows[chan][x] * tmp);
177  in_rows[RLE_ALPHA][x];
178  }
179  else
180  for( chan = RLE_ALPHA; chan < RLE_EXPONENT; chan++ )
181  out_rows[chan][x-in_hdr.xmin] = 0;
182 
183  }
185  &out_hdr );
186  }
188 
189  if (truncations)
190  fprintf(stderr,"%s: %d bytes truncated (sorry...)\n",
191  cmd_name( argv ), truncations);
192 
195  }
196 
197  /* Check for an error. EOF or EMPTY is ok if at least one image
198  * has been read. Otherwise, print an error message.
199  */
200  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
201  rle_get_error( rle_err, cmd_name( argv ), infilename );
202 
203  exit( 0 );
204 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
int xmin
Definition: rle.h:100
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
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
rle_pixel ** in_rows
Definition: unexp.c:20
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_EXPONENT
Definition: unexp.c:18
#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
rle_hdr out_hdr
Definition: unslice.c:34
const char * rle_delcom(char *name, the_hdr) const
Definition: rle_putcom.c:140
#define RLE_EOF
Definition: rle.h:74
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
char * rle_getcom(char *name, rle_hdr *the_hdr) const
Definition: rle_getcom.c:81
rle_pixel g_in
Definition: unexp.c:25
rle_pixel ** out_rows
Definition: unexp.c:21
#define MAX(i, j)
Definition: get4d.c:23
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 truncations
Definition: unexp.c:23
#define RLE_ALPHA
Definition: rle.h:65
rle_pixel b_in
Definition: unexp.c:25
rle_hdr in_hdr
Definition: unslice.c:34
rle_pixel r_in
Definition: unexp.c:25
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