diff options
-rw-r--r-- | src/library_browser.js | 33 | ||||
-rw-r--r-- | system/include/emscripten/emscripten.h | 20 | ||||
-rw-r--r-- | tests/sdl_image.c | 10 |
3 files changed, 63 insertions, 0 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index 4ef7c577..57ca5a24 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -1175,6 +1175,39 @@ mergeInto(LibraryManager.library, { var info = Browser.workers[id]; if (!info) return -1; return info.awaited; + }, + + emscripten_get_preloaded_image_data: function(path, w, h) { + if (typeof path === "number") { + path = Pointer_stringify(path); + } + + path = PATH.resolve(path); + + var canvas = Module["preloadedImages"][path]; + if (canvas) { + var ctx = canvas.getContext("2d"); + var image = ctx.getImageData(0, 0, canvas.width, canvas.height); + var buf = _malloc(canvas.width * canvas.height * 4); + + HEAPU8.set(image.data, buf); + + {{{ makeSetValue('w', '0', 'canvas.width', 'i32') }}}; + {{{ makeSetValue('h', '0', 'canvas.height', 'i32') }}}; + return buf; + } + + return 0; + }, + + emscripten_get_preloaded_image_data_from_FILE__deps: ['emscripten_get_preloaded_image_data'], + emscripten_get_preloaded_image_data_from_FILE: function(file, w, h) { + var stream = FS.getStreamFromPtr(file); + if (stream) { + return _emscripten_get_preloaded_image_data(stream.path, w, h); + } + + return 0; } }); diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h index 6e1c00d5..b495c0f5 100644 --- a/system/include/emscripten/emscripten.h +++ b/system/include/emscripten/emscripten.h @@ -14,6 +14,8 @@ extern "C" { #endif +#include <stdio.h> + #if !__EMSCRIPTEN__ #include <SDL/SDL.h> /* for SDL_Delay in async_call */ #endif @@ -515,6 +517,24 @@ int emscripten_get_compiler_setting(const char *name); */ void emscripten_debugger(); +/* + * Get preloaded image data and the size of the image. + * + * Returns pointer to loaded image or NULL. + * width/height of image are written to w/h if data is valid. + * Pointer should be free()'d + */ +char *emscripten_get_preloaded_image_data(const char *path, int *w, int *h); + +/* + * Get preloaded image data from a c FILE *. + * + * Returns pointer to loaded image or NULL. + * width/height of image are written to w/h if data is valid. + * Pointer should be free()'d + */ +char *emscripten_get_preloaded_image_data_from_FILE(FILE *file, int *w, int *h); + /* ===================================== */ /* Internal APIs. Be careful with these. */ diff --git a/tests/sdl_image.c b/tests/sdl_image.c index 523f8903..9639ea37 100644 --- a/tests/sdl_image.c +++ b/tests/sdl_image.c @@ -4,6 +4,7 @@ #include <assert.h> #include <emscripten.h> #include <unistd.h> +#include <stdlib.h> int testImage(SDL_Surface* screen, const char* fileName) { SDL_Surface *image = IMG_Load(fileName); @@ -18,7 +19,16 @@ int testImage(SDL_Surface* screen, const char* fileName) { int result = image->w; SDL_BlitSurface (image, NULL, screen, NULL); + + int w, h; + char *data = emscripten_get_preloaded_image_data(fileName, &w, &h); + + assert(data); + assert(w == image->w); + assert(h == image->h); + SDL_FreeSurface (image); + free(data); return result; } |