Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rletoppm.c
Go to the documentation of this file.
1 /*
2  * This software is copyrighted as noted below. It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is". Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * rletoppm - A conversion program to go from Utah's "rle" image format to
20  * pbmplus ppm true color image format.
21  *
22  * Author: Wesley C. Barris
23  * AHPCRC
24  * Minnesota Supercomputer Center, Inc.
25  * Date: Fri July 20 1990
26  * Copyright (c) Wesley C. Barris
27  */
28 #ifndef lint
29 static char rcsid[] = "$Header: /l/spencer/src/urt/cnv/RCS/rletoppm.c,v 3.0.1.4 1992/03/04 19:30:10 spencer Exp $";
30 #endif
31 #if 0
32 rletoppm() /* Tag. */
33 #endif
34 /*-----------------------------------------------------------------------------
35  * System includes.
36  */
37 
38 #define NO_DECLARE_MALLOC /* ppm.h does it */
39 #include "rle.h"
40 #include "ppm.h"
41 #include <stdio.h>
42 
43 #define VPRINTF if (verbose || header) fprintf
44 #define GRAYSCALE 001 /* 8 bits, no colormap */
45 #define PSEUDOCOLOR 010 /* 8 bits, colormap */
46 #define TRUECOLOR 011 /* 24 bits, colormap */
47 #define DIRECTCOLOR 100 /* 24 bits, no colormap */
48 /*
49  * PBMPLUS type declarations.
50  */
51 pixval maxval = 255;
52 /*
53  * Utah type declarations.
54  */
55 rle_hdr hdr;
57 /*
58  * Other declarations.
59  */
60 FILE *fpin;
63 int verbose = 0, header = 0;
64 int plain = 0;
65 /*-----------------------------------------------------------------------------
66  * Read the rle header.
67  */
68 void read_rle_header()
69 {
70  int i;
73  width = hdr.xmax - hdr.xmin + 1;
74  height = hdr.ymax - hdr.ymin + 1;
75  VPRINTF(stderr, "Image size: %dx%d\n", width, height);
76  if (hdr.ncolors == 1 && hdr.ncmap == 3) {
79  maplen = (1 << hdr.cmaplen);
80  VPRINTF(stderr, "Mapped color image with a map of length %d.\n", maplen);
81  }
82  else if (hdr.ncolors == 3 && hdr.ncmap == 0) {
84  VPRINTF(stderr, "24 bit color image, no colormap.\n");
85  }
86  else if (hdr.ncolors == 3 && hdr.ncmap == 3) {
87  visual = TRUECOLOR;
89  maplen = (1 << hdr.cmaplen);
90  VPRINTF(stderr, "24 bit color image with color map of length %d\n" ,maplen);
91  }
92  else if (hdr.ncolors == 1 && hdr.ncmap == 0) {
93  visual = GRAYSCALE;
94  VPRINTF(stderr, "Grayscale image.\n");
95  }
96  else {
97  fprintf(stderr,
98  "ncolors = %d, ncmap = %d, I don't know how to handle this!\n",
100  exit(-1);
101  }
102  if (hdr.alpha == 0)
103  VPRINTF(stderr, "No alpha channel.\n");
104  else if (hdr.alpha == 1)
105  VPRINTF(stderr, "Alpha channel exists!\n");
106  else {
107  fprintf(stderr, "alpha = %d, I don't know how to handle this!\n",
108  hdr.alpha);
109  exit(-1);
110  }
111  switch (hdr.background) {
112  case 0:
113  VPRINTF(stderr, "Use all pixels, ignore background color.");
114  break;
115  case 1:
116  VPRINTF(stderr,
117  "Use only non-background pixels, ignore background color.");
118  break;
119  case 2:
120  VPRINTF(stderr,
121  "Use only non-background pixels, clear to background color (default).");
122  break;
123  default:
124  VPRINTF(stderr, "Unknown background flag!\n");
125  break;
126  }
127  for (i = 0; i < hdr.ncolors; i++)
128  VPRINTF(stderr, " %d", hdr.bg_color[i]);
129  if (hdr.ncolors == 1 && hdr.ncmap == 3) {
130  VPRINTF(stderr, " (%d %d %d)\n",
131  hdr.cmap[hdr.bg_color[0]]>>8,
132  hdr.cmap[hdr.bg_color[0]+256]>>8,
133  hdr.cmap[hdr.bg_color[0]+512]>>8);
134  }
135  else if (hdr.ncolors == 3 && hdr.ncmap == 3) {
136  VPRINTF(stderr, " (%d %d %d)\n",
137  hdr.cmap[hdr.bg_color[0]]>>8,
138  hdr.cmap[hdr.bg_color[1]+256]>>8,
139  hdr.cmap[hdr.bg_color[2]+512]>>8);
140  }
141  else
142  VPRINTF(stderr, "\n");
143  if (hdr.comments)
144  for (i = 0; hdr.comments[i] != NULL; i++)
145  VPRINTF(stderr, "%s\n", hdr.comments[i]);
146 }
147 /*-----------------------------------------------------------------------------
148  * Write the ppm image data.
149  */
150 void write_ppm_data()
151 {
152  rle_pixel ***scanlines, **scanline;
153  register pixval r, g, b;
154  register pixel *pixelrow, *pP;
155  register int scan, x, y;
156 /*
157  * Allocate some stuff.
158  */
159  pixelrow = ppm_allocrow(width);
160 
161  scanlines = (rle_pixel ***)malloc( height * sizeof(rle_pixel **) );
162  RLE_CHECK_ALLOC( hdr.cmd, scanlines, "scanline pointers" );
163 
164  for ( scan = 0; scan < height; scan++ )
165  RLE_CHECK_ALLOC( hdr.cmd, (rle_row_alloc(&hdr, &scanlines[scan]) >= 0),
166  "pixel memory" );
167 /*
168  * Loop through those scan lines.
169  */
170  for (scan = 0; scan < height; scan++)
171  y = rle_getrow(&hdr, scanlines[height - scan - 1]);
172  for (scan = 0; scan < height; scan++) {
173  scanline = scanlines[scan];
174  switch (visual) {
175  case GRAYSCALE: /* 8 bits without colormap */
176  for (x = 0, pP = pixelrow; x < width; x++, pP++) {
177  r = scanline[0][x];
178  g = scanline[0][x];
179  b = scanline[0][x];
180  PPM_ASSIGN(*pP, r, g, b);
181  }
182  break;
183  case TRUECOLOR: /* 24 bits with colormap */
184  for (x = 0, pP = pixelrow; x < width; x++, pP++) {
185  r = colormap[scanline[0][x]]>>8;
186  g = colormap[scanline[1][x]+256]>>8;
187  b = colormap[scanline[2][x]+512]>>8;
188  PPM_ASSIGN(*pP, r, g, b);
189  }
190  break;
191  case DIRECTCOLOR: /* 24 bits without colormap */
192  for (x = 0, pP = pixelrow; x < width; x++, pP++) {
193  r = scanline[0][x];
194  g = scanline[1][x];
195  b = scanline[2][x];
196  PPM_ASSIGN(*pP, r, g, b);
197  }
198  break;
199  case PSEUDOCOLOR: /* 8 bits with colormap */
200  for (x = 0, pP = pixelrow; x < width; x++, pP++) {
201  r = colormap[scanline[0][x]]>>8;
202  g = colormap[scanline[0][x]+256]>>8;
203  b = colormap[scanline[0][x]+512]>>8;
204  PPM_ASSIGN(*pP, r, g, b);
205  }
206  break;
207  default:
208  break;
209  }
210  /*
211  * Write the scan line.
212  */
213  ppm_writeppmrow(stdout, pixelrow, width, maxval, plain);
214  } /* end of for scan = 0 to height */
215 
216  /* Free scanline memory. */
217  for ( scan = 0; scan < height; scan++ )
218  rle_row_free( &hdr, scanlines[scan] );
219  free( scanlines );
220 }
221 /*-----------------------------------------------------------------------------
222  * Convert a Utah rle file to a pbmplus ppm file.
223  */
224 int
226 int argc;
227 char **argv;
228 {
229  char *fname = NULL;
230 /*
231  * Get those options.
232  */
233  if (!scanargs(argc,argv,
234  "% v%- h%- p%- infile%s\n(\
235 \tConvert an RLE image to ppm true color.\n\
236 \t-v\tVerbose -- print out progress.\n\
237 \t-h\tPrint RLE header information and exit.\n\
238 \t-p\tWrite output image in \"plain\" mode.)",
239  &verbose,
240  &header,
241  &plain,
242  &fname))
243  exit(-1);
244 /*
245  * Open the file.
246  */
247  /* Initialize header. */
248  hdr = *rle_hdr_init( (rle_hdr *)NULL );
249  rle_names( &hdr, cmd_name( argv ), fname, 0 );
250 
251  fpin = rle_open_f( hdr.cmd, fname, "r" );
252 /*
253  * Read the rle file header.
254  */
255  read_rle_header();
256  if (header)
257  exit(0);
258 /*
259  * Write the ppm file header.
260  */
261  ppm_writeppminit(stdout, width, height, maxval, plain);
262 /*
263  * Write the rest of the ppm file.
264  */
265  write_ppm_data();
266  fclose(fpin);
267 
268  return 0;
269 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
rle_hdr hdr
Definition: getx10.c:84
int xmin
Definition: rle.h:100
const char ** comments
Definition: rle.h:113
#define GRAYSCALE
Definition: rletoalias.c:46
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
gpr_ $bmf_group_header_array_t header
Definition: getap.c:113
pixval maxval
Definition: rletoppm.c:51
void rle_row_free(rle_hdr *the_hdr, rle_pixel **scanp)
Definition: rle_row_alc.c:114
int height
Definition: rletoppm.c:62
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void main(int argc, char **argv)
Definition: aliastorle.c:121
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
rle_map * cmap
Definition: rle.h:112
int rle_row_alloc(rle_hdr *the_hdr, rle_pixel ***scanp)
Definition: rle_row_alc.c:56
int rle_getrow(rle_hdr *the_hdr, scanline)
Definition: rle_getrow.c:333
int * bg_color
Definition: rle.h:100
#define DIRECTCOLOR
Definition: rletoalias.c:44
int maplen
Definition: rletorla.c:80
int ymin
Definition: rle.h:100
int visual
Definition: rletorla.c:80
int verbose
Definition: rletorla.c:81
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
#define PSEUDOCOLOR
Definition: rletoalias.c:43
const char * cmd
Definition: rle.h:133
int xmax
Definition: rle.h:100
static char rcsid[]
Definition: rletoppm.c:29
FILE * fpin
Definition: rletorla.c:78
rle_map * colormap
Definition: rletorla.c:74
int background
Definition: rle.h:100
#define VPRINTF
Definition: aliastorle.c:42
int width
Definition: rletoppm.c:62
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 alpha
Definition: rle.h:100
int plain
Definition: rletoppm.c:64
unsigned short rle_map
Definition: rle.h:57
#define TRUECOLOR
Definition: rletoalias.c:45
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
FILE * rle_file
Definition: rle.h:114
int ncolors
Definition: rle.h:100
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86