stb_image.h
This file
Path
stb_image.h
7,988 lines · 276.4 KiB · C
Last changed by
Merge branch 'resize_latest' of https://github.com/jeffrbig2/stb into workingSean Barrettbefore adoption8503b88
History of this fileTake it
1/* stb_image - v2.30 - public domain image loader - http://nothings.org/stb2 no warranty implied; use at your own risk34 Do this:5 #define STB_IMAGE_IMPLEMENTATION6 before you include this file in *one* C or C++ file to create the implementation.78 // i.e. it should look like this:9 #include ...10 #include ...11 #include ...12 #define STB_IMAGE_IMPLEMENTATION13 #include "stb_image.h"1415 You can #define STBI_ASSERT(x) before the #include to avoid using assert.h.16 And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free171819 QUICK NOTES:20 Primarily of interest to game developers and other people who can21 avoid problematic images and only need the trivial interface2223 JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)24 PNG 1/2/4/8/16-bit-per-channel2526 TGA (not sure what subset, if a subset)27 BMP non-1bpp, non-RLE28 PSD (composited view only, no extra channels, 8/16 bit-per-channel)2930 GIF (*comp always reports as 4-channel)31 HDR (radiance rgbE format)32 PIC (Softimage PIC)33 PNM (PPM and PGM binary only)3435 Animated GIF still needs a proper API, but here's one way to do it:36 http://gist.github.com/urraka/685d9a6340b26b830d493738 - decode from memory or through FILE (define STBI_NO_STDIO to remove code)39 - decode from arbitrary I/O callbacks40 - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON)4142 Full documentation under "DOCUMENTATION" below.434445LICENSE4647 See end of file for license information.4849RECENT REVISION HISTORY:5051 2.30 (2024-05-31) avoid erroneous gcc warning52 2.29 (2023-05-xx) optimizations53 2.28 (2023-01-29) many error fixes, security errors, just tons of stuff54 2.27 (2021-07-11) document stbi_info better, 16-bit PNM support, bug fixes55 2.26 (2020-07-13) many minor fixes56 2.25 (2020-02-02) fix warnings57 2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically58 2.23 (2019-08-11) fix clang static analysis warning59 2.22 (2019-03-04) gif fixes, fix warnings60 2.21 (2019-02-25) fix typo in comment61 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs62 2.19 (2018-02-11) fix warning63 2.18 (2018-01-30) fix warnings64 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings65 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes66 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC67 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs68 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes69 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes70 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x6471 RGB-format JPEG; remove white matting in PSD;72 allocate large structures on the stack;73 correct channel count for PNG & BMP74 2.10 (2016-01-22) avoid warning introduced in 2.0975 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED7677 See end of file for full revision history.787980 ============================ Contributors =========================8182 Image formats Extensions, features83 Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info)84 Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info)85 Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG)86 Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks)87 Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG)88 Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip)89 Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD)90 github:urraka (animated gif) Junggon Kim (PNM comments)91 Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA)92 socks-the-fox (16-bit PNG)93 Jeremy Sawicki (handle all ImageNet JPGs)94 Optimizations & bugfixes Mikhail Morozov (1-bit BMP)95 Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)96 Arseny Kapoulkine Simon Breuss (16-bit PNM)97 John-Mark Allen98 Carmelo J Fdez-Aguera99100 Bug & warning fixes101 Marc LeBlanc David Woo Guillaume George Martins Mozeiko102 Christpher Lloyd Jerry Jansson Joseph Thomson Blazej Dariusz Roszkowski103 Phil Jordan Dave Moore Roy Eltham104 Hayaki Saito Nathan Reed Won Chun105 Luke Graham Johan Duparc Nick Verigakis the Horde3D community106 Thomas Ruf Ronny Chevalier github:rlyeh107 Janez Zemva John Bartholomew Michal Cichon github:romigrou108 Jonathan Blow Ken Hamada Tero Hanninen github:svdijk109 Eugene Golushkov Laurent Gomila Cort Stratton github:snagar110 Aruelien Pocheville Sergio Gonzalez Thibault Reuille github:Zelex111 Cass Everitt Ryamond Barbiero github:grim210112 Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw113 Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus114 Josh Tobin Neil Bickford Matthew Gregan github:poppolopoppo115 Julian Raschke Gregory Mullen Christian Floisand github:darealshinji116 Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007117 Brad Weinberger Matvey Cherevko github:mosra118 Luca Sas Alexander Veselov Zack Middleton [reserved]119 Ryan C. Gordon [reserved] [reserved]120 DO NOT ADD YOUR NAME HERE121122 Jacko Dirks123124 To add your name to the credits, pick a random blank space in the middle and fill it.125 80% of merge conflicts on stb PRs are due to people adding their name at the end126 of the credits.127*/128129#ifndef STBI_INCLUDE_STB_IMAGE_H130#define STBI_INCLUDE_STB_IMAGE_H131132// DOCUMENTATION133//134// Limitations:135// - no 12-bit-per-channel JPEG136// - no JPEGs with arithmetic coding137// - GIF always returns *comp=4138//139// Basic usage (see HDR discussion below for HDR usage):140// int x,y,n;141// unsigned char *data = stbi_load(filename, &x, &y, &n, 0);142// // ... process data if not NULL ...143// // ... x = width, y = height, n = # 8-bit components per pixel ...144// // ... replace '0' with '1'..'4' to force that many components per pixel145// // ... but 'n' will always be the number that it would have been if you said 0146// stbi_image_free(data);147//148// Standard parameters:149// int *x -- outputs image width in pixels150// int *y -- outputs image height in pixels151// int *channels_in_file -- outputs # of image components in image file152// int desired_channels -- if non-zero, # of image components requested in result153//154// The return value from an image loader is an 'unsigned char *' which points155// to the pixel data, or NULL on an allocation failure or if the image is156// corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,157// with each pixel consisting of N interleaved 8-bit components; the first158// pixel pointed to is top-left-most in the image. There is no padding between159// image scanlines or between pixels, regardless of format. The number of160// components N is 'desired_channels' if desired_channels is non-zero, or161// *channels_in_file otherwise. If desired_channels is non-zero,162// *channels_in_file has the number of components that _would_ have been163// output otherwise. E.g. if you set desired_channels to 4, you will always164// get RGBA output, but you can check *channels_in_file to see if it's trivially165// opaque because e.g. there were only 3 channels in the source image.166//167// An output image with N components has the following components interleaved168// in this order in each pixel:169//170// N=#comp components171// 1 grey172// 2 grey, alpha173// 3 red, green, blue174// 4 red, green, blue, alpha175//176// If image loading fails for any reason, the return value will be NULL,177// and *x, *y, *channels_in_file will be unchanged. The function178// stbi_failure_reason() can be queried for an extremely brief, end-user179// unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS180// to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly181// more user-friendly ones.182//183// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.184//185// To query the width, height and component count of an image without having to186// decode the full file, you can use the stbi_info family of functions:187//188// int x,y,n,ok;189// ok = stbi_info(filename, &x, &y, &n);190// // returns ok=1 and sets x, y, n if image is a supported format,191// // 0 otherwise.192//193// Note that stb_image pervasively uses ints in its public API for sizes,194// including sizes of memory buffers. This is now part of the API and thus195// hard to change without causing breakage. As a result, the various image196// loaders all have certain limits on image size; these differ somewhat197// by format but generally boil down to either just under 2GB or just under198// 1GB. When the decoded image would be larger than this, stb_image decoding199// will fail.200//201// Additionally, stb_image will reject image files that have any of their202// dimensions set to a larger value than the configurable STBI_MAX_DIMENSIONS,203// which defaults to 2**24 = 16777216 pixels. Due to the above memory limit,204// the only way to have an image with such dimensions load correctly205// is for it to have a rather extreme aspect ratio. Either way, the206// assumption here is that such larger images are likely to be malformed207// or malicious. If you do need to load an image with individual dimensions208// larger than that, and it still fits in the overall size limit, you can209// #define STBI_MAX_DIMENSIONS on your own to be something larger.210//211// ===========================================================================212//213// UNICODE:214//215// If compiling for Windows and you wish to use Unicode filenames, compile216// with217// #define STBI_WINDOWS_UTF8218// and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert219// Windows wchar_t filenames to utf8.220//221// ===========================================================================222//223// Philosophy224//225// stb libraries are designed with the following priorities:226//227// 1. easy to use228// 2. easy to maintain229// 3. good performance230//231// Sometimes I let "good performance" creep up in priority over "easy to maintain",232// and for best performance I may provide less-easy-to-use APIs that give higher233// performance, in addition to the easy-to-use ones. Nevertheless, it's important234// to keep in mind that from the standpoint of you, a client of this library,235// all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all.236//237// Some secondary priorities arise directly from the first two, some of which238// provide more explicit reasons why performance can't be emphasized.239//240// - Portable ("ease of use")241// - Small source code footprint ("easy to maintain")242// - No dependencies ("ease of use")243//244// ===========================================================================245//246// I/O callbacks247//248// I/O callbacks allow you to read from arbitrary sources, like packaged249// files or some other source. Data read from callbacks are processed250// through a small internal buffer (currently 128 bytes) to try to reduce251// overhead.252//253// The three functions you must define are "read" (reads some bytes of data),254// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).255//256// ===========================================================================257//258// SIMD support259//260// The JPEG decoder will try to automatically use SIMD kernels on x86 when261// supported by the compiler. For ARM Neon support, you must explicitly262// request it.263//264// (The old do-it-yourself SIMD API is no longer supported in the current265// code.)266//267// On x86, SSE2 will automatically be used when available based on a run-time268// test; if not, the generic C versions are used as a fall-back. On ARM targets,269// the typical path is to have separate builds for NEON and non-NEON devices270// (at least this is true for iOS and Android). Therefore, the NEON support is271// toggled by a build flag: define STBI_NEON to get NEON loops.272//273// If for some reason you do not want to use any of SIMD code, or if274// you have issues compiling it, you can disable it entirely by275// defining STBI_NO_SIMD.276//277// ===========================================================================278//279// HDR image support (disable by defining STBI_NO_HDR)280//281// stb_image supports loading HDR images in general, and currently the Radiance282// .HDR file format specifically. You can still load any file through the existing283// interface; if you attempt to load an HDR file, it will be automatically remapped284// to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;285// both of these constants can be reconfigured through this interface:286//287// stbi_hdr_to_ldr_gamma(2.2f);288// stbi_hdr_to_ldr_scale(1.0f);289//290// (note, do not use _inverse_ constants; stbi_image will invert them291// appropriately).292//293// Additionally, there is a new, parallel interface for loading files as294// (linear) floats to preserve the full dynamic range:295//296// float *data = stbi_loadf(filename, &x, &y, &n, 0);297//298// If you load LDR images through this interface, those images will299// be promoted to floating point values, run through the inverse of300// constants corresponding to the above:301//302// stbi_ldr_to_hdr_scale(1.0f);303// stbi_ldr_to_hdr_gamma(2.2f);304//305// Finally, given a filename (or an open file or memory block--see header306// file for details) containing image data, you can query for the "most307// appropriate" interface to use (that is, whether the image is HDR or308// not), using:309//310// stbi_is_hdr(char *filename);311//312// ===========================================================================313//314// iPhone PNG support:315//316// We optionally support converting iPhone-formatted PNGs (which store317// premultiplied BGRA) back to RGB, even though they're internally encoded318// differently. To enable this conversion, call319// stbi_convert_iphone_png_to_rgb(1).320//321// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per322// pixel to remove any premultiplied alpha *only* if the image file explicitly323// says there's premultiplied data (currently only happens in iPhone images,324// and only if iPhone convert-to-rgb processing is on).325//326// ===========================================================================327//328// ADDITIONAL CONFIGURATION329//330// - You can suppress implementation of any of the decoders to reduce331// your code footprint by #defining one or more of the following332// symbols before creating the implementation.333//334// STBI_NO_JPEG335// STBI_NO_PNG336// STBI_NO_BMP337// STBI_NO_PSD338// STBI_NO_TGA339// STBI_NO_GIF340// STBI_NO_HDR341// STBI_NO_PIC342// STBI_NO_PNM (.ppm and .pgm)343//344// - You can request *only* certain decoders and suppress all other ones345// (this will be more forward-compatible, as addition of new decoders346// doesn't require you to disable them explicitly):347//348// STBI_ONLY_JPEG349// STBI_ONLY_PNG350// STBI_ONLY_BMP351// STBI_ONLY_PSD352// STBI_ONLY_TGA353// STBI_ONLY_GIF354// STBI_ONLY_HDR355// STBI_ONLY_PIC356// STBI_ONLY_PNM (.ppm and .pgm)357//358// - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still359// want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB360//361// - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater362// than that size (in either width or height) without further processing.363// This is to let programs in the wild set an upper bound to prevent364// denial-of-service attacks on untrusted data, as one could generate a365// valid image of gigantic dimensions and force stb_image to allocate a366// huge block of memory and spend disproportionate time decoding it. By367// default this is set to (1 << 24), which is 16777216, but that's still368// very big.369370#ifndef STBI_NO_STDIO371#include <stdio.h>372#endif // STBI_NO_STDIO373374#define STBI_VERSION 1375376enum377{378 STBI_default = 0, // only used for desired_channels379380 STBI_grey = 1,381 STBI_grey_alpha = 2,382 STBI_rgb = 3,383 STBI_rgb_alpha = 4384};385386#include <stdlib.h>387typedef unsigned char stbi_uc;388typedef unsigned short stbi_us;389390#ifdef __cplusplus391extern "C" {392#endif393394#ifndef STBIDEF395#ifdef STB_IMAGE_STATIC396#define STBIDEF static397#else398#define STBIDEF extern399#endif400#endif401402//////////////////////////////////////////////////////////////////////////////403//404// PRIMARY API - works on images of any type405//406407//408// load image by filename, open file, or memory buffer409//410411typedef struct412{413 int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read414 void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative415 int (*eof) (void *user); // returns nonzero if we are at end of file/data416} stbi_io_callbacks;417418////////////////////////////////////419//420// 8-bits-per-channel interface421//422423STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);424STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels);425426#ifndef STBI_NO_STDIO427STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);428STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);429// for stbi_load_from_file, file pointer is left pointing immediately after image430#endif431432#ifndef STBI_NO_GIF433STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp);434#endif435436#ifdef STBI_WINDOWS_UTF8437STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);438#endif439440////////////////////////////////////441//442// 16-bits-per-channel interface443//444445STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);446STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);447448#ifndef STBI_NO_STDIO449STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);450STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);451#endif452453////////////////////////////////////454//455// float-per-channel interface456//457#ifndef STBI_NO_LINEAR458 STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);459 STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);460461 #ifndef STBI_NO_STDIO462 STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);463 STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);464 #endif465#endif466467#ifndef STBI_NO_HDR468 STBIDEF void stbi_hdr_to_ldr_gamma(float gamma);469 STBIDEF void stbi_hdr_to_ldr_scale(float scale);470#endif // STBI_NO_HDR471472#ifndef STBI_NO_LINEAR473 STBIDEF void stbi_ldr_to_hdr_gamma(float gamma);474 STBIDEF void stbi_ldr_to_hdr_scale(float scale);475#endif // STBI_NO_LINEAR476477// stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR478STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);479STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);480#ifndef STBI_NO_STDIO481STBIDEF int stbi_is_hdr (char const *filename);482STBIDEF int stbi_is_hdr_from_file(FILE *f);483#endif // STBI_NO_STDIO484485486// get a VERY brief reason for failure487// on most compilers (and ALL modern mainstream compilers) this is threadsafe488STBIDEF const char *stbi_failure_reason (void);489490// free the loaded image -- this is just free()491STBIDEF void stbi_image_free (void *retval_from_stbi_load);492493// get image dimensions & components without fully decoding494STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);495STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);496STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len);497STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user);498499#ifndef STBI_NO_STDIO500STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);501STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);502STBIDEF int stbi_is_16_bit (char const *filename);503STBIDEF int stbi_is_16_bit_from_file(FILE *f);504#endif505506507508// for image formats that explicitly notate that they have premultiplied alpha,509// we just return the colors as stored in the file. set this flag to force510// unpremultiplication. results are undefined if the unpremultiply overflow.511STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);512513// indicate whether we should process iphone images back to canonical format,514// or just pass them through "as-is"515STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);516517// flip the image vertically, so the first pixel in the output array is the bottom left518STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip);519520// as above, but only applies to images loaded on the thread that calls the function521// this function is only available if your compiler supports thread-local variables;522// calling it will fail to link if your compiler doesn't523STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply);524STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert);525STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip);526527// ZLIB client - used by PNG, available for other purposes528529STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);530STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);531STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);532STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);533534STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);535STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);536537538#ifdef __cplusplus539}540#endif541542//543//544//// end header file /////////////////////////////////////////////////////545#endif // STBI_INCLUDE_STB_IMAGE_H546547#ifdef STB_IMAGE_IMPLEMENTATION548549#if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \550 || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \551 || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \552 || defined(STBI_ONLY_ZLIB)553 #ifndef STBI_ONLY_JPEG554 #define STBI_NO_JPEG555 #endif556 #ifndef STBI_ONLY_PNG557 #define STBI_NO_PNG558 #endif559 #ifndef STBI_ONLY_BMP560 #define STBI_NO_BMP561 #endif562 #ifndef STBI_ONLY_PSD563 #define STBI_NO_PSD564 #endif565 #ifndef STBI_ONLY_TGA566 #define STBI_NO_TGA567 #endif568 #ifndef STBI_ONLY_GIF569 #define STBI_NO_GIF570 #endif571 #ifndef STBI_ONLY_HDR572 #define STBI_NO_HDR573 #endif574 #ifndef STBI_ONLY_PIC575 #define STBI_NO_PIC576 #endif577 #ifndef STBI_ONLY_PNM578 #define STBI_NO_PNM579 #endif580#endif581582#if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB)583#define STBI_NO_ZLIB584#endif585586587#include <stdarg.h>588#include <stddef.h> // ptrdiff_t on osx589#include <stdlib.h>590#include <string.h>591#include <limits.h>592593#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)594#include <math.h> // ldexp, pow595#endif596597#ifndef STBI_NO_STDIO598#include <stdio.h>599#endif600601#ifndef STBI_ASSERT602#include <assert.h>603#define STBI_ASSERT(x) assert(x)604#endif605606#ifdef __cplusplus607#define STBI_EXTERN extern "C"608#else609#define STBI_EXTERN extern610#endif611612613#ifndef _MSC_VER614 #ifdef __cplusplus615 #define stbi_inline inline616 #else617 #define stbi_inline618 #endif619#else620 #define stbi_inline __forceinline621#endif622623#ifndef STBI_NO_THREAD_LOCALS624 #if defined(__cplusplus) && __cplusplus >= 201103L625 #define STBI_THREAD_LOCAL thread_local626 #elif defined(__GNUC__) && __GNUC__ < 5627 #define STBI_THREAD_LOCAL __thread628 #elif defined(_MSC_VER)629 #define STBI_THREAD_LOCAL __declspec(thread)630 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)631 #define STBI_THREAD_LOCAL _Thread_local632 #endif633634 #ifndef STBI_THREAD_LOCAL635 #if defined(__GNUC__)636 #define STBI_THREAD_LOCAL __thread637 #endif638 #endif639#endif640641#if defined(_MSC_VER) || defined(__SYMBIAN32__)642typedef unsigned short stbi__uint16;643typedef signed short stbi__int16;644typedef unsigned int stbi__uint32;645typedef signed int stbi__int32;646#else647#include <stdint.h>648typedef uint16_t stbi__uint16;649typedef int16_t stbi__int16;650typedef uint32_t stbi__uint32;651typedef int32_t stbi__int32;652#endif653654// should produce compiler error if size is wrong655typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];656657#ifdef _MSC_VER658#define STBI_NOTUSED(v) (void)(v)659#else660#define STBI_NOTUSED(v) (void)sizeof(v)661#endif662663#ifdef _MSC_VER664#define STBI_HAS_LROTL665#endif666667#ifdef STBI_HAS_LROTL668 #define stbi_lrot(x,y) _lrotl(x,y)669#else670 #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (-(y) & 31)))671#endif672673#if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED))674// ok675#elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED)676// ok677#else678#error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)."679#endif680681#ifndef STBI_MALLOC682#define STBI_MALLOC(sz) malloc(sz)683#define STBI_REALLOC(p,newsz) realloc(p,newsz)684#define STBI_FREE(p) free(p)685#endif686687#ifndef STBI_REALLOC_SIZED688#define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz)689#endif690691// x86/x64 detection692#if defined(__x86_64__) || defined(_M_X64)693#define STBI__X64_TARGET694#elif defined(__i386) || defined(_M_IX86)695#define STBI__X86_TARGET696#endif697698#if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD)699// gcc doesn't support sse2 intrinsics unless you compile with -msse2,700// which in turn means it gets to use SSE2 everywhere. This is unfortunate,701// but previous attempts to provide the SSE2 functions with runtime702// detection caused numerous issues. The way architecture extensions are703// exposed in GCC/Clang is, sadly, not really suited for one-file libs.704// New behavior: if compiled with -msse2, we use SSE2 without any705// detection; if not, we don't use it at all.706#define STBI_NO_SIMD707#endif708709#if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD)710// Note that __MINGW32__ doesn't actually mean 32-bit, so we have to avoid STBI__X64_TARGET711//712// 32-bit MinGW wants ESP to be 16-byte aligned, but this is not in the713// Windows ABI and VC++ as well as Windows DLLs don't maintain that invariant.714// As a result, enabling SSE2 on 32-bit MinGW is dangerous when not715// simultaneously enabling "-mstackrealign".716//717// See https://github.com/nothings/stb/issues/81 for more information.718//719// So default to no SSE2 on 32-bit MinGW. If you've read this far and added720// -mstackrealign to your build settings, feel free to #define STBI_MINGW_ENABLE_SSE2.721#define STBI_NO_SIMD722#endif723724#if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET))725#define STBI_SSE2726#include <emmintrin.h>727728#ifdef _MSC_VER729730#if _MSC_VER >= 1400 // not VC6731#include <intrin.h> // __cpuid732static int stbi__cpuid3(void)733{734 int info[4];735 __cpuid(info,1);736 return info[3];737}738#else739static int stbi__cpuid3(void)740{741 int res;742 __asm {743 mov eax,1744 cpuid745 mov res,edx746 }747 return res;748}749#endif750751#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name752753#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)754static int stbi__sse2_available(void)755{756 int info3 = stbi__cpuid3();757 return ((info3 >> 26) & 1) != 0;758}759#endif760761#else // assume GCC-style if not VC++762#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))763764#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)765static int stbi__sse2_available(void)766{767 // If we're even attempting to compile this on GCC/Clang, that means768 // -msse2 is on, which means the compiler is allowed to use SSE2769 // instructions at will, and so are we.770 return 1;771}772#endif773774#endif775#endif776777// ARM NEON778#if defined(STBI_NO_SIMD) && defined(STBI_NEON)779#undef STBI_NEON780#endif781782#ifdef STBI_NEON783#include <arm_neon.h>784#ifdef _MSC_VER785#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name786#else787#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))788#endif789#endif790791#ifndef STBI_SIMD_ALIGN792#define STBI_SIMD_ALIGN(type, name) type name793#endif794795#ifndef STBI_MAX_DIMENSIONS796#define STBI_MAX_DIMENSIONS (1 << 24)797#endif798799///////////////////////////////////////////////800//801// stbi__context struct and start_xxx functions802803// stbi__context structure is our basic context used by all images, so it804// contains all the IO context, plus some basic image information805typedef struct806{807 stbi__uint32 img_x, img_y;808 int img_n, img_out_n;809810 stbi_io_callbacks io;811 void *io_user_data;812813 int read_from_callbacks;814 int buflen;815 stbi_uc buffer_start[128];816 int callback_already_read;817818 stbi_uc *img_buffer, *img_buffer_end;819 stbi_uc *img_buffer_original, *img_buffer_original_end;820} stbi__context;821822823static void stbi__refill_buffer(stbi__context *s);824825// initialize a memory-decode context826static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len)827{828 s->io.read = NULL;829 s->read_from_callbacks = 0;830 s->callback_already_read = 0;831 s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer;832 s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len;833}834835// initialize a callback-based context836static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user)837{838 s->io = *c;839 s->io_user_data = user;840 s->buflen = sizeof(s->buffer_start);841 s->read_from_callbacks = 1;842 s->callback_already_read = 0;843 s->img_buffer = s->img_buffer_original = s->buffer_start;844 stbi__refill_buffer(s);845 s->img_buffer_original_end = s->img_buffer_end;846}847848#ifndef STBI_NO_STDIO849850static int stbi__stdio_read(void *user, char *data, int size)851{852 return (int) fread(data,1,size,(FILE*) user);853}854855static void stbi__stdio_skip(void *user, int n)856{857 int ch;858 fseek((FILE*) user, n, SEEK_CUR);859 ch = fgetc((FILE*) user); /* have to read a byte to reset feof()'s flag */860 if (ch != EOF) {861 ungetc(ch, (FILE *) user); /* push byte back onto stream if valid. */862 }863}864865static int stbi__stdio_eof(void *user)866{867 return feof((FILE*) user) || ferror((FILE *) user);868}869870static stbi_io_callbacks stbi__stdio_callbacks =871{872 stbi__stdio_read,873 stbi__stdio_skip,874 stbi__stdio_eof,875};876877static void stbi__start_file(stbi__context *s, FILE *f)878{879 stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f);880}881882//static void stop_file(stbi__context *s) { }883884#endif // !STBI_NO_STDIO885886static void stbi__rewind(stbi__context *s)887{888 // conceptually rewind SHOULD rewind to the beginning of the stream,889 // but we just rewind to the beginning of the initial buffer, because890 // we only use it after doing 'test', which only ever looks at at most 92 bytes891 s->img_buffer = s->img_buffer_original;892 s->img_buffer_end = s->img_buffer_original_end;893}894895enum896{897 STBI_ORDER_RGB,898 STBI_ORDER_BGR899};900901typedef struct902{903 int bits_per_channel;904 int num_channels;905 int channel_order;906} stbi__result_info;907908#ifndef STBI_NO_JPEG909static int stbi__jpeg_test(stbi__context *s);910static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);911static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp);912#endif913914#ifndef STBI_NO_PNG915static int stbi__png_test(stbi__context *s);916static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);917static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp);918static int stbi__png_is16(stbi__context *s);919#endif920921#ifndef STBI_NO_BMP922static int stbi__bmp_test(stbi__context *s);923static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);924static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp);925#endif926927#ifndef STBI_NO_TGA928static int stbi__tga_test(stbi__context *s);929static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);930static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp);931#endif932933#ifndef STBI_NO_PSD934static int stbi__psd_test(stbi__context *s);935static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc);936static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp);937static int stbi__psd_is16(stbi__context *s);938#endif939940#ifndef STBI_NO_HDR941static int stbi__hdr_test(stbi__context *s);942static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);943static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp);944#endif945946#ifndef STBI_NO_PIC947static int stbi__pic_test(stbi__context *s);948static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);949static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp);950#endif951952#ifndef STBI_NO_GIF953static int stbi__gif_test(stbi__context *s);954static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);955static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp);956static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp);957#endif958959#ifndef STBI_NO_PNM960static int stbi__pnm_test(stbi__context *s);961static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);962static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp);963static int stbi__pnm_is16(stbi__context *s);964#endif965966static967#ifdef STBI_THREAD_LOCAL968STBI_THREAD_LOCAL969#endif970const char *stbi__g_failure_reason;971972STBIDEF const char *stbi_failure_reason(void)973{974 return stbi__g_failure_reason;975}976977#ifndef STBI_NO_FAILURE_STRINGS978static int stbi__err(const char *str)979{980 stbi__g_failure_reason = str;981 return 0;982}983#endif984985static void *stbi__malloc(size_t size)986{987 return STBI_MALLOC(size);988}989990// stb_image uses ints pervasively, including for offset calculations.991// therefore the largest decoded image size we can support with the992// current code, even on 64-bit targets, is INT_MAX. this is not a993// significant limitation for the intended use case.994//995// we do, however, need to make sure our size calculations don't996// overflow. hence a few helper functions for size calculations that997// multiply integers together, making sure that they're non-negative998// and no overflow occurs.9991000// return 1 if the sum is valid, 0 on overflow.1001// negative terms are considered invalid.1002static int stbi__addsizes_valid(int a, int b)1003{1004 if (b < 0) return 0;1005 // now 0 <= b <= INT_MAX, hence also1006 // 0 <= INT_MAX - b <= INTMAX.1007 // And "a + b <= INT_MAX" (which might overflow) is the1008 // same as a <= INT_MAX - b (no overflow)1009 return a <= INT_MAX - b;1010}10111012// returns 1 if the product is valid, 0 on overflow.1013// negative factors are considered invalid.1014static int stbi__mul2sizes_valid(int a, int b)1015{1016 if (a < 0 || b < 0) return 0;1017 if (b == 0) return 1; // mul-by-0 is always safe1018 // portable way to check for no overflows in a*b1019 return a <= INT_MAX/b;1020}10211022#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)1023// returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow1024static int stbi__mad2sizes_valid(int a, int b, int add)1025{1026 return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add);1027}1028#endif10291030// returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow1031static int stbi__mad3sizes_valid(int a, int b, int c, int add)1032{1033 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&1034 stbi__addsizes_valid(a*b*c, add);1035}10361037// returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow1038#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)1039static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)1040{1041 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&1042 stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add);1043}1044#endif10451046#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)1047// mallocs with size overflow checking1048static void *stbi__malloc_mad2(int a, int b, int add)1049{1050 if (!stbi__mad2sizes_valid(a, b, add)) return NULL;1051 return stbi__malloc(a*b + add);1052}1053#endif10541055static void *stbi__malloc_mad3(int a, int b, int c, int add)1056{1057 if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL;1058 return stbi__malloc(a*b*c + add);1059}10601061#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)1062static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)1063{1064 if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL;1065 return stbi__malloc(a*b*c*d + add);1066}1067#endif10681069// returns 1 if the sum of two signed ints is valid (between -2^31 and 2^31-1 inclusive), 0 on overflow.1070static int stbi__addints_valid(int a, int b)1071{1072 if ((a >= 0) != (b >= 0)) return 1; // a and b have different signs, so no overflow1073 if (a < 0 && b < 0) return a >= INT_MIN - b; // same as a + b >= INT_MIN; INT_MIN - b cannot overflow since b < 0.1074 return a <= INT_MAX - b;1075}10761077// returns 1 if the product of two ints fits in a signed short, 0 on overflow.1078static int stbi__mul2shorts_valid(int a, int b)1079{1080 if (b == 0 || b == -1) return 1; // multiplication by 0 is always 0; check for -1 so SHRT_MIN/b doesn't overflow1081 if ((a >= 0) == (b >= 0)) return a <= SHRT_MAX/b; // product is positive, so similar to mul2sizes_valid1082 if (b < 0) return a <= SHRT_MIN / b; // same as a * b >= SHRT_MIN1083 return a >= SHRT_MIN / b;1084}10851086// stbi__err - error1087// stbi__errpf - error returning pointer to float1088// stbi__errpuc - error returning pointer to unsigned char10891090#ifdef STBI_NO_FAILURE_STRINGS1091 #define stbi__err(x,y) 01092#elif defined(STBI_FAILURE_USERMSG)1093 #define stbi__err(x,y) stbi__err(y)1094#else1095 #define stbi__err(x,y) stbi__err(x)1096#endif10971098#define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL))1099#define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL))11001101STBIDEF void stbi_image_free(void *retval_from_stbi_load)1102{1103 STBI_FREE(retval_from_stbi_load);1104}11051106#ifndef STBI_NO_LINEAR1107static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp);1108#endif11091110#ifndef STBI_NO_HDR1111static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp);1112#endif11131114static int stbi__vertically_flip_on_load_global = 0;11151116STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)1117{1118 stbi__vertically_flip_on_load_global = flag_true_if_should_flip;1119}11201121#ifndef STBI_THREAD_LOCAL1122#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global1123#else1124static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set;11251126STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip)1127{1128 stbi__vertically_flip_on_load_local = flag_true_if_should_flip;1129 stbi__vertically_flip_on_load_set = 1;1130}11311132#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \1133 ? stbi__vertically_flip_on_load_local \1134 : stbi__vertically_flip_on_load_global)1135#endif // STBI_THREAD_LOCAL11361137static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)1138{1139 memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields1140 ri->bits_per_channel = 8; // default is 8 so most paths don't have to be changed1141 ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order1142 ri->num_channels = 0;11431144 // test the formats with a very explicit header first (at least a FOURCC1145 // or distinctive magic number first)1146 #ifndef STBI_NO_PNG1147 if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, ri);1148 #endif1149 #ifndef STBI_NO_BMP1150 if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp, ri);1151 #endif1152 #ifndef STBI_NO_GIF1153 if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp, ri);1154 #endif1155 #ifndef STBI_NO_PSD1156 if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc);1157 #else1158 STBI_NOTUSED(bpc);1159 #endif1160 #ifndef STBI_NO_PIC1161 if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri);1162 #endif11631164 // then the formats that can end up attempting to load with just 1 or 21165 // bytes matching expectations; these are prone to false positives, so1166 // try them later1167 #ifndef STBI_NO_JPEG1168 if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp, ri);1169 #endif1170 #ifndef STBI_NO_PNM1171 if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp, ri);1172 #endif11731174 #ifndef STBI_NO_HDR1175 if (stbi__hdr_test(s)) {1176 float *hdr = stbi__hdr_load(s, x,y,comp,req_comp, ri);1177 return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);1178 }1179 #endif11801181 #ifndef STBI_NO_TGA1182 // test tga last because it's a crappy test!1183 if (stbi__tga_test(s))1184 return stbi__tga_load(s,x,y,comp,req_comp, ri);1185 #endif11861187 return stbi__errpuc("unknown image type", "Image not of any known type, or corrupt");1188}11891190static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels)1191{1192 int i;1193 int img_len = w * h * channels;1194 stbi_uc *reduced;11951196 reduced = (stbi_uc *) stbi__malloc(img_len);1197 if (reduced == NULL) return stbi__errpuc("outofmem", "Out of memory");11981199 for (i = 0; i < img_len; ++i)1200 reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling12011202 STBI_FREE(orig);1203 return reduced;1204}12051206static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels)1207{1208 int i;1209 int img_len = w * h * channels;1210 stbi__uint16 *enlarged;12111212 enlarged = (stbi__uint16 *) stbi__malloc(img_len*2);1213 if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");12141215 for (i = 0; i < img_len; ++i)1216 enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff12171218 STBI_FREE(orig);1219 return enlarged;1220}12211222static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)1223{1224 int row;1225 size_t bytes_per_row = (size_t)w * bytes_per_pixel;1226 stbi_uc temp[2048];1227 stbi_uc *bytes = (stbi_uc *)image;12281229 for (row = 0; row < (h>>1); row++) {1230 stbi_uc *row0 = bytes + row*bytes_per_row;1231 stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row;1232 // swap row0 with row11233 size_t bytes_left = bytes_per_row;1234 while (bytes_left) {1235 size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp);1236 memcpy(temp, row0, bytes_copy);1237 memcpy(row0, row1, bytes_copy);1238 memcpy(row1, temp, bytes_copy);1239 row0 += bytes_copy;1240 row1 += bytes_copy;1241 bytes_left -= bytes_copy;1242 }1243 }1244}12451246#ifndef STBI_NO_GIF1247static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel)1248{1249 int slice;1250 int slice_size = w * h * bytes_per_pixel;12511252 stbi_uc *bytes = (stbi_uc *)image;1253 for (slice = 0; slice < z; ++slice) {1254 stbi__vertical_flip(bytes, w, h, bytes_per_pixel);1255 bytes += slice_size;1256 }1257}1258#endif12591260static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)1261{1262 stbi__result_info ri;1263 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8);12641265 if (result == NULL)1266 return NULL;12671268 // it is the responsibility of the loaders to make sure we get either 8 or 16 bit.1269 STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16);12701271 if (ri.bits_per_channel != 8) {1272 result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp);1273 ri.bits_per_channel = 8;1274 }12751276 // @TODO: move stbi__convert_format to here12771278 if (stbi__vertically_flip_on_load) {1279 int channels = req_comp ? req_comp : *comp;1280 stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc));1281 }12821283 return (unsigned char *) result;1284}12851286static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)1287{1288 stbi__result_info ri;1289 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16);12901291 if (result == NULL)1292 return NULL;12931294 // it is the responsibility of the loaders to make sure we get either 8 or 16 bit.1295 STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16);12961297 if (ri.bits_per_channel != 16) {1298 result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp);1299 ri.bits_per_channel = 16;1300 }13011302 // @TODO: move stbi__convert_format16 to here1303 // @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision13041305 if (stbi__vertically_flip_on_load) {1306 int channels = req_comp ? req_comp : *comp;1307 stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16));1308 }13091310 return (stbi__uint16 *) result;1311}13121313#if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR)1314static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp)1315{1316 if (stbi__vertically_flip_on_load && result != NULL) {1317 int channels = req_comp ? req_comp : *comp;1318 stbi__vertical_flip(result, *x, *y, channels * sizeof(float));1319 }1320}1321#endif13221323#ifndef STBI_NO_STDIO13241325#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)1326STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);1327STBI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);1328#endif13291330#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)1331STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)1332{1333 return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);1334}1335#endif13361337static FILE *stbi__fopen(char const *filename, char const *mode)1338{1339 FILE *f;1340#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)1341 wchar_t wMode[64];1342 wchar_t wFilename[1024];1343 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename)))1344 return 0;13451346 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode)))1347 return 0;13481349#if defined(_MSC_VER) && _MSC_VER >= 14001350 if (0 != _wfopen_s(&f, wFilename, wMode))1351 f = 0;1352#else1353 f = _wfopen(wFilename, wMode);1354#endif13551356#elif defined(_MSC_VER) && _MSC_VER >= 14001357 if (0 != fopen_s(&f, filename, mode))1358 f=0;1359#else1360 f = fopen(filename, mode);1361#endif1362 return f;1363}136413651366STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)1367{1368 FILE *f = stbi__fopen(filename, "rb");1369 unsigned char *result;1370 if (!f) return stbi__errpuc("can't fopen", "Unable to open file");1371 result = stbi_load_from_file(f,x,y,comp,req_comp);1372 fclose(f);1373 return result;1374}13751376STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)1377{1378 unsigned char *result;1379 stbi__context s;1380 stbi__start_file(&s,f);1381 result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);1382 if (result) {1383 // need to 'unget' all the characters in the IO buffer1384 fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);1385 }1386 return result;1387}13881389STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp, int req_comp)1390{1391 stbi__uint16 *result;1392 stbi__context s;1393 stbi__start_file(&s,f);1394 result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp);1395 if (result) {1396 // need to 'unget' all the characters in the IO buffer1397 fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);1398 }1399 return result;1400}14011402STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *comp, int req_comp)1403{1404 FILE *f = stbi__fopen(filename, "rb");1405 stbi__uint16 *result;1406 if (!f) return (stbi_us *) stbi__errpuc("can't fopen", "Unable to open file");1407 result = stbi_load_from_file_16(f,x,y,comp,req_comp);1408 fclose(f);1409 return result;1410}141114121413#endif //!STBI_NO_STDIO14141415STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels)1416{1417 stbi__context s;1418 stbi__start_mem(&s,buffer,len);1419 return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);1420}14211422STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels)1423{1424 stbi__context s;1425 stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user);1426 return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);1427}14281429STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)1430{1431 stbi__context s;1432 stbi__start_mem(&s,buffer,len);1433 return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);1434}14351436STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)1437{1438 stbi__context s;1439 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);1440 return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);1441}14421443#ifndef STBI_NO_GIF1444STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp)1445{1446 unsigned char *result;1447 stbi__context s;1448 stbi__start_mem(&s,buffer,len);14491450 result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);1451 if (stbi__vertically_flip_on_load) {1452 stbi__vertical_flip_slices( result, *x, *y, *z, *comp );1453 }14541455 return result;1456}1457#endif14581459#ifndef STBI_NO_LINEAR1460static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)1461{1462 unsigned char *data;1463 #ifndef STBI_NO_HDR1464 if (stbi__hdr_test(s)) {1465 stbi__result_info ri;1466 float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp, &ri);1467 if (hdr_data)1468 stbi__float_postprocess(hdr_data,x,y,comp,req_comp);1469 return hdr_data;1470 }1471 #endif1472 data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp);1473 if (data)1474 return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);1475 return stbi__errpf("unknown image type", "Image not of any known type, or corrupt");1476}14771478STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)1479{1480 stbi__context s;1481 stbi__start_mem(&s,buffer,len);1482 return stbi__loadf_main(&s,x,y,comp,req_comp);1483}14841485STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)1486{1487 stbi__context s;1488 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);1489 return stbi__loadf_main(&s,x,y,comp,req_comp);1490}14911492#ifndef STBI_NO_STDIO1493STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)1494{1495 float *result;1496 FILE *f = stbi__fopen(filename, "rb");1497 if (!f) return stbi__errpf("can't fopen", "Unable to open file");1498 result = stbi_loadf_from_file(f,x,y,comp,req_comp);1499 fclose(f);1500 return result;1501}15021503STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)1504{1505 stbi__context s;1506 stbi__start_file(&s,f);1507 return stbi__loadf_main(&s,x,y,comp,req_comp);1508}1509#endif // !STBI_NO_STDIO15101511#endif // !STBI_NO_LINEAR15121513// these is-hdr-or-not is defined independent of whether STBI_NO_LINEAR is1514// defined, for API simplicity; if STBI_NO_LINEAR is defined, it always1515// reports false!15161517STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)1518{1519 #ifndef STBI_NO_HDR1520 stbi__context s;1521 stbi__start_mem(&s,buffer,len);1522 return stbi__hdr_test(&s);1523 #else1524 STBI_NOTUSED(buffer);1525 STBI_NOTUSED(len);1526 return 0;1527 #endif1528}15291530#ifndef STBI_NO_STDIO1531STBIDEF int stbi_is_hdr (char const *filename)1532{1533 FILE *f = stbi__fopen(filename, "rb");1534 int result=0;1535 if (f) {1536 result = stbi_is_hdr_from_file(f);1537 fclose(f);1538 }1539 return result;1540}15411542STBIDEF int stbi_is_hdr_from_file(FILE *f)1543{1544 #ifndef STBI_NO_HDR1545 long pos = ftell(f);1546 int res;1547 stbi__context s;1548 stbi__start_file(&s,f);1549 res = stbi__hdr_test(&s);1550 fseek(f, pos, SEEK_SET);1551 return res;1552 #else1553 STBI_NOTUSED(f);1554 return 0;1555 #endif1556}1557#endif // !STBI_NO_STDIO15581559STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)1560{1561 #ifndef STBI_NO_HDR1562 stbi__context s;1563 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);1564 return stbi__hdr_test(&s);1565 #else1566 STBI_NOTUSED(clbk);1567 STBI_NOTUSED(user);1568 return 0;1569 #endif1570}15711572#ifndef STBI_NO_LINEAR1573static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f;15741575STBIDEF void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; }1576STBIDEF void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; }1577#endif15781579static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f;15801581STBIDEF void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; }1582STBIDEF void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; }158315841585//////////////////////////////////////////////////////////////////////////////1586//1587// Common code used by all image loaders1588//15891590enum1591{1592 STBI__SCAN_load=0,1593 STBI__SCAN_type,1594 STBI__SCAN_header1595};15961597static void stbi__refill_buffer(stbi__context *s)1598{1599 int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);1600 s->callback_already_read += (int) (s->img_buffer - s->img_buffer_original);1601 if (n == 0) {1602 // at end of file, treat same as if from memory, but need to handle case1603 // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file1604 s->read_from_callbacks = 0;1605 s->img_buffer = s->buffer_start;1606 s->img_buffer_end = s->buffer_start+1;1607 *s->img_buffer = 0;1608 } else {1609 s->img_buffer = s->buffer_start;1610 s->img_buffer_end = s->buffer_start + n;1611 }1612}16131614stbi_inline static stbi_uc stbi__get8(stbi__context *s)1615{1616 if (s->img_buffer < s->img_buffer_end)1617 return *s->img_buffer++;1618 if (s->read_from_callbacks) {1619 stbi__refill_buffer(s);1620 return *s->img_buffer++;1621 }1622 return 0;1623}16241625#if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)1626// nothing1627#else1628stbi_inline static int stbi__at_eof(stbi__context *s)1629{1630 if (s->io.read) {1631 if (!(s->io.eof)(s->io_user_data)) return 0;1632 // if feof() is true, check if buffer = end1633 // special case: we've only got the special 0 character at the end1634 if (s->read_from_callbacks == 0) return 1;1635 }16361637 return s->img_buffer >= s->img_buffer_end;1638}1639#endif16401641#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC)1642// nothing1643#else1644static void stbi__skip(stbi__context *s, int n)1645{1646 if (n == 0) return; // already there!1647 if (n < 0) {1648 s->img_buffer = s->img_buffer_end;1649 return;1650 }1651 if (s->io.read) {1652 int blen = (int) (s->img_buffer_end - s->img_buffer);1653 if (blen < n) {1654 s->img_buffer = s->img_buffer_end;1655 (s->io.skip)(s->io_user_data, n - blen);1656 return;1657 }1658 }1659 s->img_buffer += n;1660}1661#endif16621663#if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM)1664// nothing1665#else1666static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)1667{1668 if (s->io.read) {1669 int blen = (int) (s->img_buffer_end - s->img_buffer);1670 if (blen < n) {1671 int res, count;16721673 memcpy(buffer, s->img_buffer, blen);16741675 count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen);1676 res = (count == (n-blen));1677 s->img_buffer = s->img_buffer_end;1678 return res;1679 }1680 }16811682 if (s->img_buffer+n <= s->img_buffer_end) {1683 memcpy(buffer, s->img_buffer, n);1684 s->img_buffer += n;1685 return 1;1686 } else1687 return 0;1688}1689#endif16901691#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)1692// nothing1693#else1694static int stbi__get16be(stbi__context *s)1695{1696 int z = stbi__get8(s);1697 return (z << 8) + stbi__get8(s);1698}1699#endif17001701#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)1702// nothing1703#else1704static stbi__uint32 stbi__get32be(stbi__context *s)1705{1706 stbi__uint32 z = stbi__get16be(s);1707 return (z << 16) + stbi__get16be(s);1708}1709#endif17101711#if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF)1712// nothing1713#else1714static int stbi__get16le(stbi__context *s)1715{1716 int z = stbi__get8(s);1717 return z + (stbi__get8(s) << 8);1718}1719#endif17201721#ifndef STBI_NO_BMP1722static stbi__uint32 stbi__get32le(stbi__context *s)1723{1724 stbi__uint32 z = stbi__get16le(s);1725 z += (stbi__uint32)stbi__get16le(s) << 16;1726 return z;1727}1728#endif17291730#define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings17311732#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)1733// nothing1734#else1735//////////////////////////////////////////////////////////////////////////////1736//1737// generic converter from built-in img_n to req_comp1738// individual types do this automatically as much as possible (e.g. jpeg1739// does all cases internally since it needs to colorspace convert anyway,1740// and it never has alpha, so very few cases ). png can automatically1741// interleave an alpha=255 channel, but falls back to this for other cases1742//1743// assume data buffer is malloced, so malloc a new one and free that one1744// only failure mode is malloc failing17451746static stbi_uc stbi__compute_y(int r, int g, int b)1747{1748 return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8);1749}1750#endif17511752#if defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)1753// nothing1754#else1755static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)1756{1757 int i,j;1758 unsigned char *good;17591760 if (req_comp == img_n) return data;1761 STBI_ASSERT(req_comp >= 1 && req_comp <= 4);17621763 good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0);1764 if (good == NULL) {1765 STBI_FREE(data);1766 return stbi__errpuc("outofmem", "Out of memory");1767 }17681769 for (j=0; j < (int) y; ++j) {1770 unsigned char *src = data + j * x * img_n ;1771 unsigned char *dest = good + j * x * req_comp;17721773 #define STBI__COMBO(a,b) ((a)*8+(b))1774 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)1775 // convert source image with img_n components to one with req_comp components;1776 // avoid switch per pixel, so use switch per scanline and massive macros1777 switch (STBI__COMBO(img_n, req_comp)) {1778 STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break;1779 STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;1780 STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break;1781 STBI__CASE(2,1) { dest[0]=src[0]; } break;1782 STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;1783 STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;1784 STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break;1785 STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;1786 STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break;1787 STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;1788 STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break;1789 STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;1790 default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return stbi__errpuc("unsupported", "Unsupported format conversion");1791 }1792 #undef STBI__CASE1793 }17941795 STBI_FREE(data);1796 return good;1797}1798#endif17991800#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)1801// nothing1802#else1803static stbi__uint16 stbi__compute_y_16(int r, int g, int b)1804{1805 return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8);1806}1807#endif18081809#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)1810// nothing1811#else1812static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y)1813{1814 int i,j;1815 stbi__uint16 *good;18161817 if (req_comp == img_n) return data;1818 STBI_ASSERT(req_comp >= 1 && req_comp <= 4);18191820 good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2);1821 if (good == NULL) {1822 STBI_FREE(data);1823 return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");1824 }18251826 for (j=0; j < (int) y; ++j) {1827 stbi__uint16 *src = data + j * x * img_n ;1828 stbi__uint16 *dest = good + j * x * req_comp;18291830 #define STBI__COMBO(a,b) ((a)*8+(b))1831 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)1832 // convert source image with img_n components to one with req_comp components;1833 // avoid switch per pixel, so use switch per scanline and massive macros1834 switch (STBI__COMBO(img_n, req_comp)) {1835 STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break;1836 STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;1837 STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break;1838 STBI__CASE(2,1) { dest[0]=src[0]; } break;1839 STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;1840 STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;1841 STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break;1842 STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;1843 STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break;1844 STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;1845 STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break;1846 STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;1847 default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return (stbi__uint16*) stbi__errpuc("unsupported", "Unsupported format conversion");1848 }1849 #undef STBI__CASE1850 }18511852 STBI_FREE(data);1853 return good;1854}1855#endif18561857#ifndef STBI_NO_LINEAR1858static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)1859{1860 int i,k,n;1861 float *output;1862 if (!data) return NULL;1863 output = (float *) stbi__malloc_mad4(x, y, comp, sizeof(float), 0);1864 if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem", "Out of memory"); }1865 // compute number of non-alpha components1866 if (comp & 1) n = comp; else n = comp-1;1867 for (i=0; i < x*y; ++i) {1868 for (k=0; k < n; ++k) {1869 output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);1870 }1871 }1872 if (n < comp) {1873 for (i=0; i < x*y; ++i) {1874 output[i*comp + n] = data[i*comp + n]/255.0f;1875 }1876 }1877 STBI_FREE(data);1878 return output;1879}1880#endif18811882#ifndef STBI_NO_HDR1883#define stbi__float2int(x) ((int) (x))1884static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp)1885{1886 int i,k,n;1887 stbi_uc *output;1888 if (!data) return NULL;1889 output = (stbi_uc *) stbi__malloc_mad3(x, y, comp, 0);1890 if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem", "Out of memory"); }1891 // compute number of non-alpha components1892 if (comp & 1) n = comp; else n = comp-1;1893 for (i=0; i < x*y; ++i) {1894 for (k=0; k < n; ++k) {1895 float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f;1896 if (z < 0) z = 0;1897 if (z > 255) z = 255;1898 output[i*comp + k] = (stbi_uc) stbi__float2int(z);1899 }1900 if (k < comp) {1901 float z = data[i*comp+k] * 255 + 0.5f;1902 if (z < 0) z = 0;1903 if (z > 255) z = 255;1904 output[i*comp + k] = (stbi_uc) stbi__float2int(z);1905 }1906 }1907 STBI_FREE(data);1908 return output;1909}1910#endif19111912//////////////////////////////////////////////////////////////////////////////1913//1914// "baseline" JPEG/JFIF decoder1915//1916// simple implementation1917// - doesn't support delayed output of y-dimension1918// - simple interface (only one output format: 8-bit interleaved RGB)1919// - doesn't try to recover corrupt jpegs1920// - doesn't allow partial loading, loading multiple at once1921// - still fast on x86 (copying globals into locals doesn't help x86)1922// - allocates lots of intermediate memory (full size of all components)1923// - non-interleaved case requires this anyway1924// - allows good upsampling (see next)1925// high-quality1926// - upsampled channels are bilinearly interpolated, even across blocks1927// - quality integer IDCT derived from IJG's 'slow'1928// performance1929// - fast huffman; reasonable integer IDCT1930// - some SIMD kernels for common paths on targets with SSE2/NEON1931// - uses a lot of intermediate memory, could cache poorly19321933#ifndef STBI_NO_JPEG19341935// huffman decoding acceleration1936#define FAST_BITS 9 // larger handles more cases; smaller stomps less cache19371938typedef struct1939{1940 stbi_uc fast[1 << FAST_BITS];1941 // weirdly, repacking this into AoS is a 10% speed loss, instead of a win1942 stbi__uint16 code[256];1943 stbi_uc values[256];1944 stbi_uc size[257];1945 unsigned int maxcode[18];1946 int delta[17]; // old 'firstsymbol' - old 'firstcode'1947} stbi__huffman;19481949typedef struct1950{1951 stbi__context *s;1952 stbi__huffman huff_dc[4];1953 stbi__huffman huff_ac[4];1954 stbi__uint16 dequant[4][64];1955 stbi__int16 fast_ac[4][1 << FAST_BITS];19561957// sizes for components, interleaved MCUs1958 int img_h_max, img_v_max;1959 int img_mcu_x, img_mcu_y;1960 int img_mcu_w, img_mcu_h;19611962// definition of jpeg image component1963 struct1964 {1965 int id;1966 int h,v;1967 int tq;1968 int hd,ha;1969 int dc_pred;19701971 int x,y,w2,h2;1972 stbi_uc *data;1973 void *raw_data, *raw_coeff;1974 stbi_uc *linebuf;1975 short *coeff; // progressive only1976 int coeff_w, coeff_h; // number of 8x8 coefficient blocks1977 } img_comp[4];19781979 stbi__uint32 code_buffer; // jpeg entropy-coded buffer1980 int code_bits; // number of valid bits1981 unsigned char marker; // marker seen while filling entropy buffer1982 int nomore; // flag if we saw a marker so must stop19831984 int progressive;1985 int spec_start;1986 int spec_end;1987 int succ_high;1988 int succ_low;1989 int eob_run;1990 int jfif;1991 int app14_color_transform; // Adobe APP14 tag1992 int rgb;19931994 int scan_n, order[4];1995 int restart_interval, todo;19961997// kernels1998 void (*idct_block_kernel)(stbi_uc *out, int out_stride, short data[64]);1999 void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step);2000 stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs);The first 2,000 of 7,988 lines.