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.
 
 
 

68 lines
2.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 <tiffio.h>
  11. #include <fcntl.h>
  12. int tagcmpfunc (const void * a, const void * b) {
  13. uint8 * aa = (uint8 *) a;
  14. uint8 * ab = (uint8 *) b;
  15. uint16 taga = (*(aa+1) << 8) + *aa;
  16. uint16 tagb = (*(ab+1) << 8) + *ab;
  17. /* printf ("a:%i b:%i\n", taga, tagb); */
  18. return ( taga -tagb );
  19. }
  20. /* sort order of tifftags */
  21. int cleanup_tagorder (const char * filename) {
  22. int fd = open( filename, O_RDWR );
  23. if (fd < 0) {
  24. return -1;
  25. } else {
  26. /* find header */
  27. /* seek the image file directory (bytes 4-7) */
  28. uint32 hdr;
  29. if (read(fd, &hdr, 4) != 4) perror ("TIFF Header read error");
  30. if (hdr != 0x002a4949) perror ("only little endian support yet");
  31. uint32 offset;
  32. if (read(fd, &offset, 4) != 4)
  33. perror ("TIFF Header read error");
  34. // printf("diroffset to %i (0x%04lx)\n", offset, offset);
  35. //printf("byte swapped? %s\n", (TIFFIsByteSwapped(tif)?"true":"false"));
  36. /* read and seek to IFD address */
  37. lseek(fd, (off_t) offset, SEEK_SET);
  38. uint16 count;
  39. if (read(fd, &count, 2) != 2)
  40. perror ("TIFF Header read error2");
  41. /* printf( "tag_counter=%i\n", count); */
  42. /* read the old ifd entries */
  43. uint8 buffer[ count * 12];
  44. if (read(fd, buffer, count * 12) != (count *12))
  45. perror ("TIFF IFD read error");
  46. /* ok, now sort buffer */
  47. qsort(buffer, count, sizeof(uint8) * 12, tagcmpfunc);
  48. /* print tags */
  49. /*
  50. for (int i=0; i < count; i++) {
  51. uint8 * base=buffer+12*i;
  52. uint16 tag = (* (base+1) << 8) + *base;
  53. printf ("i:%i tag=%i\n", i, tag);
  54. }
  55. */
  56. /* write data back */
  57. lseek(fd, (off_t) offset + 2, SEEK_SET);
  58. if (write(fd, buffer, count * 12) != (count *12))
  59. perror ("TIFF IFD write error");
  60. }
  61. return 0;
  62. }
  63. /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 smarttab expandtab :*/