]> git.saurik.com Git - wxWidgets.git/blob - src/tiff/test/rewrite_tag.c
187ae1117bd63c2db20a2058d6dae9df2e75b06e
[wxWidgets.git] / src / tiff / test / rewrite_tag.c
1 /* $Id$ */
2
3 /*
4 * Copyright (c) 2007, 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 test _TIFFRewriteField().
30 */
31
32 #include "tif_config.h"
33
34 #include <stdio.h>
35
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39
40 #include "tiffio.h"
41 #include "tiffiop.h"
42
43 const uint32 width = 10;
44 const uint32 length = 40;
45 const uint32 rows_per_strip = 1;
46
47 int test_packbits()
48
49 {
50 TIFF *tif;
51 int i;
52 unsigned char buf[10] = {0,0,0,0,0,0,0,0,0,0};
53
54 int length = 20;
55 const char *filename = "test_packbits.tif";
56
57 /* Test whether we can write tags. */
58 tif = TIFFOpen(filename, "w");
59
60 if (!tif) {
61 fprintf (stderr, "Can't create test TIFF file %s.\n", filename);
62 return 1;
63 }
64
65 if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS)) {
66 fprintf (stderr, "Can't set Compression tag.\n");
67 goto failure;
68 }
69 if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width)) {
70 fprintf (stderr, "Can't set ImageWidth tag.\n");
71 goto failure;
72 }
73 if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length)) {
74 fprintf (stderr, "Can't set ImageLength tag.\n");
75 goto failure;
76 }
77 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8)) {
78 fprintf (stderr, "Can't set BitsPerSample tag.\n");
79 goto failure;
80 }
81 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1)) {
82 fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
83 goto failure;
84 }
85 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip)) {
86 fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
87 goto failure;
88 }
89 if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)) {
90 fprintf (stderr, "Can't set PlanarConfiguration tag.\n");
91 goto failure;
92 }
93
94 for (i = 0; i < length; i++ )
95 {
96 if( !TIFFWriteEncodedStrip( tif, i, buf, 10 ) )
97 {
98 fprintf (stderr, "Can't write image data.\n");
99 goto failure;
100 }
101 }
102
103 TIFFClose(tif);
104
105 /* Test whether we can write tags. */
106
107 tif = TIFFOpen(filename, "r+");
108
109 if (!tif) {
110 fprintf (stderr, "Can't create test TIFF file %s.\n", filename);
111 return 1;
112 }
113
114 buf[3] = 17;
115 buf[6] = 12;
116
117 if( !TIFFWriteEncodedStrip( tif, 6, buf, 10 ) )
118 {
119 fprintf (stderr, "Can't write image data.\n");
120 goto failure;
121 }
122
123 TIFFClose(tif);
124
125 unlink(filename);
126
127 return 0;
128
129 failure:
130 /* Something goes wrong; close file and return unsuccessful status. */
131 TIFFClose(tif);
132 /* unlink(filename); */
133
134 return 1;
135
136 }
137
138 /************************************************************************/
139 /* rewrite_test() */
140 /************************************************************************/
141 int rewrite_test( const char *filename, int length, int bigtiff,
142 uint64 base_value )
143
144 {
145 TIFF *tif;
146 int i;
147 unsigned char buf[10] = {5,6,7,8,9,10,11,12,13,14};
148 uint64 *rowoffset, *rowbytes;
149 uint64 *upd_rowoffset;
150 uint64 *upd_bytecount;
151
152 /* Test whether we can write tags. */
153 if( bigtiff )
154 tif = TIFFOpen(filename, "w8");
155 else
156 tif = TIFFOpen(filename, "w4");
157
158 if (!tif) {
159 fprintf (stderr, "Can't create test TIFF file %s.\n", filename);
160 return 1;
161 }
162
163 if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS)) {
164 fprintf (stderr, "Can't set Compression tag.\n");
165 goto failure;
166 }
167 if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width)) {
168 fprintf (stderr, "Can't set ImageWidth tag.\n");
169 goto failure;
170 }
171 if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length)) {
172 fprintf (stderr, "Can't set ImageLength tag.\n");
173 goto failure;
174 }
175 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8)) {
176 fprintf (stderr, "Can't set BitsPerSample tag.\n");
177 goto failure;
178 }
179 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1)) {
180 fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
181 goto failure;
182 }
183 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip)) {
184 fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
185 goto failure;
186 }
187 if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)) {
188 fprintf (stderr, "Can't set PlanarConfiguration tag.\n");
189 goto failure;
190 }
191
192 for (i = 0; i < length; i++ )
193 {
194 if( !TIFFWriteScanline( tif, buf, i, 0 ) )
195 {
196 fprintf (stderr, "Can't write image data.\n");
197 goto failure;
198 }
199 }
200
201 TIFFClose(tif);
202
203 /* Ok, now test whether we can read written values. */
204 tif = TIFFOpen(filename, "r+");
205 if (!tif) {
206 fprintf (stderr, "Can't open test TIFF file %s.\n", filename);
207 return 1;
208 }
209
210 if( !TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &rowoffset ) )
211 {
212 fprintf (stderr, "Can't fetch STRIPOFFSETS.\n");
213 goto failure;
214 }
215
216 if( !TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &rowbytes ) )
217 {
218 fprintf (stderr, "Can't fetch STRIPBYTECOUNTS.\n");
219 goto failure;
220 }
221
222 upd_rowoffset = (uint64 *) _TIFFmalloc(sizeof(uint64) * length);
223 for( i = 0; i < length; i++ )
224 upd_rowoffset[i] = base_value + i*10;
225
226 if( !_TIFFRewriteField( tif, TIFFTAG_STRIPOFFSETS, TIFF_LONG8,
227 length, upd_rowoffset ) )
228 {
229 fprintf (stderr, "Can't rewrite STRIPOFFSETS.\n");
230 goto failure;
231 }
232
233 _TIFFfree( upd_rowoffset );
234
235 upd_bytecount = (uint64 *) _TIFFmalloc(sizeof(uint64) * length);
236 for( i = 0; i < length; i++ )
237 upd_bytecount[i] = 100 + i*10;
238
239 if( !_TIFFRewriteField( tif, TIFFTAG_STRIPBYTECOUNTS, TIFF_LONG8,
240 length, upd_bytecount ) )
241 {
242 fprintf (stderr, "Can't rewrite STRIPBYTECOUNTS.\n");
243 goto failure;
244 }
245
246 _TIFFfree( upd_bytecount );
247
248 TIFFClose(tif);
249
250 /* Reopen file and read back to verify contents */
251
252 tif = TIFFOpen(filename, "r");
253 if (!tif) {
254 fprintf (stderr, "Can't open test TIFF file %s.\n", filename);
255 return 1;
256 }
257
258 if( !TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &rowoffset ) )
259 {
260 fprintf (stderr, "Can't fetch STRIPOFFSETS.\n");
261 goto failure;
262 }
263
264 for( i = 0; i < length; i++ )
265 {
266 uint64 expect = base_value + i*10;
267
268 if( rowoffset[i] != expect )
269 {
270 fprintf( stderr,
271 "%s:STRIPOFFSETS[%d]: Got %X:%08X instead of %X:%08X.\n",
272 filename, i,
273 (int) (rowoffset[i] >> 32),
274 (int) (rowoffset[i]&0xFFFFFFFF),
275 (int) (expect >> 32),
276 (int) (expect & 0xFFFFFFFF) );
277 goto failure;
278 }
279 }
280
281 if( !TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &rowbytes ) )
282 {
283 fprintf (stderr, "Can't fetch STRIPBYTECOUNTS.\n");
284 goto failure;
285 }
286
287 for( i = 0; i < length; i++ )
288 {
289 uint64 expect = 100 + i*10;
290
291 if( rowbytes[i] != expect )
292 {
293 fprintf( stderr,
294 "%s:STRIPBYTECOUNTS[%d]: Got %X:%08X instead of %X:%08X.\n",
295 filename, i,
296 (int) (rowbytes[i] >> 32),
297 (int) (rowbytes[i] & 0xFFFFFFFF),
298 (int) (expect >> 32),
299 (int) (expect & 0xFFFFFFFF) );
300 goto failure;
301 }
302 }
303
304 TIFFClose( tif );
305
306 /* All tests passed; delete file and exit with success status. */
307 unlink(filename);
308 return 0;
309
310 failure:
311 /* Something goes wrong; close file and return unsuccessful status. */
312 TIFFClose(tif);
313 /* unlink(filename); */
314
315 return 1;
316
317 }
318
319 /************************************************************************/
320 /* main() */
321 /************************************************************************/
322 int
323 main(int argc, char **argv)
324 {
325 (void) argc;
326 (void) argv;
327 int failure = 0;
328
329 failure |= test_packbits();
330
331 /* test fairly normal use */
332 failure |= rewrite_test( "rewrite1.tif", 10, 0, 100 );
333 failure |= rewrite_test( "rewrite2.tif", 10, 1, 100 );
334
335 /* test case of fitting all in directory entry */
336 failure |= rewrite_test( "rewrite3.tif", 1, 0, 100 );
337 failure |= rewrite_test( "rewrite4.tif", 1, 1, 100 );
338
339 /* test with very large values that don't fit in 4bytes (bigtiff only) */
340 failure |= rewrite_test( "rewrite5.tif", 1000, 1, 0x6000000000ULL );
341 failure |= rewrite_test( "rewrite6.tif", 1, 1, 0x6000000000ULL );
342
343 return failure;
344 }
345
346 /* vim: set ts=8 sts=8 sw=8 noet: */