From edcbd3f67d354564436d065c7f52384deda80239 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 31 Aug 2017 22:28:43 +0300 Subject: [PATCH] Tests for skipping passes (are passed if disable the passes skipping) --- Tests/test_image_resample.py | 81 ++++++++++++++++++++++++++++++++++++ _imaging.c | 4 +- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/Tests/test_image_resample.py b/Tests/test_image_resample.py index a7739c884..556a4c4d6 100644 --- a/Tests/test_image_resample.py +++ b/Tests/test_image_resample.py @@ -344,6 +344,7 @@ class CoreResamplePassesTest(PillowTestCase): cropped = im.crop(box).resize(im.size, Image.BILINEAR) self.assert_image_similar(with_box, cropped, 0.1) + class CoreResampleCoefficientsTest(PillowTestCase): def test_reduce(self): test_color = 254 @@ -458,6 +459,86 @@ class CoreResampleBoxTest(PillowTestCase): cropped = im.crop(box).resize((32, 32), resample) self.assert_image_similar(cropped, with_box, 0.4) + def test_passthrough(self): + "When no resize is required" + im = hopper() + + for size, box in [ + ((40, 50), (0, 0, 40, 50)), + ((40, 50), (0, 10, 40, 60)), + ((40, 50), (10, 0, 50, 50)), + ((40, 50), (10, 20, 50, 70)), + ]: + try: + res = im.resize(size, Image.LANCZOS, box) + self.assertEqual(res.size, size) + self.assert_image_equal(res, im.crop(box)) + except AssertionError: + print('>>>', size, box) + raise + + def test_no_passthrough(self): + "When resize is required" + im = hopper() + + for size, box in [ + ((40, 50), (0.4, 0.4, 40.4, 50.4)), + ((40, 50), (0.4, 10.4, 40.4, 60.4)), + ((40, 50), (10.4, 0.4, 50.4, 50.4)), + ((40, 50), (10.4, 20.4, 50.4, 70.4)), + ]: + try: + res = im.resize(size, Image.LANCZOS, box) + self.assertEqual(res.size, size) + with self.assertRaisesRegexp(AssertionError, "difference \d"): + # check that the difference at least that much + self.assert_image_similar(res, im.crop(box), 20) + except AssertionError: + print('>>>', size, box) + raise + + def test_skip_horizontal(self): + "Can skip resize in one dimension" + im = hopper() + + for flt in [Image.NEAREST, Image.BICUBIC]: + for size, box in [ + ((40, 50), (0, 0, 40, 90)), + ((40, 50), (0, 20, 40, 90)), + ((40, 50), (10, 0, 50, 90)), + ((40, 50), (10, 20, 50, 90)), + ]: + try: + res = im.resize(size, flt, box) + self.assertEqual(res.size, size) + # Borders should be slightly different + self.assert_image_similar( + res, im.crop(box).resize(size, flt), 0.4) + except AssertionError: + print('>>>', size, box, flt) + raise + + def test_skip_vertical(self): + "Can skip resize in one dimension" + im = hopper() + + for flt in [Image.NEAREST, Image.BICUBIC]: + for size, box in [ + ((40, 50), (0, 0, 90, 50)), + ((40, 50), (20, 0, 90, 50)), + ((40, 50), (0, 10, 90, 60)), + ((40, 50), (20, 10, 90, 60)), + ]: + try: + res = im.resize(size, flt, box) + self.assertEqual(res.size, size) + # Borders should be slightly different + self.assert_image_similar( + res, im.crop(box).resize(size, flt), 0.4) + except AssertionError: + print('>>>', size, box, flt) + raise + if __name__ == '__main__': unittest.main() diff --git a/_imaging.c b/_imaging.c index 0ed848ed8..b7726a2cc 100644 --- a/_imaging.c +++ b/_imaging.c @@ -1530,8 +1530,8 @@ _resize(ImagingObject* self, PyObject* args) } // If box's coordinates are int and box size matches requested size - if (box[0] - (int) box[0] == 0 && box[1] - (int) box[1] == 0 - && box[2] - box[0] == xsize && box[3] - box[1] == ysize) { + if (box[0] - (int) box[0] == 0 && box[2] - box[0] == xsize + && box[1] - (int) box[1] == 0 && box[3] - box[1] == ysize) { imOut = ImagingCrop(imIn, box[0], box[1], box[2], box[3]); } else if (filter == IMAGING_TRANSFORM_NEAREST) {