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

Go to the source code of this file.

Functions

void get_rle_map ()
 
void linmap ()
 
void gammap ()
 
void filemap ()
 
void mfilemap ()
 
void applymap ()
 
void shiftmap ()
 
void main (int argc, char **argv)
 
rle_map ** allocmap (int nchan, int length, rle_map *cmap)
 
void shiftmap (rle_map **map, int nchan, int length, int bits)
 
void applymap (rle_map **map, int nchan, int length, int bits, rle_map **submap, int subchan, int sublen, int subbits, rle_map **omap)
 
void linmap (double factor, int nchan, int length, int range, rle_map **amap)
 
void gammap (double gamma, int nchan, int length, int range, rle_map **amap)
 
void filemap (int tflag, char *mapfname, int nchan, int length, rle_map **amap)
 
void mfilemap (char **mfnames, int nchan, int length, rle_map **amap)
 
void get_rle_map (rle_hdr *the_hdr, char *fname)
 

Variables

static char rcsid [] = "$Header: /tmp_mnt/n/itn/hendrix/u/spencer/RCS/rleldmap.c,v 3.0.1.5 1992/02/28 22:12:27 spencer Exp spencer $"
 

Function Documentation

rle_map** allocmap ( int  nchan,
int  length,
rle_map cmap 
)

Definition at line 373 of file rleldmap.c.

377 {
378  rle_map ** map;
379  register int i;
380 
381  map = (rle_map **)malloc( nchan * sizeof( rle_map * ) );
382  RLE_CHECK_ALLOC( "rleldmap", map, "color map" );
383  if ( cmap == NULL )
384  {
385  map[0] = (rle_map *)malloc( nchan * length * sizeof( rle_map ) );
386  RLE_CHECK_ALLOC( "rleldmap", map[0], "color map" );
387  }
388  else
389  map[0] = cmap;
390  for ( i = 1; i < nchan; i++ )
391  map[i] = &map[i-1][length];
392  return map;
393 }
short map[3][256]
Definition: getap.c:115
void * malloc()
int i
Definition: rletorla.c:82
int nchan
Definition: fant.c:104
unsigned short rle_map
Definition: rle.h:57
rle_pixel ** cmap
Definition: get4d.c:47
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86
void applymap ( )
void applymap ( rle_map **  map,
int  nchan,
int  length,
int  bits,
rle_map **  submap,
int  subchan,
int  sublen,
int  subbits,
rle_map **  omap 
)

Definition at line 456 of file rleldmap.c.

461 {
462  register rle_map * s; /* pointer into submap */
463  register rle_map * o; /* pointer into omap */
464  rle_map ** mp, ** sp, ** op; /* pointer to channel of maps */
465  register int subshift;
466  int c, i, ochan = ((nchan > subchan) ? nchan : subchan);
467  int sub;
468 
469  /* Figure out how much to shift subscript */
470  for ( i = 1; i < length; i *= 2 )
471  ; /* Round up to power of 2. */
472  if ( (1 << subbits) > i ) /* Too many bits in subscript? */
473  for ( subshift = 0; (1 << (subbits + subshift)) > i; subshift-- )
474  ;
475  else /* not enough bits */
476  for ( subshift = 0; (1 << (subbits + subshift)) < i; subshift++ )
477  ;
478 
479  if ( subshift >= 0 )
480  {
481  for ( c = ochan, mp = map, sp = submap, op = omap;
482  c > 0;
483  c--, op++, (nchan > 1 ? mp++ : 0), (subchan > 1 ? sp++ : 0) )
484  for ( i = sublen, s = *sp, o = *op; i > 0; i--, s++, o++ )
485  if ( (sub = (*s) << subshift) > length )
486  *o = 0;
487  else
488  *o = (*mp)[sub];
489  }
490  else
491  {
492  subshift = -subshift;
493  for ( c = ochan, mp = map, sp = submap, op = omap;
494  c > 0;
495  c--, op++, (nchan > 1 ? mp++ : 0), (subchan > 1 ? sp++ : 0) )
496  for ( i = sublen, s = *sp, o = *op; i > 0; i--, s++, o++ )
497  if ( (sub = (*s) >> subshift) > length )
498  *o = 0;
499  else
500  *o = (*mp)[sub];
501  }
502 
503  shiftmap( omap, ochan, sublen, bits );
504 }
_urt_stack * s
Definition: rleClock.c:919
short map[3][256]
Definition: getap.c:115
void shiftmap()
int i
Definition: rletorla.c:82
int nchan
Definition: fant.c:104
unsigned short rle_map
Definition: rle.h:57
void filemap ( )
void filemap ( int  tflag,
char *  mapfname,
int  nchan,
int  length,
rle_map **  amap 
)

Definition at line 606 of file rleldmap.c.

610 {
611  FILE * mapfile;
612  register int c, i;
613  int ent;
614 
615  mapfile = rle_open_f( "rleldmap", mapfname, "r" );
616 
617  if ( tflag == 1 ) /* channel-major order */
618  for ( c = 0; c < nchan; c++ )
619  for ( i = 0; i < length; i++ )
620  switch ( fscanf( mapfile, "%d", &ent ) )
621  {
622  case EOF: /* EOF */
623  fprintf( stderr,
624  "rleldmap: Premature end of file reading map %s at channel %d, entry %d\n",
625  mapfname, c, i );
626  exit(-1);
627  /* NOTREACHED */
628  case 1: /* Got it */
629  amap[c][i] = ent;
630  break;
631  case 0: /* no conversion? */
632  fprintf( stderr,
633  "rleldmap: Bad data in map %s at channel %d, entry %d\n",
634  mapfname, c, i );
635  exit(-1);
636  /* NOTREACHED */
637  default: /* error */
638  fprintf( stderr,
639  "rleldmap: Error reading map %s at channel %d, entry %d\n",
640  mapfname, c, i );
641  exit(-1);
642  /* NOTREACHED */
643  }
644  else /* Entry-major order */
645  for ( i = 0; i < length; i++ )
646  for ( c = 0; c < nchan; c++ )
647  switch ( fscanf( mapfile, "%d", &ent ) )
648  {
649  case EOF: /* EOF */
650  fprintf( stderr,
651  "rleldmap: Premature end of file reading map %s at entry %d, channel %d\n",
652  mapfname, i, c );
653  exit(-1);
654  /* NOTREACHED */
655  case 1: /* Got it */
656  amap[c][i] = ent;
657  break;
658  case 0: /* no conversion? */
659  fprintf( stderr,
660  "rleldmap: Bad data in map %s at entry %d, channel %d\n",
661  mapfname, i, c );
662  exit(-1);
663  /* NOTREACHED */
664  default: /* error */
665  fprintf( stderr,
666  "rleldmap: Error reading map %s at entry %d, channel %d: ",
667  mapfname, i, c );
668  perror("");
669  exit(-1);
670  /* NOTREACHED */
671  }
672  if ( mapfile != stdin )
673  fclose( mapfile );
674 }
int i
Definition: rletorla.c:82
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
int nchan
Definition: fant.c:104
void gammap ( )
void gammap ( double  gamma,
int  nchan,
int  length,
int  range,
rle_map **  amap 
)

Definition at line 569 of file rleldmap.c.

573 {
574  register int i;
575  double l = length - 1;
576 
577  gamma = 1.0 / gamma;
578  for ( i = 0; i < length; i++ )
579  amap[0][i] = (rle_map)(0.5 + range * pow( (double)i / l,
580  gamma ));
581 
582  for ( i = 1; i < nchan; i++ )
583  bcopy( (char *)amap[0], (char *)amap[i],
584  length * sizeof(rle_map) );
585 }
int gamma(int x, float gamma_value)
Definition: gamma.c:24
int i
Definition: rletorla.c:82
int nchan
Definition: fant.c:104
unsigned short rle_map
Definition: rle.h:57
void get_rle_map ( )
void get_rle_map ( rle_hdr the_hdr,
char *  fname 
)

Definition at line 751 of file rleldmap.c.

754 {
755  FILE * infile;
756 
757  infile = rle_open_f( "rleldmap", fname, "r" );
758 
759  the_hdr->rle_file = infile;
760  if ( rle_get_setup( the_hdr ) < 0 )
761  {
762  fprintf( stderr,
763  "rleldmap: Can't read setup information from %s\n", fname );
764  exit(-1);
765  }
766 
767  if ( the_hdr->ncmap == 0 )
768  {
769  fprintf( stderr, "rleldmap: No color map in %s\n", fname );
770  exit(-1);
771  }
772 
773  if ( infile != stdin )
774  fclose( infile );
775 }
char * fname[3]
Definition: show3.c:31
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
FILE * infile
Definition: targatorle.c:102
int ncmap
Definition: rle.h:100
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
FILE * rle_file
Definition: rle.h:114
void linmap ( )
void linmap ( double  factor,
int  nchan,
int  length,
int  range,
rle_map **  amap 
)

Definition at line 524 of file rleldmap.c.

528 {
529  register int i;
530  double l = length - 1, m;
531 
532  for ( i = 0; i < length; i++ )
533  {
534  m = range * ((double)i / l) * factor;
535  if ( factor < 0 )
536  {
537  m = range + m;
538  if ( m < 0 )
539  m = 0;
540  }
541  else
542  if ( m > range )
543  m = range;
544  amap[0][i] = (rle_map)(0.5 + m);
545  }
546 
547  for ( i = 1; i < nchan; i++ )
548  bcopy( (char *)amap[0], (char *)amap[i],
549  length * sizeof(rle_map) );
550 }
int i
Definition: rletorla.c:82
int nchan
Definition: fant.c:104
unsigned short rle_map
Definition: rle.h:57
void main ( int  argc,
char **  argv 
)

Definition at line 139 of file rleldmap.c.

142 {
143  int apply = 0, nflag = 0, nchan = 3, length = 256, range, lbits,
144  sflag = 0, bits = 8, lflag = 0, gflag = 0,
145  tflag = 0, mflag = 0, rflag = 0, oflag = 0, nmfiles = 0;
146  int ichan, ilength, ilbits; /* Input map parameters */
147  int ochan, olength, olbits; /* output map parameters */
148  double factor = 1.0, gamma = 1.0;
149  char * mapfname = NULL, ** mfnames = NULL, * rlefname = NULL,
150  * outputfname = NULL, * inputfname = NULL;
151  char map_comment[30];
152  FILE *outfile = stdout;
153  rle_map ** imap = NULL, ** omap, ** amap, ** allocmap();
154  rle_hdr in_hdr, out_hdr, rle_f_hdr;
155  int rle_cnt, rle_err;
156 
157  in_hdr = *rle_hdr_init( NULL );
158  out_hdr = *rle_hdr_init( NULL );
159  rle_f_hdr = *rle_hdr_init( NULL );
160 
161  if ( scanargs( argc, argv,
162  "% ab%- n%-nchan!dlength!d s%-bits!d l%-factor%F g%-gamma!F \n\
163 \ttf%-file!s m%-files!*s r%-rlefile!s o%-outputfile!s inputfile%s",
164  &apply, &nflag, &nchan, &length, &sflag, &bits,
165  &lflag, &factor, &gflag, &gamma,
166  &tflag, &mapfname, &mflag, &nmfiles, &mfnames,
167  &rflag, &rlefname, &oflag, &outputfname,
168  &inputfname ) == 0 )
169  exit( 1 ); /* bad arguments */
170 
171  /* Check for exclusive flag use */
172  if ( (lflag != 0) + (gflag != 0) + (tflag != 0) + (mflag != 0) +
173  (rflag != 0) != 1 )
174  {
175  fprintf(stderr,
176  "%s: Must specify exactly one of -l -g -t -f -m -r\n",
177  cmd_name( argv ) );
178  exit(-1);
179  }
180 
181  /* Compute color map parameters */
182  if ( rflag ) /* from RLE file? */
183  {
184  if ( lflag )
185  fprintf(stderr,
186  "%s: Nchan, length and bits ignored, values from rle file used\n",
187  cmd_name( argv ));
188  rle_names( &rle_f_hdr, cmd_name( argv ), rlefname, 0 );
189  get_rle_map( &rle_f_hdr, rlefname );
190  lbits = rle_f_hdr.cmaplen;
191  length = 1 << lbits;
192  nchan = rle_f_hdr.ncmap;
193  bits = 16;
194  /* Just use the rle map */
195  amap = allocmap( length, nchan, rle_f_hdr.cmap );
196  }
197  else
198  {
199  for ( lbits = 0; length > 1<<lbits; lbits++ )
200  ; /* Get log2 of length */
201  if ( length != 1 << lbits )
202  {
203  fprintf( stderr,
204  "%s: Length (%d) rounded up to power of 2 (%d)\n",
205  cmd_name( argv ), length, 1 << lbits );
206  length = 1 << lbits; /* round length to power of 2 */
207  }
208  /* Allocate space for the applied map */
209  amap = allocmap( nchan, length, NULL );
210  }
211  range = (1 << bits) - 1;
212 
213  /* Compute the requested map */
214  if ( lflag )
215  linmap( factor, nchan, length, range, amap );
216  if ( gflag )
217  gammap( gamma, nchan, length, range, amap );
218  if ( tflag )
219  filemap( tflag, mapfname, nchan, length, amap );
220  if ( mflag )
221  mfilemap( mfnames, nchan, length, amap );
222 
223  /* Open input file and verify header */
224  in_hdr.rle_file = rle_open_f(cmd_name( argv ), inputfname, "r");
225  rle_names( &in_hdr, cmd_name( argv ), inputfname, 0 );
226  rle_names( &out_hdr, in_hdr.cmd, outputfname, 0 );
227 
228  for ( rle_cnt = 0;
229  (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS ||
230  rle_err == RLE_EMPTY;
231  rle_cnt++ )
232  {
233  if ( rle_err == RLE_EMPTY )
234  {
235  if ( rle_cnt == 0 )
236  apply = 0; /* can't apply to non-existant map */
237  else
238  break;
239  /* Build in_hdr for non-existant image (all zeros works fine) */
240  bzero( &in_hdr, sizeof in_hdr );
241  }
242 
243  /* If apply flag was given, check for compatibility of color maps */
244  if ( in_hdr.ncmap == 0 )
245  apply = 0; /* Can't apply to non-existent map */
246  if ( apply )
247  {
248  char *c;
249 
250  ilbits = in_hdr.cmaplen;
251  ilength = (1 << ilbits);
252  if ( (c = rle_getcom( "color_map_length", &in_hdr )) &&
253  atoi(c) > 0 && atoi(c) < ilength )
254  ilength = atoi(c);
255  ichan = in_hdr.ncmap;
256  if ( ! ( nchan == 1 || ichan == 1 ||
257  nchan == ichan ) )
258  {
259  fprintf( stderr,
260  "%s: Nchan (%d) and input color map (%d channels) are not compatible\n",
261  cmd_name( argv ), nchan, ichan );
262  exit(-1);
263  }
264  ochan = (nchan > ichan) ? nchan : ichan;
265 
266  if ( apply == 1 ) /* "before", omap[i] = imap[amap[i]] */
267  {
268  olength = length;
269  olbits = lbits;
270  }
271  else /* "after", omap[i] = amap[imap[i]] */
272  {
273  olength = ilength;
274  olbits = ilbits;
275  }
276 
277  /* Get convenient access to the input map */
278  imap = allocmap( in_hdr.ncmap, 1 << in_hdr.cmaplen,
279  in_hdr.cmap );
280 
281  /* Allocate an output map */
282  omap = allocmap( ochan, olength, NULL );
283 
284  /* And do the application */
285  if ( apply == 1 )
286  applymap( imap, in_hdr.ncmap, 1 << in_hdr.cmaplen, 16,
287  amap, nchan, length, bits,
288  omap );
289  else
290  applymap( amap, nchan, length, bits,
291  imap, in_hdr.ncmap, ilength, 16,
292  omap );
293  }
294  else
295  {
296  /* "Copy" the applied map, and left justify it */
297  omap = allocmap( nchan, length, NULL );
298  bcopy( amap[0], omap[0], nchan * length * sizeof(rle_map) );
299  olength = length;
300  olbits = lbits;
301  ochan = nchan;
302  shiftmap( omap, ochan, olength, bits );
303  }
304 
305  /* Now, open the output */
306  /* start by copying input parameters */
307  (void)rle_hdr_cp( &in_hdr, &out_hdr );
308  out_hdr.ncmap = ochan;
309  out_hdr.cmaplen = olbits;
310  out_hdr.cmap = omap[0];
311  if ( olength != 1 << olbits )
312  {
313  sprintf( map_comment, "color_map_length=%d", olength );
314  rle_putcom( map_comment, &out_hdr );
315  }
316  else
317  rle_delcom( "color_map_length", &out_hdr );
318  if ( rle_cnt == 0 )
319  outfile = rle_open_f(cmd_name( argv ), outputfname, "w");
320  out_hdr.rle_file = outfile;
321 
322  rle_addhist( argv, &in_hdr, &out_hdr );
323 
324  rle_put_setup( &out_hdr );
325 
326  /* Copy the rest of the input to the output */
327  if ( rle_err != RLE_EMPTY )
328  rle_cp( &in_hdr, &out_hdr );
329 
330  /* Free temp storage. */
331  free( omap[0] );
332  free( omap );
333  out_hdr.cmap = 0; /* Don't try to free it again? */
334  if ( apply )
335  {
336  free( imap );
337  }
338  if ( rle_err == RLE_EMPTY )
339  {
340  rle_err = RLE_SUCCESS;
341  break;
342  }
343  }
344 
345  /* Check for an error. EOF or EMPTY is ok if at least one image
346  * has been read. Otherwise, print an error message.
347  */
348  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
349  rle_get_error( rle_err, cmd_name( argv ), inputfname );
350 
351  exit( 0 );
352 }
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
static rle_hdr in_hdr
Definition: rletogif.c:37
FILE * outfile
Definition: giftorle.c:61
char * rle_getcom(const char *name, rle_hdr *the_hdr)
const char * rle_delcom(const char *name, rle_hdr *the_hdr)
#define RLE_EMPTY
Definition: rle.h:73
const char * rle_putcom(const char *value, rle_hdr *the_hdr)
void rle_cp(rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_cp.c:69
rle_map * cmap
Definition: rle.h:112
void get_rle_map()
void mfilemap()
void rle_addhist(char *argv[], rle_hdr *in_hdr, rle_hdr *out_hdr)
#define RLE_SUCCESS
Definition: rle.h:70
void gammap()
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 shiftmap()
int rle_get_error(int code, const char *pgmname, const char *fname)
#define RLE_EOF
Definition: rle.h:74
int gamma(int x, float gamma_value)
Definition: gamma.c:24
int ncmap
Definition: rle.h:100
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
int cmaplen
Definition: rle.h:100
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
int nchan
Definition: fant.c:104
rle_map ** allocmap(int nchan, int length, rle_map *cmap)
Definition: rleldmap.c:373
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
unsigned short rle_map
Definition: rle.h:57
void applymap()
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void filemap()
int oflag
Definition: painttorle.c:45
FILE * rle_file
Definition: rle.h:114
void linmap()
rle_hdr out_hdr
Definition: dvirle2.c:89
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
void mfilemap ( )
void mfilemap ( char **  mfnames,
int  nchan,
int  length,
rle_map **  amap 
)

Definition at line 693 of file rleldmap.c.

697 {
698  FILE * mapfile;
699  register int c, i;
700  int ent;
701 
702  for ( c = 0; c < nchan; c++, mfnames++ )
703  {
704  mapfile = rle_open_f( "rleldmap", *mfnames, "r" );
705 
706  for ( i = 0; i < length; i++ )
707  switch ( fscanf( mapfile, "%d", &ent ) )
708  {
709  case EOF: /* EOF */
710  fprintf( stderr,
711  "rleldmap: Premature end of file reading map %s at channel %d, entry %d\n",
712  *mfnames, c, i );
713  exit(-1);
714  /* NOTREACHED */
715  case 1: /* Got it */
716  amap[c][i] = ent;
717  break;
718  case 0: /* no conversion? */
719  fprintf( stderr,
720  "rleldmap: Bad data in map %s at channel %d, entry %d\n",
721  *mfnames, c, i );
722  exit(-1);
723  /* NOTREACHED */
724  default: /* error */
725  fprintf( stderr,
726  "rleldmap: Error reading map %s at channel %d, entry %d: ",
727  *mfnames, c, i );
728  perror("");
729  exit(-1);
730  /* NOTREACHED */
731  }
732  if ( mapfile != stdin )
733  fclose( mapfile );
734  }
735 }
int i
Definition: rletorla.c:82
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
int nchan
Definition: fant.c:104
void shiftmap ( )
void shiftmap ( rle_map **  map,
int  nchan,
int  length,
int  bits 
)

Definition at line 413 of file rleldmap.c.

416 {
417  register rle_map * e;
418  register int i;
419 
420  bits = 16 - bits;
421  if ( bits == 0 )
422  return; /* no work! */
423 
424  for ( i = nchan * length, e = map[0]; i > 0; i--, e++ )
425  *e <<= bits;
426 }
short map[3][256]
Definition: getap.c:115
int i
Definition: rletorla.c:82
int nchan
Definition: fant.c:104
unsigned short rle_map
Definition: rle.h:57

Variable Documentation

char rcsid[] = "$Header: /tmp_mnt/n/itn/hendrix/u/spencer/RCS/rleldmap.c,v 3.0.1.5 1992/02/28 22:12:27 spencer Exp spencer $"
static

Definition at line 28 of file rleldmap.c.