Improve efficiency when a radius is zero

This commit is contained in:
Andrew Murray 2023-08-12 18:17:07 +10:00
parent c167d7a269
commit 9f54a11a9c
3 changed files with 32 additions and 17 deletions

View File

@ -23,6 +23,7 @@ from .helper import assert_image_equal, hopper
ImageFilter.MinFilter,
ImageFilter.ModeFilter,
ImageFilter.GaussianBlur,
ImageFilter.GaussianBlur(0),
ImageFilter.GaussianBlur(5),
ImageFilter.GaussianBlur((2, 5)),
ImageFilter.BoxBlur(0),

View File

@ -170,6 +170,8 @@ class GaussianBlur(MultibandFilter):
xy = self.radius
if not isinstance(xy, (tuple, list)):
xy = (xy, xy)
if xy == (0, 0):
return image.copy()
return image.gaussian_blur(xy)
@ -202,6 +204,8 @@ class BoxBlur(MultibandFilter):
xy = self.radius
if not isinstance(xy, (tuple, list)):
xy = (xy, xy)
if xy == (0, 0):
return image.copy()
return image.box_blur(xy)

View File

@ -258,29 +258,39 @@ ImagingBoxBlur(Imaging imOut, Imaging imIn, float xradius, float yradius, int n)
return ImagingError_ModeError();
}
imTransposed = ImagingNewDirty(imIn->mode, imIn->ysize, imIn->xsize);
if (!imTransposed) {
return NULL;
}
/* Apply blur in one dimension.
Use imOut as a destination at first pass,
then use imOut as a source too. */
ImagingHorizontalBoxBlur(imOut, imIn, xradius);
for (i = 1; i < n; i++) {
ImagingHorizontalBoxBlur(imOut, imOut, xradius);
}
/* Transpose result for blur in another direction. */
ImagingTranspose(imTransposed, imOut);
/* Reuse imTransposed as a source and destination there. */
for (i = 0; i < n; i++) {
ImagingHorizontalBoxBlur(imTransposed, imTransposed, yradius);
if (xradius != 0) {
ImagingHorizontalBoxBlur(imOut, imIn, xradius);
for (i = 1; i < n; i++) {
ImagingHorizontalBoxBlur(imOut, imOut, xradius);
}
}
/* Restore original orientation. */
ImagingTranspose(imOut, imTransposed);
if (yradius != 0) {
imTransposed = ImagingNewDirty(imIn->mode, imIn->ysize, imIn->xsize);
if (!imTransposed) {
return NULL;
}
ImagingDelete(imTransposed);
/* Transpose result for blur in another direction. */
ImagingTranspose(imTransposed, xradius == 0 ? imIn : imOut);
/* Reuse imTransposed as a source and destination there. */
for (i = 0; i < n; i++) {
ImagingHorizontalBoxBlur(imTransposed, imTransposed, yradius);
}
/* Restore original orientation. */
ImagingTranspose(imOut, imTransposed);
ImagingDelete(imTransposed);
}
if (xradius == 0 && yradius == 0) {
if (!ImagingCopy2(imOut, imIn)) {
return NULL;
}
}
return imOut;
}