roentgen b75cab
/* $Id: long_tag.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
 * Module to test LONG tags read/write functions.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include "tif_config.h"
roentgen b75cab
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
roentgen b75cab
#ifdef HAVE_UNISTD_H 
roentgen b75cab
# include <unistd.h> </unistd.h>
roentgen b75cab
#endif 
roentgen b75cab
roentgen b75cab
#include "tiffio.h"
roentgen b75cab
roentgen b75cab
extern int CheckLongField(TIFF *, ttag_t, uint32);
roentgen b75cab
roentgen b75cab
const char	*filename = "long_test.tiff";
roentgen b75cab
roentgen b75cab
static struct Tags {
roentgen b75cab
	ttag_t		tag;
roentgen b75cab
	short		count;
roentgen b75cab
	uint32		value;
roentgen b75cab
} long_tags[] = {
roentgen b75cab
	{ TIFFTAG_SUBFILETYPE, 1, FILETYPE_REDUCEDIMAGE|FILETYPE_PAGE|FILETYPE_MASK }
roentgen b75cab
};
roentgen b75cab
#define NTAGS   (sizeof (long_tags) / sizeof (long_tags[0]))
roentgen b75cab
roentgen b75cab
const uint32	width = 1;
roentgen b75cab
const uint32	length = 1;
roentgen b75cab
const uint32	rows_per_strip = 1;
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
main(int argc, char **argv)
roentgen b75cab
{
roentgen b75cab
	TIFF		*tif;
roentgen b75cab
	unsigned int	i;
roentgen b75cab
	unsigned char	buf[3] = { 0, 127, 255 };
roentgen b75cab
        (void) argc;
roentgen b75cab
        (void) argv;
roentgen b75cab
roentgen b75cab
	/* Test whether we can write tags. */
roentgen b75cab
	tif = TIFFOpen(filename, "w");
roentgen b75cab
	if (!tif) {
roentgen b75cab
		fprintf (stderr, "Can't create test TIFF file %s.\n", filename);
roentgen b75cab
		return 1;
roentgen b75cab
	}
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, 8)) {
roentgen b75cab
		fprintf (stderr, "Can't set BitsPerSample tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3)) {
roentgen b75cab
		fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip)) {
roentgen b75cab
		fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)) {
roentgen b75cab
		fprintf (stderr, "Can't set PlanarConfiguration tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
	if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)) {
roentgen b75cab
		fprintf (stderr, "Can't set PhotometricInterpretation tag.\n");
roentgen b75cab
		goto failure;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	for (i = 0; i < NTAGS; i++) {
roentgen b75cab
		if (!TIFFSetField(tif, long_tags[i].tag,
roentgen b75cab
				  long_tags[i].value)) {
roentgen b75cab
			fprintf(stderr, "Can't set tag %d.\n",
roentgen b75cab
				(int)long_tags[i].tag);
roentgen b75cab
			goto failure;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/* Write dummy pixel data. */
roentgen b75cab
	if (!TIFFWriteScanline(tif, buf, 0, 0) < 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
	
roentgen b75cab
	/* Ok, now test whether we can read written values. */
roentgen b75cab
	tif = TIFFOpen(filename, "r");
roentgen b75cab
	if (!tif) {
roentgen b75cab
		fprintf (stderr, "Can't open test TIFF file %s.\n", filename);
roentgen b75cab
		return 1;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (CheckLongField(tif, TIFFTAG_IMAGEWIDTH, width) < 0)
roentgen b75cab
		goto failure;
roentgen b75cab
roentgen b75cab
	if (CheckLongField(tif, TIFFTAG_IMAGELENGTH, length) < 0)
roentgen b75cab
		goto failure;
roentgen b75cab
roentgen b75cab
	if (CheckLongField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip) < 0)
roentgen b75cab
		goto failure;
roentgen b75cab
roentgen b75cab
	for (i = 0; i < NTAGS; i++) {
roentgen b75cab
		if (CheckLongField(tif, long_tags[i].tag,
roentgen b75cab
				   long_tags[i].value) < 0)
roentgen b75cab
			goto failure;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	TIFFClose(tif);
roentgen b75cab
	
roentgen b75cab
	/* All tests passed; delete file and exit with success status. */
roentgen b75cab
	unlink(filename);
roentgen b75cab
	return 0;
roentgen b75cab
roentgen b75cab
failure:
roentgen b75cab
	/* Something goes wrong; close file and return unsuccessful status. */
roentgen b75cab
	TIFFClose(tif);
roentgen b75cab
	unlink(filename);
roentgen b75cab
	return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */