test_webp.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_WEBP
  7. static void readFileBytes(const std::string& fname, std::vector<unsigned char>& buf)
  8. {
  9. FILE * wfile = fopen(fname.c_str(), "rb");
  10. if (wfile != NULL)
  11. {
  12. fseek(wfile, 0, SEEK_END);
  13. size_t wfile_size = ftell(wfile);
  14. fseek(wfile, 0, SEEK_SET);
  15. buf.resize(wfile_size);
  16. size_t data_size = fread(&buf[0], 1, wfile_size, wfile);
  17. if(wfile)
  18. {
  19. fclose(wfile);
  20. }
  21. EXPECT_EQ(data_size, wfile_size);
  22. }
  23. }
  24. TEST(Imgcodecs_WebP, encode_decode_lossless_webp)
  25. {
  26. const string root = cvtest::TS::ptr()->get_data_path();
  27. string filename = root + "../cv/shared/lena.png";
  28. cv::Mat img = cv::imread(filename);
  29. ASSERT_FALSE(img.empty());
  30. string output = cv::tempfile(".webp");
  31. EXPECT_NO_THROW(cv::imwrite(output, img)); // lossless
  32. cv::Mat img_webp = cv::imread(output);
  33. std::vector<unsigned char> buf;
  34. readFileBytes(output, buf);
  35. EXPECT_EQ(0, remove(output.c_str()));
  36. cv::Mat decode = cv::imdecode(buf, IMREAD_COLOR);
  37. ASSERT_FALSE(decode.empty());
  38. EXPECT_TRUE(cvtest::norm(decode, img_webp, NORM_INF) == 0);
  39. cv::Mat decode_rgb = cv::imdecode(buf, IMREAD_COLOR_RGB);
  40. ASSERT_FALSE(decode_rgb.empty());
  41. cvtColor(decode_rgb, decode_rgb, COLOR_RGB2BGR);
  42. EXPECT_TRUE(cvtest::norm(decode_rgb, img_webp, NORM_INF) == 0);
  43. ASSERT_FALSE(img_webp.empty());
  44. EXPECT_TRUE(cvtest::norm(img, img_webp, NORM_INF) == 0);
  45. }
  46. TEST(Imgcodecs_WebP, encode_decode_lossy_webp)
  47. {
  48. const string root = cvtest::TS::ptr()->get_data_path();
  49. std::string input = root + "../cv/shared/lena.png";
  50. cv::Mat img = cv::imread(input);
  51. ASSERT_FALSE(img.empty());
  52. for(int q = 100; q>=0; q-=20)
  53. {
  54. std::vector<int> params;
  55. params.push_back(IMWRITE_WEBP_QUALITY);
  56. params.push_back(MAX(q,1));
  57. string output = cv::tempfile(".webp");
  58. EXPECT_NO_THROW(cv::imwrite(output, img, params));
  59. cv::Mat img_webp = cv::imread(output);
  60. EXPECT_EQ(0, remove(output.c_str()));
  61. EXPECT_FALSE(img_webp.empty());
  62. EXPECT_EQ(3, img_webp.channels());
  63. EXPECT_EQ(512, img_webp.cols);
  64. EXPECT_EQ(512, img_webp.rows);
  65. }
  66. }
  67. TEST(Imgcodecs_WebP, encode_decode_with_alpha_webp)
  68. {
  69. const string root = cvtest::TS::ptr()->get_data_path();
  70. std::string input = root + "../cv/shared/lena.png";
  71. cv::Mat img = cv::imread(input);
  72. ASSERT_FALSE(img.empty());
  73. std::vector<cv::Mat> imgs;
  74. cv::split(img, imgs);
  75. imgs.push_back(cv::Mat(imgs[0]));
  76. imgs[imgs.size() - 1] = cv::Scalar::all(128);
  77. cv::merge(imgs, img);
  78. string output = cv::tempfile(".webp");
  79. EXPECT_NO_THROW(cv::imwrite(output, img));
  80. cv::Mat img_webp = cv::imread(output, IMREAD_UNCHANGED);
  81. cv::Mat img_webp_bgr = cv::imread(output); // IMREAD_COLOR by default
  82. EXPECT_EQ(0, remove(output.c_str()));
  83. EXPECT_FALSE(img_webp.empty());
  84. EXPECT_EQ(4, img_webp.channels());
  85. EXPECT_EQ(512, img_webp.cols);
  86. EXPECT_EQ(512, img_webp.rows);
  87. EXPECT_FALSE(img_webp_bgr.empty());
  88. EXPECT_EQ(3, img_webp_bgr.channels());
  89. EXPECT_EQ(512, img_webp_bgr.cols);
  90. EXPECT_EQ(512, img_webp_bgr.rows);
  91. }
  92. #endif // HAVE_WEBP
  93. }} // namespace