Blame tools/tiffdither.c

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