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