Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/tools/pal2rgb.c

raw
1 /*
2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 #include "libport.h"
26 #include "tif_config.h"
27
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #include "tiffio.h"
38
39 #ifndef EXIT_SUCCESS
40 #define EXIT_SUCCESS 0
41 #endif
42 #ifndef EXIT_FAILURE
43 #define EXIT_FAILURE 1
44 #endif
45
46 #define streq(a, b) (strcmp(a, b) == 0)
47 #define strneq(a, b, n) (strncmp(a, b, n) == 0)
48
49 static void usage(int code);
50 static void cpTags(TIFF *in, TIFF *out);
51
52 static int checkcmap(int n, uint16_t *r, uint16_t *g, uint16_t *b)
53 {
54 while (n-- > 0)
55 if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
56 return (16);
57 fprintf(stderr, "Warning, assuming 8-bit colormap.\n");
58 return (8);
59 }
60
61 #define CopyField(tag, v) \
62 if (TIFFGetField(in, tag, &v)) \
63 TIFFSetField(out, tag, v)
64 #define CopyField3(tag, v1, v2, v3) \
65 if (TIFFGetField(in, tag, &v1, &v2, &v3)) \
66 TIFFSetField(out, tag, v1, v2, v3)
67
68 static uint16_t compression = (uint16_t)-1;
69 static uint16_t predictor = 0;
70 static int quality = 75; /* JPEG quality */
71 static int jpegcolormode = JPEGCOLORMODE_RGB;
72 static int processCompressOptions(char *);
73
74 int main(int argc, char *argv[])
75 {
76 uint16_t bitspersample, shortv;
77 uint32_t imagewidth, imagelength;
78 uint16_t config = PLANARCONFIG_CONTIG;
79 uint32_t rowsperstrip = (uint32_t)-1;
80 uint16_t photometric = PHOTOMETRIC_RGB;
81 uint16_t *rmap, *gmap, *bmap;
82 uint32_t row;
83 int cmap = -1;
84 TIFF *in, *out;
85 int c;
86
87 #if !HAVE_DECL_OPTARG
88 extern int optind;
89 extern char *optarg;
90 #endif
91
92 while ((c = getopt(argc, argv, "C:c:p:r:h")) != -1)
93 switch (c)
94 {
95 case 'C': /* force colormap interpretation */
96 cmap = atoi(optarg);
97 break;
98 case 'c': /* compression scheme */
99 if (!processCompressOptions(optarg))
100 usage(EXIT_FAILURE);
101 break;
102 case 'p': /* planar configuration */
103 if (streq(optarg, "separate"))
104 config = PLANARCONFIG_SEPARATE;
105 else if (streq(optarg, "contig"))
106 config = PLANARCONFIG_CONTIG;
107 else
108 usage(EXIT_FAILURE);
109 break;
110 case 'r': /* rows/strip */
111 rowsperstrip = atoi(optarg);
112 break;
113 case 'h':
114 usage(EXIT_SUCCESS);
115 break;
116 case '?':
117 usage(EXIT_FAILURE);
118 /*NOTREACHED*/
119 }
120 if (argc - optind != 2)
121 usage(EXIT_FAILURE);
122 in = TIFFOpen(argv[optind], "r");
123 if (in == NULL)
124 return (EXIT_FAILURE);
125 if (!TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &shortv) ||
126 shortv != PHOTOMETRIC_PALETTE)
127 {
128 fprintf(stderr, "%s: Expecting a palette image.\n", argv[optind]);
129 (void)TIFFClose(in);
130 return (EXIT_FAILURE);
131 }
132 if (!TIFFGetField(in, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap))
133 {
134 fprintf(stderr, "%s: No colormap (not a valid palette image).\n",
135 argv[optind]);
136 (void)TIFFClose(in);
137 return (EXIT_FAILURE);
138 }
139 bitspersample = 0;
140 TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample);
141 if (bitspersample != 8)
142 {
143 fprintf(stderr, "%s: Sorry, can only handle 8-bit images.\n",
144 argv[optind]);
145 (void)TIFFClose(in);
146 return (EXIT_FAILURE);
147 }
148 out = TIFFOpen(argv[optind + 1], "w");
149 if (out == NULL)
150 {
151 (void)TIFFClose(in);
152 return (EXIT_FAILURE);
153 }
154 cpTags(in, out);
155 TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &imagewidth);
156 TIFFGetField(in, TIFFTAG_IMAGELENGTH, &imagelength);
157 if (compression != (uint16_t)-1)
158 TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
159 else
160 TIFFGetField(in, TIFFTAG_COMPRESSION, &compression);
161 switch (compression)
162 {
163 case COMPRESSION_JPEG:
164 if (jpegcolormode == JPEGCOLORMODE_RGB)
165 photometric = PHOTOMETRIC_YCBCR;
166 else
167 photometric = PHOTOMETRIC_RGB;
168 TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
169 TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
170 break;
171 case COMPRESSION_LZW:
172 case COMPRESSION_ADOBE_DEFLATE:
173 case COMPRESSION_DEFLATE:
174 if (predictor != 0)
175 TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
176 break;
177 }
178 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
179 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
180 TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
181 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
182 rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip));
183 (void)TIFFGetField(in, TIFFTAG_PLANARCONFIG, &shortv);
184 if (cmap == -1)
185 cmap = checkcmap(1 << bitspersample, rmap, gmap, bmap);
186 if (cmap == 16)
187 {
188 /*
189 * Convert 16-bit colormap to 8-bit.
190 */
191 int i;
192
193 for (i = (1 << bitspersample) - 1; i >= 0; i--)
194 {
195 #define CVT(x) (((x)*255) / ((1L << 16) - 1))
196 rmap[i] = CVT(rmap[i]);
197 gmap[i] = CVT(gmap[i]);
198 bmap[i] = CVT(bmap[i]);
199 }
200 }
201 {
202 unsigned char *ibuf, *obuf;
203 register unsigned char *pp;
204 register uint32_t x;
205 tmsize_t tss_in = TIFFScanlineSize(in);
206 tmsize_t tss_out = TIFFScanlineSize(out);
207 if (tss_out / tss_in < 3)
208 {
209 /*
210 * BUG 2750: The following code does not know about chroma
211 * subsampling of JPEG data. It assumes that the output buffer is 3x
212 * the length of the input buffer due to exploding the palette into
213 * RGB tuples. If this assumption is incorrect, it could lead to a
214 * buffer overflow. Go ahead and fail now to prevent that.
215 */
216 fprintf(stderr, "Could not determine correct image size for "
217 "output. Exiting.\n");
218 return EXIT_FAILURE;
219 }
220 ibuf = (unsigned char *)_TIFFmalloc(tss_in);
221 obuf = (unsigned char *)_TIFFmalloc(tss_out);
222 switch (config)
223 {
224 case PLANARCONFIG_CONTIG:
225 for (row = 0; row < imagelength; row++)
226 {
227 if (!TIFFReadScanline(in, ibuf, row, 0))
228 goto done;
229 pp = obuf;
230 for (x = 0; x < imagewidth; x++)
231 {
232 *pp++ = (unsigned char)rmap[ibuf[x]];
233 *pp++ = (unsigned char)gmap[ibuf[x]];
234 *pp++ = (unsigned char)bmap[ibuf[x]];
235 }
236 if (!TIFFWriteScanline(out, obuf, row, 0))
237 goto done;
238 }
239 break;
240 case PLANARCONFIG_SEPARATE:
241 for (row = 0; row < imagelength; row++)
242 {
243 if (!TIFFReadScanline(in, ibuf, row, 0))
244 goto done;
245 for (pp = obuf, x = 0; x < imagewidth; x++)
246 *pp++ = (unsigned char)rmap[ibuf[x]];
247 if (!TIFFWriteScanline(out, obuf, row, 0))
248 goto done;
249 for (pp = obuf, x = 0; x < imagewidth; x++)
250 *pp++ = (unsigned char)gmap[ibuf[x]];
251 if (!TIFFWriteScanline(out, obuf, row, 0))
252 goto done;
253 for (pp = obuf, x = 0; x < imagewidth; x++)
254 *pp++ = (unsigned char)bmap[ibuf[x]];
255 if (!TIFFWriteScanline(out, obuf, row, 0))
256 goto done;
257 }
258 break;
259 }
260 _TIFFfree(ibuf);
261 _TIFFfree(obuf);
262 }
263 done:
264 (void)TIFFClose(in);
265 (void)TIFFClose(out);
266 return (EXIT_SUCCESS);
267 }
268
269 static int processCompressOptions(char *opt)
270 {
271 if (streq(opt, "none"))
272 compression = COMPRESSION_NONE;
273 else if (streq(opt, "packbits"))
274 compression = COMPRESSION_PACKBITS;
275 else if (strneq(opt, "jpeg", 4))
276 {
277 char *cp = strchr(opt, ':');
278
279 compression = COMPRESSION_JPEG;
280 while (cp)
281 {
282 if (isdigit((int)cp[1]))
283 quality = atoi(cp + 1);
284 else if (cp[1] == 'r')
285 jpegcolormode = JPEGCOLORMODE_RAW;
286 else
287 usage(EXIT_FAILURE);
288
289 cp = strchr(cp + 1, ':');
290 }
291 }
292 else if (strneq(opt, "lzw", 3))
293 {
294 char *cp = strchr(opt, ':');
295 if (cp)
296 predictor = atoi(cp + 1);
297 compression = COMPRESSION_LZW;
298 }
299 else if (strneq(opt, "zip", 3))
300 {
301 char *cp = strchr(opt, ':');
302 if (cp)
303 predictor = atoi(cp + 1);
304 compression = COMPRESSION_ADOBE_DEFLATE;
305 }
306 else
307 return (0);
308 return (1);
309 }
310
311 #define CopyField(tag, v) \
312 if (TIFFGetField(in, tag, &v)) \
313 TIFFSetField(out, tag, v)
314 #define CopyField2(tag, v1, v2) \
315 if (TIFFGetField(in, tag, &v1, &v2)) \
316 TIFFSetField(out, tag, v1, v2)
317 #define CopyField3(tag, v1, v2, v3) \
318 if (TIFFGetField(in, tag, &v1, &v2, &v3)) \
319 TIFFSetField(out, tag, v1, v2, v3)
320 #define CopyField4(tag, v1, v2, v3, v4) \
321 if (TIFFGetField(in, tag, &v1, &v2, &v3, &v4)) \
322 TIFFSetField(out, tag, v1, v2, v3, v4)
323
324 static void cpTag(TIFF *in, TIFF *out, uint16_t tag, uint16_t count,
325 TIFFDataType type)
326 {
327 switch (type)
328 {
329 case TIFF_SHORT:
330 if (count == 1)
331 {
332 uint16_t shortv;
333 CopyField(tag, shortv);
334 }
335 else if (count == 2)
336 {
337 uint16_t shortv1, shortv2;
338 CopyField2(tag, shortv1, shortv2);
339 }
340 else if (count == 4)
341 {
342 uint16_t *tr, *tg, *tb, *ta;
343 CopyField4(tag, tr, tg, tb, ta);
344 }
345 else if (count == (uint16_t)-1)
346 {
347 uint16_t shortv1;
348 uint16_t *shortav;
349 CopyField2(tag, shortv1, shortav);
350 }
351 break;
352 case TIFF_LONG:
353 {
354 uint32_t longv;
355 CopyField(tag, longv);
356 }
357 break;
358 case TIFF_RATIONAL:
359 if (count == 1)
360 {
361 float floatv;
362 CopyField(tag, floatv);
363 }
364 else if (count == (uint16_t)-1)
365 {
366 float *floatav;
367 CopyField(tag, floatav);
368 }
369 break;
370 case TIFF_ASCII:
371 {
372 char *stringv;
373 CopyField(tag, stringv);
374 }
375 break;
376 case TIFF_DOUBLE:
377 if (count == 1)
378 {
379 double doublev;
380 CopyField(tag, doublev);
381 }
382 else if (count == (uint16_t)-1)
383 {
384 double *doubleav;
385 CopyField(tag, doubleav);
386 }
387 break;
388 default:
389 TIFFError(TIFFFileName(in),
390 "Data type %d is not supported, tag %d skipped.", tag,
391 type);
392 }
393 }
394
395 #undef CopyField4
396 #undef CopyField3
397 #undef CopyField2
398 #undef CopyField
399
400 static const struct cpTag
401 {
402 uint16_t tag;
403 uint16_t count;
404 TIFFDataType type;
405 } tags[] = {
406 {TIFFTAG_IMAGEWIDTH, 1, TIFF_LONG},
407 {TIFFTAG_IMAGELENGTH, 1, TIFF_LONG},
408 {TIFFTAG_BITSPERSAMPLE, 1, TIFF_SHORT},
409 {TIFFTAG_COMPRESSION, 1, TIFF_SHORT},
410 {TIFFTAG_FILLORDER, 1, TIFF_SHORT},
411 {TIFFTAG_ROWSPERSTRIP, 1, TIFF_LONG},
412 {TIFFTAG_GROUP3OPTIONS, 1, TIFF_LONG},
413 {TIFFTAG_SUBFILETYPE, 1, TIFF_LONG},
414 {TIFFTAG_THRESHHOLDING, 1, TIFF_SHORT},
415 {TIFFTAG_DOCUMENTNAME, 1, TIFF_ASCII},
416 {TIFFTAG_IMAGEDESCRIPTION, 1, TIFF_ASCII},
417 {TIFFTAG_MAKE, 1, TIFF_ASCII},
418 {TIFFTAG_MODEL, 1, TIFF_ASCII},
419 {TIFFTAG_ORIENTATION, 1, TIFF_SHORT},
420 {TIFFTAG_MINSAMPLEVALUE, 1, TIFF_SHORT},
421 {TIFFTAG_MAXSAMPLEVALUE, 1, TIFF_SHORT},
422 {TIFFTAG_XRESOLUTION, 1, TIFF_RATIONAL},
423 {TIFFTAG_YRESOLUTION, 1, TIFF_RATIONAL},
424 {TIFFTAG_PAGENAME, 1, TIFF_ASCII},
425 {TIFFTAG_XPOSITION, 1, TIFF_RATIONAL},
426 {TIFFTAG_YPOSITION, 1, TIFF_RATIONAL},
427 {TIFFTAG_GROUP4OPTIONS, 1, TIFF_LONG},
428 {TIFFTAG_RESOLUTIONUNIT, 1, TIFF_SHORT},
429 {TIFFTAG_PAGENUMBER, 2, TIFF_SHORT},
430 {TIFFTAG_SOFTWARE, 1, TIFF_ASCII},
431 {TIFFTAG_DATETIME, 1, TIFF_ASCII},
432 {TIFFTAG_ARTIST, 1, TIFF_ASCII},
433 {TIFFTAG_HOSTCOMPUTER, 1, TIFF_ASCII},
434 {TIFFTAG_WHITEPOINT, 2, TIFF_RATIONAL},
435 {TIFFTAG_PRIMARYCHROMATICITIES, (uint16_t)-1, TIFF_RATIONAL},
436 {TIFFTAG_HALFTONEHINTS, 2, TIFF_SHORT},
437 {TIFFTAG_BADFAXLINES, 1, TIFF_LONG},
438 {TIFFTAG_CLEANFAXDATA, 1, TIFF_SHORT},
439 {TIFFTAG_CONSECUTIVEBADFAXLINES, 1, TIFF_LONG},
440 {TIFFTAG_INKSET, 1, TIFF_SHORT},
441 /*{ TIFFTAG_INKNAMES, 1, TIFF_ASCII },*/ /* Needs much
442 more
443 complicated
444 logic. See
445 tiffcp */
446 {TIFFTAG_DOTRANGE, 2, TIFF_SHORT},
447 {TIFFTAG_TARGETPRINTER, 1, TIFF_ASCII},
448 {TIFFTAG_SAMPLEFORMAT, 1, TIFF_SHORT},
449 {TIFFTAG_YCBCRCOEFFICIENTS, (uint16_t)-1, TIFF_RATIONAL},
450 {TIFFTAG_YCBCRSUBSAMPLING, 2, TIFF_SHORT},
451 {TIFFTAG_YCBCRPOSITIONING, 1, TIFF_SHORT},
452 {TIFFTAG_REFERENCEBLACKWHITE, (uint16_t)-1, TIFF_RATIONAL},
453 };
454 #define NTAGS (sizeof(tags) / sizeof(tags[0]))
455
456 static void cpTags(TIFF *in, TIFF *out)
457 {
458 const struct cpTag *p;
459 for (p = tags; p < &tags[NTAGS]; p++)
460 {
461 if (p->tag == TIFFTAG_GROUP3OPTIONS)
462 {
463 uint16_t compression;
464 if (!TIFFGetField(in, TIFFTAG_COMPRESSION, &compression) ||
465 compression != COMPRESSION_CCITTFAX3)
466 continue;
467 }
468 if (p->tag == TIFFTAG_GROUP4OPTIONS)
469 {
470 uint16_t compression;
471 if (!TIFFGetField(in, TIFFTAG_COMPRESSION, &compression) ||
472 compression != COMPRESSION_CCITTFAX4)
473 continue;
474 }
475 cpTag(in, out, p->tag, p->count, p->type);
476 }
477 }
478 #undef NTAGS
479
480 static const char usage_info[] =
481 "Convert a palette color TIFF image to a full color image\n\n"
482 "usage: pal2rgb [options] input.tif output.tif\n"
483 "where options are:\n"
484 " -p contig pack samples contiguously (e.g. RGBRGB...)\n"
485 " -p separate store samples separately (e.g. RRR...GGG...BBB...)\n"
486 " -r # make each strip have no more than # rows\n"
487 " -C 8 assume 8-bit colormap values (instead of 16-bit)\n"
488 " -C 16 assume 16-bit colormap values\n"
489 "\n"
490 #ifdef LZW_SUPPORT
491 " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding\n"
492 /* " LZW options:\n" */
493 " # set predictor value\n"
494 " For example, -c lzw:2 to get LZW-encoded data with horizontal "
495 "differencing\n"
496 #endif
497 #ifdef ZIP_SUPPORT
498 " -c zip[:opts] compress output with deflate encoding\n"
499 /* " Deflate (ZIP) options:\n" */
500 " # set predictor value\n"
501 #endif
502 #ifdef PACKBITS_SUPPORT
503 " -c packbits compress output with packbits encoding\n"
504 #endif
505 #if defined(LZW_SUPPORT) || defined(ZIP_SUPPORT) || defined(PACKBITS_SUPPORT)
506 " -c none use no compression algorithm on output\n"
507 #endif
508 ;
509
510 static void usage(int code)
511 {
512 FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr;
513
514 fprintf(out, "%s\n\n", TIFFGetVersion());
515 fprintf(out, "%s", usage_info);
516 exit(code);
517 }