]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/tools/ppm2tiff.c
bc6ee963c48a48d6e52b87263ac49c2adcd716ea
3 * Copyright (c) 1991-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
26 #include "tif_config.h"
52 extern int getopt(int, char**, char*);
55 #define streq(a,b) (strcmp(a,b) == 0)
56 #define strneq(a,b,n) (strncmp(a,b,n) == 0)
58 static uint16 compression
= COMPRESSION_PACKBITS
;
59 static uint16 predictor
= 0;
60 static int quality
= 75; /* JPEG quality */
61 static int jpegcolormode
= JPEGCOLORMODE_RGB
;
64 static void usage(void);
65 static int processCompressOptions(char*);
70 fprintf(stderr
, "%s: Not a PPM file.\n", file
);
75 main(int argc
, char* argv
[])
77 uint16 photometric
= 0;
78 uint32 rowsperstrip
= (uint32
) -1;
79 double resolution
= -1;
80 unsigned char *buf
= NULL
;
81 tsize_t linebytes
= 0;
86 unsigned int w
, h
, prec
, row
;
93 fprintf(stderr
, "%s: Too few arguments\n", argv
[0]);
96 while ((c
= getopt(argc
, argv
, "c:r:R:")) != -1)
98 case 'c': /* compression scheme */
99 if (!processCompressOptions(optarg
))
102 case 'r': /* rows/strip */
103 rowsperstrip
= atoi(optarg
);
105 case 'R': /* resolution */
106 resolution
= atof(optarg
);
113 if (optind
+ 2 < argc
) {
114 fprintf(stderr
, "%s: Too many arguments\n", argv
[0]);
119 * If only one file is specified, read input from
120 * stdin; otherwise usage is: ppm2tiff input output.
122 if (argc
- optind
> 1) {
123 infile
= argv
[optind
++];
124 in
= fopen(infile
, "rb");
126 fprintf(stderr
, "%s: Can not open.\n", infile
);
132 #if defined(HAVE_SETMODE) && defined(O_BINARY)
133 setmode(fileno(stdin
), O_BINARY
);
137 if (fgetc(in
) != 'P')
140 case '4': /* it's a PBM file */
143 photometric
= PHOTOMETRIC_MINISWHITE
;
145 case '5': /* it's a PGM file */
148 photometric
= PHOTOMETRIC_MINISBLACK
;
150 case '6': /* it's a PPM file */
153 photometric
= PHOTOMETRIC_RGB
;
154 if (compression
== COMPRESSION_JPEG
&&
155 jpegcolormode
== JPEGCOLORMODE_RGB
)
156 photometric
= PHOTOMETRIC_YCBCR
;
167 /* Skip whitespaces (blanks, TABs, CRs, LFs) */
168 if (strchr(" \t\r\n", c
))
171 /* Check for comment line */
175 } while(!(strchr("\r\n", c
) || feof(in
)));
184 if (fscanf(in
, " %u %u", &w
, &h
) != 2)
186 if (fgetc(in
) != '\n')
190 if (fscanf(in
, " %u %u %u", &w
, &h
, &prec
) != 3)
192 if (fgetc(in
) != '\n' || prec
!= 255)
196 out
= TIFFOpen(argv
[optind
], "w");
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
);
212 case COMPRESSION_LZW
:
213 case COMPRESSION_DEFLATE
:
215 TIFFSetField(out
, TIFFTAG_PREDICTOR
, predictor
);
217 case COMPRESSION_CCITTFAX3
:
218 TIFFSetField(out
, TIFFTAG_GROUP3OPTIONS
, g3opts
);
223 linebytes
= (spp
* w
+ (8 - 1)) / 8;
224 if (rowsperstrip
== (uint32
) -1) {
225 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
, h
);
227 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
,
228 TIFFDefaultStripSize(out
, rowsperstrip
));
233 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
,
234 TIFFDefaultStripSize(out
, rowsperstrip
));
237 if (TIFFScanlineSize(out
) > linebytes
)
238 buf
= (unsigned char *)_TIFFmalloc(linebytes
);
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
);
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
);
252 if (TIFFWriteScanline(out
, buf
, row
, 0) < 0)
255 (void) TIFFClose(out
);
262 processG3Options(char* cp
)
265 if( (cp
= strchr(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
;
276 } while( (cp
= strchr(cp
, ':')) );
281 processCompressOptions(char* opt
)
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
, ':');
290 compression
= COMPRESSION_JPEG
;
293 if (isdigit((int)cp
[1]))
294 quality
= atoi(cp
+1);
295 else if (cp
[1] == 'r' )
296 jpegcolormode
= JPEGCOLORMODE_RAW
;
300 cp
= strchr(cp
+1,':');
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
, ':');
310 predictor
= atoi(cp
+1);
311 compression
= COMPRESSION_LZW
;
312 } else if (strneq(opt
, "zip", 3)) {
313 char* cp
= strchr(opt
, ':');
315 predictor
= atoi(cp
+1);
316 compression
= COMPRESSION_DEFLATE
;
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)",
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",
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",
352 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
353 for (i
= 0; stuff
[i
] != NULL
; i
++)
354 fprintf(stderr
, "%s\n", stuff
[i
]);
358 /* vim: set ts=8 sts=8 sw=8 noet: */