Permalink
Browse files

update tests after #1515 - many are not ideal due to #1519

  • Loading branch information...
1 parent e0bcf44 commit df501a2fa3e5a80f40e6e11de1fe202a8d64f04a Dane Springmeyer committed Oct 4, 2012
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -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():
@@ -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
@@ -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
@@ -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
@@ -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)) \
@@ -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
@@ -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]
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -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
@@ -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
@@ -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 df501a2

Please sign in to comment.