]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/test/rewrite_tag.c
3 * Copyright (c) 2007, Frank Warmerdam <warmerdam@pobox.com>
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Module to test _TIFFRewriteField().
31 #include "tif_config.h"
42 const uint32 width
= 10;
43 const uint32 length
= 40;
44 const uint32 rows_per_strip
= 1;
51 unsigned char buf
[10] = {0,0,0,0,0,0,0,0,0,0};
54 const char *filename
= "test_packbits.tif";
56 /* Test whether we can write tags. */
57 tif
= TIFFOpen(filename
, "w");
60 fprintf (stderr
, "Can't create test TIFF file %s.\n", filename
);
64 if (!TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_PACKBITS
)) {
65 fprintf (stderr
, "Can't set Compression tag.\n");
68 if (!TIFFSetField(tif
, TIFFTAG_IMAGEWIDTH
, width
)) {
69 fprintf (stderr
, "Can't set ImageWidth tag.\n");
72 if (!TIFFSetField(tif
, TIFFTAG_IMAGELENGTH
, length
)) {
73 fprintf (stderr
, "Can't set ImageLength tag.\n");
76 if (!TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, 8)) {
77 fprintf (stderr
, "Can't set BitsPerSample tag.\n");
80 if (!TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, 1)) {
81 fprintf (stderr
, "Can't set SamplesPerPixel tag.\n");
84 if (!TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
, rows_per_strip
)) {
85 fprintf (stderr
, "Can't set SamplesPerPixel tag.\n");
88 if (!TIFFSetField(tif
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
)) {
89 fprintf (stderr
, "Can't set PlanarConfiguration tag.\n");
93 for (i
= 0; i
< length
; i
++ )
95 if( !TIFFWriteEncodedStrip( tif
, i
, buf
, 10 ) )
97 fprintf (stderr
, "Can't write image data.\n");
104 /* Test whether we can write tags. */
106 tif
= TIFFOpen(filename
, "r+");
109 fprintf (stderr
, "Can't create test TIFF file %s.\n", filename
);
116 if( !TIFFWriteEncodedStrip( tif
, 6, buf
, 10 ) )
118 fprintf (stderr
, "Can't write image data.\n");
129 /* Something goes wrong; close file and return unsuccessful status. */
131 /* unlink(filename); */
137 /************************************************************************/
139 /************************************************************************/
140 int rewrite_test( const char *filename
, int length
, int bigtiff
,
146 unsigned char buf
[10] = {5,6,7,8,9,10,11,12,13,14};
147 uint64
*rowoffset
, *rowbytes
;
148 uint64
*upd_rowoffset
;
149 uint64
*upd_bytecount
;
151 /* Test whether we can write tags. */
153 tif
= TIFFOpen(filename
, "w8");
155 tif
= TIFFOpen(filename
, "w4");
158 fprintf (stderr
, "Can't create test TIFF file %s.\n", filename
);
162 if (!TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_PACKBITS
)) {
163 fprintf (stderr
, "Can't set Compression tag.\n");
166 if (!TIFFSetField(tif
, TIFFTAG_IMAGEWIDTH
, width
)) {
167 fprintf (stderr
, "Can't set ImageWidth tag.\n");
170 if (!TIFFSetField(tif
, TIFFTAG_IMAGELENGTH
, length
)) {
171 fprintf (stderr
, "Can't set ImageLength tag.\n");
174 if (!TIFFSetField(tif
, TIFFTAG_BITSPERSAMPLE
, 8)) {
175 fprintf (stderr
, "Can't set BitsPerSample tag.\n");
178 if (!TIFFSetField(tif
, TIFFTAG_SAMPLESPERPIXEL
, 1)) {
179 fprintf (stderr
, "Can't set SamplesPerPixel tag.\n");
182 if (!TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
, rows_per_strip
)) {
183 fprintf (stderr
, "Can't set SamplesPerPixel tag.\n");
186 if (!TIFFSetField(tif
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
)) {
187 fprintf (stderr
, "Can't set PlanarConfiguration tag.\n");
191 for (i
= 0; i
< length
; i
++ )
193 if( !TIFFWriteScanline( tif
, buf
, i
, 0 ) )
195 fprintf (stderr
, "Can't write image data.\n");
202 /* Ok, now test whether we can read written values. */
203 tif
= TIFFOpen(filename
, "r+");
205 fprintf (stderr
, "Can't open test TIFF file %s.\n", filename
);
209 if( !TIFFGetField( tif
, TIFFTAG_STRIPOFFSETS
, &rowoffset
) )
211 fprintf (stderr
, "Can't fetch STRIPOFFSETS.\n");
215 if( !TIFFGetField( tif
, TIFFTAG_STRIPBYTECOUNTS
, &rowbytes
) )
217 fprintf (stderr
, "Can't fetch STRIPBYTECOUNTS.\n");
221 upd_rowoffset
= (uint64
*) _TIFFmalloc(sizeof(uint64
) * length
);
222 for( i
= 0; i
< length
; i
++ )
223 upd_rowoffset
[i
] = base_value
+ i
*10;
225 if( !_TIFFRewriteField( tif
, TIFFTAG_STRIPOFFSETS
, TIFF_LONG8
,
226 length
, upd_rowoffset
) )
228 fprintf (stderr
, "Can't rewrite STRIPOFFSETS.\n");
232 _TIFFfree( upd_rowoffset
);
234 upd_bytecount
= (uint64
*) _TIFFmalloc(sizeof(uint64
) * length
);
235 for( i
= 0; i
< length
; i
++ )
236 upd_bytecount
[i
] = 100 + i
*10;
238 if( !_TIFFRewriteField( tif
, TIFFTAG_STRIPBYTECOUNTS
, TIFF_LONG8
,
239 length
, upd_bytecount
) )
241 fprintf (stderr
, "Can't rewrite STRIPBYTECOUNTS.\n");
245 _TIFFfree( upd_bytecount
);
249 /* Reopen file and read back to verify contents */
251 tif
= TIFFOpen(filename
, "r");
253 fprintf (stderr
, "Can't open test TIFF file %s.\n", filename
);
257 if( !TIFFGetField( tif
, TIFFTAG_STRIPOFFSETS
, &rowoffset
) )
259 fprintf (stderr
, "Can't fetch STRIPOFFSETS.\n");
263 for( i
= 0; i
< length
; i
++ )
265 uint64 expect
= base_value
+ i
*10;
267 if( rowoffset
[i
] != expect
)
270 "%s:STRIPOFFSETS[%d]: Got %X:%08X instead of %X:%08X.\n",
272 (int) (rowoffset
[i
] >> 32),
273 (int) (rowoffset
[i
]&0xFFFFFFFF),
274 (int) (expect
>> 32),
275 (int) (expect
& 0xFFFFFFFF) );
280 if( !TIFFGetField( tif
, TIFFTAG_STRIPBYTECOUNTS
, &rowbytes
) )
282 fprintf (stderr
, "Can't fetch STRIPBYTECOUNTS.\n");
286 for( i
= 0; i
< length
; i
++ )
288 uint64 expect
= 100 + i
*10;
290 if( rowbytes
[i
] != expect
)
293 "%s:STRIPBYTECOUNTS[%d]: Got %X:%08X instead of %X:%08X.\n",
295 (int) (rowbytes
[i
] >> 32),
296 (int) (rowbytes
[i
] & 0xFFFFFFFF),
297 (int) (expect
>> 32),
298 (int) (expect
& 0xFFFFFFFF) );
305 /* All tests passed; delete file and exit with success status. */
310 /* Something goes wrong; close file and return unsuccessful status. */
312 /* unlink(filename); */
318 /************************************************************************/
320 /************************************************************************/
322 main(int argc
, char **argv
)
328 failure
|= test_packbits();
330 /* test fairly normal use */
331 failure
|= rewrite_test( "rewrite1.tif", 10, 0, 100 );
332 failure
|= rewrite_test( "rewrite2.tif", 10, 1, 100 );
334 /* test case of fitting all in directory entry */
335 failure
|= rewrite_test( "rewrite3.tif", 1, 0, 100 );
336 failure
|= rewrite_test( "rewrite4.tif", 1, 1, 100 );
338 /* test with very large values that don't fit in 4bytes (bigtiff only) */
339 failure
|= rewrite_test( "rewrite5.tif", 1000, 1, 0x6000000000ULL
);
340 failure
|= rewrite_test( "rewrite6.tif", 1, 1, 0x6000000000ULL
);
345 /* vim: set ts=8 sts=8 sw=8 noet: */