forked from libsdl-org/SDL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Extract SDLTest_ReadSurfacePixel
This is essentially the same as was added in d95d2d7, but with clearer error handling. It's implemented in a private header file so that it can be shared with SDL_shape, which also wants this functionality. Resolves: libsdl-org#8319 Signed-off-by: Simon McVittie <[email protected]>
- Loading branch information
Showing
3 changed files
with
122 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Copyright 1997-2023 Sam Lantinga <[email protected]> | ||
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 inline 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 == NULL || surface->format == NULL || surface->pixels == NULL) { | ||
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 == NULL) { | ||
return SDL_InvalidParamError("r"); | ||
} | ||
|
||
if (g == NULL) { | ||
return SDL_InvalidParamError("g"); | ||
} | ||
|
||
if (b == NULL) { | ||
return SDL_InvalidParamError("b"); | ||
} | ||
|
||
if (a == NULL) { | ||
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; | ||
} |