Skip to content

Commit f8dfee0

Browse files
committed
Made SDL_ReadSurfacePixel a public function
Fixes #8320
1 parent f7ba340 commit f8dfee0

9 files changed

+93
-118
lines changed

include/SDL3/SDL_surface.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,31 @@ extern DECLSPEC int SDLCALL SDL_BlitSurfaceUncheckedScaled(SDL_Surface *src,
932932
const SDL_Rect *dstrect,
933933
SDL_ScaleMode scaleMode);
934934

935+
/**
936+
* Retrieves a single pixel from a surface.
937+
*
938+
* This function prioritizes correctness over speed: it is suitable for
939+
* unit tests, but is not intended for use in a game engine.
940+
*
941+
* Like SDL_GetRGBA, this uses the entire 0..255 range when converting
942+
* color components from pixel formats with less than 8 bits per RGB
943+
* component.
944+
*
945+
* \param surface the surface to read
946+
* \param x the horizontal coordinate, 0 <= x < width
947+
* \param y the vertical coordinate, 0 <= y < height
948+
* \param r a pointer filled in with the red channel, 0-255, or NULL to ignore this channel
949+
* \param g a pointer filled in with the green channel, 0-255, or NULL to ignore this channel
950+
* \param b a pointer filled in with the blue channel, 0-255, or NULL to ignore this channel
951+
* \param a a pointer filled in with the alpha channel, 0-255, or NULL to ignore this channel
952+
* \returns 0 on success or a negative error code on failure; call
953+
* SDL_GetError() for more information.
954+
*
955+
* \since This function is available since SDL 3.0.0.
956+
*/
957+
extern DECLSPEC int SDLCALL SDL_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a);
958+
959+
935960
/**
936961
* Set the YUV conversion mode
937962
*

include/SDL3/SDL_test_compare.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,6 @@
4444
extern "C" {
4545
#endif
4646

47-
/**
48-
* Retrieves a single pixel from a surface.
49-
*
50-
* This function prioritizes correctness over speed: it is suitable for
51-
* unit tests, but is not intended for use in a game engine.
52-
*
53-
* Like SDL_GetRGBA, this uses the entire 0..255 range when converting
54-
* color components from pixel formats with less than 8 bits per RGB
55-
* component.
56-
*
57-
* \param surface The surface
58-
* \param x Horizontal coordinate, 0 <= x < width
59-
* \param y Vertical coordinate, 0 <= y < height
60-
* \param r Pointer to location to store red channel, 0-255
61-
* \param g Pointer to location to store green channel, 0-255
62-
* \param b Pointer to location to store blue channel, 0-255
63-
* \param a Pointer to location to store alpha channel, 0-255
64-
* \returns 0 if the surface is valid and the coordinates are in-bounds
65-
*/
66-
int SDLTest_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a);
67-
6847
/**
6948
* Compares a surface and with reference image data for equality
7049
*

src/dynapi/SDL_dynapi.sym

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,7 @@ SDL3_0.0.0 {
963963
SDL_GetHapticFromInstanceID;
964964
SDL_GetHapticInstanceID;
965965
SDL_GetHapticName;
966+
SDL_ReadSurfacePixel;
966967
# extra symbols go here (don't modify this line)
967968
local: *;
968969
};

src/dynapi/SDL_dynapi_overrides.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,3 +988,4 @@
988988
#define SDL_GetHapticFromInstanceID SDL_GetHapticFromInstanceID_REAL
989989
#define SDL_GetHapticInstanceID SDL_GetHapticInstanceID_REAL
990990
#define SDL_GetHapticName SDL_GetHapticName_REAL
991+
#define SDL_ReadSurfacePixel SDL_ReadSurfacePixel_REAL

src/dynapi/SDL_dynapi_procs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,3 +1013,4 @@ SDL_DYNAPI_PROC(const char*,SDL_GetHapticInstanceName,(SDL_HapticID a),(a),retur
10131013
SDL_DYNAPI_PROC(SDL_Haptic*,SDL_GetHapticFromInstanceID,(SDL_HapticID a),(a),return)
10141014
SDL_DYNAPI_PROC(SDL_HapticID,SDL_GetHapticInstanceID,(SDL_Haptic *a),(a),return)
10151015
SDL_DYNAPI_PROC(const char*,SDL_GetHapticName,(SDL_Haptic *a),(a),return)
1016+
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)

src/test/SDL_test_compare.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,11 @@
2828
*/
2929
#include <SDL3/SDL_test.h>
3030

31-
#include "../video/SDL_surface_pixel_impl.h"
32-
3331
#define FILENAME_SIZE 128
3432

3533
/* Counter for _CompareSurface calls; used for filename creation when comparisons fail */
3634
static int _CompareSurfaceCount = 0;
3735

38-
int
39-
SDLTest_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
40-
{
41-
return SDL_ReadSurfacePixel_impl(surface, x, y, r, g, b, a);
42-
}
43-
4436
static void
4537
LogErrorFormat(const char *name, const SDL_PixelFormat *format)
4638
{
@@ -96,14 +88,14 @@ int SDLTest_CompareSurfaces(SDL_Surface *surface, SDL_Surface *referenceSurface,
9688
for (i = 0; i < surface->w; i++) {
9789
int temp;
9890

99-
temp = SDLTest_ReadSurfacePixel(surface, i, j, &R, &G, &B, &A);
91+
temp = SDL_ReadSurfacePixel(surface, i, j, &R, &G, &B, &A);
10092
if (temp != 0) {
10193
SDLTest_LogError("Failed to retrieve pixel (%d,%d): %s", i, j, SDL_GetError());
10294
ret++;
10395
continue;
10496
}
10597

106-
temp = SDLTest_ReadSurfacePixel(referenceSurface, i, j, &Rd, &Gd, &Bd, &Ad);
98+
temp = SDL_ReadSurfacePixel(referenceSurface, i, j, &Rd, &Gd, &Bd, &Ad);
10799
if (temp != 0) {
108100
SDLTest_LogError("Failed to retrieve reference pixel (%d,%d): %s", i, j, SDL_GetError());
109101
ret++;

src/video/SDL_surface.c

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "SDL_video_c.h"
2525
#include "SDL_blit.h"
2626
#include "SDL_RLEaccel_c.h"
27-
#include "SDL_surface_pixel_impl.h"
2827
#include "SDL_pixels_c.h"
2928
#include "SDL_yuv_c.h"
3029
#include "../render/SDL_sysrender.h"
@@ -36,11 +35,6 @@ SDL_COMPILE_TIME_ASSERT(surface_size_assumptions,
3635

3736
SDL_COMPILE_TIME_ASSERT(can_indicate_overflow, SDL_SIZE_MAX > SDL_MAX_SINT32);
3837

39-
int SDL_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
40-
{
41-
return SDL_ReadSurfacePixel_impl(surface, x, y, r, g, b, a);
42-
}
43-
4438
/* Public routines */
4539

4640
/*
@@ -1560,6 +1554,68 @@ int SDL_PremultiplyAlpha(int width, int height,
15601554
return 0;
15611555
}
15621556

1557+
/* This function Copyright 2023 Collabora Ltd., contributed to SDL under the ZLib license */
1558+
int SDL_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
1559+
{
1560+
Uint32 pixel = 0;
1561+
size_t bytes_per_pixel;
1562+
Uint8 unused;
1563+
Uint8 *p;
1564+
1565+
if (!surface || !surface->format || !surface->pixels) {
1566+
return SDL_InvalidParamError("surface");
1567+
}
1568+
1569+
if (x < 0 || x >= surface->w) {
1570+
return SDL_InvalidParamError("x");
1571+
}
1572+
1573+
if (y < 0 || y >= surface->h) {
1574+
return SDL_InvalidParamError("y");
1575+
}
1576+
1577+
if (!r) {
1578+
r = &unused;
1579+
}
1580+
1581+
if (!g) {
1582+
g = &unused;
1583+
}
1584+
1585+
if (!b) {
1586+
b = &unused;
1587+
}
1588+
1589+
if (!a) {
1590+
a = &unused;
1591+
}
1592+
1593+
bytes_per_pixel = surface->format->BytesPerPixel;
1594+
1595+
if (bytes_per_pixel > sizeof(pixel)) {
1596+
return SDL_InvalidParamError("surface->format->BytesPerPixel");
1597+
}
1598+
1599+
if (SDL_MUSTLOCK(surface)) {
1600+
SDL_LockSurface(surface);
1601+
}
1602+
1603+
p = (Uint8 *)surface->pixels + y * surface->pitch + x * bytes_per_pixel;
1604+
/* Fill the appropriate number of least-significant bytes of pixel,
1605+
* leaving the most-significant bytes set to zero */
1606+
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
1607+
SDL_memcpy(((Uint8 *) &pixel) + (sizeof(pixel) - bytes_per_pixel), p, bytes_per_pixel);
1608+
#else
1609+
SDL_memcpy(&pixel, p, bytes_per_pixel);
1610+
#endif
1611+
SDL_GetRGBA(pixel, surface->format, r, g, b, a);
1612+
1613+
if (SDL_MUSTLOCK(surface)) {
1614+
SDL_UnlockSurface(surface);
1615+
}
1616+
return 0;
1617+
}
1618+
15631619
/*
15641620
* Free a surface created by the above function.
15651621
*/

src/video/SDL_surface_pixel_impl.h

Lines changed: 0 additions & 80 deletions
This file was deleted.

test/testshape.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static void SDL_CalculateShapeBitmap(SDL_WindowShapeMode mode, SDL_Surface *shap
8888
bitmap_scanline = bitmap + y * bytes_per_scanline;
8989
for (x = 0; x < shape->w; x++) {
9090
alpha = 0;
91-
if (SDLTest_ReadSurfacePixel(shape, x, y, &r, &g, &b, &alpha) != 0) {
91+
if (SDL_ReadSurfacePixel(shape, x, y, &r, &g, &b, &alpha) != 0) {
9292
continue;
9393
}
9494

0 commit comments

Comments
 (0)