From 751abba26271fb629b1a153aacfe2e9b748727d0 Mon Sep 17 00:00:00 2001 From: artemp Date: Wed, 12 Aug 2015 10:29:57 +0200 Subject: [PATCH] add parse_jpeg_quality method + uint test parsing jpegXX and jpeg:quality=XX options ref #3024 --- include/mapnik/image_util_jpeg.hpp | 4 +++- src/image_util_jpeg.cpp | 20 +++++++++++++++----- test/unit/imaging/image_io_test.cpp | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/include/mapnik/image_util_jpeg.hpp b/include/mapnik/image_util_jpeg.hpp index b4d2648db..f0ba502ff 100644 --- a/include/mapnik/image_util_jpeg.hpp +++ b/include/mapnik/image_util_jpeg.hpp @@ -28,7 +28,9 @@ #include namespace mapnik { - +namespace detail { +int parse_jpeg_quality(std::string params); +} struct jpeg_saver { jpeg_saver(std::ostream &, std::string const&); diff --git a/src/image_util_jpeg.cpp b/src/image_util_jpeg.cpp index 22cd3539d..9a1e726a4 100644 --- a/src/image_util_jpeg.cpp +++ b/src/image_util_jpeg.cpp @@ -41,14 +41,14 @@ namespace mapnik jpeg_saver::jpeg_saver(std::ostream & stream, std::string const& t) : stream_(stream), t_(t) {} -template -void process_rgba8_jpeg(T const& image, std::string const& type, std::ostream & stream) +namespace detail { + +int parse_jpeg_quality(std::string params) { -#if defined(HAVE_JPEG) int quality = 85; - if (type != "jpeg") + if (params != "jpeg") { - for (auto const& kv : parse_image_options(type)) + for (auto const& kv : parse_image_options(params)) { auto const& key = kv.first; auto const& val = kv.second; @@ -73,6 +73,16 @@ void process_rgba8_jpeg(T const& image, std::string const& type, std::ostream & } } } + return quality; +} + +} + +template +void process_rgba8_jpeg(T const& image, std::string const& type, std::ostream & stream) +{ +#if defined(HAVE_JPEG) + int quality = detail::parse_jpeg_quality(type); save_as_jpeg(stream, quality, image); #else throw image_writer_exception("jpeg output is not enabled in your build of Mapnik"); diff --git a/test/unit/imaging/image_io_test.cpp b/test/unit/imaging/image_io_test.cpp index d0a984d7d..6daeca899 100644 --- a/test/unit/imaging/image_io_test.cpp +++ b/test/unit/imaging/image_io_test.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,7 @@ SECTION("readers") { { REQUIRE( std::string(ex.what()) == std::string("JPEG Reader: libjpeg could not read image: Not a JPEG file: starts with 0x89 0x50") ); } + #endif REQUIRE_THROWS(mapnik::image_rgba8 im(-10,-10)); // should throw rather than overflow @@ -95,5 +97,17 @@ SECTION("readers") { } // END SECTION +SECTION("writers options") +{ +#if defined(HAVE_JPEG) + // test we can parse both jpegXX and quality=XX options + REQUIRE_THROWS(mapnik::detail::parse_jpeg_quality("jpegXX")); + REQUIRE_THROWS(mapnik::detail::parse_jpeg_quality("jpeg:quality=XX")); + int q0 = mapnik::detail::parse_jpeg_quality("jpeg50"); + int q1 = mapnik::detail::parse_jpeg_quality("jpeg:quality=50"); + REQUIRE(q0 == q1); +#endif + +} // END SECTION } // END TEST_CASE