1
/* $Header: /cvsroot/osrs/libtiff/tools/gif2tiff.c,v 1.4 2003/03/12 14:05:05 dron 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
28
* convert a GIF file into a TIFF file.
29
* based on Paul Haeberli's fromgif program which in turn is
30
* based on a GIF file reader by Marcel J.E. Mol March 23 1989
32
* if input is 320 by 200 pixel aspect is probably 1.2
33
* if input is 640 350 pixel aspect is probably 1.37
43
#if defined(_WINDOWS) || defined(MSDOS)
49
#define GIFGAMMA (1.5) /* smaller makes output img brighter */
50
#define IMAX 0xffff /* max intensity value */
51
#define EXTRAFUDGE 128 /* some people write BAD .gif files */
53
#define streq(a,b) (strcmp(a,b) == 0)
54
#define strneq(a,b,n) (strncmp(a,b,n) == 0)
56
unsigned short gamtab[256];
64
gamtab[i] = IMAX*pow(i/255.0,gam)+0.5;
68
"usage: gif2tiff [options] input.gif output.tif",
70
" -r # make each strip have no more than # rows",
72
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
73
" -c zip[:opts] compress output with deflate encoding",
74
" -c packbits compress output with packbits encoding",
75
" -c none use no compression algorithm on output",
77
"LZW and deflate options:",
78
" # set predictor value",
79
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
90
fprintf(stderr, "%s\n\n", TIFFGetVersion());
91
for (i = 0; stuff[i] != NULL; i++)
92
fprintf(stderr, "%s\n", stuff[i]);
98
unsigned char *stackp;
99
unsigned int prefix[4096];
100
unsigned char suffix[4096];
101
unsigned char stack[4096];
102
int datasize,codesize,codemask; /* Decoder working variables */
103
int clear,eoi; /* Special code values */
107
int global; /* Is there a global color map? */
108
int globalbits; /* Number of bits of global colors */
109
unsigned char globalmap[COLSIZE][3];/* RGB values for global color map */
110
unsigned char *raster; /* Decoded image data */
111
unsigned long width, height;
112
unsigned short red[COLSIZE];
113
unsigned short green[COLSIZE];
114
unsigned short blue[COLSIZE];
115
char *filename, *imagename;
117
static uint16 compression = COMPRESSION_PACKBITS;
118
static uint16 predictor = 0;
119
static uint32 rowsperstrip = (uint32) -1;
120
static int processCompressOptions(char*);
123
int checksignature(void);
124
void readscreen(void);
125
int readgifimage(char*);
126
void readextension(void);
127
int readraster(void);
128
int process(int, unsigned char**);
129
void initcolors(unsigned char [COLSIZE][3], int);
130
void rasterize(int, char*);
133
main(int argc, char* argv[])
139
while ((c = getopt(argc, argv, "c:r:")) != -1)
141
case 'c': /* compression scheme */
142
if (!processCompressOptions(optarg))
145
case 'r': /* rows/strip */
146
rowsperstrip = atoi(optarg);
152
if (argc - optind != 2)
155
makegamtab(GIFGAMMA);
156
filename = argv[optind];
157
imagename = argv[optind+1];
158
if ((infile = fopen(imagename, "r" BINMODE)) != NULL) {
161
printf("overwrite %s? ", imagename); fflush(stdout);
163
if (c != 'y' && c != 'Y')
166
if ((infile = fopen(filename, "r" BINMODE)) == NULL) {
176
processCompressOptions(char* opt)
178
if (streq(opt, "none"))
179
compression = COMPRESSION_NONE;
180
else if (streq(opt, "packbits"))
181
compression = COMPRESSION_PACKBITS;
182
else if (strneq(opt, "lzw", 3)) {
183
char* cp = strchr(opt, ':');
185
predictor = atoi(cp+1);
186
compression = COMPRESSION_LZW;
187
} else if (strneq(opt, "zip", 3)) {
188
char* cp = strchr(opt, ':');
190
predictor = atoi(cp+1);
191
compression = COMPRESSION_DEFLATE;
203
if (!checksignature())
206
while ((ch = getc(infile)) != ';' && ch != EOF) {
208
case '\0': break; /* this kludge for non-standard files */
209
case ',': if (!readgifimage(mode))
211
mode = "a"; /* subsequent images append */
213
case '!': readextension();
215
default: fprintf(stderr, "illegal GIF block type\n");
227
fread(buf,1,6,infile);
228
if (strncmp(buf,"GIF",3)) {
229
fprintf(stderr, "file is not a GIF file\n");
232
if (strncmp(&buf[3],"87a",3)) {
233
fprintf(stderr, "unknown GIF version number\n");
241
* Get information which is global to all the images stored
247
unsigned char buf[7];
249
fread(buf,1,7,infile);
250
global = buf[4] & 0x80;
252
globalbits = (buf[4] & 0x07) + 1;
253
fread(globalmap,3,1<<globalbits,infile);
258
readgifimage(char* mode)
260
unsigned char buf[9];
261
int local, interleaved;
262
unsigned char localmap[256][3];
266
if (fread(buf, 1, 9, infile) == 0) {
270
width = buf[4] + (buf[5] << 8);
271
height = buf[6] + (buf[7] << 8);
272
local = buf[8] & 0x80;
273
interleaved = buf[8] & 0x40;
275
if (local == 0 && global == 0) {
276
fprintf(stderr, "no colormap present for image\n");
279
if ((raster = (unsigned char*) _TIFFmalloc(width*height+EXTRAFUDGE)) == NULL) {
280
fprintf(stderr, "not enough memory for image\n");
284
localbits = (buf[8] & 0x7) + 1;
286
fprintf(stderr, " local colors: %d\n", 1<<localbits);
288
fread(localmap, 3, 1<<localbits, infile);
289
initcolors(localmap, 1<<localbits);
291
initcolors(globalmap, 1<<globalbits);
293
if (status = readraster())
294
rasterize(interleaved, mode);
301
* Read a GIF extension block (and do nothing with it).
311
while (count = getc(infile))
312
fread(buf, 1, count, infile);
317
* Decode a raster image
323
unsigned char *fill = raster;
324
unsigned char buf[255];
326
register unsigned long datum=0;
327
register unsigned char *ch;
328
register int count, code;
331
datasize = getc(infile);
332
clear = 1 << datasize;
336
codesize = datasize + 1;
337
codemask = (1 << codesize) - 1;
338
for (code = 0; code < clear; code++) {
343
for (count = getc(infile); count > 0; count = getc(infile)) {
344
fread(buf,1,count,infile);
345
for (ch=buf; count-- > 0; ch++) {
346
datum += (unsigned long) *ch << bits;
348
while (bits >= codesize) {
349
code = datum & codemask;
352
if (code == eoi) { /* This kludge put in */
353
goto exitloop; /* because some GIF files*/
354
} /* aren't standard */
355
if (!process(code, &fill)) {
361
if (fill >= raster + width*height) {
362
fprintf(stderr, "raster full before eoi code\n");
367
if (fill != raster + width*height) {
368
fprintf(stderr, "warning: wrong rastersize: %ld bytes\n",
369
(long) (fill-raster));
370
fprintf(stderr, " instead of %ld bytes\n",
371
(long) width*height);
378
* Process a compression code. "clear" resets the code table.
379
* Otherwise make a new code table entry, and output the bytes
380
* associated with the code.
383
process(register int code, unsigned char** fill)
386
static unsigned char firstchar;
389
codesize = datasize + 1;
390
codemask = (1 << codesize) - 1;
397
*(*fill)++ = suffix[code];
398
firstchar = oldcode = code;
402
fprintf(stderr, "code %d too large for %d\n", code, avail);
407
if (code == avail) { /* the first code is always < avail */
408
*stackp++ = firstchar;
411
while (code > clear) {
412
*stackp++ = suffix[code];
416
*stackp++ = firstchar = suffix[code];
417
prefix[avail] = oldcode;
418
suffix[avail] = firstchar;
421
if (((avail & codemask) == 0) && (avail < 4096)) {
427
*(*fill)++ = *--stackp;
428
} while (stackp > stack);
434
* Convert a color map (local or global) to arrays with R, G and B
439
initcolors(unsigned char colormap[COLSIZE][3], int ncolors)
443
for (i = 0; i < ncolors; i++) {
444
red[i] = gamtab[colormap[i][0]];
445
green[i] = gamtab[colormap[i][1]];
446
blue[i] = gamtab[colormap[i][2]];
451
rasterize(int interleaved, char* mode)
454
unsigned char *newras;
460
if ((newras = (unsigned char*) _TIFFmalloc(width*height+EXTRAFUDGE)) == NULL) {
461
fprintf(stderr, "not enough memory for image\n");
464
#define DRAWSEGMENT(offset, step) { \
465
for (row = offset; row < height; row += step) { \
466
_TIFFmemcpy(newras + row*width, ras, width);\
480
tif = TIFFOpen(imagename, mode);
482
TIFFError(imagename,"Can not open output image");
485
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32) width);
486
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32) height);
487
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
488
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
489
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
490
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
491
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
492
rowsperstrip = TIFFDefaultStripSize(tif, rowsperstrip));
493
TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
494
switch (compression) {
495
case COMPRESSION_LZW:
496
case COMPRESSION_DEFLATE:
498
TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
501
TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
502
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
504
stripsize = TIFFStripSize(tif);
505
for (row=0; row<height; row += rowsperstrip) {
506
if (TIFFWriteEncodedStrip(tif, strip, newras+row*width, stripsize) < 0)