Blame test/custom_dir.c

Packit 7838c8
/* $Id: custom_dir.c,v 1.3 2013-12-17 14:41:58 bfriesen Exp $ */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Copyright (c) 2012, Frank Warmerdam <warmerdam@pobox.com>
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 handling of custom directories like EXIF.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#include "tif_config.h"
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
#include "tif_dir.h"
Packit 7838c8
#include "tifftest.h"
Packit 7838c8
Packit 7838c8
static const char filename[] = "custom_dir.tif";
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 TIFFField
Packit 7838c8
customFields[] = {
Packit 7838c8
	{ TIFFTAG_IMAGEWIDTH, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Custom1", NULL },
Packit 7838c8
	{ TIFFTAG_DOTRANGE, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Custom2", NULL },
Packit 7838c8
};
Packit 7838c8
Packit 7838c8
static TIFFFieldArray customFieldArray = { tfiatOther, 0, 2, customFields };
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
main()
Packit 7838c8
{
Packit 7838c8
	TIFF		*tif;
Packit 7838c8
	unsigned char	buf[SPP] = { 0, 127, 255 };
Packit 7838c8
	uint64          dir_offset = 0, dir_offset2 = 0;
Packit 7838c8
	uint64          read_dir_offset = 0, read_dir_offset2 = 0;
Packit 7838c8
	uint64          *dir_offset2_ptr = NULL;
Packit 7838c8
	char           *ascii_value;
Packit 7838c8
	uint16          count16 = 0;
Packit 7838c8
	
Packit 7838c8
Packit 7838c8
	/* We write the main directory as a simple image. */
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
	/* 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
        if (!TIFFWriteDirectory( tif )) {
Packit 7838c8
		fprintf (stderr, "TIFFWriteDirectory() failed.\n");
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	/* 
Packit 7838c8
	 * Now create an EXIF directory. 
Packit 7838c8
	 */
Packit 7838c8
	if (TIFFCreateEXIFDirectory(tif) != 0) {
Packit 7838c8
		fprintf (stderr, "TIFFCreateEXIFDirectory() failed.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	if (!TIFFSetField( tif, EXIFTAG_SPECTRALSENSITIVITY, "EXIF Spectral Sensitivity")) {
Packit 7838c8
		fprintf (stderr, "Can't write SPECTRALSENSITIVITY\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}		
Packit 7838c8
	
Packit 7838c8
        if (!TIFFWriteCustomDirectory( tif, &dir_offset )) {
Packit 7838c8
		fprintf (stderr, "TIFFWriteCustomDirectory() with EXIF failed.\n");
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * Now create a custom directory with tags that conflict with mainline 
Packit 7838c8
	 * TIFF tags.
Packit 7838c8
	 */
Packit 7838c8
	
Packit 7838c8
	TIFFFreeDirectory( tif );
Packit 7838c8
	if (TIFFCreateCustomDirectory(tif, &customFieldArray) != 0) {
Packit 7838c8
		fprintf (stderr, "TIFFCreateEXIFDirectory() failed.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	if (!TIFFSetField( tif, TIFFTAG_IMAGEWIDTH, "*Custom1")) { /* not really IMAGEWIDTH */
Packit 7838c8
		fprintf (stderr, "Can't write pseudo-IMAGEWIDTH.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}		
Packit 7838c8
	
Packit 7838c8
	if (!TIFFSetField( tif, TIFFTAG_DOTRANGE, "*Custom2")) { /* not really DOTWIDTH */
Packit 7838c8
		fprintf (stderr, "Can't write pseudo-DOTWIDTH.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}		
Packit 7838c8
	
Packit 7838c8
        if (!TIFFWriteCustomDirectory( tif, &dir_offset2 )) {
Packit 7838c8
		fprintf (stderr, "TIFFWriteCustomDirectory() with EXIF failed.\n");
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * Go back to the first directory, and add the EXIFIFD pointer. 
Packit 7838c8
	 */
Packit 7838c8
	TIFFSetDirectory(tif, 0);
Packit 7838c8
	TIFFSetField(tif, TIFFTAG_EXIFIFD, dir_offset );
Packit 7838c8
	TIFFSetField(tif, TIFFTAG_SUBIFD, 1, &dir_offset2 );
Packit 7838c8
Packit 7838c8
	TIFFClose(tif);
Packit 7838c8
	
Packit 7838c8
	/* Ok, now test whether we can read written values in the EXIF directory. */
Packit 7838c8
	tif = TIFFOpen(filename, "r");
Packit 7838c8
	
Packit 7838c8
	TIFFGetField(tif, TIFFTAG_EXIFIFD, &read_dir_offset );
Packit 7838c8
	if( read_dir_offset != dir_offset ) {
Packit 7838c8
		fprintf (stderr, "Did not get expected EXIFIFD.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	TIFFGetField(tif, TIFFTAG_SUBIFD, &count16, &dir_offset2_ptr );
Packit 7838c8
	read_dir_offset2 = dir_offset2_ptr[0];
Packit 7838c8
	if( read_dir_offset2 != dir_offset2 || count16 != 1) {
Packit 7838c8
		fprintf (stderr, "Did not get expected SUBIFD.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	if( !TIFFReadEXIFDirectory(tif, read_dir_offset) ) {
Packit 7838c8
		fprintf (stderr, "TIFFReadEXIFDirectory() failed.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
	
Packit 7838c8
	if (!TIFFGetField( tif, EXIFTAG_SPECTRALSENSITIVITY, &ascii_value) ) {
Packit 7838c8
		fprintf (stderr, "reading SPECTRALSENSITIVITY failed.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	if( strcmp(ascii_value,"EXIF Spectral Sensitivity") != 0) {
Packit 7838c8
		fprintf (stderr, "got wrong SPECTRALSENSITIVITY value.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	/* Try reading the Custom directory */
Packit 7838c8
	
Packit 7838c8
	if( !TIFFReadCustomDirectory(tif, read_dir_offset2, &customFieldArray) ) {
Packit 7838c8
		fprintf (stderr, "TIFFReadCustomDirectory() failed.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
	
Packit 7838c8
	if (!TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &ascii_value) ) {
Packit 7838c8
		fprintf (stderr, "reading pseudo-IMAGEWIDTH failed.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	if( strcmp(ascii_value,"*Custom1") != 0) {
Packit 7838c8
		fprintf (stderr, "got wrong pseudo-IMAGEWIDTH value.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	if (!TIFFGetField( tif, TIFFTAG_DOTRANGE, &ascii_value) ) {
Packit 7838c8
		fprintf (stderr, "reading pseudo-DOTRANGE failed.\n" );
Packit 7838c8
		goto failure;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	if( strcmp(ascii_value,"*Custom2") != 0) {
Packit 7838c8
		fprintf (stderr, "got wrong pseudo-DOTRANGE value.\n" );
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: */