Skip to content

Commit

Permalink
Validate that pixel formats have the correct attributes
Browse files Browse the repository at this point in the history
Also added a test to save and load as BMP format to make sure pixel transformations are working correctly.

Fixes libsdl-org#11483
  • Loading branch information
slouken committed Nov 17, 2024
1 parent 572cc7a commit 8f62d92
Show file tree
Hide file tree
Showing 4 changed files with 676 additions and 91 deletions.
19 changes: 12 additions & 7 deletions include/SDL3/SDL_pixels.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,6 @@ typedef enum SDL_PackedLayout
(SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYF16) || \
(SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYF32)))

#define SDL_ISPIXELFORMAT_ALPHA(format) \
((SDL_ISPIXELFORMAT_PACKED(format) && \
((SDL_PIXELORDER(format) == SDL_PACKEDORDER_ARGB) || \
(SDL_PIXELORDER(format) == SDL_PACKEDORDER_RGBA) || \
(SDL_PIXELORDER(format) == SDL_PACKEDORDER_ABGR) || \
(SDL_PIXELORDER(format) == SDL_PACKEDORDER_BGRA))))

#define SDL_ISPIXELFORMAT_10BIT(format) \
(!SDL_ISPIXELFORMAT_FOURCC(format) && \
((SDL_PIXELTYPE(format) == SDL_PIXELTYPE_PACKED32) && \
Expand All @@ -218,6 +211,18 @@ typedef enum SDL_PackedLayout
((SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYF16) || \
(SDL_PIXELTYPE(format) == SDL_PIXELTYPE_ARRAYF32)))

#define SDL_ISPIXELFORMAT_ALPHA(format) \
((SDL_ISPIXELFORMAT_PACKED(format) && \
((SDL_PIXELORDER(format) == SDL_PACKEDORDER_ARGB) || \
(SDL_PIXELORDER(format) == SDL_PACKEDORDER_RGBA) || \
(SDL_PIXELORDER(format) == SDL_PACKEDORDER_ABGR) || \
(SDL_PIXELORDER(format) == SDL_PACKEDORDER_BGRA))) || \
(SDL_ISPIXELFORMAT_ARRAY(format) && \
((SDL_PIXELORDER(format) == SDL_ARRAYORDER_ARGB) || \
(SDL_PIXELORDER(format) == SDL_ARRAYORDER_RGBA) || \
(SDL_PIXELORDER(format) == SDL_ARRAYORDER_ABGR) || \
(SDL_PIXELORDER(format) == SDL_ARRAYORDER_BGRA))))

/* The flag is set to 1 because 0x1? is not in the printable ASCII range */
#define SDL_ISPIXELFORMAT_FOURCC(format) \
((format) && (SDL_PIXELFLAG(format) != 1))
Expand Down
13 changes: 2 additions & 11 deletions src/video/SDL_bmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,17 +664,8 @@ bool SDL_SaveBMP_IO(SDL_Surface *surface, SDL_IOStream *dst, bool closeio)
SDL_BITSPERPIXEL(surface->format));
goto done;
}
} else if ((SDL_BITSPERPIXEL(surface->format) == 24) && !save32bit &&
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
(surface->fmt->Rmask == 0x00FF0000) &&
(surface->fmt->Gmask == 0x0000FF00) &&
(surface->fmt->Bmask == 0x000000FF)
#else
(surface->fmt->Rmask == 0x000000FF) &&
(surface->fmt->Gmask == 0x0000FF00) &&
(surface->fmt->Bmask == 0x00FF0000)
#endif
) {
} else if ((surface->format == SDL_PIXELFORMAT_BGR24 && !save32bit) ||
(surface->format == SDL_PIXELFORMAT_BGRA32 && save32bit)) {
intermediate_surface = surface;
} else {
SDL_PixelFormat pixel_format;
Expand Down
1 change: 1 addition & 0 deletions src/video/SDL_surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -2597,6 +2597,7 @@ bool SDL_ClearSurface(SDL_Surface *surface, float r, float g, float b, float a)
goto done;
}
SDL_SetSurfaceColorspace(tmp, surface->colorspace);
SDL_SetSurfaceBlendMode(tmp, SDL_BLENDMODE_NONE);

float *pixels = (float *)tmp->pixels;
pixels[0] = r;
Expand Down
Loading

0 comments on commit 8f62d92

Please sign in to comment.