]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/tools/sgi2tiff.c
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
34 #define streq(a,b) (strcmp(a,b) == 0)
35 #define strneq(a,b,n) (strncmp(a,b,n) == 0)
37 static short config
= PLANARCONFIG_CONTIG
;
38 static uint16 compression
= COMPRESSION_PACKBITS
;
39 static uint16 predictor
= 0;
40 static uint16 fillorder
= 0;
41 static uint32 rowsperstrip
= (uint32
) -1;
42 static int jpegcolormode
= JPEGCOLORMODE_RGB
;
43 static int quality
= 75; /* JPEG quality */
44 static uint16 photometric
;
46 static void usage(void);
47 static int cpContig(IMAGE
*, TIFF
*);
48 static int cpSeparate(IMAGE
*, TIFF
*);
49 static int processCompressOptions(char*);
51 /* XXX image library has no prototypes */
52 extern IMAGE
* iopen(const char*, const char*);
53 extern void iclose(IMAGE
*);
54 extern void getrow(IMAGE
*, short*, int, int);
57 main(int argc
, char* argv
[])
65 while ((c
= getopt(argc
, argv
, "c:p:r:")) != -1)
67 case 'c': /* compression scheme */
68 if (!processCompressOptions(optarg
))
71 case 'f': /* fill order */
72 if (streq(optarg
, "lsb2msb"))
73 fillorder
= FILLORDER_LSB2MSB
;
74 else if (streq(optarg
, "msb2lsb"))
75 fillorder
= FILLORDER_MSB2LSB
;
79 case 'p': /* planar configuration */
80 if (streq(optarg
, "separate"))
81 config
= PLANARCONFIG_SEPARATE
;
82 else if (streq(optarg
, "contig"))
83 config
= PLANARCONFIG_CONTIG
;
87 case 'r': /* rows/strip */
88 rowsperstrip
= atoi(optarg
);
94 if (argc
- optind
!= 2)
96 in
= iopen(argv
[optind
], "r");
99 out
= TIFFOpen(argv
[optind
+1], "w");
102 TIFFSetField(out
, TIFFTAG_IMAGEWIDTH
, (uint32
) in
->xsize
);
103 TIFFSetField(out
, TIFFTAG_IMAGELENGTH
, (uint32
) in
->ysize
);
104 TIFFSetField(out
, TIFFTAG_BITSPERSAMPLE
, 8);
105 TIFFSetField(out
, TIFFTAG_COMPRESSION
, compression
);
107 photometric
= PHOTOMETRIC_MINISBLACK
;
109 photometric
= PHOTOMETRIC_RGB
;
110 switch (compression
) {
111 case COMPRESSION_JPEG
:
112 if (photometric
== PHOTOMETRIC_RGB
&& jpegcolormode
== JPEGCOLORMODE_RGB
)
113 photometric
= PHOTOMETRIC_YCBCR
;
114 TIFFSetField(out
, TIFFTAG_JPEGQUALITY
, quality
);
115 TIFFSetField(out
, TIFFTAG_JPEGCOLORMODE
, jpegcolormode
);
117 case COMPRESSION_LZW
:
118 case COMPRESSION_DEFLATE
:
120 TIFFSetField(out
, TIFFTAG_PREDICTOR
, predictor
);
123 TIFFSetField(out
, TIFFTAG_PHOTOMETRIC
, photometric
);
125 TIFFSetField(out
, TIFFTAG_FILLORDER
, fillorder
);
126 TIFFSetField(out
, TIFFTAG_ORIENTATION
, ORIENTATION_TOPLEFT
);
127 TIFFSetField(out
, TIFFTAG_SAMPLESPERPIXEL
, in
->zsize
);
130 v
[0] = EXTRASAMPLE_UNASSALPHA
;
131 TIFFSetField(out
, TIFFTAG_EXTRASAMPLES
, 1, v
);
133 TIFFSetField(out
, TIFFTAG_MINSAMPLEVALUE
, (uint16
) in
->min
);
134 TIFFSetField(out
, TIFFTAG_MAXSAMPLEVALUE
, (uint16
) in
->max
);
135 TIFFSetField(out
, TIFFTAG_PLANARCONFIG
, config
);
136 if (config
!= PLANARCONFIG_SEPARATE
)
137 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
,
138 TIFFDefaultStripSize(out
, rowsperstrip
));
139 else /* force 1 row/strip for library limitation */
140 TIFFSetField(out
, TIFFTAG_ROWSPERSTRIP
, 1L);
141 if (in
->name
[0] != '\0')
142 TIFFSetField(out
, TIFFTAG_IMAGEDESCRIPTION
, in
->name
);
143 if (config
== PLANARCONFIG_CONTIG
)
148 (void) TIFFClose(out
);
153 processCompressOptions(char* opt
)
155 if (streq(opt
, "none"))
156 compression
= COMPRESSION_NONE
;
157 else if (streq(opt
, "packbits"))
158 compression
= COMPRESSION_PACKBITS
;
159 else if (strneq(opt
, "jpeg", 4)) {
160 char* cp
= strchr(opt
, ':');
162 defcompression
= COMPRESSION_JPEG
;
165 if (isdigit((int)cp
[1]))
166 quality
= atoi(cp
+1);
167 else if (cp
[1] == 'r' )
168 jpegcolormode
= JPEGCOLORMODE_RAW
;
172 cp
= strchr(cp
+1,':');
174 } else if (strneq(opt
, "lzw", 3)) {
175 char* cp
= strchr(opt
, ':');
177 predictor
= atoi(cp
+1);
178 compression
= COMPRESSION_LZW
;
179 } else if (strneq(opt
, "zip", 3)) {
180 char* cp
= strchr(opt
, ':');
182 predictor
= atoi(cp
+1);
183 compression
= COMPRESSION_DEFLATE
;
190 cpContig(IMAGE
* in
, TIFF
* out
)
192 tdata_t buf
= _TIFFmalloc(TIFFScanlineSize(out
));
196 if (in
->zsize
== 3) {
199 r
= (short *)_TIFFmalloc(3 * in
->xsize
* sizeof (short));
202 for (y
= in
->ysize
-1; y
>= 0; y
--) {
203 uint8
* pp
= (uint8
*) buf
;
208 for (x
= 0; x
< in
->xsize
; x
++) {
214 if (TIFFWriteScanline(out
, buf
, in
->ysize
-y
-1, 0) < 0)
217 } else if (in
->zsize
== 4) {
220 r
= (short *)_TIFFmalloc(4 * in
->xsize
* sizeof (short));
224 for (y
= in
->ysize
-1; y
>= 0; y
--) {
225 uint8
* pp
= (uint8
*) buf
;
231 for (x
= 0; x
< in
->xsize
; x
++) {
238 if (TIFFWriteScanline(out
, buf
, in
->ysize
-y
-1, 0) < 0)
242 uint8
* pp
= (uint8
*) buf
;
244 r
= (short *)_TIFFmalloc(in
->xsize
* sizeof (short));
245 for (y
= in
->ysize
-1; y
>= 0; y
--) {
247 for (x
= in
->xsize
-1; x
>= 0; x
--)
249 if (TIFFWriteScanline(out
, buf
, in
->ysize
-y
-1, 0) < 0)
265 cpSeparate(IMAGE
* in
, TIFF
* out
)
267 tdata_t buf
= _TIFFmalloc(TIFFScanlineSize(out
));
268 short *r
= (short *)_TIFFmalloc(in
->xsize
* sizeof (short));
269 uint8
* pp
= (uint8
*) buf
;
272 for (z
= 0; z
< in
->zsize
; z
++) {
273 for (y
= in
->ysize
-1; y
>= 0; y
--) {
275 for (x
= 0; x
< in
->xsize
; x
++)
277 if (TIFFWriteScanline(out
, buf
, in
->ysize
-y
-1, z
) < 0)
291 "usage: sgi2tiff [options] input.rgb output.tif",
292 "where options are:",
293 " -r # make each strip have no more than # rows",
295 " -p contig pack samples contiguously (e.g. RGBRGB...)",
296 " -p separate store samples separately (e.g. RRR...GGG...BBB...)",
298 " -f lsb2msb force lsb-to-msb FillOrder for output",
299 " -f msb2lsb force msb-to-lsb FillOrder for output",
301 " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
302 " -c zip[:opts] compress output with deflate encoding",
303 " -c jpeg[:opts]compress output with JPEG encoding",
304 " -c packbits compress output with packbits encoding",
305 " -c none use no compression algorithm on output",
308 " # set compression quality level (0-100, default 75)",
309 " r output color image as RGB rather than YCbCr",
311 "LZW and deflate options:",
312 " # set predictor value",
313 "For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
324 for (i
= 0; stuff
[i
] != NULL
; i
++)
325 fprintf(stderr
, "%s\n", stuff
[i
]);