diff --git a/bindings/python/mapnik_image.cpp b/bindings/python/mapnik_image.cpp index c5f9a0b69..87c9f2d7b 100644 --- a/bindings/python/mapnik_image.cpp +++ b/bindings/python/mapnik_image.cpp @@ -152,26 +152,16 @@ struct get_pixel_visitor get_pixel_visitor(unsigned x, unsigned y) : x_(x), y_(y) {} - PyObject* operator() (mapnik::image_null const&) + object operator() (mapnik::image_null const&) { throw std::runtime_error("Can not return a null image from a pixel (shouldn't have reached here)"); } - PyObject* operator() (mapnik::image_gray32f const& im) - { - return PyFloat_FromDouble(mapnik::get_pixel(im, x_, y_)); - } - - PyObject* operator() (mapnik::image_gray64f const& im) - { - return PyFloat_FromDouble(mapnik::get_pixel(im, x_, y_)); - } - template - PyObject* operator() (T const& im) + object operator() (T const& im) { using pixel_type = typename T::pixel_type; - return PyInt_FromLong(mapnik::get_pixel(im, x_, y_)); + return object(mapnik::get_pixel(im, x_, y_)); } private: @@ -179,26 +169,24 @@ struct get_pixel_visitor unsigned y_; }; -PyObject* get_pixel(mapnik::image_any const& im, unsigned x, unsigned y) +object get_pixel(mapnik::image_any const& im, unsigned x, unsigned y, bool get_color) { if (x < static_cast(im.width()) && y < static_cast(im.height())) { - return mapnik::util::apply_visitor(get_pixel_visitor(x, y), im); + if (get_color) + { + return object( + mapnik::get_pixel(im, x, y) + ); + } + else + { + return mapnik::util::apply_visitor(get_pixel_visitor(x, y), im); + } } PyErr_SetString(PyExc_IndexError, "invalid x,y for image dimensions"); boost::python::throw_error_already_set(); - return 0; -} - -mapnik::color get_pixel_color(mapnik::image_any const& im, unsigned x, unsigned y) -{ - if (x < static_cast(im.width()) && y < static_cast(im.height())) - { - return mapnik::get_pixel(im, x, y); - } - PyErr_SetString(PyExc_IndexError, "invalid x,y for image dimensions"); - boost::python::throw_error_already_set(); - return 0; + return object(); } void set_pixel_color(mapnik::image_any & im, unsigned x, unsigned y, mapnik::color const& c) @@ -447,8 +435,12 @@ void export_image() .def("set_pixel",&set_pixel_color) .def("set_pixel",&set_pixel_double) .def("set_pixel",&set_pixel_int) - .def("get_pixel",&get_pixel) - .def("get_pixel_color",&get_pixel_color) + .def("get_pixel",&get_pixel, + ( arg("self"), + arg("x"), + arg("y"), + arg("get_color")=false + )) .def("clear",&clear) //TODO(haoyu) The method name 'tostring' might be confusing since they actually return bytes in Python 3 diff --git a/tests/python_tests/image_test.py b/tests/python_tests/image_test.py index f468c8b09..d4a473d03 100644 --- a/tests/python_tests/image_test.py +++ b/tests/python_tests/image_test.py @@ -30,14 +30,14 @@ def test_image_premultiply_values(): im = mapnik.Image(256,256) im.fill(mapnik.Color(16, 33, 255, 128)) im.premultiply() - c = im.get_pixel_color(0,0) + c = im.get_pixel(0,0, True) eq_(c.r, 8) eq_(c.g, 17) eq_(c.b, 128) eq_(c.a, 128) im.demultiply() # Do to the nature of this operation the result will not be exactly the same - c = im.get_pixel_color(0,0) + c = im.get_pixel(0,0,True) eq_(c.r,15) eq_(c.g,33) eq_(c.b,255) @@ -48,7 +48,7 @@ def test_background(): eq_(im.premultiplied(), False) im.fill(mapnik.Color(32,64,125,128)) eq_(im.premultiplied(), False) - c = im.get_pixel_color(0,0) + c = im.get_pixel(0,0,True) eq_(c.get_premultiplied(), False) eq_(c.r,32) eq_(c.g,64) @@ -57,7 +57,7 @@ def test_background(): # Now again with a premultiplied alpha im.fill(mapnik.Color(32,64,125,128,True)) eq_(im.premultiplied(), True) - c = im.get_pixel_color(0,0) + c = im.get_pixel(0,0,True) eq_(c.get_premultiplied(), True) eq_(c.r,32) eq_(c.g,64) @@ -77,7 +77,7 @@ def test_set_and_get_pixel(): eq_(c0.g, c1_int.g) eq_(c0.b, c1_int.b) eq_(c0.a, c1_int.a) - c1 = im.get_pixel_color(0,0) + c1 = im.get_pixel(0,0,True) eq_(c0.r, c1.r) eq_(c0.g, c1.g) eq_(c0.b, c1.b) @@ -89,7 +89,7 @@ def test_set_and_get_pixel(): eq_(c0_pre.g, c1_int.g) eq_(c0_pre.b, c1_int.b) eq_(c0_pre.a, c1_int.a) - c1 = im.get_pixel_color(1,1) + c1 = im.get_pixel(1,1,True) eq_(c0_pre.r, c1.r) eq_(c0_pre.g, c1.g) eq_(c0_pre.b, c1.b) @@ -108,7 +108,7 @@ def test_set_and_get_pixel(): eq_(c0.g, c1_int.g) eq_(c0.b, c1_int.b) eq_(c0.a, c1_int.a) - c1 = im.get_pixel_color(0,0) + c1 = im.get_pixel(0,0,True) eq_(c0.r, c1.r) eq_(c0.g, c1.g) eq_(c0.b, c1.b) @@ -119,7 +119,7 @@ def test_set_and_get_pixel(): eq_(c0_pre.g, c1_int.g) eq_(c0_pre.b, c1_int.b) eq_(c0_pre.a, c1_int.a) - c1 = im.get_pixel_color(1,1) + c1 = im.get_pixel(1,1,True) eq_(c0_pre.r, c1.r) eq_(c0_pre.g, c1.g) eq_(c0_pre.b, c1.b) @@ -253,12 +253,12 @@ def test_get_pixel_out_of_range_2(): @raises(IndexError) def test_get_pixel_color_out_of_range_1(): im = mapnik.Image(4,4) - c = im.get_pixel_color(5,5) + c = im.get_pixel(5,5,True) @raises(OverflowError) def test_get_pixel_color_out_of_range_2(): im = mapnik.Image(4,4) - c = im.get_pixel_color(-1,1) + c = im.get_pixel(-1,1,True) def test_set_color_to_alpha(): im = mapnik.Image(256,256) diff --git a/tests/python_tests/image_tiff_test.py b/tests/python_tests/image_tiff_test.py index d8e4f6c1c..e0535d0ae 100644 --- a/tests/python_tests/image_tiff_test.py +++ b/tests/python_tests/image_tiff_test.py @@ -67,7 +67,7 @@ def test_tiff_round_trip_rows_stripped(): filepath2 = '/tmp/mapnik-tiff-io-rows_stripped2.tiff' im = mapnik.Image(255,267) im.fill(mapnik.Color('rgba(12,255,128,.5)')) - c = im.get_pixel_color(0,0) + c = im.get_pixel(0,0,True) eq_(c.r, 12) eq_(c.g, 255) eq_(c.b, 128) @@ -75,7 +75,7 @@ def test_tiff_round_trip_rows_stripped(): eq_(c.get_premultiplied(), False) im.save(filepath,'tiff:method=stripped:rows_per_strip=8') im2 = mapnik.Image.open(filepath) - c2 = im2.get_pixel_color(0,0) + c2 = im2.get_pixel(0,0,True) eq_(c2.r, 6) eq_(c2.g, 128) eq_(c2.b, 64) @@ -104,7 +104,7 @@ def test_tiff_round_trip_buffered_tiled(): filepath3 = '/tmp/mapnik-tiff-io-buffered-tiled3.tiff' im = mapnik.Image(255,267) im.fill(mapnik.Color('rgba(33,255,128,.5)')) - c = im.get_pixel_color(0,0) + c = im.get_pixel(0,0,True) eq_(c.r, 33) eq_(c.g, 255) eq_(c.b, 128) @@ -112,7 +112,7 @@ def test_tiff_round_trip_buffered_tiled(): eq_(c.get_premultiplied(), False) im.save(filepath,'tiff:method=tiled:tile_width=32:tile_height=32') im2 = mapnik.Image.open(filepath) - c2 = im2.get_pixel_color(0,0) + c2 = im2.get_pixel(0,0,True) eq_(c2.r, 17) eq_(c2.g, 128) eq_(c2.b, 64)