summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2010-10-02 03:43:27 +0200
committerVegard Storheil Eriksen <zyp@jvnv.net>2010-10-02 03:43:27 +0200
commit8e0a72184085aa6e3e741ad5d79f5db2e63d63a6 (patch)
treef7924997c55e98ced990fc24e19b48ec0fedb535
parentbf4827f46f22b4df0fa2ddd6716fd59247f9e1bb (diff)
Replaced TextureSDL with TextureSTBI.
-rw-r--r--SConscript1
-rw-r--r--stb_image.c4954
-rw-r--r--stb_image.h7
-rw-r--r--texturesdl.cpp19
-rw-r--r--texturesdl.h8
-rw-r--r--texturestbi.cpp20
-rw-r--r--texturestbi.h8
7 files changed, 4990 insertions, 27 deletions
diff --git a/SConscript b/SConscript
index fa6ff2e..208e264 100644
--- a/SConscript
+++ b/SConscript
@@ -1,5 +1,6 @@
Import('env')
sources = Glob('*.cpp')
+sources += Glob('*.c')
env.Library('wriggle', sources)
diff --git a/stb_image.c b/stb_image.c
new file mode 100644
index 0000000..c26b420
--- /dev/null
+++ b/stb_image.c
@@ -0,0 +1,4954 @@
+/* stbi-1.29 - 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 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)
+
+ - decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code)
+ - supports installable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
+
+ Latest revisions:
+ 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
+ 1.25 (2010-07-17) refix trans_data warning (Won Chun)
+ 1.24 (2010-07-12) perf improvements reading from files
+ minor perf improvements for jpeg
+ deprecated type-specific functions in hope of feedback
+ attempt to fix trans_data warning (Won Chun)
+ 1.23 fixed bug in iPhone support
+ 1.22 (2010-07-10) removed image *writing* support to stb_image_write.h
+ stbi_info support from Jetro Lauha
+ GIF support from Jean-Marc Lienher
+ iPhone PNG-extensions from James Brown
+ warning-fixes from Nicolas Schulz and Janez Zemva
+ 1.21 fix use of 'uint8' in header (reported by jon blow)
+ 1.20 added support for Softimage PIC, by Tom Seddon
+
+ See end of file for full revision history.
+
+ TODO:
+ stbi_info support for BMP,PSD,HDR,PIC
+ rewrite stbi_info and load_file variations to share file handling code
+ (current system allows individual functions to be called directly,
+ since each does all the work, but I doubt anyone uses this in practice)
+
+
+ ============================ 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
+ Aruelien Pocheville
+
+ 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
+// 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);
+
+#ifndef STBI_NO_STDIO
+#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
+
+#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 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
+
+// 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_is_hdr_from_memory(stbi_uc const *buffer, int len);
+
+#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);
+
+extern int stbi_is_hdr (char const *filename);
+extern int stbi_is_hdr_from_file(FILE *f);
+#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 new loaders
+typedef struct
+{
+ int (*test_memory)(stbi_uc const *buffer, int len);
+ stbi_uc * (*load_from_memory)(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+ #ifndef STBI_NO_STDIO
+ int (*test_file)(FILE *f);
+ stbi_uc * (*load_from_file)(FILE *f, int *x, int *y, int *comp, int req_comp);
+ #endif
+} stbi_loader;
+
+// register a loader by filling out the above structure (you must define ALL functions)
+// returns 1 if added or already added, 0 if not added (too many loaders)
+// NOT THREADSAFE
+extern int stbi_register_loader(stbi_loader *loader);
+
+// 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
+
+
+
+
+// TYPE-SPECIFIC ACCESS
+
+#ifdef STBI_TYPE_SPECIFIC_FUNCTIONS
+
+// is it a jpeg?
+extern int stbi_jpeg_test_memory (stbi_uc const *buffer, int len);
+extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
+
+#ifndef STBI_NO_STDIO
+extern stbi_uc *stbi_jpeg_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern int stbi_jpeg_test_file (FILE *f);
+extern stbi_uc *stbi_jpeg_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+
+extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp);
+extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp);
+#endif
+
+// is it a png?
+extern int stbi_png_test_memory (stbi_uc const *buffer, int len);
+extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+extern int stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
+
+#ifndef STBI_NO_STDIO
+extern stbi_uc *stbi_png_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern int stbi_png_info (char const *filename, int *x, int *y, int *comp);
+extern int stbi_png_test_file (FILE *f);
+extern stbi_uc *stbi_png_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+extern int stbi_png_info_from_file (FILE *f, int *x, int *y, int *comp);
+#endif
+
+// is it a bmp?
+extern int stbi_bmp_test_memory (stbi_uc const *buffer, int len);
+
+extern stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+#ifndef STBI_NO_STDIO
+extern int stbi_bmp_test_file (FILE *f);
+extern stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+#endif
+
+// is it a tga?
+extern int stbi_tga_test_memory (stbi_uc const *buffer, int len);
+
+extern stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+#ifndef STBI_NO_STDIO
+extern int stbi_tga_test_file (FILE *f);
+extern stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+#endif
+
+// is it a psd?
+extern int stbi_psd_test_memory (stbi_uc const *buffer, int len);
+
+extern stbi_uc *stbi_psd_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+#ifndef STBI_NO_STDIO
+extern int stbi_psd_test_file (FILE *f);
+extern stbi_uc *stbi_psd_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+#endif
+
+// is it an hdr?
+extern int stbi_hdr_test_memory (stbi_uc const *buffer, int len);
+
+extern float * stbi_hdr_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+#ifndef STBI_NO_STDIO
+extern int stbi_hdr_test_file (FILE *f);
+extern float * stbi_hdr_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+#endif
+
+// is it a pic?
+extern int stbi_pic_test_memory (stbi_uc const *buffer, int len);
+
+extern stbi_uc *stbi_pic_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern stbi_uc *stbi_pic_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+#ifndef STBI_NO_STDIO
+extern int stbi_pic_test_file (FILE *f);
+extern stbi_uc *stbi_pic_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+#endif
+
+// is it a gif?
+extern int stbi_gif_test_memory (stbi_uc const *buffer, int len);
+
+extern stbi_uc *stbi_gif_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+extern int stbi_gif_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
+
+#ifndef STBI_NO_STDIO
+extern int stbi_gif_test_file (FILE *f);
+extern stbi_uc *stbi_gif_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+extern int stbi_gif_info (char const *filename, int *x, int *y, int *comp);
+extern int stbi_gif_info_from_file (FILE *f, int *x, int *y, int *comp);
+#endif
+
+#endif//STBI_TYPE_SPECIFIC_FUNCTIONS
+
+
+
+
+#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
+#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 __forceinline inline
+ #else
+ #define __forceinline
+ #endif
+#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) v=v
+
+#ifdef _MSC_VER
+#define STBI_HAS_LRTOL
+#endif
+
+#ifdef STBI_HAS_LRTOL
+ #define stbi_lrot(x,y) _lrotl(x,y)
+#else
+ #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
+#endif
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// Generic API that works on all image types
+//
+
+// deprecated functions
+
+// is it a jpeg?
+extern int stbi_jpeg_test_memory (stbi_uc const *buffer, int len);
+extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
+
+#ifndef STBI_NO_STDIO
+extern stbi_uc *stbi_jpeg_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern int stbi_jpeg_test_file (FILE *f);
+extern stbi_uc *stbi_jpeg_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+
+extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp);
+extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp);
+#endif
+
+// is it a png?
+extern int stbi_png_test_memory (stbi_uc const *buffer, int len);
+extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+extern int stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
+
+#ifndef STBI_NO_STDIO
+extern stbi_uc *stbi_png_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern int stbi_png_info (char const *filename, int *x, int *y, int *comp);
+extern int stbi_png_test_file (FILE *f);
+extern stbi_uc *stbi_png_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+extern int stbi_png_info_from_file (FILE *f, int *x, int *y, int *comp);
+#endif
+
+// is it a bmp?
+extern int stbi_bmp_test_memory (stbi_uc const *buffer, int len);
+
+extern stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+#ifndef STBI_NO_STDIO
+extern int stbi_bmp_test_file (FILE *f);
+extern stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+#endif
+
+// is it a tga?
+extern int stbi_tga_test_memory (stbi_uc const *buffer, int len);
+
+extern stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+#ifndef STBI_NO_STDIO
+extern int stbi_tga_test_file (FILE *f);
+extern stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+#endif
+
+// is it a psd?
+extern int stbi_psd_test_memory (stbi_uc const *buffer, int len);
+
+extern stbi_uc *stbi_psd_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+#ifndef STBI_NO_STDIO
+extern int stbi_psd_test_file (FILE *f);
+extern stbi_uc *stbi_psd_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+#endif
+
+// is it an hdr?
+extern int stbi_hdr_test_memory (stbi_uc const *buffer, int len);
+
+extern float * stbi_hdr_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+#ifndef STBI_NO_STDIO
+extern int stbi_hdr_test_file (FILE *f);
+extern float * stbi_hdr_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+#endif
+
+// is it a pic?
+extern int stbi_pic_test_memory (stbi_uc const *buffer, int len);
+
+extern stbi_uc *stbi_pic_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern stbi_uc *stbi_pic_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+#ifndef STBI_NO_STDIO
+extern int stbi_pic_test_file (FILE *f);
+extern stbi_uc *stbi_pic_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+#endif
+
+// is it a gif?
+extern int stbi_gif_test_memory (stbi_uc const *buffer, int len);
+
+extern stbi_uc *stbi_gif_load (char const *filename, int *x, int *y, int *comp, int req_comp);
+extern stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
+extern int stbi_gif_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
+
+#ifndef STBI_NO_STDIO
+extern int stbi_gif_test_file (FILE *f);
+extern stbi_uc *stbi_gif_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
+extern int stbi_gif_info (char const *filename, int *x, int *y, int *comp);
+extern int stbi_gif_info_from_file (FILE *f, int *x, int *y, int *comp);
+#endif
+
+
+// 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;
+}
+
+#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);
+}
+
+#define MAX_LOADERS 32
+stbi_loader *loaders[MAX_LOADERS];
+static int max_loaders = 0;
+
+int stbi_register_loader(stbi_loader *loader)
+{
+ int i;
+ for (i=0; i < MAX_LOADERS; ++i) {
+ // already present?
+ if (loaders[i] == loader)
+ return 1;
+ // end of the list?
+ if (loaders[i] == NULL) {
+ loaders[i] = loader;
+ max_loaders = i+1;
+ return 1;
+ }
+ }
+ // no room for it
+ return 0;
+}
+
+#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
+
+#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)
+{
+ int i;
+ if (stbi_jpeg_test_file(f)) return stbi_jpeg_load_from_file(f,x,y,comp,req_comp);
+ if (stbi_png_test_file(f)) return stbi_png_load_from_file(f,x,y,comp,req_comp);
+ if (stbi_bmp_test_file(f)) return stbi_bmp_load_from_file(f,x,y,comp,req_comp);
+ if (stbi_gif_test_file(f)) return stbi_gif_load_from_file(f,x,y,comp,req_comp);
+ if (stbi_psd_test_file(f)) return stbi_psd_load_from_file(f,x,y,comp,req_comp);
+ if (stbi_pic_test_file(f)) return stbi_pic_load_from_file(f,x,y,comp,req_comp);
+
+ #ifndef STBI_NO_HDR
+ if (stbi_hdr_test_file(f)) {
+ float *hdr = stbi_hdr_load_from_file(f, x,y,comp,req_comp);
+ return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
+ }
+ #endif
+
+ for (i=0; i < max_loaders; ++i)
+ if (loaders[i]->test_file(f))
+ return loaders[i]->load_from_file(f,x,y,comp,req_comp);
+ // test tga last because it's a crappy test!
+ if (stbi_tga_test_file(f))
+ return stbi_tga_load_from_file(f,x,y,comp,req_comp);
+ return epuc("unknown image type", "Image not of any known type, or corrupt");
+}
+#endif
+
+unsigned char *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
+{
+ int i;
+ if (stbi_jpeg_test_memory(buffer,len)) return stbi_jpeg_load_from_memory(buffer,len,x,y,comp,req_comp);
+ if (stbi_png_test_memory(buffer,len)) return stbi_png_load_from_memory(buffer,len,x,y,comp,req_comp);
+ if (stbi_bmp_test_memory(buffer,len)) return stbi_bmp_load_from_memory(buffer,len,x,y,comp,req_comp);
+ if (stbi_gif_test_memory(buffer,len)) return stbi_gif_load_from_memory(buffer,len,x,y,comp,req_comp);
+ if (stbi_psd_test_memory(buffer,len)) return stbi_psd_load_from_memory(buffer,len,x,y,comp,req_comp);
+ if (stbi_pic_test_memory(buffer,len)) return stbi_pic_load_from_memory(buffer,len,x,y,comp,req_comp);
+
+ #ifndef STBI_NO_HDR
+ if (stbi_hdr_test_memory(buffer, len)) {
+ float *hdr = stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp);
+ return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
+ }
+ #endif
+
+ for (i=0; i < max_loaders; ++i)
+ if (loaders[i]->test_memory(buffer,len))
+ return loaders[i]->load_from_memory(buffer,len,x,y,comp,req_comp);
+ // test tga last because it's a crappy test!
+ if (stbi_tga_test_memory(buffer,len))
+ return stbi_tga_load_from_memory(buffer,len,x,y,comp,req_comp);
+ return epuc("unknown image type", "Image not of any known type, or corrupt");
+}
+
+#ifndef STBI_NO_HDR
+
+#ifndef STBI_NO_STDIO
+float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
+{
+ FILE *f = fopen(filename, "rb");
+ float *result;
+ if (!f) return epf("can't fopen", "Unable to open file");
+ result = stbi_loadf_from_file(f,x,y,comp,req_comp);
+ fclose(f);
+ return result;
+}
+
+float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
+{
+ unsigned char *data;
+ #ifndef STBI_NO_HDR
+ if (stbi_hdr_test_file(f))
+ return stbi_hdr_load_from_file(f,x,y,comp,req_comp);
+ #endif
+ data = stbi_load_from_file(f, x, y, comp, req_comp);
+ if (data)
+ return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
+ return epf("unknown image type", "Image not of any known type, or corrupt");
+}
+#endif
+
+float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
+{
+ stbi_uc *data;
+ #ifndef STBI_NO_HDR
+ if (stbi_hdr_test_memory(buffer, len))
+ return stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp);
+ #endif
+ data = stbi_load_from_memory(buffer, len, x, y, comp, req_comp);
+ if (data)
+ return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
+ return epf("unknown image type", "Image not of any known type, or corrupt");
+}
+#endif
+
+// these is-hdr-or-not is defined independent of whether STBI_NO_HDR is
+// defined, for API simplicity; if STBI_NO_HDR is defined, it always
+// reports false!
+
+int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
+{
+ #ifndef STBI_NO_HDR
+ return stbi_hdr_test_memory(buffer, len);
+ #else
+ STBI_NOTUSED(buffer);
+ STBI_NOTUSED(len);
+ return 0;
+ #endif
+}
+
+#ifndef STBI_NO_STDIO
+extern int stbi_is_hdr (char const *filename)
+{
+ FILE *f = fopen(filename, "rb");
+ int result=0;
+ if (f) {
+ result = stbi_is_hdr_from_file(f);
+ fclose(f);
+ }
+ return result;
+}
+
+extern int stbi_is_hdr_from_file(FILE *f)
+{
+ #ifndef STBI_NO_HDR
+ return stbi_hdr_test_file(f);
+ #else
+ return 0;
+ #endif
+}
+
+#endif
+
+#ifndef STBI_NO_HDR
+static float h2l_gamma_i=1.0f/2.2f, h2l_scale_i=1.0f;
+static float l2h_gamma=2.2f, l2h_scale=1.0f;
+
+void stbi_hdr_to_ldr_gamma(float gamma) { h2l_gamma_i = 1/gamma; }
+void stbi_hdr_to_ldr_scale(float scale) { h2l_scale_i = 1/scale; }
+
+void stbi_ldr_to_hdr_gamma(float gamma) { l2h_gamma = gamma; }
+void stbi_ldr_to_hdr_scale(float scale) { l2h_scale = scale; }
+#endif
+
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// Common code used by all image loaders
+//
+
+enum
+{
+ SCAN_load=0,
+ SCAN_type,
+ SCAN_header
+};
+
+typedef struct
+{
+ uint32 img_x, img_y;
+ int img_n, img_out_n;
+
+ #ifndef STBI_NO_STDIO
+ FILE *img_file;
+ int buflen;
+ uint8 buffer_start[128];
+ int from_file;
+ #endif
+ uint8 *img_buffer, *img_buffer_end;
+} stbi;
+
+#ifndef STBI_NO_STDIO
+static void start_file(stbi *s, FILE *f)
+{
+ s->img_file = f;
+ s->buflen = sizeof(s->buffer_start);
+ s->img_buffer_end = s->buffer_start + s->buflen;
+ s->img_buffer = s->img_buffer_end;
+ s->from_file = 1;
+}
+#endif
+
+static void start_mem(stbi *s, uint8 const *buffer, int len)
+{
+#ifndef STBI_NO_STDIO
+ s->img_file = NULL;
+ s->from_file = 0;
+#endif
+ s->img_buffer = (uint8 *) buffer;
+ s->img_buffer_end = (uint8 *) buffer+len;
+}
+
+#ifndef STBI_NO_STDIO
+static void refill_buffer(stbi *s)
+{
+ int n = fread(s->buffer_start, 1, s->buflen, s->img_file);
+ if (n == 0) {
+ s->from_file = 0;
+ s->img_buffer = s->img_buffer_end-1;
+ *s->img_buffer = 0;
+ } else {
+ s->img_buffer = s->buffer_start;
+ s->img_buffer_end = s->buffer_start + n;
+ }
+}
+#endif
+
+__forceinline static int get8(stbi *s)
+{
+ if (s->img_buffer < s->img_buffer_end)
+ return *s->img_buffer++;
+#ifndef STBI_NO_STDIO
+ if (s->from_file) {
+ refill_buffer(s);
+ return *s->img_buffer++;
+ }
+#endif
+ return 0;
+}
+
+__forceinline static int at_eof(stbi *s)
+{
+#ifndef STBI_NO_STDIO
+ if (s->img_file) {
+ if (!feof(s->img_file)) return 0;
+ // if feof() is true, check if buffer = end
+ // special case: we've only got the special 0 character at the end
+ if (s->from_file == 0) return 1;
+ }
+#endif
+ return s->img_buffer >= s->img_buffer_end;
+}
+
+__forceinline static uint8 get8u(stbi *s)
+{
+ return (uint8) get8(s);
+}
+
+static void skip(stbi *s, int n)
+{
+#ifndef STBI_NO_STDIO
+ if (s->img_file) {
+ int blen = s->img_buffer_end - s->img_buffer;
+ if (blen < n) {
+ s->img_buffer = s->img_buffer_end;
+ fseek(s->img_file, n - blen, SEEK_CUR);
+ return;
+ }
+ }
+#endif
+ s->img_buffer += n;
+}
+
+static int getn(stbi *s, stbi_uc *buffer, int n)
+{
+#ifndef STBI_NO_STDIO
+ if (s->img_file) {
+ int blen = s->img_buffer_end - s->img_buffer;
+ if (blen < n) {
+ int res;
+ memcpy(buffer, s->img_buffer, blen);
+ res = ((int) fread(buffer + blen, 1, n - blen, s->img_file) == (n-blen));
+ s->img_buffer = s->img_buffer_end;
+ return res;
+ }
+ }
+#endif
+ if (s->img_buffer+n <= s->img_buffer_end) {
+ memcpy(buffer, s->img_buffer, n);
+ s->img_buffer += n;
+ return 1;
+ } else
+ return 0;
+}
+
+static int get16(stbi *s)
+{
+ int z = get8(s);
+ return (z << 8) + get8(s);
+}
+
+static uint32 get32(stbi *s)
+{
+ uint32 z = get16(s);
+ return (z << 16) + get16(s);
+}
+
+static int get16le(stbi *s)
+{