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
" (no longer supported by default due to Unisys patent enforcement)",
74
" -c zip[:opts] compress output with deflate encoding",
75
" -c packbits compress output with packbits encoding",
76
" -c none use no compression algorithm on output",
78
"LZW and deflate options:",
79
" # set predictor value",
80
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
91
fprintf(stderr, "%s\n\n", TIFFGetVersion());
92
for (i = 0; stuff[i] != NULL; i++)
93
fprintf(stderr, "%s\n", stuff[i]);
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 */
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;
118
static uint16 compression = COMPRESSION_PACKBITS;
119
static uint16 predictor = 0;
120
static uint32 rowsperstrip = (uint32) -1;
121
static int processCompressOptions(char*);
124
int checksignature(void);
125
void readscreen(void);
126
int readgifimage(char*);
127
void 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*);
134
main(int argc, char* argv[])
140
while ((c = getopt(argc, argv, "c:r:")) != -1)
142
case 'c': /* compression scheme */
143
if (!processCompressOptions(optarg))
146
case 'r': /* rows/strip */
147
rowsperstrip = atoi(optarg);
153
if (argc - optind != 2)
156
makegamtab(GIFGAMMA);
157
filename = argv[optind];
158
imagename = argv[optind+1];
159
if ((infile = fopen(imagename, "r" BINMODE)) != NULL) {
162
printf("overwrite %s? ", imagename); fflush(stdout);
164
if (c != 'y' && c != 'Y')
167
if ((infile = fopen(filename, "r" BINMODE)) == NULL) {
177
processCompressOptions(char* opt)
179
if (streq(opt, "none"))
180
compression = COMPRESSION_NONE;
181
else if (streq(opt, "packbits"))
182
compression = COMPRESSION_PACKBITS;
183
else if (strneq(opt, "lzw", 3)) {
184
char* cp = strchr(opt, ':');
186
predictor = atoi(cp+1);
187
compression = COMPRESSION_LZW;
188
} else if (strneq(opt, "zip", 3)) {
189
char* cp = strchr(opt, ':');
191
predictor = atoi(cp+1);
192
compression = COMPRESSION_DEFLATE;
204
if (!checksignature())
207
while ((ch = getc(infile)) != ';' && ch != EOF) {
209
case '\0': break; /* this kludge for non-standard files */
210
case ',': if (!readgifimage(mode))
212
mode = "a"; /* subsequent images append */
214
case '!': readextension();
216
default: fprintf(stderr, "illegal GIF block type\n");
228
fread(buf,1,6,infile);
229
if (strncmp(buf,"GIF",3)) {
230
fprintf(stderr, "file is not a GIF file\n");
233
if (strncmp(&buf[3],"87a",3)) {
234
fprintf(stderr, "unknown GIF version number\n");
242
* Get information which is global to all the images stored
248
unsigned char buf[7];
250
fread(buf,1,7,infile);
251
global = buf[4] & 0x80;
253
globalbits = (buf[4] & 0x07) + 1;
254
fread(globalmap,3,1<<globalbits,infile);
259
readgifimage(char* mode)
261
unsigned char buf[9];
262
int local, interleaved;
263
unsigned char localmap[256][3];
267
if (fread(buf, 1, 9, infile) == 0) {
271
width = buf[4] + (buf[5] << 8);
272
height = buf[6] + (buf[7] << 8);
273
local = buf[8] & 0x80;
274
interleaved = buf[8] & 0x40;
276
if (local == 0 && global == 0) {
277
fprintf(stderr, "no colormap present for image\n");
280
if ((raster = (unsigned char*) _TIFFmalloc(width*height+EXTRAFUDGE)) == NULL) {
281
fprintf(stderr, "not enough memory for image\n");
285
localbits = (buf[8] & 0x7) + 1;
287
fprintf(stderr, " local colors: %d\n", 1<<localbits);
289
fread(localmap, 3, 1<<localbits, infile);
290
initcolors(localmap, 1<<localbits);
292
initcolors(globalmap, 1<<globalbits);
294
if (status = readraster())
295
rasterize(interleaved, mode);
302
* Read a GIF extension block (and do nothing with it).
312
while (count = getc(infile))
313
fread(buf, 1, count, infile);
318
* Decode a raster image
324
unsigned char *fill = raster;
325
unsigned char buf[255];
327
register unsigned long datum=0;
328
register unsigned char *ch;
329
register int count, code;
332
datasize = getc(infile);
333
clear = 1 << datasize;
337
codesize = datasize + 1;
338
codemask = (1 << codesize) - 1;
339
for (code = 0; code < clear; code++) {
344
for (count = getc(infile); count > 0; count = getc(infile)) {
345
fread(buf,1,count,infile);
346
for (ch=buf; count-- > 0; ch++) {
347
datum += (unsigned long) *ch << bits;
349
while (bits >= codesize) {
350
code = datum & codemask;
353
if (code == eoi) { /* This kludge put in */
354
goto exitloop; /* because some GIF files*/
355
} /* aren't standard */
356
if (!process(code, &fill)) {
362
if (fill >= raster + width*height) {
363
fprintf(stderr, "raster full before eoi code\n");
368
if (fill != raster + width*height) {
369
fprintf(stderr, "warning: wrong rastersize: %ld bytes\n",
370
(long) (fill-raster));
371
fprintf(stderr, " instead of %ld bytes\n",
372
(long) width*height);
379
* Process a compression code. "clear" resets the code table.
380
* Otherwise make a new code table entry, and output the bytes
381
* associated with the code.
384
process(register int code, unsigned char** fill)
387
static unsigned char firstchar;
390
codesize = datasize + 1;
391
codemask = (1 << codesize) - 1;
398
*(*fill)++ = suffix[code];
399
firstchar = oldcode = code;
403
fprintf(stderr, "code %d too large for %d\n", code, avail);
408
if (code == avail) { /* the first code is always < avail */
409
*stackp++ = firstchar;
412
while (code > clear) {
413
*stackp++ = suffix[code];
417
*stackp++ = firstchar = suffix[code];
418
prefix[avail] = oldcode;
419
suffix[avail] = firstchar;
422
if (((avail & codemask) == 0) && (avail < 4096)) {
428
*(*fill)++ = *--stackp;
429
} while (stackp > stack);
435
* Convert a color map (local or global) to arrays with R, G and B
440
initcolors(unsigned char colormap[COLSIZE][3], int ncolors)
444
for (i = 0; i < ncolors; i++) {
445
red[i] = gamtab[colormap[i][0]];
446
green[i] = gamtab[colormap[i][1]];
447
blue[i] = gamtab[colormap[i][2]];
452
rasterize(int interleaved, char* mode)
455
unsigned char *newras;
461
if ((newras = (unsigned char*) _TIFFmalloc(width*height+EXTRAFUDGE)) == NULL) {
462
fprintf(stderr, "not enough memory for image\n");
465
#define DRAWSEGMENT(offset, step) { \
466
for (row = offset; row < height; row += step) { \
467
_TIFFmemcpy(newras + row*width, ras, width);\
481
tif = TIFFOpen(imagename, mode);
483
TIFFError(imagename,"Can not open output image");
486
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32) width);
487
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32) height);
488
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
489
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
490
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
491
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
492
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
493
rowsperstrip = TIFFDefaultStripSize(tif, rowsperstrip));
494
TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
495
switch (compression) {
496
case COMPRESSION_LZW:
497
case COMPRESSION_DEFLATE:
499
TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
502
TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
503
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
505
stripsize = TIFFStripSize(tif);
506
for (row=0; row<height; row += rowsperstrip) {
507
if (TIFFWriteEncodedStrip(tif, strip, newras+row*width, stripsize) < 0)