Debian Codegraph @

converter/ppm/tgatoppm.c

raw symbols SCIP
1 /* tgatoppm.c - read a TrueVision Targa file and write a portable pixmap
2 **
3 ** Partially based on tga2rast, version 1.0, by Ian MacPhedran.
4 **
5 ** Copyright (C) 1989 by Jef Poskanzer.
6 **
7 ** Permission to use, copy, modify, and distribute this software and its
8 ** documentation for any purpose and without fee is hereby granted, provided
9 ** that the above copyright notice appear in all copies and that both that
10 ** copyright notice and this permission notice appear in supporting
11 ** documentation. This software is provided "as is" without express or
12 ** implied warranty.
13 */
14
15 #define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
16 #define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */
17 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
18
19 #include <string.h>
20
21 #include "pm_c_util.h"
22 #include "ppm.h"
23 #include "tga.h"
24 #include "shhopt.h"
25 #include "nstring.h"
26
27 #define MAXCOLORS 16384
28
29 static int mapped, rlencoded;
30
31 static pixel ColorMap[MAXCOLORS];
32 static gray AlphaMap[MAXCOLORS];
33
34 struct cmdlineInfo {
35 /* All the information the user supplied in the command line,
36 in a form easy for the program to use.
37 */
38 const char *input_filename;
39 unsigned int headerdump;
40 const char *alpha_filename;
41 unsigned int alpha_stdout;
42 };
43
44
45
46 static void
47 parseCommandLine(int argc, char ** argv,
48 struct cmdlineInfo *cmdlineP) {
49 /*----------------------------------------------------------------------------
50 Note that many of the strings that this function returns in the
51 *cmdlineP structure are actually in the supplied argv array. And
52 sometimes, one of these strings is actually just a suffix of an entry
53 in argv!
54 -----------------------------------------------------------------------------*/
55 optEntry *option_def = malloc(100*sizeof(optEntry));
56 /* Instructions to OptParseOptions2 on how to parse our options.
57 */
58 optStruct3 opt;
59
60 unsigned int option_def_index;
61
62 unsigned int alpha_spec;
63
64 option_def_index = 0; /* incremented by OPTENTRY */
65 OPTENT3(0, "headerdump", OPT_FLAG, NULL, &cmdlineP->headerdump, 0);
66 OPTENT3(0, "debug", OPT_FLAG, NULL, &cmdlineP->headerdump, 0);
67 OPTENT3(0, "alphaout", OPT_STRING, &cmdlineP->alpha_filename,
68 &alpha_spec, 0);
69
70 opt.opt_table = option_def;
71 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
72 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
73
74 pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
75 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
76
77 if (argc - 1 == 0)
78 cmdlineP->input_filename = "-"; /* he wants stdin */
79 else if (argc - 1 == 1)
80 cmdlineP->input_filename = strdup(argv[1]);
81 else
82 pm_error("Too many arguments. The only argument accepted "
83 "is the input file specification");
84
85 if (alpha_spec &&
86 streq(cmdlineP->alpha_filename, "-"))
87 cmdlineP->alpha_stdout = 1;
88 else
89 cmdlineP->alpha_stdout = 0;
90
91 if (!alpha_spec)
92 cmdlineP->alpha_filename = NULL;
93 }
94
95
96
97 static unsigned char
98 getbyte(FILE * const ifP) {
99
100 unsigned char c;
101
102 if ( fread( (char*) &c, 1, 1, ifP ) != 1 )
103 pm_error( "EOF / read error" );
104
105 return c;
106 }
107
108
109
110 static int RLE_count = 0, RLE_flag = 0;
111
112
113
114 static void
115 handleRun(FILE * const ifP,
116 bool * const repeatP) {
117
118 if (rlencoded) {
119 if (RLE_count == 0) {
120 /* Have to restart run. */
121 unsigned char i;
122 i = getbyte(ifP);
123 RLE_flag = (i & 0x80);
124 if (RLE_flag == 0) {
125 /* Stream of unencoded pixels. */
126 RLE_count = i + 1;
127 } else {
128 /* Single pixel replicated. */
129 RLE_count = i - 127;
130 }
131 /* Decrement count & get pixel. */
132 --RLE_count;
133 *repeatP = false;
134 } else {
135 /* Have already read count & (at least) first pixel. */
136 --RLE_count;
137 if (RLE_flag != 0) {
138 /* Replicated pixels. */
139 *repeatP = true;
140 } else
141 *repeatP = false;
142 }
143 } else
144 *repeatP = false;
145 }
146
147
148
149 static void
150 getPixel(FILE * const ifP,
151 pixel * const dest,
152 int const size,
153 gray * const alphaP) {
154
155 static pixval red, grn, blu;
156 static pixval alpha;
157 static unsigned int l;
158 unsigned char j, k;
159 bool repeat;
160 /* Next pixel is just a repeat (from an encoded run) */
161
162 handleRun(ifP, &repeat);
163
164 if (repeat) {
165 /* Use red, grn, blu, alpha, and l from prior call to getPixel */
166 } else {
167 /* Read appropriate number of bytes, break into RGB. */
168 switch (size) {
169 case 8: /* Grayscale, read and triplicate. */
170 red = grn = blu = l = getbyte(ifP);
171 alpha = 0;
172 break;
173
174 case 16: /* 5 bits each of red green and blue. */
175 case 15: /* Watch byte order. */
176 j = getbyte(ifP);
177 k = getbyte(ifP);
178 l = ((unsigned int)k << 8) + j;
179 red = (k & 0x7C) >> 2;
180 grn = ((k & 0x03) << 3) + ((j & 0xE0) >> 5);
181 blu = j & 0x1F;
182 alpha = 0;
183 break;
184
185 case 32: /* 8 bits each of blue, green, red, and alpha */
186 case 24: /* 8 bits each of blue, green, and red. */
187 blu = getbyte(ifP);
188 grn = getbyte(ifP);
189 red = getbyte(ifP);
190 if (size == 32)
191 alpha = getbyte(ifP);
192 else
193 alpha = 0;
194 l = 0;
195 break;
196
197 default:
198 pm_error("unknown pixel size (#2) - %d", size);
199 }
200 }
201 if (mapped) {
202 *dest = ColorMap[l];
203 *alphaP = AlphaMap[l];
204 } else {
205 PPM_ASSIGN(*dest, red, grn, blu);
206 *alphaP = alpha;
207 }
208 }
209
210
211
212 static void
213 readtga(FILE * const ifP, struct ImageHeader * tgaP) {
214
215 unsigned char flags;
216 ImageIDField junk;
217
218 tgaP->IdLength = getbyte( ifP );
219 tgaP->CoMapType = getbyte( ifP );
220 tgaP->ImgType = getbyte( ifP );
221 tgaP->Index_lo = getbyte( ifP );
222 tgaP->Index_hi = getbyte( ifP );
223 tgaP->Length_lo = getbyte( ifP );
224 tgaP->Length_hi = getbyte( ifP );
225 tgaP->CoSize = getbyte( ifP );
226 tgaP->X_org_lo = getbyte( ifP );
227 tgaP->X_org_hi = getbyte( ifP );
228 tgaP->Y_org_lo = getbyte( ifP );
229 tgaP->Y_org_hi = getbyte( ifP );
230 tgaP->Width_lo = getbyte( ifP );
231 tgaP->Width_hi = getbyte( ifP );
232 tgaP->Height_lo = getbyte( ifP );
233 tgaP->Height_hi = getbyte( ifP );
234 tgaP->PixelSize = getbyte( ifP );
235 flags = getbyte( ifP );
236 tgaP->AttBits = flags & 0xf;
237 tgaP->Rsrvd = ( flags & 0x10 ) >> 4;
238 tgaP->OrgBit = ( flags & 0x20 ) >> 5;
239 tgaP->IntrLve = ( flags & 0xc0 ) >> 6;
240
241 if ( tgaP->IdLength != 0 )
242 fread( junk, 1, (int) tgaP->IdLength, ifP );
243 }
244
245
246
247 static void
248 get_map_entry(FILE * const ifP, pixel * Value, int Size, gray * Alpha) {
249
250 unsigned char j, k, r, g, b, a;
251
252 /* Read appropriate number of bytes, break into rgb & put in map. */
253 switch ( Size )
254 {
255 case 8: /* Grayscale, read and triplicate. */
256 r = g = b = getbyte( ifP );
257 a = 0;
258 break;
259
260 case 16: /* 5 bits each of red green and blue. */
261 case 15: /* Watch for byte order. */
262 j = getbyte( ifP );
263 k = getbyte( ifP );
264 r = ( k & 0x7C ) >> 2;
265 g = ( ( k & 0x03 ) << 3 ) + ( ( j & 0xE0 ) >> 5 );
266 b = j & 0x1F;
267 a = 0;
268 break;
269
270 case 32: /* 8 bits each of blue, green, red, and alpha */
271 case 24: /* 8 bits each of blue green and red. */
272 b = getbyte( ifP );
273 g = getbyte( ifP );
274 r = getbyte( ifP );
275 if ( Size == 32 )
276 a = getbyte( ifP );
277 else
278 a = 0;
279 break;
280
281 default:
282 pm_error( "unknown colormap pixel size (#2) - %d", Size );
283 }
284 PPM_ASSIGN( *Value, r, g, b );
285 *Alpha = a;
286 }
287
288
289
290 static void
291 dumpHeader(struct ImageHeader const tga_head) {
292 const char * imgTypeName;
293 switch(tga_head.ImgType) {
294 case TGA_Map: imgTypeName = "TGA_Map"; break;
295 case TGA_RGB: imgTypeName = "TGA_RGB"; break;
296 case TGA_Mono: imgTypeName = "TGA_Mono"; break;
297 case TGA_RLEMap: imgTypeName = "TGA_RLEMap"; break;
298 case TGA_RLERGB: imgTypeName = "TGA_RLERGB"; break;
299 case TGA_RLEMono: imgTypeName = "TGA_RLEMono"; break;
300 case TGA_CompMap: imgTypeName = "TGA_CompMap"; break;
301 case TGA_CompMap4: imgTypeName = "TGA_CompMap4"; break;
302 default: imgTypeName = "unknown";
303 }
304 pm_message( "IdLength = %d", (int) tga_head.IdLength );
305 pm_message( "CoMapType = %d", (int) tga_head.CoMapType );
306 pm_message( "ImgType = %d (%s)", (int) tga_head.ImgType, imgTypeName );
307 pm_message( "Index_lo = %d", (int) tga_head.Index_lo );
308 pm_message( "Index_hi = %d", (int) tga_head.Index_hi );
309 pm_message( "Length_lo = %d", (int) tga_head.Length_lo );
310 pm_message( "Length_hi = %d", (int) tga_head.Length_hi );
311 pm_message( "CoSize = %d", (int) tga_head.CoSize );
312 pm_message( "X_org_lo = %d", (int) tga_head.X_org_lo );
313 pm_message( "X_org_hi = %d", (int) tga_head.X_org_hi );
314 pm_message( "Y_org_lo = %d", (int) tga_head.Y_org_lo );
315 pm_message( "Y_org_hi = %d", (int) tga_head.Y_org_hi );
316 pm_message( "Width_lo = %d", (int) tga_head.Width_lo );
317 pm_message( "Width_hi = %d", (int) tga_head.Width_hi );
318 pm_message( "Height_lo = %d", (int) tga_head.Height_lo );
319 pm_message( "Height_hi = %d", (int) tga_head.Height_hi );
320 pm_message( "PixelSize = %d", (int) tga_head.PixelSize );
321 pm_message( "AttBits = %d", (int) tga_head.AttBits );
322 pm_message( "Rsrvd = %d", (int) tga_head.Rsrvd );
323 pm_message( "OrgBit = %d", (int) tga_head.OrgBit );
324 pm_message( "IntrLve = %d", (int) tga_head.IntrLve );
325 }
326
327
328
329 int
330 main(int argc, char * argv[]) {
331
332 struct cmdlineInfo cmdline;
333 struct ImageHeader tga_head;
334 FILE* ifP;
335 FILE *imageout_file, *alpha_file;
336 int rows, cols, row, realrow, truerow, baserow;
337 int maxval;
338 pixel** pixels; /* The image array in ppm format */
339 gray** alpha; /* The alpha channel array in pgm format */
340
341 ppm_init(&argc, argv);
342
343 parseCommandLine(argc, argv, &cmdline);
344
345 ifP = pm_openr(cmdline.input_filename);
346
347 if (cmdline.alpha_stdout)
348 alpha_file = stdout;
349 else if (cmdline.alpha_filename == NULL)
350 alpha_file = NULL;
351 else
352 alpha_file = pm_openw(cmdline.alpha_filename);
353
354 if (cmdline.alpha_stdout)
355 imageout_file = NULL;
356 else
357 imageout_file = stdout;
358
359 /* Read the Targa file header. */
360 readtga(ifP, &tga_head);
361
362 if (cmdline.headerdump)
363 dumpHeader(tga_head);
364
365 rows = ((int) tga_head.Height_lo) + ((int) tga_head.Height_hi) * 256;
366 cols = ((int) tga_head.Width_lo) + ((int) tga_head.Width_hi) * 256;
367
368 switch (tga_head.ImgType) {
369 case TGA_Map:
370 case TGA_RGB:
371 case TGA_Mono:
372 case TGA_RLEMap:
373 case TGA_RLERGB:
374 case TGA_RLEMono:
375 break;
376 case TGA_CompMap:
377 case TGA_CompMap4:
378 pm_error("Targa image type %d (compressed color-mapped data). "
379 "Cannot handle this format.", tga_head.ImgType);
380 break;
381 default:
382 pm_error("unknown Targa image type %d", tga_head.ImgType);
383 }
384
385 if (tga_head.ImgType == TGA_Map ||
386 tga_head.ImgType == TGA_RLEMap ||
387 tga_head.ImgType == TGA_CompMap ||
388 tga_head.ImgType == TGA_CompMap4)
389 { /* Color-mapped image */
390 if (tga_head.CoMapType != 1)
391 pm_error(
392 "mapped image (type %d) with color map type != 1",
393 tga_head.ImgType );
394 mapped = true;
395 /* Figure maxval from CoSize. */
396 switch (tga_head.CoSize) {
397 case 8:
398 case 24:
399 case 32:
400 maxval = 255;
401 break;
402
403 case 15:
404 case 16:
405 maxval = 31;
406 break;
407
408 default:
409 pm_error(
410 "unknown colormap pixel size - %d", tga_head.CoSize );
411 }
412 } else {
413 /* Not colormap, so figure maxval from PixelSize. */
414 mapped = false;
415 switch ( tga_head.PixelSize ) {
416 case 8:
417 case 24:
418 case 32:
419 maxval = 255;
420 break;
421
422 case 15:
423 case 16:
424 maxval = 31;
425 break;
426
427 default:
428 pm_error("unknown pixel size - %d", tga_head.PixelSize);
429 }
430 }
431
432 /* If required, read the color map information. */
433 if ( tga_head.CoMapType != 0 ) {
434 unsigned int i;
435 unsigned int temp1, temp2;
436
437 temp1 = tga_head.Index_lo + tga_head.Index_hi * 256;
438 temp2 = tga_head.Length_lo + tga_head.Length_hi * 256;
439 if ((temp1 + temp2 + 1) >= MAXCOLORS)
440 pm_error("too many colors - %d", (temp1 + temp2 + 1));
441 for (i = temp1; i < (temp1 + temp2); ++i)
442 get_map_entry(ifP, &ColorMap[i], (int) tga_head.CoSize,
443 &AlphaMap[i]);
444 }
445
446 /* Check run-length encoding. */
447 if (tga_head.ImgType == TGA_RLEMap ||
448 tga_head.ImgType == TGA_RLERGB ||
449 tga_head.ImgType == TGA_RLEMono)
450 rlencoded = 1;
451 else
452 rlencoded = 0;
453
454 /* Read the Targa file body and convert to portable format. */
455 pixels = ppm_allocarray( cols, rows );
456 alpha = pgm_allocarray( cols, rows );
457 truerow = 0;
458 baserow = 0;
459 for (row = 0; row < rows; ++row) {
460 unsigned int col;
461
462 realrow = truerow;
463 if (tga_head.OrgBit == 0)
464 realrow = rows - realrow - 1;
465
466 for (col = 0; col < cols; ++col)
467 getPixel(ifP, &(pixels[realrow][col]), (int) tga_head.PixelSize,
468 &(alpha[realrow][col]));
469 if (tga_head.IntrLve == TGA_IL_Four)
470 truerow += 4;
471 else if (tga_head.IntrLve == TGA_IL_Two)
472 truerow += 2;
473 else
474 ++truerow;
475 if (truerow >= rows)
476 truerow = ++baserow;
477 }
478 pm_close(ifP);
479
480 if (imageout_file)
481 ppm_writeppm(imageout_file, pixels, cols, rows, (pixval) maxval, 0);
482 if (alpha_file)
483 pgm_writepgm(alpha_file, alpha, cols, rows, (pixval) maxval, 0);
484 if (imageout_file)
485 pm_close(imageout_file);
486 if (alpha_file)
487 pm_close(alpha_file);
488
489 return 0;
490 }
491
492
493