diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-03-15 16:49:37 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-03-15 16:49:37 -0700 |
commit | 470919bea98605f382f82a0ff76f53cb0faa4f90 (patch) | |
tree | 1acc8b38a23bfbfa6184a7004ebecf83b47c1346 | |
parent | 36afa3f0eb2b3d63bcab798bc8ee1caceae08e2b (diff) |
almost working SDL_image test, just need async decoding
-rw-r--r-- | src/library_browser.js | 9 | ||||
-rw-r--r-- | src/library_sdl.js | 7 | ||||
-rw-r--r-- | src/shell.html | 5 | ||||
-rw-r--r-- | system/include/SDL/SDL_image.h | 138 | ||||
-rwxr-xr-x | tests/runner.py | 13 |
5 files changed, 162 insertions, 10 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index 9c3057b8..b733bfbd 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -31,11 +31,14 @@ mergeInto(LibraryManager.library, { } return ret; } - var img = new Image(); + var image = new Image(); + image.src = 'data:image/' + format + ';base64,' + encodeBase64(pixels); + assert(image.complete, 'Image could not be decoded'); // page reload might fix it, decoding is async... var canvas = document.createElement('canvas'); - img.src = 'data:image/' + format + ';base64,' + encodeBase64(pixels); + canvas.width = image.width; + canvas.height = image.height; var ctx = canvas.getContext('2d'); - ctx.drawImage(img, 0, 0); + ctx.drawImage(image, 0, 0); var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); return imageData; }, diff --git a/src/library_sdl.js b/src/library_sdl.js index 7b413c13..e741faeb 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -479,7 +479,12 @@ mergeInto(LibraryManager.library, { IMG_Load: function(filename) { filename = Pointer_stringify(filename); var format = filename.split('.').slice(-1)[0]; - var data = readBinary(filename); + var file = FS.analyzePath(filename); + if (!file || !file.object) { + console.log('Cannot find file: ' + filename); + return 0; + } + var data = file.object.contents; var raw = Browser.decodeImage(data, format); var surf = SDL.makeSurface(raw.width, raw.height, 0); // XXX Extremely inefficient! diff --git a/src/shell.html b/src/shell.html index 2f34ace3..11970241 100644 --- a/src/shell.html +++ b/src/shell.html @@ -9,11 +9,6 @@ <div id='output'></div> <hr> <script type='text/javascript'> - /** - * TODO: Encapsulate this part in a reusable token such as - * EMSCRIPTEN_ENVIRONMENT so that we can share code - * between the default shell and custom ones. - */ // connect to canvas var Module = { print: (function() { diff --git a/system/include/SDL/SDL_image.h b/system/include/SDL/SDL_image.h new file mode 100644 index 00000000..3f07d74a --- /dev/null +++ b/system/include/SDL/SDL_image.h @@ -0,0 +1,138 @@ +/* + SDL_image: An example image loading library for use with SDL + Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org> + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +/* A simple library to load images of various formats as SDL surfaces */ + +#ifndef _SDL_IMAGE_H +#define _SDL_IMAGE_H + +#include "SDL.h" +#include "SDL_version.h" +#include "begin_code.h" + +/* Set up for C function definitions, even when using C++ */ +#ifdef __cplusplus +extern "C" { +#endif + +/* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL +*/ +#define SDL_IMAGE_MAJOR_VERSION 1 +#define SDL_IMAGE_MINOR_VERSION 2 +#define SDL_IMAGE_PATCHLEVEL 12 + +/* This macro can be used to fill a version structure with the compile-time + * version of the SDL_image library. + */ +#define SDL_IMAGE_VERSION(X) \ +{ \ + (X)->major = SDL_IMAGE_MAJOR_VERSION; \ + (X)->minor = SDL_IMAGE_MINOR_VERSION; \ + (X)->patch = SDL_IMAGE_PATCHLEVEL; \ +} + +/* This function gets the version of the dynamically linked SDL_image library. + it should NOT be used to fill a version structure, instead you should + use the SDL_IMAGE_VERSION() macro. + */ +extern DECLSPEC const SDL_version * SDLCALL IMG_Linked_Version(void); + +typedef enum +{ + IMG_INIT_JPG = 0x00000001, + IMG_INIT_PNG = 0x00000002, + IMG_INIT_TIF = 0x00000004, + IMG_INIT_WEBP = 0x00000008 +} IMG_InitFlags; + +/* Loads dynamic libraries and prepares them for use. Flags should be + one or more flags from IMG_InitFlags OR'd together. + It returns the flags successfully initialized, or 0 on failure. + */ +extern DECLSPEC int SDLCALL IMG_Init(int flags); + +/* Unloads libraries loaded with IMG_Init */ +extern DECLSPEC void SDLCALL IMG_Quit(void); + +/* Load an image from an SDL data source. + The 'type' may be one of: "BMP", "GIF", "PNG", etc. + + If the image format supports a transparent pixel, SDL will set the + colorkey for the surface. You can enable RLE acceleration on the + surface afterwards by calling: + SDL_SetColorKey(image, SDL_RLEACCEL, image->format->colorkey); + */ +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type); +/* Convenience functions */ +extern DECLSPEC SDL_Surface * SDLCALL IMG_Load(const char *file); +extern DECLSPEC SDL_Surface * SDLCALL IMG_Load_RW(SDL_RWops *src, int freesrc); + +/* Invert the alpha of a surface for use with OpenGL + This function is now a no-op, and only provided for backwards compatibility. +*/ +extern DECLSPEC int SDLCALL IMG_InvertAlpha(int on); + +/* Functions to detect a file type, given a seekable source */ +extern DECLSPEC int SDLCALL IMG_isICO(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isCUR(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isBMP(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isGIF(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isJPG(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isLBM(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isPCX(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isPNG(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isPNM(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isTIF(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isXCF(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isXPM(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isXV(SDL_RWops *src); +extern DECLSPEC int SDLCALL IMG_isWEBP(SDL_RWops *src); + +/* Individual loading functions */ +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadICO_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadCUR_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadBMP_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadGIF_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadJPG_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadLBM_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPCX_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNG_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNM_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTGA_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTIF_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXCF_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXPM_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXV_RW(SDL_RWops *src); +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadWEBP_RW(SDL_RWops *src); + +extern DECLSPEC SDL_Surface * SDLCALL IMG_ReadXPMFromArray(char **xpm); + +/* We'll use SDL for reporting errors */ +#define IMG_SetError SDL_SetError +#define IMG_GetError SDL_GetError + +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +} +#endif +#include "close_code.h" + +#endif /* _SDL_IMAGE_H */ diff --git a/tests/runner.py b/tests/runner.py index 4121bb6c..0eca1598 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -6279,7 +6279,18 @@ f.close() self.run_browser('page.html', 'You should see two cool numbers', '/report_result?1') def test_emcc_sdl_image(self): - pass # load an image file, say jpg, get pixel data + # load an image file, get pixel data + + return self.skip('decoding is async, we need sync...') + + shutil.copyfile(path_from_root('tests', 'screenshot.jpg'), os.path.join(self.get_dir(), 'screenshot.jpg')) + shutil.copyfile(path_from_root('tests', 'sdl_image.c'), os.path.join(self.get_dir(), 'sdl_image.c')) + + Popen([EMCC, os.path.join(self.get_dir(), 'sdl_image.c'), '--preload-file', 'screenshot.jpg', '-o', 'page.html']).communicate() + self.run_browser('page.html', 'You should see |load me right before|.', '/report_result?1') + + def test_emcc_compression(self): + pass # test compression of both the compiled code itself in a side file, and of data files def test_emcc_worker(self): # Test running in a web worker |