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