Blame test/short_tag.c

Packit 7838c8
/* $Id: short_tag.c,v 1.9 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
 * Module to test SHORT tags read/write functions.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#include "tif_config.h"
Packit 7838c8
Packit 7838c8
#include <stdio.h>
Packit 7838c8
Packit 7838c8
#ifdef HAVE_UNISTD_H 
Packit 7838c8
# include <unistd.h> 
Packit 7838c8
#endif 
Packit 7838c8
Packit 7838c8
#include "tiffio.h"
Packit 7838c8
#include "tifftest.h"
Packit 7838c8
Packit 7838c8
static const char filename[] = "short_test.tiff";
Packit 7838c8
Packit 7838c8
#define	SPP	3		/* Samples per pixel */
Packit 7838c8
const uint16	width = 1;
Packit 7838c8
const uint16	length = 1;
Packit 7838c8
const uint16	bps = 8;
Packit 7838c8
const uint16	photometric = PHOTOMETRIC_RGB;
Packit 7838c8
const uint16	rows_per_strip = 1;
Packit 7838c8
const uint16	planarconfig = PLANARCONFIG_CONTIG;
Packit 7838c8
Packit 7838c8
static const struct {
Packit 7838c8
	const ttag_t	tag;
Packit 7838c8
	const uint16	value;
Packit 7838c8
} short_single_tags[] = {
Packit 7838c8
	{ TIFFTAG_COMPRESSION, COMPRESSION_NONE },
Packit 7838c8
	{ TIFFTAG_FILLORDER, FILLORDER_MSB2LSB },
Packit 7838c8
	{ TIFFTAG_ORIENTATION, ORIENTATION_BOTRIGHT },
Packit 7838c8
	{ TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH },
Packit 7838c8
	{ TIFFTAG_MINSAMPLEVALUE, 23 },
Packit 7838c8
	{ TIFFTAG_MAXSAMPLEVALUE, 241 },
Packit 7838c8
	{ TIFFTAG_INKSET, INKSET_MULTIINK },
Packit 7838c8
	{ TIFFTAG_NUMBEROFINKS, SPP },
Packit 7838c8
	{ TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT }
Packit 7838c8
};
Packit 7838c8
#define NSINGLETAGS   (sizeof(short_single_tags) / sizeof(short_single_tags[0]))
Packit 7838c8
Packit 7838c8
static const struct {
Packit 7838c8
	const ttag_t	tag;
Packit 7838c8
	const uint16	values[2];
Packit 7838c8
} short_paired_tags[] = {
Packit 7838c8
	{ TIFFTAG_PAGENUMBER, {1, 1} },
Packit 7838c8
	{ TIFFTAG_HALFTONEHINTS, {0, 255} },
Packit 7838c8
	{ TIFFTAG_DOTRANGE, {8, 16} },
Packit 7838c8
	{ TIFFTAG_YCBCRSUBSAMPLING, {2, 1} }
Packit 7838c8
};
Packit 7838c8
#define NPAIREDTAGS   (sizeof(short_paired_tags) / sizeof(short_paired_tags[0]))
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
main()
Packit 7838c8
{
Packit 7838c8
	TIFF		*tif;
Packit 7838c8
	size_t		i;
Packit 7838c8
	unsigned char	buf[SPP] = { 0, 127, 255 };
Packit 7838c8
Packit 7838c8
	/* Test whether we can write tags. */
Packit 7838c8
	tif = TIFFOpen(filename, "w");
Packit 7838c8
	if (!tif) {
Packit 7838c8
		fprintf (stderr, "Can't create test TIFF file %s.\n", filename);
Packit 7838c8
		return 1;
Packit 7838c8
	}
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, rows_per_strip)) {
Packit 7838c8
		fprintf (stderr, "Can't set SamplesPerPixel 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
	for (i = 0; i < NSINGLETAGS; i++) {
Packit 7838c8
		if (!TIFFSetField(tif, short_single_tags[i].tag,
Packit 7838c8
				  short_single_tags[i].value)) {
Packit 7838c8
			fprintf(stderr, "Can't set tag %lu.\n",
Packit 7838c8
				(unsigned long)short_single_tags[i].tag);
Packit 7838c8
			goto failure;
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	for (i = 0; i < NPAIREDTAGS; i++) {
Packit 7838c8
		if (!TIFFSetField(tif, short_paired_tags[i].tag,
Packit 7838c8
				  short_paired_tags[i].values[0],
Packit 7838c8
				  short_paired_tags[i].values[1])) {
Packit 7838c8
			fprintf(stderr, "Can't set tag %lu.\n",
Packit 7838c8
				(unsigned long)short_paired_tags[i].tag);
Packit 7838c8
			goto failure;
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	/* Write dummy pixel data. */
Packit 7838c8
	if (TIFFWriteScanline(tif, buf, 0, 0) == -1) {
Packit 7838c8
		fprintf (stderr, "Can't write image data.\n");
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	TIFFClose(tif);
Packit 7838c8
	
Packit 7838c8
	/* Ok, now test whether we can read written values. */
Packit 7838c8
	tif = TIFFOpen(filename, "r");
Packit 7838c8
	if (!tif) {
Packit 7838c8
		fprintf (stderr, "Can't open test TIFF file %s.\n", filename);
Packit 7838c8
		return 1;
Packit 7838c8
	}
Packit 7838c8
	
Packit 7838c8
	if (CheckLongField(tif, TIFFTAG_IMAGEWIDTH, width) < 0)
Packit 7838c8
		goto failure;
Packit 7838c8
Packit 7838c8
	if (CheckLongField(tif, TIFFTAG_IMAGELENGTH, length) < 0)
Packit 7838c8
		goto failure;
Packit 7838c8
Packit 7838c8
	if (CheckShortField(tif, TIFFTAG_BITSPERSAMPLE, bps) < 0)
Packit 7838c8
		goto failure;
Packit 7838c8
Packit 7838c8
	if (CheckShortField(tif, TIFFTAG_PHOTOMETRIC, photometric) < 0)
Packit 7838c8
		goto failure;
Packit 7838c8
Packit 7838c8
	if (CheckShortField(tif, TIFFTAG_SAMPLESPERPIXEL, SPP) < 0)
Packit 7838c8
		goto failure;
Packit 7838c8
Packit 7838c8
	if (CheckLongField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip) < 0)
Packit 7838c8
		goto failure;
Packit 7838c8
Packit 7838c8
	if (CheckShortField(tif, TIFFTAG_PLANARCONFIG, planarconfig) < 0)
Packit 7838c8
		goto failure;
Packit 7838c8
Packit 7838c8
	for (i = 0; i < NSINGLETAGS; i++) {
Packit 7838c8
		if (CheckShortField(tif, short_single_tags[i].tag,
Packit 7838c8
				    short_single_tags[i].value) < 0)
Packit 7838c8
			goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	for (i = 0; i < NPAIREDTAGS; i++) {
Packit 7838c8
		if (CheckShortPairedField(tif, short_paired_tags[i].tag,
Packit 7838c8
					  short_paired_tags[i].values) < 0)
Packit 7838c8
			goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	TIFFClose(tif);
Packit 7838c8
	
Packit 7838c8
	/* All tests passed; delete file and exit with success status. */
Packit 7838c8
	unlink(filename);
Packit 7838c8
	return 0;
Packit 7838c8
Packit 7838c8
failure:
Packit 7838c8
	/* 
Packit 7838c8
	 * Something goes wrong; close file and return unsuccessful status.
Packit 7838c8
	 * Do not remove the file for further manual investigation.
Packit 7838c8
	 */
Packit 7838c8
	TIFFClose(tif);
Packit 7838c8
	return 1;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/* vim: set ts=8 sts=8 sw=8 noet: */