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

Go to the source code of this file.

Functions

static void open_output (char *outfname)
 
static void close_output ()
 
static void begin_direct_page (rle_hdr *in_hdr)
 
static void handle_color_table (rle_hdr *in_hdr, rle_pixel **cmap)
 
static void begin_indexed_page (rle_hdr *in_hdr)
 
static void end_page ()
 
static void center_image (rle_hdr *in_hdr, float *px, float *py, float *qx, float *qy, float *rx, float *ry)
 
static void process_indexed_page (rle_hdr *in_hdr)
 
static void process_direct_page (rle_hdr *in_hdr)
 
 main (int argc, char **argv)
 

Variables

static char rcs_id [] = "$Header: /l/spencer/src/urt/cnv/RCS/rletocgm.c,v 3.0.1.1 1992/04/30 13:59:08 spencer Exp $"
 
static int verboseflag = 0
 

Function Documentation

static void begin_direct_page ( rle_hdr in_hdr)
static

Definition at line 95 of file rletocgm.c.

98 {
99  int ierr= 0, one= 1;
100  float red, green, blue;
101 
102  if (in_hdr->background) {
103  red= ((float)in_hdr->bg_color[0])/255.0;
104  green= ((float)in_hdr->bg_color[1])/255.0;
105  blue= ((float)in_hdr->bg_color[2])/255.0;
106  }
107  else {
108  red= 0.0;
109  green= 0.0;
110  blue= 0.0;
111  }
112 
113  wrbegp(&ierr);
114  if (ierr) {
115  fprintf(stderr,"rletocgm: Error writing begin page!\n");
116  exit(2);
117  }
118  wrtcsm(&one,&ierr);
119  if (ierr) {
120  fprintf(stderr,"rletocgm: Error writing color selection mode!\n");
121  exit(2);
122  }
123  wrbgdc(&red, &green, &blue, &ierr);
124  if (ierr) {
125  fprintf(stderr,"rletocgm: Error writing background color!\n");
126  exit(2);
127  }
128  wrbgpb(&ierr);
129  if (ierr) {
130  fprintf(stderr,"rletocgm: Error writing begin picture body!\n");
131  exit(2);
132  }
133 }
static unsigned char blue[256]
Definition: rastorle.c:71
int * bg_color
Definition: rle.h:100
static unsigned char green[256]
Definition: rastorle.c:71
int background
Definition: rle.h:100
static unsigned char red[256]
Definition: rastorle.c:71
static void begin_indexed_page ( rle_hdr in_hdr)
static

Definition at line 195 of file rletocgm.c.

198 {
199  int ierr= 0;
200  rle_pixel **cmap;
201  float red, green, blue;
202 
203  /* Load the color table, and write background color information */
204  cmap= buildmap( in_hdr, 3, 0.0, 1.0 );
205  if (in_hdr->background) {
206  red= (float)cmap[0][in_hdr->bg_color[0]]/255.0;
207  green= (float)cmap[1][in_hdr->bg_color[0]]/255.0;
208  blue= (float)cmap[2][in_hdr->bg_color[0]]/255.0;
209  }
210  else {
211  red= 0.0;
212  green= 0.0;
213  blue= 0.0;
214  }
215 
216  /* Preliminary CGM stuff */
217  wrbegp(&ierr);
218  if (ierr) {
219  fprintf(stderr,"rletocgm: Error writing begin page!\n");
220  exit(2);
221  }
222  wrbgdc(&red, &green, &blue, &ierr);
223  if (ierr) {
224  fprintf(stderr,"rletocgm: Error writing background color!\n");
225  exit(2);
226  }
227  wrbgpb(&ierr);
228  if (ierr) {
229  fprintf(stderr,"rletocgm: Error writing begin picture body!\n");
230  exit(2);
231  }
232 
233  /* Produce a color table */
234  handle_color_table(in_hdr, cmap);
235 
236  /* Clean up */
237  free( (char *)cmap[0] );
238 }
static unsigned char blue[256]
Definition: rastorle.c:71
int * bg_color
Definition: rle.h:100
static unsigned char green[256]
Definition: rastorle.c:71
static void handle_color_table(rle_hdr *in_hdr, rle_pixel **cmap)
Definition: rletocgm.c:135
int background
Definition: rle.h:100
rle_pixel ** buildmap(rle_hdr *the_hdr, int minmap, double orig_gamma, double new_gamma)
Definition: buildmap.c:56
unsigned char rle_pixel
Definition: rle.h:56
rle_pixel ** cmap
Definition: get4d.c:47
static unsigned char red[256]
Definition: rastorle.c:71
static void center_image ( rle_hdr in_hdr,
float *  px,
float *  py,
float *  qx,
float *  qy,
float *  rx,
float *  ry 
)
static

Definition at line 252 of file rletocgm.c.

256 {
257  float xrange, yrange;
258 
259  xrange= (float)(in_hdr->xmax - in_hdr->xmin + 1);
260  yrange= (float)(in_hdr->ymax - in_hdr->ymin + 1);
261  if (xrange>=yrange) {
262  *px= 0.0;
263  *qx= 1.0;
264  *rx= *qx;
265  *py= 0.5 * (1.0 - yrange/xrange);
266  *qy= 1.0 - *py;
267  *ry= *py;
268  }
269  else {
270  *py= 0.0;
271  *qy= 1.0;
272  *ry= *py;
273  *px= 0.5 * (1.0 - xrange/yrange);
274  *qx= 1.0 - *px;
275  *rx= *qy;
276  }
277 }
int xmin
Definition: rle.h:100
int ymin
Definition: rle.h:100
int xmax
Definition: rle.h:100
int ymax
Definition: rle.h:100
static void close_output ( )
static

Definition at line 83 of file rletocgm.c.

85 {
86  int ierr= 0;
87 
88  wrtend(&ierr);
89  if (ierr) {
90  fprintf(stderr,"rletocgm: Error closing output!\n");
91  exit(2);
92  }
93 }
static void end_page ( )
static

Definition at line 240 of file rletocgm.c.

242 {
243  int ierr= 0;
244 
245  wrendp(&ierr);
246  if (ierr) {
247  fprintf(stderr,"rletocgm: Error ending output page!\n");
248  exit(2);
249  }
250 }
static void handle_color_table ( rle_hdr in_hdr,
rle_pixel **  cmap 
)
static

Definition at line 135 of file rletocgm.c.

141 {
142  int tablesize, i, zero= 0, ierr= 0;
143  float *rarray, *garray, *barray;
144  char *comstring;
145 
146  /* Calculate table size, then reset it if there is a comment to that
147  * effect in the RLE file.
148  */
149  if (in_hdr->cmaplen)
150  tablesize= 2<<(in_hdr->cmaplen-1);
151  else tablesize= 2;
152  comstring= rle_getcom( "color_map_length", in_hdr );
153  if (!comstring) comstring= rle_getcom( "colormap_length", in_hdr );
154  if (comstring) sscanf( comstring, "%d", &tablesize );
155 
156  if ( !(rarray= (float *)malloc( tablesize*sizeof(float) )) ) {
157  fprintf(stderr,
158  "rletocgm: unable to allocate %d floats for color table reds!\n",
159  tablesize);
160  exit(2);
161  }
162  if ( !(garray= (float *)malloc( tablesize*sizeof(float) )) ) {
163  fprintf(stderr,
164  "rletocgm: unable to allocate %d floats for color table greens!\n",
165  tablesize);
166  exit(2);
167  }
168  if ( !(barray= (float *)malloc( tablesize*sizeof(float) )) ) {
169  fprintf(stderr,
170  "rletocgm: unable to allocate %d floats for color table blues!\n",
171  tablesize);
172  exit(2);
173  }
174 
175  /* Transcribe the color map into the float arrays */
176  for (i=0; i<tablesize; i++) {
177  rarray[i]= (float)cmap[0][i]/255.0;
178  garray[i]= (float)cmap[1][i]/255.0;
179  barray[i]= (float)cmap[2][i]/255.0;
180  }
181 
182  /* Actually write the map */
183  wrctbl(rarray, garray, barray, &zero, &tablesize, &ierr);
184  if (ierr) {
185  fprintf(stderr,"rletocgm: Error writing color map!\n");
186  exit(2);
187  }
188 
189  /* Clean up */
190  free( (char *)rarray );
191  free( (char *)garray );
192  free( (char *)barray );
193 }
char * rle_getcom(const char *name, rle_hdr *the_hdr)
void * malloc()
int cmaplen
Definition: rle.h:100
int i
Definition: rletorla.c:82
rle_pixel ** cmap
Definition: get4d.c:47
main ( int  argc,
char **  argv 
)

Definition at line 452 of file rletocgm.c.

455 {
456  char *infname = NULL,
457  *outfname = NULL;
458  int rle_cnt, rle_err;
459  int oflag = 0, debugflag= 0;
460  FILE *outfile;
461  rle_hdr in_hdr; /* Headers for input file. */
462 
463  in_hdr = *rle_init_hdr( (rle_hdr *)NULL );
464  if ( scanargs( argc, argv, "% o%-outfile!s d%- v%- infile%s",
465  &oflag, &outfname, &debugflag, &verboseflag, &infname ) == 0 )
466  exit( 1 );
467 
468  /* Turn on debugging if requested. */
469  if (debugflag) {
470  int ierr= 0;
471  rle_debug(1);
472  tgldbg(&ierr);
473  if (ierr) fprintf(stderr,"rletocgm: couldn't set debugging!\n");
474  }
475 
476  /* Open the input file.
477  * The output file won't be opened until the first image header
478  * has been read. This avoids unnecessarily wiping out a
479  * pre-existing file if the input is garbage.
480  */
481  rle_names( &in_hdr, cmd_name( argv ), infname, 0 );
482  in_hdr.rle_file = rle_open_f( in_hdr.cmd, infname, "r" );
483 
484  /* Read images from the input file until the end of file is
485  * encountered or an error occurs.
486  */
487  rle_cnt = 0;
488  while ( (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS )
489  {
490 
491  /* Open the CGM output file */
492  if (rle_cnt==0) open_output(oflag ? outfname : "-");
493 
494  /* Count the input images. */
495  rle_cnt++;
496 
497  /* Be verbose if requested */
498  if (verboseflag) fprintf(stderr,"image %d: ",rle_cnt);
499 
500  /* Based on the number of colors in the header, select either indexed
501  * or direct color processing for the images in the file.
502  */
503  switch (in_hdr.ncolors) {
504  case 1: process_indexed_page(&in_hdr); break;
505  case 3: process_direct_page(&in_hdr); break;
506  default: fprintf(stderr,
507  "rletocgm: %d is an invalid number of color planes!\n",
508  in_hdr.ncolors);
509  }
510  }
511 
512  /* Close the CGM output file */
513  close_output();
514 
515  /* Check for an error. EOF or EMPTY is ok if at least one image
516  * has been read. Otherwise, print an error message.
517  */
518  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
519  rle_get_error( rle_err, in_hdr.cmd, infname );
520 
521  exit( 0 );
522 }
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
#define RLE_EMPTY
Definition: rle.h:73
static void process_direct_page(rle_hdr *in_hdr)
Definition: rletocgm.c:346
#define RLE_SUCCESS
Definition: rle.h:70
static void process_indexed_page(rle_hdr *in_hdr)
Definition: rletocgm.c:279
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
static void open_output(char *outfname)
Definition: rletocgm.c:65
Definition: rle.h:96
int rle_get_error(int code, const char *pgmname, const char *fname)
#define RLE_EOF
Definition: rle.h:74
void rle_debug(int on_off)
Definition: rle_getrow.c:293
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
static void close_output()
Definition: rletocgm.c:83
char * cmd_name(char **argv)
Definition: cmd_name.c:31
int oflag
Definition: painttorle.c:45
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100
static int verboseflag
Definition: rletocgm.c:63
static void open_output ( char *  outfname)
static

Definition at line 65 of file rletocgm.c.

68 {
69  int ierr= 0, i256= 256;
70 
71  wrcopn(outfname,&ierr);
72  if (ierr) {
73  fprintf(stderr,"rletocgm: Error opening file <%s> for output!\n",outfname);
74  exit(2);
75  }
76  wrmxci(&i256,&ierr);
77  if (ierr) {
78  fprintf(stderr,"rletocgm: Error writing to file!\n");
79  exit(2);
80  }
81 }
static void process_direct_page ( rle_hdr in_hdr)
static

Definition at line 346 of file rletocgm.c.

349 {
350  int x, y;
351  rle_pixel **rows; /* Will be used for scanline storage. */
352  static float *rarray= (float *)0; /* red CGM image */
353  static float *garray= (float *)0; /* green CGM image */
354  static float *barray= (float *)0; /* blue CGM image */
355  float *rptr, *gptr, *bptr;
356  static int nx, ny; /* dimensions of image */
357  float px, py, qx, qy, rx, ry; /* CGM array boundaries */
358  int ierr= 0;
359 
360  /* Be verbose if requested */
361  if (verboseflag) fprintf(stderr,"Found direct color (3 channel) page\n");
362 
363  /* Begin an output page */
364  begin_direct_page(in_hdr);
365 
366  /* Calculate array boundaries */
367  center_image(in_hdr, &px, &py, &qx, &qy, &rx, &ry);
368 
369  /* Allocate the CGM image array, if it's not already there */
370  if (!rarray ||
371  !(nx == in_hdr->xmax - in_hdr->xmin + 1) ||
372  !(ny == in_hdr->ymax - in_hdr->ymin + 1)) {
373  if (rarray) free( (char *)rarray );
374  if (garray) free( (char *)garray );
375  if (barray) free( (char *)barray );
376  nx= in_hdr->xmax - in_hdr->xmin + 1;
377  ny= in_hdr->ymax - in_hdr->ymin + 1;
378  if ( !(rarray= (float *)malloc( nx*ny*sizeof(float) )) ) {
379  fprintf(stderr,"rletocgm: cannot allocate %d floats for red!\n",nx*ny);
380  exit(2);
381  }
382  if ( !(garray= (float *)malloc( nx*ny*sizeof(float) )) ) {
383  fprintf(stderr,"rletocgm: cannot allocate %d floats for green!\n",nx*ny);
384  exit(2);
385  }
386  if ( !(barray= (float *)malloc( nx*ny*sizeof(float) )) ) {
387  fprintf(stderr,"rletocgm: cannot allocate %d floats for blue!\n",nx*ny);
388  exit(2);
389  }
390  }
391 
392  /* Allocate memory into which the image scanlines can be read. */
393  if ( rle_row_alloc( in_hdr, &rows ) < 0 ) {
394  fprintf( stderr, "rletocgm: Unable to allocate image memory.\n" );
395  exit( RLE_NO_SPACE );
396  }
397 
398  /* Read the input image and copy it to the output file. */
399  rptr= rarray;
400  gptr= garray;
401  bptr= barray;
402  for ( y = in_hdr->ymin; y <= in_hdr->ymax; y++ )
403  {
404  /* Read a scanline. */
405  rle_getrow( in_hdr, rows );
406 
407  for (x=0; x<nx; x++) {
408  *rptr++= (float)rows[0][x]/255.0;
409  *gptr++= (float)rows[1][x]/255.0;
410  *bptr++= (float)rows[2][x]/255.0;
411  }
412  }
413 
414  /* Write the CGM cell array */
415  wcladc( rarray, garray, barray,
416  &nx, &ny, &px, &py, &qx, &qy, &rx, &ry, &ierr );
417  if (ierr) {
418  fprintf(stderr,"rletocgm: Error writing direct color cell array!\n");
419  exit(2);
420  }
421 
422  /* Protect from bad input. */
423  while ( rle_getskip( in_hdr ) != 32768 );
424 
425  /* Free memory. */
426  rle_row_free( in_hdr, rows );
427 
428  /* End the page */
429  end_page();
430 }
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
int xmin
Definition: rle.h:100
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
int ymin
Definition: rle.h:100
static int y
Definition: getami.c:691
int xmax
Definition: rle.h:100
static int x
Definition: getami.c:691
#define RLE_NO_SPACE
Definition: rle.h:72
static void begin_direct_page(rle_hdr *in_hdr)
Definition: rletocgm.c:95
unsigned int rle_getskip(rle_hdr *the_hdr)
Definition: rle_getskip.c:57
static void center_image(rle_hdr *in_hdr, float *px, float *py, float *qx, float *qy, float *rx, float *ry)
Definition: rletocgm.c:252
rle_pixel ** rows
Definition: rletopaint.c:57
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
void * malloc()
static void end_page()
Definition: rletocgm.c:240
int rle_getrow(rle_hdr *the_hdr, rle_pixel *scanline[])
static int verboseflag
Definition: rletocgm.c:63
static void process_indexed_page ( rle_hdr in_hdr)
static

Definition at line 279 of file rletocgm.c.

282 {
283  int x, y;
284  rle_pixel **rows; /* Will be used for scanline storage. */
285  static int *iarray= (int *)0; /* Stores the CGM integer image */
286  int *iptr;
287  static int nx, ny; /* dimensions of iarray */
288  float px, py, qx, qy, rx, ry; /* CGM array boundaries */
289  int ierr= 0;
290 
291  /* Be verbose if requested */
292  if (verboseflag) fprintf(stderr,"Found indexed color (1 channel) page\n");
293 
294  /* Begin an output page, including the color table */
295  begin_indexed_page(in_hdr);
296 
297  /* Calculate array boundaries */
298  center_image(in_hdr, &px, &py, &qx, &qy, &rx, &ry);
299 
300  /* Allocate the CGM image array, if it's not already there */
301  if (!iarray ||
302  !(nx == in_hdr->xmax - in_hdr->xmin + 1) ||
303  !(ny == in_hdr->ymax - in_hdr->ymin + 1)) {
304  if (iarray) free( (char *)iarray );
305  nx= in_hdr->xmax - in_hdr->xmin + 1;
306  ny= in_hdr->ymax - in_hdr->ymin + 1;
307  if ( !(iarray= (int *)malloc( nx*ny*sizeof(int) )) ) {
308  fprintf(stderr,"rletocgm: cannot allocate %d ints!\n",nx*ny);
309  exit(2);
310  }
311  }
312 
313  /* Allocate memory into which the image scanlines can be read. */
314  if ( rle_row_alloc( in_hdr, &rows ) < 0 ) {
315  fprintf( stderr, "rletocgm: Unable to allocate image memory.\n" );
316  exit( RLE_NO_SPACE );
317  }
318 
319  /* Read the input image and copy it to the output file. */
320  iptr= iarray;
321  for ( y = in_hdr->ymin; y <= in_hdr->ymax; y++ )
322  {
323  /* Read a scanline. */
324  rle_getrow( in_hdr, rows );
325 
326  for (x=0; x<nx; x++) *iptr++= rows[0][x];
327  }
328 
329  /* Write the CGM cell array */
330  wrtcla( iarray, &nx, &ny, &px, &py, &qx, &qy, &rx, &ry, &ierr );
331  if (ierr) {
332  fprintf(stderr,"rletocgm: Error writing indexed cell array!\n");
333  exit(2);
334  }
335 
336  /* Protect from bad input. */
337  while ( rle_getskip( in_hdr ) != 32768 );
338 
339  /* Free memory. */
340  rle_row_free( in_hdr, rows );
341 
342  /* End the page */
343  end_page();
344 }
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
int xmin
Definition: rle.h:100
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
int ymin
Definition: rle.h:100
static int y
Definition: getami.c:691
int xmax
Definition: rle.h:100
static int x
Definition: getami.c:691
#define RLE_NO_SPACE
Definition: rle.h:72
unsigned int rle_getskip(rle_hdr *the_hdr)
Definition: rle_getskip.c:57
static void center_image(rle_hdr *in_hdr, float *px, float *py, float *qx, float *qy, float *rx, float *ry)
Definition: rletocgm.c:252
static void begin_indexed_page(rle_hdr *in_hdr)
Definition: rletocgm.c:195
rle_pixel ** rows
Definition: rletopaint.c:57
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
void * malloc()
static void end_page()
Definition: rletocgm.c:240
int rle_getrow(rle_hdr *the_hdr, rle_pixel *scanline[])
static int verboseflag
Definition: rletocgm.c:63

Variable Documentation

char rcs_id[] = "$Header: /l/spencer/src/urt/cnv/RCS/rletocgm.c,v 3.0.1.1 1992/04/30 13:59:08 spencer Exp $"
static

Definition at line 31 of file rletocgm.c.

int verboseflag = 0
static

Definition at line 63 of file rletocgm.c.