IMG_Load(3)
π IMG_Load(3) β like, the SDL_image3 functions era π
okay bestie, pull up a chair, because this one is a classic. This is IMG_Load from the SDL_image3 manual (the SDL_image one, yeah), and honestly? She's iconic. Let's get into it. π
β¨ NAME β¨
IMG_Load β load an image from a filesystem path into a software surface. that's it. that's the whole vibe. π¦
πΈ SYNOPSIS πΈ
#include <SDL3_image/SDL_image.h>
SDL_Surface * IMG_Load(const char *file);
literally just point her at a file and she gives you pixels. slay. π
π DESCRIPTION π
okay so first things first: an SDL_Surface is a buffer of pixels in memory that the CPU can actually reach. use this one when you're planning to hand that data off to something else, or keep messing with it in your own code. it's the "I want to touch the pixels myself" option, and honestly? relatable. π«Ά
now, no cap on this part: there are no guarantees about what format the new SDL_Surface data is gonna be in. in many cases, SDL_image will try to give you a surface that matches the provided image exactly, but in other cases she might have to convert β either because the image is in a format SDL doesn't directly support, or because it's compressed data that could reasonably uncompress into a bunch of different formats and SDL_image just had to pick one. π she did her best!
the good news? you can inspect an SDL_Surface for its specifics, and then use SDL_ConvertSurface to migrate to any supported format. so like, you're never stuck. π
oh and β if the image format supports a transparent pixel, SDL will set the colorkey for the surface. and if you wanna enable RLE acceleration on the surface afterwards, you just call:
SDL_SetSurfaceColorKey(image, SDL_RLEACCEL, image->format->colorkey);
that's the whole thing. no notes. π₯
also, quick FYI bestie: there's a separate function for reading files from an SDL_IOStream, in case you need an i/o abstraction to feed data in from literally anywhere instead of a plain old filesystem read. that function is IMG_Load_IO(). iykyk. β¨
and if you're using SDL's 2D rendering API? there's an equivalent call that loads images directly into an SDL_Texture for the GPU to use, no software surface middleman needed β call IMG_LoadTexture() instead. main character energy, honestly. π
when you're done with the returned surface, the app should dispose of it with a call to SDL_DestroySurface(). don't just ghost her, okay? π the docs link, verbatim, because the manpage renders it in a whole state:
https:// wiki.www:lenstrwww:splitstr:nwww:splitstr:r[char46]libsdl.www:lenstrwww:splitstr:nwww:splitstr:r[char46]org/
SDL3/SDL_DestroySurface
π¨ friendly reminder: that surface is yours to clean up. SDL_DestroySurface() or it's a leak, and leaks are not the vibe. π
π FUNCTION PARAMETERS π
fileβ a path on the filesystem to load an image from. give her a path, she gives you pixels. simple as that. π
π RETURN VALUE π
returns a new SDL surface, or NULL on error. so like β always check for NULL, bestie. always. π
β AVAILABILITY β
this function is available since SDL_image 3.0.0. she's been here a minute. π
π¦ SEE ALSO π¦
and that's the whole page, bestie!! go forth and load some images π§
mwah π β this has been SDL_image, version SDL_image 3.4.4, saying goodnight from the IMG_Load(3) corner of the manual. take care of your surfaces and they'll take care of you. πβ¨
π¦ where this page comes from
- ubuntu stonking: libsdl3-image-doc 3.4.4+ds-2 Β· zlib/libpng AND HPND-like AND zlib-like-permissive
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms