Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/tools/tiff2bw.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 #include "tiffiop.h"
39
40 #ifndef EXIT_SUCCESS
41 #define EXIT_SUCCESS 0
42 #endif
43 #ifndef EXIT_FAILURE
44 #define EXIT_FAILURE 1
45 #endif
46
47 /* x% weighting -> fraction of full color */
48 #define PCT(x) (((x)*256 + 50) / 100)
49 int RED = PCT(30); /* 30% */
50 int GREEN = PCT(59); /* 59% */
51 int BLUE = PCT(11); /* 11% */
52
53 static void usage(int code);
54 static int processCompressOptions(char *);
55
56 static void compresscontig(unsigned char *out, unsigned char *rgb, uint32_t n)
57 {
58 register int v, red = RED, green = GREEN, blue = BLUE;
59
60 while (n-- > 0)
61 {
62 v = red * (*rgb++);
63 v += green * (*rgb++);
64 v += blue * (*rgb++);
65 *out++ = v >> 8;
66 }
67 }
68
69 static void compresssep(unsigned char *out, unsigned char *r, unsigned char *g,
70 unsigned char *b, uint32_t n)
71 {
72 register uint32_t red = RED, green = GREEN, blue = BLUE;
73
74 while (n-- > 0)
75 *out++ =
76 (unsigned char)((red * (*r++) + green * (*g++) + blue * (*b++)) >>
77 8);
78 }
79
80 static int checkcmap(TIFF *tif, int n, uint16_t *r, uint16_t *g, uint16_t *b)
81 {
82 while (n-- > 0)
83 if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
84 return (16);
85 TIFFWarning(TIFFFileName(tif), "Assuming 8-bit colormap");
86 return (8);
87 }
88
89 static void compresspalette(unsigned char *out, unsigned char *data, uint32_t n,
90 uint16_t *rmap, uint16_t *gmap, uint16_t *bmap)
91 {
92 register int v, red = RED, green = GREEN, blue = BLUE;
93
94 while (n-- > 0)
95 {
96 unsigned int ix = *data++;
97 v = red * rmap[ix];
98 v += green * gmap[ix];
99 v += blue * bmap[ix];
100 *out++ = v >> 8;
101 }
102 }
103
104 static uint16_t compression = (uint16_t)-1;
105 static uint16_t predictor = 0;
106 static int jpegcolormode = JPEGCOLORMODE_RGB;
107 static int quality = 75; /* JPEG quality */
108
109 static void cpTags(TIFF *in, TIFF *out);
110
111 int main(int argc, char *argv[])
112 {
113 uint32_t rowsperstrip = (uint32_t)-1;
114 TIFF *in, *out;
115 uint32_t w, h;
116 uint16_t samplesperpixel;
117 uint16_t bitspersample;
118 uint16_t config;
119 uint16_t photometric;
120 uint16_t *red;
121 uint16_t *green;
122 uint16_t *blue;
123 tsize_t rowsize;
124 register uint32_t row;
125 register tsample_t s;
126 unsigned char *inbuf, *outbuf;
127 char thing[1024];
128 int c;
129 #if !HAVE_DECL_OPTARG
130 extern int optind;
131 extern char *optarg;
132 #endif
133
134 in = (TIFF *)NULL;
135 out = (TIFF *)NULL;
136 inbuf = (unsigned char *)NULL;
137 outbuf = (unsigned char *)NULL;
138
139 while ((c = getopt(argc, argv, "c:r:R:G:B:h")) != -1)
140 switch (c)
141 {
142 case 'c': /* compression scheme */
143 if (!processCompressOptions(optarg))
144 usage(EXIT_FAILURE);
145 break;
146 case 'r': /* rows/strip */
147 rowsperstrip = atoi(optarg);
148 break;
149 case 'R':
150 RED = PCT(atoi(optarg));
151 break;
152 case 'G':
153 GREEN = PCT(atoi(optarg));
154 break;
155 case 'B':
156 BLUE = PCT(atoi(optarg));
157 break;
158 case 'h':
159 usage(EXIT_SUCCESS);
160 /*NOTREACHED*/
161 break;
162 case '?':
163 usage(EXIT_FAILURE);
164 /*NOTREACHED*/
165 break;
166 }
167 if (argc - optind < 2)
168 usage(EXIT_FAILURE);
169 in = TIFFOpen(argv[optind], "r");
170 if (in == NULL)
171 return (EXIT_FAILURE);
172 photometric = 0;
173 TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photometric);
174 if (photometric != PHOTOMETRIC_RGB && photometric != PHOTOMETRIC_PALETTE)
175 {
176 fprintf(
177 stderr,
178 "%s: Bad photometric; can only handle RGB and Palette images.\n",
179 argv[optind]);
180 goto tiff2bw_error;
181 }
182 TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
183 if (samplesperpixel != 1 && samplesperpixel != 3)
184 {
185 fprintf(stderr, "%s: Bad samples/pixel %u.\n", argv[optind],
186 samplesperpixel);
187 goto tiff2bw_error;
188 }
189 if (photometric == PHOTOMETRIC_RGB && samplesperpixel != 3)
190 {
191 fprintf(stderr, "%s: Bad samples/pixel %u for PHOTOMETRIC_RGB.\n",
192 argv[optind], samplesperpixel);
193 goto tiff2bw_error;
194 }
195 TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample);
196 if (bitspersample != 8)
197 {
198 fprintf(stderr, " %s: Sorry, only handle 8-bit samples.\n",
199 argv[optind]);
200 goto tiff2bw_error;
201 }
202 TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &w);
203 TIFFGetField(in, TIFFTAG_IMAGELENGTH, &h);
204 TIFFGetField(in, TIFFTAG_PLANARCONFIG, &config);
205
206 out = TIFFOpen(argv[optind + 1], "w");
207 if (out == NULL)
208 {
209 goto tiff2bw_error;
210 }
211 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, w);
212 TIFFSetField(out, TIFFTAG_IMAGELENGTH, h);
213 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
214 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
215 TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
216 cpTags(in, out);
217 if (compression != (uint16_t)-1)
218 {
219 TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
220 switch (compression)
221 {
222 case COMPRESSION_JPEG:
223 TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
224 TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
225 break;
226 case COMPRESSION_LZW:
227 case COMPRESSION_ADOBE_DEFLATE:
228 case COMPRESSION_DEFLATE:
229 if (predictor != 0)
230 TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
231 break;
232 }
233 }
234 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
235 snprintf(thing, sizeof(thing), "B&W version of %s", argv[optind]);
236 TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, thing);
237 TIFFSetField(out, TIFFTAG_SOFTWARE, "tiff2bw");
238 outbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
239 if (!outbuf)
240 {
241 fprintf(stderr, "Out of memory\n");
242 goto tiff2bw_error;
243 }
244 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
245 TIFFDefaultStripSize(out, rowsperstrip));
246
247 #define pack(a, b) ((a) << 8 | (b))
248 switch (pack(photometric, config))
249 {
250 case pack(PHOTOMETRIC_PALETTE, PLANARCONFIG_CONTIG):
251 case pack(PHOTOMETRIC_PALETTE, PLANARCONFIG_SEPARATE):
252 TIFFGetField(in, TIFFTAG_COLORMAP, &red, &green, &blue);
253 /*
254 * Convert 16-bit colormap to 8-bit (unless it looks
255 * like an old-style 8-bit colormap).
256 */
257 if (checkcmap(in, 1 << bitspersample, red, green, blue) == 16)
258 {
259 int i;
260 #define CVT(x) (((x)*255L) / ((1L << 16) - 1))
261 for (i = (1 << bitspersample) - 1; i >= 0; i--)
262 {
263 red[i] = CVT(red[i]);
264 green[i] = CVT(green[i]);
265 blue[i] = CVT(blue[i]);
266 }
267 #undef CVT
268 }
269 inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
270 if (!inbuf)
271 {
272 fprintf(stderr, "Out of memory\n");
273 goto tiff2bw_error;
274 }
275 for (row = 0; row < h; row++)
276 {
277 if (TIFFReadScanline(in, inbuf, row, 0) < 0)
278 break;
279 compresspalette(outbuf, inbuf, w, red, green, blue);
280 if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
281 break;
282 }
283 break;
284 case pack(PHOTOMETRIC_RGB, PLANARCONFIG_CONTIG):
285 inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
286 if (!inbuf)
287 {
288 fprintf(stderr, "Out of memory\n");
289 goto tiff2bw_error;
290 }
291 for (row = 0; row < h; row++)
292 {
293 if (TIFFReadScanline(in, inbuf, row, 0) < 0)
294 break;
295 compresscontig(outbuf, inbuf, w);
296 if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
297 break;
298 }
299 break;
300 case pack(PHOTOMETRIC_RGB, PLANARCONFIG_SEPARATE):
301 {
302 tmsize_t inbufsize;
303 rowsize = TIFFScanlineSize(in);
304 inbufsize = TIFFSafeMultiply(tmsize_t, 3, rowsize);
305 inbuf = (unsigned char *)_TIFFmalloc(inbufsize);
306 if (!inbuf)
307 {
308 fprintf(stderr, "Out of memory\n");
309 goto tiff2bw_error;
310 }
311 for (row = 0; row < h; row++)
312 {
313 for (s = 0; s < 3; s++)
314 if (TIFFReadScanline(in, inbuf + s * rowsize, row, s) < 0)
315 goto tiff2bw_error;
316 compresssep(outbuf, inbuf, inbuf + rowsize, inbuf + 2 * rowsize,
317 w);
318 if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
319 break;
320 }
321 break;
322 }
323 }
324 #undef pack
325 if (inbuf)
326 _TIFFfree(inbuf);
327 if (outbuf)
328 _TIFFfree(outbuf);
329 TIFFClose(in);
330 TIFFClose(out);
331 return (EXIT_SUCCESS);
332
333 tiff2bw_error:
334 if (inbuf)
335 _TIFFfree(inbuf);
336 if (outbuf)
337 _TIFFfree(outbuf);
338 if (out)
339 TIFFClose(out);
340 if (in)
341 TIFFClose(in);
342 return (EXIT_FAILURE);
343 }
344
345 static int processCompressOptions(char *opt)
346 {
347 if (streq(opt, "none"))
348 compression = COMPRESSION_NONE;
349 else if (streq(opt, "packbits"))
350 compression = COMPRESSION_PACKBITS;
351 else if (strneq(opt, "jpeg", 4))
352 {
353 char *cp = strchr(opt, ':');
354
355 compression = COMPRESSION_JPEG;
356 while (cp)
357 {
358 if (isdigit((int)cp[1]))
359 quality = atoi(cp + 1);
360 else if (cp[1] == 'r')
361 jpegcolormode = JPEGCOLORMODE_RAW;
362 else
363 usage(EXIT_FAILURE);
364
365 cp = strchr(cp + 1, ':');
366 }
367 }
368 else if (strneq(opt, "lzw", 3))
369 {
370 char *cp = strchr(opt, ':');
371 if (cp)
372 predictor = atoi(cp + 1);
373 compression = COMPRESSION_LZW;
374 }
375 else if (strneq(opt, "zip", 3))
376 {
377 char *cp = strchr(opt, ':');
378 if (cp)
379 predictor = atoi(cp + 1);
380 compression = COMPRESSION_ADOBE_DEFLATE;
381 }
382 else
383 return (0);
384 return (1);
385 }
386
387 #define CopyField(tag, v) \
388 if (TIFFGetField(in, tag, &v)) \
389 TIFFSetField(out, tag, v)
390 #define CopyField2(tag, v1, v2) \
391 if (TIFFGetField(in, tag, &v1, &v2)) \
392 TIFFSetField(out, tag, v1, v2)
393 #define CopyField3(tag, v1, v2, v3) \
394 if (TIFFGetField(in, tag, &v1, &v2, &v3)) \
395 TIFFSetField(out, tag, v1, v2, v3)
396 #define CopyField4(tag, v1, v2, v3, v4) \
397 if (TIFFGetField(in, tag, &v1, &v2, &v3, &v4)) \
398 TIFFSetField(out, tag, v1, v2, v3, v4)
399
400 static void cpTag(TIFF *in, TIFF *out, uint16_t tag, uint16_t count,
401 TIFFDataType type)
402 {
403 switch (type)
404 {
405 case TIFF_SHORT:
406 if (count == 1)
407 {
408 uint16_t shortv;
409 CopyField(tag, shortv);
410 }
411 else if (count == 2)
412 {
413 uint16_t shortv1, shortv2;
414 CopyField2(tag, shortv1, shortv2);
415 }
416 else if (count == 4)
417 {
418 uint16_t *tr, *tg, *tb, *ta;
419 CopyField4(tag, tr, tg, tb, ta);
420 }
421 else if (count == (uint16_t)-1)
422 {
423 uint16_t shortv1;
424 uint16_t *shortav;
425 CopyField2(tag, shortv1, shortav);
426 }
427 break;
428 case TIFF_LONG:
429 {
430 uint32_t longv;
431 CopyField(tag, longv);
432 }
433 break;
434 case TIFF_RATIONAL:
435 if (count == 1)
436 {
437 float floatv;
438 CopyField(tag, floatv);
439 }
440 else if (count == (uint16_t)-1)
441 {
442 float *floatav;
443 CopyField(tag, floatav);
444 }
445 break;
446 case TIFF_ASCII:
447 {
448 char *stringv;
449 CopyField(tag, stringv);
450 }
451 break;
452 case TIFF_DOUBLE:
453 if (count == 1)
454 {
455 double doublev;
456 CopyField(tag, doublev);
457 }
458 else if (count == (uint16_t)-1)
459 {
460 double *doubleav;
461 CopyField(tag, doubleav);
462 }
463 break;
464 default:
465 TIFFError(TIFFFileName(in),
466 "Data type %d is not supported, tag %d skipped.", tag,
467 type);
468 }
469 }
470
471 #undef CopyField4
472 #undef CopyField3
473 #undef CopyField2
474 #undef CopyField
475
476 static const struct cpTag
477 {
478 uint16_t tag;
479 uint16_t count;
480 TIFFDataType type;
481 } tags[] = {
482 {TIFFTAG_SUBFILETYPE, 1, TIFF_LONG},
483 {TIFFTAG_THRESHHOLDING, 1, TIFF_SHORT},
484 {TIFFTAG_DOCUMENTNAME, 1, TIFF_ASCII},
485 {TIFFTAG_IMAGEDESCRIPTION, 1, TIFF_ASCII},
486 {TIFFTAG_MAKE, 1, TIFF_ASCII},
487 {TIFFTAG_MODEL, 1, TIFF_ASCII},
488 {TIFFTAG_MINSAMPLEVALUE, 1, TIFF_SHORT},
489 {TIFFTAG_MAXSAMPLEVALUE, 1, TIFF_SHORT},
490 {TIFFTAG_XRESOLUTION, 1, TIFF_RATIONAL},
491 {TIFFTAG_YRESOLUTION, 1, TIFF_RATIONAL},
492 {TIFFTAG_PAGENAME, 1, TIFF_ASCII},
493 {TIFFTAG_XPOSITION, 1, TIFF_RATIONAL},
494 {TIFFTAG_YPOSITION, 1, TIFF_RATIONAL},
495 {TIFFTAG_RESOLUTIONUNIT, 1, TIFF_SHORT},
496 {TIFFTAG_SOFTWARE, 1, TIFF_ASCII},
497 {TIFFTAG_DATETIME, 1, TIFF_ASCII},
498 {TIFFTAG_ARTIST, 1, TIFF_ASCII},
499 {TIFFTAG_HOSTCOMPUTER, 1, TIFF_ASCII},
500 {TIFFTAG_WHITEPOINT, 2, TIFF_RATIONAL},
501 {TIFFTAG_PRIMARYCHROMATICITIES, (uint16_t)-1, TIFF_RATIONAL},
502 {TIFFTAG_HALFTONEHINTS, 2, TIFF_SHORT},
503 {TIFFTAG_INKSET, 1, TIFF_SHORT},
504 {TIFFTAG_DOTRANGE, 2, TIFF_SHORT},
505 {TIFFTAG_TARGETPRINTER, 1, TIFF_ASCII},
506 {TIFFTAG_SAMPLEFORMAT, 1, TIFF_SHORT},
507 {TIFFTAG_YCBCRCOEFFICIENTS, (uint16_t)-1, TIFF_RATIONAL},
508 {TIFFTAG_YCBCRSUBSAMPLING, 2, TIFF_SHORT},
509 {TIFFTAG_YCBCRPOSITIONING, 1, TIFF_SHORT},
510 {TIFFTAG_REFERENCEBLACKWHITE, (uint16_t)-1, TIFF_RATIONAL},
511 {TIFFTAG_EXTRASAMPLES, (uint16_t)-1, TIFF_SHORT},
512 {TIFFTAG_SMINSAMPLEVALUE, 1, TIFF_DOUBLE},
513 {TIFFTAG_SMAXSAMPLEVALUE, 1, TIFF_DOUBLE},
514 {TIFFTAG_STONITS, 1, TIFF_DOUBLE},
515 };
516 #define NTAGS (sizeof(tags) / sizeof(tags[0]))
517
518 static void cpTags(TIFF *in, TIFF *out)
519 {
520 const struct cpTag *p;
521 for (p = tags; p < &tags[NTAGS]; p++)
522 {
523 if (p->tag == TIFFTAG_GROUP3OPTIONS)
524 {
525 uint16_t compression;
526 if (!TIFFGetField(in, TIFFTAG_COMPRESSION, &compression) ||
527 compression != COMPRESSION_CCITTFAX3)
528 continue;
529 }
530 if (p->tag == TIFFTAG_GROUP4OPTIONS)
531 {
532 uint16_t compression;
533 if (!TIFFGetField(in, TIFFTAG_COMPRESSION, &compression) ||
534 compression != COMPRESSION_CCITTFAX4)
535 continue;
536 }
537 cpTag(in, out, p->tag, p->count, p->type);
538 }
539 }
540 #undef NTAGS
541
542 static const char usage_info[] =
543 "Convert a color TIFF image to greyscale\n\n"
544 "usage: tiff2bw [options] input.tif output.tif\n"
545 "where options are:\n"
546 " -R % use #% from red channel\n"
547 " -G % use #% from green channel\n"
548 " -B % use #% from blue channel\n"
549 "\n"
550 " -r # make each strip have no more than # rows\n"
551 "\n"
552 #ifdef LZW_SUPPORT
553 " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding\n"
554 /* " LZW options:\n" */
555 " # set predictor value\n"
556 " For example, -c lzw:2 for LZW-encoded data with horizontal "
557 "differencing\n"
558 #endif
559 #ifdef ZIP_SUPPORT
560 " -c zip[:opts] compress output with deflate encoding\n"
561 /* " Deflate (ZIP) options:\n" */
562 " # set predictor value\n"
563 #endif
564 #ifdef PACKBITS_SUPPORT
565 " -c packbits compress output with packbits encoding\n"
566 #endif
567 #ifdef CCITT_SUPPORT
568 " -c g3[:opts] compress output with CCITT Group 3 encoding\n"
569 " -c g4 compress output with CCITT Group 4 encoding\n"
570 #endif
571 #if defined(LZW_SUPPORT) || defined(ZIP_SUPPORT) || \
572 defined(PACKBITS_SUPPORT) || defined(CCITT_SUPPORT)
573 " -c none use no compression algorithm on output\n"
574 #endif
575 "\n";
576
577 static void usage(int code)
578 {
579 FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr;
580
581 fprintf(out, "%s\n\n", TIFFGetVersion());
582 fprintf(out, "%s", usage_info);
583 exit(code);
584 }