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

Go to the source code of this file.

Macros

#define CLAMP(x)   ((x)<0 ? 0 : ((x)>255 ? 255 : (x)))
 

Functions

void copy_raw ()
 
void main (int argc, char **argv)
 
void copy_raw (rle_hdr *the_hdr, int y, rle_op **scan, int *nraw, rle_op ***save_scan, int **save_nraw)
 

Macro Definition Documentation

#define CLAMP (   x)    ((x)<0 ? 0 : ((x)>255 ? 255 : (x)))

Definition at line 20 of file rlespiff.c.

Function Documentation

void copy_raw ( )
void copy_raw ( rle_hdr the_hdr,
int  y,
rle_op **  scan,
int nraw,
rle_op ***  save_scan,
int **  save_nraw 
)

Definition at line 295 of file rlespiff.c.

302 {
303  int totlen = 0;
304  register int c;
305 
306  /* Allocate space to save counts */
307  save_nraw[y] = (int *)malloc( (the_hdr->ncolors + the_hdr->alpha) *
308  sizeof(int) );
309  if ( save_nraw[y] == NULL )
310  goto malloc_err;
311  if ( the_hdr->alpha )
312  save_nraw[y]++;
313  /* Count total number of raw data to save, and save counts. */
314  for ( c = -the_hdr->alpha; c < the_hdr->ncolors; c++ )
315  totlen += save_nraw[y][c] = nraw[c];
316 
317  /* Allocate space to save raw data */
318  save_scan[y] = (rle_op **)malloc( (the_hdr->ncolors + the_hdr->alpha) *
319  sizeof(rle_op *) );
320  if ( save_scan[y] == NULL )
321  goto malloc_err;
322 /* BUG fixed by Michel GAUDET
323  * In case of totlen = 0 (happens if all runs in a given line)
324  * the return value of malloc is NULL
325  * and it is not an error of malloc
326  * Correction : add one unit to the size
327  */
328  save_scan[y][0] = (rle_op *)malloc( 1 + totlen * sizeof(rle_op) );
329  if ( save_scan[y][0] == NULL )
330  goto malloc_err;
331  if ( the_hdr->alpha )
332  save_scan[y]++;
333 
334  /* Save raw data */
335  for ( c = -the_hdr->alpha; c < the_hdr->ncolors; c++ )
336  {
337  if ( c > -the_hdr->alpha )
338  save_scan[y][c] = save_scan[y][c-1] + nraw[c-1];
339  bcopy( scan[c], save_scan[y][c], nraw[c] * sizeof(rle_op) );
340  }
341  return;
342 
343 malloc_err:
344  RLE_CHECK_ALLOC( the_hdr->cmd, 0, 0 );
345 }
static int y
Definition: getami.c:691
const char * cmd
Definition: rle.h:133
int
Definition: getami.c:848
void * malloc()
int alpha
Definition: rle.h:100
Definition: rle_raw.h:47
int ncolors
Definition: rle.h:100
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86
void main ( int  argc,
char **  argv 
)

Definition at line 60 of file rlespiff.c.

63 {
64  int rle_cnt = 0;
65  int tflag = 0,
66  threshold = 10;
67  int bflag = 0,
68  blacklevel = 0;
69  int wflag = 0,
70  whitelevel = 255;
71  int sflag = 0;
72  int nhist;
73  int verbose = 0;
74  int oflag = 0;
75  char *outfname = NULL,
76  *infname = NULL;
77  FILE *outfile = stdout;
78  int rle_err;
80  out_hdr;
81  rle_op **scan, ***save_scan;
82  register rle_op *scanp;
83  int *nraw, **save_nraw;
84  int y, ynext;
85  int c, hc, i, x;
86  register int t; /* Temp value. */
87  long int (*histo)[256];
88  int thresh; /* Mapped threshold. */
89  int maxval, minval;
90  int *slide;
91  float *stretch;
92 
93  in_hdr = *rle_hdr_init( NULL );
94  out_hdr = *rle_hdr_init( NULL );
95 
96  if ( scanargs( argc, argv,
97  "% t%-threshold!d b%-blacklevel!d w%-whitelevel!d s%- v%- \
98 \to%-outfile!s infile%s",
99  &tflag, &threshold, &bflag, &blacklevel,
100  &wflag, &whitelevel,
101  &sflag, &verbose, &oflag, &outfname, &infname ) == 0 )
102  exit( 1 );
103 
104  /* Open the files */
105  in_hdr.rle_file = rle_open_f( cmd_name( argv ), infname, "r" );
106  rle_names( &in_hdr, cmd_name( argv ), infname, 0 );
107  rle_names( &out_hdr, in_hdr.cmd, outfname, 0 );
108 
109  /* Loop over all images in input file */
110  for ( rle_cnt = 0;
111  (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS;
112  rle_cnt++ )
113  {
114  if ( rle_cnt == 0 )
115  outfile = rle_open_f( cmd_name( argv ), outfname, "w" );
116 
117  if ( rle_raw_alloc( &in_hdr, &scan, &nraw ) < 0 )
118  RLE_CHECK_ALLOC( in_hdr.cmd, 0, "image data" );
119 
120  save_scan = (rle_op ***)calloc( in_hdr.ymax + 1,
121  sizeof( rle_op ** ) );
122  save_nraw = (int **)calloc( in_hdr.ymax + 1,
123  sizeof( int * ) );
124  RLE_CHECK_ALLOC( in_hdr.cmd, save_scan && save_nraw, "image data" );
125 
126  nhist = sflag ? in_hdr.ncolors : 1;
127  histo = (long int (*)[256]) calloc( 256 * nhist, sizeof(long int) );
128  stretch = (float *)calloc( nhist, sizeof(float) );
129  slide = (int *)calloc( nhist, sizeof(int) );
130  RLE_CHECK_ALLOC( in_hdr.cmd, histo && stretch && slide, 0 );
131 
132  /* Read the input and find min & max */
133  while ( (y = rle_getraw( &in_hdr, scan, nraw )) != 32768 )
134  {
135  /* Histogram values. */
136  for ( c = 0; c < in_hdr.ncolors; c++ )
137  {
138  if ( sflag )
139  hc = c;
140  else
141  hc = 0;
142 
143  for ( i = 0, scanp = scan[c]; i < nraw[c]; i++, scanp++ )
144  switch ( scanp->opcode )
145  {
146  case RByteDataOp:
147  for ( x = 0; x < scanp->length; x++ )
148  histo[hc][scanp->u.pixels[x]]++;
149  break;
150  case RRunDataOp:
151  histo[hc][scanp->u.run_val] += scanp->length;
152  break;
153  }
154  }
155 
156  /* Save scanline data for second pass. */
157  copy_raw( &in_hdr, y, scan, nraw, save_scan, save_nraw );
158  }
159 
160 
161  /* Determine min & max */
162  thresh = (((double)threshold * (in_hdr.xmax - in_hdr.xmin + 1) *
163  (in_hdr.ymax - in_hdr.ymin + 1) +
164  500000)) / 1000000;
165 
166  if ( !sflag )
167  thresh *= in_hdr.ncolors;
168 
169  for ( hc = 0; hc < nhist; hc++ )
170  {
171  for ( i = 0; i < 256 && histo[hc][i] <= thresh; i++ )
172  ;
173  minval = i;
174  for ( i = 255; i >= 0 && histo[hc][i] <= thresh; i-- )
175  ;
176  maxval = i;
177 
178  /* Mapping parameters */
179  if ( maxval > minval )
180  stretch[hc] = (float)(whitelevel - blacklevel) /
181  (maxval - minval);
182  else
183  stretch[hc] = 1.0;
184  slide[hc] = blacklevel - stretch[hc] * minval;
185 
186  if ( verbose )
187  {
188  fprintf( stderr, "%s image %d",
189  in_hdr.rle_file == stdin ? "Standard input" : infname,
190  rle_cnt );
191  if ( sflag )
192  fprintf( stderr, ", channel %d", hc );
193  fprintf( stderr, " min = %d, max = %d\n", minval, maxval );
194  }
195  }
196 
197  /* Pass 2 -- map pixels and write output file */
198  (void)rle_hdr_cp( &in_hdr, &out_hdr );
199  out_hdr.rle_file = outfile;
200  rle_addhist( argv, &in_hdr, &out_hdr );
201  rle_put_setup( &out_hdr );
202  for ( y = in_hdr.ymin, ynext = -1;
203  y <= in_hdr.ymax;
204  y++ )
205  {
206  if ( save_scan[y] )
207  {
208  /* Map pixel values */
209  for ( c = 0; c < in_hdr.ncolors; c++ )
210  {
211  float h_stretch;
212  int h_slide;
213 
214  if ( sflag )
215  {
216  h_stretch = stretch[c];
217  h_slide = slide[c];
218  }
219  else
220  {
221  h_stretch = stretch[0];
222  h_slide = slide[0];
223  }
224 
225  for ( i = 0, scanp = save_scan[y][c];
226  i < save_nraw[y][c];
227  i++, scanp++ )
228  switch ( scanp->opcode )
229  {
230  case RByteDataOp:
231  for ( x = 0; x < scanp->length; x++ )
232  {
233  t = scanp->u.pixels[x] * h_stretch + h_slide;
234  scanp->u.pixels[x] = CLAMP( t );
235  }
236  break;
237  case RRunDataOp:
238  t = scanp->u.run_val * h_stretch + h_slide;
239  scanp->u.run_val = CLAMP( t );
240  break;
241  }
242  }
243 
244  if ( ynext == -1 )
245  {
246  ynext = in_hdr.ymin;
247  if ( y - ynext > 0 )
248  rle_skiprow( &out_hdr, y - ynext );
249  }
250  else
251  if ( y - ynext > 1 )
252  rle_skiprow( &out_hdr, y - ynext );
253  rle_putraw( save_scan[y], save_nraw[y], &out_hdr );
254  rle_freeraw( &in_hdr, save_scan[y], save_nraw[y] );
255  rle_raw_free( &in_hdr, save_scan[y], save_nraw[y] );
256  ynext = y; /* Most recent "real" scanline. */
257  }
258  }
259  rle_puteof( &out_hdr );
260  rle_raw_free( &in_hdr, scan, nraw );
261  free( stretch );
262  free( slide );
263  free( histo );
264  }
265 
266  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
267  rle_get_error( rle_err, argv[0], infname );
268 
269  exit( 0 );
270 }
void rle_skiprow(rle_hdr *the_hdr, int nrow)
Definition: rle_putrow.c:393
#define RRunDataOp
Definition: rle_code.h:41
int xmin
Definition: rle.h:100
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
void rle_freeraw(rle_hdr *the_hdr, scanraw, nraw)
Definition: rle_getraw.c:268
static rle_hdr in_hdr
Definition: rletogif.c:37
FILE * outfile
Definition: giftorle.c:61
int length
Definition: rle_raw.h:51
int opcode
Definition: rle_raw.h:49
#define RLE_EMPTY
Definition: rle.h:73
int rle_raw_alloc()
void rle_addhist(char *argv[], rle_hdr *in_hdr, rle_hdr *out_hdr)
#define RLE_SUCCESS
Definition: rle.h:70
void rle_raw_free()
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
unsigned char ** scan
Definition: rle.c:26
int run_val
Definition: rle_raw.h:54
int verbose
Definition: aliastorle.c:96
string infname
Definition: getbob.c:68
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
Definition: rle.h:96
#define sflag
void rle_putraw()
void copy_raw()
int xmax
Definition: rle.h:100
static int x
Definition: getami.c:691
#define CLAMP(x)
Definition: rlespiff.c:20
int rle_get_error(int code, const char *pgmname, const char *fname)
#define RLE_EOF
Definition: rle.h:74
int
Definition: getami.c:848
int ymax
Definition: rle.h:100
rle_pixel * pixels
Definition: rle_raw.h:53
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
int i
Definition: rletorla.c:82
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
union rle_op::@7 u
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
char * cmd_name(char **argv)
Definition: cmd_name.c:31
gray maxval
Definition: pgmtorle.c:53
int oflag
Definition: painttorle.c:45
Definition: rle_raw.h:47
FILE * rle_file
Definition: rle.h:114
unsigned int rle_getraw(rle_hdr *the_hdr, scanraw, nraw)
Definition: rle_getraw.c:78
rle_hdr out_hdr
Definition: dvirle2.c:89
int ncolors
Definition: rle.h:100
#define RByteDataOp
Definition: rle_code.h:40
#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