Made SDL_ReadSurfacePixel a public function

Fixes https://github.com/libsdl-org/SDL/issues/8320
This commit is contained in:
Sam Lantinga
2024-01-18 04:36:37 -08:00
parent f7ba340999
commit f8dfee01bb
9 changed files with 93 additions and 118 deletions

View File

@@ -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: *;
};

View File

@@ -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

View File

@@ -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)

View File

@@ -28,19 +28,11 @@
*/
#include <SDL3/SDL_test.h>
#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++;

View File

@@ -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.
*/

View File

@@ -1,80 +0,0 @@
/*
Copyright 1997-2024 Sam Lantinga <slouken@libsdl.org>
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;
}