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.
 
 
 

80 lines
2.1 KiB

  1. /* fixes broken TIFF Files
  2. *
  3. * fixes invalid DateTime-field 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_datetime.h"
  11. /** check if date / time values are within correct ranges
  12. * @param year year
  13. * @param month month
  14. * @param day day
  15. * @param hour hour
  16. * @param min min
  17. * @param sec sec
  18. * @return 0 if success, otherwise -1
  19. */
  20. int test_plausibility (int * year, int * month, int * day, int * hour, int * min, int * sec) {
  21. if (FLAGGED == flag_be_verbose) printf ("found: y=%d m=%d d=%d h=%d m=%d s=%d\n", *year, *month, *day, *hour, *min, *sec);
  22. if (
  23. 1500 < *year &&
  24. 2100 > *year &&
  25. 0 < *month &&
  26. 13 > *month &&
  27. 0 < *day &&
  28. 32 > *day &&
  29. 0 <= *hour &&
  30. 24 > *hour &&
  31. 0 <= *min &&
  32. 60 > *min &&
  33. 0 <= *sec &&
  34. 60 > *sec
  35. ) {
  36. return 0;
  37. } else {
  38. return -1;
  39. }
  40. }
  41. /** loads a tiff, fix it if needed, stores tiff
  42. * @param filename filename which should be processed, repaired
  43. */
  44. int check_datetime(const char * filename ) {
  45. /* load file */
  46. TIFF* tif = TIFFOpen(filename, "r+");
  47. if (NULL == tif) {
  48. fprintf( stderr, "file '%s' could not be opened\n", filename);
  49. exit (FIXIT_TIFF_READ_PERMISSION_ERROR);
  50. };
  51. /* find date-tag and fix it */
  52. char *datetime=NULL;
  53. int returncode = 0;
  54. int found=TIFFGetField(tif, TIFFTAG_DATETIME, &datetime);
  55. if (1==found) { /* there exists a datetime field */
  56. int day=0;
  57. int month=0;
  58. int year=0;
  59. int hour=0;
  60. int min=0;
  61. int sec=0;
  62. if (6 == sscanf(datetime, "%04d:%02d:%02d%02d:%02d:%02d", &year, &month, &day, &hour, &min, &sec)) {
  63. returncode = test_plausibility(&year, &month, &day, &hour, &min, &sec);
  64. } else {
  65. returncode=-2;
  66. }
  67. } else if (0 == found) {
  68. if (FLAGGED == flag_be_verbose) printf ("no datetime found!\n");
  69. }
  70. TIFFClose(tif);
  71. if (0 == returncode) return FIXIT_TIFF_IS_VALID;
  72. else return FIXIT_TIFF_IS_INVALID;
  73. }