Skip to content

Adds SDL3_image port #24634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,11 @@ var SDL2_IMAGE_FORMATS = [];
// [link]
var SDL2_MIXER_FORMATS = ["ogg"];

// Formats to support in SDL3_image. Valid values: bmp, gif, lbm, pcx, png, pnm,
// tga, webp, xcf, xpm, xv
// [link]
var SDL3_IMAGE_FORMATS = [];

// 1 = use sqlite3 from emscripten-ports
// Alternate syntax: --use-port=sqlite3
// [compile+link]
Expand Down
78 changes: 78 additions & 0 deletions test/browser/test_sdl3_image.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2025 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

#include <stdio.h>
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <assert.h>
#include <emscripten.h>
#include <unistd.h>
#include <stdlib.h>

#ifndef BITSPERPIXEL
#define BITSPERPIXEL 32
#endif

int testImage(SDL_Renderer* renderer, const char* fileName) {
SDL_Surface *image = IMG_Load(fileName);
if (!image)
{
printf("IMG_Load: %s\n", SDL_GetError());
return 0;
}
assert(SDL_GetPixelFormatDetails(image->format)->bits_per_pixel == BITSPERPIXEL);
assert(SDL_GetPixelFormatDetails(image->format)->bytes_per_pixel == BITSPERPIXEL / 8);
assert(image->pitch == BITSPERPIXEL / 8 * image->w);
int result = image->w;

#ifndef NO_PRELOADED
int w, h;
char *data = emscripten_get_preloaded_image_data(fileName, &w, &h);

assert(data);
assert(w == image->w);
assert(h == image->h);
#endif

SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, image);

SDL_RenderTexture(renderer, tex, NULL, NULL);

SDL_DestroyTexture(tex);

SDL_DestroySurface(image);

#ifndef NO_PRELOADED
free(data);
#endif

return result;
}

int main() {
SDL_Init(SDL_INIT_VIDEO);

SDL_Window *window;
SDL_Renderer *renderer;

SDL_CreateWindowAndRenderer("Test Window", 600, 450, 0, &window, &renderer);

int result = 0;

result = testImage(renderer, SCREENSHOT_DIRNAME "/" SCREENSHOT_BASENAME); // absolute path
assert(result != 0);

chdir(SCREENSHOT_DIRNAME);
result = testImage(renderer, "./" SCREENSHOT_BASENAME); // relative path
assert(result != 0);

SDL_RenderPresent(renderer);

printf("you should see an image.\n");

return result;
}
42 changes: 42 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2992,6 +2992,48 @@ def test_sdl2_image_formats(self):
'--use-port=sdl2', '--use-port=sdl2_image:formats=jpg',
])

@requires_graphics_hardware
@also_with_wasm2js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@also_with_wasm2js

Perhaps we don't need wasm2js here, as there isn't something very specific to test in SDL3+wasm2js (vs SDL2+wasm2js).

def test_sdl3_image(self):
# Same test as for SDL2, but with SDL3.
shutil.copy(test_file('screenshot.jpg'), '.')

for dest, dirname, basename in [('screenshot.jpg', '/', 'screenshot.jpg'),
('screenshot.jpg@/assets/screenshot.jpg', '/assets', 'screenshot.jpg')]:
self.btest_exit('test_sdl3_image.c', 600, cflags=[
'-O2',
'--preload-file', dest,
'-DSCREENSHOT_DIRNAME="' + dirname + '"',
'-DSCREENSHOT_BASENAME="' + basename + '"',
'-sUSE_SDL=3', '-sUSE_SDL_IMAGE=3', '--use-preload-plugins',
])

@requires_graphics_hardware
def test_sdl3_image_jpeg(self):
shutil.copy(test_file('screenshot.jpg'), 'screenshot.jpeg')
self.btest_exit('test_sdl3_image.c', 600, cflags=[
'--preload-file', 'screenshot.jpeg',
'-DSCREENSHOT_DIRNAME="/"', '-DSCREENSHOT_BASENAME="screenshot.jpeg"',
'-sUSE_SDL=3', '-sUSE_SDL_IMAGE=3', '--use-preload-plugins',
])

@also_with_wasmfs
@requires_graphics_hardware
@with_all_sjlj
Comment on lines +3020 to +3022
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@also_with_wasmfs
@requires_graphics_hardware
@with_all_sjlj
@requires_graphics_hardware

As above, removing some testing I think is less crucial.

def test_sdl3_image_formats(self):
shutil.copy(test_file('screenshot.png'), '.')
shutil.copy(test_file('screenshot.jpg'), '.')
self.btest_exit('test_sdl3_image.c', 512, cflags=[
'--preload-file', 'screenshot.png',
'-DSCREENSHOT_DIRNAME="/"', '-DSCREENSHOT_BASENAME="screenshot.png"', '-DNO_PRELOADED',
'-sUSE_SDL=3', '-sUSE_SDL_IMAGE=3', '-sSDL3_IMAGE_FORMATS=png',
])
self.btest_exit('test_sdl3_image.c', 600, cflags=[
'--preload-file', 'screenshot.jpg',
'-DSCREENSHOT_DIRNAME="/"', '-DSCREENSHOT_BASENAME="screenshot.jpg"', '-DBITSPERPIXEL=24', '-DNO_PRELOADED',
'--use-port=sdl3', '--use-port=sdl3_image:formats=jpg',
])

def test_sdl2_key(self):
self.btest_exit('test_sdl2_key.c', 37182145, cflags=['-sUSE_SDL=2', '--pre-js', test_file('browser/fake_events.js')])

Expand Down
136 changes: 136 additions & 0 deletions tools/ports/sdl3_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Copyright 2025 The Emscripten Authors. All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License. Both these licenses can be
# found in the LICENSE file.

import os

from typing import Dict, Set

TAG = 'release-3.2.4'
HASH = '102b8ea30506a1aa0a23196d807e5cdf1ff9efcbcf9db2518a3fffef82f94bda96891129c24a8008a4ec374c9dfc39d5321cf7e573f3e3b10e85b2cfbbc1ad9b'

deps = ['sdl3']
variants = {
'sdl3_image-jpg': {'SDL3_IMAGE_FORMATS': ["jpg"]},
'sdl3_image-png': {'SDL3_IMAGE_FORMATS': ["png"]},
'sdl3_image-jpg-mt': {'SDL3_IMAGE_FORMATS': ["jpg"], 'PTHREADS': 1},
'sdl3_image-png-mt': {'SDL3_IMAGE_FORMATS': ["png"], 'PTHREADS': 1},
}

OPTIONS = {
'formats': 'A comma separated list of formats (ex: --use-port=sdl3_image:formats=png,jpg)',
}

SUPPORTED_FORMATS = {'avif', 'bmp', 'gif', 'jpg', 'jxl', 'lbm', 'pcx', 'png',
'pnm', 'qoi', 'svg', 'tga', 'tif', 'webp', 'xcf', 'xpm', 'xv'}

# user options (from --use-port)
opts: Dict[str, Set] = {
'formats': set(),
}


def needed(settings):
return settings.USE_SDL_IMAGE == 3


def get_formats(settings):
return opts['formats'].union(settings.SDL3_IMAGE_FORMATS)


def get_lib_name(settings):
formats = '-'.join(sorted(get_formats(settings)))

libname = 'libSDL3_image'
if formats != '':
libname += '-' + formats
if settings.PTHREADS:
libname += '-mt'
if settings.SUPPORT_LONGJMP == 'wasm':
libname += '-wasm-sjlj'
return libname + '.a'


def get(ports, settings, shared):
ports.fetch_project('sdl3_image', f'https://github.com/libsdl-org/SDL_image/archive/{TAG}.zip', sha512hash=HASH)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing to the sdl2_image port, I see this in the diff:

-  ports.fetch_project('sdl2_image', f'https://github.com/libsdl-org/SDL_image/archive/refs/tags/{TAG}.zip', sha512hash=HASH)
+  ports.fetch_project('sdl3_image', f'https://github.com/libsdl-org/SDL_image/archive/{TAG}.zip', sha512hash=HASH)

Is /ref/tags/ not needed?

libname = get_lib_name(settings)

def create(final):
src_root = ports.get_dir('sdl3_image', 'SDL_image-' + TAG)
ports.install_header_dir(os.path.join(src_root, 'include'), target='.')
srcs = [
"src/IMG.c",
"src/IMG_ImageIO.m",
"src/IMG_WIC.c",
"src/IMG_avif.c",
"src/IMG_bmp.c",
"src/IMG_gif.c",
"src/IMG_jpg.c",
"src/IMG_jxl.c",
"src/IMG_lbm.c",
"src/IMG_pcx.c",
"src/IMG_png.c",
"src/IMG_pnm.c",
"src/IMG_qoi.c",
"src/IMG_stb.c",
"src/IMG_svg.c",
"src/IMG_tga.c",
"src/IMG_tif.c",
"src/IMG_webp.c",
"src/IMG_xcf.c",
"src/IMG_xpm.c",
"src/IMG_xv.c",
"src/IMG_xxx.c",
]

flags = ['-sUSE_SDL=3', '-Wno-format-security']

formats = get_formats(settings)

flags.extend(f'-DLOAD_{fmt.upper()}' for fmt in formats)

if 'png' in formats:
flags += ['-sUSE_LIBPNG']

if 'jpg' in formats:
flags += ['-sUSE_LIBJPEG']

if settings.PTHREADS:
flags += ['-pthread']

if settings.SUPPORT_LONGJMP == 'wasm':
flags.append('-sSUPPORT_LONGJMP=wasm')

ports.build_port(src_root, final, 'sdl3_image', flags=flags, srcs=srcs)

return [shared.cache.get_lib(libname, create, what='port')]


def clear(ports, settings, shared):
shared.cache.erase_lib(get_lib_name(settings))


def process_dependencies(settings):
settings.USE_SDL = 3
formats = get_formats(settings)
if 'png' in formats:
deps.append('libpng')
settings.USE_LIBPNG = 1
if 'jpg' in formats:
deps.append('libjpeg')
settings.USE_LIBJPEG = 1


def handle_options(options, error_handler):
formats = options['formats'].split(',')
for format in formats:
format = format.lower().strip()
if format not in SUPPORTED_FORMATS:
error_handler(f'{format} is not a supported format')
else:
opts['formats'].add(format)


def show():
return 'sdl3_image (-sUSE_SDL_IMAGE=3 or --use-port=sdl3_image; zlib license)'
1 change: 1 addition & 0 deletions tools/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
'USE_FREETYPE',
'SDL2_MIXER_FORMATS',
'SDL2_IMAGE_FORMATS',
'SDL3_IMAGE_FORMATS',
'USE_SQLITE3',
}

Expand Down