test_exif.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level
  3. // directory of this distribution and at http://opencv.org/license.html
  4. #include <string>
  5. #include <vector>
  6. #include "test_precomp.hpp"
  7. namespace opencv_test { namespace {
  8. static Mat makeCirclesImage(Size size, int type, int nbits)
  9. {
  10. Mat img(size, type);
  11. img.setTo(Scalar::all(0));
  12. RNG& rng = theRNG();
  13. int maxval = (int)(1 << nbits);
  14. for (int i = 0; i < 100; i++) {
  15. int x = rng.uniform(0, img.cols);
  16. int y = rng.uniform(0, img.rows);
  17. int radius = rng.uniform(5, std::min(img.cols, img.rows) / 5);
  18. int b = rng.uniform(0, maxval);
  19. int g = rng.uniform(0, maxval);
  20. int r = rng.uniform(0, maxval);
  21. circle(img, Point(x, y), radius, Scalar(b, g, r), -1, LINE_AA);
  22. }
  23. return img;
  24. }
  25. static std::vector<uchar> getSampleExifData() {
  26. return {
  27. 'M', 'M', 0, '*', 0, 0, 0, 8, 0, 10, 1, 0, 0, 4, 0, 0, 0, 1, 0, 0, 5,
  28. 0, 1, 1, 0, 4, 0, 0, 0, 1, 0, 0, 2, 208, 1, 2, 0, 3, 0, 0, 0, 1,
  29. 0, 10, 0, 0, 1, 18, 0, 3, 0, 0, 0, 1, 0, 1, 0, 0, 1, 14, 0, 2, 0, 0,
  30. 0, '"', 0, 0, 0, 176, 1, '1', 0, 2, 0, 0, 0, 7, 0, 0, 0, 210, 1, 26,
  31. 0, 5, 0, 0, 0, 1, 0, 0, 0, 218, 1, 27, 0, 5, 0, 0, 0, 1, 0, 0, 0,
  32. 226, 1, '(', 0, 3, 0, 0, 0, 1, 0, 2, 0, 0, 135, 'i', 0, 4, 0, 0, 0,
  33. 1, 0, 0, 0, 134, 0, 0, 0, 0, 0, 3, 144, 0, 0, 7, 0, 0, 0, 4, '0', '2',
  34. '2', '1', 160, 2, 0, 4, 0, 0, 0, 1, 0, 0, 5, 0, 160, 3, 0, 4, 0, 0,
  35. 0, 1, 0, 0, 2, 208, 0, 0, 0, 0, 'S', 'a', 'm', 'p', 'l', 'e', ' ', '1', '0',
  36. '-', 'b', 'i', 't', ' ', 'i', 'm', 'a', 'g', 'e', ' ', 'w', 'i', 't', 'h', ' ',
  37. 'm', 'e', 't', 'a', 'd', 'a', 't', 'a', 0, 'O', 'p', 'e', 'n', 'C', 'V', 0, 0,
  38. 0, 0, 0, 'H', 0, 0, 0, 1, 0, 0, 0, 'H', 0, 0, 0, 1
  39. };
  40. }
  41. static std::vector<uchar> getSampleXmpData() {
  42. return {
  43. '<','x',':','x','m','p','m','e','t','a',' ','x','m','l','n','s',':','x','=',
  44. '"','a','d','o','b','e',':','x','m','p','"','>',
  45. '<','x','m','p',':','C','r','e','a','t','o','r','T','o','o','l','>',
  46. 'O','p','e','n','C','V',
  47. '<','/','x','m','p',':','C','r','e','a','t','o','r','T','o','o','l','>',
  48. '<','/','x',':','x','m','p','m','e','t','a','>',0
  49. };
  50. }
  51. // Returns a Minimal ICC profile data (Generated with help from ChatGPT)
  52. static std::vector<uchar> getSampleIccpData() {
  53. std::vector<uchar> iccp_data(192, 0);
  54. iccp_data[3] = 192; // Profile size: 192 bytes
  55. iccp_data[12] = 'm';
  56. iccp_data[13] = 'n';
  57. iccp_data[14] = 't';
  58. iccp_data[15] = 'r';
  59. iccp_data[16] = 'R';
  60. iccp_data[17] = 'G';
  61. iccp_data[18] = 'B';
  62. iccp_data[19] = ' ';
  63. iccp_data[20] = 'X';
  64. iccp_data[21] = 'Y';
  65. iccp_data[22] = 'Z';
  66. iccp_data[23] = ' ';
  67. // File signature 'acsp' at offset 36 (0x24)
  68. iccp_data[36] = 'a';
  69. iccp_data[37] = 'c';
  70. iccp_data[38] = 's';
  71. iccp_data[39] = 'p';
  72. // Illuminant D50 at offset 68 (0x44), example values:
  73. iccp_data[68] = 0x00;
  74. iccp_data[69] = 0x00;
  75. iccp_data[70] = 0xF6;
  76. iccp_data[71] = 0xD6; // 0.9642
  77. iccp_data[72] = 0x00;
  78. iccp_data[73] = 0x01;
  79. iccp_data[74] = 0x00;
  80. iccp_data[75] = 0x00; // 1.0
  81. iccp_data[76] = 0x00;
  82. iccp_data[77] = 0x00;
  83. iccp_data[78] = 0xD3;
  84. iccp_data[79] = 0x2D; // 0.8249
  85. // Tag count at offset 128 (0x80) = 1
  86. iccp_data[131] = 1;
  87. // Tag record at offset 132 (0x84): signature 'desc', offset 128, size 64
  88. iccp_data[132] = 'd';
  89. iccp_data[133] = 'e';
  90. iccp_data[134] = 's';
  91. iccp_data[135] = 'c';
  92. iccp_data[139] = 128; // offset
  93. iccp_data[143] = 64; // size
  94. // Tag data 'desc' at offset 128 (start of tag data)
  95. // Set type 'desc' etc. here, for simplicity fill zeros
  96. iccp_data[144] = 'd';
  97. iccp_data[145] = 'e';
  98. iccp_data[146] = 's';
  99. iccp_data[147] = 'c';
  100. // ASCII string length at offset 156
  101. iccp_data[156] = 20; // length
  102. // ASCII string "Minimal ICC Profile" starting at offset 160
  103. iccp_data[160] = 'M';
  104. iccp_data[161] = 'i';
  105. iccp_data[162] = 'n';
  106. iccp_data[163] = 'i';
  107. iccp_data[164] = 'm';
  108. iccp_data[165] = 'a';
  109. iccp_data[166] = 'l';
  110. iccp_data[167] = ' ';
  111. iccp_data[168] = 'I';
  112. iccp_data[169] = 'C';
  113. iccp_data[170] = 'C';
  114. iccp_data[171] = ' ';
  115. iccp_data[172] = 'P';
  116. iccp_data[173] = 'r';
  117. iccp_data[174] = 'o';
  118. iccp_data[175] = 'f';
  119. iccp_data[176] = 'i';
  120. iccp_data[177] = 'l';
  121. iccp_data[178] = 'e';
  122. return iccp_data;
  123. }
  124. /**
  125. * Test to check whether the EXIF orientation tag was processed successfully or not.
  126. * The test uses a set of 8 images named testExifOrientation_{1 to 8}.(extension).
  127. * Each test image is a 10x10 square, divided into four smaller sub-squares:
  128. * (R corresponds to Red, G to Green, B to Blue, W to White)
  129. * --------- ---------
  130. * | R | G | | G | R |
  131. * |-------| - (tag 1) |-------| - (tag 2)
  132. * | B | W | | W | B |
  133. * --------- ---------
  134. *
  135. * --------- ---------
  136. * | W | B | | B | W |
  137. * |-------| - (tag 3) |-------| - (tag 4)
  138. * | G | R | | R | G |
  139. * --------- ---------
  140. *
  141. * --------- ---------
  142. * | R | B | | G | W |
  143. * |-------| - (tag 5) |-------| - (tag 6)
  144. * | G | W | | R | B |
  145. * --------- ---------
  146. *
  147. * --------- ---------
  148. * | W | G | | B | R |
  149. * |-------| - (tag 7) |-------| - (tag 8)
  150. * | B | R | | W | G |
  151. * --------- ---------
  152. *
  153. *
  154. * Each image contains an EXIF field with an orientation tag (0x112).
  155. * After reading each image and applying the orientation tag,
  156. * the resulting image should be:
  157. * ---------
  158. * | R | G |
  159. * |-------|
  160. * | B | W |
  161. * ---------
  162. *
  163. * Note:
  164. * The flags parameter of the imread function is set as IMREAD_COLOR | IMREAD_ANYCOLOR | IMREAD_ANYDEPTH.
  165. * Using this combination is an undocumented trick to load images similarly to the IMREAD_UNCHANGED flag,
  166. * preserving the alpha channel (if present) while also applying the orientation.
  167. */
  168. typedef testing::TestWithParam<string> Exif;
  169. TEST_P(Exif, exif_orientation)
  170. {
  171. const string root = cvtest::TS::ptr()->get_data_path();
  172. const string filename = root + GetParam();
  173. const int colorThresholdHigh = 250;
  174. const int colorThresholdLow = 5;
  175. // Refer to the note in the explanation above.
  176. Mat m_img = imread(filename, IMREAD_COLOR | IMREAD_ANYCOLOR | IMREAD_ANYDEPTH);
  177. ASSERT_FALSE(m_img.empty());
  178. if (m_img.channels() == 3)
  179. {
  180. Vec3b vec;
  181. //Checking the first quadrant (with supposed red)
  182. vec = m_img.at<Vec3b>(2, 2); //some point inside the square
  183. EXPECT_LE(vec.val[0], colorThresholdLow);
  184. EXPECT_LE(vec.val[1], colorThresholdLow);
  185. EXPECT_GE(vec.val[2], colorThresholdHigh);
  186. //Checking the second quadrant (with supposed green)
  187. vec = m_img.at<Vec3b>(2, 7); //some point inside the square
  188. EXPECT_LE(vec.val[0], colorThresholdLow);
  189. EXPECT_GE(vec.val[1], colorThresholdHigh);
  190. EXPECT_LE(vec.val[2], colorThresholdLow);
  191. //Checking the third quadrant (with supposed blue)
  192. vec = m_img.at<Vec3b>(7, 2); //some point inside the square
  193. EXPECT_GE(vec.val[0], colorThresholdHigh);
  194. EXPECT_LE(vec.val[1], colorThresholdLow);
  195. EXPECT_LE(vec.val[2], colorThresholdLow);
  196. }
  197. else
  198. {
  199. Vec4b vec;
  200. //Checking the first quadrant (with supposed red)
  201. vec = m_img.at<Vec4b>(2, 2); //some point inside the square
  202. EXPECT_LE(vec.val[0], colorThresholdLow);
  203. EXPECT_LE(vec.val[1], colorThresholdLow);
  204. EXPECT_GE(vec.val[2], colorThresholdHigh);
  205. //Checking the second quadrant (with supposed green)
  206. vec = m_img.at<Vec4b>(2, 7); //some point inside the square
  207. EXPECT_LE(vec.val[0], colorThresholdLow);
  208. EXPECT_GE(vec.val[1], colorThresholdHigh);
  209. EXPECT_LE(vec.val[2], colorThresholdLow);
  210. //Checking the third quadrant (with supposed blue)
  211. vec = m_img.at<Vec4b>(7, 2); //some point inside the square
  212. EXPECT_GE(vec.val[0], colorThresholdHigh);
  213. EXPECT_LE(vec.val[1], colorThresholdLow);
  214. EXPECT_LE(vec.val[2], colorThresholdLow);
  215. }
  216. }
  217. const std::vector<std::string> exif_files
  218. {
  219. #ifdef HAVE_JPEG
  220. "readwrite/testExifOrientation_1.jpg",
  221. "readwrite/testExifOrientation_2.jpg",
  222. "readwrite/testExifOrientation_3.jpg",
  223. "readwrite/testExifOrientation_4.jpg",
  224. "readwrite/testExifOrientation_5.jpg",
  225. "readwrite/testExifOrientation_6.jpg",
  226. "readwrite/testExifOrientation_7.jpg",
  227. "readwrite/testExifOrientation_8.jpg",
  228. #endif
  229. #ifdef OPENCV_IMGCODECS_PNG_WITH_EXIF
  230. "readwrite/testExifOrientation_1.png",
  231. "readwrite/testExifOrientation_2.png",
  232. "readwrite/testExifOrientation_3.png",
  233. "readwrite/testExifOrientation_4.png",
  234. "readwrite/testExifOrientation_5.png",
  235. "readwrite/testExifOrientation_6.png",
  236. "readwrite/testExifOrientation_7.png",
  237. "readwrite/testExifOrientation_8.png",
  238. #endif
  239. #ifdef HAVE_AVIF
  240. "readwrite/testExifOrientation_1.avif",
  241. "readwrite/testExifOrientation_2.avif",
  242. "readwrite/testExifOrientation_3.avif",
  243. "readwrite/testExifOrientation_4.avif",
  244. "readwrite/testExifOrientation_5.avif",
  245. "readwrite/testExifOrientation_6.avif",
  246. "readwrite/testExifOrientation_7.avif",
  247. "readwrite/testExifOrientation_8.avif",
  248. #endif
  249. #ifdef HAVE_WEBP
  250. "readwrite/testExifOrientation_1.webp",
  251. "readwrite/testExifOrientation_2.webp",
  252. "readwrite/testExifOrientation_3.webp",
  253. "readwrite/testExifOrientation_4.webp",
  254. "readwrite/testExifOrientation_5.webp",
  255. "readwrite/testExifOrientation_6.webp",
  256. "readwrite/testExifOrientation_7.webp",
  257. "readwrite/testExifOrientation_8.webp",
  258. #endif
  259. };
  260. INSTANTIATE_TEST_CASE_P(Imgcodecs, Exif,
  261. testing::ValuesIn(exif_files));
  262. #ifdef HAVE_AVIF
  263. typedef testing::TestWithParam<int> MatChannels;
  264. TEST_P(MatChannels, Imgcodecs_Avif_ReadWriteWithExif)
  265. {
  266. int avif_nbits = 10;
  267. int avif_speed = 10;
  268. int avif_quality = 85;
  269. int imgdepth = avif_nbits > 8 ? CV_16U : CV_8U;
  270. int imgtype = CV_MAKETYPE(imgdepth, GetParam());
  271. const string outputname = cv::tempfile(".avif");
  272. Mat img = makeCirclesImage(Size(1280, 720), imgtype, avif_nbits);
  273. std::vector<int> metadata_types = {IMAGE_METADATA_EXIF};
  274. std::vector<std::vector<uchar>> metadata = {
  275. getSampleExifData() };
  276. std::vector<int> write_params = {
  277. IMWRITE_AVIF_DEPTH, avif_nbits,
  278. IMWRITE_AVIF_SPEED, avif_speed,
  279. IMWRITE_AVIF_QUALITY, avif_quality
  280. };
  281. imwriteWithMetadata(outputname, img, metadata_types, metadata, write_params);
  282. std::vector<uchar> compressed;
  283. imencodeWithMetadata(outputname, img, metadata_types, metadata, compressed, write_params);
  284. std::vector<int> read_metadata_types, read_metadata_types2;
  285. std::vector<std::vector<uchar> > read_metadata, read_metadata2;
  286. Mat img2 = imreadWithMetadata(outputname, read_metadata_types, read_metadata, IMREAD_UNCHANGED);
  287. Mat img3 = imdecodeWithMetadata(compressed, read_metadata_types2, read_metadata2, IMREAD_UNCHANGED);
  288. EXPECT_EQ(img2.cols, img.cols);
  289. EXPECT_EQ(img2.rows, img.rows);
  290. EXPECT_EQ(img2.type(), imgtype);
  291. EXPECT_EQ(read_metadata_types, read_metadata_types2);
  292. ASSERT_GE(read_metadata_types.size(), 1u);
  293. EXPECT_EQ(read_metadata, read_metadata2);
  294. EXPECT_EQ(read_metadata_types[0], IMAGE_METADATA_EXIF);
  295. EXPECT_EQ(read_metadata_types.size(), read_metadata.size());
  296. EXPECT_EQ(read_metadata[0], metadata[0]);
  297. EXPECT_EQ(cv::norm(img2, img3, NORM_INF), 0.);
  298. double mse = cv::norm(img, img2, NORM_L2SQR)/(img.rows*img.cols);
  299. EXPECT_LT(mse, 1500);
  300. remove(outputname.c_str());
  301. }
  302. INSTANTIATE_TEST_CASE_P(Imgcodecs, MatChannels,
  303. testing::Values(1,3,4));
  304. #endif // HAVE_AVIF
  305. #ifdef HAVE_WEBP
  306. TEST(Imgcodecs_WebP, Read_Write_With_Exif_Xmp_Iccp)
  307. {
  308. int imgtype = CV_MAKETYPE(CV_8U, 3);
  309. const std::string outputname = cv::tempfile(".webp");
  310. cv::Mat img = makeCirclesImage(cv::Size(160, 120), imgtype, 8);
  311. std::vector<int> metadata_types = {IMAGE_METADATA_EXIF, IMAGE_METADATA_XMP, IMAGE_METADATA_ICCP};
  312. std::vector<std::vector<uchar>> metadata = {
  313. getSampleExifData(),
  314. getSampleXmpData(),
  315. getSampleIccpData()
  316. };
  317. int webp_quality = 101; // 101 is lossless compression
  318. std::vector<int> write_params = {IMWRITE_WEBP_QUALITY, webp_quality};
  319. imwriteWithMetadata(outputname, img, metadata_types, metadata, write_params);
  320. std::vector<uchar> compressed;
  321. imencodeWithMetadata(outputname, img, metadata_types, metadata, compressed, write_params);
  322. std::vector<int> read_metadata_types, read_metadata_types2;
  323. std::vector<std::vector<uchar>> read_metadata, read_metadata2;
  324. cv::Mat img2 = imreadWithMetadata(outputname, read_metadata_types, read_metadata, cv::IMREAD_UNCHANGED);
  325. cv::Mat img3 = imdecodeWithMetadata(compressed, read_metadata_types2, read_metadata2, cv::IMREAD_UNCHANGED);
  326. EXPECT_EQ(img2.cols, img.cols);
  327. EXPECT_EQ(img2.rows, img.rows);
  328. EXPECT_EQ(img2.type(), imgtype);
  329. EXPECT_EQ(read_metadata_types, read_metadata_types2);
  330. EXPECT_EQ(read_metadata_types.size(), 3u);
  331. EXPECT_EQ(read_metadata, read_metadata2);
  332. EXPECT_EQ(read_metadata, metadata);
  333. EXPECT_EQ(cv::norm(img2, img3, cv::NORM_INF), 0.0);
  334. double mse = cv::norm(img, img2, cv::NORM_L2SQR) / (img.rows * img.cols);
  335. EXPECT_EQ(mse, 0);
  336. remove(outputname.c_str());
  337. }
  338. #endif // HAVE_WEBP
  339. TEST(Imgcodecs_Jpeg, Read_Write_With_Exif)
  340. {
  341. int jpeg_quality = 95;
  342. int imgtype = CV_MAKETYPE(CV_8U, 3);
  343. const string outputname = cv::tempfile(".jpeg");
  344. Mat img = makeCirclesImage(Size(1280, 720), imgtype, 8);
  345. std::vector<int> metadata_types = {IMAGE_METADATA_EXIF};
  346. std::vector<std::vector<uchar>> metadata = {
  347. getSampleExifData() };
  348. std::vector<int> write_params = {
  349. IMWRITE_JPEG_QUALITY, jpeg_quality
  350. };
  351. imwriteWithMetadata(outputname, img, metadata_types, metadata, write_params);
  352. std::vector<uchar> compressed;
  353. imencodeWithMetadata(outputname, img, metadata_types, metadata, compressed, write_params);
  354. std::vector<int> read_metadata_types, read_metadata_types2;
  355. std::vector<std::vector<uchar> > read_metadata, read_metadata2;
  356. Mat img2 = imreadWithMetadata(outputname, read_metadata_types, read_metadata, IMREAD_UNCHANGED);
  357. Mat img3 = imdecodeWithMetadata(compressed, read_metadata_types2, read_metadata2, IMREAD_UNCHANGED);
  358. EXPECT_EQ(img2.cols, img.cols);
  359. EXPECT_EQ(img2.rows, img.rows);
  360. EXPECT_EQ(img2.type(), imgtype);
  361. EXPECT_EQ(read_metadata_types, read_metadata_types2);
  362. EXPECT_GE(read_metadata_types.size(), 1u);
  363. EXPECT_EQ(read_metadata, read_metadata2);
  364. EXPECT_EQ(read_metadata_types[0], IMAGE_METADATA_EXIF);
  365. EXPECT_EQ(read_metadata_types.size(), read_metadata.size());
  366. EXPECT_EQ(read_metadata[0], metadata[0]);
  367. EXPECT_EQ(cv::norm(img2, img3, NORM_INF), 0.);
  368. double mse = cv::norm(img, img2, NORM_L2SQR)/(img.rows*img.cols);
  369. EXPECT_LT(mse, 80);
  370. remove(outputname.c_str());
  371. }
  372. TEST(Imgcodecs_Png, Read_Write_With_Exif)
  373. {
  374. int png_compression = 3;
  375. int imgtype = CV_MAKETYPE(CV_8U, 3);
  376. const string outputname = cv::tempfile(".png");
  377. Mat img = makeCirclesImage(Size(160, 120), imgtype, 8);
  378. std::vector<int> metadata_types = {IMAGE_METADATA_EXIF};
  379. std::vector<std::vector<uchar>> metadata = {
  380. getSampleExifData() };
  381. std::vector<int> write_params = {
  382. IMWRITE_PNG_COMPRESSION, png_compression
  383. };
  384. imwriteWithMetadata(outputname, img, metadata_types, metadata, write_params);
  385. std::vector<uchar> compressed;
  386. imencodeWithMetadata(outputname, img, metadata_types, metadata, compressed, write_params);
  387. std::vector<int> read_metadata_types, read_metadata_types2;
  388. std::vector<std::vector<uchar> > read_metadata, read_metadata2;
  389. Mat img2 = imreadWithMetadata(outputname, read_metadata_types, read_metadata, IMREAD_UNCHANGED);
  390. Mat img3 = imdecodeWithMetadata(compressed, read_metadata_types2, read_metadata2, IMREAD_UNCHANGED);
  391. EXPECT_EQ(img2.cols, img.cols);
  392. EXPECT_EQ(img2.rows, img.rows);
  393. EXPECT_EQ(img2.type(), imgtype);
  394. EXPECT_EQ(read_metadata_types, read_metadata_types2);
  395. ASSERT_GE(read_metadata_types.size(), 1u);
  396. EXPECT_EQ(read_metadata, read_metadata2);
  397. EXPECT_EQ(read_metadata_types[0], IMAGE_METADATA_EXIF);
  398. EXPECT_EQ(read_metadata_types.size(), read_metadata.size());
  399. EXPECT_EQ(read_metadata[0], metadata[0]);
  400. EXPECT_EQ(cv::norm(img2, img3, NORM_INF), 0.);
  401. double mse = cv::norm(img, img2, NORM_L2SQR)/(img.rows*img.cols);
  402. EXPECT_EQ(mse, 0); // png is lossless
  403. remove(outputname.c_str());
  404. }
  405. TEST(Imgcodecs_Png, Read_Write_With_Exif_Xmp_Iccp)
  406. {
  407. int png_compression = 3;
  408. int imgtype = CV_MAKETYPE(CV_8U, 3);
  409. const string outputname = cv::tempfile(".png");
  410. Mat img = makeCirclesImage(Size(160, 120), imgtype, 8);
  411. std::vector<int> metadata_types = { IMAGE_METADATA_EXIF, IMAGE_METADATA_XMP, IMAGE_METADATA_ICCP };
  412. std::vector<std::vector<uchar>> metadata = {
  413. getSampleExifData(),
  414. getSampleXmpData(),
  415. getSampleIccpData(),
  416. };
  417. std::vector<int> write_params = {
  418. IMWRITE_PNG_COMPRESSION, png_compression
  419. };
  420. imwriteWithMetadata(outputname, img, metadata_types, metadata, write_params);
  421. std::vector<uchar> compressed;
  422. imencodeWithMetadata(outputname, img, metadata_types, metadata, compressed, write_params);
  423. std::vector<int> read_metadata_types, read_metadata_types2;
  424. std::vector<std::vector<uchar> > read_metadata, read_metadata2;
  425. Mat img2 = imreadWithMetadata(outputname, read_metadata_types, read_metadata, IMREAD_UNCHANGED);
  426. Mat img3 = imdecodeWithMetadata(compressed, read_metadata_types2, read_metadata2, IMREAD_UNCHANGED);
  427. EXPECT_EQ(img2.cols, img.cols);
  428. EXPECT_EQ(img2.rows, img.rows);
  429. EXPECT_EQ(img2.type(), imgtype);
  430. EXPECT_EQ(metadata_types, read_metadata_types);
  431. EXPECT_EQ(read_metadata_types, read_metadata_types2);
  432. EXPECT_EQ(metadata, read_metadata);
  433. remove(outputname.c_str());
  434. }
  435. TEST(Imgcodecs_Png, Read_Exif_From_Text)
  436. {
  437. const string root = cvtest::TS::ptr()->get_data_path();
  438. const string filename = root + "../perf/320x260.png";
  439. const string dst_file = cv::tempfile(".png");
  440. std::vector<uchar> exif_data =
  441. { 'M' , 'M' , 0, '*' , 0, 0, 0, 8, 0, 4, 1,
  442. 26, 0, 5, 0, 0, 0, 1, 0, 0, 0, 62, 1, 27, 0, 5, 0, 0, 0, 1, 0, 0, 0,
  443. 70, 1, 40, 0, 3, 0, 0, 0, 1, 0, 2, 0, 0, 1, 49, 0, 2, 0, 0, 0, 18, 0,
  444. 0, 0, 78, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 1, 0, 0, 0, 96, 0, 0, 0,
  445. 1, 80, 97, 105, 110, 116, 46, 78, 69, 84, 32, 118, 51, 46, 53, 46, 49, 48, 0
  446. };
  447. std::vector<int> read_metadata_types;
  448. std::vector<std::vector<uchar> > read_metadata;
  449. Mat img = imreadWithMetadata(filename, read_metadata_types, read_metadata, IMREAD_GRAYSCALE);
  450. std::vector<int> metadata_types = { IMAGE_METADATA_EXIF };
  451. EXPECT_EQ(read_metadata_types, metadata_types);
  452. EXPECT_EQ(read_metadata[0], exif_data);
  453. }
  454. static size_t locateString(const uchar* exif, size_t exif_size, const std::string& pattern)
  455. {
  456. size_t plen = pattern.size();
  457. for (size_t i = 0; i + plen <= exif_size; i++) {
  458. if (exif[i] == pattern[0] && memcmp(&exif[i], pattern.c_str(), plen) == 0)
  459. return i;
  460. }
  461. return 0xFFFFFFFFu;
  462. }
  463. typedef std::tuple<std::string, size_t, std::string, size_t, size_t, size_t> ReadExif_Sanity_Params;
  464. typedef testing::TestWithParam<ReadExif_Sanity_Params> ReadExif_Sanity;
  465. TEST_P(ReadExif_Sanity, Check)
  466. {
  467. std::string filename = get<0>(GetParam());
  468. size_t exif_size = get<1>(GetParam());
  469. std::string pattern = get<2>(GetParam());
  470. size_t ploc = get<3>(GetParam());
  471. size_t expected_xmp_size = get<4>(GetParam());
  472. size_t expected_iccp_size = get<5>(GetParam());
  473. const string root = cvtest::TS::ptr()->get_data_path();
  474. filename = root + filename;
  475. std::vector<int> metadata_types, metadata_types2;
  476. std::vector<std::vector<uchar> > metadata, metadata2;
  477. Mat img = imreadWithMetadata(filename, metadata_types, metadata);
  478. std::vector<uchar> compressed;
  479. imencodeWithMetadata(".jpg", img, metadata_types, metadata, compressed);
  480. img = imdecodeWithMetadata(compressed, metadata_types2, metadata2);
  481. EXPECT_EQ(metadata_types, metadata_types2);
  482. EXPECT_EQ(metadata, metadata2);
  483. EXPECT_EQ(img.type(), CV_8UC3);
  484. ASSERT_GE(metadata_types.size(), 1u);
  485. EXPECT_EQ(metadata_types.size(), metadata.size());
  486. const Mat exif = Mat(metadata[IMAGE_METADATA_EXIF]);
  487. EXPECT_EQ(exif.type(), CV_8U);
  488. EXPECT_EQ(exif.total(), exif_size);
  489. ASSERT_GE(exif_size, 26u); // minimal exif should take at least 26 bytes
  490. // (the header + IDF0 with at least 1 entry).
  491. EXPECT_TRUE(exif.data[0] == 'I' || exif.data[0] == 'M');
  492. EXPECT_EQ(exif.data[0], exif.data[1]);
  493. EXPECT_EQ(locateString(exif.data, exif_size, pattern), ploc);
  494. if (metadata_types.size() > IMAGE_METADATA_XMP)
  495. {
  496. const Mat xmp = Mat(metadata[IMAGE_METADATA_XMP]);
  497. EXPECT_EQ(xmp.type(), CV_8U);
  498. EXPECT_GT(xmp.total(), 0u);
  499. size_t xmp_size = xmp.total() * xmp.elemSize();
  500. EXPECT_EQ(expected_xmp_size, xmp_size);
  501. }
  502. if (metadata_types.size() > IMAGE_METADATA_ICCP)
  503. {
  504. const Mat iccp = Mat(metadata[IMAGE_METADATA_ICCP]);
  505. EXPECT_EQ(iccp.type(), CV_8U);
  506. EXPECT_GT(iccp.total(), 0u);
  507. size_t iccp_size = iccp.total() * iccp.elemSize();
  508. EXPECT_EQ(expected_iccp_size, iccp_size);
  509. }
  510. }
  511. static const std::vector<ReadExif_Sanity_Params> exif_sanity_params
  512. {
  513. #ifdef HAVE_JPEG
  514. ReadExif_Sanity_Params("readwrite/testExifOrientation_3.jpg", 916, "Photoshop", 120, 3597, 940),
  515. #endif
  516. #ifdef OPENCV_IMGCODECS_PNG_WITH_EXIF
  517. ReadExif_Sanity_Params("readwrite/testExifOrientation_5.png", 112, "ExifTool", 102, 505, 0),
  518. #endif
  519. #ifdef HAVE_AVIF
  520. ReadExif_Sanity_Params("readwrite/testExifOrientation_7.avif", 913, "Photoshop", 120, 3597, 940),
  521. #endif
  522. };
  523. INSTANTIATE_TEST_CASE_P(Imgcodecs, ReadExif_Sanity,
  524. testing::ValuesIn(exif_sanity_params));
  525. }}