Debian Codegraph @

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

raw
1 /*
2 * Copyright (c) 1991-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 #ifdef HAVE_FCNTL_H
38 #include <fcntl.h>
39 #endif
40
41 #ifdef HAVE_IO_H
42 #include <io.h>
43 #endif
44
45 #include "tiffio.h"
46
47 #ifndef EXIT_SUCCESS
48 #define EXIT_SUCCESS 0
49 #endif
50 #ifndef EXIT_FAILURE
51 #define EXIT_FAILURE 1
52 #endif
53
54 #define streq(a, b) (strcmp(a, b) == 0)
55 #define strneq(a, b, n) (strncmp(a, b, n) == 0)
56
57 static uint16_t compression = COMPRESSION_PACKBITS;
58 static uint16_t predictor = 0;
59 static int quality = 75; /* JPEG quality */
60 static int jpegcolormode = JPEGCOLORMODE_RGB;
61 static uint32_t g3opts;
62
63 static void usage(int code);
64 static int processCompressOptions(char *);
65
66 static void pack_none(unsigned char *buf, unsigned int smpls, uint16_t bps)
67 {
68 (void)buf;
69 (void)smpls;
70 (void)bps;
71 return;
72 }
73
74 static void pack_swab(unsigned char *buf, unsigned int smpls, uint16_t bps)
75 {
76 unsigned int s;
77 unsigned char h;
78 unsigned char l;
79 (void)bps;
80
81 for (s = 0; smpls > s; s++)
82 {
83
84 h = buf[s * 2 + 0];
85 l = buf[s * 2 + 1];
86
87 buf[s * 2 + 0] = l;
88 buf[s * 2 + 1] = h;
89 }
90 return;
91 }
92
93 static void pack_bytes(unsigned char *buf, unsigned int smpls, uint16_t bps)
94 {
95 unsigned int s;
96 unsigned int in;
97 unsigned int out;
98 int bits;
99 uint16_t t;
100
101 in = 0;
102 out = 0;
103 bits = 0;
104 t = 0;
105
106 for (s = 0; smpls > s; s++)
107 {
108
109 t <<= bps;
110 t |= (uint16_t)buf[in++];
111
112 bits += bps;
113
114 if (8 <= bits)
115 {
116 bits -= 8;
117 buf[out++] = (t >> bits) & 0xFF;
118 }
119 }
120 if (0 != bits)
121 buf[out] = (t << (8 - bits)) & 0xFF;
122 }
123
124 static void pack_words(unsigned char *buf, unsigned int smpls, uint16_t bps)
125 {
126 unsigned int s;
127 unsigned int in;
128 unsigned int out;
129 int bits;
130 uint32_t t;
131
132 in = 0;
133 out = 0;
134 bits = 0;
135 t = 0;
136
137 for (s = 0; smpls > s; s++)
138 {
139
140 t <<= bps;
141 t |= (uint32_t)buf[in++] << 8;
142 t |= (uint32_t)buf[in++] << 0;
143
144 bits += bps;
145
146 if (16 <= bits)
147 {
148
149 bits -= 16;
150 buf[out++] = (t >> (bits + 8));
151 buf[out++] = (t >> (bits + 0));
152 }
153 }
154 if (0 != bits)
155 {
156 t <<= 16 - bits;
157
158 buf[out++] = (t >> (0 + 8));
159 buf[out++] = (t >> (0 + 0));
160 }
161 }
162
163 static void BadPPM(char *file)
164 {
165 fprintf(stderr, "%s: Not a PPM file.\n", file);
166 exit(EXIT_FAILURE);
167 }
168
169 static tmsize_t multiply_ms(tmsize_t m1, tmsize_t m2)
170 {
171 if (m1 == 0 || m2 > TIFF_TMSIZE_T_MAX / m1)
172 return 0;
173 return m1 * m2;
174 }
175
176 int main(int argc, char *argv[])
177 {
178 uint16_t photometric = 0;
179 uint32_t rowsperstrip = (uint32_t)-1;
180 double resolution = -1;
181 unsigned char *buf = NULL;
182 tmsize_t linebytes = 0;
183 int pbm;
184 uint16_t spp = 1;
185 uint16_t bpp = 8;
186 void (*pack_func)(unsigned char *buf, unsigned int smpls, uint16_t bps);
187 TIFF *out;
188 FILE *in;
189 unsigned int w, h, prec, row;
190 char *infile;
191 int c;
192 #if !HAVE_DECL_OPTARG
193 extern int optind;
194 extern char *optarg;
195 #endif
196 tmsize_t scanline_size;
197
198 if (argc < 2)
199 {
200 fprintf(stderr, "%s: Too few arguments\n", argv[0]);
201 usage(EXIT_FAILURE);
202 }
203 while ((c = getopt(argc, argv, "c:r:R:h")) != -1)
204 switch (c)
205 {
206 case 'c': /* compression scheme */
207 if (!processCompressOptions(optarg))
208 usage(EXIT_FAILURE);
209 break;
210 case 'r': /* rows/strip */
211 rowsperstrip = atoi(optarg);
212 break;
213 case 'R': /* resolution */
214 resolution = atof(optarg);
215 break;
216 case 'h':
217 usage(EXIT_SUCCESS);
218 break;
219 case '?':
220 usage(EXIT_FAILURE);
221 /*NOTREACHED*/
222 }
223
224 if (optind + 2 < argc)
225 {
226 fprintf(stderr, "%s: Too many arguments\n", argv[0]);
227 usage(EXIT_FAILURE);
228 }
229
230 /*
231 * If only one file is specified, read input from
232 * stdin; otherwise usage is: ppm2tiff input output.
233 */
234 if (argc - optind > 1)
235 {
236 infile = argv[optind++];
237 in = fopen(infile, "rb");
238 if (in == NULL)
239 {
240 fprintf(stderr, "%s: Can not open.\n", infile);
241 return (EXIT_FAILURE);
242 }
243 }
244 else
245 {
246 infile = "<stdin>";
247 in = stdin;
248 #if defined(HAVE_SETMODE) && defined(O_BINARY)
249 setmode(fileno(stdin), O_BINARY);
250 #endif
251 }
252
253 if (fgetc(in) != 'P')
254 BadPPM(infile);
255 switch (fgetc(in))
256 {
257 case '4': /* it's a PBM file */
258 pbm = !0;
259 spp = 1;
260 photometric = PHOTOMETRIC_MINISWHITE;
261 break;
262 case '5': /* it's a PGM file */
263 pbm = 0;
264 spp = 1;
265 photometric = PHOTOMETRIC_MINISBLACK;
266 break;
267 case '6': /* it's a PPM file */
268 pbm = 0;
269 spp = 3;
270 photometric = PHOTOMETRIC_RGB;
271 if (compression == COMPRESSION_JPEG &&
272 jpegcolormode == JPEGCOLORMODE_RGB)
273 photometric = PHOTOMETRIC_YCBCR;
274 break;
275 default:
276 BadPPM(infile);
277 }
278
279 /* Parse header */
280 while (1)
281 {
282 if (feof(in))
283 BadPPM(infile);
284 c = fgetc(in);
285 /* Skip whitespaces (blanks, TABs, CRs, LFs) */
286 if (strchr(" \t\r\n", c))
287 continue;
288
289 /* Check for comment line */
290 if (c == '#')
291 {
292 do
293 {
294 c = fgetc(in);
295 } while (!(strchr("\r\n", c) || feof(in)));
296 continue;
297 }
298
299 ungetc(c, in);
300 break;
301 }
302 if (pbm)
303 {
304 if (fscanf(in, " %u %u", &w, &h) != 2)
305 BadPPM(infile);
306 if (fgetc(in) != '\n')
307 BadPPM(infile);
308 bpp = 1;
309 pack_func = pack_none;
310 }
311 else
312 {
313 if (fscanf(in, " %u %u %u", &w, &h, &prec) != 3)
314 BadPPM(infile);
315 if (fgetc(in) != '\n' || 0 == prec || 65535 < prec)
316 BadPPM(infile);
317
318 if (0 != (prec & (prec + 1)))
319 {
320 fprintf(stderr, "%s: unsupported maxval %u.\n", infile, prec);
321 exit(EXIT_FAILURE);
322 }
323 bpp = 0;
324 if ((prec + 1) & 0xAAAAAAAA)
325 bpp |= 1;
326 if ((prec + 1) & 0xCCCCCCCC)
327 bpp |= 2;
328 if ((prec + 1) & 0xF0F0F0F0)
329 bpp |= 4;
330 if ((prec + 1) & 0xFF00FF00)
331 bpp |= 8;
332 if ((prec + 1) & 0xFFFF0000)
333 bpp |= 16;
334
335 switch (bpp)
336 {
337 case 8:
338 pack_func = pack_none;
339 break;
340 case 16:
341 {
342 const unsigned short i = 0x0100;
343
344 if (0 == *(unsigned char *)&i)
345 pack_func = pack_swab;
346 else
347 pack_func = pack_none;
348 }
349 break;
350 default:
351 if (8 >= bpp)
352 pack_func = pack_bytes;
353 else
354 pack_func = pack_words;
355 break;
356 }
357 }
358 out = TIFFOpen(argv[optind], "w");
359 if (out == NULL)
360 return (EXIT_FAILURE);
361 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32_t)w);
362 TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32_t)h);
363 TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
364 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
365 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bpp);
366 TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
367 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
368 TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
369 switch (compression)
370 {
371 case COMPRESSION_JPEG:
372 TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
373 TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
374 break;
375 case COMPRESSION_LZW:
376 case COMPRESSION_ADOBE_DEFLATE:
377 case COMPRESSION_DEFLATE:
378 if (predictor != 0)
379 TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
380 break;
381 case COMPRESSION_CCITTFAX3:
382 TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, g3opts);
383 break;
384 }
385 if (pbm)
386 {
387 /* if round-up overflows, result will be zero, OK */
388 linebytes = (multiply_ms(spp, w) + (8 - 1)) / 8;
389 }
390 else if (bpp <= 8)
391 {
392 linebytes = multiply_ms(spp, w);
393 }
394 else
395 {
396 linebytes = multiply_ms(2 * spp, w);
397 }
398 if (rowsperstrip == (uint32_t)-1)
399 {
400 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, h);
401 }
402 else
403 {
404 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
405 TIFFDefaultStripSize(out, rowsperstrip));
406 }
407 if (linebytes == 0)
408 {
409 fprintf(stderr, "%s: scanline size overflow\n", infile);
410 (void)TIFFClose(out);
411 exit(EXIT_FAILURE);
412 }
413 scanline_size = TIFFScanlineSize(out);
414 if (scanline_size == 0)
415 {
416 /* overflow - TIFFScanlineSize already printed a message */
417 (void)TIFFClose(out);
418 exit(EXIT_FAILURE);
419 }
420 if (scanline_size < linebytes)
421 buf = (unsigned char *)_TIFFmalloc(linebytes);
422 else
423 buf = (unsigned char *)_TIFFmalloc(scanline_size);
424 if (buf == NULL)
425 {
426 fprintf(stderr, "%s: Not enough memory\n", infile);
427 (void)TIFFClose(out);
428 exit(EXIT_FAILURE);
429 }
430 if (resolution > 0)
431 {
432 TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution);
433 TIFFSetField(out, TIFFTAG_YRESOLUTION, resolution);
434 TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
435 }
436 for (row = 0; row < h; row++)
437 {
438 if (fread(buf, linebytes, 1, in) != 1)
439 {
440 fprintf(stderr, "%s: scanline %u: Read error.\n", infile, row);
441 break;
442 }
443 pack_func(buf, w * spp, bpp);
444 if (TIFFWriteScanline(out, buf, row, 0) < 0)
445 break;
446 }
447 if (in != stdin)
448 fclose(in);
449 (void)TIFFClose(out);
450 if (buf)
451 _TIFFfree(buf);
452 return (EXIT_SUCCESS);
453 }
454
455 static void processG3Options(char *cp)
456 {
457 g3opts = 0;
458 if ((cp = strchr(cp, ':')))
459 {
460 do
461 {
462 cp++;
463 if (strneq(cp, "1d", 2))
464 g3opts &= ~GROUP3OPT_2DENCODING;
465 else if (strneq(cp, "2d", 2))
466 g3opts |= GROUP3OPT_2DENCODING;
467 else if (strneq(cp, "fill", 4))
468 g3opts |= GROUP3OPT_FILLBITS;
469 else
470 usage(EXIT_FAILURE);
471 } while ((cp = strchr(cp, ':')));
472 }
473 }
474
475 static int processCompressOptions(char *opt)
476 {
477 if (streq(opt, "none"))
478 compression = COMPRESSION_NONE;
479 else if (streq(opt, "packbits"))
480 compression = COMPRESSION_PACKBITS;
481 else if (strneq(opt, "jpeg", 4))
482 {
483 char *cp = strchr(opt, ':');
484
485 compression = COMPRESSION_JPEG;
486 while (cp)
487 {
488 if (isdigit((int)cp[1]))
489 quality = atoi(cp + 1);
490 else if (cp[1] == 'r')
491 jpegcolormode = JPEGCOLORMODE_RAW;
492 else
493 usage(EXIT_FAILURE);
494
495 cp = strchr(cp + 1, ':');
496 }
497 }
498 else if (strneq(opt, "g3", 2))
499 {
500 processG3Options(opt);
501 compression = COMPRESSION_CCITTFAX3;
502 }
503 else if (streq(opt, "g4"))
504 {
505 compression = COMPRESSION_CCITTFAX4;
506 }
507 else if (strneq(opt, "lzw", 3))
508 {
509 char *cp = strchr(opt, ':');
510 if (cp)
511 predictor = atoi(cp + 1);
512 compression = COMPRESSION_LZW;
513 }
514 else if (strneq(opt, "zip", 3))
515 {
516 char *cp = strchr(opt, ':');
517 if (cp)
518 predictor = atoi(cp + 1);
519 compression = COMPRESSION_ADOBE_DEFLATE;
520 }
521 else
522 return (0);
523 return (1);
524 }
525
526 static const char usage_info[] =
527 "Create a TIFF file from PPM, PGM and PBM image files\n\n"
528 "usage: ppm2tiff [options] input.ppm output.tif\n"
529 "where options are:\n"
530 " -r # make each strip have no more than # rows\n"
531 " -R # set x&y resolution (dpi)\n"
532 "\n"
533 #ifdef JPEG_SUPPORT
534 " -c jpeg[:opts] compress output with JPEG encoding\n"
535 /* "JPEG options:\n" */
536 " # set compression quality level (0-100, default 75)\n"
537 " r output color image as RGB rather than YCbCr\n"
538 #endif
539 #ifdef LZW_SUPPORT
540 " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding\n"
541 /* " LZW options:\n" */
542 " # set predictor value\n"
543 " For example, -c lzw:2 for LZW-encoded data with horizontal "
544 "differencing\n"
545 #endif
546 #ifdef ZIP_SUPPORT
547 " -c zip[:opts] compress output with deflate encoding\n"
548 /* " Deflate (ZIP) options:\n" */
549 " # set predictor value\n"
550 #endif
551 #ifdef PACKBITS_SUPPORT
552 " -c packbits compress output with packbits encoding (the default)\n"
553 #endif
554 #ifdef CCITT_SUPPORT
555 " -c g3[:opts] compress output with CCITT Group 3 encoding\n"
556 " -c g4 compress output with CCITT Group 4 encoding\n"
557 #endif
558 #if defined(JPEG_SUPPORT) || defined(LZW_SUPPORT) || defined(ZIP_SUPPORT) || \
559 defined(PACKBITS_SUPPORT) || defined(CCITT_SUPPORT)
560 " -c none use no compression algorithm on output\n"
561 #endif
562 ;
563
564 static void usage(int code)
565 {
566 FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr;
567
568 fprintf(out, "%s\n\n", TIFFGetVersion());
569 fprintf(out, "%s", usage_info);
570 exit(code);
571 }