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