Blame test/strip.c

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