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
30// Forward decl
31struct AnimationBase;
32
33// Forward decl
34struct AnimationRef;
35
36// Forward decl
37struct Animation;
38
39#ifdef SDL3PP_DOC
40
46#define SDL_IMAGE_MAJOR_VERSION
47
48#define SDL_IMAGE_MINOR_VERSION
49
50#define SDL_IMAGE_MICRO_VERSION
51
53
57#define SDL_IMAGE_VERSION \
58 SDL_VERSIONNUM( \
59 SDL_IMAGE_MAJOR_VERSION, SDL_IMAGE_MINOR_VERSION, SDL_IMAGE_MICRO_VERSION)
60
64#define SDL_IMAGE_VERSION_ATLEAST(X, Y, Z) \
65 ((SDL_IMAGE_MAJOR_VERSION >= X) && \
66 (SDL_IMAGE_MAJOR_VERSION > X || SDL_IMAGE_MINOR_VERSION >= Y) && \
67 (SDL_IMAGE_MAJOR_VERSION > X || SDL_IMAGE_MINOR_VERSION > Y || \
68 SDL_IMAGE_MICRO_VERSION >= Z))
69
70#endif // SDL3PP_DOC
71
79inline int IMG_Version() { return ::IMG_Version(); }
80
132{
133 return Surface{IMG_LoadTyped_IO(src.get(), false, type)};
134}
135
170inline Surface LoadSurface(StringParam file) { return Surface{IMG_Load(file)}; }
171
216{
217 return Surface{IMG_Load_IO(src.get(), false)};
218}
219
243{
244 return Texture{IMG_LoadTexture(renderer.get(), file)};
245}
246
280{
281 return Texture{IMG_LoadTexture_IO(renderer.get(), src.get(), false)};
282}
283
325 IOStreamBase& src,
326 StringParam type)
327{
328 return Texture{
329 IMG_LoadTextureTyped_IO(renderer.get(), src.get(), false, type)};
330}
331
374inline bool isAVIF(IOStreamBase& src) { return IMG_isAVIF(src.get()); }
375
417inline bool isICO(IOStreamBase& src) { return IMG_isICO(src.get()); }
418
460inline bool isCUR(IOStreamBase& src) { return IMG_isCUR(src.get()); }
461
503inline bool isBMP(IOStreamBase& src) { return IMG_isBMP(src.get()); }
504
546inline bool isGIF(IOStreamBase& src) { return IMG_isGIF(src.get()); }
547
589inline bool isJPG(IOStreamBase& src) { return IMG_isJPG(src.get()); }
590
632inline bool isJXL(IOStreamBase& src) { return IMG_isJXL(src.get()); }
633
675inline bool isLBM(IOStreamBase& src) { return IMG_isLBM(src.get()); }
676
718inline bool isPCX(IOStreamBase& src) { return IMG_isPCX(src.get()); }
719
761inline bool isPNG(IOStreamBase& src) { return IMG_isPNG(src.get()); }
762
804inline bool isPNM(IOStreamBase& src) { return IMG_isPNM(src.get()); }
805
847inline bool isSVG(IOStreamBase& src) { return IMG_isSVG(src.get()); }
848
890inline bool isQOI(IOStreamBase& src) { return IMG_isQOI(src.get()); }
891
933inline bool isTIF(IOStreamBase& src) { return IMG_isTIF(src.get()); }
934
976inline bool isXCF(IOStreamBase& src) { return IMG_isXCF(src.get()); }
977
1019inline bool isXPM(IOStreamBase& src) { return IMG_isXPM(src.get()); }
1020
1062inline bool isXV(IOStreamBase& src) { return IMG_isXV(src.get()); }
1063
1105inline bool isWEBP(IOStreamBase& src) { return IMG_isWEBP(src.get()); }
1106
1140{
1141 return Surface{IMG_LoadAVIF_IO(src.get())};
1142}
1143
1177{
1178 return Surface{IMG_LoadICO_IO(src.get())};
1179}
1180
1214{
1215 return Surface{IMG_LoadCUR_IO(src.get())};
1216}
1217
1251{
1252 return Surface{IMG_LoadGIF_IO(src.get())};
1253}
1254
1288{
1289 return Surface{IMG_LoadJPG_IO(src.get())};
1290}
1291
1325{
1326 return Surface{IMG_LoadJXL_IO(src.get())};
1327}
1328
1362{
1363 return Surface{IMG_LoadLBM_IO(src.get())};
1364}
1365
1399{
1400 return Surface{IMG_LoadPCX_IO(src.get())};
1401}
1402
1436{
1437 return Surface{IMG_LoadPNG_IO(src.get())};
1438}
1439
1473{
1474 return Surface{IMG_LoadPNM_IO(src.get())};
1475}
1476
1510{
1511 return Surface{IMG_LoadSVG_IO(src.get())};
1512}
1513
1547{
1548 return Surface{IMG_LoadQOI_IO(src.get())};
1549}
1550
1584{
1585 return Surface{IMG_LoadTGA_IO(src.get())};
1586}
1587
1621{
1622 return Surface{IMG_LoadTIF_IO(src.get())};
1623}
1624
1658{
1659 return Surface{IMG_LoadXCF_IO(src.get())};
1660}
1661
1695{
1696 return Surface{IMG_LoadXPM_IO(src.get())};
1697}
1698
1732{
1733 return Surface{IMG_LoadXV_IO(src.get())};
1734}
1735
1769{
1770 return Surface{IMG_LoadWEBP_IO(src.get())};
1771}
1772
1792inline Surface LoadSizedSVG(IOStreamBase& src, int width, int height)
1793{
1794 return Surface{IMG_LoadSizedSVG_IO(src.get(), width, height)};
1795}
1796
1814inline Surface ReadXPMFromArray(char** xpm)
1815{
1816 return Surface{IMG_ReadXPMFromArray(xpm)};
1817}
1818
1837{
1838 return Surface{IMG_ReadXPMFromArrayToRGB888(xpm)};
1839}
1840
1854inline void SaveAVIF(SurfaceBase& surface, StringParam file, int quality)
1855{
1856 CheckError(IMG_SaveAVIF(surface.get(), file, quality));
1857}
1858
1872inline void SaveAVIF(SurfaceRef surface, IOStreamBase& dst, int quality)
1873{
1874 CheckError(IMG_SaveAVIF_IO(surface.get(), dst.get(), false, quality));
1875}
1876
1890inline void SavePNG(SurfaceBase& surface, StringParam file)
1891{
1892 CheckError(IMG_SavePNG(surface.get(), file));
1893}
1894
1906inline void SavePNG(SurfaceRef surface, IOStreamBase& dst)
1907{
1908 CheckError(IMG_SavePNG_IO(surface.get(), dst.get(), false));
1909}
1910
1924inline void SaveJPG(SurfaceBase& surface, StringParam file, int quality)
1925{
1926 CheckError(IMG_SaveJPG(surface.get(), file, quality));
1927}
1928
1942inline void SaveJPG(SurfaceRef surface, IOStreamBase& dst, int quality)
1943{
1944 CheckError(IMG_SaveJPG_IO(surface.get(), dst.get(), false, quality));
1945}
1946
1957struct AnimationBase : Resource<IMG_Animation*>
1958{
1959 using Resource::Resource;
1960
1972 : Resource(IMG_LoadAnimation(file))
1973 {
1974 }
1975
1987 : Resource(IMG_LoadAnimation_IO(src.get(), false))
1988 {
1989 }
1990
2009 : Resource(IMG_LoadAnimationTyped_IO(src.get(), false, type))
2010 {
2012
2016 int GetWidth() const { return get()->w; }
2017
2021 int GetHeight() const { return get()->h; }
2022
2026 Point GetSize() const { return Point(GetWidth(), GetHeight()); }
2031 int GetCount() const { return get()->count; }
2032
2038 SurfaceRef GetFrame(int index) const { return get()->frames[index]; }
2039
2045 int GetDelay(int index) const { return get()->delays[index]; }
2046};
2047
2057{
2059
2063 constexpr AnimationRef(const AnimationRef& other)
2064 : AnimationBase(other.get())
2065 {
2066 }
2067
2071 constexpr AnimationRef(AnimationRef&& other)
2072 : AnimationBase(other.release())
2073 {
2074 }
2075
2079 constexpr ~AnimationRef() = default;
2080
2085 {
2086 release(other.release());
2087 return *this;
2088 }
2089
2099 void reset(IMG_Animation* newResource = {})
2100 {
2101 IMG_FreeAnimation(release(newResource));
2102 }
2103};
2104
2114{
2116
2120 constexpr explicit Animation(IMG_Animation* resource = {}) {}
2121
2122 constexpr Animation(const Animation& other) = delete;
2123
2127 constexpr Animation(Animation&& other) = default;
2128
2133
2138 {
2140 return *this;
2141 }
2142};
2143
2161{
2162 return Animation{IMG_LoadGIFAnimation_IO(src.get())};
2163}
2164
2181{
2182 return Animation{IMG_LoadWEBPAnimation_IO(src.get())};
2183}
2184
2185#pragma region impl
2186
2188
2190 : Resource(CheckError(IMG_Load(file)))
2191{
2192}
2193
2195 : Resource(CheckError(IMG_Load_IO(src.get(), false)))
2196{
2197}
2198
2200 : Resource(CheckError(IMG_LoadTexture(renderer.get(), file)))
2201{
2202}
2203
2205 : Resource(CheckError(IMG_LoadTexture_IO(renderer.get(), src.get(), false)))
2206{
2207}
2208
2209#pragma endregion impl
2210
2211} // namespace SDL
2212
2213#else // defined(SDL3PP_ENABLE_IMAGE) || defined(SDL3PP_DOC)
2214
2215namespace SDL {
2216
2217inline SurfaceBase::SurfaceBase(StringParam file)
2218 : Resource(CheckError(SDL_LoadBMP(file)))
2219{
2220}
2221
2222inline SurfaceBase::SurfaceBase(IOStreamBase& src)
2223 : Resource(CheckError(SDL_LoadBMP_IO(src.get(), false)))
2224{
2225}
2226
2227inline TextureBase::TextureBase(RendererBase& renderer, StringParam file)
2228 : Resource(CheckError(LoadTextureBMP(renderer, file).release()))
2229{
2230}
2231
2232inline TextureBase::TextureBase(RendererBase& renderer, IOStream& src)
2233 : Resource(CheckError(LoadTextureBMP(renderer, src).release()))
2234{
2235}
2236
2237}
2238
2239#endif // defined(SDL3PP_ENABLE_IMAGE) || defined(SDL3PP_DOC)
2240
2241#endif /* SDL3PP_IMAGE_H_ */
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr IMG_Animation * release(IMG_Animation * newResource={})
Return contained resource and empties or replace value.
Definition SDL3pp_resource.h:60
constexpr Resource(T resource={})
Constructs the underlying resource.
Definition SDL3pp_resource.h:22
constexpr T get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
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(RendererBase &renderer, IOStreamBase &src)
Load a BMP texture from a seekable SDL data stream.
Definition SDL3pp_render.h:3598
Surface ReadXPMFromArrayToRGB888(char **xpm)
Load an XPM image from a memory array.
Definition SDL3pp_image.h:1836
Surface LoadXCF(IOStreamBase &src)
Load a XCF image directly.
Definition SDL3pp_image.h:1657
Surface LoadJXL(IOStreamBase &src)
Load a JXL image directly.
Definition SDL3pp_image.h:1324
Surface LoadTGA(IOStreamBase &src)
Load a TGA image directly.
Definition SDL3pp_image.h:1583
Surface ReadXPMFromArray(char **xpm)
Load an XPM image from a memory array.
Definition SDL3pp_image.h:1814
bool isXV(IOStreamBase &src)
Detect XV image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:1062
Surface LoadWEBP(IOStreamBase &src)
Load a WEBP image directly.
Definition SDL3pp_image.h:1768
int IMG_Version()
This function gets the version of the dynamically linked SDL_image library.
Definition SDL3pp_image.h:79
bool isLBM(IOStreamBase &src)
Detect LBM image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:675
bool isPNM(IOStreamBase &src)
Detect PNM image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:804
Surface LoadCUR(IOStreamBase &src)
Load a CUR image directly.
Definition SDL3pp_image.h:1213
Surface LoadTIF(IOStreamBase &src)
Load a TIFF image directly.
Definition SDL3pp_image.h:1620
Surface LoadSizedSVG(IOStreamBase &src, int width, int height)
Load an SVG image, scaled to a specific size.
Definition SDL3pp_image.h:1792
Animation LoadGIFAnimation(IOStreamBase &src)
Load a GIF animation directly.
Definition SDL3pp_image.h:2160
bool isWEBP(IOStreamBase &src)
Detect WEBP image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:1105
bool isXPM(IOStreamBase &src)
Detect XPM image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:1019
Surface LoadLBM(IOStreamBase &src)
Load a LBM image directly.
Definition SDL3pp_image.h:1361
bool isCUR(IOStreamBase &src)
Detect CUR image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:460
bool isPNG(IOStreamBase &src)
Detect PNG image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:761
Surface LoadAVIF(IOStreamBase &src)
Load a AVIF image directly.
Definition SDL3pp_image.h:1139
Surface LoadSVG(IOStreamBase &src)
Load a SVG image directly.
Definition SDL3pp_image.h:1509
Surface LoadXV(IOStreamBase &src)
Load a XV image directly.
Definition SDL3pp_image.h:1731
void SavePNG(SurfaceBase &surface, StringParam file)
Save an SurfaceBase into a PNG image file.
Definition SDL3pp_image.h:1890
Surface LoadGIF(IOStreamBase &src)
Load a GIF image directly.
Definition SDL3pp_image.h:1250
Surface LoadPNG(IOStreamBase &src)
Load a PNG image directly.
Definition SDL3pp_image.h:1435
Animation LoadWEBPAnimation(IOStreamBase &src)
Load a WEBP animation directly.
Definition SDL3pp_image.h:2180
Texture LoadTexture(RendererBase &renderer, StringParam file)
Load an image from a filesystem path into a GPU texture.
Definition SDL3pp_image.h:242
bool isBMP(IOStreamBase &src)
Detect BMP image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:503
void SaveAVIF(SurfaceBase &surface, StringParam file, int quality)
Save an SurfaceBase into a AVIF image file.
Definition SDL3pp_image.h:1854
Surface LoadICO(IOStreamBase &src)
Load a ICO image directly.
Definition SDL3pp_image.h:1176
bool isAVIF(IOStreamBase &src)
Detect AVIF image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:374
bool isJPG(IOStreamBase &src)
Detect JPG image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:589
Surface LoadPCX(IOStreamBase &src)
Load a PCX image directly.
Definition SDL3pp_image.h:1398
bool isPCX(IOStreamBase &src)
Detect PCX image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:718
Surface LoadXPM(IOStreamBase &src)
Load a XPM image directly.
Definition SDL3pp_image.h:1694
Surface LoadSurface(IOStreamBase &src, StringParam type)
Load an image from an SDL data source into a software surface.
Definition SDL3pp_image.h:131
bool isGIF(IOStreamBase &src)
Detect GIF image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:546
Surface LoadPNM(IOStreamBase &src)
Load a PNM image directly.
Definition SDL3pp_image.h:1472
bool isQOI(IOStreamBase &src)
Detect QOI image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:890
bool isXCF(IOStreamBase &src)
Detect XCF image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:976
void SaveJPG(SurfaceBase &surface, StringParam file, int quality)
Save an SurfaceBase into a JPEG image file.
Definition SDL3pp_image.h:1924
bool isJXL(IOStreamBase &src)
Detect JXL image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:632
Surface LoadQOI(IOStreamBase &src)
Load a QOI image directly.
Definition SDL3pp_image.h:1546
bool isSVG(IOStreamBase &src)
Detect SVG image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:847
Surface LoadJPG(IOStreamBase &src)
Load a JPG image directly.
Definition SDL3pp_image.h:1287
bool isTIF(IOStreamBase &src)
Detect TIFF image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:933
bool isICO(IOStreamBase &src)
Detect ICO image data on a readable/seekable IOStreamBase.
Definition SDL3pp_image.h:417
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Animated image support.
Definition SDL3pp_image.h:1958
SurfaceRef GetFrame(int index) const
Return the frame image under given index.
Definition SDL3pp_image.h:2038
int GetHeight() const
Get the height in pixels.
Definition SDL3pp_image.h:2021
int GetDelay(int index) const
Return the frame delay under given index.
Definition SDL3pp_image.h:2045
AnimationBase(IOStreamBase &src, StringParam type)
Load an animation from an SDL datasource.
Definition SDL3pp_image.h:2008
int GetWidth() const
Get the width in pixels.
Definition SDL3pp_image.h:2016
AnimationBase(StringParam file)
Load an animation from a file.
Definition SDL3pp_image.h:1971
int GetCount() const
Return the number of frames.
Definition SDL3pp_image.h:2031
Point GetSize() const
Get the size in pixels.
Definition SDL3pp_image.h:2026
AnimationBase(IOStreamBase &src)
Load an animation from an IOStreamBase.
Definition SDL3pp_image.h:1986
Handle to a non owned animation.
Definition SDL3pp_image.h:2057
void reset(IMG_Animation *newResource={})
Dispose of an AnimationBase and free its resources.
Definition SDL3pp_image.h:2099
constexpr AnimationRef(AnimationRef &&other)
Move constructor.
Definition SDL3pp_image.h:2071
constexpr AnimationRef(const AnimationRef &other)
Copy constructor.
Definition SDL3pp_image.h:2063
AnimationRef & operator=(AnimationRef other)
Assignment operator.
Definition SDL3pp_image.h:2084
constexpr ~AnimationRef()=default
Default constructor.
Handle to an owned animation.
Definition SDL3pp_image.h:2114
~Animation()
Frees up resource when object goes out of scope.
Definition SDL3pp_image.h:2132
constexpr Animation(Animation &&other)=default
Move constructor.
constexpr Animation(IMG_Animation *resource={})
Constructs from the underlying resource.
Definition SDL3pp_image.h:2120
Animation & operator=(Animation other)
Assignment operator.
Definition SDL3pp_image.h:2137
The read/write operation structure.
Definition SDL3pp_iostream.h:107
Handle to an owned iOStream.
Definition SDL3pp_iostream.h:1613
The structure that defines a point (using integers)
Definition SDL3pp_rect.h:41
A structure representing rendering state.
Definition SDL3pp_render.h:157
A collection of pixels used in software blitting.
Definition SDL3pp_surface.h:138
SurfaceBase(StringParam file)
Load an image from a filesystem path into a software surface.
Definition SDL3pp_image.h:2189
Handle to a non owned surface.
Definition SDL3pp_surface.h:1814
Handle to an owned surface.
Definition SDL3pp_surface.h:1873
TextureBase(RendererBase &renderer, StringParam file)
Load an image from a filesystem path into a software surface.
Definition SDL3pp_image.h:2199
Handle to an owned texture.
Definition SDL3pp_render.h:2922