roentgen b75cab
/* $Id: tiff2bw.c,v 1.15 2010-07-02 12:02:56 dron 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
#include <ctype.h></ctype.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
/* x% weighting -> fraction of full color */
roentgen b75cab
#define	PCT(x)	(((x)*255+127)/100)
roentgen b75cab
int	RED = PCT(30);		/* 30% */
roentgen b75cab
int	GREEN = PCT(59);	/* 59% */
roentgen b75cab
int	BLUE = PCT(11);		/* 11% */
roentgen b75cab
roentgen b75cab
static	void usage(void);
roentgen b75cab
static	int processCompressOptions(char*);
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
compresscontig(unsigned char* out, unsigned char* rgb, uint32 n)
roentgen b75cab
{
roentgen b75cab
	register int v, red = RED, green = GREEN, blue = BLUE;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		v = red*(*rgb++);
roentgen b75cab
		v += green*(*rgb++);
roentgen b75cab
		v += blue*(*rgb++);
roentgen b75cab
		*out++ = v>>8;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
compresssep(unsigned char* out,
roentgen b75cab
	    unsigned char* r, unsigned char* g, unsigned char* b, uint32 n)
roentgen b75cab
{
roentgen b75cab
	register uint32 red = RED, green = GREEN, blue = BLUE;
roentgen b75cab
roentgen b75cab
	while (n-- > 0)
roentgen b75cab
		*out++ = (unsigned char)
roentgen b75cab
			((red*(*r++) + green*(*g++) + blue*(*b++)) >> 8);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
checkcmap(TIFF* tif, int n, uint16* r, uint16* g, uint16* b)
roentgen b75cab
{
roentgen b75cab
	while (n-- > 0)
roentgen b75cab
		if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
roentgen b75cab
			return (16);
roentgen b75cab
	TIFFWarning(TIFFFileName(tif), "Assuming 8-bit colormap");
roentgen b75cab
	return (8);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
compresspalette(unsigned char* out, unsigned char* data, uint32 n, uint16* rmap, uint16* gmap, uint16* bmap)
roentgen b75cab
{
roentgen b75cab
	register int v, red = RED, green = GREEN, blue = BLUE;
roentgen b75cab
roentgen b75cab
	while (n-- > 0) {
roentgen b75cab
		unsigned int ix = *data++;
roentgen b75cab
		v = red*rmap[ix];
roentgen b75cab
		v += green*gmap[ix];
roentgen b75cab
		v += blue*bmap[ix];
roentgen b75cab
		*out++ = v>>8;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static	uint16 compression = (uint16) -1;
roentgen b75cab
static	uint16 predictor = 0;
roentgen b75cab
static	int jpegcolormode = JPEGCOLORMODE_RGB;
roentgen b75cab
static	int quality = 75;		/* JPEG quality */
roentgen b75cab
roentgen b75cab
static	void cpTags(TIFF* in, TIFF* out);
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
main(int argc, char* argv[])
roentgen b75cab
{
roentgen b75cab
	uint32 rowsperstrip = (uint32) -1;
roentgen b75cab
	TIFF *in, *out;
roentgen b75cab
	uint32 w, h;
roentgen b75cab
	uint16 samplesperpixel;
roentgen b75cab
	uint16 bitspersample;
roentgen b75cab
	uint16 config;
roentgen b75cab
	uint16 photometric;
roentgen b75cab
	uint16* red;
roentgen b75cab
	uint16* green;
roentgen b75cab
	uint16* blue;
roentgen b75cab
	tsize_t rowsize;
roentgen b75cab
	register uint32 row;
roentgen b75cab
	register tsample_t s;
roentgen b75cab
	unsigned char *inbuf, *outbuf;
roentgen b75cab
	char thing[1024];
roentgen b75cab
	int c;
roentgen b75cab
	extern int optind;
roentgen b75cab
	extern char *optarg;
roentgen b75cab
roentgen b75cab
	while ((c = getopt(argc, argv, "c:r:R:G:B:")) != -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 'r':		/* rows/strip */
roentgen b75cab
			rowsperstrip = atoi(optarg);
roentgen b75cab
			break;
roentgen b75cab
		case 'R':
roentgen b75cab
			RED = PCT(atoi(optarg));
roentgen b75cab
			break;
roentgen b75cab
		case 'G':
roentgen b75cab
			GREEN = PCT(atoi(optarg));
roentgen b75cab
			break;
roentgen b75cab
		case 'B':
roentgen b75cab
			BLUE = PCT(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
	photometric = 0;
roentgen b75cab
	TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photometric);
roentgen b75cab
	if (photometric != PHOTOMETRIC_RGB && photometric != PHOTOMETRIC_PALETTE ) {
roentgen b75cab
		fprintf(stderr,
roentgen b75cab
	    "%s: Bad photometric; can only handle RGB and Palette images.\n",
roentgen b75cab
		    argv[optind]);
roentgen b75cab
		return (-1);
roentgen b75cab
	}
roentgen b75cab
	TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
roentgen b75cab
	if (samplesperpixel != 1 && samplesperpixel != 3) {
roentgen b75cab
		fprintf(stderr, "%s: Bad samples/pixel %u.\n",
roentgen b75cab
		    argv[optind], samplesperpixel);
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[optind]);
roentgen b75cab
		return (-1);
roentgen b75cab
	}
roentgen b75cab
	TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &w);
roentgen b75cab
	TIFFGetField(in, TIFFTAG_IMAGELENGTH, &h);
roentgen b75cab
	TIFFGetField(in, TIFFTAG_PLANARCONFIG, &config);
roentgen b75cab
roentgen b75cab
	out = TIFFOpen(argv[optind+1], "w");
roentgen b75cab
	if (out == NULL)
roentgen b75cab
		return (-1);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_IMAGEWIDTH, w);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_IMAGELENGTH, h);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
roentgen b75cab
	cpTags(in, out);
roentgen b75cab
	if (compression != (uint16) -1) {
roentgen b75cab
		TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
roentgen b75cab
		switch (compression) {
roentgen b75cab
		case COMPRESSION_JPEG:
roentgen b75cab
			TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);
roentgen b75cab
			TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode);
roentgen b75cab
			break;
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
	}
roentgen b75cab
	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
roentgen b75cab
	sprintf(thing, "B&W version of %s", argv[optind]);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, thing);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_SOFTWARE, "tiff2bw");
roentgen b75cab
	outbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));
roentgen b75cab
	TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
roentgen b75cab
	    TIFFDefaultStripSize(out, rowsperstrip));
roentgen b75cab
roentgen b75cab
#define	pack(a,b)	((a)<<8 | (b))
roentgen b75cab
	switch (pack(photometric, config)) {
roentgen b75cab
	case pack(PHOTOMETRIC_PALETTE, PLANARCONFIG_CONTIG):
roentgen b75cab
	case pack(PHOTOMETRIC_PALETTE, PLANARCONFIG_SEPARATE):
roentgen b75cab
		TIFFGetField(in, TIFFTAG_COLORMAP, &red, &green, &blue);
roentgen b75cab
		/*
roentgen b75cab
		 * Convert 16-bit colormap to 8-bit (unless it looks
roentgen b75cab
		 * like an old-style 8-bit colormap).
roentgen b75cab
		 */
roentgen b75cab
		if (checkcmap(in, 1<
roentgen b75cab
			int i;
roentgen b75cab
#define	CVT(x)		(((x) * 255L) / ((1L<<16)-1))
roentgen b75cab
			for (i = (1<<bitspersample)-1; i="">= 0; i--) {</bitspersample)-1;>
roentgen b75cab
				red[i] = CVT(red[i]);
roentgen b75cab
				green[i] = CVT(green[i]);
roentgen b75cab
				blue[i] = CVT(blue[i]);
roentgen b75cab
			}
roentgen b75cab
#undef CVT
roentgen b75cab
		}
roentgen b75cab
		inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
roentgen b75cab
		for (row = 0; row < h; row++) {
roentgen b75cab
			if (TIFFReadScanline(in, inbuf, row, 0) < 0)
roentgen b75cab
				break;
roentgen b75cab
			compresspalette(outbuf, inbuf, w, red, green, blue);
roentgen b75cab
			if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
roentgen b75cab
				break;
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	case pack(PHOTOMETRIC_RGB, PLANARCONFIG_CONTIG):
roentgen b75cab
		inbuf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in));
roentgen b75cab
		for (row = 0; row < h; row++) {
roentgen b75cab
			if (TIFFReadScanline(in, inbuf, row, 0) < 0)
roentgen b75cab
				break;
roentgen b75cab
			compresscontig(outbuf, inbuf, w);
roentgen b75cab
			if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
roentgen b75cab
				break;
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	case pack(PHOTOMETRIC_RGB, PLANARCONFIG_SEPARATE):
roentgen b75cab
		rowsize = TIFFScanlineSize(in);
roentgen b75cab
		inbuf = (unsigned char *)_TIFFmalloc(3*rowsize);
roentgen b75cab
		for (row = 0; row < h; row++) {
roentgen b75cab
			for (s = 0; s < 3; s++)
roentgen b75cab
				if (TIFFReadScanline(in,
roentgen b75cab
				    inbuf+s*rowsize, row, s) < 0)
roentgen b75cab
					 return (-1);
roentgen b75cab
			compresssep(outbuf,
roentgen b75cab
			    inbuf, inbuf+rowsize, inbuf+2*rowsize, w);
roentgen b75cab
			if (TIFFWriteScanline(out, outbuf, row, 0) < 0)
roentgen b75cab
				break;
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
#undef pack
roentgen b75cab
	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, "jpeg", 4)) {
roentgen b75cab
		char* cp = strchr(opt, ':');
roentgen b75cab
roentgen b75cab
                compression = COMPRESSION_JPEG;
roentgen b75cab
                while( cp )
roentgen b75cab
                {
roentgen b75cab
                    if (isdigit((int)cp[1]))
roentgen b75cab
			quality = atoi(cp+1);
roentgen b75cab
                    else if (cp[1] == 'r' )
roentgen b75cab
			jpegcolormode = JPEGCOLORMODE_RAW;
roentgen b75cab
                    else
roentgen b75cab
                        usage();
roentgen b75cab
roentgen b75cab
                    cp = strchr(cp+1,':');
roentgen b75cab
                }
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
#define	CopyField(tag, v) \
roentgen b75cab
    if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
roentgen b75cab
#define	CopyField2(tag, v1, v2) \
roentgen b75cab
    if (TIFFGetField(in, tag, &v1, &v2)) TIFFSetField(out, tag, v1, v2)
roentgen b75cab
#define	CopyField3(tag, v1, v2, v3) \
roentgen b75cab
    if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3)
roentgen b75cab
#define	CopyField4(tag, v1, v2, v3, v4) \
roentgen b75cab
    if (TIFFGetField(in, tag, &v1, &v2, &v3, &v4)) TIFFSetField(out, tag, v1, v2, v3, v4)
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
cpTag(TIFF* in, TIFF* out, uint16 tag, uint16 count, TIFFDataType type)
roentgen b75cab
{
roentgen b75cab
	switch (type) {
roentgen b75cab
	case TIFF_SHORT:
roentgen b75cab
		if (count == 1) {
roentgen b75cab
			uint16 shortv;
roentgen b75cab
			CopyField(tag, shortv);
roentgen b75cab
		} else if (count == 2) {
roentgen b75cab
			uint16 shortv1, shortv2;
roentgen b75cab
			CopyField2(tag, shortv1, shortv2);
roentgen b75cab
		} else if (count == 4) {
roentgen b75cab
			uint16 *tr, *tg, *tb, *ta;
roentgen b75cab
			CopyField4(tag, tr, tg, tb, ta);
roentgen b75cab
		} else if (count == (uint16) -1) {
roentgen b75cab
			uint16 shortv1;
roentgen b75cab
			uint16* shortav;
roentgen b75cab
			CopyField2(tag, shortv1, shortav);
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	case TIFF_LONG:
roentgen b75cab
		{ uint32 longv;
roentgen b75cab
		  CopyField(tag, longv);
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	case TIFF_RATIONAL:
roentgen b75cab
		if (count == 1) {
roentgen b75cab
			float floatv;
roentgen b75cab
			CopyField(tag, floatv);
roentgen b75cab
		} else if (count == (uint16) -1) {
roentgen b75cab
			float* floatav;
roentgen b75cab
			CopyField(tag, floatav);
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	case TIFF_ASCII:
roentgen b75cab
		{ char* stringv;
roentgen b75cab
		  CopyField(tag, stringv);
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	case TIFF_DOUBLE:
roentgen b75cab
		if (count == 1) {
roentgen b75cab
			double doublev;
roentgen b75cab
			CopyField(tag, doublev);
roentgen b75cab
		} else if (count == (uint16) -1) {
roentgen b75cab
			double* doubleav;
roentgen b75cab
			CopyField(tag, doubleav);
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
          default:
roentgen b75cab
                TIFFError(TIFFFileName(in),
roentgen b75cab
                          "Data type %d is not supported, tag %d skipped.",
roentgen b75cab
                          tag, type);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#undef CopyField4
roentgen b75cab
#undef CopyField3
roentgen b75cab
#undef CopyField2
roentgen b75cab
#undef CopyField
roentgen b75cab
roentgen b75cab
static struct cpTag {
roentgen b75cab
	uint16	tag;
roentgen b75cab
	uint16	count;
roentgen b75cab
	TIFFDataType type;
roentgen b75cab
} tags[] = {
roentgen b75cab
	{ TIFFTAG_SUBFILETYPE,		1, TIFF_LONG },
roentgen b75cab
	{ TIFFTAG_THRESHHOLDING,	1, TIFF_SHORT },
roentgen b75cab
	{ TIFFTAG_DOCUMENTNAME,		1, TIFF_ASCII },
roentgen b75cab
	{ TIFFTAG_IMAGEDESCRIPTION,	1, TIFF_ASCII },
roentgen b75cab
	{ TIFFTAG_MAKE,			1, TIFF_ASCII },
roentgen b75cab
	{ TIFFTAG_MODEL,		1, TIFF_ASCII },
roentgen b75cab
	{ TIFFTAG_MINSAMPLEVALUE,	1, TIFF_SHORT },
roentgen b75cab
	{ TIFFTAG_MAXSAMPLEVALUE,	1, TIFF_SHORT },
roentgen b75cab
	{ TIFFTAG_XRESOLUTION,		1, TIFF_RATIONAL },
roentgen b75cab
	{ TIFFTAG_YRESOLUTION,		1, TIFF_RATIONAL },
roentgen b75cab
	{ TIFFTAG_PAGENAME,		1, TIFF_ASCII },
roentgen b75cab
	{ TIFFTAG_XPOSITION,		1, TIFF_RATIONAL },
roentgen b75cab
	{ TIFFTAG_YPOSITION,		1, TIFF_RATIONAL },
roentgen b75cab
	{ TIFFTAG_RESOLUTIONUNIT,	1, TIFF_SHORT },
roentgen b75cab
	{ TIFFTAG_SOFTWARE,		1, TIFF_ASCII },
roentgen b75cab
	{ TIFFTAG_DATETIME,		1, TIFF_ASCII },
roentgen b75cab
	{ TIFFTAG_ARTIST,		1, TIFF_ASCII },
roentgen b75cab
	{ TIFFTAG_HOSTCOMPUTER,		1, TIFF_ASCII },
roentgen b75cab
	{ TIFFTAG_WHITEPOINT,		2, TIFF_RATIONAL },
roentgen b75cab
	{ TIFFTAG_PRIMARYCHROMATICITIES,(uint16) -1,TIFF_RATIONAL },
roentgen b75cab
	{ TIFFTAG_HALFTONEHINTS,	2, TIFF_SHORT },
roentgen b75cab
	{ TIFFTAG_INKSET,		1, TIFF_SHORT },
roentgen b75cab
	{ TIFFTAG_DOTRANGE,		2, TIFF_SHORT },
roentgen b75cab
	{ TIFFTAG_TARGETPRINTER,	1, TIFF_ASCII },
roentgen b75cab
	{ TIFFTAG_SAMPLEFORMAT,		1, TIFF_SHORT },
roentgen b75cab
	{ TIFFTAG_YCBCRCOEFFICIENTS,	(uint16) -1,TIFF_RATIONAL },
roentgen b75cab
	{ TIFFTAG_YCBCRSUBSAMPLING,	2, TIFF_SHORT },
roentgen b75cab
	{ TIFFTAG_YCBCRPOSITIONING,	1, TIFF_SHORT },
roentgen b75cab
	{ TIFFTAG_REFERENCEBLACKWHITE,	(uint16) -1,TIFF_RATIONAL },
roentgen b75cab
	{ TIFFTAG_EXTRASAMPLES,		(uint16) -1, TIFF_SHORT },
roentgen b75cab
	{ TIFFTAG_SMINSAMPLEVALUE,	1, TIFF_DOUBLE },
roentgen b75cab
	{ TIFFTAG_SMAXSAMPLEVALUE,	1, TIFF_DOUBLE },
roentgen b75cab
	{ TIFFTAG_STONITS,		1, TIFF_DOUBLE },
roentgen b75cab
};
roentgen b75cab
#define	NTAGS	(sizeof (tags) / sizeof (tags[0]))
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
cpTags(TIFF* in, TIFF* out)
roentgen b75cab
{
roentgen b75cab
    struct cpTag *p;
roentgen b75cab
    for (p = tags; p < &tags[NTAGS]; p++)
roentgen b75cab
	cpTag(in, out, p->tag, p->count, p->type);
roentgen b75cab
}
roentgen b75cab
#undef NTAGS
roentgen b75cab
roentgen b75cab
char* stuff[] = {
roentgen b75cab
"usage: tiff2bw [options] input.tif output.tif",
roentgen b75cab
"where options are:",
roentgen b75cab
" -R %		use #% from red channel",
roentgen b75cab
" -G %		use #% from green channel",
roentgen b75cab
" -B %		use #% from blue channel",
roentgen b75cab
"",
roentgen b75cab
" -r #		make each strip have no more than # rows",
roentgen b75cab
"",
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
"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
 */