tif_print.c 26 KB

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