]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/tools/sgisv.c
3 * Copyright (c) 1990-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
35 typedef unsigned char unsigned char;
36 typedef unsigned long uint32
;
38 #define streq(a,b) (strcmp(a,b) == 0)
39 #define strneq(a,b,n) (strncmp(a,b,n) == 0)
41 uint32 rowsperstrip
= (uint32
) -1;
42 uint16 compression
= COMPRESSION_PACKBITS
;
43 uint16 config
= PLANARCONFIG_CONTIG
;
47 uint16 photometric
= PHOTOMETRIC_RGB
;
48 int jpegcolormode
= JPEGCOLORMODE_RGB
;
49 int quality
= 75; /* JPEG quality */
51 static void usage(void);
52 static void tiffsv(char*, int, int, int, int);
55 main(int argc
, char* argv
[])
61 while ((c
= getopt(argc
, argv
, "c:p:r:")) != -1)
63 case 'b': /* save as b&w */
64 photometric
= PHOTOMETRIC_MINISBLACK
;
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]))
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
, ':');
81 predictor
= atoi(cp
+1);
82 compression
= COMPRESSION_LZW
;
86 case 'p': /* planar configuration */
87 if (streq(optarg
, "separate"))
88 config
= PLANARCONFIG_SEPARATE
;
89 else if (streq(optarg
, "contig"))
90 config
= PLANARCONFIG_CONTIG
;
94 case 'r': /* rows/strip */
95 rowsperstrip
= atoi(optarg
);
101 if (argc
- optind
!= 1 && argc
- optind
!= 5)
103 xmaxscreen
= getgdesc(GD_XPMAX
)-1;
104 ymaxscreen
= getgdesc(GD_YPMAX
)-1;
108 if (argc
- optind
== 5)
110 atoi(argv
[optind
+1]), atoi(argv
[optind
+2]),
111 atoi(argv
[optind
+3]), atoi(argv
[optind
+4]));
113 tiffsv(argv
[optind
], 0, xmaxscreen
, 0, ymaxscreen
);
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...)",
123 " -r # make each strip have no more than # rows",
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",
131 " # set compression quality level (0-100, default 75)",
132 " r output color image as RGB rather than YCbCr",
135 " # set predictor value for Lempel-Ziv & Welch encoding",
136 "For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
147 for (i
= 0; stuff
[i
] != NULL
; i
++)
148 fprintf(stderr
, "%s\n", stuff
[i
]);
153 svRGBSeparate(TIFF
* tif
, uint32
* ss
, int xsize
, int ysize
)
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
;
161 for (y
= 0; y
<= ysize
; y
+= rowsperstrip
) {
162 unsigned char *rp
, *gp
, *bp
;
169 rp
= rbuf
; gp
= gbuf
; bp
= bbuf
;
171 for (x
= 0; x
<= xsize
; x
++) {
177 rp
+= xsize
+1, gp
+= xsize
+1, bp
+= xsize
+1;
180 if (TIFFWriteEncodedStrip(tif
, TIFFComputeStrip(tif
,y
,0),
181 rbuf
, stripsize
) < 0)
183 if (TIFFWriteEncodedStrip(tif
, TIFFComputeStrip(tif
,y
,1),
184 gbuf
, stripsize
) < 0)
186 if (TIFFWriteEncodedStrip(tif
, TIFFComputeStrip(tif
,y
,2),
187 bbuf
, stripsize
) < 0)
194 svRGBContig(TIFF
* tif
, uint32
* ss
, int xsize
, int ysize
)
197 tsize_t stripsize
= TIFFStripSize(tif
);
198 unsigned char *strip
= (unsigned char *)_TIFFmalloc(stripsize
);
200 for (y
= 0; y
<= ysize
; y
+= rowsperstrip
) {
201 register unsigned char *pp
= strip
;
208 for (x
= 0; x
<= xsize
; x
++) {
217 if (TIFFWriteEncodedStrip(tif
, TIFFComputeStrip(tif
,y
,0),
218 strip
, stripsize
) < 0)
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% */
233 svGrey(TIFF
* tif
, uint32
* ss
, int xsize
, int ysize
)
236 unsigned char *buf
= (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif
));
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;
243 if (TIFFWriteScanline(tif
, buf
, (uint32
) y
, 0) < 0)
250 #define MIN(a,b) ((a)<(b)?(a):(b))
251 #define ABS(x) ((x)<0?-(x):(x))
254 tiffsv(char* name
, int x1
, int x2
, int y1
, int y2
)
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
);
288 case COMPRESSION_LZW
:
290 TIFFSetField(tif
, TIFFTAG_PREDICTOR
, predictor
);
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
);
303 svRGBContig(tif
, scrbuf
, xsize
, ysize
);
305 svGrey(tif
, scrbuf
, xsize
, ysize
);
306 (void) TIFFClose(tif
);
307 _TIFFfree((char *)scrbuf
);