diff --git a/src_c/alphablit.c b/src_c/alphablit.c index e975218fda..a4867053b7 100644 --- a/src_c/alphablit.c +++ b/src_c/alphablit.c @@ -2841,7 +2841,8 @@ premul_surf_color_by_alpha(SDL_Surface *src, SDL_Surface *dst) // since we know dst is a copy of src we can simplify the normal checks #if !defined(__EMSCRIPTEN__) #if SDL_BYTEORDER == SDL_LIL_ENDIAN - if ((PG_SURF_BytesPerPixel(src) == 4) && pg_has_avx2()) { + if ((PG_SURF_BytesPerPixel(src) == 4) && + (src->pitch % PG_SURF_BytesPerPixel(src) == 0) && pg_has_avx2()) { premul_surf_color_by_alpha_avx2(src, dst); return 0; } @@ -2873,6 +2874,8 @@ premul_surf_color_by_alpha_non_simd(SDL_Surface *src, SDL_Surface *dst) int srcbpp = PG_FORMAT_BytesPerPixel(srcfmt); int dstbpp = PG_FORMAT_BytesPerPixel(dstfmt); Uint8 *src_pixels = (Uint8 *)src->pixels; + int srcskip = src->pitch - (width * srcbpp); + int dstskip = dst->pitch - (width * dstbpp); Uint8 *dst_pixels = (Uint8 *)dst->pixels; int srcpxskip = PG_SURF_BytesPerPixel(src); @@ -2898,6 +2901,8 @@ premul_surf_color_by_alpha_non_simd(SDL_Surface *src, SDL_Surface *dst) dst_pixels += dstpxskip; }, n, width); + src_pixels += srcskip; + dst_pixels += dstskip; } } diff --git a/src_c/simd_blitters_sse2.c b/src_c/simd_blitters_sse2.c index 65bb926721..257f48de96 100644 --- a/src_c/simd_blitters_sse2.c +++ b/src_c/simd_blitters_sse2.c @@ -793,7 +793,9 @@ premul_surf_color_by_alpha_sse2(SDL_Surface *src, SDL_Surface *dst) int width = src->w; int height = src->h; Uint32 *srcp = (Uint32 *)src->pixels; + int srcskip = src->pitch - width * PG_SURF_BytesPerPixel(src); Uint32 *dstp = (Uint32 *)dst->pixels; + int dstskip = dst->pitch - width * PG_SURF_BytesPerPixel(dst); SDL_PixelFormat *srcfmt = src->format; Uint32 amask = srcfmt->Amask; @@ -846,6 +848,8 @@ premul_surf_color_by_alpha_sse2(SDL_Surface *src, SDL_Surface *dst) ++dstp; }, n, width); + srcp = (Uint32 *)((Uint8 *)srcp + srcskip); + dstp = (Uint32 *)((Uint8 *)dstp + dstskip); } } diff --git a/test/surface_test.py b/test/surface_test.py index ca60ccb8e0..df64e6882d 100644 --- a/test/surface_test.py +++ b/test/surface_test.py @@ -4038,6 +4038,26 @@ def test_surface_premul_alpha(self): ), ) + def create_surface_from_byte_width(byte_width): + surf_height = 5 + byte_data = bytes(byte_width * surf_height) # 50 bytes + surf_width = byte_width // 4 # width = 2 + + dest = pygame.image.frombuffer( + byte_data, (surf_width, surf_height), "RGBA", pitch=byte_width + ) + dest.fill((120, 50, 70, 200)) + return dest + + test_surf = create_surface_from_byte_width(10) + test_surf = test_surf.premul_alpha() + + for y in range(0, test_surf.get_height()): + for x in range(0, test_surf.get_width()): + self.assertEqual( + test_surf.get_at((x, y)), pygame.Color(94, 39, 55, 200) + ) + def test_surface_premul_alpha_ip(self): """Ensure that .premul_alpha_ip() works correctly"""