Debian Codegraph @

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

raw
1 /*
2 * Copyright (c) 2019, Even Rouault <even.rouault at spatialys.com>
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 test 'D' and 'O' open flags
28 */
29
30 #include "tif_config.h"
31
32 #include <assert.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 "tifftest.h"
42
43 int test(int classictif, int height, int tiled)
44 {
45 const char *filename = "defer_strile_loading.tif";
46 TIFF *tif;
47 int i;
48 int ret = 0;
49 FILE *f;
50
51 (void)ret;
52
53 tif = TIFFOpen(filename,
54 classictif ? "wDO"
55 : "w8DO"); /* O should be ignored in write mode */
56 if (!tif)
57 {
58 fprintf(stderr, "cannot create %s\n", filename);
59 return 1;
60 }
61 ret = TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
62 assert(ret);
63 ret = TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, 1);
64 assert(ret);
65 ret = TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
66 assert(ret);
67 ret = TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
68 assert(ret);
69 ret = TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
70 assert(ret);
71 ret = TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
72 assert(ret);
73 if (tiled)
74 {
75 int j;
76 ret = TIFFSetField(tif, TIFFTAG_TILEWIDTH, 16);
77 assert(ret);
78 ret = TIFFSetField(tif, TIFFTAG_TILELENGTH, 16);
79 assert(ret);
80 for (j = 0; j < (height + 15) / 16; j++)
81 {
82 unsigned char tilebuffer[256];
83 memset(tilebuffer, (unsigned char)j, 256);
84 ret = TIFFWriteEncodedTile(tif, j, tilebuffer, 256);
85 assert(ret == 256);
86 }
87 }
88 else
89 {
90 ret = TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
91 assert(ret);
92 for (i = 0; i < height; i++)
93 {
94 unsigned char c = (unsigned char)i;
95 ret = TIFFWriteEncodedStrip(tif, i, &c, 1);
96 assert(ret == 1);
97
98 if (i == 1 && height > 100000)
99 i = height - 2;
100 }
101 }
102 TIFFClose(tif);
103
104 f = fopen(filename, "rb");
105 if (!f)
106 return 1;
107
108 for (i = 0; i < 2; i++)
109 {
110 tif = TIFFOpen(filename, i == 0 ? "rD" : "rO");
111 if (!tif)
112 {
113 fprintf(stderr, "cannot open %s\n", filename);
114 fclose(f);
115 return 1;
116 }
117 if (tiled)
118 {
119 int j;
120 for (j = 0; j < (height + 15) / 16; j++)
121 {
122 int retry;
123 unsigned char expected_c = (unsigned char)j;
124
125 for (retry = 0; retry < 2; retry++)
126 {
127 unsigned char tilebuffer[256];
128 memset(tilebuffer, 0, 256);
129 ret = TIFFReadEncodedTile(tif, j, tilebuffer, 256);
130 assert(ret == 256);
131 if (tilebuffer[0] != expected_c ||
132 tilebuffer[255] != expected_c)
133 {
134 fprintf(stderr, "unexpected value at tile %d: %d %d\n",
135 j, tilebuffer[0], tilebuffer[255]);
136 TIFFClose(tif);
137 fclose(f);
138 return 1;
139 }
140 }
141
142 {
143 int err = 0;
144 int offset, size;
145 unsigned char inputbuffer[256];
146 unsigned char tilebuffer[256];
147
148 offset = TIFFGetStrileOffsetWithErr(tif, j, &err);
149 assert(offset != 0);
150 assert(err == 0);
151
152 size = TIFFGetStrileByteCountWithErr(tif, j, &err);
153 (void)size;
154 assert(size == 256);
155 assert(err == 0);
156
157 fseek(f, offset, SEEK_SET);
158 {
159 size_t nread = fread(inputbuffer, 256, 1, f);
160 (void)nread;
161 }
162
163 memset(tilebuffer, 0, 256);
164 ret = TIFFReadFromUserBuffer(tif, j, inputbuffer, 256,
165 tilebuffer, 256);
166 assert(ret == 1);
167 if (tilebuffer[0] != expected_c ||
168 tilebuffer[255] != expected_c)
169 {
170 fprintf(stderr, "unexpected value at tile %d: %d %d\n",
171 j, tilebuffer[0], tilebuffer[255]);
172 TIFFClose(tif);
173 fclose(f);
174 return 1;
175 }
176 }
177 }
178 }
179 else
180 {
181 int j;
182 for (j = 0; j < height; j++)
183 {
184 int retry;
185 unsigned char expected_c = (unsigned char)j;
186 for (retry = 0; retry < 2; retry++)
187 {
188 unsigned char c = 0;
189 ret = TIFFReadEncodedStrip(tif, j, &c, 1);
190 assert(ret == 1);
191 if (c != expected_c)
192 {
193 fprintf(stderr, "unexpected value at line %d: %d\n", j,
194 c);
195 TIFFClose(tif);
196 fclose(f);
197 return 1;
198 }
199 }
200
201 {
202 int err = 0;
203 int offset, size;
204 unsigned char inputbuffer[1];
205 unsigned char tilebuffer[1];
206
207 offset = TIFFGetStrileOffsetWithErr(tif, j, &err);
208 assert(offset != 0);
209 assert(err == 0);
210
211 size = TIFFGetStrileByteCountWithErr(tif, j, &err);
212 (void)size;
213 assert(size == 1);
214 assert(err == 0);
215
216 fseek(f, offset, SEEK_SET);
217 {
218 size_t nread = fread(inputbuffer, 1, 1, f);
219 (void)nread;
220 }
221 memset(tilebuffer, 0, 1);
222 ret = TIFFReadFromUserBuffer(tif, j, inputbuffer, 1,
223 tilebuffer, 1);
224 assert(ret == 1);
225 if (tilebuffer[0] != expected_c)
226 {
227 fprintf(stderr, "unexpected value at line %d: %d\n", j,
228 tilebuffer[0]);
229 TIFFClose(tif);
230 fclose(f);
231 return 1;
232 }
233 }
234
235 if (j == 1 && height > 100000)
236 j = height - 2;
237 }
238
239 if (height > 100000)
240 {
241 /* Missing strip */
242 int err = 0;
243 ret = TIFFGetStrileOffsetWithErr(tif, 2, &err);
244 assert(ret == 0);
245 assert(err == 0);
246
247 ret = TIFFGetStrileByteCountWithErr(tif, 2, &err);
248 assert(ret == 0);
249 assert(err == 0);
250 }
251 }
252
253 {
254 int err = 0;
255 ret = TIFFGetStrileOffsetWithErr(tif, 0xFFFFFFFFU, &err);
256 assert(ret == 0);
257 assert(err == 1);
258
259 ret = TIFFGetStrileByteCountWithErr(tif, 0xFFFFFFFFU, &err);
260 assert(ret == 0);
261 assert(err == 1);
262 }
263
264 {
265 toff_t *offsets = NULL;
266 toff_t *bytecounts = NULL;
267 ret = TIFFGetField(
268 tif, tiled ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS,
269 &offsets);
270 assert(ret);
271 assert(offsets);
272 ret = TIFFGetField(
273 tif, tiled ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS,
274 &bytecounts);
275 assert(ret);
276 assert(bytecounts);
277 if (tiled)
278 {
279 assert(bytecounts[0] == 256);
280 }
281 else
282 {
283 assert(bytecounts[0] == 1);
284 if (height > 1 && height <= 100000)
285 {
286 assert(offsets[1] == offsets[0] + 1);
287 assert(offsets[height - 1] == offsets[0] + height - 1);
288 }
289 assert(bytecounts[height - 1] == 1);
290 }
291 }
292
293 TIFFClose(tif);
294 }
295 fclose(f);
296
297 unlink(filename);
298 return 0;
299 }
300
301 int main()
302 {
303 int is_classic;
304 for (is_classic = 1; is_classic >= 0; is_classic--)
305 {
306 int tiled;
307 for (tiled = 0; tiled <= 1; tiled++)
308 {
309 if (test(is_classic, 1, tiled))
310 return 1;
311 if (test(is_classic, 8192, tiled))
312 return 1;
313 }
314 if (test(is_classic, 2000000, 0))
315 return 1;
316 }
317 return 0;
318 }