mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-10-31 07:57:27 +03:00 
			
		
		
		
	Merge branch 'fix-blur-alpha'
This commit is contained in:
		
						commit
						e3793447fc
					
				
							
								
								
									
										
											BIN
										
									
								
								Tests/images/color_snakes.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Tests/images/color_snakes.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 1.3 KiB | 
|  | @ -5,6 +5,7 @@ from PIL import ImageOps | ||||||
| from PIL import ImageFilter | from PIL import ImageFilter | ||||||
| 
 | 
 | ||||||
| im = Image.open("Tests/images/hopper.ppm") | im = Image.open("Tests/images/hopper.ppm") | ||||||
|  | snakes = Image.open("Tests/images/color_snakes.png") | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class TestImageOpsUsm(PillowTestCase): | class TestImageOpsUsm(PillowTestCase): | ||||||
|  | @ -16,7 +17,7 @@ class TestImageOpsUsm(PillowTestCase): | ||||||
|         self.assertEqual(i.size, (128, 128)) |         self.assertEqual(i.size, (128, 128)) | ||||||
|         # i.save("blur.bmp") |         # i.save("blur.bmp") | ||||||
| 
 | 
 | ||||||
|         i = ImageOps.usm(im, 2.0, 125, 8) |         i = ImageOps.unsharp_mask(im, 2.0, 125, 8) | ||||||
|         self.assertEqual(i.mode, "RGB") |         self.assertEqual(i.mode, "RGB") | ||||||
|         self.assertEqual(i.size, (128, 128)) |         self.assertEqual(i.size, (128, 128)) | ||||||
|         # i.save("usm.bmp") |         # i.save("usm.bmp") | ||||||
|  | @ -33,7 +34,7 @@ class TestImageOpsUsm(PillowTestCase): | ||||||
|         self.assertEqual(i.mode, "RGB") |         self.assertEqual(i.mode, "RGB") | ||||||
|         self.assertEqual(i.size, (128, 128)) |         self.assertEqual(i.size, (128, 128)) | ||||||
| 
 | 
 | ||||||
|     def test_usm(self): |     def test_usm_formats(self): | ||||||
| 
 | 
 | ||||||
|         usm = ImageOps.unsharp_mask |         usm = ImageOps.unsharp_mask | ||||||
|         self.assertRaises(ValueError, lambda: usm(im.convert("1"))) |         self.assertRaises(ValueError, lambda: usm(im.convert("1"))) | ||||||
|  | @ -45,7 +46,7 @@ class TestImageOpsUsm(PillowTestCase): | ||||||
|         usm(im.convert("CMYK")) |         usm(im.convert("CMYK")) | ||||||
|         self.assertRaises(ValueError, lambda: usm(im.convert("YCbCr"))) |         self.assertRaises(ValueError, lambda: usm(im.convert("YCbCr"))) | ||||||
| 
 | 
 | ||||||
|     def test_blur(self): |     def test_blur_formats(self): | ||||||
| 
 | 
 | ||||||
|         blur = ImageOps.gaussian_blur |         blur = ImageOps.gaussian_blur | ||||||
|         self.assertRaises(ValueError, lambda: blur(im.convert("1"))) |         self.assertRaises(ValueError, lambda: blur(im.convert("1"))) | ||||||
|  | @ -57,6 +58,33 @@ class TestImageOpsUsm(PillowTestCase): | ||||||
|         blur(im.convert("CMYK")) |         blur(im.convert("CMYK")) | ||||||
|         self.assertRaises(ValueError, lambda: blur(im.convert("YCbCr"))) |         self.assertRaises(ValueError, lambda: blur(im.convert("YCbCr"))) | ||||||
| 
 | 
 | ||||||
|  |     def test_usm_accuracy(self): | ||||||
|  | 
 | ||||||
|  |         i = snakes._new(ImageOps.unsharp_mask(snakes, 5, 1024, 0)) | ||||||
|  |         # Image should not be changed because it have only 0 and 255 levels. | ||||||
|  |         self.assertEqual(i.tobytes(), snakes.tobytes()) | ||||||
|  | 
 | ||||||
|  |     def test_blur_accuracy(self): | ||||||
|  | 
 | ||||||
|  |         i = snakes._new(ImageOps.gaussian_blur(snakes, 1)) | ||||||
|  |         # Alpha channel must match whole. | ||||||
|  |         self.assertEqual(i.split()[3], snakes.split()[3]) | ||||||
|  |         # These pixels surrounded with pixels with 255 intensity. | ||||||
|  |         # They must be 255. | ||||||
|  |         for x, y, c in [(1, 0, 1), (2, 0, 1), (7, 8, 1), (8, 8, 1), (2, 9, 1), | ||||||
|  |                         (7, 3, 0), (8, 3, 0), (5, 8, 0), (5, 9, 0), (1, 3, 0), | ||||||
|  |                         (4, 3, 2), (4, 2, 2)]: | ||||||
|  |             self.assertEqual(i.im.getpixel((x, y))[c], 255) | ||||||
|  |         # Fuzzy match. | ||||||
|  |         gp = lambda x, y: i.im.getpixel((x, y)) | ||||||
|  |         self.assertTrue(211 <= gp(7, 4)[0] <= 213) | ||||||
|  |         self.assertTrue(211 <= gp(7, 5)[2] <= 213) | ||||||
|  |         self.assertTrue(211 <= gp(7, 6)[2] <= 213) | ||||||
|  |         self.assertTrue(211 <= gp(7, 7)[1] <= 213) | ||||||
|  |         self.assertTrue(211 <= gp(8, 4)[0] <= 213) | ||||||
|  |         self.assertTrue(211 <= gp(8, 5)[2] <= 213) | ||||||
|  |         self.assertTrue(211 <= gp(8, 6)[2] <= 213) | ||||||
|  |         self.assertTrue(211 <= gp(8, 7)[1] <= 213) | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     unittest.main() |     unittest.main() | ||||||
|  |  | ||||||
|  | @ -51,7 +51,7 @@ static inline UINT8 clip(double in) | ||||||
|         return (UINT8) 255; |         return (UINT8) 255; | ||||||
|     if (in <= 0.0) |     if (in <= 0.0) | ||||||
|         return (UINT8) 0; |         return (UINT8) 0; | ||||||
|     return (UINT8) in; |     return (UINT8) (in + 0.5); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static Imaging | static Imaging | ||||||
|  | @ -79,6 +79,7 @@ gblur(Imaging im, Imaging imOut, float floatRadius, int channels, int padding) | ||||||
| 
 | 
 | ||||||
|     int radius = 0; |     int radius = 0; | ||||||
|     float remainder = 0.0; |     float remainder = 0.0; | ||||||
|  |     int hasAlpha = 0; | ||||||
| 
 | 
 | ||||||
|     int i; |     int i; | ||||||
| 
 | 
 | ||||||
|  | @ -189,6 +190,10 @@ gblur(Imaging im, Imaging imOut, float floatRadius, int channels, int padding) | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     if (strcmp(im->mode, "RGBX") == 0 || strcmp(im->mode, "RGBA") == 0) { | ||||||
|  |         hasAlpha = 1; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     /* perform a blur on each column in the buffer, and place in the
 |     /* perform a blur on each column in the buffer, and place in the
 | ||||||
|        output image */ |        output image */ | ||||||
|     for (x = 0; x < im->xsize; x++) { |     for (x = 0; x < im->xsize; x++) { | ||||||
|  | @ -204,6 +209,7 @@ gblur(Imaging im, Imaging imOut, float floatRadius, int channels, int padding) | ||||||
|                     offset = -y; |                     offset = -y; | ||||||
|                 else if (y + offset >= im->ysize) |                 else if (y + offset >= im->ysize) | ||||||
|                     offset = im->ysize - y - 1; |                     offset = im->ysize - y - 1; | ||||||
|  | 
 | ||||||
|                 /* add (neighbor pixel value * maskData[pix]) to the current
 |                 /* add (neighbor pixel value * maskData[pix]) to the current
 | ||||||
|                    pixel value */ |                    pixel value */ | ||||||
|                 for (channel = 0; channel < channels; channel++) { |                 for (channel = 0; channel < channels; channel++) { | ||||||
|  | @ -215,9 +221,8 @@ gblur(Imaging im, Imaging imOut, float floatRadius, int channels, int padding) | ||||||
|             } |             } | ||||||
|             /* if the image is RGBX or RGBA, copy the 4th channel data to
 |             /* if the image is RGBX or RGBA, copy the 4th channel data to
 | ||||||
|                newPixel, so it gets put in imOut */ |                newPixel, so it gets put in imOut */ | ||||||
| 	    if (strcmp(im->mode, "RGBX") == 0 |             if (hasAlpha) { | ||||||
| 		|| strcmp(im->mode, "RGBA") == 0) { |                 newPixel[3] = (float) ((UINT8 *) & im->image32[y][x])[3]; | ||||||
| 	      newPixel[3] = (float) ((UINT8 *) & line[x + offset])[3]; |  | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             /* pack the channels into an INT32 so we can put them back in
 |             /* pack the channels into an INT32 so we can put them back in
 | ||||||
|  | @ -288,6 +293,7 @@ ImagingUnsharpMask(Imaging im, Imaging imOut, float radius, int percent, | ||||||
|     int channel = 0; |     int channel = 0; | ||||||
|     int channels = 0; |     int channels = 0; | ||||||
|     int padding = 0; |     int padding = 0; | ||||||
|  |     int hasAlpha = 0; | ||||||
| 
 | 
 | ||||||
|     int x = 0; |     int x = 0; | ||||||
|     int y = 0; |     int y = 0; | ||||||
|  | @ -332,6 +338,10 @@ ImagingUnsharpMask(Imaging im, Imaging imOut, float radius, int percent, | ||||||
| 
 | 
 | ||||||
|     ImagingSectionEnter(&cookie); |     ImagingSectionEnter(&cookie); | ||||||
| 
 | 
 | ||||||
|  |     if (strcmp(im->mode, "RGBX") == 0 || strcmp(im->mode, "RGBA") == 0) { | ||||||
|  |         hasAlpha = 1; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     for (y = 0; y < im->ysize; y++) { |     for (y = 0; y < im->ysize; y++) { | ||||||
|         if (channels == 1) { |         if (channels == 1) { | ||||||
|             lineIn8 = im->image8[y]; |             lineIn8 = im->image8[y]; | ||||||
|  | @ -380,8 +390,7 @@ ImagingUnsharpMask(Imaging im, Imaging imOut, float radius, int percent, | ||||||
|                             (channel * 8); |                             (channel * 8); | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
| 		if (strcmp(im->mode, "RGBX") == 0 |                 if (hasAlpha) { | ||||||
| 		    || strcmp(im->mode, "RGBA") == 0) { |  | ||||||
|                     /* preserve the alpha channel
 |                     /* preserve the alpha channel
 | ||||||
|                        this may not work for little-endian systems, fix it! */ |                        this may not work for little-endian systems, fix it! */ | ||||||
|                     newPixel = |                     newPixel = | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user