diff options
-rwxr-xr-x | emcc | 4 | ||||
-rw-r--r-- | src/library_sdl.js | 168 | ||||
-rw-r--r-- | src/settings.js | 6 | ||||
-rwxr-xr-x | tests/runner.py | 10 | ||||
-rw-r--r-- | tests/sdl_stb_image.c | 49 | ||||
-rw-r--r-- | tests/sdl_stb_image_data.c | 55 | ||||
-rw-r--r-- | third_party/stb_image.c | 4673 |
7 files changed, 4918 insertions, 47 deletions
@@ -1087,6 +1087,10 @@ try: if shared.Settings.DLOPEN_SUPPORT: shared.Settings.LINKABLE = 1 + if shared.Settings.STB_IMAGE and final_suffix in JS_CONTAINING_SUFFIXES: + input_files.append(shared.path_from_root('third_party', 'stb_image.c')) + shared.Settings.EXPORTED_FUNCTIONS += ['_stbi_load', '_stbi_load_from_memory', '_stbi_image_free'] + ## Compile source code to bitcode logging.debug('compiling to bitcode') diff --git a/src/library_sdl.js b/src/library_sdl.js index 4477e457..80c7ac07 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -1238,61 +1238,133 @@ var LibrarySDL = { return flags; // We support JPG, PNG, TIF because browsers do }, - IMG_Load_RW__deps: ['SDL_LockSurface'], - IMG_Load_RW: function(rwopsID, freesrc) { - var rwops = SDL.rwops[rwopsID]; + IMG_Load_RW__deps: ['SDL_LockSurface', 'SDL_FreeRW'], + IMG_Load_RW: function(rwopsID, freeSrc) { + try { + // stb_image integration support + var cleanup = function() { + if (rwops && freeSrc) _SDL_FreeRW(rwopsID); + }; + function addCleanup(func) { + var old = cleanup; + cleanup = function() { + old(); + func(); + } + } + function callStbImage(func, params) { + var x = Module['_malloc']({{{ QUANTUM_SIZE }}}); + var y = Module['_malloc']({{{ QUANTUM_SIZE }}}); + var comp = Module['_malloc']({{{ QUANTUM_SIZE }}}); + addCleanup(function() { + Module['_free'](x); + Module['_free'](y); + Module['_free'](comp); + if (data) Module['_stbi_image_free'](data); + }); + var data = Module['_' + func].apply(null, params.concat([x, y, comp, 0])); + if (!data) return null; + return { + rawData: true, + data: data, + width: {{{ makeGetValue('x', 0, 'i32') }}}, + height: {{{ makeGetValue('y', 0, 'i32') }}}, + size: {{{ makeGetValue('x', 0, 'i32') }}} * {{{ makeGetValue('y', 0, 'i32') }}} * {{{ makeGetValue('comp', 0, 'i32') }}}, + bpp: {{{ makeGetValue('comp', 0, 'i32') }}} + }; + } - if (rwops === undefined) { - return 0; - } + var rwops = SDL.rwops[rwopsID]; + if (rwops === undefined) { + return 0; + } - var filename = rwops.filename; - - if (filename === undefined) { - Runtime.warnOnce('Only file names that have been preloaded are supported for IMG_Load_RW.'); - // TODO. Support loading image data from embedded files, similarly to Mix_LoadWAV_RW - // TODO. Support loading image data from byte arrays, similarly to Mix_LoadWAV_RW - return 0; - } - - filename = FS.standardizePath(filename); - if (filename[0] == '/') { - // Convert the path to relative - filename = filename.substr(1); - } - var raw = Module["preloadedImages"][filename]; - if (!raw) { - if (raw === null) Module.printErr('Trying to reuse preloaded image, but freePreloadedMediaOnUse is set!'); - Runtime.warnOnce('Cannot find preloaded image ' + filename); - return 0; - } - if (Module['freePreloadedMediaOnUse']) { - Module["preloadedImages"][filename] = null; - } - var surf = SDL.makeSurface(raw.width, raw.height, 0, false, 'load:' + filename); - var surfData = SDL.surfaces[surf]; - surfData.ctx.globalCompositeOperation = "copy"; - surfData.ctx.drawImage(raw, 0, 0, raw.width, raw.height, 0, 0, raw.width, raw.height); - surfData.ctx.globalCompositeOperation = "source-over"; - // XXX SDL does not specify that loaded images must have available pixel data, in fact - // there are cases where you just want to blit them, so you just need the hardware - // accelerated version. However, code everywhere seems to assume that the pixels - // are in fact available, so we retrieve it here. This does add overhead though. - _SDL_LockSurface(surf); - surfData.locked--; // The surface is not actually locked in this hack - if (SDL.GL) { - // After getting the pixel data, we can free the canvas and context if we do not need to do 2D canvas blitting - surfData.canvas = surfData.ctx = null; + var filename = rwops.filename; + if (filename === undefined) { +#if STB_IMAGE + var raw = callStbImage('stbi_load_from_memory', [rwops.bytes, rwops.count]); + if (!raw) return 0; +#else + Runtime.warnOnce('Only file names that have been preloaded are supported for IMG_Load_RW. Consider using STB_IMAGE=1 if you want synchronous image decoding (see settings.js)'); + return 0; +#endif + } + + if (!raw) { + filename = FS.standardizePath(filename); + if (filename[0] == '/') { + // Convert the path to relative + filename = filename.substr(1); + } + var raw = Module["preloadedImages"][filename]; + if (!raw) { + if (raw === null) Module.printErr('Trying to reuse preloaded image, but freePreloadedMediaOnUse is set!'); +#if STB_IMAGE + var name = Module['_malloc'](filename.length+1); + writeStringToMemory(filename, name); + addCleanup(function() { + Module['_free'](name); + }); + var raw = callStbImage('stbi_load', [name]); + if (!raw) return 0; +#else + Runtime.warnOnce('Cannot find preloaded image ' + filename); + Runtime.warnOnce('Cannot find preloaded image ' + filename + '. Consider using STB_IMAGE=1 if you want synchronous image decoding (see settings.js)'); + return 0; +#endif + } else if (Module['freePreloadedMediaOnUse']) { + Module["preloadedImages"][filename] = null; + } + } + + var surf = SDL.makeSurface(raw.width, raw.height, 0, false, 'load:' + filename); + var surfData = SDL.surfaces[surf]; + surfData.ctx.globalCompositeOperation = "copy"; + if (!raw.rawData) { + surfData.ctx.drawImage(raw, 0, 0, raw.width, raw.height, 0, 0, raw.width, raw.height); + } else { + var imageData = surfData.ctx.getImageData(0, 0, surfData.width, surfData.height); + if (raw.bpp == 4) { + imageData.data.set({{{ makeHEAPView('U8', 'raw.data', 'raw.data+raw.size') }}}); + } else if (raw.bpp == 3) { + var pixels = raw.size/3; + var data = imageData.data; + var sourcePtr = raw.data; + var destPtr = 0; + for (var i = 0; i < pixels; i++) { + data[destPtr++] = {{{ makeGetValue('sourcePtr++', 0, 'i8', null, 1) }}}; + data[destPtr++] = {{{ makeGetValue('sourcePtr++', 0, 'i8', null, 1) }}}; + data[destPtr++] = {{{ makeGetValue('sourcePtr++', 0, 'i8', null, 1) }}}; + data[destPtr++] = 255; + } + } else { + Module.printErr('cannot handle bpp ' + raw.bpp); + return 0; + } + surfData.ctx.putImageData(imageData, 0, 0); + } + surfData.ctx.globalCompositeOperation = "source-over"; + // XXX SDL does not specify that loaded images must have available pixel data, in fact + // there are cases where you just want to blit them, so you just need the hardware + // accelerated version. However, code everywhere seems to assume that the pixels + // are in fact available, so we retrieve it here. This does add overhead though. + _SDL_LockSurface(surf); + surfData.locked--; // The surface is not actually locked in this hack + if (SDL.GL) { + // After getting the pixel data, we can free the canvas and context if we do not need to do 2D canvas blitting + surfData.canvas = surfData.ctx = null; + } + return surf; + } finally { + cleanup(); } - return surf; }, SDL_LoadBMP: 'IMG_Load', SDL_LoadBMP_RW: 'IMG_Load_RW', - IMG_Load__deps: ['IMG_Load_RW', 'SDL_RWFromFile', 'SDL_FreeRW'], + IMG_Load__deps: ['IMG_Load_RW', 'SDL_RWFromFile'], IMG_Load: function(filename){ var rwops = _SDL_RWFromFile(filename); - var result = _IMG_Load_RW(rwops); - _SDL_FreeRW(rwops); + var result = _IMG_Load_RW(rwops, 1); return result; }, @@ -2055,11 +2127,13 @@ var LibrarySDL = { // Misc SDL_InitSubSystem: function(flags) { return 0 }, + SDL_RWFromConstMem: function(mem, size) { var id = SDL.rwops.length; // TODO: recycle ids when they are null SDL.rwops.push({ bytes: mem, count: size }); return id; }, + SDL_RWFromMem: 'SDL_RWFromConstMem', SDL_RWFromFile: function(_name, mode) { var id = SDL.rwops.length; // TODO: recycle ids when they are null diff --git a/src/settings.js b/src/settings.js index ed2afdb5..87ab820d 100644 --- a/src/settings.js +++ b/src/settings.js @@ -194,6 +194,12 @@ var FORCE_GL_EMULATION = 0; // Forces inclusion of full GL emulation code. var DISABLE_GL_EMULATION = 0; // Disable inclusion of full GL emulation code. Useful when you don't want emulation // but do need INCLUDE_FULL_LIBRARY or MAIN_MODULE. +var STB_IMAGE = 0; // Enables building of stb-image, a tiny public-domain library for decoding images, allowing + // decoding of images without using the browser's built-in decoders. The benefit is that this + // can be done synchronously, however, it will not be as fast as the browser itself. + // When enabled, stb-image will be used automatically from IMG_Load and IMG_Load_RW. You + // can also call the stbi_* functions directly yourself. + var DISABLE_EXCEPTION_CATCHING = 0; // Disables generating code to actually catch exceptions. If the code you // are compiling does not actually rely on catching exceptions (but the // compiler generates code for it, maybe because of stdlibc++ stuff), diff --git a/tests/runner.py b/tests/runner.py index 663f7d24..7bb2cd08 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -12983,6 +12983,16 @@ Press any key to continue.''' shutil.copyfile(path_from_root('tests', 'screenshot.jpg'), os.path.join(self.get_dir(), 'screenshot.not')) self.btest('sdl_image_prepare_data.c', reference='screenshot.jpg', args=['--preload-file', 'screenshot.not']) + def test_sdl_stb_image(self): + # load an image file, get pixel data. + shutil.copyfile(path_from_root('tests', 'screenshot.jpg'), os.path.join(self.get_dir(), 'screenshot.not')) + self.btest('sdl_stb_image.c', reference='screenshot.jpg', args=['-s', 'STB_IMAGE=1', '--preload-file', 'screenshot.not']) + + def test_sdl_stb_image_data(self): + # load an image file, get pixel data. + shutil.copyfile(path_from_root('tests', 'screenshot.jpg'), os.path.join(self.get_dir(), 'screenshot.not')) + self.btest('sdl_stb_image_data.c', reference='screenshot.jpg', args=['-s', 'STB_IMAGE=1', '--preload-file', 'screenshot.not']) + def test_sdl_canvas(self): open(os.path.join(self.get_dir(), 'sdl_canvas.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_canvas.c')).read())) diff --git a/tests/sdl_stb_image.c b/tests/sdl_stb_image.c new file mode 100644 index 00000000..6d39cb8e --- /dev/null +++ b/tests/sdl_stb_image.c @@ -0,0 +1,49 @@ +#include <stdio.h> +#include <SDL/SDL.h> +#include <SDL/SDL_image.h> +#include <assert.h> +#include <emscripten.h> + +SDL_Surface* screen; + +int testImage(const char* fileName) { + SDL_Surface *image = IMG_Load(fileName); + if (!image) + { + printf("IMG_Load: %s\n", IMG_GetError()); + return 0; + } + assert(image->format->BitsPerPixel == 32); + assert(image->format->BytesPerPixel == 4); + assert(image->pitch == 4*image->w); + int result = image->w; + + SDL_BlitSurface (image, NULL, screen, NULL); + SDL_FreeSurface (image); + + return result; +} + +void ready() { + printf("ready!\n"); + + testImage("screenshot.jpg"); // relative path + + SDL_Flip(screen); +} + +int main() { + SDL_Init(SDL_INIT_VIDEO); + screen = SDL_SetVideoMode(600, 450, 32, SDL_SWSURFACE); + + printf("rename..\n"); + + rename("screenshot.not", "screenshot.jpg"); + + printf("decode..\n"); + + ready(); + + return 0; +} + diff --git a/tests/sdl_stb_image_data.c b/tests/sdl_stb_image_data.c new file mode 100644 index 00000000..d702b4cc --- /dev/null +++ b/tests/sdl_stb_image_data.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <SDL/SDL.h> +#include <SDL/SDL_image.h> +#include <assert.h> +#include <emscripten.h> + +#define SIZE 203164 +SDL_Surface* screen; +char *buffer; + +int testImage() { + SDL_Surface *image = IMG_Load_RW(SDL_RWFromMem(buffer, SIZE), 1); + if (!image) + { + printf("IMG_Load: %s\n", IMG_GetError()); + return 0; + } + + printf("load succeeded\n"); + + assert(image->format->BitsPerPixel == 32); + assert(image->format->BytesPerPixel == 4); + assert(image->pitch == 4*image->w); + int result = image->w; + + SDL_BlitSurface (image, NULL, screen, NULL); + SDL_FreeSurface (image); + + return result; +} + +void ready() { + testImage(); + + SDL_Flip(screen); +} + +int main() { + SDL_Init(SDL_INIT_VIDEO); + screen = SDL_SetVideoMode(600, 450, 32, SDL_SWSURFACE); + + printf("prepare..\n"); + + FILE *f = fopen("screenshot.not", "rb"); + buffer = malloc(SIZE); + fread(buffer, SIZE, 1, f); + fclose(f); + + ready(); + + return 0; +} + diff --git a/third_party/stb_image.c b/third_party/stb_image.c new file mode 100644 index 00000000..098b91ae --- /dev/null +++ b/third_party/stb_image.c @@ -0,0 +1,4673 @@ +/* stbi-1.33 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c
+ when you control the images you're loading
+ no warranty implied; use at your own risk
+
+ QUICK NOTES:
+ Primarily of interest to game developers and other people who can
+ avoid problematic images and only need the trivial interface
+
+ JPEG baseline (no JPEG progressive)
+ PNG 8-bit-per-channel only
+
+ TGA (not sure what subset, if a subset)
+ BMP non-1bpp, non-RLE
+ PSD (composited view only, no extra channels)
+
+ GIF (*comp always reports as 4-channel)
+ HDR (radiance rgbE format)
+ PIC (Softimage PIC)
+
+ - decode from memory or through FILE (define STBI_NO_STDIO to remove code)
+ - decode from arbitrary I/O callbacks
+ - overridable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
+
+ Latest revisions:
+ 1.33 (2011-07-14) minor fixes suggested by Dave Moore
+ 1.32 (2011-07-13) info support for all filetypes (SpartanJ)
+ 1.31 (2011-06-19) a few more leak fixes, bug in PNG handling (SpartanJ)
+ 1.30 (2011-06-11) added ability to load files via io callbacks (Ben Wenger)
+ 1.29 (2010-08-16) various warning fixes from Aurelien Pocheville
+ 1.28 (2010-08-01) fix bug in GIF palette transparency (SpartanJ)
+ 1.27 (2010-08-01) cast-to-uint8 to fix warnings (Laurent Gomila)
+ allow trailing 0s at end of image data (Laurent Gomila)
+ 1.26 (2010-07-24) fix bug in file buffering for PNG reported by SpartanJ
+
+ See end of file for full revision history.
+
+ TODO:
+ stbi_info support for BMP,PSD,HDR,PIC
+
+
+ ============================ Contributors =========================
+
+ Image formats Optimizations & bugfixes
+ Sean Barrett (jpeg, png, bmp) Fabian "ryg" Giesen
+ Nicolas Schulz (hdr, psd)
+ Jonathan Dummer (tga) Bug fixes & warning fixes
+ Jean-Marc Lienher (gif) Marc LeBlanc
+ Tom Seddon (pic) Christpher Lloyd
+ Thatcher Ulrich (psd) Dave Moore
+ Won Chun
+ the Horde3D community
+ Extensions, features Janez Zemva
+ Jetro Lauha (stbi_info) Jonathan Blow
+ James "moose2000" Brown (iPhone PNG) Laurent Gomila
+ Ben "Disch" Wenger (io callbacks) Aruelien Pocheville
+ Martin "SpartanJ" Golini Ryamond Barbiero
+ David Woo
+
+
+ If your name should be here but isn't, let Sean know.
+
+*/
+
+#ifndef STBI_INCLUDE_STB_IMAGE_H
+#define STBI_INCLUDE_STB_IMAGE_H
+
+// To get a header file for this, either cut and paste the header,
+// or create stb_image.h, #define STBI_HEADER_FILE_ONLY, and
+// then include stb_image.c from it.
+
+//// begin header file ////////////////////////////////////////////////////
+//
+// Limitations:
+// - no jpeg progressive support
+// - non-HDR formats support 8-bit samples only (jpeg, png)
+// - no delayed line count (jpeg) -- IJG doesn't support either
+// - no 1-bit BMP
+// - GIF always returns *comp=4
+//
+// Basic usage (see HDR discussion below):
+// int x,y,n;
+// unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
+// // ... process data if not NULL ...
+// // ... x = width, y = height, n = # 8-bit components per pixel ...
+// // ... replace '0' with '1'..'4' to force that many components per pixel
+// // ... but 'n' will always be the number that it would have been if you said 0
+// stbi_image_free(data)
+//
+// Standard parameters:
+// int *x -- outputs image width in pixels
+// int *y -- outputs image height in pixels
+// int *comp -- outputs # of image components in image file
+// int req_comp -- if non-zero, # of image components requested in result
+//
+// The return value from an image loader is an 'unsigned char *' which points
+// to the pixel data. The pixel data consists of *y scanlines of *x pixels,
+// with each pixel consisting of N interleaved 8-bit components; the first
+// pixel pointed to is top-left-most in the image. There is no padding between
+// image scanlines or between pixels, regardless of format. The number of
+// components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
+// If req_comp is non-zero, *comp has the number of components that _would_
+// have been output otherwise. E.g. if you set req_comp to 4, you will always
+// get RGBA output, but you can check *comp to easily see if it's opaque.
+//
+// An output image with N components has the following components interleaved
+// in this order in each pixel:
+//
+// N=#comp components
+// 1 grey
+// 2 grey, alpha
+// 3 red, green, blue
+// 4 red, green, blue, alpha
+//
+// If image loading fails for any reason, the return value will be NULL,
+// and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
+// can be queried for an extremely brief, end-user unfriendly explanation
+// of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
+// compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
+// more user-friendly ones.
+//
+// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
+//
+// ===========================================================================
+//
+// iPhone PNG support:
+//
+// By default we convert iphone-formatted PNGs back to RGB; nominally they
+// would silently load as BGR, except the existing code should have just
+// failed on such iPhone PNGs. But you can disable this conversion by
+// by calling stbi_convert_iphone_png_to_rgb(0), in which case
+// you will always just get the native iphone "format" through.
+//
+// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
+// pixel to remove any premultiplied alpha *only* if the image file explicitly
+// says there's premultiplied data (currently only happens in iPhone images,
+// and only if iPhone convert-to-rgb processing is on).
+//
+// ===========================================================================
+//
+// HDR image support (disable by defining STBI_NO_HDR)
+//
+// stb_image now supports loading HDR images in general, and currently
+// the Radiance .HDR file format, although the support is provided
+// generically. You can still load any file through the existing interface;
+// if you attempt to load an HDR file, it will be automatically remapped to
+// LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
+// both of these constants can be reconfigured through this interface:
+//
+// stbi_hdr_to_ldr_gamma(2.2f);
+// stbi_hdr_to_ldr_scale(1.0f);
+//
+// (note, do not use _inverse_ constants; stbi_image will invert them
+// appropriately).
+//
+// Additionally, there is a new, parallel interface for loading files as
+// (linear) floats to preserve the full dynamic range:
+//
+// float *data = stbi_loadf(filename, &x, &y, &n, 0);
+//
+// If you load LDR images through this interface, those images will
+// be promoted to floating point values, run through the inverse of
+// constants corresponding to the above:
+//
+// stbi_ldr_to_hdr_scale(1.0f);
+// stbi_ldr_to_hdr_gamma(2.2f);
+//
+// Finally, given a filename (or an open file or memory block--see header
+// file for details) containing image data, you can query for the "most
+// appropriate" interface to use (that is, whether the image is HDR or
+// not), using:
+//
+// stbi_is_hdr(char *filename);
+//
+// ===========================================================================
+//
+// I/O callbacks
+//
+// I/O callbacks allow you to read from arbitrary sources, like packaged
+// files or some other source. Data read from callbacks are processed
+// through a small internal buffer (currently 128 bytes) to try to reduce
+// overhead.
+//
+// The three functions you must define are "read" (reads some bytes of data),
+// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
+
+
+#ifndef STBI_NO_STDIO
+
+#if defined(_MSC_VER) && _MSC_VER >= 0x1400
+#define _CRT_SECURE_NO_WARNINGS // suppress bogus warnings about fopen()
+#endif
+
+#include <stdio.h>
+#endif
+
+#define STBI_VERSION 1
+
+enum
+{
+ STBI_default = 0, // only used for req_comp
+
+ STBI_grey = 1,
+ STBI_grey_alpha = 2,
+ STBI_rgb = 3,
+ STBI_rgb_alpha = 4
+};
+
+typedef unsigned char stbi_uc;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// PRIMARY API - works on images of any type
+//
+
+//
+// load image by filename, open file, or memory buffer
+//
+
+extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+
+#ifndef STBI_NO_STDIO
+extern stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+// for stbi_load_from_file, file pointer is left pointing immediately after image
+#endif
+
+typedef struct
+{
+ int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
+ void (*skip) (void *user,unsigned n); // skip the next 'n' bytes
+ int (*eof) (void *user); // returns nonzero if we are at end of file/data
+} stbi_io_callbacks;
+
+extern stbi_uc *stbi_load_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
+
+#ifndef STBI_NO_HDR
+ extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+
+ #ifndef STBI_NO_STDIO
+ extern float *stbi_loadf (char const *filename, int *x, int *y, int *comp, int req_comp);
+ extern float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+ #endif
+
+ extern float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp);
+
+ extern void stbi_hdr_to_ldr_gamma(float gamma);
+ extern void stbi_hdr_to_ldr_scale(float scale);
+
+ extern void stbi_ldr_to_hdr_gamma(float gamma);
+ extern void stbi_ldr_to_hdr_scale(float scale);
+#endif // STBI_NO_HDR
+
+// stbi_is_hdr is always defined
+extern int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
+extern int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
+#ifndef STBI_NO_STDIO
+extern int stbi_is_hdr (char const *filename);
+extern int stbi_is_hdr_from_file(FILE *f);
+#endif // STBI_NO_STDIO
+
+
+// get a VERY brief reason for failure
+// NOT THREADSAFE
+extern const char *stbi_failure_reason (void);
+
+// free the loaded image -- this is just free()
+extern void stbi_image_free (void *retval_from_stbi_load);
+
+// get image dimensions & components without fully decoding
+extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
+extern int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
+
+#ifndef STBI_NO_STDIO
+extern int stbi_info (char const *filename, int *x, int *y, int *comp);
+extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
+
+#endif
+
+
+
+// for image formats that explicitly notate that they have premultiplied alpha,
+// we just return the colors as stored in the file. set this flag to force
+// unpremultiplication. results are undefined if the unpremultiply overflow.
+extern void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
+
+// indicate whether we should process iphone images back to canonical format,
+// or just pass them through "as-is"
+extern void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
+
+
+// ZLIB client - used by PNG, available for other purposes
+
+extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
+extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
+extern int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
+
+extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
+extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
+
+
+// define faster low-level operations (typically SIMD support)
+#ifdef STBI_SIMD
+typedef void (*stbi_idct_8x8)(stbi_uc *out, int out_stride, short data[64], unsigned short *dequantize);
+// compute an integer IDCT on "input"
+// input[x] = data[x] * dequantize[x]
+// write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
+// CLAMP results to 0..255
+typedef void (*stbi_YCbCr_to_RGB_run)(stbi_uc *output, stbi_uc const *y, stbi_uc const *cb, stbi_uc const *cr, int count, int step);
+// compute a conversion from YCbCr to RGB
+// 'count' pixels
+// write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
+// y: Y input channel
+// cb: Cb input channel; scale/biased to be 0..255
+// cr: Cr input channel; scale/biased to be 0..255
+
+extern void stbi_install_idct(stbi_idct_8x8 func);
+extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
+#endif // STBI_SIMD
+
+
+#ifdef __cplusplus
+}
+#endif
+
+//
+//
+//// end header file /////////////////////////////////////////////////////
+#endif // STBI_INCLUDE_STB_IMAGE_H
+
+#ifndef STBI_HEADER_FILE_ONLY
+
+#ifndef STBI_NO_HDR
+#include <math.h> // ldexp
+#include <string.h> // strcmp, strtok
+#endif
+
+#ifndef STBI_NO_STDIO
+#include <stdio.h>
+#endif
+#include <stdlib.h>
+#include <memory.h>
+#include <assert.h>
+#include <stdarg.h>
+
+#ifndef _MSC_VER
+ #ifdef __cplusplus
+ #define stbi_inline inline
+ #else
+ #define stbi_inline
+ #endif
+#else
+ #define stbi_inline __forceinline
+#endif
+
+
+// implementation:
+typedef unsigned char uint8;
+typedef unsigned short uint16;
+typedef signed short int16;
+typedef unsigned int uint32;
+typedef signed int int32;
+typedef unsigned int uint;
+
+// should produce compiler error if size is wrong
+typedef unsigned char validate_uint32[sizeof(uint32)==4 ? 1 : -1];
+
+#if defined(STBI_NO_STDIO) && !defined(STBI_NO_WRITE)
+#define STBI_NO_WRITE
+#endif
+
+#define STBI_NOTUSED(v) (void)sizeof(v)
+
+#ifdef _MSC_VER
+#define STBI_HAS_LROTL
+#endif
+
+#ifdef STBI_HAS_LROTL
+ #define stbi_lrot(x,y) _lrotl(x,y)
+#else
+ #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
+#endif
+
+///////////////////////////////////////////////
+//
+// stbi struct and start_xxx functions
+
+// stbi structure is our basic context used by all images, so it
+// contains all the IO context, plus some basic image information
+typedef struct
+{
+ uint32 img_x, img_y;
+ int img_n, img_out_n;
+
+ stbi_io_callbacks io;
+ void *io_user_data;
+
+ int read_from_callbacks;
+ int buflen;
+ uint8 buffer_start[128];
+
+ uint8 *img_buffer, *img_buffer_end;
+ uint8 *img_buffer_original;
+} stbi;
+
+
+static void refill_buffer(stbi *s);
+
+// initialize a memory-decode context
+static void start_mem(stbi *s, uint8 const *buffer, int len)
+{
+ s->io.read = NULL;
+ s->read_from_callbacks = 0;
+ s->img_buffer = s->img_buffer_original = (uint8 *) buffer;
+ s->img_buffer_end = (uint8 *) buffer+len;
+}
+
+// initialize a callback-based context
+static void start_callbacks(stbi *s, stbi_io_callbacks *c, void *user)
+{
+ s->io = *c;
+ s->io_user_data = user;
+ s->buflen = sizeof(s->buffer_start);
+ s->read_from_callbacks = 1;
+ s->img_buffer_original = s->buffer_start;
+ refill_buffer(s);
+}
+
+#ifndef STBI_NO_STDIO
+
+static int stdio_read(void *user, char *data, int size)
+{
+ return (int) fread(data,1,size,(FILE*) user);
+}
+
+static void stdio_skip(void *user, unsigned n)
+{
+ fseek((FILE*) user, n, SEEK_CUR);
+}
+
+static int stdio_eof(void *user)
+{
+ return feof((FILE*) user);
+}
+
+static stbi_io_callbacks stbi_stdio_callbacks =
+{
+ stdio_read,
+ stdio_skip,
+ stdio_eof,
+};
+
+static void start_file(stbi *s, FILE *f)
+{
+ start_callbacks(s, &stbi_stdio_callbacks, (void *) f);
+}
+
+//static void stop_file(stbi *s) { }
+
+#endif // !STBI_NO_STDIO
+
+static void stbi_rewind(stbi *s)
+{
+ // conceptually rewind SHOULD rewind to the beginning of the stream,
+ // but we just rewind to the beginning of the initial buffer, because
+ // we only use it after doing 'test', which only ever looks at at most 92 bytes
+ s->img_buffer = s->img_buffer_original;
+}
+
+static int stbi_jpeg_test(stbi *s);
+static stbi_uc *stbi_jpeg_load(stbi *s, int *x, int *y, int *comp, int req_comp);
+static int stbi_jpeg_info(stbi *s, int *x, int *y, int *comp);
+static int stbi_png_test(stbi *s);
+static stbi_uc *stbi_png_load(stbi *s, int *x, int *y, int *comp, int req_comp);
+static int stbi_png_info(stbi *s, int *x, int *y, int *comp);
+static int stbi_bmp_test(stbi *s);
+static stbi_uc *stbi_bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp);
+static int stbi_tga_test(stbi *s);
+static stbi_uc *stbi_tga_load(stbi *s, int *x, int *y, int *comp, int req_comp);
+static int stbi_tga_info(stbi *s, int *x, int *y, int *comp);
+static int stbi_psd_test(stbi *s);
+static stbi_uc *stbi_psd_load(stbi *s, int *x, int *y, int *comp, int req_comp);
+static int stbi_hdr_test(stbi *s);
+static float *stbi_hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp);
+static int stbi_pic_test(stbi *s);
+static stbi_uc *stbi_pic_load(stbi *s, int *x, int *y, int *comp, int req_comp);
+static int stbi_gif_test(stbi *s);
+static stbi_uc *stbi_gif_load(stbi *s, int *x, int *y, int *comp, int req_comp);
+static int stbi_gif_info(stbi *s, int *x, int *y, int *comp);
+
+
+// this is not threadsafe
+static const char *failure_reason;
+
+const char *stbi_failure_reason(void)
+{
+ return failure_reason;
+}
+
+static int e(const char *str)
+{
+ failure_reason = str;
+ return 0;
+}
+
+// e - error
+// epf - error returning pointer to float
+// epuc - error returning pointer to unsigned char
+
+#ifdef STBI_NO_FAILURE_STRINGS
+ #define e(x,y) 0
+#elif defined(STBI_FAILURE_USERMSG)
+ #define e(x,y) e(y)
+#else
+ #define e(x,y) e(x)
+#endif
+
+#define epf(x,y) ((float *) (e(x,y)?NULL:NULL))
+#define epuc(x,y) ((unsigned char *) (e(x,y)?NULL:NULL))
+
+void stbi_image_free(void *retval_from_stbi_load)
+{
+ free(retval_from_stbi_load);
+}
+
+#ifndef STBI_NO_HDR
+static float *ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
+static stbi_uc *hdr_to_ldr(float *data, int x, int y, int comp);
+#endif
+
+static unsigned char *stbi_load_main(stbi *s, int *x, int *y, int *comp, int req_comp)
+{
+ if (stbi_jpeg_test(s)) return stbi_jpeg_load(s,x,y,comp,req_comp);
+ if (stbi_png_test(s)) return stbi_png_load(s,x,y,comp,req_comp);
+ if (stbi_bmp_test(s)) return stbi_bmp_load(s,x,y,comp,req_comp);
+ if (stbi_gif_test(s)) return stbi_gif_load(s,x,y,comp,req_comp);
+ if (stbi_psd_test(s)) return stbi_psd_load(s,x,y,comp,req_comp);
+ if (stbi_pic_test(s)) return stbi_pic_load(s,x,y,comp,req_comp);
+
+ #ifndef STBI_NO_HDR
+ if (stbi_hdr_test(s)) {
+ float *hdr = stbi_hdr_load(s, x,y,comp,req_comp);
+ return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
+ }
+ #endif
+
+ // test tga last because it's a crappy test!
+ if (stbi_tga_test(s))
+ return stbi_tga_load(s,x,y,comp,req_comp);
+ return epuc("unknown image type", "Image not of any known type, or corrupt");
+}
+
+#ifndef STBI_NO_STDIO
+unsigned char *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
+{
+ FILE *f = fopen(filename, "rb");
+ unsigned char *result;
+ if (!f) return epuc("can't fopen", "Unable to open file");
+ result = stbi_load_from_file(f,x,y,comp,req_comp);
+ fclose(f);
+ return result;
+}
+
+unsigned char *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
+{
+ stbi s;
+ start_file(&s,f);
+ return stbi_load_main(&s,x,y,comp,req_comp);
+}
+#endif //!STBI_NO_STDIO
+
+unsigned char *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
+{
+ stbi s;
+ start_mem(&s,buffer,len);
+ return stbi_load_main(&s,x,y,comp,req_comp);
+}
+
+unsigned char *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
+{
+ stbi s;
+ start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
+ return stbi_load_main(&s,x,y,comp,req_comp);
+}
+
+#ifndef STBI_NO_HDR
+
+float *stbi_loadf_main(stbi *s, int *x, int *y, int *comp, int req_comp)
+{
+ unsigned |