Debian Codegraph @

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

raw
1 /*
2 * Copyright (c) 2004, Andrey Kiselev <[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 * Functions to test strip interface of libtiff.
28 */
29
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "tiffio.h"
34
35 int write_strips(TIFF *tif, const tdata_t array, const tsize_t size)
36 {
37 tstrip_t strip, nstrips;
38 tsize_t stripsize, offset;
39
40 stripsize = TIFFStripSize(tif);
41 if (!stripsize)
42 {
43 fprintf(stderr, "Wrong size of strip.\n");
44 return -1;
45 }
46
47 nstrips = TIFFNumberOfStrips(tif);
48 for (offset = 0, strip = 0; offset < size && strip < nstrips;
49 offset += stripsize, strip++)
50 {
51 /*
52 * Properly write last strip.
53 */
54 tsize_t bufsize = size - offset;
55 if (bufsize > stripsize)
56 bufsize = stripsize;
57
58 if (TIFFWriteEncodedStrip(tif, strip, (char *)array + offset,
59 bufsize) != bufsize)
60 {
61 fprintf(stderr, "Can't write strip %" PRIu32 ".\n", strip);
62 return -1;
63 }
64 }
65
66 return 0;
67 }
68
69 int read_strips(TIFF *tif, const tdata_t array, const tsize_t size)
70 {
71 tstrip_t strip, nstrips;
72 tsize_t stripsize, offset;
73 tdata_t buf = NULL;
74
75 stripsize = TIFFStripSize(tif);
76 if (!stripsize)
77 {
78 fprintf(stderr, "Wrong size of strip.\n");
79 return -1;
80 }
81
82 buf = _TIFFmalloc(stripsize);
83 if (!buf)
84 {
85 fprintf(stderr, "Can't allocate space for strip buffer.\n");
86 return -1;
87 }
88
89 nstrips = TIFFNumberOfStrips(tif);
90 for (offset = 0, strip = 0; offset < size && strip < nstrips;
91 offset += stripsize, strip++)
92 {
93 /*
94 * Properly read last strip.
95 */
96 tsize_t bufsize = size - offset;
97 if (bufsize > stripsize)
98 bufsize = stripsize;
99
100 if (TIFFReadEncodedStrip(tif, strip, buf, -1) != bufsize)
101 {
102 fprintf(stderr, "Can't read strip %" PRIu32 ".\n", strip);
103 return -1;
104 }
105 if (memcmp(buf, (char *)array + offset, bufsize) != 0)
106 {
107 fprintf(stderr, "Wrong data read for strip %" PRIu32 ".\n", strip);
108 _TIFFfree(buf);
109 return -1;
110 }
111 }
112
113 _TIFFfree(buf);
114
115 return 0;
116 }
117
118 int create_image_striped(const char *name, uint32_t width, uint32_t length,
119 uint32_t rowsperstrip, uint16_t compression,
120 uint16_t spp, uint16_t bps, uint16_t photometric,
121 uint16_t sampleformat, uint16_t planarconfig,
122 const tdata_t array, const tsize_t size)
123 {
124 TIFF *tif;
125
126 /* Test whether we can write tags. */
127 tif = TIFFOpen(name, "w");
128 if (!tif)
129 goto openfailure;
130
131 if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width))
132 {
133 fprintf(stderr, "Can't set ImageWidth tag.\n");
134 goto failure;
135 }
136 if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length))
137 {
138 fprintf(stderr, "Can't set ImageLength tag.\n");
139 goto failure;
140 }
141 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps))
142 {
143 fprintf(stderr, "Can't set BitsPerSample tag.\n");
144 goto failure;
145 }
146 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp))
147 {
148 fprintf(stderr, "Can't set SamplesPerPixel tag.\n");
149 goto failure;
150 }
151 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip))
152 {
153 fprintf(stderr, "Can't set RowsPerStrip tag.\n");
154 goto failure;
155 }
156 if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, planarconfig))
157 {
158 fprintf(stderr, "Can't set PlanarConfiguration tag.\n");
159 goto failure;
160 }
161 if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric))
162 {
163 fprintf(stderr, "Can't set PhotometricInterpretation tag.\n");
164 goto failure;
165 }
166
167 if (write_strips(tif, array, size) < 0)
168 {
169 fprintf(stderr, "Can't write image data.\n");
170 goto failure;
171 }
172
173 TIFFClose(tif);
174 return 0;
175
176 failure:
177 TIFFClose(tif);
178 openfailure:
179 fprintf(stderr,
180 "Can't create test TIFF file %s:\n"
181 " ImageWidth=%" PRIu32 ", ImageLength=%" PRIu32
182 ", RowsPerStrip=%" PRIu32 ", Compression=%" PRIu16 ",\n"
183 " BitsPerSample=%" PRIu16 ", SamplesPerPixel=%" PRIu16
184 ", SampleFormat=%" PRIu16 ",\n"
185 " PlanarConfiguration=%" PRIu16
186 ", PhotometricInterpretation=%" PRIu16 ".\n",
187 name, width, length, rowsperstrip, compression, bps, spp,
188 sampleformat, planarconfig, photometric);
189 return -1;
190 }
191
192 int read_image_striped(const char *name, uint32_t width, uint32_t length,
193 uint32_t rowsperstrip, uint16_t compression,
194 uint16_t spp, uint16_t bps, uint16_t photometric,
195 uint16_t sampleformat, uint16_t planarconfig,
196 const tdata_t array, const tsize_t size)
197 {
198 TIFF *tif;
199 uint16_t value_u16;
200 uint32_t value_u32;
201
202 /* Test whether we can read written values. */
203 tif = TIFFOpen(name, "r");
204 if (!tif)
205 goto openfailure;
206
207 if (TIFFIsTiled(tif))
208 {
209 fprintf(stderr, "Can't read image %s, it is tiled.\n", name);
210 goto failure;
211 }
212 if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &value_u32) ||
213 value_u32 != width)
214 {
215 fprintf(stderr, "Can't get tag %d.\n", TIFFTAG_IMAGEWIDTH);
216 goto failure;
217 }
218 if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &value_u32) ||
219 value_u32 != length)
220 {
221 fprintf(stderr, "Can't get tag %d.\n", TIFFTAG_IMAGELENGTH);
222 goto failure;
223 }
224 if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &value_u16) ||
225 value_u16 != bps)
226 {
227 fprintf(stderr, "Can't get tag %d.\n", TIFFTAG_BITSPERSAMPLE);
228 goto failure;
229 }
230 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &value_u16) ||
231 value_u16 != photometric)
232 {
233 fprintf(stderr, "Can't get tag %d.\n", TIFFTAG_PHOTOMETRIC);
234 goto failure;
235 }
236 if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &value_u16) ||
237 value_u16 != spp)
238 {
239 fprintf(stderr, "Can't get tag %d.\n", TIFFTAG_SAMPLESPERPIXEL);
240 goto failure;
241 }
242 if (!TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &value_u32) ||
243 value_u32 != rowsperstrip)
244 {
245 fprintf(stderr, "Can't get tag %d.\n", TIFFTAG_ROWSPERSTRIP);
246 goto failure;
247 }
248 if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &value_u16) ||
249 value_u16 != planarconfig)
250 {
251 fprintf(stderr, "Can't get tag %d.\n", TIFFTAG_PLANARCONFIG);
252 goto failure;
253 }
254
255 if (read_strips(tif, array, size) < 0)
256 {
257 fprintf(stderr, "Can't read image data.\n");
258 goto failure;
259 }
260
261 TIFFClose(tif);
262 return 0;
263
264 failure:
265 TIFFClose(tif);
266 openfailure:
267 fprintf(stderr,
268 "Can't read test TIFF file %s:\n"
269 " ImageWidth=%" PRIu32 ", ImageLength=%" PRIu32
270 ", RowsPerStrip=%" PRIu32 ", Compression=%" PRIu16 ",\n"
271 " BitsPerSample=%" PRIu16 ", SamplesPerPixel=%" PRIu16
272 ", SampleFormat=%" PRIu16 ",\n"
273 " PlanarConfiguration=%" PRIu16
274 ", PhotometricInterpretation=%" PRIu16 ".\n",
275 name, width, length, rowsperstrip, compression, bps, spp,
276 sampleformat, planarconfig, photometric);
277 return -1;
278 }
279
280 int write_scanlines(TIFF *tif, const tdata_t array, const tsize_t size)
281 {
282 uint32_t length, row;
283 tsize_t scanlinesize, offset;
284 (void)size;
285
286 if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &length))
287 {
288 fprintf(stderr, "Can't get tag %d.\n", TIFFTAG_IMAGELENGTH);
289 return -1;
290 }
291
292 scanlinesize = TIFFScanlineSize(tif);
293 if (!scanlinesize)
294 {
295 fprintf(stderr, "Wrong size of scanline.\n");
296 return -1;
297 }
298
299 for (offset = 0, row = 0; row < length; offset += scanlinesize, row++)
300 {
301 if (TIFFWriteScanline(tif, (char *)array + offset, row, 0) == -1)
302 {
303 fprintf(stderr, "Can't write image data at row %" PRIu32 ".\n",
304 row);
305 return -1;
306 }
307 }
308
309 return 0;
310 }