SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_image.h
1#ifndef SDL3PP_IMAGE_H_
2#define SDL3PP_IMAGE_H_
3
4#include "SDL3pp_error.h"
5#include "SDL3pp_render.h"
6#include "SDL3pp_surface.h"
7#include "SDL3pp_version.h"
8#include "SDL3pp_video.h"
9
10#if !defined(SDL3PP_DISABLE_IMAGE) && !defined(SDL3PP_ENABLE_IMAGE) && \
11 __has_include(<SDL3_image/SDL_image.h>)
12#define SDL3PP_ENABLE_IMAGE
13#endif
14
15#if defined(SDL3PP_ENABLE_IMAGE) || defined(SDL3PP_DOC)
16
17#include <SDL3_image/SDL_image.h>
18
19namespace SDL {
20
31// Forward decl
32struct AnimationRef;
33
34// Forward decl
35struct Animation;
36
46
56
57#ifdef SDL3PP_DOC
58
64#define SDL_IMAGE_MAJOR_VERSION
65
66#define SDL_IMAGE_MINOR_VERSION
67
68#define SDL_IMAGE_MICRO_VERSION
69
71
75#define SDL_IMAGE_VERSION \
76 SDL_VERSIONNUM( \
77 SDL_IMAGE_MAJOR_VERSION, SDL_IMAGE_MINOR_VERSION, SDL_IMAGE_MICRO_VERSION)
78
82#define SDL_IMAGE_VERSION_ATLEAST(X, Y, Z) \
83 ((SDL_IMAGE_MAJOR_VERSION >= X) && \
84 (SDL_IMAGE_MAJOR_VERSION > X || SDL_IMAGE_MINOR_VERSION >= Y) && \
85 (SDL_IMAGE_MAJOR_VERSION > X || SDL_IMAGE_MINOR_VERSION > Y || \
86 SDL_IMAGE_MICRO_VERSION >= Z))
87
88#endif // SDL3PP_DOC
89
97inline int IMG_Version() { return ::IMG_Version(); }
98
149{
150 return Surface{IMG_LoadTyped_IO(src.get(), false, type)};
151}
152
187inline Surface LoadSurface(StringParam file) { return Surface{IMG_Load(file)}; }
188
231{
232 return Surface{IMG_Load_IO(src.get(), false)};
233}
234
258{
259 return Texture{IMG_LoadTexture(renderer.get(), file)};
260}
261
295{
296 return Texture{IMG_LoadTexture_IO(renderer.get(), src.get(), false)};
297}
298
341 IOStreamRef src,
342 StringParam type)
343{
344 return Texture{
345 IMG_LoadTextureTyped_IO(renderer.get(), src.get(), false, type)};
346}
347
390inline bool isAVIF(IOStreamRef src) { return IMG_isAVIF(src.get()); }
391
433inline bool isICO(IOStreamRef src) { return IMG_isICO(src.get()); }
434
476inline bool isCUR(IOStreamRef src) { return IMG_isCUR(src.get()); }
477
519inline bool isBMP(IOStreamRef src) { return IMG_isBMP(src.get()); }
520
562inline bool isGIF(IOStreamRef src) { return IMG_isGIF(src.get()); }
563
605inline bool isJPG(IOStreamRef src) { return IMG_isJPG(src.get()); }
606
648inline bool isJXL(IOStreamRef src) { return IMG_isJXL(src.get()); }
649
691inline bool isLBM(IOStreamRef src) { return IMG_isLBM(src.get()); }
692
734inline bool isPCX(IOStreamRef src) { return IMG_isPCX(src.get()); }
735
777inline bool isPNG(IOStreamRef src) { return IMG_isPNG(src.get()); }
778
820inline bool isPNM(IOStreamRef src) { return IMG_isPNM(src.get()); }
821
863inline bool isSVG(IOStreamRef src) { return IMG_isSVG(src.get()); }
864
906inline bool isQOI(IOStreamRef src) { return IMG_isQOI(src.get()); }
907
949inline bool isTIF(IOStreamRef src) { return IMG_isTIF(src.get()); }
950
992inline bool isXCF(IOStreamRef src) { return IMG_isXCF(src.get()); }
993
1035inline bool isXPM(IOStreamRef src) { return IMG_isXPM(src.get()); }
1036
1078inline bool isXV(IOStreamRef src) { return IMG_isXV(src.get()); }
1079
1121inline bool isWEBP(IOStreamRef src) { return IMG_isWEBP(src.get()); }
1122
1156{
1157 return Surface{IMG_LoadAVIF_IO(src.get())};
1158}
1159
1193{
1194 return Surface{IMG_LoadICO_IO(src.get())};
1195}
1196
1230{
1231 return Surface{IMG_LoadCUR_IO(src.get())};
1232}
1233
1267{
1268 return Surface{IMG_LoadBMP_IO(src.get())};
1269}
1270
1304{
1305 return Surface{IMG_LoadGIF_IO(src.get())};
1306}
1307
1341{
1342 return Surface{IMG_LoadJPG_IO(src.get())};
1343}
1344
1378{
1379 return Surface{IMG_LoadJXL_IO(src.get())};
1380}
1381
1415{
1416 return Surface{IMG_LoadLBM_IO(src.get())};
1417}
1418
1452{
1453 return Surface{IMG_LoadPCX_IO(src.get())};
1454}
1455
1489{
1490 return Surface{IMG_LoadPNG_IO(src.get())};
1491}
1492
1526{
1527 return Surface{IMG_LoadPNM_IO(src.get())};
1528}
1529
1563{
1564 return Surface{IMG_LoadSVG_IO(src.get())};
1565}
1566
1600{
1601 return Surface{IMG_LoadQOI_IO(src.get())};
1602}
1603
1637{
1638 return Surface{IMG_LoadTGA_IO(src.get())};
1639}
1640
1674{
1675 return Surface{IMG_LoadTIF_IO(src.get())};
1676}
1677
1711{
1712 return Surface{IMG_LoadXCF_IO(src.get())};
1713}
1714
1748{
1749 return Surface{IMG_LoadXPM_IO(src.get())};
1750}
1751
1785{
1786 return Surface{IMG_LoadXV_IO(src.get())};
1787}
1788
1822{
1823 return Surface{IMG_LoadWEBP_IO(src.get())};
1824}
1825
1845inline Surface LoadSizedSVG(IOStreamRef src, int width, int height)
1846{
1847 return Surface{IMG_LoadSizedSVG_IO(src.get(), width, height)};
1848}
1849
1864inline Surface ReadXPMFromArray(char** xpm)
1865{
1866 return Surface{IMG_ReadXPMFromArray(xpm)};
1867}
1868
1886{
1887 return Surface{IMG_ReadXPMFromArrayToRGB888(xpm)};
1888}
1889
1903inline void SaveAVIF(SurfaceRef surface, StringParam file, int quality)
1904{
1905 CheckError(IMG_SaveAVIF(surface.get(), file, quality));
1906}
1907
1921inline void SaveAVIF(SurfaceRef surface, IOStreamRef dst, int quality)
1922{
1923 CheckError(IMG_SaveAVIF_IO(surface.get(), dst.get(), false, quality));
1924}
1925
1939inline void SavePNG(SurfaceRef surface, StringParam file)
1940{
1941 CheckError(IMG_SavePNG(surface.get(), file));
1942}
1943
1955inline void SavePNG(SurfaceRef surface, IOStreamRef dst)
1956{
1957 CheckError(IMG_SavePNG_IO(surface.get(), dst.get(), false));
1958}
1959
1973inline void SaveJPG(SurfaceRef surface, StringParam file, int quality)
1974{
1975 CheckError(IMG_SaveJPG(surface.get(), file, quality));
1976}
1977
1991inline void SaveJPG(SurfaceRef surface, IOStreamRef dst, int quality)
1992{
1993 CheckError(IMG_SaveJPG_IO(surface.get(), dst.get(), false, quality));
1994}
1995
2005struct AnimationRef : Resource<IMG_Animation*>
2006{
2007 using Resource::Resource;
2008
2012 int GetWidth() const { return get()->w; }
2013
2017 int GetHeight() const { return get()->h; }
2018
2022 Point GetSize() const { return Point(GetWidth(), GetHeight()); }
2023
2027 int GetCount() const { return get()->count; }
2028
2034 SurfaceRef GetFrame(int index) const { return get()->frames[index]; }
2035
2041 int GetDelay(int index) const { return get()->delays[index]; }
2042
2055 static void reset(IMG_Animation* resource) { IMG_FreeAnimation(resource); }
2056};
2057
2065struct Animation : ResourceUnique<AnimationRef>
2066{
2068
2081 {
2082 return Animation(IMG_LoadAnimation(file));
2083 }
2084
2097 {
2098 return Animation(IMG_LoadAnimation_IO(src, false));
2099 }
2100
2120 {
2121 return Animation(IMG_LoadAnimationTyped_IO(src, false, type));
2122 }
2123
2133 void Free() { reset(); }
2138
2139};
2140
2141
2143{
2144 return AnimationShared(std::move(*this));
2145}
2146
2156struct AnimationUnsafe : ResourceUnsafe<AnimationRef>
2157{
2159
2163 constexpr explicit AnimationUnsafe(Animation&& other)
2164 : AnimationUnsafe(other.release())
2165 {
2166 }
2167};
2168
2187{
2188 return Animation{IMG_LoadGIFAnimation_IO(src)};
2189}
2190
2209{
2210 return Animation{IMG_LoadWEBPAnimation_IO(src)};
2211}
2212
2213#pragma region impl
2214
2216
2218{
2219 return Surface(IMG_Load(file));
2220}
2221
2223{
2224 return Surface(IMG_Load_IO(src.get(), false));
2225}
2226
2228{
2229 return Texture(IMG_LoadTexture(renderer.get(), file));
2230}
2231
2233{
2234 return Texture(IMG_LoadTexture_IO(renderer.get(), src.get(), false));
2235}
2236
2237#pragma endregion impl
2238
2239} // namespace SDL
2240
2241#else // defined(SDL3PP_ENABLE_IMAGE) || defined(SDL3PP_DOC)
2242
2243namespace SDL {
2244
2245inline Surface Surface::Load(StringParam file)
2246{
2247 return Surface(SDL_LoadBMP(file));
2248}
2249
2250inline Surface Surface::Load(IOStreamRef src)
2251{
2252 return Surface(SDL_LoadBMP_IO(src.get(), false));
2253}
2254
2255inline Texture Texture::Load(RendererRef renderer, StringParam file)
2256{
2257 return Texture(LoadTextureBMP(renderer, std::move(file)));
2258}
2259
2260inline Texture Texture::Load(RendererRef renderer, IOStreamRef src)
2261{
2262 return Texture(LoadTextureBMP(renderer, src));
2263}
2264
2265}
2266
2267#endif // defined(SDL3PP_ENABLE_IMAGE) || defined(SDL3PP_DOC)
2268
2269#endif /* SDL3PP_IMAGE_H_ */
RESOURCE release()
Returns reference and reset this.
Definition SDL3pp_resource.h:178
Implement shared ownership for a resource.
Definition SDL3pp_resource.h:283
Implement unique ownership for a resource.
Definition SDL3pp_resource.h:226
constexpr ResourceUnique(std::nullptr_t=nullptr)
Default constructor.
Definition SDL3pp_resource.h:231
void reset()
Resets the value, destroying the resource if not nullptr.
Definition SDL3pp_resource.h:265
A dumb pointer to resource.
Definition SDL3pp_resource.h:197
constexpr ResourceUnsafe()=default
Default constructor.
Implement weak ownership for a resource.
Definition SDL3pp_resource.h:328
A SDL managed resource.
Definition SDL3pp_resource.h:29
constexpr Resource(T resource={})
Constructs from the underlying resource.
Definition SDL3pp_resource.h:37
constexpr T get() const
Return contained resource;.
Definition SDL3pp_resource.h:76
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
Texture LoadTextureBMP(RendererRef &renderer, IOStreamRef &src)
Load a BMP texture from a seekable SDL data stream.
Definition SDL3pp_render.h:3630
Surface ReadXPMFromArrayToRGB888(char **xpm)
Load an XPM image from a memory array.
Definition SDL3pp_image.h:1885
Surface LoadTGA(IOStreamRef src)
Load a TGA image directly.
Definition SDL3pp_image.h:1636
Surface LoadPNG(IOStreamRef src)
Load a PNG image directly.
Definition SDL3pp_image.h:1488
bool isPNG(IOStreamRef src)
Detect PNG image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:777
Surface ReadXPMFromArray(char **xpm)
Load an XPM image from a memory array.
Definition SDL3pp_image.h:1864
Surface LoadXPM(IOStreamRef src)
Load a XPM image directly.
Definition SDL3pp_image.h:1747
bool isTIF(IOStreamRef src)
Detect TIFF image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:949
int IMG_Version()
This function gets the version of the dynamically linked SDL_image library.
Definition SDL3pp_image.h:97
bool isWEBP(IOStreamRef src)
Detect WEBP image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:1121
Surface LoadQOI(IOStreamRef src)
Load a QOI image directly.
Definition SDL3pp_image.h:1599
bool isXPM(IOStreamRef src)
Detect XPM image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:1035
bool isPCX(IOStreamRef src)
Detect PCX image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:734
Surface LoadGIF(IOStreamRef src)
Load a GIF image directly.
Definition SDL3pp_image.h:1303
AnimationShared share()
Move this animation into a AnimationShared.
Definition SDL3pp_image.h:2142
bool isJXL(IOStreamRef src)
Detect JXL image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:648
bool isLBM(IOStreamRef src)
Detect LBM image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:691
bool isICO(IOStreamRef src)
Detect ICO image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:433
bool isPNM(IOStreamRef src)
Detect PNM image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:820
void SaveAVIF(SurfaceRef surface, StringParam file, int quality)
Save an Surface into a AVIF image file.
Definition SDL3pp_image.h:1903
Surface LoadSizedSVG(IOStreamRef src, int width, int height)
Load an SVG image, scaled to a specific size.
Definition SDL3pp_image.h:1845
void SaveJPG(SurfaceRef surface, StringParam file, int quality)
Save an Surface into a JPEG image file.
Definition SDL3pp_image.h:1973
bool isJPG(IOStreamRef src)
Detect JPG image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:605
Surface LoadPCX(IOStreamRef src)
Load a PCX image directly.
Definition SDL3pp_image.h:1451
bool isXCF(IOStreamRef src)
Detect XCF image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:992
Surface LoadPNM(IOStreamRef src)
Load a PNM image directly.
Definition SDL3pp_image.h:1525
Surface LoadSVG(IOStreamRef src)
Load a SVG image directly.
Definition SDL3pp_image.h:1562
Surface LoadTIF(IOStreamRef src)
Load a TIFF image directly.
Definition SDL3pp_image.h:1673
Animation LoadGIFAnimation(IOStreamRef src)
Load a GIF animation directly.
Definition SDL3pp_image.h:2186
Surface LoadCUR(IOStreamRef src)
Load a CUR image directly.
Definition SDL3pp_image.h:1229
Surface LoadJPG(IOStreamRef src)
Load a JPG image directly.
Definition SDL3pp_image.h:1340
bool isSVG(IOStreamRef src)
Detect SVG image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:863
ResourceShared< Animation > AnimationShared
Handle to a shared animation.
Definition SDL3pp_image.h:45
Surface LoadXCF(IOStreamRef src)
Load a XCF image directly.
Definition SDL3pp_image.h:1710
void SavePNG(SurfaceRef surface, StringParam file)
Save an Surface into a PNG image file.
Definition SDL3pp_image.h:1939
Surface LoadSurface(IOStreamRef src, StringParam type)
Load an image from an SDL data source into a software surface.
Definition SDL3pp_image.h:148
bool isBMP(IOStreamRef src)
Detect BMP image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:519
Surface LoadWEBP(IOStreamRef src)
Load a WEBP image directly.
Definition SDL3pp_image.h:1821
Surface LoadAVIF(IOStreamRef src)
Load a AVIF image directly.
Definition SDL3pp_image.h:1155
Surface LoadICO(IOStreamRef src)
Load a ICO image directly.
Definition SDL3pp_image.h:1192
bool isXV(IOStreamRef src)
Detect XV image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:1078
Texture LoadTexture(RendererRef renderer, StringParam file)
Load an image from a filesystem path into a GPU texture.
Definition SDL3pp_image.h:257
bool isQOI(IOStreamRef src)
Detect QOI image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:906
Surface LoadJXL(IOStreamRef src)
Load a JXL image directly.
Definition SDL3pp_image.h:1377
Surface LoadLBM(IOStreamRef src)
Load a LBM image directly.
Definition SDL3pp_image.h:1414
bool isAVIF(IOStreamRef src)
Detect AVIF image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:390
Surface LoadXV(IOStreamRef src)
Load a XV image directly.
Definition SDL3pp_image.h:1784
bool isGIF(IOStreamRef src)
Detect GIF image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:562
bool isCUR(IOStreamRef src)
Detect CUR image data on a readable/seekable IOStream.
Definition SDL3pp_image.h:476
Surface LoadBMP(IOStreamRef src)
Load a BMP image directly.
Definition SDL3pp_image.h:1266
Animation LoadWEBPAnimation(IOStreamRef src)
Load a WEBP animation directly.
Definition SDL3pp_image.h:2208
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Animated image support.
Definition SDL3pp_image.h:2006
int GetDelay(int index) const
Return the frame delay under given index.
Definition SDL3pp_image.h:2041
int GetCount() const
Return the number of frames.
Definition SDL3pp_image.h:2027
SurfaceRef GetFrame(int index) const
Return the frame image under given index.
Definition SDL3pp_image.h:2034
int GetWidth() const
Get the width in pixels.
Definition SDL3pp_image.h:2012
static void reset(IMG_Animation *resource)
Dispose of an AnimationRef and free its resources.
Definition SDL3pp_image.h:2055
Point GetSize() const
Get the size in pixels.
Definition SDL3pp_image.h:2022
int GetHeight() const
Get the height in pixels.
Definition SDL3pp_image.h:2017
Unsafe Handle to animation.
Definition SDL3pp_image.h:2157
constexpr AnimationUnsafe(Animation &&other)
Constructs AnimationUnsafe from Animation.
Definition SDL3pp_image.h:2163
Handle to an owned animation.
Definition SDL3pp_image.h:2066
static Animation Load(StringParam file)
Load an animation from a file.
Definition SDL3pp_image.h:2080
void Free()
Dispose of an AnimationRef and free its resources.
Definition SDL3pp_image.h:2133
static Animation Load(IOStreamRef src)
Load an animation from an IOStreamRef.
Definition SDL3pp_image.h:2096
static Animation LoadTyped(IOStreamRef src, StringParam type)
Load an animation from an SDL datasource.
Definition SDL3pp_image.h:2119
The read/write operation structure.
Definition SDL3pp_iostream.h:123
The structure that defines a point (using integers).
Definition SDL3pp_rect.h:46
A structure representing rendering state.
Definition SDL3pp_render.h:182
A collection of pixels used in software blitting.
Definition SDL3pp_surface.h:153
Handle to an owned surface.
Definition SDL3pp_surface.h:1824
static Surface Load(StringParam file)
Load an image from a filesystem path into a software surface.
Definition SDL3pp_image.h:2217
Handle to an owned texture.
Definition SDL3pp_render.h:2685
static Texture Load(RendererRef renderer, StringParam file)
Load an image from a filesystem path into a software surface.
Definition SDL3pp_image.h:2227