2012-04-04 23:54:07 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-07-07 02:44:15 +02:00
|
|
|
import mapnik
|
2015-03-15 19:07:46 +01:00
|
|
|
from unittest import TestCase
|
2012-02-18 17:51:18 +01:00
|
|
|
|
2012-11-21 00:08:53 +01:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
|
2012-02-18 17:51:18 +01:00
|
|
|
|
2015-01-27 21:26:28 +01:00
|
|
|
# returns true if pixels are not nearly identical
|
|
|
|
def compare_pixels(pixel1, pixel2, alpha=True, pixel_threshold=0):
|
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))
|
2012-11-21 00:08:53 +01:00
|
|
|
if alpha:
|
|
|
|
a_diff = abs(((pixel1 >> 24) & 0xff) - ((pixel2 >> 24) & 0xff))
|
2015-01-27 21:26:28 +01:00
|
|
|
if(r_diff > pixel_threshold or
|
|
|
|
g_diff > pixel_threshold or
|
|
|
|
b_diff > pixel_threshold or
|
|
|
|
a_diff > pixel_threshold):
|
2012-11-21 00:08:53 +01:00
|
|
|
return True
|
2012-02-18 17:51:18 +01:00
|
|
|
else:
|
2015-01-27 21:26:28 +01:00
|
|
|
if(r_diff > pixel_threshold or
|
|
|
|
g_diff > pixel_threshold or
|
|
|
|
b_diff > pixel_threshold):
|
2012-11-21 00:08:53 +01:00
|
|
|
return True
|
|
|
|
return False
|
2012-02-18 17:51:18 +01:00
|
|
|
|
2012-07-07 02:44:15 +02:00
|
|
|
# compare two images and return number of different pixels
|
2013-03-25 03:56:14 +01:00
|
|
|
def compare(actual, expected, alpha=True):
|
2012-07-07 02:44:15 +02:00
|
|
|
im1 = mapnik.Image.open(actual)
|
2014-10-01 02:41:31 +02:00
|
|
|
im2 = mapnik.Image.open(expected)
|
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:
|
|
|
|
return delta_pixels
|
2015-01-29 03:20:14 +01:00
|
|
|
return im1.compare(im2, 0, alpha)
|
2012-02-18 17:51:18 +01:00
|
|
|
|
2012-11-21 00:08:53 +01:00
|
|
|
def compare_grids(actual, expected, threshold=0, alpha=True):
|
|
|
|
global errors
|
|
|
|
global passed
|
|
|
|
im1 = json.loads(open(actual).read())
|
2014-10-01 02:41:31 +02:00
|
|
|
im2 = json.loads(open(expected).read())
|
2012-11-21 00:08:53 +01:00
|
|
|
# TODO - real diffing
|
2014-10-01 02:41:31 +02:00
|
|
|
if not im1['data'] == im2['data']:
|
2013-03-25 03:56:14 +01:00
|
|
|
return 99999999
|
2014-10-01 02:41:31 +02:00
|
|
|
if not im1['keys'] == im2['keys']:
|
|
|
|
return 99999999
|
|
|
|
grid1 = im1['grid']
|
|
|
|
grid2 = im2['grid']
|
2015-03-15 19:07:46 +01:00
|
|
|
try:
|
|
|
|
assertSequenceEqual(grid1, grid2)
|
|
|
|
return 0
|
|
|
|
except:
|
|
|
|
# dimensions must be exact
|
|
|
|
width1 = len(grid1[0])
|
|
|
|
width2 = len(grid2[0])
|
|
|
|
if not width1 == width2:
|
|
|
|
return 99999999
|
|
|
|
height1 = len(grid1)
|
|
|
|
height2 = len(grid2)
|
|
|
|
if not height1 == height2:
|
|
|
|
return 99999999
|
|
|
|
diff = 0;
|
|
|
|
for y in range(0,height1-1):
|
|
|
|
row1 = grid1[y]
|
|
|
|
row2 = grid2[y]
|
|
|
|
if row1 == row2:
|
|
|
|
continue;
|
|
|
|
#width = min(len(row1),len(row2))
|
|
|
|
for w in range(0,width1):
|
|
|
|
if row1[w] != row2[w]:
|
|
|
|
diff += 1
|
|
|
|
return diff
|