From 965de320a52d628a6707b533aead0e2e5a5da73f Mon Sep 17 00:00:00 2001
From: Ankith <itsankith26@gmail.com>
Date: Mon, 9 Dec 2024 23:25:41 +0530
Subject: [PATCH] Port imageext.c to SDL3(_image)

---
 .github/workflows/build-sdl3.yml     | 11 +++++++++++
 buildconfig/download_win_prebuilt.py | 16 ++++++++++++++++
 dev.py                               |  1 -
 meson.build                          | 14 ++++++++++----
 src_c/imageext.c                     |  8 ++++++++
 src_c/meson.build                    |  6 +++---
 6 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/.github/workflows/build-sdl3.yml b/.github/workflows/build-sdl3.yml
index 066439e04e..7b2cfe9987 100644
--- a/.github/workflows/build-sdl3.yml
+++ b/.github/workflows/build-sdl3.yml
@@ -86,6 +86,17 @@ jobs:
         cmake --build . --config Release --parallel
         sudo cmake --install . --config Release
 
+    - name: Install SDL3_image
+      if: matrix.os != 'windows-latest'
+      run: |
+        git clone https://github.com/libsdl-org/SDL_image
+        cd SDL_image
+        mkdir build
+        cd build
+        cmake -DCMAKE_BUILD_TYPE=Release ..
+        cmake --build . --config Release --parallel
+        sudo cmake --install . --config Release
+
     - name: Build with SDL3
       run: python3 dev.py build --sdl3
 
diff --git a/buildconfig/download_win_prebuilt.py b/buildconfig/download_win_prebuilt.py
index 0a5d6f6b5a..538d7f4922 100644
--- a/buildconfig/download_win_prebuilt.py
+++ b/buildconfig/download_win_prebuilt.py
@@ -90,6 +90,10 @@ def get_urls(x86=True, x64=True):
         '983484dd816abf25cdd5bce88ac69dbca1ea713a'
         ],
         [
+        'https://github.com/libsdl-org/SDL_image/releases/download/preview-3.1.0/SDL3_image-devel-3.1.0-VC.zip',
+        '8538fea0cc4aabba2fc64db06196f1bb76a2785f'
+        ],
+        [
         'https://github.com/libsdl-org/SDL_ttf/releases/download/release-2.22.0/SDL2_ttf-devel-2.22.0-VC.zip',
         '2d4f131909af2985b5ebc5ed296d28628c87c243'
         ],
@@ -213,6 +217,18 @@ def copy(src, dst):
                 'SDL2_image-2.8.2'
             )
         )
+        copy(
+            os.path.join(
+                temp_dir,
+                'SDL3_image-devel-3.1.0-VC/SDL3_image-3.1.0'
+            ),
+            os.path.join(
+                move_to_dir,
+                prebuilt_dir,
+                'SDL3_image-3.1.0'
+            )
+        )
+
         copy(
             os.path.join(
                 temp_dir,
diff --git a/dev.py b/dev.py
index c7a5dfb29e..7695f7eba0 100644
--- a/dev.py
+++ b/dev.py
@@ -26,7 +26,6 @@
 
 SDL3_ARGS = [
     "-Csetup-args=-Dsdl_api=3",
-    "-Csetup-args=-Dimage=disabled",
     "-Csetup-args=-Dmixer=disabled",
     "-Csetup-args=-Dfont=disabled",
 ]
diff --git a/meson.build b/meson.build
index b4f99359b3..489b4008e8 100644
--- a/meson.build
+++ b/meson.build
@@ -110,7 +110,7 @@ if plat == 'win' and host_machine.cpu_family().startswith('x86')
     endif
 
     sdl_ver = (sdl_api == 3) ? '3.1.6' : '2.30.10'
-    sdl_image_ver = '2.8.2'
+    sdl_image_ver = (sdl_api == 3) ? '3.1.0' : '2.8.2'
     sdl_mixer_ver = '2.8.0'
     sdl_ttf_ver = '2.22.0'
 
@@ -131,12 +131,18 @@ if plat == 'win' and host_machine.cpu_family().startswith('x86')
         pg_lib_dirs += sdl_image_lib_dir
         dlls += [
             sdl_image_lib_dir / '@0@.dll'.format(sdl_image),
-            sdl_image_lib_dir / 'optional' / 'libjpeg-62.dll',
-            sdl_image_lib_dir / 'optional' / 'libpng16-16.dll',
-            sdl_image_lib_dir / 'optional' / 'libtiff-5.dll',
+            sdl_image_lib_dir / 'optional' / (sdl_api == 3 ? 'libtiff-6.dll' : 'libtiff-5.dll'),
             sdl_image_lib_dir / 'optional' / 'libwebp-7.dll',
             sdl_image_lib_dir / 'optional' / 'libwebpdemux-2.dll',
         ]
+        # temporary solution to get things compiling under SDL3_image. In the future
+        # we would want to have libpng and libjpeg on SDL3_image as well.
+        if sdl_api != 3
+            dlls += [
+                sdl_image_lib_dir / 'optional' / 'libjpeg-62.dll',
+                sdl_image_lib_dir / 'optional' / 'libpng16-16.dll',
+            ]
+        endif
     endif
 
     # SDL_mixer
diff --git a/src_c/imageext.c b/src_c/imageext.c
index d2823fcb00..bb5873c5d7 100644
--- a/src_c/imageext.c
+++ b/src_c/imageext.c
@@ -46,7 +46,15 @@
 
 #include "pgopengl.h"
 
+#ifdef PG_SDL3
+#include <SDL3_image/SDL_image.h>
+
+// SDL3_images uses SDL3 error reporting API
+#define IMG_GetError SDL_GetError
+#else
 #include <SDL_image.h>
+#endif
+
 #ifdef WIN32
 #define strcasecmp _stricmp
 #else
diff --git a/src_c/meson.build b/src_c/meson.build
index bb94eddcb2..c8eb46677b 100644
--- a/src_c/meson.build
+++ b/src_c/meson.build
@@ -407,9 +407,6 @@ endif
 
 # optional modules
 
-# TODO: support SDL3
-if sdl_api != 3
-
 if sdl_image_dep.found()
     imageext = py.extension_module(
         'imageext',
@@ -421,6 +418,9 @@ if sdl_image_dep.found()
     )
 endif
 
+# TODO: support SDL3
+if sdl_api != 3
+
 if sdl_ttf_dep.found()
     font = py.extension_module(
         'font',