From 8d0452ed5996a4b88046974ccabddca6e50d87df Mon Sep 17 00:00:00 2001 From: "andrey.morozov" Date: Sat, 29 Jun 2013 21:59:25 +0400 Subject: [PATCH] added test to save image with webp with alpha channel --- modules/highgui/src/grfmt_webp.cpp | 10 +++++++++- modules/highgui/test/test_grfmt.cpp | 23 ++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/modules/highgui/src/grfmt_webp.cpp b/modules/highgui/src/grfmt_webp.cpp index 3db4369fe..270262095 100644 --- a/modules/highgui/src/grfmt_webp.cpp +++ b/modules/highgui/src/grfmt_webp.cpp @@ -143,7 +143,15 @@ bool WebPDecoder::readData(Mat &img) uchar* out_data = img.data; unsigned int out_data_size = m_width * m_height * 3 * sizeof(uchar); - uchar *res_ptr = WebPDecodeBGRInto(data.data, data.total(), out_data, out_data_size, m_width * 3); + uchar *res_ptr = 0; + if (channels == 3) + { + res_ptr = WebPDecodeBGRInto(data.data, data.total(), out_data, out_data_size, img.step); + } + else if (channels == 4) + { + res_ptr = WebPDecodeBGRAInto(data.data, data.total(), out_data, out_data_size, img.step); + } if(res_ptr == out_data) { diff --git a/modules/highgui/test/test_grfmt.cpp b/modules/highgui/test/test_grfmt.cpp index 7fc80b2c1..d4e18dd2d 100644 --- a/modules/highgui/test/test_grfmt.cpp +++ b/modules/highgui/test/test_grfmt.cpp @@ -391,7 +391,7 @@ TEST(Highgui_WebP, encode_decode_lossy_webp) cv::Mat img = cv::imread(input); ASSERT_FALSE(img.empty()); - for(int q = 100; q>=0; q-=10) + for(int q = 100; q>=0; q-=20) { std::vector params; params.push_back(IMWRITE_WEBP_QUALITY); @@ -405,4 +405,25 @@ TEST(Highgui_WebP, encode_decode_lossy_webp) } } +TEST(Highgui_WebP, encode_decode_with_alpha_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::vector imgs; + cv::split(img, imgs); + imgs.push_back(cv::Mat(imgs[0])); + imgs[imgs.size() - 1] = cv::Scalar::all(128); + cv::merge(imgs, img); + + string output = cv::tempfile(".webp"); + + EXPECT_NO_THROW(cv::imwrite(output, img)); + cv::Mat img_webp = cv::imread(output); + remove(output.c_str()); + EXPECT_FALSE(img_webp.empty()); +} + #endif