use mapnik.Image for visual tests - closes #1296
This commit is contained in:
parent
d721cdb6b9
commit
269569250c
2 changed files with 38 additions and 20 deletions
|
@ -118,6 +118,18 @@ bool painted(mapnik::image_32 const& im)
|
|||
return im.painted();
|
||||
}
|
||||
|
||||
unsigned get_pixel(mapnik::image_32 const& im, int x, int y)
|
||||
{
|
||||
if (x < static_cast<int>(im.width()) && y < static_cast<int>(im.height()))
|
||||
{
|
||||
mapnik::image_data_32 const & data = im.data();
|
||||
return data(x,y);
|
||||
}
|
||||
PyErr_SetString(PyExc_IndexError, "invalid x,y for image dimensions");
|
||||
boost::python::throw_error_already_set();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void set_pixel(mapnik::image_32 & im, unsigned x, unsigned y, mapnik::color const& c)
|
||||
{
|
||||
im.setPixel(x, y, c.rgba());
|
||||
|
@ -216,6 +228,7 @@ void export_image()
|
|||
.def("premultiply",&image_32::premultiply)
|
||||
.def("demultiply",&image_32::demultiply)
|
||||
.def("set_pixel",&set_pixel)
|
||||
.def("get_pixel",&get_pixel)
|
||||
//TODO(haoyu) The method name 'tostring' might be confusing since they actually return bytes in Python 3
|
||||
|
||||
.def("tostring",&tostring1)
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
#import math, operator
|
||||
import Image
|
||||
import sys
|
||||
import mapnik
|
||||
|
||||
COMPUTE_THRESHOLD = 16
|
||||
|
||||
|
@ -11,37 +10,43 @@ passed = 0
|
|||
|
||||
# returns true if pixels are not identical
|
||||
def compare_pixels(pixel1, pixel2):
|
||||
r_diff = abs(pixel1[0] - pixel2[0])
|
||||
g_diff = abs(pixel1[1] - pixel2[1])
|
||||
b_diff = abs(pixel1[2] - pixel2[2])
|
||||
if(r_diff > COMPUTE_THRESHOLD or g_diff > COMPUTE_THRESHOLD or b_diff > COMPUTE_THRESHOLD):
|
||||
if pixel1 == pixel2:
|
||||
return False
|
||||
# will only work on little endian
|
||||
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):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
# compare tow images and return number of different pixels
|
||||
def compare(fn1, fn2):
|
||||
# compare two images and return number of different pixels
|
||||
def compare(actual, expected):
|
||||
global errors
|
||||
global passed
|
||||
im1 = Image.open(fn1)
|
||||
im1 = mapnik.Image.open(actual)
|
||||
try:
|
||||
im2 = Image.open(fn2)
|
||||
im2 = mapnik.Image.open(expected)
|
||||
except IOError:
|
||||
errors.append((None, fn1, fn2))
|
||||
errors.append((None, actual, expected))
|
||||
return -1
|
||||
diff = 0
|
||||
pixels = im1.size[0] * im1.size[1]
|
||||
delta_pixels = im2.size[0] * im2.size[1] - pixels
|
||||
pixels = im1.width() * im1.height()
|
||||
delta_pixels = (im2.width() * im2.height()) - pixels
|
||||
if delta_pixels != 0:
|
||||
errors.append((delta_pixels, fn1, fn2))
|
||||
errors.append((delta_pixels, actual, expected))
|
||||
return delta_pixels
|
||||
im1 = im1.getdata()
|
||||
im2 = im2.getdata()
|
||||
for i in range(3, pixels - 1, 3):
|
||||
if(compare_pixels(im1[i], im2[i])):
|
||||
diff = diff + 1
|
||||
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
|
||||
if diff != 0:
|
||||
errors.append((diff, fn1, fn2))
|
||||
errors.append((diff, actual, expected))
|
||||
passed += 1
|
||||
return diff
|
||||
|
||||
|
|
Loading…
Reference in a new issue