Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/archive/tools/sgi2tiff.c

raw
1 /*
2 * Copyright (c) 1991-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 #include <ctype.h>
26 #include <gl/image.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "tiffio.h"
32
33 #define streq(a, b) (strcmp(a, b) == 0)
34 #define strneq(a, b, n) (strncmp(a, b, n) == 0)
35
36 static short config = PLANARCONFIG_CONTIG;
37 static uint16_t compression = COMPRESSION_PACKBITS;
38 static uint16_t predictor = 0;
39 static uint16_t fillorder = 0;
40 static uint32_t rowsperstrip = (uint32_t)-1;
41 static int jpegcolormode = JPEGCOLORMODE_RGB;
42 static int quality = 75; /* JPEG quality */
43 static uint16_t photometric;
44
45 static void usage(void);
46 static int cpContig(IMAGE *, TIFF *);
47 static int cpSeparate(IMAGE *, TIFF *);
48 static int processCompressOptions(char *);
49
50 /* XXX image library has no prototypes */
51 extern IMAGE *iopen(const char *, const char *);
52 extern void iclose(IMAGE *);
53 extern void getrow(IMAGE *, short *, int, int);
54
55 int main(int argc, char *argv[])
56 {
57 IMAGE *in;
58 TIFF *out;
59 int c;
60 #if !HAVE_DECL_OPTARG
61 extern int optind;
62 extern char *optarg;
63 #endif
64
65 while ((c = getopt(argc, argv, "c:p:r:")) != -1)
66 switch (c)
67 {
68 case 'c': /* compression scheme */
69 if (!processCompressOptions(optarg))
70 usage();
71 break;
72 case 'f': /* fill order */
73 if (streq(optarg, "lsb2msb"))
74 fillorder = FILLORDER_LSB2MSB;
75 else if (streq(optarg, "msb2lsb"))
76 fillorder = FILLORDER_MSB2LSB;
77 else
78 usage();
79 break;
80 case 'p': /* planar configuration */
81 if (streq(optarg, "separate"))
82 config = PLANARCONFIG_SEPARATE;
83 else if (streq(optarg, "contig"))
84 config = PLANARCONFIG_CONTIG;
85 else
86 usage();
87 break;
88 case 'r': /* rows/strip */
89 rowsperstrip = atoi(optarg);
90 break;
91 case '?':
92 usage();
93 /*NOTREACHED*/
94 }
95 if (argc - optind != 2)
96 usage();
97 in = iopen(argv[optind], "r");
98 if (in == NULL)
99 return (-1);
100 out = TIFFOpen(argv[optind + 1], "w");
101 if (out == NULL)
102 return (-2);
103 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32_t)in->xsize);
104 TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32_t)in->ysize);
105 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
106 TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
107 if (in->zsize == 1)
108 photometric = PHOTOMETRIC_MINISBLACK;
109 else
110 photometric = PHOTOMETRIC_RGB;
111 switch (compression)
112 {
113 case COMPRESSION_JPEG:
114 if (photometric == PHOTOMETRIC_RGB &&
115 jpegcolormode == JPEGCOLORMODE_RGB)
116 photometric = PHOTOMETRIC_YCBCR;
117 TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
118 TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
119 break;
120 case COMPRESSION_LZW:
121 case COMPRESSION_ADOBE_DEFLATE:
122 case COMPRESSION_DEFLATE:
123 if (predictor != 0)
124 TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
125 break;
126 }
127 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric);
128 if (fillorder != 0)
129 TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);
130 TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
131 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, in->zsize);
132 if (in->zsize > 3)
133 {
134 uint16_t v[1];
135 v[0] = EXTRASAMPLE_UNASSALPHA;
136 TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, v);
137 }
138 TIFFSetField(out, TIFFTAG_MINSAMPLEVALUE, (uint16_t)in->min);
139 TIFFSetField(out, TIFFTAG_MAXSAMPLEVALUE, (uint16_t)in->max);
140 TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
141 if (config != PLANARCONFIG_SEPARATE)
142 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
143 TIFFDefaultStripSize(out, rowsperstrip));
144 else /* force 1 row/strip for library limitation */
145 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 1L);
146 if (in->name[0] != '\0')
147 TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, in->name);
148 if (config == PLANARCONFIG_CONTIG)
149 cpContig(in, out);
150 else
151 cpSeparate(in, out);
152 (void)iclose(in);
153 (void)TIFFClose(out);
154 return (0);
155 }
156
157 static int processCompressOptions(char *opt)
158 {
159 if (streq(opt, "none"))
160 compression = COMPRESSION_NONE;
161 else if (streq(opt, "packbits"))
162 compression = COMPRESSION_PACKBITS;
163 else if (strneq(opt, "jpeg", 4))
164 {
165 char *cp = strchr(opt, ':');
166
167 defcompression = COMPRESSION_JPEG;
168 while (cp)
169 {
170 if (isdigit((int)cp[1]))
171 quality = atoi(cp + 1);
172 else if (cp[1] == 'r')
173 jpegcolormode = JPEGCOLORMODE_RAW;
174 else
175 usage();
176
177 cp = strchr(cp + 1, ':');
178 }
179 }
180 else if (strneq(opt, "lzw", 3))
181 {
182 char *cp = strchr(opt, ':');
183 if (cp)
184 predictor = atoi(cp + 1);
185 compression = COMPRESSION_LZW;
186 }
187 else if (strneq(opt, "zip", 3))
188 {
189 char *cp = strchr(opt, ':');
190 if (cp)
191 predictor = atoi(cp + 1);
192 compression = COMPRESSION_ADOBE_DEFLATE;
193 }
194 else
195 return (0);
196 return (1);
197 }
198
199 static int cpContig(IMAGE *in, TIFF *out)
200 {
201 tdata_t buf = _TIFFmalloc(TIFFScanlineSize(out));
202 short *r = NULL;
203 int x, y;
204
205 if (in->zsize == 3)
206 {
207 short *g, *b;
208
209 r = (short *)_TIFFmalloc(3 * in->xsize * sizeof(short));
210 g = r + in->xsize;
211 b = g + in->xsize;
212 for (y = in->ysize - 1; y >= 0; y--)
213 {
214 uint8_t *pp = (uint8_t *)buf;
215
216 getrow(in, r, y, 0);
217 getrow(in, g, y, 1);
218 getrow(in, b, y, 2);
219 for (x = 0; x < in->xsize; x++)
220 {
221 pp[0] = r[x];
222 pp[1] = g[x];
223 pp[2] = b[x];
224 pp += 3;
225 }
226 if (TIFFWriteScanline(out, buf, in->ysize - y - 1, 0) < 0)
227 goto bad;
228 }
229 }
230 else if (in->zsize == 4)
231 {
232 short *g, *b, *a;
233
234 r = (short *)_TIFFmalloc(4 * in->xsize * sizeof(short));
235 g = r + in->xsize;
236 b = g + in->xsize;
237 a = b + in->xsize;
238 for (y = in->ysize - 1; y >= 0; y--)
239 {
240 uint8_t *pp = (uint8_t *)buf;
241
242 getrow(in, r, y, 0);
243 getrow(in, g, y, 1);
244 getrow(in, b, y, 2);
245 getrow(in, a, y, 3);
246 for (x = 0; x < in->xsize; x++)
247 {
248 pp[0] = r[x];
249 pp[1] = g[x];
250 pp[2] = b[x];
251 pp[3] = a[x];
252 pp += 4;
253 }
254 if (TIFFWriteScanline(out, buf, in->ysize - y - 1, 0) < 0)
255 goto bad;
256 }
257 }
258 else
259 {
260 uint8_t *pp = (uint8_t *)buf;
261
262 r = (short *)_TIFFmalloc(in->xsize * sizeof(short));
263 for (y = in->ysize - 1; y >= 0; y--)
264 {
265 getrow(in, r, y, 0);
266 for (x = in->xsize - 1; x >= 0; x--)
267 pp[x] = r[x];
268 if (TIFFWriteScanline(out, buf, in->ysize - y - 1, 0) < 0)
269 goto bad;
270 }
271 }
272 if (r)
273 _TIFFfree(r);
274 _TIFFfree(buf);
275 return (1);
276 bad:
277 if (r)
278 _TIFFfree(r);
279 _TIFFfree(buf);
280 return (0);
281 }
282
283 static int cpSeparate(IMAGE *in, TIFF *out)
284 {
285 tdata_t buf = _TIFFmalloc(TIFFScanlineSize(out));
286 short *r = (short *)_TIFFmalloc(in->xsize * sizeof(short));
287 uint8_t *pp = (uint8_t *)buf;
288 int x, y, z;
289
290 for (z = 0; z < in->zsize; z++)
291 {
292 for (y = in->ysize - 1; y >= 0; y--)
293 {
294 getrow(in, r, y, z);
295 for (x = 0; x < in->xsize; x++)
296 pp[x] = r[x];
297 if (TIFFWriteScanline(out, buf, in->ysize - y - 1, z) < 0)
298 goto bad;
299 }
300 }
301 _TIFFfree(r);
302 _TIFFfree(buf);
303 return (1);
304 bad:
305 _TIFFfree(r);
306 _TIFFfree(buf);
307 return (0);
308 }
309
310 char *stuff[] = {
311 "usage: sgi2tiff [options] input.rgb output.tif",
312 "where options are:",
313 " -r # make each strip have no more than # rows",
314 "",
315 " -p contig pack samples contiguously (e.g. RGBRGB...)",
316 " -p separate store samples separately (e.g. RRR...GGG...BBB...)",
317 "",
318 " -f lsb2msb force lsb-to-msb FillOrder for output",
319 " -f msb2lsb force msb-to-lsb FillOrder for output",
320 "",
321 " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
322 " -c zip[:opts] compress output with deflate encoding",
323 " -c jpeg[:opts]compress output with JPEG encoding",
324 " -c packbits compress output with packbits encoding",
325 " -c none use no compression algorithm on output",
326 "",
327 "JPEG options:",
328 " # set compression quality level (0-100, default 75)",
329 " r output color image as RGB rather than YCbCr",
330 "",
331 "LZW and deflate options:",
332 " # set predictor value",
333 "For example, -c lzw:2 to get LZW-encoded data with horizontal "
334 "differencing",
335 NULL};
336
337 static void usage(void)
338 {
339 char buf[BUFSIZ];
340 int i;
341
342 setbuf(stderr, buf);
343 for (i = 0; stuff[i] != NULL; i++)
344 fprintf(stderr, "%s\n", stuff[i]);
345 exit(-1);
346 }