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