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