roentgen b75cab
/* $Id: rgb2ycbcr.c,v 1.14 2011-05-31 17:03:16 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1991-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 <string.h></string.h>
roentgen b75cab
#include <stdlib.h></stdlib.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 "tiffiop.h"
roentgen b75cab
#include "tiffio.h"
roentgen b75cab
roentgen b75cab
#define	streq(a,b)	(strcmp(a,b) == 0)
roentgen b75cab
#define	CopyField(tag, v) \
roentgen b75cab
    if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
roentgen b75cab
roentgen b75cab
#ifndef howmany
roentgen b75cab
#define	howmany(x, y)	(((x)+((y)-1))/(y))
roentgen b75cab
#endif
roentgen b75cab
#define	roundup(x, y)	(howmany(x,y)*((uint32)(y)))
roentgen b75cab
roentgen b75cab
#define	LumaRed		ycbcrCoeffs[0]
roentgen b75cab
#define	LumaGreen	ycbcrCoeffs[1]
roentgen b75cab
#define	LumaBlue	ycbcrCoeffs[2]
roentgen b75cab
roentgen b75cab
uint16	compression = COMPRESSION_PACKBITS;
roentgen b75cab
uint32	rowsperstrip = (uint32) -1;
roentgen b75cab
roentgen b75cab
uint16	horizSubSampling = 2;		/* YCbCr horizontal subsampling */
roentgen b75cab
uint16	vertSubSampling = 2;		/* YCbCr vertical subsampling */
roentgen b75cab
float	ycbcrCoeffs[3] = { .299F, .587F, .114F };
roentgen b75cab
/* default coding range is CCIR Rec 601-1 with no headroom/footroom */
roentgen b75cab
float	refBlackWhite[6] = { 0.F, 255.F, 128.F, 255.F, 128.F, 255.F };
roentgen b75cab
roentgen b75cab
static	int tiffcvt(TIFF* in, TIFF* out);
roentgen b75cab
static	void usage(int code);
roentgen b75cab
static	void setupLumaTables(void);
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
main(int argc, char* argv[])
roentgen b75cab
{
roentgen b75cab
	TIFF *in, *out;
roentgen b75cab
	int c;
roentgen b75cab
	extern int optind;
roentgen b75cab
	extern char *optarg;
roentgen b75cab
roentgen b75cab
	while ((c = getopt(argc, argv, "c:h:r:v:z")) != -1)
roentgen b75cab
		switch (c) {
roentgen b75cab
		case 'c':
roentgen b75cab
			if (streq(optarg, "none"))
roentgen b75cab
			    compression = COMPRESSION_NONE;
roentgen b75cab
			else if (streq(optarg, "packbits"))
roentgen b75cab
			    compression = COMPRESSION_PACKBITS;
roentgen b75cab
			else if (streq(optarg, "lzw"))
roentgen b75cab
			    compression = COMPRESSION_LZW;
roentgen b75cab
			else if (streq(optarg, "jpeg"))
roentgen b75cab
			    compression = COMPRESSION_JPEG;
roentgen b75cab
			else if (streq(optarg, "zip"))
roentgen b75cab
			    compression = COMPRESSION_ADOBE_DEFLATE;
roentgen b75cab
			else
roentgen b75cab
			    usage(-1);
roentgen b75cab
			break;
roentgen b75cab
		case 'h':
roentgen b75cab
			horizSubSampling = atoi(optarg);
roentgen b75cab
			break;
roentgen b75cab
		case 'v':
roentgen b75cab
			vertSubSampling = atoi(optarg);
roentgen b75cab
			break;
roentgen b75cab
		case 'r':
roentgen b75cab
			rowsperstrip = atoi(optarg);
roentgen b75cab
			break;
roentgen b75cab
		case 'z':	/* CCIR Rec 601-1 w/ headroom/footroom */
roentgen b75cab
			refBlackWhite[0] = 16.;
roentgen b75cab
			refBlackWhite[1] = 235.;
roentgen b75cab
			refBlackWhite[2] = 128.;
roentgen b75cab
			refBlackWhite[3] = 240.;
roentgen b75cab
			refBlackWhite[4] = 128.;
roentgen b75cab
			refBlackWhite[5] = 240.;
roentgen b75cab
			break;
roentgen b75cab
		case '?':
roentgen b75cab
			usage(0);
roentgen b75cab
			/*NOTREACHED*/
roentgen b75cab
		}
roentgen b75cab
	if (argc - optind < 2)
roentgen b75cab
		usage(-1);
roentgen b75cab
	out = TIFFOpen(argv[argc-1], "w");
roentgen b75cab
	if (out == NULL)
roentgen b75cab
		return (-2);
roentgen b75cab
	setupLumaTables();
roentgen b75cab
	for (; optind < argc-1; optind++) {
roentgen b75cab
		in = TIFFOpen(argv[optind], "r");
roentgen b75cab
		if (in != NULL) {
roentgen b75cab
			do {
roentgen b75cab
				if (!tiffcvt(in, out) ||
roentgen b75cab
				    !TIFFWriteDirectory(out)) {
roentgen b75cab
					(void) TIFFClose(out);
roentgen b75cab
					return (1);
roentgen b75cab
				}
roentgen b75cab
			} while (TIFFReadDirectory(in));
roentgen b75cab
			(void) TIFFClose(in);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	(void) TIFFClose(out);
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
float	*lumaRed;
roentgen b75cab
float	*lumaGreen;
roentgen b75cab
float	*lumaBlue;
roentgen b75cab
float	D1, D2;
roentgen b75cab
int	Yzero;
roentgen b75cab
roentgen b75cab
static float*
roentgen b75cab
setupLuma(float c)
roentgen b75cab
{
roentgen b75cab
	float *v = (float *)_TIFFmalloc(256 * sizeof (float));
roentgen b75cab
	int i;
roentgen b75cab
	for (i = 0; i < 256; i++)
roentgen b75cab
		v[i] = c * i;
roentgen b75cab
	return (v);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static unsigned
roentgen b75cab
V2Code(float f, float RB, float RW, int CR)
roentgen b75cab
{
roentgen b75cab
	unsigned int c = (unsigned int)((((f)*(RW-RB)/CR)+RB)+.5);
roentgen b75cab
	return (c > 255 ? 255 : c);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
setupLumaTables(void)
roentgen b75cab
{
roentgen b75cab
	lumaRed = setupLuma(LumaRed);
roentgen b75cab
	lumaGreen = setupLuma(LumaGreen);
roentgen b75cab
	lumaBlue = setupLuma(LumaBlue);
roentgen b75cab
	D1 = 1.F/(2.F - 2.F*LumaBlue);
roentgen b75cab
	D2 = 1.F/(2.F - 2.F*LumaRed);
roentgen b75cab
	Yzero = V2Code(0, refBlackWhite[0], refBlackWhite[1], 255);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
cvtClump(unsigned char* op, uint32* raster, uint32 ch, uint32 cw, uint32 w)
roentgen b75cab
{
roentgen b75cab
	float Y, Cb = 0, Cr = 0;
roentgen b75cab
	uint32 j, k;
roentgen b75cab
	/*
roentgen b75cab
	 * Convert ch-by-cw block of RGB
roentgen b75cab
	 * to YCbCr and sample accordingly.
roentgen b75cab
	 */
roentgen b75cab
	for (k = 0; k < ch; k++) {
roentgen b75cab
		for (j = 0; j < cw; j++) {
roentgen b75cab
			uint32 RGB = (raster - k*w)[j];
roentgen b75cab
			Y = lumaRed[TIFFGetR(RGB)] +
roentgen b75cab
			    lumaGreen[TIFFGetG(RGB)] +
roentgen b75cab
			    lumaBlue[TIFFGetB(RGB)];
roentgen b75cab
			/* accumulate chrominance */
roentgen b75cab
			Cb += (TIFFGetB(RGB) - Y) * D1;
roentgen b75cab
			Cr += (TIFFGetR(RGB) - Y) * D2;
roentgen b75cab
			/* emit luminence */
roentgen b75cab
			*op++ = V2Code(Y,
roentgen b75cab
			    refBlackWhite[0], refBlackWhite[1], 255);
roentgen b75cab
		}
roentgen b75cab
		for (; j < horizSubSampling; j++)
roentgen b75cab
			*op++ = Yzero;
roentgen b75cab
	}
roentgen b75cab
	for (; k < vertSubSampling; k++) {
roentgen b75cab
		for (j = 0; j < horizSubSampling; j++)
roentgen b75cab
			*op++ = Yzero;
roentgen b75cab
	}
roentgen b75cab
	/* emit sampled chrominance values */
roentgen b75cab
	*op++ = V2Code(Cb / (ch*cw), refBlackWhite[2], refBlackWhite[3], 127);
roentgen b75cab
	*op++ = V2Code(Cr / (ch*cw), refBlackWhite[4], refBlackWhite[5], 127);
roentgen b75cab
}
roentgen b75cab
#undef LumaRed
roentgen b75cab
#undef LumaGreen
roentgen b75cab
#undef LumaBlue
roentgen b75cab
#undef V2Code
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Convert a strip of RGB data to YCbCr and
roentgen b75cab
 * sample to generate the output data.
roentgen b75cab
 */
roentgen b75cab
static void
roentgen b75cab
cvtStrip(unsigned char* op, uint32* raster, uint32 nrows, uint32 width)
roentgen b75cab
{
roentgen b75cab
	uint32 x;
roentgen b75cab
	int clumpSize = vertSubSampling * horizSubSampling + 2;
roentgen b75cab
	uint32 *tp;
roentgen b75cab
roentgen b75cab
	for (; nrows >= vertSubSampling; nrows -= vertSubSampling) {
roentgen b75cab
		tp = raster;
roentgen b75cab
		for (x = width; x >= horizSubSampling; x -= horizSubSampling) {
roentgen b75cab
			cvtClump(op, tp,
roentgen b75cab
			    vertSubSampling, horizSubSampling, width);
roentgen b75cab
			op += clumpSize;
roentgen b75cab
			tp += horizSubSampling;
roentgen b75cab
		}
roentgen b75cab
		if (x > 0) {
roentgen b75cab
			cvtClump(op, tp, vertSubSampling, x, width);
roentgen b75cab
			op += clumpSize;
roentgen b75cab
		}
roentgen b75cab
		raster -= vertSubSampling*width;
roentgen b75cab
	}
roentgen b75cab
	if (nrows > 0) {
roentgen b75cab
		tp = raster;
roentgen b75cab
		for (x = width; x >= horizSubSampling; x -= horizSubSampling) {
roentgen b75cab
			cvtClump(op, tp, nrows, horizSubSampling, width);
roentgen b75cab
			op += clumpSize;
roentgen b75cab
			tp += horizSubSampling;
roentgen b75cab
		}
roentgen b75cab
		if (x > 0)
roentgen b75cab
			cvtClump(op, tp, nrows, x, width);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
cvtRaster(TIFF* tif, uint32* raster, uint32 width, uint32 height)
roentgen b75cab
{
roentgen b75cab
	uint32 y;
roentgen b75cab
	tstrip_t strip = 0;
roentgen b75cab
	tsize_t cc, acc;
roentgen b75cab
	unsigned char* buf;
roentgen b75cab
	uint32 rwidth = roundup(width, horizSubSampling);
roentgen b75cab
	uint32 rheight = roundup(height, vertSubSampling);
roentgen b75cab
	uint32 nrows = (rowsperstrip > rheight ? rheight : rowsperstrip);
roentgen b75cab
        uint32 rnrows = roundup(nrows,vertSubSampling);
roentgen b75cab
roentgen b75cab
	cc = rnrows*rwidth +
roentgen b75cab
	    2*((rnrows*rwidth) / (horizSubSampling*vertSubSampling));
roentgen b75cab
	buf = (unsigned char*)_TIFFmalloc(cc);
roentgen b75cab
	// FIXME unchecked malloc
roentgen b75cab
	for (y = height; (int32) y > 0; y -= nrows) {
roentgen b75cab
		uint32 nr = (y > nrows ? nrows : y);
roentgen b75cab
		cvtStrip(buf, raster + (y-1)*width, nr, width);
roentgen b75cab
		nr = roundup(nr, vertSubSampling);
roentgen b75cab
		acc = nr*rwidth +
roentgen b75cab
			2*((nr*rwidth)/(horizSubSampling*vertSubSampling));
roentgen b75cab
		if (!TIFFWriteEncodedStrip(tif, strip++, buf, acc)) {
roentgen b75cab
			_TIFFfree(buf);
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	_TIFFfree(buf);
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
tiffcvt(TIFF* in, TIFF* out)
roentgen b75cab
{
roentgen b75cab
	uint32 width, height;		/* image width & height */
roentgen b75cab
	uint32* raster;			/* retrieve RGBA image */
roentgen b75cab
	uint16 shortv;
roentgen b75cab
	float floatv;
roentgen b75cab
	char *stringv;
roentgen b75cab
	uint32 longv;
roentgen b75cab
	int result;
roentgen b75cab
	size_t pixel_count;
roentgen b75cab
roentgen b75cab
	TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width);
roentgen b75cab
	TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height);
roentgen b75cab
	pixel_count = width * height;
roentgen b75cab
roentgen b75cab
 	/* XXX: Check the integer overflow. */
roentgen b75cab
 	if (!width || !height || pixel_count / width != height) {
roentgen b75cab
 		TIFFError(TIFFFileName(in),
roentgen b75cab
 			  "Malformed input file; "
roentgen b75cab
 			  "can't allocate buffer for raster of %lux%lu size",
roentgen b75cab
 			  (unsigned long)width, (unsigned long)height);
roentgen b75cab
 		return 0;
roentgen b75cab
 	}
roentgen b75cab
 
roentgen b75cab
 	raster = (uint32*)_TIFFCheckMalloc(in, pixel_count, sizeof(uint32),
roentgen b75cab
 					   "raster buffer");
roentgen b75cab
  	if (raster == 0) {
roentgen b75cab
 		TIFFError(TIFFFileName(in),
roentgen b75cab
 			  "Failed to allocate buffer (%lu elements of %lu each)",
roentgen b75cab
 			  (unsigned long)pixel_count,
roentgen b75cab
 			  (unsigned long)sizeof(uint32));
roentgen b75cab
  		return (0);
roentgen b75cab
  	}
roentgen b75cab
roentgen b75cab
	if (!TIFFReadRGBAImage(in, width, height, raster, 0)) {
roentgen b75cab
		_TIFFfree(raster);
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	CopyField(TIFFTAG_SUBFILETYPE, longv);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
roentgen b75cab
	if (compression == COMPRESSION_JPEG)
roentgen b75cab
		TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW);
roentgen b75cab
	CopyField(TIFFTAG_FILLORDER, shortv);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
roentgen b75cab
	CopyField(TIFFTAG_XRESOLUTION, floatv);
roentgen b75cab
	CopyField(TIFFTAG_YRESOLUTION, floatv);
roentgen b75cab
	CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
roentgen b75cab
	{ char buf[2048];
roentgen b75cab
	  char *cp = strrchr(TIFFFileName(in), '/');
roentgen b75cab
	  sprintf(buf, "YCbCr conversion of %s", cp ? cp+1 : TIFFFileName(in));
roentgen b75cab
	  TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, buf);
roentgen b75cab
	}
roentgen b75cab
	TIFFSetField(out, TIFFTAG_SOFTWARE, TIFFGetVersion());
roentgen b75cab
	CopyField(TIFFTAG_DOCUMENTNAME, stringv);
roentgen b75cab
roentgen b75cab
	TIFFSetField(out, TIFFTAG_REFERENCEBLACKWHITE, refBlackWhite);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_YCBCRSUBSAMPLING,
roentgen b75cab
	    horizSubSampling, vertSubSampling);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_YCBCRPOSITIONING, YCBCRPOSITION_CENTERED);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_YCBCRCOEFFICIENTS, ycbcrCoeffs);
roentgen b75cab
	rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);
roentgen b75cab
	TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
roentgen b75cab
roentgen b75cab
	result = cvtRaster(out, raster, width, height);
roentgen b75cab
        _TIFFfree(raster);
roentgen b75cab
        return result;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
char* stuff[] = {
roentgen b75cab
    "usage: rgb2ycbcr [-c comp] [-r rows] [-h N] [-v N] input... output\n",
roentgen b75cab
    "where comp is one of the following compression algorithms:\n",
roentgen b75cab
    " jpeg\t\tJPEG encoding\n",
roentgen b75cab
    " lzw\t\tLempel-Ziv & Welch encoding\n",
roentgen b75cab
    " zip\t\tdeflate encoding\n",
roentgen b75cab
    " packbits\tPackBits encoding (default)\n",
roentgen b75cab
    " none\t\tno compression\n",
roentgen b75cab
    "and the other options are:\n",
roentgen b75cab
    " -r\trows/strip\n",
roentgen b75cab
    " -h\thorizontal sampling factor (1,2,4)\n",
roentgen b75cab
    " -v\tvertical sampling factor (1,2,4)\n",
roentgen b75cab
    NULL
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
usage(int code)
roentgen b75cab
{
roentgen b75cab
	char buf[BUFSIZ];
roentgen b75cab
	int i;
roentgen b75cab
roentgen b75cab
	setbuf(stderr, buf);
roentgen b75cab
       
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(code);
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
 */