Skip to content

Commit 5ea9f5e

Browse files
authored
Merge pull request #2028 from yunline/transform.blur-fix
transform.blur: fix #2027
2 parents b30a4b9 + 0b679b5 commit 5ea9f5e

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src_c/transform.c

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,39 +3074,41 @@ box_blur(SDL_Surface *src, SDL_Surface *dst, int radius, SDL_bool repeat)
30743074
Uint8 *srcpx = (Uint8 *)src->pixels;
30753075
Uint8 *dstpx = (Uint8 *)dst->pixels;
30763076
Uint8 nb = src->format->BytesPerPixel;
3077-
int w = dst->w, h = dst->h, pitch = dst->pitch;
3077+
int w = dst->w, h = dst->h;
3078+
int dst_pitch = dst->pitch;
3079+
int src_pitch = src->pitch;
30783080
int i, x, y, color;
3079-
Uint32 *buf = malloc(pitch * sizeof(Uint32));
3080-
Uint32 *sum_v = malloc(pitch * sizeof(Uint32));
3081+
Uint32 *buf = malloc(dst_pitch * sizeof(Uint32));
3082+
Uint32 *sum_v = malloc(dst_pitch * sizeof(Uint32));
30813083
Uint32 *sum_h = malloc(nb * sizeof(Uint32));
30823084

3083-
memset(sum_v, 0, pitch * sizeof(Uint32));
3085+
memset(sum_v, 0, dst_pitch * sizeof(Uint32));
30843086
for (y = 0; y <= radius; y++) { // y-pre
3085-
for (i = 0; i < pitch; i++) {
3086-
sum_v[i] += srcpx[pitch * y + i];
3087+
for (i = 0; i < dst_pitch; i++) {
3088+
sum_v[i] += srcpx[src_pitch * y + i];
30873089
}
30883090
}
30893091
if (repeat) {
3090-
for (i = 0; i < pitch; i++) {
3092+
for (i = 0; i < dst_pitch; i++) {
30913093
sum_v[i] += srcpx[i] * radius;
30923094
}
30933095
}
30943096
for (y = 0; y < h; y++) { // y
3095-
for (i = 0; i < pitch; i++) {
3097+
for (i = 0; i < dst_pitch; i++) {
30963098
buf[i] = sum_v[i] / (radius * 2 + 1);
30973099

30983100
// update vertical sum
30993101
if (y - radius >= 0) {
3100-
sum_v[i] -= srcpx[pitch * (y - radius) + i];
3102+
sum_v[i] -= srcpx[src_pitch * (y - radius) + i];
31013103
}
31023104
else if (repeat) {
31033105
sum_v[i] -= srcpx[i];
31043106
}
31053107
if (y + radius + 1 < h) {
3106-
sum_v[i] += srcpx[pitch * (y + radius + 1) + i];
3108+
sum_v[i] += srcpx[src_pitch * (y + radius + 1) + i];
31073109
}
31083110
else if (repeat) {
3109-
sum_v[i] += srcpx[pitch * (h - 1) + i];
3111+
sum_v[i] += srcpx[src_pitch * (h - 1) + i];
31103112
}
31113113
}
31123114

@@ -3123,7 +3125,7 @@ box_blur(SDL_Surface *src, SDL_Surface *dst, int radius, SDL_bool repeat)
31233125
}
31243126
for (x = 0; x < w; x++) { // x
31253127
for (color = 0; color < nb; color++) {
3126-
dstpx[pitch * y + nb * x + color] =
3128+
dstpx[dst_pitch * y + nb * x + color] =
31273129
sum_h[color] / (radius * 2 + 1);
31283130

31293131
// update horizontal sum
@@ -3154,10 +3156,12 @@ gaussian_blur(SDL_Surface *src, SDL_Surface *dst, int radius, SDL_bool repeat)
31543156
Uint8 *srcpx = (Uint8 *)src->pixels;
31553157
Uint8 *dstpx = (Uint8 *)dst->pixels;
31563158
Uint8 nb = src->format->BytesPerPixel;
3157-
int w = dst->w, h = dst->h, pitch = dst->pitch;
3159+
int w = dst->w, h = dst->h;
3160+
int dst_pitch = dst->pitch;
3161+
int src_pitch = src->pitch;
31583162
int i, j, x, y, color;
3159-
float *buf = malloc(pitch * sizeof(float));
3160-
float *buf2 = malloc(pitch * sizeof(float));
3163+
float *buf = malloc(dst_pitch * sizeof(float));
3164+
float *buf2 = malloc(dst_pitch * sizeof(float));
31613165
float *lut = malloc((radius + 1) * sizeof(float));
31623166
float lut_sum = 0.0;
31633167

@@ -3174,24 +3178,25 @@ gaussian_blur(SDL_Surface *src, SDL_Surface *dst, int radius, SDL_bool repeat)
31743178
lut[i] /= lut_sum;
31753179
}
31763180

3177-
for (i = 0; i < pitch; i++) {
3181+
for (i = 0; i < dst_pitch; i++) {
31783182
buf[i] = 0.0;
31793183
buf2[i] = 0.0;
31803184
}
31813185

31823186
for (y = 0; y < h; y++) {
31833187
for (j = -radius; j <= radius; j++) {
3184-
for (i = 0; i < pitch; i++) {
3188+
for (i = 0; i < dst_pitch; i++) {
31853189
if (y + j >= 0 && y + j < h) {
3186-
buf[i] += (float)srcpx[pitch * (y + j) + i] * lut[abs(j)];
3190+
buf[i] +=
3191+
(float)srcpx[src_pitch * (y + j) + i] * lut[abs(j)];
31873192
}
31883193
else if (repeat) {
31893194
if (y + j < 0) {
31903195
buf[i] += (float)srcpx[i] * lut[abs(j)];
31913196
}
31923197
else {
3193-
buf[i] +=
3194-
(float)srcpx[pitch * (h - 1) + i] * lut[abs(j)];
3198+
buf[i] += (float)srcpx[src_pitch * (h - 1) + i] *
3199+
lut[abs(j)];
31953200
}
31963201
}
31973202
}
@@ -3216,8 +3221,8 @@ gaussian_blur(SDL_Surface *src, SDL_Surface *dst, int radius, SDL_bool repeat)
32163221
}
32173222
}
32183223
}
3219-
for (i = 0; i < pitch; i++) {
3220-
dstpx[pitch * y + i] = (Uint8)buf2[i];
3224+
for (i = 0; i < dst_pitch; i++) {
3225+
dstpx[dst_pitch * y + i] = (Uint8)buf2[i];
32213226
buf[i] = 0.0;
32223227
buf2[i] = 0.0;
32233228
}

0 commit comments

Comments
 (0)