tool to fixes some issues in baseline tiffs (with extensions), see http://andreas-romeyke.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

104 lines
3.1 KiB

  1. /* fixes broken TIFF Files
  2. *
  3. * fixes unused tags in Baseline-TIFFs,
  4. * based on http://www.awaresystems.be/imaging/tiff/tifftags/baseline.html
  5. *
  6. * author: Andreas Romeyke, 2013
  7. * licensed under conditions of libtiff
  8. */
  9. #include "fixit_tiff.h"
  10. #include "tiff_helper.h"
  11. #include <tiffio.h>
  12. /* scans first IDF and returns count of tags */
  13. int TIFFGetRawTagListCount (TIFF * tif) {
  14. int fd = TIFFFileno( tif);
  15. /* seek the image file directory (bytes 4-7) */
  16. lseek(fd, (off_t) 4, SEEK_SET);
  17. uint32 offset;
  18. if (read(fd, &offset, 4) != 4)
  19. perror ("TIFF Header read error");
  20. if (TIFFIsByteSwapped(tif))
  21. TIFFSwabLong(&offset);
  22. // printf("diroffset to %i (0x%04lx)\n", offset, offset);
  23. //printf("byte swapped? %s\n", (TIFFIsByteSwapped(tif)?"true":"false"));
  24. /* read and seek to IFD address */
  25. lseek(fd, (off_t) offset, SEEK_SET);
  26. uint16 count;
  27. if (read(fd, &count, 2) != 2)
  28. perror ("TIFF Header read error2");
  29. if (TIFFIsByteSwapped(tif))
  30. TIFFSwabShort(&count);
  31. return count;
  32. }
  33. /* scans first IDF and returns the n-th tag */
  34. uint32 TIFFGetRawTagListEntry( TIFF * tif, int tagidx ) {
  35. int count = TIFFGetRawTagListCount( tif);
  36. int fd = TIFFFileno( tif);
  37. //printf("count %i\n", count);
  38. /* read count of tags (2 Bytes) */
  39. int i;
  40. for (i = 0; i<count; i++) {
  41. uint16 tagid;
  42. if (read(fd, &tagid, 2) != 2)
  43. perror ("TIFF IFD read error");
  44. if (TIFFIsByteSwapped(tif))
  45. TIFFSwabShort(&tagid);
  46. if (i == tagidx) return tagid;
  47. //printf("tag idx=%i, tag=%i (0x%lx)\n", i, tagid, tagid);
  48. lseek(fd, (off_t) 10, SEEK_CUR);
  49. }
  50. /* loop each tag until end or given tag found */
  51. return -1;
  52. }
  53. void print_baseline_tags (TIFF * tif) {
  54. int i;
  55. int tag_counter=TIFFGetRawTagListCount(tif);
  56. int tagidx;
  57. uint32 tags[tag_counter];
  58. printf ("tag count=%i, [*] means: tag is a baseline tag\n", tag_counter);
  59. for (tagidx=0; tagidx < tag_counter; tagidx++) {
  60. tags[tagidx] = TIFFGetRawTagListEntry( tif, tagidx );
  61. }
  62. for (tagidx=0; tagidx < tag_counter; tagidx++) {
  63. int found = 0;
  64. for (i=0; i<count_of_baselinetags; i++) {
  65. /* printf ("tags[%i]=%i\n",tagidx, tags[tagidx]); */
  66. if (tags[tagidx] == baselinetags[i]) {
  67. found=1;
  68. break;
  69. }
  70. }
  71. printf ("\ttag %5u (0x%4x) %s\n", tags[tagidx], tags[tagidx], (found==1?"[*]":""));
  72. }
  73. }
  74. void print_required_tags (TIFF * tif) {
  75. int i;
  76. int tag_counter=TIFFGetRawTagListCount(tif);
  77. int tagidx;
  78. uint32 tags[tag_counter];
  79. printf ("[*] means: tag already exists\n");
  80. for (tagidx=0; tagidx < tag_counter; tagidx++) {
  81. tags[tagidx] = TIFFGetRawTagListEntry( tif, tagidx );
  82. }
  83. for (i=0; i<count_of_required_baselinetags; i++) {
  84. int found = 0;
  85. for (tagidx=0; tagidx < tag_counter; tagidx++) {
  86. // printf ("tags[%i]=%i\n",tagidx, tags[tagidx]);
  87. if (tags[tagidx] == required_baselinetags[i]) {
  88. found=1;
  89. break;
  90. }
  91. }
  92. printf ("###\ttag %5i (0x%4x) %s\n", required_baselinetags[i], required_baselinetags[i], (found==1?"[*]":""));
  93. }
  94. }
  95. /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 smarttab expandtab :*/