| 1 |
/* |
| 2 |
* Copyright (c) 1990-1997 Sam Leffler |
| 3 |
* Copyright (c) 1991-1997 Silicon Graphics, Inc. |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 6 |
* its documentation for any purpose is hereby granted without fee, provided |
| 7 |
* that (i) the above copyright notices and this permission notice appear in |
| 8 |
* all copies of the software and related documentation, and (ii) the names of |
| 9 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 10 |
* publicity relating to the software without the specific, prior written |
| 11 |
* permission of Sam Leffler and Silicon Graphics. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 14 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 15 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* |
| 17 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 18 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 19 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 20 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 21 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 22 |
* OF THIS SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#include <ctype.h> |
| 30 |
#include <gl.h> |
| 31 |
|
| 32 |
#include "tiffio.h" |
| 33 |
|
| 34 |
typedef unsigned char unsigned char; |
| 35 |
typedef unsigned long uint32_t; |
| 36 |
|
| 37 |
#define streq(a, b) (strcmp(a, b) == 0) |
| 38 |
#define strneq(a, b, n) (strncmp(a, b, n) == 0) |
| 39 |
|
| 40 |
uint32_t rowsperstrip = (uint32_t)-1; |
| 41 |
uint16_t compression = COMPRESSION_PACKBITS; |
| 42 |
uint16_t config = PLANARCONFIG_CONTIG; |
| 43 |
uint16_t predictor = 0; |
| 44 |
int xmaxscreen; |
| 45 |
int ymaxscreen; |
| 46 |
uint16_t photometric = PHOTOMETRIC_RGB; |
| 47 |
int jpegcolormode = JPEGCOLORMODE_RGB; |
| 48 |
int quality = 75; /* JPEG quality */ |
| 49 |
|
| 50 |
static void usage(void); |
| 51 |
static void tiffsv(char *, int, int, int, int); |
| 52 |
|
| 53 |
int main(int argc, char *argv[]) |
| 54 |
{ |
| 55 |
int c; |
| 56 |
#if !HAVE_DECL_OPTARG |
| 57 |
extern int optind; |
| 58 |
extern char *optarg; |
| 59 |
#endif |
| 60 |
|
| 61 |
while ((c = getopt(argc, argv, "c:p:r:")) != -1) |
| 62 |
switch (c) |
| 63 |
{ |
| 64 |
case 'b': /* save as b&w */ |
| 65 |
photometric = PHOTOMETRIC_MINISBLACK; |
| 66 |
break; |
| 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 |
{ |
| 74 |
char *cp = strchr(optarg, ':'); |
| 75 |
if (cp && isdigit(cp[1])) |
| 76 |
quality = atoi(cp + 1); |
| 77 |
if (cp && strchr(cp, 'r')) |
| 78 |
jpegcolormode = JPEGCOLORMODE_RAW; |
| 79 |
compression = COMPRESSION_JPEG; |
| 80 |
} |
| 81 |
else if (strneq(optarg, "lzw", 3)) |
| 82 |
{ |
| 83 |
char *cp = strchr(optarg, ':'); |
| 84 |
if (cp) |
| 85 |
predictor = atoi(cp + 1); |
| 86 |
compression = COMPRESSION_LZW; |
| 87 |
} |
| 88 |
else |
| 89 |
usage(); |
| 90 |
break; |
| 91 |
case 'p': /* planar configuration */ |
| 92 |
if (streq(optarg, "separate")) |
| 93 |
config = PLANARCONFIG_SEPARATE; |
| 94 |
else if (streq(optarg, "contig")) |
| 95 |
config = PLANARCONFIG_CONTIG; |
| 96 |
else |
| 97 |
usage(); |
| 98 |
break; |
| 99 |
case 'r': /* rows/strip */ |
| 100 |
rowsperstrip = atoi(optarg); |
| 101 |
break; |
| 102 |
case '?': |
| 103 |
usage(); |
| 104 |
/*NOTREACHED*/ |
| 105 |
} |
| 106 |
if (argc - optind != 1 && argc - optind != 5) |
| 107 |
usage(); |
| 108 |
xmaxscreen = getgdesc(GD_XPMAX) - 1; |
| 109 |
ymaxscreen = getgdesc(GD_YPMAX) - 1; |
| 110 |
foreground(); |
| 111 |
noport(); |
| 112 |
winopen("tiffsv"); |
| 113 |
if (argc - optind == 5) |
| 114 |
tiffsv(argv[optind], atoi(argv[optind + 1]), atoi(argv[optind + 2]), |
| 115 |
atoi(argv[optind + 3]), atoi(argv[optind + 4])); |
| 116 |
else |
| 117 |
tiffsv(argv[optind], 0, xmaxscreen, 0, ymaxscreen); |
| 118 |
return (0); |
| 119 |
} |
| 120 |
|
| 121 |
char *stuff[] = { |
| 122 |
"usage: tiffsv [options] outimage.tif [x1 x2 y1 y2] [-b]", |
| 123 |
"where options are:", |
| 124 |
" -p contig pack samples contiguously (e.g. RGBRGB...)", |
| 125 |
" -p separate store samples separately (e.g. RRR...GGG...BBB...)", |
| 126 |
"", |
| 127 |
" -r # make each strip have no more than # rows", |
| 128 |
"", |
| 129 |
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding", |
| 130 |
" -c jpeg[:opts]compress output with JPEG encoding", |
| 131 |
" -c packbits compress output with packbits encoding", |
| 132 |
" -c none use no compression algorithm on output", |
| 133 |
"", |
| 134 |
"JPEG options:", |
| 135 |
" # set compression quality level (0-100, default 75)", |
| 136 |
" r output color image as RGB rather than YCbCr", |
| 137 |
"", |
| 138 |
"LZW options:", |
| 139 |
" # set predictor value for Lempel-Ziv & Welch encoding", |
| 140 |
"For example, -c lzw:2 to get LZW-encoded data with horizontal " |
| 141 |
"differencing", |
| 142 |
NULL}; |
| 143 |
|
| 144 |
static void 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_t *ss, int xsize, int ysize) |
| 156 |
{ |
| 157 |
tsize_t stripsize = TIFFStripSize(tif); |
| 158 |
unsigned char *rbuf = (unsigned char *)_TIFFmalloc(3 * stripsize); |
| 159 |
unsigned char *gbuf = rbuf + stripsize; |
| 160 |
unsigned char *bbuf = gbuf + stripsize; |
| 161 |
register int y; |
| 162 |
|
| 163 |
for (y = 0; y <= ysize; y += rowsperstrip) |
| 164 |
{ |
| 165 |
unsigned char *rp, *gp, *bp; |
| 166 |
register int x; |
| 167 |
register uint32_t n; |
| 168 |
|
| 169 |
n = rowsperstrip; |
| 170 |
if (n > ysize - y + 1) |
| 171 |
n = ysize - y + 1; |
| 172 |
rp = rbuf; |
| 173 |
gp = gbuf; |
| 174 |
bp = bbuf; |
| 175 |
do |
| 176 |
{ |
| 177 |
for (x = 0; x <= xsize; x++) |
| 178 |
{ |
| 179 |
uint32_t v = ss[x]; |
| 180 |
rp[x] = v; |
| 181 |
gp[x] = v >> 8; |
| 182 |
bp[x] = v >> 16; |
| 183 |
} |
| 184 |
rp += xsize + 1, gp += xsize + 1, bp += xsize + 1; |
| 185 |
ss += xsize + 1; |
| 186 |
} while (--n); |
| 187 |
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif, y, 0), rbuf, |
| 188 |
stripsize) < 0) |
| 189 |
break; |
| 190 |
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif, y, 1), gbuf, |
| 191 |
stripsize) < 0) |
| 192 |
break; |
| 193 |
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif, y, 2), bbuf, |
| 194 |
stripsize) < 0) |
| 195 |
break; |
| 196 |
} |
| 197 |
_TIFFfree(rbuf); |
| 198 |
} |
| 199 |
|
| 200 |
static void svRGBContig(TIFF *tif, uint32_t *ss, int xsize, int ysize) |
| 201 |
{ |
| 202 |
register int x, y; |
| 203 |
tsize_t stripsize = TIFFStripSize(tif); |
| 204 |
unsigned char *strip = (unsigned char *)_TIFFmalloc(stripsize); |
| 205 |
|
| 206 |
for (y = 0; y <= ysize; y += rowsperstrip) |
| 207 |
{ |
| 208 |
register unsigned char *pp = strip; |
| 209 |
register uint32_t n; |
| 210 |
|
| 211 |
n = rowsperstrip; |
| 212 |
if (n > ysize - y + 1) |
| 213 |
n = ysize - y + 1; |
| 214 |
do |
| 215 |
{ |
| 216 |
for (x = 0; x <= xsize; x++) |
| 217 |
{ |
| 218 |
uint32_t v = ss[x]; |
| 219 |
pp[0] = v; |
| 220 |
pp[1] = v >> 8; |
| 221 |
pp[2] = v >> 16; |
| 222 |
pp += 3; |
| 223 |
} |
| 224 |
ss += xsize + 1; |
| 225 |
} while (--n); |
| 226 |
if (TIFFWriteEncodedStrip(tif, TIFFComputeStrip(tif, y, 0), strip, |
| 227 |
stripsize) < 0) |
| 228 |
break; |
| 229 |
} |
| 230 |
_TIFFfree(strip); |
| 231 |
} |
| 232 |
|
| 233 |
#undef RED |
| 234 |
#undef GREEN |
| 235 |
#undef BLUE |
| 236 |
#define CVT(x) (((x)*255) / 100) |
| 237 |
#define RED CVT(28) /* 28% */ |
| 238 |
#define GREEN CVT(59) /* 59% */ |
| 239 |
#define BLUE CVT(11) /* 11% */ |
| 240 |
|
| 241 |
static void svGrey(TIFF *tif, uint32_t *ss, int xsize, int ysize) |
| 242 |
{ |
| 243 |
register int x, y; |
| 244 |
unsigned char *buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif)); |
| 245 |
|
| 246 |
for (y = 0; y <= ysize; y++) |
| 247 |
{ |
| 248 |
for (x = 0; x <= xsize; x++) |
| 249 |
{ |
| 250 |
unsigned char *cp = (unsigned char *)&ss[x]; |
| 251 |
buf[x] = (RED * cp[3] + GREEN * cp[2] + BLUE * cp[1]) >> 8; |
| 252 |
} |
| 253 |
if (TIFFWriteScanline(tif, buf, (uint32_t)y, 0) < 0) |
| 254 |
break; |
| 255 |
ss += xsize + 1; |
| 256 |
} |
| 257 |
_TIFFfree(buf); |
| 258 |
} |
| 259 |
|
| 260 |
#define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 261 |
#define ABS(x) ((x) < 0 ? -(x) : (x)) |
| 262 |
|
| 263 |
static void tiffsv(char *name, int x1, int x2, int y1, int y2) |
| 264 |
{ |
| 265 |
TIFF *tif; |
| 266 |
int xsize, ysize; |
| 267 |
int xorg, yorg; |
| 268 |
uint32_t *scrbuf; |
| 269 |
|
| 270 |
xorg = MIN(x1, x2); |
| 271 |
yorg = MIN(y1, y2); |
| 272 |
if (xorg < 0) |
| 273 |
xorg = 0; |
| 274 |
if (yorg < 0) |
| 275 |
yorg = 0; |
| 276 |
xsize = ABS(x2 - x1); |
| 277 |
ysize = ABS(y2 - y1); |
| 278 |
if (xorg + xsize > xmaxscreen) |
| 279 |
xsize = xmaxscreen - xorg; |
| 280 |
if (yorg + ysize > ymaxscreen) |
| 281 |
ysize = ymaxscreen - yorg; |
| 282 |
tif = TIFFOpen(name, "w"); |
| 283 |
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32_t)(xsize + 1)); |
| 284 |
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32_t)(ysize + 1)); |
| 285 |
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
| 286 |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, |
| 287 |
photometric == PHOTOMETRIC_RGB ? 3 : 1); |
| 288 |
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, config); |
| 289 |
TIFFSetField(tif, TIFFTAG_COMPRESSION, compression); |
| 290 |
switch (compression) |
| 291 |
{ |
| 292 |
case COMPRESSION_JPEG: |
| 293 |
if (photometric == PHOTOMETRIC_RGB && |
| 294 |
jpegcolormode == JPEGCOLORMODE_RGB) |
| 295 |
photometric = PHOTOMETRIC_YCBCR; |
| 296 |
TIFFSetField(tif, TIFFTAG_JPEGQUALITY, quality); |
| 297 |
TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, jpegcolormode); |
| 298 |
break; |
| 299 |
case COMPRESSION_LZW: |
| 300 |
if (predictor != 0) |
| 301 |
TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor); |
| 302 |
break; |
| 303 |
} |
| 304 |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric); |
| 305 |
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT); |
| 306 |
rowsperstrip = TIFFDefaultStripSize(tif, rowsperstrip); |
| 307 |
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip); |
| 308 |
scrbuf = |
| 309 |
(uint32_t *)_TIFFmalloc((xsize + 1) * (ysize + 1) * sizeof(uint32_t)); |
| 310 |
readdisplay(xorg, yorg, xorg + xsize, yorg + ysize, scrbuf, RD_FREEZE); |
| 311 |
if (photometric == PHOTOMETRIC_RGB) |
| 312 |
{ |
| 313 |
if (config == PLANARCONFIG_SEPARATE) |
| 314 |
svRGBSeparate(tif, scrbuf, xsize, ysize); |
| 315 |
else |
| 316 |
svRGBContig(tif, scrbuf, xsize, ysize); |
| 317 |
} |
| 318 |
else |
| 319 |
svGrey(tif, scrbuf, xsize, ysize); |
| 320 |
(void)TIFFClose(tif); |
| 321 |
_TIFFfree((char *)scrbuf); |
| 322 |
} |