Skip to content

Commit

Permalink
Update SDL_image bindings for release 2.8.0 (#274)
Browse files Browse the repository at this point in the history
* Bump CI binaries to latest release

* Update bindings to image 2.8.0

* Skip pallete tests on very old SDL2

* Sync hints with SDL 2.28.5

* Handle new macOS DMG layout

* Add test for new image function

* Updated NEWS.rst

* Fixed silly test bug
  • Loading branch information
a-hurst authored Dec 16, 2023
1 parent dd9609b commit e52a594
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 18 deletions.
11 changes: 11 additions & 0 deletions .ci/getsdl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
}

libversions = {
'2.28.5': {
'SDL2': '2.28.5',
'SDL2_mixer': '2.6.3',
'SDL2_ttf': '2.20.2',
'SDL2_image': '2.8.1',
'SDL2_gfx': '1.0.4'
},
'2.28.0': {
'SDL2': '2.28.0',
'SDL2_mixer': '2.6.0',
Expand Down Expand Up @@ -166,6 +173,8 @@ def getDLLs(platform_name, version):
dllname = lib + '.framework'
dllpath = os.path.join(mountpoint, dllname)
dlloutpath = os.path.join(dlldir, dllname)
optpath = os.path.join(mountpoint, 'optional')
extraframeworkpath = os.path.join(dlloutpath, 'Versions', 'A', 'Frameworks')

# Download disk image containing library
libversion = libversions[version][lib]
Expand All @@ -177,6 +186,8 @@ def getDLLs(platform_name, version):
# Mount image, extract framework, then unmount
sub.check_call(['hdiutil', 'attach', outpath, '-mountpoint', mountpoint])
shutil.copytree(dllpath, dlloutpath, symlinks=True)
if os.path.isdir(optpath):
shutil.copytree(optpath, extraframeworkpath, symlinks=True)
sub.call(['hdiutil', 'unmount', mountpoint])

elif platform_name in ['win32', 'win-amd64']:
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

strategy:
matrix:
sdl2: ['2.28.0']
sdl2: ['2.28.5']

env:
PYSDL2_DLL_VERSION: ${{ matrix.sdl2 }}
Expand Down Expand Up @@ -56,14 +56,14 @@ jobs:
fail-fast: false
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
sdl2: ['2.28.0']
sdl2: ['2.28.5']
name-prefix: ['Linux (Python ']
include:
- python-version: 'pypy-2.7'
sdl2: '2.28.0'
sdl2: '2.28.5'
name-prefix: 'Experimental / Linux ('
- python-version: 'pypy-3.8'
sdl2: '2.28.0'
sdl2: '2.28.5'
name-prefix: 'Experimental / Linux ('

env:
Expand Down Expand Up @@ -105,7 +105,7 @@ jobs:
fail-fast: false
matrix:
python-version: ['3.10']
sdl2: ['2.28.0', '2.26.5', '2.24.0']
sdl2: ['2.28.5', '2.26.5', '2.24.0']
name-prefix: ['macOS (Python ']
include:
- python-version: '3.10'
Expand Down Expand Up @@ -159,14 +159,14 @@ jobs:
python-version: ['3.10']
architecture: ['x64']
sdl2: [
'2.28.0', '2.26.5', '2.24.0', '2.0.22', '2.0.20', '2.0.18', '2.0.16', '2.0.14',
'2.28.5', '2.26.5', '2.24.0', '2.0.22', '2.0.20', '2.0.18', '2.0.16', '2.0.14',
'2.0.12', '2.0.10', '2.0.9', '2.0.8', '2.0.7', '2.0.6', '2.0.5'
]
name-prefix: ['Windows (Python ']
include:
- python-version: '3.8'
architecture: 'x86'
sdl2: '2.28.0'
sdl2: '2.28.5'
name-prefix: 'Windows 32-bit (Python '
- python-version: '3.8'
architecture: 'x86'
Expand Down Expand Up @@ -218,7 +218,7 @@ jobs:
fail-fast: false
matrix:
architecture: ['x86']
sdl2: ['2.28.0', '2.0.5']
sdl2: ['2.28.5', '2.0.5']

env:
PYSDL2_DLL_VERSION: ${{ matrix.sdl2 }}
Expand Down
2 changes: 2 additions & 0 deletions doc/modules/sdl2_sdlimage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ Animation objects and functions

.. autofunction:: IMG_LoadGIFAnimation_RW

.. autofunction:: IMG_LoadWEBPAnimation_RW

.. autofunction:: IMG_FreeAnimation


Expand Down
11 changes: 11 additions & 0 deletions doc/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ This describes the latest changes between the PySDL2 releases.
0.9.17 (Unreleased)
-------------------

New Features:

* Updated to wrap new function added in SDL_image 2.8.0 (PR #274).
* Updated to wrap new hints added in SDL 2.28.2 and 2.28.5 (PR #274).


Fixed Bugs:

* Fixed proper exporting of controller enum constants for the NVidia Shield and
Joycon controllers.


0.9.16
------
Expand Down
4 changes: 4 additions & 0 deletions sdl2/hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"SDL_HINT_JOYSTICK_HIDAPI_CORRELATE_XINPUT",
"SDL_HINT_JOYSTICK_ROG_CHAKRAM",
"SDL_HINT_JOYSTICK_THREAD",
"SDL_HINT_JOYSTICK_WGI",
"SDL_HINT_KMSDRM_REQUIRE_DRM_MASTER",
"SDL_HINT_JOYSTICK_DEVICE",
"SDL_HINT_LINUX_DIGITAL_HATS",
Expand Down Expand Up @@ -120,6 +121,7 @@
"SDL_HINT_RENDER_OPENGL_SHADERS",
"SDL_HINT_RENDER_SCALE_QUALITY",
"SDL_HINT_RENDER_VSYNC",
"SDL_HINT_RENDER_METAL_PREFER_LOW_POWER_DEVICE",
"SDL_HINT_PS2_DYNAMIC_VSYNC",
"SDL_HINT_RETURN_KEY_HIDES_IME",
"SDL_HINT_RPI_VIDEO_LAYER",
Expand Down Expand Up @@ -214,6 +216,7 @@
SDL_HINT_RENDER_LINE_METHOD = b"SDL_RENDER_LINE_METHOD"
SDL_HINT_RENDER_DRIVER = b"SDL_RENDER_DRIVER"
SDL_HINT_RENDER_LOGICAL_SIZE_MODE = b"SDL_RENDER_LOGICAL_SIZE_MODE"
SDL_HINT_RENDER_METAL_PREFER_LOW_POWER_DEVICE = b"SDL_RENDER_METAL_PREFER_LOW_POWER_DEVICE"
SDL_HINT_RENDER_OPENGL_SHADERS = b"SDL_RENDER_OPENGL_SHADERS"
SDL_HINT_RENDER_SCALE_QUALITY = b"SDL_RENDER_SCALE_QUALITY"
SDL_HINT_RENDER_VSYNC = b"SDL_RENDER_VSYNC"
Expand Down Expand Up @@ -336,6 +339,7 @@
SDL_HINT_JOYSTICK_RAWINPUT_CORRELATE_XINPUT = b"SDL_JOYSTICK_RAWINPUT_CORRELATE_XINPUT"
SDL_HINT_JOYSTICK_ROG_CHAKRAM = b"SDL_JOYSTICK_ROG_CHAKRAM"
SDL_HINT_JOYSTICK_THREAD = b"SDL_JOYSTICK_THREAD"
SDL_HINT_JOYSTICK_WGI = b"SDL_JOYSTICK_WGI"
SDL_HINT_JOYSTICK_DEVICE = b"SDL_JOYSTICK_DEVICE"


Expand Down
36 changes: 28 additions & 8 deletions sdl2/sdlimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def get_dll_file():
# Constants, enums, type definitions, and macros

SDL_IMAGE_MAJOR_VERSION = 2
SDL_IMAGE_MINOR_VERSION = 6
SDL_IMAGE_PATCHLEVEL = 0
SDL_IMAGE_MINOR_VERSION = 8
SDL_IMAGE_PATCHLEVEL = 1

def SDL_IMAGE_VERSION(x):
x.major = SDL_IMAGE_MAJOR_VERSION
Expand Down Expand Up @@ -173,6 +173,7 @@ class IMG_Animation(Structure):
SDLFunc("IMG_LoadAnimationTyped_RW", [_P(SDL_RWops), c_int, c_char_p], _P(IMG_Animation), added='2.6.0'),
SDLFunc("IMG_FreeAnimation", [_P(IMG_Animation)], added='2.6.0'),
SDLFunc("IMG_LoadGIFAnimation_RW", [_P(SDL_RWops)], _P(IMG_Animation), added='2.6.0'),
SDLFunc("IMG_LoadWEBPAnimation_RW", [_P(SDL_RWops)], _P(IMG_Animation), added='2.8.0'),
]
_ctypes = AttributeDict()
for f in _funcdefs:
Expand Down Expand Up @@ -1114,8 +1115,8 @@ def IMG_LoadAnimation(file):
"""Loads an animated image from a file.
For more information on how to work with imported animations, see the
:class:`IMG_Animation` documentation. As of SDL_image 2.6.0, this currently
only supports GIF animations.
:class:`IMG_Animation` documentation. As of SDL_image 2.8.0, this currently
supports GIF and WEBP animations.
`Note: Added in SDL_image 2.6.0`
Expand All @@ -1134,8 +1135,8 @@ def IMG_LoadAnimation_RW(src, freesrc):
"""Loads an animated image from an SDL file object.
For more information on how to work with imported animations, see the
:class:`IMG_Animation` documentation. As of SDL_image 2.6.0, this currently
only supports GIF animations.
:class:`IMG_Animation` documentation. As of SDL_image 2.8.0, this currently
supports GIF and WEBP animations.
`Note: Added in SDL_image 2.6.0`
Expand All @@ -1156,8 +1157,8 @@ def IMG_LoadAnimationTyped_RW(src, freesrc, type):
"""Loads an animated image from an SDL file object as a specific format.
For more information on how to work with imported animations, see the
:class:`IMG_Animation` documentation. As of SDL_image 2.6.0, the only
supported animation type is ``b'GIF'``.
:class:`IMG_Animation` documentation. As of SDL_image 2.8.0, the only
supported animation types are ``b'GIF'`` and ``b'WEBP'``.
`Note: Added in SDL_image 2.6.0`
Expand Down Expand Up @@ -1210,6 +1211,25 @@ def IMG_LoadGIFAnimation_RW(src):
"""
return _ctypes["IMG_LoadGIFAnimation_RW"](src)

def IMG_LoadWEBPAnimation_RW(src):
"""Loads a WEBP animation from an SDL file object.
For more information on how to work with imported animations, see the
:class:`IMG_Animation` documentation. Use the :func:`IMG_GetError`
function to check for any errors.
`Note: Added in SDL_image 2.8.0`
Args:
src (:obj:`SDL_RWops`): The file object from which to load the WEBP.
Returns:
POINTER(:obj:`IMG_Animation`): A pointer to an animation object, or a
a null pointer if there was an error.
"""
return _ctypes["IMG_LoadWEBPAnimation_RW"](src)


IMG_SetError = SDL_SetError
IMG_GetError = SDL_GetError
Binary file added sdl2/test/resources/animationtest.webp
Binary file not shown.
4 changes: 4 additions & 0 deletions sdl2/test/sdl2ext_image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ def test_pillow_to_image(with_sdl):
check_image_contents(sf)
surf.SDL_FreeSurface(sf)

# Pallete conversion not working reliably on 2.0.5 64-bit
if sdl2.dll.version_tuple < (2, 0, 6):
pytest.skip("Requires SDL2 >= 2.0.6")

# Try converting a palette image
palette_img = pil_img.convert("P", palette=WEB_PALETTE)
sfp = sdl2ext.pillow_to_surface(palette_img)
Expand Down
14 changes: 13 additions & 1 deletion sdl2/test/sdlimage_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
]

animation_formats = [
"gif",
"gif", "webp",
]


Expand All @@ -53,6 +53,10 @@
if img_ver < 2060:
formats.remove("qoi")

# WEBP animations unsupported on SDL2_image < 2.8.0
if img_ver < 2080:
animation_formats.remove("webp")

# SVG unsupported on SDL2_image < 2.0.2 as well as in Conda's current (2.0.5)
# Windows binaries
if img_ver < 2002 or (isconda and iswindows):
Expand Down Expand Up @@ -727,3 +731,11 @@ def test_IMG_LoadGIFAnimation_RW():
fp.close()
_verify_anim_load(anim)
sdlimage.IMG_FreeAnimation(anim)

@pytest.mark.skipif(sdlimage.dll.version < 2080, reason="Added in 2.8.0")
def test_IMG_LoadWEBPAnimation_RW():
fp = io.open(_get_animation_path("webp"), "rb")
anim = sdlimage.IMG_LoadWEBPAnimation_RW(rwops.rw_from_object(fp))
fp.close()
_verify_anim_load(anim)
sdlimage.IMG_FreeAnimation(anim)
2 changes: 1 addition & 1 deletion sdl2/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

SDL_MAJOR_VERSION = 2
SDL_MINOR_VERSION = 28
SDL_PATCHLEVEL = 0
SDL_PATCHLEVEL = 5

def SDL_VERSION(x):
x.major = SDL_MAJOR_VERSION
Expand Down

0 comments on commit e52a594

Please sign in to comment.