]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/tools/ppm2tiff.c
new method for contentScaleFactor on main screen
[wxWidgets.git] / src / tiff / tools / ppm2tiff.c
1 /* $Id$ */
2
3 /*
4 * Copyright (c) 1991-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27 #include "tif_config.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37
38 #ifdef HAVE_FCNTL_H
39 # include <fcntl.h>
40 #endif
41
42 #ifdef HAVE_IO_H
43 # include <io.h>
44 #endif
45
46 #ifdef NEED_LIBPORT
47 # include "libport.h"
48 #endif
49
50 #include "tiffio.h"
51
52 #ifndef HAVE_GETOPT
53 extern int getopt(int, char**, char*);
54 #endif
55
56 #define streq(a,b) (strcmp(a,b) == 0)
57 #define strneq(a,b,n) (strncmp(a,b,n) == 0)
58
59 static uint16 compression = COMPRESSION_PACKBITS;
60 static uint16 predictor = 0;
61 static int quality = 75; /* JPEG quality */
62 static int jpegcolormode = JPEGCOLORMODE_RGB;
63 static uint32 g3opts;
64
65 static void usage(void);
66 static int processCompressOptions(char*);
67
68 static void
69 BadPPM(char* file)
70 {
71 fprintf(stderr, "%s: Not a PPM file.\n", file);
72 exit(-2);
73 }
74
75 int
76 main(int argc, char* argv[])
77 {
78 uint16 photometric = 0;
79 uint32 rowsperstrip = (uint32) -1;
80 double resolution = -1;
81 unsigned char *buf = NULL;
82 tsize_t linebytes = 0;
83 uint16 spp = 1;
84 uint16 bpp = 8;
85 TIFF *out;
86 FILE *in;
87 unsigned int w, h, prec, row;
88 char *infile;
89 int c;
90 extern int optind;
91 extern char* optarg;
92
93 if (argc < 2) {
94 fprintf(stderr, "%s: Too few arguments\n", argv[0]);
95 usage();
96 }
97 while ((c = getopt(argc, argv, "c:r:R:")) != -1)
98 switch (c) {
99 case 'c': /* compression scheme */
100 if (!processCompressOptions(optarg))
101 usage();
102 break;
103 case 'r': /* rows/strip */
104 rowsperstrip = atoi(optarg);
105 break;
106 case 'R': /* resolution */
107 resolution = atof(optarg);
108 break;
109 case '?':
110 usage();
111 /*NOTREACHED*/
112 }
113
114 if (optind + 2 < argc) {
115 fprintf(stderr, "%s: Too many arguments\n", argv[0]);
116 usage();
117 }
118
119 /*
120 * If only one file is specified, read input from
121 * stdin; otherwise usage is: ppm2tiff input output.
122 */
123 if (argc - optind > 1) {
124 infile = argv[optind++];
125 in = fopen(infile, "rb");
126 if (in == NULL) {
127 fprintf(stderr, "%s: Can not open.\n", infile);
128 return (-1);
129 }
130 } else {
131 infile = "<stdin>";
132 in = stdin;
133 #if defined(HAVE_SETMODE) && defined(O_BINARY)
134 setmode(fileno(stdin), O_BINARY);
135 #endif
136 }
137
138 if (fgetc(in) != 'P')
139 BadPPM(infile);
140 switch (fgetc(in)) {
141 case '4': /* it's a PBM file */
142 bpp = 1;
143 spp = 1;
144 photometric = PHOTOMETRIC_MINISWHITE;
145 break;
146 case '5': /* it's a PGM file */
147 bpp = 8;
148 spp = 1;
149 photometric = PHOTOMETRIC_MINISBLACK;
150 break;
151 case '6': /* it's a PPM file */
152 bpp = 8;
153 spp = 3;
154 photometric = PHOTOMETRIC_RGB;
155 if (compression == COMPRESSION_JPEG &&
156 jpegcolormode == JPEGCOLORMODE_RGB)
157 photometric = PHOTOMETRIC_YCBCR;
158 break;
159 default:
160 BadPPM(infile);
161 }
162
163 /* Parse header */
164 while(1) {
165 if (feof(in))
166 BadPPM(infile);
167 c = fgetc(in);
168 /* Skip whitespaces (blanks, TABs, CRs, LFs) */
169 if (strchr(" \t\r\n", c))
170 continue;
171
172 /* Check for comment line */
173 if (c == '#') {
174 do {
175 c = fgetc(in);
176 } while(!(strchr("\r\n", c) || feof(in)));
177 continue;
178 }
179
180 ungetc(c, in);
181 break;
182 }
183 switch (bpp) {
184 case 1:
185 if (fscanf(in, " %u %u", &w, &h) != 2)
186 BadPPM(infile);
187 if (fgetc(in) != '\n')
188 BadPPM(infile);
189 break;
190 case 8:
191 if (fscanf(in, " %u %u %u", &w, &h, &prec) != 3)
192 BadPPM(infile);
193 if (fgetc(in) != '\n' || prec != 255)
194 BadPPM(infile);
195 break;
196 }
197 out = TIFFOpen(argv[optind], "w");
198 if (out == NULL)
199 return (-4);
200 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) w);
201 TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) h);
202 TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
203 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
204 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bpp);
205 TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
206 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
207 TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
208 switch (compression) {
209 case COMPRESSION_JPEG:
210 TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
211 TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
212 break;
213 case COMPRESSION_LZW:
214 case COMPRESSION_DEFLATE:
215 if (predictor != 0)
216 TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
217 break;
218 case COMPRESSION_CCITTFAX3:
219 TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, g3opts);
220 break;
221 }
222 switch (bpp) {
223 case 1:
224 linebytes = (spp * w + (8 - 1)) / 8;
225 if (rowsperstrip == (uint32) -1) {
226 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, h);
227 } else {
228 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
229 TIFFDefaultStripSize(out, rowsperstrip));
230 }
231 break;
232 case 8:
233 linebytes = spp * w;
234 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
235 TIFFDefaultStripSize(out, rowsperstrip));
236 break;
237 }
238 if (TIFFScanlineSize(out) > linebytes)
239 buf = (unsigned char *)_TIFFmalloc(linebytes);
240 else
241 buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
242 if (resolution > 0) {
243 TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution);
244 TIFFSetField(out, TIFFTAG_YRESOLUTION, resolution);
245 TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
246 }
247 for (row = 0; row < h; row++) {
248 if (fread(buf, linebytes, 1, in) != 1) {
249 fprintf(stderr, "%s: scanline %lu: Read error.\n",
250 infile, (unsigned long) row);
251 break;
252 }
253 if (TIFFWriteScanline(out, buf, row, 0) < 0)
254 break;
255 }
256 (void) TIFFClose(out);
257 if (buf)
258 _TIFFfree(buf);
259 return (0);
260 }
261
262 static void
263 processG3Options(char* cp)
264 {
265 g3opts = 0;
266 if( (cp = strchr(cp, ':')) ) {
267 do {
268 cp++;
269 if (strneq(cp, "1d", 2))
270 g3opts &= ~GROUP3OPT_2DENCODING;
271 else if (strneq(cp, "2d", 2))
272 g3opts |= GROUP3OPT_2DENCODING;
273 else if (strneq(cp, "fill", 4))
274 g3opts |= GROUP3OPT_FILLBITS;
275 else
276 usage();
277 } while( (cp = strchr(cp, ':')) );
278 }
279 }
280
281 static int
282 processCompressOptions(char* opt)
283 {
284 if (streq(opt, "none"))
285 compression = COMPRESSION_NONE;
286 else if (streq(opt, "packbits"))
287 compression = COMPRESSION_PACKBITS;
288 else if (strneq(opt, "jpeg", 4)) {
289 char* cp = strchr(opt, ':');
290
291 compression = COMPRESSION_JPEG;
292 while (cp)
293 {
294 if (isdigit((int)cp[1]))
295 quality = atoi(cp+1);
296 else if (cp[1] == 'r' )
297 jpegcolormode = JPEGCOLORMODE_RAW;
298 else
299 usage();
300
301 cp = strchr(cp+1,':');
302 }
303 } else if (strneq(opt, "g3", 2)) {
304 processG3Options(opt);
305 compression = COMPRESSION_CCITTFAX3;
306 } else if (streq(opt, "g4")) {
307 compression = COMPRESSION_CCITTFAX4;
308 } else if (strneq(opt, "lzw", 3)) {
309 char* cp = strchr(opt, ':');
310 if (cp)
311 predictor = atoi(cp+1);
312 compression = COMPRESSION_LZW;
313 } else if (strneq(opt, "zip", 3)) {
314 char* cp = strchr(opt, ':');
315 if (cp)
316 predictor = atoi(cp+1);
317 compression = COMPRESSION_DEFLATE;
318 } else
319 return (0);
320 return (1);
321 }
322
323 char* stuff[] = {
324 "usage: ppm2tiff [options] input.ppm output.tif",
325 "where options are:",
326 " -r # make each strip have no more than # rows",
327 " -R # set x&y resolution (dpi)",
328 "",
329 " -c jpeg[:opts] compress output with JPEG encoding",
330 " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
331 " -c zip[:opts] compress output with deflate encoding",
332 " -c packbits compress output with packbits encoding (the default)",
333 " -c g3[:opts] compress output with CCITT Group 3 encoding",
334 " -c g4 compress output with CCITT Group 4 encoding",
335 " -c none use no compression algorithm on output",
336 "",
337 "JPEG options:",
338 " # set compression quality level (0-100, default 75)",
339 " r output color image as RGB rather than YCbCr",
340 "LZW and deflate options:",
341 " # set predictor value",
342 "For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
343 NULL
344 };
345
346 static void
347 usage(void)
348 {
349 char buf[BUFSIZ];
350 int i;
351
352 setbuf(stderr, buf);
353 fprintf(stderr, "%s\n\n", TIFFGetVersion());
354 for (i = 0; stuff[i] != NULL; i++)
355 fprintf(stderr, "%s\n", stuff[i]);
356 exit(-1);
357 }
358
359 /* vim: set ts=8 sts=8 sw=8 noet: */
360 /*
361 * Local Variables:
362 * mode: c
363 * c-basic-offset: 8
364 * fill-column: 78
365 * End:
366 */