1
/* $Header: /cvsroot/osrs/libtiff/tools/sgisv.c,v 1.3 1999/12/27 17:35:01 mwelles 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 u_char;
37
typedef unsigned long u_long;
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
" (no longer supported by default due to Unisys patent enforcement)",
128
" -c jpeg[:opts]compress output with JPEG encoding",
129
" -c packbits compress output with packbits encoding",
130
" -c none use no compression algorithm on output",
133
" # set compression quality level (0-100, default 75)",
134
" r output color image as RGB rather than YCbCr",
137
" # set predictor value for Lempel-Ziv & Welch encoding",
138
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
149
for (i = 0; stuff[i] != NULL; i++)
150
fprintf(stderr, "%s\n", stuff[i]);
155
svRGBSeparate(TIFF* tif, u_long* ss, int xsize, int ysize)
157
tsize_t stripsize = TIFFStripSize(tif);
158
u_char *rbuf = (u_char *)_TIFFmalloc(3*stripsize);
159
u_char *gbuf = rbuf + stripsize;
160
u_char *bbuf = gbuf + stripsize;
163
for (y = 0; y <= ysize; y += rowsperstrip) {
164
u_char *rp, *gp, *bp;
171
rp = rbuf; gp = gbuf; bp = bbuf;
173
for (x = 0; x <= xsize; x++) {
179
rp += xsize+1, gp += xsize+1, bp += xsize+1;
182
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
183
rbuf, stripsize) < 0)
185
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,1),
186
gbuf, stripsize) < 0)
188
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,2),
189
bbuf, stripsize) < 0)
196
svRGBContig(TIFF* tif, u_long* ss, int xsize, int ysize)
199
tsize_t stripsize = TIFFStripSize(tif);
200
u_char *strip = (u_char *)_TIFFmalloc(stripsize);
202
for (y = 0; y <= ysize; y += rowsperstrip) {
203
register u_char *pp = strip;
210
for (x = 0; x <= xsize; x++) {
219
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif,y,0),
220
strip, stripsize) < 0)
229
#define CVT(x) (((x)*255)/100)
230
#define RED CVT(28) /* 28% */
231
#define GREEN CVT(59) /* 59% */
232
#define BLUE CVT(11) /* 11% */
235
svGrey(TIFF* tif, u_long* ss, int xsize, int ysize)
238
u_char *buf = (u_char *)_TIFFmalloc(TIFFScanlineSize(tif));
240
for (y = 0; y <= ysize; y++) {
241
for (x = 0; x <= xsize; x++) {
242
u_char *cp = (u_char *)&ss[x];
243
buf[x] = (RED*cp[3] + GREEN*cp[2] + BLUE*cp[1]) >> 8;
245
if (TIFFWriteScanline(tif, buf, (uint32) y, 0) < 0)
252
#define MIN(a,b) ((a)<(b)?(a):(b))
253
#define ABS(x) ((x)<0?-(x):(x))
256
tiffsv(char* name, int x1, int x2, int y1, int y2)
271
if (xorg+xsize > xmaxscreen)
272
xsize = xmaxscreen-xorg;
273
if (yorg+ysize > ymaxscreen)
274
ysize = ymaxscreen-yorg;
275
tif = TIFFOpen(name, "w");
276
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32) (xsize+1));
277
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32) (ysize+1));
278
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
279
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL,
280
photometric == PHOTOMETRIC_RGB ? 3 : 1);
281
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, config);
282
TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
283
switch (compression) {
284
case COMPRESSION_JPEG:
285
if (photometric == PHOTOMETRIC_RGB && jpegcolormode == JPEGCOLORMODE_RGB)
286
photometric = PHOTOMETRIC_YCBCR;
287
TIFFSetField(tif, TIFFTAG_JPEGQUALITY, quality);
288
TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
290
case COMPRESSION_LZW:
292
TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
295
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric);
296
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
297
rowsperstrip = TIFFDefaultStripSize(tif, rowsperstrip);
298
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
299
scrbuf = (u_long *)_TIFFmalloc((xsize+1)*(ysize+1)*sizeof (u_long));
300
readdisplay(xorg, yorg, xorg+xsize, yorg+ysize, scrbuf, RD_FREEZE);
301
if (photometric == PHOTOMETRIC_RGB) {
302
if (config == PLANARCONFIG_SEPARATE)
303
svRGBSeparate(tif, scrbuf, xsize, ysize);
305
svRGBContig(tif, scrbuf, xsize, ysize);
307
svGrey(tif, scrbuf, xsize, ysize);
308
(void) TIFFClose(tif);
309
_TIFFfree((char *)scrbuf);