use mapnik.Image for visual tests - closes #1296

This commit is contained in:
Dane Springmeyer 2012-07-06 17:44:15 -07:00
parent d721cdb6b9
commit 269569250c
2 changed files with 38 additions and 20 deletions

View file

@ -118,6 +118,18 @@ bool painted(mapnik::image_32 const& im)
return im.painted(); 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) void set_pixel(mapnik::image_32 & im, unsigned x, unsigned y, mapnik::color const& c)
{ {
im.setPixel(x, y, c.rgba()); im.setPixel(x, y, c.rgba());
@ -216,6 +228,7 @@ void export_image()
.def("premultiply",&image_32::premultiply) .def("premultiply",&image_32::premultiply)
.def("demultiply",&image_32::demultiply) .def("demultiply",&image_32::demultiply)
.def("set_pixel",&set_pixel) .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 //TODO(haoyu) The method name 'tostring' might be confusing since they actually return bytes in Python 3
.def("tostring",&tostring1) .def("tostring",&tostring1)

View file

@ -1,8 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
#import math, operator
import Image
import sys import sys
import mapnik
COMPUTE_THRESHOLD = 16 COMPUTE_THRESHOLD = 16
@ -11,37 +10,43 @@ passed = 0
# returns true if pixels are not identical # returns true if pixels are not identical
def compare_pixels(pixel1, pixel2): def compare_pixels(pixel1, pixel2):
r_diff = abs(pixel1[0] - pixel2[0]) if pixel1 == pixel2:
g_diff = abs(pixel1[1] - pixel2[1]) return False
b_diff = abs(pixel1[2] - pixel2[2]) # will only work on little endian
if(r_diff > COMPUTE_THRESHOLD or g_diff > COMPUTE_THRESHOLD or b_diff > COMPUTE_THRESHOLD): 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 return True
else: else:
return False return False
# compare tow images and return number of different pixels # compare two images and return number of different pixels
def compare(fn1, fn2): def compare(actual, expected):
global errors global errors
global passed global passed
im1 = Image.open(fn1) im1 = mapnik.Image.open(actual)
try: try:
im2 = Image.open(fn2) im2 = mapnik.Image.open(expected)
except IOError: except IOError:
errors.append((None, fn1, fn2)) errors.append((None, actual, expected))
return -1 return -1
diff = 0 diff = 0
pixels = im1.size[0] * im1.size[1] pixels = im1.width() * im1.height()
delta_pixels = im2.size[0] * im2.size[1] - pixels delta_pixels = (im2.width() * im2.height()) - pixels
if delta_pixels != 0: if delta_pixels != 0:
errors.append((delta_pixels, fn1, fn2)) errors.append((delta_pixels, actual, expected))
return delta_pixels return delta_pixels
im1 = im1.getdata() for x in range(0,im1.width(),2):
im2 = im2.getdata() for y in range(0,im1.height(),2):
for i in range(3, pixels - 1, 3): if compare_pixels(im1.get_pixel(x,y),im2.get_pixel(x,y)):
if(compare_pixels(im1[i], im2[i])): diff += 1
diff = diff + 1
if diff != 0: if diff != 0:
errors.append((diff, fn1, fn2)) errors.append((diff, actual, expected))
passed += 1 passed += 1
return diff return diff