]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/tools/sgisv.c
Compilation fix for wxMSW with wxUSE_MSGBOX_HOOK==0.
[wxWidgets.git] / src / tiff / tools / sgisv.c
1
2 /*
3 * Copyright (c) 1990-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 *
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.
13 *
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.
17 *
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
23 * OF THIS SOFTWARE.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <gl.h>
31 #include <ctype.h>
32
33 #include "tiffio.h"
34
35 typedef unsigned char unsigned char;
36 typedef unsigned long uint32;
37
38 #define streq(a,b) (strcmp(a,b) == 0)
39 #define strneq(a,b,n) (strncmp(a,b,n) == 0)
40
41 uint32 rowsperstrip = (uint32) -1;
42 uint16 compression = COMPRESSION_PACKBITS;
43 uint16 config = PLANARCONFIG_CONTIG;
44 uint16 predictor = 0;
45 int xmaxscreen;
46 int ymaxscreen;
47 uint16 photometric = PHOTOMETRIC_RGB;
48 int jpegcolormode = JPEGCOLORMODE_RGB;
49 int quality = 75; /* JPEG quality */
50
51 static void usage(void);
52 static void tiffsv(char*, int, int, int, int);
53
54 int
55 main(int argc, char* argv[])
56 {
57 int c;
58 extern int optind;
59 extern char* optarg;
60
61 while ((c = getopt(argc, argv, "c:p:r:")) != -1)
62 switch (c) {
63 case 'b': /* save as b&w */
64 photometric = PHOTOMETRIC_MINISBLACK;
65 break;
66 case 'c': /* compression scheme */
67 if (streq(optarg, "none"))
68 compression = COMPRESSION_NONE;
69 else if (streq(optarg, "packbits"))
70 compression = COMPRESSION_PACKBITS;
71 else if (strneq(optarg, "jpeg", 4)) {
72 char* cp = strchr(optarg, ':');
73 if (cp && isdigit(cp[1]))
74 quality = atoi(cp+1);
75 if (cp && strchr(cp, 'r'))
76 jpegcolormode = JPEGCOLORMODE_RAW;
77 compression = COMPRESSION_JPEG;
78 } else if (strneq(optarg, "lzw", 3)) {
79 char* cp = strchr(optarg, ':');
80 if (cp)
81 predictor = atoi(cp+1);
82 compression = COMPRESSION_LZW;
83 } else
84 usage();
85 break;
86 case 'p': /* planar configuration */
87 if (streq(optarg, "separate"))
88 config = PLANARCONFIG_SEPARATE;
89 else if (streq(optarg, "contig"))
90 config = PLANARCONFIG_CONTIG;
91 else
92 usage();
93 break;
94 case 'r': /* rows/strip */
95 rowsperstrip = atoi(optarg);
96 break;
97 case '?':
98 usage();
99 /*NOTREACHED*/
100 }
101 if (argc - optind != 1 && argc - optind != 5)
102 usage();
103 xmaxscreen = getgdesc(GD_XPMAX)-1;
104 ymaxscreen = getgdesc(GD_YPMAX)-1;
105 foreground();
106 noport();
107 winopen("tiffsv");
108 if (argc - optind == 5)
109 tiffsv(argv[optind],
110 atoi(argv[optind+1]), atoi(argv[optind+2]),
111 atoi(argv[optind+3]), atoi(argv[optind+4]));
112 else
113 tiffsv(argv[optind], 0, xmaxscreen, 0, ymaxscreen);
114 return (0);
115 }
116
117 char* stuff[] = {
118 "usage: tiffsv [options] outimage.tif [x1 x2 y1 y2] [-b]",
119 "where options are:",
120 " -p contig pack samples contiguously (e.g. RGBRGB...)",
121 " -p separate store samples separately (e.g. RRR...GGG...BBB...)",
122 "",
123 " -r # make each strip have no more than # rows",
124 "",
125 " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
126 " -c jpeg[:opts]compress output with JPEG encoding",
127 " -c packbits compress output with packbits encoding",
128 " -c none use no compression algorithm on output",
129 "",
130 "JPEG options:",
131 " # set compression quality level (0-100, default 75)",
132 " r output color image as RGB rather than YCbCr",
133 "",
134 "LZW options:",
135 " # set predictor value for Lempel-Ziv & Welch encoding",
136 "For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
137 NULL
138 };
139
140 static void
141 usage(void)
142 {
143 char buf[BUFSIZ];
144 int i;
145
146 setbuf(stderr, buf);
147 for (i = 0; stuff[i] != NULL; i++)
148 fprintf(stderr, "%s\n", stuff[i]);
149 exit(-1);
150 }
151
152 static void
153 svRGBSeparate(TIFF* tif, uint32* ss, int xsize, int ysize)
154 {
155 tsize_t stripsize = TIFFStripSize(tif);
156 unsigned char *rbuf = (unsigned char *)_TIFFmalloc(3*stripsize);
157 unsigned char *gbuf = rbuf + stripsize;
158 unsigned char *bbuf = gbuf + stripsize;
159 register int y;
160
161 for (y = 0; y <= ysize; y += rowsperstrip) {
162 unsigned char *rp, *gp, *bp;
163 register int x;
164 register uint32 n;
165
166 n = rowsperstrip;
167 if (n > ysize-y+1)
168 n = ysize-y+1;
169 rp = rbuf; gp = gbuf; bp = bbuf;
170 do {
171 for (x = 0; x <= xsize; x++) {
172 uint32 v = ss[x];
173 rp[x] = v;
174 gp[x] = v >> 8;
175 bp[x] = v >> 16;
176 }
177 rp += xsize+1, gp += xsize+1, bp += xsize+1;
178 ss += xsize+1;
179 } while (--n);
180 if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
181 rbuf, stripsize) < 0)
182 break;
183 if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,1),
184 gbuf, stripsize) < 0)
185 break;
186 if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,2),
187 bbuf, stripsize) < 0)
188 break;
189 }
190 _TIFFfree(rbuf);
191 }
192
193 static void
194 svRGBContig(TIFF* tif, uint32* ss, int xsize, int ysize)
195 {
196 register int x, y;
197 tsize_t stripsize = TIFFStripSize(tif);
198 unsigned char *strip = (unsigned char *)_TIFFmalloc(stripsize);
199
200 for (y = 0; y <= ysize; y += rowsperstrip) {
201 register unsigned char *pp = strip;
202 register uint32 n;
203
204 n = rowsperstrip;
205 if (n > ysize-y+1)
206 n = ysize-y+1;
207 do {
208 for (x = 0; x <= xsize; x++) {
209 uint32 v = ss[x];
210 pp[0] = v;
211 pp[1] = v >> 8;
212 pp[2] = v >> 16;
213 pp += 3;
214 }
215 ss += xsize+1;
216 } while (--n);
217 if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
218 strip, stripsize) < 0)
219 break;
220 }
221 _TIFFfree(strip);
222 }
223
224 #undef RED
225 #undef GREEN
226 #undef BLUE
227 #define CVT(x) (((x)*255)/100)
228 #define RED CVT(28) /* 28% */
229 #define GREEN CVT(59) /* 59% */
230 #define BLUE CVT(11) /* 11% */
231
232 static void
233 svGrey(TIFF* tif, uint32* ss, int xsize, int ysize)
234 {
235 register int x, y;
236 unsigned char *buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
237
238 for (y = 0; y <= ysize; y++) {
239 for (x = 0; x <= xsize; x++) {
240 unsigned char *cp = (unsigned char *)&ss[x];
241 buf[x] = (RED*cp[3] + GREEN*cp[2] + BLUE*cp[1]) >> 8;
242 }
243 if (TIFFWriteScanline(tif, buf, (uint32) y, 0) < 0)
244 break;
245 ss += xsize+1;
246 }
247 _TIFFfree(buf);
248 }
249
250 #define MIN(a,b) ((a)<(b)?(a):(b))
251 #define ABS(x) ((x)<0?-(x):(x))
252
253 static void
254 tiffsv(char* name, int x1, int x2, int y1, int y2)
255 {
256 TIFF *tif;
257 int xsize, ysize;
258 int xorg, yorg;
259 uint32 *scrbuf;
260
261 xorg = MIN(x1,x2);
262 yorg = MIN(y1,y2);
263 if (xorg<0)
264 xorg = 0;
265 if (yorg<0)
266 yorg = 0;
267 xsize = ABS(x2-x1);
268 ysize = ABS(y2-y1);
269 if (xorg+xsize > xmaxscreen)
270 xsize = xmaxscreen-xorg;
271 if (yorg+ysize > ymaxscreen)
272 ysize = ymaxscreen-yorg;
273 tif = TIFFOpen(name, "w");
274 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32) (xsize+1));
275 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32) (ysize+1));
276 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
277 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL,
278 photometric == PHOTOMETRIC_RGB ? 3 : 1);
279 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, config);
280 TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
281 switch (compression) {
282 case COMPRESSION_JPEG:
283 if (photometric == PHOTOMETRIC_RGB && jpegcolormode == JPEGCOLORMODE_RGB)
284 photometric = PHOTOMETRIC_YCBCR;
285 TIFFSetField(tif, TIFFTAG_JPEGQUALITY, quality);
286 TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
287 break;
288 case COMPRESSION_LZW:
289 if (predictor != 0)
290 TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
291 break;
292 }
293 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
294 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
295 rowsperstrip = TIFFDefaultStripSize(tif, rowsperstrip);
296 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
297 scrbuf = (uint32 *)_TIFFmalloc((xsize+1)*(ysize+1)*sizeof (uint32));
298 readdisplay(xorg, yorg, xorg+xsize, yorg+ysize, scrbuf, RD_FREEZE);
299 if (photometric == PHOTOMETRIC_RGB) {
300 if (config == PLANARCONFIG_SEPARATE)
301 svRGBSeparate(tif, scrbuf, xsize, ysize);
302 else
303 svRGBContig(tif, scrbuf, xsize, ysize);
304 } else
305 svGrey(tif, scrbuf, xsize, ysize);
306 (void) TIFFClose(tif);
307 _TIFFfree((char *)scrbuf);
308 }
309 /*
310 * Local Variables:
311 * mode: c
312 * c-basic-offset: 8
313 * fill-column: 78
314 * End:
315 */