test_png.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 directory
  3. // of this distribution and at http://opencv.org/license.html
  4. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. #ifdef HAVE_PNG
  7. TEST(Imgcodecs_Png, write_big)
  8. {
  9. const string root = cvtest::TS::ptr()->get_data_path();
  10. const string filename = root + "readwrite/read.png";
  11. const string dst_file = cv::tempfile(".png");
  12. Mat img;
  13. ASSERT_NO_THROW(img = imread(filename));
  14. ASSERT_FALSE(img.empty());
  15. EXPECT_EQ(13043, img.cols);
  16. EXPECT_EQ(13917, img.rows);
  17. ASSERT_NO_THROW(imwrite(dst_file, img));
  18. EXPECT_EQ(0, remove(dst_file.c_str()));
  19. }
  20. TEST(Imgcodecs_Png, encode)
  21. {
  22. vector<uchar> buff;
  23. Mat img_gt = Mat::zeros(1000, 1000, CV_8U);
  24. vector<int> param;
  25. param.push_back(IMWRITE_PNG_COMPRESSION);
  26. param.push_back(3); //default(3) 0-9.
  27. EXPECT_NO_THROW(imencode(".png", img_gt, buff, param));
  28. Mat img;
  29. EXPECT_NO_THROW(img = imdecode(buff, IMREAD_ANYDEPTH)); // hang
  30. EXPECT_FALSE(img.empty());
  31. EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), img, img_gt);
  32. }
  33. TEST(Imgcodecs_Png, regression_ImreadVSCvtColor)
  34. {
  35. const string root = cvtest::TS::ptr()->get_data_path();
  36. const string imgName = root + "../cv/shared/lena.png";
  37. Mat original_image = imread(imgName);
  38. Mat gray_by_codec = imread(imgName, IMREAD_GRAYSCALE);
  39. Mat gray_by_cvt;
  40. cvtColor(original_image, gray_by_cvt, COLOR_BGR2GRAY);
  41. Mat diff;
  42. absdiff(gray_by_codec, gray_by_cvt, diff);
  43. EXPECT_LT(cvtest::mean(diff)[0], 1.);
  44. EXPECT_PRED_FORMAT2(cvtest::MatComparator(10, 0), gray_by_codec, gray_by_cvt);
  45. }
  46. // Test OpenCV issue 3075 is solved
  47. TEST(Imgcodecs_Png, read_color_palette_with_alpha)
  48. {
  49. const string root = cvtest::TS::ptr()->get_data_path();
  50. Mat img;
  51. // First Test : Read PNG with alpha, imread flag -1
  52. img = imread(root + "readwrite/color_palette_alpha.png", IMREAD_UNCHANGED);
  53. ASSERT_FALSE(img.empty());
  54. ASSERT_TRUE(img.channels() == 4);
  55. // pixel is red in BGRA
  56. EXPECT_EQ(img.at<Vec4b>(0, 0), Vec4b(0, 0, 255, 255));
  57. EXPECT_EQ(img.at<Vec4b>(0, 1), Vec4b(0, 0, 255, 255));
  58. // Second Test : Read PNG without alpha, imread flag -1
  59. img = imread(root + "readwrite/color_palette_no_alpha.png", IMREAD_UNCHANGED);
  60. ASSERT_FALSE(img.empty());
  61. ASSERT_TRUE(img.channels() == 3);
  62. // pixel is red in BGR
  63. EXPECT_EQ(img.at<Vec3b>(0, 0), Vec3b(0, 0, 255));
  64. EXPECT_EQ(img.at<Vec3b>(0, 1), Vec3b(0, 0, 255));
  65. // Third Test : Read PNG with alpha, imread flag 1
  66. img = imread(root + "readwrite/color_palette_alpha.png", IMREAD_COLOR);
  67. ASSERT_FALSE(img.empty());
  68. ASSERT_TRUE(img.channels() == 3);
  69. // pixel is red in BGR
  70. EXPECT_EQ(img.at<Vec3b>(0, 0), Vec3b(0, 0, 255));
  71. EXPECT_EQ(img.at<Vec3b>(0, 1), Vec3b(0, 0, 255));
  72. // Fourth Test : Read PNG without alpha, imread flag 1
  73. img = imread(root + "readwrite/color_palette_no_alpha.png", IMREAD_COLOR);
  74. ASSERT_FALSE(img.empty());
  75. ASSERT_TRUE(img.channels() == 3);
  76. // pixel is red in BGR
  77. EXPECT_EQ(img.at<Vec3b>(0, 0), Vec3b(0, 0, 255));
  78. EXPECT_EQ(img.at<Vec3b>(0, 1), Vec3b(0, 0, 255));
  79. }
  80. /**
  81. * Test for check whether reading exif orientation tag was processed successfully or not
  82. * The test info is the set of 8 images named testExifRotate_{1 to 8}.png
  83. * The test image is the square 10x10 points divided by four sub-squares:
  84. * (R corresponds to Red, G to Green, B to Blue, W to white)
  85. * --------- ---------
  86. * | R | G | | G | R |
  87. * |-------| - (tag 1) |-------| - (tag 2)
  88. * | B | W | | W | B |
  89. * --------- ---------
  90. *
  91. * --------- ---------
  92. * | W | B | | B | W |
  93. * |-------| - (tag 3) |-------| - (tag 4)
  94. * | G | R | | R | G |
  95. * --------- ---------
  96. *
  97. * --------- ---------
  98. * | R | B | | G | W |
  99. * |-------| - (tag 5) |-------| - (tag 6)
  100. * | G | W | | R | B |
  101. * --------- ---------
  102. *
  103. * --------- ---------
  104. * | W | G | | B | R |
  105. * |-------| - (tag 7) |-------| - (tag 8)
  106. * | B | R | | W | G |
  107. * --------- ---------
  108. *
  109. *
  110. * Every image contains exif field with orientation tag (0x112)
  111. * After reading each image and applying the orientation tag,
  112. * the resulting image should be:
  113. * ---------
  114. * | R | G |
  115. * |-------|
  116. * | B | W |
  117. * ---------
  118. *
  119. */
  120. typedef testing::TestWithParam<string> Imgcodecs_PNG_Exif;
  121. // Solution to issue 16579: PNG read doesn't support Exif orientation data
  122. #ifdef OPENCV_IMGCODECS_PNG_WITH_EXIF
  123. TEST_P(Imgcodecs_PNG_Exif, exif_orientation)
  124. #else
  125. TEST_P(Imgcodecs_PNG_Exif, DISABLED_exif_orientation)
  126. #endif
  127. {
  128. const string root = cvtest::TS::ptr()->get_data_path();
  129. const string filename = root + GetParam();
  130. const int colorThresholdHigh = 250;
  131. const int colorThresholdLow = 5;
  132. Mat m_img = imread(filename);
  133. ASSERT_FALSE(m_img.empty());
  134. Vec3b vec;
  135. //Checking the first quadrant (with supposed red)
  136. vec = m_img.at<Vec3b>(2, 2); //some point inside the square
  137. EXPECT_LE(vec.val[0], colorThresholdLow);
  138. EXPECT_LE(vec.val[1], colorThresholdLow);
  139. EXPECT_GE(vec.val[2], colorThresholdHigh);
  140. //Checking the second quadrant (with supposed green)
  141. vec = m_img.at<Vec3b>(2, 7); //some point inside the square
  142. EXPECT_LE(vec.val[0], colorThresholdLow);
  143. EXPECT_GE(vec.val[1], colorThresholdHigh);
  144. EXPECT_LE(vec.val[2], colorThresholdLow);
  145. //Checking the third quadrant (with supposed blue)
  146. vec = m_img.at<Vec3b>(7, 2); //some point inside the square
  147. EXPECT_GE(vec.val[0], colorThresholdHigh);
  148. EXPECT_LE(vec.val[1], colorThresholdLow);
  149. EXPECT_LE(vec.val[2], colorThresholdLow);
  150. }
  151. const string exif_files[] =
  152. {
  153. "readwrite/testExifOrientation_1.png",
  154. "readwrite/testExifOrientation_2.png",
  155. "readwrite/testExifOrientation_3.png",
  156. "readwrite/testExifOrientation_4.png",
  157. "readwrite/testExifOrientation_5.png",
  158. "readwrite/testExifOrientation_6.png",
  159. "readwrite/testExifOrientation_7.png",
  160. "readwrite/testExifOrientation_8.png"
  161. };
  162. INSTANTIATE_TEST_CASE_P(ExifFiles, Imgcodecs_PNG_Exif,
  163. testing::ValuesIn(exif_files));
  164. #endif // HAVE_PNG
  165. }} // namespace