Debian Codegraph @

converter/other/rasttopnm.c

raw
1 /* rasttopnm.c - read a Sun rasterfile and produce a portable anymap
2 **
3 ** Copyright (C) 1989, 1991 by Jef Poskanzer.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12
13 #include "pm_c_util.h"
14 #include "mallocvar.h"
15 #include "shhopt.h"
16 #include "pnm.h"
17 #include "rast.h"
18
19
20
21 struct cmdlineInfo {
22 /* All the information the user supplied in the command line,
23 in a form easy for the program to use.
24 */
25 const char * inputFileName;
26 unsigned int index;
27 unsigned int dumpheader;
28 unsigned int dumpcolormap;
29 };
30
31
32
33 static void
34 parseCommandLine(int argc, const char ** argv,
35 struct cmdlineInfo * const cmdlineP) {
36 /*----------------------------------------------------------------------------
37 Note that the file spec array we return is stored in the storage that
38 was passed to us as the argv array.
39 -----------------------------------------------------------------------------*/
40 optEntry * option_def; /* Used by OPTENT3 */
41 optStruct3 opt;
42
43 unsigned int option_def_index;
44
45 MALLOCARRAY_NOFAIL(option_def, 100);
46
47 option_def_index = 0; /* incremented by OPTENT3 */
48
49 opt.opt_table = option_def;
50 opt.short_allowed = false; /* We have no short (old-fashioned) options */
51 opt.allowNegNum = false; /* We have no parms that are negative numbers */
52
53 OPTENT3(0, "index", OPT_FLAG, NULL,
54 &cmdlineP->index, 0);
55 OPTENT3(0, "dumpheader", OPT_FLAG, NULL,
56 &cmdlineP->dumpheader, 0);
57 OPTENT3(0, "dumpcolormap", OPT_FLAG, NULL,
58 &cmdlineP->dumpcolormap, 0);
59
60 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
61 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
62
63 if (argc-1 == 0)
64 cmdlineP->inputFileName = "-";
65 else if (argc-1 != 1)
66 pm_error("Program takes zero or one argument (filename). You "
67 "specified %d", argc-1);
68 else
69 cmdlineP->inputFileName = argv[1];
70 }
71
72
73
74 static bool
75 colorMapIsGrayscale(colormap_t const colorMap) {
76 /*----------------------------------------------------------------------------
77 The color map contains only gray.
78 -----------------------------------------------------------------------------*/
79 unsigned int i;
80 bool grayscale;
81
82 for (i = 0, grayscale = true; i < colorMap.length; ++i) {
83 if (colorMap.map[0][i] != colorMap.map[1][i] ||
84 colorMap.map[1][i] != colorMap.map[2][i]) {
85 grayscale = false;
86 }
87 }
88 return grayscale;
89 }
90
91
92
93 static void
94 analyzeImage(struct rasterfile const header,
95 colormap_t const colorMap,
96 int * const formatP,
97 xelval * const maxvalP,
98 bool * const grayscaleP,
99 xel * const zeroP,
100 xel * const oneP) {
101
102 bool const grayscale =
103 header.ras_maplength == 0 || colorMapIsGrayscale(colorMap);
104
105 *grayscaleP = grayscale;
106
107 switch (header.ras_depth) {
108 case 1:
109 if (header.ras_maptype == RMT_NONE && header.ras_maplength == 0) {
110 *maxvalP = 1;
111 *formatP = PBM_TYPE;
112 PNM_ASSIGN1(*zeroP, 1);
113 PNM_ASSIGN1(*oneP, 0);
114 } else if (header.ras_maptype == RMT_EQUAL_RGB &&
115 header.ras_maplength == 6) {
116 if (grayscale) {
117 *maxvalP = 255;
118 *formatP = PGM_TYPE;
119 PNM_ASSIGN1(*zeroP, colorMap.map[0][0]);
120 PNM_ASSIGN1(*oneP, colorMap.map[0][1]);
121 } else {
122 *maxvalP = 255;
123 *formatP = PPM_TYPE;
124 PPM_ASSIGN(
125 *zeroP, colorMap.map[0][0], colorMap.map[1][0],
126 colorMap.map[2][0]);
127 PPM_ASSIGN(
128 *oneP, colorMap.map[0][1], colorMap.map[1][1],
129 colorMap.map[2][1]);
130 }
131 } else
132 pm_error(
133 "this depth-1 rasterfile has a non-standard colormap - "
134 "type %ld length %ld",
135 header.ras_maptype, header.ras_maplength);
136 break;
137
138 case 8:
139 if (grayscale) {
140 *maxvalP = 255;
141 *formatP = PGM_TYPE;
142 } else if (header.ras_maptype == RMT_EQUAL_RGB) {
143 *maxvalP = 255;
144 *formatP = PPM_TYPE;
145 } else
146 pm_error(
147 "this depth-8 rasterfile has a non-standard colormap - "
148 "type %ld length %ld",
149 header.ras_maptype, header.ras_maplength);
150 break;
151
152 case 24:
153 case 32:
154 if (header.ras_maptype == RMT_NONE && header.ras_maplength == 0);
155 else if (header.ras_maptype == RMT_RAW || header.ras_maplength == 768);
156 else
157 pm_error(
158 "this depth-%ld rasterfile has a non-standard colormap - "
159 "type %ld length %ld",
160 header.ras_depth, header.ras_maptype, header.ras_maplength);
161 *maxvalP = 255;
162 *formatP = PPM_TYPE;
163 break;
164
165 default:
166 pm_error("invalid depth: %ld. Can handle only depth 1, 8, 24, or 32.",
167 header.ras_depth);
168 }
169 }
170
171
172
173 static void
174 reportOutputType(int const format) {
175
176 switch (PNM_FORMAT_TYPE(format)) {
177 case PBM_TYPE:
178 pm_message("writing PBM file");
179 break;
180 case PGM_TYPE:
181 pm_message("writing PGM file");
182 break;
183 case PPM_TYPE:
184 pm_message("writing PPM file");
185 break;
186 default:
187 abort();
188 }
189 }
190
191
192
193 static void
194 convertRowDepth1(const unsigned char * const rastLine,
195 unsigned int const cols,
196 xel const zeroXel,
197 xel const oneXel,
198 xel * const xelrow) {
199
200 const unsigned char * byteP;
201 unsigned int col;
202 unsigned char mask;
203
204 byteP = rastLine; /* initial value */
205
206 for (col = 0, mask = 0x80; col < cols; ++col) {
207 if (mask == 0x00) {
208 ++byteP;
209 mask = 0x80;
210 }
211 xelrow[col] = (*byteP & mask) ? oneXel : zeroXel;
212 mask = mask >> 1;
213 }
214 }
215
216
217
218 static void
219 convertRowDepth8(const unsigned char * const lineStart,
220 unsigned int const cols,
221 bool const colorMapped,
222 bool const useIndexForColor,
223 bool const grayscale,
224 colormap_t const colorMap,
225 xel * const xelrow) {
226 /*----------------------------------------------------------------------------
227 Convert a line of raster data from the RAST input to a row of raster
228 data for the PNM output.
229
230 'lineStart' is where the RAST row starts. 'xelrow' is where to put the
231 PNM row. 'cols' is the number of pixels in the row.
232
233 'colorMapped' means the RAST image is colormapped. If so, 'colorMap'
234 is the color map from the RAST file and 'useIndexForColor' means not
235 to use that map but instead to create a PGM row of the colormap
236 _indices_.
237
238 'grayscale' means it is a grayscale image; the output is PGM.
239 -----------------------------------------------------------------------------*/
240 const unsigned char * byteP;
241 unsigned int col;
242
243 byteP = lineStart; /* initial value */
244
245 for (col = 0; col < cols; ++col) {
246 if (colorMapped && !useIndexForColor) {
247 if (grayscale)
248 PNM_ASSIGN1(xelrow[col], colorMap.map[0][*byteP]);
249 else
250 PPM_ASSIGN(xelrow[col],
251 colorMap.map[0][*byteP],
252 colorMap.map[1][*byteP],
253 colorMap.map[2][*byteP]);
254 } else
255 PNM_ASSIGN1(xelrow[col], *byteP);
256
257 ++byteP;
258 }
259 }
260
261
262
263 static void
264 convertRowRgb(const unsigned char * const lineStart,
265 unsigned int const cols,
266 unsigned int const depth,
267 long const rastType,
268 bool const colorMapped,
269 bool const useIndexForColor,
270 colormap_t const colorMap,
271 xel * const xelrow) {
272
273 const unsigned char * byteP;
274 unsigned int col;
275
276 byteP = lineStart; /* initial value */
277
278 for (col = 0; col < cols; ++col) {
279 xelval r, g, b;
280
281 if (depth == 32)
282 ++byteP;
283 if (rastType == RT_FORMAT_RGB) {
284 r = *byteP++;
285 g = *byteP++;
286 b = *byteP++;
287 } else {
288 b = *byteP++;
289 g = *byteP++;
290 r = *byteP++;
291 }
292 if (colorMapped && !useIndexForColor)
293 PPM_ASSIGN(xelrow[col],
294 colorMap.map[0][r],
295 colorMap.map[1][g],
296 colorMap.map[2][b]);
297 else
298 PPM_ASSIGN(xelrow[col], r, g, b);
299 }
300 }
301
302
303
304 static void
305 writePnm(FILE * const ofP,
306 const struct pixrect * const pixRectP,
307 unsigned int const cols,
308 unsigned int const rows,
309 xelval const maxval,
310 int const format,
311 unsigned int const depth,
312 long const rastType,
313 bool const grayscale,
314 bool const colorMapped,
315 colormap_t const colorMap,
316 xel const zeroXel,
317 xel const oneXel,
318 bool const useIndexForColor) {
319
320 struct mpr_data const mprData = *pixRectP->pr_data;
321
322 xel * xelrow;
323 unsigned int row;
324 unsigned char * lineStart;
325
326 pnm_writepnminit(ofP, cols, rows, maxval, format, 0);
327
328 xelrow = pnm_allocrow(cols);
329
330 reportOutputType(format);
331
332 for (row = 0, lineStart = mprData.md_image;
333 row < rows;
334 ++row, lineStart += mprData.md_linebytes) {
335
336 switch (depth) {
337 case 1:
338 convertRowDepth1(lineStart, cols, zeroXel, oneXel, xelrow);
339 break;
340 case 8:
341 convertRowDepth8(lineStart, cols, colorMapped, useIndexForColor,
342 grayscale, colorMap, xelrow);
343 break;
344 case 24:
345 case 32:
346 convertRowRgb(lineStart, cols, depth, rastType, colorMapped,
347 useIndexForColor, colorMap, xelrow);
348 break;
349 default:
350 pm_error("Invalid depth value: %u", depth);
351 }
352 pnm_writepnmrow(ofP, xelrow, cols, maxval, format, 0);
353 }
354 }
355
356
357
358 static void
359 dumpHeader(struct rasterfile const header) {
360
361 const char * typeName;
362
363 switch (header.ras_type) {
364 case RT_OLD: typeName = "old"; break;
365 case RT_STANDARD: typeName = "standard"; break;
366 case RT_BYTE_ENCODED: typeName = "byte encoded"; break;
367 case RT_FORMAT_RGB: typeName = "format rgb"; break;
368 case RT_FORMAT_TIFF: typeName = "format_tiff"; break;
369 case RT_FORMAT_IFF: typeName = "format_iff"; break;
370 case RT_EXPERIMENTAL: typeName = "experimental"; break;
371 default: typeName = "???";
372 }
373
374 pm_message("type: %s (%lu)", typeName, (unsigned long)header.ras_type);
375 pm_message("%luw x %lul x %lud",
376 (unsigned long)header.ras_width,
377 (unsigned long)header.ras_height,
378 (unsigned long)header.ras_depth);
379 pm_message("raster length: %lu", (unsigned long)header.ras_length);
380
381 if (header.ras_maplength)
382 pm_message("Has color map");
383 }
384
385
386
387 static void
388 dumpHeaderAnalysis(bool const grayscale,
389 unsigned int const depth,
390 xel const zero,
391 xel const one) {
392
393 pm_message("grayscale: %s", grayscale ? "YES" : "NO");
394
395 if (depth == 1) {
396 pm_message("Zero color: (%u,%u,%u)",
397 PNM_GETR(zero),
398 PNM_GETG(zero),
399 PNM_GETB(zero));
400
401 pm_message("One color: (%u,%u,%u)",
402 PNM_GETR(one),
403 PNM_GETG(one),
404 PNM_GETB(one));
405 }
406 }
407
408
409
410 static void
411 dumpColorMap(colormap_t const colorMap) {
412
413 unsigned int i;
414 const char * typeName;
415
416 switch (colorMap.type) {
417 case RMT_NONE: typeName = "NONE"; break;
418 case RMT_EQUAL_RGB: typeName = "EQUAL_RGB"; break;
419 case RMT_RAW: typeName = "RAW"; break;
420 default: typeName = "???";
421 }
422
423 pm_message("color map type = %s (%u)", typeName, colorMap.type);
424
425 pm_message("color map size = %u", colorMap.length);
426
427 for (i = 0; i < colorMap.length; ++i)
428 pm_message("color %u: (%u, %u, %u)", i,
429 (unsigned char)colorMap.map[0][i],
430 (unsigned char)colorMap.map[1][i],
431 (unsigned char)colorMap.map[2][i]);
432 }
433
434
435
436 int
437 main(int argc, const char ** const argv) {
438
439 struct cmdlineInfo cmdline;
440 FILE * ifP;
441 struct rasterfile header;
442 colormap_t colorMap;
443 bool grayscale;
444 struct pixrect * pr;
445 int format;
446 xelval maxval;
447 xel zero, one;
448 int rc;
449
450 pm_proginit(&argc, argv);
451
452 parseCommandLine(argc, argv, &cmdline);
453
454 ifP = pm_openr(cmdline.inputFileName);
455
456 rc = pr_load_header(ifP, &header);
457 if (rc != 0 )
458 pm_error("unable to read in rasterfile header");
459
460 if (cmdline.dumpheader)
461 dumpHeader(header);
462
463 if (header.ras_maplength != 0) {
464 int rc;
465
466 rc = pr_load_colormap(ifP, &header, &colorMap);
467
468 if (rc != 0 )
469 pm_error("unable to read colormap from RAST file");
470
471 if (cmdline.dumpcolormap)
472 dumpColorMap(colorMap);
473 }
474
475 analyzeImage(header, colorMap, &format, &maxval, &grayscale, &zero, &one);
476
477 if (cmdline.dumpheader)
478 dumpHeaderAnalysis(grayscale, header.ras_depth, zero, one);
479
480 pr = pr_load_image(ifP, &header, NULL);
481 if (pr == NULL )
482 pm_error("unable to read in the image from the rasterfile" );
483
484 if (cmdline.index && header.ras_maplength == 0)
485 pm_error("You requested to use color map indices as colors (-index), "
486 "but this is not a color mapped image");
487
488 writePnm(stdout, pr, header.ras_width, header.ras_height, maxval, format,
489 header.ras_depth, header.ras_type, grayscale,
490 header.ras_maplength > 0, colorMap, zero, one, cmdline.index);
491
492 pm_close(ifP);
493 pm_close(stdout);
494
495 return 0;
496 }
497
498
499