Skip to content

Commit

Permalink
Removed IMG_Init() and IMG_Quit()
Browse files Browse the repository at this point in the history
IMG_Init() and IMG_Quit() are no longer necessary. If an image format requires dynamically loading a support library, that will be done automatically.
  • Loading branch information
slouken committed Dec 6, 2024
1 parent c5bb635 commit f389281
Show file tree
Hide file tree
Showing 28 changed files with 66 additions and 493 deletions.
19 changes: 1 addition & 18 deletions cmake/test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,15 @@
#include <SDL3/SDL_main.h>
#include <SDL3_image/SDL_image.h>

#define TEST_INIT_FLAG(FLAG) do { \
if ((IMG_Init(FLAG) & FLAG) == FLAG) { \
SDL_Log("IMG_Init("#FLAG") succeeded"); \
} else { \
SDL_Log("IMG_Init("#FLAG") failed"); \
} \
} while (0);

#define FOREACH_INIT_FLAGS(X) \
X(IMG_INIT_JPG) \
X(IMG_INIT_PNG) \
X(IMG_INIT_TIF) \
X(IMG_INIT_WEBP) \
X(IMG_INIT_JXL) \
X(IMG_INIT_AVIF) \

int main(int argc, char *argv[])
{
if (!SDL_Init(0)) {
SDL_Log("SDL_Init(0) failed: %s\n", SDL_GetError());
return 1;
}

FOREACH_INIT_FLAGS(TEST_INIT_FLAG)
IMG_Version();

IMG_Quit();
SDL_Quit();
return 0;
}
6 changes: 6 additions & 0 deletions docs/README-migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# Migrating to SDL_image 3.0

This guide provides useful information for migrating applications from SDL_image 2.0 to SDL_image 3.0.

IMG_Init() and IMG_Quit() are no longer necessary. If an image format requires dynamically loading a support library, that will be done automatically.
101 changes: 0 additions & 101 deletions include/SDL3_image/SDL_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,107 +67,6 @@ extern "C" {
*/
extern SDL_DECLSPEC int SDLCALL IMG_Version(void);

/**
* Initialization flags
*/
typedef Uint32 IMG_InitFlags;

#define IMG_INIT_JPG 0x00000001
#define IMG_INIT_PNG 0x00000002
#define IMG_INIT_TIF 0x00000004
#define IMG_INIT_WEBP 0x00000008
#define IMG_INIT_JXL 0x00000010
#define IMG_INIT_AVIF 0x00000020

/**
* Initialize SDL_image.
*
* This function loads dynamic libraries that SDL_image needs, and prepares
* them for use. This must be the first function you call in SDL_image, and if
* it fails you should not continue with the library.
*
* Flags should be one or more flags from IMG_InitFlags OR'd together. It
* returns the flags successfully initialized, or 0 on failure.
*
* Currently, these flags are:
*
* - `IMG_INIT_JPG`
* - `IMG_INIT_PNG`
* - `IMG_INIT_TIF`
* - `IMG_INIT_WEBP`
* - `IMG_INIT_JXL`
* - `IMG_INIT_AVIF`
*
* More flags may be added in a future SDL_image release.
*
* This function may need to load external shared libraries to support various
* codecs, which means this function can fail to initialize that support on an
* otherwise-reasonable system if the library isn't available; this is not
* just a question of exceptional circumstances like running out of memory at
* startup!
*
* Note that you may call this function more than once to initialize with
* additional flags. The return value will reflect both new flags that
* successfully initialized, and also include flags that had previously been
* initialized as well.
*
* As this will return previously-initialized flags, it's legal to call this
* with zero (no flags set). This is a safe no-op that can be used to query
* the current initialization state without changing it at all.
*
* Since this returns previously-initialized flags as well as new ones, and
* you can call this with zero, you should not check for a zero return value
* to determine an error condition. Instead, you should check to make sure all
* the flags you require are set in the return value. If you have a game with
* data in a specific format, this might be a fatal error. If you're a generic
* image displaying app, perhaps you are fine with only having JPG and PNG
* support and can live without WEBP, even if you request support for
* everything.
*
* Unlike other SDL satellite libraries, calls to IMG_Init do not stack; a
* single call to IMG_Quit() will deinitialize everything and does not have to
* be paired with a matching IMG_Init call. For that reason, it's considered
* best practices to have a single IMG_Init and IMG_Quit call in your program.
* While this isn't required, be aware of the risks of deviating from that
* behavior.
*
* After initializing SDL_image, the app may begin to load images into
* SDL_Surfaces or SDL_Textures.
*
* \param flags initialization flags, OR'd together.
* \returns all currently initialized flags.
*
* \since This function is available since SDL_image 3.0.0.
*
* \sa IMG_Quit
*/
extern SDL_DECLSPEC IMG_InitFlags SDLCALL IMG_Init(IMG_InitFlags flags);

/**
* Deinitialize SDL_image.
*
* This should be the last function you call in SDL_image, after freeing all
* other resources. This will unload any shared libraries it is using for
* various codecs.
*
* After this call, a call to IMG_Init(0) will return 0 (no codecs loaded).
*
* You can safely call IMG_Init() to reload various codec support after this
* call.
*
* Unlike other SDL satellite libraries, calls to IMG_Init do not stack; a
* single call to IMG_Quit() will deinitialize everything and does not have to
* be paired with a matching IMG_Init call. For that reason, it's considered
* best practices to have a single IMG_Init and IMG_Quit call in your program.
* While this isn't required, be aware of the risks of deviating from that
* behavior.
*
* \since This function is available since SDL_image 3.0.0.
*
* \sa IMG_Init
*/
extern SDL_DECLSPEC void SDLCALL IMG_Quit(void);

/**
* Load an image from an SDL data source into a software surface.
*
Expand Down
65 changes: 0 additions & 65 deletions src/IMG.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
/* A simple library to load images of various formats as SDL surfaces */

#include <SDL3_image/SDL_image.h>
#include "IMG.h"

#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
Expand Down Expand Up @@ -89,70 +88,6 @@ int IMG_Version(void)
return SDL_IMAGE_VERSION;
}

static IMG_InitFlags initialized = 0;

IMG_InitFlags IMG_Init(IMG_InitFlags flags)
{
IMG_InitFlags result = 0;

if (flags & IMG_INIT_AVIF) {
if ((initialized & IMG_INIT_AVIF) || IMG_InitAVIF() == 0) {
result |= IMG_INIT_AVIF;
}
}
if (flags & IMG_INIT_JPG) {
if ((initialized & IMG_INIT_JPG) || IMG_InitJPG() == 0) {
result |= IMG_INIT_JPG;
}
}
if (flags & IMG_INIT_JXL) {
if ((initialized & IMG_INIT_JXL) || IMG_InitJXL() == 0) {
result |= IMG_INIT_JXL;
}
}
if (flags & IMG_INIT_PNG) {
if ((initialized & IMG_INIT_PNG) || IMG_InitPNG() == 0) {
result |= IMG_INIT_PNG;
}
}
if (flags & IMG_INIT_TIF) {
if ((initialized & IMG_INIT_TIF) || IMG_InitTIF() == 0) {
result |= IMG_INIT_TIF;
}
}
if (flags & IMG_INIT_WEBP) {
if ((initialized & IMG_INIT_WEBP) || IMG_InitWEBP() == 0) {
result |= IMG_INIT_WEBP;
}
}
initialized |= result;

return initialized;
}

void IMG_Quit(void)
{
if (initialized & IMG_INIT_AVIF) {
IMG_QuitAVIF();
}
if (initialized & IMG_INIT_JPG) {
IMG_QuitJPG();
}
if (initialized & IMG_INIT_JXL) {
IMG_QuitJXL();
}
if (initialized & IMG_INIT_PNG) {
IMG_QuitPNG();
}
if (initialized & IMG_INIT_TIF) {
IMG_QuitTIF();
}
if (initialized & IMG_INIT_WEBP) {
IMG_QuitWEBP();
}
initialized = 0;
}

#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
/* Load an image from a file */
SDL_Surface *IMG_Load(const char *file)
Expand Down
38 changes: 0 additions & 38 deletions src/IMG.h

This file was deleted.

36 changes: 0 additions & 36 deletions src/IMG_ImageIO.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#if defined(__APPLE__) && !defined(SDL_IMAGE_USE_COMMON_BACKEND)

#include <SDL3_image/SDL_image.h>
#include "IMG.h"

// Used because CGDataProviderCreate became deprecated in 10.5
#include <AvailabilityMacros.h>
Expand Down Expand Up @@ -357,41 +356,6 @@ static CFDictionaryRef CreateHintDictionary(CFStringRef uti_string_hint)
}


#ifdef JPG_USES_IMAGEIO

int IMG_InitJPG(void)
{
return 0;
}

void IMG_QuitJPG(void)
{
}

#endif /* JPG_USES_IMAGEIO */

#ifdef PNG_USES_IMAGEIO

int IMG_InitPNG(void)
{
return 0;
}

void IMG_QuitPNG(void)
{
}

#endif /* PNG_USES_IMAGEIO */

int IMG_InitTIF(void)
{
return 0;
}

void IMG_QuitTIF(void)
{
}

static bool Internal_isType (SDL_IOStream *rw_ops, CFStringRef uti_string_to_test)
{
bool is_type = false;
Expand Down
41 changes: 6 additions & 35 deletions src/IMG_WIC.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
#if defined(SDL_IMAGE_USE_WIC_BACKEND)

#include <SDL3_image/SDL_image.h>
#include "IMG.h"
#define COBJMACROS
#include <initguid.h>
#include <wincodec.h>

static IWICImagingFactory* wicFactory = NULL;

static int WIC_Init(void)
static bool WIC_Init(void)
{
if (wicFactory == NULL) {
HRESULT hr = CoCreateInstance(
Expand All @@ -40,49 +39,21 @@ static int WIC_Init(void)
(void**)&wicFactory
);
if (FAILED(hr)) {
return -1;
return false;
}
}

return 0;
return true;
}

#if 0
static void WIC_Quit(void)
{
if (wicFactory) {
IWICImagingFactory_Release(wicFactory);
}
}

int IMG_InitPNG(void)
{
return WIC_Init();
}

void IMG_QuitPNG(void)
{
WIC_Quit();
}

int IMG_InitJPG(void)
{
return WIC_Init();
}

void IMG_QuitJPG(void)
{
WIC_Quit();
}

int IMG_InitTIF(void)
{
return WIC_Init();
}

void IMG_QuitTIF(void)
{
WIC_Quit();
}
#endif // 0

bool IMG_isPNG(SDL_IOStream *src)
{
Expand Down Expand Up @@ -214,7 +185,7 @@ static SDL_Surface* WIC_LoadImage(SDL_IOStream *src)
IWICFormatConverter* formatConverter = NULL;
UINT width, height;

if (wicFactory == NULL && (WIC_Init() < 0)) {
if (!WIC_Init()) {
SDL_SetError("WIC failed to initialize!");
return NULL;
}
Expand Down
Loading

0 comments on commit f389281

Please sign in to comment.