test_webp.cpp 3.2 KB

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