2012-04-04 21:54:07 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-02-18 16:51:18 +00:00
|
|
|
import sys
|
2012-07-07 00:44:15 +00:00
|
|
|
import mapnik
|
2012-02-18 16:51:18 +00:00
|
|
|
|
2012-11-20 23:08:53 +00:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
|
2012-02-18 16:51:18 +00:00
|
|
|
COMPUTE_THRESHOLD = 16
|
|
|
|
|
|
|
|
errors = []
|
2012-04-04 21:54:07 +00:00
|
|
|
passed = 0
|
2012-02-18 16:51:18 +00:00
|
|
|
|
|
|
|
# returns true if pixels are not identical
|
2012-11-20 23:08:53 +00:00
|
|
|
def compare_pixels(pixel1, pixel2, alpha=True):
|
2012-07-07 00:44:15 +00: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))
|
2012-11-20 23:08:53 +00:00
|
|
|
if alpha:
|
|
|
|
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):
|
|
|
|
return True
|
2012-02-18 16:51:18 +00:00
|
|
|
else:
|
2012-11-20 23:08:53 +00:00
|
|
|
if(r_diff > COMPUTE_THRESHOLD or
|
|
|
|
g_diff > COMPUTE_THRESHOLD or
|
|
|
|
b_diff > COMPUTE_THRESHOLD):
|
|
|
|
return True
|
|
|
|
return False
|
2012-02-18 16:51:18 +00:00
|
|
|
|
2012-09-28 22:07:33 +00:00
|
|
|
def fail(actual,expected,message):
|
2012-09-28 01:58:50 +00:00
|
|
|
global errors
|
2012-09-28 22:07:33 +00:00
|
|
|
errors.append((message, actual, expected))
|
2012-09-28 01:58:50 +00:00
|
|
|
|
2012-07-07 00:44:15 +00:00
|
|
|
# compare two images and return number of different pixels
|
2012-11-20 23:08:53 +00:00
|
|
|
def compare(actual, expected, threshold=0, alpha=True):
|
2012-02-18 16:51:18 +00:00
|
|
|
global errors
|
2012-04-04 21:54:07 +00:00
|
|
|
global passed
|
2012-07-07 00:44:15 +00:00
|
|
|
im1 = mapnik.Image.open(actual)
|
2012-02-20 16:42:12 +00:00
|
|
|
try:
|
2012-07-07 00:44:15 +00:00
|
|
|
im2 = mapnik.Image.open(expected)
|
2012-07-28 23:47:16 +00:00
|
|
|
except RuntimeError:
|
2012-07-07 00:44:15 +00:00
|
|
|
errors.append((None, actual, expected))
|
2012-02-20 16:42:12 +00:00
|
|
|
return -1
|
2012-02-18 16:51:18 +00:00
|
|
|
diff = 0
|
2012-07-07 00:44:15 +00:00
|
|
|
pixels = im1.width() * im1.height()
|
|
|
|
delta_pixels = (im2.width() * im2.height()) - pixels
|
2012-03-08 17:29:46 +00:00
|
|
|
if delta_pixels != 0:
|
2012-07-07 00:44:15 +00:00
|
|
|
errors.append((delta_pixels, actual, expected))
|
2012-03-08 17:29:46 +00:00
|
|
|
return delta_pixels
|
2012-07-07 00:44:15 +00:00
|
|
|
for x in range(0,im1.width(),2):
|
|
|
|
for y in range(0,im1.height(),2):
|
2012-11-20 23:08:53 +00:00
|
|
|
if compare_pixels(im1.get_pixel(x,y),im2.get_pixel(x,y),alpha=alpha):
|
2012-07-07 00:44:15 +00:00
|
|
|
diff += 1
|
2012-11-20 23:08:53 +00:00
|
|
|
if diff > threshold: # accept one pixel different
|
2012-07-07 00:44:15 +00:00
|
|
|
errors.append((diff, actual, expected))
|
2012-04-04 21:54:07 +00:00
|
|
|
passed += 1
|
2012-02-18 16:51:18 +00:00
|
|
|
return diff
|
|
|
|
|
2012-11-20 23:08:53 +00:00
|
|
|
def compare_grids(actual, expected, threshold=0, alpha=True):
|
|
|
|
global errors
|
|
|
|
global passed
|
|
|
|
im1 = json.loads(open(actual).read())
|
|
|
|
try:
|
|
|
|
im2 = json.loads(open(expected).read())
|
|
|
|
except RuntimeError:
|
|
|
|
errors.append((None, actual, expected))
|
|
|
|
return -1
|
|
|
|
equal = (im1 == im2)
|
|
|
|
diff = 0
|
|
|
|
# TODO - real diffing
|
|
|
|
if not equal:
|
|
|
|
errors.append((1, actual, expected))
|
|
|
|
passed += 1
|
|
|
|
return diff
|
|
|
|
|
2012-09-26 18:27:25 +00:00
|
|
|
def summary(generate=False):
|
2012-02-18 16:51:18 +00:00
|
|
|
global errors
|
2012-04-04 21:54:07 +00:00
|
|
|
global passed
|
2012-09-28 22:07:33 +00:00
|
|
|
|
2012-02-18 16:51:18 +00:00
|
|
|
if len(errors) != 0:
|
2012-12-07 04:14:14 +00:00
|
|
|
msg = "\nVisual text rendering: %s failed / %s passed" % (len(errors),passed)
|
2012-09-28 22:07:33 +00:00
|
|
|
print msg
|
|
|
|
for idx,error in enumerate(errors):
|
|
|
|
if error[0] is None:
|
2012-09-26 18:27:25 +00:00
|
|
|
if generate:
|
|
|
|
actual = open(error[1],'r').read()
|
|
|
|
open(error[2],'wb').write(actual)
|
2012-11-20 23:29:26 +00:00
|
|
|
print str(idx+1) + ") Generating reference image: '%s'" % error[2]
|
2012-09-26 18:27:25 +00:00
|
|
|
continue
|
|
|
|
else:
|
2012-11-20 23:29:26 +00:00
|
|
|
print str(idx+1) + ")Could not verify %s: No reference image found!" % error[1]
|
2012-09-28 22:07:33 +00:00
|
|
|
elif isinstance(error[0],int):
|
|
|
|
print str(idx+1) + ") \x1b[34m%s different pixels\x1b[0m:\n\t%s (\x1b[31mactual\x1b[0m)\n\t%s (\x1b[32mexpected\x1b[0m)" % error
|
|
|
|
elif isinstance(error[0],str):
|
|
|
|
print str(idx+1) + ") \x1b[31mfailure to run test:\x1b[0m %s" % error[0]
|
2012-02-18 16:51:18 +00:00
|
|
|
sys.exit(1)
|
2012-03-23 19:42:11 +00:00
|
|
|
else:
|
2012-12-07 04:14:14 +00:00
|
|
|
msg = '\nAll %s visual tests passed: \x1b[1;32m✓ \x1b[0m' % passed
|
2012-09-28 22:07:33 +00:00
|
|
|
print msg
|
2012-03-23 19:42:11 +00:00
|
|
|
sys.exit(0)
|