Debian Codegraph @

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

raw
1 /*
2 * TIFF Library
3 *
4 * The purpose of this test suite is to test the correctness of
5 * writing and reading transfer functions with TIFFTAG_TRANSFERFUNCTION.
6 *
7 * Depending on the SamplesPerPixel (and ExtraSamples) values,
8 * exactly one (1) or exactly three (3) transfer functions shall be written
9 * to file and are expected for reading from file.
10 * An exception is if all three transfer functions are equal.
11 * Then only one transfer function is written to file and
12 * at reading that transfer function is returned in three
13 * pointers to TIFFGetField().
14 * If only one array is returned, as for images with SamplesPerPixel=1,
15 * the other two are set to NULL.
16 *
17 * Each transfer function length is (1 << BitsPerSample) times uint16_t values.
18 *
19 */
20 /* clang-format off */
21
22 #include "tif_config.h"
23
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "tiffio.h"
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h> /* for unlink() on linux */
32 #endif
33
34 /* Define basic TIFF file settings */
35 uint16_t spp = 3;
36 const uint16_t width = 4;
37 const uint16_t length = 6;
38 const uint16_t bps = 8;
39 const uint16_t photometric = PHOTOMETRIC_RGB;
40 const uint16_t rows_per_strip = 100; /* up to 2**32-1 */
41 const uint16_t planarconfig = PLANARCONFIG_CONTIG;
42
43 /* Data and pointer to three transfer functions. */
44 uint16_t *pTransferFunctionData;
45 uint32_t nSamplesPerTransferFunction;
46 /* Pointers into pTransferFunctionData for writing */
47 const uint16_t *ptf0;
48 const uint16_t *ptf1;
49 const uint16_t *ptf2;
50 /* Pointers for reading */
51 const uint16_t *ptfR0;
52 const uint16_t *ptfR1;
53 const uint16_t *ptfR2;
54
55 #define GOTOFAILURE(x) \
56 { \
57 retval = x; \
58 goto failure; \
59 }
60
61 /*-- setup_transferfunctions() --
62 * Initializes the transfer function array pTransferFunctionData
63 * with three transfer functions.
64 * Pointer ptf0, ptf1, ptf2 are initialized to the first array element of
65 * each transfer function.
66 */
67 int setup_transfer_functions(void)
68 {
69 if (pTransferFunctionData)
70 _TIFFfree(pTransferFunctionData);
71
72 /* Setup array with some more values to shift start of the three arrays. */
73 nSamplesPerTransferFunction = ((uint32_t)1 << bps);
74 pTransferFunctionData = _TIFFmalloc(3 * (tmsize_t)nSamplesPerTransferFunction * sizeof(uint16_t));
75 if (!pTransferFunctionData)
76 return 1;
77
78 for (uint32_t i = 0; i < 3 * nSamplesPerTransferFunction; i++)
79 pTransferFunctionData[i] = (uint16_t)(i + 1);
80
81 ptf0 = &pTransferFunctionData[0];
82 ptf1 = &pTransferFunctionData[nSamplesPerTransferFunction];
83 ptf2 = &pTransferFunctionData[2 * nSamplesPerTransferFunction];
84
85 return 0;
86 } /*-- setup_transferfunctions() --*/
87
88 /*--read_check_transferfunctions --
89 * The file is opened and the transfer functions are read.
90 * Then the read transfer functions are compared against the arrays provided
91 * with ptfx0, ptfx1, ptfx2.
92 * Depending on blnClose, the opened file is closed.
93 * TIFF **ptif returns the TIFF pointer.
94 * The function returns a bit-field with one bit set for each successfully read transfer function.
95 */
96 int read_check_transferfunctions(TIFF **ptif, const char *filename, int blnClose, const uint16_t *ptfx0,
97 const uint16_t *ptfx1, const uint16_t *ptfx2)
98 {
99 /* Test reading of transfer functions */
100 int retval = 0;
101 TIFF *tif = TIFFOpen(filename, "r");
102 *ptif = tif;
103 if (!tif)
104 {
105 fprintf(stderr, "read_check_transferfunctions(): Can't open %s for reading.\n", filename);
106 GOTOFAILURE(-1);
107 }
108
109 if (!TIFFGetField(tif, TIFFTAG_TRANSFERFUNCTION, &ptfR0, &ptfR1, &ptfR2))
110 {
111 fprintf(stderr, "read_check_transferfunctions(): TIFFGetField for TIFFTAG_TRANSFERFUNCTION failed.\n");
112 GOTOFAILURE(-1);
113 }
114 else
115 {
116 if (ptfR0 != NULL && !_TIFFmemcmp(ptfx0, ptfR0, nSamplesPerTransferFunction * sizeof(uint16_t)))
117 retval += 1;
118 if (ptfR1 != NULL && !_TIFFmemcmp(ptfx1, ptfR1, nSamplesPerTransferFunction * sizeof(uint16_t)))
119 retval += 2;
120 if (ptfR2 != NULL && !_TIFFmemcmp(ptfx2, ptfR2, nSamplesPerTransferFunction * sizeof(uint16_t)))
121 retval += 4;
122 }
123
124 failure:
125 if (blnClose)
126 {
127 TIFFClose(tif);
128 tif = NULL;
129 }
130 *ptif = tif;
131
132 return retval;
133 } /*-- read_check_transferfunctions() --*/
134
135 /*-- write_basic_IFD_data() --
136 * Opens a file for writing and initializes a basic TIFF image with the
137 * globally specified parameters.
138 *
139 * wrtTransferFunction: 0 = don't write TIFFTAG_TRANSFERFUNCTION tag
140 * 1 = write TIFFTAG_TRANSFERFUNCTION tag before and
141 * 2 = after ExtraSamples tag.
142 * blnExtraSamples=true writes ExtraSamples tag.
143 * Only if blnCloseFile is true, the file is closed.
144 * TIFF **ptif returns the pointer to the opened TIFF file.
145 */
146 int write_basic_IFD_data(TIFF **ptif, const char *filename, int wrtTransferFunction, int nExtraSamples,
147 int blnCloseFile)
148 {
149 unsigned char buf[3] = {0, 127, 255};
150 int retval = 0;
151 uint8_t *bufLine = NULL;
152 TIFF *tif = TIFFOpen(filename, "w");
153 *ptif = tif;
154 if (!tif)
155 {
156 fprintf(stderr, "write_basic_IFD_data(): Can't create %s\n", filename);
157 GOTOFAILURE(1);
158 }
159
160 if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width))
161 {
162 fprintf(stderr, "write_basic_IFD_data(): Can't set ImageWidth tag.\n");
163 GOTOFAILURE(1);
164 }
165 if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length))
166 {
167 fprintf(stderr, "write_basic_IFD_data(): Can't set ImageLength tag.\n");
168 GOTOFAILURE(1);
169 }
170 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps))
171 {
172 fprintf(stderr, "write_basic_IFD_data(): Can't set BitsPerSample tag.\n");
173 GOTOFAILURE(1);
174 }
175 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp))
176 {
177 fprintf(stderr, "write_basic_IFD_data(): Can't set SamplesPerPixel tag.\n");
178 GOTOFAILURE(1);
179 }
180 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip))
181 {
182 fprintf(stderr, "write_basic_IFD_data(): Can't set SamplesPerPixel tag.\n");
183 GOTOFAILURE(1);
184 }
185 if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, planarconfig))
186 {
187 fprintf(stderr, "write_basic_IFD_data(): Can't set PlanarConfiguration tag.\n");
188 GOTOFAILURE(1);
189 }
190 if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric))
191 {
192 fprintf(stderr, "write_basic_IFD_data(): Can't set PhotometricInterpretation tag.\n");
193 GOTOFAILURE(1);
194 }
195
196 /* Set transfer function before ExtraSamples*/
197 if (wrtTransferFunction == 1)
198 {
199
200 if (!TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, ptf0, ptf1, ptf2))
201 {
202 fprintf(stderr, "write_basic_IFD_data(): TIFFSetField for TIFFTAG_TRANSFERFUNCTION failed.\n");
203 GOTOFAILURE(1);
204 }
205 }
206
207 /* Set ExtraSamples thus SamplesPerPixel for transfer functions is reduced by one. */
208 uint16_t extraSamples[4] = {EXTRASAMPLE_UNSPECIFIED, EXTRASAMPLE_UNSPECIFIED, EXTRASAMPLE_UNSPECIFIED,
209 EXTRASAMPLE_UNSPECIFIED};
210 if (nExtraSamples > 0 && nExtraSamples < 4)
211 {
212 if (!TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, nExtraSamples, extraSamples))
213 {
214 fprintf(stderr, "write_basic_IFD_data(): Warning: Could not set TIFFTAG_EXTRASAMPLES tag.\n");
215 }
216 }
217
218 /* Set transfer function after ExtraSamples*/
219 if (wrtTransferFunction == 2)
220 {
221
222 if (!TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, ptf0, ptf1, ptf2))
223 {
224 fprintf(stderr, "write_basic_IFD_data(): TIFFSetField for TIFFTAG_TRANSFERFUNCTION failed.\n");
225 GOTOFAILURE(1);
226 }
227 }
228
229 /* Setup buffer for image line */
230 size_t bufLen = (size_t)width * spp * (bps + 7) / 8;
231 bufLine = _TIFFmalloc(bufLen);
232 if (!bufLine)
233 {
234 fprintf(stderr, "write_basic_IFD_data(): Can't allocate bufLine buffer.\n");
235 GOTOFAILURE(1);
236 }
237 if (bufLen > sizeof(buf))
238 _TIFFmemcpy(bufLine, buf, sizeof(buf));
239
240 /* Write dummy pixel data. */
241 for (int i = 0; i < length; i++)
242 {
243 if (TIFFWriteScanline(tif, bufLine, i, 0) == -1)
244 {
245 fprintf(stderr, "write_basic_IFD_data(): Can't write image data.\n");
246 GOTOFAILURE(1);
247 }
248 size_t idx = (size_t)i * spp < bufLen ? (size_t)i * spp : bufLen - 2;
249 bufLine[idx] = (uint8_t)(i * 80);
250 }
251
252 failure:
253 if (bufLine)
254 _TIFFfree(bufLine);
255 if (blnCloseFile)
256 {
257 TIFFClose(tif);
258 tif = NULL;
259 }
260 *ptif = tif;
261
262 return retval;
263 } /*-- write_basic_IFD_data() --*/
264
265 /*==== main() ====*/
266 int main()
267 {
268
269 const char *filename = "test_transferfunction_write_read.tif";
270
271 if (setup_transfer_functions())
272 return 1;
273
274 int ret;
275 TIFF *tif = NULL;
276
277 fprintf(stderr,
278 "--- Testing TIFF_TRANSFERFUNCTION tag writing and reading. ---\n Expect 6 warning messages ...\n");
279
280 /* Test with RGB data */
281 spp = 3;
282 if (write_basic_IFD_data(&tif, filename, 2, 0, true))
283 return 1;
284 ret = read_check_transferfunctions(&tif, filename, true, ptf0, ptf1, ptf2);
285 if (ret != 7)
286 {
287 fprintf(stderr, "TRANSFERFUNCTION for RGB (SPP=3) could not be read correctly.\n");
288 return 1;
289 }
290
291 /* Test with Grayscale data */
292 spp = 1;
293 if (write_basic_IFD_data(&tif, filename, 2, 0, true))
294 return 1;
295 ret = read_check_transferfunctions(&tif, filename, true, ptf0, ptf1, ptf2);
296 if (ret != 1)
297 {
298 fprintf(stderr, "TRANSFERFUNCTION SPP=1 could not be read correctly.\n");
299 return 1;
300 }
301
302 /* Test with RGB and ExtraSample, thus only 2 SamplesPerPixel left.
303 * ExtraSample is written before transfer function.
304 * --> For v4.5.1: This gives an Error when reading the transfer functions!
305 */
306 spp = 3;
307 if (write_basic_IFD_data(&tif, filename, 2, 1, true))
308 return 1;
309 ret = read_check_transferfunctions(&tif, filename, true, ptf0, ptf1, ptf2);
310 if (ret != 7)
311 {
312 fprintf(stderr, "One ExtraSample plus TRANSFERFUNCTION SPP=3 could not be read correctly.\n");
313 return 1;
314 }
315 /* Now write two ExtraSamples, thus only one transfer function is written to file and read. */
316 if (write_basic_IFD_data(&tif, filename, 2, 2, true))
317 return 1;
318 ret = read_check_transferfunctions(&tif, filename, true, ptf0, ptf1, ptf2);
319 if (ret != 1)
320 {
321 fprintf(stderr, "Two ExtraSamples plus TRANSFERFUNCTION SPP=3 could not be read correctly.\n");
322 return 1;
323 }
324
325 /* Test with tree times the same transfer functions.
326 * Thus, only one should be written to file and when read copied three times.
327 */
328 spp = 3;
329 if (write_basic_IFD_data(&tif, filename, 0, 0, false))
330 return 1;
331 ret = TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, ptf0, ptf0, ptf0);
332 TIFFClose(tif);
333 ret = read_check_transferfunctions(&tif, filename, true, ptf0, ptf0, ptf0);
334 if (ret != 7)
335 {
336 fprintf(stderr, "TRANSFERFUNCTIONs with same content could not be read correctly.\n");
337 return 1;
338 }
339
340 /*---- Test the setting of the transfer functions with different wrong calls of TIFFSetField(). ----*/
341 /* (ptf0, NULL, NULL) For v4.5.1: writes ptf0 to file and reads it back.
342 * Now, this should work for spp=1 but not for spp=3
343 */
344 spp = 1;
345 if (write_basic_IFD_data(&tif, filename, 0, 0, false))
346 return 1;
347 ret = TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, ptf0, NULL, NULL);
348 TIFFClose(tif);
349 ret = read_check_transferfunctions(&tif, filename, true, ptf0, ptf1, ptf2);
350 if (ret != 1)
351 {
352 fprintf(stderr, "Unexpected at %d.\n", __LINE__);
353 return 1;
354 }
355 spp = 3;
356 /* (ptf0, NULL, NULL) For v4.5.1: writes ptf0 to file and reads it back.*/
357 if (write_basic_IFD_data(&tif, filename, 0, 0, false))
358 return 1;
359 ret = TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, ptf0, NULL, NULL);
360 TIFFClose(tif);
361 ret = read_check_transferfunctions(&tif, filename, true, ptf0, ptf1, ptf2);
362 if (ret != -1)
363 {
364 fprintf(stderr, "Unexpected at %d.\n", __LINE__);
365 return 1;
366 }
367
368 /* (ptf0, NULL, ptf2): For v4.5.1: Overflow error, when second ptf1 is copied in
369 * TIFFWriteDirectoryTagTransferfunction().*/
370 if (write_basic_IFD_data(&tif, filename, 0, 0, false))
371 return 1;
372 ret = TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, ptf0, NULL, ptf2);
373 TIFFClose(tif);
374 ret = read_check_transferfunctions(&tif, filename, true, ptf0, ptf1, ptf2);
375 if (ret != -1)
376 {
377 fprintf(stderr, "Unexpected at %d.\n", __LINE__);
378 return 1;
379 }
380
381 /* (ptf0, ptf1, NULL) For v4.5.1: writes ptf0 and ptf1 to file but does not read it back, because only 2 but 3
382 * required.*/
383 if (write_basic_IFD_data(&tif, filename, 0, 0, false))
384 return 1;
385 ret = TIFFSetField(tif, TIFFTAG_TRANSFERFUNCTION, ptf0, ptf1, NULL);
386 TIFFClose(tif);
387 ret = read_check_transferfunctions(&tif, filename, true, ptf0, ptf1, ptf2);
388 if (ret != -1)
389 {
390 fprintf(stderr, "Unexpected at %d.\n", __LINE__);
391 return 1;
392 }
393
394 /* All tests passed; delete file and exit with success status. */
395 unlink(filename);
396 fprintf(stderr, "-------- Test finished OK ----------\n");
397 return 0;
398 }