repeat all affine tests with PERSPECTIVE

This commit is contained in:
homm 2016-07-11 00:26:12 +03:00
parent ad3f7238d2
commit 1321713688
2 changed files with 24 additions and 12 deletions

View File

@ -142,6 +142,8 @@ class TestImageTransform(PillowTestCase):
class TestImageTransformAffine(PillowTestCase): class TestImageTransformAffine(PillowTestCase):
transform = Image.AFFINE
def _test_image(self): def _test_image(self):
im = hopper('RGB') im = hopper('RGB')
return im.crop((10, 20, im.width - 10, im.height - 20)) return im.crop((10, 20, im.width - 10, im.height - 20))
@ -152,7 +154,8 @@ class TestImageTransformAffine(PillowTestCase):
angle = - math.radians(angle) angle = - math.radians(angle)
matrix = [ matrix = [
round(math.cos(angle), 15), round(math.sin(angle), 15), 0.0, round(math.cos(angle), 15), round(math.sin(angle), 15), 0.0,
round(-math.sin(angle), 15), round(math.cos(angle), 15), 0.0 round(-math.sin(angle), 15), round(math.cos(angle), 15), 0.0,
0, 0,
] ]
matrix[2] = (1 - matrix[0] - matrix[1]) * im.width / 2 matrix[2] = (1 - matrix[0] - matrix[1]) * im.width / 2
matrix[5] = (1 - matrix[3] - matrix[4]) * im.height / 2 matrix[5] = (1 - matrix[3] - matrix[4]) * im.height / 2
@ -162,7 +165,7 @@ class TestImageTransformAffine(PillowTestCase):
else: else:
transposed = im transposed = im
for resample in [Image.NEAREST, Image.BILINEAR, Image.BICUBIC]: for resample in [Image.NEAREST, Image.BILINEAR, Image.BICUBIC]:
transformed = im.transform(transposed.size, Image.AFFINE, transformed = im.transform(transposed.size, self.transform,
matrix, resample) matrix, resample)
self.assert_image_equal(transposed, transformed) self.assert_image_equal(transposed, transformed)
@ -183,16 +186,18 @@ class TestImageTransformAffine(PillowTestCase):
matrix = [ matrix = [
1 / scale, 0, 0, 1 / scale, 0, 0,
0, 1 / scale, 0, 0, 1 / scale, 0,
0, 0,
] ]
size = int(round(im.width * scale)), int(round(im.height * scale)) size = int(round(im.width * scale)), int(round(im.height * scale))
transformed = im.transform( transformed = im.transform(
size, Image.AFFINE, matrix, Image.NEAREST) size, self.transform, matrix, Image.NEAREST)
matrix = [ matrix = [
scale, 0, 0, scale, 0, 0,
0, scale, 0, 0, scale, 0,
0, 0,
] ]
transformed = transformed.transform( transformed = transformed.transform(
im.size, Image.AFFINE, matrix, Image.NEAREST) im.size, self.transform, matrix, Image.NEAREST)
self.assert_image_equal(im, transformed) self.assert_image_equal(im, transformed)
def test_resize_1_1x(self): def test_resize_1_1x(self):
@ -216,17 +221,19 @@ class TestImageTransformAffine(PillowTestCase):
matrix_up = [ matrix_up = [
1, 0, -x, 1, 0, -x,
0, 1, -y, 0, 1, -y,
0, 0,
] ]
matrix_down = [ matrix_down = [
1, 0, x, 1, 0, x,
0, 1, y, 0, 1, y,
0, 0,
] ]
for resample, epsilon in [(Image.NEAREST, 0), for resample, epsilon in [(Image.NEAREST, 0),
(Image.BILINEAR, 1.5), (Image.BICUBIC, 1)]: (Image.BILINEAR, 1.5), (Image.BICUBIC, 1)]:
transformed = im.transform( transformed = im.transform(
size_up, Image.AFFINE, matrix_up, resample) size_up, self.transform, matrix_up, resample)
transformed = transformed.transform( transformed = transformed.transform(
im.size, Image.AFFINE, matrix_down, resample) im.size, self.transform, matrix_down, resample)
self.assert_image_similar(transformed, im, epsilon * epsilonscale) self.assert_image_similar(transformed, im, epsilon * epsilonscale)
def test_translate_0_1(self): def test_translate_0_1(self):
@ -239,6 +246,11 @@ class TestImageTransformAffine(PillowTestCase):
self._test_translate(50, 50, 0) self._test_translate(50, 50, 0)
class TestImageTransformPerspective(TestImageTransformAffine):
# Repeat all tests for AFFINE transormations with PERSPECTIVE
transform = Image.PERSPECTIVE
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -299,8 +299,8 @@ quad_transform(double* xout, double* yout, int x, int y, void* data)
static int static int
nearest_filter8(void* out, Imaging im, double xin, double yin) nearest_filter8(void* out, Imaging im, double xin, double yin)
{ {
int x = COORD(xin + 0.5); int x = COORD(xin);
int y = COORD(yin + 0.5); int y = COORD(yin);
if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
return 0; return 0;
((UINT8*)out)[0] = im->image8[y][x]; ((UINT8*)out)[0] = im->image8[y][x];
@ -310,8 +310,8 @@ nearest_filter8(void* out, Imaging im, double xin, double yin)
static int static int
nearest_filter16(void* out, Imaging im, double xin, double yin) nearest_filter16(void* out, Imaging im, double xin, double yin)
{ {
int x = COORD(xin + 0.5); int x = COORD(xin);
int y = COORD(yin + 0.5); int y = COORD(yin);
if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
return 0; return 0;
((INT16*)out)[0] = ((INT16*)(im->image8[y]))[x]; ((INT16*)out)[0] = ((INT16*)(im->image8[y]))[x];
@ -321,8 +321,8 @@ nearest_filter16(void* out, Imaging im, double xin, double yin)
static int static int
nearest_filter32(void* out, Imaging im, double xin, double yin) nearest_filter32(void* out, Imaging im, double xin, double yin)
{ {
int x = COORD(xin + 0.5); int x = COORD(xin);
int y = COORD(yin + 0.5); int y = COORD(yin);
if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
return 0; return 0;
((INT32*)out)[0] = im->image32[y][x]; ((INT32*)out)[0] = im->image32[y][x];