Coverage Report

Created: 2026-06-07 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtiff/libtiff/tif_print.c
Line
Count
Source
1
/*
2
 * Copyright (c) 1988-1997 Sam Leffler
3
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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
 * Directory Printing Support
29
 */
30
#include "tiffiop.h"
31
#include <stdio.h>
32
33
#include <ctype.h>
34
35
static void _TIFFprintAsciiBounded(FILE *fd, const char *cp, size_t max_chars);
36
37
static const char *const photoNames[] = {
38
    "min-is-white",                      /* PHOTOMETRIC_MINISWHITE */
39
    "min-is-black",                      /* PHOTOMETRIC_MINISBLACK */
40
    "RGB color",                         /* PHOTOMETRIC_RGB */
41
    "palette color (RGB from colormap)", /* PHOTOMETRIC_PALETTE */
42
    "transparency mask",                 /* PHOTOMETRIC_MASK */
43
    "separated",                         /* PHOTOMETRIC_SEPARATED */
44
    "YCbCr",                             /* PHOTOMETRIC_YCBCR */
45
    "7 (0x7)",
46
    "CIE L*a*b*", /* PHOTOMETRIC_CIELAB */
47
    "ICC L*a*b*", /* PHOTOMETRIC_ICCLAB */
48
    "ITU L*a*b*"  /* PHOTOMETRIC_ITULAB */
49
};
50
0
#define NPHOTONAMES (sizeof(photoNames) / sizeof(photoNames[0]))
51
52
static const char *const orientNames[] = {
53
    "0 (0x0)",
54
    "row 0 top, col 0 lhs",    /* ORIENTATION_TOPLEFT */
55
    "row 0 top, col 0 rhs",    /* ORIENTATION_TOPRIGHT */
56
    "row 0 bottom, col 0 rhs", /* ORIENTATION_BOTRIGHT */
57
    "row 0 bottom, col 0 lhs", /* ORIENTATION_BOTLEFT */
58
    "row 0 lhs, col 0 top",    /* ORIENTATION_LEFTTOP */
59
    "row 0 rhs, col 0 top",    /* ORIENTATION_RIGHTTOP */
60
    "row 0 rhs, col 0 bottom", /* ORIENTATION_RIGHTBOT */
61
    "row 0 lhs, col 0 bottom", /* ORIENTATION_LEFTBOT */
62
};
63
0
#define NORIENTNAMES (sizeof(orientNames) / sizeof(orientNames[0]))
64
65
static const struct tagname
66
{
67
    uint16_t tag;
68
    const char *name;
69
} tagnames[] = {
70
    {TIFFTAG_GDAL_METADATA, "GDAL Metadata"},
71
    {TIFFTAG_GDAL_NODATA, "GDAL NoDataValue"},
72
};
73
404k
#define NTAGS (sizeof(tagnames) / sizeof(tagnames[0]))
74
75
static void _TIFFPrintField(FILE *fd, const TIFFField *fip,
76
                            uint32_t value_count, void *raw_data)
77
145k
{
78
145k
    uint32_t j;
79
80
    /* Print a user-friendly name for tags of relatively common use, but */
81
    /* which aren't registered by libtiff itself. */
82
145k
    const char *field_name = fip->field_name;
83
145k
    if (TIFFFieldIsAnonymous(fip))
84
135k
    {
85
404k
        for (size_t i = 0; i < NTAGS; ++i)
86
270k
        {
87
270k
            if (fip->field_tag == tagnames[i].tag)
88
121
            {
89
121
                field_name = tagnames[i].name;
90
121
                break;
91
121
            }
92
270k
        }
93
135k
    }
94
145k
    fprintf(fd, "  %s: ", field_name);
95
96
2.11M
    for (j = 0; j < value_count; j++)
97
1.98M
    {
98
1.98M
        if (fip->field_type == TIFF_BYTE)
99
396k
            fprintf(fd, "%" PRIu8, ((uint8_t *)raw_data)[j]);
100
1.58M
        else if (fip->field_type == TIFF_UNDEFINED)
101
525k
            fprintf(fd, "0x%" PRIx8, ((uint8_t *)raw_data)[j]);
102
1.05M
        else if (fip->field_type == TIFF_SBYTE)
103
113k
            fprintf(fd, "%" PRId8, ((int8_t *)raw_data)[j]);
104
946k
        else if (fip->field_type == TIFF_SHORT)
105
224k
            fprintf(fd, "%" PRIu16, ((uint16_t *)raw_data)[j]);
106
721k
        else if (fip->field_type == TIFF_SSHORT)
107
67.0k
            fprintf(fd, "%" PRId16, ((int16_t *)raw_data)[j]);
108
654k
        else if (fip->field_type == TIFF_LONG)
109
411k
            fprintf(fd, "%" PRIu32, ((uint32_t *)raw_data)[j]);
110
242k
        else if (fip->field_type == TIFF_SLONG)
111
24.7k
            fprintf(fd, "%" PRId32, ((int32_t *)raw_data)[j]);
112
218k
        else if (fip->field_type == TIFF_IFD)
113
23.1k
            fprintf(fd, "0x%" PRIx32, ((uint32_t *)raw_data)[j]);
114
195k
        else if (fip->field_type == TIFF_RATIONAL ||
115
160k
                 fip->field_type == TIFF_SRATIONAL)
116
88.9k
        {
117
88.9k
            int tv_size = TIFFFieldSetGetSize(fip);
118
88.9k
            if (tv_size == 8)
119
797
                fprintf(fd, "%lf", ((double *)raw_data)[j]);
120
88.1k
            else
121
88.1k
                fprintf(fd, "%f", (double)((float *)raw_data)[j]);
122
88.9k
        }
123
106k
        else if (fip->field_type == TIFF_FLOAT)
124
66.9k
            fprintf(fd, "%f", (double)((float *)raw_data)[j]);
125
39.0k
        else if (fip->field_type == TIFF_LONG8)
126
2.26k
            fprintf(fd, "%" PRIu64, ((uint64_t *)raw_data)[j]);
127
36.8k
        else if (fip->field_type == TIFF_SLONG8)
128
2.35k
            fprintf(fd, "%" PRId64, ((int64_t *)raw_data)[j]);
129
34.4k
        else if (fip->field_type == TIFF_IFD8)
130
976
            fprintf(fd, "0x%" PRIx64, ((uint64_t *)raw_data)[j]);
131
33.4k
        else if (fip->field_type == TIFF_DOUBLE)
132
23.7k
            fprintf(fd, "%lf", ((double *)raw_data)[j]);
133
9.70k
        else if (fip->field_type == TIFF_ASCII)
134
9.70k
        {
135
9.70k
            fprintf(fd, "%s", (char *)raw_data);
136
9.70k
            break;
137
9.70k
        }
138
0
        else
139
0
        {
140
0
            fprintf(fd, "<unsupported data type in TIFFPrint>");
141
0
            break;
142
0
        }
143
144
1.97M
        if (j < value_count - 1)
145
1.83M
            fprintf(fd, ",");
146
1.97M
    }
147
148
145k
    fprintf(fd, "\n");
149
145k
}
150
151
static int _TIFFPrettyPrintField(TIFF *tif, const TIFFField *fip, FILE *fd,
152
                                 uint32_t tag, uint32_t value_count,
153
                                 void *raw_data)
154
145k
{
155
145k
    (void)tif;
156
157
    /* do not try to pretty print auto-defined fields */
158
145k
    if (TIFFFieldIsAnonymous(fip))
159
135k
    {
160
135k
        return 0;
161
135k
    }
162
163
10.1k
    switch (tag)
164
10.1k
    {
165
0
        case TIFFTAG_INKSET:
166
0
            if (value_count == 2 && fip->field_type == TIFF_SHORT)
167
0
            {
168
0
                fprintf(fd, "  Ink Set: ");
169
0
                switch (*((uint16_t *)raw_data))
170
0
                {
171
0
                    case INKSET_CMYK:
172
0
                        fprintf(fd, "CMYK\n");
173
0
                        break;
174
0
                    default:
175
0
                        fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n",
176
0
                                *((uint16_t *)raw_data),
177
0
                                *((uint16_t *)raw_data));
178
0
                        break;
179
0
                }
180
0
                return 1;
181
0
            }
182
0
            return 0;
183
184
0
        case TIFFTAG_DOTRANGE:
185
0
            if (value_count == 2 && fip->field_type == TIFF_SHORT)
186
0
            {
187
0
                fprintf(fd, "  Dot Range: %" PRIu16 "-%" PRIu16 "\n",
188
0
                        ((uint16_t *)raw_data)[0], ((uint16_t *)raw_data)[1]);
189
0
                return 1;
190
0
            }
191
0
            return 0;
192
193
0
        case TIFFTAG_WHITEPOINT:
194
0
            if (value_count == 2 && fip->field_type == TIFF_RATIONAL)
195
0
            {
196
0
                fprintf(fd, "  White Point: %g-%g\n",
197
0
                        (double)((float *)raw_data)[0],
198
0
                        (double)((float *)raw_data)[1]);
199
0
                return 1;
200
0
            }
201
0
            return 0;
202
203
0
        case TIFFTAG_XMLPACKET:
204
0
        {
205
0
            uint32_t i;
206
207
0
            fprintf(fd, "  XMLPacket (XMP Metadata):\n");
208
0
            for (i = 0; i < value_count; i++)
209
0
                fputc(((char *)raw_data)[i], fd);
210
0
            fprintf(fd, "\n");
211
0
            return 1;
212
0
        }
213
0
        case TIFFTAG_RICHTIFFIPTC:
214
0
            fprintf(fd, "  RichTIFFIPTC Data: <present>, %" PRIu32 " bytes\n",
215
0
                    value_count);
216
0
            return 1;
217
218
0
        case TIFFTAG_PHOTOSHOP:
219
0
            fprintf(fd, "  Photoshop Data: <present>, %" PRIu32 " bytes\n",
220
0
                    value_count);
221
0
            return 1;
222
223
0
        case TIFFTAG_ICCPROFILE:
224
0
            fprintf(fd, "  ICC Profile: <present>, %" PRIu32 " bytes\n",
225
0
                    value_count);
226
0
            return 1;
227
228
0
        case TIFFTAG_STONITS:
229
0
            if (value_count == 1 && fip->field_type == TIFF_DOUBLE)
230
0
            {
231
0
                fprintf(fd, "  Sample to Nits conversion factor: %.4e\n",
232
0
                        *((double *)raw_data));
233
0
                return 1;
234
0
            }
235
0
            return 0;
236
237
10.1k
        default:
238
10.1k
            break;
239
10.1k
    }
240
241
10.1k
    return 0;
242
10.1k
}
243
244
/*
245
 * Print the contents of the current directory
246
 * to the specified stdio file stream.
247
 */
248
void TIFFPrintDirectory(TIFF *tif, FILE *fd, long flags)
249
17.4k
{
250
17.4k
    TIFFDirectory *td = &tif->tif_dir;
251
17.4k
    const char *sep;
252
253
17.4k
    fprintf(fd, "TIFF Directory at offset 0x%" PRIx64 " (%" PRIu64 ")\n",
254
17.4k
            tif->tif_diroff, tif->tif_diroff);
255
17.4k
    if (TIFFFieldSet(tif, FIELD_SUBFILETYPE))
256
0
    {
257
0
        fprintf(fd, "  Subfile Type:");
258
0
        sep = " ";
259
0
        if (td->td_subfiletype & FILETYPE_REDUCEDIMAGE)
260
0
        {
261
0
            fprintf(fd, "%sreduced-resolution image", sep);
262
0
            sep = "/";
263
0
        }
264
0
        if (td->td_subfiletype & FILETYPE_PAGE)
265
0
        {
266
0
            fprintf(fd, "%smulti-page document", sep);
267
0
            sep = "/";
268
0
        }
269
0
        if (td->td_subfiletype & FILETYPE_MASK)
270
0
            fprintf(fd, "%stransparency mask", sep);
271
0
        fprintf(fd, " (%" PRIu32 " = 0x%" PRIx32 ")\n", td->td_subfiletype,
272
0
                td->td_subfiletype);
273
0
    }
274
17.4k
    if (TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS))
275
0
    {
276
0
        fprintf(fd, "  Image Width: %" PRIu32 " Image Length: %" PRIu32,
277
0
                td->td_imagewidth, td->td_imagelength);
278
0
        if (TIFFFieldSet(tif, FIELD_IMAGEDEPTH))
279
0
            fprintf(fd, " Image Depth: %" PRIu32, td->td_imagedepth);
280
0
        fprintf(fd, "\n");
281
0
    }
282
17.4k
    if (TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
283
0
    {
284
0
        fprintf(fd, "  Tile Width: %" PRIu32 " Tile Length: %" PRIu32,
285
0
                td->td_tilewidth, td->td_tilelength);
286
0
        if (TIFFFieldSet(tif, FIELD_TILEDEPTH))
287
0
            fprintf(fd, " Tile Depth: %" PRIu32, td->td_tiledepth);
288
0
        fprintf(fd, "\n");
289
0
    }
290
17.4k
    if (TIFFFieldSet(tif, FIELD_RESOLUTION))
291
0
    {
292
0
        fprintf(fd, "  Resolution: %g, %g", (double)td->td_xresolution,
293
0
                (double)td->td_yresolution);
294
0
        if (TIFFFieldSet(tif, FIELD_RESOLUTIONUNIT))
295
0
        {
296
0
            switch (td->td_resolutionunit)
297
0
            {
298
0
                case RESUNIT_NONE:
299
0
                    fprintf(fd, " (unitless)");
300
0
                    break;
301
0
                case RESUNIT_INCH:
302
0
                    fprintf(fd, " pixels/inch");
303
0
                    break;
304
0
                case RESUNIT_CENTIMETER:
305
0
                    fprintf(fd, " pixels/cm");
306
0
                    break;
307
0
                default:
308
0
                    fprintf(fd, " (unit %" PRIu16 " = 0x%" PRIx16 ")",
309
0
                            td->td_resolutionunit, td->td_resolutionunit);
310
0
                    break;
311
0
            }
312
0
        }
313
0
        fprintf(fd, "\n");
314
0
    }
315
17.4k
    if (TIFFFieldSet(tif, FIELD_POSITION))
316
0
        fprintf(fd, "  Position: %g, %g\n", (double)td->td_xposition,
317
0
                (double)td->td_yposition);
318
17.4k
    if (TIFFFieldSet(tif, FIELD_BITSPERSAMPLE))
319
0
        fprintf(fd, "  Bits/Sample: %" PRIu16 "\n", td->td_bitspersample);
320
17.4k
    if (TIFFFieldSet(tif, FIELD_SAMPLEFORMAT))
321
0
    {
322
0
        fprintf(fd, "  Sample Format: ");
323
0
        switch (td->td_sampleformat)
324
0
        {
325
0
            case SAMPLEFORMAT_VOID:
326
0
                fprintf(fd, "void\n");
327
0
                break;
328
0
            case SAMPLEFORMAT_INT:
329
0
                fprintf(fd, "signed integer\n");
330
0
                break;
331
0
            case SAMPLEFORMAT_UINT:
332
0
                fprintf(fd, "unsigned integer\n");
333
0
                break;
334
0
            case SAMPLEFORMAT_IEEEFP:
335
0
                fprintf(fd, "IEEE floating point\n");
336
0
                break;
337
0
            case SAMPLEFORMAT_COMPLEXINT:
338
0
                fprintf(fd, "complex signed integer\n");
339
0
                break;
340
0
            case SAMPLEFORMAT_COMPLEXIEEEFP:
341
0
                fprintf(fd, "complex IEEE floating point\n");
342
0
                break;
343
0
            default:
344
0
                fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n",
345
0
                        td->td_sampleformat, td->td_sampleformat);
346
0
                break;
347
0
        }
348
0
    }
349
17.4k
    if (TIFFFieldSet(tif, FIELD_COMPRESSION))
350
17.4k
    {
351
17.4k
        const TIFFCodec *c = TIFFFindCODEC(td->td_compression);
352
17.4k
        fprintf(fd, "  Compression Scheme: ");
353
17.4k
        if (c)
354
17.4k
            fprintf(fd, "%s\n", c->name);
355
0
        else
356
0
            fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", td->td_compression,
357
0
                    td->td_compression);
358
17.4k
    }
359
17.4k
    if (TIFFFieldSet(tif, FIELD_PHOTOMETRIC))
360
0
    {
361
0
        fprintf(fd, "  Photometric Interpretation: ");
362
0
        if (td->td_photometric < NPHOTONAMES)
363
0
            fprintf(fd, "%s\n", photoNames[td->td_photometric]);
364
0
        else
365
0
        {
366
0
            switch (td->td_photometric)
367
0
            {
368
0
                case PHOTOMETRIC_LOGL:
369
0
                    fprintf(fd, "CIE Log2(L)\n");
370
0
                    break;
371
0
                case PHOTOMETRIC_LOGLUV:
372
0
                    fprintf(fd, "CIE Log2(L) (u',v')\n");
373
0
                    break;
374
0
                default:
375
0
                    fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n",
376
0
                            td->td_photometric, td->td_photometric);
377
0
                    break;
378
0
            }
379
0
        }
380
0
    }
381
17.4k
    if (TIFFFieldSet(tif, FIELD_EXTRASAMPLES) && td->td_extrasamples &&
382
0
        td->td_sampleinfo)
383
0
    {
384
0
        uint16_t i;
385
0
        fprintf(fd, "  Extra Samples: %" PRIu16 "<", td->td_extrasamples);
386
0
        sep = "";
387
0
        for (i = 0; i < td->td_extrasamples; i++)
388
0
        {
389
0
            switch (td->td_sampleinfo[i])
390
0
            {
391
0
                case EXTRASAMPLE_UNSPECIFIED:
392
0
                    fprintf(fd, "%sunspecified", sep);
393
0
                    break;
394
0
                case EXTRASAMPLE_ASSOCALPHA:
395
0
                    fprintf(fd, "%sassoc-alpha", sep);
396
0
                    break;
397
0
                case EXTRASAMPLE_UNASSALPHA:
398
0
                    fprintf(fd, "%sunassoc-alpha", sep);
399
0
                    break;
400
0
                default:
401
0
                    fprintf(fd, "%s%" PRIu16 " (0x%" PRIx16 ")", sep,
402
0
                            td->td_sampleinfo[i], td->td_sampleinfo[i]);
403
0
                    break;
404
0
            }
405
0
            sep = ", ";
406
0
        }
407
0
        fprintf(fd, ">\n");
408
0
    }
409
17.4k
    if (TIFFFieldSet(tif, FIELD_INKNAMES))
410
0
    {
411
0
        char *cp;
412
0
        uint16_t i;
413
0
        fprintf(fd, "  Ink Names: ");
414
0
        i = td->td_samplesperpixel;
415
0
        sep = "";
416
0
        for (cp = td->td_inknames;
417
0
             i > 0 && cp < td->td_inknames + td->td_inknameslen;
418
0
             cp = strchr(cp, '\0') + 1, i--)
419
0
        {
420
0
            size_t max_chars =
421
0
                (size_t)(td->td_inknameslen - (cp - td->td_inknames));
422
0
            fputs(sep, fd);
423
0
            _TIFFprintAsciiBounded(fd, cp, max_chars);
424
0
            sep = ", ";
425
0
        }
426
0
        fputs("\n", fd);
427
0
    }
428
17.4k
    if (TIFFFieldSet(tif, FIELD_NUMBEROFINKS))
429
0
    {
430
0
        fprintf(fd, "  NumberOfInks: %d\n", td->td_numberofinks);
431
0
    }
432
17.4k
    if (TIFFFieldSet(tif, FIELD_THRESHHOLDING))
433
0
    {
434
0
        fprintf(fd, "  Thresholding: ");
435
0
        switch (td->td_threshholding)
436
0
        {
437
0
            case THRESHHOLD_BILEVEL:
438
0
                fprintf(fd, "bilevel art scan\n");
439
0
                break;
440
0
            case THRESHHOLD_HALFTONE:
441
0
                fprintf(fd, "halftone or dithered scan\n");
442
0
                break;
443
0
            case THRESHHOLD_ERRORDIFFUSE:
444
0
                fprintf(fd, "error diffused\n");
445
0
                break;
446
0
            default:
447
0
                fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n",
448
0
                        td->td_threshholding, td->td_threshholding);
449
0
                break;
450
0
        }
451
0
    }
452
17.4k
    if (TIFFFieldSet(tif, FIELD_FILLORDER))
453
0
    {
454
0
        fprintf(fd, "  FillOrder: ");
455
0
        switch (td->td_fillorder)
456
0
        {
457
0
            case FILLORDER_MSB2LSB:
458
0
                fprintf(fd, "msb-to-lsb\n");
459
0
                break;
460
0
            case FILLORDER_LSB2MSB:
461
0
                fprintf(fd, "lsb-to-msb\n");
462
0
                break;
463
0
            default:
464
0
                fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", td->td_fillorder,
465
0
                        td->td_fillorder);
466
0
                break;
467
0
        }
468
0
    }
469
17.4k
    if (TIFFFieldSet(tif, FIELD_YCBCRSUBSAMPLING))
470
0
    {
471
0
        fprintf(fd, "  YCbCr Subsampling: %" PRIu16 ", %" PRIu16 "\n",
472
0
                td->td_ycbcrsubsampling[0], td->td_ycbcrsubsampling[1]);
473
0
    }
474
17.4k
    if (TIFFFieldSet(tif, FIELD_YCBCRPOSITIONING))
475
0
    {
476
0
        fprintf(fd, "  YCbCr Positioning: ");
477
0
        switch (td->td_ycbcrpositioning)
478
0
        {
479
0
            case YCBCRPOSITION_CENTERED:
480
0
                fprintf(fd, "centered\n");
481
0
                break;
482
0
            case YCBCRPOSITION_COSITED:
483
0
                fprintf(fd, "cosited\n");
484
0
                break;
485
0
            default:
486
0
                fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n",
487
0
                        td->td_ycbcrpositioning, td->td_ycbcrpositioning);
488
0
                break;
489
0
        }
490
0
    }
491
17.4k
    if (TIFFFieldSet(tif, FIELD_HALFTONEHINTS))
492
0
        fprintf(fd, "  Halftone Hints: light %" PRIu16 " dark %" PRIu16 "\n",
493
0
                td->td_halftonehints[0], td->td_halftonehints[1]);
494
17.4k
    if (TIFFFieldSet(tif, FIELD_ORIENTATION))
495
0
    {
496
0
        fprintf(fd, "  Orientation: ");
497
0
        if (td->td_orientation < NORIENTNAMES)
498
0
            fprintf(fd, "%s\n", orientNames[td->td_orientation]);
499
0
        else
500
0
            fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n", td->td_orientation,
501
0
                    td->td_orientation);
502
0
    }
503
17.4k
    if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
504
0
        fprintf(fd, "  Samples/Pixel: %" PRIx16 "\n", td->td_samplesperpixel);
505
17.4k
    if (TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
506
0
    {
507
0
        fprintf(fd, "  Rows/Strip: ");
508
0
        if (td->td_rowsperstrip == (uint32_t)-1)
509
0
            fprintf(fd, "(infinite)\n");
510
0
        else
511
0
            fprintf(fd, "%" PRIu32 "\n", td->td_rowsperstrip);
512
0
    }
513
17.4k
    if (TIFFFieldSet(tif, FIELD_MINSAMPLEVALUE))
514
0
        fprintf(fd, "  Min Sample Value: %" PRIu16 "\n", td->td_minsamplevalue);
515
17.4k
    if (TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
516
0
        fprintf(fd, "  Max Sample Value: %" PRIu16 "\n", td->td_maxsamplevalue);
517
17.4k
    if (TIFFFieldSet(tif, FIELD_SMINSAMPLEVALUE))
518
0
    {
519
0
        int i;
520
0
        int count =
521
0
            (tif->tif_flags & TIFF_PERSAMPLE) ? td->td_samplesperpixel : 1;
522
0
        fprintf(fd, "  SMin Sample Value:");
523
0
        for (i = 0; i < count; ++i)
524
0
            fprintf(fd, " %g", td->td_sminsamplevalue[i]);
525
0
        fprintf(fd, "\n");
526
0
    }
527
17.4k
    if (TIFFFieldSet(tif, FIELD_SMAXSAMPLEVALUE))
528
0
    {
529
0
        int i;
530
0
        int count =
531
0
            (tif->tif_flags & TIFF_PERSAMPLE) ? td->td_samplesperpixel : 1;
532
0
        fprintf(fd, "  SMax Sample Value:");
533
0
        for (i = 0; i < count; ++i)
534
0
            fprintf(fd, " %g", td->td_smaxsamplevalue[i]);
535
0
        fprintf(fd, "\n");
536
0
    }
537
17.4k
    if (TIFFFieldSet(tif, FIELD_PLANARCONFIG))
538
0
    {
539
0
        fprintf(fd, "  Planar Configuration: ");
540
0
        switch (td->td_planarconfig)
541
0
        {
542
0
            case PLANARCONFIG_CONTIG:
543
0
                fprintf(fd, "single image plane\n");
544
0
                break;
545
0
            case PLANARCONFIG_SEPARATE:
546
0
                fprintf(fd, "separate image planes\n");
547
0
                break;
548
0
            default:
549
0
                fprintf(fd, "%" PRIu16 " (0x%" PRIx16 ")\n",
550
0
                        td->td_planarconfig, td->td_planarconfig);
551
0
                break;
552
0
        }
553
0
    }
554
17.4k
    if (TIFFFieldSet(tif, FIELD_PAGENUMBER))
555
0
        fprintf(fd, "  Page Number: %" PRIu16 "-%" PRIu16 "\n",
556
0
                td->td_pagenumber[0], td->td_pagenumber[1]);
557
17.4k
    if (TIFFFieldSet(tif, FIELD_COLORMAP) && td->td_colormap[0] &&
558
0
        td->td_colormap[1] && td->td_colormap[2])
559
0
    {
560
0
        fprintf(fd, "  Color Map: ");
561
0
        if (flags & TIFFPRINT_COLORMAP)
562
0
        {
563
0
            fprintf(fd, "\n");
564
0
            uint64_t n = 1ULL << td->td_bitspersample;
565
0
            for (uint64_t l = 0u; l < n; l++)
566
0
                fprintf(fd,
567
0
                        "   %5" PRIu64 ": %5" PRIu16 " %5" PRIu16 " %5" PRIu16
568
0
                        "\n",
569
0
                        l, td->td_colormap[0][l], td->td_colormap[1][l],
570
0
                        td->td_colormap[2][l]);
571
0
        }
572
0
        else
573
0
            fprintf(fd, "(present)\n");
574
0
    }
575
17.4k
    if (TIFFFieldSet(tif, FIELD_REFBLACKWHITE))
576
0
    {
577
0
        int i;
578
0
        fprintf(fd, "  Reference Black/White:\n");
579
0
        for (i = 0; i < 3; i++)
580
0
            fprintf(fd, "    %2d: %5g %5g\n", i,
581
0
                    (double)td->td_refblackwhite[2 * i + 0],
582
0
                    (double)td->td_refblackwhite[2 * i + 1]);
583
0
    }
584
17.4k
    if (TIFFFieldSet(tif, FIELD_TRANSFERFUNCTION) &&
585
0
        td->td_transferfunction[0] &&
586
0
        ((td->td_samplesperpixel - td->td_extrasamples > 1 &&
587
0
          td->td_transferfunction[1] && td->td_transferfunction[2]) ||
588
0
         td->td_samplesperpixel - td->td_extrasamples <= 1))
589
0
    {
590
0
        fprintf(fd, "  Transfer Function: ");
591
0
        if (flags & TIFFPRINT_CURVES)
592
0
        {
593
0
            fprintf(fd, "\n");
594
0
            uint64_t n = 1ULL << td->td_bitspersample;
595
0
            for (uint64_t l = 0; l < n; l++)
596
0
            {
597
0
                uint16_t i;
598
0
                fprintf(fd, "    %2" PRIu64 ": %5" PRIu16, l,
599
0
                        td->td_transferfunction[0][l]);
600
0
                for (i = 1;
601
0
                     i < td->td_samplesperpixel - td->td_extrasamples && i < 3;
602
0
                     i++)
603
0
                    fprintf(fd, " %5" PRIu16, td->td_transferfunction[i][l]);
604
0
                fputc('\n', fd);
605
0
            }
606
0
        }
607
0
        else
608
0
            fprintf(fd, "(present)\n");
609
0
    }
610
17.4k
    if (TIFFFieldSet(tif, FIELD_SUBIFD) && (td->td_subifd))
611
0
    {
612
0
        uint16_t i;
613
0
        fprintf(fd, "  SubIFD Offsets:");
614
0
        for (i = 0; i < td->td_nsubifd; i++)
615
0
            fprintf(fd, " %5" PRIu64, td->td_subifd[i]);
616
0
        fputc('\n', fd);
617
0
    }
618
619
    /*
620
    ** Custom tag support.
621
    */
622
17.4k
    {
623
17.4k
        int i;
624
17.4k
        short count;
625
626
17.4k
        count = (short)TIFFGetTagListCount(tif);
627
184k
        for (i = 0; i < count; i++)
628
166k
        {
629
166k
            uint32_t tag = TIFFGetTagListEntry(tif, i);
630
166k
            const TIFFField *fip;
631
166k
            uint32_t value_count;
632
166k
            int mem_alloc = 0;
633
166k
            void *raw_data = NULL;
634
166k
            uint16_t dotrange[2]; /* must be kept in that scope and not moved in
635
                                     the below TIFFTAG_DOTRANGE specific case */
636
637
166k
            fip = TIFFFieldWithTag(tif, tag);
638
166k
            if (fip == NULL)
639
0
                continue;
640
641
166k
            if (fip->field_passcount)
642
156k
            {
643
156k
                if (fip->field_readcount == TIFF_VARIABLE2)
644
156k
                {
645
156k
                    if (TIFFGetField(tif, tag, &value_count, &raw_data) != 1)
646
0
                        continue;
647
156k
                }
648
239
                else if (fip->field_readcount == TIFF_VARIABLE)
649
239
                {
650
239
                    uint16_t small_value_count;
651
239
                    if (TIFFGetField(tif, tag, &small_value_count, &raw_data) !=
652
239
                        1)
653
0
                        continue;
654
239
                    value_count = small_value_count;
655
239
                }
656
0
                else
657
0
                {
658
0
                    assert(fip->field_readcount == TIFF_VARIABLE ||
659
0
                           fip->field_readcount == TIFF_VARIABLE2);
660
0
                    continue;
661
0
                }
662
156k
            }
663
9.92k
            else
664
9.92k
            {
665
9.92k
                if (fip->field_readcount == TIFF_VARIABLE ||
666
9.57k
                    fip->field_readcount == TIFF_VARIABLE2)
667
347
                    value_count = 1;
668
9.57k
                else if (fip->field_readcount == TIFF_SPP)
669
0
                    value_count = td->td_samplesperpixel;
670
9.57k
                else
671
9.57k
                    value_count = (uint32_t)fip->field_readcount;
672
9.92k
                if (fip->field_tag == TIFFTAG_DOTRANGE &&
673
0
                    strcmp(fip->field_name, "DotRange") == 0)
674
0
                {
675
                    /* TODO: This is an evil exception and should not have been
676
                       handled this way ... likely best if we move it into
677
                       the directory structure with an explicit field in
678
                       libtiff 4.1 and assign it a FIELD_ value */
679
0
                    raw_data = dotrange;
680
0
                    TIFFGetField(tif, tag, dotrange + 0, dotrange + 1);
681
0
                }
682
9.92k
                else if (fip->field_type == TIFF_ASCII ||
683
7.46k
                         fip->field_readcount == TIFF_VARIABLE ||
684
7.46k
                         fip->field_readcount == TIFF_VARIABLE2 ||
685
7.46k
                         fip->field_readcount == TIFF_SPP || value_count > 1)
686
4.86k
                {
687
4.86k
                    if (TIFFGetField(tif, tag, &raw_data) != 1)
688
0
                        continue;
689
4.86k
                }
690
5.06k
                else
691
5.06k
                {
692
                    /*--: Rational2Double: For Rationals evaluate
693
                     * "set_get_field_type" to determine internal storage size.
694
                     */
695
5.06k
                    int tv_size = TIFFFieldSetGetSize(fip);
696
5.06k
                    raw_data = _TIFFCheckMalloc(tif, value_count, tv_size,
697
5.06k
                                                "for tag data");
698
5.06k
                    mem_alloc = 1;
699
5.06k
                    if (TIFFGetField(tif, tag, raw_data) != 1)
700
0
                    {
701
0
                        _TIFFfreeExt(tif, raw_data);
702
0
                        continue;
703
0
                    }
704
5.06k
                }
705
9.92k
            }
706
707
            /*
708
             * Catch the tags which needs to be specially handled
709
             * and pretty print them. If tag not handled in
710
             * _TIFFPrettyPrintField() fall down and print it as
711
             * any other tag.
712
             */
713
166k
            if (raw_data != NULL &&
714
145k
                !_TIFFPrettyPrintField(tif, fip, fd, tag, value_count,
715
145k
                                       raw_data))
716
145k
                _TIFFPrintField(fd, fip, value_count, raw_data);
717
718
166k
            if (mem_alloc)
719
5.06k
                _TIFFfreeExt(tif, raw_data);
720
166k
        }
721
17.4k
    }
722
723
17.4k
    if (tif->tif_tagmethods.printdir)
724
0
        (*tif->tif_tagmethods.printdir)(tif, fd, flags);
725
726
17.4k
    if ((flags & TIFFPRINT_STRIPS) && TIFFFieldSet(tif, FIELD_STRIPOFFSETS))
727
0
    {
728
0
        uint32_t s;
729
730
0
        fprintf(fd, "  %" PRIu32 " %s:\n", td->td_nstrips,
731
0
                isTiled(tif) ? "Tiles" : "Strips");
732
0
        for (s = 0; s < td->td_nstrips; s++)
733
0
            fprintf(fd, "    %3" PRIu32 ": [%8" PRIu64 ", %8" PRIu64 "]\n", s,
734
0
                    TIFFGetStrileOffset(tif, s),
735
0
                    TIFFGetStrileByteCount(tif, s));
736
0
    }
737
17.4k
}
738
739
void _TIFFprintAscii(FILE *fd, const char *cp)
740
0
{
741
0
    _TIFFprintAsciiBounded(fd, cp, strlen(cp));
742
0
}
743
744
static void _TIFFprintAsciiBounded(FILE *fd, const char *cp, size_t max_chars)
745
0
{
746
0
    for (; max_chars > 0 && *cp != '\0'; cp++, max_chars--)
747
0
    {
748
0
        const char *tp;
749
750
0
        if (isprint((int)*cp))
751
0
        {
752
0
            fputc(*cp, fd);
753
0
            continue;
754
0
        }
755
0
        for (tp = "\tt\bb\rr\nn\vv"; *tp; tp++)
756
0
            if (*tp++ == *cp)
757
0
                break;
758
0
        if (*tp)
759
0
            fprintf(fd, "\\%c", *tp);
760
0
        else
761
0
            fprintf(fd, "\\%03o", (unsigned int)(*cp & 0xff));
762
0
    }
763
0
}
764
765
void _TIFFprintAsciiTag(FILE *fd, const char *name, const char *value)
766
0
{
767
0
    fprintf(fd, "  %s: \"", name);
768
0
    _TIFFprintAscii(fd, value);
769
0
    fprintf(fd, "\"\n");
770
0
}