1 /* $Id: sgi2tiff.c,v 1.6 2010-03-10 18:56:49 bfriesen 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
35 #define streq(a,b) (strcmp(a,b) == 0)
36 #define strneq(a,b,n) (strncmp(a,b,n) == 0)
38 static short config = PLANARCONFIG_CONTIG;
39 static uint16 compression = COMPRESSION_PACKBITS;
40 static uint16 predictor = 0;
41 static uint16 fillorder = 0;
42 static uint32 rowsperstrip = (uint32) -1;
43 static int jpegcolormode = JPEGCOLORMODE_RGB;
44 static int quality = 75; /* JPEG quality */
45 static uint16 photometric;
47 static void usage(void);
48 static int cpContig(IMAGE*, TIFF*);
49 static int cpSeparate(IMAGE*, TIFF*);
50 static int processCompressOptions(char*);
52 /* XXX image library has no prototypes */
53 extern IMAGE* iopen(const char*, const char*);
54 extern void iclose(IMAGE*);
55 extern void getrow(IMAGE*, short*, int, int);
58 main(int argc, char* argv[])
66 while ((c = getopt(argc, argv, "c:p:r:")) != -1)
68 case 'c': /* compression scheme */
69 if (!processCompressOptions(optarg))
72 case 'f': /* fill order */
73 if (streq(optarg, "lsb2msb"))
74 fillorder = FILLORDER_LSB2MSB;
75 else if (streq(optarg, "msb2lsb"))
76 fillorder = FILLORDER_MSB2LSB;
80 case 'p': /* planar configuration */
81 if (streq(optarg, "separate"))
82 config = PLANARCONFIG_SEPARATE;
83 else if (streq(optarg, "contig"))
84 config = PLANARCONFIG_CONTIG;
88 case 'r': /* rows/strip */
89 rowsperstrip = atoi(optarg);
95 if (argc - optind != 2)
97 in = iopen(argv[optind], "r");
100 out = TIFFOpen(argv[optind+1], "w");
103 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32) in->xsize);
104 TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32) in->ysize);
105 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
106 TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
108 photometric = PHOTOMETRIC_MINISBLACK;
110 photometric = PHOTOMETRIC_RGB;
111 switch (compression) {
112 case COMPRESSION_JPEG:
113 if (photometric == PHOTOMETRIC_RGB && jpegcolormode == JPEGCOLORMODE_RGB)
114 photometric = PHOTOMETRIC_YCBCR;
115 TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
116 TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
118 case COMPRESSION_LZW:
119 case COMPRESSION_DEFLATE:
121 TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
124 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
126 TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);
127 TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
128 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, in->zsize);
131 v[0] = EXTRASAMPLE_UNASSALPHA;
132 TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, v);
134 TIFFSetField(out, TIFFTAG_MINSAMPLEVALUE, (uint16) in->min);
135 TIFFSetField(out, TIFFTAG_MAXSAMPLEVALUE, (uint16) in->max);
136 TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
137 if (config != PLANARCONFIG_SEPARATE)
138 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
139 TIFFDefaultStripSize(out, rowsperstrip));
140 else /* force 1 row/strip for library limitation */
141 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 1L);
142 if (in->name[0] != '\0')
143 TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, in->name);
144 if (config == PLANARCONFIG_CONTIG)
149 (void) TIFFClose(out);
154 processCompressOptions(char* opt)
156 if (streq(opt, "none"))
157 compression = COMPRESSION_NONE;
158 else if (streq(opt, "packbits"))
159 compression = COMPRESSION_PACKBITS;
160 else if (strneq(opt, "jpeg", 4)) {
161 char* cp = strchr(opt, ':');
163 defcompression = COMPRESSION_JPEG;
166 if (isdigit((int)cp[1]))
167 quality = atoi(cp+1);
168 else if (cp[1] == 'r' )
169 jpegcolormode = JPEGCOLORMODE_RAW;
173 cp = strchr(cp+1,':');
175 } else if (strneq(opt, "lzw", 3)) {
176 char* cp = strchr(opt, ':');
178 predictor = atoi(cp+1);
179 compression = COMPRESSION_LZW;
180 } else if (strneq(opt, "zip", 3)) {
181 char* cp = strchr(opt, ':');
183 predictor = atoi(cp+1);
184 compression = COMPRESSION_DEFLATE;
191 cpContig(IMAGE* in, TIFF* out)
193 tdata_t buf = _TIFFmalloc(TIFFScanlineSize(out));
197 if (in->zsize == 3) {
200 r = (short *)_TIFFmalloc(3 * in->xsize * sizeof (short));
203 for (y = in->ysize-1; y >= 0; y--) {
204 uint8* pp = (uint8*) buf;
209 for (x = 0; x < in->xsize; x++) {
215 if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
218 } else if (in->zsize == 4) {
221 r = (short *)_TIFFmalloc(4 * in->xsize * sizeof (short));
225 for (y = in->ysize-1; y >= 0; y--) {
226 uint8* pp = (uint8*) buf;
232 for (x = 0; x < in->xsize; x++) {
239 if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
243 uint8* pp = (uint8*) buf;
245 r = (short *)_TIFFmalloc(in->xsize * sizeof (short));
246 for (y = in->ysize-1; y >= 0; y--) {
248 for (x = in->xsize-1; x >= 0; x--)
250 if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
266 cpSeparate(IMAGE* in, TIFF* out)
268 tdata_t buf = _TIFFmalloc(TIFFScanlineSize(out));
269 short *r = (short *)_TIFFmalloc(in->xsize * sizeof (short));
270 uint8* pp = (uint8*) buf;
273 for (z = 0; z < in->zsize; z++) {
274 for (y = in->ysize-1; y >= 0; y--) {
276 for (x = 0; x < in->xsize; x++)
278 if (TIFFWriteScanline(out, buf, in->ysize-y-1, z) < 0)
292 "usage: sgi2tiff [options] input.rgb output.tif",
293 "where options are:",
294 " -r # make each strip have no more than # rows",
296 " -p contig pack samples contiguously (e.g. RGBRGB...)",
297 " -p separate store samples separately (e.g. RRR...GGG...BBB...)",
299 " -f lsb2msb force lsb-to-msb FillOrder for output",
300 " -f msb2lsb force msb-to-lsb FillOrder for output",
302 " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
303 " -c zip[:opts] compress output with deflate encoding",
304 " -c jpeg[:opts]compress output with JPEG encoding",
305 " -c packbits compress output with packbits encoding",
306 " -c none use no compression algorithm on output",
309 " # set compression quality level (0-100, default 75)",
310 " r output color image as RGB rather than YCbCr",
312 "LZW and deflate options:",
313 " # set predictor value",
314 "For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
325 for (i = 0; stuff[i] != NULL; i++)
326 fprintf(stderr, "%s\n", stuff[i]);