|
roentgen |
b75cab |
/* $Id: gif2tiff.c,v 1.12 2010-12-15 00:22:44 faxguy Exp $ */
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* Copyright (c) 1990-1997 Sam Leffler
|
|
roentgen |
b75cab |
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
* Permission to use, copy, modify, distribute, and sell this software and
|
|
roentgen |
b75cab |
* its documentation for any purpose is hereby granted without fee, provided
|
|
roentgen |
b75cab |
* that (i) the above copyright notices and this permission notice appear in
|
|
roentgen |
b75cab |
* all copies of the software and related documentation, and (ii) the names of
|
|
roentgen |
b75cab |
* Sam Leffler and Silicon Graphics may not be used in any advertising or
|
|
roentgen |
b75cab |
* publicity relating to the software without the specific, prior written
|
|
roentgen |
b75cab |
* permission of Sam Leffler and Silicon Graphics.
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|
roentgen |
b75cab |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|
roentgen |
b75cab |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
|
|
roentgen |
b75cab |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
|
|
roentgen |
b75cab |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
roentgen |
b75cab |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
|
|
roentgen |
b75cab |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
roentgen |
b75cab |
* OF THIS SOFTWARE.
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* convert a GIF file into a TIFF file.
|
|
roentgen |
b75cab |
* based on Paul Haeberli's fromgif program which in turn is
|
|
roentgen |
b75cab |
* based on a GIF file reader by Marcel J.E. Mol March 23 1989
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
* if input is 320 by 200 pixel aspect is probably 1.2
|
|
roentgen |
b75cab |
* if input is 640 350 pixel aspect is probably 1.37
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
#include "tif_config.h"
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#include <stdio.h></stdio.h>
|
|
roentgen |
b75cab |
#include <stdlib.h></stdlib.h>
|
|
roentgen |
b75cab |
#include <string.h></string.h>
|
|
roentgen |
b75cab |
#include <math.h></math.h>
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#ifdef HAVE_UNISTD_H
|
|
roentgen |
b75cab |
# include <unistd.h></unistd.h>
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#ifdef NEED_LIBPORT
|
|
roentgen |
b75cab |
# include "libport.h"
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#include "tiffio.h"
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#define GIFGAMMA (1.5) /* smaller makes output img brighter */
|
|
roentgen |
b75cab |
#define IMAX 0xffff /* max intensity value */
|
|
roentgen |
b75cab |
#define EXTRAFUDGE 128 /* some people write BAD .gif files */
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#define streq(a,b) (strcmp(a,b) == 0)
|
|
roentgen |
b75cab |
#define strneq(a,b,n) (strncmp(a,b,n) == 0)
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
unsigned short gamtab[256];
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
void
|
|
roentgen |
b75cab |
makegamtab(float gam)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
int i;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
for(i=0; i<256; i++)
|
|
roentgen |
b75cab |
gamtab[i] = (unsigned short) (IMAX*pow(i/255.0,gam)+0.5);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
char* stuff[] = {
|
|
roentgen |
b75cab |
"usage: gif2tiff [options] input.gif output.tif",
|
|
roentgen |
b75cab |
"where options are:",
|
|
roentgen |
b75cab |
" -r # make each strip have no more than # rows",
|
|
roentgen |
b75cab |
"",
|
|
roentgen |
b75cab |
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding",
|
|
roentgen |
b75cab |
" -c zip[:opts] compress output with deflate encoding",
|
|
roentgen |
b75cab |
" -c packbits compress output with packbits encoding",
|
|
roentgen |
b75cab |
" -c none use no compression algorithm on output",
|
|
roentgen |
b75cab |
"",
|
|
roentgen |
b75cab |
"LZW and deflate options:",
|
|
roentgen |
b75cab |
" # set predictor value",
|
|
roentgen |
b75cab |
"For example, -c lzw:2 to get LZW-encoded data with horizontal differencing",
|
|
roentgen |
b75cab |
NULL
|
|
roentgen |
b75cab |
};
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static void
|
|
roentgen |
b75cab |
usage(void)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
char buf[BUFSIZ];
|
|
roentgen |
b75cab |
int i;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
setbuf(stderr, buf);
|
|
roentgen |
b75cab |
fprintf(stderr, "%s\n\n", TIFFGetVersion());
|
|
roentgen |
b75cab |
for (i = 0; stuff[i] != NULL; i++)
|
|
roentgen |
b75cab |
fprintf(stderr, "%s\n", stuff[i]);
|
|
roentgen |
b75cab |
exit(-1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#define COLSIZE 256
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
unsigned char *stackp;
|
|
roentgen |
b75cab |
unsigned int prefix[4096];
|
|
roentgen |
b75cab |
unsigned char suffix[4096];
|
|
roentgen |
b75cab |
unsigned char stack[4096];
|
|
roentgen |
b75cab |
int datasize,codesize,codemask; /* Decoder working variables */
|
|
roentgen |
b75cab |
int clear,eoi; /* Special code values */
|
|
roentgen |
b75cab |
int avail, oldcode;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
FILE *infile;
|
|
roentgen |
b75cab |
int global; /* Is there a global color map? */
|
|
roentgen |
b75cab |
int globalbits; /* Number of bits of global colors */
|
|
roentgen |
b75cab |
unsigned char globalmap[COLSIZE][3];/* RGB values for global color map */
|
|
roentgen |
b75cab |
unsigned char *raster; /* Decoded image data */
|
|
roentgen |
b75cab |
unsigned long width, height;
|
|
roentgen |
b75cab |
unsigned short red[COLSIZE];
|
|
roentgen |
b75cab |
unsigned short green[COLSIZE];
|
|
roentgen |
b75cab |
unsigned short blue[COLSIZE];
|
|
roentgen |
b75cab |
char *filename, *imagename;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static uint16 compression = COMPRESSION_PACKBITS;
|
|
roentgen |
b75cab |
static uint16 predictor = 0;
|
|
roentgen |
b75cab |
static uint32 rowsperstrip = (uint32) -1;
|
|
roentgen |
b75cab |
static int processCompressOptions(char*);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
int convert(void);
|
|
roentgen |
b75cab |
int checksignature(void);
|
|
roentgen |
b75cab |
void readscreen(void);
|
|
roentgen |
b75cab |
int readgifimage(char*);
|
|
roentgen |
b75cab |
void readextension(void);
|
|
roentgen |
b75cab |
int readraster(void);
|
|
roentgen |
b75cab |
int process(int, unsigned char**);
|
|
roentgen |
b75cab |
void initcolors(unsigned char [COLSIZE][3], int);
|
|
roentgen |
b75cab |
void rasterize(int, char*);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
int
|
|
roentgen |
b75cab |
main(int argc, char* argv[])
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
extern int optind;
|
|
roentgen |
b75cab |
extern char *optarg;
|
|
roentgen |
b75cab |
int c, status;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
while ((c = getopt(argc, argv, "c:r:")) != -1)
|
|
roentgen |
b75cab |
switch (c) {
|
|
roentgen |
b75cab |
case 'c': /* compression scheme */
|
|
roentgen |
b75cab |
if (!processCompressOptions(optarg))
|
|
roentgen |
b75cab |
usage();
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case 'r': /* rows/strip */
|
|
roentgen |
b75cab |
rowsperstrip = atoi(optarg);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case '?':
|
|
roentgen |
b75cab |
usage();
|
|
roentgen |
b75cab |
/*NOTREACHED*/
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (argc - optind != 2)
|
|
roentgen |
b75cab |
usage();
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
makegamtab(GIFGAMMA);
|
|
roentgen |
b75cab |
filename = argv[optind];
|
|
roentgen |
b75cab |
imagename = argv[optind+1];
|
|
roentgen |
b75cab |
if ((infile = fopen(imagename, "rb")) != NULL) {
|
|
roentgen |
b75cab |
int c;
|
|
roentgen |
b75cab |
fclose(infile);
|
|
roentgen |
b75cab |
printf("overwrite %s? ", imagename); fflush(stdout);
|
|
roentgen |
b75cab |
c = getc(stdin);
|
|
roentgen |
b75cab |
if (c != 'y' && c != 'Y')
|
|
roentgen |
b75cab |
return (1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if ((infile = fopen(filename, "rb")) == NULL) {
|
|
roentgen |
b75cab |
perror(filename);
|
|
roentgen |
b75cab |
return (1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
status = convert();
|
|
roentgen |
b75cab |
fclose(infile);
|
|
roentgen |
b75cab |
return (status);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
static int
|
|
roentgen |
b75cab |
processCompressOptions(char* opt)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
if (streq(opt, "none"))
|
|
roentgen |
b75cab |
compression = COMPRESSION_NONE;
|
|
roentgen |
b75cab |
else if (streq(opt, "packbits"))
|
|
roentgen |
b75cab |
compression = COMPRESSION_PACKBITS;
|
|
roentgen |
b75cab |
else if (strneq(opt, "lzw", 3)) {
|
|
roentgen |
b75cab |
char* cp = strchr(opt, ':');
|
|
roentgen |
b75cab |
if (cp)
|
|
roentgen |
b75cab |
predictor = atoi(cp+1);
|
|
roentgen |
b75cab |
compression = COMPRESSION_LZW;
|
|
roentgen |
b75cab |
} else if (strneq(opt, "zip", 3)) {
|
|
roentgen |
b75cab |
char* cp = strchr(opt, ':');
|
|
roentgen |
b75cab |
if (cp)
|
|
roentgen |
b75cab |
predictor = atoi(cp+1);
|
|
roentgen |
b75cab |
compression = COMPRESSION_DEFLATE;
|
|
roentgen |
b75cab |
} else
|
|
roentgen |
b75cab |
return (0);
|
|
roentgen |
b75cab |
return (1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
int
|
|
roentgen |
b75cab |
convert(void)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
int ch;
|
|
roentgen |
b75cab |
char* mode = "w";
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (!checksignature())
|
|
roentgen |
b75cab |
return (-1);
|
|
roentgen |
b75cab |
readscreen();
|
|
roentgen |
b75cab |
while ((ch = getc(infile)) != ';' && ch != EOF) {
|
|
roentgen |
b75cab |
switch (ch) {
|
|
roentgen |
b75cab |
case '\0': break; /* this kludge for non-standard files */
|
|
roentgen |
b75cab |
case ',': if (!readgifimage(mode))
|
|
roentgen |
b75cab |
return (-1);
|
|
roentgen |
b75cab |
mode = "a"; /* subsequent images append */
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
case '!': readextension();
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
default: fprintf(stderr, "illegal GIF block type\n");
|
|
roentgen |
b75cab |
return (-1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
return (0);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
int
|
|
roentgen |
b75cab |
checksignature(void)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
char buf[6];
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fread(buf,1,6,infile);
|
|
roentgen |
b75cab |
if (strncmp(buf,"GIF",3)) {
|
|
roentgen |
b75cab |
fprintf(stderr, "file is not a GIF file\n");
|
|
roentgen |
b75cab |
return 0;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (strncmp(&buf[3],"87a",3)) {
|
|
roentgen |
b75cab |
fprintf(stderr, "unknown GIF version number\n");
|
|
roentgen |
b75cab |
return 0;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* readscreen -
|
|
roentgen |
b75cab |
* Get information which is global to all the images stored
|
|
roentgen |
b75cab |
* in the file
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
void
|
|
roentgen |
b75cab |
readscreen(void)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
unsigned char buf[7];
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fread(buf,1,7,infile);
|
|
roentgen |
b75cab |
global = buf[4] & 0x80;
|
|
roentgen |
b75cab |
if (global) {
|
|
roentgen |
b75cab |
globalbits = (buf[4] & 0x07) + 1;
|
|
roentgen |
b75cab |
fread(globalmap,3,((size_t)1)<
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
int
|
|
roentgen |
b75cab |
readgifimage(char* mode)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
unsigned char buf[9];
|
|
roentgen |
b75cab |
int local, interleaved;
|
|
roentgen |
b75cab |
unsigned char localmap[256][3];
|
|
roentgen |
b75cab |
int localbits;
|
|
roentgen |
b75cab |
int status;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (fread(buf, 1, 9, infile) == 0) {
|
|
roentgen |
b75cab |
perror(filename);
|
|
roentgen |
b75cab |
return (0);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
width = buf[4] + (buf[5] << 8);
|
|
roentgen |
b75cab |
height = buf[6] + (buf[7] << 8);
|
|
roentgen |
b75cab |
local = buf[8] & 0x80;
|
|
roentgen |
b75cab |
interleaved = buf[8] & 0x40;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (local == 0 && global == 0) {
|
|
roentgen |
b75cab |
fprintf(stderr, "no colormap present for image\n");
|
|
roentgen |
b75cab |
return (0);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if ((raster = (unsigned char*) _TIFFmalloc(width*height+EXTRAFUDGE)) == NULL) {
|
|
roentgen |
b75cab |
fprintf(stderr, "not enough memory for image\n");
|
|
roentgen |
b75cab |
return (0);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (local) {
|
|
roentgen |
b75cab |
localbits = (buf[8] & 0x7) + 1;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fprintf(stderr, " local colors: %d\n", 1<
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
fread(localmap, 3, ((size_t)1)<
|
|
roentgen |
b75cab |
initcolors(localmap, 1<
|
|
roentgen |
b75cab |
} else if (global) {
|
|
roentgen |
b75cab |
initcolors(globalmap, 1<
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if ((status = readraster()))
|
|
roentgen |
b75cab |
rasterize(interleaved, mode);
|
|
roentgen |
b75cab |
_TIFFfree(raster);
|
|
roentgen |
b75cab |
return status;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* readextension -
|
|
roentgen |
b75cab |
* Read a GIF extension block (and do nothing with it).
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
void
|
|
roentgen |
b75cab |
readextension(void)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
int count;
|
|
roentgen |
b75cab |
char buf[255];
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
(void) getc(infile);
|
|
roentgen |
b75cab |
while ((count = getc(infile)))
|
|
roentgen |
b75cab |
fread(buf, 1, count, infile);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* readraster -
|
|
roentgen |
b75cab |
* Decode a raster image
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
int
|
|
roentgen |
b75cab |
readraster(void)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
unsigned char *fill = raster;
|
|
roentgen |
b75cab |
unsigned char buf[255];
|
|
roentgen |
b75cab |
register int bits=0;
|
|
roentgen |
b75cab |
register unsigned long datum=0;
|
|
roentgen |
b75cab |
register unsigned char *ch;
|
|
roentgen |
b75cab |
register int count, code;
|
|
roentgen |
b75cab |
int status = 1;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
datasize = getc(infile);
|
|
roentgen |
b75cab |
clear = 1 << datasize;
|
|
roentgen |
b75cab |
eoi = clear + 1;
|
|
roentgen |
b75cab |
avail = clear + 2;
|
|
roentgen |
b75cab |
oldcode = -1;
|
|
roentgen |
b75cab |
codesize = datasize + 1;
|
|
roentgen |
b75cab |
codemask = (1 << codesize) - 1;
|
|
roentgen |
b75cab |
for (code = 0; code < clear; code++) {
|
|
roentgen |
b75cab |
prefix[code] = 0;
|
|
roentgen |
b75cab |
suffix[code] = code;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
stackp = stack;
|
|
roentgen |
b75cab |
for (count = getc(infile); count > 0; count = getc(infile)) {
|
|
roentgen |
b75cab |
fread(buf,1,count,infile);
|
|
roentgen |
b75cab |
for (ch=buf; count-- > 0; ch++) {
|
|
roentgen |
b75cab |
datum += (unsigned long) *ch << bits;
|
|
roentgen |
b75cab |
bits += 8;
|
|
roentgen |
b75cab |
while (bits >= codesize) {
|
|
roentgen |
b75cab |
code = datum & codemask;
|
|
roentgen |
b75cab |
datum >>= codesize;
|
|
roentgen |
b75cab |
bits -= codesize;
|
|
roentgen |
b75cab |
if (code == eoi) { /* This kludge put in */
|
|
roentgen |
b75cab |
goto exitloop; /* because some GIF files*/
|
|
roentgen |
b75cab |
} /* aren't standard */
|
|
roentgen |
b75cab |
if (!process(code, &fill)) {
|
|
roentgen |
b75cab |
status = 0;
|
|
roentgen |
b75cab |
goto exitloop;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (fill >= raster + width*height) {
|
|
roentgen |
b75cab |
fprintf(stderr, "raster full before eoi code\n");
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
exitloop:
|
|
roentgen |
b75cab |
if (fill != raster + width*height) {
|
|
roentgen |
b75cab |
fprintf(stderr, "warning: wrong rastersize: %ld bytes\n",
|
|
roentgen |
b75cab |
(long) (fill-raster));
|
|
roentgen |
b75cab |
fprintf(stderr, " instead of %ld bytes\n",
|
|
roentgen |
b75cab |
(long) width*height);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
return status;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* process -
|
|
roentgen |
b75cab |
* Process a compression code. "clear" resets the code table.
|
|
roentgen |
b75cab |
* Otherwise make a new code table entry, and output the bytes
|
|
roentgen |
b75cab |
* associated with the code.
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
int
|
|
roentgen |
b75cab |
process(register int code, unsigned char** fill)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
int incode;
|
|
roentgen |
b75cab |
static unsigned char firstchar;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (code == clear) {
|
|
roentgen |
b75cab |
codesize = datasize + 1;
|
|
roentgen |
b75cab |
codemask = (1 << codesize) - 1;
|
|
roentgen |
b75cab |
avail = clear + 2;
|
|
roentgen |
b75cab |
oldcode = -1;
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (oldcode == -1) {
|
|
roentgen |
b75cab |
*(*fill)++ = suffix[code];
|
|
roentgen |
b75cab |
firstchar = oldcode = code;
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (code > avail) {
|
|
roentgen |
b75cab |
fprintf(stderr, "code %d too large for %d\n", code, avail);
|
|
roentgen |
b75cab |
return 0;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
incode = code;
|
|
roentgen |
b75cab |
if (code == avail) { /* the first code is always < avail */
|
|
roentgen |
b75cab |
*stackp++ = firstchar;
|
|
roentgen |
b75cab |
code = oldcode;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
while (code > clear) {
|
|
roentgen |
b75cab |
*stackp++ = suffix[code];
|
|
roentgen |
b75cab |
code = prefix[code];
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
*stackp++ = firstchar = suffix[code];
|
|
roentgen |
b75cab |
prefix[avail] = oldcode;
|
|
roentgen |
b75cab |
suffix[avail] = firstchar;
|
|
roentgen |
b75cab |
avail++;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (((avail & codemask) == 0) && (avail < 4096)) {
|
|
roentgen |
b75cab |
codesize++;
|
|
roentgen |
b75cab |
codemask += avail;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
oldcode = incode;
|
|
roentgen |
b75cab |
do {
|
|
roentgen |
b75cab |
*(*fill)++ = *--stackp;
|
|
roentgen |
b75cab |
} while (stackp > stack);
|
|
roentgen |
b75cab |
return 1;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* initcolors -
|
|
roentgen |
b75cab |
* Convert a color map (local or global) to arrays with R, G and B
|
|
roentgen |
b75cab |
* values.
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
void
|
|
roentgen |
b75cab |
initcolors(unsigned char colormap[COLSIZE][3], int ncolors)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
register int i;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
for (i = 0; i < ncolors; i++) {
|
|
roentgen |
b75cab |
red[i] = gamtab[colormap[i][0]];
|
|
roentgen |
b75cab |
green[i] = gamtab[colormap[i][1]];
|
|
roentgen |
b75cab |
blue[i] = gamtab[colormap[i][2]];
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
void
|
|
roentgen |
b75cab |
rasterize(int interleaved, char* mode)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
register unsigned long row;
|
|
roentgen |
b75cab |
unsigned char *newras;
|
|
roentgen |
b75cab |
unsigned char *ras;
|
|
roentgen |
b75cab |
TIFF *tif;
|
|
roentgen |
b75cab |
tstrip_t strip;
|
|
roentgen |
b75cab |
tsize_t stripsize;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if ((newras = (unsigned char*) _TIFFmalloc(width*height+EXTRAFUDGE)) == NULL) {
|
|
roentgen |
b75cab |
fprintf(stderr, "not enough memory for image\n");
|
|
roentgen |
b75cab |
return;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
#define DRAWSEGMENT(offset, step) { \
|
|
roentgen |
b75cab |
for (row = offset; row < height; row += step) { \
|
|
roentgen |
b75cab |
_TIFFmemcpy(newras + row*width, ras, width);\
|
|
roentgen |
b75cab |
ras += width; \
|
|
roentgen |
b75cab |
} \
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
ras = raster;
|
|
roentgen |
b75cab |
if (interleaved) {
|
|
roentgen |
b75cab |
DRAWSEGMENT(0, 8);
|
|
roentgen |
b75cab |
DRAWSEGMENT(4, 8);
|
|
roentgen |
b75cab |
DRAWSEGMENT(2, 4);
|
|
roentgen |
b75cab |
DRAWSEGMENT(1, 2);
|
|
roentgen |
b75cab |
} else
|
|
roentgen |
b75cab |
DRAWSEGMENT(0, 1);
|
|
roentgen |
b75cab |
#undef DRAWSEGMENT
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
tif = TIFFOpen(imagename, mode);
|
|
roentgen |
b75cab |
if (!tif) {
|
|
roentgen |
b75cab |
TIFFError(imagename,"Can not open output image");
|
|
roentgen |
b75cab |
exit(-1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (uint32) width);
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (uint32) height);
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP,
|
|
roentgen |
b75cab |
rowsperstrip = TIFFDefaultStripSize(tif, rowsperstrip));
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_COMPRESSION, compression);
|
|
roentgen |
b75cab |
switch (compression) {
|
|
roentgen |
b75cab |
case COMPRESSION_LZW:
|
|
roentgen |
b75cab |
case COMPRESSION_DEFLATE:
|
|
roentgen |
b75cab |
if (predictor != 0)
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor);
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_COLORMAP, red, green, blue);
|
|
roentgen |
b75cab |
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
|
|
roentgen |
b75cab |
strip = 0;
|
|
roentgen |
b75cab |
stripsize = TIFFStripSize(tif);
|
|
roentgen |
b75cab |
for (row=0; row
|
|
roentgen |
b75cab |
if (rowsperstrip > height-row) {
|
|
roentgen |
b75cab |
rowsperstrip = height-row;
|
|
roentgen |
b75cab |
stripsize = TIFFVStripSize(tif, rowsperstrip);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (TIFFWriteEncodedStrip(tif, strip, newras+row*width, stripsize) < 0)
|
|
roentgen |
b75cab |
break;
|
|
roentgen |
b75cab |
strip++;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
TIFFClose(tif);
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
_TIFFfree(newras);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/* vim: set ts=8 sts=8 sw=8 noet: */
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* Local Variables:
|
|
roentgen |
b75cab |
* mode: c
|
|
roentgen |
b75cab |
* c-basic-offset: 8
|
|
roentgen |
b75cab |
* fill-column: 78
|
|
roentgen |
b75cab |
* End:
|
|
roentgen |
b75cab |
*/
|