roentgen b75cab
/* $Id: tiffdither.c,v 1.12 2010-03-10 18:56:50 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1988-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and 
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Sam Leffler and Silicon Graphics.
roentgen b75cab
 * 
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
roentgen b75cab
 * 
roentgen b75cab
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#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	streq(a,b)	(strcmp(a,b) == 0)
roentgen b75cab
#define	strneq(a,b,n)	(strncmp(a,b,n) == 0)
roentgen b75cab
roentgen b75cab
#define	CopyField(tag, v) \
roentgen b75cab
	if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
roentgen b75cab
roentgen b75cab
uint32	imagewidth;
roentgen b75cab
uint32	imagelength;
roentgen b75cab
int	threshold = 128;
roentgen b75cab
roentgen b75cab
static	void usage(void);
roentgen b75cab
roentgen b75cab
/* 
roentgen b75cab
 * Floyd-Steinberg error propragation with threshold.
roentgen b75cab
 * This code is stolen from tiffmedian.
roentgen b75cab
 */
roentgen b75cab
static void
roentgen b75cab
fsdither(TIFF* in, TIFF* out)
roentgen b75cab
{
roentgen b75cab
	unsigned char *outline, *inputline, *inptr;
roentgen b75cab
	short *thisline, *nextline, *tmpptr;
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
	int bit;
roentgen b75cab
	tsize_t outlinesize;
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 * sizeof (short));
roentgen b75cab
	nextline = (short *)_TIFFmalloc(imagewidth * sizeof (short));
roentgen b75cab
	outlinesize = TIFFScanlineSize(out);
roentgen b75cab
	outline = (unsigned char *) _TIFFmalloc(outlinesize);
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Get first line
roentgen b75cab
	 */
roentgen b75cab
	if (TIFFReadScanline(in, inputline, 0, 0) <= 0)
roentgen b75cab
            goto skip_on_error;
roentgen b75cab
roentgen b75cab
	inptr = inputline;
roentgen b75cab
	nextptr = nextline;
roentgen b75cab
	for (j = 0; j < imagewidth; ++j)
roentgen b75cab
		*nextptr++ = *inptr++;
roentgen b75cab
	for (i = 1; i < imagelength; ++i) {
roentgen b75cab
		tmpptr = thisline;
roentgen b75cab
		thisline = nextline;
roentgen b75cab
		nextline = tmpptr;
roentgen b75cab
		lastline = (i == imax);
roentgen b75cab
		if (TIFFReadScanline(in, inputline, i, 0) <= 0)
roentgen b75cab
			break;
roentgen b75cab
		inptr = inputline;
roentgen b75cab
		nextptr = nextline;
roentgen b75cab
		for (j = 0; j < imagewidth; ++j)
roentgen b75cab
			*nextptr++ = *inptr++;
roentgen b75cab
		thisptr = thisline;
roentgen b75cab
		nextptr = nextline;
roentgen b75cab
		_TIFFmemset(outptr = outline, 0, outlinesize);
roentgen b75cab
		bit = 0x80;
roentgen b75cab
		for (j = 0; j < imagewidth; ++j) {
roentgen b75cab
			register int v;
roentgen b75cab
roentgen b75cab
			lastpixel = (j == jmax);
roentgen b75cab
			v = *thisptr++;
roentgen b75cab
			if (v < 0)
roentgen b75cab
				v = 0;
roentgen b75cab
			else if (v > 255)
roentgen b75cab
				v = 255;
roentgen b75cab
			if (v > threshold) {
roentgen b75cab
				*outptr |= bit;
roentgen b75cab
				v -= 255;
roentgen b75cab
			}
roentgen b75cab
			bit >>= 1;
roentgen b75cab
			if (bit == 0) {
roentgen b75cab
				outptr++;
roentgen b75cab
				bit = 0x80;
roentgen b75cab
			}
roentgen b75cab
			if (!lastpixel)
roentgen b75cab
				thisptr[0] += v * 7 / 16;
roentgen b75cab
			if (!lastline) {
roentgen b75cab
				if (j != 0)
roentgen b75cab
					nextptr[-1] += v * 3 / 16;
roentgen b75cab
				*nextptr++ += v * 5 / 16;
roentgen b75cab
				if (!lastpixel)
roentgen b75cab
					nextptr[0] += v / 16;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		if (TIFFWriteScanline(out, outline, i-1, 0) < 0)
roentgen b75cab
			break;
roentgen b75cab
	}
roentgen b75cab
  skip_on_error:
roentgen b75cab
	_TIFFfree(inputline);
roentgen b75cab
	_TIFFfree(thisline);
roentgen b75cab
	_TIFFfree(nextline);
roentgen b75cab
	_TIFFfree(outline);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static	uint16 compression = COMPRESSION_PACKBITS;
roentgen b75cab
static	uint16 predictor = 0;
roentgen b75cab
static	uint32 group3options = 0;
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
processG3Options(char* cp)
roentgen b75cab
{
roentgen b75cab
	if ((cp = strchr(cp, ':'))) {
roentgen b75cab
		do {
roentgen b75cab
			cp++;
roentgen b75cab
			if (strneq(cp, "1d", 2))
roentgen b75cab
				group3options &= ~GROUP3OPT_2DENCODING;
roentgen b75cab
			else if (strneq(cp, "2d", 2))
roentgen b75cab
				group3options |= GROUP3OPT_2DENCODING;
roentgen b75cab
			else if (strneq(cp, "fill", 4))
roentgen b75cab
				group3options |= GROUP3OPT_FILLBITS;
roentgen b75cab
			else
roentgen b75cab
				usage();
roentgen b75cab
		} while ((cp = strchr(cp, ':')));
roentgen b75cab
	}
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, "g3", 2)) {
roentgen b75cab
		processG3Options(opt);
roentgen b75cab
		compression = COMPRESSION_CCITTFAX3;
roentgen b75cab
	} else if (streq(opt, "g4"))
roentgen b75cab
		compression = COMPRESSION_CCITTFAX4;
roentgen b75cab
	else if (strneq(opt, "lzw", 3)) {
roentgen b75cab
		char* cp = strchr(opt, ':');
roentgen b75cab
		if (cp)
roentgen b75cab
			predictor = atoi(cp+1);
roentgen b75cab
		compression = COMPRESSION_LZW;
roentgen b75cab
	} else if (strneq(opt, "zip", 3)) {
roentgen b75cab
		char* cp = strchr(opt, ':');
roentgen b75cab
		if (cp)
roentgen b75cab
			predictor = atoi(cp+1);
roentgen b75cab
		compression = COMPRESSION_DEFLATE;
roentgen b75cab
	} else
roentgen b75cab
		return (0);
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
main(int argc, char* argv[])
roentgen b75cab
{
roentgen b75cab
	TIFF *in, *out;
roentgen b75cab
	uint16 samplesperpixel, bitspersample = 1, shortv;
roentgen b75cab
	float floatv;
roentgen b75cab
	char thing[1024];
roentgen b75cab
	uint32 rowsperstrip = (uint32) -1;
roentgen b75cab
	uint16 fillorder = 0;
roentgen b75cab
	int c;
roentgen b75cab
	extern int optind;
roentgen b75cab
	extern char *optarg;
roentgen b75cab
roentgen b75cab
	while ((c = getopt(argc, argv, "c:f:r:t:")) != -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 'f':		/* fill order */
roentgen b75cab
			if (streq(optarg, "lsb2msb"))
roentgen b75cab
				fillorder = FILLORDER_LSB2MSB;
roentgen b75cab
			else if (streq(optarg, "msb2lsb"))
roentgen b75cab
				fillorder = FILLORDER_MSB2LSB;
roentgen b75cab
			else
roentgen b75cab
				usage();
roentgen b75cab
			break;
roentgen b75cab
		case 'r':		/* rows/strip */
roentgen b75cab
			rowsperstrip = atoi(optarg);
roentgen b75cab
			break;
roentgen b75cab
		case 't':
roentgen b75cab
			threshold = atoi(optarg);
roentgen b75cab
			if (threshold < 0)
roentgen b75cab
				threshold = 0;
roentgen b75cab
			else if (threshold > 255)
roentgen b75cab
				threshold = 255;
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_SAMPLESPERPIXEL, &samplesperpixel);
roentgen b75cab
	if (samplesperpixel != 1) {
roentgen b75cab
		fprintf(stderr, "%s: Not a b&w image.\n", argv[0]);
roentgen b75cab
		return (-1);
roentgen b75cab
	}
roentgen b75cab
	TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample);
roentgen b75cab
	if (bitspersample != 8) {
roentgen b75cab
		fprintf(stderr,
roentgen b75cab
		    " %s: Sorry, only handle 8-bit samples.\n", argv[0]);
roentgen b75cab
		return (-1);
roentgen b75cab
	}
roentgen b75cab
	out = TIFFOpen(argv[optind+1], "w");
roentgen b75cab
	if (out == NULL)
roentgen b75cab
		return (-1);
roentgen b75cab
	CopyField(TIFFTAG_IMAGEWIDTH, imagewidth);
roentgen b75cab
	TIFFGetField(in, TIFFTAG_IMAGELENGTH, &imagelength);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_IMAGELENGTH, imagelength-1);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 1);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
roentgen b75cab
	if (fillorder)
roentgen b75cab
		TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);
roentgen b75cab
	else
roentgen b75cab
		CopyField(TIFFTAG_FILLORDER, shortv);
roentgen b75cab
	sprintf(thing, "Dithered B&W version of %s", argv[optind]);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, thing);
roentgen b75cab
	CopyField(TIFFTAG_PHOTOMETRIC, shortv);
roentgen b75cab
	CopyField(TIFFTAG_ORIENTATION, shortv);
roentgen b75cab
	CopyField(TIFFTAG_XRESOLUTION, floatv);
roentgen b75cab
	CopyField(TIFFTAG_YRESOLUTION, floatv);
roentgen b75cab
	CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
roentgen b75cab
        rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
roentgen b75cab
	switch (compression) {
roentgen b75cab
	case COMPRESSION_CCITTFAX3:
roentgen b75cab
		TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, group3options);
roentgen b75cab
		break;
roentgen b75cab
	case COMPRESSION_LZW:
roentgen b75cab
	case COMPRESSION_DEFLATE:
roentgen b75cab
		if (predictor)
roentgen b75cab
			TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
	fsdither(in, out);
roentgen b75cab
	TIFFClose(in);
roentgen b75cab
	TIFFClose(out);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
char* stuff[] = {
roentgen b75cab
"usage: tiffdither [options] input.tif output.tif",
roentgen b75cab
"where options are:",
roentgen b75cab
" -r #		make each strip have no more than # rows",
roentgen b75cab
" -f lsb2msb	force lsb-to-msb FillOrder for output",
roentgen b75cab
" -f msb2lsb	force msb-to-lsb FillOrder for output",
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 g3[:opts]	compress output with CCITT Group 3 encoding",
roentgen b75cab
" -c g4		compress output with CCITT Group 4 encoding",
roentgen b75cab
" -c none	use no compression algorithm on output",
roentgen b75cab
"",
roentgen b75cab
"Group 3 options:",
roentgen b75cab
" 1d		use default CCITT Group 3 1D-encoding",
roentgen b75cab
" 2d		use optional CCITT Group 3 2D-encoding",
roentgen b75cab
" fill		byte-align EOL codes",
roentgen b75cab
"For example, -c g3:2d:fill to get G3-2D-encoded data with byte-aligned EOLs",
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
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */