kusano 7d535a
/* $Id: tiffdither.c,v 1.12 2010-03-10 18:56:50 bfriesen Exp $ */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Copyright (c) 1988-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
#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	streq(a,b)	(strcmp(a,b) == 0)
kusano 7d535a
#define	strneq(a,b,n)	(strncmp(a,b,n) == 0)
kusano 7d535a
kusano 7d535a
#define	CopyField(tag, v) \
kusano 7d535a
	if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
kusano 7d535a
kusano 7d535a
uint32	imagewidth;
kusano 7d535a
uint32	imagelength;
kusano 7d535a
int	threshold = 128;
kusano 7d535a
kusano 7d535a
static	void usage(void);
kusano 7d535a
kusano 7d535a
/* 
kusano 7d535a
 * Floyd-Steinberg error propragation with threshold.
kusano 7d535a
 * This code is stolen from tiffmedian.
kusano 7d535a
 */
kusano 7d535a
static void
kusano 7d535a
fsdither(TIFF* in, TIFF* out)
kusano 7d535a
{
kusano 7d535a
	unsigned char *outline, *inputline, *inptr;
kusano 7d535a
	short *thisline, *nextline, *tmpptr;
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
	int bit;
kusano 7d535a
	tsize_t outlinesize;
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 * sizeof (short));
kusano 7d535a
	nextline = (short *)_TIFFmalloc(imagewidth * sizeof (short));
kusano 7d535a
	outlinesize = TIFFScanlineSize(out);
kusano 7d535a
	outline = (unsigned char *) _TIFFmalloc(outlinesize);
kusano 7d535a
kusano 7d535a
	/*
kusano 7d535a
	 * Get first line
kusano 7d535a
	 */
kusano 7d535a
	if (TIFFReadScanline(in, inputline, 0, 0) <= 0)
kusano 7d535a
            goto skip_on_error;
kusano 7d535a
kusano 7d535a
	inptr = inputline;
kusano 7d535a
	nextptr = nextline;
kusano 7d535a
	for (j = 0; j < imagewidth; ++j)
kusano 7d535a
		*nextptr++ = *inptr++;
kusano 7d535a
	for (i = 1; i < imagelength; ++i) {
kusano 7d535a
		tmpptr = thisline;
kusano 7d535a
		thisline = nextline;
kusano 7d535a
		nextline = tmpptr;
kusano 7d535a
		lastline = (i == imax);
kusano 7d535a
		if (TIFFReadScanline(in, inputline, i, 0) <= 0)
kusano 7d535a
			break;
kusano 7d535a
		inptr = inputline;
kusano 7d535a
		nextptr = nextline;
kusano 7d535a
		for (j = 0; j < imagewidth; ++j)
kusano 7d535a
			*nextptr++ = *inptr++;
kusano 7d535a
		thisptr = thisline;
kusano 7d535a
		nextptr = nextline;
kusano 7d535a
		_TIFFmemset(outptr = outline, 0, outlinesize);
kusano 7d535a
		bit = 0x80;
kusano 7d535a
		for (j = 0; j < imagewidth; ++j) {
kusano 7d535a
			register int v;
kusano 7d535a
kusano 7d535a
			lastpixel = (j == jmax);
kusano 7d535a
			v = *thisptr++;
kusano 7d535a
			if (v < 0)
kusano 7d535a
				v = 0;
kusano 7d535a
			else if (v > 255)
kusano 7d535a
				v = 255;
kusano 7d535a
			if (v > threshold) {
kusano 7d535a
				*outptr |= bit;
kusano 7d535a
				v -= 255;
kusano 7d535a
			}
kusano 7d535a
			bit >>= 1;
kusano 7d535a
			if (bit == 0) {
kusano 7d535a
				outptr++;
kusano 7d535a
				bit = 0x80;
kusano 7d535a
			}
kusano 7d535a
			if (!lastpixel)
kusano 7d535a
				thisptr[0] += v * 7 / 16;
kusano 7d535a
			if (!lastline) {
kusano 7d535a
				if (j != 0)
kusano 7d535a
					nextptr[-1] += v * 3 / 16;
kusano 7d535a
				*nextptr++ += v * 5 / 16;
kusano 7d535a
				if (!lastpixel)
kusano 7d535a
					nextptr[0] += v / 16;
kusano 7d535a
			}
kusano 7d535a
		}
kusano 7d535a
		if (TIFFWriteScanline(out, outline, i-1, 0) < 0)
kusano 7d535a
			break;
kusano 7d535a
	}
kusano 7d535a
  skip_on_error:
kusano 7d535a
	_TIFFfree(inputline);
kusano 7d535a
	_TIFFfree(thisline);
kusano 7d535a
	_TIFFfree(nextline);
kusano 7d535a
	_TIFFfree(outline);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static	uint16 compression = COMPRESSION_PACKBITS;
kusano 7d535a
static	uint16 predictor = 0;
kusano 7d535a
static	uint32 group3options = 0;
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
processG3Options(char* cp)
kusano 7d535a
{
kusano 7d535a
	if ((cp = strchr(cp, ':'))) {
kusano 7d535a
		do {
kusano 7d535a
			cp++;
kusano 7d535a
			if (strneq(cp, "1d", 2))
kusano 7d535a
				group3options &= ~GROUP3OPT_2DENCODING;
kusano 7d535a
			else if (strneq(cp, "2d", 2))
kusano 7d535a
				group3options |= GROUP3OPT_2DENCODING;
kusano 7d535a
			else if (strneq(cp, "fill", 4))
kusano 7d535a
				group3options |= GROUP3OPT_FILLBITS;
kusano 7d535a
			else
kusano 7d535a
				usage();
kusano 7d535a
		} while ((cp = strchr(cp, ':')));
kusano 7d535a
	}
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, "g3", 2)) {
kusano 7d535a
		processG3Options(opt);
kusano 7d535a
		compression = COMPRESSION_CCITTFAX3;
kusano 7d535a
	} else if (streq(opt, "g4"))
kusano 7d535a
		compression = COMPRESSION_CCITTFAX4;
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
main(int argc, char* argv[])
kusano 7d535a
{
kusano 7d535a
	TIFF *in, *out;
kusano 7d535a
	uint16 samplesperpixel, bitspersample = 1, shortv;
kusano 7d535a
	float floatv;
kusano 7d535a
	char thing[1024];
kusano 7d535a
	uint32 rowsperstrip = (uint32) -1;
kusano 7d535a
	uint16 fillorder = 0;
kusano 7d535a
	int c;
kusano 7d535a
	extern int optind;
kusano 7d535a
	extern char *optarg;
kusano 7d535a
kusano 7d535a
	while ((c = getopt(argc, argv, "c:f:r:t:")) != -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 'f':		/* fill order */
kusano 7d535a
			if (streq(optarg, "lsb2msb"))
kusano 7d535a
				fillorder = FILLORDER_LSB2MSB;
kusano 7d535a
			else if (streq(optarg, "msb2lsb"))
kusano 7d535a
				fillorder = FILLORDER_MSB2LSB;
kusano 7d535a
			else
kusano 7d535a
				usage();
kusano 7d535a
			break;
kusano 7d535a
		case 'r':		/* rows/strip */
kusano 7d535a
			rowsperstrip = atoi(optarg);
kusano 7d535a
			break;
kusano 7d535a
		case 't':
kusano 7d535a
			threshold = atoi(optarg);
kusano 7d535a
			if (threshold < 0)
kusano 7d535a
				threshold = 0;
kusano 7d535a
			else if (threshold > 255)
kusano 7d535a
				threshold = 255;
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_SAMPLESPERPIXEL, &samplesperpixel);
kusano 7d535a
	if (samplesperpixel != 1) {
kusano 7d535a
		fprintf(stderr, "%s: Not a b&w image.\n", argv[0]);
kusano 7d535a
		return (-1);
kusano 7d535a
	}
kusano 7d535a
	TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample);
kusano 7d535a
	if (bitspersample != 8) {
kusano 7d535a
		fprintf(stderr,
kusano 7d535a
		    " %s: Sorry, only handle 8-bit samples.\n", argv[0]);
kusano 7d535a
		return (-1);
kusano 7d535a
	}
kusano 7d535a
	out = TIFFOpen(argv[optind+1], "w");
kusano 7d535a
	if (out == NULL)
kusano 7d535a
		return (-1);
kusano 7d535a
	CopyField(TIFFTAG_IMAGEWIDTH, imagewidth);
kusano 7d535a
	TIFFGetField(in, TIFFTAG_IMAGELENGTH, &imagelength);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_IMAGELENGTH, imagelength-1);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 1);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
kusano 7d535a
	if (fillorder)
kusano 7d535a
		TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);
kusano 7d535a
	else
kusano 7d535a
		CopyField(TIFFTAG_FILLORDER, shortv);
kusano 7d535a
	sprintf(thing, "Dithered B&W version of %s", argv[optind]);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, thing);
kusano 7d535a
	CopyField(TIFFTAG_PHOTOMETRIC, shortv);
kusano 7d535a
	CopyField(TIFFTAG_ORIENTATION, shortv);
kusano 7d535a
	CopyField(TIFFTAG_XRESOLUTION, floatv);
kusano 7d535a
	CopyField(TIFFTAG_YRESOLUTION, floatv);
kusano 7d535a
	CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
kusano 7d535a
        rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
kusano 7d535a
	TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
kusano 7d535a
	switch (compression) {
kusano 7d535a
	case COMPRESSION_CCITTFAX3:
kusano 7d535a
		TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, group3options);
kusano 7d535a
		break;
kusano 7d535a
	case COMPRESSION_LZW:
kusano 7d535a
	case COMPRESSION_DEFLATE:
kusano 7d535a
		if (predictor)
kusano 7d535a
			TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
kusano 7d535a
		break;
kusano 7d535a
	}
kusano 7d535a
	fsdither(in, out);
kusano 7d535a
	TIFFClose(in);
kusano 7d535a
	TIFFClose(out);
kusano 7d535a
	return (0);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
char* stuff[] = {
kusano 7d535a
"usage: tiffdither [options] input.tif output.tif",
kusano 7d535a
"where options are:",
kusano 7d535a
" -r #		make each strip have no more than # rows",
kusano 7d535a
" -f lsb2msb	force lsb-to-msb FillOrder for output",
kusano 7d535a
" -f msb2lsb	force msb-to-lsb FillOrder for output",
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 g3[:opts]	compress output with CCITT Group 3 encoding",
kusano 7d535a
" -c g4		compress output with CCITT Group 4 encoding",
kusano 7d535a
" -c none	use no compression algorithm on output",
kusano 7d535a
"",
kusano 7d535a
"Group 3 options:",
kusano 7d535a
" 1d		use default CCITT Group 3 1D-encoding",
kusano 7d535a
" 2d		use optional CCITT Group 3 2D-encoding",
kusano 7d535a
" fill		byte-align EOL codes",
kusano 7d535a
"For example, -c g3:2d:fill to get G3-2D-encoded data with byte-aligned EOLs",
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
/* 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
 */