check_dng.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. SUITE (test_suite)
  92. {
  93. RUN_TEST(generate_simple_dng);
  94. }
  95. GREATEST_MAIN_DEFS();
  96. int
  97. main(int argc, char **argv)
  98. {
  99. GREATEST_MAIN_BEGIN();
  100. libdng_init();
  101. RUN_SUITE(test_suite);
  102. GREATEST_MAIN_END();
  103. }