Merge pull request #1954 from uploadcare/skip-resampling-passes

Do not do unnecessary passes on resizing
This commit is contained in:
wiredfool 2016-06-30 12:23:07 +01:00 committed by GitHub
commit 291c00c58a
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

@ -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;
}