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