check_dng.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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;
  19. if (TIFFGetField(im, tag, &temp) != 1) {
  20. FAILm(name);
  21. }
  22. ASSERT_EQ_FMTm(name, expected, temp, "%d");
  23. PASS();
  24. }
  25. TEST generate_simple_dng(void)
  26. {
  27. libdng_info info = {0};
  28. libdng_new(&info);
  29. ASSERT_EQm("Set mode", 1, libdng_set_mode_from_name(&info, "RGGB"));
  30. ASSERT_EQm("Set make", 1, libdng_set_make_model(&info, "Make", "Model"));
  31. ASSERT_EQm("Set software", 1, libdng_set_software(&info, "Software"));
  32. ASSERT_EQm("Set orientation", 1, libdng_set_orientation(&info, 4));
  33. uint8_t *data = malloc(1280 * 720);
  34. ASSERT_EQm("Write DNG", 1, libdng_write(&info, "test.dng", 1280, 720, data, 1280 * 720));
  35. free(data);
  36. libdng_free(&info);
  37. // Use LibTIFF directly to verify results
  38. TIFF *im = TIFFOpen("test.dng", "r");
  39. if (im == NULL) {
  40. FAILm("Could not open result");
  41. }
  42. // Check IFD0 with most metadata and the thumbnail image
  43. CHECK_CALL(check_int_tag(im, TIFFTAG_ORIENTATION, "ORIENTATION", 4));
  44. CHECK_CALL(check_int_tag(im, TIFFTAG_BITSPERSAMPLE, "THUMB_BPS", 8));
  45. CHECK_CALL(check_int_tag(im, TIFFTAG_SAMPLESPERPIXEL, "THUMB_CHANNELS", 3));
  46. CHECK_CALL(check_int_tag(im, TIFFTAG_PHOTOMETRIC, "THUMB_PHOTOMETRIC", PHOTOMETRIC_RGB));
  47. CHECK_CALL(check_str_tag(im, TIFFTAG_MAKE, "MAKE", "Make"));
  48. CHECK_CALL(check_str_tag(im, TIFFTAG_MODEL, "MODEL", "Model"));
  49. CHECK_CALL(check_str_tag(im, TIFFTAG_UNIQUECAMERAMODEL, "UCM", "Make Model"));
  50. CHECK_CALL(check_str_tag(im, TIFFTAG_SOFTWARE, "SOFTWARE", "Software"));
  51. CHECK_CALL(check_int_tag(im, TIFFTAG_ORIENTATION, "ORIENTATION", 4));
  52. // Switch to IFD1 which has the raw data
  53. int subifd_count = 0;
  54. void *ptr;
  55. toff_t subifd_offsets[2];
  56. TIFFGetField(im, TIFFTAG_SUBIFD, &subifd_count, &ptr);
  57. memcpy(subifd_offsets, ptr, subifd_count * sizeof(subifd_offsets[0]));
  58. TIFFSetSubDirectory(im, subifd_offsets[0]);
  59. // Check IFD1 metadata
  60. CHECK_CALL(check_int_tag(im, TIFFTAG_IMAGEWIDTH, "RAW_WIDTH", 1280));
  61. CHECK_CALL(check_int_tag(im, TIFFTAG_IMAGELENGTH, "RAW_HEIGHT", 720));
  62. CHECK_CALL(check_int_tag(im, TIFFTAG_BITSPERSAMPLE, "RAW_BPS", 8));
  63. CHECK_CALL(check_int_tag(im, TIFFTAG_SAMPLESPERPIXEL, "RAW_CHANNELS", 1));
  64. CHECK_CALL(check_int_tag(im, TIFFTAG_PHOTOMETRIC, "RAW_PHOTOMETRIC", PHOTOMETRIC_CFA));
  65. PASS();
  66. }
  67. SUITE (test_suite)
  68. {
  69. RUN_TEST(generate_simple_dng);
  70. }
  71. GREATEST_MAIN_DEFS();
  72. int
  73. main(int argc, char **argv)
  74. {
  75. GREATEST_MAIN_BEGIN();
  76. libdng_init();
  77. RUN_SUITE(test_suite);
  78. GREATEST_MAIN_END();
  79. }