Skip to content

Add test & fix for unusual pitch in surface.premul_alpha() #2882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src_c/alphablit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src_c/simd_blitters_sse2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/surface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down
Loading