roentgen b75cab
/* $Id: tif_getimage.c,v 1.82 2012-06-06 00:17:49 fwarmerdam 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
/*
roentgen b75cab
 * TIFF Library
roentgen b75cab
 *
roentgen b75cab
 * Read and return a packed RGBA image.
roentgen b75cab
 */
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
roentgen b75cab
static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
roentgen b75cab
static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
roentgen b75cab
static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
roentgen b75cab
static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
roentgen b75cab
static int PickContigCase(TIFFRGBAImage*);
roentgen b75cab
static int PickSeparateCase(TIFFRGBAImage*);
roentgen b75cab
roentgen b75cab
static int BuildMapUaToAa(TIFFRGBAImage* img);
roentgen b75cab
static int BuildMapBitdepth16To8(TIFFRGBAImage* img);
roentgen b75cab
roentgen b75cab
static const char photoTag[] = "PhotometricInterpretation";
roentgen b75cab
roentgen b75cab
/* 
roentgen b75cab
 * Helper constants used in Orientation tag handling
roentgen b75cab
 */
roentgen b75cab
#define FLIP_VERTICALLY 0x01
roentgen b75cab
#define FLIP_HORIZONTALLY 0x02
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Color conversion constants. We will define display types here.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
static const TIFFDisplay display_sRGB = {
roentgen b75cab
	{			/* XYZ -> luminance matrix */
roentgen b75cab
		{  3.2410F, -1.5374F, -0.4986F },
roentgen b75cab
		{  -0.9692F, 1.8760F, 0.0416F },
roentgen b75cab
		{  0.0556F, -0.2040F, 1.0570F }
roentgen b75cab
	},	
roentgen b75cab
	100.0F, 100.0F, 100.0F,	/* Light o/p for reference white */
roentgen b75cab
	255, 255, 255,		/* Pixel values for ref. white */
roentgen b75cab
	1.0F, 1.0F, 1.0F,	/* Residual light o/p for black pixel */
roentgen b75cab
	2.4F, 2.4F, 2.4F,	/* Gamma values for the three guns */
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Check the image to see if TIFFReadRGBAImage can deal with it.
roentgen b75cab
 * 1/0 is returned according to whether or not the image can
roentgen b75cab
 * be handled.  If 0 is returned, emsg contains the reason
roentgen b75cab
 * why it is being rejected.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFRGBAImageOK(TIFF* tif, char emsg[1024])
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory* td = &tif->tif_dir;
roentgen b75cab
	uint16 photometric;
roentgen b75cab
	int colorchannels;
roentgen b75cab
roentgen b75cab
	if (!tif->tif_decodestatus) {
roentgen b75cab
		sprintf(emsg, "Sorry, requested compression method is not configured");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	switch (td->td_bitspersample) {
roentgen b75cab
		case 1:
roentgen b75cab
		case 2:
roentgen b75cab
		case 4:
roentgen b75cab
		case 8:
roentgen b75cab
		case 16:
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
roentgen b75cab
			    td->td_bitspersample);
roentgen b75cab
			return (0);
roentgen b75cab
	}
roentgen b75cab
	colorchannels = td->td_samplesperpixel - td->td_extrasamples;
roentgen b75cab
	if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
roentgen b75cab
		switch (colorchannels) {
roentgen b75cab
			case 1:
roentgen b75cab
				photometric = PHOTOMETRIC_MINISBLACK;
roentgen b75cab
				break;
roentgen b75cab
			case 3:
roentgen b75cab
				photometric = PHOTOMETRIC_RGB;
roentgen b75cab
				break;
roentgen b75cab
			default:
roentgen b75cab
				sprintf(emsg, "Missing needed %s tag", photoTag);
roentgen b75cab
				return (0);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	switch (photometric) {
roentgen b75cab
		case PHOTOMETRIC_MINISWHITE:
roentgen b75cab
		case PHOTOMETRIC_MINISBLACK:
roentgen b75cab
		case PHOTOMETRIC_PALETTE:
roentgen b75cab
			if (td->td_planarconfig == PLANARCONFIG_CONTIG
roentgen b75cab
			    && td->td_samplesperpixel != 1
roentgen b75cab
			    && td->td_bitspersample < 8 ) {
roentgen b75cab
				sprintf(emsg,
roentgen b75cab
				    "Sorry, can not handle contiguous data with %s=%d, "
roentgen b75cab
				    "and %s=%d and Bits/Sample=%d",
roentgen b75cab
				    photoTag, photometric,
roentgen b75cab
				    "Samples/pixel", td->td_samplesperpixel,
roentgen b75cab
				    td->td_bitspersample);
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			/*
roentgen b75cab
			 * We should likely validate that any extra samples are either
roentgen b75cab
			 * to be ignored, or are alpha, and if alpha we should try to use
roentgen b75cab
			 * them.  But for now we won't bother with this.
roentgen b75cab
			*/
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_YCBCR:
roentgen b75cab
			/*
roentgen b75cab
			 * TODO: if at all meaningful and useful, make more complete
roentgen b75cab
			 * support check here, or better still, refactor to let supporting
roentgen b75cab
			 * code decide whether there is support and what meaningfull
roentgen b75cab
			 * error to return
roentgen b75cab
			 */
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_RGB:
roentgen b75cab
			if (colorchannels < 3) {
roentgen b75cab
				sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
roentgen b75cab
				    "Color channels", colorchannels);
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_SEPARATED:
roentgen b75cab
			{
roentgen b75cab
				uint16 inkset;
roentgen b75cab
				TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
roentgen b75cab
				if (inkset != INKSET_CMYK) {
roentgen b75cab
					sprintf(emsg,
roentgen b75cab
					    "Sorry, can not handle separated image with %s=%d",
roentgen b75cab
					    "InkSet", inkset);
roentgen b75cab
					return 0;
roentgen b75cab
				}
roentgen b75cab
				if (td->td_samplesperpixel < 4) {
roentgen b75cab
					sprintf(emsg,
roentgen b75cab
					    "Sorry, can not handle separated image with %s=%d",
roentgen b75cab
					    "Samples/pixel", td->td_samplesperpixel);
roentgen b75cab
					return 0;
roentgen b75cab
				}
roentgen b75cab
				break;
roentgen b75cab
			}
roentgen b75cab
		case PHOTOMETRIC_LOGL:
roentgen b75cab
			if (td->td_compression != COMPRESSION_SGILOG) {
roentgen b75cab
				sprintf(emsg, "Sorry, LogL data must have %s=%d",
roentgen b75cab
				    "Compression", COMPRESSION_SGILOG);
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_LOGLUV:
roentgen b75cab
			if (td->td_compression != COMPRESSION_SGILOG &&
roentgen b75cab
			    td->td_compression != COMPRESSION_SGILOG24) {
roentgen b75cab
				sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
roentgen b75cab
				    "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
roentgen b75cab
				sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
roentgen b75cab
				    "Planarconfiguration", td->td_planarconfig);
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_CIELAB:
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			sprintf(emsg, "Sorry, can not handle image with %s=%d",
roentgen b75cab
			    photoTag, photometric);
roentgen b75cab
			return (0);
roentgen b75cab
	}
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
TIFFRGBAImageEnd(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
	if (img->Map)
roentgen b75cab
		_TIFFfree(img->Map), img->Map = NULL;
roentgen b75cab
	if (img->BWmap)
roentgen b75cab
		_TIFFfree(img->BWmap), img->BWmap = NULL;
roentgen b75cab
	if (img->PALmap)
roentgen b75cab
		_TIFFfree(img->PALmap), img->PALmap = NULL;
roentgen b75cab
	if (img->ycbcr)
roentgen b75cab
		_TIFFfree(img->ycbcr), img->ycbcr = NULL;
roentgen b75cab
	if (img->cielab)
roentgen b75cab
		_TIFFfree(img->cielab), img->cielab = NULL;
roentgen b75cab
	if (img->UaToAa)
roentgen b75cab
		_TIFFfree(img->UaToAa), img->UaToAa = NULL;
roentgen b75cab
	if (img->Bitdepth16To8)
roentgen b75cab
		_TIFFfree(img->Bitdepth16To8), img->Bitdepth16To8 = NULL;
roentgen b75cab
roentgen b75cab
	if( img->redcmap ) {
roentgen b75cab
		_TIFFfree( img->redcmap );
roentgen b75cab
		_TIFFfree( img->greencmap );
roentgen b75cab
		_TIFFfree( img->bluecmap );
roentgen b75cab
                img->redcmap = img->greencmap = img->bluecmap = NULL;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
isCCITTCompression(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
    uint16 compress;
roentgen b75cab
    TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
roentgen b75cab
    return (compress == COMPRESSION_CCITTFAX3 ||
roentgen b75cab
	    compress == COMPRESSION_CCITTFAX4 ||
roentgen b75cab
	    compress == COMPRESSION_CCITTRLE ||
roentgen b75cab
	    compress == COMPRESSION_CCITTRLEW);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
roentgen b75cab
{
roentgen b75cab
	uint16* sampleinfo;
roentgen b75cab
	uint16 extrasamples;
roentgen b75cab
	uint16 planarconfig;
roentgen b75cab
	uint16 compress;
roentgen b75cab
	int colorchannels;
roentgen b75cab
	uint16 *red_orig, *green_orig, *blue_orig;
roentgen b75cab
	int n_color;
roentgen b75cab
roentgen b75cab
	/* Initialize to normal values */
roentgen b75cab
	img->row_offset = 0;
roentgen b75cab
	img->col_offset = 0;
roentgen b75cab
	img->redcmap = NULL;
roentgen b75cab
	img->greencmap = NULL;
roentgen b75cab
	img->bluecmap = NULL;
roentgen b75cab
	img->req_orientation = ORIENTATION_BOTLEFT;     /* It is the default */
roentgen b75cab
roentgen b75cab
	img->tif = tif;
roentgen b75cab
	img->stoponerr = stop;
roentgen b75cab
	TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
roentgen b75cab
	switch (img->bitspersample) {
roentgen b75cab
		case 1:
roentgen b75cab
		case 2:
roentgen b75cab
		case 4:
roentgen b75cab
		case 8:
roentgen b75cab
		case 16:
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
roentgen b75cab
			    img->bitspersample);
roentgen b75cab
			goto fail_return;
roentgen b75cab
	}
roentgen b75cab
	img->alpha = 0;
roentgen b75cab
	TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
roentgen b75cab
	TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
roentgen b75cab
	    &extrasamples, &sampleinfo);
roentgen b75cab
	if (extrasamples >= 1)
roentgen b75cab
	{
roentgen b75cab
		switch (sampleinfo[0]) {
roentgen b75cab
			case EXTRASAMPLE_UNSPECIFIED:          /* Workaround for some images without */
roentgen b75cab
				if (img->samplesperpixel > 3)  /* correct info about alpha channel */
roentgen b75cab
					img->alpha = EXTRASAMPLE_ASSOCALPHA;
roentgen b75cab
				break;
roentgen b75cab
			case EXTRASAMPLE_ASSOCALPHA:           /* data is pre-multiplied */
roentgen b75cab
			case EXTRASAMPLE_UNASSALPHA:           /* data is not pre-multiplied */
roentgen b75cab
				img->alpha = sampleinfo[0];
roentgen b75cab
				break;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
#ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
roentgen b75cab
	if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
roentgen b75cab
		img->photometric = PHOTOMETRIC_MINISWHITE;
roentgen b75cab
roentgen b75cab
	if( extrasamples == 0
roentgen b75cab
	    && img->samplesperpixel == 4
roentgen b75cab
	    && img->photometric == PHOTOMETRIC_RGB )
roentgen b75cab
	{
roentgen b75cab
		img->alpha = EXTRASAMPLE_ASSOCALPHA;
roentgen b75cab
		extrasamples = 1;
roentgen b75cab
	}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
	colorchannels = img->samplesperpixel - extrasamples;
roentgen b75cab
	TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
roentgen b75cab
	TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
roentgen b75cab
	if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
roentgen b75cab
		switch (colorchannels) {
roentgen b75cab
			case 1:
roentgen b75cab
				if (isCCITTCompression(tif))
roentgen b75cab
					img->photometric = PHOTOMETRIC_MINISWHITE;
roentgen b75cab
				else
roentgen b75cab
					img->photometric = PHOTOMETRIC_MINISBLACK;
roentgen b75cab
				break;
roentgen b75cab
			case 3:
roentgen b75cab
				img->photometric = PHOTOMETRIC_RGB;
roentgen b75cab
				break;
roentgen b75cab
			default:
roentgen b75cab
				sprintf(emsg, "Missing needed %s tag", photoTag);
roentgen b75cab
                                goto fail_return;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	switch (img->photometric) {
roentgen b75cab
		case PHOTOMETRIC_PALETTE:
roentgen b75cab
			if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
roentgen b75cab
			    &red_orig, &green_orig, &blue_orig)) {
roentgen b75cab
				sprintf(emsg, "Missing required \"Colormap\" tag");
roentgen b75cab
                                goto fail_return;
roentgen b75cab
			}
roentgen b75cab
roentgen b75cab
			/* copy the colormaps so we can modify them */
roentgen b75cab
			n_color = (1L << img->bitspersample);
roentgen b75cab
			img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
roentgen b75cab
			img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
roentgen b75cab
			img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
roentgen b75cab
			if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
roentgen b75cab
				sprintf(emsg, "Out of memory for colormap copy");
roentgen b75cab
                                goto fail_return;
roentgen b75cab
			}
roentgen b75cab
roentgen b75cab
			_TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
roentgen b75cab
			_TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
roentgen b75cab
			_TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
roentgen b75cab
roentgen b75cab
			/* fall thru... */
roentgen b75cab
		case PHOTOMETRIC_MINISWHITE:
roentgen b75cab
		case PHOTOMETRIC_MINISBLACK:
roentgen b75cab
			if (planarconfig == PLANARCONFIG_CONTIG
roentgen b75cab
			    && img->samplesperpixel != 1
roentgen b75cab
			    && img->bitspersample < 8 ) {
roentgen b75cab
				sprintf(emsg,
roentgen b75cab
				    "Sorry, can not handle contiguous data with %s=%d, "
roentgen b75cab
				    "and %s=%d and Bits/Sample=%d",
roentgen b75cab
				    photoTag, img->photometric,
roentgen b75cab
				    "Samples/pixel", img->samplesperpixel,
roentgen b75cab
				    img->bitspersample);
roentgen b75cab
                                goto fail_return;
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_YCBCR:
roentgen b75cab
			/* It would probably be nice to have a reality check here. */
roentgen b75cab
			if (planarconfig == PLANARCONFIG_CONTIG)
roentgen b75cab
				/* can rely on libjpeg to convert to RGB */
roentgen b75cab
				/* XXX should restore current state on exit */
roentgen b75cab
				switch (compress) {
roentgen b75cab
					case COMPRESSION_JPEG:
roentgen b75cab
						/*
roentgen b75cab
						 * TODO: when complete tests verify complete desubsampling
roentgen b75cab
						 * and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
roentgen b75cab
						 * favor of tif_getimage.c native handling
roentgen b75cab
						 */
roentgen b75cab
						TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
roentgen b75cab
						img->photometric = PHOTOMETRIC_RGB;
roentgen b75cab
						break;
roentgen b75cab
					default:
roentgen b75cab
						/* do nothing */;
roentgen b75cab
						break;
roentgen b75cab
				}
roentgen b75cab
			/*
roentgen b75cab
			 * TODO: if at all meaningful and useful, make more complete
roentgen b75cab
			 * support check here, or better still, refactor to let supporting
roentgen b75cab
			 * code decide whether there is support and what meaningfull
roentgen b75cab
			 * error to return
roentgen b75cab
			 */
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_RGB:
roentgen b75cab
			if (colorchannels < 3) {
roentgen b75cab
				sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
roentgen b75cab
				    "Color channels", colorchannels);
roentgen b75cab
                                goto fail_return;
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_SEPARATED:
roentgen b75cab
			{
roentgen b75cab
				uint16 inkset;
roentgen b75cab
				TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
roentgen b75cab
				if (inkset != INKSET_CMYK) {
roentgen b75cab
					sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
roentgen b75cab
					    "InkSet", inkset);
roentgen b75cab
                                        goto fail_return;
roentgen b75cab
				}
roentgen b75cab
				if (img->samplesperpixel < 4) {
roentgen b75cab
					sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
roentgen b75cab
					    "Samples/pixel", img->samplesperpixel);
roentgen b75cab
                                        goto fail_return;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_LOGL:
roentgen b75cab
			if (compress != COMPRESSION_SGILOG) {
roentgen b75cab
				sprintf(emsg, "Sorry, LogL data must have %s=%d",
roentgen b75cab
				    "Compression", COMPRESSION_SGILOG);
roentgen b75cab
                                goto fail_return;
roentgen b75cab
			}
roentgen b75cab
			TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
roentgen b75cab
			img->photometric = PHOTOMETRIC_MINISBLACK;	/* little white lie */
roentgen b75cab
			img->bitspersample = 8;
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_LOGLUV:
roentgen b75cab
			if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
roentgen b75cab
				sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
roentgen b75cab
				    "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
roentgen b75cab
                                goto fail_return;
roentgen b75cab
			}
roentgen b75cab
			if (planarconfig != PLANARCONFIG_CONTIG) {
roentgen b75cab
				sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
roentgen b75cab
				    "Planarconfiguration", planarconfig);
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
roentgen b75cab
			img->photometric = PHOTOMETRIC_RGB;		/* little white lie */
roentgen b75cab
			img->bitspersample = 8;
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_CIELAB:
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			sprintf(emsg, "Sorry, can not handle image with %s=%d",
roentgen b75cab
			    photoTag, img->photometric);
roentgen b75cab
                        goto fail_return;
roentgen b75cab
	}
roentgen b75cab
	img->Map = NULL;
roentgen b75cab
	img->BWmap = NULL;
roentgen b75cab
	img->PALmap = NULL;
roentgen b75cab
	img->ycbcr = NULL;
roentgen b75cab
	img->cielab = NULL;
roentgen b75cab
	img->UaToAa = NULL;
roentgen b75cab
	img->Bitdepth16To8 = NULL;
roentgen b75cab
	TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
roentgen b75cab
	TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
roentgen b75cab
	TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
roentgen b75cab
	img->isContig =
roentgen b75cab
	    !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
roentgen b75cab
	if (img->isContig) {
roentgen b75cab
		if (!PickContigCase(img)) {
roentgen b75cab
			sprintf(emsg, "Sorry, can not handle image");
roentgen b75cab
			goto fail_return;
roentgen b75cab
		}
roentgen b75cab
	} else {
roentgen b75cab
		if (!PickSeparateCase(img)) {
roentgen b75cab
			sprintf(emsg, "Sorry, can not handle image");
roentgen b75cab
			goto fail_return;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	return 1;
roentgen b75cab
roentgen b75cab
  fail_return:
roentgen b75cab
        _TIFFfree( img->redcmap );
roentgen b75cab
        _TIFFfree( img->greencmap );
roentgen b75cab
        _TIFFfree( img->bluecmap );
roentgen b75cab
        img->redcmap = img->greencmap = img->bluecmap = NULL;
roentgen b75cab
        return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
roentgen b75cab
{
roentgen b75cab
    if (img->get == NULL) {
roentgen b75cab
		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	if (img->put.any == NULL) {
roentgen b75cab
		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
roentgen b75cab
		"No \"put\" routine setupl; probably can not handle image format");
roentgen b75cab
		return (0);
roentgen b75cab
    }
roentgen b75cab
    return (*img->get)(img, raster, w, h);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Read the specified image into an ABGR-format rastertaking in account
roentgen b75cab
 * specified orientation.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFReadRGBAImageOriented(TIFF* tif,
roentgen b75cab
			  uint32 rwidth, uint32 rheight, uint32* raster,
roentgen b75cab
			  int orientation, int stop)
roentgen b75cab
{
roentgen b75cab
    char emsg[1024] = "";
roentgen b75cab
    TIFFRGBAImage img;
roentgen b75cab
    int ok;
roentgen b75cab
roentgen b75cab
	if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
roentgen b75cab
		img.req_orientation = orientation;
roentgen b75cab
		/* XXX verify rwidth and rheight against width and height */
roentgen b75cab
		ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
roentgen b75cab
			rwidth, img.height);
roentgen b75cab
		TIFFRGBAImageEnd(&img);
roentgen b75cab
	} else {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
roentgen b75cab
		ok = 0;
roentgen b75cab
    }
roentgen b75cab
    return (ok);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Read the specified image into an ABGR-format raster. Use bottom left
roentgen b75cab
 * origin for raster by default.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFReadRGBAImage(TIFF* tif,
roentgen b75cab
		  uint32 rwidth, uint32 rheight, uint32* raster, int stop)
roentgen b75cab
{
roentgen b75cab
	return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
roentgen b75cab
					 ORIENTATION_BOTLEFT, stop);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int 
roentgen b75cab
setorientation(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
	switch (img->orientation) {
roentgen b75cab
		case ORIENTATION_TOPLEFT:
roentgen b75cab
		case ORIENTATION_LEFTTOP:
roentgen b75cab
			if (img->req_orientation == ORIENTATION_TOPRIGHT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_RIGHTTOP)
roentgen b75cab
				return FLIP_HORIZONTALLY;
roentgen b75cab
			else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_RIGHTBOT)
roentgen b75cab
				return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
roentgen b75cab
			else if (img->req_orientation == ORIENTATION_BOTLEFT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_LEFTBOT)
roentgen b75cab
				return FLIP_VERTICALLY;
roentgen b75cab
			else
roentgen b75cab
				return 0;
roentgen b75cab
		case ORIENTATION_TOPRIGHT:
roentgen b75cab
		case ORIENTATION_RIGHTTOP:
roentgen b75cab
			if (img->req_orientation == ORIENTATION_TOPLEFT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_LEFTTOP)
roentgen b75cab
				return FLIP_HORIZONTALLY;
roentgen b75cab
			else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_RIGHTBOT)
roentgen b75cab
				return FLIP_VERTICALLY;
roentgen b75cab
			else if (img->req_orientation == ORIENTATION_BOTLEFT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_LEFTBOT)
roentgen b75cab
				return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
roentgen b75cab
			else
roentgen b75cab
				return 0;
roentgen b75cab
		case ORIENTATION_BOTRIGHT:
roentgen b75cab
		case ORIENTATION_RIGHTBOT:
roentgen b75cab
			if (img->req_orientation == ORIENTATION_TOPLEFT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_LEFTTOP)
roentgen b75cab
				return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
roentgen b75cab
			else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_RIGHTTOP)
roentgen b75cab
				return FLIP_VERTICALLY;
roentgen b75cab
			else if (img->req_orientation == ORIENTATION_BOTLEFT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_LEFTBOT)
roentgen b75cab
				return FLIP_HORIZONTALLY;
roentgen b75cab
			else
roentgen b75cab
				return 0;
roentgen b75cab
		case ORIENTATION_BOTLEFT:
roentgen b75cab
		case ORIENTATION_LEFTBOT:
roentgen b75cab
			if (img->req_orientation == ORIENTATION_TOPLEFT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_LEFTTOP)
roentgen b75cab
				return FLIP_VERTICALLY;
roentgen b75cab
			else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_RIGHTTOP)
roentgen b75cab
				return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
roentgen b75cab
			else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
roentgen b75cab
			    img->req_orientation == ORIENTATION_RIGHTBOT)
roentgen b75cab
				return FLIP_HORIZONTALLY;
roentgen b75cab
			else
roentgen b75cab
				return 0;
roentgen b75cab
		default:	/* NOTREACHED */
roentgen b75cab
			return 0;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Get an tile-organized image that has
roentgen b75cab
 *	PlanarConfiguration contiguous if SamplesPerPixel > 1
roentgen b75cab
 * or
roentgen b75cab
 *	SamplesPerPixel == 1
roentgen b75cab
 */	
roentgen b75cab
static int
roentgen b75cab
gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
roentgen b75cab
{
roentgen b75cab
    TIFF* tif = img->tif;
roentgen b75cab
    tileContigRoutine put = img->put.contig;
roentgen b75cab
    uint32 col, row, y, rowstoread;
roentgen b75cab
    tmsize_t pos;
roentgen b75cab
    uint32 tw, th;
roentgen b75cab
    unsigned char* buf;
roentgen b75cab
    int32 fromskew, toskew;
roentgen b75cab
    uint32 nrow;
roentgen b75cab
    int ret = 1, flip;
roentgen b75cab
roentgen b75cab
    buf = (unsigned char*) _TIFFmalloc(TIFFTileSize(tif));
roentgen b75cab
    if (buf == 0) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
roentgen b75cab
		return (0);
roentgen b75cab
    }
roentgen b75cab
    _TIFFmemset(buf, 0, TIFFTileSize(tif));
roentgen b75cab
    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
roentgen b75cab
    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
roentgen b75cab
roentgen b75cab
    flip = setorientation(img);
roentgen b75cab
    if (flip & FLIP_VERTICALLY) {
roentgen b75cab
	    y = h - 1;
roentgen b75cab
	    toskew = -(int32)(tw + w);
roentgen b75cab
    }
roentgen b75cab
    else {
roentgen b75cab
	    y = 0;
roentgen b75cab
	    toskew = -(int32)(tw - w);
roentgen b75cab
    }
roentgen b75cab
     
roentgen b75cab
    for (row = 0; row < h; row += nrow)
roentgen b75cab
    {
roentgen b75cab
        rowstoread = th - (row + img->row_offset) % th;
roentgen b75cab
    	nrow = (row + rowstoread > h ? h - row : rowstoread);
roentgen b75cab
	for (col = 0; col < w; col += tw) 
roentgen b75cab
        {
roentgen b75cab
	    if (TIFFReadTile(tif, buf, col+img->col_offset,  
roentgen b75cab
			     row+img->row_offset, 0, 0)==(tmsize_t)(-1) && img->stoponerr)
roentgen b75cab
            {
roentgen b75cab
                ret = 0;
roentgen b75cab
                break;
roentgen b75cab
            }
roentgen b75cab
	    
roentgen b75cab
	    pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);  
roentgen b75cab
roentgen b75cab
    	    if (col + tw > w) 
roentgen b75cab
            {
roentgen b75cab
                /*
roentgen b75cab
                 * Tile is clipped horizontally.  Calculate
roentgen b75cab
                 * visible portion and skewing factors.
roentgen b75cab
                 */
roentgen b75cab
                uint32 npix = w - col;
roentgen b75cab
                fromskew = tw - npix;
roentgen b75cab
                (*put)(img, raster+y*w+col, col, y,
roentgen b75cab
                       npix, nrow, fromskew, toskew + fromskew, buf + pos);
roentgen b75cab
            }
roentgen b75cab
            else 
roentgen b75cab
            {
roentgen b75cab
                (*put)(img, raster+y*w+col, col, y, tw, nrow, 0, toskew, buf + pos);
roentgen b75cab
            }
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
        y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
roentgen b75cab
    }
roentgen b75cab
    _TIFFfree(buf);
roentgen b75cab
roentgen b75cab
    if (flip & FLIP_HORIZONTALLY) {
roentgen b75cab
	    uint32 line;
roentgen b75cab
roentgen b75cab
	    for (line = 0; line < h; line++) {
roentgen b75cab
		    uint32 *left = raster + (line * w);
roentgen b75cab
		    uint32 *right = left + w - 1;
roentgen b75cab
		    
roentgen b75cab
		    while ( left < right ) {
roentgen b75cab
			    uint32 temp = *left;
roentgen b75cab
			    *left = *right;
roentgen b75cab
			    *right = temp;
roentgen b75cab
			    left++, right--;
roentgen b75cab
		    }
roentgen b75cab
	    }
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    return (ret);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Get an tile-organized image that has
roentgen b75cab
 *	 SamplesPerPixel > 1
roentgen b75cab
 *	 PlanarConfiguration separated
roentgen b75cab
 * We assume that all such images are RGB.
roentgen b75cab
 */	
roentgen b75cab
static int
roentgen b75cab
gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
roentgen b75cab
{
roentgen b75cab
	TIFF* tif = img->tif;
roentgen b75cab
	tileSeparateRoutine put = img->put.separate;
roentgen b75cab
	uint32 col, row, y, rowstoread;
roentgen b75cab
	tmsize_t pos;
roentgen b75cab
	uint32 tw, th;
roentgen b75cab
	unsigned char* buf;
roentgen b75cab
	unsigned char* p0;
roentgen b75cab
	unsigned char* p1;
roentgen b75cab
	unsigned char* p2;
roentgen b75cab
	unsigned char* pa;
roentgen b75cab
	tmsize_t tilesize;
roentgen b75cab
	tmsize_t bufsize;
roentgen b75cab
	int32 fromskew, toskew;
roentgen b75cab
	int alpha = img->alpha;
roentgen b75cab
	uint32 nrow;
roentgen b75cab
	int ret = 1, flip;
roentgen b75cab
        int colorchannels;
roentgen b75cab
roentgen b75cab
	tilesize = TIFFTileSize(tif);  
roentgen b75cab
	bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,tilesize);
roentgen b75cab
	if (bufsize == 0) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtTileSeparate");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	buf = (unsigned char*) _TIFFmalloc(bufsize);
roentgen b75cab
	if (buf == 0) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	_TIFFmemset(buf, 0, bufsize);
roentgen b75cab
	p0 = buf;
roentgen b75cab
	p1 = p0 + tilesize;
roentgen b75cab
	p2 = p1 + tilesize;
roentgen b75cab
	pa = (alpha?(p2+tilesize):NULL);
roentgen b75cab
	TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
roentgen b75cab
	TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
roentgen b75cab
roentgen b75cab
	flip = setorientation(img);
roentgen b75cab
	if (flip & FLIP_VERTICALLY) {
roentgen b75cab
		y = h - 1;
roentgen b75cab
		toskew = -(int32)(tw + w);
roentgen b75cab
	}
roentgen b75cab
	else {
roentgen b75cab
		y = 0;
roentgen b75cab
		toskew = -(int32)(tw - w);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
        switch( img->photometric )
roentgen b75cab
        {
roentgen b75cab
          case PHOTOMETRIC_MINISWHITE:
roentgen b75cab
          case PHOTOMETRIC_MINISBLACK:
roentgen b75cab
          case PHOTOMETRIC_PALETTE:
roentgen b75cab
            colorchannels = 1;
roentgen b75cab
            p2 = p1 = p0;
roentgen b75cab
            break;
roentgen b75cab
roentgen b75cab
          default:
roentgen b75cab
            colorchannels = 3;
roentgen b75cab
            break;
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
	for (row = 0; row < h; row += nrow)
roentgen b75cab
	{
roentgen b75cab
		rowstoread = th - (row + img->row_offset) % th;
roentgen b75cab
		nrow = (row + rowstoread > h ? h - row : rowstoread);
roentgen b75cab
		for (col = 0; col < w; col += tw)
roentgen b75cab
		{
roentgen b75cab
			if (TIFFReadTile(tif, p0, col+img->col_offset,  
roentgen b75cab
			    row+img->row_offset,0,0)==(tmsize_t)(-1) && img->stoponerr)
roentgen b75cab
			{
roentgen b75cab
				ret = 0;
roentgen b75cab
				break;
roentgen b75cab
			}
roentgen b75cab
			if (colorchannels > 1 
roentgen b75cab
                            && TIFFReadTile(tif, p1, col+img->col_offset,  
roentgen b75cab
                                            row+img->row_offset,0,1) == (tmsize_t)(-1) 
roentgen b75cab
                            && img->stoponerr)
roentgen b75cab
			{
roentgen b75cab
				ret = 0;
roentgen b75cab
				break;
roentgen b75cab
			}
roentgen b75cab
			if (colorchannels > 1 
roentgen b75cab
                            && TIFFReadTile(tif, p2, col+img->col_offset,  
roentgen b75cab
                                            row+img->row_offset,0,2) == (tmsize_t)(-1) 
roentgen b75cab
                            && img->stoponerr)
roentgen b75cab
			{
roentgen b75cab
				ret = 0;
roentgen b75cab
				break;
roentgen b75cab
			}
roentgen b75cab
			if (alpha
roentgen b75cab
                            && TIFFReadTile(tif,pa,col+img->col_offset,  
roentgen b75cab
                                            row+img->row_offset,0,colorchannels) == (tmsize_t)(-1) 
roentgen b75cab
                            && img->stoponerr)
roentgen b75cab
                        {
roentgen b75cab
                            ret = 0;
roentgen b75cab
                            break;
roentgen b75cab
			}
roentgen b75cab
roentgen b75cab
			pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);  
roentgen b75cab
roentgen b75cab
			if (col + tw > w)
roentgen b75cab
			{
roentgen b75cab
				/*
roentgen b75cab
				 * Tile is clipped horizontally.  Calculate
roentgen b75cab
				 * visible portion and skewing factors.
roentgen b75cab
				 */
roentgen b75cab
				uint32 npix = w - col;
roentgen b75cab
				fromskew = tw - npix;
roentgen b75cab
				(*put)(img, raster+y*w+col, col, y,
roentgen b75cab
				    npix, nrow, fromskew, toskew + fromskew,
roentgen b75cab
				    p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
roentgen b75cab
			} else {
roentgen b75cab
				(*put)(img, raster+y*w+col, col, y,
roentgen b75cab
				    tw, nrow, 0, toskew, p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
roentgen b75cab
		y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (flip & FLIP_HORIZONTALLY) {
roentgen b75cab
		uint32 line;
roentgen b75cab
roentgen b75cab
		for (line = 0; line < h; line++) {
roentgen b75cab
			uint32 *left = raster + (line * w);
roentgen b75cab
			uint32 *right = left + w - 1;
roentgen b75cab
roentgen b75cab
			while ( left < right ) {
roentgen b75cab
				uint32 temp = *left;
roentgen b75cab
				*left = *right;
roentgen b75cab
				*right = temp;
roentgen b75cab
				left++, right--;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	_TIFFfree(buf);
roentgen b75cab
	return (ret);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Get a strip-organized image that has
roentgen b75cab
 *	PlanarConfiguration contiguous if SamplesPerPixel > 1
roentgen b75cab
 * or
roentgen b75cab
 *	SamplesPerPixel == 1
roentgen b75cab
 */	
roentgen b75cab
static int
roentgen b75cab
gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
roentgen b75cab
{
roentgen b75cab
	TIFF* tif = img->tif;
roentgen b75cab
	tileContigRoutine put = img->put.contig;
roentgen b75cab
	uint32 row, y, nrow, nrowsub, rowstoread;
roentgen b75cab
	tmsize_t pos;
roentgen b75cab
	unsigned char* buf;
roentgen b75cab
	uint32 rowsperstrip;
roentgen b75cab
	uint16 subsamplinghor,subsamplingver;
roentgen b75cab
	uint32 imagewidth = img->width;
roentgen b75cab
	tmsize_t scanline;
roentgen b75cab
	int32 fromskew, toskew;
roentgen b75cab
	int ret = 1, flip;
roentgen b75cab
roentgen b75cab
	buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
roentgen b75cab
	if (buf == 0) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	_TIFFmemset(buf, 0, TIFFStripSize(tif));
roentgen b75cab
roentgen b75cab
	flip = setorientation(img);
roentgen b75cab
	if (flip & FLIP_VERTICALLY) {
roentgen b75cab
		y = h - 1;
roentgen b75cab
		toskew = -(int32)(w + w);
roentgen b75cab
	} else {
roentgen b75cab
		y = 0;
roentgen b75cab
		toskew = -(int32)(w - w);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
roentgen b75cab
	TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
roentgen b75cab
	scanline = TIFFScanlineSize(tif);
roentgen b75cab
	fromskew = (w < imagewidth ? imagewidth - w : 0);
roentgen b75cab
	for (row = 0; row < h; row += nrow)
roentgen b75cab
	{
roentgen b75cab
		rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
roentgen b75cab
		nrow = (row + rowstoread > h ? h - row : rowstoread);
roentgen b75cab
		nrowsub = nrow;
roentgen b75cab
		if ((nrowsub%subsamplingver)!=0)
roentgen b75cab
			nrowsub+=subsamplingver-nrowsub%subsamplingver;
roentgen b75cab
		if (TIFFReadEncodedStrip(tif,
roentgen b75cab
		    TIFFComputeStrip(tif,row+img->row_offset, 0),
roentgen b75cab
		    buf,
roentgen b75cab
		    ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline)==(tmsize_t)(-1)
roentgen b75cab
		    && img->stoponerr)
roentgen b75cab
		{
roentgen b75cab
			ret = 0;
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
roentgen b75cab
		pos = ((row + img->row_offset) % rowsperstrip) * scanline;
roentgen b75cab
		(*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
roentgen b75cab
		y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (flip & FLIP_HORIZONTALLY) {
roentgen b75cab
		uint32 line;
roentgen b75cab
roentgen b75cab
		for (line = 0; line < h; line++) {
roentgen b75cab
			uint32 *left = raster + (line * w);
roentgen b75cab
			uint32 *right = left + w - 1;
roentgen b75cab
roentgen b75cab
			while ( left < right ) {
roentgen b75cab
				uint32 temp = *left;
roentgen b75cab
				*left = *right;
roentgen b75cab
				*right = temp;
roentgen b75cab
				left++, right--;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	_TIFFfree(buf);
roentgen b75cab
	return (ret);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Get a strip-organized image with
roentgen b75cab
 *	 SamplesPerPixel > 1
roentgen b75cab
 *	 PlanarConfiguration separated
roentgen b75cab
 * We assume that all such images are RGB.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
roentgen b75cab
{
roentgen b75cab
	TIFF* tif = img->tif;
roentgen b75cab
	tileSeparateRoutine put = img->put.separate;
roentgen b75cab
	unsigned char *buf;
roentgen b75cab
	unsigned char *p0, *p1, *p2, *pa;
roentgen b75cab
	uint32 row, y, nrow, rowstoread;
roentgen b75cab
	tmsize_t pos;
roentgen b75cab
	tmsize_t scanline;
roentgen b75cab
	uint32 rowsperstrip, offset_row;
roentgen b75cab
	uint32 imagewidth = img->width;
roentgen b75cab
	tmsize_t stripsize;
roentgen b75cab
	tmsize_t bufsize;
roentgen b75cab
	int32 fromskew, toskew;
roentgen b75cab
	int alpha = img->alpha;
roentgen b75cab
	int ret = 1, flip, colorchannels;
roentgen b75cab
roentgen b75cab
	stripsize = TIFFStripSize(tif);  
roentgen b75cab
	bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,stripsize);
roentgen b75cab
	if (bufsize == 0) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtStripSeparate");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	p0 = buf = (unsigned char *)_TIFFmalloc(bufsize);
roentgen b75cab
	if (buf == 0) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	_TIFFmemset(buf, 0, bufsize);
roentgen b75cab
	p1 = p0 + stripsize;
roentgen b75cab
	p2 = p1 + stripsize;
roentgen b75cab
	pa = (alpha?(p2+stripsize):NULL);
roentgen b75cab
roentgen b75cab
	flip = setorientation(img);
roentgen b75cab
	if (flip & FLIP_VERTICALLY) {
roentgen b75cab
		y = h - 1;
roentgen b75cab
		toskew = -(int32)(w + w);
roentgen b75cab
	}
roentgen b75cab
	else {
roentgen b75cab
		y = 0;
roentgen b75cab
		toskew = -(int32)(w - w);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
        switch( img->photometric )
roentgen b75cab
        {
roentgen b75cab
          case PHOTOMETRIC_MINISWHITE:
roentgen b75cab
          case PHOTOMETRIC_MINISBLACK:
roentgen b75cab
          case PHOTOMETRIC_PALETTE:
roentgen b75cab
            colorchannels = 1;
roentgen b75cab
            p2 = p1 = p0;
roentgen b75cab
            break;
roentgen b75cab
roentgen b75cab
          default:
roentgen b75cab
            colorchannels = 3;
roentgen b75cab
            break;
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
	TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
roentgen b75cab
	scanline = TIFFScanlineSize(tif);  
roentgen b75cab
	fromskew = (w < imagewidth ? imagewidth - w : 0);
roentgen b75cab
	for (row = 0; row < h; row += nrow)
roentgen b75cab
	{
roentgen b75cab
		rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
roentgen b75cab
		nrow = (row + rowstoread > h ? h - row : rowstoread);
roentgen b75cab
		offset_row = row + img->row_offset;
roentgen b75cab
		if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
roentgen b75cab
		    p0, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
roentgen b75cab
		    && img->stoponerr)
roentgen b75cab
		{
roentgen b75cab
			ret = 0;
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
		if (colorchannels > 1 
roentgen b75cab
                    && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
roentgen b75cab
                                            p1, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
roentgen b75cab
		    && img->stoponerr)
roentgen b75cab
		{
roentgen b75cab
			ret = 0;
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
		if (colorchannels > 1 
roentgen b75cab
                    && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
roentgen b75cab
                                            p2, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
roentgen b75cab
		    && img->stoponerr)
roentgen b75cab
		{
roentgen b75cab
			ret = 0;
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
		if (alpha)
roentgen b75cab
		{
roentgen b75cab
			if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, colorchannels),
roentgen b75cab
			    pa, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
roentgen b75cab
			    && img->stoponerr)
roentgen b75cab
			{
roentgen b75cab
				ret = 0;
roentgen b75cab
				break;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
roentgen b75cab
		pos = ((row + img->row_offset) % rowsperstrip) * scanline;
roentgen b75cab
		(*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, p0 + pos, p1 + pos,
roentgen b75cab
		    p2 + pos, (alpha?(pa+pos):NULL));
roentgen b75cab
		y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (flip & FLIP_HORIZONTALLY) {
roentgen b75cab
		uint32 line;
roentgen b75cab
roentgen b75cab
		for (line = 0; line < h; line++) {
roentgen b75cab
			uint32 *left = raster + (line * w);
roentgen b75cab
			uint32 *right = left + w - 1;
roentgen b75cab
roentgen b75cab
			while ( left < right ) {
roentgen b75cab
				uint32 temp = *left;
roentgen b75cab
				*left = *right;
roentgen b75cab
				*right = temp;
roentgen b75cab
				left++, right--;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	_TIFFfree(buf);
roentgen b75cab
	return (ret);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * The following routines move decoded data returned
roentgen b75cab
 * from the TIFF library into rasters filled with packed
roentgen b75cab
 * ABGR pixels (i.e. suitable for passing to lrecwrite.)
roentgen b75cab
 *
roentgen b75cab
 * The routines have been created according to the most
roentgen b75cab
 * important cases and optimized.  PickContigCase and
roentgen b75cab
 * PickSeparateCase analyze the parameters and select
roentgen b75cab
 * the appropriate "get" and "put" routine to use.
roentgen b75cab
 */
roentgen b75cab
#define	REPEAT8(op)	REPEAT4(op); REPEAT4(op)
roentgen b75cab
#define	REPEAT4(op)	REPEAT2(op); REPEAT2(op)
roentgen b75cab
#define	REPEAT2(op)	op; op
roentgen b75cab
#define	CASE8(x,op)			\
roentgen b75cab
    switch (x) {			\
roentgen b75cab
    case 7: op; case 6: op; case 5: op;	\
roentgen b75cab
    case 4: op; case 3: op; case 2: op;	\
roentgen b75cab
    case 1: op;				\
roentgen b75cab
    }
roentgen b75cab
#define	CASE4(x,op)	switch (x) { case 3: op; case 2: op; case 1: op; }
roentgen b75cab
#define	NOP
roentgen b75cab
roentgen b75cab
#define	UNROLL8(w, op1, op2) {		\
roentgen b75cab
    uint32 _x;				\
roentgen b75cab
    for (_x = w; _x >= 8; _x -= 8) {	\
roentgen b75cab
	op1;				\
roentgen b75cab
	REPEAT8(op2);			\
roentgen b75cab
    }					\
roentgen b75cab
    if (_x > 0) {			\
roentgen b75cab
	op1;				\
roentgen b75cab
	CASE8(_x,op2);			\
roentgen b75cab
    }					\
roentgen b75cab
}
roentgen b75cab
#define	UNROLL4(w, op1, op2) {		\
roentgen b75cab
    uint32 _x;				\
roentgen b75cab
    for (_x = w; _x >= 4; _x -= 4) {	\
roentgen b75cab
	op1;				\
roentgen b75cab
	REPEAT4(op2);			\
roentgen b75cab
    }					\
roentgen b75cab
    if (_x > 0) {			\
roentgen b75cab
	op1;				\
roentgen b75cab
	CASE4(_x,op2);			\
roentgen b75cab
    }					\
roentgen b75cab
}
roentgen b75cab
#define	UNROLL2(w, op1, op2) {		\
roentgen b75cab
    uint32 _x;				\
roentgen b75cab
    for (_x = w; _x >= 2; _x -= 2) {	\
roentgen b75cab
	op1;				\
roentgen b75cab
	REPEAT2(op2);			\
roentgen b75cab
    }					\
roentgen b75cab
    if (_x) {				\
roentgen b75cab
	op1;				\
roentgen b75cab
	op2;				\
roentgen b75cab
    }					\
roentgen b75cab
}
roentgen b75cab
    
roentgen b75cab
#define	SKEW(r,g,b,skew)	{ r += skew; g += skew; b += skew; }
roentgen b75cab
#define	SKEW4(r,g,b,a,skew)	{ r += skew; g += skew; b += skew; a+= skew; }
roentgen b75cab
roentgen b75cab
#define A1 (((uint32)0xffL)<<24)
roentgen b75cab
#define	PACK(r,g,b)	\
roentgen b75cab
	((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
roentgen b75cab
#define	PACK4(r,g,b,a)	\
roentgen b75cab
	((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
roentgen b75cab
#define W2B(v) (((v)>>8)&0xff)
roentgen b75cab
/* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
roentgen b75cab
#define	PACKW(r,g,b)	\
roentgen b75cab
	((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
roentgen b75cab
#define	PACKW4(r,g,b,a)	\
roentgen b75cab
	((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
roentgen b75cab
roentgen b75cab
#define	DECLAREContigPutFunc(name) \
roentgen b75cab
static void name(\
roentgen b75cab
    TIFFRGBAImage* img, \
roentgen b75cab
    uint32* cp, \
roentgen b75cab
    uint32 x, uint32 y, \
roentgen b75cab
    uint32 w, uint32 h, \
roentgen b75cab
    int32 fromskew, int32 toskew, \
roentgen b75cab
    unsigned char* pp \
roentgen b75cab
)
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit palette => colormap/RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(put8bitcmaptile)
roentgen b75cab
{
roentgen b75cab
    uint32** PALmap = img->PALmap;
roentgen b75cab
    int samplesperpixel = img->samplesperpixel;
roentgen b75cab
roentgen b75cab
    (void) y;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	for (x = w; x-- > 0;)
roentgen b75cab
        {
roentgen b75cab
	    *cp++ = PALmap[*pp][0];
roentgen b75cab
            pp += samplesperpixel;
roentgen b75cab
        }
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 4-bit palette => colormap/RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(put4bitcmaptile)
roentgen b75cab
{
roentgen b75cab
    uint32** PALmap = img->PALmap;
roentgen b75cab
roentgen b75cab
    (void) x; (void) y;
roentgen b75cab
    fromskew /= 2;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	uint32* bw;
roentgen b75cab
	UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 2-bit palette => colormap/RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(put2bitcmaptile)
roentgen b75cab
{
roentgen b75cab
    uint32** PALmap = img->PALmap;
roentgen b75cab
roentgen b75cab
    (void) x; (void) y;
roentgen b75cab
    fromskew /= 4;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	uint32* bw;
roentgen b75cab
	UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 1-bit palette => colormap/RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(put1bitcmaptile)
roentgen b75cab
{
roentgen b75cab
    uint32** PALmap = img->PALmap;
roentgen b75cab
roentgen b75cab
    (void) x; (void) y;
roentgen b75cab
    fromskew /= 8;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	uint32* bw;
roentgen b75cab
	UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit greyscale => colormap/RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putgreytile)
roentgen b75cab
{
roentgen b75cab
    int samplesperpixel = img->samplesperpixel;
roentgen b75cab
    uint32** BWmap = img->BWmap;
roentgen b75cab
roentgen b75cab
    (void) y;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	for (x = w; x-- > 0;)
roentgen b75cab
        {
roentgen b75cab
	    *cp++ = BWmap[*pp][0];
roentgen b75cab
            pp += samplesperpixel;
roentgen b75cab
        }
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit greyscale with associated alpha => colormap/RGBA
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putagreytile)
roentgen b75cab
{
roentgen b75cab
    int samplesperpixel = img->samplesperpixel;
roentgen b75cab
    uint32** BWmap = img->BWmap;
roentgen b75cab
roentgen b75cab
    (void) y;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	for (x = w; x-- > 0;)
roentgen b75cab
        {
roentgen b75cab
            *cp++ = BWmap[*pp][0] & (*(pp+1) << 24 | ~A1);
roentgen b75cab
            pp += samplesperpixel;
roentgen b75cab
        }
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 16-bit greyscale => colormap/RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(put16bitbwtile)
roentgen b75cab
{
roentgen b75cab
    int samplesperpixel = img->samplesperpixel;
roentgen b75cab
    uint32** BWmap = img->BWmap;
roentgen b75cab
roentgen b75cab
    (void) y;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
        uint16 *wp = (uint16 *) pp;
roentgen b75cab
roentgen b75cab
	for (x = w; x-- > 0;)
roentgen b75cab
        {
roentgen b75cab
            /* use high order byte of 16bit value */
roentgen b75cab
roentgen b75cab
	    *cp++ = BWmap[*wp >> 8][0];
roentgen b75cab
            pp += 2 * samplesperpixel;
roentgen b75cab
            wp += samplesperpixel;
roentgen b75cab
        }
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 1-bit bilevel => colormap/RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(put1bitbwtile)
roentgen b75cab
{
roentgen b75cab
    uint32** BWmap = img->BWmap;
roentgen b75cab
roentgen b75cab
    (void) x; (void) y;
roentgen b75cab
    fromskew /= 8;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	uint32* bw;
roentgen b75cab
	UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 2-bit greyscale => colormap/RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(put2bitbwtile)
roentgen b75cab
{
roentgen b75cab
    uint32** BWmap = img->BWmap;
roentgen b75cab
roentgen b75cab
    (void) x; (void) y;
roentgen b75cab
    fromskew /= 4;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	uint32* bw;
roentgen b75cab
	UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 4-bit greyscale => colormap/RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(put4bitbwtile)
roentgen b75cab
{
roentgen b75cab
    uint32** BWmap = img->BWmap;
roentgen b75cab
roentgen b75cab
    (void) x; (void) y;
roentgen b75cab
    fromskew /= 2;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	uint32* bw;
roentgen b75cab
	UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed samples, no Map => RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putRGBcontig8bittile)
roentgen b75cab
{
roentgen b75cab
    int samplesperpixel = img->samplesperpixel;
roentgen b75cab
roentgen b75cab
    (void) x; (void) y;
roentgen b75cab
    fromskew *= samplesperpixel;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	UNROLL8(w, NOP,
roentgen b75cab
	    *cp++ = PACK(pp[0], pp[1], pp[2]);
roentgen b75cab
	    pp += samplesperpixel);
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed samples => RGBA w/ associated alpha
roentgen b75cab
 * (known to have Map == NULL)
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putRGBAAcontig8bittile)
roentgen b75cab
{
roentgen b75cab
    int samplesperpixel = img->samplesperpixel;
roentgen b75cab
roentgen b75cab
    (void) x; (void) y;
roentgen b75cab
    fromskew *= samplesperpixel;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	UNROLL8(w, NOP,
roentgen b75cab
	    *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
roentgen b75cab
	    pp += samplesperpixel);
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed samples => RGBA w/ unassociated alpha
roentgen b75cab
 * (known to have Map == NULL)
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putRGBUAcontig8bittile)
roentgen b75cab
{
roentgen b75cab
	int samplesperpixel = img->samplesperpixel;
roentgen b75cab
	(void) y;
roentgen b75cab
	fromskew *= samplesperpixel;
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		uint32 r, g, b, a;
roentgen b75cab
		uint8* m;
roentgen b75cab
		for (x = w; x-- > 0;) {
roentgen b75cab
			a = pp[3];
roentgen b75cab
			m = img->UaToAa+(a<<8);
roentgen b75cab
			r = m[pp[0]];
roentgen b75cab
			g = m[pp[1]];
roentgen b75cab
			b = m[pp[2]];
roentgen b75cab
			*cp++ = PACK4(r,g,b,a);
roentgen b75cab
			pp += samplesperpixel;
roentgen b75cab
		}
roentgen b75cab
		cp += toskew;
roentgen b75cab
		pp += fromskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 16-bit packed samples => RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putRGBcontig16bittile)
roentgen b75cab
{
roentgen b75cab
	int samplesperpixel = img->samplesperpixel;
roentgen b75cab
	uint16 *wp = (uint16 *)pp;
roentgen b75cab
	(void) y;
roentgen b75cab
	fromskew *= samplesperpixel;
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		for (x = w; x-- > 0;) {
roentgen b75cab
			*cp++ = PACK(img->Bitdepth16To8[wp[0]],
roentgen b75cab
			    img->Bitdepth16To8[wp[1]],
roentgen b75cab
			    img->Bitdepth16To8[wp[2]]);
roentgen b75cab
			wp += samplesperpixel;
roentgen b75cab
		}
roentgen b75cab
		cp += toskew;
roentgen b75cab
		wp += fromskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 16-bit packed samples => RGBA w/ associated alpha
roentgen b75cab
 * (known to have Map == NULL)
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putRGBAAcontig16bittile)
roentgen b75cab
{
roentgen b75cab
	int samplesperpixel = img->samplesperpixel;
roentgen b75cab
	uint16 *wp = (uint16 *)pp;
roentgen b75cab
	(void) y;
roentgen b75cab
	fromskew *= samplesperpixel;
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		for (x = w; x-- > 0;) {
roentgen b75cab
			*cp++ = PACK4(img->Bitdepth16To8[wp[0]],
roentgen b75cab
			    img->Bitdepth16To8[wp[1]],
roentgen b75cab
			    img->Bitdepth16To8[wp[2]],
roentgen b75cab
			    img->Bitdepth16To8[wp[3]]);
roentgen b75cab
			wp += samplesperpixel;
roentgen b75cab
		}
roentgen b75cab
		cp += toskew;
roentgen b75cab
		wp += fromskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 16-bit packed samples => RGBA w/ unassociated alpha
roentgen b75cab
 * (known to have Map == NULL)
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putRGBUAcontig16bittile)
roentgen b75cab
{
roentgen b75cab
	int samplesperpixel = img->samplesperpixel;
roentgen b75cab
	uint16 *wp = (uint16 *)pp;
roentgen b75cab
	(void) y;
roentgen b75cab
	fromskew *= samplesperpixel;
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		uint32 r,g,b,a;
roentgen b75cab
		uint8* m;
roentgen b75cab
		for (x = w; x-- > 0;) {
roentgen b75cab
			a = img->Bitdepth16To8[wp[3]];
roentgen b75cab
			m = img->UaToAa+(a<<8);
roentgen b75cab
			r = m[img->Bitdepth16To8[wp[0]]];
roentgen b75cab
			g = m[img->Bitdepth16To8[wp[1]]];
roentgen b75cab
			b = m[img->Bitdepth16To8[wp[2]]];
roentgen b75cab
			*cp++ = PACK4(r,g,b,a);
roentgen b75cab
			wp += samplesperpixel;
roentgen b75cab
		}
roentgen b75cab
		cp += toskew;
roentgen b75cab
		wp += fromskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed CMYK samples w/o Map => RGB
roentgen b75cab
 *
roentgen b75cab
 * NB: The conversion of CMYK->RGB is *very* crude.
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
roentgen b75cab
{
roentgen b75cab
    int samplesperpixel = img->samplesperpixel;
roentgen b75cab
    uint16 r, g, b, k;
roentgen b75cab
roentgen b75cab
    (void) x; (void) y;
roentgen b75cab
    fromskew *= samplesperpixel;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	UNROLL8(w, NOP,
roentgen b75cab
	    k = 255 - pp[3];
roentgen b75cab
	    r = (k*(255-pp[0]))/255;
roentgen b75cab
	    g = (k*(255-pp[1]))/255;
roentgen b75cab
	    b = (k*(255-pp[2]))/255;
roentgen b75cab
	    *cp++ = PACK(r, g, b);
roentgen b75cab
	    pp += samplesperpixel);
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed CMYK samples w/Map => RGB
roentgen b75cab
 *
roentgen b75cab
 * NB: The conversion of CMYK->RGB is *very* crude.
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
roentgen b75cab
{
roentgen b75cab
    int samplesperpixel = img->samplesperpixel;
roentgen b75cab
    TIFFRGBValue* Map = img->Map;
roentgen b75cab
    uint16 r, g, b, k;
roentgen b75cab
roentgen b75cab
    (void) y;
roentgen b75cab
    fromskew *= samplesperpixel;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	for (x = w; x-- > 0;) {
roentgen b75cab
	    k = 255 - pp[3];
roentgen b75cab
	    r = (k*(255-pp[0]))/255;
roentgen b75cab
	    g = (k*(255-pp[1]))/255;
roentgen b75cab
	    b = (k*(255-pp[2]))/255;
roentgen b75cab
	    *cp++ = PACK(Map[r], Map[g], Map[b]);
roentgen b75cab
	    pp += samplesperpixel;
roentgen b75cab
	}
roentgen b75cab
	pp += fromskew;
roentgen b75cab
	cp += toskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#define	DECLARESepPutFunc(name) \
roentgen b75cab
static void name(\
roentgen b75cab
    TIFFRGBAImage* img,\
roentgen b75cab
    uint32* cp,\
roentgen b75cab
    uint32 x, uint32 y, \
roentgen b75cab
    uint32 w, uint32 h,\
roentgen b75cab
    int32 fromskew, int32 toskew,\
roentgen b75cab
    unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a\
roentgen b75cab
)
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit unpacked samples => RGB
roentgen b75cab
 */
roentgen b75cab
DECLARESepPutFunc(putRGBseparate8bittile)
roentgen b75cab
{
roentgen b75cab
    (void) img; (void) x; (void) y; (void) a;
roentgen b75cab
    while (h-- > 0) {
roentgen b75cab
	UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
roentgen b75cab
	SKEW(r, g, b, fromskew);
roentgen b75cab
	cp += toskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit unpacked samples => RGBA w/ associated alpha
roentgen b75cab
 */
roentgen b75cab
DECLARESepPutFunc(putRGBAAseparate8bittile)
roentgen b75cab
{
roentgen b75cab
	(void) img; (void) x; (void) y; 
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
roentgen b75cab
		SKEW4(r, g, b, a, fromskew);
roentgen b75cab
		cp += toskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit unpacked CMYK samples => RGBA
roentgen b75cab
 */
roentgen b75cab
DECLARESepPutFunc(putCMYKseparate8bittile)
roentgen b75cab
{
roentgen b75cab
	(void) img; (void) y;
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		uint32 rv, gv, bv, kv;
roentgen b75cab
		for (x = w; x-- > 0;) {
roentgen b75cab
			kv = 255 - *a++;
roentgen b75cab
			rv = (kv*(255-*r++))/255;
roentgen b75cab
			gv = (kv*(255-*g++))/255;
roentgen b75cab
			bv = (kv*(255-*b++))/255;
roentgen b75cab
			*cp++ = PACK4(rv,gv,bv,255);
roentgen b75cab
		}
roentgen b75cab
		SKEW4(r, g, b, a, fromskew);
roentgen b75cab
		cp += toskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit unpacked samples => RGBA w/ unassociated alpha
roentgen b75cab
 */
roentgen b75cab
DECLARESepPutFunc(putRGBUAseparate8bittile)
roentgen b75cab
{
roentgen b75cab
	(void) img; (void) y;
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		uint32 rv, gv, bv, av;
roentgen b75cab
		uint8* m;
roentgen b75cab
		for (x = w; x-- > 0;) {
roentgen b75cab
			av = *a++;
roentgen b75cab
			m = img->UaToAa+(av<<8);
roentgen b75cab
			rv = m[*r++];
roentgen b75cab
			gv = m[*g++];
roentgen b75cab
			bv = m[*b++];
roentgen b75cab
			*cp++ = PACK4(rv,gv,bv,av);
roentgen b75cab
		}
roentgen b75cab
		SKEW4(r, g, b, a, fromskew);
roentgen b75cab
		cp += toskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 16-bit unpacked samples => RGB
roentgen b75cab
 */
roentgen b75cab
DECLARESepPutFunc(putRGBseparate16bittile)
roentgen b75cab
{
roentgen b75cab
	uint16 *wr = (uint16*) r;
roentgen b75cab
	uint16 *wg = (uint16*) g;
roentgen b75cab
	uint16 *wb = (uint16*) b;
roentgen b75cab
	(void) img; (void) y; (void) a;
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		for (x = 0; x < w; x++)
roentgen b75cab
			*cp++ = PACK(img->Bitdepth16To8[*wr++],
roentgen b75cab
			    img->Bitdepth16To8[*wg++],
roentgen b75cab
			    img->Bitdepth16To8[*wb++]);
roentgen b75cab
		SKEW(wr, wg, wb, fromskew);
roentgen b75cab
		cp += toskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 16-bit unpacked samples => RGBA w/ associated alpha
roentgen b75cab
 */
roentgen b75cab
DECLARESepPutFunc(putRGBAAseparate16bittile)
roentgen b75cab
{
roentgen b75cab
	uint16 *wr = (uint16*) r;
roentgen b75cab
	uint16 *wg = (uint16*) g;
roentgen b75cab
	uint16 *wb = (uint16*) b;
roentgen b75cab
	uint16 *wa = (uint16*) a;
roentgen b75cab
	(void) img; (void) y;
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		for (x = 0; x < w; x++)
roentgen b75cab
			*cp++ = PACK4(img->Bitdepth16To8[*wr++],
roentgen b75cab
			    img->Bitdepth16To8[*wg++],
roentgen b75cab
			    img->Bitdepth16To8[*wb++],
roentgen b75cab
			    img->Bitdepth16To8[*wa++]);
roentgen b75cab
		SKEW4(wr, wg, wb, wa, fromskew);
roentgen b75cab
		cp += toskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 16-bit unpacked samples => RGBA w/ unassociated alpha
roentgen b75cab
 */
roentgen b75cab
DECLARESepPutFunc(putRGBUAseparate16bittile)
roentgen b75cab
{
roentgen b75cab
	uint16 *wr = (uint16*) r;
roentgen b75cab
	uint16 *wg = (uint16*) g;
roentgen b75cab
	uint16 *wb = (uint16*) b;
roentgen b75cab
	uint16 *wa = (uint16*) a;
roentgen b75cab
	(void) img; (void) y;
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		uint32 r,g,b,a;
roentgen b75cab
		uint8* m;
roentgen b75cab
		for (x = w; x-- > 0;) {
roentgen b75cab
			a = img->Bitdepth16To8[*wa++];
roentgen b75cab
			m = img->UaToAa+(a<<8);
roentgen b75cab
			r = m[img->Bitdepth16To8[*wr++]];
roentgen b75cab
			g = m[img->Bitdepth16To8[*wg++]];
roentgen b75cab
			b = m[img->Bitdepth16To8[*wb++]];
roentgen b75cab
			*cp++ = PACK4(r,g,b,a);
roentgen b75cab
		}
roentgen b75cab
		SKEW4(wr, wg, wb, wa, fromskew);
roentgen b75cab
		cp += toskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed CIE L*a*b 1976 samples => RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putcontig8bitCIELab)
roentgen b75cab
{
roentgen b75cab
	float X, Y, Z;
roentgen b75cab
	uint32 r, g, b;
roentgen b75cab
	(void) y;
roentgen b75cab
	fromskew *= 3;
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		for (x = w; x-- > 0;) {
roentgen b75cab
			TIFFCIELabToXYZ(img->cielab,
roentgen b75cab
					(unsigned char)pp[0],
roentgen b75cab
					(signed char)pp[1],
roentgen b75cab
					(signed char)pp[2],
roentgen b75cab
					&X, &Y, &Z);
roentgen b75cab
			TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
roentgen b75cab
			*cp++ = PACK(r, g, b);
roentgen b75cab
			pp += 3;
roentgen b75cab
		}
roentgen b75cab
		cp += toskew;
roentgen b75cab
		pp += fromskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * YCbCr -> RGB conversion and packing routines.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#define	YCbCrtoRGB(dst, Y) {						\
roentgen b75cab
	uint32 r, g, b;							\
roentgen b75cab
	TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b);		\
roentgen b75cab
	dst = PACK(r, g, b);						\
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed YCbCr samples => RGB 
roentgen b75cab
 * This function is generic for different sampling sizes, 
roentgen b75cab
 * and can handle blocks sizes that aren't multiples of the
roentgen b75cab
 * sampling size.  However, it is substantially less optimized
roentgen b75cab
 * than the specific sampling cases.  It is used as a fallback
roentgen b75cab
 * for difficult blocks.
roentgen b75cab
 */
roentgen b75cab
#ifdef notdef
roentgen b75cab
static void putcontig8bitYCbCrGenericTile( 
roentgen b75cab
    TIFFRGBAImage* img, 
roentgen b75cab
    uint32* cp, 
roentgen b75cab
    uint32 x, uint32 y, 
roentgen b75cab
    uint32 w, uint32 h, 
roentgen b75cab
    int32 fromskew, int32 toskew, 
roentgen b75cab
    unsigned char* pp,
roentgen b75cab
    int h_group, 
roentgen b75cab
    int v_group )
roentgen b75cab
roentgen b75cab
{
roentgen b75cab
    uint32* cp1 = cp+w+toskew;
roentgen b75cab
    uint32* cp2 = cp1+w+toskew;
roentgen b75cab
    uint32* cp3 = cp2+w+toskew;
roentgen b75cab
    int32 incr = 3*w+4*toskew;
roentgen b75cab
    int32   Cb, Cr;
roentgen b75cab
    int     group_size = v_group * h_group + 2;
roentgen b75cab
roentgen b75cab
    (void) y;
roentgen b75cab
    fromskew = (fromskew * group_size) / h_group;
roentgen b75cab
roentgen b75cab
    for( yy = 0; yy < h; yy++ )
roentgen b75cab
    {
roentgen b75cab
        unsigned char *pp_line;
roentgen b75cab
        int     y_line_group = yy / v_group;
roentgen b75cab
        int     y_remainder = yy - y_line_group * v_group;
roentgen b75cab
roentgen b75cab
        pp_line = pp + v_line_group * 
roentgen b75cab
roentgen b75cab
        
roentgen b75cab
        for( xx = 0; xx < w; xx++ )
roentgen b75cab
        {
roentgen b75cab
            Cb = pp
roentgen b75cab
        }
roentgen b75cab
    }
roentgen b75cab
    for (; h >= 4; h -= 4) {
roentgen b75cab
	x = w>>2;
roentgen b75cab
	do {
roentgen b75cab
	    Cb = pp[16];
roentgen b75cab
	    Cr = pp[17];
roentgen b75cab
roentgen b75cab
	    YCbCrtoRGB(cp [0], pp[ 0]);
roentgen b75cab
	    YCbCrtoRGB(cp [1], pp[ 1]);
roentgen b75cab
	    YCbCrtoRGB(cp [2], pp[ 2]);
roentgen b75cab
	    YCbCrtoRGB(cp [3], pp[ 3]);
roentgen b75cab
	    YCbCrtoRGB(cp1[0], pp[ 4]);
roentgen b75cab
	    YCbCrtoRGB(cp1[1], pp[ 5]);
roentgen b75cab
	    YCbCrtoRGB(cp1[2], pp[ 6]);
roentgen b75cab
	    YCbCrtoRGB(cp1[3], pp[ 7]);
roentgen b75cab
	    YCbCrtoRGB(cp2[0], pp[ 8]);
roentgen b75cab
	    YCbCrtoRGB(cp2[1], pp[ 9]);
roentgen b75cab
	    YCbCrtoRGB(cp2[2], pp[10]);
roentgen b75cab
	    YCbCrtoRGB(cp2[3], pp[11]);
roentgen b75cab
	    YCbCrtoRGB(cp3[0], pp[12]);
roentgen b75cab
	    YCbCrtoRGB(cp3[1], pp[13]);
roentgen b75cab
	    YCbCrtoRGB(cp3[2], pp[14]);
roentgen b75cab
	    YCbCrtoRGB(cp3[3], pp[15]);
roentgen b75cab
roentgen b75cab
	    cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
roentgen b75cab
	    pp += 18;
roentgen b75cab
	} while (--x);
roentgen b75cab
	cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
roentgen b75cab
{
roentgen b75cab
    uint32* cp1 = cp+w+toskew;
roentgen b75cab
    uint32* cp2 = cp1+w+toskew;
roentgen b75cab
    uint32* cp3 = cp2+w+toskew;
roentgen b75cab
    int32 incr = 3*w+4*toskew;
roentgen b75cab
roentgen b75cab
    (void) y;
roentgen b75cab
    /* adjust fromskew */
roentgen b75cab
    fromskew = (fromskew * 18) / 4;
roentgen b75cab
    if ((h & 3) == 0 && (w & 3) == 0) {				        
roentgen b75cab
        for (; h >= 4; h -= 4) {
roentgen b75cab
            x = w>>2;
roentgen b75cab
            do {
roentgen b75cab
                int32 Cb = pp[16];
roentgen b75cab
                int32 Cr = pp[17];
roentgen b75cab
roentgen b75cab
                YCbCrtoRGB(cp [0], pp[ 0]);
roentgen b75cab
                YCbCrtoRGB(cp [1], pp[ 1]);
roentgen b75cab
                YCbCrtoRGB(cp [2], pp[ 2]);
roentgen b75cab
                YCbCrtoRGB(cp [3], pp[ 3]);
roentgen b75cab
                YCbCrtoRGB(cp1[0], pp[ 4]);
roentgen b75cab
                YCbCrtoRGB(cp1[1], pp[ 5]);
roentgen b75cab
                YCbCrtoRGB(cp1[2], pp[ 6]);
roentgen b75cab
                YCbCrtoRGB(cp1[3], pp[ 7]);
roentgen b75cab
                YCbCrtoRGB(cp2[0], pp[ 8]);
roentgen b75cab
                YCbCrtoRGB(cp2[1], pp[ 9]);
roentgen b75cab
                YCbCrtoRGB(cp2[2], pp[10]);
roentgen b75cab
                YCbCrtoRGB(cp2[3], pp[11]);
roentgen b75cab
                YCbCrtoRGB(cp3[0], pp[12]);
roentgen b75cab
                YCbCrtoRGB(cp3[1], pp[13]);
roentgen b75cab
                YCbCrtoRGB(cp3[2], pp[14]);
roentgen b75cab
                YCbCrtoRGB(cp3[3], pp[15]);
roentgen b75cab
roentgen b75cab
                cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
roentgen b75cab
                pp += 18;
roentgen b75cab
            } while (--x);
roentgen b75cab
            cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
roentgen b75cab
            pp += fromskew;
roentgen b75cab
        }
roentgen b75cab
    } else {
roentgen b75cab
        while (h > 0) {
roentgen b75cab
            for (x = w; x > 0;) {
roentgen b75cab
                int32 Cb = pp[16];
roentgen b75cab
                int32 Cr = pp[17];
roentgen b75cab
                switch (x) {
roentgen b75cab
                default:
roentgen b75cab
                    switch (h) {
roentgen b75cab
                    default: YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
roentgen b75cab
                    case 3:  YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
roentgen b75cab
                    case 2:  YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
roentgen b75cab
                    case 1:  YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
roentgen b75cab
                    }                                    /* FALLTHROUGH */
roentgen b75cab
                case 3:
roentgen b75cab
                    switch (h) {
roentgen b75cab
                    default: YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
roentgen b75cab
                    case 3:  YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
roentgen b75cab
                    case 2:  YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
roentgen b75cab
                    case 1:  YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
roentgen b75cab
                    }                                    /* FALLTHROUGH */
roentgen b75cab
                case 2:
roentgen b75cab
                    switch (h) {
roentgen b75cab
                    default: YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
roentgen b75cab
                    case 3:  YCbCrtoRGB(cp2[1], pp[ 9]); /* FALLTHROUGH */
roentgen b75cab
                    case 2:  YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
roentgen b75cab
                    case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
roentgen b75cab
                    }                                    /* FALLTHROUGH */
roentgen b75cab
                case 1:
roentgen b75cab
                    switch (h) {
roentgen b75cab
                    default: YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
roentgen b75cab
                    case 3:  YCbCrtoRGB(cp2[0], pp[ 8]); /* FALLTHROUGH */
roentgen b75cab
                    case 2:  YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
roentgen b75cab
                    case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
roentgen b75cab
                    }                                    /* FALLTHROUGH */
roentgen b75cab
                }
roentgen b75cab
                if (x < 4) {
roentgen b75cab
                    cp += x; cp1 += x; cp2 += x; cp3 += x;
roentgen b75cab
                    x = 0;
roentgen b75cab
                }
roentgen b75cab
                else {
roentgen b75cab
                    cp += 4; cp1 += 4; cp2 += 4; cp3 += 4;
roentgen b75cab
                    x -= 4;
roentgen b75cab
                }
roentgen b75cab
                pp += 18;
roentgen b75cab
            }
roentgen b75cab
            if (h <= 4)
roentgen b75cab
                break;
roentgen b75cab
            h -= 4;
roentgen b75cab
            cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
roentgen b75cab
            pp += fromskew;
roentgen b75cab
        }
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
roentgen b75cab
{
roentgen b75cab
    uint32* cp1 = cp+w+toskew;
roentgen b75cab
    int32 incr = 2*toskew+w;
roentgen b75cab
roentgen b75cab
    (void) y;
roentgen b75cab
    fromskew = (fromskew * 10) / 4;
roentgen b75cab
    if ((h & 3) == 0 && (w & 1) == 0) {
roentgen b75cab
        for (; h >= 2; h -= 2) {
roentgen b75cab
            x = w>>2;
roentgen b75cab
            do {
roentgen b75cab
                int32 Cb = pp[8];
roentgen b75cab
                int32 Cr = pp[9];
roentgen b75cab
                
roentgen b75cab
                YCbCrtoRGB(cp [0], pp[0]);
roentgen b75cab
                YCbCrtoRGB(cp [1], pp[1]);
roentgen b75cab
                YCbCrtoRGB(cp [2], pp[2]);
roentgen b75cab
                YCbCrtoRGB(cp [3], pp[3]);
roentgen b75cab
                YCbCrtoRGB(cp1[0], pp[4]);
roentgen b75cab
                YCbCrtoRGB(cp1[1], pp[5]);
roentgen b75cab
                YCbCrtoRGB(cp1[2], pp[6]);
roentgen b75cab
                YCbCrtoRGB(cp1[3], pp[7]);
roentgen b75cab
                
roentgen b75cab
                cp += 4, cp1 += 4;
roentgen b75cab
                pp += 10;
roentgen b75cab
            } while (--x);
roentgen b75cab
            cp += incr, cp1 += incr;
roentgen b75cab
            pp += fromskew;
roentgen b75cab
        }
roentgen b75cab
    } else {
roentgen b75cab
        while (h > 0) {
roentgen b75cab
            for (x = w; x > 0;) {
roentgen b75cab
                int32 Cb = pp[8];
roentgen b75cab
                int32 Cr = pp[9];
roentgen b75cab
                switch (x) {
roentgen b75cab
                default:
roentgen b75cab
                    switch (h) {
roentgen b75cab
                    default: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
roentgen b75cab
                    case 1:  YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
roentgen b75cab
                    }                                    /* FALLTHROUGH */
roentgen b75cab
                case 3:
roentgen b75cab
                    switch (h) {
roentgen b75cab
                    default: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
roentgen b75cab
                    case 1:  YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
roentgen b75cab
                    }                                    /* FALLTHROUGH */
roentgen b75cab
                case 2:
roentgen b75cab
                    switch (h) {
roentgen b75cab
                    default: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
roentgen b75cab
                    case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
roentgen b75cab
                    }                                    /* FALLTHROUGH */
roentgen b75cab
                case 1:
roentgen b75cab
                    switch (h) {
roentgen b75cab
                    default: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
roentgen b75cab
                    case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
roentgen b75cab
                    }                                    /* FALLTHROUGH */
roentgen b75cab
                }
roentgen b75cab
                if (x < 4) {
roentgen b75cab
                    cp += x; cp1 += x;
roentgen b75cab
                    x = 0;
roentgen b75cab
                }
roentgen b75cab
                else {
roentgen b75cab
                    cp += 4; cp1 += 4;
roentgen b75cab
                    x -= 4;
roentgen b75cab
                }
roentgen b75cab
                pp += 10;
roentgen b75cab
            }
roentgen b75cab
            if (h <= 2)
roentgen b75cab
                break;
roentgen b75cab
            h -= 2;
roentgen b75cab
            cp += incr, cp1 += incr;
roentgen b75cab
            pp += fromskew;
roentgen b75cab
        }
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
roentgen b75cab
{
roentgen b75cab
    (void) y;
roentgen b75cab
    /* XXX adjust fromskew */
roentgen b75cab
    do {
roentgen b75cab
	x = w>>2;
roentgen b75cab
	do {
roentgen b75cab
	    int32 Cb = pp[4];
roentgen b75cab
	    int32 Cr = pp[5];
roentgen b75cab
roentgen b75cab
	    YCbCrtoRGB(cp [0], pp[0]);
roentgen b75cab
	    YCbCrtoRGB(cp [1], pp[1]);
roentgen b75cab
	    YCbCrtoRGB(cp [2], pp[2]);
roentgen b75cab
	    YCbCrtoRGB(cp [3], pp[3]);
roentgen b75cab
roentgen b75cab
	    cp += 4;
roentgen b75cab
	    pp += 6;
roentgen b75cab
	} while (--x);
roentgen b75cab
roentgen b75cab
        if( (w&3) != 0 )
roentgen b75cab
        {
roentgen b75cab
	    int32 Cb = pp[4];
roentgen b75cab
	    int32 Cr = pp[5];
roentgen b75cab
roentgen b75cab
            switch( (w&3) ) {
roentgen b75cab
              case 3: YCbCrtoRGB(cp [2], pp[2]);
roentgen b75cab
              case 2: YCbCrtoRGB(cp [1], pp[1]);
roentgen b75cab
              case 1: YCbCrtoRGB(cp [0], pp[0]);
roentgen b75cab
              case 0: break;
roentgen b75cab
            }
roentgen b75cab
roentgen b75cab
            cp += (w&3);
roentgen b75cab
            pp += 6;
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
	cp += toskew;
roentgen b75cab
	pp += fromskew;
roentgen b75cab
    } while (--h);
roentgen b75cab
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
roentgen b75cab
{
roentgen b75cab
	uint32* cp2;
roentgen b75cab
	int32 incr = 2*toskew+w;
roentgen b75cab
	(void) y;
roentgen b75cab
	fromskew = (fromskew / 2) * 6;
roentgen b75cab
	cp2 = cp+w+toskew;
roentgen b75cab
	while (h>=2) {
roentgen b75cab
		x = w;
roentgen b75cab
		while (x>=2) {
roentgen b75cab
			uint32 Cb = pp[4];
roentgen b75cab
			uint32 Cr = pp[5];
roentgen b75cab
			YCbCrtoRGB(cp[0], pp[0]);
roentgen b75cab
			YCbCrtoRGB(cp[1], pp[1]);
roentgen b75cab
			YCbCrtoRGB(cp2[0], pp[2]);
roentgen b75cab
			YCbCrtoRGB(cp2[1], pp[3]);
roentgen b75cab
			cp += 2;
roentgen b75cab
			cp2 += 2;
roentgen b75cab
			pp += 6;
roentgen b75cab
			x -= 2;
roentgen b75cab
		}
roentgen b75cab
		if (x==1) {
roentgen b75cab
			uint32 Cb = pp[4];
roentgen b75cab
			uint32 Cr = pp[5];
roentgen b75cab
			YCbCrtoRGB(cp[0], pp[0]);
roentgen b75cab
			YCbCrtoRGB(cp2[0], pp[2]);
roentgen b75cab
			cp ++ ;
roentgen b75cab
			cp2 ++ ;
roentgen b75cab
			pp += 6;
roentgen b75cab
		}
roentgen b75cab
		cp += incr;
roentgen b75cab
		cp2 += incr;
roentgen b75cab
		pp += fromskew;
roentgen b75cab
		h-=2;
roentgen b75cab
	}
roentgen b75cab
	if (h==1) {
roentgen b75cab
		x = w;
roentgen b75cab
		while (x>=2) {
roentgen b75cab
			uint32 Cb = pp[4];
roentgen b75cab
			uint32 Cr = pp[5];
roentgen b75cab
			YCbCrtoRGB(cp[0], pp[0]);
roentgen b75cab
			YCbCrtoRGB(cp[1], pp[1]);
roentgen b75cab
			cp += 2;
roentgen b75cab
			cp2 += 2;
roentgen b75cab
			pp += 6;
roentgen b75cab
			x -= 2;
roentgen b75cab
		}
roentgen b75cab
		if (x==1) {
roentgen b75cab
			uint32 Cb = pp[4];
roentgen b75cab
			uint32 Cr = pp[5];
roentgen b75cab
			YCbCrtoRGB(cp[0], pp[0]);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
roentgen b75cab
{
roentgen b75cab
	(void) y;
roentgen b75cab
	fromskew = (fromskew * 4) / 2;
roentgen b75cab
	do {
roentgen b75cab
		x = w>>1;
roentgen b75cab
		do {
roentgen b75cab
			int32 Cb = pp[2];
roentgen b75cab
			int32 Cr = pp[3];
roentgen b75cab
roentgen b75cab
			YCbCrtoRGB(cp[0], pp[0]);
roentgen b75cab
			YCbCrtoRGB(cp[1], pp[1]);
roentgen b75cab
roentgen b75cab
			cp += 2;
roentgen b75cab
			pp += 4;
roentgen b75cab
		} while (--x);
roentgen b75cab
roentgen b75cab
		if( (w&1) != 0 )
roentgen b75cab
		{
roentgen b75cab
			int32 Cb = pp[2];
roentgen b75cab
			int32 Cr = pp[3];
roentgen b75cab
roentgen b75cab
			YCbCrtoRGB(cp[0], pp[0]);
roentgen b75cab
roentgen b75cab
			cp += 1;
roentgen b75cab
			pp += 4;
roentgen b75cab
		}
roentgen b75cab
roentgen b75cab
		cp += toskew;
roentgen b75cab
		pp += fromskew;
roentgen b75cab
	} while (--h);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
roentgen b75cab
{
roentgen b75cab
	uint32* cp2;
roentgen b75cab
	int32 incr = 2*toskew+w;
roentgen b75cab
	(void) y;
roentgen b75cab
	fromskew = (fromskew / 2) * 4;
roentgen b75cab
	cp2 = cp+w+toskew;
roentgen b75cab
	while (h>=2) {
roentgen b75cab
		x = w;
roentgen b75cab
		do {
roentgen b75cab
			uint32 Cb = pp[2];
roentgen b75cab
			uint32 Cr = pp[3];
roentgen b75cab
			YCbCrtoRGB(cp[0], pp[0]);
roentgen b75cab
			YCbCrtoRGB(cp2[0], pp[1]);
roentgen b75cab
			cp ++;
roentgen b75cab
			cp2 ++;
roentgen b75cab
			pp += 4;
roentgen b75cab
		} while (--x);
roentgen b75cab
		cp += incr;
roentgen b75cab
		cp2 += incr;
roentgen b75cab
		pp += fromskew;
roentgen b75cab
		h-=2;
roentgen b75cab
	}
roentgen b75cab
	if (h==1) {
roentgen b75cab
		x = w;
roentgen b75cab
		do {
roentgen b75cab
			uint32 Cb = pp[2];
roentgen b75cab
			uint32 Cr = pp[3];
roentgen b75cab
			YCbCrtoRGB(cp[0], pp[0]);
roentgen b75cab
			cp ++;
roentgen b75cab
			pp += 4;
roentgen b75cab
		} while (--x);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed YCbCr samples w/ no subsampling => RGB
roentgen b75cab
 */
roentgen b75cab
DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
roentgen b75cab
{
roentgen b75cab
	(void) y;
roentgen b75cab
	fromskew *= 3;
roentgen b75cab
	do {
roentgen b75cab
		x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
roentgen b75cab
		do {
roentgen b75cab
			int32 Cb = pp[1];
roentgen b75cab
			int32 Cr = pp[2];
roentgen b75cab
roentgen b75cab
			YCbCrtoRGB(*cp++, pp[0]);
roentgen b75cab
roentgen b75cab
			pp += 3;
roentgen b75cab
		} while (--x);
roentgen b75cab
		cp += toskew;
roentgen b75cab
		pp += fromskew;
roentgen b75cab
	} while (--h);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * 8-bit packed YCbCr samples w/ no subsampling => RGB
roentgen b75cab
 */
roentgen b75cab
DECLARESepPutFunc(putseparate8bitYCbCr11tile)
roentgen b75cab
{
roentgen b75cab
	(void) y;
roentgen b75cab
	(void) a;
roentgen b75cab
	/* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
roentgen b75cab
	while (h-- > 0) {
roentgen b75cab
		x = w;
roentgen b75cab
		do {
roentgen b75cab
			uint32 dr, dg, db;
roentgen b75cab
			TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
roentgen b75cab
			*cp++ = PACK(dr,dg,db);
roentgen b75cab
		} while (--x);
roentgen b75cab
		SKEW(r, g, b, fromskew);
roentgen b75cab
		cp += toskew;
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
#undef YCbCrtoRGB
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
initYCbCrConversion(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "initYCbCrConversion";
roentgen b75cab
roentgen b75cab
	float *luma, *refBlackWhite;
roentgen b75cab
roentgen b75cab
	if (img->ycbcr == NULL) {
roentgen b75cab
		img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
roentgen b75cab
		    TIFFroundup_32(sizeof (TIFFYCbCrToRGB), sizeof (long))  
roentgen b75cab
		    + 4*256*sizeof (TIFFRGBValue)
roentgen b75cab
		    + 2*256*sizeof (int)
roentgen b75cab
		    + 3*256*sizeof (int32)
roentgen b75cab
		    );
roentgen b75cab
		if (img->ycbcr == NULL) {
roentgen b75cab
			TIFFErrorExt(img->tif->tif_clientdata, module,
roentgen b75cab
			    "No space for YCbCr->RGB conversion state");
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
roentgen b75cab
	TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
roentgen b75cab
	    &refBlackWhite);
roentgen b75cab
	if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
roentgen b75cab
		return(0);
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static tileContigRoutine
roentgen b75cab
initCIELabConversion(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "initCIELabConversion";
roentgen b75cab
roentgen b75cab
	float   *whitePoint;
roentgen b75cab
	float   refWhite[3];
roentgen b75cab
roentgen b75cab
	if (!img->cielab) {
roentgen b75cab
		img->cielab = (TIFFCIELabToRGB *)
roentgen b75cab
			_TIFFmalloc(sizeof(TIFFCIELabToRGB));
roentgen b75cab
		if (!img->cielab) {
roentgen b75cab
			TIFFErrorExt(img->tif->tif_clientdata, module,
roentgen b75cab
			    "No space for CIE L*a*b*->RGB conversion state.");
roentgen b75cab
			return NULL;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
roentgen b75cab
	refWhite[1] = 100.0F;
roentgen b75cab
	refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
roentgen b75cab
	refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
roentgen b75cab
		      / whitePoint[1] * refWhite[1];
roentgen b75cab
	if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {
roentgen b75cab
		TIFFErrorExt(img->tif->tif_clientdata, module,
roentgen b75cab
		    "Failed to initialize CIE L*a*b*->RGB conversion state.");
roentgen b75cab
		_TIFFfree(img->cielab);
roentgen b75cab
		return NULL;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	return putcontig8bitCIELab;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Greyscale images with less than 8 bits/sample are handled
roentgen b75cab
 * with a table to avoid lots of shifts and masks.  The table
roentgen b75cab
 * is setup so that put*bwtile (below) can retrieve 8/bitspersample
roentgen b75cab
 * pixel values simply by indexing into the table with one
roentgen b75cab
 * number.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
makebwmap(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
    TIFFRGBValue* Map = img->Map;
roentgen b75cab
    int bitspersample = img->bitspersample;
roentgen b75cab
    int nsamples = 8 / bitspersample;
roentgen b75cab
    int i;
roentgen b75cab
    uint32* p;
roentgen b75cab
roentgen b75cab
    if( nsamples == 0 )
roentgen b75cab
        nsamples = 1;
roentgen b75cab
roentgen b75cab
    img->BWmap = (uint32**) _TIFFmalloc(
roentgen b75cab
	256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
roentgen b75cab
    if (img->BWmap == NULL) {
roentgen b75cab
		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");
roentgen b75cab
		return (0);
roentgen b75cab
    }
roentgen b75cab
    p = (uint32*)(img->BWmap + 256);
roentgen b75cab
    for (i = 0; i < 256; i++) {
roentgen b75cab
	TIFFRGBValue c;
roentgen b75cab
	img->BWmap[i] = p;
roentgen b75cab
	switch (bitspersample) {
roentgen b75cab
#define	GREY(x)	c = Map[x]; *p++ = PACK(c,c,c);
roentgen b75cab
	case 1:
roentgen b75cab
	    GREY(i>>7);
roentgen b75cab
	    GREY((i>>6)&1);
roentgen b75cab
	    GREY((i>>5)&1);
roentgen b75cab
	    GREY((i>>4)&1);
roentgen b75cab
	    GREY((i>>3)&1);
roentgen b75cab
	    GREY((i>>2)&1);
roentgen b75cab
	    GREY((i>>1)&1);
roentgen b75cab
	    GREY(i&1);
roentgen b75cab
	    break;
roentgen b75cab
	case 2:
roentgen b75cab
	    GREY(i>>6);
roentgen b75cab
	    GREY((i>>4)&3);
roentgen b75cab
	    GREY((i>>2)&3);
roentgen b75cab
	    GREY(i&3);
roentgen b75cab
	    break;
roentgen b75cab
	case 4:
roentgen b75cab
	    GREY(i>>4);
roentgen b75cab
	    GREY(i&0xf);
roentgen b75cab
	    break;
roentgen b75cab
	case 8:
roentgen b75cab
        case 16:
roentgen b75cab
	    GREY(i);
roentgen b75cab
	    break;
roentgen b75cab
	}
roentgen b75cab
#undef	GREY
roentgen b75cab
    }
roentgen b75cab
    return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Construct a mapping table to convert from the range
roentgen b75cab
 * of the data samples to [0,255] --for display.  This
roentgen b75cab
 * process also handles inverting B&W images when needed.
roentgen b75cab
 */ 
roentgen b75cab
static int
roentgen b75cab
setupMap(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
    int32 x, range;
roentgen b75cab
roentgen b75cab
    range = (int32)((1L<<img->bitspersample)-1);</img->
roentgen b75cab
    
roentgen b75cab
    /* treat 16 bit the same as eight bit */
roentgen b75cab
    if( img->bitspersample == 16 )
roentgen b75cab
        range = (int32) 255;
roentgen b75cab
roentgen b75cab
    img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
roentgen b75cab
    if (img->Map == NULL) {
roentgen b75cab
		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
roentgen b75cab
			"No space for photometric conversion table");
roentgen b75cab
		return (0);
roentgen b75cab
    }
roentgen b75cab
    if (img->photometric == PHOTOMETRIC_MINISWHITE) {
roentgen b75cab
	for (x = 0; x <= range; x++)
roentgen b75cab
	    img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);
roentgen b75cab
    } else {
roentgen b75cab
	for (x = 0; x <= range; x++)
roentgen b75cab
	    img->Map[x] = (TIFFRGBValue) ((x * 255) / range);
roentgen b75cab
    }
roentgen b75cab
    if (img->bitspersample <= 16 &&
roentgen b75cab
	(img->photometric == PHOTOMETRIC_MINISBLACK ||
roentgen b75cab
	 img->photometric == PHOTOMETRIC_MINISWHITE)) {
roentgen b75cab
	/*
roentgen b75cab
	 * Use photometric mapping table to construct
roentgen b75cab
	 * unpacking tables for samples <= 8 bits.
roentgen b75cab
	 */
roentgen b75cab
	if (!makebwmap(img))
roentgen b75cab
	    return (0);
roentgen b75cab
	/* no longer need Map, free it */
roentgen b75cab
	_TIFFfree(img->Map), img->Map = NULL;
roentgen b75cab
    }
roentgen b75cab
    return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
checkcmap(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
    uint16* r = img->redcmap;
roentgen b75cab
    uint16* g = img->greencmap;
roentgen b75cab
    uint16* b = img->bluecmap;
roentgen b75cab
    long n = 1L<<img->bitspersample;</img->
roentgen b75cab
roentgen b75cab
    while (n-- > 0)
roentgen b75cab
	if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
roentgen b75cab
	    return (16);
roentgen b75cab
    return (8);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
cvtcmap(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
    uint16* r = img->redcmap;
roentgen b75cab
    uint16* g = img->greencmap;
roentgen b75cab
    uint16* b = img->bluecmap;
roentgen b75cab
    long i;
roentgen b75cab
roentgen b75cab
    for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {</img->
roentgen b75cab
#define	CVT(x)		((uint16)((x)>>8))
roentgen b75cab
	r[i] = CVT(r[i]);
roentgen b75cab
	g[i] = CVT(g[i]);
roentgen b75cab
	b[i] = CVT(b[i]);
roentgen b75cab
#undef	CVT
roentgen b75cab
    }
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Palette images with <= 8 bits/sample are handled
roentgen b75cab
 * with a table to avoid lots of shifts and masks.  The table
roentgen b75cab
 * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
roentgen b75cab
 * pixel values simply by indexing into the table with one
roentgen b75cab
 * number.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
makecmap(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
    int bitspersample = img->bitspersample;
roentgen b75cab
    int nsamples = 8 / bitspersample;
roentgen b75cab
    uint16* r = img->redcmap;
roentgen b75cab
    uint16* g = img->greencmap;
roentgen b75cab
    uint16* b = img->bluecmap;
roentgen b75cab
    uint32 *p;
roentgen b75cab
    int i;
roentgen b75cab
roentgen b75cab
    img->PALmap = (uint32**) _TIFFmalloc(
roentgen b75cab
	256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
roentgen b75cab
    if (img->PALmap == NULL) {
roentgen b75cab
		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
    p = (uint32*)(img->PALmap + 256);
roentgen b75cab
    for (i = 0; i < 256; i++) {
roentgen b75cab
	TIFFRGBValue c;
roentgen b75cab
	img->PALmap[i] = p;
roentgen b75cab
#define	CMAP(x)	c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
roentgen b75cab
	switch (bitspersample) {
roentgen b75cab
	case 1:
roentgen b75cab
	    CMAP(i>>7);
roentgen b75cab
	    CMAP((i>>6)&1);
roentgen b75cab
	    CMAP((i>>5)&1);
roentgen b75cab
	    CMAP((i>>4)&1);
roentgen b75cab
	    CMAP((i>>3)&1);
roentgen b75cab
	    CMAP((i>>2)&1);
roentgen b75cab
	    CMAP((i>>1)&1);
roentgen b75cab
	    CMAP(i&1);
roentgen b75cab
	    break;
roentgen b75cab
	case 2:
roentgen b75cab
	    CMAP(i>>6);
roentgen b75cab
	    CMAP((i>>4)&3);
roentgen b75cab
	    CMAP((i>>2)&3);
roentgen b75cab
	    CMAP(i&3);
roentgen b75cab
	    break;
roentgen b75cab
	case 4:
roentgen b75cab
	    CMAP(i>>4);
roentgen b75cab
	    CMAP(i&0xf);
roentgen b75cab
	    break;
roentgen b75cab
	case 8:
roentgen b75cab
	    CMAP(i);
roentgen b75cab
	    break;
roentgen b75cab
	}
roentgen b75cab
#undef CMAP
roentgen b75cab
    }
roentgen b75cab
    return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* 
roentgen b75cab
 * Construct any mapping table used
roentgen b75cab
 * by the associated put routine.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
buildMap(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
    switch (img->photometric) {
roentgen b75cab
    case PHOTOMETRIC_RGB:
roentgen b75cab
    case PHOTOMETRIC_YCBCR:
roentgen b75cab
    case PHOTOMETRIC_SEPARATED:
roentgen b75cab
	if (img->bitspersample == 8)
roentgen b75cab
	    break;
roentgen b75cab
	/* fall thru... */
roentgen b75cab
    case PHOTOMETRIC_MINISBLACK:
roentgen b75cab
    case PHOTOMETRIC_MINISWHITE:
roentgen b75cab
	if (!setupMap(img))
roentgen b75cab
	    return (0);
roentgen b75cab
	break;
roentgen b75cab
    case PHOTOMETRIC_PALETTE:
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(img) == 16)
roentgen b75cab
	    cvtcmap(img);
roentgen b75cab
	else
roentgen b75cab
	    TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");
roentgen b75cab
	/*
roentgen b75cab
	 * Use mapping table and colormap to construct
roentgen b75cab
	 * unpacking tables for samples < 8 bits.
roentgen b75cab
	 */
roentgen b75cab
	if (img->bitspersample <= 8 && !makecmap(img))
roentgen b75cab
	    return (0);
roentgen b75cab
	break;
roentgen b75cab
    }
roentgen b75cab
    return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Select the appropriate conversion routine for packed data.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
PickContigCase(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
	img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
roentgen b75cab
	img->put.contig = NULL;
roentgen b75cab
	switch (img->photometric) {
roentgen b75cab
		case PHOTOMETRIC_RGB:
roentgen b75cab
			switch (img->bitspersample) {
roentgen b75cab
				case 8:
roentgen b75cab
					if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
roentgen b75cab
						img->put.contig = putRGBAAcontig8bittile;
roentgen b75cab
					else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
roentgen b75cab
					{
roentgen b75cab
						if (BuildMapUaToAa(img))
roentgen b75cab
							img->put.contig = putRGBUAcontig8bittile;
roentgen b75cab
					}
roentgen b75cab
					else
roentgen b75cab
						img->put.contig = putRGBcontig8bittile;
roentgen b75cab
					break;
roentgen b75cab
				case 16:
roentgen b75cab
					if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
roentgen b75cab
					{
roentgen b75cab
						if (BuildMapBitdepth16To8(img))
roentgen b75cab
							img->put.contig = putRGBAAcontig16bittile;
roentgen b75cab
					}
roentgen b75cab
					else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
roentgen b75cab
					{
roentgen b75cab
						if (BuildMapBitdepth16To8(img) &&
roentgen b75cab
						    BuildMapUaToAa(img))
roentgen b75cab
							img->put.contig = putRGBUAcontig16bittile;
roentgen b75cab
					}
roentgen b75cab
					else
roentgen b75cab
					{
roentgen b75cab
						if (BuildMapBitdepth16To8(img))
roentgen b75cab
							img->put.contig = putRGBcontig16bittile;
roentgen b75cab
					}
roentgen b75cab
					break;
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_SEPARATED:
roentgen b75cab
			if (buildMap(img)) {
roentgen b75cab
				if (img->bitspersample == 8) {
roentgen b75cab
					if (!img->Map)
roentgen b75cab
						img->put.contig = putRGBcontig8bitCMYKtile;
roentgen b75cab
					else
roentgen b75cab
						img->put.contig = putRGBcontig8bitCMYKMaptile;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_PALETTE:
roentgen b75cab
			if (buildMap(img)) {
roentgen b75cab
				switch (img->bitspersample) {
roentgen b75cab
					case 8:
roentgen b75cab
						img->put.contig = put8bitcmaptile;
roentgen b75cab
						break;
roentgen b75cab
					case 4:
roentgen b75cab
						img->put.contig = put4bitcmaptile;
roentgen b75cab
						break;
roentgen b75cab
					case 2:
roentgen b75cab
						img->put.contig = put2bitcmaptile;
roentgen b75cab
						break;
roentgen b75cab
					case 1:
roentgen b75cab
						img->put.contig = put1bitcmaptile;
roentgen b75cab
						break;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_MINISWHITE:
roentgen b75cab
		case PHOTOMETRIC_MINISBLACK:
roentgen b75cab
			if (buildMap(img)) {
roentgen b75cab
				switch (img->bitspersample) {
roentgen b75cab
					case 16:
roentgen b75cab
						img->put.contig = put16bitbwtile;
roentgen b75cab
						break;
roentgen b75cab
					case 8:
roentgen b75cab
						if (img->alpha && img->samplesperpixel == 2)
roentgen b75cab
							img->put.contig = putagreytile;
roentgen b75cab
						else
roentgen b75cab
							img->put.contig = putgreytile;
roentgen b75cab
						break;
roentgen b75cab
					case 4:
roentgen b75cab
						img->put.contig = put4bitbwtile;
roentgen b75cab
						break;
roentgen b75cab
					case 2:
roentgen b75cab
						img->put.contig = put2bitbwtile;
roentgen b75cab
						break;
roentgen b75cab
					case 1:
roentgen b75cab
						img->put.contig = put1bitbwtile;
roentgen b75cab
						break;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_YCBCR:
roentgen b75cab
			if ((img->bitspersample==8) && (img->samplesperpixel==3))
roentgen b75cab
			{
roentgen b75cab
				if (initYCbCrConversion(img)!=0)
roentgen b75cab
				{
roentgen b75cab
					/*
roentgen b75cab
					 * The 6.0 spec says that subsampling must be
roentgen b75cab
					 * one of 1, 2, or 4, and that vertical subsampling
roentgen b75cab
					 * must always be <= horizontal subsampling; so
roentgen b75cab
					 * there are only a few possibilities and we just
roentgen b75cab
					 * enumerate the cases.
roentgen b75cab
					 * Joris: added support for the [1,2] case, nonetheless, to accomodate
roentgen b75cab
					 * some OJPEG files
roentgen b75cab
					 */
roentgen b75cab
					uint16 SubsamplingHor;
roentgen b75cab
					uint16 SubsamplingVer;
roentgen b75cab
					TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
roentgen b75cab
					switch ((SubsamplingHor<<4)|SubsamplingVer) {
roentgen b75cab
						case 0x44:
roentgen b75cab
							img->put.contig = putcontig8bitYCbCr44tile;
roentgen b75cab
							break;
roentgen b75cab
						case 0x42:
roentgen b75cab
							img->put.contig = putcontig8bitYCbCr42tile;
roentgen b75cab
							break;
roentgen b75cab
						case 0x41:
roentgen b75cab
							img->put.contig = putcontig8bitYCbCr41tile;
roentgen b75cab
							break;
roentgen b75cab
						case 0x22:
roentgen b75cab
							img->put.contig = putcontig8bitYCbCr22tile;
roentgen b75cab
							break;
roentgen b75cab
						case 0x21:
roentgen b75cab
							img->put.contig = putcontig8bitYCbCr21tile;
roentgen b75cab
							break;
roentgen b75cab
						case 0x12:
roentgen b75cab
							img->put.contig = putcontig8bitYCbCr12tile;
roentgen b75cab
							break;
roentgen b75cab
						case 0x11:
roentgen b75cab
							img->put.contig = putcontig8bitYCbCr11tile;
roentgen b75cab
							break;
roentgen b75cab
					}
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PHOTOMETRIC_CIELAB:
roentgen b75cab
			if (buildMap(img)) {
roentgen b75cab
				if (img->bitspersample == 8)
roentgen b75cab
					img->put.contig = initCIELabConversion(img);
roentgen b75cab
				break;
roentgen b75cab
			}
roentgen b75cab
	}
roentgen b75cab
	return ((img->get!=NULL) && (img->put.contig!=NULL));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Select the appropriate conversion routine for unpacked data.
roentgen b75cab
 *
roentgen b75cab
 * NB: we assume that unpacked single channel data is directed
roentgen b75cab
 *	 to the "packed routines.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
PickSeparateCase(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
	img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
roentgen b75cab
	img->put.separate = NULL;
roentgen b75cab
	switch (img->photometric) {
roentgen b75cab
	case PHOTOMETRIC_MINISWHITE:
roentgen b75cab
	case PHOTOMETRIC_MINISBLACK:
roentgen b75cab
		/* greyscale images processed pretty much as RGB by gtTileSeparate */
roentgen b75cab
	case PHOTOMETRIC_RGB:
roentgen b75cab
		switch (img->bitspersample) {
roentgen b75cab
		case 8:
roentgen b75cab
			if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
roentgen b75cab
				img->put.separate = putRGBAAseparate8bittile;
roentgen b75cab
			else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
roentgen b75cab
			{
roentgen b75cab
				if (BuildMapUaToAa(img))
roentgen b75cab
					img->put.separate = putRGBUAseparate8bittile;
roentgen b75cab
			}
roentgen b75cab
			else
roentgen b75cab
				img->put.separate = putRGBseparate8bittile;
roentgen b75cab
			break;
roentgen b75cab
		case 16:
roentgen b75cab
			if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
roentgen b75cab
			{
roentgen b75cab
				if (BuildMapBitdepth16To8(img))
roentgen b75cab
					img->put.separate = putRGBAAseparate16bittile;
roentgen b75cab
			}
roentgen b75cab
			else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
roentgen b75cab
			{
roentgen b75cab
				if (BuildMapBitdepth16To8(img) &&
roentgen b75cab
				    BuildMapUaToAa(img))
roentgen b75cab
					img->put.separate = putRGBUAseparate16bittile;
roentgen b75cab
			}
roentgen b75cab
			else
roentgen b75cab
			{
roentgen b75cab
				if (BuildMapBitdepth16To8(img))
roentgen b75cab
					img->put.separate = putRGBseparate16bittile;
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	case PHOTOMETRIC_SEPARATED:
roentgen b75cab
		if (img->bitspersample == 8 && img->samplesperpixel == 4)
roentgen b75cab
		{
roentgen b75cab
			img->alpha = 1; // Not alpha, but seems like the only way to get 4th band
roentgen b75cab
			img->put.separate = putCMYKseparate8bittile;
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	case PHOTOMETRIC_YCBCR:
roentgen b75cab
		if ((img->bitspersample==8) && (img->samplesperpixel==3))
roentgen b75cab
		{
roentgen b75cab
			if (initYCbCrConversion(img)!=0)
roentgen b75cab
			{
roentgen b75cab
				uint16 hs, vs;
roentgen b75cab
				TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
roentgen b75cab
				switch ((hs<<4)|vs) {
roentgen b75cab
				case 0x11:
roentgen b75cab
					img->put.separate = putseparate8bitYCbCr11tile;
roentgen b75cab
					break;
roentgen b75cab
					/* TODO: add other cases here */
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		break;
roentgen b75cab
	}
roentgen b75cab
	return ((img->get!=NULL) && (img->put.separate!=NULL));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
BuildMapUaToAa(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
	static const char module[]="BuildMapUaToAa";
roentgen b75cab
	uint8* m;
roentgen b75cab
	uint16 na,nv;
roentgen b75cab
	assert(img->UaToAa==NULL);
roentgen b75cab
	img->UaToAa=_TIFFmalloc(65536);
roentgen b75cab
	if (img->UaToAa==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	m=img->UaToAa;
roentgen b75cab
	for (na=0; na<256; na++)
roentgen b75cab
	{
roentgen b75cab
		for (nv=0; nv<256; nv++)
roentgen b75cab
			*m++=(nv*na+127)/255;
roentgen b75cab
	}
roentgen b75cab
	return(1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
BuildMapBitdepth16To8(TIFFRGBAImage* img)
roentgen b75cab
{
roentgen b75cab
	static const char module[]="BuildMapBitdepth16To8";
roentgen b75cab
	uint8* m;
roentgen b75cab
	uint32 n;
roentgen b75cab
	assert(img->Bitdepth16To8==NULL);
roentgen b75cab
	img->Bitdepth16To8=_TIFFmalloc(65536);
roentgen b75cab
	if (img->Bitdepth16To8==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	m=img->Bitdepth16To8;
roentgen b75cab
	for (n=0; n<65536; n++)
roentgen b75cab
		*m++=(n+128)/257;
roentgen b75cab
	return(1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Read a whole strip off data from the file, and convert to RGBA form.
roentgen b75cab
 * If this is the last strip, then it will only contain the portion of
roentgen b75cab
 * the strip that is actually within the image space.  The result is
roentgen b75cab
 * organized in bottom to top form.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
roentgen b75cab
roentgen b75cab
{
roentgen b75cab
    char 	emsg[1024] = "";
roentgen b75cab
    TIFFRGBAImage img;
roentgen b75cab
    int 	ok;
roentgen b75cab
    uint32	rowsperstrip, rows_to_read;
roentgen b75cab
roentgen b75cab
    if( TIFFIsTiled( tif ) )
roentgen b75cab
    {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
roentgen b75cab
                  "Can't use TIFFReadRGBAStrip() with tiled file.");
roentgen b75cab
	return (0);
roentgen b75cab
    }
roentgen b75cab
    
roentgen b75cab
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
roentgen b75cab
    if( (row % rowsperstrip) != 0 )
roentgen b75cab
    {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
roentgen b75cab
				"Row passed to TIFFReadRGBAStrip() must be first in a strip.");
roentgen b75cab
		return (0);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
roentgen b75cab
roentgen b75cab
        img.row_offset = row;
roentgen b75cab
        img.col_offset = 0;
roentgen b75cab
roentgen b75cab
        if( row + rowsperstrip > img.height )
roentgen b75cab
            rows_to_read = img.height - row;
roentgen b75cab
        else
roentgen b75cab
            rows_to_read = rowsperstrip;
roentgen b75cab
        
roentgen b75cab
	ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read );
roentgen b75cab
        
roentgen b75cab
	TIFFRGBAImageEnd(&img);
roentgen b75cab
    } else {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
roentgen b75cab
		ok = 0;
roentgen b75cab
    }
roentgen b75cab
    
roentgen b75cab
    return (ok);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Read a whole tile off data from the file, and convert to RGBA form.
roentgen b75cab
 * The returned RGBA data is organized from bottom to top of tile,
roentgen b75cab
 * and may include zeroed areas if the tile extends off the image.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
roentgen b75cab
roentgen b75cab
{
roentgen b75cab
    char 	emsg[1024] = "";
roentgen b75cab
    TIFFRGBAImage img;
roentgen b75cab
    int 	ok;
roentgen b75cab
    uint32	tile_xsize, tile_ysize;
roentgen b75cab
    uint32	read_xsize, read_ysize;
roentgen b75cab
    uint32	i_row;
roentgen b75cab
roentgen b75cab
    /*
roentgen b75cab
     * Verify that our request is legal - on a tile file, and on a
roentgen b75cab
     * tile boundary.
roentgen b75cab
     */
roentgen b75cab
    
roentgen b75cab
    if( !TIFFIsTiled( tif ) )
roentgen b75cab
    {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
roentgen b75cab
				  "Can't use TIFFReadRGBATile() with stripped file.");
roentgen b75cab
		return (0);
roentgen b75cab
    }
roentgen b75cab
    
roentgen b75cab
    TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
roentgen b75cab
    TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
roentgen b75cab
    if( (col % tile_xsize) != 0 || (row % tile_ysize) != 0 )
roentgen b75cab
    {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
roentgen b75cab
                  "Row/col passed to TIFFReadRGBATile() must be top"
roentgen b75cab
                  "left corner of a tile.");
roentgen b75cab
	return (0);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    /*
roentgen b75cab
     * Setup the RGBA reader.
roentgen b75cab
     */
roentgen b75cab
    
roentgen b75cab
    if (!TIFFRGBAImageOK(tif, emsg) 
roentgen b75cab
	|| !TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
roentgen b75cab
	    TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
roentgen b75cab
	    return( 0 );
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    /*
roentgen b75cab
     * The TIFFRGBAImageGet() function doesn't allow us to get off the
roentgen b75cab
     * edge of the image, even to fill an otherwise valid tile.  So we
roentgen b75cab
     * figure out how much we can read, and fix up the tile buffer to
roentgen b75cab
     * a full tile configuration afterwards.
roentgen b75cab
     */
roentgen b75cab
roentgen b75cab
    if( row + tile_ysize > img.height )
roentgen b75cab
        read_ysize = img.height - row;
roentgen b75cab
    else
roentgen b75cab
        read_ysize = tile_ysize;
roentgen b75cab
    
roentgen b75cab
    if( col + tile_xsize > img.width )
roentgen b75cab
        read_xsize = img.width - col;
roentgen b75cab
    else
roentgen b75cab
        read_xsize = tile_xsize;
roentgen b75cab
roentgen b75cab
    /*
roentgen b75cab
     * Read the chunk of imagery.
roentgen b75cab
     */
roentgen b75cab
    
roentgen b75cab
    img.row_offset = row;
roentgen b75cab
    img.col_offset = col;
roentgen b75cab
roentgen b75cab
    ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize );
roentgen b75cab
        
roentgen b75cab
    TIFFRGBAImageEnd(&img);
roentgen b75cab
roentgen b75cab
    /*
roentgen b75cab
     * If our read was incomplete we will need to fix up the tile by
roentgen b75cab
     * shifting the data around as if a full tile of data is being returned.
roentgen b75cab
     *
roentgen b75cab
     * This is all the more complicated because the image is organized in
roentgen b75cab
     * bottom to top format. 
roentgen b75cab
     */
roentgen b75cab
roentgen b75cab
    if( read_xsize == tile_xsize && read_ysize == tile_ysize )
roentgen b75cab
        return( ok );
roentgen b75cab
roentgen b75cab
    for( i_row = 0; i_row < read_ysize; i_row++ ) {
roentgen b75cab
        memmove( raster + (tile_ysize - i_row - 1) * tile_xsize,
roentgen b75cab
                 raster + (read_ysize - i_row - 1) * read_xsize,
roentgen b75cab
                 read_xsize * sizeof(uint32) );
roentgen b75cab
        _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize+read_xsize,
roentgen b75cab
                     0, sizeof(uint32) * (tile_xsize - read_xsize) );
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    for( i_row = read_ysize; i_row < tile_ysize; i_row++ ) {
roentgen b75cab
        _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize,
roentgen b75cab
                     0, sizeof(uint32) * tile_xsize );
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    return (ok);
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
 */