roentgen b75cab
/* $Id: short_tag.c,v 1.8 2008/04/15 14:20:30 dron 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 SHORT 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
#include "tifftest.h"
roentgen b75cab
roentgen b75cab
static const char filename[] = "short_test.tiff";
roentgen b75cab
roentgen b75cab
#define	SPP	3		/* Samples per pixel */
roentgen b75cab
const uint16	width = 1;
roentgen b75cab
const uint16	length = 1;
roentgen b75cab
const uint16	bps = 8;
roentgen b75cab
const uint16	photometric = PHOTOMETRIC_RGB;
roentgen b75cab
const uint16	rows_per_strip = 1;
roentgen b75cab
const uint16	planarconfig = PLANARCONFIG_CONTIG;
roentgen b75cab
roentgen b75cab
static const struct {
roentgen b75cab
	const ttag_t	tag;
roentgen b75cab
	const uint16	value;
roentgen b75cab
} short_single_tags[] = {
roentgen b75cab
	{ TIFFTAG_COMPRESSION, COMPRESSION_NONE },
roentgen b75cab
	{ TIFFTAG_FILLORDER, FILLORDER_MSB2LSB },
roentgen b75cab
	{ TIFFTAG_ORIENTATION, ORIENTATION_BOTRIGHT },
roentgen b75cab
	{ TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH },
roentgen b75cab
	{ TIFFTAG_MINSAMPLEVALUE, 23 },
roentgen b75cab
	{ TIFFTAG_MAXSAMPLEVALUE, 241 },
roentgen b75cab
	{ TIFFTAG_INKSET, INKSET_MULTIINK },
roentgen b75cab
	{ TIFFTAG_NUMBEROFINKS, SPP },
roentgen b75cab
	{ TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT }
roentgen b75cab
};
roentgen b75cab
#define NSINGLETAGS   (sizeof(short_single_tags) / sizeof(short_single_tags[0]))
roentgen b75cab
roentgen b75cab
static const struct {
roentgen b75cab
	const ttag_t	tag;
roentgen b75cab
	const uint16	values[2];
roentgen b75cab
} short_paired_tags[] = {
roentgen b75cab
	{ TIFFTAG_PAGENUMBER, {1, 1} },
roentgen b75cab
	{ TIFFTAG_HALFTONEHINTS, {0, 255} },
roentgen b75cab
	{ TIFFTAG_DOTRANGE, {8, 16} },
roentgen b75cab
	{ TIFFTAG_YCBCRSUBSAMPLING, {2, 1} }
roentgen b75cab
};
roentgen b75cab
#define NPAIREDTAGS   (sizeof(short_paired_tags) / sizeof(short_paired_tags[0]))
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
main()
roentgen b75cab
{
roentgen b75cab
	TIFF		*tif;
roentgen b75cab
	size_t		i;
roentgen b75cab
	unsigned char	buf[SPP] = { 0, 127, 255 };
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, 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, 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)) {
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
	for (i = 0; i < NSINGLETAGS; i++) {
roentgen b75cab
		if (!TIFFSetField(tif, short_single_tags[i].tag,
roentgen b75cab
				  short_single_tags[i].value)) {
roentgen b75cab
			fprintf(stderr, "Can't set tag %lu.\n",
roentgen b75cab
				(unsigned long)short_single_tags[i].tag);
roentgen b75cab
			goto failure;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	for (i = 0; i < NPAIREDTAGS; i++) {
roentgen b75cab
		if (!TIFFSetField(tif, short_paired_tags[i].tag,
roentgen b75cab
				  short_paired_tags[i].values[0],
roentgen b75cab
				  short_paired_tags[i].values[1])) {
roentgen b75cab
			fprintf(stderr, "Can't set tag %lu.\n",
roentgen b75cab
				(unsigned long)short_paired_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 (CheckShortField(tif, TIFFTAG_BITSPERSAMPLE, bps) < 0)
roentgen b75cab
		goto failure;
roentgen b75cab
roentgen b75cab
	if (CheckShortField(tif, TIFFTAG_PHOTOMETRIC, photometric) < 0)
roentgen b75cab
		goto failure;
roentgen b75cab
roentgen b75cab
	if (CheckShortField(tif, TIFFTAG_SAMPLESPERPIXEL, SPP) < 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
	if (CheckShortField(tif, TIFFTAG_PLANARCONFIG, planarconfig) < 0)
roentgen b75cab
		goto failure;
roentgen b75cab
roentgen b75cab
	for (i = 0; i < NSINGLETAGS; i++) {
roentgen b75cab
		if (CheckShortField(tif, short_single_tags[i].tag,
roentgen b75cab
				    short_single_tags[i].value) < 0)
roentgen b75cab
			goto failure;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	for (i = 0; i < NPAIREDTAGS; i++) {
roentgen b75cab
		if (CheckShortPairedField(tif, short_paired_tags[i].tag,
roentgen b75cab
					  short_paired_tags[i].values) < 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
	/* 
roentgen b75cab
	 * Something goes wrong; close file and return unsuccessful status.
roentgen b75cab
	 * Do not remove the file for further manual investigation.
roentgen b75cab
	 */
roentgen b75cab
	TIFFClose(tif);
roentgen b75cab
	return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */