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

Go to the source code of this file.

Data Structures

struct  alias_hdr
 
struct  bitmap_hdr
 

Macros

#define byte   unsigned char
 
#define Fread(p, s, n, f)    if ( fread(p, s, n, f) != n ) error(3)
 
#define Fwrite(p, s, n, f)    if ( fwrite(p, s, n, f) != n ) error(4)
 
#define VPRINTF   if (verbose) fprintf
 
#define PSEUDOCOLOR   1
 
#define DIRECTCOLOR   2
 
#define TRUECOLOR   3
 
#define GRAYSCALE   4
 

Typedefs

typedef unsigned char color_map[3]
 

Functions

static void read_rle (FILE *, bitmap_hdr *)
 
static void write_alias (FILE *, bitmap_hdr *)
 
static void error (int)
 
static void rlecmap_to_bitmapcmap (rle_hdr *, bitmap_hdr *)
 
static void code_alias24 (unsigned char *, unsigned char *, unsigned char *, int, FILE *)
 
static char * Malloc (long int)
 
void main (int argc, char **argv)
 

Variables

static char rcs_id [] = "$Header: /l/spencer/src/urt/cnv/RCS/rletoalias.c,v 3.0.1.1 1992/04/30 13:58:47 spencer Exp $"
 
int verbose = 0
 
rle_hdr the_hdr
 

Macro Definition Documentation

#define byte   unsigned char

Definition at line 39 of file rletoalias.c.

#define DIRECTCOLOR   2

Definition at line 44 of file rletoalias.c.

#define Fread (   p,
  s,
  n,
 
)    if ( fread(p, s, n, f) != n ) error(3)

Definition at line 40 of file rletoalias.c.

#define Fwrite (   p,
  s,
  n,
 
)    if ( fwrite(p, s, n, f) != n ) error(4)

Definition at line 41 of file rletoalias.c.

#define GRAYSCALE   4

Definition at line 46 of file rletoalias.c.

#define PSEUDOCOLOR   1

Definition at line 43 of file rletoalias.c.

#define TRUECOLOR   3

Definition at line 45 of file rletoalias.c.

#define VPRINTF   if (verbose) fprintf

Definition at line 42 of file rletoalias.c.

Typedef Documentation

typedef unsigned char color_map[3]

Definition at line 69 of file rletoalias.c.

Function Documentation

static void code_alias24 ( unsigned char *  rbuf,
unsigned char *  gbuf,
unsigned char *  bbuf,
int  xmax,
FILE *  handle 
)
static

Definition at line 371 of file rletoalias.c.

375 {
376  byte r, g, b;
377  unsigned int number= 0;
378  int repeat= 1;
379  static byte buf[5120];
380  byte *bufptr, *end;
381 
382  bufptr= buf;
383  end= rbuf + xmax;
384  r= *rbuf++; g= *gbuf++; b= *bbuf++;
385 
386  while (rbuf < end)
387  /*
388  * While we have the same pattern ( and < 255 ), we continue
389  * skipping bytes ( pixels ).
390  */
391  if (r== *rbuf && g== *gbuf && b== *bbuf && repeat < 255) {
392  repeat++;
393  rbuf++; gbuf++; bbuf++;
394  }else {
395  /*
396  * A different triple or repeat == 255 was found, then we add
397  * this to the output buffer.
398  */
399  *bufptr++ = repeat;
400  *bufptr++ = b;
401  *bufptr++ = g;
402  *bufptr++ = r;
403  number += 4;
404  repeat = 1;
405  r= *rbuf++; g= *gbuf++; b= *bbuf++;
406  }
407 
408  /*
409  * We need add the last triplet.
410  */
411  *bufptr++ = repeat;
412  *bufptr++ = b;
413  *bufptr++ = g;
414  *bufptr++ = r;
415  number += 4;
416  /*
417  * And ... write it !
418  */
419  Fwrite(buf, number, 1, handle);
420 }
static unsigned char g
Definition: getami.c:692
static unsigned char r
Definition: getami.c:692
static unsigned char b
Definition: getami.c:692
static char buf[4096 +1]
Definition: into.c:46
unsigned int byte
Definition: hilbert.c:47
short bbuf[8192]
Definition: iristorle.c:33
short rbuf[8192]
Definition: iristorle.c:31
#define Fwrite(p, s, n, f)
Definition: rletoalias.c:41
short gbuf[8192]
Definition: iristorle.c:32
static void error ( int  code)
static

Definition at line 423 of file rletoalias.c.

425 {
426  fprintf(stderr, "%s: ", the_hdr.cmd);
427  switch (code) {
428  case 0: fprintf(stderr, "Usage: %s [-v] [-o outfile] [infile] \n",
429  the_hdr.cmd);
430  break;
431  case 1: fprintf(stderr, "Cannot open file.\n");
432  break;
433  case 2: fprintf(stderr, "Out of memory.\n");
434  break;
435  case 3: fprintf(stderr, "Error while reading input file\n");
436  break;
437  case 4: fprintf(stderr, "Error while writing output file\n");
438  break;
439  case 5: fprintf(stderr, "Input file is not an Alias pix\n");
440  break;
441  case 6: fprintf(stderr, "Incorrect # of planes or # of colors\n");
442  break;
443  case 99: fprintf(stderr, "Not ready\n");
444  break;
445  default: fprintf(stderr, "Unknow erro code (%d)\n", code);
446  break;
447  }
448  exit(1);
449 }
const char * cmd
Definition: rle.h:133
rle_hdr the_hdr
Definition: rletoalias.c:78
void main ( int  argc,
char **  argv 
)

Definition at line 112 of file rletoalias.c.

115 {
116  char *inname = NULL;
117  char *outname = NULL;
118  FILE *infile;
119  FILE *outfile = stdout;
120  int oflag = 0;
122 
123  the_hdr = *rle_hdr_init( (rle_hdr *)NULL );
124  /*
125  * Get options
126  */
127  if ( !scanargs( argc, argv, "% v%- o%-outfile!s infile%s",
128  &verbose, &oflag, &outname, &inname ))
129  exit( 1 );
130 
131  /*
132  * Open rle file.
133  */
134  rle_names( &the_hdr, cmd_name( argv ), inname, 0 );
135  infile = rle_open_f(the_hdr.cmd, inname, "r");
136 
137  /*
138  * Translate Utah's rle format to our bitmap.
139  */
140  read_rle(infile, &bitmap);
141  fclose(infile); /* we finish with this file */
142 
143  outfile = rle_open_f(cmd_name(argv), outname, "w");
144  /* An output file name ? */
145  rle_names( &the_hdr, cmd_name( argv ), outname, 0 );
146  VPRINTF(stderr, "Writing to file %s\n", the_hdr.file_name);
147  outfile= rle_open_f(the_hdr.cmd, outname, "w");
148 
149  write_alias(outfile, &bitmap);
150  fclose(outfile); /* it is not necesary, but ... */
151 
152  exit(0);
153 }
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
int verbose
Definition: rletoalias.c:74
FILE * outfile
Definition: giftorle.c:61
static void read_rle(FILE *, bitmap_hdr *)
Definition: rletoalias.c:156
#define VPRINTF
Definition: rletoalias.c:42
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
const char * cmd
Definition: rle.h:133
Definition: rle.h:96
FILE * infile
Definition: targatorle.c:102
gpr_ $bitmap_desc_t bitmap
Definition: getap.c:110
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
rle_hdr the_hdr
Definition: rletoalias.c:78
static void write_alias(FILE *, bitmap_hdr *)
Definition: rletoalias.c:325
char * cmd_name(char **argv)
Definition: cmd_name.c:31
const char * file_name
Definition: rle.h:134
int oflag
Definition: painttorle.c:45
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
static char * Malloc ( long int  size)
static

Definition at line 455 of file rletoalias.c.

457 {
458  char *ptr;
459 
460  if ((ptr = (char *) malloc(size)) == NULL)
461  error(2);
462 
463  /*
464  * Usually compilers fill buffers with zeros,
465  * but ...
466  */
467  bzero( ptr, size );
468  return ptr;
469 }
int * ptr
Definition: scanargs.c:57
void * malloc()
gpr_ $offset_t size
Definition: getap.c:96
static void error(int)
Definition: rletoalias.c:423
static void read_rle ( FILE *  handle,
bitmap_hdr image 
)
static

Definition at line 156 of file rletoalias.c.

159 {
160  register int i, j;
161  rle_pixel **row;
162  byte *r, *g = 0, *b = 0;
163  int totalsize;
164  color_map *map = 0;
165  int type = 0;
166  int two_lines;
167  int offset_last;
168 
169  /*
170  * Read the file's configuration.
171  */
172  the_hdr.rle_file = handle;
173  rle_get_setup_ok( &the_hdr, NULL, NULL );
174 
175  /*
176  * Fill our bitmap.
177  */
178  image->xsize = the_hdr.xmax - the_hdr.xmin + 1;
179  image->ysize = the_hdr.ymax - the_hdr.ymin + 1;
180  image->depth = ( the_hdr.ncolors < 3 ? the_hdr.cmaplen : 24 );
181  image->colors = ( 1 << image->depth );
182  totalsize= image->xsize * image->ysize;
183  offset_last = totalsize - image->xsize;
184  two_lines = 2 * image->xsize;
185  VPRINTF(stderr, "Image size: %dx%d\n", image->xsize, image->ysize);
186  VPRINTF(stderr, "Image depth: %d\n", image->depth);
187  VPRINTF(stderr, "%s colormap\n",
188  (the_hdr.ncmap ? "With" : "Without"));
189 
190  /*
191  * Check the coherence and image type.
192  */
193  VPRINTF(stderr, "Image type ");
194  switch ( the_hdr.ncolors ) {
195  case 1: switch ( the_hdr.ncmap ) {
196  case 0: type = GRAYSCALE; /* 8 planes, no cmap */
197  VPRINTF(stderr, "GRAYSCALE\n");
198  break;
199  case 3: type = PSEUDOCOLOR; /* 8 planes, cmap */
200  VPRINTF(stderr, "PSEUDOCOLOR\n");
201  break;
202  default: VPRINTF(stderr, "unkown\n");
203  error(6);
204  break;
205  }
206  break;
207  case 3: switch ( the_hdr.ncmap ) {
208  case 0: type = DIRECTCOLOR; /* 24 planes, no cmap */
209  VPRINTF(stderr, "DIRECTCOLOR\n");
210  break;
211  case 3: type = TRUECOLOR; /* 24 planes, cmap */
212  VPRINTF(stderr, "TRUECOLOR\n");
213  break;
214  default: VPRINTF(stderr, "unkown\n");
215  error(6);
216  break;
217  }
218 
219  break;
220  default: error(6);
221  VPRINTF(stderr, "unkown\n");
222  break;
223  }
224 
225  /*
226  * Allocate some memory.
227  */
228  if (rle_row_alloc(&the_hdr, &row) < 0)
229  error(2);
230  if (image->depth > 8 || type == GRAYSCALE) {
231  /*
232  * 24 planes => we need three components
233  * GRAYSCALE use 8 planes but it's defined like 24 planes
234  */
235  r= image->r = (byte *) Malloc(totalsize);
236  g= image->g = (byte *) Malloc(totalsize);
237  b= image->b = (byte *) Malloc(totalsize);
238  if ( type == TRUECOLOR) {
239  image->colors = 256; /* well, a trap to rlecmap_to_bit... */
241  map = (color_map *) image->cmap; /* will be more easy use it */
242  }
243  }else {
244  /* 8 planes => one component and cmap */
245  r= image->r = (byte *) Malloc(totalsize);
246  /* Convert rle cmap to bitmap cmap */
248  map = (color_map *) image->cmap; /* will be more easy use it */
249  }
250 
251  /*
252  * Read the input image and convert it to a simple bitmap.
253  * RLE writes lines in reverse order, so we point to last
254  * line.
255  */
256  VPRINTF(stderr, "Uncompressing RLE file\n");
257  r += offset_last;
258  g += offset_last;
259  b += offset_last;
260  for (j= 0; j< image->ysize; j++) {
261  rle_getrow(&the_hdr, row);
262  switch ( type ) {
263  case GRAYSCALE :
264  case DIRECTCOLOR: for (i= 0; i< image->xsize; i++) {
265  *r++ = row[0][i];
266  *g++ = row[1][i];
267  *b++ = row[2][i];
268  }
269  break;
270  case PSEUDOCOLOR: for (i= 0; i<image->xsize; i++) {
271  *r++ = row[0][i];
272  }
273  break;
274  case TRUECOLOR : for (i= 0; i< image->xsize; i++) {
275  *r++ = map[row[0][i]][0];
276  *g++ = map[row[1][i]][1];
277  *b++ = map[row[2][i]][i];
278  }
279  break;
280  default : error(6);
281  break;
282  }
283  /*
284  * Pointers to next byte of current line, so we substract
285  * two lines.
286  */
287  r -= two_lines;
288  g -= two_lines;
289  b -= two_lines;
290  }
291 
292  /*
293  * TRUECOLOR has map of colors, but we'll not use it.
294  */
295  if ( type == TRUECOLOR )
296  free(image->cmap);
297 }
static unsigned char g
Definition: getami.c:692
int xmin
Definition: rle.h:100
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
#define GRAYSCALE
Definition: rletoalias.c:46
int type
Definition: getx11.c:92
static unsigned char r
Definition: getami.c:692
unsigned char color_map[3]
Definition: rletoalias.c:69
short map[3][256]
Definition: getap.c:115
#define DIRECTCOLOR
Definition: rletoalias.c:44
int ymin
Definition: rle.h:100
#define VPRINTF
Definition: rletoalias.c:42
unsigned char * r
Definition: aliastorle.c:61
static void rlecmap_to_bitmapcmap(rle_hdr *, bitmap_hdr *)
Definition: rletoalias.c:300
#define PSEUDOCOLOR
Definition: rletoalias.c:43
static unsigned char b
Definition: getami.c:692
int xmax
Definition: rle.h:100
static char * Malloc(long int)
Definition: rletoalias.c:455
unsigned char * g
Definition: aliastorle.c:61
unsigned int byte
Definition: hilbert.c:47
int ncmap
Definition: rle.h:100
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
int cmaplen
Definition: rle.h:100
int i
Definition: rletorla.c:82
rle_hdr the_hdr
Definition: rletoalias.c:78
static void error(int)
Definition: rletoalias.c:423
unsigned char * cmap
Definition: aliastorle.c:62
int colors
Definition: aliastorle.c:60
unsigned char * b
Definition: aliastorle.c:61
#define TRUECOLOR
Definition: rletoalias.c:45
int row
Definition: rle.c:27
void rle_get_setup_ok(rle_hdr *the_hdr, const char *prog_name, const char *file_name)
int rle_getrow(rle_hdr *the_hdr, rle_pixel *scanline[])
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100
static void rlecmap_to_bitmapcmap ( rle_hdr rle,
bitmap_hdr bitmap 
)
static

Definition at line 300 of file rletoalias.c.

303 {
304  register int i;
305  rle_map *rch, *gch, *bch;
306  byte *ptr;
307 
308  /* Allocate memory */
309  ptr= bitmap->cmap= (byte *) Malloc(bitmap->colors * 3);
310 
311  /*
312  * We'll use 3 ptrs, first to R channel, second to G channel, ...
313  */
314  rch = rle->cmap;
315  gch = &(rle->cmap[bitmap->colors]);
316  bch = &(rle->cmap[2 * bitmap->colors]);
317  for (i= 0; i< bitmap->colors; i++) {
318  *ptr++ = (byte) (*rch++ >> 8);
319  *ptr++ = (byte) (*gch++ >> 8);
320  *ptr++ = (byte) (*bch++ >> 8);
321  }
322 }
#define byte
Definition: rletoalias.c:39
rle_map * cmap
Definition: rle.h:112
static char * Malloc(long int)
Definition: rletoalias.c:455
unsigned int byte
Definition: hilbert.c:47
int * ptr
Definition: scanargs.c:57
int i
Definition: rletorla.c:82
unsigned short rle_map
Definition: rle.h:57
unsigned char * cmap
Definition: aliastorle.c:62
int colors
Definition: aliastorle.c:60
static void write_alias ( FILE *  handle,
bitmap_hdr image 
)
static

Definition at line 325 of file rletoalias.c.

328 {
329  register int i;
330  alias_hdr alias;
331  byte *rbuf, *gbuf, *bbuf;
332 
333  /*
334  * Alias 8 bits files are b&w ( no cmap ), so Alias
335  * don't support really a 8 bits file.
336  */
337  if (image->depth <= 8) {
338  fprintf(stderr, "Bitmap with 8 planes\n");
339  error(99);
340  }
341 
342  VPRINTF(stderr, "Writing Alias file\n");
343  /* Fill the Alias header and write it */
344  alias.yinit = 0;
345  alias.xinit = 0;
346  alias.xsize = image->xsize;
347  alias.ysize = image->ysize;
348  alias.depth = image->depth;
349  Fwrite(&alias, sizeof(alias_hdr), 1, handle);
350 
351  /* Set the pointers to buffers */
352  rbuf = image->r;
353  gbuf = image->g;
354  bbuf = image->b;
355 
356  /* Compress each line */
357  for (i= 0; i< image->ysize; i++) {
358  /*
359  * Alias "pix" format do a compression method on
360  * each raster line.
361  */
362  code_alias24(rbuf, gbuf, bbuf, image->xsize, handle);
363  /* Move pointers to next lines */
364  rbuf += image->xsize;
365  gbuf += image->xsize;
366  bbuf += image->xsize;
367  }
368 }
short xsize
Definition: aliastorle.c:48
short depth
Definition: aliastorle.c:50
short xinit
Definition: aliastorle.c:49
#define VPRINTF
Definition: rletoalias.c:42
unsigned char * r
Definition: aliastorle.c:61
short yinit
Definition: aliastorle.c:49
unsigned char * g
Definition: aliastorle.c:61
static void code_alias24(unsigned char *, unsigned char *, unsigned char *, int, FILE *)
Definition: rletoalias.c:371
unsigned int byte
Definition: hilbert.c:47
int i
Definition: rletorla.c:82
short bbuf[8192]
Definition: iristorle.c:33
short rbuf[8192]
Definition: iristorle.c:31
#define Fwrite(p, s, n, f)
Definition: rletoalias.c:41
static void error(int)
Definition: rletoalias.c:423
short ysize
Definition: aliastorle.c:48
unsigned char * b
Definition: aliastorle.c:61
short gbuf[8192]
Definition: iristorle.c:32

Variable Documentation

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

Definition at line 29 of file rletoalias.c.

rle_hdr the_hdr

Definition at line 78 of file rletoalias.c.

int verbose = 0

Definition at line 74 of file rletoalias.c.