diff --git a/Tests/test_image_resample.py b/Tests/test_image_resample.py index 9ee9ac48f..68c4ce402 100644 --- a/Tests/test_image_resample.py +++ b/Tests/test_image_resample.py @@ -254,5 +254,25 @@ class CoreResampleAlphaCorrectTest(PillowTestCase): self.run_dity_case(case.resize((20, 20), Image.LANCZOS), (255,)) +class CoreResamplePassesTest(PillowTestCase): + def test_horizontal(self): + im = hopper('L') + count = Image.core.getcount() + im.resize((im.size[0] + 10, im.size[1]), Image.BILINEAR) + self.assertEqual(Image.core.getcount(), count + 1) + + def test_vertical(self): + im = hopper('L') + count = Image.core.getcount() + im.resize((im.size[0], im.size[1] + 10), Image.BILINEAR) + self.assertEqual(Image.core.getcount(), count + 1) + + def test_both(self): + im = hopper('L') + count = Image.core.getcount() + im.resize((im.size[0] + 10, im.size[1] + 10), Image.BILINEAR) + self.assertEqual(Image.core.getcount(), count + 2) + + if __name__ == '__main__': unittest.main() diff --git a/libImaging/Resample.c b/libImaging/Resample.c index 92caa748f..54862d725 100644 --- a/libImaging/Resample.c +++ b/libImaging/Resample.c @@ -492,8 +492,8 @@ ImagingResampleVertical_32bpc(Imaging imIn, int ysize, struct filter *filterp) Imaging ImagingResample(Imaging imIn, int xsize, int ysize, int filter) { - Imaging imTemp; - Imaging imOut; + Imaging imTemp = NULL; + Imaging imOut = NULL; struct filter *filterp; Imaging (*ResampleHorizontal)(Imaging imIn, int xsize, struct filter *filterp); Imaging (*ResampleVertical)(Imaging imIn, int xsize, struct filter *filterp); @@ -540,15 +540,28 @@ ImagingResample(Imaging imIn, int xsize, int ysize, int filter) } /* two-pass resize, first pass */ - imTemp = ResampleHorizontal(imIn, xsize, filterp); - if ( ! imTemp) - return NULL; + if (imIn->xsize != xsize) { + imTemp = ResampleHorizontal(imIn, xsize, filterp); + if ( ! imTemp) + return NULL; + imOut = imIn = imTemp; + } /* second pass */ - imOut = ResampleVertical(imTemp, ysize, filterp); - ImagingDelete(imTemp); - if ( ! imOut) - return NULL; + if (imIn->ysize != ysize) { + /* imIn can be the original image or horizontally resampled one */ + imOut = ResampleVertical(imIn, ysize, filterp); + /* it's safe to call ImagingDelete with empty value + if there was no previous step. */ + ImagingDelete(imTemp); + if ( ! imOut) + return NULL; + } + + /* none of the previous steps are performed, copying */ + if ( ! imOut) { + imOut = ImagingCopy(imIn); + } return imOut; }