2013-04-17 15:50:35 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Artem Pavlenko
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
#include <mapnik/util/conversions.hpp>
|
|
|
|
|
|
|
|
// webp
|
2013-04-17 15:50:35 +02:00
|
|
|
#include <webp/encode.h>
|
|
|
|
|
2013-08-15 20:47:28 +02:00
|
|
|
// stl
|
2013-04-17 15:50:35 +02:00
|
|
|
#include <stdexcept>
|
2013-08-15 20:47:28 +02:00
|
|
|
#include <string>
|
2013-11-10 08:02:24 +01:00
|
|
|
#include <cstring>
|
2013-09-19 04:51:14 +02:00
|
|
|
|
2013-04-17 15:50:35 +02:00
|
|
|
namespace mapnik {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-30 10:46:09 +02:00
|
|
|
std::string webp_encoding_error(WebPEncodingError error)
|
|
|
|
{
|
2013-08-15 20:47:28 +02:00
|
|
|
std::string os;
|
2013-09-16 08:08:04 +02:00
|
|
|
switch (error)
|
|
|
|
{
|
2013-08-15 20:47:28 +02:00
|
|
|
case VP8_ENC_ERROR_OUT_OF_MEMORY: os = "memory error allocating objects"; break;
|
|
|
|
case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY: os = "memory error while flushing bits"; break;
|
|
|
|
case VP8_ENC_ERROR_NULL_PARAMETER: os = "a pointer parameter is NULL"; break;
|
|
|
|
case VP8_ENC_ERROR_INVALID_CONFIGURATION: os = "configuration is invalid"; break;
|
|
|
|
case VP8_ENC_ERROR_BAD_DIMENSION: os = "picture has invalid width/height"; break;
|
|
|
|
case VP8_ENC_ERROR_PARTITION0_OVERFLOW: os = "partition is bigger than 512k"; break;
|
|
|
|
case VP8_ENC_ERROR_PARTITION_OVERFLOW: os = "partition is bigger than 16M"; break;
|
|
|
|
case VP8_ENC_ERROR_BAD_WRITE: os = "error while flushing bytes"; break;
|
|
|
|
case VP8_ENC_ERROR_FILE_TOO_BIG: os = "file is bigger than 4G"; break;
|
|
|
|
default:
|
|
|
|
mapnik::util::to_string(os,error);
|
|
|
|
os = "unknown error (" + os + ")"; break;
|
2013-04-17 15:50:35 +02:00
|
|
|
}
|
2013-08-15 20:47:28 +02:00
|
|
|
return os;
|
2013-04-17 15:50:35 +02:00
|
|
|
}
|
|
|
|
|
2013-09-27 05:07:04 +02:00
|
|
|
template <typename T2>
|
|
|
|
inline int import_image_data(T2 const& image,
|
|
|
|
WebPPicture & pic,
|
|
|
|
bool alpha)
|
|
|
|
{
|
2013-09-27 21:17:31 +02:00
|
|
|
ImageData<typename T2::pixel_type> const& data = image.data();
|
|
|
|
int stride = sizeof(typename T2::pixel_type) * image.width();
|
|
|
|
if (data.width() == image.width() &&
|
|
|
|
data.height() == image.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
|
|
|
{
|
2013-09-27 21:17:31 +02:00
|
|
|
return WebPPictureImportRGBA(&pic, data.getBytes(), stride);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if (WEBP_ENCODER_ABI_VERSION >> 8) >= 1
|
|
|
|
return WebPPictureImportRGBX(&pic, data.getBytes(), stride);
|
|
|
|
#else
|
|
|
|
return WebPPictureImportRGBA(&pic, data.getBytes(), stride);
|
|
|
|
#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
|
|
|
|
image_data_32 im(image.width(),image.height());
|
|
|
|
for (unsigned y = 0; y < image.height(); ++y)
|
|
|
|
{
|
|
|
|
typename T2::pixel_type const * row_from = image.getRow(y);
|
|
|
|
image_data_32::pixel_type * row_to = im.getRow(y);
|
|
|
|
std::memcpy(row_to,row_from,stride);
|
|
|
|
}
|
|
|
|
if (alpha)
|
|
|
|
{
|
|
|
|
return WebPPictureImportRGBA(&pic, im.getBytes(), stride);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if (WEBP_ENCODER_ABI_VERSION >> 8) >= 1
|
|
|
|
return WebPPictureImportRGBX(&pic, im.getBytes(), stride);
|
|
|
|
#else
|
|
|
|
return WebPPictureImportRGBA(&pic, im.getBytes(), stride);
|
|
|
|
#endif
|
|
|
|
}
|
2013-09-27 05:07:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline int import_image_data(image_data_32 const& im,
|
|
|
|
WebPPicture & pic,
|
|
|
|
bool alpha)
|
|
|
|
{
|
|
|
|
int stride = sizeof(image_data_32::pixel_type) * im.width();
|
|
|
|
if (alpha)
|
|
|
|
{
|
|
|
|
return WebPPictureImportRGBA(&pic, im.getBytes(), stride);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if (WEBP_ENCODER_ABI_VERSION >> 8) >= 1
|
|
|
|
return WebPPictureImportRGBX(&pic, im.getBytes(), stride);
|
|
|
|
#else
|
|
|
|
return WebPPictureImportRGBA(&pic, im.getBytes(), stride);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-17 15:50:35 +02:00
|
|
|
template <typename T1, typename T2>
|
2013-07-19 07:09:17 +02:00
|
|
|
void save_as_webp(T1& file,
|
2013-10-04 08:59:10 +02:00
|
|
|
T2 const& image,
|
2013-10-04 09:24:30 +02:00
|
|
|
WebPConfig const& config,
|
|
|
|
bool alpha)
|
2013-04-17 15:50:35 +02:00
|
|
|
{
|
|
|
|
bool valid = WebPValidateConfig(&config);
|
2013-09-16 08:08:04 +02:00
|
|
|
if (!valid)
|
|
|
|
{
|
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);
|
|
|
|
if (WebPPictureAlloc(&pic)) {
|
|
|
|
ok = 1;
|
|
|
|
const int width = pic.width;
|
|
|
|
const int height = pic.height;
|
|
|
|
for (int y = 0; y < height; ++y) {
|
|
|
|
typename T2::pixel_type const * row = image.getRow(y);
|
|
|
|
for (int x = 0; x < width; ++x) {
|
|
|
|
const unsigned rgba = row[x];
|
|
|
|
unsigned a = (rgba >> 24) & 0xff;
|
|
|
|
unsigned r = rgba & 0xff;
|
|
|
|
unsigned g = (rgba >> 8 ) & 0xff;
|
|
|
|
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
|
|
|
|
ok = import_image_data(image,pic,alpha);
|
|
|
|
}
|
2013-08-30 10:46:09 +02:00
|
|
|
#else
|
2013-09-27 05:07:04 +02:00
|
|
|
ok = import_image_data(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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MAPNIK_WEBP_IO_HPP
|