Skip to content

Commit

Permalink
Added support for saving AVIF images
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jan 26, 2024
1 parent abbf3a2 commit c1600c4
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 35 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
3.0.0:
* Added support for loading HDR AVIF images
* Added AVIF save support:
IMG_SaveAVIF() and IMG_SaveAVIF_RW()
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ option(SDL3IMAGE_XCF "Support loading XCF images" ON)
option(SDL3IMAGE_XPM "Support loading XPM images" ON)
option(SDL3IMAGE_XV "Support loading XV images" ON)

cmake_dependent_option(SDL3IMAGE_AVIF_SAVE "Add AVIF save support" ON SDL3IMAGE_AVIF OFF)
cmake_dependent_option(SDL3IMAGE_JPG_SAVE "Add JPEG save support" ON SDL3IMAGE_JPG OFF)
cmake_dependent_option(SDL3IMAGE_PNG_SAVE "Add PNG save support" ON SDL3IMAGE_PNG OFF)

Expand Down Expand Up @@ -506,7 +507,10 @@ if(SDL3IMAGE_AVIF)
endif()
endif()
if(SDL3IMAGE_AVIF_ENABLED)
target_compile_definitions(${sdl3_image_target_name} PRIVATE LOAD_AVIF)
target_compile_definitions(${sdl3_image_target_name} PRIVATE
LOAD_AVIF
SDL_IMAGE_SAVE_AVIF=$<BOOL:${SDL3IMAGE_AVIF_SAVE}>
)
if(SDL3IMAGE_AVIF_SHARED)
target_include_directories(${sdl3_image_target_name} PRIVATE
$<TARGET_PROPERTY:avif,INCLUDE_DIRECTORIES>
Expand Down
8 changes: 6 additions & 2 deletions examples/showimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@ int main(int argc, char *argv[])
if (surface) {
int result;
const char *ext = SDL_strrchr(saveFile, '.');
if ( ext && SDL_strcasecmp(ext, ".bmp") == 0 ) {
if ( ext && SDL_strcasecmp(ext, ".avif") == 0 ) {
result = IMG_SaveAVIF(surface, saveFile, 90);
} else if ( ext && SDL_strcasecmp(ext, ".bmp") == 0 ) {
result = SDL_SaveBMP(surface, saveFile);
} else if ( ext && SDL_strcasecmp(ext, ".jpg") == 0 ) {
result = IMG_SaveJPG(surface, saveFile, 90);
} else {
} else if ( ext && SDL_strcasecmp(ext, ".png") == 0 ) {
result = IMG_SavePNG(surface, saveFile);
} else {
result = SDL_SetError("Unknown save file type");
}
if ( result < 0 ) {
SDL_Log("Couldn't save %s: %s\n", saveFile, SDL_GetError());
Expand Down
38 changes: 30 additions & 8 deletions include/SDL3_image/SDL_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -1969,6 +1969,36 @@ extern DECLSPEC SDL_Surface * SDLCALL IMG_ReadXPMFromArray(char **xpm);
*/
extern DECLSPEC SDL_Surface * SDLCALL IMG_ReadXPMFromArrayToRGB888(char **xpm);

/**
* Save an SDL_Surface into a AVIF image file.
*
* If the file already exists, it will be overwritten.
*
* \param surface the SDL surface to save
* \param file path on the filesystem to write new file to.
* \returns 0 if successful, -1 on error
*
* \since This function is available since SDL_image 3.0.0.
*
* \sa IMG_SaveAVIF_RW
*/
extern DECLSPEC int SDLCALL IMG_SaveAVIF(SDL_Surface *surface, const char *file, int quality);

/**
* Save an SDL_Surface into AVIF image data, via an SDL_RWops.
*
* If you just want to save to a filename, you can use IMG_SaveAVIF() instead.
*
* \param surface the SDL surface to save
* \param dst the SDL_RWops to save the image data to.
* \returns 0 if successful, -1 on error.
*
* \since This function is available since SDL_image 3.0.0.
*
* \sa IMG_SaveAVIF
*/
extern DECLSPEC int SDLCALL IMG_SaveAVIF_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst, int quality);

/**
* Save an SDL_Surface into a PNG image file.
*
Expand All @@ -1981,8 +2011,6 @@ extern DECLSPEC SDL_Surface * SDLCALL IMG_ReadXPMFromArrayToRGB888(char **xpm);
* \since This function is available since SDL_image 3.0.0.
*
* \sa IMG_SavePNG_RW
* \sa IMG_SaveJPG
* \sa IMG_SaveJPG_RW
*/
extern DECLSPEC int SDLCALL IMG_SavePNG(SDL_Surface *surface, const char *file);

Expand All @@ -1998,8 +2026,6 @@ extern DECLSPEC int SDLCALL IMG_SavePNG(SDL_Surface *surface, const char *file);
* \since This function is available since SDL_image 3.0.0.
*
* \sa IMG_SavePNG
* \sa IMG_SaveJPG
* \sa IMG_SaveJPG_RW
*/
extern DECLSPEC int SDLCALL IMG_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst);

Expand All @@ -2017,8 +2043,6 @@ extern DECLSPEC int SDLCALL IMG_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst,
* \since This function is available since SDL_image 3.0.0.
*
* \sa IMG_SaveJPG_RW
* \sa IMG_SavePNG
* \sa IMG_SavePNG_RW
*/
extern DECLSPEC int SDLCALL IMG_SaveJPG(SDL_Surface *surface, const char *file, int quality);

Expand All @@ -2034,8 +2058,6 @@ extern DECLSPEC int SDLCALL IMG_SaveJPG(SDL_Surface *surface, const char *file,
* \since This function is available since SDL_image 3.0.0.
*
* \sa IMG_SaveJPG
* \sa IMG_SavePNG
* \sa IMG_SavePNG_RW
*/
extern DECLSPEC int SDLCALL IMG_SaveJPG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst, int quality);

Expand Down
Loading

0 comments on commit c1600c4

Please sign in to comment.