| 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 |
/* |
| 26 |
* convert a GIF file into a TIFF file. |
| 27 |
* based on Paul Haeberli's fromgif program which in turn is |
| 28 |
* based on a GIF file reader by Marcel J.E. Mol March 23 1989 |
| 29 |
* |
| 30 |
* if input is 320 by 200 pixel aspect is probably 1.2 |
| 31 |
* if input is 640 350 pixel aspect is probably 1.37 |
| 32 |
* |
| 33 |
*/ |
| 34 |
#include "tif_config.h" |
| 35 |
|
| 36 |
#include <errno.h> |
| 37 |
#include <math.h> |
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> |
| 41 |
|
| 42 |
#ifdef HAVE_UNISTD_H |
| 43 |
#include <unistd.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifdef NEED_LIBPORT |
| 47 |
#include "libport.h" |
| 48 |
#endif |
| 49 |
|
| 50 |
#include "tiffio.h" |
| 51 |
|
| 52 |
#define GIFGAMMA (1.5) /* smaller makes output img brighter */ |
| 53 |
#define IMAX 0xffff /* max intensity value */ |
| 54 |
#define EXTRAFUDGE 128 /* some people write BAD .gif files */ |
| 55 |
|
| 56 |
#define streq(a, b) (strcmp(a, b) == 0) |
| 57 |
#define strneq(a, b, n) (strncmp(a, b, n) == 0) |
| 58 |
|
| 59 |
unsigned short gamtab[256]; |
| 60 |
|
| 61 |
void makegamtab(float gam) |
| 62 |
{ |
| 63 |
int i; |
| 64 |
|
| 65 |
for (i = 0; i < 256; i++) |
| 66 |
gamtab[i] = (unsigned short)(IMAX * pow(i / 255.0, gam) + 0.5); |
| 67 |
} |
| 68 |
|
| 69 |
char *stuff[] = { |
| 70 |
"usage: gif2tiff [options] input.gif output.tif", |
| 71 |
"where options are:", |
| 72 |
" -r # make each strip have no more than # rows", |
| 73 |
"", |
| 74 |
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding", |
| 75 |
" -c zip[:opts] compress output with deflate encoding", |
| 76 |
" -c packbits compress output with packbits encoding", |
| 77 |
" -c none use no compression algorithm on output", |
| 78 |
"", |
| 79 |
"LZW and deflate options:", |
| 80 |
" # set predictor value", |
| 81 |
"For example, -c lzw:2 to get LZW-encoded data with horizontal " |
| 82 |
"differencing", |
| 83 |
NULL}; |
| 84 |
|
| 85 |
static void usage(void) |
| 86 |
{ |
| 87 |
char buf[BUFSIZ]; |
| 88 |
int i; |
| 89 |
|
| 90 |
setbuf(stderr, buf); |
| 91 |
fprintf(stderr, "%s\n\n", TIFFGetVersion()); |
| 92 |
for (i = 0; stuff[i] != NULL; i++) |
| 93 |
fprintf(stderr, "%s\n", stuff[i]); |
| 94 |
exit(-1); |
| 95 |
} |
| 96 |
|
| 97 |
#define COLSIZE 256 |
| 98 |
|
| 99 |
unsigned char *stackp; |
| 100 |
unsigned int prefix[4096]; |
| 101 |
unsigned char suffix[4096]; |
| 102 |
unsigned char stack[4096]; |
| 103 |
int datasize, codesize, codemask; /* Decoder working variables */ |
| 104 |
int clear, eoi; /* Special code values */ |
| 105 |
int avail, oldcode; |
| 106 |
|
| 107 |
FILE *infile; |
| 108 |
int global; /* Is there a global color map? */ |
| 109 |
int globalbits; /* Number of bits of global colors */ |
| 110 |
unsigned char globalmap[COLSIZE][3]; /* RGB values for global color map */ |
| 111 |
unsigned char *raster; /* Decoded image data */ |
| 112 |
unsigned long width, height; |
| 113 |
unsigned short red[COLSIZE]; |
| 114 |
unsigned short green[COLSIZE]; |
| 115 |
unsigned short blue[COLSIZE]; |
| 116 |
char *filename, *imagename; |
| 117 |
|
| 118 |
static uint16_t compression = COMPRESSION_PACKBITS; |
| 119 |
static uint16_t predictor = 0; |
| 120 |
static uint32_t rowsperstrip = (uint32_t)-1; |
| 121 |
static int processCompressOptions(char *); |
| 122 |
|
| 123 |
int convert(void); |
| 124 |
int checksignature(void); |
| 125 |
int readscreen(void); |
| 126 |
int readgifimage(char *); |
| 127 |
int readextension(void); |
| 128 |
int readraster(void); |
| 129 |
int process(int, unsigned char **); |
| 130 |
void initcolors(unsigned char[COLSIZE][3], int); |
| 131 |
void rasterize(int, char *); |
| 132 |
|
| 133 |
int main(int argc, char *argv[]) |
| 134 |
{ |
| 135 |
#if !HAVE_DECL_OPTARG |
| 136 |
extern int optind; |
| 137 |
extern char *optarg; |
| 138 |
#endif |
| 139 |
|
| 140 |
int c, status; |
| 141 |
|
| 142 |
while ((c = getopt(argc, argv, "c:r:")) != -1) |
| 143 |
switch (c) |
| 144 |
{ |
| 145 |
case 'c': /* compression scheme */ |
| 146 |
if (!processCompressOptions(optarg)) |
| 147 |
usage(); |
| 148 |
break; |
| 149 |
case 'r': /* rows/strip */ |
| 150 |
rowsperstrip = atoi(optarg); |
| 151 |
break; |
| 152 |
case '?': |
| 153 |
usage(); |
| 154 |
/*NOTREACHED*/ |
| 155 |
} |
| 156 |
if (argc - optind != 2) |
| 157 |
usage(); |
| 158 |
|
| 159 |
makegamtab(GIFGAMMA); |
| 160 |
filename = argv[optind]; |
| 161 |
imagename = argv[optind + 1]; |
| 162 |
if ((infile = fopen(imagename, "rb")) != NULL) |
| 163 |
{ |
| 164 |
int c; |
| 165 |
fclose(infile); |
| 166 |
printf("overwrite %s? ", imagename); |
| 167 |
fflush(stdout); |
| 168 |
c = getc(stdin); |
| 169 |
if (c != 'y' && c != 'Y') |
| 170 |
return (1); |
| 171 |
} |
| 172 |
if ((infile = fopen(filename, "rb")) == NULL) |
| 173 |
{ |
| 174 |
perror(filename); |
| 175 |
return (1); |
| 176 |
} |
| 177 |
status = convert(); |
| 178 |
fclose(infile); |
| 179 |
return (status); |
| 180 |
} |
| 181 |
|
| 182 |
static int processCompressOptions(char *opt) |
| 183 |
{ |
| 184 |
if (streq(opt, "none")) |
| 185 |
compression = COMPRESSION_NONE; |
| 186 |
else if (streq(opt, "packbits")) |
| 187 |
compression = COMPRESSION_PACKBITS; |
| 188 |
else if (strneq(opt, "lzw", 3)) |
| 189 |
{ |
| 190 |
char *cp = strchr(opt, ':'); |
| 191 |
if (cp) |
| 192 |
predictor = atoi(cp + 1); |
| 193 |
compression = COMPRESSION_LZW; |
| 194 |
} |
| 195 |
else if (strneq(opt, "zip", 3)) |
| 196 |
{ |
| 197 |
char *cp = strchr(opt, ':'); |
| 198 |
if (cp) |
| 199 |
predictor = atoi(cp + 1); |
| 200 |
compression = COMPRESSION_ADOBE_DEFLATE; |
| 201 |
} |
| 202 |
else |
| 203 |
return (0); |
| 204 |
return (1); |
| 205 |
} |
| 206 |
|
| 207 |
int convert(void) |
| 208 |
{ |
| 209 |
int ch; |
| 210 |
char *mode = "w"; |
| 211 |
|
| 212 |
if (!checksignature()) |
| 213 |
return (-1); |
| 214 |
if (!readscreen()) |
| 215 |
return (-1); |
| 216 |
while ((ch = getc(infile)) != ';' && ch != EOF) |
| 217 |
{ |
| 218 |
switch (ch) |
| 219 |
{ |
| 220 |
case '\0': |
| 221 |
break; /* this kludge for non-standard files */ |
| 222 |
case ',': |
| 223 |
if (!readgifimage(mode)) |
| 224 |
return (-1); |
| 225 |
mode = "a"; /* subsequent images append */ |
| 226 |
break; |
| 227 |
case '!': |
| 228 |
if (!readextension()) |
| 229 |
return (-1); |
| 230 |
break; |
| 231 |
default: |
| 232 |
fprintf(stderr, "illegal GIF block type\n"); |
| 233 |
return (-1); |
| 234 |
} |
| 235 |
} |
| 236 |
return (0); |
| 237 |
} |
| 238 |
|
| 239 |
int checksignature(void) |
| 240 |
{ |
| 241 |
char buf[6]; |
| 242 |
|
| 243 |
if (fread(buf, 1, 6, infile) != 6) |
| 244 |
{ |
| 245 |
fprintf(stderr, "short read from file %s (%s)\n", filename, |
| 246 |
strerror(errno)); |
| 247 |
return 0; |
| 248 |
} |
| 249 |
if (strncmp(buf, "GIF", 3)) |
| 250 |
{ |
| 251 |
fprintf(stderr, "file is not a GIF file\n"); |
| 252 |
return 0; |
| 253 |
} |
| 254 |
if (strncmp(&buf[3], "87a", 3)) |
| 255 |
{ |
| 256 |
fprintf(stderr, "unknown GIF version number\n"); |
| 257 |
return 0; |
| 258 |
} |
| 259 |
return 1; |
| 260 |
} |
| 261 |
|
| 262 |
/* |
| 263 |
* readscreen - |
| 264 |
* Get information which is global to all the images stored |
| 265 |
* in the file |
| 266 |
*/ |
| 267 |
int readscreen(void) |
| 268 |
{ |
| 269 |
unsigned char buf[7]; |
| 270 |
|
| 271 |
if (fread(buf, 1, 7, infile) != 7) |
| 272 |
{ |
| 273 |
fprintf(stderr, "short read from file %s (%s)\n", filename, |
| 274 |
strerror(errno)); |
| 275 |
return 0; |
| 276 |
} |
| 277 |
global = buf[4] & 0x80; |
| 278 |
if (global) |
| 279 |
{ |
| 280 |
globalbits = (buf[4] & 0x07) + 1; |
| 281 |
if (fread(globalmap, 3, ((size_t)1) << globalbits, infile) != |
| 282 |
((size_t)1) << globalbits) |
| 283 |
{ |
| 284 |
fprintf(stderr, "short read from file %s (%s)\n", filename, |
| 285 |
strerror(errno)); |
| 286 |
return 0; |
| 287 |
} |
| 288 |
} |
| 289 |
return 1; |
| 290 |
} |
| 291 |
|
| 292 |
int readgifimage(char *mode) |
| 293 |
{ |
| 294 |
unsigned char buf[9]; |
| 295 |
int local, interleaved; |
| 296 |
unsigned char localmap[256][3]; |
| 297 |
int localbits; |
| 298 |
int status; |
| 299 |
size_t raster_size; |
| 300 |
|
| 301 |
if (fread(buf, 1, 9, infile) != 9) |
| 302 |
{ |
| 303 |
fprintf(stderr, "short read from file %s (%s)\n", filename, |
| 304 |
strerror(errno)); |
| 305 |
return (0); |
| 306 |
} |
| 307 |
width = (buf[4] + (buf[5] << 8)) & 0xffff; /* 16 bit */ |
| 308 |
height = (buf[6] + (buf[7] << 8)) & 0xffff; /* 16 bit */ |
| 309 |
local = buf[8] & 0x80; |
| 310 |
interleaved = buf[8] & 0x40; |
| 311 |
if (width == 0UL || height == 0UL || (width > 2000000000UL / height)) |
| 312 |
{ |
| 313 |
fprintf(stderr, "Invalid value of width or height\n"); |
| 314 |
return (0); |
| 315 |
} |
| 316 |
if (local == 0 && global == 0) |
| 317 |
{ |
| 318 |
fprintf(stderr, "no colormap present for image\n"); |
| 319 |
return (0); |
| 320 |
} |
| 321 |
raster_size = width * height; |
| 322 |
if ((raster_size / width) == height) |
| 323 |
{ |
| 324 |
raster_size += EXTRAFUDGE; /* Add elbow room */ |
| 325 |
} |
| 326 |
else |
| 327 |
{ |
| 328 |
raster_size = 0; |
| 329 |
} |
| 330 |
if ((raster = (unsigned char *)_TIFFmalloc(raster_size)) == NULL) |
| 331 |
{ |
| 332 |
fprintf(stderr, "not enough memory for image\n"); |
| 333 |
return (0); |
| 334 |
} |
| 335 |
if (local) |
| 336 |
{ |
| 337 |
localbits = (buf[8] & 0x7) + 1; |
| 338 |
|
| 339 |
fprintf(stderr, " local colors: %d\n", 1 << localbits); |
| 340 |
|
| 341 |
if (fread(localmap, 3, ((size_t)1) << localbits, infile) != |
| 342 |
((size_t)1) << localbits) |
| 343 |
{ |
| 344 |
fprintf(stderr, "short read from file %s (%s)\n", filename, |
| 345 |
strerror(errno)); |
| 346 |
return (0); |
| 347 |
} |
| 348 |
initcolors(localmap, 1 << localbits); |
| 349 |
} |
| 350 |
else if (global) |
| 351 |
{ |
| 352 |
initcolors(globalmap, 1 << globalbits); |
| 353 |
} |
| 354 |
if ((status = readraster())) |
| 355 |
rasterize(interleaved, mode); |
| 356 |
_TIFFfree(raster); |
| 357 |
return status; |
| 358 |
} |
| 359 |
|
| 360 |
/* |
| 361 |
* readextension - |
| 362 |
* Read a GIF extension block (and do nothing with it). |
| 363 |
* |
| 364 |
*/ |
| 365 |
int readextension(void) |
| 366 |
{ |
| 367 |
int count; |
| 368 |
char buf[255]; |
| 369 |
int status = 1; |
| 370 |
|
| 371 |
(void)getc(infile); |
| 372 |
while ((count = getc(infile)) && count <= 255) |
| 373 |
if (fread(buf, 1, count, infile) != (size_t)count) |
| 374 |
{ |
| 375 |
fprintf(stderr, "short read from file %s (%s)\n", filename, |
| 376 |
strerror(errno)); |
| 377 |
status = 0; |
| 378 |
break; |
| 379 |
} |
| 380 |
return status; |
| 381 |
} |
| 382 |
|
| 383 |
/* |
| 384 |
* readraster - |
| 385 |
* Decode a raster image |
| 386 |
* |
| 387 |
*/ |
| 388 |
int readraster(void) |
| 389 |
{ |
| 390 |
unsigned char *fill = raster; |
| 391 |
unsigned char buf[255]; |
| 392 |
register int bits = 0; |
| 393 |
register unsigned long datum = 0; |
| 394 |
register unsigned char *ch; |
| 395 |
register int count, code; |
| 396 |
int status = 1; |
| 397 |
|
| 398 |
datasize = getc(infile); |
| 399 |
if (datasize > 12) |
| 400 |
return 0; |
| 401 |
clear = 1 << datasize; |
| 402 |
eoi = clear + 1; |
| 403 |
avail = clear + 2; |
| 404 |
oldcode = -1; |
| 405 |
codesize = datasize + 1; |
| 406 |
codemask = (1 << codesize) - 1; |
| 407 |
for (code = 0; code < clear; code++) |
| 408 |
{ |
| 409 |
prefix[code] = 0; |
| 410 |
suffix[code] = code; |
| 411 |
} |
| 412 |
stackp = stack; |
| 413 |
for (count = getc(infile); count > 0 && count <= 255; count = getc(infile)) |
| 414 |
{ |
| 415 |
if (fread(buf, 1, count, infile) != (size_t)count) |
| 416 |
{ |
| 417 |
fprintf(stderr, "short read from file %s (%s)\n", filename, |
| 418 |
strerror(errno)); |
| 419 |
return 0; |
| 420 |
} |
| 421 |
for (ch = buf; count-- > 0; ch++) |
| 422 |
{ |
| 423 |
datum += (unsigned long)*ch << bits; |
| 424 |
bits += 8; |
| 425 |
while (bits >= codesize) |
| 426 |
{ |
| 427 |
code = datum & codemask; |
| 428 |
datum >>= codesize; |
| 429 |
bits -= codesize; |
| 430 |
if (code == eoi) |
| 431 |
{ /* This kludge put in */ |
| 432 |
goto exitloop; /* because some GIF files*/ |
| 433 |
} /* aren't standard */ |
| 434 |
if (!process(code, &fill)) |
| 435 |
{ |
| 436 |
status = 0; |
| 437 |
goto exitloop; |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
if (fill >= raster + width * height) |
| 442 |
{ |
| 443 |
fprintf(stderr, "raster full before eoi code\n"); |
| 444 |
break; |
| 445 |
} |
| 446 |
} |
| 447 |
exitloop: |
| 448 |
if (fill != raster + width * height) |
| 449 |
{ |
| 450 |
fprintf(stderr, "warning: wrong rastersize: %ld bytes\n", |
| 451 |
(long)(fill - raster)); |
| 452 |
fprintf(stderr, " instead of %ld bytes\n", |
| 453 |
(long)width * height); |
| 454 |
} |
| 455 |
return status; |
| 456 |
} |
| 457 |
|
| 458 |
/* |
| 459 |
* process - |
| 460 |
* Process a compression code. "clear" resets the code table. |
| 461 |
* Otherwise make a new code table entry, and output the bytes |
| 462 |
* associated with the code. |
| 463 |
*/ |
| 464 |
int process(register int code, unsigned char **fill) |
| 465 |
{ |
| 466 |
int incode; |
| 467 |
static unsigned char firstchar; |
| 468 |
|
| 469 |
if (code == clear) |
| 470 |
{ |
| 471 |
codesize = datasize + 1; |
| 472 |
codemask = (1 << codesize) - 1; |
| 473 |
avail = clear + 2; |
| 474 |
oldcode = -1; |
| 475 |
return 1; |
| 476 |
} |
| 477 |
|
| 478 |
if (oldcode == -1) |
| 479 |
{ |
| 480 |
if (code >= clear) |
| 481 |
{ |
| 482 |
fprintf(stderr, "bad input: code=%d is larger than clear=%d\n", |
| 483 |
code, clear); |
| 484 |
return 0; |
| 485 |
} |
| 486 |
if (*fill >= raster + width * height) |
| 487 |
{ |
| 488 |
fprintf(stderr, "raster full before eoi code\n"); |
| 489 |
return 0; |
| 490 |
} |
| 491 |
*(*fill)++ = suffix[code]; |
| 492 |
firstchar = oldcode = code; |
| 493 |
return 1; |
| 494 |
} |
| 495 |
if (code > avail) |
| 496 |
{ |
| 497 |
fprintf(stderr, "code %d too large for %d\n", code, avail); |
| 498 |
return 0; |
| 499 |
} |
| 500 |
|
| 501 |
incode = code; |
| 502 |
if (code == avail) |
| 503 |
{ /* the first code is always < avail */ |
| 504 |
*stackp++ = firstchar; |
| 505 |
code = oldcode; |
| 506 |
} |
| 507 |
while (code > clear) |
| 508 |
{ |
| 509 |
*stackp++ = suffix[code]; |
| 510 |
code = prefix[code]; |
| 511 |
} |
| 512 |
|
| 513 |
*stackp++ = firstchar = suffix[code]; |
| 514 |
prefix[avail] = oldcode; |
| 515 |
suffix[avail] = firstchar; |
| 516 |
avail++; |
| 517 |
|
| 518 |
if (((avail & codemask) == 0) && (avail < 4096)) |
| 519 |
{ |
| 520 |
codesize++; |
| 521 |
codemask += avail; |
| 522 |
} |
| 523 |
oldcode = incode; |
| 524 |
do |
| 525 |
{ |
| 526 |
if (*fill >= raster + width * height) |
| 527 |
{ |
| 528 |
fprintf(stderr, "raster full before eoi code\n"); |
| 529 |
return 0; |
| 530 |
} |
| 531 |
*(*fill)++ = *--stackp; |
| 532 |
} while (stackp > stack); |
| 533 |
return 1; |
| 534 |
} |
| 535 |
|
| 536 |
/* |
| 537 |
* initcolors - |
| 538 |
* Convert a color map (local or global) to arrays with R, G and B |
| 539 |
* values. |
| 540 |
* |
| 541 |
*/ |
| 542 |
void initcolors(unsigned char colormap[COLSIZE][3], int ncolors) |
| 543 |
{ |
| 544 |
register int i; |
| 545 |
|
| 546 |
for (i = 0; i < ncolors; i++) |
| 547 |
{ |
| 548 |
red[i] = gamtab[colormap[i][0]]; |
| 549 |
green[i] = gamtab[colormap[i][1]]; |
| 550 |
blue[i] = gamtab[colormap[i][2]]; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
void rasterize(int interleaved, char *mode) |
| 555 |
{ |
| 556 |
register unsigned long row; |
| 557 |
unsigned char *newras; |
| 558 |
unsigned char *ras; |
| 559 |
TIFF *tif; |
| 560 |
tstrip_t strip; |
| 561 |
tsize_t stripsize; |
| 562 |
|
| 563 |
if ((newras = (unsigned char *)_TIFFmalloc(width * height + EXTRAFUDGE)) == |
| 564 |
NULL) |
| 565 |
{ |
| 566 |
fprintf(stderr, "not enough memory for image\n"); |
| 567 |
return; |
| 568 |
} |
| 569 |
#define DRAWSEGMENT(offset, step) \ |
| 570 |
{ \ |
| 571 |
for (row = offset; row < height; row += step) \ |
| 572 |
{ \ |
| 573 |
_TIFFmemcpy(newras + row * width, ras, width); \ |
| 574 |
ras += width; \ |
| 575 |
} \ |
| 576 |
} |
| 577 |
ras = raster; |
| 578 |
if (interleaved) |
| 579 |
{ |
| 580 |
DRAWSEGMENT(0, 8); |
| 581 |
DRAWSEGMENT(4, 8); |
| 582 |
DRAWSEGMENT(2, 4); |
| 583 |
DRAWSEGMENT(1, 2); |
| 584 |
} |
| 585 |
else |
| 586 |
DRAWSEGMENT(0, 1); |
| 587 |
#undef DRAWSEGMENT |
| 588 |
|
| 589 |
tif = TIFFOpen(imagename, mode); |
| 590 |
if (!tif) |
| 591 |
{ |
| 592 |
TIFFError(imagename, "Can not open output image"); |
| 593 |
exit(-1); |
| 594 |
} |
| 595 |
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32_t)width); |
| 596 |
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32_t)height); |
| 597 |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE); |
| 598 |
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 599 |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); |
| 600 |
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); |
| 601 |
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, |
| 602 |
rowsperstrip = TIFFDefaultStripSize(tif, rowsperstrip)); |
| 603 |
TIFFSetField(tif, TIFFTAG_COMPRESSION, compression); |
| 604 |
switch (compression) |
| 605 |
{ |
| 606 |
case COMPRESSION_LZW: |
| 607 |
case COMPRESSION_ADOBE_DEFLATE: |
| 608 |
case COMPRESSION_DEFLATE: |
| 609 |
if (predictor != 0) |
| 610 |
TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor); |
| 611 |
break; |
| 612 |
} |
| 613 |
TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue); |
| 614 |
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); |
| 615 |
strip = 0; |
| 616 |
stripsize = TIFFStripSize(tif); |
| 617 |
for (row = 0; row < height; row += rowsperstrip) |
| 618 |
{ |
| 619 |
if (rowsperstrip > height - row) |
| 620 |
{ |
| 621 |
rowsperstrip = height - row; |
| 622 |
stripsize = TIFFVStripSize(tif, rowsperstrip); |
| 623 |
} |
| 624 |
if (TIFFWriteEncodedStrip(tif, strip, newras + row * width, stripsize) < |
| 625 |
0) |
| 626 |
break; |
| 627 |
strip++; |
| 628 |
} |
| 629 |
TIFFClose(tif); |
| 630 |
|
| 631 |
_TIFFfree(newras); |
| 632 |
} |