2013-03-07 23:29:19 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#ifdef HAVE_WEBP
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
|
|
#include <webp/decode.h>
|
|
|
|
#include <webp/encode.h>
|
2013-06-30 02:00:26 +08:00
|
|
|
|
2013-03-07 23:29:19 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "grfmt_webp.hpp"
|
|
|
|
|
2013-03-13 20:22:44 +08:00
|
|
|
#include "opencv2/imgproc.hpp"
|
2013-03-07 23:29:19 +08:00
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
#include <opencv2/core/utils/configuration.private.hpp>
|
2013-06-30 02:00:26 +08:00
|
|
|
|
2013-03-07 23:29:19 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
// 64Mb limit to avoid memory DDOS
|
|
|
|
static size_t param_maxFileSize = utils::getConfigurationParameterSizeT("OPENCV_IMGCODECS_WEBP_MAX_FILE_SIZE", 64*1024*1024);
|
|
|
|
|
|
|
|
static const size_t WEBP_HEADER_SIZE = 32;
|
|
|
|
|
2013-03-07 23:29:19 +08:00
|
|
|
WebPDecoder::WebPDecoder()
|
|
|
|
{
|
|
|
|
m_buf_supported = true;
|
2017-06-26 19:09:21 +08:00
|
|
|
channels = 0;
|
2018-08-31 03:12:01 +08:00
|
|
|
fs_size = 0;
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
|
|
|
|
2013-06-30 02:00:26 +08:00
|
|
|
WebPDecoder::~WebPDecoder() {}
|
2013-03-07 23:29:19 +08:00
|
|
|
|
2013-06-30 02:00:26 +08:00
|
|
|
size_t WebPDecoder::signatureLength() const
|
2013-03-07 23:29:19 +08:00
|
|
|
{
|
2013-06-30 02:00:26 +08:00
|
|
|
return WEBP_HEADER_SIZE;
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
|
|
|
|
2013-06-30 02:00:26 +08:00
|
|
|
bool WebPDecoder::checkSignature(const String & signature) const
|
2013-03-07 23:29:19 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
2013-06-30 02:00:26 +08:00
|
|
|
if(signature.size() >= WEBP_HEADER_SIZE)
|
2013-03-07 23:29:19 +08:00
|
|
|
{
|
2013-06-30 02:00:26 +08:00
|
|
|
WebPBitstreamFeatures features;
|
|
|
|
if(VP8_STATUS_OK == WebPGetFeatures((uint8_t *)signature.c_str(),
|
|
|
|
WEBP_HEADER_SIZE, &features))
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
}
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-30 02:00:26 +08:00
|
|
|
ImageDecoder WebPDecoder::newDecoder() const
|
|
|
|
{
|
2013-08-13 20:47:18 +08:00
|
|
|
return makePtr<WebPDecoder>();
|
2013-06-30 02:00:26 +08:00
|
|
|
}
|
|
|
|
|
2013-03-07 23:29:19 +08:00
|
|
|
bool WebPDecoder::readHeader()
|
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
uint8_t header[WEBP_HEADER_SIZE] = { 0 };
|
2013-03-07 23:29:19 +08:00
|
|
|
if (m_buf.empty())
|
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
fs.open(m_filename.c_str(), std::ios::binary);
|
|
|
|
fs.seekg(0, std::ios::end);
|
2018-09-02 17:53:41 +08:00
|
|
|
fs_size = safeCastToSizeT(fs.tellg(), "File is too large");
|
2018-08-31 03:12:01 +08:00
|
|
|
fs.seekg(0, std::ios::beg);
|
|
|
|
CV_Assert(fs && "File stream error");
|
|
|
|
CV_CheckGE(fs_size, WEBP_HEADER_SIZE, "File is too small");
|
|
|
|
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");
|
|
|
|
|
|
|
|
fs.read((char*)header, sizeof(header));
|
|
|
|
CV_Assert(fs && "Can't read WEBP_HEADER_SIZE bytes");
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
CV_CheckGE(m_buf.total(), WEBP_HEADER_SIZE, "Buffer is too small");
|
|
|
|
memcpy(header, m_buf.ptr(), sizeof(header));
|
2013-03-07 23:29:19 +08:00
|
|
|
data = m_buf;
|
|
|
|
}
|
|
|
|
|
2013-06-30 02:00:26 +08:00
|
|
|
WebPBitstreamFeatures features;
|
2018-08-31 03:12:01 +08:00
|
|
|
if (VP8_STATUS_OK == WebPGetFeatures(header, sizeof(header), &features))
|
2013-03-07 23:29:19 +08:00
|
|
|
{
|
2013-06-30 02:00:26 +08:00
|
|
|
m_width = features.width;
|
|
|
|
m_height = features.height;
|
|
|
|
|
|
|
|
if (features.has_alpha)
|
|
|
|
{
|
|
|
|
m_type = CV_8UC4;
|
|
|
|
channels = 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_type = CV_8UC3;
|
|
|
|
channels = 3;
|
|
|
|
}
|
|
|
|
|
2013-03-07 23:29:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebPDecoder::readData(Mat &img)
|
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
CV_CheckGE(m_width, 0, ""); CV_CheckGE(m_height, 0, "");
|
|
|
|
|
|
|
|
CV_CheckEQ(img.cols, m_width, "");
|
|
|
|
CV_CheckEQ(img.rows, m_height, "");
|
|
|
|
|
|
|
|
if (m_buf.empty())
|
2013-03-07 23:29:19 +08:00
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
fs.seekg(0, std::ios::beg); CV_Assert(fs && "File stream error");
|
|
|
|
data.create(1, validateToInt(fs_size), CV_8UC1);
|
|
|
|
fs.read((char*)data.ptr(), fs_size);
|
|
|
|
CV_Assert(fs && "Can't read file data");
|
|
|
|
fs.close();
|
|
|
|
}
|
|
|
|
CV_Assert(data.type() == CV_8UC1); CV_Assert(data.rows == 1);
|
2017-04-24 17:53:50 +08:00
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
{
|
|
|
|
Mat read_img;
|
|
|
|
CV_CheckType(img.type(), img.type() == CV_8UC1 || img.type() == CV_8UC3 || img.type() == CV_8UC4, "");
|
|
|
|
if (img.type() != m_type)
|
|
|
|
{
|
|
|
|
read_img.create(m_height, m_width, m_type);
|
|
|
|
}
|
|
|
|
else
|
2013-06-30 02:00:26 +08:00
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
read_img = img; // copy header
|
2013-06-30 02:00:26 +08:00
|
|
|
}
|
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
uchar* out_data = read_img.ptr();
|
|
|
|
size_t out_data_size = read_img.dataend - out_data;
|
2013-03-07 23:29:19 +08:00
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
uchar *res_ptr = NULL;
|
2013-06-30 01:59:25 +08:00
|
|
|
if (channels == 3)
|
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
CV_CheckTypeEQ(read_img.type(), CV_8UC3, "");
|
2014-08-13 19:08:27 +08:00
|
|
|
res_ptr = WebPDecodeBGRInto(data.ptr(), data.total(), out_data,
|
2018-08-31 03:12:01 +08:00
|
|
|
(int)out_data_size, (int)read_img.step);
|
2013-06-30 01:59:25 +08:00
|
|
|
}
|
|
|
|
else if (channels == 4)
|
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
CV_CheckTypeEQ(read_img.type(), CV_8UC4, "");
|
2014-08-13 19:08:27 +08:00
|
|
|
res_ptr = WebPDecodeBGRAInto(data.ptr(), data.total(), out_data,
|
2018-08-31 03:12:01 +08:00
|
|
|
(int)out_data_size, (int)read_img.step);
|
2013-06-30 01:59:25 +08:00
|
|
|
}
|
2013-03-07 23:29:19 +08:00
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
if (res_ptr != out_data)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (read_img.data == img.data && img.type() == m_type)
|
2013-03-07 23:29:19 +08:00
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
else if (img.type() == CV_8UC1)
|
|
|
|
{
|
|
|
|
cvtColor(read_img, img, COLOR_BGR2GRAY);
|
|
|
|
}
|
|
|
|
else if (img.type() == CV_8UC3 && m_type == CV_8UC4)
|
|
|
|
{
|
|
|
|
cvtColor(read_img, img, COLOR_BGRA2BGR);
|
|
|
|
}
|
2018-09-07 23:43:47 +08:00
|
|
|
else if (img.type() == CV_8UC4 && m_type == CV_8UC3)
|
2018-08-31 03:12:01 +08:00
|
|
|
{
|
2018-09-07 23:43:47 +08:00
|
|
|
cvtColor(read_img, img, COLOR_BGR2BGRA);
|
2018-08-31 03:12:01 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CV_Error(Error::StsInternal, "");
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 03:12:01 +08:00
|
|
|
return true;
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
WebPEncoder::WebPEncoder()
|
|
|
|
{
|
|
|
|
m_description = "WebP files (*.webp)";
|
|
|
|
m_buf_supported = true;
|
|
|
|
}
|
|
|
|
|
2013-06-30 02:00:26 +08:00
|
|
|
WebPEncoder::~WebPEncoder() { }
|
2013-03-07 23:29:19 +08:00
|
|
|
|
|
|
|
ImageEncoder WebPEncoder::newEncoder() const
|
|
|
|
{
|
2013-08-13 20:47:18 +08:00
|
|
|
return makePtr<WebPEncoder>();
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebPEncoder::write(const Mat& img, const std::vector<int>& params)
|
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
CV_CheckDepthEQ(img.depth(), CV_8U, "WebP codec supports 8U images only");
|
2013-03-07 23:29:19 +08:00
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
const int width = img.cols, height = img.rows;
|
2013-03-07 23:29:19 +08:00
|
|
|
|
|
|
|
bool comp_lossless = true;
|
2013-06-30 02:00:26 +08:00
|
|
|
float quality = 100.0f;
|
2013-03-07 23:29:19 +08:00
|
|
|
|
|
|
|
if (params.size() > 1)
|
|
|
|
{
|
|
|
|
if (params[0] == CV_IMWRITE_WEBP_QUALITY)
|
|
|
|
{
|
|
|
|
comp_lossless = false;
|
2013-06-30 02:00:26 +08:00
|
|
|
quality = static_cast<float>(params[1]);
|
|
|
|
if (quality < 1.0f)
|
2013-03-07 23:29:19 +08:00
|
|
|
{
|
2013-06-30 02:00:26 +08:00
|
|
|
quality = 1.0f;
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
2013-06-30 02:00:26 +08:00
|
|
|
if (quality > 100.0f)
|
2013-03-07 23:29:19 +08:00
|
|
|
{
|
|
|
|
comp_lossless = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
int channels = img.channels();
|
|
|
|
CV_Check(channels, channels == 1 || channels == 3 || channels == 4, "");
|
2013-03-07 23:29:19 +08:00
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
const Mat *image = &img;
|
|
|
|
Mat temp;
|
2013-03-07 23:29:19 +08:00
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
if (channels == 1)
|
2013-03-07 23:29:19 +08:00
|
|
|
{
|
2018-10-31 23:08:24 +08:00
|
|
|
cvtColor(*image, temp, COLOR_GRAY2BGR);
|
2013-03-07 23:29:19 +08:00
|
|
|
image = &temp;
|
|
|
|
channels = 3;
|
|
|
|
}
|
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
uint8_t *out = NULL;
|
|
|
|
size_t size = 0;
|
2013-03-07 23:29:19 +08:00
|
|
|
if (comp_lossless)
|
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
if (channels == 3)
|
2013-06-30 02:00:26 +08:00
|
|
|
{
|
2014-08-13 19:08:27 +08:00
|
|
|
size = WebPEncodeLosslessBGR(image->ptr(), width, height, (int)image->step, &out);
|
2013-06-30 02:00:26 +08:00
|
|
|
}
|
2018-08-31 03:12:01 +08:00
|
|
|
else if (channels == 4)
|
2013-06-30 02:00:26 +08:00
|
|
|
{
|
2014-08-13 19:08:27 +08:00
|
|
|
size = WebPEncodeLosslessBGRA(image->ptr(), width, height, (int)image->step, &out);
|
2013-06-30 02:00:26 +08:00
|
|
|
}
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
if (channels == 3)
|
2013-06-30 02:00:26 +08:00
|
|
|
{
|
2014-08-13 19:08:27 +08:00
|
|
|
size = WebPEncodeBGR(image->ptr(), width, height, (int)image->step, quality, &out);
|
2013-06-30 02:00:26 +08:00
|
|
|
}
|
2018-08-31 03:12:01 +08:00
|
|
|
else if (channels == 4)
|
2013-06-30 02:00:26 +08:00
|
|
|
{
|
2014-08-13 19:08:27 +08:00
|
|
|
size = WebPEncodeBGRA(image->ptr(), width, height, (int)image->step, quality, &out);
|
2013-06-30 02:00:26 +08:00
|
|
|
}
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
2018-08-31 03:12:01 +08:00
|
|
|
#if WEBP_DECODER_ABI_VERSION >= 0x0206
|
|
|
|
Ptr<uint8_t> out_cleaner(out, WebPFree);
|
|
|
|
#else
|
|
|
|
Ptr<uint8_t> out_cleaner(out, free);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CV_Assert(size > 0);
|
2013-03-07 23:29:19 +08:00
|
|
|
|
2018-08-31 03:12:01 +08:00
|
|
|
if (m_buf)
|
2013-03-07 23:29:19 +08:00
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
m_buf->resize(size);
|
|
|
|
memcpy(&(*m_buf)[0], out, size);
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
2018-08-31 03:12:01 +08:00
|
|
|
else
|
2013-03-07 23:29:19 +08:00
|
|
|
{
|
2018-08-31 03:12:01 +08:00
|
|
|
FILE *fd = fopen(m_filename.c_str(), "wb");
|
|
|
|
if (fd != NULL)
|
|
|
|
{
|
|
|
|
fwrite(out, size, sizeof(uint8_t), fd);
|
|
|
|
fclose(fd); fd = NULL;
|
|
|
|
}
|
2013-03-07 23:29:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return size > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|