Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
Macros | Functions | Variables
unexp.c File Reference
#include <stdio.h>
#include <math.h>
#include "rle.h"
Include dependency graph for unexp.c:

Go to the source code of this file.

Macros

#define MAX(i, j)    ( (i) > (j) ? (i) : (j) )
 
#define RLE_EXPONENT   (in_hdr.ncolors-1) /* Assume last channel is exp */
 

Functions

void main (int argc, argv)
 

Variables

rle_pixel ** in_rows
 
rle_pixel ** out_rows
 
int truncations = 0
 
rle_hdr in_hdr
 
rle_hdr out_hdr
 
rle_pixel r_in
 
rle_pixel g_in
 
rle_pixel b_in
 

Macro Definition Documentation

#define MAX (   i,
 
)    ( (i) > (j) ? (i) : (j) )

Definition at line 16 of file unexp.c.

#define RLE_EXPONENT   (in_hdr.ncolors-1) /* Assume last channel is exp */

Definition at line 18 of file unexp.c.

Function Documentation

void main ( int  argc,
argv   
)

Definition at line 28 of file unexp.c.

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 
114  rle_get_setup( &in_hdr );
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" );
125 
126  rle_addhist( argv, &in_hdr, &out_hdr );
127 
129 
130  out_hdr.xmin = 0;
131  out_hdr.xmax -= in_hdr.xmin;
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  }
187  rle_puteof( &out_hdr );
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 }
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
int xmin
Definition: rle.h:100
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
FILE * outfile
Definition: giftorle.c:61
char * rle_getcom(const char *name, rle_hdr *the_hdr)
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
const char * rle_delcom(const char *name, rle_hdr *the_hdr)
#define RLE_EMPTY
Definition: rle.h:73
rle_pixel ** in_rows
Definition: unexp.c:20
Boolean verbose_flag
Definition: getx11.c:80
void rle_putrow(rle_pixel *rows[], int rowlen, rle_hdr *the_hdr)
#define RLE_EXPONENT
Definition: unexp.c:18
void rle_addhist(char *argv[], rle_hdr *in_hdr, rle_hdr *out_hdr)
#define RLE_SUCCESS
Definition: rle.h:70
int ymin
Definition: rle.h:100
void rle_puteof(rle_hdr *the_hdr)
Definition: rle_putrow.c:474
static int y
Definition: getami.c:691
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
const char * cmd
Definition: rle.h:133
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
int xmax
Definition: rle.h:100
static int x
Definition: getami.c:691
int rle_get_error(int code, const char *pgmname, const char *fname)
#define RLE_EOF
Definition: rle.h:74
rle_pixel ** out_rows
Definition: unexp.c:21
int
Definition: getami.c:848
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
int truncations
Definition: unexp.c:23
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
#define RLE_ALPHA
Definition: rle.h:65
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
rle_hdr in_hdr
Definition: unexp.c:24
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
#define MAX(i, j)
Definition: unexp.c:16
rle_hdr out_hdr
Definition: unexp.c:24
char * cmd_name(char **argv)
Definition: cmd_name.c:31
gray maxval
Definition: pgmtorle.c:53
int oflag
Definition: painttorle.c:45
int rle_getrow(rle_hdr *the_hdr, rle_pixel *scanline[])
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267

Variable Documentation

rle_pixel b_in

Definition at line 25 of file unexp.c.

rle_pixel g_in

Definition at line 25 of file unexp.c.

rle_hdr in_hdr

Definition at line 24 of file unexp.c.

rle_pixel** in_rows

Definition at line 20 of file unexp.c.

rle_hdr out_hdr

Definition at line 24 of file unexp.c.

rle_pixel** out_rows

Definition at line 21 of file unexp.c.

rle_pixel r_in

Definition at line 25 of file unexp.c.

int truncations = 0

Definition at line 23 of file unexp.c.