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.
 
 
 

70 lines
2.6 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 "check_baseline.h"
  11. #include "tiff_helper.h"
  12. #include <tiffio.h>
  13. /** load a tiff, clean it up if needed, store tiff
  14. * @param filename filename which should be processed, repaired
  15. */
  16. int cleanup_baseline(const char * filename ) {
  17. if (
  18. (FIXIT_TIFF_IS_VALID == check_baseline (filename)) &&
  19. (FIXIT_TIFF_IS_VALID == check_required (filename))
  20. ) return FIXIT_TIFF_IS_VALID;
  21. else {
  22. /* load file */
  23. TIFF* tif = TIFFOpen(filename, "r+");
  24. if (NULL == tif) {
  25. fprintf( stderr, "file '%s' could not be opened\n", filename);
  26. exit (FIXIT_TIFF_READ_PERMISSION_ERROR);
  27. };
  28. uint32 tag_counter=TIFFGetRawTagListCount(tif);
  29. uint32 tagidx;
  30. uint32 tags[tag_counter];
  31. for (tagidx=0; tagidx < tag_counter; tagidx++) {
  32. tags[tagidx] = TIFFGetRawTagListEntry( tif, tagidx );
  33. }
  34. /* iterate through all tiff-tags in tiff file
  35. * delete all tags not in baselinetags
  36. */
  37. for (tagidx=0; tagidx < tag_counter; tagidx++) {
  38. printf ("found tag %u [0x%x] (%u)\n", tags[tagidx],tags[tagidx], tagidx);
  39. int found = 0;
  40. int baseline_index=0;
  41. for (baseline_index = 0; baseline_index < count_of_baselinetags; baseline_index++) {
  42. if (tags[tagidx] == baselinetags[baseline_index]) {
  43. printf ("DEBUG tag=%u base=%i idx=%i\n", tags[tagidx], baselinetags[baseline_index], baseline_index);
  44. found = 1;
  45. break;
  46. }
  47. }
  48. if (found == 0 ) {
  49. if (FLAGGED == flag_be_verbose) printf("removed tag %u\n", tags[tagidx]);
  50. TIFFUnsetField(tif, tags[tagidx]);
  51. }
  52. }
  53. if (FLAGGED == flag_be_verbose) printf("After correction\n-----------------\n");
  54. if (FLAGGED == flag_be_verbose) TIFFPrintDirectory(tif, stdout, TIFFPRINT_NONE);
  55. /* write data back, only if no flag_check_only is set */
  56. int written = TIFFRewriteDirectory(tif); /* HINT: TIFFWriteDirectory is errorneous (because sometimes libtiff could extend IFD with missed tags automatically, use TIFFReWriteDirectory instead. This adds a new(!) IFD at the end of the TIFF file. */
  57. if (1 != written) {
  58. fprintf(stderr, "something is wrong, tiffdir could not be written to file '%s'\n", filename);
  59. exit (FIXIT_TIFF_WRITE_ERROR);
  60. }
  61. TIFFClose(tif);
  62. }
  63. if (FIXIT_TIFF_IS_VALID == check_baseline (filename)) return FIXIT_TIFF_IS_CORRECTED;
  64. else return FIXIT_TIFF_IS_CHECKED;
  65. }