roentgen b75cab
/* $Id: strip.c,v 1.4 2008/03/28 01:42:06 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 2004, Andrey Kiselev  <dron@ak4719.spb.edu></dron@ak4719.spb.edu>
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and 
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Sam Leffler and Silicon Graphics.
roentgen b75cab
 * 
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
roentgen b75cab
 * 
roentgen b75cab
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * TIFF Library
roentgen b75cab
 *
roentgen b75cab
 * Functions to test strip interface of libtiff.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
#include <string.h></string.h>
roentgen b75cab
roentgen b75cab
#include "tiffio.h"
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
write_strips(TIFF *tif, const tdata_t array, const tsize_t size)
roentgen b75cab
{
roentgen b75cab
	tstrip_t	strip, nstrips;
roentgen b75cab
	tsize_t		stripsize, offset;
roentgen b75cab
roentgen b75cab
	stripsize = TIFFStripSize(tif);
roentgen b75cab
	if (!stripsize) {
roentgen b75cab
		fprintf (stderr, "Wrong size of strip.\n");
roentgen b75cab
		return -1;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	nstrips = TIFFNumberOfStrips(tif);
roentgen b75cab
	for (offset = 0, strip = 0;
roentgen b75cab
	     offset < size && strip < nstrips;
roentgen b75cab
	     offset+=stripsize, strip++) {
roentgen b75cab
		/*
roentgen b75cab
		 * Properly write last strip.
roentgen b75cab
		 */
roentgen b75cab
		tsize_t	bufsize = size - offset;
roentgen b75cab
		if (bufsize > stripsize)
roentgen b75cab
			bufsize = stripsize;
roentgen b75cab
roentgen b75cab
		if (TIFFWriteEncodedStrip(tif, strip, (char *)array + offset,
roentgen b75cab
					  bufsize) != bufsize) {
roentgen b75cab
			fprintf (stderr, "Can't write strip %lu.\n",
roentgen b75cab
				 (unsigned long)strip);
roentgen b75cab
			return -1;
roentgen b75cab
		}
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
	return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
read_strips(TIFF *tif, const tdata_t array, const tsize_t size)
roentgen b75cab
{
roentgen b75cab
	tstrip_t	strip, nstrips;
roentgen b75cab
	tsize_t		stripsize, offset;
roentgen b75cab
	tdata_t		buf = NULL;
roentgen b75cab
roentgen b75cab
	stripsize = TIFFStripSize(tif);
roentgen b75cab
	if (!stripsize) {
roentgen b75cab
		fprintf (stderr, "Wrong size of strip.\n");
roentgen b75cab
		return -1;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	buf = _TIFFmalloc(stripsize);
roentgen b75cab
	if (!buf) {
roentgen b75cab
		fprintf (stderr, "Can't allocate space for strip buffer.\n");
roentgen b75cab
		return -1;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	nstrips = TIFFNumberOfStrips(tif);
roentgen b75cab
	for (offset = 0, strip = 0;
roentgen b75cab
	     offset < size && strip < nstrips;
roentgen b75cab
	     offset+=stripsize, strip++) {
roentgen b75cab
		/*
roentgen b75cab
		 * Properly read last strip.
roentgen b75cab
		 */
roentgen b75cab
		tsize_t	bufsize = size - offset;
roentgen b75cab
		if (bufsize > stripsize)
roentgen b75cab
			bufsize = stripsize;
roentgen b75cab
roentgen b75cab
		if (TIFFReadEncodedStrip(tif, strip, buf, -1) != bufsize) {
roentgen b75cab
			fprintf (stderr, "Can't read strip %lu.\n",
roentgen b75cab
				 (unsigned long)strip);
roentgen b75cab
			return -1;
roentgen b75cab
		}
roentgen b75cab
		if (memcmp(buf, (char *)array + offset, bufsize) != 0) {
roentgen b75cab
			fprintf (stderr, "Wrong data read for strip %lu.\n",
roentgen b75cab
				 (unsigned long)strip);
roentgen b75cab
			_TIFFfree(buf);
roentgen b75cab
			return -1;
roentgen b75cab
		}
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
	_TIFFfree(buf);
roentgen b75cab
roentgen b75cab
	return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
create_image_striped(const char *name, uint32 width, uint32 length,
roentgen b75cab
		      uint32 rowsperstrip, uint16 compression,
roentgen b75cab
		      uint16 spp, uint16 bps, uint16 photometric,
roentgen b75cab
		      uint16 sampleformat, uint16 planarconfig,
roentgen b75cab
		      const tdata_t array, const tsize_t size)
roentgen b75cab
{
roentgen b75cab
	TIFF		*tif;
roentgen b75cab
roentgen b75cab
	/* Test whether we can write tags. */
roentgen b75cab
	tif = TIFFOpen(name, "w");
roentgen b75cab
	if (!tif)
roentgen b75cab
		goto openfailure;
roentgen b75cab
roentgen b75cab
	if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width)) {
roentgen b75cab
		fprintf (stderr, "Can't set ImageWidth tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length)) {
roentgen b75cab
		fprintf (stderr, "Can't set ImageLength tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps)) {
roentgen b75cab
		fprintf (stderr, "Can't set BitsPerSample tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp)) {
roentgen b75cab
		fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip)) {
roentgen b75cab
		fprintf (stderr, "Can't set RowsPerStrip tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, planarconfig)) {
roentgen b75cab
		fprintf (stderr, "Can't set PlanarConfiguration tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric)) {
roentgen b75cab
		fprintf (stderr, "Can't set PhotometricInterpretation tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (write_strips(tif, array, size) < 0) {
roentgen b75cab
		fprintf (stderr, "Can't write image data.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	TIFFClose(tif);
roentgen b75cab
	return 0;
roentgen b75cab
roentgen b75cab
failure:
roentgen b75cab
	TIFFClose(tif);
roentgen b75cab
openfailure:
roentgen b75cab
	fprintf (stderr, "Can't create test TIFF file %s:\n"
roentgen b75cab
"    ImageWidth=%ld, ImageLength=%ld, RowsPerStrip=%ld, Compression=%d,\n"
roentgen b75cab
"    BitsPerSample=%d, SamplesPerPixel=%d, SampleFormat=%d,\n"
roentgen b75cab
"    PlanarConfiguration=%d, PhotometricInterpretation=%d.\n",
roentgen b75cab
		 name, (long) width, (long) length, (long) rowsperstrip,
roentgen b75cab
                 compression, bps, spp, sampleformat, planarconfig,
roentgen b75cab
		 photometric);
roentgen b75cab
	return -1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
read_image_striped(const char *name, uint32 width, uint32 length,
roentgen b75cab
		    uint32 rowsperstrip, uint16 compression,
roentgen b75cab
		    uint16 spp, uint16 bps, uint16 photometric,
roentgen b75cab
		    uint16 sampleformat, uint16 planarconfig,
roentgen b75cab
		    const tdata_t array, const tsize_t size)
roentgen b75cab
{
roentgen b75cab
	TIFF		*tif;
roentgen b75cab
	uint16		value_u16;
roentgen b75cab
	uint32		value_u32;
roentgen b75cab
roentgen b75cab
	/* Test whether we can read written values. */
roentgen b75cab
	tif = TIFFOpen(name, "r");
roentgen b75cab
	if (!tif)
roentgen b75cab
		goto openfailure;
roentgen b75cab
	
roentgen b75cab
	if (TIFFIsTiled(tif)) {
roentgen b75cab
		fprintf (stderr, "Can't read image %s, it is tiled.\n",
roentgen b75cab
			 name);
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &value_u32)
roentgen b75cab
	    || value_u32 != width) {
roentgen b75cab
		fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_IMAGEWIDTH);
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &value_u32)
roentgen b75cab
	    || value_u32 != length) {
roentgen b75cab
		fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_IMAGELENGTH);
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &value_u16)
roentgen b75cab
	    || value_u16 != bps) {
roentgen b75cab
		fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_BITSPERSAMPLE);
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &value_u16)
roentgen b75cab
	    || value_u16 != photometric) {
roentgen b75cab
		fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_PHOTOMETRIC);
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &value_u16)
roentgen b75cab
	    || value_u16 != spp) {
roentgen b75cab
		fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_SAMPLESPERPIXEL);
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &value_u32)
roentgen b75cab
	    || value_u32 != rowsperstrip) {
roentgen b75cab
		fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_ROWSPERSTRIP);
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &value_u16)
roentgen b75cab
	    || value_u16 != planarconfig) {
roentgen b75cab
		fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_PLANARCONFIG);
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (read_strips(tif, array, size) < 0) {
roentgen b75cab
		fprintf (stderr, "Can't read image data.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	TIFFClose(tif);
roentgen b75cab
	return 0;
roentgen b75cab
roentgen b75cab
failure:
roentgen b75cab
	TIFFClose(tif);
roentgen b75cab
openfailure:
roentgen b75cab
	fprintf (stderr, "Can't read test TIFF file %s:\n"
roentgen b75cab
"    ImageWidth=%ld, ImageLength=%ld, RowsPerStrip=%ld, Compression=%d,\n"
roentgen b75cab
"    BitsPerSample=%d, SamplesPerPixel=%d, SampleFormat=%d,\n"
roentgen b75cab
"    PlanarConfiguration=%d, PhotometricInterpretation=%d.\n",
roentgen b75cab
		 name, (long) width, (long) length, (long) rowsperstrip,
roentgen b75cab
                 compression, bps, spp, sampleformat, planarconfig,
roentgen b75cab
		 photometric);
roentgen b75cab
	return -1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
write_scanlines(TIFF *tif, const tdata_t array, const tsize_t size)
roentgen b75cab
{
roentgen b75cab
	uint32		length, row;
roentgen b75cab
	tsize_t		scanlinesize, offset;
roentgen b75cab
        (void) size;
roentgen b75cab
roentgen b75cab
	if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &length)) {
roentgen b75cab
		fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_IMAGELENGTH);
roentgen b75cab
		return -1;
roentgen b75cab
	}
roentgen b75cab
	
roentgen b75cab
	scanlinesize = TIFFScanlineSize(tif);
roentgen b75cab
	if (!scanlinesize) {
roentgen b75cab
		fprintf (stderr, "Wrong size of scanline.\n");
roentgen b75cab
		return -1;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	for (offset = 0, row = 0; row < length; offset+=scanlinesize, row++) {
roentgen b75cab
		if (TIFFWriteScanline(tif, (char *)array + offset, row, 0) < 0) {
roentgen b75cab
			fprintf (stderr,
roentgen b75cab
				 "Can't write image data at row %lu.\n", (long) row);
roentgen b75cab
			return -1;
roentgen b75cab
		}
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
	return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */