Blame test/strip.c

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