]>
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"
53 extern int getopt(int, char**, char*);
56 #define streq(a,b) (strcmp(a,b) == 0)
57 #define strneq(a,b,n) (strncmp(a,b,n) == 0)
59 static uint16 compression
= COMPRESSION_PACKBITS
;
60 static uint16 predictor
= 0;
61 static int quality
= 75; /* JPEG quality */
62 static int jpegcolormode
= JPEGCOLORMODE_RGB
;
65 static void usage(void);
66 static int processCompressOptions(char*);
71 fprintf(stderr
, "%s: Not a PPM file.\n", file
);
76 main(int argc
, char* argv
[])
78 uint16 photometric
= 0;
79 uint32 rowsperstrip
= (uint32
) -1;
80 double resolution
= -1;
81 unsigned char *buf
= NULL
;
82 tsize_t linebytes
= 0;
87 unsigned int w
, h
, prec
, row
;
94 fprintf(stderr
, "%s: Too few arguments\n", argv
[0]);
97 while ((c
= getopt(argc
, argv
, "c:r:R:")) != -1)
99 case 'c': /* compression scheme */
100 if (!processCompressOptions(optarg
))
103 case 'r': /* rows/strip */
104 rowsperstrip
= atoi(optarg
);
106 case 'R': /* resolution */
107 resolution
= atof(optarg
);
114 if (optind
+ 2 < argc
) {
115 fprintf(stderr
, "%s: Too many arguments\n", argv
[0]);
120 * If only one file is specified, read input from
121 * stdin; otherwise usage is: ppm2tiff input output.
123 if (argc
- optind
> 1) {
124 infile
= argv
[optind
++];
125 in
= fopen(infile
, "rb");
127 fprintf(stderr
, "%s: Can not open.\n", infile
);
133 #if defined(HAVE_SETMODE) && defined(O_BINARY)
134 setmode(fileno(stdin
), O_BINARY
);
138 if (fgetc(in
) != 'P')
141 case '4': /* it's a PBM file */
144 photometric
= PHOTOMETRIC_MINISWHITE
;
146 case '5': /* it's a PGM file */
149 photometric
= PHOTOMETRIC_MINISBLACK
;
151 case '6': /* it's a PPM file */
154 photometric
= PHOTOMETRIC_RGB
;
155 if (compression
== COMPRESSION_JPEG
&&
156 jpegcolormode
== JPEGCOLORMODE_RGB
)
157 photometric
= PHOTOMETRIC_YCBCR
;
168 /* Skip whitespaces (blanks, TABs, CRs, LFs) */
169 if (strchr(" \t\r\n", c
))
172 /* Check for comment line */
176 } while(!(strchr("\r\n", c
) || feof(in
)));
185 if (fscanf(in
, " %u %u", &w
, &h
) != 2)
187 if (fgetc(in
) != '\n')
191 if (fscanf(in
, " %u %u %u", &w
, &h
, &prec
) != 3)
193 if (fgetc(in
) != '\n' || prec
!= 255)
197 out
= TIFFOpen(argv
[optind
], "w");
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
);
213 case COMPRESSION_LZW
:
214 case COMPRESSION_DEFLATE
:
216 TIFFSetField(out
, TIFFTAG_PREDICTOR
, predictor
);
218 case COMPRESSION_CCITTFAX3
:
219 TIFFSetField(out
, TIFFTAG_GROUP3OPTIONS
, g3opts
);
224 linebytes
= (spp
* w
+ (8 - 1)) / 8;
225 if (rowsperstrip
== (uint32
) -1) {
226 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
, h
);
228 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
,
229 TIFFDefaultStripSize(out
, rowsperstrip
));
234 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
,
235 TIFFDefaultStripSize(out
, rowsperstrip
));
238 if (TIFFScanlineSize(out
) > linebytes
)
239 buf
= (unsigned char *)_TIFFmalloc(linebytes
);
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
);
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
);
253 if (TIFFWriteScanline(out
, buf
, row
, 0) < 0)
256 (void) TIFFClose(out
);
263 processG3Options(char* cp
)
266 if( (cp
= strchr(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
;
277 } while( (cp
= strchr(cp
, ':')) );
282 processCompressOptions(char* opt
)
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
, ':');
291 compression
= COMPRESSION_JPEG
;
294 if (isdigit((int)cp
[1]))
295 quality
= atoi(cp
+1);
296 else if (cp
[1] == 'r' )
297 jpegcolormode
= JPEGCOLORMODE_RAW
;
301 cp
= strchr(cp
+1,':');
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
, ':');
311 predictor
= atoi(cp
+1);
312 compression
= COMPRESSION_LZW
;
313 } else if (strneq(opt
, "zip", 3)) {
314 char* cp
= strchr(opt
, ':');
316 predictor
= atoi(cp
+1);
317 compression
= COMPRESSION_DEFLATE
;
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)",
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",
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",
353 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
354 for (i
= 0; stuff
[i
] != NULL
; i
++)
355 fprintf(stderr
, "%s\n", stuff
[i
]);
359 /* vim: set ts=8 sts=8 sw=8 noet: */