im/view.is_solid method in python bindings to match node-mapnik - closes #1728
This commit is contained in:
parent
b5a74292a5
commit
662ff26f94
3 changed files with 107 additions and 36 deletions
|
@ -107,6 +107,28 @@ bool painted(mapnik::image_32 const& im)
|
|||
return im.painted();
|
||||
}
|
||||
|
||||
bool is_solid(mapnik::image_32 const& im)
|
||||
{
|
||||
if (im.width() > 0 && im.height() > 0)
|
||||
{
|
||||
mapnik::image_data_32 const & data = im.data();
|
||||
mapnik::image_data_32::pixel_type const* first_row = data.getRow(0);
|
||||
mapnik::image_data_32::pixel_type const first_pixel = first_row[0];
|
||||
for (unsigned y = 0; y < im.height(); ++y)
|
||||
{
|
||||
mapnik::image_data_32::pixel_type const * row = data.getRow(y);
|
||||
for (unsigned x = 0; x < im.width(); ++x)
|
||||
{
|
||||
if (first_pixel != row[x])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned get_pixel(mapnik::image_32 const& im, int x, int y)
|
||||
{
|
||||
if (x < static_cast<int>(im.width()) && y < static_cast<int>(im.height()))
|
||||
|
@ -206,6 +228,7 @@ void export_image()
|
|||
.def("height",&image_32::height)
|
||||
.def("view",&image_32::get_view)
|
||||
.def("painted",&painted)
|
||||
.def("is_solid",&is_solid)
|
||||
.add_property("background",make_function
|
||||
(&image_32::get_background,return_value_policy<copy_const_reference>()),
|
||||
&image_32::set_background, "The background color of the image.")
|
||||
|
|
|
@ -76,6 +76,27 @@ PyObject* view_tostring3(image_view<image_data_32> const & view, std::string con
|
|||
(s.data(),s.size());
|
||||
}
|
||||
|
||||
bool is_solid(image_view<image_data_32> const& view)
|
||||
{
|
||||
if (view.width() > 0 && view.height() > 0)
|
||||
{
|
||||
mapnik::image_view<image_data_32>::pixel_type const* first_row = view.getRow(0);
|
||||
mapnik::image_view<image_data_32>::pixel_type const first_pixel = first_row[0];
|
||||
for (unsigned y = 0; y < view.height(); ++y)
|
||||
{
|
||||
mapnik::image_view<image_data_32>::pixel_type const * row = view.getRow(y);
|
||||
for (unsigned x = 0; x < view.width(); ++x)
|
||||
{
|
||||
if (first_pixel != row[x])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void save_view1(image_view<image_data_32> const& view,
|
||||
std::string const& filename)
|
||||
{
|
||||
|
@ -104,6 +125,7 @@ void export_image_view()
|
|||
class_<image_view<image_data_32> >("ImageView","A view into an image.",no_init)
|
||||
.def("width",&image_view<image_data_32>::width)
|
||||
.def("height",&image_view<image_data_32>::height)
|
||||
.def("is_solid",&is_solid)
|
||||
.def("tostring",&view_tostring1)
|
||||
.def("tostring",&view_tostring2)
|
||||
.def("tostring",&view_tostring3)
|
||||
|
|
|
@ -12,56 +12,76 @@ def setup():
|
|||
# from another directory we need to chdir()
|
||||
os.chdir(execution_path('.'))
|
||||
|
||||
|
||||
def test_simplest_render():
|
||||
m = mapnik.Map(256, 256)
|
||||
i = mapnik.Image(m.width, m.height)
|
||||
|
||||
mapnik.render(m, i)
|
||||
|
||||
s = i.tostring()
|
||||
|
||||
im = mapnik.Image(m.width, m.height)
|
||||
eq_(im.painted(),False)
|
||||
eq_(im.is_solid(),True)
|
||||
mapnik.render(m, im)
|
||||
eq_(im.painted(),False)
|
||||
eq_(im.is_solid(),True)
|
||||
s = im.tostring()
|
||||
eq_(s, 256 * 256 * '\x00\x00\x00\x00')
|
||||
|
||||
def test_render_image_to_string():
|
||||
i = mapnik.Image(256, 256)
|
||||
|
||||
i.background = mapnik.Color('black')
|
||||
|
||||
s = i.tostring()
|
||||
|
||||
im = mapnik.Image(256, 256)
|
||||
im.background = mapnik.Color('black')
|
||||
eq_(im.painted(),False)
|
||||
eq_(im.is_solid(),True)
|
||||
s = im.tostring()
|
||||
eq_(s, 256 * 256 * '\x00\x00\x00\xff')
|
||||
s = im.tostring('png')
|
||||
|
||||
s = i.tostring('png')
|
||||
def test_non_solid_image():
|
||||
im = mapnik.Image(256, 256)
|
||||
im.background = mapnik.Color('black')
|
||||
eq_(im.painted(),False)
|
||||
eq_(im.is_solid(),True)
|
||||
# set one pixel to a different color
|
||||
im.set_pixel(0,0,mapnik.Color('white'))
|
||||
eq_(im.painted(),False)
|
||||
eq_(im.is_solid(),False)
|
||||
|
||||
def test_non_solid_image_view():
|
||||
im = mapnik.Image(256, 256)
|
||||
im.background = mapnik.Color('black')
|
||||
view = im.view(0,0,256,256)
|
||||
eq_(view.is_solid(),True)
|
||||
# set one pixel to a different color
|
||||
im.set_pixel(0,0,mapnik.Color('white'))
|
||||
eq_(im.is_solid(),False)
|
||||
# view, since it is the exact dimensions of the image
|
||||
# should also be non-solid
|
||||
eq_(view.is_solid(),False)
|
||||
# but not a view that excludes the single diff pixel
|
||||
view2 = im.view(1,1,256,256)
|
||||
eq_(view2.is_solid(),True)
|
||||
|
||||
def test_setting_alpha():
|
||||
w,h = 256,256
|
||||
im1 = mapnik.Image(w,h)
|
||||
# white, half transparent
|
||||
im1.background = mapnik.Color('rgba(255,255,255,.5)')
|
||||
|
||||
eq_(im1.painted(),False)
|
||||
eq_(im1.is_solid(),True)
|
||||
# pure white
|
||||
im2 = mapnik.Image(w,h)
|
||||
im2.background = mapnik.Color('rgba(255,255,255,1)')
|
||||
im2.set_alpha(.5)
|
||||
|
||||
eq_(im2.painted(),False)
|
||||
eq_(im2.is_solid(),True)
|
||||
eq_(len(im1.tostring()), len(im2.tostring()))
|
||||
|
||||
|
||||
def test_render_image_to_file():
|
||||
i = mapnik.Image(256, 256)
|
||||
|
||||
i.background = mapnik.Color('black')
|
||||
|
||||
im = mapnik.Image(256, 256)
|
||||
im.background = mapnik.Color('black')
|
||||
if mapnik.has_jpeg():
|
||||
i.save('test.jpg')
|
||||
i.save('test.png', 'png')
|
||||
|
||||
im.save('test.jpg')
|
||||
im.save('test.png', 'png')
|
||||
if os.path.exists('test.jpg'):
|
||||
os.remove('test.jpg')
|
||||
else:
|
||||
return False
|
||||
|
||||
if os.path.exists('test.png'):
|
||||
os.remove('test.png')
|
||||
else:
|
||||
|
@ -71,34 +91,32 @@ def get_paired_images(w,h,mapfile):
|
|||
tmp_map = 'tmp_map.xml'
|
||||
m = mapnik.Map(w,h)
|
||||
mapnik.load_map(m,mapfile)
|
||||
i = mapnik.Image(w,h)
|
||||
im = mapnik.Image(w,h)
|
||||
m.zoom_all()
|
||||
mapnik.render(m,i)
|
||||
mapnik.render(m,im)
|
||||
mapnik.save_map(m,tmp_map)
|
||||
m2 = mapnik.Map(w,h)
|
||||
mapnik.load_map(m2,tmp_map)
|
||||
i2 = mapnik.Image(w,h)
|
||||
im2 = mapnik.Image(w,h)
|
||||
m2.zoom_all()
|
||||
mapnik.render(m2,i2)
|
||||
mapnik.render(m2,im2)
|
||||
os.remove(tmp_map)
|
||||
return i,i2
|
||||
return im,im2
|
||||
|
||||
def test_render_from_serialization():
|
||||
try:
|
||||
i,i2 = get_paired_images(100,100,'../data/good_maps/building_symbolizer.xml')
|
||||
eq_(i.tostring(),i2.tostring())
|
||||
im,im2 = get_paired_images(100,100,'../data/good_maps/building_symbolizer.xml')
|
||||
eq_(im.tostring(),im2.tostring())
|
||||
|
||||
i,i2 = get_paired_images(100,100,'../data/good_maps/polygon_symbolizer.xml')
|
||||
eq_(i.tostring(),i2.tostring())
|
||||
im,im2 = get_paired_images(100,100,'../data/good_maps/polygon_symbolizer.xml')
|
||||
eq_(im.tostring(),im2.tostring())
|
||||
except RuntimeError, e:
|
||||
# only test datasources that we have installed
|
||||
if not 'Could not create datasource' in str(e):
|
||||
raise RuntimeError(e)
|
||||
|
||||
def test_render_points():
|
||||
|
||||
if not mapnik.has_cairo(): return
|
||||
|
||||
# create and populate point datasource (WGS84 lat-lon coordinates)
|
||||
ds = mapnik.MemoryDatasource()
|
||||
context = mapnik.Context()
|
||||
|
@ -163,6 +181,14 @@ def test_render_with_scale_factor():
|
|||
for size in sizes:
|
||||
im = mapnik.Image(256, 256)
|
||||
mapnik.render(m,im,size)
|
||||
expected_file = './images/support/marker-text-line-scale-factor-%s.png' % size
|
||||
actual_file = '/tmp/' + os.path.basename(expected_file)
|
||||
im.save(actual_file,'png8')
|
||||
#im.save(expected_file,'png8')
|
||||
# we save and re-open here so both png8 images are ready as full color png
|
||||
actual = mapnik.Image.open(expected_file)
|
||||
expected = mapnik.Image.open(expected_file)
|
||||
eq_(actual.tostring(),expected.tostring(), 'failed comparing actual (%s) and expected (%s)' % (actual_file,expected_file))
|
||||
im.save('./images/support/marker-text-line-scale-factor-%s.png' % size,'png8')
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Add table
Reference in a new issue