2012-04-04 23:54:07 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-02-18 17:51:18 +01:00
|
|
|
import sys
|
2012-07-07 02:44:15 +02:00
|
|
|
import mapnik
|
2012-02-18 17:51:18 +01:00
|
|
|
|
|
|
|
COMPUTE_THRESHOLD = 16
|
|
|
|
|
|
|
|
errors = []
|
2012-04-04 23:54:07 +02:00
|
|
|
passed = 0
|
2012-02-18 17:51:18 +01:00
|
|
|
|
|
|
|
# returns true if pixels are not identical
|
|
|
|
def compare_pixels(pixel1, pixel2):
|
2012-07-07 02:44:15 +02:00
|
|
|
if pixel1 == pixel2:
|
|
|
|
return False
|
|
|
|
r_diff = abs((pixel1 & 0xff) - (pixel2 & 0xff))
|
|
|
|
g_diff = abs(((pixel1 >> 8) & 0xff) - ((pixel2 >> 8) & 0xff))
|
|
|
|
b_diff = abs(((pixel1 >> 16) & 0xff)- ((pixel2 >> 16) & 0xff))
|
|
|
|
a_diff = abs(((pixel1 >> 24) & 0xff) - ((pixel2 >> 24) & 0xff))
|
|
|
|
if(r_diff > COMPUTE_THRESHOLD or
|
|
|
|
g_diff > COMPUTE_THRESHOLD or
|
|
|
|
b_diff > COMPUTE_THRESHOLD or
|
|
|
|
a_diff > COMPUTE_THRESHOLD):
|
2012-02-18 17:51:18 +01:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2012-09-28 03:58:50 +02:00
|
|
|
def fail(actual,expected):
|
|
|
|
global errors
|
|
|
|
errors.append(('N/A', actual, expected))
|
|
|
|
|
2012-07-07 02:44:15 +02:00
|
|
|
# compare two images and return number of different pixels
|
|
|
|
def compare(actual, expected):
|
2012-02-18 17:51:18 +01:00
|
|
|
global errors
|
2012-04-04 23:54:07 +02:00
|
|
|
global passed
|
2012-07-07 02:44:15 +02:00
|
|
|
im1 = mapnik.Image.open(actual)
|
2012-02-20 17:42:12 +01:00
|
|
|
try:
|
2012-07-07 02:44:15 +02:00
|
|
|
im2 = mapnik.Image.open(expected)
|
2012-07-29 01:47:16 +02:00
|
|
|
except RuntimeError:
|
2012-07-07 02:44:15 +02:00
|
|
|
errors.append((None, actual, expected))
|
2012-02-20 17:42:12 +01:00
|
|
|
return -1
|
2012-02-18 17:51:18 +01:00
|
|
|
diff = 0
|
2012-07-07 02:44:15 +02:00
|
|
|
pixels = im1.width() * im1.height()
|
|
|
|
delta_pixels = (im2.width() * im2.height()) - pixels
|
2012-03-08 18:29:46 +01:00
|
|
|
if delta_pixels != 0:
|
2012-07-07 02:44:15 +02:00
|
|
|
errors.append((delta_pixels, actual, expected))
|
2012-03-08 18:29:46 +01:00
|
|
|
return delta_pixels
|
2012-07-07 02:44:15 +02:00
|
|
|
for x in range(0,im1.width(),2):
|
|
|
|
for y in range(0,im1.height(),2):
|
|
|
|
if compare_pixels(im1.get_pixel(x,y),im2.get_pixel(x,y)):
|
|
|
|
diff += 1
|
2012-02-18 17:51:18 +01:00
|
|
|
if diff != 0:
|
2012-07-07 02:44:15 +02:00
|
|
|
errors.append((diff, actual, expected))
|
2012-04-04 23:54:07 +02:00
|
|
|
passed += 1
|
2012-02-18 17:51:18 +01:00
|
|
|
return diff
|
|
|
|
|
2012-09-26 20:27:25 +02:00
|
|
|
def summary(generate=False):
|
2012-02-18 17:51:18 +01:00
|
|
|
global errors
|
2012-04-04 23:54:07 +02:00
|
|
|
global passed
|
2012-03-23 20:42:11 +01:00
|
|
|
print "-"*80
|
2012-09-28 04:18:07 +02:00
|
|
|
print "Visual text rendering summary:"
|
|
|
|
print "-"*80
|
2012-02-18 17:51:18 +01:00
|
|
|
if len(errors) != 0:
|
|
|
|
for error in errors:
|
2012-06-07 22:26:50 +02:00
|
|
|
if (error[0] is None):
|
2012-09-26 20:27:25 +02:00
|
|
|
if generate:
|
|
|
|
actual = open(error[1],'r').read()
|
|
|
|
open(error[2],'wb').write(actual)
|
|
|
|
print "Generating reference image: '%s'" % error[2]
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
print "Could not verify %s: No reference image found!" % error[1]
|
2012-02-20 17:42:12 +01:00
|
|
|
else:
|
2012-09-28 04:18:07 +02:00
|
|
|
print "\x1b[34m%s different pixels\x1b[0m:\n\t%s (\x1b[31mactual\x1b[0m)\n\t%s (\x1b[32mexpected\x1b[0m)" % error
|
2012-02-18 17:51:18 +01:00
|
|
|
sys.exit(1)
|
2012-03-23 20:42:11 +01:00
|
|
|
else:
|
2012-04-04 23:54:07 +02:00
|
|
|
print 'All %s tests passed: \x1b[1;32m✓ \x1b[0m' % passed
|
2012-03-23 20:42:11 +01:00
|
|
|
sys.exit(0)
|