roentgen b75cab
/* $Id: tiffmedian.c,v 1.10 2010-03-10 18:56:50 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Apply median cut on an image.
roentgen b75cab
 *
roentgen b75cab
 * tiffmedian [-c n] [-f] input output
roentgen b75cab
 *     -C n		- set colortable size.  Default is 256.
roentgen b75cab
 *     -f		- use Floyd-Steinberg dithering.
roentgen b75cab
 *     -c lzw		- compress output with LZW 
roentgen b75cab
 *     -c none		- use no compression on output
roentgen b75cab
 *     -c packbits	- use packbits compression on output
roentgen b75cab
 *     -r n		- create output with n rows/strip of data
roentgen b75cab
 * (by default the compression scheme and rows/strip are taken
roentgen b75cab
 *  from the input file)
roentgen b75cab
 *
roentgen b75cab
 * Notes:
roentgen b75cab
 *
roentgen b75cab
 * [1] Floyd-Steinberg dither:
roentgen b75cab
 *  I should point out that the actual fractions we used were, assuming
roentgen b75cab
 *  you are at X, moving left to right:
roentgen b75cab
 *
roentgen b75cab
 *		    X     7/16
roentgen b75cab
 *	     3/16   5/16  1/16    
roentgen b75cab
 *
roentgen b75cab
 *  Note that the error goes to four neighbors, not three.  I think this
roentgen b75cab
 *  will probably do better (at least for black and white) than the
roentgen b75cab
 *  3/8-3/8-1/4 distribution, at the cost of greater processing.  I have
roentgen b75cab
 *  seen the 3/8-3/8-1/4 distribution described as "our" algorithm before,
roentgen b75cab
 *  but I have no idea who the credit really belongs to.
roentgen b75cab
roentgen b75cab
 *  Also, I should add that if you do zig-zag scanning (see my immediately
roentgen b75cab
 *  previous message), it is sufficient (but not quite as good) to send
roentgen b75cab
 *  half the error one pixel ahead (e.g. to the right on lines you scan
roentgen b75cab
 *  left to right), and half one pixel straight down.  Again, this is for
roentgen b75cab
 *  black and white;  I've not tried it with color.
roentgen b75cab
 *  -- 
roentgen b75cab
 *					    Lou Steinberg
roentgen b75cab
 *
roentgen b75cab
 * [2] Color Image Quantization for Frame Buffer Display, Paul Heckbert,
roentgen b75cab
 *	Siggraph '82 proceedings, pp. 297-307
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
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	MAX_CMAP_SIZE	256
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
#define	COLOR_DEPTH	8
roentgen b75cab
#define	MAX_COLOR	256
roentgen b75cab
roentgen b75cab
#define	B_DEPTH		5		/* # bits/pixel to use */
roentgen b75cab
#define	B_LEN		(1L<
roentgen b75cab
roentgen b75cab
#define	C_DEPTH		2
roentgen b75cab
#define	C_LEN		(1L<
roentgen b75cab
roentgen b75cab
#define	COLOR_SHIFT	(COLOR_DEPTH-B_DEPTH)
roentgen b75cab
roentgen b75cab
typedef	struct colorbox {
roentgen b75cab
	struct	colorbox *next, *prev;
roentgen b75cab
	int	rmin, rmax;
roentgen b75cab
	int	gmin, gmax;
roentgen b75cab
	int	bmin, bmax;
roentgen b75cab
	uint32	total;
roentgen b75cab
} Colorbox;
roentgen b75cab
roentgen b75cab
typedef struct {
roentgen b75cab
	int	num_ents;
roentgen b75cab
	int	entries[MAX_CMAP_SIZE][2];
roentgen b75cab
} C_cell;
roentgen b75cab
roentgen b75cab
uint16	rm[MAX_CMAP_SIZE], gm[MAX_CMAP_SIZE], bm[MAX_CMAP_SIZE];
roentgen b75cab
int	num_colors;
roentgen b75cab
uint32	histogram[B_LEN][B_LEN][B_LEN];
roentgen b75cab
Colorbox *freeboxes;
roentgen b75cab
Colorbox *usedboxes;
roentgen b75cab
C_cell	**ColorCells;
roentgen b75cab
TIFF	*in, *out;
roentgen b75cab
uint32	rowsperstrip = (uint32) -1;
roentgen b75cab
uint16	compression = (uint16) -1;
roentgen b75cab
uint16	bitspersample = 1;
roentgen b75cab
uint16	samplesperpixel;
roentgen b75cab
uint32	imagewidth;
roentgen b75cab
uint32	imagelength;
roentgen b75cab
uint16	predictor = 0;
roentgen b75cab
roentgen b75cab
static	void get_histogram(TIFF*, Colorbox*);
roentgen b75cab
static	void splitbox(Colorbox*);
roentgen b75cab
static	void shrinkbox(Colorbox*);
roentgen b75cab
static	void map_colortable(void);
roentgen b75cab
static	void quant(TIFF*, TIFF*);
roentgen b75cab
static	void quant_fsdither(TIFF*, TIFF*);
roentgen b75cab
static	Colorbox* largest_box(void);
roentgen b75cab
roentgen b75cab
static	void usage(void);
roentgen b75cab
static	int processCompressOptions(char*);
roentgen b75cab
roentgen b75cab
#define	CopyField(tag, v) \
roentgen b75cab
	if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
main(int argc, char* argv[])
roentgen b75cab
{
roentgen b75cab
	int i, dither = 0;
roentgen b75cab
	uint16 shortv, config, photometric;
roentgen b75cab
	Colorbox *box_list, *ptr;
roentgen b75cab
	float floatv;
roentgen b75cab
	uint32 longv;
roentgen b75cab
	int c;
roentgen b75cab
	extern int optind;
roentgen b75cab
	extern char* optarg;
roentgen b75cab
roentgen b75cab
	num_colors = MAX_CMAP_SIZE;
roentgen b75cab
	while ((c = getopt(argc, argv, "c:C:r:f")) != -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 'C':		/* set colormap size */
roentgen b75cab
			num_colors = atoi(optarg);
roentgen b75cab
			if (num_colors > MAX_CMAP_SIZE) {
roentgen b75cab
				fprintf(stderr,
roentgen b75cab
				   "-c: colormap too big, max %d\n",
roentgen b75cab
				   MAX_CMAP_SIZE);
roentgen b75cab
				usage();
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case 'f':		/* dither */
roentgen b75cab
			dither = 1;
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
	in = TIFFOpen(argv[optind], "r");
roentgen b75cab
	if (in == NULL)
roentgen b75cab
		return (-1);
roentgen b75cab
	TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &imagewidth);
roentgen b75cab
	TIFFGetField(in, TIFFTAG_IMAGELENGTH, &imagelength);
roentgen b75cab
	TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample);
roentgen b75cab
	TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
roentgen b75cab
	if (bitspersample != 8 && bitspersample != 16) {
roentgen b75cab
		fprintf(stderr, "%s: Image must have at least 8-bits/sample\n",
roentgen b75cab
		    argv[optind]);
roentgen b75cab
		return (-3);
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photometric) ||
roentgen b75cab
	    photometric != PHOTOMETRIC_RGB || samplesperpixel < 3) {
roentgen b75cab
		fprintf(stderr, "%s: Image must have RGB data\n", argv[optind]);
roentgen b75cab
		return (-4);
roentgen b75cab
	}
roentgen b75cab
	TIFFGetField(in, TIFFTAG_PLANARCONFIG, &config);
roentgen b75cab
	if (config != PLANARCONFIG_CONTIG) {
roentgen b75cab
		fprintf(stderr, "%s: Can only handle contiguous data packing\n",
roentgen b75cab
		    argv[optind]);
roentgen b75cab
		return (-5);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * STEP 1:  create empty boxes
roentgen b75cab
	 */
roentgen b75cab
	usedboxes = NULL;
roentgen b75cab
	box_list = freeboxes = (Colorbox *)_TIFFmalloc(num_colors*sizeof (Colorbox));
roentgen b75cab
	freeboxes[0].next = &freeboxes[1];
roentgen b75cab
	freeboxes[0].prev = NULL;
roentgen b75cab
	for (i = 1; i < num_colors-1; ++i) {
roentgen b75cab
		freeboxes[i].next = &freeboxes[i+1];
roentgen b75cab
		freeboxes[i].prev = &freeboxes[i-1];
roentgen b75cab
	}
roentgen b75cab
	freeboxes[num_colors-1].next = NULL;
roentgen b75cab
	freeboxes[num_colors-1].prev = &freeboxes[num_colors-2];
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * STEP 2: get histogram, initialize first box
roentgen b75cab
	 */
roentgen b75cab
	ptr = freeboxes;
roentgen b75cab
	freeboxes = ptr->next;
roentgen b75cab
	if (freeboxes)
roentgen b75cab
		freeboxes->prev = NULL;
roentgen b75cab
	ptr->next = usedboxes;
roentgen b75cab
	usedboxes = ptr;
roentgen b75cab
	if (ptr->next)
roentgen b75cab
		ptr->next->prev = ptr;
roentgen b75cab
	get_histogram(in, ptr);
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * STEP 3: continually subdivide boxes until no more free
roentgen b75cab
	 * boxes remain or until all colors assigned.
roentgen b75cab
	 */
roentgen b75cab
	while (freeboxes != NULL) {
roentgen b75cab
		ptr = largest_box();
roentgen b75cab
		if (ptr != NULL)
roentgen b75cab
			splitbox(ptr);
roentgen b75cab
		else
roentgen b75cab
			freeboxes = NULL;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * STEP 4: assign colors to all boxes
roentgen b75cab
	 */
roentgen b75cab
	for (i = 0, ptr = usedboxes; ptr != NULL; ++i, ptr = ptr->next) {
roentgen b75cab
		rm[i] = ((ptr->rmin + ptr->rmax) << COLOR_SHIFT) / 2;
roentgen b75cab
		gm[i] = ((ptr->gmin + ptr->gmax) << COLOR_SHIFT) / 2;
roentgen b75cab
		bm[i] = ((ptr->bmin + ptr->bmax) << COLOR_SHIFT) / 2;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/* We're done with the boxes now */
roentgen b75cab
	_TIFFfree(box_list);
roentgen b75cab
	freeboxes = usedboxes = NULL;
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * STEP 5: scan histogram and map all values to closest color
roentgen b75cab
	 */
roentgen b75cab
	/* 5a: create cell list as described in Heckbert[2] */
roentgen b75cab
	ColorCells = (C_cell **)_TIFFmalloc(C_LEN*C_LEN*C_LEN*sizeof (C_cell*));
roentgen b75cab
	_TIFFmemset(ColorCells, 0, C_LEN*C_LEN*C_LEN*sizeof (C_cell*));
roentgen b75cab
	/* 5b: create mapping from truncated pixel space to color
roentgen b75cab
	   table entries */
roentgen b75cab
	map_colortable();
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * STEP 6: scan image, match input values to table entries
roentgen b75cab
	 */
roentgen b75cab
	out = TIFFOpen(argv[optind+1], "w");
roentgen b75cab
	if (out == NULL)
roentgen b75cab
		return (-2);
roentgen b75cab
roentgen b75cab
	CopyField(TIFFTAG_SUBFILETYPE, longv);
roentgen b75cab
	CopyField(TIFFTAG_IMAGEWIDTH, longv);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, (short)COLOR_DEPTH);
roentgen b75cab
	if (compression != (uint16)-1) {
roentgen b75cab
		TIFFSetField(out, 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(out, TIFFTAG_PREDICTOR, predictor);
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
	} else
roentgen b75cab
		CopyField(TIFFTAG_COMPRESSION, compression);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, (short)PHOTOMETRIC_PALETTE);
roentgen b75cab
	CopyField(TIFFTAG_ORIENTATION, shortv);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, (short)1);
roentgen b75cab
	CopyField(TIFFTAG_PLANARCONFIG, shortv);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
roentgen b75cab
	    TIFFDefaultStripSize(out, rowsperstrip));
roentgen b75cab
	CopyField(TIFFTAG_MINSAMPLEVALUE, shortv);
roentgen b75cab
	CopyField(TIFFTAG_MAXSAMPLEVALUE, shortv);
roentgen b75cab
	CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
roentgen b75cab
	CopyField(TIFFTAG_XRESOLUTION, floatv);
roentgen b75cab
	CopyField(TIFFTAG_YRESOLUTION, floatv);
roentgen b75cab
	CopyField(TIFFTAG_XPOSITION, floatv);
roentgen b75cab
	CopyField(TIFFTAG_YPOSITION, floatv);
roentgen b75cab
roentgen b75cab
	if (dither)
roentgen b75cab
		quant_fsdither(in, out);
roentgen b75cab
	else
roentgen b75cab
		quant(in, out);
roentgen b75cab
	/*
roentgen b75cab
	 * Scale colormap to TIFF-required 16-bit values.
roentgen b75cab
	 */
roentgen b75cab
#define	SCALE(x)	(((x)*((1L<<16)-1))/255)
roentgen b75cab
	for (i = 0; i < MAX_CMAP_SIZE; ++i) {
roentgen b75cab
		rm[i] = SCALE(rm[i]);
roentgen b75cab
		gm[i] = SCALE(gm[i]);
roentgen b75cab
		bm[i] = SCALE(bm[i]);
roentgen b75cab
	}
roentgen b75cab
	TIFFSetField(out, TIFFTAG_COLORMAP, rm, gm, bm);
roentgen b75cab
	(void) TIFFClose(out);
roentgen b75cab
	return (0);
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
char* stuff[] = {
roentgen b75cab
"usage: tiffmedian [options] input.tif output.tif",
roentgen b75cab
"where options are:",
roentgen b75cab
" -r #		make each strip have no more than # rows",
roentgen b75cab
" -C #		create a colormap with # entries",
roentgen b75cab
" -f		use Floyd-Steinberg dithering",
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
static void
roentgen b75cab
get_histogram(TIFF* in, Colorbox* box)
roentgen b75cab
{
roentgen b75cab
	register unsigned char *inptr;
roentgen b75cab
	register int red, green, blue;
roentgen b75cab
	register uint32 j, i;
roentgen b75cab
	unsigned char *inputline;
roentgen b75cab
roentgen b75cab
	inputline = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
roentgen b75cab
	if (inputline == NULL) {
roentgen b75cab
		fprintf(stderr, "No space for scanline buffer\n");
roentgen b75cab
		exit(-1);
roentgen b75cab
	}
roentgen b75cab
	box->rmin = box->gmin = box->bmin = 999;
roentgen b75cab
	box->rmax = box->gmax = box->bmax = -1;
roentgen b75cab
	box->total = imagewidth * imagelength;
roentgen b75cab
roentgen b75cab
	{ register uint32 *ptr = &histogram[0][0][0];
roentgen b75cab
	  for (i = B_LEN*B_LEN*B_LEN; i-- > 0;)
roentgen b75cab
		*ptr++ = 0;
roentgen b75cab
	}
roentgen b75cab
	for (i = 0; i < imagelength; i++) {
roentgen b75cab
		if (TIFFReadScanline(in, inputline, i, 0) <= 0)
roentgen b75cab
			break;
roentgen b75cab
		inptr = inputline;
roentgen b75cab
		for (j = imagewidth; j-- > 0;) {
roentgen b75cab
			red = *inptr++ >> COLOR_SHIFT;
roentgen b75cab
			green = *inptr++ >> COLOR_SHIFT;
roentgen b75cab
			blue = *inptr++ >> COLOR_SHIFT;
roentgen b75cab
			if (red < box->rmin)
roentgen b75cab
				box->rmin = red;
roentgen b75cab
		        if (red > box->rmax)
roentgen b75cab
				box->rmax = red;
roentgen b75cab
		        if (green < box->gmin)
roentgen b75cab
				box->gmin = green;
roentgen b75cab
		        if (green > box->gmax)
roentgen b75cab
				box->gmax = green;
roentgen b75cab
		        if (blue < box->bmin)
roentgen b75cab
				box->bmin = blue;
roentgen b75cab
		        if (blue > box->bmax)
roentgen b75cab
				box->bmax = blue;
roentgen b75cab
		        histogram[red][green][blue]++;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	_TIFFfree(inputline);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static Colorbox *
roentgen b75cab
largest_box(void)
roentgen b75cab
{
roentgen b75cab
	register Colorbox *p, *b;
roentgen b75cab
	register uint32 size;
roentgen b75cab
roentgen b75cab
	b = NULL;
roentgen b75cab
	size = 0;
roentgen b75cab
	for (p = usedboxes; p != NULL; p = p->next)
roentgen b75cab
		if ((p->rmax > p->rmin || p->gmax > p->gmin ||
roentgen b75cab
		    p->bmax > p->bmin) &&  p->total > size)
roentgen b75cab
		        size = (b = p)->total;
roentgen b75cab
	return (b);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
splitbox(Colorbox* ptr)
roentgen b75cab
{
roentgen b75cab
	uint32		hist2[B_LEN];
roentgen b75cab
	int		first=0, last=0;
roentgen b75cab
	register Colorbox	*new;
roentgen b75cab
	register uint32	*iptr, *histp;
roentgen b75cab
	register int	i, j;
roentgen b75cab
	register int	ir,ig,ib;
roentgen b75cab
	register uint32 sum, sum1, sum2;
roentgen b75cab
	enum { RED, GREEN, BLUE } axis;
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * See which axis is the largest, do a histogram along that
roentgen b75cab
	 * axis.  Split at median point.  Contract both new boxes to
roentgen b75cab
	 * fit points and return
roentgen b75cab
	 */
roentgen b75cab
	i = ptr->rmax - ptr->rmin;
roentgen b75cab
	if (i >= ptr->gmax - ptr->gmin && i >= ptr->bmax - ptr->bmin)
roentgen b75cab
		axis = RED;
roentgen b75cab
	else if (ptr->gmax - ptr->gmin >= ptr->bmax - ptr->bmin)
roentgen b75cab
		axis = GREEN;
roentgen b75cab
	else
roentgen b75cab
		axis = BLUE;
roentgen b75cab
	/* get histogram along longest axis */
roentgen b75cab
	switch (axis) {
roentgen b75cab
	case RED:
roentgen b75cab
		histp = &hist2[ptr->rmin];
roentgen b75cab
	        for (ir = ptr->rmin; ir <= ptr->rmax; ++ir) {
roentgen b75cab
			*histp = 0;
roentgen b75cab
			for (ig = ptr->gmin; ig <= ptr->gmax; ++ig) {
roentgen b75cab
				iptr = &histogram[ir][ig][ptr->bmin];
roentgen b75cab
				for (ib = ptr->bmin; ib <= ptr->bmax; ++ib)
roentgen b75cab
					*histp += *iptr++;
roentgen b75cab
			}
roentgen b75cab
			histp++;
roentgen b75cab
	        }
roentgen b75cab
	        first = ptr->rmin;
roentgen b75cab
		last = ptr->rmax;
roentgen b75cab
	        break;
roentgen b75cab
	case GREEN:
roentgen b75cab
	        histp = &hist2[ptr->gmin];
roentgen b75cab
	        for (ig = ptr->gmin; ig <= ptr->gmax; ++ig) {
roentgen b75cab
			*histp = 0;
roentgen b75cab
			for (ir = ptr->rmin; ir <= ptr->rmax; ++ir) {
roentgen b75cab
				iptr = &histogram[ir][ig][ptr->bmin];
roentgen b75cab
				for (ib = ptr->bmin; ib <= ptr->bmax; ++ib)
roentgen b75cab
					*histp += *iptr++;
roentgen b75cab
			}
roentgen b75cab
			histp++;
roentgen b75cab
	        }
roentgen b75cab
	        first = ptr->gmin;
roentgen b75cab
		last = ptr->gmax;
roentgen b75cab
	        break;
roentgen b75cab
	case BLUE:
roentgen b75cab
	        histp = &hist2[ptr->bmin];
roentgen b75cab
	        for (ib = ptr->bmin; ib <= ptr->bmax; ++ib) {
roentgen b75cab
			*histp = 0;
roentgen b75cab
			for (ir = ptr->rmin; ir <= ptr->rmax; ++ir) {
roentgen b75cab
				iptr = &histogram[ir][ptr->gmin][ib];
roentgen b75cab
				for (ig = ptr->gmin; ig <= ptr->gmax; ++ig) {
roentgen b75cab
					*histp += *iptr;
roentgen b75cab
					iptr += B_LEN;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
			histp++;
roentgen b75cab
	        }
roentgen b75cab
	        first = ptr->bmin;
roentgen b75cab
		last = ptr->bmax;
roentgen b75cab
	        break;
roentgen b75cab
	}
roentgen b75cab
	/* find median point */
roentgen b75cab
	sum2 = ptr->total / 2;
roentgen b75cab
	histp = &hist2[first];
roentgen b75cab
	sum = 0;
roentgen b75cab
	for (i = first; i <= last && (sum += *histp++) < sum2; ++i)
roentgen b75cab
		;
roentgen b75cab
	if (i == first)
roentgen b75cab
		i++;
roentgen b75cab
roentgen b75cab
	/* Create new box, re-allocate points */
roentgen b75cab
	new = freeboxes;
roentgen b75cab
	freeboxes = new->next;
roentgen b75cab
	if (freeboxes)
roentgen b75cab
		freeboxes->prev = NULL;
roentgen b75cab
	if (usedboxes)
roentgen b75cab
		usedboxes->prev = new;
roentgen b75cab
	new->next = usedboxes;
roentgen b75cab
	usedboxes = new;
roentgen b75cab
roentgen b75cab
	histp = &hist2[first];
roentgen b75cab
	for (sum1 = 0, j = first; j < i; j++)
roentgen b75cab
		sum1 += *histp++;
roentgen b75cab
	for (sum2 = 0, j = i; j <= last; j++)
roentgen b75cab
	    sum2 += *histp++;
roentgen b75cab
	new->total = sum1;
roentgen b75cab
	ptr->total = sum2;
roentgen b75cab
roentgen b75cab
	new->rmin = ptr->rmin;
roentgen b75cab
	new->rmax = ptr->rmax;
roentgen b75cab
	new->gmin = ptr->gmin;
roentgen b75cab
	new->gmax = ptr->gmax;
roentgen b75cab
	new->bmin = ptr->bmin;
roentgen b75cab
	new->bmax = ptr->bmax;
roentgen b75cab
	switch (axis) {
roentgen b75cab
	case RED:
roentgen b75cab
		new->rmax = i-1;
roentgen b75cab
	        ptr->rmin = i;
roentgen b75cab
	        break;
roentgen b75cab
	case GREEN:
roentgen b75cab
	        new->gmax = i-1;
roentgen b75cab
	        ptr->gmin = i;
roentgen b75cab
	        break;
roentgen b75cab
	case BLUE:
roentgen b75cab
	        new->bmax = i-1;
roentgen b75cab
	        ptr->bmin = i;
roentgen b75cab
	        break;
roentgen b75cab
	}
roentgen b75cab
	shrinkbox(new);
roentgen b75cab
	shrinkbox(ptr);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
shrinkbox(Colorbox* box)
roentgen b75cab
{
roentgen b75cab
	register uint32 *histp;
roentgen b75cab
	register int	ir, ig, ib;
roentgen b75cab
roentgen b75cab
	if (box->rmax > box->rmin) {
roentgen b75cab
		for (ir = box->rmin; ir <= box->rmax; ++ir)
roentgen b75cab
			for (ig = box->gmin; ig <= box->gmax; ++ig) {
roentgen b75cab
				histp = &histogram[ir][ig][box->bmin];
roentgen b75cab
			        for (ib = box->bmin; ib <= box->bmax; ++ib)
roentgen b75cab
					if (*histp++ != 0) {
roentgen b75cab
						box->rmin = ir;
roentgen b75cab
						goto have_rmin;
roentgen b75cab
					}
roentgen b75cab
			}
roentgen b75cab
	have_rmin:
roentgen b75cab
		if (box->rmax > box->rmin)
roentgen b75cab
			for (ir = box->rmax; ir >= box->rmin; --ir)
roentgen b75cab
				for (ig = box->gmin; ig <= box->gmax; ++ig) {
roentgen b75cab
					histp = &histogram[ir][ig][box->bmin];
roentgen b75cab
					ib = box->bmin;
roentgen b75cab
					for (; ib <= box->bmax; ++ib)
roentgen b75cab
						if (*histp++ != 0) {
roentgen b75cab
							box->rmax = ir;
roentgen b75cab
							goto have_rmax;
roentgen b75cab
						}
roentgen b75cab
			        }
roentgen b75cab
	}
roentgen b75cab
have_rmax:
roentgen b75cab
	if (box->gmax > box->gmin) {
roentgen b75cab
		for (ig = box->gmin; ig <= box->gmax; ++ig)
roentgen b75cab
			for (ir = box->rmin; ir <= box->rmax; ++ir) {
roentgen b75cab
				histp = &histogram[ir][ig][box->bmin];
roentgen b75cab
			        for (ib = box->bmin; ib <= box->bmax; ++ib)
roentgen b75cab
				if (*histp++ != 0) {
roentgen b75cab
					box->gmin = ig;
roentgen b75cab
					goto have_gmin;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
	have_gmin:
roentgen b75cab
		if (box->gmax > box->gmin)
roentgen b75cab
			for (ig = box->gmax; ig >= box->gmin; --ig)
roentgen b75cab
				for (ir = box->rmin; ir <= box->rmax; ++ir) {
roentgen b75cab
					histp = &histogram[ir][ig][box->bmin];
roentgen b75cab
					ib = box->bmin;
roentgen b75cab
					for (; ib <= box->bmax; ++ib)
roentgen b75cab
						if (*histp++ != 0) {
roentgen b75cab
							box->gmax = ig;
roentgen b75cab
							goto have_gmax;
roentgen b75cab
						}
roentgen b75cab
			        }
roentgen b75cab
	}
roentgen b75cab
have_gmax:
roentgen b75cab
	if (box->bmax > box->bmin) {
roentgen b75cab
		for (ib = box->bmin; ib <= box->bmax; ++ib)
roentgen b75cab
			for (ir = box->rmin; ir <= box->rmax; ++ir) {
roentgen b75cab
				histp = &histogram[ir][box->gmin][ib];
roentgen b75cab
			        for (ig = box->gmin; ig <= box->gmax; ++ig) {
roentgen b75cab
					if (*histp != 0) {
roentgen b75cab
						box->bmin = ib;
roentgen b75cab
						goto have_bmin;
roentgen b75cab
					}
roentgen b75cab
					histp += B_LEN;
roentgen b75cab
			        }
roentgen b75cab
		        }
roentgen b75cab
	have_bmin:
roentgen b75cab
		if (box->bmax > box->bmin)
roentgen b75cab
			for (ib = box->bmax; ib >= box->bmin; --ib)
roentgen b75cab
				for (ir = box->rmin; ir <= box->rmax; ++ir) {
roentgen b75cab
					histp = &histogram[ir][box->gmin][ib];
roentgen b75cab
					ig = box->gmin;
roentgen b75cab
					for (; ig <= box->gmax; ++ig) {
roentgen b75cab
						if (*histp != 0) {
roentgen b75cab
							box->bmax = ib;
roentgen b75cab
							goto have_bmax;
roentgen b75cab
						}
roentgen b75cab
						histp += B_LEN;
roentgen b75cab
					}
roentgen b75cab
			        }
roentgen b75cab
	}
roentgen b75cab
have_bmax:
roentgen b75cab
	;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static C_cell *
roentgen b75cab
create_colorcell(int red, int green, int blue)
roentgen b75cab
{
roentgen b75cab
	register int ir, ig, ib, i;
roentgen b75cab
	register C_cell *ptr;
roentgen b75cab
	int mindist, next_n;
roentgen b75cab
	register int tmp, dist, n;
roentgen b75cab
roentgen b75cab
	ir = red >> (COLOR_DEPTH-C_DEPTH);
roentgen b75cab
	ig = green >> (COLOR_DEPTH-C_DEPTH);
roentgen b75cab
	ib = blue >> (COLOR_DEPTH-C_DEPTH);
roentgen b75cab
	ptr = (C_cell *)_TIFFmalloc(sizeof (C_cell));
roentgen b75cab
	*(ColorCells + ir*C_LEN*C_LEN + ig*C_LEN + ib) = ptr;
roentgen b75cab
	ptr->num_ents = 0;
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Step 1: find all colors inside this cell, while we're at
roentgen b75cab
	 *	   it, find distance of centermost point to furthest corner
roentgen b75cab
	 */
roentgen b75cab
	mindist = 99999999;
roentgen b75cab
	for (i = 0; i < num_colors; ++i) {
roentgen b75cab
		if (rm[i]>>(COLOR_DEPTH-C_DEPTH) != ir  ||
roentgen b75cab
		    gm[i]>>(COLOR_DEPTH-C_DEPTH) != ig  ||
roentgen b75cab
		    bm[i]>>(COLOR_DEPTH-C_DEPTH) != ib)
roentgen b75cab
			continue;
roentgen b75cab
		ptr->entries[ptr->num_ents][0] = i;
roentgen b75cab
		ptr->entries[ptr->num_ents][1] = 0;
roentgen b75cab
		++ptr->num_ents;
roentgen b75cab
	        tmp = rm[i] - red;
roentgen b75cab
	        if (tmp < (MAX_COLOR/C_LEN/2))
roentgen b75cab
			tmp = MAX_COLOR/C_LEN-1 - tmp;
roentgen b75cab
	        dist = tmp*tmp;
roentgen b75cab
	        tmp = gm[i] - green;
roentgen b75cab
	        if (tmp < (MAX_COLOR/C_LEN/2))
roentgen b75cab
			tmp = MAX_COLOR/C_LEN-1 - tmp;
roentgen b75cab
	        dist += tmp*tmp;
roentgen b75cab
	        tmp = bm[i] - blue;
roentgen b75cab
	        if (tmp < (MAX_COLOR/C_LEN/2))
roentgen b75cab
			tmp = MAX_COLOR/C_LEN-1 - tmp;
roentgen b75cab
	        dist += tmp*tmp;
roentgen b75cab
	        if (dist < mindist)
roentgen b75cab
			mindist = dist;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Step 3: find all points within that distance to cell.
roentgen b75cab
	 */
roentgen b75cab
	for (i = 0; i < num_colors; ++i) {
roentgen b75cab
		if (rm[i] >> (COLOR_DEPTH-C_DEPTH) == ir  &&
roentgen b75cab
		    gm[i] >> (COLOR_DEPTH-C_DEPTH) == ig  &&
roentgen b75cab
		    bm[i] >> (COLOR_DEPTH-C_DEPTH) == ib)
roentgen b75cab
			continue;
roentgen b75cab
		dist = 0;
roentgen b75cab
	        if ((tmp = red - rm[i]) > 0 ||
roentgen b75cab
		    (tmp = rm[i] - (red + MAX_COLOR/C_LEN-1)) > 0 )
roentgen b75cab
			dist += tmp*tmp;
roentgen b75cab
	        if ((tmp = green - gm[i]) > 0 ||
roentgen b75cab
		    (tmp = gm[i] - (green + MAX_COLOR/C_LEN-1)) > 0 )
roentgen b75cab
			dist += tmp*tmp;
roentgen b75cab
	        if ((tmp = blue - bm[i]) > 0 ||
roentgen b75cab
		    (tmp = bm[i] - (blue + MAX_COLOR/C_LEN-1)) > 0 )
roentgen b75cab
			dist += tmp*tmp;
roentgen b75cab
	        if (dist < mindist) {
roentgen b75cab
			ptr->entries[ptr->num_ents][0] = i;
roentgen b75cab
			ptr->entries[ptr->num_ents][1] = dist;
roentgen b75cab
			++ptr->num_ents;
roentgen b75cab
	        }
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Sort color cells by distance, use cheap exchange sort
roentgen b75cab
	 */
roentgen b75cab
	for (n = ptr->num_ents - 1; n > 0; n = next_n) {
roentgen b75cab
		next_n = 0;
roentgen b75cab
		for (i = 0; i < n; ++i)
roentgen b75cab
			if (ptr->entries[i][1] > ptr->entries[i+1][1]) {
roentgen b75cab
				tmp = ptr->entries[i][0];
roentgen b75cab
				ptr->entries[i][0] = ptr->entries[i+1][0];
roentgen b75cab
				ptr->entries[i+1][0] = tmp;
roentgen b75cab
				tmp = ptr->entries[i][1];
roentgen b75cab
				ptr->entries[i][1] = ptr->entries[i+1][1];
roentgen b75cab
				ptr->entries[i+1][1] = tmp;
roentgen b75cab
				next_n = i;
roentgen b75cab
		        }
roentgen b75cab
	}
roentgen b75cab
	return (ptr);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
map_colortable(void)
roentgen b75cab
{
roentgen b75cab
	register uint32 *histp = &histogram[0][0][0];
roentgen b75cab
	register C_cell *cell;
roentgen b75cab
	register int j, tmp, d2, dist;
roentgen b75cab
	int ir, ig, ib, i;
roentgen b75cab
roentgen b75cab
	for (ir = 0; ir < B_LEN; ++ir)
roentgen b75cab
		for (ig = 0; ig < B_LEN; ++ig)
roentgen b75cab
			for (ib = 0; ib < B_LEN; ++ib, histp++) {
roentgen b75cab
				if (*histp == 0) {
roentgen b75cab
					*histp = -1;
roentgen b75cab
					continue;
roentgen b75cab
				}
roentgen b75cab
				cell = *(ColorCells +
roentgen b75cab
				    (((ir>>(B_DEPTH-C_DEPTH)) << C_DEPTH*2) +
roentgen b75cab
				    ((ig>>(B_DEPTH-C_DEPTH)) << C_DEPTH) +
roentgen b75cab
				    (ib>>(B_DEPTH-C_DEPTH))));
roentgen b75cab
				if (cell == NULL )
roentgen b75cab
					cell = create_colorcell(
roentgen b75cab
					    ir << COLOR_SHIFT,
roentgen b75cab
					    ig << COLOR_SHIFT,
roentgen b75cab
					    ib << COLOR_SHIFT);
roentgen b75cab
				dist = 9999999;
roentgen b75cab
				for (i = 0; i < cell->num_ents &&
roentgen b75cab
				    dist > cell->entries[i][1]; ++i) {
roentgen b75cab
					j = cell->entries[i][0];
roentgen b75cab
					d2 = rm[j] - (ir << COLOR_SHIFT);
roentgen b75cab
					d2 *= d2;
roentgen b75cab
					tmp = gm[j] - (ig << COLOR_SHIFT);
roentgen b75cab
					d2 += tmp*tmp;
roentgen b75cab
					tmp = bm[j] - (ib << COLOR_SHIFT);
roentgen b75cab
					d2 += tmp*tmp;
roentgen b75cab
					if (d2 < dist) {
roentgen b75cab
						dist = d2;
roentgen b75cab
						*histp = j;
roentgen b75cab
					}
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * straight quantization.  Each pixel is mapped to the colors
roentgen b75cab
 * closest to it.  Color values are rounded to the nearest color
roentgen b75cab
 * table entry.
roentgen b75cab
 */
roentgen b75cab
static void
roentgen b75cab
quant(TIFF* in, TIFF* out)
roentgen b75cab
{
roentgen b75cab
	unsigned char	*outline, *inputline;
roentgen b75cab
	register unsigned char	*outptr, *inptr;
roentgen b75cab
	register uint32 i, j;
roentgen b75cab
	register int red, green, blue;
roentgen b75cab
roentgen b75cab
	inputline = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
roentgen b75cab
	outline = (unsigned char *)_TIFFmalloc(imagewidth);
roentgen b75cab
	for (i = 0; i < imagelength; i++) {
roentgen b75cab
		if (TIFFReadScanline(in, inputline, i, 0) <= 0)
roentgen b75cab
			break;
roentgen b75cab
		inptr = inputline;
roentgen b75cab
		outptr = outline;
roentgen b75cab
		for (j = 0; j < imagewidth; j++) {
roentgen b75cab
			red = *inptr++ >> COLOR_SHIFT;
roentgen b75cab
			green = *inptr++ >> COLOR_SHIFT;
roentgen b75cab
			blue = *inptr++ >> COLOR_SHIFT;
roentgen b75cab
			*outptr++ = (unsigned char)histogram[red][green][blue];
roentgen b75cab
		}
roentgen b75cab
		if (TIFFWriteScanline(out, outline, i, 0) < 0)
roentgen b75cab
			break;
roentgen b75cab
	}
roentgen b75cab
	_TIFFfree(inputline);
roentgen b75cab
	_TIFFfree(outline);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#define	SWAP(type,a,b)	{ type p; p = a; a = b; b = p; }
roentgen b75cab
roentgen b75cab
#define	GetInputLine(tif, row, bad)				\
roentgen b75cab
	if (TIFFReadScanline(tif, inputline, row, 0) <= 0)	\
roentgen b75cab
		bad;						\
roentgen b75cab
	inptr = inputline;					\
roentgen b75cab
	nextptr = nextline;					\
roentgen b75cab
	for (j = 0; j < imagewidth; ++j) {			\
roentgen b75cab
		*nextptr++ = *inptr++;				\
roentgen b75cab
		*nextptr++ = *inptr++;				\
roentgen b75cab
		*nextptr++ = *inptr++;				\
roentgen b75cab
	}
roentgen b75cab
#define	GetComponent(raw, cshift, c)				\
roentgen b75cab
	cshift = raw;						\
roentgen b75cab
	if (cshift < 0)						\
roentgen b75cab
		cshift = 0;					\
roentgen b75cab
	else if (cshift >= MAX_COLOR)				\
roentgen b75cab
		cshift = MAX_COLOR-1;				\
roentgen b75cab
	c = cshift;						\
roentgen b75cab
	cshift >>= COLOR_SHIFT;
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
quant_fsdither(TIFF* in, TIFF* out)
roentgen b75cab
{
roentgen b75cab
	unsigned char *outline, *inputline, *inptr;
roentgen b75cab
	short *thisline, *nextline;
roentgen b75cab
	register unsigned char	*outptr;
roentgen b75cab
	register short *thisptr, *nextptr;
roentgen b75cab
	register uint32 i, j;
roentgen b75cab
	uint32 imax, jmax;
roentgen b75cab
	int lastline, lastpixel;
roentgen b75cab
roentgen b75cab
	imax = imagelength - 1;
roentgen b75cab
	jmax = imagewidth - 1;
roentgen b75cab
	inputline = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
roentgen b75cab
	thisline = (short *)_TIFFmalloc(imagewidth * 3 * sizeof (short));
roentgen b75cab
	nextline = (short *)_TIFFmalloc(imagewidth * 3 * sizeof (short));
roentgen b75cab
	outline = (unsigned char *) _TIFFmalloc(TIFFScanlineSize(out));
roentgen b75cab
roentgen b75cab
	GetInputLine(in, 0, goto bad);		/* get first line */
roentgen b75cab
	for (i = 1; i <= imagelength; ++i) {
roentgen b75cab
		SWAP(short *, thisline, nextline);
roentgen b75cab
		lastline = (i >= imax);
roentgen b75cab
		if (i <= imax)
roentgen b75cab
			GetInputLine(in, i, break);
roentgen b75cab
		thisptr = thisline;
roentgen b75cab
		nextptr = nextline;
roentgen b75cab
		outptr = outline;
roentgen b75cab
		for (j = 0; j < imagewidth; ++j) {
roentgen b75cab
			int red, green, blue;
roentgen b75cab
			register int oval, r2, g2, b2;
roentgen b75cab
roentgen b75cab
			lastpixel = (j == jmax);
roentgen b75cab
			GetComponent(*thisptr++, r2, red);
roentgen b75cab
			GetComponent(*thisptr++, g2, green);
roentgen b75cab
			GetComponent(*thisptr++, b2, blue);
roentgen b75cab
			oval = histogram[r2][g2][b2];
roentgen b75cab
			if (oval == -1) {
roentgen b75cab
				int ci;
roentgen b75cab
				register int cj, tmp, d2, dist;
roentgen b75cab
				register C_cell	*cell;
roentgen b75cab
roentgen b75cab
				cell = *(ColorCells +
roentgen b75cab
				    (((r2>>(B_DEPTH-C_DEPTH)) << C_DEPTH*2) +
roentgen b75cab
				    ((g2>>(B_DEPTH-C_DEPTH)) << C_DEPTH ) +
roentgen b75cab
				    (b2>>(B_DEPTH-C_DEPTH))));
roentgen b75cab
				if (cell == NULL)
roentgen b75cab
					cell = create_colorcell(red,
roentgen b75cab
					    green, blue);
roentgen b75cab
				dist = 9999999;
roentgen b75cab
				for (ci = 0; ci < cell->num_ents && dist > cell->entries[ci][1]; ++ci) {
roentgen b75cab
					cj = cell->entries[ci][0];
roentgen b75cab
					d2 = (rm[cj] >> COLOR_SHIFT) - r2;
roentgen b75cab
					d2 *= d2;
roentgen b75cab
					tmp = (gm[cj] >> COLOR_SHIFT) - g2;
roentgen b75cab
					d2 += tmp*tmp;
roentgen b75cab
					tmp = (bm[cj] >> COLOR_SHIFT) - b2;
roentgen b75cab
					d2 += tmp*tmp;
roentgen b75cab
					if (d2 < dist) {
roentgen b75cab
						dist = d2;
roentgen b75cab
						oval = cj;
roentgen b75cab
					}
roentgen b75cab
				}
roentgen b75cab
				histogram[r2][g2][b2] = oval;
roentgen b75cab
			}
roentgen b75cab
			*outptr++ = oval;
roentgen b75cab
			red -= rm[oval];
roentgen b75cab
			green -= gm[oval];
roentgen b75cab
			blue -= bm[oval];
roentgen b75cab
			if (!lastpixel) {
roentgen b75cab
				thisptr[0] += blue * 7 / 16;
roentgen b75cab
				thisptr[1] += green * 7 / 16;
roentgen b75cab
				thisptr[2] += red * 7 / 16;
roentgen b75cab
			}
roentgen b75cab
			if (!lastline) {
roentgen b75cab
				if (j != 0) {
roentgen b75cab
					nextptr[-3] += blue * 3 / 16;
roentgen b75cab
					nextptr[-2] += green * 3 / 16;
roentgen b75cab
					nextptr[-1] += red * 3 / 16;
roentgen b75cab
				}
roentgen b75cab
				nextptr[0] += blue * 5 / 16;
roentgen b75cab
				nextptr[1] += green * 5 / 16;
roentgen b75cab
				nextptr[2] += red * 5 / 16;
roentgen b75cab
				if (!lastpixel) {
roentgen b75cab
					nextptr[3] += blue / 16;
roentgen b75cab
				        nextptr[4] += green / 16;
roentgen b75cab
				        nextptr[5] += red / 16;
roentgen b75cab
				}
roentgen b75cab
				nextptr += 3;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		if (TIFFWriteScanline(out, outline, i-1, 0) < 0)
roentgen b75cab
			break;
roentgen b75cab
	}
roentgen b75cab
bad:
roentgen b75cab
	_TIFFfree(inputline);
roentgen b75cab
	_TIFFfree(thisline);
roentgen b75cab
	_TIFFfree(nextline);
roentgen b75cab
	_TIFFfree(outline);
roentgen b75cab
}
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
 */