1
/* $Header: /cvsroot/osrs/libtiff/tools/sgi2tiff.c,v 1.3 1999/12/27 17:35:01 mwelles 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, ':');
162
if (cp && isdigit(cp[1]))
163
quality = atoi(cp+1);
164
if (cp && strchr(cp, 'r'))
165
jpegcolormode = JPEGCOLORMODE_RAW;
166
compression = COMPRESSION_JPEG;
167
} else if (strneq(opt, "lzw", 3)) {
168
char* cp = strchr(opt, ':');
170
predictor = atoi(cp+1);
171
compression = COMPRESSION_LZW;
172
} else if (strneq(opt, "zip", 3)) {
173
char* cp = strchr(opt, ':');
175
predictor = atoi(cp+1);
176
compression = COMPRESSION_DEFLATE;
183
cpContig(IMAGE* in, TIFF* out)
185
tdata_t buf = _TIFFmalloc(TIFFScanlineSize(out));
189
if (in->zsize == 3) {
192
r = (short *)_TIFFmalloc(3 * in->xsize * sizeof (short));
195
for (y = in->ysize-1; y >= 0; y--) {
196
uint8* pp = (uint8*) buf;
201
for (x = 0; x < in->xsize; x++) {
207
if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
210
} else if (in->zsize == 4) {
213
r = (short *)_TIFFmalloc(4 * in->xsize * sizeof (short));
217
for (y = in->ysize-1; y >= 0; y--) {
218
uint8* pp = (uint8*) buf;
224
for (x = 0; x < in->xsize; x++) {
231
if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
235
uint8* pp = (uint8*) buf;
237
r = (short *)_TIFFmalloc(in->xsize * sizeof (short));
238
for (y = in->ysize-1; y >= 0; y--) {
240
for (x = in->xsize-1; x >= 0; x--)
242
if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
258
cpSeparate(IMAGE* in, TIFF* out)
260
tdata_t buf = _TIFFmalloc(TIFFScanlineSize(out));
261
short *r = (short *)_TIFFmalloc(in->xsize * sizeof (short));
262
uint8* pp = (uint8*) buf;
265
for (z = 0; z < in->zsize; z++) {
266
for (y = in->ysize-1; y >= 0; y--) {
268
for (x = 0; x < in->xsize; x++)
270
if (TIFFWriteScanline(out, buf, in->ysize-y-1, z) < 0)
284
"usage: sgi2tiff [options] input.rgb output.tif",
285
"where options are:",
286
" -r # make each strip have no more than # rows",
288
" -p contig pack samples contiguously (e.g. RGBRGB...)",
289
" -p separate store samples separately (e.g. RRR...GGG...BBB...)",
291
" -f lsb2msb force lsb-to-msb FillOrder for output",
292
" -f msb2lsb force msb-to-lsb FillOrder for output",
294
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
295
" -c zip[:opts] compress output with deflate encoding",
296
" -c jpeg[:opts]compress output with JPEG encoding",
297
" -c packbits compress output with packbits encoding",
298
" -c none use no compression algorithm on output",
301
" # set compression quality level (0-100, default 75)",
302
" r output color image as RGB rather than YCbCr",
304
"LZW and deflate options:",
305
" # set predictor value",
306
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
317
for (i = 0; stuff[i] != NULL; i++)
318
fprintf(stderr, "%s\n", stuff[i]);