Path: blob/master/modules/imgcodecs/src/grfmt_webp.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#ifdef HAVE_WEBP4344#include "precomp.hpp"4546#include <webp/decode.h>47#include <webp/encode.h>4849#include <stdio.h>50#include <limits.h>5152#include "grfmt_webp.hpp"5354#include "opencv2/imgproc.hpp"5556#include <opencv2/core/utils/configuration.private.hpp>5758namespace cv59{6061// 64Mb limit to avoid memory DDOS62static size_t param_maxFileSize = utils::getConfigurationParameterSizeT("OPENCV_IMGCODECS_WEBP_MAX_FILE_SIZE", 64*1024*1024);6364static const size_t WEBP_HEADER_SIZE = 32;6566WebPDecoder::WebPDecoder()67{68m_buf_supported = true;69channels = 0;70fs_size = 0;71}7273WebPDecoder::~WebPDecoder() {}7475size_t WebPDecoder::signatureLength() const76{77return WEBP_HEADER_SIZE;78}7980bool WebPDecoder::checkSignature(const String & signature) const81{82bool ret = false;8384if(signature.size() >= WEBP_HEADER_SIZE)85{86WebPBitstreamFeatures features;87if(VP8_STATUS_OK == WebPGetFeatures((uint8_t *)signature.c_str(),88WEBP_HEADER_SIZE, &features))89{90ret = true;91}92}9394return ret;95}9697ImageDecoder WebPDecoder::newDecoder() const98{99return makePtr<WebPDecoder>();100}101102bool WebPDecoder::readHeader()103{104uint8_t header[WEBP_HEADER_SIZE] = { 0 };105if (m_buf.empty())106{107fs.open(m_filename.c_str(), std::ios::binary);108fs.seekg(0, std::ios::end);109fs_size = safeCastToSizeT(fs.tellg(), "File is too large");110fs.seekg(0, std::ios::beg);111CV_Assert(fs && "File stream error");112CV_CheckGE(fs_size, WEBP_HEADER_SIZE, "File is too small");113CV_CheckLE(fs_size, param_maxFileSize, "File is too large. Increase OPENCV_IMGCODECS_WEBP_MAX_FILE_SIZE parameter if you want to process large files");114115fs.read((char*)header, sizeof(header));116CV_Assert(fs && "Can't read WEBP_HEADER_SIZE bytes");117}118else119{120CV_CheckGE(m_buf.total(), WEBP_HEADER_SIZE, "Buffer is too small");121memcpy(header, m_buf.ptr(), sizeof(header));122data = m_buf;123}124125WebPBitstreamFeatures features;126if (VP8_STATUS_OK == WebPGetFeatures(header, sizeof(header), &features))127{128m_width = features.width;129m_height = features.height;130131if (features.has_alpha)132{133m_type = CV_8UC4;134channels = 4;135}136else137{138m_type = CV_8UC3;139channels = 3;140}141142return true;143}144145return false;146}147148bool WebPDecoder::readData(Mat &img)149{150CV_CheckGE(m_width, 0, ""); CV_CheckGE(m_height, 0, "");151152CV_CheckEQ(img.cols, m_width, "");153CV_CheckEQ(img.rows, m_height, "");154155if (m_buf.empty())156{157fs.seekg(0, std::ios::beg); CV_Assert(fs && "File stream error");158data.create(1, validateToInt(fs_size), CV_8UC1);159fs.read((char*)data.ptr(), fs_size);160CV_Assert(fs && "Can't read file data");161fs.close();162}163CV_Assert(data.type() == CV_8UC1); CV_Assert(data.rows == 1);164165{166Mat read_img;167CV_CheckType(img.type(), img.type() == CV_8UC1 || img.type() == CV_8UC3 || img.type() == CV_8UC4, "");168if (img.type() != m_type)169{170read_img.create(m_height, m_width, m_type);171}172else173{174read_img = img; // copy header175}176177uchar* out_data = read_img.ptr();178size_t out_data_size = read_img.dataend - out_data;179180uchar *res_ptr = NULL;181if (channels == 3)182{183CV_CheckTypeEQ(read_img.type(), CV_8UC3, "");184res_ptr = WebPDecodeBGRInto(data.ptr(), data.total(), out_data,185(int)out_data_size, (int)read_img.step);186}187else if (channels == 4)188{189CV_CheckTypeEQ(read_img.type(), CV_8UC4, "");190res_ptr = WebPDecodeBGRAInto(data.ptr(), data.total(), out_data,191(int)out_data_size, (int)read_img.step);192}193194if (res_ptr != out_data)195return false;196197if (read_img.data == img.data && img.type() == m_type)198{199// nothing200}201else if (img.type() == CV_8UC1)202{203cvtColor(read_img, img, COLOR_BGR2GRAY);204}205else if (img.type() == CV_8UC3 && m_type == CV_8UC4)206{207cvtColor(read_img, img, COLOR_BGRA2BGR);208}209else if (img.type() == CV_8UC4 && m_type == CV_8UC3)210{211cvtColor(read_img, img, COLOR_BGR2BGRA);212}213else214{215CV_Error(Error::StsInternal, "");216}217}218return true;219}220221WebPEncoder::WebPEncoder()222{223m_description = "WebP files (*.webp)";224m_buf_supported = true;225}226227WebPEncoder::~WebPEncoder() { }228229ImageEncoder WebPEncoder::newEncoder() const230{231return makePtr<WebPEncoder>();232}233234bool WebPEncoder::write(const Mat& img, const std::vector<int>& params)235{236CV_CheckDepthEQ(img.depth(), CV_8U, "WebP codec supports 8U images only");237238const int width = img.cols, height = img.rows;239240bool comp_lossless = true;241float quality = 100.0f;242243if (params.size() > 1)244{245if (params[0] == CV_IMWRITE_WEBP_QUALITY)246{247comp_lossless = false;248quality = static_cast<float>(params[1]);249if (quality < 1.0f)250{251quality = 1.0f;252}253if (quality > 100.0f)254{255comp_lossless = true;256}257}258}259260int channels = img.channels();261CV_Check(channels, channels == 1 || channels == 3 || channels == 4, "");262263const Mat *image = &img;264Mat temp;265266if (channels == 1)267{268cvtColor(*image, temp, CV_GRAY2BGR);269image = &temp;270channels = 3;271}272273uint8_t *out = NULL;274size_t size = 0;275if (comp_lossless)276{277if (channels == 3)278{279size = WebPEncodeLosslessBGR(image->ptr(), width, height, (int)image->step, &out);280}281else if (channels == 4)282{283size = WebPEncodeLosslessBGRA(image->ptr(), width, height, (int)image->step, &out);284}285}286else287{288if (channels == 3)289{290size = WebPEncodeBGR(image->ptr(), width, height, (int)image->step, quality, &out);291}292else if (channels == 4)293{294size = WebPEncodeBGRA(image->ptr(), width, height, (int)image->step, quality, &out);295}296}297#if WEBP_DECODER_ABI_VERSION >= 0x0206298Ptr<uint8_t> out_cleaner(out, WebPFree);299#else300Ptr<uint8_t> out_cleaner(out, free);301#endif302303CV_Assert(size > 0);304305if (m_buf)306{307m_buf->resize(size);308memcpy(&(*m_buf)[0], out, size);309}310else311{312FILE *fd = fopen(m_filename.c_str(), "wb");313if (fd != NULL)314{315fwrite(out, size, sizeof(uint8_t), fd);316fclose(fd); fd = NULL;317}318}319320return size > 0;321}322323}324325#endif326327328