Do not make unnecessary passes on resizing

This commit is contained in:
homm 2016-06-15 00:55:57 +03:00
parent b3591351f6
commit 78fad0f819
2 changed files with 42 additions and 9 deletions

View File

@ -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()

View File

@ -494,8 +494,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);
@ -542,15 +542,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;
}