1
/* $Header: /cvsroot/osrs/libtiff/tools/ppm2tiff.c,v 1.6 2003/05/05 10:40:04 dron Exp $ */
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
34
#if defined(_WINDOWS) || defined(MSDOS)
40
#define streq(a,b) (strcmp(a,b) == 0)
41
#define strneq(a,b,n) (strncmp(a,b,n) == 0)
43
static uint16 compression = COMPRESSION_PACKBITS;
44
static uint16 predictor = 0;
45
static int quality = 75; /* JPEG quality */
46
static int jpegcolormode = JPEGCOLORMODE_RGB;
48
static void usage(void);
49
static int processCompressOptions(char*);
54
fprintf(stderr, "%s: Not a PPM file.\n", file);
59
main(int argc, char* argv[])
62
uint32 rowsperstrip = (uint32) -1;
63
double resolution = -1;
64
unsigned char *buf = NULL;
78
fprintf(stderr, "%s: Too few arguments\n", argv[0]);
81
while ((c = getopt(argc, argv, "c:r:R:")) != -1)
83
case 'c': /* compression scheme */
84
if (!processCompressOptions(optarg))
87
case 'r': /* rows/strip */
88
rowsperstrip = atoi(optarg);
90
case 'R': /* resolution */
91
resolution = atof(optarg);
98
if ( optind + 2 < argc ) {
99
fprintf(stderr, "%s: Too many arguments\n", argv[0]);
104
* If only one file is specified, read input from
105
* stdin; otherwise usage is: ppm2tiff input output.
107
if (argc - optind > 1) {
108
infile = argv[optind++];
109
in = fopen(infile, "r" BINMODE);
111
fprintf(stderr, "%s: Can not open.\n", infile);
119
if (fgetc(in) != 'P')
122
case '5': /* it's a PGM file */
124
photometric = PHOTOMETRIC_MINISBLACK;
126
case '6': /* it's a PPM file */
128
photometric = PHOTOMETRIC_RGB;
129
if (compression == COMPRESSION_JPEG &&
130
jpegcolormode == JPEGCOLORMODE_RGB)
131
photometric = PHOTOMETRIC_YCBCR;
142
/* Skip whitespaces (blanks, TABs, CRs, LFs) */
143
if (strchr(" \t\r\n", c))
146
/* Check fo comment line */
150
} while(!strchr("\r\n", c) || feof(in));
157
if (fscanf(in, " %ld %ld %d", &w, &h, &prec) != 3)
159
if (fgetc(in) != '\n' || w <= 0 || h <= 0 || prec != 255)
162
out = TIFFOpen(argv[optind], "w");
165
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, w);
166
TIFFSetField(out, TIFFTAG_IMAGELENGTH, h);
167
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
168
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
169
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
170
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
171
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
172
TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
173
switch (compression) {
174
case COMPRESSION_JPEG:
175
TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
176
TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
178
case COMPRESSION_LZW:
179
case COMPRESSION_DEFLATE:
181
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
185
if (TIFFScanlineSize(out) > linebytes)
186
buf = (unsigned char *)_TIFFmalloc(linebytes);
188
buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
189
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
190
TIFFDefaultStripSize(out, rowsperstrip));
191
if (resolution > 0) {
192
TIFFSetField(out, TIFFTAG_XRESOLUTION, resolution);
193
TIFFSetField(out, TIFFTAG_YRESOLUTION, resolution);
194
TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
196
for (row = 0; row < h; row++) {
197
if (fread(buf, linebytes, 1, in) != 1) {
198
fprintf(stderr, "%s: scanline %lu: Read error.\n",
199
infile, (unsigned long) row);
202
if (TIFFWriteScanline(out, buf, row, 0) < 0)
205
(void) TIFFClose(out);
212
processCompressOptions(char* opt)
214
if (streq(opt, "none"))
215
compression = COMPRESSION_NONE;
216
else if (streq(opt, "packbits"))
217
compression = COMPRESSION_PACKBITS;
218
else if (strneq(opt, "jpeg", 4)) {
219
char* cp = strchr(opt, ':');
220
if (cp && isdigit(cp[1]))
221
quality = atoi(cp+1);
222
if (cp && strchr(cp, 'r'))
223
jpegcolormode = JPEGCOLORMODE_RAW;
224
compression = COMPRESSION_JPEG;
225
} else if (strneq(opt, "lzw", 3)) {
226
char* cp = strchr(opt, ':');
228
predictor = atoi(cp+1);
229
compression = COMPRESSION_LZW;
230
} else if (strneq(opt, "zip", 3)) {
231
char* cp = strchr(opt, ':');
233
predictor = atoi(cp+1);
234
compression = COMPRESSION_DEFLATE;
241
"usage: ppm2tiff [options] input.ppm output.tif",
242
"where options are:",
243
" -r # make each strip have no more than # rows",
244
" -R # set x&y resolution (dpi)",
246
" -c jpeg[:opts] compress output with JPEG encoding",
247
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
248
" -c zip[:opts] compress output with deflate encoding",
249
" -c packbits compress output with packbits encoding (the default)",
250
" -c none use no compression algorithm on output",
253
" # set compression quality level (0-100, default 75)",
254
" r output color image as RGB rather than YCbCr",
255
"LZW and deflate options:",
256
" # set predictor value",
257
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
268
fprintf(stderr, "%s\n\n", TIFFGetVersion());
269
for (i = 0; stuff[i] != NULL; i++)
270
fprintf(stderr, "%s\n", stuff[i]);