2015-01-08 21:55:51 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 14:39:07 +00:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2015-01-08 21:55:51 +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
|
|
|
|
#if defined(HAVE_JPEG)
|
|
|
|
#include <mapnik/jpeg_io.hpp>
|
|
|
|
#endif
|
|
|
|
|
2015-01-09 00:31:14 +00:00
|
|
|
#include <mapnik/image_util.hpp>
|
2015-01-08 21:55:51 +00:00
|
|
|
#include <mapnik/image_util_jpeg.hpp>
|
2015-01-22 17:39:37 +00:00
|
|
|
#include <mapnik/image.hpp>
|
2015-01-09 00:31:14 +00:00
|
|
|
#include <mapnik/image_view.hpp>
|
2015-05-15 15:54:15 +00:00
|
|
|
#include <mapnik/image_options.hpp>
|
2015-01-09 00:31:14 +00:00
|
|
|
#include <mapnik/util/conversions.hpp>
|
2015-01-08 21:55:51 +00:00
|
|
|
|
|
|
|
// stl
|
|
|
|
#include <string>
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
namespace mapnik {
|
2015-01-08 21:55:51 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
jpeg_saver::jpeg_saver(std::ostream& stream, std::string const& t)
|
|
|
|
: stream_(stream)
|
|
|
|
, t_(t)
|
|
|
|
{}
|
2015-01-08 21:55:51 +00:00
|
|
|
|
2015-08-12 08:29:57 +00:00
|
|
|
namespace detail {
|
|
|
|
|
2015-08-12 18:00:04 +00:00
|
|
|
int parse_jpeg_quality(std::string const& params)
|
2015-01-08 21:55:51 +00:00
|
|
|
{
|
|
|
|
int quality = 85;
|
2015-08-12 08:29:57 +00:00
|
|
|
if (params != "jpeg")
|
2015-01-08 21:55:51 +00:00
|
|
|
{
|
2015-08-12 08:29:57 +00:00
|
|
|
for (auto const& kv : parse_image_options(params))
|
2015-01-08 21:55:51 +00:00
|
|
|
{
|
2015-05-15 15:54:15 +00:00
|
|
|
auto const& key = kv.first;
|
|
|
|
auto const& val = kv.second;
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
if (key == "jpeg")
|
|
|
|
continue;
|
|
|
|
else if (key.size() > 4 && key.substr(0, 4) == "jpeg")
|
2015-08-11 15:13:54 +00:00
|
|
|
{
|
|
|
|
if (!mapnik::util::string2int(key.substr(4), quality))
|
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
throw image_writer_exception("invalid jpeg quality: '" + key.substr(4) + "'");
|
2015-08-11 15:13:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-26 09:43:31 +00:00
|
|
|
else if (key == "quality")
|
2015-03-06 03:51:48 +00:00
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
if (val && !(*val).empty())
|
2015-03-06 03:51:48 +00:00
|
|
|
{
|
2015-05-15 15:54:15 +00:00
|
|
|
if (!mapnik::util::string2int(*val, quality) || quality < 0 || quality > 100)
|
2015-03-06 03:51:48 +00:00
|
|
|
{
|
2015-05-15 15:54:15 +00:00
|
|
|
throw image_writer_exception("invalid jpeg quality: '" + *val + "'");
|
2015-03-06 03:51:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 21:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-12 08:29:57 +00:00
|
|
|
return quality;
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
} // namespace detail
|
2015-08-12 08:29:57 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
template<typename T>
|
|
|
|
void process_rgba8_jpeg(T const& image, std::string const& type, std::ostream& stream)
|
2015-08-12 08:29:57 +00:00
|
|
|
{
|
|
|
|
#if defined(HAVE_JPEG)
|
|
|
|
int quality = detail::parse_jpeg_quality(type);
|
2015-01-08 21:55:51 +00:00
|
|
|
save_as_jpeg(stream, quality, image);
|
|
|
|
#else
|
2015-05-14 11:53:21 +00:00
|
|
|
throw image_writer_exception("jpeg output is not enabled in your build of Mapnik");
|
2015-01-08 21:55:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-01-09 00:31:14 +00:00
|
|
|
template<>
|
2022-01-26 09:43:31 +00:00
|
|
|
void jpeg_saver::operator()<image_rgba8>(image_rgba8 const& image) const
|
2015-01-09 00:31:14 +00:00
|
|
|
{
|
|
|
|
process_rgba8_jpeg(image, t_, stream_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2022-01-26 09:43:31 +00:00
|
|
|
void jpeg_saver::operator()<image_view_rgba8>(image_view_rgba8 const& image) const
|
2015-01-09 00:31:14 +00:00
|
|
|
{
|
|
|
|
process_rgba8_jpeg(image, t_, stream_);
|
|
|
|
}
|
|
|
|
|
2015-01-14 18:42:30 +00:00
|
|
|
template<>
|
2022-01-26 09:43:31 +00:00
|
|
|
void jpeg_saver::operator()<image_null>(image_null const& image) const
|
2015-01-14 18:42:30 +00:00
|
|
|
{
|
2015-05-14 11:53:21 +00:00
|
|
|
throw image_writer_exception("Can not save a null image to jpeg");
|
2015-01-14 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 02:28:16 +00:00
|
|
|
template<>
|
2022-01-26 09:43:31 +00:00
|
|
|
void jpeg_saver::operator()<image_view_null>(image_view_null const& image) const
|
2015-03-20 02:28:16 +00:00
|
|
|
{
|
2015-05-14 11:53:21 +00:00
|
|
|
throw image_writer_exception("Can not save a null image to jpeg");
|
2015-03-20 02:28:16 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
template<typename T>
|
|
|
|
void jpeg_saver::operator()(T const& image) const
|
2015-01-09 00:31:14 +00:00
|
|
|
{
|
2015-05-14 11:53:21 +00:00
|
|
|
throw image_writer_exception("Mapnik does not support jpeg grayscale images");
|
2015-01-09 00:31:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
template void jpeg_saver::operator()<image_gray8>(image_gray8 const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_gray8s>(image_gray8s const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_gray16>(image_gray16 const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_gray16s>(image_gray16s const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_gray32>(image_gray32 const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_gray32s>(image_gray32s const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_gray32f>(image_gray32f const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_gray64>(image_gray64 const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_gray64s>(image_gray64s const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_gray64f>(image_gray64f const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_view_gray8>(image_view_gray8 const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_view_gray8s>(image_view_gray8s const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_view_gray16>(image_view_gray16 const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_view_gray16s>(image_view_gray16s const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_view_gray32>(image_view_gray32 const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_view_gray32s>(image_view_gray32s const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_view_gray32f>(image_view_gray32f const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_view_gray64>(image_view_gray64 const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_view_gray64s>(image_view_gray64s const& image) const;
|
|
|
|
template void jpeg_saver::operator()<image_view_gray64f>(image_view_gray64f const& image) const;
|
|
|
|
|
|
|
|
} // namespace mapnik
|