1 /* $Id: custom_dir.c,v 1.3 2013-12-17 14:41:58 bfriesen Exp $ */
2 
3 /*
4  * Copyright (c) 2012, Frank Warmerdam <warmerdam@pobox.com>
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and
7  * its documentation for any purpose is hereby granted without fee, provided
8  * that (i) the above copyright notices and this permission notice appear in
9  * all copies of the software and related documentation, and (ii) the names of
10  * Sam Leffler and Silicon Graphics may not be used in any advertising or
11  * publicity relating to the software without the specific, prior written
12  * permission of Sam Leffler and Silicon Graphics.
13  *
14  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17  *
18  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23  * OF THIS SOFTWARE.
24  */
25 
26 /*
27  * TIFF Library
28  *
29  * Module to handling of custom directories like EXIF.
30  */
31 
32 #include "tif_config.h"
33 #include <stdio.h>
34 #include <string.h>
35 
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 
40 #include "tiffio.h"
41 #include "tif_dir.h"
42 #include "tifftest.h"
43 
44 static const char filename[] = "custom_dir.tif";
45 
46 #define	SPP	3		/* Samples per pixel */
47 const uint16	width = 1;
48 const uint16	length = 1;
49 const uint16	bps = 8;
50 const uint16	photometric = PHOTOMETRIC_RGB;
51 const uint16	rows_per_strip = 1;
52 const uint16	planarconfig = PLANARCONFIG_CONTIG;
53 
54 static TIFFField
55 customFields[] = {
56 	{ TIFFTAG_IMAGEWIDTH, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Custom1", NULL },
57 	{ TIFFTAG_DOTRANGE, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Custom2", NULL },
58 };
59 
60 static TIFFFieldArray customFieldArray = { tfiatOther, 0, 2, customFields };
61 
62 int
main()63 main()
64 {
65 	TIFF		*tif;
66 	unsigned char	buf[SPP] = { 0, 127, 255 };
67 	uint64          dir_offset = 0, dir_offset2 = 0;
68 	uint64          read_dir_offset = 0, read_dir_offset2 = 0;
69 	uint64          *dir_offset2_ptr = NULL;
70 	char           *ascii_value;
71 	uint16          count16 = 0;
72 
73 
74 	/* We write the main directory as a simple image. */
75 	tif = TIFFOpen(filename, "w+");
76 	if (!tif) {
77 		fprintf (stderr, "Can't create test TIFF file %s.\n", filename);
78 		return 1;
79 	}
80 
81 	if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width)) {
82 		fprintf (stderr, "Can't set ImageWidth tag.\n");
83 		goto failure;
84 	}
85 	if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length)) {
86 		fprintf (stderr, "Can't set ImageLength tag.\n");
87 		goto failure;
88 	}
89 	if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps)) {
90 		fprintf (stderr, "Can't set BitsPerSample tag.\n");
91 		goto failure;
92 	}
93 	if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, SPP)) {
94 		fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
95 		goto failure;
96 	}
97 	if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip)) {
98 		fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
99 		goto failure;
100 	}
101 	if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, planarconfig)) {
102 		fprintf (stderr, "Can't set PlanarConfiguration tag.\n");
103 		goto failure;
104 	}
105 	if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric)) {
106 		fprintf (stderr, "Can't set PhotometricInterpretation tag.\n");
107 		goto failure;
108 	}
109 
110 	/* Write dummy pixel data. */
111 	if (TIFFWriteScanline(tif, buf, 0, 0) == -1) {
112 		fprintf (stderr, "Can't write image data.\n");
113 		goto failure;
114 	}
115 
116         if (!TIFFWriteDirectory( tif )) {
117 		fprintf (stderr, "TIFFWriteDirectory() failed.\n");
118 		goto failure;
119 	}
120 
121 	/*
122 	 * Now create an EXIF directory.
123 	 */
124 	if (TIFFCreateEXIFDirectory(tif) != 0) {
125 		fprintf (stderr, "TIFFCreateEXIFDirectory() failed.\n" );
126 		goto failure;
127 	}
128 
129 	if (!TIFFSetField( tif, EXIFTAG_SPECTRALSENSITIVITY, "EXIF Spectral Sensitivity")) {
130 		fprintf (stderr, "Can't write SPECTRALSENSITIVITY\n" );
131 		goto failure;
132 	}
133 
134         if (!TIFFWriteCustomDirectory( tif, &dir_offset )) {
135 		fprintf (stderr, "TIFFWriteCustomDirectory() with EXIF failed.\n");
136 		goto failure;
137 	}
138 
139 	/*
140 	 * Now create a custom directory with tags that conflict with mainline
141 	 * TIFF tags.
142 	 */
143 
144 	TIFFFreeDirectory( tif );
145 	if (TIFFCreateCustomDirectory(tif, &customFieldArray) != 0) {
146 		fprintf (stderr, "TIFFCreateEXIFDirectory() failed.\n" );
147 		goto failure;
148 	}
149 
150 	if (!TIFFSetField( tif, TIFFTAG_IMAGEWIDTH, "*Custom1")) { /* not really IMAGEWIDTH */
151 		fprintf (stderr, "Can't write pseudo-IMAGEWIDTH.\n" );
152 		goto failure;
153 	}
154 
155 	if (!TIFFSetField( tif, TIFFTAG_DOTRANGE, "*Custom2")) { /* not really DOTWIDTH */
156 		fprintf (stderr, "Can't write pseudo-DOTWIDTH.\n" );
157 		goto failure;
158 	}
159 
160         if (!TIFFWriteCustomDirectory( tif, &dir_offset2 )) {
161 		fprintf (stderr, "TIFFWriteCustomDirectory() with EXIF failed.\n");
162 		goto failure;
163 	}
164 
165 	/*
166 	 * Go back to the first directory, and add the EXIFIFD pointer.
167 	 */
168 	TIFFSetDirectory(tif, 0);
169 	TIFFSetField(tif, TIFFTAG_EXIFIFD, dir_offset );
170 	TIFFSetField(tif, TIFFTAG_SUBIFD, 1, &dir_offset2 );
171 
172 	TIFFClose(tif);
173 
174 	/* Ok, now test whether we can read written values in the EXIF directory. */
175 	tif = TIFFOpen(filename, "r");
176 
177 	TIFFGetField(tif, TIFFTAG_EXIFIFD, &read_dir_offset );
178 	if( read_dir_offset != dir_offset ) {
179 		fprintf (stderr, "Did not get expected EXIFIFD.\n" );
180 		goto failure;
181 	}
182 
183 	TIFFGetField(tif, TIFFTAG_SUBIFD, &count16, &dir_offset2_ptr );
184 	read_dir_offset2 = dir_offset2_ptr[0];
185 	if( read_dir_offset2 != dir_offset2 || count16 != 1) {
186 		fprintf (stderr, "Did not get expected SUBIFD.\n" );
187 		goto failure;
188 	}
189 
190 	if( !TIFFReadEXIFDirectory(tif, read_dir_offset) ) {
191 		fprintf (stderr, "TIFFReadEXIFDirectory() failed.\n" );
192 		goto failure;
193 	}
194 
195 	if (!TIFFGetField( tif, EXIFTAG_SPECTRALSENSITIVITY, &ascii_value) ) {
196 		fprintf (stderr, "reading SPECTRALSENSITIVITY failed.\n" );
197 		goto failure;
198 	}
199 
200 	if( strcmp(ascii_value,"EXIF Spectral Sensitivity") != 0) {
201 		fprintf (stderr, "got wrong SPECTRALSENSITIVITY value.\n" );
202 		goto failure;
203 	}
204 
205 	/* Try reading the Custom directory */
206 
207 	if( !TIFFReadCustomDirectory(tif, read_dir_offset2, &customFieldArray) ) {
208 		fprintf (stderr, "TIFFReadCustomDirectory() failed.\n" );
209 		goto failure;
210 	}
211 
212 	if (!TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &ascii_value) ) {
213 		fprintf (stderr, "reading pseudo-IMAGEWIDTH failed.\n" );
214 		goto failure;
215 	}
216 
217 	if( strcmp(ascii_value,"*Custom1") != 0) {
218 		fprintf (stderr, "got wrong pseudo-IMAGEWIDTH value.\n" );
219 		goto failure;
220 	}
221 
222 	if (!TIFFGetField( tif, TIFFTAG_DOTRANGE, &ascii_value) ) {
223 		fprintf (stderr, "reading pseudo-DOTRANGE failed.\n" );
224 		goto failure;
225 	}
226 
227 	if( strcmp(ascii_value,"*Custom2") != 0) {
228 		fprintf (stderr, "got wrong pseudo-DOTRANGE value.\n" );
229 		goto failure;
230 	}
231 
232 	TIFFClose(tif);
233 
234 	/* All tests passed; delete file and exit with success status. */
235 	unlink(filename);
236 	return 0;
237 
238 failure:
239 	/*
240 	 * Something goes wrong; close file and return unsuccessful status.
241 	 * Do not remove the file for further manual investigation.
242 	 */
243 	TIFFClose(tif);
244 	return 1;
245 }
246 
247 /* vim: set ts=8 sts=8 sw=8 noet: */
248