From c83ffc6aea0696779ade12a95036a04e779a3754 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 8 Sep 2020 18:51:58 +1000 Subject: [PATCH 1/2] Fixed effect_spread bug for zero distance --- Tests/test_image.py | 11 +++++++++++ src/libImaging/Effects.c | 16 ++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 9e664a7c8..89894c9a7 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -454,6 +454,17 @@ class TestImage: with Image.open("Tests/images/effect_spread.png") as im3: assert_image_similar(im2, im3, 110) + def test_effect_spread_zero(self): + # Arrange + im = hopper() + distance = 0 + + # Act + im2 = im.effect_spread(distance) + + # Assert + assert_image_equal(im, im2) + def test_check_size(self): # Checking that the _check_size function throws value errors when we want it to with pytest.raises(ValueError): diff --git a/src/libImaging/Effects.c b/src/libImaging/Effects.c index b9ff889d2..e09f4e9a4 100644 --- a/src/libImaging/Effects.c +++ b/src/libImaging/Effects.c @@ -133,13 +133,17 @@ ImagingEffectSpread(Imaging imIn, int distance) #define SPREAD(type, image)\ for (y = 0; y < imOut->ysize; y++) {\ for (x = 0; x < imOut->xsize; x++) {\ - int xx = x + (rand() % distance) - distance/2;\ - int yy = y + (rand() % distance) - distance/2;\ - if (xx >= 0 && xx < imIn->xsize && yy >= 0 && yy < imIn->ysize) {\ - imOut->image[yy][xx] = imIn->image[y][x];\ - imOut->image[y][x] = imIn->image[yy][xx];\ + if (distance == 0) {\ + imOut->image[y][x] = imIn->image[y][x];\ } else {\ - imOut->image[y][x] = imIn->image[y][x];\ + int xx = x + (rand() % distance) - distance/2;\ + int yy = y + (rand() % distance) - distance/2;\ + if (xx >= 0 && xx < imIn->xsize && yy >= 0 && yy < imIn->ysize) {\ + imOut->image[yy][xx] = imIn->image[y][x];\ + imOut->image[y][x] = imIn->image[yy][xx];\ + } else {\ + imOut->image[y][x] = imIn->image[y][x];\ + }\ }\ }\ } From 68da661db9fc7c29b0aab4b0b5f9d329bafc6c36 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Wed, 9 Sep 2020 10:08:46 +0300 Subject: [PATCH 2/2] Small optimisation: move distance==0 comparison to outer loop, to check once per call instead of once per pixel --- src/libImaging/Effects.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/libImaging/Effects.c b/src/libImaging/Effects.c index e09f4e9a4..e0f1d0339 100644 --- a/src/libImaging/Effects.c +++ b/src/libImaging/Effects.c @@ -131,11 +131,15 @@ ImagingEffectSpread(Imaging imIn, int distance) } #define SPREAD(type, image)\ - for (y = 0; y < imOut->ysize; y++) {\ - for (x = 0; x < imOut->xsize; x++) {\ - if (distance == 0) {\ + if (distance == 0) {\ + for (y = 0; y < imOut->ysize; y++) {\ + for (x = 0; x < imOut->xsize; x++) {\ imOut->image[y][x] = imIn->image[y][x];\ - } else {\ + }\ + }\ + } else {\ + for (y = 0; y < imOut->ysize; y++) {\ + for (x = 0; x < imOut->xsize; x++) {\ int xx = x + (rand() % distance) - distance/2;\ int yy = y + (rand() % distance) - distance/2;\ if (xx >= 0 && xx < imIn->xsize && yy >= 0 && yy < imIn->ysize) {\