2
0

tiffdump.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /* $Id: tiffdump.c,v 1.26 2012-06-15 21:51:54 fwarmerdam Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. #include "tif_config.h"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #ifdef HAVE_UNISTD_H
  30. # include <unistd.h>
  31. #endif
  32. #ifdef HAVE_FCNTL_H
  33. # include <fcntl.h>
  34. #endif
  35. #ifdef HAVE_SYS_TYPES_H
  36. # include <sys/types.h>
  37. #endif
  38. #ifdef HAVE_IO_H
  39. # include <io.h>
  40. #endif
  41. #ifdef NEED_LIBPORT
  42. # include "libport.h"
  43. #endif
  44. #ifndef HAVE_GETOPT
  45. extern int getopt(int, char**, char*);
  46. #endif
  47. #include "tiffio.h"
  48. #ifndef O_BINARY
  49. # define O_BINARY 0
  50. #endif
  51. static union
  52. {
  53. TIFFHeaderClassic classic;
  54. TIFFHeaderBig big;
  55. TIFFHeaderCommon common;
  56. } hdr;
  57. char* appname;
  58. char* curfile;
  59. int swabflag;
  60. int bigendian;
  61. int bigtiff;
  62. uint32 maxitems = 24; /* maximum indirect data items to print */
  63. const char* bytefmt = "%s%#02x"; /* BYTE */
  64. const char* sbytefmt = "%s%d"; /* SBYTE */
  65. const char* shortfmt = "%s%u"; /* SHORT */
  66. const char* sshortfmt = "%s%d"; /* SSHORT */
  67. const char* longfmt = "%s%lu"; /* LONG */
  68. const char* slongfmt = "%s%ld"; /* SLONG */
  69. const char* ifdfmt = "%s%#04lx"; /* IFD offset */
  70. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  71. const char* long8fmt = "%s%I64u"; /* LONG8 */
  72. const char* slong8fmt = "%s%I64d"; /* SLONG8 */
  73. const char* ifd8fmt = "%s%#08I64x"; /* IFD offset8*/
  74. #else
  75. const char* long8fmt = "%s%llu"; /* LONG8 */
  76. const char* slong8fmt = "%s%lld"; /* SLONG8 */
  77. const char* ifd8fmt = "%s%#08llx"; /* IFD offset8*/
  78. #endif
  79. const char* rationalfmt = "%s%g"; /* RATIONAL */
  80. const char* srationalfmt = "%s%g"; /* SRATIONAL */
  81. const char* floatfmt = "%s%g"; /* FLOAT */
  82. const char* doublefmt = "%s%g"; /* DOUBLE */
  83. static void dump(int, uint64);
  84. extern int optind;
  85. extern char* optarg;
  86. void
  87. usage()
  88. {
  89. fprintf(stderr, "usage: %s [-h] [-o offset] [-m maxitems] file.tif ...\n", appname);
  90. exit(-1);
  91. }
  92. int
  93. main(int argc, char* argv[])
  94. {
  95. int one = 1, fd;
  96. int multiplefiles = (argc > 1);
  97. int c;
  98. uint64 diroff = 0;
  99. bigendian = (*(char *)&one == 0);
  100. appname = argv[0];
  101. while ((c = getopt(argc, argv, "m:o:h")) != -1) {
  102. switch (c) {
  103. case 'h': /* print values in hex */
  104. shortfmt = "%s%#x";
  105. sshortfmt = "%s%#x";
  106. longfmt = "%s%#lx";
  107. slongfmt = "%s%#lx";
  108. break;
  109. case 'o':
  110. diroff = (uint64) strtoul(optarg, NULL, 0);
  111. break;
  112. case 'm':
  113. maxitems = strtoul(optarg, NULL, 0);
  114. break;
  115. default:
  116. usage();
  117. }
  118. }
  119. if (optind >= argc)
  120. usage();
  121. for (; optind < argc; optind++) {
  122. fd = open(argv[optind], O_RDONLY|O_BINARY, 0);
  123. if (fd < 0) {
  124. perror(argv[0]);
  125. return (-1);
  126. }
  127. if (multiplefiles)
  128. printf("%s:\n", argv[optind]);
  129. curfile = argv[optind];
  130. swabflag = 0;
  131. bigtiff = 0;
  132. dump(fd, diroff);
  133. close(fd);
  134. }
  135. return (0);
  136. }
  137. #define ord(e) ((int)e)
  138. static uint64 ReadDirectory(int, unsigned, uint64);
  139. static void ReadError(char*);
  140. static void Error(const char*, ...);
  141. static void Fatal(const char*, ...);
  142. static void
  143. dump(int fd, uint64 diroff)
  144. {
  145. unsigned i;
  146. lseek(fd, (off_t) 0, 0);
  147. if (read(fd, (char*) &hdr, sizeof (TIFFHeaderCommon)) != sizeof (TIFFHeaderCommon))
  148. ReadError("TIFF header");
  149. if (hdr.common.tiff_magic != TIFF_BIGENDIAN
  150. && hdr.common.tiff_magic != TIFF_LITTLEENDIAN &&
  151. #if HOST_BIGENDIAN
  152. /* MDI is sensitive to the host byte order, unlike TIFF */
  153. MDI_BIGENDIAN != hdr.common.tiff_magic
  154. #else
  155. MDI_LITTLEENDIAN != hdr.common.tiff_magic
  156. #endif
  157. ) {
  158. Fatal("Not a TIFF or MDI file, bad magic number %u (%#x)",
  159. hdr.common.tiff_magic, hdr.common.tiff_magic);
  160. }
  161. if (hdr.common.tiff_magic == TIFF_BIGENDIAN
  162. || hdr.common.tiff_magic == MDI_BIGENDIAN)
  163. swabflag = !bigendian;
  164. else
  165. swabflag = bigendian;
  166. if (swabflag)
  167. TIFFSwabShort(&hdr.common.tiff_version);
  168. if (hdr.common.tiff_version==42)
  169. {
  170. if (read(fd, (char*) &hdr.classic.tiff_diroff, 4) != 4)
  171. ReadError("TIFF header");
  172. if (swabflag)
  173. TIFFSwabLong(&hdr.classic.tiff_diroff);
  174. printf("Magic: %#x <%s-endian> Version: %#x <%s>\n",
  175. hdr.classic.tiff_magic,
  176. hdr.classic.tiff_magic == TIFF_BIGENDIAN ? "big" : "little",
  177. 42,"ClassicTIFF");
  178. if (diroff == 0)
  179. diroff = hdr.classic.tiff_diroff;
  180. }
  181. else if (hdr.common.tiff_version==43)
  182. {
  183. if (read(fd, (char*) &hdr.big.tiff_offsetsize, 12) != 12)
  184. ReadError("TIFF header");
  185. if (swabflag)
  186. {
  187. TIFFSwabShort(&hdr.big.tiff_offsetsize);
  188. TIFFSwabShort(&hdr.big.tiff_unused);
  189. TIFFSwabLong8(&hdr.big.tiff_diroff);
  190. }
  191. printf("Magic: %#x <%s-endian> Version: %#x <%s>\n",
  192. hdr.big.tiff_magic,
  193. hdr.big.tiff_magic == TIFF_BIGENDIAN ? "big" : "little",
  194. 43,"BigTIFF");
  195. printf("OffsetSize: %#x Unused: %#x\n",
  196. hdr.big.tiff_offsetsize,hdr.big.tiff_unused);
  197. if (diroff == 0)
  198. diroff = hdr.big.tiff_diroff;
  199. bigtiff = 1;
  200. }
  201. else
  202. Fatal("Not a TIFF file, bad version number %u (%#x)",
  203. hdr.common.tiff_version, hdr.common.tiff_version);
  204. for (i = 0; diroff != 0; i++) {
  205. if (i > 0)
  206. putchar('\n');
  207. diroff = ReadDirectory(fd, i, diroff);
  208. }
  209. }
  210. static const int datawidth[] = {
  211. 0, /* 00 = undefined */
  212. 1, /* 01 = TIFF_BYTE */
  213. 1, /* 02 = TIFF_ASCII */
  214. 2, /* 03 = TIFF_SHORT */
  215. 4, /* 04 = TIFF_LONG */
  216. 8, /* 05 = TIFF_RATIONAL */
  217. 1, /* 06 = TIFF_SBYTE */
  218. 1, /* 07 = TIFF_UNDEFINED */
  219. 2, /* 08 = TIFF_SSHORT */
  220. 4, /* 09 = TIFF_SLONG */
  221. 8, /* 10 = TIFF_SRATIONAL */
  222. 4, /* 11 = TIFF_FLOAT */
  223. 8, /* 12 = TIFF_DOUBLE */
  224. 4, /* 13 = TIFF_IFD */
  225. 0, /* 14 = undefined */
  226. 0, /* 15 = undefined */
  227. 8, /* 16 = TIFF_LONG8 */
  228. 8, /* 17 = TIFF_SLONG8 */
  229. 8, /* 18 = TIFF_IFD8 */
  230. };
  231. #define NWIDTHS (sizeof (datawidth) / sizeof (datawidth[0]))
  232. static void PrintTag(FILE*, uint16);
  233. static void PrintType(FILE*, uint16);
  234. static void PrintData(FILE*, uint16, uint32, unsigned char*);
  235. /*
  236. * Read the next TIFF directory from a file
  237. * and convert it to the internal format.
  238. * We read directories sequentially.
  239. */
  240. static uint64
  241. ReadDirectory(int fd, unsigned int ix, uint64 off)
  242. {
  243. uint16 dircount;
  244. uint32 direntrysize;
  245. void* dirmem = NULL;
  246. uint64 nextdiroff = 0;
  247. uint32 n;
  248. uint8* dp;
  249. if (off == 0) /* no more directories */
  250. goto done;
  251. #if defined(__WIN32__) && defined(_MSC_VER)
  252. if (_lseeki64(fd, (__int64)off, SEEK_SET) != (__int64)off) {
  253. #else
  254. if (lseek(fd, (off_t)off, SEEK_SET) != (off_t)off) {
  255. #endif
  256. Fatal("Seek error accessing TIFF directory");
  257. goto done;
  258. }
  259. if (!bigtiff) {
  260. if (read(fd, (char*) &dircount, sizeof (uint16)) != sizeof (uint16)) {
  261. ReadError("directory count");
  262. goto done;
  263. }
  264. if (swabflag)
  265. TIFFSwabShort(&dircount);
  266. direntrysize = 12;
  267. } else {
  268. uint64 dircount64 = 0;
  269. if (read(fd, (char*) &dircount64, sizeof (uint64)) != sizeof (uint64)) {
  270. ReadError("directory count");
  271. goto done;
  272. }
  273. if (swabflag)
  274. TIFFSwabLong8(&dircount64);
  275. if (dircount64>0xFFFF) {
  276. Error("Sanity check on directory count failed");
  277. goto done;
  278. }
  279. dircount = (uint16)dircount64;
  280. direntrysize = 20;
  281. }
  282. dirmem = _TIFFmalloc(dircount * direntrysize);
  283. if (dirmem == NULL) {
  284. Fatal("No space for TIFF directory");
  285. goto done;
  286. }
  287. n = read(fd, (char*) dirmem, dircount*direntrysize);
  288. if (n != dircount*direntrysize) {
  289. n /= direntrysize;
  290. Error(
  291. #if defined(__WIN32__) && defined(_MSC_VER)
  292. "Could only read %lu of %u entries in directory at offset %#I64x",
  293. (unsigned long)n, dircount, (unsigned __int64) off);
  294. #else
  295. "Could only read %lu of %u entries in directory at offset %#llx",
  296. (unsigned long)n, dircount, (unsigned long long) off);
  297. #endif
  298. dircount = n;
  299. nextdiroff = 0;
  300. } else {
  301. if (!bigtiff) {
  302. uint32 nextdiroff32;
  303. if (read(fd, (char*) &nextdiroff32, sizeof (uint32)) != sizeof (uint32))
  304. nextdiroff32 = 0;
  305. if (swabflag)
  306. TIFFSwabLong(&nextdiroff32);
  307. nextdiroff = nextdiroff32;
  308. } else {
  309. if (read(fd, (char*) &nextdiroff, sizeof (uint64)) != sizeof (uint64))
  310. nextdiroff = 0;
  311. if (swabflag)
  312. TIFFSwabLong8(&nextdiroff);
  313. }
  314. }
  315. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  316. printf("Directory %u: offset %I64u (%#I64x) next %I64u (%#I64x)\n", ix,
  317. (unsigned __int64)off, (unsigned __int64)off,
  318. (unsigned __int64)nextdiroff, (unsigned __int64)nextdiroff);
  319. #else
  320. printf("Directory %u: offset %llu (%#llx) next %llu (%#llx)\n", ix,
  321. (unsigned long long)off, (unsigned long long)off,
  322. (unsigned long long)nextdiroff, (unsigned long long)nextdiroff);
  323. #endif
  324. for (dp = (uint8*)dirmem, n = dircount; n > 0; n--) {
  325. uint16 tag;
  326. uint16 type;
  327. uint16 typewidth;
  328. uint64 count;
  329. uint64 datasize;
  330. int datafits;
  331. void* datamem;
  332. uint64 dataoffset;
  333. int datatruncated;
  334. tag = *(uint16*)dp;
  335. if (swabflag)
  336. TIFFSwabShort(&tag);
  337. dp += sizeof(uint16);
  338. type = *(uint16*)dp;
  339. dp += sizeof(uint16);
  340. if (swabflag)
  341. TIFFSwabShort(&type);
  342. PrintTag(stdout, tag);
  343. putchar(' ');
  344. PrintType(stdout, type);
  345. putchar(' ');
  346. if (!bigtiff)
  347. {
  348. uint32 count32;
  349. count32 = *(uint32*)dp;
  350. if (swabflag)
  351. TIFFSwabLong(&count32);
  352. dp += sizeof(uint32);
  353. count = count32;
  354. }
  355. else
  356. {
  357. count = *(uint64*)dp;
  358. if (swabflag)
  359. TIFFSwabLong8(&count);
  360. dp += sizeof(uint64);
  361. }
  362. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  363. printf("%I64u<", (unsigned __int64)count);
  364. #else
  365. printf("%llu<", (unsigned long long)count);
  366. #endif
  367. if (type >= NWIDTHS)
  368. typewidth = 0;
  369. else
  370. typewidth = datawidth[type];
  371. datasize = count*typewidth;
  372. datafits = 1;
  373. datamem = dp;
  374. dataoffset = 0;
  375. datatruncated = 0;
  376. if (!bigtiff)
  377. {
  378. if (datasize>4)
  379. {
  380. uint32 dataoffset32;
  381. datafits = 0;
  382. datamem = NULL;
  383. dataoffset32 = *(uint32*)dp;
  384. if (swabflag)
  385. TIFFSwabLong(&dataoffset32);
  386. dataoffset = dataoffset32;
  387. }
  388. dp += sizeof(uint32);
  389. }
  390. else
  391. {
  392. if (datasize>8)
  393. {
  394. datafits = 0;
  395. datamem = NULL;
  396. dataoffset = *(uint64*)dp;
  397. if (swabflag)
  398. TIFFSwabLong8(&dataoffset);
  399. }
  400. dp += sizeof(uint64);
  401. }
  402. if (datasize>0x10000)
  403. {
  404. datatruncated = 1;
  405. count = 0x10000/typewidth;
  406. datasize = count*typewidth;
  407. }
  408. if (count>maxitems)
  409. {
  410. datatruncated = 1;
  411. count = maxitems;
  412. datasize = count*typewidth;
  413. }
  414. if (!datafits)
  415. {
  416. datamem = _TIFFmalloc((uint32)datasize);
  417. if (datamem) {
  418. #if defined(__WIN32__) && defined(_MSC_VER)
  419. if (_lseeki64(fd, (__int64)dataoffset, SEEK_SET)
  420. != (__int64)dataoffset)
  421. #else
  422. if (lseek(fd, (off_t)dataoffset, 0) !=
  423. (off_t)dataoffset)
  424. #endif
  425. {
  426. Error(
  427. "Seek error accessing tag %u value", tag);
  428. _TIFFfree(datamem);
  429. datamem = NULL;
  430. }
  431. if (read(fd, datamem, (size_t)datasize) != (TIFF_SSIZE_T)datasize)
  432. {
  433. Error(
  434. "Read error accessing tag %u value", tag);
  435. _TIFFfree(datamem);
  436. datamem = NULL;
  437. }
  438. } else
  439. Error("No space for data for tag %u",tag);
  440. }
  441. if (datamem)
  442. {
  443. if (swabflag)
  444. {
  445. switch (type)
  446. {
  447. case TIFF_BYTE:
  448. case TIFF_ASCII:
  449. case TIFF_SBYTE:
  450. case TIFF_UNDEFINED:
  451. break;
  452. case TIFF_SHORT:
  453. case TIFF_SSHORT:
  454. TIFFSwabArrayOfShort((uint16*)datamem,(tmsize_t)count);
  455. break;
  456. case TIFF_LONG:
  457. case TIFF_SLONG:
  458. case TIFF_FLOAT:
  459. case TIFF_IFD:
  460. TIFFSwabArrayOfLong((uint32*)datamem,(tmsize_t)count);
  461. break;
  462. case TIFF_RATIONAL:
  463. case TIFF_SRATIONAL:
  464. TIFFSwabArrayOfLong((uint32*)datamem,(tmsize_t)count*2);
  465. break;
  466. case TIFF_DOUBLE:
  467. case TIFF_LONG8:
  468. case TIFF_SLONG8:
  469. case TIFF_IFD8:
  470. TIFFSwabArrayOfLong8((uint64*)datamem,(tmsize_t)count);
  471. break;
  472. }
  473. }
  474. PrintData(stdout,type,(uint32)count,datamem);
  475. if (datatruncated)
  476. printf(" ...");
  477. if (!datafits)
  478. _TIFFfree(datamem);
  479. }
  480. printf(">\n");
  481. }
  482. done:
  483. if (dirmem)
  484. _TIFFfree((char *)dirmem);
  485. return (nextdiroff);
  486. }
  487. static const struct tagname {
  488. uint16 tag;
  489. const char* name;
  490. } tagnames[] = {
  491. { TIFFTAG_SUBFILETYPE, "SubFileType" },
  492. { TIFFTAG_OSUBFILETYPE, "OldSubFileType" },
  493. { TIFFTAG_IMAGEWIDTH, "ImageWidth" },
  494. { TIFFTAG_IMAGELENGTH, "ImageLength" },
  495. { TIFFTAG_BITSPERSAMPLE, "BitsPerSample" },
  496. { TIFFTAG_COMPRESSION, "Compression" },
  497. { TIFFTAG_PHOTOMETRIC, "Photometric" },
  498. { TIFFTAG_THRESHHOLDING, "Threshholding" },
  499. { TIFFTAG_CELLWIDTH, "CellWidth" },
  500. { TIFFTAG_CELLLENGTH, "CellLength" },
  501. { TIFFTAG_FILLORDER, "FillOrder" },
  502. { TIFFTAG_DOCUMENTNAME, "DocumentName" },
  503. { TIFFTAG_IMAGEDESCRIPTION, "ImageDescription" },
  504. { TIFFTAG_MAKE, "Make" },
  505. { TIFFTAG_MODEL, "Model" },
  506. { TIFFTAG_STRIPOFFSETS, "StripOffsets" },
  507. { TIFFTAG_ORIENTATION, "Orientation" },
  508. { TIFFTAG_SAMPLESPERPIXEL, "SamplesPerPixel" },
  509. { TIFFTAG_ROWSPERSTRIP, "RowsPerStrip" },
  510. { TIFFTAG_STRIPBYTECOUNTS, "StripByteCounts" },
  511. { TIFFTAG_MINSAMPLEVALUE, "MinSampleValue" },
  512. { TIFFTAG_MAXSAMPLEVALUE, "MaxSampleValue" },
  513. { TIFFTAG_XRESOLUTION, "XResolution" },
  514. { TIFFTAG_YRESOLUTION, "YResolution" },
  515. { TIFFTAG_PLANARCONFIG, "PlanarConfig" },
  516. { TIFFTAG_PAGENAME, "PageName" },
  517. { TIFFTAG_XPOSITION, "XPosition" },
  518. { TIFFTAG_YPOSITION, "YPosition" },
  519. { TIFFTAG_FREEOFFSETS, "FreeOffsets" },
  520. { TIFFTAG_FREEBYTECOUNTS, "FreeByteCounts" },
  521. { TIFFTAG_GRAYRESPONSEUNIT, "GrayResponseUnit" },
  522. { TIFFTAG_GRAYRESPONSECURVE,"GrayResponseCurve" },
  523. { TIFFTAG_GROUP3OPTIONS, "Group3Options" },
  524. { TIFFTAG_GROUP4OPTIONS, "Group4Options" },
  525. { TIFFTAG_RESOLUTIONUNIT, "ResolutionUnit" },
  526. { TIFFTAG_PAGENUMBER, "PageNumber" },
  527. { TIFFTAG_COLORRESPONSEUNIT,"ColorResponseUnit" },
  528. { TIFFTAG_TRANSFERFUNCTION, "TransferFunction" },
  529. { TIFFTAG_SOFTWARE, "Software" },
  530. { TIFFTAG_DATETIME, "DateTime" },
  531. { TIFFTAG_ARTIST, "Artist" },
  532. { TIFFTAG_HOSTCOMPUTER, "HostComputer" },
  533. { TIFFTAG_PREDICTOR, "Predictor" },
  534. { TIFFTAG_WHITEPOINT, "Whitepoint" },
  535. { TIFFTAG_PRIMARYCHROMATICITIES,"PrimaryChromaticities" },
  536. { TIFFTAG_COLORMAP, "Colormap" },
  537. { TIFFTAG_HALFTONEHINTS, "HalftoneHints" },
  538. { TIFFTAG_TILEWIDTH, "TileWidth" },
  539. { TIFFTAG_TILELENGTH, "TileLength" },
  540. { TIFFTAG_TILEOFFSETS, "TileOffsets" },
  541. { TIFFTAG_TILEBYTECOUNTS, "TileByteCounts" },
  542. { TIFFTAG_BADFAXLINES, "BadFaxLines" },
  543. { TIFFTAG_CLEANFAXDATA, "CleanFaxData" },
  544. { TIFFTAG_CONSECUTIVEBADFAXLINES, "ConsecutiveBadFaxLines" },
  545. { TIFFTAG_SUBIFD, "SubIFD" },
  546. { TIFFTAG_INKSET, "InkSet" },
  547. { TIFFTAG_INKNAMES, "InkNames" },
  548. { TIFFTAG_NUMBEROFINKS, "NumberOfInks" },
  549. { TIFFTAG_DOTRANGE, "DotRange" },
  550. { TIFFTAG_TARGETPRINTER, "TargetPrinter" },
  551. { TIFFTAG_EXTRASAMPLES, "ExtraSamples" },
  552. { TIFFTAG_SAMPLEFORMAT, "SampleFormat" },
  553. { TIFFTAG_SMINSAMPLEVALUE, "SMinSampleValue" },
  554. { TIFFTAG_SMAXSAMPLEVALUE, "SMaxSampleValue" },
  555. { TIFFTAG_JPEGPROC, "JPEGProcessingMode" },
  556. { TIFFTAG_JPEGIFOFFSET, "JPEGInterchangeFormat" },
  557. { TIFFTAG_JPEGIFBYTECOUNT, "JPEGInterchangeFormatLength" },
  558. { TIFFTAG_JPEGRESTARTINTERVAL,"JPEGRestartInterval" },
  559. { TIFFTAG_JPEGLOSSLESSPREDICTORS,"JPEGLosslessPredictors" },
  560. { TIFFTAG_JPEGPOINTTRANSFORM,"JPEGPointTransform" },
  561. { TIFFTAG_JPEGTABLES, "JPEGTables" },
  562. { TIFFTAG_JPEGQTABLES, "JPEGQTables" },
  563. { TIFFTAG_JPEGDCTABLES, "JPEGDCTables" },
  564. { TIFFTAG_JPEGACTABLES, "JPEGACTables" },
  565. { TIFFTAG_YCBCRCOEFFICIENTS,"YCbCrCoefficients" },
  566. { TIFFTAG_YCBCRSUBSAMPLING, "YCbCrSubsampling" },
  567. { TIFFTAG_YCBCRPOSITIONING, "YCbCrPositioning" },
  568. { TIFFTAG_REFERENCEBLACKWHITE, "ReferenceBlackWhite" },
  569. { TIFFTAG_REFPTS, "IgReferencePoints (Island Graphics)" },
  570. { TIFFTAG_REGIONTACKPOINT, "IgRegionTackPoint (Island Graphics)" },
  571. { TIFFTAG_REGIONWARPCORNERS,"IgRegionWarpCorners (Island Graphics)" },
  572. { TIFFTAG_REGIONAFFINE, "IgRegionAffine (Island Graphics)" },
  573. { TIFFTAG_MATTEING, "OBSOLETE Matteing (Silicon Graphics)" },
  574. { TIFFTAG_DATATYPE, "OBSOLETE DataType (Silicon Graphics)" },
  575. { TIFFTAG_IMAGEDEPTH, "ImageDepth (Silicon Graphics)" },
  576. { TIFFTAG_TILEDEPTH, "TileDepth (Silicon Graphics)" },
  577. { 32768, "OLD BOGUS Matteing tag" },
  578. { TIFFTAG_COPYRIGHT, "Copyright" },
  579. { TIFFTAG_ICCPROFILE, "ICC Profile" },
  580. { TIFFTAG_JBIGOPTIONS, "JBIG Options" },
  581. { TIFFTAG_STONITS, "StoNits" },
  582. };
  583. #define NTAGS (sizeof (tagnames) / sizeof (tagnames[0]))
  584. static void
  585. PrintTag(FILE* fd, uint16 tag)
  586. {
  587. const struct tagname *tp;
  588. for (tp = tagnames; tp < &tagnames[NTAGS]; tp++)
  589. if (tp->tag == tag) {
  590. fprintf(fd, "%s (%u)", tp->name, tag);
  591. return;
  592. }
  593. fprintf(fd, "%u (%#x)", tag, tag);
  594. }
  595. static void
  596. PrintType(FILE* fd, uint16 type)
  597. {
  598. static const char *typenames[] = {
  599. "0",
  600. "BYTE",
  601. "ASCII",
  602. "SHORT",
  603. "LONG",
  604. "RATIONAL",
  605. "SBYTE",
  606. "UNDEFINED",
  607. "SSHORT",
  608. "SLONG",
  609. "SRATIONAL",
  610. "FLOAT",
  611. "DOUBLE",
  612. "IFD",
  613. "14",
  614. "15",
  615. "LONG8",
  616. "SLONG8",
  617. "IFD8"
  618. };
  619. #define NTYPES (sizeof (typenames) / sizeof (typenames[0]))
  620. if (type < NTYPES)
  621. fprintf(fd, "%s (%u)", typenames[type], type);
  622. else
  623. fprintf(fd, "%u (%#x)", type, type);
  624. }
  625. #undef NTYPES
  626. #include <ctype.h>
  627. static void
  628. PrintASCII(FILE* fd, uint32 cc, const unsigned char* cp)
  629. {
  630. for (; cc > 0; cc--, cp++) {
  631. const char* tp;
  632. if (isprint(*cp)) {
  633. fputc(*cp, fd);
  634. continue;
  635. }
  636. for (tp = "\tt\bb\rr\nn\vv"; *tp; tp++)
  637. if (*tp++ == *cp)
  638. break;
  639. if (*tp)
  640. fprintf(fd, "\\%c", *tp);
  641. else if (*cp)
  642. fprintf(fd, "\\%03o", *cp);
  643. else
  644. fprintf(fd, "\\0");
  645. }
  646. }
  647. static void
  648. PrintData(FILE* fd, uint16 type, uint32 count, unsigned char* data)
  649. {
  650. char* sep = "";
  651. switch (type) {
  652. case TIFF_BYTE:
  653. while (count-- > 0)
  654. fprintf(fd, bytefmt, sep, *data++), sep = " ";
  655. break;
  656. case TIFF_SBYTE:
  657. while (count-- > 0)
  658. fprintf(fd, sbytefmt, sep, *(char *)data++), sep = " ";
  659. break;
  660. case TIFF_UNDEFINED:
  661. while (count-- > 0)
  662. fprintf(fd, bytefmt, sep, *data++), sep = " ";
  663. break;
  664. case TIFF_ASCII:
  665. PrintASCII(fd, count, data);
  666. break;
  667. case TIFF_SHORT: {
  668. uint16 *wp = (uint16*)data;
  669. while (count-- > 0)
  670. fprintf(fd, shortfmt, sep, *wp++), sep = " ";
  671. break;
  672. }
  673. case TIFF_SSHORT: {
  674. int16 *wp = (int16*)data;
  675. while (count-- > 0)
  676. fprintf(fd, sshortfmt, sep, *wp++), sep = " ";
  677. break;
  678. }
  679. case TIFF_LONG: {
  680. uint32 *lp = (uint32*)data;
  681. while (count-- > 0) {
  682. fprintf(fd, longfmt, sep, (unsigned long) *lp++);
  683. sep = " ";
  684. }
  685. break;
  686. }
  687. case TIFF_SLONG: {
  688. int32 *lp = (int32*)data;
  689. while (count-- > 0)
  690. fprintf(fd, slongfmt, sep, (long) *lp++), sep = " ";
  691. break;
  692. }
  693. case TIFF_LONG8: {
  694. uint64 *llp = (uint64*)data;
  695. while (count-- > 0) {
  696. #if defined(__WIN32__) && defined(_MSC_VER)
  697. fprintf(fd, long8fmt, sep, (unsigned __int64) *llp++);
  698. #else
  699. fprintf(fd, long8fmt, sep, (unsigned long long) *llp++);
  700. #endif
  701. sep = " ";
  702. }
  703. break;
  704. }
  705. case TIFF_SLONG8: {
  706. int64 *llp = (int64*)data;
  707. while (count-- > 0)
  708. #if defined(__WIN32__) && defined(_MSC_VER)
  709. fprintf(fd, slong8fmt, sep, (__int64) *llp++), sep = " ";
  710. #else
  711. fprintf(fd, slong8fmt, sep, (long long) *llp++), sep = " ";
  712. #endif
  713. break;
  714. }
  715. case TIFF_RATIONAL: {
  716. uint32 *lp = (uint32*)data;
  717. while (count-- > 0) {
  718. if (lp[1] == 0)
  719. fprintf(fd, "%sNan (%lu/%lu)", sep,
  720. (unsigned long) lp[0],
  721. (unsigned long) lp[1]);
  722. else
  723. fprintf(fd, rationalfmt, sep,
  724. (double)lp[0] / (double)lp[1]);
  725. sep = " ";
  726. lp += 2;
  727. }
  728. break;
  729. }
  730. case TIFF_SRATIONAL: {
  731. int32 *lp = (int32*)data;
  732. while (count-- > 0) {
  733. if (lp[1] == 0)
  734. fprintf(fd, "%sNan (%ld/%ld)", sep,
  735. (long) lp[0], (long) lp[1]);
  736. else
  737. fprintf(fd, srationalfmt, sep,
  738. (double)lp[0] / (double)lp[1]);
  739. sep = " ";
  740. lp += 2;
  741. }
  742. break;
  743. }
  744. case TIFF_FLOAT: {
  745. float *fp = (float *)data;
  746. while (count-- > 0)
  747. fprintf(fd, floatfmt, sep, *fp++), sep = " ";
  748. break;
  749. }
  750. case TIFF_DOUBLE: {
  751. double *dp = (double *)data;
  752. while (count-- > 0)
  753. fprintf(fd, doublefmt, sep, *dp++), sep = " ";
  754. break;
  755. }
  756. case TIFF_IFD: {
  757. uint32 *lp = (uint32*)data;
  758. while (count-- > 0) {
  759. fprintf(fd, ifdfmt, sep, (unsigned long) *lp++);
  760. sep = " ";
  761. }
  762. break;
  763. }
  764. case TIFF_IFD8: {
  765. uint64 *llp = (uint64*)data;
  766. while (count-- > 0) {
  767. #if defined(__WIN32__) && defined(_MSC_VER)
  768. fprintf(fd, ifd8fmt, sep, (unsigned __int64) *llp++);
  769. #else
  770. fprintf(fd, ifd8fmt, sep, (unsigned long long) *llp++);
  771. #endif
  772. sep = " ";
  773. }
  774. break;
  775. }
  776. }
  777. }
  778. static void
  779. ReadError(char* what)
  780. {
  781. Fatal("Error while reading %s", what);
  782. }
  783. #include <stdarg.h>
  784. static void
  785. vError(FILE* fd, const char* fmt, va_list ap)
  786. {
  787. fprintf(fd, "%s: ", curfile);
  788. vfprintf(fd, fmt, ap);
  789. fprintf(fd, ".\n");
  790. }
  791. static void
  792. Error(const char* fmt, ...)
  793. {
  794. va_list ap;
  795. va_start(ap, fmt);
  796. vError(stderr, fmt, ap);
  797. va_end(ap);
  798. }
  799. static void
  800. Fatal(const char* fmt, ...)
  801. {
  802. va_list ap;
  803. va_start(ap, fmt);
  804. vError(stderr, fmt, ap);
  805. va_end(ap);
  806. exit(-1);
  807. }
  808. /* vim: set ts=8 sts=8 sw=8 noet: */
  809. /*
  810. * Local Variables:
  811. * mode: c
  812. * c-basic-offset: 8
  813. * fill-column: 78
  814. * End:
  815. */