Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgcodecs/src/grfmt_webp.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#ifdef HAVE_WEBP
44
45
#include "precomp.hpp"
46
47
#include <webp/decode.h>
48
#include <webp/encode.h>
49
50
#include <stdio.h>
51
#include <limits.h>
52
53
#include "grfmt_webp.hpp"
54
55
#include "opencv2/imgproc.hpp"
56
57
#include <opencv2/core/utils/configuration.private.hpp>
58
59
namespace cv
60
{
61
62
// 64Mb limit to avoid memory DDOS
63
static size_t param_maxFileSize = utils::getConfigurationParameterSizeT("OPENCV_IMGCODECS_WEBP_MAX_FILE_SIZE", 64*1024*1024);
64
65
static const size_t WEBP_HEADER_SIZE = 32;
66
67
WebPDecoder::WebPDecoder()
68
{
69
m_buf_supported = true;
70
channels = 0;
71
fs_size = 0;
72
}
73
74
WebPDecoder::~WebPDecoder() {}
75
76
size_t WebPDecoder::signatureLength() const
77
{
78
return WEBP_HEADER_SIZE;
79
}
80
81
bool WebPDecoder::checkSignature(const String & signature) const
82
{
83
bool ret = false;
84
85
if(signature.size() >= WEBP_HEADER_SIZE)
86
{
87
WebPBitstreamFeatures features;
88
if(VP8_STATUS_OK == WebPGetFeatures((uint8_t *)signature.c_str(),
89
WEBP_HEADER_SIZE, &features))
90
{
91
ret = true;
92
}
93
}
94
95
return ret;
96
}
97
98
ImageDecoder WebPDecoder::newDecoder() const
99
{
100
return makePtr<WebPDecoder>();
101
}
102
103
bool WebPDecoder::readHeader()
104
{
105
uint8_t header[WEBP_HEADER_SIZE] = { 0 };
106
if (m_buf.empty())
107
{
108
fs.open(m_filename.c_str(), std::ios::binary);
109
fs.seekg(0, std::ios::end);
110
fs_size = safeCastToSizeT(fs.tellg(), "File is too large");
111
fs.seekg(0, std::ios::beg);
112
CV_Assert(fs && "File stream error");
113
CV_CheckGE(fs_size, WEBP_HEADER_SIZE, "File is too small");
114
CV_CheckLE(fs_size, param_maxFileSize, "File is too large. Increase OPENCV_IMGCODECS_WEBP_MAX_FILE_SIZE parameter if you want to process large files");
115
116
fs.read((char*)header, sizeof(header));
117
CV_Assert(fs && "Can't read WEBP_HEADER_SIZE bytes");
118
}
119
else
120
{
121
CV_CheckGE(m_buf.total(), WEBP_HEADER_SIZE, "Buffer is too small");
122
memcpy(header, m_buf.ptr(), sizeof(header));
123
data = m_buf;
124
}
125
126
WebPBitstreamFeatures features;
127
if (VP8_STATUS_OK == WebPGetFeatures(header, sizeof(header), &features))
128
{
129
m_width = features.width;
130
m_height = features.height;
131
132
if (features.has_alpha)
133
{
134
m_type = CV_8UC4;
135
channels = 4;
136
}
137
else
138
{
139
m_type = CV_8UC3;
140
channels = 3;
141
}
142
143
return true;
144
}
145
146
return false;
147
}
148
149
bool WebPDecoder::readData(Mat &img)
150
{
151
CV_CheckGE(m_width, 0, ""); CV_CheckGE(m_height, 0, "");
152
153
CV_CheckEQ(img.cols, m_width, "");
154
CV_CheckEQ(img.rows, m_height, "");
155
156
if (m_buf.empty())
157
{
158
fs.seekg(0, std::ios::beg); CV_Assert(fs && "File stream error");
159
data.create(1, validateToInt(fs_size), CV_8UC1);
160
fs.read((char*)data.ptr(), fs_size);
161
CV_Assert(fs && "Can't read file data");
162
fs.close();
163
}
164
CV_Assert(data.type() == CV_8UC1); CV_Assert(data.rows == 1);
165
166
{
167
Mat read_img;
168
CV_CheckType(img.type(), img.type() == CV_8UC1 || img.type() == CV_8UC3 || img.type() == CV_8UC4, "");
169
if (img.type() != m_type)
170
{
171
read_img.create(m_height, m_width, m_type);
172
}
173
else
174
{
175
read_img = img; // copy header
176
}
177
178
uchar* out_data = read_img.ptr();
179
size_t out_data_size = read_img.dataend - out_data;
180
181
uchar *res_ptr = NULL;
182
if (channels == 3)
183
{
184
CV_CheckTypeEQ(read_img.type(), CV_8UC3, "");
185
res_ptr = WebPDecodeBGRInto(data.ptr(), data.total(), out_data,
186
(int)out_data_size, (int)read_img.step);
187
}
188
else if (channels == 4)
189
{
190
CV_CheckTypeEQ(read_img.type(), CV_8UC4, "");
191
res_ptr = WebPDecodeBGRAInto(data.ptr(), data.total(), out_data,
192
(int)out_data_size, (int)read_img.step);
193
}
194
195
if (res_ptr != out_data)
196
return false;
197
198
if (read_img.data == img.data && img.type() == m_type)
199
{
200
// nothing
201
}
202
else if (img.type() == CV_8UC1)
203
{
204
cvtColor(read_img, img, COLOR_BGR2GRAY);
205
}
206
else if (img.type() == CV_8UC3 && m_type == CV_8UC4)
207
{
208
cvtColor(read_img, img, COLOR_BGRA2BGR);
209
}
210
else if (img.type() == CV_8UC4 && m_type == CV_8UC3)
211
{
212
cvtColor(read_img, img, COLOR_BGR2BGRA);
213
}
214
else
215
{
216
CV_Error(Error::StsInternal, "");
217
}
218
}
219
return true;
220
}
221
222
WebPEncoder::WebPEncoder()
223
{
224
m_description = "WebP files (*.webp)";
225
m_buf_supported = true;
226
}
227
228
WebPEncoder::~WebPEncoder() { }
229
230
ImageEncoder WebPEncoder::newEncoder() const
231
{
232
return makePtr<WebPEncoder>();
233
}
234
235
bool WebPEncoder::write(const Mat& img, const std::vector<int>& params)
236
{
237
CV_CheckDepthEQ(img.depth(), CV_8U, "WebP codec supports 8U images only");
238
239
const int width = img.cols, height = img.rows;
240
241
bool comp_lossless = true;
242
float quality = 100.0f;
243
244
if (params.size() > 1)
245
{
246
if (params[0] == CV_IMWRITE_WEBP_QUALITY)
247
{
248
comp_lossless = false;
249
quality = static_cast<float>(params[1]);
250
if (quality < 1.0f)
251
{
252
quality = 1.0f;
253
}
254
if (quality > 100.0f)
255
{
256
comp_lossless = true;
257
}
258
}
259
}
260
261
int channels = img.channels();
262
CV_Check(channels, channels == 1 || channels == 3 || channels == 4, "");
263
264
const Mat *image = &img;
265
Mat temp;
266
267
if (channels == 1)
268
{
269
cvtColor(*image, temp, CV_GRAY2BGR);
270
image = &temp;
271
channels = 3;
272
}
273
274
uint8_t *out = NULL;
275
size_t size = 0;
276
if (comp_lossless)
277
{
278
if (channels == 3)
279
{
280
size = WebPEncodeLosslessBGR(image->ptr(), width, height, (int)image->step, &out);
281
}
282
else if (channels == 4)
283
{
284
size = WebPEncodeLosslessBGRA(image->ptr(), width, height, (int)image->step, &out);
285
}
286
}
287
else
288
{
289
if (channels == 3)
290
{
291
size = WebPEncodeBGR(image->ptr(), width, height, (int)image->step, quality, &out);
292
}
293
else if (channels == 4)
294
{
295
size = WebPEncodeBGRA(image->ptr(), width, height, (int)image->step, quality, &out);
296
}
297
}
298
#if WEBP_DECODER_ABI_VERSION >= 0x0206
299
Ptr<uint8_t> out_cleaner(out, WebPFree);
300
#else
301
Ptr<uint8_t> out_cleaner(out, free);
302
#endif
303
304
CV_Assert(size > 0);
305
306
if (m_buf)
307
{
308
m_buf->resize(size);
309
memcpy(&(*m_buf)[0], out, size);
310
}
311
else
312
{
313
FILE *fd = fopen(m_filename.c_str(), "wb");
314
if (fd != NULL)
315
{
316
fwrite(out, size, sizeof(uint8_t), fd);
317
fclose(fd); fd = NULL;
318
}
319
}
320
321
return size > 0;
322
}
323
324
}
325
326
#endif
327
328