add parse_jpeg_quality method + uint test parsing jpegXX and jpeg:quality=XX options

ref #3024
This commit is contained in:
artemp 2015-08-12 10:29:57 +02:00
parent bf009489d7
commit 751abba262
3 changed files with 32 additions and 6 deletions

View file

@ -28,7 +28,9 @@
#include <iostream> #include <iostream>
namespace mapnik { namespace mapnik {
namespace detail {
int parse_jpeg_quality(std::string params);
}
struct jpeg_saver struct jpeg_saver
{ {
jpeg_saver(std::ostream &, std::string const&); jpeg_saver(std::ostream &, std::string const&);

View file

@ -41,14 +41,14 @@ namespace mapnik
jpeg_saver::jpeg_saver(std::ostream & stream, std::string const& t) jpeg_saver::jpeg_saver(std::ostream & stream, std::string const& t)
: stream_(stream), t_(t) {} : stream_(stream), t_(t) {}
template <typename T> namespace detail {
void process_rgba8_jpeg(T const& image, std::string const& type, std::ostream & stream)
int parse_jpeg_quality(std::string params)
{ {
#if defined(HAVE_JPEG)
int quality = 85; 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& key = kv.first;
auto const& val = kv.second; 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 <typename T>
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); save_as_jpeg(stream, quality, image);
#else #else
throw image_writer_exception("jpeg output is not enabled in your build of Mapnik"); throw image_writer_exception("jpeg output is not enabled in your build of Mapnik");

View file

@ -4,6 +4,7 @@
#include <mapnik/image.hpp> #include <mapnik/image.hpp>
#include <mapnik/image_reader.hpp> #include <mapnik/image_reader.hpp>
#include <mapnik/image_util.hpp> #include <mapnik/image_util.hpp>
#include <mapnik/image_util_jpeg.hpp>
#include <mapnik/util/fs.hpp> #include <mapnik/util/fs.hpp>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
@ -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") ); REQUIRE( std::string(ex.what()) == std::string("JPEG Reader: libjpeg could not read image: Not a JPEG file: starts with 0x89 0x50") );
} }
#endif #endif
REQUIRE_THROWS(mapnik::image_rgba8 im(-10,-10)); // should throw rather than overflow REQUIRE_THROWS(mapnik::image_rgba8 im(-10,-10)); // should throw rather than overflow
@ -95,5 +97,17 @@ SECTION("readers") {
} // END SECTION } // 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 } // END TEST_CASE