2013-07-19 05:09:17 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 14:39:07 +00:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2013-07-19 05:09:17 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
// mapnik
|
2014-06-12 14:14:05 +00:00
|
|
|
#include <mapnik/make_unique.hpp>
|
2013-07-19 05:09:17 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
|
|
|
#include <mapnik/image_reader.hpp>
|
|
|
|
|
2020-11-19 14:30:30 +00:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-08 01:53:09 +00:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2013-07-19 05:09:17 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <webp/types.h>
|
|
|
|
#include <webp/decode.h>
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:30:30 +00:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2014-10-21 23:37:27 +00:00
|
|
|
|
2013-07-19 05:09:17 +00:00
|
|
|
// stl
|
|
|
|
#include <fstream>
|
2016-08-26 09:09:22 +00:00
|
|
|
#include <memory>
|
2013-07-19 05:09:17 +00:00
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
|
|
|
|
struct external_buffer_policy
|
|
|
|
{
|
|
|
|
external_buffer_policy( uint8_t const* data, std::size_t size)
|
|
|
|
: data_(data),
|
|
|
|
size_(size) {}
|
|
|
|
|
|
|
|
uint8_t const* data() const
|
|
|
|
{
|
|
|
|
return data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t size() const
|
|
|
|
{
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t const* data_;
|
|
|
|
std::size_t size_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct internal_buffer_policy
|
|
|
|
{
|
|
|
|
internal_buffer_policy(std::size_t size)
|
|
|
|
: data_((size!=0) ? static_cast<uint8_t*>(::operator new(sizeof(uint8_t) * size)) : 0),
|
|
|
|
size_(size)
|
|
|
|
{}
|
|
|
|
|
|
|
|
uint8_t * data() const
|
|
|
|
{
|
|
|
|
return data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t size() const
|
|
|
|
{
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
~internal_buffer_policy()
|
|
|
|
{
|
|
|
|
::operator delete(data_), data_=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t * data_;
|
|
|
|
std::size_t size_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class webp_reader : public image_reader
|
|
|
|
{
|
2014-07-07 17:23:15 +00:00
|
|
|
using buffer_policy_type = T;
|
2013-07-19 05:09:17 +00:00
|
|
|
private:
|
|
|
|
struct config_guard
|
|
|
|
{
|
|
|
|
config_guard(WebPDecoderConfig & config)
|
|
|
|
: config_(config) {}
|
|
|
|
|
|
|
|
~config_guard()
|
|
|
|
{
|
|
|
|
WebPFreeDecBuffer(&config_.output);
|
|
|
|
}
|
|
|
|
WebPDecoderConfig & config_;
|
|
|
|
};
|
2013-09-20 03:19:01 +00:00
|
|
|
|
|
|
|
std::unique_ptr<buffer_policy_type> buffer_;
|
2013-07-19 05:09:17 +00:00
|
|
|
size_t size_;
|
|
|
|
unsigned width_;
|
|
|
|
unsigned height_;
|
2014-06-12 01:50:58 +00:00
|
|
|
bool has_alpha_;
|
2013-07-19 05:09:17 +00:00
|
|
|
public:
|
|
|
|
explicit webp_reader(char const* data, std::size_t size);
|
|
|
|
explicit webp_reader(std::string const& filename);
|
|
|
|
~webp_reader();
|
2014-12-03 09:44:56 +00:00
|
|
|
unsigned width() const final;
|
|
|
|
unsigned height() const final;
|
2014-12-10 11:53:39 +00:00
|
|
|
boost::optional<box2d<double> > bounding_box() const final;
|
2014-12-03 09:44:56 +00:00
|
|
|
inline bool has_alpha() const final { return has_alpha_; }
|
2015-01-22 02:31:02 +00:00
|
|
|
void read(unsigned x,unsigned y,image_rgba8& image) final;
|
2015-01-22 01:40:12 +00:00
|
|
|
image_any read(unsigned x, unsigned y, unsigned width, unsigned height) final;
|
2013-07-19 05:09:17 +00:00
|
|
|
private:
|
|
|
|
void init();
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
image_reader* create_webp_reader(char const * data, std::size_t size)
|
|
|
|
{
|
|
|
|
return new webp_reader<external_buffer_policy>(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
image_reader* create_webp_reader2(std::string const& filename)
|
|
|
|
{
|
|
|
|
return new webp_reader<internal_buffer_policy>(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const bool registered = register_image_reader("webp", create_webp_reader);
|
|
|
|
const bool registered2 = register_image_reader("webp", create_webp_reader2);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ctor
|
|
|
|
template <typename T>
|
|
|
|
webp_reader<T>::webp_reader(char const* data, std::size_t size)
|
|
|
|
: buffer_(new buffer_policy_type(reinterpret_cast<uint8_t const*>(data), size)),
|
|
|
|
width_(0),
|
2014-06-12 01:50:58 +00:00
|
|
|
height_(0),
|
|
|
|
has_alpha_(false)
|
2013-07-19 05:09:17 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
webp_reader<T>::webp_reader(std::string const& filename)
|
2013-09-20 03:19:01 +00:00
|
|
|
: buffer_(nullptr),
|
2013-07-19 05:09:17 +00:00
|
|
|
size_(0),
|
|
|
|
width_(0),
|
2014-06-12 01:50:58 +00:00
|
|
|
height_(0),
|
|
|
|
has_alpha_(false)
|
2013-07-19 05:09:17 +00:00
|
|
|
{
|
|
|
|
std::ifstream file(filename.c_str(), std::ios::binary);
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
throw image_reader_exception("WEBP: Can't read file:" + filename);
|
|
|
|
}
|
|
|
|
std::streampos beg = file.tellg();
|
|
|
|
file.seekg (0, std::ios::end);
|
|
|
|
std::streampos end = file.tellg();
|
|
|
|
std::size_t file_size = end - beg;
|
|
|
|
file.seekg (0, std::ios::beg);
|
2013-09-20 03:19:01 +00:00
|
|
|
|
2014-03-11 04:34:48 +00:00
|
|
|
auto buffer = std::make_unique<buffer_policy_type>(file_size);
|
2013-09-20 03:19:01 +00:00
|
|
|
file.read(reinterpret_cast<char*>(buffer->data()), buffer->size());
|
2013-07-19 05:09:17 +00:00
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
throw image_reader_exception("WEBP: Failed to read:" + filename);
|
|
|
|
}
|
2013-09-20 03:19:01 +00:00
|
|
|
|
|
|
|
buffer_ = std::move(buffer);
|
2013-07-19 05:09:17 +00:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// dtor
|
|
|
|
template <typename T>
|
|
|
|
webp_reader<T>::~webp_reader()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void webp_reader<T>::init()
|
|
|
|
{
|
2014-06-13 03:54:50 +00:00
|
|
|
WebPDecoderConfig config;
|
|
|
|
config_guard guard(config);
|
|
|
|
if (!WebPInitDecoderConfig(&config))
|
2013-07-19 05:09:17 +00:00
|
|
|
{
|
2014-06-13 03:54:50 +00:00
|
|
|
throw image_reader_exception("WEBP reader: WebPInitDecoderConfig failed");
|
|
|
|
}
|
|
|
|
if (WebPGetFeatures(buffer_->data(), buffer_->size(), &config.input) == VP8_STATUS_OK) {
|
|
|
|
width_ = config.input.width;
|
|
|
|
height_ = config.input.height;
|
|
|
|
has_alpha_ = config.input.has_alpha;
|
2013-07-19 05:09:17 +00:00
|
|
|
}
|
2014-06-12 01:50:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw image_reader_exception("WEBP reader: WebPGetFeatures failed");
|
|
|
|
}
|
2013-07-19 05:09:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
unsigned webp_reader<T>::width() const
|
|
|
|
{
|
|
|
|
return width_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
unsigned webp_reader<T>::height() const
|
|
|
|
{
|
|
|
|
return height_;
|
|
|
|
}
|
|
|
|
|
2014-12-10 09:44:51 +00:00
|
|
|
template <typename T>
|
2014-12-10 11:53:39 +00:00
|
|
|
boost::optional<box2d<double> > webp_reader<T>::bounding_box() const
|
2014-12-10 09:44:51 +00:00
|
|
|
{
|
2014-12-10 11:53:39 +00:00
|
|
|
return boost::optional<box2d<double> >();
|
2014-12-10 09:44:51 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 05:09:17 +00:00
|
|
|
template <typename T>
|
2015-01-22 02:31:02 +00:00
|
|
|
void webp_reader<T>::read(unsigned x0, unsigned y0,image_rgba8& image)
|
2013-07-19 05:09:17 +00:00
|
|
|
{
|
|
|
|
WebPDecoderConfig config;
|
|
|
|
config_guard guard(config);
|
|
|
|
if (!WebPInitDecoderConfig(&config))
|
|
|
|
{
|
|
|
|
throw image_reader_exception("WEBP reader: WebPInitDecoderConfig failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
config.options.use_cropping = 1;
|
|
|
|
config.options.crop_left = x0;
|
|
|
|
config.options.crop_top = y0;
|
2014-11-20 11:46:16 +00:00
|
|
|
config.options.crop_width = std::min(static_cast<std::size_t>(width_ - x0), image.width());
|
|
|
|
config.options.crop_height = std::min(static_cast<std::size_t>(height_ - y0), image.height());
|
2013-07-19 05:09:17 +00:00
|
|
|
|
|
|
|
if (WebPGetFeatures(buffer_->data(), buffer_->size(), &config.input) != VP8_STATUS_OK)
|
|
|
|
{
|
|
|
|
throw image_reader_exception("WEBP reader: WebPGetFeatures failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
config.output.colorspace = MODE_RGBA;
|
2015-05-04 10:49:11 +00:00
|
|
|
config.output.u.RGBA.rgba = reinterpret_cast<uint8_t *>(image.bytes());
|
2013-07-19 05:09:17 +00:00
|
|
|
config.output.u.RGBA.stride = 4 * image.width();
|
|
|
|
config.output.u.RGBA.size = image.width() * image.height() * 4;
|
|
|
|
config.output.is_external_memory = 1;
|
|
|
|
if (WebPDecode(buffer_->data(), buffer_->size(), &config) != VP8_STATUS_OK)
|
|
|
|
{
|
|
|
|
throw image_reader_exception("WEBP reader: WebPDecode failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 09:44:56 +00:00
|
|
|
template <typename T>
|
2015-01-22 01:40:12 +00:00
|
|
|
image_any webp_reader<T>::read(unsigned x, unsigned y, unsigned width, unsigned height)
|
2014-12-03 09:44:56 +00:00
|
|
|
{
|
2015-01-22 02:31:02 +00:00
|
|
|
image_rgba8 data(width,height);
|
2014-12-03 09:56:19 +00:00
|
|
|
read(x, y, data);
|
2015-01-22 01:40:12 +00:00
|
|
|
return image_any(std::move(data));
|
2014-12-03 09:44:56 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 05:09:17 +00:00
|
|
|
}
|