Blame test/ascii_tag.c

Packit 7838c8
/* $Id: ascii_tag.c,v 1.8 2013-12-17 14:41:57 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 ASCII tags read/write functions.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#include "tif_config.h"
Packit 7838c8
Packit 7838c8
#include <stdio.h>
Packit 7838c8
#include <string.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
Packit 7838c8
static const char filename[] = "ascii_test.tiff";
Packit 7838c8
Packit 7838c8
static const struct {
Packit 7838c8
	ttag_t		tag;
Packit 7838c8
	const char	*value;
Packit 7838c8
} ascii_tags[] = {
Packit 7838c8
	{ TIFFTAG_DOCUMENTNAME, "Test TIFF image" },
Packit 7838c8
	{ TIFFTAG_IMAGEDESCRIPTION, "Temporary test image" },
Packit 7838c8
	{ TIFFTAG_MAKE, "This is not scanned image" },
Packit 7838c8
	{ TIFFTAG_MODEL, "No scanner" },
Packit 7838c8
	{ TIFFTAG_PAGENAME, "Test page" },
Packit 7838c8
	{ TIFFTAG_SOFTWARE, "Libtiff library" },
Packit 7838c8
	{ TIFFTAG_DATETIME, "2004:09:10 16:09:00" },
Packit 7838c8
	{ TIFFTAG_ARTIST, "Andrey V. Kiselev" },
Packit 7838c8
	{ TIFFTAG_HOSTCOMPUTER, "Debian GNU/Linux (Sarge)" },
Packit 7838c8
	{ TIFFTAG_TARGETPRINTER, "No printer" },
Packit 7838c8
	{ TIFFTAG_COPYRIGHT, "Copyright (c) 2004, Andrey Kiselev" },
Packit 7838c8
	{ TIFFTAG_FAXSUBADDRESS, "Fax subaddress" },
Packit 7838c8
	/* DGN tags */
Packit 7838c8
	{ TIFFTAG_UNIQUECAMERAMODEL, "No camera" },
Packit 7838c8
	{ TIFFTAG_CAMERASERIALNUMBER, "1234567890" }
Packit 7838c8
};
Packit 7838c8
#define NTAGS   (sizeof (ascii_tags) / sizeof (ascii_tags[0]))
Packit 7838c8
Packit 7838c8
static const char ink_names[] = "Red\0Green\0Blue";
Packit 7838c8
const int ink_names_size = 15;
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
main()
Packit 7838c8
{
Packit 7838c8
	TIFF		*tif;
Packit 7838c8
	size_t		i;
Packit 7838c8
	unsigned char	buf[] = { 0, 127, 255 };
Packit 7838c8
	char		*value;
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, 1)) {
Packit 7838c8
		fprintf (stderr, "Can't set ImageWidth tag.\n");
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
	if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, 1)) {
Packit 7838c8
		fprintf (stderr, "Can't set ImageLength tag.\n");
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
	if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8)) {
Packit 7838c8
		fprintf (stderr, "Can't set BitsPerSample tag.\n");
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
	if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, sizeof(buf))) {
Packit 7838c8
		fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
	if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)) {
Packit 7838c8
		fprintf (stderr, "Can't set PlanarConfiguration tag.\n");
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
	if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)) {
Packit 7838c8
		fprintf (stderr, "Can't set PhotometricInterpretation tag.\n");
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	for (i = 0; i < NTAGS; i++) {
Packit 7838c8
		if (!TIFFSetField(tif, ascii_tags[i].tag,
Packit 7838c8
				  ascii_tags[i].value)) {
Packit 7838c8
			fprintf(stderr, "Can't set tag %lu.\n",
Packit 7838c8
				(unsigned long)ascii_tags[i].tag);
Packit 7838c8
			goto failure;
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	/* InkNames tag has special form, so we handle it separately. */
Packit 7838c8
	if (!TIFFSetField(tif, TIFFTAG_NUMBEROFINKS, 3)) {
Packit 7838c8
		fprintf (stderr, "Can't set tag %d (NUMBEROFINKS).\n",
Packit 7838c8
                         TIFFTAG_NUMBEROFINKS);
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
	if (!TIFFSetField(tif, TIFFTAG_INKNAMES, ink_names_size, ink_names)) {
Packit 7838c8
		fprintf (stderr, "Can't set tag %d (INKNAMES).\n",
Packit 7838c8
			 TIFFTAG_INKNAMES);
Packit 7838c8
		goto failure;
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
	for (i = 0; i < NTAGS; i++) {
Packit 7838c8
		if (!TIFFGetField(tif, ascii_tags[i].tag, &value)
Packit 7838c8
		    || strcmp(value, ascii_tags[i].value)) {
Packit 7838c8
			fprintf(stderr, "Can't get tag %lu.\n",
Packit 7838c8
				(unsigned long)ascii_tags[i].tag);
Packit 7838c8
			goto failure;
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	if (!TIFFGetField(tif, TIFFTAG_INKNAMES, &value)
Packit 7838c8
	    || memcmp(value, ink_names, ink_names_size)) {
Packit 7838c8
		fprintf (stderr, "Can't get tag %d (INKNAMES).\n",
Packit 7838c8
			 TIFFTAG_INKNAMES);
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: */