diff --git a/include/SDL3/SDL_surface.h b/include/SDL3/SDL_surface.h index 7ccbda915557c8..6ea4da1e8d0219 100644 --- a/include/SDL3/SDL_surface.h +++ b/include/SDL3/SDL_surface.h @@ -932,6 +932,25 @@ extern DECLSPEC int SDLCALL SDL_BlitSurfaceUncheckedScaled(SDL_Surface *src, const SDL_Rect *dstrect, SDL_ScaleMode scaleMode); +/** + * A very slow function to read an RGBA pixel from a surface. + * + * This function takes care of the details of extracting color information from a pixel in an RGBA surface, however this is very slow and can be done much more efficiently if you know the format of the surface in advance. + * + * \param surface the surface to read from + * \param x the x coordinate of the pixel to read + * \param y the y coordinate of the pixel to read + * \param r a pointer filled in with the red channel, or NULL to ignore this channel + * \param g a pointer filled in with the green channel, or NULL to ignore this channel + * \param b a pointer filled in with the blue channel, or NULL to ignore this channel + * \param a a pointer filled in with the alpha channel, or NULL to ignore this channel + * \returns 0 on success or a negative error code on failure; call + * SDL_GetError() for more information. + * + * \since This function is available since SDL 3.0.0. + */ +extern DECLSPEC int SDLCALL SDL_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a); + /** * Set the YUV conversion mode * diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym index 23f3cbbdeea958..7a6c205d0d3a97 100644 --- a/src/dynapi/SDL_dynapi.sym +++ b/src/dynapi/SDL_dynapi.sym @@ -963,6 +963,7 @@ SDL3_0.0.0 { SDL_GetHapticFromInstanceID; SDL_GetHapticInstanceID; SDL_GetHapticName; + SDL_ReadSurfacePixel; # extra symbols go here (don't modify this line) local: *; }; diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index 83da3e39e8734b..837e46f219398f 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -988,3 +988,4 @@ #define SDL_GetHapticFromInstanceID SDL_GetHapticFromInstanceID_REAL #define SDL_GetHapticInstanceID SDL_GetHapticInstanceID_REAL #define SDL_GetHapticName SDL_GetHapticName_REAL +#define SDL_ReadSurfacePixel SDL_ReadSurfacePixel_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 8314eb918f921a..3b313b9a780c39 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -1013,3 +1013,4 @@ SDL_DYNAPI_PROC(const char*,SDL_GetHapticInstanceName,(SDL_HapticID a),(a),retur SDL_DYNAPI_PROC(SDL_Haptic*,SDL_GetHapticFromInstanceID,(SDL_HapticID a),(a),return) SDL_DYNAPI_PROC(SDL_HapticID,SDL_GetHapticInstanceID,(SDL_Haptic *a),(a),return) SDL_DYNAPI_PROC(const char*,SDL_GetHapticName,(SDL_Haptic *a),(a),return) +SDL_DYNAPI_PROC(int,SDL_ReadSurfacePixel,(SDL_Surface *a, int b, int c, Uint8 *d, Uint8 *e, Uint8 *f, Uint8 *g),(a,b,c,d,e,f,g),return) diff --git a/src/test/SDL_test_compare.c b/src/test/SDL_test_compare.c index d7adb22daf6ba4..db49719a23f06e 100644 --- a/src/test/SDL_test_compare.c +++ b/src/test/SDL_test_compare.c @@ -28,19 +28,11 @@ */ #include -#include "../video/SDL_surface_pixel_impl.h" - #define FILENAME_SIZE 128 /* Counter for _CompareSurface calls; used for filename creation when comparisons fail */ static int _CompareSurfaceCount = 0; -int -SDLTest_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a) -{ - return SDL_ReadSurfacePixel_impl(surface, x, y, r, g, b, a); -} - static void LogErrorFormat(const char *name, const SDL_PixelFormat *format) { @@ -96,14 +88,14 @@ int SDLTest_CompareSurfaces(SDL_Surface *surface, SDL_Surface *referenceSurface, for (i = 0; i < surface->w; i++) { int temp; - temp = SDLTest_ReadSurfacePixel(surface, i, j, &R, &G, &B, &A); + temp = SDL_ReadSurfacePixel(surface, i, j, &R, &G, &B, &A); if (temp != 0) { SDLTest_LogError("Failed to retrieve pixel (%d,%d): %s", i, j, SDL_GetError()); ret++; continue; } - temp = SDLTest_ReadSurfacePixel(referenceSurface, i, j, &Rd, &Gd, &Bd, &Ad); + temp = SDL_ReadSurfacePixel(referenceSurface, i, j, &Rd, &Gd, &Bd, &Ad); if (temp != 0) { SDLTest_LogError("Failed to retrieve reference pixel (%d,%d): %s", i, j, SDL_GetError()); ret++; diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c index c051d83687b71c..3ca8519c837a5f 100644 --- a/src/video/SDL_surface.c +++ b/src/video/SDL_surface.c @@ -24,7 +24,6 @@ #include "SDL_video_c.h" #include "SDL_blit.h" #include "SDL_RLEaccel_c.h" -#include "SDL_surface_pixel_impl.h" #include "SDL_pixels_c.h" #include "SDL_yuv_c.h" #include "../render/SDL_sysrender.h" @@ -36,11 +35,6 @@ SDL_COMPILE_TIME_ASSERT(surface_size_assumptions, SDL_COMPILE_TIME_ASSERT(can_indicate_overflow, SDL_SIZE_MAX > SDL_MAX_SINT32); -int SDL_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a) -{ - return SDL_ReadSurfacePixel_impl(surface, x, y, r, g, b, a); -} - /* Public routines */ /* @@ -1560,6 +1554,68 @@ int SDL_PremultiplyAlpha(int width, int height, return 0; } +/* This function Copyright 2023 Collabora Ltd., contributed to SDL under the ZLib license */ +int SDL_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a) +{ + Uint32 pixel = 0; + size_t bytes_per_pixel; + Uint8 unused; + Uint8 *p; + + if (!surface || !surface->format || !surface->pixels) { + return SDL_InvalidParamError("surface"); + } + + if (x < 0 || x >= surface->w) { + return SDL_InvalidParamError("x"); + } + + if (y < 0 || y >= surface->h) { + return SDL_InvalidParamError("y"); + } + + if (!r) { + r = &unused; + } + + if (!g) { + g = &unused; + } + + if (!b) { + b = &unused; + } + + if (!a) { + a = &unused; + } + + bytes_per_pixel = surface->format->BytesPerPixel; + + if (bytes_per_pixel > sizeof(pixel)) { + return SDL_InvalidParamError("surface->format->BytesPerPixel"); + } + + if (SDL_MUSTLOCK(surface)) { + SDL_LockSurface(surface); + } + + p = (Uint8 *)surface->pixels + y * surface->pitch + x * bytes_per_pixel; + /* Fill the appropriate number of least-significant bytes of pixel, + * leaving the most-significant bytes set to zero */ +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + SDL_memcpy(((Uint8 *) &pixel) + (sizeof(pixel) - bytes_per_pixel), p, bytes_per_pixel); +#else + SDL_memcpy(&pixel, p, bytes_per_pixel); +#endif + SDL_GetRGBA(pixel, surface->format, r, g, b, a); + + if (SDL_MUSTLOCK(surface)) { + SDL_UnlockSurface(surface); + } + return 0; +} + /* * Free a surface created by the above function. */ diff --git a/src/video/SDL_surface_pixel_impl.h b/src/video/SDL_surface_pixel_impl.h deleted file mode 100644 index 67f772fe1533a8..00000000000000 --- a/src/video/SDL_surface_pixel_impl.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - Copyright 1997-2024 Sam Lantinga - Copyright 2023 Collabora Ltd. - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. -*/ - -#include "SDL3/SDL.h" - -/* Internal implementation of SDL_ReadSurfacePixel, shared between SDL_shape - * and SDLTest */ -static int SDL_ReadSurfacePixel_impl(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a) -{ - Uint32 pixel = 0; - size_t bytes_per_pixel; - void *p; - - if (!surface || !surface->format || !surface->pixels) { - return SDL_InvalidParamError("surface"); - } - - if (x < 0 || x >= surface->w) { - return SDL_InvalidParamError("x"); - } - - if (y < 0 || y >= surface->h) { - return SDL_InvalidParamError("y"); - } - - if (!r) { - return SDL_InvalidParamError("r"); - } - - if (!g) { - return SDL_InvalidParamError("g"); - } - - if (!b) { - return SDL_InvalidParamError("b"); - } - - if (!a) { - return SDL_InvalidParamError("a"); - } - - bytes_per_pixel = surface->format->BytesPerPixel; - - if (bytes_per_pixel > sizeof(pixel)) { - return SDL_InvalidParamError("surface->format->BytesPerPixel"); - } - - SDL_LockSurface(surface); - - p = (Uint8 *)surface->pixels + y * surface->pitch + x * bytes_per_pixel; - /* Fill the appropriate number of least-significant bytes of pixel, - * leaving the most-significant bytes set to zero */ -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - SDL_memcpy(((Uint8 *) &pixel) + (sizeof(pixel) - bytes_per_pixel), p, bytes_per_pixel); -#else - SDL_memcpy(&pixel, p, bytes_per_pixel); -#endif - SDL_GetRGBA(pixel, surface->format, r, g, b, a); - - SDL_UnlockSurface(surface); - return 0; -}