1 /* $Id: tif_print.c,v 1.59 2012-06-13 01:08:51 fwarmerdam Exp $ */
2 
3 /*
4  * Copyright (c) 1988-1997 Sam Leffler
5  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and
8  * its documentation for any purpose is hereby granted without fee, provided
9  * that (i) the above copyright notices and this permission notice appear in
10  * all copies of the software and related documentation, and (ii) the names of
11  * Sam Leffler and Silicon Graphics may not be used in any advertising or
12  * publicity relating to the software without the specific, prior written
13  * permission of Sam Leffler and Silicon Graphics.
14  *
15  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  */
26 
27 /*
28  * TIFF Library.
29  *
30  * Directory Printing Support
31  */
32 #include "tiffiop.h"
33 #include <stdio.h>
34 
35 #include <ctype.h>
36 
37 static void
38 _TIFFprintAsciiBounded(FILE* fd, const char* cp, int max_chars);
39 
40 static const char *photoNames[] = {
41     "min-is-white",				/* PHOTOMETRIC_MINISWHITE */
42     "min-is-black",				/* PHOTOMETRIC_MINISBLACK */
43     "RGB color",				/* PHOTOMETRIC_RGB */
44     "palette color (RGB from colormap)",	/* PHOTOMETRIC_PALETTE */
45     "transparency mask",			/* PHOTOMETRIC_MASK */
46     "separated",				/* PHOTOMETRIC_SEPARATED */
47     "YCbCr",					/* PHOTOMETRIC_YCBCR */
48     "7 (0x7)",
49     "CIE L*a*b*",				/* PHOTOMETRIC_CIELAB */
50 };
51 #define	NPHOTONAMES	(sizeof (photoNames) / sizeof (photoNames[0]))
52 
53 static const char *orientNames[] = {
54     "0 (0x0)",
55     "row 0 top, col 0 lhs",			/* ORIENTATION_TOPLEFT */
56     "row 0 top, col 0 rhs",			/* ORIENTATION_TOPRIGHT */
57     "row 0 bottom, col 0 rhs",			/* ORIENTATION_BOTRIGHT */
58     "row 0 bottom, col 0 lhs",			/* ORIENTATION_BOTLEFT */
59     "row 0 lhs, col 0 top",			/* ORIENTATION_LEFTTOP */
60     "row 0 rhs, col 0 top",			/* ORIENTATION_RIGHTTOP */
61     "row 0 rhs, col 0 bottom",			/* ORIENTATION_RIGHTBOT */
62     "row 0 lhs, col 0 bottom",			/* ORIENTATION_LEFTBOT */
63 };
64 #define	NORIENTNAMES	(sizeof (orientNames) / sizeof (orientNames[0]))
65 
66 static void
_TIFFPrintField(FILE * fd,const TIFFField * fip,uint32 value_count,void * raw_data)67 _TIFFPrintField(FILE* fd, const TIFFField *fip,
68         uint32 value_count, void *raw_data)
69 {
70     uint32 j;
71 
72     fprintf(fd, "  %s: ", fip->field_name);
73 
74     for(j = 0; j < value_count; j++) {
75         if(fip->field_type == TIFF_BYTE)
76             fprintf(fd, "%u", ((uint8 *) raw_data)[j]);
77         else if(fip->field_type == TIFF_UNDEFINED)
78             fprintf(fd, "0x%x",
79                 (unsigned int) ((unsigned char *) raw_data)[j]);
80         else if(fip->field_type == TIFF_SBYTE)
81             fprintf(fd, "%d", ((int8 *) raw_data)[j]);
82         else if(fip->field_type == TIFF_SHORT)
83             fprintf(fd, "%u", ((uint16 *) raw_data)[j]);
84         else if(fip->field_type == TIFF_SSHORT)
85             fprintf(fd, "%d", ((int16 *) raw_data)[j]);
86         else if(fip->field_type == TIFF_LONG)
87             fprintf(fd, "%lu",
88                 (unsigned long)((uint32 *) raw_data)[j]);
89         else if(fip->field_type == TIFF_SLONG)
90             fprintf(fd, "%ld", (long)((int32 *) raw_data)[j]);
91         else if(fip->field_type == TIFF_IFD)
92             fprintf(fd, "0x%lx",
93                 (unsigned long)((uint32 *) raw_data)[j]);
94         else if(fip->field_type == TIFF_RATIONAL
95             || fip->field_type == TIFF_SRATIONAL
96             || fip->field_type == TIFF_FLOAT)
97             fprintf(fd, "%f", ((float *) raw_data)[j]);
98         else if(fip->field_type == TIFF_LONG8)
99 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
100             fprintf(fd, "%I64u",
101                 (unsigned __int64)((uint64 *) raw_data)[j]);
102 #else
103             fprintf(fd, "%llu",
104                 (unsigned long long)((uint64 *) raw_data)[j]);
105 #endif
106         else if(fip->field_type == TIFF_SLONG8)
107 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
108             fprintf(fd, "%I64d", (__int64)((int64 *) raw_data)[j]);
109 #else
110             fprintf(fd, "%lld", (long long)((int64 *) raw_data)[j]);
111 #endif
112         else if(fip->field_type == TIFF_IFD8)
113 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
114             fprintf(fd, "0x%I64x",
115                 (unsigned __int64)((uint64 *) raw_data)[j]);
116 #else
117             fprintf(fd, "0x%llx",
118                 (unsigned long long)((uint64 *) raw_data)[j]);
119 #endif
120         else if(fip->field_type == TIFF_FLOAT)
121             fprintf(fd, "%f", ((float *)raw_data)[j]);
122         else if(fip->field_type == TIFF_DOUBLE)
123             fprintf(fd, "%f", ((double *) raw_data)[j]);
124         else if(fip->field_type == TIFF_ASCII) {
125             fprintf(fd, "%s", (char *) raw_data);
126             break;
127         }
128         else {
129             fprintf(fd, "<unsupported data type in TIFFPrint>");
130             break;
131         }
132 
133         if(j < value_count - 1)
134             fprintf(fd, ",");
135     }
136 
137     fprintf(fd, "\n");
138 }
139 
140 static int
_TIFFPrettyPrintField(TIFF * tif,const TIFFField * fip,FILE * fd,uint32 tag,uint32 value_count,void * raw_data)141 _TIFFPrettyPrintField(TIFF* tif, const TIFFField *fip, FILE* fd, uint32 tag,
142               uint32 value_count, void *raw_data)
143 {
144         (void) tif;
145 
146     /* do not try to pretty print auto-defined fields */
147     if (strncmp(fip->field_name,"Tag ", 4) == 0) {
148         return 0;
149     }
150 
151     switch (tag)
152     {
153         case TIFFTAG_INKSET:
154             if (value_count == 2 && fip->field_type == TIFF_SHORT) {
155                 fprintf(fd, "  Ink Set: ");
156                 switch (*((uint16*)raw_data)) {
157                 case INKSET_CMYK:
158                     fprintf(fd, "CMYK\n");
159                     break;
160                 default:
161                     fprintf(fd, "%u (0x%x)\n",
162                         *((uint16*)raw_data),
163                         *((uint16*)raw_data));
164                     break;
165                 }
166                 return 1;
167             }
168             return 0;
169 
170         case TIFFTAG_DOTRANGE:
171             if (value_count == 2 && fip->field_type == TIFF_SHORT) {
172                 fprintf(fd, "  Dot Range: %u-%u\n",
173                     ((uint16*)raw_data)[0], ((uint16*)raw_data)[1]);
174                 return 1;
175             }
176             return 0;
177 
178         case TIFFTAG_WHITEPOINT:
179             if (value_count == 2 && fip->field_type == TIFF_RATIONAL) {
180                 fprintf(fd, "  White Point: %g-%g\n",
181                     ((float *)raw_data)[0], ((float *)raw_data)[1]);
182                 return 1;
183             }
184             return 0;
185 
186         case TIFFTAG_XMLPACKET:
187         {
188             uint32 i;
189 
190             fprintf(fd, "  XMLPacket (XMP Metadata):\n" );
191             for(i = 0; i < value_count; i++)
192                 fputc(((char *)raw_data)[i], fd);
193             fprintf( fd, "\n" );
194             return 1;
195         }
196         case TIFFTAG_RICHTIFFIPTC:
197             /*
198              * XXX: for some weird reason RichTIFFIPTC tag
199              * defined as array of LONG values.
200              */
201             fprintf(fd,
202                 "  RichTIFFIPTC Data: <present>, %lu bytes\n",
203                 (unsigned long) value_count * 4);
204             return 1;
205 
206         case TIFFTAG_PHOTOSHOP:
207             fprintf(fd, "  Photoshop Data: <present>, %lu bytes\n",
208                 (unsigned long) value_count);
209             return 1;
210 
211         case TIFFTAG_ICCPROFILE:
212             fprintf(fd, "  ICC Profile: <present>, %lu bytes\n",
213                 (unsigned long) value_count);
214             return 1;
215 
216         case TIFFTAG_STONITS:
217             if (value_count == 1 && fip->field_type == TIFF_DOUBLE) {
218                 fprintf(fd,
219                     "  Sample to Nits conversion factor: %.4e\n",
220                     *((double*)raw_data));
221                 return 1;
222             }
223             return 0;
224     }
225 
226     return 0;
227 }
228 
229 /*
230  * Print the contents of the current directory
231  * to the specified stdio file stream.
232  */
233 void
TIFFPrintDirectory(TIFF * tif,FILE * fd,long flags)234 TIFFPrintDirectory(TIFF* tif, FILE* fd, long flags)
235 {
236     TIFFDirectory *td = &tif->tif_dir;
237     char *sep;
238     uint16 i;
239     long l, n;
240 
241 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
242     fprintf(fd, "TIFF Directory at offset 0x%I64x (%I64u)\n",
243         (unsigned __int64) tif->tif_diroff,
244         (unsigned __int64) tif->tif_diroff);
245 #else
246     fprintf(fd, "TIFF Directory at offset 0x%llx (%llu)\n",
247         (unsigned long long) tif->tif_diroff,
248         (unsigned long long) tif->tif_diroff);
249 #endif
250     if (TIFFFieldSet(tif,FIELD_SUBFILETYPE)) {
251         fprintf(fd, "  Subfile Type:");
252         sep = " ";
253         if (td->td_subfiletype & FILETYPE_REDUCEDIMAGE) {
254             fprintf(fd, "%sreduced-resolution image", sep);
255             sep = "/";
256         }
257         if (td->td_subfiletype & FILETYPE_PAGE) {
258             fprintf(fd, "%smulti-page document", sep);
259             sep = "/";
260         }
261         if (td->td_subfiletype & FILETYPE_MASK)
262             fprintf(fd, "%stransparency mask", sep);
263         fprintf(fd, " (%lu = 0x%lx)\n",
264             (long) td->td_subfiletype, (long) td->td_subfiletype);
265     }
266     if (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS)) {
267         fprintf(fd, "  Image Width: %lu Image Length: %lu",
268             (unsigned long) td->td_imagewidth, (unsigned long) td->td_imagelength);
269         if (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))
270             fprintf(fd, " Image Depth: %lu",
271                 (unsigned long) td->td_imagedepth);
272         fprintf(fd, "\n");
273     }
274     if (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS)) {
275         fprintf(fd, "  Tile Width: %lu Tile Length: %lu",
276             (unsigned long) td->td_tilewidth, (unsigned long) td->td_tilelength);
277         if (TIFFFieldSet(tif,FIELD_TILEDEPTH))
278             fprintf(fd, " Tile Depth: %lu",
279                 (unsigned long) td->td_tiledepth);
280         fprintf(fd, "\n");
281     }
282     if (TIFFFieldSet(tif,FIELD_RESOLUTION)) {
283         fprintf(fd, "  Resolution: %g, %g",
284             td->td_xresolution, td->td_yresolution);
285         if (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT)) {
286             switch (td->td_resolutionunit) {
287             case RESUNIT_NONE:
288                 fprintf(fd, " (unitless)");
289                 break;
290             case RESUNIT_INCH:
291                 fprintf(fd, " pixels/inch");
292                 break;
293             case RESUNIT_CENTIMETER:
294                 fprintf(fd, " pixels/cm");
295                 break;
296             default:
297                 fprintf(fd, " (unit %u = 0x%x)",
298                     td->td_resolutionunit,
299                     td->td_resolutionunit);
300                 break;
301             }
302         }
303         fprintf(fd, "\n");
304     }
305     if (TIFFFieldSet(tif,FIELD_POSITION))
306         fprintf(fd, "  Position: %g, %g\n",
307             td->td_xposition, td->td_yposition);
308     if (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
309         fprintf(fd, "  Bits/Sample: %u\n", td->td_bitspersample);
310     if (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT)) {
311         fprintf(fd, "  Sample Format: ");
312         switch (td->td_sampleformat) {
313         case SAMPLEFORMAT_VOID:
314             fprintf(fd, "void\n");
315             break;
316         case SAMPLEFORMAT_INT:
317             fprintf(fd, "signed integer\n");
318             break;
319         case SAMPLEFORMAT_UINT:
320             fprintf(fd, "unsigned integer\n");
321             break;
322         case SAMPLEFORMAT_IEEEFP:
323             fprintf(fd, "IEEE floating point\n");
324             break;
325         case SAMPLEFORMAT_COMPLEXINT:
326             fprintf(fd, "complex signed integer\n");
327             break;
328         case SAMPLEFORMAT_COMPLEXIEEEFP:
329             fprintf(fd, "complex IEEE floating point\n");
330             break;
331         default:
332             fprintf(fd, "%u (0x%x)\n",
333                 td->td_sampleformat, td->td_sampleformat);
334             break;
335         }
336     }
337     if (TIFFFieldSet(tif,FIELD_COMPRESSION)) {
338         const TIFFCodec* c = TIFFFindCODEC(td->td_compression);
339         fprintf(fd, "  Compression Scheme: ");
340         if (c)
341             fprintf(fd, "%s\n", c->name);
342         else
343             fprintf(fd, "%u (0x%x)\n",
344                 td->td_compression, td->td_compression);
345     }
346     if (TIFFFieldSet(tif,FIELD_PHOTOMETRIC)) {
347         fprintf(fd, "  Photometric Interpretation: ");
348         if (td->td_photometric < NPHOTONAMES)
349             fprintf(fd, "%s\n", photoNames[td->td_photometric]);
350         else {
351             switch (td->td_photometric) {
352             case PHOTOMETRIC_LOGL:
353                 fprintf(fd, "CIE Log2(L)\n");
354                 break;
355             case PHOTOMETRIC_LOGLUV:
356                 fprintf(fd, "CIE Log2(L) (u',v')\n");
357                 break;
358             default:
359                 fprintf(fd, "%u (0x%x)\n",
360                     td->td_photometric, td->td_photometric);
361                 break;
362             }
363         }
364     }
365     if (TIFFFieldSet(tif,FIELD_EXTRASAMPLES) && td->td_extrasamples) {
366         fprintf(fd, "  Extra Samples: %u<", td->td_extrasamples);
367         sep = "";
368         for (i = 0; i < td->td_extrasamples; i++) {
369             switch (td->td_sampleinfo[i]) {
370             case EXTRASAMPLE_UNSPECIFIED:
371                 fprintf(fd, "%sunspecified", sep);
372                 break;
373             case EXTRASAMPLE_ASSOCALPHA:
374                 fprintf(fd, "%sassoc-alpha", sep);
375                 break;
376             case EXTRASAMPLE_UNASSALPHA:
377                 fprintf(fd, "%sunassoc-alpha", sep);
378                 break;
379             default:
380                 fprintf(fd, "%s%u (0x%x)", sep,
381                     td->td_sampleinfo[i], td->td_sampleinfo[i]);
382                 break;
383             }
384             sep = ", ";
385         }
386         fprintf(fd, ">\n");
387     }
388     if (TIFFFieldSet(tif,FIELD_INKNAMES)) {
389         char* cp;
390         fprintf(fd, "  Ink Names: ");
391         i = td->td_samplesperpixel;
392         sep = "";
393         for (cp = td->td_inknames;
394              i > 0 && cp < td->td_inknames + td->td_inknameslen;
395              cp = strchr(cp,'\0')+1, i--) {
396             int max_chars =
397                 td->td_inknameslen - (cp - td->td_inknames);
398             fputs(sep, fd);
399             _TIFFprintAsciiBounded(fd, cp, max_chars);
400             sep = ", ";
401         }
402                 fputs("\n", fd);
403     }
404     if (TIFFFieldSet(tif,FIELD_THRESHHOLDING)) {
405         fprintf(fd, "  Thresholding: ");
406         switch (td->td_threshholding) {
407         case THRESHHOLD_BILEVEL:
408             fprintf(fd, "bilevel art scan\n");
409             break;
410         case THRESHHOLD_HALFTONE:
411             fprintf(fd, "halftone or dithered scan\n");
412             break;
413         case THRESHHOLD_ERRORDIFFUSE:
414             fprintf(fd, "error diffused\n");
415             break;
416         default:
417             fprintf(fd, "%u (0x%x)\n",
418                 td->td_threshholding, td->td_threshholding);
419             break;
420         }
421     }
422     if (TIFFFieldSet(tif,FIELD_FILLORDER)) {
423         fprintf(fd, "  FillOrder: ");
424         switch (td->td_fillorder) {
425         case FILLORDER_MSB2LSB:
426             fprintf(fd, "msb-to-lsb\n");
427             break;
428         case FILLORDER_LSB2MSB:
429             fprintf(fd, "lsb-to-msb\n");
430             break;
431         default:
432             fprintf(fd, "%u (0x%x)\n",
433                 td->td_fillorder, td->td_fillorder);
434             break;
435         }
436     }
437     if (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))
438         {
439         fprintf(fd, "  YCbCr Subsampling: %u, %u\n",
440             td->td_ycbcrsubsampling[0], td->td_ycbcrsubsampling[1] );
441     }
442     if (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING)) {
443         fprintf(fd, "  YCbCr Positioning: ");
444         switch (td->td_ycbcrpositioning) {
445         case YCBCRPOSITION_CENTERED:
446             fprintf(fd, "centered\n");
447             break;
448         case YCBCRPOSITION_COSITED:
449             fprintf(fd, "cosited\n");
450             break;
451         default:
452             fprintf(fd, "%u (0x%x)\n",
453                 td->td_ycbcrpositioning, td->td_ycbcrpositioning);
454             break;
455         }
456     }
457     if (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))
458         fprintf(fd, "  Halftone Hints: light %u dark %u\n",
459             td->td_halftonehints[0], td->td_halftonehints[1]);
460     if (TIFFFieldSet(tif,FIELD_ORIENTATION)) {
461         fprintf(fd, "  Orientation: ");
462         if (td->td_orientation < NORIENTNAMES)
463             fprintf(fd, "%s\n", orientNames[td->td_orientation]);
464         else
465             fprintf(fd, "%u (0x%x)\n",
466                 td->td_orientation, td->td_orientation);
467     }
468     if (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
469         fprintf(fd, "  Samples/Pixel: %u\n", td->td_samplesperpixel);
470     if (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP)) {
471         fprintf(fd, "  Rows/Strip: ");
472         if (td->td_rowsperstrip == (uint32) -1)
473             fprintf(fd, "(infinite)\n");
474         else
475             fprintf(fd, "%lu\n", (unsigned long) td->td_rowsperstrip);
476     }
477     if (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))
478         fprintf(fd, "  Min Sample Value: %u\n", td->td_minsamplevalue);
479     if (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))
480         fprintf(fd, "  Max Sample Value: %u\n", td->td_maxsamplevalue);
481     if (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE)) {
482         int count = (tif->tif_flags & TIFF_PERSAMPLE) ? td->td_samplesperpixel : 1;
483         fprintf(fd, "  SMin Sample Value:");
484         for (i = 0; i < count; ++i)
485             fprintf(fd, " %g", td->td_sminsamplevalue[i]);
486         fprintf(fd, "\n");
487     }
488     if (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE)) {
489         int count = (tif->tif_flags & TIFF_PERSAMPLE) ? td->td_samplesperpixel : 1;
490         fprintf(fd, "  SMax Sample Value:");
491         for (i = 0; i < count; ++i)
492             fprintf(fd, " %g", td->td_smaxsamplevalue[i]);
493         fprintf(fd, "\n");
494     }
495     if (TIFFFieldSet(tif,FIELD_PLANARCONFIG)) {
496         fprintf(fd, "  Planar Configuration: ");
497         switch (td->td_planarconfig) {
498         case PLANARCONFIG_CONTIG:
499             fprintf(fd, "single image plane\n");
500             break;
501         case PLANARCONFIG_SEPARATE:
502             fprintf(fd, "separate image planes\n");
503             break;
504         default:
505             fprintf(fd, "%u (0x%x)\n",
506                 td->td_planarconfig, td->td_planarconfig);
507             break;
508         }
509     }
510     if (TIFFFieldSet(tif,FIELD_PAGENUMBER))
511         fprintf(fd, "  Page Number: %u-%u\n",
512             td->td_pagenumber[0], td->td_pagenumber[1]);
513     if (TIFFFieldSet(tif,FIELD_COLORMAP)) {
514         fprintf(fd, "  Color Map: ");
515         if (flags & TIFFPRINT_COLORMAP) {
516             fprintf(fd, "\n");
517             n = 1L<<td->td_bitspersample;
518             for (l = 0; l < n; l++)
519                 fprintf(fd, "   %5lu: %5u %5u %5u\n",
520                     l,
521                     td->td_colormap[0][l],
522                     td->td_colormap[1][l],
523                     td->td_colormap[2][l]);
524         } else
525             fprintf(fd, "(present)\n");
526     }
527     if (TIFFFieldSet(tif,FIELD_REFBLACKWHITE)) {
528         fprintf(fd, "  Reference Black/White:\n");
529         for (i = 0; i < 3; i++)
530         fprintf(fd, "    %2d: %5g %5g\n", i,
531             td->td_refblackwhite[2*i+0],
532             td->td_refblackwhite[2*i+1]);
533     }
534     if (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION)) {
535         fprintf(fd, "  Transfer Function: ");
536         if (flags & TIFFPRINT_CURVES) {
537             fprintf(fd, "\n");
538             n = 1L<<td->td_bitspersample;
539             for (l = 0; l < n; l++) {
540                 fprintf(fd, "    %2lu: %5u",
541                     l, td->td_transferfunction[0][l]);
542                 for (i = 1; i < td->td_samplesperpixel; i++)
543                     fprintf(fd, " %5u",
544                         td->td_transferfunction[i][l]);
545                 fputc('\n', fd);
546             }
547         } else
548             fprintf(fd, "(present)\n");
549     }
550     if (TIFFFieldSet(tif, FIELD_SUBIFD) && (td->td_subifd)) {
551         fprintf(fd, "  SubIFD Offsets:");
552         for (i = 0; i < td->td_nsubifd; i++)
553 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
554             fprintf(fd, " %5I64u",
555                 (unsigned __int64) td->td_subifd[i]);
556 #else
557             fprintf(fd, " %5llu",
558                 (unsigned long long) td->td_subifd[i]);
559 #endif
560         fputc('\n', fd);
561     }
562 
563     /*
564     ** Custom tag support.
565     */
566     {
567         int  i;
568         short count;
569 
570         count = (short) TIFFGetTagListCount(tif);
571         for(i = 0; i < count; i++) {
572             uint32 tag = TIFFGetTagListEntry(tif, i);
573             const TIFFField *fip;
574             uint32 value_count;
575             int mem_alloc = 0;
576             void *raw_data;
577 
578             fip = TIFFFieldWithTag(tif, tag);
579             if(fip == NULL)
580                 continue;
581 
582             if(fip->field_passcount) {
583                 if (fip->field_readcount == TIFF_VARIABLE2 ) {
584                     if(TIFFGetField(tif, tag, &value_count, &raw_data) != 1)
585                         continue;
586                 } else if (fip->field_readcount == TIFF_VARIABLE2 ) {
587                     uint16 small_value_count;
588                     if(TIFFGetField(tif, tag, &small_value_count, &raw_data) != 1)
589                         continue;
590                     value_count = small_value_count;
591                 } else {
592                     assert (fip->field_readcount == TIFF_VARIABLE
593                         || fip->field_readcount == TIFF_VARIABLE2);
594                     continue;
595                 }
596             } else {
597                 if (fip->field_readcount == TIFF_VARIABLE
598                     || fip->field_readcount == TIFF_VARIABLE2)
599                     value_count = 1;
600                 else if (fip->field_readcount == TIFF_SPP)
601                     value_count = td->td_samplesperpixel;
602                 else
603                     value_count = fip->field_readcount;
604                 if (fip->field_tag == TIFFTAG_DOTRANGE
605                     && strcmp(fip->field_name,"DotRange") == 0) {
606                     /* TODO: This is an evil exception and should not have been
607                        handled this way ... likely best if we move it into
608                        the directory structure with an explicit field in
609                        libtiff 4.1 and assign it a FIELD_ value */
610                     static uint16 dotrange[2];
611                     raw_data = dotrange;
612                     TIFFGetField(tif, tag, dotrange+0, dotrange+1);
613                 } else if (fip->field_type == TIFF_ASCII
614                        || fip->field_readcount == TIFF_VARIABLE
615                        || fip->field_readcount == TIFF_VARIABLE2
616                        || fip->field_readcount == TIFF_SPP
617                        || value_count > 1) {
618                     if(TIFFGetField(tif, tag, &raw_data) != 1)
619                         continue;
620                 } else {
621                     raw_data = _TIFFmalloc(
622                         _TIFFDataSize(fip->field_type)
623                         * value_count);
624                     mem_alloc = 1;
625                     if(TIFFGetField(tif, tag, raw_data) != 1) {
626                         _TIFFfree(raw_data);
627                         continue;
628                     }
629                 }
630             }
631 
632             /*
633              * Catch the tags which needs to be specially handled
634              * and pretty print them. If tag not handled in
635              * _TIFFPrettyPrintField() fall down and print it as
636              * any other tag.
637              */
638             if (!_TIFFPrettyPrintField(tif, fip, fd, tag, value_count, raw_data))
639                 _TIFFPrintField(fd, fip, value_count, raw_data);
640 
641             if(mem_alloc)
642                 _TIFFfree(raw_data);
643         }
644     }
645 
646     if (tif->tif_tagmethods.printdir)
647         (*tif->tif_tagmethods.printdir)(tif, fd, flags);
648 
649         _TIFFFillStriles( tif );
650 
651     if ((flags & TIFFPRINT_STRIPS) &&
652         TIFFFieldSet(tif,FIELD_STRIPOFFSETS)) {
653         uint32 s;
654 
655         fprintf(fd, "  %lu %s:\n",
656             (long) td->td_nstrips,
657             isTiled(tif) ? "Tiles" : "Strips");
658         for (s = 0; s < td->td_nstrips; s++)
659 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
660             fprintf(fd, "    %3lu: [%8I64u, %8I64u]\n",
661                 (unsigned long) s,
662                 (unsigned __int64) td->td_stripoffset[s],
663                 (unsigned __int64) td->td_stripbytecount[s]);
664 #else
665             fprintf(fd, "    %3lu: [%8llu, %8llu]\n",
666                 (unsigned long) s,
667                 (unsigned long long) td->td_stripoffset[s],
668                 (unsigned long long) td->td_stripbytecount[s]);
669 #endif
670     }
671 }
672 
673 void
_TIFFprintAscii(FILE * fd,const char * cp)674 _TIFFprintAscii(FILE* fd, const char* cp)
675 {
676     _TIFFprintAsciiBounded( fd, cp, strlen(cp));
677 }
678 
679 static void
_TIFFprintAsciiBounded(FILE * fd,const char * cp,int max_chars)680 _TIFFprintAsciiBounded(FILE* fd, const char* cp, int max_chars)
681 {
682     for (; max_chars > 0 && *cp != '\0'; cp++, max_chars--) {
683         const char* tp;
684 
685         if (isprint((int)*cp)) {
686             fputc(*cp, fd);
687             continue;
688         }
689         for (tp = "\tt\bb\rr\nn\vv"; *tp; tp++)
690             if (*tp++ == *cp)
691                 break;
692         if (*tp)
693             fprintf(fd, "\\%c", *tp);
694         else
695             fprintf(fd, "\\%03o", *cp & 0xff);
696     }
697 }
698 
699 void
_TIFFprintAsciiTag(FILE * fd,const char * name,const char * value)700 _TIFFprintAsciiTag(FILE* fd, const char* name, const char* value)
701 {
702     fprintf(fd, "  %s: \"", name);
703     _TIFFprintAscii(fd, value);
704     fprintf(fd, "\"\n");
705 }
706 
707 /* vim: set ts=8 sts=8 sw=8 noet: */
708 /*
709  * Local Variables:
710  * mode: c
711  * c-basic-offset: 8
712  * fill-column: 78
713  * End:
714  */
715