From 465ee6c14e21a8020fe92f13d3c03230b6bffc32 Mon Sep 17 00:00:00 2001
From: Artem Shubovych <shybovycha@gmail.com>
Date: Sun, 22 Dec 2024 13:59:46 +1100
Subject: [PATCH] Add sdl3_image port

---
 ports/sdl3-image/cmake-sdl3.patch | 13 ++++++++
 ports/sdl3-image/portfile.cmake   | 55 +++++++++++++++++++++++++++++++
 ports/sdl3-image/usage            |  9 +++++
 ports/sdl3-image/vcpkg.json       | 45 +++++++++++++++++++++++++
 versions/baseline.json            |  4 +++
 versions/s-/sdl3-image.json       |  9 +++++
 6 files changed, 135 insertions(+)
 create mode 100644 ports/sdl3-image/cmake-sdl3.patch
 create mode 100644 ports/sdl3-image/portfile.cmake
 create mode 100644 ports/sdl3-image/usage
 create mode 100644 ports/sdl3-image/vcpkg.json
 create mode 100644 versions/s-/sdl3-image.json

diff --git a/ports/sdl3-image/cmake-sdl3.patch b/ports/sdl3-image/cmake-sdl3.patch
new file mode 100644
index 00000000000000..ab51c3a52f9b84
--- /dev/null
+++ b/ports/sdl3-image/cmake-sdl3.patch
@@ -0,0 +1,13 @@
+diff --git a/cmake/SDL3_imageConfig.cmake.in b/cmake/SDL3_imageConfig.cmake.in
+index db72fd70..d942a698 100644
+--- a/cmake/SDL3_imageConfig.cmake.in
++++ b/cmake/SDL3_imageConfig.cmake.in
+@@ -43,6 +43,8 @@ set(SDLIMAGE_BACKEND_WIC       @SDLIMAGE_BACKEND_WIC@)
+ 
+ set(SDLIMAGE_SDL3_REQUIRED_VERSION  @SDL_REQUIRED_VERSION@)
+ 
++find_package(SDL3 CONFIG REQUIRED)
++
+ set(SDL3_image_SDL3_image-shared_FOUND FALSE)
+ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/SDL3_image-shared-targets.cmake")
+     include("${CMAKE_CURRENT_LIST_DIR}/SDL3_image-shared-targets.cmake")
diff --git a/ports/sdl3-image/portfile.cmake b/ports/sdl3-image/portfile.cmake
new file mode 100644
index 00000000000000..73f2f73b34c7fc
--- /dev/null
+++ b/ports/sdl3-image/portfile.cmake
@@ -0,0 +1,55 @@
+vcpkg_from_github(
+    OUT_SOURCE_PATH SOURCE_PATH
+    REPO libsdl-org/SDL_image
+    REF "preview-${VERSION}"
+    SHA512 e139fd9474213757f473ca96cb7df78e6b122ac1a0f8b88e66d28955b8ee0390f83ee14dfe4f188aa4ba14b812c5522ce366e61a00609a3079930d68d8233921
+    HEAD_REF main
+    PATCHES
+        cmake-sdl3.patch
+)
+
+vcpkg_check_features(
+    OUT_FEATURE_OPTIONS FEATURE_OPTIONS
+    FEATURES
+        libjpeg-turbo SDLIMAGE_JPG
+        libwebp       SDLIMAGE_WEBP
+        tiff          SDLIMAGE_TIF
+)
+
+vcpkg_cmake_configure(
+    SOURCE_PATH "${SOURCE_PATH}"
+    OPTIONS
+        ${FEATURE_OPTIONS}
+        -DSDLIMAGE_BACKEND_IMAGEIO=OFF
+        -DSDLIMAGE_BACKEND_STB=OFF
+        -DSDLIMAGE_DEPS_SHARED=OFF
+        -DSDLIMAGE_SAMPLES=OFF
+        -DSDLIMAGE_VENDORED=OFF
+)
+
+vcpkg_cmake_install()
+vcpkg_copy_pdbs()
+
+if(EXISTS "${CURRENT_PACKAGES_DIR}/cmake")
+    vcpkg_cmake_config_fixup(PACKAGE_NAME SDL3_image CONFIG_PATH cmake)
+elseif(EXISTS "${CURRENT_PACKAGES_DIR}/SDL3_image.framework/Resources")
+    vcpkg_cmake_config_fixup(PACKAGE_NAME SDL3_image CONFIG_PATH SDL3_image.framework/Resources)
+else()
+    vcpkg_cmake_config_fixup(PACKAGE_NAME SDL3_image CONFIG_PATH lib/cmake/SDL3_image)
+endif()
+
+vcpkg_fixup_pkgconfig()
+
+if(NOT VCPKG_TARGET_IS_LINUX AND NOT VCPKG_TARGET_IS_ANDROID AND NOT VCPKG_BUILD_TYPE)
+    vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/debug/lib/pkgconfig/SDL3-image.pc" "-lSDL3_image" "-lSDL3_imaged")
+endif()
+
+file(REMOVE_RECURSE 
+    "${CURRENT_PACKAGES_DIR}/debug/share"
+    "${CURRENT_PACKAGES_DIR}/debug/include"
+    "${CURRENT_PACKAGES_DIR}/SDL3_image.framework"
+    "${CURRENT_PACKAGES_DIR}/debug/SDL3_image.framework"
+)
+
+file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}")
+vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE.txt")
diff --git a/ports/sdl3-image/usage b/ports/sdl3-image/usage
new file mode 100644
index 00000000000000..4f44962718d6dd
--- /dev/null
+++ b/ports/sdl3-image/usage
@@ -0,0 +1,9 @@
+sdl3-image provides CMake targets:
+
+  find_package(SDL3_image CONFIG REQUIRED)
+  target_link_libraries(main PRIVATE $<IF:$<TARGET_EXISTS:SDL3_image::SDL3_image-shared>,SDL3_image::SDL3_image-shared,SDL3_image-static>)
+
+sdl3-image provides pkg-config modules:
+
+  # image loading library for Simple DirectMedia Layer
+  sdl3-image
diff --git a/ports/sdl3-image/vcpkg.json b/ports/sdl3-image/vcpkg.json
new file mode 100644
index 00000000000000..e686176581f848
--- /dev/null
+++ b/ports/sdl3-image/vcpkg.json
@@ -0,0 +1,45 @@
+{
+  "name": "sdl3-image",
+  "version": "3.1.0",
+  "description": "SDL_image is an image file loading library. It loads images as SDL surfaces and textures, and supports the following formats: BMP, GIF, JPEG, LBM, PCX, PNG, PNM, TGA, TIFF, WEBP, XCF, XPM, XV",
+  "homepage": "https://github.com/libsdl-org/SDL_image",
+  "license": "Zlib",
+  "dependencies": [
+    "libpng",
+    {
+      "name": "sdl3",
+      "default-features": false
+    },
+    {
+      "name": "vcpkg-cmake",
+      "host": true
+    },
+    {
+      "name": "vcpkg-cmake-config",
+      "host": true
+    }
+  ],
+  "features": {
+    "libjpeg-turbo": {
+      "description": "Support for JPEG image format",
+      "dependencies": [
+        "libjpeg-turbo"
+      ]
+    },
+    "libwebp": {
+      "description": "Support for WEBP image format.",
+      "dependencies": [
+        "libwebp"
+      ]
+    },
+    "tiff": {
+      "description": "Support for TIFF image format",
+      "dependencies": [
+        {
+          "name": "tiff",
+          "default-features": false
+        }
+      ]
+    }
+  }
+}
diff --git a/versions/baseline.json b/versions/baseline.json
index 9fbdb6bf34425f..982041db0e45de 100644
--- a/versions/baseline.json
+++ b/versions/baseline.json
@@ -8276,6 +8276,10 @@
       "baseline": "3.1.8-preview",
       "port-version": 0
     },
+    "sdl3-image": {
+      "baseline": "3.1.0",
+      "port-version": 0
+    },
     "seacas": {
       "baseline": "2022-11-22",
       "port-version": 8
diff --git a/versions/s-/sdl3-image.json b/versions/s-/sdl3-image.json
new file mode 100644
index 00000000000000..8d9355feb960fc
--- /dev/null
+++ b/versions/s-/sdl3-image.json
@@ -0,0 +1,9 @@
+{
+  "versions": [
+    {
+      "git-tree": "e5c443cbdf6896a4989f1a0a6034be21c3fc43c0",
+      "version": "3.1.0",
+      "port-version": 0
+    }
+  ]
+}