mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 09:57:43 +03:00 
			
		
		
		
	Merge pull request #970 from homm/fix-bicubic-stretch
Fix bicubic stretch interpolation
This commit is contained in:
		
						commit
						20be641329
					
				
							
								
								
									
										40
									
								
								Tests/test_imaging_stretch.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								Tests/test_imaging_stretch.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,40 @@
 | 
				
			||||||
 | 
					"""
 | 
				
			||||||
 | 
					Tests for ImagingCore.stretch functionality.
 | 
				
			||||||
 | 
					"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from helper import unittest, PillowTestCase
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from PIL import Image
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					im = Image.open("Tests/images/hopper.ppm").copy()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestImagingStretch(PillowTestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_modes(self):
 | 
				
			||||||
 | 
					        self.assertRaises(ValueError, im.convert("1").im.stretch,
 | 
				
			||||||
 | 
					                          (15, 12), Image.ANTIALIAS)
 | 
				
			||||||
 | 
					        self.assertRaises(ValueError, im.convert("P").im.stretch,
 | 
				
			||||||
 | 
					                          (15, 12), Image.ANTIALIAS)
 | 
				
			||||||
 | 
					        for mode in ["L", "I", "F", "RGB", "RGBA", "CMYK", "YCbCr"]:
 | 
				
			||||||
 | 
					            s = im.convert(mode).im
 | 
				
			||||||
 | 
					            r = s.stretch((15, 12), Image.ANTIALIAS)
 | 
				
			||||||
 | 
					            self.assertEqual(r.mode, mode)
 | 
				
			||||||
 | 
					            self.assertEqual(r.size, (15, 12))
 | 
				
			||||||
 | 
					            self.assertEqual(r.bands, s.bands)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_reduce_filters(self):
 | 
				
			||||||
 | 
					        # There is no Image.NEAREST because im.stretch implementation
 | 
				
			||||||
 | 
					        # is not NEAREST for reduction. It should be removed
 | 
				
			||||||
 | 
					        # or renamed to supersampling.
 | 
				
			||||||
 | 
					        for f in [Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
 | 
				
			||||||
 | 
					            r = im.im.stretch((15, 12), f)
 | 
				
			||||||
 | 
					            self.assertEqual(r.mode, "RGB")
 | 
				
			||||||
 | 
					            self.assertEqual(r.size, (15, 12))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_enlarge_filters(self):
 | 
				
			||||||
 | 
					        for f in [Image.BILINEAR, Image.BICUBIC, Image.ANTIALIAS]:
 | 
				
			||||||
 | 
					            r = im.im.stretch((764, 414), f)
 | 
				
			||||||
 | 
					            self.assertEqual(r.mode, "RGB")
 | 
				
			||||||
 | 
					            self.assertEqual(r.size, (764, 414))
 | 
				
			||||||
| 
						 | 
					@ -64,16 +64,14 @@ static struct filter BILINEAR = { bilinear_filter, 1.0 };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static inline float bicubic_filter(float x)
 | 
					static inline float bicubic_filter(float x)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    /* FIXME: double-check this algorithm */
 | 
					    /* http://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm */
 | 
				
			||||||
    /* FIXME: for best results, "a" should be -0.5 to -1.0, but we'll
 | 
					#define a -0.5
 | 
				
			||||||
       set it to zero for now, to match the 1.1 magnifying filter */
 | 
					 | 
				
			||||||
#define a 0.0
 | 
					 | 
				
			||||||
    if (x < 0.0)
 | 
					    if (x < 0.0)
 | 
				
			||||||
        x = -x;
 | 
					        x = -x;
 | 
				
			||||||
    if (x < 1.0)
 | 
					    if (x < 1.0)
 | 
				
			||||||
        return (((a + 2.0) * x) - (a + 3.0)) * x*x + 1;
 | 
					        return ((a + 2.0) * x - (a + 3.0)) * x*x + 1;
 | 
				
			||||||
    if (x < 2.0)
 | 
					    if (x < 2.0)
 | 
				
			||||||
        return (((a * x) - 5*a) * x + 8) * x - 4*a;
 | 
					        return (((x - 5) * x + 8) * x - 4) * a;
 | 
				
			||||||
    return 0.0;
 | 
					    return 0.0;
 | 
				
			||||||
#undef a
 | 
					#undef a
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -97,6 +95,9 @@ ImagingStretch(Imaging imOut, Imaging imIn, int filter)
 | 
				
			||||||
    if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
 | 
					    if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
 | 
				
			||||||
        return (Imaging) ImagingError_ModeError();
 | 
					        return (Imaging) ImagingError_ModeError();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0)
 | 
				
			||||||
 | 
					        return (Imaging) ImagingError_ModeError();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* check filter */
 | 
					    /* check filter */
 | 
				
			||||||
    switch (filter) {
 | 
					    switch (filter) {
 | 
				
			||||||
    case IMAGING_TRANSFORM_NEAREST:
 | 
					    case IMAGING_TRANSFORM_NEAREST:
 | 
				
			||||||
| 
						 | 
					@ -131,7 +132,6 @@ ImagingStretch(Imaging imOut, Imaging imIn, int filter)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (filterscale < 1.0) {
 | 
					    if (filterscale < 1.0) {
 | 
				
			||||||
        filterscale = 1.0;
 | 
					        filterscale = 1.0;
 | 
				
			||||||
        support = 0.5;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    support = support * filterscale;
 | 
					    support = support * filterscale;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user