Skip to content

Commit

Permalink
update tests after mapnik#1515 - many are not ideal due to mapnik#1519
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer authored and PetrDlouhy committed Aug 21, 2013
1 parent 0552251 commit 1b65c1f
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 37 deletions.
Binary file added tests/data/images/yellow_half_trans.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 72 additions & 20 deletions tests/python_tests/compositing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from nose.tools import *
import os,sys
from utilities import execution_path
from utilities import Todo
from utilities import execution_path, Todo, get_unique_colors, pixel2channels
import mapnik

def setup():
Expand All @@ -18,10 +17,7 @@ def debug_image(image,step=2):
for x in range(0,image.width(),step):
for y in range(0,image.height(),step):
pixel = image.get_pixel(x,y)
alpha = (pixel >> 24) & 0xff
red = pixel & 0xff
green = (pixel >> 8) & 0xff
blue = (pixel >> 16) & 0xff
red,green,blue,alpha = pixel2channels(pixel)
print "rgba(%s,%s,%s,%s) at %s,%s" % (red,green,blue,alpha,x,y)

# note: it is impossible to know for all pixel colors
Expand All @@ -33,14 +29,11 @@ def validate_pixels_are_not_premultiplied(image):
for x in range(0,image.width(),2):
for y in range(0,image.height(),2):
pixel = image.get_pixel(x,y)
alpha = (pixel >> 24) & 0xff
red,green,blue,alpha = pixel2channels(pixel)
if alpha > 0:
transparent = False
if alpha < 255:
fully_opaque = False
red = pixel & 0xff
green = (pixel >> 8) & 0xff
blue = (pixel >> 16) & 0xff
color_max = max(red,green,blue)
if color_max > alpha:
over_alpha = True
Expand All @@ -51,12 +44,9 @@ def validate_pixels_are_not_premultiplied2(image):
for x in range(0,image.width(),2):
for y in range(0,image.height(),2):
pixel = image.get_pixel(x,y)
alpha = (pixel >> 24) & 0xff
red,green,blue,alpha = pixel2channels(pixel)
#each value of the color channels will never be bigger than that of the alpha channel.
if alpha > 0:
red = pixel & 0xff
green = (pixel >> 8) & 0xff
blue = (pixel >> 16) & 0xff
if red > 0 and red > alpha:
print 'red: %s, a: %s' % (red,alpha)
looks_not_multiplied = True
Expand All @@ -67,12 +57,9 @@ def validate_pixels_are_premultiplied(image):
for x in range(0,image.width(),2):
for y in range(0,image.height(),2):
pixel = image.get_pixel(x,y)
alpha = (pixel >> 24) & 0xff
red,green,blue,alpha = pixel2channels(pixel)
if alpha > 0:
pixel = image.get_pixel(x,y)
red = pixel & 0xff
green = (pixel >> 8) & 0xff
blue = (pixel >> 16) & 0xff
is_valid = ((0 <= red <= alpha) and is_pre(red,alpha)) \
and ((0 <= green <= alpha) and is_pre(green,alpha)) \
and ((0 <= blue <= alpha) and is_pre(blue,alpha)) \
Expand All @@ -96,10 +83,10 @@ def test_compare_images():
expected = 'images/composited/' + name + '.png'
valid = validate_pixels_are_premultiplied(a)
if not valid[0]:
print '%s not validly pre-:\n\t%s pixels (%s)' % (name,len(valid[1]),valid[1][0])
fails.append('%s not validly premultiplied!:\n\t %s pixels (%s)' % (name,len(valid[1]),valid[1][0]))
a.demultiply()
if not validate_pixels_are_not_premultiplied(a):
print '%s not validly demultiplied' % (name)
fails.append('%s not validly demultiplied' % (name))
a.save(actual)
if not os.path.exists(expected):
print 'generating expected test image: %s' % expected
Expand Down Expand Up @@ -167,6 +154,71 @@ def test_style_level_opacity():
expected_im = mapnik.Image.open(expected)
eq_(im.tostring(),expected_im.tostring(), 'failed comparing actual (%s) and expected (%s)' % (actual,'tests/python_tests/'+ expected))

def test_rounding_and_color_expectations():
m = mapnik.Map(1,1)
m.background = mapnik.Color('rgba(255,255,255,.4999999)')
im = mapnik.Image(m.width,m.height)
mapnik.render(m,im)
# ugh 252, see: https://github.com/mapnik/mapnik/issues/1519
eq_(get_unique_colors(im),['rgba(252,252,252,127)'])
m = mapnik.Map(1,1)
m.background = mapnik.Color('rgba(255,255,255,.5)')
im = mapnik.Image(m.width,m.height)
mapnik.render(m,im)
eq_(get_unique_colors(im),['rgba(253,253,253,128)'])
im_file = mapnik.Image.open('../data/images/stripes_pattern.png')
eq_(get_unique_colors(im_file),['rgba(0,0,0,0)', 'rgba(74,74,74,255)'])
# should have no effect
im_file.premultiply()
eq_(get_unique_colors(im_file),['rgba(0,0,0,0)', 'rgba(74,74,74,255)'])
im_file.set_alpha(.5)
# should have effect now that image has transparency
im_file.premultiply()
eq_(get_unique_colors(im_file),['rgba(0,0,0,0)', 'rgba(37,37,37,127)'])
# should restore to original nonpremultiplied colors
im_file.demultiply()
eq_(get_unique_colors(im_file),['rgba(0,0,0,0)', 'rgba(74,74,74,127)'])


def test_background_image_and_background_color():
m = mapnik.Map(8,8)
m.background = mapnik.Color('rgba(255,255,255,.5)')
m.background_image = '../data/images/stripes_pattern.png'
im = mapnik.Image(m.width,m.height)
mapnik.render(m,im)
# note: data loss due to rounding as per https://github.com/mapnik/mapnik/issues/1519
# means that background will roundtrip to 253 not 255
#eq_(get_unique_colors(im),['rgba(255,255,255,128)', 'rgba(74,74,74,255)'])
eq_(get_unique_colors(im),['rgba(253,253,253,128)', 'rgba(74,74,74,255)'])

def test_background_image_with_alpha_and_background_color():
m = mapnik.Map(10,10)
m.background = mapnik.Color('rgba(255,255,255,.5)')
m.background_image = '../data/images/yellow_half_trans.png'
im = mapnik.Image(m.width,m.height)
mapnik.render(m,im)
eq_(get_unique_colors(im),['rgba(255,255,85,191)'])

def test_background_image_with_alpha_and_background_color_against_composited_control():
m = mapnik.Map(10,10)
m.background = mapnik.Color('rgba(255,255,255,.5)')
m.background_image = '../data/images/yellow_half_trans.png'
im = mapnik.Image(m.width,m.height)
mapnik.render(m,im)
# create and composite the expected result
im1 = mapnik.Image(10,10)
im1.background = mapnik.Color('rgba(255,255,255,.5)')
im1.premultiply()
im2 = mapnik.Image(10,10)
im2.background = mapnik.Color('rgba(255,255,0,.5)')
im2.premultiply()
im1.composite(im2)
im1.demultiply()
# compare image rendered (compositing in `agg_renderer<T>::setup`)
# vs image composited via python bindings
raise Todo("looks like we need to investigate PNG rounding when saving")
eq_(get_unique_colors(im),get_unique_colors(im1))

if __name__ == "__main__":
setup()
[eval(run)() for run in dir() if 'test_' in run]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 1 addition & 17 deletions tests/python_tests/raster_symbolizer_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python

from nose.tools import *
from utilities import execution_path, contains_word
from utilities import execution_path, contains_word, get_unique_colors

import os, mapnik

Expand Down Expand Up @@ -103,22 +103,6 @@ def test_load_save_map():
if not 'Could not create datasource' in str(e):
raise RuntimeError(str(e))

def pixel2rgba(pixel):
alpha = (pixel >> 24) & 0xff
red = pixel & 0xff
green = (pixel >> 8) & 0xff
blue = (pixel >> 16) & 0xff
return 'rgba(%s,%s,%s,%s)' % (red,green,blue,alpha)

def get_unique_colors(im):
pixels = []
for x in range(im.width()):
for y in range(im.height()):
pixel = im.get_pixel(x,y)
if pixel not in pixels:
pixels.append(pixel)
return map(pixel2rgba,pixels)

def test_raster_with_alpha_blends_correctly_with_background():
WIDTH = 500
HEIGHT = 500
Expand Down
20 changes: 20 additions & 0 deletions tests/python_tests/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,23 @@ def contains_word(word, bytestring_):
assert len(bytestring_)%n == 0, "len(bytestring_) not multiple of len(word)"
chunks = [bytestring_[i:i+n] for i in xrange(0, len(bytestring_), n)]
return word in chunks

def pixel2channels(pixel):
alpha = (pixel >> 24) & 0xff
red = pixel & 0xff
green = (pixel >> 8) & 0xff
blue = (pixel >> 16) & 0xff
return red,green,blue,alpha

def pixel2rgba(pixel):
return 'rgba(%s,%s,%s,%s)' % pixel2channels(pixel)

def get_unique_colors(im):
pixels = []
for x in range(im.width()):
for y in range(im.height()):
pixel = im.get_pixel(x,y)
if pixel not in pixels:
pixels.append(pixel)
pixels = sorted(pixels)
return map(pixel2rgba,pixels)

0 comments on commit 1b65c1f

Please sign in to comment.