1 /* $Id: sgisv.c,v 1.5.2.1 2010-06-08 18:50:44 bfriesen Exp $ */
4 * Copyright (c) 1990-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
36 typedef unsigned char unsigned char;
37 typedef unsigned long uint32;
39 #define streq(a,b) (strcmp(a,b) == 0)
40 #define strneq(a,b,n) (strncmp(a,b,n) == 0)
42 uint32 rowsperstrip = (uint32) -1;
43 uint16 compression = COMPRESSION_PACKBITS;
44 uint16 config = PLANARCONFIG_CONTIG;
48 uint16 photometric = PHOTOMETRIC_RGB;
49 int jpegcolormode = JPEGCOLORMODE_RGB;
50 int quality = 75; /* JPEG quality */
52 static void usage(void);
53 static void tiffsv(char*, int, int, int, int);
56 main(int argc, char* argv[])
62 while ((c = getopt(argc, argv, "c:p:r:")) != -1)
64 case 'b': /* save as b&w */
65 photometric = PHOTOMETRIC_MINISBLACK;
67 case 'c': /* compression scheme */
68 if (streq(optarg, "none"))
69 compression = COMPRESSION_NONE;
70 else if (streq(optarg, "packbits"))
71 compression = COMPRESSION_PACKBITS;
72 else if (strneq(optarg, "jpeg", 4)) {
73 char* cp = strchr(optarg, ':');
74 if (cp && isdigit(cp[1]))
76 if (cp && strchr(cp, 'r'))
77 jpegcolormode = JPEGCOLORMODE_RAW;
78 compression = COMPRESSION_JPEG;
79 } else if (strneq(optarg, "lzw", 3)) {
80 char* cp = strchr(optarg, ':');
82 predictor = atoi(cp+1);
83 compression = COMPRESSION_LZW;
87 case 'p': /* planar configuration */
88 if (streq(optarg, "separate"))
89 config = PLANARCONFIG_SEPARATE;
90 else if (streq(optarg, "contig"))
91 config = PLANARCONFIG_CONTIG;
95 case 'r': /* rows/strip */
96 rowsperstrip = atoi(optarg);
102 if (argc - optind != 1 && argc - optind != 5)
104 xmaxscreen = getgdesc(GD_XPMAX)-1;
105 ymaxscreen = getgdesc(GD_YPMAX)-1;
109 if (argc - optind == 5)
111 atoi(argv[optind+1]), atoi(argv[optind+2]),
112 atoi(argv[optind+3]), atoi(argv[optind+4]));
114 tiffsv(argv[optind], 0, xmaxscreen, 0, ymaxscreen);
119 "usage: tiffsv [options] outimage.tif [x1 x2 y1 y2] [-b]",
120 "where options are:",
121 " -p contig pack samples contiguously (e.g. RGBRGB...)",
122 " -p separate store samples separately (e.g. RRR...GGG...BBB...)",
124 " -r # make each strip have no more than # rows",
126 " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
127 " -c jpeg[:opts]compress output with JPEG encoding",
128 " -c packbits compress output with packbits encoding",
129 " -c none use no compression algorithm on output",
132 " # set compression quality level (0-100, default 75)",
133 " r output color image as RGB rather than YCbCr",
136 " # set predictor value for Lempel-Ziv & Welch encoding",
137 "For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
148 for (i = 0; stuff[i] != NULL; i++)
149 fprintf(stderr, "%s\n", stuff[i]);
154 svRGBSeparate(TIFF* tif, uint32* ss, int xsize, int ysize)
156 tsize_t stripsize = TIFFStripSize(tif);
157 unsigned char *rbuf = (unsigned char *)_TIFFmalloc(3*stripsize);
158 unsigned char *gbuf = rbuf + stripsize;
159 unsigned char *bbuf = gbuf + stripsize;
162 for (y = 0; y <= ysize; y += rowsperstrip) {
163 unsigned char *rp, *gp, *bp;
170 rp = rbuf; gp = gbuf; bp = bbuf;
172 for (x = 0; x <= xsize; x++) {
178 rp += xsize+1, gp += xsize+1, bp += xsize+1;
181 if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
182 rbuf, stripsize) < 0)
184 if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,1),
185 gbuf, stripsize) < 0)
187 if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,2),
188 bbuf, stripsize) < 0)
195 svRGBContig(TIFF* tif, uint32* ss, int xsize, int ysize)
198 tsize_t stripsize = TIFFStripSize(tif);
199 unsigned char *strip = (unsigned char *)_TIFFmalloc(stripsize);
201 for (y = 0; y <= ysize; y += rowsperstrip) {
202 register unsigned char *pp = strip;
209 for (x = 0; x <= xsize; x++) {
218 if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
219 strip, stripsize) < 0)
228 #define CVT(x) (((x)*255)/100)
229 #define RED CVT(28) /* 28% */
230 #define GREEN CVT(59) /* 59% */
231 #define BLUE CVT(11) /* 11% */
234 svGrey(TIFF* tif, uint32* ss, int xsize, int ysize)
237 unsigned char *buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
239 for (y = 0; y <= ysize; y++) {
240 for (x = 0; x <= xsize; x++) {
241 unsigned char *cp = (unsigned char *)&ss[x];
242 buf[x] = (RED*cp[3] + GREEN*cp[2] + BLUE*cp[1]) >> 8;
244 if (TIFFWriteScanline(tif, buf, (uint32) y, 0) < 0)
251 #define MIN(a,b) ((a)<(b)?(a):(b))
252 #define ABS(x) ((x)<0?-(x):(x))
255 tiffsv(char* name, int x1, int x2, int y1, int y2)
270 if (xorg+xsize > xmaxscreen)
271 xsize = xmaxscreen-xorg;
272 if (yorg+ysize > ymaxscreen)
273 ysize = ymaxscreen-yorg;
274 tif = TIFFOpen(name, "w");
275 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32) (xsize+1));
276 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32) (ysize+1));
277 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
278 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL,
279 photometric == PHOTOMETRIC_RGB ? 3 : 1);
280 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, config);
281 TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
282 switch (compression) {
283 case COMPRESSION_JPEG:
284 if (photometric == PHOTOMETRIC_RGB && jpegcolormode == JPEGCOLORMODE_RGB)
285 photometric = PHOTOMETRIC_YCBCR;
286 TIFFSetField(tif, TIFFTAG_JPEGQUALITY, quality);
287 TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
289 case COMPRESSION_LZW:
291 TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
294 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
295 TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
296 rowsperstrip = TIFFDefaultStripSize(tif, rowsperstrip);
297 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
298 scrbuf = (uint32 *)_TIFFmalloc((xsize+1)*(ysize+1)*sizeof (uint32));
299 readdisplay(xorg, yorg, xorg+xsize, yorg+ysize, scrbuf, RD_FREEZE);
300 if (photometric == PHOTOMETRIC_RGB) {
301 if (config == PLANARCONFIG_SEPARATE)
302 svRGBSeparate(tif, scrbuf, xsize, ysize);
304 svRGBContig(tif, scrbuf, xsize, ysize);
306 svGrey(tif, scrbuf, xsize, ysize);
307 (void) TIFFClose(tif);
308 _TIFFfree((char *)scrbuf);