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
The walk stopped after 200 commits: lines older than aeec66c point at it.
Merge branch 'resize_latest' of https://github.com/jeffrbig2/stb into workingSean Barrettbefore adoption8503b88
line 1why, one step back1/* stb_image - v2.30 - public domain image loader - http://nothings.org/stbMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 2–50why, one step back2 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:50Merge branch 'resize_latest' of https://github.com/jeffrbig2/stb into workingSean Barrettbefore adoption8503b88
line 51why, one step back51 2.30 (2024-05-31) avoid erroneous gcc warningMerge branch 'master' into workingSean Barrettbefore adoption6e9f34d
lines 52–54why, one step back52 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 fixesupdate version numbersSean Barrettbefore adoption314d0a6
line 55why, one step back55 2.26 (2020-07-13) many minor fixesstb_image: fix new warningsSean Barrettbefore adoption0224a44
line 56why, one step back56 2.25 (2020-02-02) fix warningsudpate version numbersSean Barrettbefore adoption5e4a061
line 57why, one step back57 2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_verticallyUpdate version numbersSean Barrettbefore adoption787f1d6
line 58why, one step back58 2.23 (2019-08-11) fix clang static analysis warningupdate version numbersSean Barrettbefore adoption2c2908f
line 59why, one step back59 2.22 (2019-03-04) gif fixes, fix warningsupdate version numbersSean Barrettbefore adoptionc963e40
line 60why, one step back60 2.21 (2019-02-25) fix typo in commentFix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 61why, one step back61 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefsMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 62–95why, one step back62 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)Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
line 96why, one step back96 Arseny Kapoulkine Simon Breuss (16-bit PNM)Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 97–100why, one step back97 John-Mark Allen98 Carmelo J Fdez-Aguera99100 Bug & warning fixesMerge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
lines 101–102why, one step back101 Marc LeBlanc David Woo Guillaume George Martins Mozeiko102 Christpher Lloyd Jerry Jansson Joseph Thomson Blazej Dariusz Roszkowskiremove trailign whitespaceSean Barrettbefore adoptione140649
line 103why, one step back103 Phil Jordan Dave Moore Roy ElthamMerge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
line 104why, one step back104 Hayaki Saito Nathan Reed Won Chunremove trailign whitespaceSean Barrettbefore adoptione140649
lines 105–108why, one step back105 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:svdijkMerge branch 'dev' into workingSean Barrettbefore adoptionc497f72
line 109why, one step back109 Eugene Golushkov Laurent Gomila Cort Stratton github:snagarremove trailign whitespaceSean Barrettbefore adoptione140649
lines 110–113why, one step back110 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:phprusMerge branch 'master' into workingSean Barrettbefore adoption6e9f34d
line 114why, one step back114 Josh Tobin Neil Bickford Matthew Gregan github:poppolopopporemove trailign whitespaceSean Barrettbefore adoptione140649
lines 115–116why, one step back115 Julian Raschke Gregory Mullen Christian Floisand github:darealshinji116 Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
line 117why, one step back117 Brad Weinberger Matvey Cherevko github:mosraMerge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
lines 118–119why, one step back118 Luca Sas Alexander Veselov Zack Middleton [reserved]119 Ryan C. Gordon [reserved] [reserved]remove trailign whitespaceSean Barrettbefore adoptione140649
line 120why, one step back120 DO NOT ADD YOUR NAME HEREMerge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
line 121why, one step back121Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
lines 122–123why, one step back122 Jacko Dirks123Merge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
lines 124–126why, one step back124 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.Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 127–145why, one step back127*/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 0Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
line 146why, one step back146// stbi_image_free(data);Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 147–184why, one step back147//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//Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
lines 185–210why, one step back185// 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//Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 211–315why, one step back211// ===========================================================================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//Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
lines 316–319why, one step back316// 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).Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 320–360why, one step back320//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//Merge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
lines 361–368why, one step back361// - 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.Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 369–393why, one step back369370#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#endif393Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 394why, one step back394#ifndef STBIDEFMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 395–399why, one step back395#ifdef STB_IMAGE_STATIC396#define STBIDEF static397#else398#define STBIDEF extern399#endifMerge branch 'master' into workingSean Barrettbefore adoptiond940053
line 400why, one step back400#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 401–486why, one step back401402//////////////////////////////////////////////////////////////////////////////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 failureudpate version numbersSean Barrettbefore adoption5e4a061
line 487why, one step back487// on most compilers (and ALL modern mainstream compilers) this is threadsafeMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 488–518why, one step back488STBIDEF 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);udpate version numbersSean Barrettbefore adoption5e4a061
line 519why, one step back519stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 520why, one step back520// as above, but only applies to images loaded on the thread that calls the functionudpate version numbersSean Barrettbefore adoption5e4a061
lines 521–522why, one step back521// this function is only available if your compiler supports thread-local variables;522// calling it will fail to link if your compiler doesn'tMerge branch 'master' into workingSean Barrettbefore adoption6e9f34d
lines 523–524why, one step back523STBIDEF 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);stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 525why, one step back525STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip);Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 526–622why, one step back526527// 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#endif622stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 623why, one step back623#ifndef STBI_NO_THREAD_LOCALSstb_image: fix new warningsSean Barrettbefore adoption0224a44
line 624why, one step back624 #if defined(__cplusplus) && __cplusplus >= 201103Lstb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 625why, one step back625 #define STBI_THREAD_LOCAL thread_localMerge branch 'master' of https://github.com/recp/stb into workingSean Barrettbefore adoption589a678
line 626why, one step back626 #elif defined(__GNUC__) && __GNUC__ < 5stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 627why, one step back627 #define STBI_THREAD_LOCAL __threadudpate version numbersSean Barrettbefore adoption5e4a061
line 628why, one step back628 #elif defined(_MSC_VER)stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 629why, one step back629 #define STBI_THREAD_LOCAL __declspec(thread)Merge branch 'master' of https://github.com/recp/stb into workingSean Barrettbefore adoption589a678
lines 630–637why, one step back630 #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 #endiftweak indentationSean Barrettbefore adoption802a1df
line 638why, one step back638 #endifstb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 639why, one step back639#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
line 640why, one step back640Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
line 641why, one step back641#if defined(_MSC_VER) || defined(__SYMBIAN32__)Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 642–669why, one step back642typedef 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#elseMerge branch 'dev' into workingSean Barrettbefore adoptionc497f72
line 670why, one step back670 #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (-(y) & 31)))Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 671–783why, one step back671#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>Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
lines 784–786why, one step back784#ifdef _MSC_VER785#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name786#elseMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 787–788why, one step back787#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))788#endifMerge branch 'dev' into workingSean Barrettbefore adoptionc497f72
line 789why, one step back789#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 790–794why, one step back790791#ifndef STBI_SIMD_ALIGN792#define STBI_SIMD_ALIGN(type, name) type name793#endif794Merge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
lines 795–798why, one step back795#ifndef STBI_MAX_DIMENSIONS796#define STBI_MAX_DIMENSIONS (1 << 24)797#endif798Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 799–815why, one step back799///////////////////////////////////////////////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];Merge branch 'bmp-assert' of https://github.com/zturtleman/stb into workingSean Barrettbefore adoptionfd9c3ea
line 816why, one step back816 int callback_already_read;Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 817–829why, one step back817818 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;Merge branch 'bmp-assert' of https://github.com/zturtleman/stb into workingSean Barrettbefore adoptionfd9c3ea
line 830why, one step back830 s->callback_already_read = 0;Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 831–841why, one step back831 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;Merge branch 'bmp-assert' of https://github.com/zturtleman/stb into workingSean Barrettbefore adoptionfd9c3ea
lines 842–843why, one step back842 s->callback_already_read = 0;843 s->img_buffer = s->img_buffer_original = s->buffer_start;Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 844–856why, one step back844 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{Merge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
line 857why, one step back857 int ch;Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
line 858why, one step back858 fseek((FILE*) user, n, SEEK_CUR);Merge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
lines 859–862why, one step back859 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 }Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 863–866why, one step back863}864865static int stbi__stdio_eof(void *user)866{Merge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
line 867why, one step back867 return feof((FILE*) user) || ferror((FILE *) user);Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 868–962why, one step back868}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);Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
line 963why, one step back963static int stbi__pnm_is16(stbi__context *s);Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 964–965why, one step back964#endif965Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 966why, one step back966staticstb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
lines 967–970why, one step back967#ifdef STBI_THREAD_LOCAL968STBI_THREAD_LOCAL969#endif970const char *stbi__g_failure_reason;Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 971–976why, one step back971972STBIDEF const char *stbi_failure_reason(void)973{974 return stbi__g_failure_reason;975}976Merge branch 'fix_issue_746' of https://github.com/BlackMATov/stb into work2Sean Barrettbefore adoption6877b1a
line 977why, one step back977#ifndef STBI_NO_FAILURE_STRINGSMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 978–982why, one step back978static int stbi__err(const char *str)979{980 stbi__g_failure_reason = str;981 return 0;982}Merge branch 'fix_issue_746' of https://github.com/BlackMATov/stb into work2Sean Barrettbefore adoption6877b1a
line 983why, one step back983#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 984–1021why, one step back984985static 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}1021Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1022why, one step back1022#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1023–1027why, one step back1023// 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}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1028why, one step back1028#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1029–1037why, one step back10291030// 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 overflowMerge branch 'master' into workingSean Barrettbefore adoption6e9f34d
line 1038why, one step back1038#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1039–1045why, one step back1039static 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#endif1045Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1046why, one step back1046#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1047–1052why, one step back1047// 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}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1053why, one step back1053#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1054–1060why, one step back10541055static 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}1060Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
line 1061why, one step back1061#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM)Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1062–1068why, one step back1062static 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#endif1068Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
lines 1069–1085why, one step back1069// 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}1085Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1086–1113why, one step back1086// 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#endif1113stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 1114why, one step back1114static int stbi__vertically_flip_on_load_global = 0;Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1115–1117why, one step back11151116STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)1117{stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 1118why, one step back1118 stbi__vertically_flip_on_load_global = flag_true_if_should_flip;Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
line 1119why, one step back1119}stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
lines 1120–1135why, one step back11201121#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_LOCALMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1136–1143why, one step back11361137static 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;1143Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
lines 1144–1145why, one step back1144 // test the formats with a very explicit header first (at least a FOURCC1145 // or distinctive magic number first)Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1146–1156why, one step back1146 #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);Merge branch 'fix_issue_745' of https://github.com/BlackMATov/stb into work2Sean Barrettbefore adoption81d1537
lines 1157–1158why, one step back1157 #else1158 STBI_NOTUSED(bpc);Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1159–1162why, one step back1159 #endif1160 #ifndef STBI_NO_PIC1161 if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri);1162 #endifMerge branch 'dev' into workingSean Barrettbefore adoptionc497f72
lines 1163–1169why, one step back11631164 // 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 #endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1170–1253why, one step back1170 #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) {Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
lines 1254–1255why, one step back1254 stbi__vertical_flip(bytes, w, h, bytes_per_pixel);1255 bytes += slice_size;Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1256–1267why, one step back1256 }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;1267Merge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
lines 1268–1270why, one step back1268 // 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);1270Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1271–1293why, one step back1271 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;1293Merge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
lines 1294–1296why, one step back1294 // 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);1296Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1297–1324why, one step back1297 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_STDIO1324Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
line 1325why, one step back1325#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1326–1329why, one step back1326STBI_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#endif1329Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
line 1330why, one step back1330#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1331–1332why, one step back1331STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)1332{Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1333why, one step back1333 return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1334–1339why, one step back1334}1335#endif13361337static FILE *stbi__fopen(char const *filename, char const *mode)1338{1339 FILE *f;Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
line 1340why, one step back1340#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8)Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1341–1342why, one step back1341 wchar_t wMode[64];1342 wchar_t wFilename[1024];Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
line 1343why, one step back1343 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename)))Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
line 1344why, one step back1344 return 0;Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 1345why, one step back1345Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
line 1346why, one step back1346 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode)))Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1347–1348why, one step back1347 return 0;1348Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
line 1349why, one step back1349#if defined(_MSC_VER) && _MSC_VER >= 1400Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1350–1446why, one step back1350 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;Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
lines 1447–1449why, one step back1447 stbi__context s;1448 stbi__start_mem(&s,buffer,len);1449Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1450–1451why, one step back1450 result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);1451 if (stbi__vertically_flip_on_load) {Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 1452why, one step back1452 stbi__vertical_flip_slices( result, *x, *y, *z, *comp );Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1453–1454why, one step back1453 }1454Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 1455why, one step back1455 return result;Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1456–1599why, one step back1456}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);Merge branch 'bmp-assert' of https://github.com/zturtleman/stb into workingSean Barrettbefore adoptionfd9c3ea
line 1600why, one step back1600 s->callback_already_read += (int) (s->img_buffer - s->img_buffer_original);Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1601–1624why, one step back1601 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}1624Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1625–1627why, one step back1625#if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)1626// nothing1627#elseMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1628–1638why, one step back1628stbi_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}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1639why, one step back1639#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
line 1640why, one step back1640Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1641–1643why, one step back1641#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#elseMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1644–1645why, one step back1644static void stbi__skip(stbi__context *s, int n)1645{Merge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
line 1646why, one step back1646 if (n == 0) return; // already there!Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1647–1660why, one step back1647 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}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1661why, one step back1661#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
line 1662why, one step back1662Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1663–1665why, one step back1663#if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM)1664// nothing1665#elseMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1666–1688why, one step back1666static 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}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1689why, one step back1689#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
line 1690why, one step back1690Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1691–1693why, one step back1691#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)1692// nothing1693#elseMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1694–1698why, one step back1694static int stbi__get16be(stbi__context *s)1695{1696 int z = stbi__get8(s);1697 return (z << 8) + stbi__get8(s);1698}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1699why, one step back1699#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
line 1700why, one step back1700Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1701–1703why, one step back1701#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)1702// nothing1703#elseMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1704–1708why, one step back1704static stbi__uint32 stbi__get32be(stbi__context *s)1705{1706 stbi__uint32 z = stbi__get16be(s);1707 return (z << 16) + stbi__get16be(s);1708}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1709why, one step back1709#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1710–1724why, one step back17101711#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);Merge branch 'dev' into workingSean Barrettbefore adoptionc497f72
lines 1725–1726why, one step back1725 z += (stbi__uint32)stbi__get16le(s) << 16;1726 return z;Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1727–1731why, one step back1727}1728#endif17291730#define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings1731Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1732–1734why, one step back1732#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#elseMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1735–1749why, one step back1735//////////////////////////////////////////////////////////////////////////////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}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1750why, one step back1750#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
line 1751why, one step back1751Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1752–1754why, one step back1752#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#elseMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1755–1789why, one step back1755static 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;Merge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
line 1790why, one step back1790 default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return stbi__errpuc("unsupported", "Unsupported format conversion");Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1791–1797why, one step back1791 }1792 #undef STBI__CASE1793 }17941795 STBI_FREE(data);1796 return good;1797}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1798why, one step back1798#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
line 1799why, one step back1799Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1800–1802why, one step back1800#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)1801// nothing1802#elseMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1803–1806why, one step back1803static 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}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1807why, one step back1807#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
line 1808why, one step back1808Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1809–1811why, one step back1809#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)1810// nothing1811#elseMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1812–1846why, one step back1812static 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;Merge branch 'stb-image-fuzzing-fixes' of https://github.com/rcgordon/stb into workingSean Barrettbefore adoptionbfaccab
line 1847why, one step back1847 default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return (stbi__uint16*) stbi__errpuc("unsupported", "Unsupported format conversion");Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1848–1854why, one step back1848 }1849 #undef STBI__CASE1850 }18511852 STBI_FREE(data);1853 return good;1854}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1855why, one step back1855#endifMerge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c
lines 1856–2007why, one step back18561857#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.