Skip to content

Commit b742938

Browse files
committed
Add test, fix for unusual pitch in premul_alpha()
fixes #2750
1 parent b484849 commit b742938

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src_c/alphablit.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2841,7 +2841,8 @@ premul_surf_color_by_alpha(SDL_Surface *src, SDL_Surface *dst)
28412841
// since we know dst is a copy of src we can simplify the normal checks
28422842
#if !defined(__EMSCRIPTEN__)
28432843
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
2844-
if ((PG_SURF_BytesPerPixel(src) == 4) && pg_has_avx2()) {
2844+
if ((PG_SURF_BytesPerPixel(src) == 4) &&
2845+
(src->pitch % PG_SURF_BytesPerPixel(src) == 0) && pg_has_avx2()) {
28452846
premul_surf_color_by_alpha_avx2(src, dst);
28462847
return 0;
28472848
}
@@ -2873,6 +2874,8 @@ premul_surf_color_by_alpha_non_simd(SDL_Surface *src, SDL_Surface *dst)
28732874
int srcbpp = PG_FORMAT_BytesPerPixel(srcfmt);
28742875
int dstbpp = PG_FORMAT_BytesPerPixel(dstfmt);
28752876
Uint8 *src_pixels = (Uint8 *)src->pixels;
2877+
int srcskip = src->pitch - (width * srcbpp);
2878+
int dstskip = dst->pitch - (width * dstbpp);
28762879
Uint8 *dst_pixels = (Uint8 *)dst->pixels;
28772880

28782881
int srcpxskip = PG_SURF_BytesPerPixel(src);
@@ -2898,6 +2901,8 @@ premul_surf_color_by_alpha_non_simd(SDL_Surface *src, SDL_Surface *dst)
28982901
dst_pixels += dstpxskip;
28992902
},
29002903
n, width);
2904+
src_pixels += srcskip;
2905+
dst_pixels += dstskip;
29012906
}
29022907
}
29032908

src_c/simd_blitters_sse2.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,9 @@ premul_surf_color_by_alpha_sse2(SDL_Surface *src, SDL_Surface *dst)
783783
int width = src->w;
784784
int height = src->h;
785785
Uint32 *srcp = (Uint32 *)src->pixels;
786+
int srcskip = src->pitch - width * PG_SURF_BytesPerPixel(src);
786787
Uint32 *dstp = (Uint32 *)dst->pixels;
788+
int dstskip = dst->pitch - width * PG_SURF_BytesPerPixel(dst);
787789

788790
SDL_PixelFormat *srcfmt = src->format;
789791
Uint32 amask = srcfmt->Amask;
@@ -836,6 +838,8 @@ premul_surf_color_by_alpha_sse2(SDL_Surface *src, SDL_Surface *dst)
836838
++dstp;
837839
},
838840
n, width);
841+
(Uint8 *)srcp += srcskip;
842+
(Uint8 *)dstp += dstskip;
839843
}
840844
}
841845

test/surface_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4029,6 +4029,26 @@ def test_surface_premul_alpha(self):
40294029
),
40304030
)
40314031

4032+
def create_surface_from_byte_width(byte_width):
4033+
surf_height = 5
4034+
byte_data = bytes(byte_width * surf_height) # 50 bytes
4035+
surf_width = byte_width // 4 # width = 2
4036+
4037+
dest = pygame.image.frombuffer(
4038+
byte_data, (surf_width, surf_height), "RGBA", pitch=byte_width
4039+
)
4040+
dest.fill((120, 50, 70, 200))
4041+
return dest
4042+
4043+
test_surf = create_surface_from_byte_width(10)
4044+
test_surf = test_surf.premul_alpha()
4045+
4046+
for y in range(0, test_surf.get_height()):
4047+
for x in range(0, test_surf.get_width()):
4048+
self.assertEqual(
4049+
test_surf.get_at((x, y)), pygame.Color(94, 39, 55, 200)
4050+
)
4051+
40324052

40334053
class SurfaceSelfBlitTest(unittest.TestCase):
40344054
"""Blit to self tests.

0 commit comments

Comments
 (0)