From eb012a599303b271f17a811cd730696f8a290fd7 Mon Sep 17 00:00:00 2001 From: AoD314 Date: Thu, 7 Mar 2013 19:26:44 +0400 Subject: [PATCH] added tests for WebP --- modules/highgui/test/test_grfmt.cpp | 91 +++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/modules/highgui/test/test_grfmt.cpp b/modules/highgui/test/test_grfmt.cpp index 09926e1f38..43198f5af7 100644 --- a/modules/highgui/test/test_grfmt.cpp +++ b/modules/highgui/test/test_grfmt.cpp @@ -332,3 +332,94 @@ TEST(Highgui_Tiff, decode_tile16384x16384) remove(file4.c_str()); } #endif + +#ifdef HAVE_WEBP + +TEST(Highgui_WebP, encode_decode_lossless_webp) +{ + cvtest::TS& ts = *cvtest::TS::ptr(); + std::string input = std::string(ts.get_data_path()) + "../cv/shared/lena.png"; + cv::Mat img = cv::imread(input); + ASSERT_FALSE(img.empty()); + + std::string output = cv::tempfile(".webp"); + EXPECT_NO_THROW(cv::imwrite(output, img)); // lossless + + cv::Mat img_webp = cv::imread(output); + + std::vector buf; + + FILE * wfile = NULL; + + wfile = fopen(output.c_str(), "rb"); + if (wfile != NULL) + { + fseek(wfile, 0, SEEK_END); + size_t wfile_size = ftell(wfile); + fseek(wfile, 0, SEEK_SET); + + buf.resize(wfile_size); + + size_t data_size = fread(&buf[0], 1, wfile_size, wfile); + + if(wfile) + { + fclose(wfile); + } + + if (data_size != wfile_size) + { + EXPECT_TRUE(false); + } + } + + cv::Mat decode = cv::imdecode(buf, CV_LOAD_IMAGE_COLOR); + ASSERT_FALSE(decode.empty()); + EXPECT_TRUE(cv::norm(decode, img_webp, NORM_INF) == 0); + + ASSERT_FALSE(img_webp.empty()); + + EXPECT_TRUE(cv::norm(img, img_webp, NORM_INF) == 0); +} + +TEST(Highgui_WebP, encode_decode_lossy_webp) +{ + cvtest::TS& ts = *cvtest::TS::ptr(); + std::string input = std::string(ts.get_data_path()) + "/../cv/shared/lena.png"; + cv::Mat img = cv::imread(input); + ASSERT_FALSE(img.empty()); + + for(int q = 100; q>=0; q-=5) + { + std::vector params; + params.push_back(CV_IMWRITE_WEBP_QUALITY); + params.push_back(q); + string output = cv::tempfile(".webp"); + + EXPECT_NO_THROW(cv::imwrite(output, img, params)); + cv::Mat img_webp = cv::imread(output); + EXPECT_FALSE(img_webp.empty()); + } +} + +TEST(Highgui_WebP, encode_big_image_webp) +{ + cvtest::TS& ts = *cvtest::TS::ptr(); + std::string input = std::string(ts.get_data_path()) + "/../cv/shared/lena.png"; + cv::Mat img = cv::imread(input); + cv::resize(img, img, cv::Size(8192, 8192)); + + std::vector params; + params.push_back(CV_IMWRITE_WEBP_QUALITY); + params.push_back(95); + + std::string output = cv::tempfile(".webp"); + EXPECT_NO_THROW(cv::imwrite(output, img, params)); + + img.create(10000, 10000, CV_8UC3); + cv::randu(img, 0, 256); + output = cv::tempfile(".webp"); + EXPECT_NO_THROW(cv::imwrite(output, img)); +} + +#endif