2013-04-17 15:50:35 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2013-04-17 15:50:35 +02: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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef MAPNIK_WEBP_IO_HPP
|
|
|
|
#define MAPNIK_WEBP_IO_HPP
|
|
|
|
|
2013-08-15 20:47:28 +02:00
|
|
|
// mapnik
|
2022-01-29 12:52:23 +01:00
|
|
|
#include <mapnik/config.hpp>
|
2015-01-22 18:39:37 +01:00
|
|
|
#include <mapnik/image.hpp>
|
2013-08-15 20:47:28 +02:00
|
|
|
#include <mapnik/util/conversions.hpp>
|
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-08 02:53:09 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2022-08-17 17:22:07 +02:00
|
|
|
extern "C" {
|
2013-04-17 15:50:35 +02:00
|
|
|
#include <webp/encode.h>
|
2014-08-09 22:57:01 +02:00
|
|
|
}
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2013-04-17 15:50:35 +02:00
|
|
|
|
2013-08-15 20:47:28 +02:00
|
|
|
// stl
|
2014-10-21 16:52:01 +02:00
|
|
|
#include <algorithm>
|
2013-04-17 15:50:35 +02:00
|
|
|
#include <stdexcept>
|
2013-08-15 20:47:28 +02:00
|
|
|
#include <string>
|
2013-11-28 07:50:15 +01:00
|
|
|
|
2013-04-17 15:50:35 +02:00
|
|
|
namespace mapnik {
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename T>
|
2013-04-17 15:50:35 +02:00
|
|
|
int webp_stream_write(const uint8_t* data, size_t data_size, const WebPPicture* picture)
|
|
|
|
{
|
|
|
|
T* out = static_cast<T*>(picture->custom_ptr);
|
|
|
|
out->write(reinterpret_cast<const char*>(data), data_size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-29 12:52:23 +01:00
|
|
|
std::string MAPNIK_DECL webp_encoding_error(WebPEncodingError error);
|
2013-04-17 15:50:35 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename T2>
|
|
|
|
inline int import_image(T2 const& im_in, WebPPicture& pic, bool alpha)
|
2013-09-27 05:07:04 +02:00
|
|
|
{
|
2015-02-04 22:41:58 +01:00
|
|
|
image<typename T2::pixel> const& data = im_in.data();
|
2015-05-22 05:39:14 +02:00
|
|
|
std::size_t width = im_in.width();
|
|
|
|
std::size_t height = im_in.height();
|
2015-07-30 20:10:23 +02:00
|
|
|
std::size_t stride = sizeof(typename T2::pixel_type) * width;
|
2022-01-26 10:43:31 +01:00
|
|
|
if (data.width() == width && data.height() == height)
|
2013-09-27 05:07:04 +02:00
|
|
|
{
|
2013-09-27 21:17:31 +02:00
|
|
|
if (alpha)
|
2013-09-27 05:07:04 +02:00
|
|
|
{
|
2015-07-30 20:10:23 +02:00
|
|
|
return WebPPictureImportRGBA(&pic, data.bytes(), static_cast<int>(stride));
|
2013-09-27 21:17:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
#if (WEBP_ENCODER_ABI_VERSION >> 8) >= 1
|
2015-07-30 20:10:23 +02:00
|
|
|
return WebPPictureImportRGBX(&pic, data.bytes(), static_cast<int>(stride));
|
2022-01-26 10:43:31 +01:00
|
|
|
#else
|
2015-07-30 20:10:23 +02:00
|
|
|
return WebPPictureImportRGBA(&pic, data.bytes(), static_cast<int>(stride));
|
2022-01-26 10:43:31 +01:00
|
|
|
#endif
|
2013-09-27 05:07:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-27 21:17:31 +02:00
|
|
|
// need to copy: https://github.com/mapnik/mapnik/issues/2024
|
2022-01-26 10:43:31 +01:00
|
|
|
image_rgba8 im(width, height);
|
2015-05-22 05:39:14 +02:00
|
|
|
for (unsigned y = 0; y < height; ++y)
|
2013-09-27 21:17:31 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
typename T2::pixel_type const* row_from = im_in.get_row(y);
|
|
|
|
image_rgba8::pixel_type* row_to = im.get_row(y);
|
2015-05-22 05:39:14 +02:00
|
|
|
std::copy(row_from, row_from + width, row_to);
|
2013-09-27 21:17:31 +02:00
|
|
|
}
|
|
|
|
if (alpha)
|
|
|
|
{
|
2015-07-30 20:10:23 +02:00
|
|
|
return WebPPictureImportRGBA(&pic, im.bytes(), static_cast<int>(stride));
|
2013-09-27 21:17:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
#if (WEBP_ENCODER_ABI_VERSION >> 8) >= 1
|
2015-07-30 20:10:23 +02:00
|
|
|
return WebPPictureImportRGBX(&pic, im.bytes(), static_cast<int>(stride));
|
2022-01-26 10:43:31 +01:00
|
|
|
#else
|
2015-07-30 20:10:23 +02:00
|
|
|
return WebPPictureImportRGBA(&pic, im.bytes(), static_cast<int>(stride));
|
2022-01-26 10:43:31 +01:00
|
|
|
#endif
|
2013-09-27 21:17:31 +02:00
|
|
|
}
|
2013-09-27 05:07:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<>
|
|
|
|
inline int import_image(image_rgba8 const& im, WebPPicture& pic, bool alpha)
|
2013-09-27 05:07:04 +02:00
|
|
|
{
|
2015-07-30 20:10:23 +02:00
|
|
|
std::size_t stride = sizeof(image_rgba8::pixel_type) * im.width();
|
2013-09-27 05:07:04 +02:00
|
|
|
if (alpha)
|
|
|
|
{
|
2015-07-30 20:10:23 +02:00
|
|
|
return WebPPictureImportRGBA(&pic, im.bytes(), static_cast<int>(stride));
|
2013-09-27 05:07:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if (WEBP_ENCODER_ABI_VERSION >> 8) >= 1
|
2015-07-30 20:10:23 +02:00
|
|
|
return WebPPictureImportRGBX(&pic, im.bytes(), static_cast<int>(stride));
|
2013-09-27 05:07:04 +02:00
|
|
|
#else
|
2015-07-30 20:10:23 +02:00
|
|
|
return WebPPictureImportRGBA(&pic, im.bytes(), static_cast<int>(stride));
|
2013-09-27 05:07:04 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename T1, typename T2>
|
|
|
|
void save_as_webp(T1& file, T2 const& image, WebPConfig const& config, bool alpha)
|
2013-04-17 15:50:35 +02:00
|
|
|
{
|
2014-08-25 23:38:03 +02:00
|
|
|
if (WebPValidateConfig(&config) != 1)
|
2013-09-16 08:08:04 +02:00
|
|
|
{
|
2013-04-17 15:50:35 +02:00
|
|
|
throw std::runtime_error("Invalid configuration");
|
|
|
|
}
|
|
|
|
|
|
|
|
WebPPicture pic;
|
|
|
|
if (!WebPPictureInit(&pic))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("version mismatch");
|
|
|
|
}
|
|
|
|
pic.width = image.width();
|
|
|
|
pic.height = image.height();
|
2013-09-27 05:07:04 +02:00
|
|
|
int ok = 0;
|
2013-08-30 10:46:09 +02:00
|
|
|
#if (WEBP_ENCODER_ABI_VERSION >> 8) >= 1
|
2013-10-04 09:24:30 +02:00
|
|
|
pic.use_argb = !!config.lossless;
|
2013-09-27 05:07:04 +02:00
|
|
|
// lossless fast track
|
|
|
|
if (pic.use_argb)
|
2013-08-15 20:47:28 +02:00
|
|
|
{
|
2013-09-27 05:07:04 +02:00
|
|
|
pic.colorspace = static_cast<WebPEncCSP>(pic.colorspace | WEBP_CSP_ALPHA_BIT);
|
2022-01-26 10:43:31 +01:00
|
|
|
if (WebPPictureAlloc(&pic))
|
|
|
|
{
|
2013-09-27 05:07:04 +02:00
|
|
|
ok = 1;
|
|
|
|
const int width = pic.width;
|
|
|
|
const int height = pic.height;
|
2022-01-26 10:43:31 +01:00
|
|
|
for (int y = 0; y < height; ++y)
|
|
|
|
{
|
|
|
|
typename T2::pixel_type const* row = image.get_row(y);
|
|
|
|
for (int x = 0; x < width; ++x)
|
|
|
|
{
|
2013-09-27 05:07:04 +02:00
|
|
|
const unsigned rgba = row[x];
|
|
|
|
unsigned a = (rgba >> 24) & 0xff;
|
|
|
|
unsigned r = rgba & 0xff;
|
2022-01-26 10:43:31 +01:00
|
|
|
unsigned g = (rgba >> 8) & 0xff;
|
2013-09-27 05:07:04 +02:00
|
|
|
unsigned b = (rgba >> 16) & 0xff;
|
|
|
|
const uint32_t argb = (a << 24) | (r << 16) | (g << 8) | (b);
|
|
|
|
pic.argb[x + y * pic.argb_stride] = argb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-15 20:47:28 +02:00
|
|
|
}
|
|
|
|
else
|
2013-04-17 15:50:35 +02:00
|
|
|
{
|
2013-09-27 05:07:04 +02:00
|
|
|
// different approach for lossy since ImportYUVAFromRGBA is needed
|
|
|
|
// to prepare WebPPicture and working with view pixels is not viable
|
2022-01-26 10:43:31 +01:00
|
|
|
ok = import_image(image, pic, alpha);
|
2013-09-27 05:07:04 +02:00
|
|
|
}
|
2013-08-30 10:46:09 +02:00
|
|
|
#else
|
2022-01-26 10:43:31 +01:00
|
|
|
ok = import_image(image, pic, alpha);
|
2013-08-30 10:46:09 +02:00
|
|
|
#endif
|
2013-07-19 07:09:17 +02:00
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(webp_encoding_error(pic.error_code));
|
|
|
|
}
|
2013-04-17 15:50:35 +02:00
|
|
|
|
|
|
|
pic.writer = webp_stream_write<T1>;
|
|
|
|
pic.custom_ptr = &file;
|
|
|
|
ok = WebPEncode(&config, &pic);
|
|
|
|
WebPPictureFree(&pic);
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(webp_encoding_error(pic.error_code));
|
|
|
|
}
|
|
|
|
file.flush();
|
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
} // namespace mapnik
|
2013-04-17 15:50:35 +02:00
|
|
|
|
|
|
|
#endif // MAPNIK_WEBP_IO_HPP
|