check_dng.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <stdbool.h>
  2. #include <tiffio.h>
  3. #include "greatest.h"
  4. #include "libdng.h"
  5. static enum greatest_test_res
  6. check_str_tag(TIFF *im, uint32_t tag, const char *name, const char *expected)
  7. {
  8. char *temp;
  9. if (TIFFGetField(im, tag, &temp) != 1) {
  10. FAILm(name);
  11. }
  12. ASSERT_STR_EQm(name, expected, temp);
  13. PASS();
  14. }
  15. static enum greatest_test_res
  16. check_int_tag(TIFF *im, uint32_t tag, const char *name, int expected)
  17. {
  18. uint32_t temp = 0;
  19. if (TIFFGetField(im, tag, &temp) != 1) {
  20. FAILm(name);
  21. }
  22. ASSERT_EQ_FMTm(name, expected, temp, "%d");
  23. PASS();
  24. }
  25. static enum greatest_test_res
  26. check_float_tag(TIFF *im, uint32_t tag, const char *name, float expected)
  27. {
  28. float temp;
  29. if (TIFFGetField(im, tag, &temp) != 1) {
  30. FAILm(name);
  31. }
  32. ASSERT_EQ_FMTm(name, expected, temp, "%f");
  33. PASS();
  34. }
  35. TEST generate_simple_dng(void)
  36. {
  37. libdng_info info = {0};
  38. libdng_new(&info);
  39. ASSERT_EQm("Set mode", 1, libdng_set_mode_from_name(&info, "RGGB"));
  40. ASSERT_EQm("Set make", 1, libdng_set_make_model(&info, "Make", "Model"));
  41. ASSERT_EQm("Set software", 1, libdng_set_software(&info, "Software"));
  42. ASSERT_EQm("Set orientation", 1, libdng_set_orientation(&info, 4));
  43. ASSERT_EQm("Set exposuretime", 1, libdng_set_exposure_time(&info, 0.04f));
  44. ASSERT_EQm("Set fnumber", 1, libdng_set_fnumber(&info, 2.8f));
  45. ASSERT_EQm("Set fnumber", 1, libdng_set_focal_length(&info, 50.0f, 1.5f));
  46. ASSERT_EQm("Set framerate", 1, libdng_set_frame_rate(&info, 30.0f));
  47. uint8_t *data = malloc(1280 * 720);
  48. ASSERT_EQm("Write DNG", 1, libdng_write(&info, "test.dng", 1280, 720, data, 1280 * 720));
  49. free(data);
  50. libdng_free(&info);
  51. // Use LibTIFF directly to verify results
  52. TIFF *im = TIFFOpen("test.dng", "r");
  53. if (im == NULL) {
  54. FAILm("Could not open result");
  55. }
  56. toff_t exif_offset;
  57. if (TIFFGetField(im, TIFFTAG_EXIFIFD, &exif_offset) != 1) {
  58. FAILm("Could not find EXIF data");
  59. }
  60. // Check IFD0 with most metadata and the thumbnail image
  61. CHECK_CALL(check_int_tag(im, TIFFTAG_ORIENTATION, "ORIENTATION", 4));
  62. CHECK_CALL(check_int_tag(im, TIFFTAG_BITSPERSAMPLE, "THUMB_BPS", 8));
  63. CHECK_CALL(check_int_tag(im, TIFFTAG_SAMPLESPERPIXEL, "THUMB_CHANNELS", 3));
  64. CHECK_CALL(check_int_tag(im, TIFFTAG_PHOTOMETRIC, "THUMB_PHOTOMETRIC", PHOTOMETRIC_RGB));
  65. CHECK_CALL(check_str_tag(im, TIFFTAG_MAKE, "MAKE", "Make"));
  66. CHECK_CALL(check_str_tag(im, TIFFTAG_MODEL, "MODEL", "Model"));
  67. CHECK_CALL(check_str_tag(im, TIFFTAG_UNIQUECAMERAMODEL, "UCM", "Make Model"));
  68. CHECK_CALL(check_str_tag(im, TIFFTAG_SOFTWARE, "SOFTWARE", "Software"));
  69. CHECK_CALL(check_int_tag(im, TIFFTAG_ORIENTATION, "ORIENTATION", 4));
  70. // Switch to IFD1 which has the raw data
  71. int subifd_count = 0;
  72. void *ptr;
  73. toff_t subifd_offsets[2];
  74. TIFFGetField(im, TIFFTAG_SUBIFD, &subifd_count, &ptr);
  75. memcpy(subifd_offsets, ptr, subifd_count * sizeof(subifd_offsets[0]));
  76. TIFFSetSubDirectory(im, subifd_offsets[0]);
  77. // Check IFD1 metadata
  78. CHECK_CALL(check_int_tag(im, TIFFTAG_IMAGEWIDTH, "RAW_WIDTH", 1280));
  79. CHECK_CALL(check_int_tag(im, TIFFTAG_IMAGELENGTH, "RAW_HEIGHT", 720));
  80. CHECK_CALL(check_int_tag(im, TIFFTAG_BITSPERSAMPLE, "RAW_BPS", 8));
  81. CHECK_CALL(check_int_tag(im, TIFFTAG_SAMPLESPERPIXEL, "RAW_CHANNELS", 1));
  82. CHECK_CALL(check_int_tag(im, TIFFTAG_PHOTOMETRIC, "RAW_PHOTOMETRIC", PHOTOMETRIC_CFA));
  83. // Switch to the EXIF block with the generic picture metadata
  84. TIFFReadEXIFDirectory(im, exif_offset);
  85. CHECK_CALL(check_float_tag(im, EXIFTAG_EXPOSURETIME, "EXPOSURETIME", 0.04f));
  86. CHECK_CALL(check_float_tag(im, EXIFTAG_FNUMBER, "FNUMBER", 2.8f));
  87. CHECK_CALL(check_float_tag(im, EXIFTAG_FOCALLENGTH, "FOCALLENGTH", 50.0f));
  88. CHECK_CALL(check_int_tag(im, EXIFTAG_FOCALLENGTHIN35MMFILM, "FOCALLENGTHIN35MMFILM", 75));
  89. PASS();
  90. }
  91. TEST read_dng(void)
  92. {
  93. // Generate test timestamp
  94. struct tm testtime;
  95. strptime("2024-08-27 01:02:03", "%Y-%m-%d %H:%M:%S", &testtime);
  96. // Generate a file to read
  97. libdng_info info = {0};
  98. libdng_new(&info);
  99. libdng_set_datetime(&info, testtime);
  100. libdng_set_mode_from_name(&info, "RGGB");
  101. libdng_set_make_model(&info, "Make", "Model");
  102. libdng_set_software(&info, "Software");
  103. libdng_set_orientation(&info, LIBDNG_ORIENTATION_RIGHTBOT);
  104. libdng_set_exposure_time(&info, 1.4f);
  105. libdng_set_exposure_program(&info, LIBDNG_EXPOSUREPROGRAM_PORTRAIT);
  106. libdng_set_fnumber(&info, 1.8f);
  107. libdng_set_focal_length(&info, 75.0f, 1.6f);
  108. uint8_t *data = malloc(1280 * 720);
  109. libdng_write(&info, "test.dng", 1280, 720, data, 1280 * 720);
  110. free(data);
  111. libdng_free(&info);
  112. // Read the file again
  113. libdng_info dng = {0};
  114. libdng_read(&dng, "test.dng");
  115. ASSERT_EQm("Bit depth", 8, dng.bit_depth);
  116. ASSERT_EQm("CFFA[0]", 0, dng.cfapattern[0]);
  117. ASSERT_EQm("CFFA[1]", 1, dng.cfapattern[1]);
  118. ASSERT_EQm("CFFA[2]", 1, dng.cfapattern[2]);
  119. ASSERT_EQm("CFFA[3]", 2, dng.cfapattern[3]);
  120. ASSERT_STR_EQm("Make", "Make", dng.camera_make);
  121. ASSERT_STR_EQm("Model", "Model", dng.camera_model);
  122. ASSERT_STR_EQm("Software", "Software", dng.software);
  123. ASSERT_EQm("Orientation", LIBDNG_ORIENTATION_RIGHTBOT, dng.orientation);
  124. ASSERT_EQ_FMTm("Exposure time", 1.4f, dng.exposure_time, "%f");
  125. ASSERT_EQm("Exposure program", LIBDNG_EXPOSUREPROGRAM_PORTRAIT, dng.exposure_program);
  126. ASSERT_EQ_FMTm("FNumber", 1.8f, dng.fnumber, "%f");
  127. ASSERT_EQ_FMTm("Focal length", 75.0f, dng.focal_length, "%f");
  128. ASSERT_EQ_FMTm("Crop factor", 1.6f, dng.crop_factor, "%f");
  129. ASSERT_EQm("Year", testtime.tm_year, dng.datetime.tm_year);
  130. ASSERT_EQm("Month", testtime.tm_mon, dng.datetime.tm_mon);
  131. ASSERT_EQm("Day", testtime.tm_yday, dng.datetime.tm_yday);
  132. ASSERT_EQ_FMTm("Hour", testtime.tm_hour, dng.datetime.tm_hour, "%d");
  133. ASSERT_EQm("Minute", testtime.tm_min, dng.datetime.tm_min);
  134. ASSERT_EQm("Second", testtime.tm_sec, dng.datetime.tm_sec);
  135. PASS();
  136. }
  137. SUITE (test_suite)
  138. {
  139. RUN_TEST(generate_simple_dng);
  140. RUN_TEST(read_dng);
  141. }
  142. GREATEST_MAIN_DEFS();
  143. int
  144. main(int argc, char **argv)
  145. {
  146. GREATEST_MAIN_BEGIN();
  147. libdng_init();
  148. RUN_SUITE(test_suite);
  149. GREATEST_MAIN_END();
  150. }