Merge branch 'fix-blur-alpha'

This commit is contained in:
homm 2014-10-08 17:40:57 +04:00
commit e3793447fc
3 changed files with 221 additions and 184 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -5,6 +5,7 @@ from PIL import ImageOps
from PIL import ImageFilter
im = Image.open("Tests/images/hopper.ppm")
snakes = Image.open("Tests/images/color_snakes.png")
class TestImageOpsUsm(PillowTestCase):
@ -16,7 +17,7 @@ class TestImageOpsUsm(PillowTestCase):
self.assertEqual(i.size, (128, 128))
# 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.size, (128, 128))
# i.save("usm.bmp")
@ -33,7 +34,7 @@ class TestImageOpsUsm(PillowTestCase):
self.assertEqual(i.mode, "RGB")
self.assertEqual(i.size, (128, 128))
def test_usm(self):
def test_usm_formats(self):
usm = ImageOps.unsharp_mask
self.assertRaises(ValueError, lambda: usm(im.convert("1")))
@ -45,7 +46,7 @@ class TestImageOpsUsm(PillowTestCase):
usm(im.convert("CMYK"))
self.assertRaises(ValueError, lambda: usm(im.convert("YCbCr")))
def test_blur(self):
def test_blur_formats(self):
blur = ImageOps.gaussian_blur
self.assertRaises(ValueError, lambda: blur(im.convert("1")))
@ -57,6 +58,33 @@ class TestImageOpsUsm(PillowTestCase):
blur(im.convert("CMYK"))
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__':
unittest.main()

View File

@ -51,7 +51,7 @@ static inline UINT8 clip(double in)
return (UINT8) 255;
if (in <= 0.0)
return (UINT8) 0;
return (UINT8) in;
return (UINT8) (in + 0.5);
}
static Imaging
@ -79,6 +79,7 @@ gblur(Imaging im, Imaging imOut, float floatRadius, int channels, int padding)
int radius = 0;
float remainder = 0.0;
int hasAlpha = 0;
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
output image */
for (x = 0; x < im->xsize; x++) {
@ -204,6 +209,7 @@ gblur(Imaging im, Imaging imOut, float floatRadius, int channels, int padding)
offset = -y;
else if (y + offset >= im->ysize)
offset = im->ysize - y - 1;
/* add (neighbor pixel value * maskData[pix]) to the current
pixel value */
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
newPixel, so it gets put in imOut */
if (strcmp(im->mode, "RGBX") == 0
|| strcmp(im->mode, "RGBA") == 0) {
newPixel[3] = (float) ((UINT8 *) & line[x + offset])[3];
if (hasAlpha) {
newPixel[3] = (float) ((UINT8 *) & im->image32[y][x])[3];
}
/* 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 channels = 0;
int padding = 0;
int hasAlpha = 0;
int x = 0;
int y = 0;
@ -332,6 +338,10 @@ ImagingUnsharpMask(Imaging im, Imaging imOut, float radius, int percent,
ImagingSectionEnter(&cookie);
if (strcmp(im->mode, "RGBX") == 0 || strcmp(im->mode, "RGBA") == 0) {
hasAlpha = 1;
}
for (y = 0; y < im->ysize; y++) {
if (channels == 1) {
lineIn8 = im->image8[y];
@ -380,8 +390,7 @@ ImagingUnsharpMask(Imaging im, Imaging imOut, float radius, int percent,
(channel * 8);
}
}
if (strcmp(im->mode, "RGBX") == 0
|| strcmp(im->mode, "RGBA") == 0) {
if (hasAlpha) {
/* preserve the alpha channel
this may not work for little-endian systems, fix it! */
newPixel =