From da6c630db90a8ac58803ab1ea612cb6fe81f3f86 Mon Sep 17 00:00:00 2001 From: artemp Date: Wed, 17 Apr 2013 14:50:35 +0100 Subject: [PATCH] + add webp writing support --- include/mapnik/webp_io.hpp | 108 +++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 include/mapnik/webp_io.hpp diff --git a/include/mapnik/webp_io.hpp b/include/mapnik/webp_io.hpp new file mode 100644 index 000000000..e2499dde1 --- /dev/null +++ b/include/mapnik/webp_io.hpp @@ -0,0 +1,108 @@ +/***************************************************************************** + * + * 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 + +#include + +#include +#include +#include + + +namespace mapnik { + +template +int webp_stream_write(const uint8_t* data, size_t data_size, const WebPPicture* picture) +{ + T* out = static_cast(picture->custom_ptr); + out->write(reinterpret_cast(data), data_size); + return true; +} + +std::string webp_encoding_error(WebPEncodingError error) { + std::ostringstream os; + switch (error) { + 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: os << "unknown error (" << error << ")"; break; + } + os << " during encoding"; + return os.str(); +} + +template +void save_as_webp(T1& file, int quality, int compression, T2 const& image) +{ + WebPConfig config; + if (!WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, quality)) + { + throw std::runtime_error("version mismatch"); + } + + // Add additional tuning + if (compression >= 0) config.method = compression; + + bool valid = WebPValidateConfig(&config); + if (!valid) { + 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(); + if (!WebPPictureAlloc(&pic)) + { + throw std::runtime_error("memory error"); + } + + int stride = sizeof(typename T2::pixel_type) * image.width(); + uint8_t const* bytes = reinterpret_cast(image.getBytes()); + int ok = WebPPictureImportRGBA(&pic, bytes, stride); + + pic.writer = webp_stream_write; + 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