summaryrefslogtreecommitdiff
path: root/test/test_directory.c
blob: 12a6fd4e62b400a485d8673fa6590e12e7767356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * TIFF Library
 *
 * The purpose of this test suite is to test the correctness of
 * TIFFWriteDirectory() when appending multiple directories to an open file.
 *
 * Currently, there is an optimization where the TIFF data structure stores the
 * offset of the last written directory in order to avoid having to traverse the
 * entire directory list each time a new one is added. The offset is not stored
 * in the file itself, only in the in-memory data structure. This means we still
 * go through the entire list the first time a directory is appended to a
 * newly-opened file, and the shortcut is taken for subsequent directory writes.
 *
 * In order to test the correctness of the optimization, the
 * `test_lastdir_offset` function writes 10 directories to two different files.
 * For the first file we use the optimization, by simply calling
 * TIFFWriteDirectory() repeatedly on an open TIFF handle. For the second file,
 * we avoid the optimization by closing the file after each call to
 * TIFFWriteDirectory(), which means the next directory write will traverse the
 * entire list.
 *
 * Finally, the two files are compared to check that the number of directories
 * written is the same, and that their offsets match. The test is then repeated
 * using BigTIFF files.
 *
 */

#include "tif_config.h"

#include <stdbool.h>
#include <stdio.h>

#include "tiffio.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#define SPP 3            /* Samples per pixel */
#define N_DIRECTORIES 10 /* Number of directories to write */
const uint16_t width = 1;
const uint16_t length = 1;
const uint16_t bps = 8;
const uint16_t photometric = PHOTOMETRIC_RGB;
const uint16_t rows_per_strip = 1;
const uint16_t planarconfig = PLANARCONFIG_CONTIG;

int write_data_to_current_directory(TIFF *tif)
{
    unsigned char buf[SPP] = {0, 127, 255};

    if (!tif)
    {
        fprintf(stderr, "Invalid TIFF handle.\n");
        return 1;
    }
    if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width))
    {
        fprintf(stderr, "Can't set ImageWidth tag.\n");
        return 1;
    }
    if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length))
    {
        fprintf(stderr, "Can't set ImageLength tag.\n");
        return 1;
    }
    if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps))
    {
        fprintf(stderr, "Can't set BitsPerSample tag.\n");
        return 1;
    }
    if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, SPP))
    {
        fprintf(stderr, "Can't set SamplesPerPixel tag.\n");
        return 1;
    }
    if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip))
    {
        fprintf(stderr, "Can't set SamplesPerPixel tag.\n");
        return 1;
    }
    if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, planarconfig))
    {
        fprintf(stderr, "Can't set PlanarConfiguration tag.\n");
        return 1;
    }
    if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric))
    {
        fprintf(stderr, "Can't set PhotometricInterpretation tag.\n");
        return 1;
    }

    /* Write dummy pixel data. */
    if (TIFFWriteScanline(tif, buf, 0, 0) == -1)
    {
        fprintf(stderr, "Can't write image data.\n");
        return 1;
    }

    return 0;
}

int write_directory_to_closed_file(const char *filename, bool is_big_tiff)
{
    TIFF *tif;
    tif = TIFFOpen(filename, is_big_tiff ? "a8" : "a");
    if (!tif)
    {
        fprintf(stderr, "Can't create/open %s\n", filename);
        return 1;
    }

    if (write_data_to_current_directory(tif))
    {
        fprintf(stderr, "Can't write data to current directory.\n");
        TIFFClose(tif);
        return 1;
    }

    if (!TIFFWriteDirectory(tif))
    {
        fprintf(stderr, "TIFFWriteDirectory() failed.\n");
        TIFFClose(tif);
        return 1;
    }

    TIFFClose(tif);
    return 0;
}

int count_directories(const char *filename, int *count)
{
    TIFF *tif;
    *count = 0;

    tif = TIFFOpen(filename, "r");
    if (!tif)
    {
        fprintf(stderr, "Can't read %s\n", filename);
        return 1;
    }

    do
    {
        (*count)++;
    } while (TIFFReadDirectory(tif));

    TIFFClose(tif);
    return 0;
}

/* Gets a list of the directory offsets in a file. Assumes the file has at least
 * N_DIRECTORIES directories */
int get_dir_offsets(const char *filename, uint64_t *offsets)
{
    TIFF *tif;
    int i;

    tif = TIFFOpen(filename, "r");
    if (!tif)
    {
        fprintf(stderr, "Can't read %s\n", filename);
        return 1;
    }

    for (i = 0; i < N_DIRECTORIES; i++)
    {
        offsets[i] = TIFFCurrentDirOffset(tif);
        if (!TIFFReadDirectory(tif) && i < (N_DIRECTORIES - 1))
        {
            fprintf(stderr, "Can't read %d.th directory from %s\n", i,
                    filename);
            TIFFClose(tif);
            return 2;
        }
    }

    TIFFClose(tif);
    return 0;
}

/* Checks that rewriting a directory does not break the directory linked list
 *
 * This could happen because TIFFRewriteDirectory relies on the traversal of the
 * directory linked list in order to move the rewritten directory to the end of
 * the file. This means the `lastdir_offset` optimization should be skipped,
 * otherwise the linked list will be broken at the point where it connected to
 * the rewritten directory, resulting in the loss of the directories that come
 * after it.
 */
int test_rewrite_lastdir_offset(bool is_big_tiff)
{
    const char *filename = "test_directory_rewrite.tif";
    int i, count;
    TIFF *tif;

    /* Create a file and write N_DIRECTORIES (10) directories to it */
    tif = TIFFOpen(filename, is_big_tiff ? "w8" : "w");
    if (!tif)
    {
        fprintf(stderr, "Can't create %s\n", filename);
        return 1;
    }
    for (i = 0; i < N_DIRECTORIES; i++)
    {
        if (write_data_to_current_directory(tif))
        {
            fprintf(stderr, "Can't write data to current directory in %s\n",
                    filename);
            goto failure;
        }
        if (!TIFFWriteDirectory(tif))
        {
            fprintf(stderr, "Can't write directory to %s\n", filename);
            goto failure;
        }
    }

    /* Without closing it, go to the fifth directory */
    TIFFSetDirectory(tif, 4);

    /* Rewrite the fifth directory by calling TIFFRewriteDirectory */
    if (write_data_to_current_directory(tif))
    {
        fprintf(stderr, "Can't write data to fifth directory in %s\n",
                filename);
        goto failure;
    }
    if (!TIFFRewriteDirectory(tif))
    {
        fprintf(stderr, "Can't rewrite fifth directory to %s\n", filename);
        goto failure;
    }

    TIFFClose(tif);
    tif = NULL;

    /* Check that the file has the expected number of directories*/
    if (count_directories(filename, &count))
    {
        fprintf(stderr, "Error counting directories in file %s.\n", filename);
        goto failure;
    }
    if (count != N_DIRECTORIES)
    {
        fprintf(
            stderr,
            "Unexpected number of directories in %s. Expected %i, found %i.\n",
            filename, N_DIRECTORIES, count);
        goto failure;
    }

    unlink(filename);
    return 0;

failure:
    if (tif)
    {
        TIFFClose(tif);
        tif = NULL;
    }
    unlink(filename);
    return 1;
}

/* Compares multi-directory files written with and without the lastdir
 * optimization */
int test_lastdir_offset(bool is_big_tiff)
{
    const char *filename_optimized = "test_directory_optimized.tif";
    const char *filename_non_optimized = "test_directory_non_optimized.tif";
    int i, count_optimized, count_non_optimized;
    uint64_t offsets_optimized[N_DIRECTORIES];
    uint64_t offsets_non_optimized[N_DIRECTORIES];
    TIFF *tif;

    /* First file: open it and add multiple directories. This uses the lastdir
     * optimization. */
    tif = TIFFOpen(filename_optimized, is_big_tiff ? "w8" : "w");
    if (!tif)
    {
        fprintf(stderr, "Can't create %s\n", filename_optimized);
        return 1;
    }
    for (i = 0; i < N_DIRECTORIES; i++)
    {
        if (write_data_to_current_directory(tif))
        {
            fprintf(stderr, "Can't write data to current directory in %s\n",
                    filename_optimized);
            goto failure;
        }
        if (!TIFFWriteDirectory(tif))
        {
            fprintf(stderr, "Can't write directory to %s\n",
                    filename_optimized);
            goto failure;
        }
    }
    TIFFClose(tif);
    tif = NULL;

    /* Second file: close it after adding each directory. This avoids the
     * lastdir optimization. */
    for (i = 0; i < N_DIRECTORIES; i++)
    {
        if (write_directory_to_closed_file(filename_non_optimized, is_big_tiff))
        {
            fprintf(stderr, "Can't write directory to %s\n",
                    filename_non_optimized);
            goto failure;
        }
    }

    /* Check that both files have the expected number of directories */
    if (count_directories(filename_optimized, &count_optimized))
    {
        fprintf(stderr, "Error counting directories in file %s.\n",
                filename_optimized);
        goto failure;
    }
    if (count_optimized != N_DIRECTORIES)
    {
        fprintf(
            stderr,
            "Unexpected number of directories in %s. Expected %i, found %i.\n",
            filename_optimized, N_DIRECTORIES, count_optimized);
        goto failure;
    }
    if (count_directories(filename_non_optimized, &count_non_optimized))
    {
        fprintf(stderr, "Error counting directories in file %s.\n",
                filename_non_optimized);
        goto failure;
    }
    if (count_non_optimized != N_DIRECTORIES)
    {
        fprintf(
            stderr,
            "Unexpected number of directories in %s. Expected %i, found %i.\n",
            filename_non_optimized, N_DIRECTORIES, count_non_optimized);
        goto failure;
    }

    /* Check that both files have the same directory offsets */
    if (get_dir_offsets(filename_optimized, offsets_optimized))
    {
        fprintf(stderr, "Error reading directory offsets from %s.\n",
                filename_optimized);
        goto failure;
    }
    if (get_dir_offsets(filename_non_optimized, offsets_non_optimized))
    {
        fprintf(stderr, "Error reading directory offsets from %s.\n",
                filename_non_optimized);
        goto failure;
    }
    for (i = 0; i < N_DIRECTORIES; i++)
    {
        if (offsets_optimized[i] != offsets_non_optimized[i])
        {
            fprintf(stderr,
                    "Unexpected directory offset for directory %i, expected "
                    "offset %" PRIu64 " but got %" PRIu64 ".\n",
                    i, offsets_non_optimized[i], offsets_optimized[i]);
            goto failure;
        }
    }

    unlink(filename_optimized);
    unlink(filename_non_optimized);

    return 0;

failure:
    if (tif)
    {
        TIFFClose(tif);
        tif = NULL;
    }
    unlink(filename_optimized);
    unlink(filename_non_optimized);
    return 1;
}

int main()
{
    if (test_lastdir_offset(false))
    {
        fprintf(stderr, "Failed during non-BigTIFF WriteDirectory test.\n");
        return 1;
    }
    if (test_lastdir_offset(true))
    {
        fprintf(stderr, "Failed during BigTIFF WriteDirectory test.\n");
        return 1;
    }

    if (test_rewrite_lastdir_offset(false))
    {
        fprintf(stderr, "Failed during non-BigTIFF RewriteDirectory test.\n");
        return 1;
    }
    if (test_rewrite_lastdir_offset(true))
    {
        fprintf(stderr, "Failed during BigTIFF RewriteDirectory test.\n");
        return 1;
    }
    return 0;
}