Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/reST/ref/transform.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ Instead, always begin with the original image and scale to the desired size.)

.. versionchanged:: 2.3.0
Passing the calling surface as destination surface raises a ``ValueError``

.. versionchanged:: 2.3.1
Now the standard deviation of the Gaussian kernel is equal to the radius.
Blur results will be slightly different.

.. ## pygame.transform.gaussian_blur ##

Expand Down
21 changes: 10 additions & 11 deletions src_c/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -3060,7 +3060,7 @@ box_blur(SDL_Surface *src, SDL_Surface *dst, int radius, SDL_bool repeat)
}

static void
gaussian_blur(SDL_Surface *src, SDL_Surface *dst, int radius, SDL_bool repeat)
gaussian_blur(SDL_Surface *src, SDL_Surface *dst, int sigma, SDL_bool repeat)
{
Uint8 *srcpx = (Uint8 *)src->pixels;
Uint8 *dstpx = (Uint8 *)dst->pixels;
Expand All @@ -3069,21 +3069,20 @@ gaussian_blur(SDL_Surface *src, SDL_Surface *dst, int radius, SDL_bool repeat)
int dst_pitch = dst->pitch;
int src_pitch = src->pitch;
int i, j, x, y, color;
int kernel_radius = sigma * 2;
float *buf = malloc(dst_pitch * sizeof(float));
float *buf2 = malloc(dst_pitch * sizeof(float));
float *lut = malloc((radius + 1) * sizeof(float));
float *lut = malloc((kernel_radius + 1) * sizeof(float));
float lut_sum = 0.0;

for (i = 0; i <= radius; i++) { // init gaussian lut
// Gaussian function, radius=2*sigma
lut[i] = (float)radius / 2.0f *
expf(-powf((float)i, 2.0f) /
(2.0f * powf((float)radius / 2.0f, 2.0f))) *
0.3989422804014327f;
for (i = 0; i <= kernel_radius; i++) { // init gaussian lut
// Gaussian function
lut[i] =
expf(-powf((float)i, 2.0f) / (2.0f * powf((float)sigma, 2.0f)));
lut_sum += lut[i] * 2;
}
lut_sum -= lut[0];
for (i = 0; i <= radius; i++) {
for (i = 0; i <= kernel_radius; i++) {
lut[i] /= lut_sum;
}

Expand All @@ -3093,7 +3092,7 @@ gaussian_blur(SDL_Surface *src, SDL_Surface *dst, int radius, SDL_bool repeat)
}

for (y = 0; y < h; y++) {
for (j = -radius; j <= radius; j++) {
for (j = -kernel_radius; j <= kernel_radius; j++) {
for (i = 0; i < dst_pitch; i++) {
if (y + j >= 0 && y + j < h) {
buf[i] +=
Expand All @@ -3112,7 +3111,7 @@ gaussian_blur(SDL_Surface *src, SDL_Surface *dst, int radius, SDL_bool repeat)
}

for (x = 0; x < w; x++) {
for (j = -radius; j <= radius; j++) {
for (j = -kernel_radius; j <= kernel_radius; j++) {
for (color = 0; color < nb; color++) {
if (x + j >= 0 && x + j < w) {
buf2[nb * x + color] +=
Expand Down
56 changes: 28 additions & 28 deletions test/transform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1502,36 +1502,36 @@ def test_box_blur(self):

def test_gaussian_blur(self):
data1 = {
(10, 49): (109, 107, 48, 255),
(47, 66): (134, 138, 66, 255),
(84, 103): (139, 74, 47, 255),
(121, 140): (152, 129, 69, 255),
(148, 177): (162, 185, 91, 255),
(195, 214): (167, 125, 62, 255),
(232, 21): (153, 51, 36, 255),
(269, 288): (120, 163, 95, 255),
(306, 325): (115, 160, 92, 255),
(343, 362): (109, 145, 66, 255),
(384, 379): (112, 146, 71, 255),
(417, 436): (123, 83, 48, 255),
(454, 473): (170, 94, 74, 255),
(491, 510): (75, 42, 33, 255),
(10, 49): (74, 67, 32, 255),
(47, 66): (112, 93, 47, 255),
(84, 103): (141, 104, 57, 255),
(121, 140): (153, 118, 64, 255),
(148, 177): (161, 140, 73, 255),
(195, 214): (162, 137, 72, 255),
(232, 21): (124, 50, 35, 255),
(269, 288): (123, 146, 83, 255),
(306, 325): (118, 154, 85, 255),
(343, 362): (114, 141, 73, 255),
(384, 379): (114, 116, 61, 255),
(417, 436): (121, 78, 47, 255),
(454, 473): (107, 54, 39, 255),
(491, 510): (57, 28, 22, 255),
}
data2 = {
(10, 49): (109, 107, 48, 255),
(47, 66): (134, 138, 66, 255),
(84, 103): (139, 74, 47, 255),
(121, 140): (152, 129, 69, 255),
(148, 177): (162, 185, 91, 255),
(195, 214): (167, 125, 62, 255),
(232, 21): (153, 51, 36, 255),
(269, 288): (120, 163, 95, 255),
(306, 325): (115, 160, 92, 255),
(343, 362): (109, 145, 66, 255),
(384, 379): (112, 146, 71, 255),
(417, 436): (123, 83, 48, 255),
(454, 473): (170, 94, 74, 255),
(491, 510): (75, 42, 33, 255),
(10, 49): (74, 67, 32, 255),
(47, 66): (112, 93, 47, 255),
(84, 103): (141, 104, 57, 255),
(121, 140): (153, 118, 64, 255),
(148, 177): (161, 140, 73, 255),
(195, 214): (162, 137, 72, 255),
(232, 21): (124, 50, 35, 255),
(269, 288): (123, 146, 83, 255),
(306, 325): (118, 154, 85, 255),
(343, 362): (114, 141, 73, 255),
(384, 379): (114, 116, 61, 255),
(417, 436): (121, 78, 47, 255),
(454, 473): (107, 54, 39, 255),
(491, 510): (57, 28, 22, 255),
}

data_fname = example_path("data")
Expand Down