From c011524b6732c9cd82d0c9f149aaef342039d213 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Tue, 2 Jul 2013 16:01:23 -0400 Subject: [PATCH] add mapnik.Image.premultiplied() to query status of pixel premultiplication --- CHANGELOG.md | 4 ++++ bindings/python/mapnik_image.cpp | 1 + include/mapnik/graphics.hpp | 6 ++++++ src/graphics.cpp | 11 ++++++++--- tests/python_tests/image_test.py | 8 ++++++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99206016a..571db49a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ Developers: Please commit along with changes. For a complete change history, see the git log. +## Future + +- Added `premultiplied` property on mapnik::image_32 / mapnik.Image to enable knowledge of premultiplied status of image buffer. + ## 2.2.0 Released June 3rd, 2013 diff --git a/bindings/python/mapnik_image.cpp b/bindings/python/mapnik_image.cpp index 5dde3c86c..5b3db5e9e 100644 --- a/bindings/python/mapnik_image.cpp +++ b/bindings/python/mapnik_image.cpp @@ -272,6 +272,7 @@ void export_image() arg("mode")=mapnik::src_over, arg("opacity")=1.0f )) + .def("premultiplied",&image_32::premultiplied) .def("premultiply",&image_32::premultiply) .def("demultiply",&image_32::demultiply) .def("set_pixel",&set_pixel) diff --git a/include/mapnik/graphics.hpp b/include/mapnik/graphics.hpp index dff2cbe42..ae7681304 100644 --- a/include/mapnik/graphics.hpp +++ b/include/mapnik/graphics.hpp @@ -54,6 +54,7 @@ private: boost::optional background_; image_data_32 data_; bool painted_; + bool premultiplied_; public: image_32(int width,int height); image_32(image_32 const& rhs); @@ -72,6 +73,11 @@ public: return painted_; } + bool premultiplied() const + { + return premultiplied_; + } + inline void clear() { std::memset(data_.getData(),0,sizeof(mapnik::image_data_32::pixel_type)*data_.width()*data_.height()); diff --git a/src/graphics.cpp b/src/graphics.cpp index 06dfce493..5119cf397 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -45,19 +45,22 @@ image_32::image_32(int width,int height) :width_(width), height_(height), data_(width,height), - painted_(false) {} + painted_(false), + premultiplied_(false) {} image_32::image_32(const image_32& rhs) :width_(rhs.width_), height_(rhs.height_), data_(rhs.data_), - painted_(rhs.painted_) {} + painted_(rhs.painted_), + premultiplied_(rhs.premultiplied_) {} #ifdef HAVE_CAIRO image_32::image_32(cairo_surface_ptr const& surface) :width_(cairo_image_surface_get_width(&*surface)), height_(cairo_image_surface_get_height(&*surface)), - data_(width_, height_) + data_(width_, height_), + premultiplied_(false) { painted_ = true; if ( cairo_image_surface_get_format(&*surface) != CAIRO_FORMAT_ARGB32) @@ -193,6 +196,7 @@ void image_32::premultiply() agg::rendering_buffer buffer(data_.getBytes(),width_,height_,width_ * 4); agg::pixfmt_rgba32 pixf(buffer); pixf.premultiply(); + premultiplied_ = true; } void image_32::demultiply() @@ -200,6 +204,7 @@ void image_32::demultiply() agg::rendering_buffer buffer(data_.getBytes(),width_,height_,width_ * 4); agg::pixfmt_rgba32 pixf(buffer); pixf.demultiply(); + premultiplied_ = false; } void image_32::composite_pixel(unsigned op, int x,int y, unsigned c, unsigned cover, double opacity) diff --git a/tests/python_tests/image_test.py b/tests/python_tests/image_test.py index 595d5213f..fa75b32eb 100644 --- a/tests/python_tests/image_test.py +++ b/tests/python_tests/image_test.py @@ -12,6 +12,14 @@ def setup(): # from another directory we need to chdir() os.chdir(execution_path('.')) +def test_image_premultiply(): + im = mapnik.Image(256,256) + eq_(im.premultiplied(),False) + im.premultiply() + eq_(im.premultiplied(),True) + im.demultiply() + eq_(im.premultiplied(),False) + @raises(RuntimeError) def test_negative_image_dimensions(): im = mapnik.Image(-40,40)