]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/tools/ppm2tiff.c
4 * Copyright (c) 1991-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
27 #include "tif_config.h"
41 extern int getopt(int, char**, char*);
44 #if defined(_WINDOWS) || defined(MSDOS)
50 #define streq(a,b) (strcmp(a,b) == 0)
51 #define strneq(a,b,n) (strncmp(a,b,n) == 0)
53 static uint16 compression
= COMPRESSION_PACKBITS
;
54 static uint16 predictor
= 0;
55 static int quality
= 75; /* JPEG quality */
56 static int jpegcolormode
= JPEGCOLORMODE_RGB
;
59 static void usage(void);
60 static int processCompressOptions(char*);
65 fprintf(stderr
, "%s: Not a PPM file.\n", file
);
70 main(int argc
, char* argv
[])
72 uint16 photometric
= 0;
73 uint32 rowsperstrip
= (uint32
) -1;
74 double resolution
= -1;
75 unsigned char *buf
= NULL
;
76 tsize_t linebytes
= 0;
81 unsigned int w
, h
, prec
, row
;
88 fprintf(stderr
, "%s: Too few arguments\n", argv
[0]);
91 while ((c
= getopt(argc
, argv
, "c:r:R:")) != -1)
93 case 'c': /* compression scheme */
94 if (!processCompressOptions(optarg
))
97 case 'r': /* rows/strip */
98 rowsperstrip
= atoi(optarg
);
100 case 'R': /* resolution */
101 resolution
= atof(optarg
);
108 if (optind
+ 2 < argc
) {
109 fprintf(stderr
, "%s: Too many arguments\n", argv
[0]);
114 * If only one file is specified, read input from
115 * stdin; otherwise usage is: ppm2tiff input output.
117 if (argc
- optind
> 1) {
118 infile
= argv
[optind
++];
119 in
= fopen(infile
, "r" BINMODE
);
121 fprintf(stderr
, "%s: Can not open.\n", infile
);
129 if (fgetc(in
) != 'P')
132 case '4': /* it's a PBM file */
135 photometric
= PHOTOMETRIC_MINISWHITE
;
137 case '5': /* it's a PGM file */
140 photometric
= PHOTOMETRIC_MINISBLACK
;
142 case '6': /* it's a PPM file */
145 photometric
= PHOTOMETRIC_RGB
;
146 if (compression
== COMPRESSION_JPEG
&&
147 jpegcolormode
== JPEGCOLORMODE_RGB
)
148 photometric
= PHOTOMETRIC_YCBCR
;
159 /* Skip whitespaces (blanks, TABs, CRs, LFs) */
160 if (strchr(" \t\r\n", c
))
163 /* Check for comment line */
167 } while(!strchr("\r\n", c
) || feof(in
));
176 if (fscanf(in
, " %u %u", &w
, &h
) != 2)
178 if (fgetc(in
) != '\n')
182 if (fscanf(in
, " %u %u %u", &w
, &h
, &prec
) != 3)
184 if (fgetc(in
) != '\n' || prec
!= 255)
188 out
= TIFFOpen(argv
[optind
], "w");
191 TIFFSetField(out
, TIFFTAG_IMAGEWIDTH
, (uint32
) w
);
192 TIFFSetField(out
, TIFFTAG_IMAGELENGTH
, (uint32
) h
);
193 TIFFSetField(out
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
194 TIFFSetField(out
, TIFFTAG_SAMPLESPERPIXEL
, spp
);
195 TIFFSetField(out
, TIFFTAG_BITSPERSAMPLE
, bpp
);
196 TIFFSetField(out
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
);
197 TIFFSetField(out
, TIFFTAG_PHOTOMETRIC
, photometric
);
198 TIFFSetField(out
, TIFFTAG_COMPRESSION
, compression
);
199 switch (compression
) {
200 case COMPRESSION_JPEG
:
201 TIFFSetField(out
, TIFFTAG_JPEGQUALITY
, quality
);
202 TIFFSetField(out
, TIFFTAG_JPEGCOLORMODE
, jpegcolormode
);
204 case COMPRESSION_LZW
:
205 case COMPRESSION_DEFLATE
:
207 TIFFSetField(out
, TIFFTAG_PREDICTOR
, predictor
);
209 case COMPRESSION_CCITTFAX3
:
210 TIFFSetField(out
, TIFFTAG_GROUP3OPTIONS
, g3opts
);
215 linebytes
= (spp
* w
+ (8 - 1)) / 8;
216 if (rowsperstrip
== (uint32
) -1) {
217 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
, h
);
219 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
,
220 TIFFDefaultStripSize(out
, rowsperstrip
));
225 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
,
226 TIFFDefaultStripSize(out
, rowsperstrip
));
229 if (TIFFScanlineSize(out
) > linebytes
)
230 buf
= (unsigned char *)_TIFFmalloc(linebytes
);
232 buf
= (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out
));
233 if (resolution
> 0) {
234 TIFFSetField(out
, TIFFTAG_XRESOLUTION
, resolution
);
235 TIFFSetField(out
, TIFFTAG_YRESOLUTION
, resolution
);
236 TIFFSetField(out
, TIFFTAG_RESOLUTIONUNIT
, RESUNIT_INCH
);
238 for (row
= 0; row
< h
; row
++) {
239 if (fread(buf
, linebytes
, 1, in
) != 1) {
240 fprintf(stderr
, "%s: scanline %lu: Read error.\n",
241 infile
, (unsigned long) row
);
244 if (TIFFWriteScanline(out
, buf
, row
, 0) < 0)
247 (void) TIFFClose(out
);
254 processG3Options(char* cp
)
257 if( (cp
= strchr(cp
, ':')) ) {
260 if (strneq(cp
, "1d", 2))
261 g3opts
&= ~GROUP3OPT_2DENCODING
;
262 else if (strneq(cp
, "2d", 2))
263 g3opts
|= GROUP3OPT_2DENCODING
;
264 else if (strneq(cp
, "fill", 4))
265 g3opts
|= GROUP3OPT_FILLBITS
;
268 } while( (cp
= strchr(cp
, ':')) );
273 processCompressOptions(char* opt
)
275 if (streq(opt
, "none"))
276 compression
= COMPRESSION_NONE
;
277 else if (streq(opt
, "packbits"))
278 compression
= COMPRESSION_PACKBITS
;
279 else if (strneq(opt
, "jpeg", 4)) {
280 char* cp
= strchr(opt
, ':');
282 compression
= COMPRESSION_JPEG
;
285 if (isdigit((int)cp
[1]))
286 quality
= atoi(cp
+1);
287 else if (cp
[1] == 'r' )
288 jpegcolormode
= JPEGCOLORMODE_RAW
;
292 cp
= strchr(cp
+1,':');
294 } else if (strneq(opt
, "g3", 2)) {
295 processG3Options(opt
);
296 compression
= COMPRESSION_CCITTFAX3
;
297 } else if (streq(opt
, "g4")) {
298 compression
= COMPRESSION_CCITTFAX4
;
299 } else if (strneq(opt
, "lzw", 3)) {
300 char* cp
= strchr(opt
, ':');
302 predictor
= atoi(cp
+1);
303 compression
= COMPRESSION_LZW
;
304 } else if (strneq(opt
, "zip", 3)) {
305 char* cp
= strchr(opt
, ':');
307 predictor
= atoi(cp
+1);
308 compression
= COMPRESSION_DEFLATE
;
315 "usage: ppm2tiff [options] input.ppm output.tif",
316 "where options are:",
317 " -r # make each strip have no more than # rows",
318 " -R # set x&y resolution (dpi)",
320 " -c jpeg[:opts] compress output with JPEG encoding",
321 " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
322 " -c zip[:opts] compress output with deflate encoding",
323 " -c packbits compress output with packbits encoding (the default)",
324 " -c g3[:opts] compress output with CCITT Group 3 encoding",
325 " -c g4 compress output with CCITT Group 4 encoding",
326 " -c none use no compression algorithm on output",
329 " # set compression quality level (0-100, default 75)",
330 " r output color image as RGB rather than YCbCr",
331 "LZW and deflate options:",
332 " # set predictor value",
333 "For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
344 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
345 for (i
= 0; stuff
[i
] != NULL
; i
++)
346 fprintf(stderr
, "%s\n", stuff
[i
]);
350 /* vim: set ts=8 sts=8 sw=8 noet: */