From a67e5453d5353c597a1e1d4483c371b1b7ca4f2f Mon Sep 17 00:00:00 2001 From: homm Date: Thu, 16 Jun 2016 21:57:02 +0300 Subject: [PATCH] add tests, fix errors ) --- Tests/test_image_resample.py | 73 +++++++++++++++++++++++++++++++++++- Tests/test_image_resize.py | 9 +++-- libImaging/Resample.c | 2 +- 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/Tests/test_image_resample.py b/Tests/test_image_resample.py index 68c4ce402..1483d15a1 100644 --- a/Tests/test_image_resample.py +++ b/Tests/test_image_resample.py @@ -10,7 +10,7 @@ class TestImagingResampleVulnerability(PillowTestCase): ysize = 1000 # unimportant try: # any resampling filter will do here - im.im.resize((xsize, ysize), Image.LINEAR) + im.im.resize((xsize, ysize), Image.BILINEAR) self.fail("Resize should raise MemoryError on invalid xsize") except MemoryError: self.assertTrue(True, "Should raise MemoryError") @@ -89,6 +89,15 @@ class TestImagingCoreResampleAccuracy(PillowTestCase): for y in range(image.size[1]) ) + def test_reduce_box(self): + for mode in ['RGBX', 'RGB', 'La', 'L']: + case = self.make_case(mode, (8, 8), 0xe1) + case = case.resize((4, 4), Image.BOX) + data = ('e1 e1' + 'e1 e1') + for channel in case.split(): + self.check_case(channel, self.make_sample(data, (4, 4))) + def test_reduce_bilinear(self): for mode in ['RGBX', 'RGB', 'La', 'L']: case = self.make_case(mode, (8, 8), 0xe1) @@ -98,6 +107,15 @@ class TestImagingCoreResampleAccuracy(PillowTestCase): for channel in case.split(): self.check_case(channel, self.make_sample(data, (4, 4))) + def test_reduce_hamming(self): + for mode in ['RGBX', 'RGB', 'La', 'L']: + case = self.make_case(mode, (8, 8), 0xe1) + case = case.resize((4, 4), Image.HAMMING) + data = ('e1 da' + 'da d3') + for channel in case.split(): + self.check_case(channel, self.make_sample(data, (4, 4))) + def test_reduce_bicubic(self): for mode in ['RGBX', 'RGB', 'La', 'L']: case = self.make_case(mode, (12, 12), 0xe1) @@ -108,6 +126,16 @@ class TestImagingCoreResampleAccuracy(PillowTestCase): for channel in case.split(): self.check_case(channel, self.make_sample(data, (6, 6))) + def test_reduce_mitchell(self): + for mode in ['RGBX', 'RGB', 'La', 'L']: + case = self.make_case(mode, (12, 12), 0xe1) + case = case.resize((6, 6), Image.MITCHELL) + data = ('e1 e2 cc' + 'e2 e3 cd' + 'cc cd bb') + for channel in case.split(): + self.check_case(channel, self.make_sample(data, (6, 6))) + def test_reduce_lanczos(self): for mode in ['RGBX', 'RGB', 'La', 'L']: case = self.make_case(mode, (16, 16), 0xe1) @@ -119,6 +147,15 @@ class TestImagingCoreResampleAccuracy(PillowTestCase): for channel in case.split(): self.check_case(channel, self.make_sample(data, (8, 8))) + def test_enlarge_box(self): + for mode in ['RGBX', 'RGB', 'La', 'L']: + case = self.make_case(mode, (2, 2), 0xe1) + case = case.resize((4, 4), Image.BOX) + data = ('e1 e1' + 'e1 e1') + for channel in case.split(): + self.check_case(channel, self.make_sample(data, (4, 4))) + def test_enlarge_bilinear(self): for mode in ['RGBX', 'RGB', 'La', 'L']: case = self.make_case(mode, (2, 2), 0xe1) @@ -128,6 +165,17 @@ class TestImagingCoreResampleAccuracy(PillowTestCase): for channel in case.split(): self.check_case(channel, self.make_sample(data, (4, 4))) + def test_enlarge_hamming(self): + for mode in ['RGBX', 'RGB', 'La', 'L']: + case = self.make_case(mode, (4, 4), 0xe1) + case = case.resize((8, 8), Image.HAMMING) + data = ('e1 e1 ea d1' + 'e1 e1 ea d1' + 'ea ea f4 d9' + 'd1 d1 d9 c4') + for channel in case.split(): + self.check_case(channel, self.make_sample(data, (8, 8))) + def test_enlarge_bicubic(self): for mode in ['RGBX', 'RGB', 'La', 'L']: case = self.make_case(mode, (4, 4), 0xe1) @@ -139,6 +187,17 @@ class TestImagingCoreResampleAccuracy(PillowTestCase): for channel in case.split(): self.check_case(channel, self.make_sample(data, (8, 8))) + def test_enlarge_mitchell(self): + for mode in ['RGBX', 'RGB', 'La', 'L']: + case = self.make_case(mode, (4, 4), 0xe1) + case = case.resize((8, 8), Image.MITCHELL) + data = ('e1 e4 e6 b2' + 'e4 e7 e9 b3' + 'e6 e9 eb b4' + 'b2 b3 b5 9a') + for channel in case.split(): + self.check_case(channel, self.make_sample(data, (8, 8))) + def test_enlarge_lanczos(self): for mode in ['RGBX', 'RGB', 'La', 'L']: case = self.make_case(mode, (6, 6), 0xe1) @@ -211,15 +270,21 @@ class CoreResampleAlphaCorrectTest(PillowTestCase): @unittest.skip("current implementation isn't precise enough") def test_levels_rgba(self): case = self.make_levels_case('RGBA') + self.run_levels_case(case.resize((512, 32), Image.BOX)) self.run_levels_case(case.resize((512, 32), Image.BILINEAR)) + self.run_levels_case(case.resize((512, 32), Image.HAMMING)) self.run_levels_case(case.resize((512, 32), Image.BICUBIC)) + self.run_levels_case(case.resize((512, 32), Image.MITCHELL)) self.run_levels_case(case.resize((512, 32), Image.LANCZOS)) @unittest.skip("current implementation isn't precise enough") def test_levels_la(self): case = self.make_levels_case('LA') + self.run_levels_case(case.resize((512, 32), Image.BOX)) self.run_levels_case(case.resize((512, 32), Image.BILINEAR)) + self.run_levels_case(case.resize((512, 32), Image.HAMMING)) self.run_levels_case(case.resize((512, 32), Image.BICUBIC)) + self.run_levels_case(case.resize((512, 32), Image.MITCHELL)) self.run_levels_case(case.resize((512, 32), Image.LANCZOS)) def make_dity_case(self, mode, clean_pixel, dirty_pixel): @@ -243,14 +308,20 @@ class CoreResampleAlphaCorrectTest(PillowTestCase): def test_dirty_pixels_rgba(self): case = self.make_dity_case('RGBA', (255, 255, 0, 128), (0, 0, 255, 0)) + self.run_dity_case(case.resize((20, 20), Image.BOX), (255, 255, 0)) self.run_dity_case(case.resize((20, 20), Image.BILINEAR), (255, 255, 0)) + self.run_dity_case(case.resize((20, 20), Image.HAMMING), (255, 255, 0)) self.run_dity_case(case.resize((20, 20), Image.BICUBIC), (255, 255, 0)) + self.run_dity_case(case.resize((20, 20), Image.MITCHELL), (255, 255, 0)) self.run_dity_case(case.resize((20, 20), Image.LANCZOS), (255, 255, 0)) def test_dirty_pixels_la(self): case = self.make_dity_case('LA', (255, 128), (0, 0)) + self.run_dity_case(case.resize((20, 20), Image.BOX), (255,)) self.run_dity_case(case.resize((20, 20), Image.BILINEAR), (255,)) + self.run_dity_case(case.resize((20, 20), Image.HAMMING), (255,)) self.run_dity_case(case.resize((20, 20), Image.BICUBIC), (255,)) + self.run_dity_case(case.resize((20, 20), Image.MITCHELL), (255,)) self.run_dity_case(case.resize((20, 20), Image.LANCZOS), (255,)) diff --git a/Tests/test_image_resize.py b/Tests/test_image_resize.py index d59f8ba14..567fd219a 100644 --- a/Tests/test_image_resize.py +++ b/Tests/test_image_resize.py @@ -39,13 +39,15 @@ class TestImagingCoreResize(PillowTestCase): self.assertEqual(r.im.bands, im.im.bands) def test_reduce_filters(self): - for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.LANCZOS]: + for f in [Image.LINEAR, Image.BOX, Image.BILINEAR, Image.HAMMING, + Image.BICUBIC, Image.LANCZOS]: r = self.resize(hopper("RGB"), (15, 12), f) self.assertEqual(r.mode, "RGB") self.assertEqual(r.size, (15, 12)) def test_enlarge_filters(self): - for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.LANCZOS]: + for f in [Image.LINEAR, Image.BOX, Image.BILINEAR, Image.HAMMING, + Image.BICUBIC, Image.LANCZOS]: r = self.resize(hopper("RGB"), (212, 195), f) self.assertEqual(r.mode, "RGB") self.assertEqual(r.size, (212, 195)) @@ -64,7 +66,8 @@ class TestImagingCoreResize(PillowTestCase): } samples['dirty'].putpixel((1, 1), 128) - for f in [Image.LINEAR, Image.BILINEAR, Image.BICUBIC, Image.LANCZOS]: + for f in [Image.LINEAR, Image.BOX, Image.BILINEAR, Image.HAMMING, + Image.BICUBIC, Image.LANCZOS]: # samples resized with current filter references = dict( (name, self.resize(ch, (4, 4), f)) diff --git a/libImaging/Resample.c b/libImaging/Resample.c index 9010f3239..feecc7066 100644 --- a/libImaging/Resample.c +++ b/libImaging/Resample.c @@ -87,7 +87,7 @@ static inline double lanczos_filter(double x) return 0.0; } -static struct filter BOX = { bilinear_filter, 0.5 }; +static struct filter BOX = { box_filter, 0.5 }; static struct filter BILINEAR = { bilinear_filter, 1.0 }; static struct filter HAMMING = { hamming_filter, 1.0 }; static struct filter BICUBIC = { bicubic_filter, 2.0 };