Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/test/custom_dir.c

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