GEL
2
GEL is a library for Geometry and Linear Algebra
|
00001 /* stbi-1.16 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c 00002 when you control the images you're loading 00003 00004 QUICK NOTES: 00005 Primarily of interest to game developers and other people who can 00006 avoid problematic images and only need the trivial interface 00007 00008 JPEG baseline (no JPEG progressive, no oddball channel decimations) 00009 PNG non-interlaced 00010 BMP non-1bpp, non-RLE 00011 TGA (not sure what subset, if a subset) 00012 PSD (composited view only, no extra channels) 00013 HDR (radiance rgbE format) 00014 writes BMP,TGA (define STBI_NO_WRITE to remove code) 00015 decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code) 00016 supports installable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD) 00017 00018 TODO: 00019 stbi_info_* 00020 00021 history: 00022 1.16 major bugfix - convert_format converted one too many pixels 00023 1.15 initialize some fields for thread safety 00024 1.14 fix threadsafe conversion bug; header-file-only version (#define STBI_HEADER_FILE_ONLY before including) 00025 1.13 threadsafe 00026 1.12 const qualifiers in the API 00027 1.11 Support installable IDCT, colorspace conversion routines 00028 1.10 Fixes for 64-bit (don't use "unsigned long") 00029 optimized upsampling by Fabian "ryg" Giesen 00030 1.09 Fix format-conversion for PSD code (bad global variables!) 00031 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz 00032 1.07 attempt to fix C++ warning/errors again 00033 1.06 attempt to fix C++ warning/errors again 00034 1.05 fix TGA loading to return correct *comp and use good luminance calc 00035 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free 00036 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR 00037 1.02 support for (subset of) HDR files, float interface for preferred access to them 00038 1.01 fix bug: possible bug in handling right-side up bmps... not sure 00039 fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all 00040 1.00 interface to zlib that skips zlib header 00041 0.99 correct handling of alpha in palette 00042 0.98 TGA loader by lonesock; dynamically add loaders (untested) 00043 0.97 jpeg errors on too large a file; also catch another malloc failure 00044 0.96 fix detection of invalid v value - particleman@mollyrocket forum 00045 0.95 during header scan, seek to markers in case of padding 00046 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same 00047 0.93 handle jpegtran output; verbose errors 00048 0.92 read 4,8,16,24,32-bit BMP files of several formats 00049 0.91 output 24-bit Windows 3.0 BMP files 00050 0.90 fix a few more warnings; bump version number to approach 1.0 00051 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd 00052 0.60 fix compiling as c++ 00053 0.59 fix warnings: merge Dave Moore's -Wall fixes 00054 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian 00055 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less 00056 than 16 available 00057 0.56 fix bug: zlib uncompressed mode len vs. nlen 00058 0.55 fix bug: restart_interval not initialized to 0 00059 0.54 allow NULL for 'int *comp' 00060 0.53 fix bug in png 3->4; speedup png decoding 00061 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments 00062 0.51 obey req_comp requests, 1-component jpegs return as 1-component, 00063 on 'test' only check type, not whether we support this variant 00064 */ 00065 00066 #ifndef HEADER_STB_IMAGE_AUGMENTED 00067 #define HEADER_STB_IMAGE_AUGMENTED 00068 00070 // 00071 // Limitations: 00072 // - no progressive/interlaced support (jpeg, png) 00073 // - 8-bit samples only (jpeg, png) 00074 // - not threadsafe 00075 // - channel subsampling of at most 2 in each dimension (jpeg) 00076 // - no delayed line count (jpeg) -- IJG doesn't support either 00077 // 00078 // Basic usage (see HDR discussion below): 00079 // int x,y,n; 00080 // unsigned char *data = stbi_load(filename, &x, &y, &n, 0); 00081 // // ... process data if not NULL ... 00082 // // ... x = width, y = height, n = # 8-bit components per pixel ... 00083 // // ... replace '0' with '1'..'4' to force that many components per pixel 00084 // stbi_image_free(data) 00085 // 00086 // Standard parameters: 00087 // int *x -- outputs image width in pixels 00088 // int *y -- outputs image height in pixels 00089 // int *comp -- outputs # of image components in image file 00090 // int req_comp -- if non-zero, # of image components requested in result 00091 // 00092 // The return value from an image loader is an 'unsigned char *' which points 00093 // to the pixel data. The pixel data consists of *y scanlines of *x pixels, 00094 // with each pixel consisting of N interleaved 8-bit components; the first 00095 // pixel pointed to is top-left-most in the image. There is no padding between 00096 // image scanlines or between pixels, regardless of format. The number of 00097 // components N is 'req_comp' if req_comp is non-zero, or *comp otherwise. 00098 // If req_comp is non-zero, *comp has the number of components that _would_ 00099 // have been output otherwise. E.g. if you set req_comp to 4, you will always 00100 // get RGBA output, but you can check *comp to easily see if it's opaque. 00101 // 00102 // An output image with N components has the following components interleaved 00103 // in this order in each pixel: 00104 // 00105 // N=#comp components 00106 // 1 grey 00107 // 2 grey, alpha 00108 // 3 red, green, blue 00109 // 4 red, green, blue, alpha 00110 // 00111 // If image loading fails for any reason, the return value will be NULL, 00112 // and *x, *y, *comp will be unchanged. The function stbi_failure_reason() 00113 // can be queried for an extremely brief, end-user unfriendly explanation 00114 // of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid 00115 // compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly 00116 // more user-friendly ones. 00117 // 00118 // Paletted PNG and BMP images are automatically depalettized. 00119 // 00120 // 00121 // =========================================================================== 00122 // 00123 // HDR image support (disable by defining STBI_NO_HDR) 00124 // 00125 // stb_image now supports loading HDR images in general, and currently 00126 // the Radiance .HDR file format, although the support is provided 00127 // generically. You can still load any file through the existing interface; 00128 // if you attempt to load an HDR file, it will be automatically remapped to 00129 // LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1; 00130 // both of these constants can be reconfigured through this interface: 00131 // 00132 // stbi_hdr_to_ldr_gamma(2.2f); 00133 // stbi_hdr_to_ldr_scale(1.0f); 00134 // 00135 // (note, do not use _inverse_ constants; stbi_image will invert them 00136 // appropriately). 00137 // 00138 // Additionally, there is a new, parallel interface for loading files as 00139 // (linear) floats to preserve the full dynamic range: 00140 // 00141 // float *data = stbi_loadf(filename, &x, &y, &n, 0); 00142 // 00143 // If you load LDR images through this interface, those images will 00144 // be promoted to floating point values, run through the inverse of 00145 // constants corresponding to the above: 00146 // 00147 // stbi_ldr_to_hdr_scale(1.0f); 00148 // stbi_ldr_to_hdr_gamma(2.2f); 00149 // 00150 // Finally, given a filename (or an open file or memory block--see header 00151 // file for details) containing image data, you can query for the "most 00152 // appropriate" interface to use (that is, whether the image is HDR or 00153 // not), using: 00154 // 00155 // stbi_is_hdr(char *filename); 00156 00157 #ifndef STBI_NO_STDIO 00158 #include <stdio.h> 00159 #endif 00160 00161 #define STBI_VERSION 1 00162 00163 enum 00164 { 00165 STBI_default = 0, // only used for req_comp 00166 00167 STBI_grey = 1, 00168 STBI_grey_alpha = 2, 00169 STBI_rgb = 3, 00170 STBI_rgb_alpha = 4, 00171 }; 00172 00173 typedef unsigned char stbi_uc; 00174 00175 #ifdef __cplusplus 00176 extern "C" { 00177 #endif 00178 00179 // WRITING API 00180 00181 #if !defined(STBI_NO_WRITE) && !defined(STBI_NO_STDIO) 00182 // write a BMP/TGA file given tightly packed 'comp' channels (no padding, nor bmp-stride-padding) 00183 // (you must include the appropriate extension in the filename). 00184 // returns TRUE on success, FALSE if couldn't open file, error writing file 00185 extern int stbi_write_bmp (char const *filename, int x, int y, int comp, void *data); 00186 extern int stbi_write_tga (char const *filename, int x, int y, int comp, void *data); 00187 #endif 00188 00189 // PRIMARY API - works on images of any type 00190 00191 // load image by filename, open file, or memory buffer 00192 #ifndef STBI_NO_STDIO 00193 extern stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp); 00194 extern stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); 00195 extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp); 00196 #endif 00197 extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); 00198 // for stbi_load_from_file, file pointer is left pointing immediately after image 00199 00200 #ifndef STBI_NO_HDR 00201 #ifndef STBI_NO_STDIO 00202 extern float *stbi_loadf (char const *filename, int *x, int *y, int *comp, int req_comp); 00203 extern float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); 00204 #endif 00205 extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); 00206 00207 extern void stbi_hdr_to_ldr_gamma(float gamma); 00208 extern void stbi_hdr_to_ldr_scale(float scale); 00209 00210 extern void stbi_ldr_to_hdr_gamma(float gamma); 00211 extern void stbi_ldr_to_hdr_scale(float scale); 00212 00213 #endif // STBI_NO_HDR 00214 00215 // get a VERY brief reason for failure 00216 // NOT THREADSAFE 00217 extern char *stbi_failure_reason (void); 00218 00219 // free the loaded image -- this is just free() 00220 extern void stbi_image_free (void *retval_from_stbi_load); 00221 00222 // get image dimensions & components without fully decoding 00223 extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); 00224 extern int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len); 00225 #ifndef STBI_NO_STDIO 00226 extern int stbi_info (char const *filename, int *x, int *y, int *comp); 00227 extern int stbi_is_hdr (char const *filename); 00228 extern int stbi_is_hdr_from_file(FILE *f); 00229 #endif 00230 00231 // ZLIB client - used by PNG, available for other purposes 00232 00233 extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen); 00234 extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen); 00235 extern int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); 00236 00237 extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen); 00238 extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); 00239 00240 // TYPE-SPECIFIC ACCESS 00241 00242 // is it a jpeg? 00243 extern int stbi_jpeg_test_memory (stbi_uc const *buffer, int len); 00244 extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); 00245 extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); 00246 00247 #ifndef STBI_NO_STDIO 00248 extern stbi_uc *stbi_jpeg_load (char const *filename, int *x, int *y, int *comp, int req_comp); 00249 extern int stbi_jpeg_test_file (FILE *f); 00250 extern stbi_uc *stbi_jpeg_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); 00251 00252 extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp); 00253 extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp); 00254 #endif 00255 00256 // is it a png? 00257 extern int stbi_png_test_memory (stbi_uc const *buffer, int len); 00258 extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); 00259 extern int stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp); 00260 00261 #ifndef STBI_NO_STDIO 00262 extern stbi_uc *stbi_png_load (char const *filename, int *x, int *y, int *comp, int req_comp); 00263 extern int stbi_png_info (char const *filename, int *x, int *y, int *comp); 00264 extern int stbi_png_test_file (FILE *f); 00265 extern stbi_uc *stbi_png_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); 00266 extern int stbi_png_info_from_file (FILE *f, int *x, int *y, int *comp); 00267 #endif 00268 00269 // is it a bmp? 00270 extern int stbi_bmp_test_memory (stbi_uc const *buffer, int len); 00271 00272 extern stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp); 00273 extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); 00274 #ifndef STBI_NO_STDIO 00275 extern int stbi_bmp_test_file (FILE *f); 00276 extern stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); 00277 #endif 00278 00279 // is it a tga? 00280 extern int stbi_tga_test_memory (stbi_uc const *buffer, int len); 00281 00282 extern stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp); 00283 extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); 00284 #ifndef STBI_NO_STDIO 00285 extern int stbi_tga_test_file (FILE *f); 00286 extern stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); 00287 #endif 00288 00289 // is it a psd? 00290 extern int stbi_psd_test_memory (stbi_uc const *buffer, int len); 00291 00292 extern stbi_uc *stbi_psd_load (char const *filename, int *x, int *y, int *comp, int req_comp); 00293 extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); 00294 #ifndef STBI_NO_STDIO 00295 extern int stbi_psd_test_file (FILE *f); 00296 extern stbi_uc *stbi_psd_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); 00297 #endif 00298 00299 // is it an hdr? 00300 extern int stbi_hdr_test_memory (stbi_uc const *buffer, int len); 00301 00302 extern float * stbi_hdr_load (char const *filename, int *x, int *y, int *comp, int req_comp); 00303 extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); 00304 extern stbi_uc *stbi_hdr_load_rgbe (char const *filename, int *x, int *y, int *comp, int req_comp); 00305 extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); 00306 #ifndef STBI_NO_STDIO 00307 extern int stbi_hdr_test_file (FILE *f); 00308 extern float * stbi_hdr_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp); 00309 extern stbi_uc *stbi_hdr_load_rgbe_file (FILE *f, int *x, int *y, int *comp, int req_comp); 00310 #endif 00311 00312 // define new loaders 00313 typedef struct 00314 { 00315 int (*test_memory)(stbi_uc const *buffer, int len); 00316 stbi_uc * (*load_from_memory)(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp); 00317 #ifndef STBI_NO_STDIO 00318 int (*test_file)(FILE *f); 00319 stbi_uc * (*load_from_file)(FILE *f, int *x, int *y, int *comp, int req_comp); 00320 #endif 00321 } stbi_loader; 00322 00323 // register a loader by filling out the above structure (you must defined ALL functions) 00324 // returns 1 if added or already added, 0 if not added (too many loaders) 00325 // NOT THREADSAFE 00326 extern int stbi_register_loader(stbi_loader *loader); 00327 00328 // define faster low-level operations (typically SIMD support) 00329 #if STBI_SIMD 00330 typedef void (*stbi_idct_8x8)(uint8 *out, int out_stride, short data[64], unsigned short *dequantize); 00331 // compute an integer IDCT on "input" 00332 // input[x] = data[x] * dequantize[x] 00333 // write results to 'out': 64 samples, each run of 8 spaced by 'out_stride' 00334 // CLAMP results to 0..255 00335 typedef void (*stbi_YCbCr_to_RGB_run)(uint8 *output, uint8 const *y, uint8 const *cb, uint8 const *cr, int count, int step); 00336 // compute a conversion from YCbCr to RGB 00337 // 'count' pixels 00338 // write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B 00339 // y: Y input channel 00340 // cb: Cb input channel; scale/biased to be 0..255 00341 // cr: Cr input channel; scale/biased to be 0..255 00342 00343 extern void stbi_install_idct(stbi_idct_8x8 func); 00344 extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func); 00345 #endif // STBI_SIMD 00346 00347 #ifdef __cplusplus 00348 } 00349 #endif 00350 00351 // 00352 // 00354 #endif // STBI_INCLUDE_STB_IMAGE_H