Updated image_any building switch statement to support all the correct types, added tests for all the new data formats with get and set in python.

This commit is contained in:
Blake Thompson 2015-02-16 14:23:55 -06:00
parent 89a58c5879
commit 0cd9c1fc34
4 changed files with 116 additions and 8 deletions

View file

@ -156,13 +156,6 @@ struct get_pixel_visitor
{
throw std::runtime_error("Can not return a null image from a pixel (shouldn't have reached here)");
}
template <typename T>
PyObject* operator() (T const& im)
{
using pixel_type = typename T::pixel_type;
return PyInt_FromLong(mapnik::get_pixel<pixel_type>(im, x_, y_));
}
PyObject* operator() (mapnik::image_gray32f const& im)
{
@ -174,6 +167,13 @@ struct get_pixel_visitor
return PyFloat_FromDouble(mapnik::get_pixel<double>(im, x_, y_));
}
template <typename T>
PyObject* operator() (T const& im)
{
using pixel_type = typename T::pixel_type;
return PyInt_FromLong(mapnik::get_pixel<pixel_type>(im, x_, y_));
}
private:
unsigned x_;
unsigned y_;

View file

@ -285,10 +285,24 @@ inline image_any create_image_any(int width,
{
case image_dtype_gray8:
return image_any(std::move(image_gray8(width, height, initialize, premultiplied, painted)));
case image_dtype_gray8s:
return image_any(std::move(image_gray8s(width, height, initialize, premultiplied, painted)));
case image_dtype_gray16:
return image_any(std::move(image_gray16(width, height, initialize, premultiplied, painted)));
case image_dtype_gray16s:
return image_any(std::move(image_gray16s(width, height, initialize, premultiplied, painted)));
case image_dtype_gray32:
return image_any(std::move(image_gray32(width, height, initialize, premultiplied, painted)));
case image_dtype_gray32s:
return image_any(std::move(image_gray32s(width, height, initialize, premultiplied, painted)));
case image_dtype_gray32f:
return image_any(std::move(image_gray32f(width, height, initialize, premultiplied, painted)));
case image_dtype_gray64:
return image_any(std::move(image_gray64(width, height, initialize, premultiplied, painted)));
case image_dtype_gray64s:
return image_any(std::move(image_gray64s(width, height, initialize, premultiplied, painted)));
case image_dtype_gray64f:
return image_any(std::move(image_gray64f(width, height, initialize, premultiplied, painted)));
case image_dtype_null:
return image_any(std::move(image_null()));
case image_dtype_rgba8:

View file

@ -1676,7 +1676,20 @@ struct visitor_get_pixel
{
if (check_bounds(data, x_, y_))
{
return static_cast<T1>(data(x_, y_));
T1 val;
try
{
val = numeric_cast<T1>(data(x_,y_));
}
catch(negative_overflow&)
{
val = std::numeric_limits<T1>::min();
}
catch(positive_overflow&)
{
val = std::numeric_limits<T1>::max();
}
return val;
}
else
{

View file

@ -125,6 +125,78 @@ def test_set_and_get_pixel():
eq_(c0_pre.b, c1.b)
eq_(c0_pre.a, c1.a)
def test_pixel_gray8():
im = mapnik.Image(4,4,mapnik.ImageType.gray8)
val_list = range(20)
for v in val_list:
im.set_pixel(0,0, v)
eq_(im.get_pixel(0,0), v)
im.set_pixel(0,0, -v)
eq_(im.get_pixel(0,0), 0)
def test_pixel_gray8s():
im = mapnik.Image(4,4,mapnik.ImageType.gray8s)
val_list = range(20)
for v in val_list:
im.set_pixel(0,0, v)
eq_(im.get_pixel(0,0), v)
im.set_pixel(0,0, -v)
eq_(im.get_pixel(0,0), -v)
def test_pixel_gray16():
im = mapnik.Image(4,4,mapnik.ImageType.gray16)
val_list = range(20)
for v in val_list:
im.set_pixel(0,0, v)
eq_(im.get_pixel(0,0), v)
im.set_pixel(0,0, -v)
eq_(im.get_pixel(0,0), 0)
def test_pixel_gray16s():
im = mapnik.Image(4,4,mapnik.ImageType.gray16s)
val_list = range(20)
for v in val_list:
im.set_pixel(0,0, v)
eq_(im.get_pixel(0,0), v)
im.set_pixel(0,0, -v)
eq_(im.get_pixel(0,0), -v)
def test_pixel_gray32():
im = mapnik.Image(4,4,mapnik.ImageType.gray32)
val_list = range(20)
for v in val_list:
im.set_pixel(0,0, v)
eq_(im.get_pixel(0,0), v)
im.set_pixel(0,0, -v)
eq_(im.get_pixel(0,0), 0)
def test_pixel_gray32s():
im = mapnik.Image(4,4,mapnik.ImageType.gray32s)
val_list = range(20)
for v in val_list:
im.set_pixel(0,0, v)
eq_(im.get_pixel(0,0), v)
im.set_pixel(0,0, -v)
eq_(im.get_pixel(0,0), -v)
def test_pixel_gray64():
im = mapnik.Image(4,4,mapnik.ImageType.gray64)
val_list = range(20)
for v in val_list:
im.set_pixel(0,0, v)
eq_(im.get_pixel(0,0), v)
im.set_pixel(0,0, -v)
eq_(im.get_pixel(0,0), 0)
def test_pixel_gray64s():
im = mapnik.Image(4,4,mapnik.ImageType.gray64s)
val_list = range(20)
for v in val_list:
im.set_pixel(0,0, v)
eq_(im.get_pixel(0,0), v)
im.set_pixel(0,0, -v)
eq_(im.get_pixel(0,0), -v)
def test_pixel_floats():
im = mapnik.Image(4,4,mapnik.ImageType.gray32f)
val_list = [0.9, 0.99, 0.999, 0.9999, 0.99999, 1, 1.0001, 1.001, 1.01, 1.1]
@ -134,6 +206,15 @@ def test_pixel_floats():
im.set_pixel(0,0, -v)
assert_almost_equal(im.get_pixel(0,0), -v)
def test_pixel_doubles():
im = mapnik.Image(4,4,mapnik.ImageType.gray64f)
val_list = [0.9, 0.99, 0.999, 0.9999, 0.99999, 1, 1.0001, 1.001, 1.01, 1.1]
for v in val_list:
im.set_pixel(0,0, v)
assert_almost_equal(im.get_pixel(0,0), v)
im.set_pixel(0,0, -v)
assert_almost_equal(im.get_pixel(0,0), -v)
def test_pixel_overflow():
im = mapnik.Image(4,4,mapnik.ImageType.gray8)
im.set_pixel(0,0,256)