Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
  Modified tif_getimage.c that takes uint64 as output pixel type
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
kusano ca9c32
Toshihiro Shimizu 890ddd
/* $Id: tif_getimage.c,v 1.82 2012-06-06 00:17:49 fwarmerdam Exp $ */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Copyright (c) 1991-1997 Sam Leffler
Toshihiro Shimizu 890ddd
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * Permission to use, copy, modify, distribute, and sell this software and 
Toshihiro Shimizu 890ddd
 * its documentation for any purpose is hereby granted without fee, provided
Toshihiro Shimizu 890ddd
 * that (i) the above copyright notices and this permission notice appear in
Toshihiro Shimizu 890ddd
 * all copies of the software and related documentation, and (ii) the names of
Toshihiro Shimizu 890ddd
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
Toshihiro Shimizu 890ddd
 * publicity relating to the software without the specific, prior written
Toshihiro Shimizu 890ddd
 * permission of Sam Leffler and Silicon Graphics.
Toshihiro Shimizu 890ddd
 * 
Toshihiro Shimizu 890ddd
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
Toshihiro Shimizu 890ddd
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
Toshihiro Shimizu 890ddd
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
Toshihiro Shimizu 890ddd
 * 
Toshihiro Shimizu 890ddd
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
Toshihiro Shimizu 890ddd
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
Toshihiro Shimizu 890ddd
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
Toshihiro Shimizu 890ddd
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
Toshihiro Shimizu 890ddd
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
Toshihiro Shimizu 890ddd
 * OF THIS SOFTWARE.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * TIFF Library
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * Read and return a packed RGBA image.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
#include "tiffiop.h"
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
kusano ca9c32
typedef int (*gtFunc_32)(TIFFRGBAImage*, uint32*, uint32, uint32);
kusano ca9c32
typedef int (*gtFunc_64)(TIFFRGBAImage*, uint64*, uint32, uint32);
kusano ca9c32
typedef void (*tileContigRoutine_64)
kusano ca9c32
    (TIFFRGBAImage*, uint64*, uint32, uint32, uint32, uint32, int32, int32,
kusano ca9c32
	unsigned char*);
kusano ca9c32
typedef void (*tileSeparateRoutine_64)
kusano ca9c32
    (TIFFRGBAImage*, uint64*, uint32, uint32, uint32, uint32, int32, int32,
kusano ca9c32
	unsigned char*, unsigned char*, unsigned char*, unsigned char*);
kusano ca9c32
Toshihiro Shimizu 890ddd
kusano ca9c32
static int gtTileContig(TIFFRGBAImage*, uint64*, uint32, uint32);
kusano ca9c32
static int gtTileSeparate(TIFFRGBAImage*, uint64*, uint32, uint32);
kusano ca9c32
static int gtStripContig(TIFFRGBAImage*, uint64*, uint32, uint32);
kusano ca9c32
static int gtStripSeparate(TIFFRGBAImage*, uint64*, uint32, uint32);
kusano ca9c32
static int PickContigCase(TIFFRGBAImage*);
kusano ca9c32
static int PickSeparateCase(TIFFRGBAImage*);
kusano ca9c32
kusano ca9c32
static int BuildMapUaToAa(TIFFRGBAImage* img);
kusano ca9c32
static int BuildMapBitdepth16To8(TIFFRGBAImage* img);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static const char photoTag[] = "PhotometricInterpretation";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* 
Toshihiro Shimizu 890ddd
 * Helper constants used in Orientation tag handling
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
#define FLIP_VERTICALLY 0x01
Toshihiro Shimizu 890ddd
#define FLIP_HORIZONTALLY 0x02
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Color conversion constants. We will define display types here.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static const TIFFDisplay display_sRGB = {
kusano ca9c32
	{			/* XYZ -> luminance matrix */
kusano ca9c32
		{  3.2410F, -1.5374F, -0.4986F },
kusano ca9c32
		{  -0.9692F, 1.8760F, 0.0416F },
kusano ca9c32
		{  0.0556F, -0.2040F, 1.0570F }
kusano ca9c32
	},	
kusano ca9c32
	100.0F, 100.0F, 100.0F,	/* Light o/p for reference white */
kusano ca9c32
	255, 255, 255,		/* Pixel values for ref. white */
kusano ca9c32
	1.0F, 1.0F, 1.0F,	/* Residual light o/p for black pixel */
kusano ca9c32
	2.4F, 2.4F, 2.4F,	/* Gamma values for the three guns */
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
kusano ca9c32
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
isCCITTCompression(TIFF* tif)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    uint16 compress;
kusano ca9c32
    TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
kusano ca9c32
    return (compress == COMPRESSION_CCITTFAX3 ||
kusano ca9c32
	    compress == COMPRESSION_CCITTFAX4 ||
kusano ca9c32
	    compress == COMPRESSION_CCITTRLE ||
kusano ca9c32
	    compress == COMPRESSION_CCITTRLEW);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
kusano ca9c32
int
kusano ca9c32
TIFFRGBAImageBegin_64(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	uint16* sampleinfo;
Toshihiro Shimizu 890ddd
	uint16 extrasamples;
Toshihiro Shimizu 890ddd
	uint16 planarconfig;
Toshihiro Shimizu 890ddd
	uint16 compress;
Toshihiro Shimizu 890ddd
	int colorchannels;
Toshihiro Shimizu 890ddd
	uint16 *red_orig, *green_orig, *blue_orig;
Toshihiro Shimizu 890ddd
	int n_color;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* Initialize to normal values */
Toshihiro Shimizu 890ddd
	img->row_offset = 0;
Toshihiro Shimizu 890ddd
	img->col_offset = 0;
Toshihiro Shimizu 890ddd
	img->redcmap = NULL;
Toshihiro Shimizu 890ddd
	img->greencmap = NULL;
Toshihiro Shimizu 890ddd
	img->bluecmap = NULL;
kusano ca9c32
	img->req_orientation = ORIENTATION_BOTLEFT;     /* It is the default */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	img->tif = tif;
Toshihiro Shimizu 890ddd
	img->stoponerr = stop;
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
Toshihiro Shimizu 890ddd
	switch (img->bitspersample) {
kusano ca9c32
		case 1:
kusano ca9c32
		case 2:
kusano ca9c32
		case 4:
kusano ca9c32
		case 8:
kusano ca9c32
		case 16:
kusano ca9c32
			break;
kusano ca9c32
		default:
kusano ca9c32
			sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
kusano ca9c32
			    img->bitspersample);
kusano ca9c32
			goto fail_return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	img->alpha = 0;
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
kusano ca9c32
	    &extrasamples, &sampleinfo);
kusano ca9c32
	if (extrasamples >= 1)
kusano ca9c32
	{
Toshihiro Shimizu 890ddd
		switch (sampleinfo[0]) {
kusano ca9c32
			case EXTRASAMPLE_UNSPECIFIED:          /* Workaround for some images without */
kusano ca9c32
				if (img->samplesperpixel > 3)  /* correct info about alpha channel */
kusano ca9c32
					img->alpha = EXTRASAMPLE_ASSOCALPHA;
kusano ca9c32
				break;
kusano ca9c32
			case EXTRASAMPLE_ASSOCALPHA:           /* data is pre-multiplied */
kusano ca9c32
			case EXTRASAMPLE_UNASSALPHA:           /* data is not pre-multiplied */
kusano ca9c32
				img->alpha = sampleinfo[0];
kusano ca9c32
				break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
kusano ca9c32
	if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
Toshihiro Shimizu 890ddd
		img->photometric = PHOTOMETRIC_MINISWHITE;
Toshihiro Shimizu 890ddd
kusano ca9c32
	if( extrasamples == 0
kusano ca9c32
	    && img->samplesperpixel == 4
kusano ca9c32
	    && img->photometric == PHOTOMETRIC_RGB )
kusano ca9c32
	{
Toshihiro Shimizu 890ddd
		img->alpha = EXTRASAMPLE_ASSOCALPHA;
Toshihiro Shimizu 890ddd
		extrasamples = 1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	colorchannels = img->samplesperpixel - extrasamples;
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
Toshihiro Shimizu 890ddd
	if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
Toshihiro Shimizu 890ddd
		switch (colorchannels) {
kusano ca9c32
			case 1:
kusano ca9c32
				if (isCCITTCompression(tif))
kusano ca9c32
					img->photometric = PHOTOMETRIC_MINISWHITE;
kusano ca9c32
				else
kusano ca9c32
					img->photometric = PHOTOMETRIC_MINISBLACK;
kusano ca9c32
				break;
kusano ca9c32
			case 3:
kusano ca9c32
				img->photometric = PHOTOMETRIC_RGB;
kusano ca9c32
				break;
kusano ca9c32
			default:
kusano ca9c32
				sprintf(emsg, "Missing needed %s tag", photoTag);
kusano ca9c32
                                goto fail_return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	switch (img->photometric) {
kusano ca9c32
		case PHOTOMETRIC_PALETTE:
kusano ca9c32
			if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
kusano ca9c32
			    &red_orig, &green_orig, &blue_orig)) {
kusano ca9c32
				sprintf(emsg, "Missing required \"Colormap\" tag");
kusano ca9c32
                                goto fail_return;
kusano ca9c32
			}
Toshihiro Shimizu 890ddd
kusano ca9c32
			/* copy the colormaps so we can modify them */
kusano ca9c32
			n_color = (1L << img->bitspersample);
kusano ca9c32
			img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
kusano ca9c32
			img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
kusano ca9c32
			img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
kusano ca9c32
			if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
kusano ca9c32
				sprintf(emsg, "Out of memory for colormap copy");
kusano ca9c32
                                goto fail_return;
kusano ca9c32
			}
Toshihiro Shimizu 890ddd
kusano ca9c32
			_TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
kusano ca9c32
			_TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
kusano ca9c32
			_TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
kusano ca9c32
kusano ca9c32
			/* fall thru... */
kusano ca9c32
		case PHOTOMETRIC_MINISWHITE:
kusano ca9c32
		case PHOTOMETRIC_MINISBLACK:
kusano ca9c32
			if (planarconfig == PLANARCONFIG_CONTIG
kusano ca9c32
			    && img->samplesperpixel != 1
kusano ca9c32
			    && img->bitspersample < 8 ) {
kusano ca9c32
				sprintf(emsg,
kusano ca9c32
				    "Sorry, can not handle contiguous data with %s=%d, "
kusano ca9c32
				    "and %s=%d and Bits/Sample=%d",
kusano ca9c32
				    photoTag, img->photometric,
kusano ca9c32
				    "Samples/pixel", img->samplesperpixel,
kusano ca9c32
				    img->bitspersample);
kusano ca9c32
                                goto fail_return;
kusano ca9c32
			}
kusano ca9c32
			break;
kusano ca9c32
		case PHOTOMETRIC_YCBCR:
kusano ca9c32
			/* It would probably be nice to have a reality check here. */
kusano ca9c32
			if (planarconfig == PLANARCONFIG_CONTIG)
kusano ca9c32
				/* can rely on libjpeg to convert to RGB */
kusano ca9c32
				/* XXX should restore current state on exit */
kusano ca9c32
				switch (compress) {
kusano ca9c32
					case COMPRESSION_JPEG:
kusano ca9c32
						/*
Toshihiro Shimizu 890ddd
						 * TODO: when complete tests verify complete desubsampling
Toshihiro Shimizu 890ddd
						 * and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
Toshihiro Shimizu 890ddd
						 * favor of tif_getimage.c native handling
Toshihiro Shimizu 890ddd
						 */
kusano ca9c32
						TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
kusano ca9c32
						img->photometric = PHOTOMETRIC_RGB;
kusano ca9c32
						break;
kusano ca9c32
					default:
kusano ca9c32
						/* do nothing */;
kusano ca9c32
						break;
kusano ca9c32
				}
kusano ca9c32
			/*
Toshihiro Shimizu 890ddd
			 * TODO: if at all meaningful and useful, make more complete
Toshihiro Shimizu 890ddd
			 * support check here, or better still, refactor to let supporting
Toshihiro Shimizu 890ddd
			 * code decide whether there is support and what meaningfull
Toshihiro Shimizu 890ddd
			 * error to return
Toshihiro Shimizu 890ddd
			 */
kusano ca9c32
			break;
kusano ca9c32
		case PHOTOMETRIC_RGB:
kusano ca9c32
			if (colorchannels < 3) {
kusano ca9c32
				sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
kusano ca9c32
				    "Color channels", colorchannels);
kusano ca9c32
                                goto fail_return;
kusano ca9c32
			}
kusano ca9c32
			break;
kusano ca9c32
		case PHOTOMETRIC_SEPARATED:
kusano ca9c32
			{
kusano ca9c32
				uint16 inkset;
kusano ca9c32
				TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
kusano ca9c32
				if (inkset != INKSET_CMYK) {
kusano ca9c32
					sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
kusano ca9c32
					    "InkSet", inkset);
kusano ca9c32
                                        goto fail_return;
kusano ca9c32
				}
kusano ca9c32
				if (img->samplesperpixel < 4) {
kusano ca9c32
					sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
kusano ca9c32
					    "Samples/pixel", img->samplesperpixel);
kusano ca9c32
                                        goto fail_return;
kusano ca9c32
				}
kusano ca9c32
			}
kusano ca9c32
			break;
kusano ca9c32
		case PHOTOMETRIC_LOGL:
kusano ca9c32
			if (compress != COMPRESSION_SGILOG) {
kusano ca9c32
				sprintf(emsg, "Sorry, LogL data must have %s=%d",
kusano ca9c32
				    "Compression", COMPRESSION_SGILOG);
kusano ca9c32
                                goto fail_return;
kusano ca9c32
			}
kusano ca9c32
			TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
kusano ca9c32
			img->photometric = PHOTOMETRIC_MINISBLACK;	/* little white lie */
kusano ca9c32
			img->bitspersample = 8;
kusano ca9c32
			break;
kusano ca9c32
		case PHOTOMETRIC_LOGLUV:
kusano ca9c32
			if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
kusano ca9c32
				sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
kusano ca9c32
				    "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
kusano ca9c32
                                goto fail_return;
kusano ca9c32
			}
kusano ca9c32
			if (planarconfig != PLANARCONFIG_CONTIG) {
kusano ca9c32
				sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
kusano ca9c32
				    "Planarconfiguration", planarconfig);
kusano ca9c32
				return (0);
kusano ca9c32
			}
kusano ca9c32
			TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
kusano ca9c32
			img->photometric = PHOTOMETRIC_RGB;		/* little white lie */
kusano ca9c32
			img->bitspersample = 8;
kusano ca9c32
			break;
kusano ca9c32
		case PHOTOMETRIC_CIELAB:
kusano ca9c32
			break;
kusano ca9c32
		default:
kusano ca9c32
			sprintf(emsg, "Sorry, can not handle image with %s=%d",
kusano ca9c32
			    photoTag, img->photometric);
kusano ca9c32
                        goto fail_return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	img->Map = NULL;
Toshihiro Shimizu 890ddd
	img->BWmap = NULL;
Toshihiro Shimizu 890ddd
	img->PALmap = NULL;
Toshihiro Shimizu 890ddd
	img->ycbcr = NULL;
Toshihiro Shimizu 890ddd
	img->cielab = NULL;
Toshihiro Shimizu 890ddd
	img->UaToAa = NULL;
Toshihiro Shimizu 890ddd
	img->Bitdepth16To8 = NULL;
Toshihiro Shimizu 890ddd
	TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
Toshihiro Shimizu 890ddd
	TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
Toshihiro Shimizu 890ddd
	img->isContig =
kusano ca9c32
	    !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
Toshihiro Shimizu 890ddd
	if (img->isContig) {
Toshihiro Shimizu 890ddd
		if (!PickContigCase(img)) {
Toshihiro Shimizu 890ddd
			sprintf(emsg, "Sorry, can not handle image");
Toshihiro Shimizu 890ddd
			goto fail_return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (!PickSeparateCase(img)) {
Toshihiro Shimizu 890ddd
			sprintf(emsg, "Sorry, can not handle image");
Toshihiro Shimizu 890ddd
			goto fail_return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
kusano ca9c32
  fail_return:
kusano ca9c32
        _TIFFfree( img->redcmap );
kusano ca9c32
        _TIFFfree( img->greencmap );
kusano ca9c32
        _TIFFfree( img->bluecmap );
kusano ca9c32
        img->redcmap = img->greencmap = img->bluecmap = NULL;
kusano ca9c32
        return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
kusano ca9c32
kusano ca9c32
int
kusano ca9c32
TIFFRGBAImageGet_64(TIFFRGBAImage* img, uint64* raster, uint32 w, uint32 h)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
  gtFunc_64 get = (gtFunc_64) img->get;
Toshihiro Shimizu 890ddd
kusano ca9c32
    if (img->get == NULL) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
Toshihiro Shimizu 890ddd
		return (0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (img->put.any == NULL) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
kusano ca9c32
		"No \"put\" routine setupl; probably can not handle image format");
Toshihiro Shimizu 890ddd
		return (0);
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
kusano ca9c32
   // Casting between 2 function pointers is allowed in and C++ and works as expected
kusano ca9c32
   // if re-casted back.
kusano ca9c32
   // See C 6.3.2.3 (8) and 5.2.10 (6)
Toshihiro Shimizu 890ddd
kusano ca9c32
   return (*get)(img, raster, w, h);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
kusano ca9c32
static int 
kusano ca9c32
setorientation(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	switch (img->orientation) {
kusano ca9c32
		case ORIENTATION_TOPLEFT:
kusano ca9c32
		case ORIENTATION_LEFTTOP:
kusano ca9c32
			if (img->req_orientation == ORIENTATION_TOPRIGHT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_RIGHTTOP)
kusano ca9c32
				return FLIP_HORIZONTALLY;
kusano ca9c32
			else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_RIGHTBOT)
kusano ca9c32
				return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
kusano ca9c32
			else if (img->req_orientation == ORIENTATION_BOTLEFT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_LEFTBOT)
kusano ca9c32
				return FLIP_VERTICALLY;
kusano ca9c32
			else
kusano ca9c32
				return 0;
kusano ca9c32
		case ORIENTATION_TOPRIGHT:
kusano ca9c32
		case ORIENTATION_RIGHTTOP:
kusano ca9c32
			if (img->req_orientation == ORIENTATION_TOPLEFT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_LEFTTOP)
kusano ca9c32
				return FLIP_HORIZONTALLY;
kusano ca9c32
			else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_RIGHTBOT)
kusano ca9c32
				return FLIP_VERTICALLY;
kusano ca9c32
			else if (img->req_orientation == ORIENTATION_BOTLEFT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_LEFTBOT)
kusano ca9c32
				return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
kusano ca9c32
			else
kusano ca9c32
				return 0;
kusano ca9c32
		case ORIENTATION_BOTRIGHT:
kusano ca9c32
		case ORIENTATION_RIGHTBOT:
kusano ca9c32
			if (img->req_orientation == ORIENTATION_TOPLEFT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_LEFTTOP)
kusano ca9c32
				return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
kusano ca9c32
			else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_RIGHTTOP)
kusano ca9c32
				return FLIP_VERTICALLY;
kusano ca9c32
			else if (img->req_orientation == ORIENTATION_BOTLEFT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_LEFTBOT)
kusano ca9c32
				return FLIP_HORIZONTALLY;
kusano ca9c32
			else
kusano ca9c32
				return 0;
kusano ca9c32
		case ORIENTATION_BOTLEFT:
kusano ca9c32
		case ORIENTATION_LEFTBOT:
kusano ca9c32
			if (img->req_orientation == ORIENTATION_TOPLEFT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_LEFTTOP)
kusano ca9c32
				return FLIP_VERTICALLY;
kusano ca9c32
			else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_RIGHTTOP)
kusano ca9c32
				return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
kusano ca9c32
			else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
kusano ca9c32
			    img->req_orientation == ORIENTATION_RIGHTBOT)
kusano ca9c32
				return FLIP_HORIZONTALLY;
kusano ca9c32
			else
kusano ca9c32
				return 0;
kusano ca9c32
		default:	/* NOTREACHED */
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Get an tile-organized image that has
Toshihiro Shimizu 890ddd
 *	PlanarConfiguration contiguous if SamplesPerPixel > 1
Toshihiro Shimizu 890ddd
 * or
Toshihiro Shimizu 890ddd
 *	SamplesPerPixel == 1
kusano ca9c32
 */	
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
gtTileContig(TIFFRGBAImage* img, uint64* raster, uint32 w, uint32 h)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    TIFF* tif = img->tif;
kusano ca9c32
    tileContigRoutine_64 put = (tileContigRoutine_64) img->put.contig;
kusano ca9c32
    uint32 col, row, y, rowstoread;
kusano ca9c32
    tmsize_t pos;
kusano ca9c32
    uint32 tw, th;
kusano ca9c32
    unsigned char* buf;
kusano ca9c32
    int32 fromskew, toskew;
kusano ca9c32
    uint32 nrow;
kusano ca9c32
    int ret = 1, flip;
kusano ca9c32
kusano ca9c32
    buf = (unsigned char*) _TIFFmalloc(TIFFTileSize(tif));
kusano ca9c32
    if (buf == 0) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
Toshihiro Shimizu 890ddd
		return (0);
kusano ca9c32
    }
kusano ca9c32
    _TIFFmemset(buf, 0, TIFFTileSize(tif));
kusano ca9c32
    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
kusano ca9c32
    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
kusano ca9c32
kusano ca9c32
    flip = setorientation(img);
kusano ca9c32
    if (flip & FLIP_VERTICALLY) {
kusano ca9c32
	    y = h - 1;
kusano ca9c32
	    toskew = -(int32)(tw + w);
kusano ca9c32
    }
kusano ca9c32
    else {
kusano ca9c32
	    y = 0;
kusano ca9c32
	    toskew = -(int32)(tw - w);
kusano ca9c32
    }
kusano ca9c32
     
kusano ca9c32
    for (row = 0; row < h; row += nrow)
kusano ca9c32
    {
kusano ca9c32
        rowstoread = th - (row + img->row_offset) % th;
kusano ca9c32
    	nrow = (row + rowstoread > h ? h - row : rowstoread);
kusano ca9c32
	for (col = 0; col < w; col += tw) 
kusano ca9c32
        {
kusano ca9c32
	    if (TIFFReadTile(tif, buf, col+img->col_offset,  
kusano ca9c32
			     row+img->row_offset, 0, 0)==(tmsize_t)(-1) && img->stoponerr)
kusano ca9c32
            {
kusano ca9c32
                ret = 0;
kusano ca9c32
                break;
kusano ca9c32
            }
kusano ca9c32
	    
kusano ca9c32
	    pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);  
kusano ca9c32
kusano ca9c32
    	    if (col + tw > w) 
kusano ca9c32
            {
kusano ca9c32
                /*
Toshihiro Shimizu 890ddd
                 * Tile is clipped horizontally.  Calculate
Toshihiro Shimizu 890ddd
                 * visible portion and skewing factors.
Toshihiro Shimizu 890ddd
                 */
kusano ca9c32
                uint32 npix = w - col;
kusano ca9c32
                fromskew = tw - npix;
kusano ca9c32
                (*put)(img, raster+y*w+col, col, y,
kusano ca9c32
                       npix, nrow, fromskew, toskew + fromskew, buf + pos);
kusano ca9c32
            }
kusano ca9c32
            else 
kusano ca9c32
            {
kusano ca9c32
                (*put)(img, raster+y*w+col, col, y, tw, nrow, 0, toskew, buf + pos);
kusano ca9c32
            }
kusano ca9c32
        }
kusano ca9c32
kusano ca9c32
        y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
kusano ca9c32
    }
kusano ca9c32
    _TIFFfree(buf);
kusano ca9c32
kusano ca9c32
    if (flip & FLIP_HORIZONTALLY) {
kusano ca9c32
	    uint32 line;
kusano ca9c32
kusano ca9c32
	    for (line = 0; line < h; line++) {
kusano ca9c32
		    uint64 *left = raster + (line * w);
kusano ca9c32
		    uint64 *right = left + w - 1;
kusano ca9c32
		    
kusano ca9c32
		    while ( left < right ) {
kusano ca9c32
			    uint64 temp = *left;
kusano ca9c32
			    *left = *right;
kusano ca9c32
			    *right = temp;
kusano ca9c32
			    left++, right--;
kusano ca9c32
		    }
kusano ca9c32
	    }
kusano ca9c32
    }
kusano ca9c32
kusano ca9c32
    return (ret);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Get an tile-organized image that has
Toshihiro Shimizu 890ddd
 *	 SamplesPerPixel > 1
Toshihiro Shimizu 890ddd
 *	 PlanarConfiguration separated
Toshihiro Shimizu 890ddd
 * We assume that all such images are RGB.
kusano ca9c32
 */	
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
gtTileSeparate(TIFFRGBAImage* img, uint64* raster, uint32 w, uint32 h)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	TIFF* tif = img->tif;
kusano ca9c32
	tileSeparateRoutine_64 put = (tileSeparateRoutine_64) img->put.separate;
Toshihiro Shimizu 890ddd
	uint32 col, row, y, rowstoread;
Toshihiro Shimizu 890ddd
	tmsize_t pos;
Toshihiro Shimizu 890ddd
	uint32 tw, th;
kusano ca9c32
	unsigned char* buf;
kusano ca9c32
	unsigned char* p0;
kusano ca9c32
	unsigned char* p1;
kusano ca9c32
	unsigned char* p2;
kusano ca9c32
	unsigned char* pa;
Toshihiro Shimizu 890ddd
	tmsize_t tilesize;
Toshihiro Shimizu 890ddd
	tmsize_t bufsize;
Toshihiro Shimizu 890ddd
	int32 fromskew, toskew;
Toshihiro Shimizu 890ddd
	int alpha = img->alpha;
Toshihiro Shimizu 890ddd
	uint32 nrow;
Toshihiro Shimizu 890ddd
	int ret = 1, flip;
kusano ca9c32
        int colorchannels;
Toshihiro Shimizu 890ddd
kusano ca9c32
	tilesize = TIFFTileSize(tif);  
kusano ca9c32
	bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,tilesize);
Toshihiro Shimizu 890ddd
	if (bufsize == 0) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtTileSeparate");
Toshihiro Shimizu 890ddd
		return (0);
Toshihiro Shimizu 890ddd
	}
kusano ca9c32
	buf = (unsigned char*) _TIFFmalloc(bufsize);
Toshihiro Shimizu 890ddd
	if (buf == 0) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
Toshihiro Shimizu 890ddd
		return (0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	_TIFFmemset(buf, 0, bufsize);
Toshihiro Shimizu 890ddd
	p0 = buf;
Toshihiro Shimizu 890ddd
	p1 = p0 + tilesize;
Toshihiro Shimizu 890ddd
	p2 = p1 + tilesize;
kusano ca9c32
	pa = (alpha?(p2+tilesize):NULL);
Toshihiro Shimizu 890ddd
	TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
Toshihiro Shimizu 890ddd
	TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	flip = setorientation(img);
Toshihiro Shimizu 890ddd
	if (flip & FLIP_VERTICALLY) {
Toshihiro Shimizu 890ddd
		y = h - 1;
Toshihiro Shimizu 890ddd
		toskew = -(int32)(tw + w);
kusano ca9c32
	}
kusano ca9c32
	else {
Toshihiro Shimizu 890ddd
		y = 0;
Toshihiro Shimizu 890ddd
		toskew = -(int32)(tw - w);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
kusano ca9c32
        switch( img->photometric )
kusano ca9c32
        {
kusano ca9c32
          case PHOTOMETRIC_MINISWHITE:
kusano ca9c32
          case PHOTOMETRIC_MINISBLACK:
kusano ca9c32
          case PHOTOMETRIC_PALETTE:
kusano ca9c32
            colorchannels = 1;
kusano ca9c32
            p2 = p1 = p0;
kusano ca9c32
            break;
Toshihiro Shimizu 890ddd
kusano ca9c32
          default:
kusano ca9c32
            colorchannels = 3;
kusano ca9c32
            break;
kusano ca9c32
        }
Toshihiro Shimizu 890ddd
kusano ca9c32
	for (row = 0; row < h; row += nrow)
kusano ca9c32
	{
Toshihiro Shimizu 890ddd
		rowstoread = th - (row + img->row_offset) % th;
Toshihiro Shimizu 890ddd
		nrow = (row + rowstoread > h ? h - row : rowstoread);
kusano ca9c32
		for (col = 0; col < w; col += tw)
kusano ca9c32
		{
kusano ca9c32
			if (TIFFReadTile(tif, p0, col+img->col_offset,  
kusano ca9c32
			    row+img->row_offset,0,0)==(tmsize_t)(-1) && img->stoponerr)
kusano ca9c32
			{
Toshihiro Shimizu 890ddd
				ret = 0;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
kusano ca9c32
			if (colorchannels > 1 
kusano ca9c32
                            && TIFFReadTile(tif, p1, col+img->col_offset,  
kusano ca9c32
                                            row+img->row_offset,0,1) == (tmsize_t)(-1) 
kusano ca9c32
                            && img->stoponerr)
kusano ca9c32
			{
Toshihiro Shimizu 890ddd
				ret = 0;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
kusano ca9c32
			if (colorchannels > 1 
kusano ca9c32
                            && TIFFReadTile(tif, p2, col+img->col_offset,  
kusano ca9c32
                                            row+img->row_offset,0,2) == (tmsize_t)(-1) 
kusano ca9c32
                            && img->stoponerr)
kusano ca9c32
			{
Toshihiro Shimizu 890ddd
				ret = 0;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
kusano ca9c32
			if (alpha
kusano ca9c32
                            && TIFFReadTile(tif,pa,col+img->col_offset,  
kusano ca9c32
                                            row+img->row_offset,0,colorchannels) == (tmsize_t)(-1) 
kusano ca9c32
                            && img->stoponerr)
kusano ca9c32
                        {
kusano ca9c32
                            ret = 0;
kusano ca9c32
                            break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
kusano ca9c32
			pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);  
Toshihiro Shimizu 890ddd
kusano ca9c32
			if (col + tw > w)
kusano ca9c32
			{
Toshihiro Shimizu 890ddd
				/*
Toshihiro Shimizu 890ddd
				 * Tile is clipped horizontally.  Calculate
Toshihiro Shimizu 890ddd
				 * visible portion and skewing factors.
Toshihiro Shimizu 890ddd
				 */
Toshihiro Shimizu 890ddd
				uint32 npix = w - col;
Toshihiro Shimizu 890ddd
				fromskew = tw - npix;
kusano ca9c32
				(*put)(img, raster+y*w+col, col, y,
kusano ca9c32
				    npix, nrow, fromskew, toskew + fromskew,
kusano ca9c32
				    p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
Toshihiro Shimizu 890ddd
			} else {
kusano ca9c32
				(*put)(img, raster+y*w+col, col, y,
kusano ca9c32
				    tw, nrow, 0, toskew, p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
kusano ca9c32
		y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (flip & FLIP_HORIZONTALLY) {
Toshihiro Shimizu 890ddd
		uint32 line;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (line = 0; line < h; line++) {
Toshihiro Shimizu 890ddd
			uint64 *left = raster + (line * w);
Toshihiro Shimizu 890ddd
			uint64 *right = left + w - 1;
Toshihiro Shimizu 890ddd
kusano ca9c32
			while ( left < right ) {
Toshihiro Shimizu 890ddd
				uint64 temp = *left;
Toshihiro Shimizu 890ddd
				*left = *right;
Toshihiro Shimizu 890ddd
				*right = temp;
Toshihiro Shimizu 890ddd
				left++, right--;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	_TIFFfree(buf);
Toshihiro Shimizu 890ddd
	return (ret);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Get a strip-organized image that has
Toshihiro Shimizu 890ddd
 *	PlanarConfiguration contiguous if SamplesPerPixel > 1
Toshihiro Shimizu 890ddd
 * or
Toshihiro Shimizu 890ddd
 *	SamplesPerPixel == 1
kusano ca9c32
 */	
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
gtStripContig(TIFFRGBAImage* img, uint64* raster, uint32 w, uint32 h)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	TIFF* tif = img->tif;
kusano ca9c32
	tileContigRoutine_64 put = (tileContigRoutine_64) img->put.contig;
Toshihiro Shimizu 890ddd
	uint32 row, y, nrow, nrowsub, rowstoread;
Toshihiro Shimizu 890ddd
	tmsize_t pos;
kusano ca9c32
	unsigned char* buf;
Toshihiro Shimizu 890ddd
	uint32 rowsperstrip;
kusano ca9c32
	uint16 subsamplinghor,subsamplingver;
Toshihiro Shimizu 890ddd
	uint32 imagewidth = img->width;
Toshihiro Shimizu 890ddd
	tmsize_t scanline;
Toshihiro Shimizu 890ddd
	int32 fromskew, toskew;
Toshihiro Shimizu 890ddd
	int ret = 1, flip;
Toshihiro Shimizu 890ddd
kusano ca9c32
	buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
Toshihiro Shimizu 890ddd
	if (buf == 0) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
Toshihiro Shimizu 890ddd
		return (0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	_TIFFmemset(buf, 0, TIFFStripSize(tif));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	flip = setorientation(img);
Toshihiro Shimizu 890ddd
	if (flip & FLIP_VERTICALLY) {
Toshihiro Shimizu 890ddd
		y = h - 1;
Toshihiro Shimizu 890ddd
		toskew = -(int32)(w + w);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		y = 0;
Toshihiro Shimizu 890ddd
		toskew = -(int32)(w - w);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
Toshihiro Shimizu 890ddd
	scanline = TIFFScanlineSize(tif);
Toshihiro Shimizu 890ddd
	fromskew = (w < imagewidth ? imagewidth - w : 0);
kusano ca9c32
	for (row = 0; row < h; row += nrow)
kusano ca9c32
	{
Toshihiro Shimizu 890ddd
		rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
Toshihiro Shimizu 890ddd
		nrow = (row + rowstoread > h ? h - row : rowstoread);
Toshihiro Shimizu 890ddd
		nrowsub = nrow;
kusano ca9c32
		if ((nrowsub%subsamplingver)!=0)
kusano ca9c32
			nrowsub+=subsamplingver-nrowsub%subsamplingver;
Toshihiro Shimizu 890ddd
		if (TIFFReadEncodedStrip(tif,
kusano ca9c32
		    TIFFComputeStrip(tif,row+img->row_offset, 0),
kusano ca9c32
		    buf,
kusano ca9c32
		    ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline)==(tmsize_t)(-1)
kusano ca9c32
		    && img->stoponerr)
kusano ca9c32
		{
Toshihiro Shimizu 890ddd
			ret = 0;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		pos = ((row + img->row_offset) % rowsperstrip) * scanline;
kusano ca9c32
		(*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
kusano ca9c32
		y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (flip & FLIP_HORIZONTALLY) {
Toshihiro Shimizu 890ddd
		uint32 line;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (line = 0; line < h; line++) {
Toshihiro Shimizu 890ddd
			uint64 *left = raster + (line * w);
Toshihiro Shimizu 890ddd
			uint64 *right = left + w - 1;
Toshihiro Shimizu 890ddd
kusano ca9c32
			while ( left < right ) {
Toshihiro Shimizu 890ddd
				uint64 temp = *left;
Toshihiro Shimizu 890ddd
				*left = *right;
Toshihiro Shimizu 890ddd
				*right = temp;
Toshihiro Shimizu 890ddd
				left++, right--;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	_TIFFfree(buf);
Toshihiro Shimizu 890ddd
	return (ret);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Get a strip-organized image with
Toshihiro Shimizu 890ddd
 *	 SamplesPerPixel > 1
Toshihiro Shimizu 890ddd
 *	 PlanarConfiguration separated
Toshihiro Shimizu 890ddd
 * We assume that all such images are RGB.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
gtStripSeparate(TIFFRGBAImage* img, uint64* raster, uint32 w, uint32 h)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	TIFF* tif = img->tif;
kusano ca9c32
	tileSeparateRoutine_64 put = (tileSeparateRoutine_64) img->put.separate;
Toshihiro Shimizu 890ddd
	unsigned char *buf;
Toshihiro Shimizu 890ddd
	unsigned char *p0, *p1, *p2, *pa;
Toshihiro Shimizu 890ddd
	uint32 row, y, nrow, rowstoread;
Toshihiro Shimizu 890ddd
	tmsize_t pos;
Toshihiro Shimizu 890ddd
	tmsize_t scanline;
Toshihiro Shimizu 890ddd
	uint32 rowsperstrip, offset_row;
Toshihiro Shimizu 890ddd
	uint32 imagewidth = img->width;
Toshihiro Shimizu 890ddd
	tmsize_t stripsize;
Toshihiro Shimizu 890ddd
	tmsize_t bufsize;
Toshihiro Shimizu 890ddd
	int32 fromskew, toskew;
Toshihiro Shimizu 890ddd
	int alpha = img->alpha;
Toshihiro Shimizu 890ddd
	int ret = 1, flip, colorchannels;
Toshihiro Shimizu 890ddd
kusano ca9c32
	stripsize = TIFFStripSize(tif);  
kusano ca9c32
	bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,stripsize);
Toshihiro Shimizu 890ddd
	if (bufsize == 0) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtStripSeparate");
Toshihiro Shimizu 890ddd
		return (0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	p0 = buf = (unsigned char *)_TIFFmalloc(bufsize);
Toshihiro Shimizu 890ddd
	if (buf == 0) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
Toshihiro Shimizu 890ddd
		return (0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	_TIFFmemset(buf, 0, bufsize);
Toshihiro Shimizu 890ddd
	p1 = p0 + stripsize;
Toshihiro Shimizu 890ddd
	p2 = p1 + stripsize;
kusano ca9c32
	pa = (alpha?(p2+stripsize):NULL);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	flip = setorientation(img);
Toshihiro Shimizu 890ddd
	if (flip & FLIP_VERTICALLY) {
Toshihiro Shimizu 890ddd
		y = h - 1;
Toshihiro Shimizu 890ddd
		toskew = -(int32)(w + w);
kusano ca9c32
	}
kusano ca9c32
	else {
Toshihiro Shimizu 890ddd
		y = 0;
Toshihiro Shimizu 890ddd
		toskew = -(int32)(w - w);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
kusano ca9c32
        switch( img->photometric )
kusano ca9c32
        {
kusano ca9c32
          case PHOTOMETRIC_MINISWHITE:
kusano ca9c32
          case PHOTOMETRIC_MINISBLACK:
kusano ca9c32
          case PHOTOMETRIC_PALETTE:
kusano ca9c32
            colorchannels = 1;
kusano ca9c32
            p2 = p1 = p0;
kusano ca9c32
            break;
Toshihiro Shimizu 890ddd
kusano ca9c32
          default:
kusano ca9c32
            colorchannels = 3;
kusano ca9c32
            break;
kusano ca9c32
        }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
kusano ca9c32
	scanline = TIFFScanlineSize(tif);  
Toshihiro Shimizu 890ddd
	fromskew = (w < imagewidth ? imagewidth - w : 0);
kusano ca9c32
	for (row = 0; row < h; row += nrow)
kusano ca9c32
	{
Toshihiro Shimizu 890ddd
		rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
Toshihiro Shimizu 890ddd
		nrow = (row + rowstoread > h ? h - row : rowstoread);
Toshihiro Shimizu 890ddd
		offset_row = row + img->row_offset;
Toshihiro Shimizu 890ddd
		if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
kusano ca9c32
		    p0, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
kusano ca9c32
		    && img->stoponerr)
kusano ca9c32
		{
Toshihiro Shimizu 890ddd
			ret = 0;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
kusano ca9c32
		if (colorchannels > 1 
kusano ca9c32
                    && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
kusano ca9c32
                                            p1, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
kusano ca9c32
		    && img->stoponerr)
kusano ca9c32
		{
Toshihiro Shimizu 890ddd
			ret = 0;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
kusano ca9c32
		if (colorchannels > 1 
kusano ca9c32
                    && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
kusano ca9c32
                                            p2, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
kusano ca9c32
		    && img->stoponerr)
kusano ca9c32
		{
Toshihiro Shimizu 890ddd
			ret = 0;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
kusano ca9c32
		if (alpha)
kusano ca9c32
		{
Toshihiro Shimizu 890ddd
			if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, colorchannels),
kusano ca9c32
			    pa, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
kusano ca9c32
			    && img->stoponerr)
kusano ca9c32
			{
Toshihiro Shimizu 890ddd
				ret = 0;
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		pos = ((row + img->row_offset) % rowsperstrip) * scanline;
kusano ca9c32
		(*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, p0 + pos, p1 + pos,
kusano ca9c32
		    p2 + pos, (alpha?(pa+pos):NULL));
kusano ca9c32
		y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (flip & FLIP_HORIZONTALLY) {
Toshihiro Shimizu 890ddd
		uint32 line;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (line = 0; line < h; line++) {
Toshihiro Shimizu 890ddd
			uint64 *left = raster + (line * w);
Toshihiro Shimizu 890ddd
			uint64 *right = left + w - 1;
Toshihiro Shimizu 890ddd
kusano ca9c32
			while ( left < right ) {
Toshihiro Shimizu 890ddd
				uint64 temp = *left;
Toshihiro Shimizu 890ddd
				*left = *right;
Toshihiro Shimizu 890ddd
				*right = temp;
Toshihiro Shimizu 890ddd
				left++, right--;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	_TIFFfree(buf);
Toshihiro Shimizu 890ddd
	return (ret);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * The following routines move decoded data returned
Toshihiro Shimizu 890ddd
 * from the TIFF library into rasters filled with packed
Toshihiro Shimizu 890ddd
 * ABGR pixels (i.e. suitable for passing to lrecwrite.)
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * The routines have been created according to the most
Toshihiro Shimizu 890ddd
 * important cases and optimized.  PickContigCase and
Toshihiro Shimizu 890ddd
 * PickSeparateCase analyze the parameters and select
Toshihiro Shimizu 890ddd
 * the appropriate "get" and "put" routine to use.
Toshihiro Shimizu 890ddd
 */
kusano ca9c32
#define	REPEAT8(op)	REPEAT4(op); REPEAT4(op)
kusano ca9c32
#define	REPEAT4(op)	REPEAT2(op); REPEAT2(op)
kusano ca9c32
#define	REPEAT2(op)	op; op
kusano ca9c32
#define	CASE8(x,op)			\
kusano ca9c32
    switch (x) {			\
kusano ca9c32
    case 7: op; case 6: op; case 5: op;	\
kusano ca9c32
    case 4: op; case 3: op; case 2: op;	\
kusano ca9c32
    case 1: op;				\
kusano ca9c32
    }
kusano ca9c32
#define	CASE4(x,op)	switch (x) { case 3: op; case 2: op; case 1: op; }
kusano ca9c32
#define	NOP
kusano ca9c32
kusano ca9c32
#define	UNROLL8(w, op1, op2) {		\
kusano ca9c32
    uint32 _x;				\
kusano ca9c32
    for (_x = w; _x >= 8; _x -= 8) {	\
kusano ca9c32
	op1;				\
kusano ca9c32
	REPEAT8(op2);			\
kusano ca9c32
    }					\
kusano ca9c32
    if (_x > 0) {			\
kusano ca9c32
	op1;				\
kusano ca9c32
	CASE8(_x,op2);			\
kusano ca9c32
    }					\
kusano ca9c32
}
kusano ca9c32
#define	UNROLL4(w, op1, op2) {		\
kusano ca9c32
    uint64 _x;				\
kusano ca9c32
    for (_x = w; _x >= 4; _x -= 4) {	\
kusano ca9c32
	op1;				\
kusano ca9c32
	REPEAT4(op2);			\
kusano ca9c32
    }					\
kusano ca9c32
    if (_x > 0) {			\
kusano ca9c32
	op1;				\
kusano ca9c32
	CASE4(_x,op2);			\
kusano ca9c32
    }					\
kusano ca9c32
}
kusano ca9c32
#define	UNROLL2(w, op1, op2) {		\
kusano ca9c32
    uint64 _x;				\
kusano ca9c32
    for (_x = w; _x >= 2; _x -= 2) {	\
kusano ca9c32
	op1;				\
kusano ca9c32
	REPEAT2(op2);			\
kusano ca9c32
    }					\
kusano ca9c32
    if (_x) {				\
kusano ca9c32
	op1;				\
kusano ca9c32
	op2;				\
kusano ca9c32
    }					\
kusano ca9c32
}
kusano ca9c32
    
kusano ca9c32
#define	SKEW(r,g,b,skew)	{ r += skew; g += skew; b += skew; }
kusano ca9c32
#define	SKEW4(r,g,b,a,skew)	{ r += skew; g += skew; b += skew; a+= skew; }
kusano ca9c32
kusano ca9c32
#define A1 (((uint64)0xffffL)<<48)
kusano ca9c32
#define	PACK_32(r,g,b)	\
kusano ca9c32
	((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|(0xff<<24))
kusano ca9c32
#define	PACK(r,g,b)	\
kusano ca9c32
	((uint64)(r)|((uint64)(g)<<16)|((uint64)(b)<<32)|A1)
kusano ca9c32
#define	PACK4(r,g,b,a)	\
kusano ca9c32
	((uint64)(r)|((uint64)(g)<<16)|((uint64)(b)<<32)|((uint64)(a)<<48))
kusano ca9c32
#define W2B(v) (((v)>>16)&0xffff)
Toshihiro Shimizu 890ddd
/* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
kusano ca9c32
#define	PACKW(r,g,b)	\
kusano ca9c32
	((uint64)W2B(r)|((uint64)W2B(g)<<16)|((uint64)W2B(b)<<32)|A1)
kusano ca9c32
#define	PACKW4(r,g,b,a)	\
kusano ca9c32
	((uint64)W2B(r)|((uint64)W2B(g)<<16)|((uint64)W2B(b)<<32)|((uint64)W2B(a)<<48))
kusano ca9c32
kusano ca9c32
#define	DECLAREContigPutFunc(name) \
kusano ca9c32
static void name(\
kusano ca9c32
    TIFFRGBAImage* img, \
kusano ca9c32
    uint64* cp, \
kusano ca9c32
    uint32 x, uint32 y, \
kusano ca9c32
    uint32 w, uint32 h, \
kusano ca9c32
    int32 fromskew, int32 toskew, \
kusano ca9c32
    unsigned char* pp \
kusano ca9c32
)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit greyscale => colormap/RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putgreytile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    int samplesperpixel = img->samplesperpixel;
kusano ca9c32
    uint64** BWmap = (uint64**) img->BWmap;
kusano ca9c32
kusano ca9c32
    (void) y;
kusano ca9c32
    while (h-- > 0) {
kusano ca9c32
	for (x = w; x-- > 0;)
kusano ca9c32
        {
kusano ca9c32
	    *cp++ = BWmap[*pp][0];
kusano ca9c32
            pp += samplesperpixel;
kusano ca9c32
        }
kusano ca9c32
	cp += toskew;
kusano ca9c32
	pp += fromskew;
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit greyscale with associated alpha => colormap/RGBA
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putagreytile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    int samplesperpixel = img->samplesperpixel;
kusano ca9c32
    uint64** BWmap = (uint64**) img->BWmap;
kusano ca9c32
kusano ca9c32
    (void) y;
kusano ca9c32
    while (h-- > 0) {
kusano ca9c32
	for (x = w; x-- > 0;)
kusano ca9c32
        {
kusano ca9c32
            *cp++ = BWmap[*pp][0] & ((uint64)*(pp+1) << 48 | ~A1);
kusano ca9c32
            pp += samplesperpixel;
kusano ca9c32
        }
kusano ca9c32
	cp += toskew;
kusano ca9c32
	pp += fromskew;
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 16-bit greyscale => colormap/RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(put16bitbwtile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    int samplesperpixel = img->samplesperpixel;
kusano ca9c32
    uint64** BWmap = (uint64**) img->BWmap;
kusano ca9c32
kusano ca9c32
    (void) y;
kusano ca9c32
    while (h-- > 0) {
kusano ca9c32
        uint16 *wp = (uint16 *) pp;
kusano ca9c32
kusano ca9c32
	for (x = w; x-- > 0;)
kusano ca9c32
        {
kusano ca9c32
            /* use high order byte of 16bit value */
kusano ca9c32
kusano ca9c32
	    *cp++ = BWmap[*wp >> 8][0];
kusano ca9c32
            pp += 2 * samplesperpixel;
kusano ca9c32
            wp += samplesperpixel;
kusano ca9c32
        }
kusano ca9c32
	cp += toskew;
kusano ca9c32
	pp += fromskew;
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 1-bit bilevel => colormap/RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(put1bitbwtile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    uint64** BWmap = (uint64**) img->BWmap;
kusano ca9c32
kusano ca9c32
    (void) x; (void) y;
kusano ca9c32
    fromskew /= 8;
kusano ca9c32
    while (h-- > 0) {
kusano ca9c32
	uint64* bw;
kusano ca9c32
	UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
kusano ca9c32
	cp += toskew;
kusano ca9c32
	pp += fromskew;
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 2-bit greyscale => colormap/RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(put2bitbwtile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    uint64** BWmap = (uint64**) img->BWmap;
kusano ca9c32
kusano ca9c32
    (void) x; (void) y;
kusano ca9c32
    fromskew /= 4;
kusano ca9c32
    while (h-- > 0) {
kusano ca9c32
	uint64* bw;
kusano ca9c32
	UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
kusano ca9c32
	cp += toskew;
kusano ca9c32
	pp += fromskew;
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 4-bit greyscale => colormap/RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(put4bitbwtile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    uint64** BWmap = (uint64**) img->BWmap;
kusano ca9c32
kusano ca9c32
    (void) x; (void) y;
kusano ca9c32
    fromskew /= 2;
kusano ca9c32
    while (h-- > 0) {
kusano ca9c32
	uint64* bw;
kusano ca9c32
	UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
kusano ca9c32
	cp += toskew;
kusano ca9c32
	pp += fromskew;
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed samples, no Map => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putRGBcontig8bittile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    int samplesperpixel = img->samplesperpixel;
kusano ca9c32
kusano ca9c32
    (void) x; (void) y;
kusano ca9c32
    fromskew *= samplesperpixel;
kusano ca9c32
    while (h-- > 0) {
kusano ca9c32
	UNROLL8(w, NOP,
kusano ca9c32
	    *cp++ = PACK(pp[0] << 8, pp[1] << 8, pp[2] << 8);
kusano ca9c32
	    pp += samplesperpixel);
kusano ca9c32
	cp += toskew;
kusano ca9c32
	pp += fromskew;
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed samples => RGBA
Toshihiro Shimizu 890ddd
 * (known to have Map == NULL)
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putRGBAAcontig8bittile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    int samplesperpixel = img->samplesperpixel;
kusano ca9c32
kusano ca9c32
    (void) x; (void) y;
kusano ca9c32
    fromskew *= samplesperpixel;
kusano ca9c32
    while (h-- > 0) {
kusano ca9c32
	UNROLL8(w, NOP,
kusano ca9c32
	    *cp++ = PACK4(pp[0] << 8, pp[1] << 8, pp[2] << 8, pp[3] << 8);
kusano ca9c32
	    pp += samplesperpixel);
kusano ca9c32
	cp += toskew;
kusano ca9c32
	pp += fromskew;
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 16-bit packed samples => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putRGBcontig16bittile)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int samplesperpixel = img->samplesperpixel;
Toshihiro Shimizu 890ddd
	uint16 *wp = (uint16 *)pp;
kusano ca9c32
	(void) y;
Toshihiro Shimizu 890ddd
	fromskew *= samplesperpixel;
Toshihiro Shimizu 890ddd
	while (h-- > 0) {
Toshihiro Shimizu 890ddd
		for (x = w; x-- > 0;) {
Toshihiro Shimizu 890ddd
			*cp++ = PACK(wp[0], wp[1], wp[2]);
Toshihiro Shimizu 890ddd
			wp += samplesperpixel;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		cp += toskew;
Toshihiro Shimizu 890ddd
		wp += fromskew;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 16-bit packed samples => RGBA w/ associated alpha
Toshihiro Shimizu 890ddd
 * (known to have Map == NULL)
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putRGBAAcontig16bittile)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int samplesperpixel = img->samplesperpixel;
Toshihiro Shimizu 890ddd
	uint16 *wp = (uint16 *)pp;
kusano ca9c32
	(void) y;
Toshihiro Shimizu 890ddd
	fromskew *= samplesperpixel;
Toshihiro Shimizu 890ddd
	while (h-- > 0) {
Toshihiro Shimizu 890ddd
		for (x = w; x-- > 0;) {
Toshihiro Shimizu 890ddd
			*cp++ = PACK4(wp[0], wp[1], wp[2], wp[3]);
Toshihiro Shimizu 890ddd
			wp += samplesperpixel;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		cp += toskew;
Toshihiro Shimizu 890ddd
		wp += fromskew;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed CMYK samples w/o Map => RGB
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * NB: The conversion of CMYK->RGB is *very* crude.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    int samplesperpixel = img->samplesperpixel;
kusano ca9c32
    uint16 r, g, b, k;
kusano ca9c32
kusano ca9c32
    (void) x; (void) y;
kusano ca9c32
    fromskew *= samplesperpixel;
kusano ca9c32
    while (h-- > 0) {
kusano ca9c32
	UNROLL8(w, NOP,
kusano ca9c32
	    k = 255 - pp[3];
kusano ca9c32
	    r = (k*(255-pp[0]));
kusano ca9c32
	    g = (k*(255-pp[1]));
kusano ca9c32
	    b = (k*(255-pp[2]));
kusano ca9c32
	    *cp++ = PACK(r, g, b);
kusano ca9c32
	    pp += samplesperpixel);
kusano ca9c32
	cp += toskew;
kusano ca9c32
	pp += fromskew;
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed CMYK samples w/Map => RGB
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * NB: The conversion of CMYK->RGB is *very* crude.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    int samplesperpixel = img->samplesperpixel;
kusano ca9c32
    TIFFRGBValue* Map = img->Map;
kusano ca9c32
    uint16 r, g, b, k;
kusano ca9c32
kusano ca9c32
    (void) y;
kusano ca9c32
    fromskew *= samplesperpixel;
kusano ca9c32
    while (h-- > 0) {
kusano ca9c32
	for (x = w; x-- > 0;) {
kusano ca9c32
	    k = 255 - pp[3];
kusano ca9c32
	    r = (k*(255-pp[0]))/255;
kusano ca9c32
	    g = (k*(255-pp[1]))/255;
kusano ca9c32
	    b = (k*(255-pp[2]))/255;
kusano ca9c32
	    *cp++ = PACK(Map[r] << 8, Map[g] << 8, Map[b] << 8);
kusano ca9c32
	    pp += samplesperpixel;
kusano ca9c32
	}
kusano ca9c32
	pp += fromskew;
kusano ca9c32
	cp += toskew;
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
kusano ca9c32
#define	DECLARESepPutFunc(name) \
kusano ca9c32
static void name(\
kusano ca9c32
    TIFFRGBAImage* img,\
kusano ca9c32
    uint64* cp,\
kusano ca9c32
    uint32 x, uint32 y, \
kusano ca9c32
    uint32 w, uint32 h,\
kusano ca9c32
    int32 fromskew, int32 toskew,\
kusano ca9c32
    unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a\
kusano ca9c32
)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit unpacked samples => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLARESepPutFunc(putRGBseparate8bittile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    (void) img; (void) x; (void) y; (void) a;
kusano ca9c32
    while (h-- > 0) {
kusano ca9c32
	UNROLL8(w, NOP, *cp++ = PACK(*r++ << 8, *g++ << 8, *b++ << 8));
kusano ca9c32
	SKEW(r, g, b, fromskew);
kusano ca9c32
	cp += toskew;
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit unpacked samples => RGBA w/ associated alpha
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLARESepPutFunc(putRGBAAseparate8bittile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	(void) img; (void) x; (void) y; 
Toshihiro Shimizu 890ddd
	while (h-- > 0) {
Toshihiro Shimizu 890ddd
		UNROLL8(w, NOP, *cp++ = PACK4(*r++ << 8, *g++ << 8, *b++ << 8, *a++ << 8));
Toshihiro Shimizu 890ddd
		SKEW4(r, g, b, a, fromskew);
Toshihiro Shimizu 890ddd
		cp += toskew;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit unpacked CMYK samples => RGBA
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLARESepPutFunc(putCMYKseparate8bittile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	(void) img; (void) y;
Toshihiro Shimizu 890ddd
	while (h-- > 0) {
Toshihiro Shimizu 890ddd
		uint32 rv, gv, bv, kv;
Toshihiro Shimizu 890ddd
		for (x = w; x-- > 0;) {
Toshihiro Shimizu 890ddd
			kv = 255 - *a++;
kusano ca9c32
			rv = (kv*(255-*r++));
kusano ca9c32
			gv = (kv*(255-*g++));
kusano ca9c32
			bv = (kv*(255-*b++));
kusano ca9c32
			*cp++ = PACK4(rv,gv,bv,65535);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		SKEW4(r, g, b, a, fromskew);
Toshihiro Shimizu 890ddd
		cp += toskew;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 16-bit unpacked samples => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLARESepPutFunc(putRGBseparate16bittile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	uint16 *wr = (uint16*) r;
kusano ca9c32
	uint16 *wg = (uint16*) g;
kusano ca9c32
	uint16 *wb = (uint16*) b;
kusano ca9c32
	(void) img; (void) y; (void) a;
Toshihiro Shimizu 890ddd
	while (h-- > 0) {
Toshihiro Shimizu 890ddd
		for (x = 0; x < w; x++)
Toshihiro Shimizu 890ddd
			*cp++ = PACK(*wr++, *wg++, *wb++);
Toshihiro Shimizu 890ddd
		SKEW(wr, wg, wb, fromskew);
Toshihiro Shimizu 890ddd
		cp += toskew;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 16-bit unpacked samples => RGBA w/ associated alpha
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLARESepPutFunc(putRGBAAseparate16bittile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	uint16 *wr = (uint16*) r;
kusano ca9c32
	uint16 *wg = (uint16*) g;
kusano ca9c32
	uint16 *wb = (uint16*) b;
kusano ca9c32
	uint16 *wa = (uint16*) a;
kusano ca9c32
	(void) img; (void) y;
Toshihiro Shimizu 890ddd
	while (h-- > 0) {
Toshihiro Shimizu 890ddd
		for (x = 0; x < w; x++)
Toshihiro Shimizu 890ddd
			*cp++ = PACK4(*wr++, *wg++, *wb++, *wa++);
Toshihiro Shimizu 890ddd
		SKEW4(wr, wg, wb, wa, fromskew);
Toshihiro Shimizu 890ddd
		cp += toskew;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed CIE L*a*b 1976 samples => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putcontig8bitCIELab)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	float X, Y, Z;
Toshihiro Shimizu 890ddd
	uint32 r, g, b;
kusano ca9c32
	(void) y;
Toshihiro Shimizu 890ddd
	fromskew *= 3;
Toshihiro Shimizu 890ddd
	while (h-- > 0) {
Toshihiro Shimizu 890ddd
		for (x = w; x-- > 0;) {
Toshihiro Shimizu 890ddd
			TIFFCIELabToXYZ(img->cielab,
kusano ca9c32
					(unsigned char)pp[0],
kusano ca9c32
					(signed char)pp[1],
kusano ca9c32
					(signed char)pp[2],
kusano ca9c32
					&X, &Y, &Z);
Toshihiro Shimizu 890ddd
			TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
Toshihiro Shimizu 890ddd
			*cp++ = PACK(r << 8, g << 8, b << 8);
Toshihiro Shimizu 890ddd
			pp += 3;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		cp += toskew;
Toshihiro Shimizu 890ddd
		pp += fromskew;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * YCbCr -> RGB conversion and packing routines.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
kusano ca9c32
#define	YCbCrtoRGB(dst, Y) {						\
kusano ca9c32
	uint32 r, g, b;							\
kusano ca9c32
	TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b);		\
kusano ca9c32
	dst = PACK(((uint64)r) << 8, ((uint64)g) << 8, ((uint64)b) << 8);						\
kusano ca9c32
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    uint64* cp1 = cp+w+toskew;
kusano ca9c32
    uint64* cp2 = cp1+w+toskew;
kusano ca9c32
    uint64* cp3 = cp2+w+toskew;
kusano ca9c32
    int32 incr = 3*w+4*toskew;
kusano ca9c32
kusano ca9c32
    (void) y;
kusano ca9c32
    /* adjust fromskew */
kusano ca9c32
    fromskew = (fromskew * 18) / 4;
kusano ca9c32
    if ((h & 3) == 0 && (w & 3) == 0) {				        
kusano ca9c32
        for (; h >= 4; h -= 4) {
kusano ca9c32
            x = w>>2;
kusano ca9c32
            do {
kusano ca9c32
                int32 Cb = pp[16];
kusano ca9c32
                int32 Cr = pp[17];
kusano ca9c32
kusano ca9c32
                YCbCrtoRGB(cp [0], pp[ 0]);
kusano ca9c32
                YCbCrtoRGB(cp [1], pp[ 1]);
kusano ca9c32
                YCbCrtoRGB(cp [2], pp[ 2]);
kusano ca9c32
                YCbCrtoRGB(cp [3], pp[ 3]);
kusano ca9c32
                YCbCrtoRGB(cp1[0], pp[ 4]);
kusano ca9c32
                YCbCrtoRGB(cp1[1], pp[ 5]);
kusano ca9c32
                YCbCrtoRGB(cp1[2], pp[ 6]);
kusano ca9c32
                YCbCrtoRGB(cp1[3], pp[ 7]);
kusano ca9c32
                YCbCrtoRGB(cp2[0], pp[ 8]);
kusano ca9c32
                YCbCrtoRGB(cp2[1], pp[ 9]);
kusano ca9c32
                YCbCrtoRGB(cp2[2], pp[10]);
kusano ca9c32
                YCbCrtoRGB(cp2[3], pp[11]);
kusano ca9c32
                YCbCrtoRGB(cp3[0], pp[12]);
kusano ca9c32
                YCbCrtoRGB(cp3[1], pp[13]);
kusano ca9c32
                YCbCrtoRGB(cp3[2], pp[14]);
kusano ca9c32
                YCbCrtoRGB(cp3[3], pp[15]);
kusano ca9c32
kusano ca9c32
                cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
kusano ca9c32
                pp += 18;
kusano ca9c32
            } while (--x);
kusano ca9c32
            cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
kusano ca9c32
            pp += fromskew;
kusano ca9c32
        }
kusano ca9c32
    } else {
kusano ca9c32
        while (h > 0) {
kusano ca9c32
            for (x = w; x > 0;) {
kusano ca9c32
                int32 Cb = pp[16];
kusano ca9c32
                int32 Cr = pp[17];
kusano ca9c32
                switch (x) {
kusano ca9c32
                default:
kusano ca9c32
                    switch (h) {
kusano ca9c32
                    default: YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
kusano ca9c32
                    case 3:  YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
kusano ca9c32
                    case 2:  YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
kusano ca9c32
                    case 1:  YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
kusano ca9c32
                    }                                    /* FALLTHROUGH */
kusano ca9c32
                case 3:
kusano ca9c32
                    switch (h) {
kusano ca9c32
                    default: YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
kusano ca9c32
                    case 3:  YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
kusano ca9c32
                    case 2:  YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
kusano ca9c32
                    case 1:  YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
kusano ca9c32
                    }                                    /* FALLTHROUGH */
kusano ca9c32
                case 2:
kusano ca9c32
                    switch (h) {
kusano ca9c32
                    default: YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
kusano ca9c32
                    case 3:  YCbCrtoRGB(cp2[1], pp[ 9]); /* FALLTHROUGH */
kusano ca9c32
                    case 2:  YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
kusano ca9c32
                    case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
kusano ca9c32
                    }                                    /* FALLTHROUGH */
kusano ca9c32
                case 1:
kusano ca9c32
                    switch (h) {
kusano ca9c32
                    default: YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
kusano ca9c32
                    case 3:  YCbCrtoRGB(cp2[0], pp[ 8]); /* FALLTHROUGH */
kusano ca9c32
                    case 2:  YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
kusano ca9c32
                    case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
kusano ca9c32
                    }                                    /* FALLTHROUGH */
kusano ca9c32
                }
kusano ca9c32
                if (x < 4) {
kusano ca9c32
                    cp += x; cp1 += x; cp2 += x; cp3 += x;
kusano ca9c32
                    x = 0;
kusano ca9c32
                }
kusano ca9c32
                else {
kusano ca9c32
                    cp += 4; cp1 += 4; cp2 += 4; cp3 += 4;
kusano ca9c32
                    x -= 4;
kusano ca9c32
                }
kusano ca9c32
                pp += 18;
kusano ca9c32
            }
kusano ca9c32
            if (h <= 4)
kusano ca9c32
                break;
kusano ca9c32
            h -= 4;
kusano ca9c32
            cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
kusano ca9c32
            pp += fromskew;
kusano ca9c32
        }
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    uint64* cp1 = cp+w+toskew;
kusano ca9c32
    int32 incr = 2*toskew+w;
kusano ca9c32
kusano ca9c32
    (void) y;
kusano ca9c32
    fromskew = (fromskew * 10) / 4;
kusano ca9c32
    if ((h & 3) == 0 && (w & 1) == 0) {
kusano ca9c32
        for (; h >= 2; h -= 2) {
kusano ca9c32
            x = w>>2;
kusano ca9c32
            do {
kusano ca9c32
                int32 Cb = pp[8];
kusano ca9c32
                int32 Cr = pp[9];
kusano ca9c32
                
kusano ca9c32
                YCbCrtoRGB(cp [0], pp[0]);
kusano ca9c32
                YCbCrtoRGB(cp [1], pp[1]);
kusano ca9c32
                YCbCrtoRGB(cp [2], pp[2]);
kusano ca9c32
                YCbCrtoRGB(cp [3], pp[3]);
kusano ca9c32
                YCbCrtoRGB(cp1[0], pp[4]);
kusano ca9c32
                YCbCrtoRGB(cp1[1], pp[5]);
kusano ca9c32
                YCbCrtoRGB(cp1[2], pp[6]);
kusano ca9c32
                YCbCrtoRGB(cp1[3], pp[7]);
kusano ca9c32
                
kusano ca9c32
                cp += 4, cp1 += 4;
kusano ca9c32
                pp += 10;
kusano ca9c32
            } while (--x);
kusano ca9c32
            cp += incr, cp1 += incr;
kusano ca9c32
            pp += fromskew;
kusano ca9c32
        }
kusano ca9c32
    } else {
kusano ca9c32
        while (h > 0) {
kusano ca9c32
            for (x = w; x > 0;) {
kusano ca9c32
                int32 Cb = pp[8];
kusano ca9c32
                int32 Cr = pp[9];
kusano ca9c32
                switch (x) {
kusano ca9c32
                default:
kusano ca9c32
                    switch (h) {
kusano ca9c32
                    default: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
kusano ca9c32
                    case 1:  YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
kusano ca9c32
                    }                                    /* FALLTHROUGH */
kusano ca9c32
                case 3:
kusano ca9c32
                    switch (h) {
kusano ca9c32
                    default: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
kusano ca9c32
                    case 1:  YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
kusano ca9c32
                    }                                    /* FALLTHROUGH */
kusano ca9c32
                case 2:
kusano ca9c32
                    switch (h) {
kusano ca9c32
                    default: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
kusano ca9c32
                    case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
kusano ca9c32
                    }                                    /* FALLTHROUGH */
kusano ca9c32
                case 1:
kusano ca9c32
                    switch (h) {
kusano ca9c32
                    default: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
kusano ca9c32
                    case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
kusano ca9c32
                    }                                    /* FALLTHROUGH */
kusano ca9c32
                }
kusano ca9c32
                if (x < 4) {
kusano ca9c32
                    cp += x; cp1 += x;
kusano ca9c32
                    x = 0;
kusano ca9c32
                }
kusano ca9c32
                else {
kusano ca9c32
                    cp += 4; cp1 += 4;
kusano ca9c32
                    x -= 4;
kusano ca9c32
                }
kusano ca9c32
                pp += 10;
kusano ca9c32
            }
kusano ca9c32
            if (h <= 2)
kusano ca9c32
                break;
kusano ca9c32
            h -= 2;
kusano ca9c32
            cp += incr, cp1 += incr;
kusano ca9c32
            pp += fromskew;
kusano ca9c32
        }
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    (void) y;
kusano ca9c32
    /* XXX adjust fromskew */
kusano ca9c32
    do {
kusano ca9c32
	x = w>>2;
Toshihiro Shimizu 890ddd
	do {
kusano ca9c32
	    int32 Cb = pp[4];
kusano ca9c32
	    int32 Cr = pp[5];
kusano ca9c32
kusano ca9c32
	    YCbCrtoRGB(cp [0], pp[0]);
kusano ca9c32
	    YCbCrtoRGB(cp [1], pp[1]);
kusano ca9c32
	    YCbCrtoRGB(cp [2], pp[2]);
kusano ca9c32
	    YCbCrtoRGB(cp [3], pp[3]);
kusano ca9c32
kusano ca9c32
	    cp += 4;
kusano ca9c32
	    pp += 6;
kusano ca9c32
	} while (--x);
kusano ca9c32
kusano ca9c32
        if( (w&3) != 0 )
kusano ca9c32
        {
kusano ca9c32
	    int32 Cb = pp[4];
kusano ca9c32
	    int32 Cr = pp[5];
kusano ca9c32
kusano ca9c32
            switch( (w&3) ) {
kusano ca9c32
              case 3: YCbCrtoRGB(cp [2], pp[2]);
kusano ca9c32
              case 2: YCbCrtoRGB(cp [1], pp[1]);
kusano ca9c32
              case 1: YCbCrtoRGB(cp [0], pp[0]);
kusano ca9c32
              case 0: break;
kusano ca9c32
            }
kusano ca9c32
kusano ca9c32
            cp += (w&3);
kusano ca9c32
            pp += 6;
kusano ca9c32
        }
kusano ca9c32
kusano ca9c32
	cp += toskew;
kusano ca9c32
	pp += fromskew;
kusano ca9c32
    } while (--h);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	uint64* cp2;
kusano ca9c32
	int32 incr = 2*toskew+w;
kusano ca9c32
	(void) y;
Toshihiro Shimizu 890ddd
	fromskew = (fromskew / 2) * 6;
kusano ca9c32
	cp2 = cp+w+toskew;
kusano ca9c32
	while (h>=2) {
Toshihiro Shimizu 890ddd
		x = w;
kusano ca9c32
		while (x>=2) {
Toshihiro Shimizu 890ddd
			uint32 Cb = pp[4];
Toshihiro Shimizu 890ddd
			uint32 Cr = pp[5];
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp[0], pp[0]);
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp[1], pp[1]);
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp2[0], pp[2]);
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp2[1], pp[3]);
Toshihiro Shimizu 890ddd
			cp += 2;
Toshihiro Shimizu 890ddd
			cp2 += 2;
Toshihiro Shimizu 890ddd
			pp += 6;
Toshihiro Shimizu 890ddd
			x -= 2;
Toshihiro Shimizu 890ddd
		}
kusano ca9c32
		if (x==1) {
Toshihiro Shimizu 890ddd
			uint32 Cb = pp[4];
Toshihiro Shimizu 890ddd
			uint32 Cr = pp[5];
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp[0], pp[0]);
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp2[0], pp[2]);
kusano ca9c32
			cp ++ ;
kusano ca9c32
			cp2 ++ ;
Toshihiro Shimizu 890ddd
			pp += 6;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		cp += incr;
Toshihiro Shimizu 890ddd
		cp2 += incr;
Toshihiro Shimizu 890ddd
		pp += fromskew;
kusano ca9c32
		h-=2;
Toshihiro Shimizu 890ddd
	}
kusano ca9c32
	if (h==1) {
Toshihiro Shimizu 890ddd
		x = w;
kusano ca9c32
		while (x>=2) {
Toshihiro Shimizu 890ddd
			uint32 Cb = pp[4];
Toshihiro Shimizu 890ddd
			uint32 Cr = pp[5];
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp[0], pp[0]);
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp[1], pp[1]);
Toshihiro Shimizu 890ddd
			cp += 2;
Toshihiro Shimizu 890ddd
			cp2 += 2;
Toshihiro Shimizu 890ddd
			pp += 6;
Toshihiro Shimizu 890ddd
			x -= 2;
Toshihiro Shimizu 890ddd
		}
kusano ca9c32
		if (x==1) {
Toshihiro Shimizu 890ddd
			uint32 Cb = pp[4];
Toshihiro Shimizu 890ddd
			uint32 Cr = pp[5];
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp[0], pp[0]);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	(void) y;
Toshihiro Shimizu 890ddd
	fromskew = (fromskew * 4) / 2;
Toshihiro Shimizu 890ddd
	do {
kusano ca9c32
		x = w>>1;
Toshihiro Shimizu 890ddd
		do {
Toshihiro Shimizu 890ddd
			int32 Cb = pp[2];
Toshihiro Shimizu 890ddd
			int32 Cr = pp[3];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp[0], pp[0]);
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp[1], pp[1]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			cp += 2;
Toshihiro Shimizu 890ddd
			pp += 4;
Toshihiro Shimizu 890ddd
		} while (--x);
Toshihiro Shimizu 890ddd
kusano ca9c32
		if( (w&1) != 0 )
kusano ca9c32
		{
Toshihiro Shimizu 890ddd
			int32 Cb = pp[2];
Toshihiro Shimizu 890ddd
			int32 Cr = pp[3];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp[0], pp[0]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			cp += 1;
Toshihiro Shimizu 890ddd
			pp += 4;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		cp += toskew;
Toshihiro Shimizu 890ddd
		pp += fromskew;
Toshihiro Shimizu 890ddd
	} while (--h);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	uint64* cp2;
kusano ca9c32
	int32 incr = 2*toskew+w;
kusano ca9c32
	(void) y;
Toshihiro Shimizu 890ddd
	fromskew = (fromskew / 2) * 4;
kusano ca9c32
	cp2 = cp+w+toskew;
kusano ca9c32
	while (h>=2) {
Toshihiro Shimizu 890ddd
		x = w;
Toshihiro Shimizu 890ddd
		do {
Toshihiro Shimizu 890ddd
			uint32 Cb = pp[2];
Toshihiro Shimizu 890ddd
			uint32 Cr = pp[3];
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp[0], pp[0]);
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp2[0], pp[1]);
kusano ca9c32
			cp ++;
kusano ca9c32
			cp2 ++;
Toshihiro Shimizu 890ddd
			pp += 4;
Toshihiro Shimizu 890ddd
		} while (--x);
Toshihiro Shimizu 890ddd
		cp += incr;
Toshihiro Shimizu 890ddd
		cp2 += incr;
Toshihiro Shimizu 890ddd
		pp += fromskew;
kusano ca9c32
		h-=2;
Toshihiro Shimizu 890ddd
	}
kusano ca9c32
	if (h==1) {
Toshihiro Shimizu 890ddd
		x = w;
Toshihiro Shimizu 890ddd
		do {
Toshihiro Shimizu 890ddd
			uint32 Cb = pp[2];
Toshihiro Shimizu 890ddd
			uint32 Cr = pp[3];
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(cp[0], pp[0]);
kusano ca9c32
			cp ++;
Toshihiro Shimizu 890ddd
			pp += 4;
Toshihiro Shimizu 890ddd
		} while (--x);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed YCbCr samples w/ no subsampling => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	(void) y;
Toshihiro Shimizu 890ddd
	fromskew *= 3;
Toshihiro Shimizu 890ddd
	do {
Toshihiro Shimizu 890ddd
		x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
Toshihiro Shimizu 890ddd
		do {
Toshihiro Shimizu 890ddd
			int32 Cb = pp[1];
Toshihiro Shimizu 890ddd
			int32 Cr = pp[2];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			YCbCrtoRGB(*cp++, pp[0]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			pp += 3;
Toshihiro Shimizu 890ddd
		} while (--x);
Toshihiro Shimizu 890ddd
		cp += toskew;
Toshihiro Shimizu 890ddd
		pp += fromskew;
Toshihiro Shimizu 890ddd
	} while (--h);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * 8-bit packed YCbCr samples w/ no subsampling => RGB
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
DECLARESepPutFunc(putseparate8bitYCbCr11tile)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	(void) y;
kusano ca9c32
	(void) a;
Toshihiro Shimizu 890ddd
	/* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
Toshihiro Shimizu 890ddd
	while (h-- > 0) {
Toshihiro Shimizu 890ddd
		x = w;
Toshihiro Shimizu 890ddd
		do {
Toshihiro Shimizu 890ddd
			uint32 dr, dg, db;
kusano ca9c32
			TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
kusano ca9c32
			*cp++ = PACK(dr,dg,db);
Toshihiro Shimizu 890ddd
		} while (--x);
Toshihiro Shimizu 890ddd
		SKEW(r, g, b, fromskew);
Toshihiro Shimizu 890ddd
		cp += toskew;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#undef YCbCrtoRGB
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
initYCbCrConversion(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static const char module[] = "initYCbCrConversion";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	float *luma, *refBlackWhite;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (img->ycbcr == NULL) {
kusano ca9c32
		img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
kusano ca9c32
		    TIFFroundup_32(sizeof (TIFFYCbCrToRGB), sizeof (long))  
kusano ca9c32
		    + 4*256*sizeof (TIFFRGBValue)
kusano ca9c32
		    + 2*256*sizeof (int)
kusano ca9c32
		    + 3*256*sizeof (int32)
kusano ca9c32
		    );
Toshihiro Shimizu 890ddd
		if (img->ycbcr == NULL) {
Toshihiro Shimizu 890ddd
			TIFFErrorExt(img->tif->tif_clientdata, module,
kusano ca9c32
			    "No space for YCbCr->RGB conversion state");
Toshihiro Shimizu 890ddd
			return (0);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
kusano ca9c32
	    &refBlackWhite);
Toshihiro Shimizu 890ddd
	if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
kusano ca9c32
		return(0);
Toshihiro Shimizu 890ddd
	return (1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static tileContigRoutine_64
kusano ca9c32
initCIELabConversion(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static const char module[] = "initCIELabConversion";
Toshihiro Shimizu 890ddd
kusano ca9c32
	float   *whitePoint;
kusano ca9c32
	float   refWhite[3];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!img->cielab) {
Toshihiro Shimizu 890ddd
		img->cielab = (TIFFCIELabToRGB *)
Toshihiro Shimizu 890ddd
			_TIFFmalloc(sizeof(TIFFCIELabToRGB));
Toshihiro Shimizu 890ddd
		if (!img->cielab) {
Toshihiro Shimizu 890ddd
			TIFFErrorExt(img->tif->tif_clientdata, module,
kusano ca9c32
			    "No space for CIE L*a*b*->RGB conversion state.");
Toshihiro Shimizu 890ddd
			return NULL;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
Toshihiro Shimizu 890ddd
	refWhite[1] = 100.0F;
Toshihiro Shimizu 890ddd
	refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
kusano ca9c32
	refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
kusano ca9c32
		      / whitePoint[1] * refWhite[1];
Toshihiro Shimizu 890ddd
	if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(img->tif->tif_clientdata, module,
kusano ca9c32
		    "Failed to initialize CIE L*a*b*->RGB conversion state.");
Toshihiro Shimizu 890ddd
		_TIFFfree(img->cielab);
Toshihiro Shimizu 890ddd
		return NULL;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return putcontig8bitCIELab;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Greyscale images with less than 8 bits/sample are handled
Toshihiro Shimizu 890ddd
 * with a table to avoid lots of shifts and masks.  The table
Toshihiro Shimizu 890ddd
 * is setup so that put*bwtile (below) can retrieve 8/bitspersample
Toshihiro Shimizu 890ddd
 * pixel values simply by indexing into the table with one
Toshihiro Shimizu 890ddd
 * number.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
makebwmap(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    TIFFRGBValue* Map = img->Map;
kusano ca9c32
    int bitspersample = img->bitspersample;
kusano ca9c32
    int nsamples = 8 / bitspersample;
kusano ca9c32
    int i;
kusano ca9c32
    uint64* p;
kusano ca9c32
kusano ca9c32
    if( nsamples == 0 )
kusano ca9c32
        nsamples = 1;
kusano ca9c32
kusano ca9c32
    img->BWmap = (uint32**) _TIFFmalloc(
kusano ca9c32
	256*sizeof (uint32 *)+(256*nsamples*sizeof(uint64)));
kusano ca9c32
    if (img->BWmap == NULL) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");
Toshihiro Shimizu 890ddd
		return (0);
kusano ca9c32
    }
kusano ca9c32
    p = (uint64*)(img->BWmap + 256);
kusano ca9c32
    for (i = 0; i < 256; i++) {
kusano ca9c32
	TIFFRGBValue c;
kusano ca9c32
	img->BWmap[i] = (uint32*) p;
kusano ca9c32
	switch (bitspersample) {
kusano ca9c32
#define	GREY(x)	c = Map[x]; *p++ = PACK(c << 8,c << 8,c << 8);
kusano ca9c32
	case 1:
kusano ca9c32
	    GREY(i>>7);
kusano ca9c32
	    GREY((i>>6)&1);
kusano ca9c32
	    GREY((i>>5)&1);
kusano ca9c32
	    GREY((i>>4)&1);
kusano ca9c32
	    GREY((i>>3)&1);
kusano ca9c32
	    GREY((i>>2)&1);
kusano ca9c32
	    GREY((i>>1)&1);
kusano ca9c32
	    GREY(i&1);
kusano ca9c32
	    break;
kusano ca9c32
	case 2:
kusano ca9c32
	    GREY(i>>6);
kusano ca9c32
	    GREY((i>>4)&3);
kusano ca9c32
	    GREY((i>>2)&3);
kusano ca9c32
	    GREY(i&3);
kusano ca9c32
	    break;
kusano ca9c32
	case 4:
kusano ca9c32
	    GREY(i>>4);
kusano ca9c32
	    GREY(i&0xf);
kusano ca9c32
	    break;
kusano ca9c32
	case 8:
kusano ca9c32
        case 16:
kusano ca9c32
	    GREY(i);
kusano ca9c32
	    break;
Toshihiro Shimizu 890ddd
	}
kusano ca9c32
#undef	GREY
kusano ca9c32
    }
kusano ca9c32
    return (1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Construct a mapping table to convert from the range
Toshihiro Shimizu 890ddd
 * of the data samples to [0,255] --for display.  This
Toshihiro Shimizu 890ddd
 * process also handles inverting B&W images when needed.
kusano ca9c32
 */ 
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
setupMap(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    int32 x, range;
Toshihiro Shimizu 890ddd
kusano ca9c32
    range = (int32)((1L<<img->bitspersample)-1);</img->
kusano ca9c32
    
kusano ca9c32
    /* treat 16 bit the same as eight bit */
kusano ca9c32
    if( img->bitspersample == 16 )
kusano ca9c32
        range = (int32) 255;
Toshihiro Shimizu 890ddd
kusano ca9c32
    img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
kusano ca9c32
    if (img->Map == NULL) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
kusano ca9c32
			"No space for photometric conversion table");
Toshihiro Shimizu 890ddd
		return (0);
kusano ca9c32
    }
kusano ca9c32
    if (img->photometric == PHOTOMETRIC_MINISWHITE) {
kusano ca9c32
	for (x = 0; x <= range; x++)
kusano ca9c32
	    img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);
kusano ca9c32
    } else {
kusano ca9c32
	for (x = 0; x <= range; x++)
kusano ca9c32
	    img->Map[x] = (TIFFRGBValue) ((x * 255) / range);
kusano ca9c32
    }
kusano ca9c32
    if (img->bitspersample <= 16 &&
kusano ca9c32
	(img->photometric == PHOTOMETRIC_MINISBLACK ||
kusano ca9c32
	 img->photometric == PHOTOMETRIC_MINISWHITE)) {
kusano ca9c32
	/*
Toshihiro Shimizu 890ddd
	 * Use photometric mapping table to construct
Toshihiro Shimizu 890ddd
	 * unpacking tables for samples <= 8 bits.
Toshihiro Shimizu 890ddd
	 */
kusano ca9c32
	if (!makebwmap(img))
kusano ca9c32
	    return (0);
kusano ca9c32
	/* no longer need Map, free it */
kusano ca9c32
	_TIFFfree(img->Map), img->Map = NULL;
kusano ca9c32
    }
kusano ca9c32
    return (1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
checkcmap(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    uint16* r = img->redcmap;
kusano ca9c32
    uint16* g = img->greencmap;
kusano ca9c32
    uint16* b = img->bluecmap;
kusano ca9c32
    long n = 1L<<img->bitspersample;</img->
kusano ca9c32
kusano ca9c32
    while (n-- > 0)
kusano ca9c32
	if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
kusano ca9c32
	    return (16);
kusano ca9c32
    return (8);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static void
kusano ca9c32
cvtcmap(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    uint16* r = img->redcmap;
kusano ca9c32
    uint16* g = img->greencmap;
kusano ca9c32
    uint16* b = img->bluecmap;
kusano ca9c32
    long i;
kusano ca9c32
kusano ca9c32
    for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {</img->
kusano ca9c32
#define	CVT(x)		((uint16)((x)>>8))
kusano ca9c32
	r[i] = CVT(r[i]);
kusano ca9c32
	g[i] = CVT(g[i]);
kusano ca9c32
	b[i] = CVT(b[i]);
kusano ca9c32
#undef	CVT
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Palette images with <= 8 bits/sample are handled
Toshihiro Shimizu 890ddd
 * with a table to avoid lots of shifts and masks.  The table
Toshihiro Shimizu 890ddd
 * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
Toshihiro Shimizu 890ddd
 * pixel values simply by indexing into the table with one
Toshihiro Shimizu 890ddd
 * number.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
makecmap(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    int bitspersample = img->bitspersample;
kusano ca9c32
    int nsamples = 8 / bitspersample;
kusano ca9c32
    uint16* r = img->redcmap;
kusano ca9c32
    uint16* g = img->greencmap;
kusano ca9c32
    uint16* b = img->bluecmap;
kusano ca9c32
    uint32 *p;
kusano ca9c32
    int i;
kusano ca9c32
kusano ca9c32
    img->PALmap = (uint32**) _TIFFmalloc(
kusano ca9c32
	256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
kusano ca9c32
    if (img->PALmap == NULL) {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");
Toshihiro Shimizu 890ddd
		return (0);
Toshihiro Shimizu 890ddd
	}
kusano ca9c32
    p = (uint32*)(img->PALmap + 256);
kusano ca9c32
    for (i = 0; i < 256; i++) {
kusano ca9c32
	TIFFRGBValue c;
kusano ca9c32
	img->PALmap[i] = p;
kusano ca9c32
#define	CMAP(x)	c = (TIFFRGBValue) x; *p++ = PACK_32(r[c]&0xff, g[c]&0xff, b[c]&0xff);
kusano ca9c32
	switch (bitspersample) {
kusano ca9c32
	case 1:
kusano ca9c32
	    CMAP(i>>7);
kusano ca9c32
	    CMAP((i>>6)&1);
kusano ca9c32
	    CMAP((i>>5)&1);
kusano ca9c32
	    CMAP((i>>4)&1);
kusano ca9c32
	    CMAP((i>>3)&1);
kusano ca9c32
	    CMAP((i>>2)&1);
kusano ca9c32
	    CMAP((i>>1)&1);
kusano ca9c32
	    CMAP(i&1);
kusano ca9c32
	    break;
kusano ca9c32
	case 2:
kusano ca9c32
	    CMAP(i>>6);
kusano ca9c32
	    CMAP((i>>4)&3);
kusano ca9c32
	    CMAP((i>>2)&3);
kusano ca9c32
	    CMAP(i&3);
kusano ca9c32
	    break;
kusano ca9c32
	case 4:
kusano ca9c32
	    CMAP(i>>4);
kusano ca9c32
	    CMAP(i&0xf);
kusano ca9c32
	    break;
kusano ca9c32
	case 8:
kusano ca9c32
	    CMAP(i);
kusano ca9c32
	    break;
Toshihiro Shimizu 890ddd
	}
kusano ca9c32
#undef CMAP
kusano ca9c32
    }
kusano ca9c32
    return (1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* 
Toshihiro Shimizu 890ddd
 * Construct any mapping table used
Toshihiro Shimizu 890ddd
 * by the associated put routine.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
buildMap(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    switch (img->photometric) {
kusano ca9c32
    case PHOTOMETRIC_RGB:
kusano ca9c32
    case PHOTOMETRIC_YCBCR:
kusano ca9c32
    case PHOTOMETRIC_SEPARATED:
kusano ca9c32
	if (img->bitspersample == 8)
kusano ca9c32
	    break;
Toshihiro Shimizu 890ddd
	/* fall thru... */
kusano ca9c32
    case PHOTOMETRIC_MINISBLACK:
kusano ca9c32
    case PHOTOMETRIC_MINISWHITE:
kusano ca9c32
	if (!setupMap(img))
kusano ca9c32
	    return (0);
kusano ca9c32
	break;
kusano ca9c32
    case PHOTOMETRIC_PALETTE:
kusano ca9c32
	/*
Toshihiro Shimizu 890ddd
	 * Convert 16-bit colormap to 8-bit (unless it looks
Toshihiro Shimizu 890ddd
	 * like an old-style 8-bit colormap).
Toshihiro Shimizu 890ddd
	 */
kusano ca9c32
	if (checkcmap(img) == 16)
kusano ca9c32
	    cvtcmap(img);
kusano ca9c32
	else
kusano ca9c32
	    TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");
kusano ca9c32
	/*
Toshihiro Shimizu 890ddd
	 * Use mapping table and colormap to construct
Toshihiro Shimizu 890ddd
	 * unpacking tables for samples < 8 bits.
Toshihiro Shimizu 890ddd
	 */
kusano ca9c32
	if (img->bitspersample <= 8 && !makecmap(img))
kusano ca9c32
	    return (0);
kusano ca9c32
	break;
kusano ca9c32
    }
kusano ca9c32
    return (1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Select the appropriate conversion routine for packed data.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
PickContigCase(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
   img->get = (gtFunc_32) (TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig);
Toshihiro Shimizu 890ddd
	img->put.contig = NULL;
Toshihiro Shimizu 890ddd
	switch (img->photometric) {
kusano ca9c32
		case PHOTOMETRIC_RGB:
kusano ca9c32
			switch (img->bitspersample) {
kusano ca9c32
				case 8:
kusano ca9c32
               if (img->alpha != EXTRASAMPLE_UNSPECIFIED)
kusano ca9c32
						img->put.contig = (tileContigRoutine) putRGBAAcontig8bittile;
kusano ca9c32
					else
kusano ca9c32
						img->put.contig = (tileContigRoutine) putRGBcontig8bittile;
kusano ca9c32
					break;
kusano ca9c32
				case 16:
kusano ca9c32
					if (img->alpha != EXTRASAMPLE_UNSPECIFIED)
kusano ca9c32
						img->put.contig = (tileContigRoutine) putRGBAAcontig16bittile;
kusano ca9c32
					else
kusano ca9c32
					{
kusano ca9c32
						if (BuildMapBitdepth16To8(img))
kusano ca9c32
							img->put.contig = (tileContigRoutine) putRGBcontig16bittile;
kusano ca9c32
					}
kusano ca9c32
					break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			break;
kusano ca9c32
		case PHOTOMETRIC_SEPARATED:
kusano ca9c32
			if (buildMap(img)) {
kusano ca9c32
				if (img->bitspersample == 8) {
kusano ca9c32
					if (!img->Map)
kusano ca9c32
						img->put.contig = (tileContigRoutine) putRGBcontig8bitCMYKtile;
kusano ca9c32
					else
kusano ca9c32
						img->put.contig = (tileContigRoutine) putRGBcontig8bitCMYKMaptile;
kusano ca9c32
				}
Toshihiro Shimizu 890ddd
			}
kusano ca9c32
			break;
kusano ca9c32
		case PHOTOMETRIC_PALETTE:
kusano ca9c32
		case PHOTOMETRIC_MINISWHITE:
kusano ca9c32
		case PHOTOMETRIC_MINISBLACK:
kusano ca9c32
			if (buildMap(img)) {
kusano ca9c32
				switch (img->bitspersample) {
kusano ca9c32
					case 16:
kusano ca9c32
						img->put.contig = (tileContigRoutine) put16bitbwtile;
kusano ca9c32
						break;
kusano ca9c32
					case 8:
kusano ca9c32
						if (img->alpha && img->samplesperpixel == 2)
kusano ca9c32
							img->put.contig = (tileContigRoutine) putagreytile;
kusano ca9c32
						else
kusano ca9c32
							img->put.contig = (tileContigRoutine) putgreytile;
kusano ca9c32
						break;
kusano ca9c32
					case 4:
kusano ca9c32
						img->put.contig = (tileContigRoutine) put4bitbwtile;
kusano ca9c32
						break;
kusano ca9c32
					case 2:
kusano ca9c32
						img->put.contig = (tileContigRoutine) put2bitbwtile;
kusano ca9c32
						break;
kusano ca9c32
					case 1:
kusano ca9c32
						img->put.contig = (tileContigRoutine) put1bitbwtile;
kusano ca9c32
						break;
kusano ca9c32
				}
Toshihiro Shimizu 890ddd
			}
kusano ca9c32
			break;
kusano ca9c32
		case PHOTOMETRIC_YCBCR:
kusano ca9c32
			if ((img->bitspersample==8) && (img->samplesperpixel==3))
kusano ca9c32
			{
kusano ca9c32
				if (initYCbCrConversion(img)!=0)
kusano ca9c32
				{
kusano ca9c32
					/*
Toshihiro Shimizu 890ddd
					 * The 6.0 spec says that subsampling must be
Toshihiro Shimizu 890ddd
					 * one of 1, 2, or 4, and that vertical subsampling
Toshihiro Shimizu 890ddd
					 * must always be <= horizontal subsampling; so
Toshihiro Shimizu 890ddd
					 * there are only a few possibilities and we just
Toshihiro Shimizu 890ddd
					 * enumerate the cases.
Toshihiro Shimizu 890ddd
					 * Joris: added support for the [1,2] case, nonetheless, to accomodate
Toshihiro Shimizu 890ddd
					 * some OJPEG files
Toshihiro Shimizu 890ddd
					 */
kusano ca9c32
					uint16 SubsamplingHor;
kusano ca9c32
					uint16 SubsamplingVer;
kusano ca9c32
					TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
kusano ca9c32
					switch ((SubsamplingHor<<4)|SubsamplingVer) {
kusano ca9c32
						case 0x44:
kusano ca9c32
							img->put.contig = (tileContigRoutine) putcontig8bitYCbCr44tile;
kusano ca9c32
							break;
kusano ca9c32
						case 0x42:
kusano ca9c32
							img->put.contig = (tileContigRoutine) putcontig8bitYCbCr42tile;
kusano ca9c32
							break;
kusano ca9c32
						case 0x41:
kusano ca9c32
							img->put.contig = (tileContigRoutine) putcontig8bitYCbCr41tile;
kusano ca9c32
							break;
kusano ca9c32
						case 0x22:
kusano ca9c32
							img->put.contig = (tileContigRoutine) putcontig8bitYCbCr22tile;
kusano ca9c32
							break;
kusano ca9c32
						case 0x21:
kusano ca9c32
							img->put.contig = (tileContigRoutine) putcontig8bitYCbCr21tile;
kusano ca9c32
							break;
kusano ca9c32
						case 0x12:
kusano ca9c32
							img->put.contig = (tileContigRoutine) putcontig8bitYCbCr12tile;
kusano ca9c32
							break;
kusano ca9c32
						case 0x11:
kusano ca9c32
							img->put.contig = (tileContigRoutine) putcontig8bitYCbCr11tile;
kusano ca9c32
							break;
kusano ca9c32
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			break;
kusano ca9c32
		case PHOTOMETRIC_CIELAB:
kusano ca9c32
			if (buildMap(img)) {
kusano ca9c32
				if (img->bitspersample == 8)
kusano ca9c32
					img->put.contig = (tileContigRoutine) initCIELabConversion(img);
kusano ca9c32
				break;
kusano ca9c32
			}
Toshihiro Shimizu 890ddd
	}
kusano ca9c32
	return ((img->get!=NULL) && (img->put.contig!=NULL));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Select the appropriate conversion routine for unpacked data.
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * NB: we assume that unpacked single channel data is directed
Toshihiro Shimizu 890ddd
 *	 to the "packed routines.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
PickSeparateCase(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	img->get = (gtFunc_32) (TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate);
Toshihiro Shimizu 890ddd
	img->put.separate = NULL;
Toshihiro Shimizu 890ddd
	switch (img->photometric) {
Toshihiro Shimizu 890ddd
	case PHOTOMETRIC_MINISWHITE:
Toshihiro Shimizu 890ddd
	case PHOTOMETRIC_MINISBLACK:
kusano ca9c32
		/* greyscale images processed pretty much as RGB by gtTileSeparate */
Toshihiro Shimizu 890ddd
	case PHOTOMETRIC_RGB:
Toshihiro Shimizu 890ddd
		switch (img->bitspersample) {
Toshihiro Shimizu 890ddd
		case 8:
Toshihiro Shimizu 890ddd
			if (img->alpha != EXTRASAMPLE_UNSPECIFIED)
kusano ca9c32
				img->put.separate = (tileSeparateRoutine) putRGBAAseparate8bittile;
Toshihiro Shimizu 890ddd
			else
kusano ca9c32
				img->put.separate = (tileSeparateRoutine) putRGBseparate8bittile;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		case 16:
kusano ca9c32
			if (img->alpha != EXTRASAMPLE_UNSPECIFIED)
kusano ca9c32
			{
Toshihiro Shimizu 890ddd
				if (BuildMapBitdepth16To8(img))
kusano ca9c32
					img->put.separate = (tileSeparateRoutine) putRGBAAseparate16bittile;
kusano ca9c32
			}
kusano ca9c32
			else
kusano ca9c32
			{
Toshihiro Shimizu 890ddd
				if (BuildMapBitdepth16To8(img))
kusano ca9c32
					img->put.separate = (tileSeparateRoutine) putRGBseparate16bittile;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case PHOTOMETRIC_SEPARATED:
kusano ca9c32
		if (img->bitspersample == 8 && img->samplesperpixel == 4)
kusano ca9c32
		{
Toshihiro Shimizu 890ddd
			img->alpha = 1; // Not alpha, but seems like the only way to get 4th band
kusano ca9c32
			img->put.separate = (tileSeparateRoutine) putCMYKseparate8bittile;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case PHOTOMETRIC_YCBCR:
kusano ca9c32
		if ((img->bitspersample==8) && (img->samplesperpixel==3))
kusano ca9c32
		{
kusano ca9c32
			if (initYCbCrConversion(img)!=0)
kusano ca9c32
			{
Toshihiro Shimizu 890ddd
				uint16 hs, vs;
Toshihiro Shimizu 890ddd
				TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
kusano ca9c32
				switch ((hs<<4)|vs) {
Toshihiro Shimizu 890ddd
				case 0x11:
kusano ca9c32
					img->put.separate = (tileSeparateRoutine) putseparate8bitYCbCr11tile;
Toshihiro Shimizu 890ddd
					break;
Toshihiro Shimizu 890ddd
					/* TODO: add other cases here */
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	}
kusano ca9c32
	return ((img->get!=NULL) && (img->put.separate!=NULL));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int
kusano ca9c32
BuildMapBitdepth16To8(TIFFRGBAImage* img)
Toshihiro Shimizu 890ddd
{
kusano ca9c32
	static const char module[]="BuildMapBitdepth16To8";
kusano ca9c32
	uint8* m;
Toshihiro Shimizu 890ddd
	uint32 n;
kusano ca9c32
	assert(img->Bitdepth16To8==NULL);
kusano ca9c32
	img->Bitdepth16To8=_TIFFmalloc(65536);
kusano ca9c32
	if (img->Bitdepth16To8==NULL)
kusano ca9c32
	{
kusano ca9c32
		TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
kusano ca9c32
		return(0);
kusano ca9c32
	}
kusano ca9c32
	m=img->Bitdepth16To8;
kusano ca9c32
	for (n=0; n<65536; n++)
kusano ca9c32
		*m++=(n+128)/257;
kusano ca9c32
	return(1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
kusano ca9c32
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Read a whole strip off data from the file, and convert to RGBA form.
Toshihiro Shimizu 890ddd
 * If this is the last strip, then it will only contain the portion of
Toshihiro Shimizu 890ddd
 * the strip that is actually within the image space.  The result is
Toshihiro Shimizu 890ddd
 * organized in bottom to top form.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
kusano ca9c32
kusano ca9c32
int
kusano ca9c32
TIFFReadRGBAStrip_64(TIFF* tif, uint32 row, uint64 * raster )
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    char 	emsg[1024] = "";
kusano ca9c32
    TIFFRGBAImage img;
kusano ca9c32
    int 	ok;
kusano ca9c32
    uint32	rowsperstrip, rows_to_read;
Toshihiro Shimizu 890ddd
kusano ca9c32
    if( TIFFIsTiled( tif ) )
kusano ca9c32
    {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
kusano ca9c32
                  "Can't use TIFFReadRGBAStrip() with tiled file.");
kusano ca9c32
	return (0);
kusano ca9c32
    }
kusano ca9c32
    
kusano ca9c32
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
kusano ca9c32
    if( (row % rowsperstrip) != 0 )
kusano ca9c32
    {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
kusano ca9c32
				"Row passed to TIFFReadRGBAStrip() must be first in a strip.");
Toshihiro Shimizu 890ddd
		return (0);
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
kusano ca9c32
    if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin_64(&img, tif, 0, emsg)) {
Toshihiro Shimizu 890ddd
kusano ca9c32
        img.row_offset = row;
kusano ca9c32
        img.col_offset = 0;
Toshihiro Shimizu 890ddd
kusano ca9c32
        if( row + rowsperstrip > img.height )
kusano ca9c32
            rows_to_read = img.height - row;
kusano ca9c32
        else
kusano ca9c32
            rows_to_read = rowsperstrip;
kusano ca9c32
        
kusano ca9c32
	ok = TIFFRGBAImageGet_64(&img, raster, img.width, rows_to_read );
kusano ca9c32
        
kusano ca9c32
	TIFFRGBAImageEnd(&img);
kusano ca9c32
    } else {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
Toshihiro Shimizu 890ddd
		ok = 0;
kusano ca9c32
    }
kusano ca9c32
    
kusano ca9c32
    return (ok);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Read a whole tile off data from the file, and convert to RGBA form.
Toshihiro Shimizu 890ddd
 * The returned RGBA data is organized from bottom to top of tile,
Toshihiro Shimizu 890ddd
 * and may include zeroed areas if the tile extends off the image.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
kusano ca9c32
int
kusano ca9c32
TIFFReadRGBATile_64(TIFF* tif, uint32 col, uint32 row, uint64 * raster)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
{
kusano ca9c32
    char 	emsg[1024] = "";
kusano ca9c32
    TIFFRGBAImage img;
kusano ca9c32
    int 	ok;
kusano ca9c32
    uint32	tile_xsize, tile_ysize;
kusano ca9c32
    uint32	read_xsize, read_ysize;
kusano ca9c32
    uint32	i_row;
kusano ca9c32
kusano ca9c32
    /*
Toshihiro Shimizu 890ddd
     * Verify that our request is legal - on a tile file, and on a
Toshihiro Shimizu 890ddd
     * tile boundary.
Toshihiro Shimizu 890ddd
     */
kusano ca9c32
    
kusano ca9c32
    if( !TIFFIsTiled( tif ) )
kusano ca9c32
    {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
kusano ca9c32
				  "Can't use TIFFReadRGBATile() with stripped file.");
Toshihiro Shimizu 890ddd
		return (0);
kusano ca9c32
    }
kusano ca9c32
    
kusano ca9c32
    TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
kusano ca9c32
    TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
kusano ca9c32
    if( (col % tile_xsize) != 0 || (row % tile_ysize) != 0 )
kusano ca9c32
    {
Toshihiro Shimizu 890ddd
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
kusano ca9c32
                  "Row/col passed to TIFFReadRGBATile() must be top"
kusano ca9c32
                  "left corner of a tile.");
kusano ca9c32
	return (0);
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
kusano ca9c32
    /*
Toshihiro Shimizu 890ddd
     * Setup the RGBA reader.
Toshihiro Shimizu 890ddd
     */
kusano ca9c32
    
kusano ca9c32
    if (!TIFFRGBAImageOK(tif, emsg) 
kusano ca9c32
	|| !TIFFRGBAImageBegin_64(&img, tif, 0, emsg)) {
kusano ca9c32
	    TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
kusano ca9c32
	    return( 0 );
kusano ca9c32
    }
kusano ca9c32
kusano ca9c32
    /*
Toshihiro Shimizu 890ddd
     * The TIFFRGBAImageGet() function doesn't allow us to get off the
Toshihiro Shimizu 890ddd
     * edge of the image, even to fill an otherwise valid tile.  So we
Toshihiro Shimizu 890ddd
     * figure out how much we can read, and fix up the tile buffer to
Toshihiro Shimizu 890ddd
     * a full tile configuration afterwards.
Toshihiro Shimizu 890ddd
     */
Toshihiro Shimizu 890ddd
kusano ca9c32
    if( row + tile_ysize > img.height )
kusano ca9c32
        read_ysize = img.height - row;
kusano ca9c32
    else
kusano ca9c32
        read_ysize = tile_ysize;
kusano ca9c32
    
kusano ca9c32
    if( col + tile_xsize > img.width )
kusano ca9c32
        read_xsize = img.width - col;
kusano ca9c32
    else
kusano ca9c32
        read_xsize = tile_xsize;
kusano ca9c32
kusano ca9c32
    /*
Toshihiro Shimizu 890ddd
     * Read the chunk of imagery.
Toshihiro Shimizu 890ddd
     */
kusano ca9c32
    
kusano ca9c32
    img.row_offset = row;
kusano ca9c32
    img.col_offset = col;
Toshihiro Shimizu 890ddd
kusano ca9c32
    ok = TIFFRGBAImageGet_64(&img, raster, read_xsize, read_ysize );
kusano ca9c32
        
kusano ca9c32
    TIFFRGBAImageEnd(&img);
Toshihiro Shimizu 890ddd
kusano ca9c32
    /*
Toshihiro Shimizu 890ddd
     * If our read was incomplete we will need to fix up the tile by
Toshihiro Shimizu 890ddd
     * shifting the data around as if a full tile of data is being returned.
Toshihiro Shimizu 890ddd
     *
Toshihiro Shimizu 890ddd
     * This is all the more complicated because the image is organized in
Toshihiro Shimizu 890ddd
     * bottom to top format. 
Toshihiro Shimizu 890ddd
     */
Toshihiro Shimizu 890ddd
kusano ca9c32
    if( read_xsize == tile_xsize && read_ysize == tile_ysize )
kusano ca9c32
        return( ok );
Toshihiro Shimizu 890ddd
kusano ca9c32
    for( i_row = 0; i_row < read_ysize; i_row++ ) {
kusano ca9c32
        memmove( raster + (tile_ysize - i_row - 1) * tile_xsize,
kusano ca9c32
                 raster + (read_ysize - i_row - 1) * read_xsize,
kusano ca9c32
                 read_xsize * sizeof(uint64) );
kusano ca9c32
        _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize+read_xsize,
kusano ca9c32
                     0, sizeof(uint64) * (tile_xsize - read_xsize) );
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
kusano ca9c32
    for( i_row = read_ysize; i_row < tile_ysize; i_row++ ) {
kusano ca9c32
        _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize,
kusano ca9c32
                     0, sizeof(uint64) * tile_xsize );
kusano ca9c32
    }
Toshihiro Shimizu 890ddd
kusano ca9c32
    return (ok);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* vim: set ts=8 sts=8 sw=8 noet: */
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * Local Variables:
Toshihiro Shimizu 890ddd
 * mode: c
Toshihiro Shimizu 890ddd
 * c-basic-offset: 8
Toshihiro Shimizu 890ddd
 * fill-column: 78
Toshihiro Shimizu 890ddd
 * End:
Toshihiro Shimizu 890ddd
 */