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