Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgcodecs/test/test_webp.cpp
16354 views
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
6
namespace opencv_test { namespace {
7
8
#ifdef HAVE_WEBP
9
10
TEST(Imgcodecs_WebP, encode_decode_lossless_webp)
11
{
12
const string root = cvtest::TS::ptr()->get_data_path();
13
string filename = root + "../cv/shared/lena.png";
14
cv::Mat img = cv::imread(filename);
15
ASSERT_FALSE(img.empty());
16
17
string output = cv::tempfile(".webp");
18
EXPECT_NO_THROW(cv::imwrite(output, img)); // lossless
19
20
cv::Mat img_webp = cv::imread(output);
21
22
std::vector<unsigned char> buf;
23
24
FILE * wfile = NULL;
25
26
wfile = fopen(output.c_str(), "rb");
27
if (wfile != NULL)
28
{
29
fseek(wfile, 0, SEEK_END);
30
size_t wfile_size = ftell(wfile);
31
fseek(wfile, 0, SEEK_SET);
32
33
buf.resize(wfile_size);
34
35
size_t data_size = fread(&buf[0], 1, wfile_size, wfile);
36
37
if(wfile)
38
{
39
fclose(wfile);
40
}
41
42
if (data_size != wfile_size)
43
{
44
EXPECT_TRUE(false);
45
}
46
}
47
48
EXPECT_EQ(0, remove(output.c_str()));
49
50
cv::Mat decode = cv::imdecode(buf, IMREAD_COLOR);
51
ASSERT_FALSE(decode.empty());
52
EXPECT_TRUE(cvtest::norm(decode, img_webp, NORM_INF) == 0);
53
54
ASSERT_FALSE(img_webp.empty());
55
56
EXPECT_TRUE(cvtest::norm(img, img_webp, NORM_INF) == 0);
57
}
58
59
TEST(Imgcodecs_WebP, encode_decode_lossy_webp)
60
{
61
const string root = cvtest::TS::ptr()->get_data_path();
62
std::string input = root + "../cv/shared/lena.png";
63
cv::Mat img = cv::imread(input);
64
ASSERT_FALSE(img.empty());
65
66
for(int q = 100; q>=0; q-=20)
67
{
68
std::vector<int> params;
69
params.push_back(IMWRITE_WEBP_QUALITY);
70
params.push_back(q);
71
string output = cv::tempfile(".webp");
72
73
EXPECT_NO_THROW(cv::imwrite(output, img, params));
74
cv::Mat img_webp = cv::imread(output);
75
EXPECT_EQ(0, remove(output.c_str()));
76
EXPECT_FALSE(img_webp.empty());
77
EXPECT_EQ(3, img_webp.channels());
78
EXPECT_EQ(512, img_webp.cols);
79
EXPECT_EQ(512, img_webp.rows);
80
}
81
}
82
83
TEST(Imgcodecs_WebP, encode_decode_with_alpha_webp)
84
{
85
const string root = cvtest::TS::ptr()->get_data_path();
86
std::string input = root + "../cv/shared/lena.png";
87
cv::Mat img = cv::imread(input);
88
ASSERT_FALSE(img.empty());
89
90
std::vector<cv::Mat> imgs;
91
cv::split(img, imgs);
92
imgs.push_back(cv::Mat(imgs[0]));
93
imgs[imgs.size() - 1] = cv::Scalar::all(128);
94
cv::merge(imgs, img);
95
96
string output = cv::tempfile(".webp");
97
98
EXPECT_NO_THROW(cv::imwrite(output, img));
99
cv::Mat img_webp = cv::imread(output, IMREAD_UNCHANGED);
100
cv::Mat img_webp_bgr = cv::imread(output); // IMREAD_COLOR by default
101
EXPECT_EQ(0, remove(output.c_str()));
102
EXPECT_FALSE(img_webp.empty());
103
EXPECT_EQ(4, img_webp.channels());
104
EXPECT_EQ(512, img_webp.cols);
105
EXPECT_EQ(512, img_webp.rows);
106
EXPECT_FALSE(img_webp_bgr.empty());
107
EXPECT_EQ(3, img_webp_bgr.channels());
108
EXPECT_EQ(512, img_webp_bgr.cols);
109
EXPECT_EQ(512, img_webp_bgr.rows);
110
}
111
112
#endif // HAVE_WEBP
113
114
}} // namespace
115
116