Skip to content
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

SDLTest_CompareSurfaces: Decode pixels correctly on big-endian platforms #8317

Merged
merged 1 commit into from
Sep 29, 2023
Merged
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
26 changes: 24 additions & 2 deletions src/test/SDL_test_compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@
/* Counter for _CompareSurface calls; used for filename creation when comparisons fail */
static int _CompareSurfaceCount = 0;

static Uint32
GetPixel(Uint8 *p, size_t bytes_per_pixel)
{
Uint32 ret = 0;

SDL_assert(bytes_per_pixel <= sizeof(ret));

/* Fill the appropriate number of least-significant bytes of ret,
* leaving the most-significant bytes set to zero, so that ret can
* be decoded with SDL_GetRGBA afterwards. */
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
SDL_memcpy(((Uint8 *) &ret) + (sizeof(ret) - bytes_per_pixel), p, bytes_per_pixel);
#else
SDL_memcpy(&ret, p, bytes_per_pixel);
#endif

return ret;
}

/* Compare surfaces */
int SDLTest_CompareSurfaces(SDL_Surface *surface, SDL_Surface *referenceSurface, int allowable_error)
{
Expand Down Expand Up @@ -71,11 +90,14 @@ int SDLTest_CompareSurfaces(SDL_Surface *surface, SDL_Surface *referenceSurface,
/* Compare image - should be same format. */
for (j = 0; j < surface->h; j++) {
for (i = 0; i < surface->w; i++) {
Uint32 pixel;
p = (Uint8 *)surface->pixels + j * surface->pitch + i * bpp;
p_reference = (Uint8 *)referenceSurface->pixels + j * referenceSurface->pitch + i * bpp_reference;

SDL_GetRGBA(*(Uint32 *)p, surface->format, &R, &G, &B, &A);
SDL_GetRGBA(*(Uint32 *)p_reference, referenceSurface->format, &Rd, &Gd, &Bd, &Ad);
pixel = GetPixel(p, bpp);
SDL_GetRGBA(pixel, surface->format, &R, &G, &B, &A);
pixel = GetPixel(p_reference, bpp_reference);
SDL_GetRGBA(pixel, referenceSurface->format, &Rd, &Gd, &Bd, &Ad);

dist = 0;
dist += (R - Rd) * (R - Rd);
Expand Down