kusano 7d535a
/* $Header: /cvs/maptools/cvsroot/libtiff/contrib/pds/tif_imageiter.c,v 1.4 2010-06-08 18:55:15 bfriesen Exp $ */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Copyright (c) 1991-1996 Sam Leffler
kusano 7d535a
 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
kusano 7d535a
 *
kusano 7d535a
 * Permission to use, copy, modify, distribute, and sell this software and 
kusano 7d535a
 * its documentation for any purpose is hereby granted without fee, provided
kusano 7d535a
 * that (i) the above copyright notices and this permission notice appear in
kusano 7d535a
 * all copies of the software and related documentation, and (ii) the names of
kusano 7d535a
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
kusano 7d535a
 * publicity relating to the software without the specific, prior written
kusano 7d535a
 * permission of Sam Leffler and Silicon Graphics.
kusano 7d535a
 * 
kusano 7d535a
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
kusano 7d535a
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
kusano 7d535a
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
kusano 7d535a
 * 
kusano 7d535a
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
kusano 7d535a
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
kusano 7d535a
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
kusano 7d535a
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
kusano 7d535a
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
kusano 7d535a
 * OF THIS SOFTWARE.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * TIFF Library
kusano 7d535a
 *
kusano 7d535a
 * Written by Conrad J. Poelman, PL/WSAT, Kirtland AFB, NM on 26 Mar 96.
kusano 7d535a
 *
kusano 7d535a
 * This file contains code to allow a calling program to "iterate" over each
kusano 7d535a
 * pixels in an image as it is read from the file. The iterator takes care of
kusano 7d535a
 * reading strips versus (possibly clipped) tiles, decoding the information
kusano 7d535a
 * according to the decoding method, and so on, so that calling program can
kusano 7d535a
 * ignore those details. The calling program does, however, need to be
kusano 7d535a
 * conscious of the type of the pixel data that it is receiving.
kusano 7d535a
 *
kusano 7d535a
 * For reasons of efficiency, the callback function actually gets called for 
kusano 7d535a
 * "blocks" of pixels rather than for individual pixels. The format of the
kusano 7d535a
 * callback arguments is given below.
kusano 7d535a
 *
kusano 7d535a
 * This code was taken from TIFFReadRGBAImage() in tif_getimage.c of the original
kusano 7d535a
 * TIFF distribution, and simplified and generalized to provide this general
kusano 7d535a
 * iteration capability. Those routines could certainly be re-implemented in terms
kusano 7d535a
 * of a TIFFImageIter if desired.
kusano 7d535a
 *
kusano 7d535a
 */
kusano 7d535a
#include "tiffiop.h"
kusano 7d535a
#include "tif_imageiter.h"
kusano 7d535a
#include <assert.h></assert.h>
kusano 7d535a
#include <stdio.h></stdio.h>
kusano 7d535a
kusano 7d535a
static	int gtTileContig(TIFFImageIter*, void *udata, uint32, uint32);
kusano 7d535a
static	int gtTileSeparate(TIFFImageIter*, void *udata, uint32, uint32);
kusano 7d535a
static	int gtStripContig(TIFFImageIter*, void *udata, uint32, uint32);
kusano 7d535a
static	int gtStripSeparate(TIFFImageIter*, void *udata, uint32, uint32);
kusano 7d535a
kusano 7d535a
static	const char photoTag[] = "PhotometricInterpretation";
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
isCCITTCompression(TIFF* tif)
kusano 7d535a
{
kusano 7d535a
    uint16 compress;
kusano 7d535a
    TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
kusano 7d535a
    return (compress == COMPRESSION_CCITTFAX3 ||
kusano 7d535a
	    compress == COMPRESSION_CCITTFAX4 ||
kusano 7d535a
	    compress == COMPRESSION_CCITTRLE ||
kusano 7d535a
	    compress == COMPRESSION_CCITTRLEW);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
int
kusano 7d535a
TIFFImageIterBegin(TIFFImageIter* img, TIFF* tif, int stop, char emsg[1024])
kusano 7d535a
{
kusano 7d535a
    uint16* sampleinfo;
kusano 7d535a
    uint16 extrasamples;
kusano 7d535a
    uint16 planarconfig;
kusano 7d535a
    int colorchannels;
kusano 7d535a
kusano 7d535a
    img->tif = tif;
kusano 7d535a
    img->stoponerr = stop;
kusano 7d535a
    TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
kusano 7d535a
    img->alpha = 0;
kusano 7d535a
    TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
kusano 7d535a
    TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
kusano 7d535a
	&extrasamples, &sampleinfo);
kusano 7d535a
    if (extrasamples == 1)
kusano 7d535a
	switch (sampleinfo[0]) {
kusano 7d535a
	case EXTRASAMPLE_ASSOCALPHA:	/* data is pre-multiplied */
kusano 7d535a
	case EXTRASAMPLE_UNASSALPHA:	/* data is not pre-multiplied */
kusano 7d535a
	    img->alpha = sampleinfo[0];
kusano 7d535a
	    break;
kusano 7d535a
	}
kusano 7d535a
    colorchannels = img->samplesperpixel - extrasamples;
kusano 7d535a
    TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
kusano 7d535a
    if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
kusano 7d535a
	switch (colorchannels) {
kusano 7d535a
	case 1:
kusano 7d535a
	    if (isCCITTCompression(tif))
kusano 7d535a
		img->photometric = PHOTOMETRIC_MINISWHITE;
kusano 7d535a
	    else
kusano 7d535a
		img->photometric = PHOTOMETRIC_MINISBLACK;
kusano 7d535a
	    break;
kusano 7d535a
	case 3:
kusano 7d535a
	    img->photometric = PHOTOMETRIC_RGB;
kusano 7d535a
	    break;
kusano 7d535a
	default:
kusano 7d535a
	    sprintf(emsg, "Missing needed %s tag", photoTag);
kusano 7d535a
	    return (0);
kusano 7d535a
	}
kusano 7d535a
    }
kusano 7d535a
    switch (img->photometric) {
kusano 7d535a
    case PHOTOMETRIC_PALETTE:
kusano 7d535a
	if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
kusano 7d535a
	    &img->redcmap, &img->greencmap, &img->bluecmap)) {
kusano 7d535a
		TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Missing required \"Colormap\" tag");
kusano 7d535a
	    return (0);
kusano 7d535a
	}
kusano 7d535a
	/* fall thru... */
kusano 7d535a
    case PHOTOMETRIC_MINISWHITE:
kusano 7d535a
    case PHOTOMETRIC_MINISBLACK:
kusano 7d535a
/* This should work now so skip the check - BSR
kusano 7d535a
	if (planarconfig == PLANARCONFIG_CONTIG && img->samplesperpixel != 1) {
kusano 7d535a
	    sprintf(emsg,
kusano 7d535a
		"Sorry, can not handle contiguous data with %s=%d, and %s=%d",
kusano 7d535a
		photoTag, img->photometric,
kusano 7d535a
		"Samples/pixel", img->samplesperpixel);
kusano 7d535a
	    return (0);
kusano 7d535a
	}
kusano 7d535a
 */
kusano 7d535a
	break;
kusano 7d535a
    case PHOTOMETRIC_YCBCR:
kusano 7d535a
	if (planarconfig != PLANARCONFIG_CONTIG) {
kusano 7d535a
	    sprintf(emsg, "Sorry, can not handle YCbCr images with %s=%d",
kusano 7d535a
		"Planarconfiguration", planarconfig);
kusano 7d535a
	    return (0);
kusano 7d535a
	}
kusano 7d535a
	/* It would probably be nice to have a reality check here. */
kusano 7d535a
	{ uint16 compress;
kusano 7d535a
	  TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
kusano 7d535a
	  if (compress == COMPRESSION_JPEG && planarconfig == PLANARCONFIG_CONTIG) {
kusano 7d535a
	    /* can rely on libjpeg to convert to RGB */
kusano 7d535a
	    /* XXX should restore current state on exit */
kusano 7d535a
	    TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
kusano 7d535a
	    img->photometric = PHOTOMETRIC_RGB;
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
	break;
kusano 7d535a
    case PHOTOMETRIC_RGB: 
kusano 7d535a
	if (colorchannels < 3) {
kusano 7d535a
	    sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
kusano 7d535a
		"Color channels", colorchannels);
kusano 7d535a
	    return (0);
kusano 7d535a
	}
kusano 7d535a
	break;
kusano 7d535a
    case PHOTOMETRIC_SEPARATED: {
kusano 7d535a
	uint16 inkset;
kusano 7d535a
	TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
kusano 7d535a
	if (inkset != INKSET_CMYK) {
kusano 7d535a
	    sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
kusano 7d535a
		"InkSet", inkset);
kusano 7d535a
	    return (0);
kusano 7d535a
	}
kusano 7d535a
	if (img->samplesperpixel != 4) {
kusano 7d535a
	    sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
kusano 7d535a
		"Samples/pixel", img->samplesperpixel);
kusano 7d535a
	    return (0);
kusano 7d535a
	}
kusano 7d535a
	break;
kusano 7d535a
    }
kusano 7d535a
    default:
kusano 7d535a
	sprintf(emsg, "Sorry, can not handle image with %s=%d",
kusano 7d535a
	    photoTag, img->photometric);
kusano 7d535a
	return (0);
kusano 7d535a
    }
kusano 7d535a
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
kusano 7d535a
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
kusano 7d535a
kusano 7d535a
    TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
kusano 7d535a
    switch (img->orientation) {
kusano 7d535a
    case ORIENTATION_BOTRIGHT:
kusano 7d535a
    case ORIENTATION_RIGHTBOT:	/* XXX */
kusano 7d535a
    case ORIENTATION_LEFTBOT:	/* XXX */
kusano 7d535a
	TIFFWarning(TIFFFileName(tif), "using bottom-left orientation");
kusano 7d535a
	img->orientation = ORIENTATION_BOTLEFT;
kusano 7d535a
	/* fall thru... */
kusano 7d535a
    case ORIENTATION_BOTLEFT:
kusano 7d535a
	break;
kusano 7d535a
    case ORIENTATION_TOPRIGHT:
kusano 7d535a
    case ORIENTATION_RIGHTTOP:	/* XXX */
kusano 7d535a
    case ORIENTATION_LEFTTOP:	/* XXX */
kusano 7d535a
    default:
kusano 7d535a
	TIFFWarning(TIFFFileName(tif), "using top-left orientation");
kusano 7d535a
	img->orientation = ORIENTATION_TOPLEFT;
kusano 7d535a
	/* fall thru... */
kusano 7d535a
    case ORIENTATION_TOPLEFT:
kusano 7d535a
	break;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    img->isContig =
kusano 7d535a
	!(planarconfig == PLANARCONFIG_SEPARATE && colorchannels > 1);
kusano 7d535a
    if (img->isContig) {
kusano 7d535a
	img->get = TIFFIsTiled(tif) ? gtTileContig : gtStripContig;
kusano 7d535a
    } else {
kusano 7d535a
	img->get = TIFFIsTiled(tif) ? gtTileSeparate : gtStripSeparate;
kusano 7d535a
    }
kusano 7d535a
    return (1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
int
kusano 7d535a
TIFFImageIterGet(TIFFImageIter* img, void *udata, uint32 w, uint32 h)
kusano 7d535a
{
kusano 7d535a
    if (img->get == NULL) {
kusano 7d535a
	TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
kusano 7d535a
	return (0);
kusano 7d535a
    }
kusano 7d535a
    if (img->callback.any == NULL) {
kusano 7d535a
	TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
kusano 7d535a
		"No \"put\" routine setupl; probably can not handle image format");
kusano 7d535a
	return (0);
kusano 7d535a
    }
kusano 7d535a
    return (*img->get)(img, udata, w, h);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
TIFFImageIterEnd(TIFFImageIter* img)
kusano 7d535a
{
kusano 7d535a
    /* Nothing to free... ? */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Read the specified image into an ABGR-format raster.
kusano 7d535a
 */
kusano 7d535a
int
kusano 7d535a
TIFFReadImageIter(TIFF* tif,
kusano 7d535a
    uint32 rwidth, uint32 rheight, uint8* raster, int stop)
kusano 7d535a
{
kusano 7d535a
    char emsg[1024];
kusano 7d535a
    TIFFImageIter img;
kusano 7d535a
    int ok;
kusano 7d535a
kusano 7d535a
    if (TIFFImageIterBegin(&img, tif, stop, emsg)) {
kusano 7d535a
	/* XXX verify rwidth and rheight against width and height */
kusano 7d535a
	ok = TIFFImageIterGet(&img, raster, rwidth, img.height);
kusano 7d535a
	TIFFImageIterEnd(&img);
kusano 7d535a
    } else {
kusano 7d535a
	TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), emsg);
kusano 7d535a
	ok = 0;
kusano 7d535a
    }
kusano 7d535a
    return (ok);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Get an tile-organized image that has
kusano 7d535a
 *	PlanarConfiguration contiguous if SamplesPerPixel > 1
kusano 7d535a
 * or
kusano 7d535a
 *	SamplesPerPixel == 1
kusano 7d535a
 */	
kusano 7d535a
static int
kusano 7d535a
gtTileContig(TIFFImageIter* img, void *udata, uint32 w, uint32 h)
kusano 7d535a
{
kusano 7d535a
    TIFF* tif = img->tif;
kusano 7d535a
    ImageIterTileContigRoutine callback = img->callback.contig;
kusano 7d535a
    uint16 orientation;
kusano 7d535a
    uint32 col, row;
kusano 7d535a
    uint32 tw, th;
kusano 7d535a
    u_char* buf;
kusano 7d535a
    int32 fromskew;
kusano 7d535a
    uint32 nrow;
kusano 7d535a
kusano 7d535a
    buf = (u_char*) _TIFFmalloc(TIFFTileSize(tif));
kusano 7d535a
    if (buf == 0) {
kusano 7d535a
	TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
kusano 7d535a
	return (0);
kusano 7d535a
    }
kusano 7d535a
    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
kusano 7d535a
    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
kusano 7d535a
    orientation = img->orientation;
kusano 7d535a
    for (row = 0; row < h; row += th) {
kusano 7d535a
	nrow = (row + th > h ? h - row : th);
kusano 7d535a
	for (col = 0; col < w; col += tw) {
kusano 7d535a
	    if (TIFFReadTile(tif, buf, col, row, 0, 0) < 0 && img->stoponerr)
kusano 7d535a
		break;
kusano 7d535a
	    if (col + tw > w) {
kusano 7d535a
		/*
kusano 7d535a
		 * Tile is clipped horizontally.  Calculate
kusano 7d535a
		 * visible portion and skewing factors.
kusano 7d535a
		 */
kusano 7d535a
		uint32 npix = w - col;
kusano 7d535a
		fromskew = tw - npix;
kusano 7d535a
		(*callback)(img, udata, col, row, npix, nrow, fromskew, buf);
kusano 7d535a
	    } else {
kusano 7d535a
		(*callback)(img, udata, col, row, tw, nrow, 0, buf);
kusano 7d535a
	    }
kusano 7d535a
	}
kusano 7d535a
    }
kusano 7d535a
    _TIFFfree(buf);
kusano 7d535a
    return (1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Get an tile-organized image that has
kusano 7d535a
 *	 SamplesPerPixel > 1
kusano 7d535a
 *	 PlanarConfiguration separated
kusano 7d535a
 * We assume that all such images are RGB.
kusano 7d535a
 */	
kusano 7d535a
static int
kusano 7d535a
gtTileSeparate(TIFFImageIter* img, void *udata, uint32 w, uint32 h)
kusano 7d535a
{
kusano 7d535a
    TIFF* tif = img->tif;
kusano 7d535a
    ImageIterTileSeparateRoutine callback = img->callback.separate;
kusano 7d535a
    uint16 orientation;
kusano 7d535a
    uint32 col, row;
kusano 7d535a
    uint32 tw, th;
kusano 7d535a
    u_char* buf;
kusano 7d535a
    u_char* r;
kusano 7d535a
    u_char* g;
kusano 7d535a
    u_char* b;
kusano 7d535a
    u_char* a;
kusano 7d535a
    tsize_t tilesize;
kusano 7d535a
    int32 fromskew;
kusano 7d535a
    int alpha = img->alpha;
kusano 7d535a
    uint32 nrow;
kusano 7d535a
kusano 7d535a
    tilesize = TIFFTileSize(tif);
kusano 7d535a
    buf = (u_char*) _TIFFmalloc(4*tilesize);
kusano 7d535a
    if (buf == 0) {
kusano 7d535a
	TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
kusano 7d535a
	return (0);
kusano 7d535a
    }
kusano 7d535a
    r = buf;
kusano 7d535a
    g = r + tilesize;
kusano 7d535a
    b = g + tilesize;
kusano 7d535a
    a = b + tilesize;
kusano 7d535a
    if (!alpha)
kusano 7d535a
	memset(a, 0xff, tilesize);
kusano 7d535a
    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
kusano 7d535a
    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
kusano 7d535a
    orientation = img->orientation;
kusano 7d535a
    for (row = 0; row < h; row += th) {
kusano 7d535a
	nrow = (row + th > h ? h - row : th);
kusano 7d535a
	for (col = 0; col < w; col += tw) {
kusano 7d535a
	    if (TIFFReadTile(tif, r, col, row,0,0) < 0 && img->stoponerr)
kusano 7d535a
		break;
kusano 7d535a
	    if (TIFFReadTile(tif, g, col, row,0,1) < 0 && img->stoponerr)
kusano 7d535a
		break;
kusano 7d535a
	    if (TIFFReadTile(tif, b, col, row,0,2) < 0 && img->stoponerr)
kusano 7d535a
		break;
kusano 7d535a
	    if (alpha && TIFFReadTile(tif,a,col,row,0,3) < 0 && img->stoponerr)
kusano 7d535a
		break;
kusano 7d535a
	    if (col + tw > w) {
kusano 7d535a
		/*
kusano 7d535a
		 * Tile is clipped horizontally.  Calculate
kusano 7d535a
		 * visible portion and skewing factors.
kusano 7d535a
		 */
kusano 7d535a
		uint32 npix = w - col;
kusano 7d535a
		fromskew = tw - npix;
kusano 7d535a
		(*callback)(img, udata, col, row, npix, nrow, fromskew, r, g, b, a);
kusano 7d535a
	    } else {
kusano 7d535a
		(*callback)(img, udata, col, row, tw, nrow, 0, r, g, b, a);
kusano 7d535a
	    }
kusano 7d535a
	}
kusano 7d535a
    }
kusano 7d535a
    _TIFFfree(buf);
kusano 7d535a
    return (1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Get a strip-organized image that has
kusano 7d535a
 *	PlanarConfiguration contiguous if SamplesPerPixel > 1
kusano 7d535a
 * or
kusano 7d535a
 *	SamplesPerPixel == 1
kusano 7d535a
 */	
kusano 7d535a
static int
kusano 7d535a
gtStripContig(TIFFImageIter* img, void *udata, uint32 w, uint32 h)
kusano 7d535a
{
kusano 7d535a
    TIFF* tif = img->tif;
kusano 7d535a
    ImageIterTileContigRoutine callback = img->callback.contig;
kusano 7d535a
    uint16 orientation;
kusano 7d535a
    uint32 row, nrow;
kusano 7d535a
    u_char* buf;
kusano 7d535a
    uint32 rowsperstrip;
kusano 7d535a
    uint32 imagewidth = img->width;
kusano 7d535a
    tsize_t scanline;
kusano 7d535a
    int32 fromskew;
kusano 7d535a
kusano 7d535a
    buf = (u_char*) _TIFFmalloc(TIFFStripSize(tif));
kusano 7d535a
    if (buf == 0) {
kusano 7d535a
	TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
kusano 7d535a
	return (0);
kusano 7d535a
    }
kusano 7d535a
    orientation = img->orientation;
kusano 7d535a
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
kusano 7d535a
    scanline = TIFFScanlineSize(tif);
kusano 7d535a
    fromskew = (w < imagewidth ? imagewidth - w : 0);
kusano 7d535a
    for (row = 0; row < h; row += rowsperstrip) {
kusano 7d535a
	nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);
kusano 7d535a
	if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0),
kusano 7d535a
	    buf, nrow*scanline) < 0 && img->stoponerr)
kusano 7d535a
		break;
kusano 7d535a
	(*callback)(img, udata, 0, row, w, nrow, fromskew, buf);
kusano 7d535a
    }
kusano 7d535a
    _TIFFfree(buf);
kusano 7d535a
    return (1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Get a strip-organized image with
kusano 7d535a
 *	 SamplesPerPixel > 1
kusano 7d535a
 *	 PlanarConfiguration separated
kusano 7d535a
 * We assume that all such images are RGB.
kusano 7d535a
 */
kusano 7d535a
static int
kusano 7d535a
gtStripSeparate(TIFFImageIter* img, void *udata, uint32 w, uint32 h)
kusano 7d535a
{
kusano 7d535a
    TIFF* tif = img->tif;
kusano 7d535a
    ImageIterTileSeparateRoutine callback = img->callback.separate;
kusano 7d535a
    uint16 orientation;
kusano 7d535a
    u_char *buf;
kusano 7d535a
    u_char *r, *g, *b, *a;
kusano 7d535a
    uint32 row, nrow;
kusano 7d535a
    tsize_t scanline;
kusano 7d535a
    uint32 rowsperstrip;
kusano 7d535a
    uint32 imagewidth = img->width;
kusano 7d535a
    tsize_t stripsize;
kusano 7d535a
    int32 fromskew;
kusano 7d535a
    int alpha = img->alpha;
kusano 7d535a
kusano 7d535a
    stripsize = TIFFStripSize(tif);
kusano 7d535a
    r = buf = (u_char *)_TIFFmalloc(4*stripsize);
kusano 7d535a
    if (buf == 0) {
kusano 7d535a
	TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
kusano 7d535a
	return (0);
kusano 7d535a
    }
kusano 7d535a
    g = r + stripsize;
kusano 7d535a
    b = g + stripsize;
kusano 7d535a
    a = b + stripsize;
kusano 7d535a
    if (!alpha)
kusano 7d535a
	memset(a, 0xff, stripsize);
kusano 7d535a
    orientation = img->orientation;
kusano 7d535a
    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
kusano 7d535a
    scanline = TIFFScanlineSize(tif);
kusano 7d535a
    fromskew = (w < imagewidth ? imagewidth - w : 0);
kusano 7d535a
    for (row = 0; row < h; row += rowsperstrip) {
kusano 7d535a
	nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);
kusano 7d535a
	if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0),
kusano 7d535a
	    r, nrow*scanline) < 0 && img->stoponerr)
kusano 7d535a
	    break;
kusano 7d535a
	if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 1),
kusano 7d535a
	    g, nrow*scanline) < 0 && img->stoponerr)
kusano 7d535a
	    break;
kusano 7d535a
	if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 2),
kusano 7d535a
	    b, nrow*scanline) < 0 && img->stoponerr)
kusano 7d535a
	    break;
kusano 7d535a
	if (alpha &&
kusano 7d535a
	    (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 3),
kusano 7d535a
	    a, nrow*scanline) < 0 && img->stoponerr))
kusano 7d535a
	    break;
kusano 7d535a
	(*callback)(img, udata, 0, row, w, nrow, fromskew, r, g, b, a);
kusano 7d535a
    }
kusano 7d535a
    _TIFFfree(buf);
kusano 7d535a
    return (1);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
DECLAREContigCallbackFunc(TestContigCallback)
kusano 7d535a
{
kusano 7d535a
    printf("Contig Callback called with x = %d, y = %d, w = %d, h = %d, fromskew = %d\n",
kusano 7d535a
	   x, y, w, h, fromskew);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
DECLARESepCallbackFunc(TestSepCallback)
kusano 7d535a
{
kusano 7d535a
    printf("Sep Callback called with x = %d, y = %d, w = %d, h = %d, fromskew = %d\n",
kusano 7d535a
	   x, y, w, h, fromskew);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
#ifdef MAIN
kusano 7d535a
main(int argc, char **argv)
kusano 7d535a
{
kusano 7d535a
    char emsg[1024];
kusano 7d535a
    TIFFImageIter img;
kusano 7d535a
    int ok;
kusano 7d535a
    int stop = 1;
kusano 7d535a
kusano 7d535a
    TIFF *tif;
kusano 7d535a
    unsigned long nx, ny;
kusano 7d535a
    unsigned short BitsPerSample, SamplesPerPixel;
kusano 7d535a
    int isColorMapped, isPliFile;
kusano 7d535a
    unsigned char *ColorMap;
kusano 7d535a
    unsigned char *data;
kusano 7d535a
kusano 7d535a
    if (argc < 2) {
kusano 7d535a
	fprintf(stderr,"usage: %s tiff_file\n",argv[0]);
kusano 7d535a
	exit(1);
kusano 7d535a
    }
kusano 7d535a
    tif = (TIFF *)PLIGetImage(argv[1], (void *) &data, &ColorMap, 
kusano 7d535a
			      &nx, &ny, &BitsPerSample, &SamplesPerPixel, 
kusano 7d535a
			      &isColorMapped, &isPliFile);
kusano 7d535a
    if (tif != NULL) {
kusano 7d535a
kusano 7d535a
	if (TIFFImageIterBegin(&img, tif, stop, emsg)) {
kusano 7d535a
	    /* Here need to set data and callback function! */
kusano 7d535a
	    if (img.isContig) {
kusano 7d535a
		img.callback = TestContigCallback;
kusano 7d535a
	    } else {
kusano 7d535a
		img.callback = TestSepCallback;
kusano 7d535a
	    }
kusano 7d535a
	    ok = TIFFImageIterGet(&img, NULL, img.width, img.height);
kusano 7d535a
	    TIFFImageIterEnd(&img);
kusano 7d535a
	} else {
kusano 7d535a
	    TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), emsg);
kusano 7d535a
	}
kusano 7d535a
    }
kusano 7d535a
    
kusano 7d535a
}
kusano 7d535a
#endif
kusano 7d535a
/*
kusano 7d535a
 * Local Variables:
kusano 7d535a
 * mode: c
kusano 7d535a
 * c-basic-offset: 8
kusano 7d535a
 * fill-column: 78
kusano 7d535a
 * End:
kusano 7d535a
 */