stb_image.h
This file
Path
stb_image.h
7,658 lines · 261.1 KiB · C
Last changed by
Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
History of this fileTake it
The walk stopped after 200 commits: lines older than c79fa78 point at it.
Update version numbersSean Barrettbefore adoption787f1d6
line 1why, one step back1/* stb_image - v2.23 - public domain image loader - http://nothings.org/stbstb_dxt: fix bug with constant color & varying alphaSean Barrettbefore adoptiond808adb
line 2why, one step back2 no warranty implied; use at your own risktweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 3–50why, one step back34 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:50udpate version numbersSean Barrettbefore adoption5e4a061
line 51why, one step back51 2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_verticallyUpdate version numbersSean Barrettbefore adoption787f1d6
line 52why, one step back52 2.23 (2019-08-11) fix clang static analysis warningupdate version numbersSean Barrettbefore adoption2c2908f
line 53why, one step back53 2.22 (2019-03-04) gif fixes, fix warningsupdate version numbersSean Barrettbefore adoptionc963e40
line 54why, one step back54 2.21 (2019-02-25) fix typo in commentFix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 55why, one step back55 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefsMerge branch 'working'Sean Barrettbefore adoption19850aa
line 56why, one step back56 2.19 (2018-02-11) fix warningstb_image: fix warningsSean Barrettbefore adoptionccee11a
line 57why, one step back57 2.18 (2018-01-30) fix warningsMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 58–59why, one step back58 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings59 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixesdocsSean Barrettbefore adoptiond795785
line 60why, one step back60 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCCtweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 61–84why, one step back61 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs62 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes63 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes64 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x6465 RGB-format JPEG; remove white matting in PSD;66 allocate large structures on the stack;67 correct channel count for PNG & BMP68 2.10 (2016-01-22) avoid warning introduced in 2.0969 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED7071 See end of file for full revision history.727374 ============================ Contributors =========================7576 Image formats Extensions, features77 Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info)78 Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info)79 Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG)80 Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks)81 Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG)82 Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip)83 Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD)84 github:urraka (animated gif) Junggon Kim (PNM comments)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 85why, one step back85 Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA)tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 86–87why, one step back86 socks-the-fox (16-bit PNG)87 Jeremy Sawicki (handle all ImageNet JPGs)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 88–89why, one step back88 Optimizations & bugfixes Mikhail Morozov (1-bit BMP)89 Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)tweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 90why, one step back90 Arseny KapoulkineMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 91why, one step back91 John-Mark Allenstb_image: remove non-ASCII character from creditsSean Barrettbefore adoption694b61f
line 92why, one step back92 Carmelo J Fdez-Agueratweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 93–94why, one step back9394 Bug & warning fixesMerge branch 'master' of https://github.com/Try/stb into work2Sean Barrettbefore adoption1586318
line 95why, one step back95 Marc LeBlanc David Woo Guillaume George Martins Mozeiko Alexander Veselovreorganize contributor list (removing one redundant name and adding one new one as well)Sean Barrettbefore adoption56a61e1
lines 96–98why, one step back96 Christpher Lloyd Jerry Jansson Joseph Thomson Phil Jordan97 Dave Moore Roy Eltham Hayaki Saito Nathan Reed98 Won Chun Luke Graham Johan Duparc Nick VerigakisMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 99–107why, one step back99 the Horde3D community Thomas Ruf Ronny Chevalier github:rlyeh100 Janez Zemva John Bartholomew Michal Cichon github:romigrou101 Jonathan Blow Ken Hamada Tero Hanninen github:svdijk102 Laurent Gomila Cort Stratton Sergio Gonzalez github:snagar103 Aruelien Pocheville Thibault Reuille Cass Everitt github:Zelex104 Ryamond Barbiero Paul Du Bois Engin Manap github:grim210105 Aldo Culquicondor Philipp Wiesemann Dale Weiler github:sammyhw106 Oriol Ferrer Mesia Josh Tobin Matthew Gregan github:phprus107 Julian Raschke Gregory Mullen Baldur Karlsson github:poppolopoppowindows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
line 108why, one step back108 Christian Floisand Kevin Schmidt JR Smith github:darealshinjiMerge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 109–110why, one step back109 Brad Weinberger Matvey Cherevko github:Michaelangel007110 Blazej Dariusz RoszkowskiFix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 111why, one step back111tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 112–144why, one step back112*/113114#ifndef STBI_INCLUDE_STB_IMAGE_H115#define STBI_INCLUDE_STB_IMAGE_H116117// DOCUMENTATION118//119// Limitations:120// - no 12-bit-per-channel JPEG121// - no JPEGs with arithmetic coding122// - GIF always returns *comp=4123//124// Basic usage (see HDR discussion below for HDR usage):125// int x,y,n;126// unsigned char *data = stbi_load(filename, &x, &y, &n, 0);127// // ... process data if not NULL ...128// // ... x = width, y = height, n = # 8-bit components per pixel ...129// // ... replace '0' with '1'..'4' to force that many components per pixel130// // ... but 'n' will always be the number that it would have been if you said 0131// stbi_image_free(data)132//133// Standard parameters:134// int *x -- outputs image width in pixels135// int *y -- outputs image height in pixels136// int *channels_in_file -- outputs # of image components in image file137// int desired_channels -- if non-zero, # of image components requested in result138//139// The return value from an image loader is an 'unsigned char *' which points140// to the pixel data, or NULL on an allocation failure or if the image is141// corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,142// with each pixel consisting of N interleaved 8-bit components; the first143// pixel pointed to is top-left-most in the image. There is no padding between144// image scanlines or between pixels, regardless of format. The number ofMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 145–150why, one step back145// components N is 'desired_channels' if desired_channels is non-zero, or146// *channels_in_file otherwise. If desired_channels is non-zero,147// *channels_in_file has the number of components that _would_ have been148// output otherwise. E.g. if you set desired_channels to 4, you will always149// get RGBA output, but you can check *channels_in_file to see if it's trivially150// opaque because e.g. there were only 3 channels in the source image.tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 151–161why, one step back151//152// An output image with N components has the following components interleaved153// in this order in each pixel:154//155// N=#comp components156// 1 grey157// 2 grey, alpha158// 3 red, green, blue159// 4 red, green, blue, alpha160//161// If image loading fails for any reason, the return value will be NULL,Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 162–165why, one step back162// and *x, *y, *channels_in_file will be unchanged. The function163// stbi_failure_reason() can be queried for an extremely brief, end-user164// unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS165// to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightlytweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 166–171why, one step back166// more user-friendly ones.167//168// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.169//170// ===========================================================================171//windows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
lines 172–176why, one step back172// UNICODE:173//174// If compiling for Windows and you wish to use Unicode filenames, compile175// with176// #define STBI_WINDOWS_UTF8various fixes for clangSean Barrettbefore adoption5fe7fb5
line 177why, one step back177// and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convertwindows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
lines 178–181why, one step back178// Windows wchar_t filenames to utf8.179//180// ===========================================================================181//tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 182–191why, one step back182// Philosophy183//184// stb libraries are designed with the following priorities:185//186// 1. easy to use187// 2. easy to maintain188// 3. good performance189//190// Sometimes I let "good performance" creep up in priority over "easy to maintain",191// and for best performance I may provide less-easy-to-use APIs that give higherwindows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
line 192why, one step back192// performance, in addition to the easy-to-use ones. Nevertheless, it's importanttweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 193–196why, one step back193// to keep in mind that from the standpoint of you, a client of this library,194// all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all.195//196// Some secondary priorities arise directly from the first two, some of whichwindows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
line 197why, one step back197// provide more explicit reasons why performance can't be emphasized.tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 198–239why, one step back198//199// - Portable ("ease of use")200// - Small source code footprint ("easy to maintain")201// - No dependencies ("ease of use")202//203// ===========================================================================204//205// I/O callbacks206//207// I/O callbacks allow you to read from arbitrary sources, like packaged208// files or some other source. Data read from callbacks are processed209// through a small internal buffer (currently 128 bytes) to try to reduce210// overhead.211//212// The three functions you must define are "read" (reads some bytes of data),213// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).214//215// ===========================================================================216//217// SIMD support218//219// The JPEG decoder will try to automatically use SIMD kernels on x86 when220// supported by the compiler. For ARM Neon support, you must explicitly221// request it.222//223// (The old do-it-yourself SIMD API is no longer supported in the current224// code.)225//226// On x86, SSE2 will automatically be used when available based on a run-time227// test; if not, the generic C versions are used as a fall-back. On ARM targets,228// the typical path is to have separate builds for NEON and non-NEON devices229// (at least this is true for iOS and Android). Therefore, the NEON support is230// toggled by a build flag: define STBI_NEON to get NEON loops.231//232// If for some reason you do not want to use any of SIMD code, or if233// you have issues compiling it, you can disable it entirely by234// defining STBI_NO_SIMD.235//236// ===========================================================================237//238// HDR image support (disable by defining STBI_NO_HDR)239//windows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
lines 240–243why, one step back240// stb_image supports loading HDR images in general, and currently the Radiance241// .HDR file format specifically. You can still load any file through the existing242// interface; if you attempt to load an HDR file, it will be automatically remapped243// to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 244–276why, one step back244// both of these constants can be reconfigured through this interface:245//246// stbi_hdr_to_ldr_gamma(2.2f);247// stbi_hdr_to_ldr_scale(1.0f);248//249// (note, do not use _inverse_ constants; stbi_image will invert them250// appropriately).251//252// Additionally, there is a new, parallel interface for loading files as253// (linear) floats to preserve the full dynamic range:254//255// float *data = stbi_loadf(filename, &x, &y, &n, 0);256//257// If you load LDR images through this interface, those images will258// be promoted to floating point values, run through the inverse of259// constants corresponding to the above:260//261// stbi_ldr_to_hdr_scale(1.0f);262// stbi_ldr_to_hdr_gamma(2.2f);263//264// Finally, given a filename (or an open file or memory block--see header265// file for details) containing image data, you can query for the "most266// appropriate" interface to use (that is, whether the image is HDR or267// not), using:268//269// stbi_is_hdr(char *filename);270//271// ===========================================================================272//273// iPhone PNG support:274//275// By default we convert iphone-formatted PNGs back to RGB, even though276// they are internally encoded differently. You can disable this conversionwindows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
line 277why, one step back277// by calling stbi_convert_iphone_png_to_rgb(0), in which casetweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 278–330why, one step back278// you will always just get the native iphone "format" through (which279// is BGR stored in RGB).280//281// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per282// pixel to remove any premultiplied alpha *only* if the image file explicitly283// says there's premultiplied data (currently only happens in iPhone images,284// and only if iPhone convert-to-rgb processing is on).285//286// ===========================================================================287//288// ADDITIONAL CONFIGURATION289//290// - You can suppress implementation of any of the decoders to reduce291// your code footprint by #defining one or more of the following292// symbols before creating the implementation.293//294// STBI_NO_JPEG295// STBI_NO_PNG296// STBI_NO_BMP297// STBI_NO_PSD298// STBI_NO_TGA299// STBI_NO_GIF300// STBI_NO_HDR301// STBI_NO_PIC302// STBI_NO_PNM (.ppm and .pgm)303//304// - You can request *only* certain decoders and suppress all other ones305// (this will be more forward-compatible, as addition of new decoders306// doesn't require you to disable them explicitly):307//308// STBI_ONLY_JPEG309// STBI_ONLY_PNG310// STBI_ONLY_BMP311// STBI_ONLY_PSD312// STBI_ONLY_TGA313// STBI_ONLY_GIF314// STBI_ONLY_HDR315// STBI_ONLY_PIC316// STBI_ONLY_PNM (.ppm and .pgm)317//318// - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still319// want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB320//321322323#ifndef STBI_NO_STDIO324#include <stdio.h>325#endif // STBI_NO_STDIO326327#define STBI_VERSION 1328329enum330{Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 331why, one step back331 STBI_default = 0, // only used for desired_channelstweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 332–338why, one step back332333 STBI_grey = 1,334 STBI_grey_alpha = 2,335 STBI_rgb = 3,336 STBI_rgb_alpha = 4337};338windows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
line 339why, one step back339#include <stdlib.h>tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 340–346why, one step back340typedef unsigned char stbi_uc;341typedef unsigned short stbi_us;342343#ifdef __cplusplus344extern "C" {345#endif346Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 347why, one step back347#ifndef STBIDEFtweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 348–352why, one step back348#ifdef STB_IMAGE_STATIC349#define STBIDEF static350#else351#define STBIDEF extern352#endifMerge branch 'master' into workingSean Barrettbefore adoptiond940053
line 353why, one step back353#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 354–379why, one step back354355//////////////////////////////////////////////////////////////////////////////356//357// PRIMARY API - works on images of any type358//359360//361// load image by filename, open file, or memory buffer362//363364typedef struct365{366 int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read367 void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative368 int (*eof) (void *user); // returns nonzero if we are at end of file/data369} stbi_io_callbacks;370371////////////////////////////////////372//373// 8-bits-per-channel interface374//375376STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);377STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels);378379#ifndef STBI_NO_STDIOMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 380–381why, one step back380STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);381STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 382–384why, one step back382// for stbi_load_from_file, file pointer is left pointing immediately after image383#endif384windows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
lines 385–392why, one step back385#ifndef STBI_NO_GIF386STBIDEF 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);387#endif388389#ifdef STBI_WINDOWS_UTF8390STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);391#endif392tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 393–397why, one step back393////////////////////////////////////394//395// 16-bits-per-channel interface396//397Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 398–400why, one step back398STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);399STBIDEF 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);400tweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 401why, one step back401#ifndef STBI_NO_STDIOMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 402why, one step back402STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 403–414why, one step back403STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);404#endif405406////////////////////////////////////407//408// float-per-channel interface409//410#ifndef STBI_NO_LINEAR411 STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);412 STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);413414 #ifndef STBI_NO_STDIOMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 415why, one step back415 STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 416–439why, one step back416 STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);417 #endif418#endif419420#ifndef STBI_NO_HDR421 STBIDEF void stbi_hdr_to_ldr_gamma(float gamma);422 STBIDEF void stbi_hdr_to_ldr_scale(float scale);423#endif // STBI_NO_HDR424425#ifndef STBI_NO_LINEAR426 STBIDEF void stbi_ldr_to_hdr_gamma(float gamma);427 STBIDEF void stbi_ldr_to_hdr_scale(float scale);428#endif // STBI_NO_LINEAR429430// stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR431STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);432STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);433#ifndef STBI_NO_STDIO434STBIDEF int stbi_is_hdr (char const *filename);435STBIDEF int stbi_is_hdr_from_file(FILE *f);436#endif // STBI_NO_STDIO437438439// get a VERY brief reason for failureudpate version numbersSean Barrettbefore adoption5e4a061
line 440why, one step back440// on most compilers (and ALL modern mainstream compilers) this is threadsafetweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 441–448why, one step back441STBIDEF const char *stbi_failure_reason (void);442443// free the loaded image -- this is just free()444STBIDEF void stbi_image_free (void *retval_from_stbi_load);445446// get image dimensions & components without fully decoding447STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);448STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 449–450why, one step back449STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len);450STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user);tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 451–452why, one step back451452#ifndef STBI_NO_STDIOMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 453–456why, one step back453STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);454STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);455STBIDEF int stbi_is_16_bit (char const *filename);456STBIDEF int stbi_is_16_bit_from_file(FILE *f);tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 457–471why, one step back457#endif458459460461// for image formats that explicitly notate that they have premultiplied alpha,462// we just return the colors as stored in the file. set this flag to force463// unpremultiplication. results are undefined if the unpremultiply overflow.464STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);465466// indicate whether we should process iphone images back to canonical format,467// or just pass them through "as-is"468STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);469470// flip the image vertically, so the first pixel in the output array is the bottom left471STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip);udpate version numbersSean Barrettbefore adoption5e4a061
line 472why, one step back472stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 473why, one step back473// as above, but only applies to images loaded on the thread that calls the functionudpate version numbersSean Barrettbefore adoption5e4a061
lines 474–476why, one step back474// this function is only available if your compiler supports thread-local variables;475// calling it will fail to link if your compiler doesn't476#if __cplusplus >= 201103L || __STDC_VERSION_ >= 201112L || defined(__GNUC__) || defined(_MSC_VER)stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 477why, one step back477STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip);udpate version numbersSean Barrettbefore adoption5e4a061
line 478why, one step back478#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 479–545why, one step back479480// ZLIB client - used by PNG, available for other purposes481482STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);483STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);484STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);485STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);486487STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);488STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);489490491#ifdef __cplusplus492}493#endif494495//496//497//// end header file /////////////////////////////////////////////////////498#endif // STBI_INCLUDE_STB_IMAGE_H499500#ifdef STB_IMAGE_IMPLEMENTATION501502#if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \503 || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \504 || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \505 || defined(STBI_ONLY_ZLIB)506 #ifndef STBI_ONLY_JPEG507 #define STBI_NO_JPEG508 #endif509 #ifndef STBI_ONLY_PNG510 #define STBI_NO_PNG511 #endif512 #ifndef STBI_ONLY_BMP513 #define STBI_NO_BMP514 #endif515 #ifndef STBI_ONLY_PSD516 #define STBI_NO_PSD517 #endif518 #ifndef STBI_ONLY_TGA519 #define STBI_NO_TGA520 #endif521 #ifndef STBI_ONLY_GIF522 #define STBI_NO_GIF523 #endif524 #ifndef STBI_ONLY_HDR525 #define STBI_NO_HDR526 #endif527 #ifndef STBI_ONLY_PIC528 #define STBI_NO_PIC529 #endif530 #ifndef STBI_ONLY_PNM531 #define STBI_NO_PNM532 #endif533#endif534535#if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB)536#define STBI_NO_ZLIB537#endif538539540#include <stdarg.h>541#include <stddef.h> // ptrdiff_t on osx542#include <stdlib.h>543#include <string.h>544#include <limits.h>545fix STBI_NO_HDR and STBI_NO_LINEAR to compile correctly even if you don't #include <math.h>Sean Barrettbefore adoption602e986
lines 546–547why, one step back546#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)547#include <math.h> // ldexp, powtweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 548–558why, one step back548#endif549550#ifndef STBI_NO_STDIO551#include <stdio.h>552#endif553554#ifndef STBI_ASSERT555#include <assert.h>556#define STBI_ASSERT(x) assert(x)557#endif558windows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
lines 559–563why, one step back559#ifdef __cplusplus560#define STBI_EXTERN extern "C"561#else562#define STBI_EXTERN extern563#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 564why, one step back564windows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
line 565why, one step back565tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 566–575why, one step back566#ifndef _MSC_VER567 #ifdef __cplusplus568 #define stbi_inline inline569 #else570 #define stbi_inline571 #endif572#else573 #define stbi_inline __forceinline574#endif575stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
lines 576–582why, one step back576#ifndef STBI_NO_THREAD_LOCALS577 #if __cplusplus >= 201103L578 #define STBI_THREAD_LOCAL thread_local579 #elif __STDC_VERSION_ >= 201112L580 #define STBI_THREAD_LOCAL _Thread_local581 #elif defined(__GNUC__)582 #define STBI_THREAD_LOCAL __threadudpate version numbersSean Barrettbefore adoption5e4a061
line 583why, one step back583 #elif defined(_MSC_VER)stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
lines 584–586why, one step back584 #define STBI_THREAD_LOCAL __declspec(thread)585#endif586#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 587–699why, one step back587588#ifdef _MSC_VER589typedef unsigned short stbi__uint16;590typedef signed short stbi__int16;591typedef unsigned int stbi__uint32;592typedef signed int stbi__int32;593#else594#include <stdint.h>595typedef uint16_t stbi__uint16;596typedef int16_t stbi__int16;597typedef uint32_t stbi__uint32;598typedef int32_t stbi__int32;599#endif600601// should produce compiler error if size is wrong602typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];603604#ifdef _MSC_VER605#define STBI_NOTUSED(v) (void)(v)606#else607#define STBI_NOTUSED(v) (void)sizeof(v)608#endif609610#ifdef _MSC_VER611#define STBI_HAS_LROTL612#endif613614#ifdef STBI_HAS_LROTL615 #define stbi_lrot(x,y) _lrotl(x,y)616#else617 #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))618#endif619620#if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED))621// ok622#elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED)623// ok624#else625#error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)."626#endif627628#ifndef STBI_MALLOC629#define STBI_MALLOC(sz) malloc(sz)630#define STBI_REALLOC(p,newsz) realloc(p,newsz)631#define STBI_FREE(p) free(p)632#endif633634#ifndef STBI_REALLOC_SIZED635#define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz)636#endif637638// x86/x64 detection639#if defined(__x86_64__) || defined(_M_X64)640#define STBI__X64_TARGET641#elif defined(__i386) || defined(_M_IX86)642#define STBI__X86_TARGET643#endif644645#if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD)646// gcc doesn't support sse2 intrinsics unless you compile with -msse2,647// which in turn means it gets to use SSE2 everywhere. This is unfortunate,648// but previous attempts to provide the SSE2 functions with runtime649// detection caused numerous issues. The way architecture extensions are650// exposed in GCC/Clang is, sadly, not really suited for one-file libs.651// New behavior: if compiled with -msse2, we use SSE2 without any652// detection; if not, we don't use it at all.653#define STBI_NO_SIMD654#endif655656#if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD)657// Note that __MINGW32__ doesn't actually mean 32-bit, so we have to avoid STBI__X64_TARGET658//659// 32-bit MinGW wants ESP to be 16-byte aligned, but this is not in the660// Windows ABI and VC++ as well as Windows DLLs don't maintain that invariant.661// As a result, enabling SSE2 on 32-bit MinGW is dangerous when not662// simultaneously enabling "-mstackrealign".663//664// See https://github.com/nothings/stb/issues/81 for more information.665//666// So default to no SSE2 on 32-bit MinGW. If you've read this far and added667// -mstackrealign to your build settings, feel free to #define STBI_MINGW_ENABLE_SSE2.668#define STBI_NO_SIMD669#endif670671#if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET))672#define STBI_SSE2673#include <emmintrin.h>674675#ifdef _MSC_VER676677#if _MSC_VER >= 1400 // not VC6678#include <intrin.h> // __cpuid679static int stbi__cpuid3(void)680{681 int info[4];682 __cpuid(info,1);683 return info[3];684}685#else686static int stbi__cpuid3(void)687{688 int res;689 __asm {690 mov eax,1691 cpuid692 mov res,edx693 }694 return res;695}696#endif697698#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name699Merge branch 'issue-609' of https://github.com/dp304/stb into workingSean Barrettbefore adoption7056eae
line 700why, one step back700#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 701why, one step back701static int stbi__sse2_available(void)tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 702–705why, one step back702{703 int info3 = stbi__cpuid3();704 return ((info3 >> 26) & 1) != 0;705}Merge branch 'issue-609' of https://github.com/dp304/stb into workingSean Barrettbefore adoption7056eae
lines 706–707why, one step back706#endif707tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 708–710why, one step back708#else // assume GCC-style if not VC++709#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))710Merge branch 'issue-609' of https://github.com/dp304/stb into workingSean Barrettbefore adoption7056eae
line 711why, one step back711#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 712why, one step back712static int stbi__sse2_available(void)tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 713–719why, one step back713{714 // If we're even attempting to compile this on GCC/Clang, that means715 // -msse2 is on, which means the compiler is allowed to use SSE2716 // instructions at will, and so are we.717 return 1;718}719#endifMerge branch 'issue-609' of https://github.com/dp304/stb into workingSean Barrettbefore adoption7056eae
line 720why, one step back720tweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 721why, one step back721#endifMerge branch 'issue-609' of https://github.com/dp304/stb into workingSean Barrettbefore adoption7056eae
line 722why, one step back722#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 723–849why, one step back723724// ARM NEON725#if defined(STBI_NO_SIMD) && defined(STBI_NEON)726#undef STBI_NEON727#endif728729#ifdef STBI_NEON730#include <arm_neon.h>731// assume GCC or Clang on ARM targets732#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))733#endif734735#ifndef STBI_SIMD_ALIGN736#define STBI_SIMD_ALIGN(type, name) type name737#endif738739///////////////////////////////////////////////740//741// stbi__context struct and start_xxx functions742743// stbi__context structure is our basic context used by all images, so it744// contains all the IO context, plus some basic image information745typedef struct746{747 stbi__uint32 img_x, img_y;748 int img_n, img_out_n;749750 stbi_io_callbacks io;751 void *io_user_data;752753 int read_from_callbacks;754 int buflen;755 stbi_uc buffer_start[128];756757 stbi_uc *img_buffer, *img_buffer_end;758 stbi_uc *img_buffer_original, *img_buffer_original_end;759} stbi__context;760761762static void stbi__refill_buffer(stbi__context *s);763764// initialize a memory-decode context765static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len)766{767 s->io.read = NULL;768 s->read_from_callbacks = 0;769 s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer;770 s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len;771}772773// initialize a callback-based context774static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user)775{776 s->io = *c;777 s->io_user_data = user;778 s->buflen = sizeof(s->buffer_start);779 s->read_from_callbacks = 1;780 s->img_buffer_original = s->buffer_start;781 stbi__refill_buffer(s);782 s->img_buffer_original_end = s->img_buffer_end;783}784785#ifndef STBI_NO_STDIO786787static int stbi__stdio_read(void *user, char *data, int size)788{789 return (int) fread(data,1,size,(FILE*) user);790}791792static void stbi__stdio_skip(void *user, int n)793{794 fseek((FILE*) user, n, SEEK_CUR);795}796797static int stbi__stdio_eof(void *user)798{799 return feof((FILE*) user);800}801802static stbi_io_callbacks stbi__stdio_callbacks =803{804 stbi__stdio_read,805 stbi__stdio_skip,806 stbi__stdio_eof,807};808809static void stbi__start_file(stbi__context *s, FILE *f)810{811 stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f);812}813814//static void stop_file(stbi__context *s) { }815816#endif // !STBI_NO_STDIO817818static void stbi__rewind(stbi__context *s)819{820 // conceptually rewind SHOULD rewind to the beginning of the stream,821 // but we just rewind to the beginning of the initial buffer, because822 // we only use it after doing 'test', which only ever looks at at most 92 bytes823 s->img_buffer = s->img_buffer_original;824 s->img_buffer_end = s->img_buffer_original_end;825}826827enum828{829 STBI_ORDER_RGB,830 STBI_ORDER_BGR831};832833typedef struct834{835 int bits_per_channel;836 int num_channels;837 int channel_order;838} stbi__result_info;839840#ifndef STBI_NO_JPEG841static int stbi__jpeg_test(stbi__context *s);842static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);843static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp);844#endif845846#ifndef STBI_NO_PNG847static int stbi__png_test(stbi__context *s);848static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);849static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 850why, one step back850static int stbi__png_is16(stbi__context *s);tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 851–868why, one step back851#endif852853#ifndef STBI_NO_BMP854static int stbi__bmp_test(stbi__context *s);855static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);856static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp);857#endif858859#ifndef STBI_NO_TGA860static int stbi__tga_test(stbi__context *s);861static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);862static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp);863#endif864865#ifndef STBI_NO_PSD866static int stbi__psd_test(stbi__context *s);867static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc);868static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 869why, one step back869static int stbi__psd_is16(stbi__context *s);tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 870–886why, one step back870#endif871872#ifndef STBI_NO_HDR873static int stbi__hdr_test(stbi__context *s);874static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);875static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp);876#endif877878#ifndef STBI_NO_PIC879static int stbi__pic_test(stbi__context *s);880static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);881static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp);882#endif883884#ifndef STBI_NO_GIF885static int stbi__gif_test(stbi__context *s);886static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 887why, one step back887static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp);tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 888–896why, one step back888static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp);889#endif890891#ifndef STBI_NO_PNM892static int stbi__pnm_test(stbi__context *s);893static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);894static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp);895#endif896Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 897why, one step back897staticstb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
lines 898–901why, one step back898#ifdef STBI_THREAD_LOCAL899STBI_THREAD_LOCAL900#endif901const char *stbi__g_failure_reason;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 902–907why, one step back902903STBIDEF const char *stbi_failure_reason(void)904{905 return stbi__g_failure_reason;906}907Merge branch 'fix_issue_746' of https://github.com/BlackMATov/stb into work2Sean Barrettbefore adoption6877b1a
line 908why, one step back908#ifndef STBI_NO_FAILURE_STRINGStweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 909–913why, one step back909static int stbi__err(const char *str)910{911 stbi__g_failure_reason = str;912 return 0;913}Merge branch 'fix_issue_746' of https://github.com/BlackMATov/stb into work2Sean Barrettbefore adoption6877b1a
line 914why, one step back914#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 915–952why, one step back915916static void *stbi__malloc(size_t size)917{918 return STBI_MALLOC(size);919}920921// stb_image uses ints pervasively, including for offset calculations.922// therefore the largest decoded image size we can support with the923// current code, even on 64-bit targets, is INT_MAX. this is not a924// significant limitation for the intended use case.925//926// we do, however, need to make sure our size calculations don't927// overflow. hence a few helper functions for size calculations that928// multiply integers together, making sure that they're non-negative929// and no overflow occurs.930931// return 1 if the sum is valid, 0 on overflow.932// negative terms are considered invalid.933static int stbi__addsizes_valid(int a, int b)934{935 if (b < 0) return 0;936 // now 0 <= b <= INT_MAX, hence also937 // 0 <= INT_MAX - b <= INTMAX.938 // And "a + b <= INT_MAX" (which might overflow) is the939 // same as a <= INT_MAX - b (no overflow)940 return a <= INT_MAX - b;941}942943// returns 1 if the product is valid, 0 on overflow.944// negative factors are considered invalid.945static int stbi__mul2sizes_valid(int a, int b)946{947 if (a < 0 || b < 0) return 0;948 if (b == 0) return 1; // mul-by-0 is always safe949 // portable way to check for no overflows in a*b950 return a <= INT_MAX/b;951}952Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 953why, one step back953#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 954–958why, one step back954// returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow955static int stbi__mad2sizes_valid(int a, int b, int add)956{957 return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add);958}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 959why, one step back959#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 960–968why, one step back960961// returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow962static int stbi__mad3sizes_valid(int a, int b, int c, int add)963{964 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&965 stbi__addsizes_valid(a*b*c, add);966}967968// returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflowmore cases of STB_NO_HDR and STB_NO_LINEAR interferingSean Barrettbefore adoptiona967296
line 969why, one step back969#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 970–974why, one step back970static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)971{972 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&973 stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add);974}Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 975why, one step back975#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 976why, one step back976Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 977why, one step back977#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 978–983why, one step back978// mallocs with size overflow checking979static void *stbi__malloc_mad2(int a, int b, int add)980{981 if (!stbi__mad2sizes_valid(a, b, add)) return NULL;982 return stbi__malloc(a*b + add);983}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 984why, one step back984#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 985–991why, one step back985986static void *stbi__malloc_mad3(int a, int b, int c, int add)987{988 if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL;989 return stbi__malloc(a*b*c + add);990}991more cases of STB_NO_HDR and STB_NO_LINEAR interferingSean Barrettbefore adoptiona967296
line 992why, one step back992#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 993–997why, one step back993static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)994{995 if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL;996 return stbi__malloc(a*b*c*d + add);997}Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 998why, one step back998#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 999–1027why, one step back9991000// stbi__err - error1001// stbi__errpf - error returning pointer to float1002// stbi__errpuc - error returning pointer to unsigned char10031004#ifdef STBI_NO_FAILURE_STRINGS1005 #define stbi__err(x,y) 01006#elif defined(STBI_FAILURE_USERMSG)1007 #define stbi__err(x,y) stbi__err(y)1008#else1009 #define stbi__err(x,y) stbi__err(x)1010#endif10111012#define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL))1013#define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL))10141015STBIDEF void stbi_image_free(void *retval_from_stbi_load)1016{1017 STBI_FREE(retval_from_stbi_load);1018}10191020#ifndef STBI_NO_LINEAR1021static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp);1022#endif10231024#ifndef STBI_NO_HDR1025static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp);1026#endif1027stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 1028why, one step back1028static int stbi__vertically_flip_on_load_global = 0;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1029–1031why, one step back10291030STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)1031{stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
line 1032why, one step back1032 stbi__vertically_flip_on_load_global = flag_true_if_should_flip;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1033why, one step back1033}stb_image: use thread-locals for vertically_flip flag and g_failure_reasonSean Barrettbefore adoptioneb48fbd
lines 1034–1049why, one step back10341035#ifndef STBI_THREAD_LOCAL1036#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global1037#else1038static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set;10391040STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip)1041{1042 stbi__vertically_flip_on_load_local = flag_true_if_should_flip;1043 stbi__vertically_flip_on_load_set = 1;1044}10451046#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \1047 ? stbi__vertically_flip_on_load_local \1048 : stbi__vertically_flip_on_load_global)1049#endif // STBI_THREAD_LOCALtweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1050–1071why, one step back10501051static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)1052{1053 memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields1054 ri->bits_per_channel = 8; // default is 8 so most paths don't have to be changed1055 ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order1056 ri->num_channels = 0;10571058 #ifndef STBI_NO_JPEG1059 if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp, ri);1060 #endif1061 #ifndef STBI_NO_PNG1062 if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, ri);1063 #endif1064 #ifndef STBI_NO_BMP1065 if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp, ri);1066 #endif1067 #ifndef STBI_NO_GIF1068 if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp, ri);1069 #endif1070 #ifndef STBI_NO_PSD1071 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 1072–1073why, one step back1072 #else1073 STBI_NOTUSED(bpc);tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1074–1129why, one step back1074 #endif1075 #ifndef STBI_NO_PIC1076 if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri);1077 #endif1078 #ifndef STBI_NO_PNM1079 if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp, ri);1080 #endif10811082 #ifndef STBI_NO_HDR1083 if (stbi__hdr_test(s)) {1084 float *hdr = stbi__hdr_load(s, x,y,comp,req_comp, ri);1085 return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);1086 }1087 #endif10881089 #ifndef STBI_NO_TGA1090 // test tga last because it's a crappy test!1091 if (stbi__tga_test(s))1092 return stbi__tga_load(s,x,y,comp,req_comp, ri);1093 #endif10941095 return stbi__errpuc("unknown image type", "Image not of any known type, or corrupt");1096}10971098static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels)1099{1100 int i;1101 int img_len = w * h * channels;1102 stbi_uc *reduced;11031104 reduced = (stbi_uc *) stbi__malloc(img_len);1105 if (reduced == NULL) return stbi__errpuc("outofmem", "Out of memory");11061107 for (i = 0; i < img_len; ++i)1108 reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling11091110 STBI_FREE(orig);1111 return reduced;1112}11131114static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels)1115{1116 int i;1117 int img_len = w * h * channels;1118 stbi__uint16 *enlarged;11191120 enlarged = (stbi__uint16 *) stbi__malloc(img_len*2);1121 if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");11221123 for (i = 0; i < img_len; ++i)1124 enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff11251126 STBI_FREE(orig);1127 return enlarged;1128}1129Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1130–1153why, one step back1130static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)1131{1132 int row;1133 size_t bytes_per_row = (size_t)w * bytes_per_pixel;1134 stbi_uc temp[2048];1135 stbi_uc *bytes = (stbi_uc *)image;11361137 for (row = 0; row < (h>>1); row++) {1138 stbi_uc *row0 = bytes + row*bytes_per_row;1139 stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row;1140 // swap row0 with row11141 size_t bytes_left = bytes_per_row;1142 while (bytes_left) {1143 size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp);1144 memcpy(temp, row0, bytes_copy);1145 memcpy(row0, row1, bytes_copy);1146 memcpy(row1, temp, bytes_copy);1147 row0 += bytes_copy;1148 row1 += bytes_copy;1149 bytes_left -= bytes_copy;1150 }1151 }1152}1153Merge branch 'issue-609' of https://github.com/dp304/stb into workingSean Barrettbefore adoption7056eae
line 1154why, one step back1154#ifndef STBI_NO_GIFMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1155–1161why, one step back1155static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel)1156{1157 int slice;1158 int slice_size = w * h * bytes_per_pixel;11591160 stbi_uc *bytes = (stbi_uc *)image;1161 for (slice = 0; slice < z; ++slice) {Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
lines 1162–1163why, one step back1162 stbi__vertical_flip(bytes, w, h, bytes_per_pixel);1163 bytes += slice_size;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1164–1165why, one step back1164 }1165}Merge branch 'issue-609' of https://github.com/dp304/stb into workingSean Barrettbefore adoption7056eae
line 1166why, one step back1166#endifMerge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1167why, one step back1167tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1168–1185why, one step back1168static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)1169{1170 stbi__result_info ri;1171 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8);11721173 if (result == NULL)1174 return NULL;11751176 if (ri.bits_per_channel != 8) {1177 STBI_ASSERT(ri.bits_per_channel == 16);1178 result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp);1179 ri.bits_per_channel = 8;1180 }11811182 // @TODO: move stbi__convert_format to here11831184 if (stbi__vertically_flip_on_load) {1185 int channels = req_comp ? req_comp : *comp;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1186why, one step back1186 stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc));tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1187–1210why, one step back1187 }11881189 return (unsigned char *) result;1190}11911192static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)1193{1194 stbi__result_info ri;1195 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16);11961197 if (result == NULL)1198 return NULL;11991200 if (ri.bits_per_channel != 16) {1201 STBI_ASSERT(ri.bits_per_channel == 8);1202 result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp);1203 ri.bits_per_channel = 16;1204 }12051206 // @TODO: move stbi__convert_format16 to here1207 // @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision12081209 if (stbi__vertically_flip_on_load) {1210 int channels = req_comp ? req_comp : *comp;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1211why, one step back1211 stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16));tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1212–1216why, one step back1212 }12131214 return (stbi__uint16 *) result;1215}1216Merge branch 'issue-609' of https://github.com/dp304/stb into workingSean Barrettbefore adoption7056eae
line 1217why, one step back1217#if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR)tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1218–1220why, one step back1218static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp)1219{1220 if (stbi__vertically_flip_on_load && result != NULL) {Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1221–1222why, one step back1221 int channels = req_comp ? req_comp : *comp;1222 stbi__vertical_flip(result, *x, *y, channels * sizeof(float));tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1223–1228why, one step back1223 }1224}1225#endif12261227#ifndef STBI_NO_STDIO1228windows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
lines 1229–1236why, one step back1229#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)1230STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);1231STBI_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);1232#endif12331234#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)1235STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)1236{Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1237why, one step back1237 return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);Merge branch 'unicode-file-support' of https://github.com/jrsmith17/stb into tempSean Barrettbefore adoptionae773aa
line 1238why, one step back1238}windows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
line 1239why, one step back1239#endifMerge branch 'unicode-file-support' of https://github.com/jrsmith17/stb into tempSean Barrettbefore adoptionae773aa
line 1240why, one step back1240tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1241–1243why, one step back1241static FILE *stbi__fopen(char const *filename, char const *mode)1242{1243 FILE *f;windows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
lines 1244–1248why, one step back1244#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)1245 wchar_t wMode[64];1246 wchar_t wFilename[1024];1247 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)))1248 return 0;Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 1249why, one step back1249windows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
lines 1250–1251why, one step back1250 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)))1251 return 0;Merge branch 'unicode-file-support' of https://github.com/jrsmith17/stb into tempSean Barrettbefore adoptionae773aa
line 1252why, one step back1252windows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
line 1253why, one step back1253#if _MSC_VER >= 1400Merge branch 'unicode-file-support' of https://github.com/jrsmith17/stb into tempSean Barrettbefore adoptionae773aa
lines 1254–1256why, one step back1254 if (0 != _wfopen_s(&f, wFilename, wMode))1255 f = 0;1256#elsewindows unicode: don't malloc buffers, add explicit STBI_WINDOWS_UTF8 #defineSean Barrettbefore adoptionf82dbd6
lines 1257–1260why, one step back1257 f = _wfopen(wFilename, wMode);1258#endif12591260#elif defined(_MSC_VER) && _MSC_VER >= 1400tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1261–1318why, one step back1261 if (0 != fopen_s(&f, filename, mode))1262 f=0;1263#else1264 f = fopen(filename, mode);1265#endif1266 return f;1267}126812691270STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)1271{1272 FILE *f = stbi__fopen(filename, "rb");1273 unsigned char *result;1274 if (!f) return stbi__errpuc("can't fopen", "Unable to open file");1275 result = stbi_load_from_file(f,x,y,comp,req_comp);1276 fclose(f);1277 return result;1278}12791280STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)1281{1282 unsigned char *result;1283 stbi__context s;1284 stbi__start_file(&s,f);1285 result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);1286 if (result) {1287 // need to 'unget' all the characters in the IO buffer1288 fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);1289 }1290 return result;1291}12921293STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp, int req_comp)1294{1295 stbi__uint16 *result;1296 stbi__context s;1297 stbi__start_file(&s,f);1298 result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp);1299 if (result) {1300 // need to 'unget' all the characters in the IO buffer1301 fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);1302 }1303 return result;1304}13051306STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *comp, int req_comp)1307{1308 FILE *f = stbi__fopen(filename, "rb");1309 stbi__uint16 *result;1310 if (!f) return (stbi_us *) stbi__errpuc("can't fopen", "Unable to open file");1311 result = stbi_load_from_file_16(f,x,y,comp,req_comp);1312 fclose(f);1313 return result;1314}131513161317#endif //!STBI_NO_STDIO1318Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1319–1332why, one step back1319STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels)1320{1321 stbi__context s;1322 stbi__start_mem(&s,buffer,len);1323 return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);1324}13251326STBIDEF 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)1327{1328 stbi__context s;1329 stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user);1330 return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);1331}1332tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1333–1346why, one step back1333STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)1334{1335 stbi__context s;1336 stbi__start_mem(&s,buffer,len);1337 return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);1338}13391340STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)1341{1342 stbi__context s;1343 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);1344 return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);1345}1346Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1347–1350why, one step back1347#ifndef STBI_NO_GIF1348STBIDEF 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)1349{1350 unsigned char *result;Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
lines 1351–1353why, one step back1351 stbi__context s;1352 stbi__start_mem(&s,buffer,len);1353Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1354–1355why, one step back1354 result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);1355 if (stbi__vertically_flip_on_load) {Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 1356why, one step back1356 stbi__vertical_flip_slices( result, *x, *y, *z, *comp );Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1357–1358why, one step back1357 }1358Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a
line 1359why, one step back1359 return result;Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
lines 1360–1362why, one step back1360}1361#endif1362tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1363–1445why, one step back1363#ifndef STBI_NO_LINEAR1364static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)1365{1366 unsigned char *data;1367 #ifndef STBI_NO_HDR1368 if (stbi__hdr_test(s)) {1369 stbi__result_info ri;1370 float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp, &ri);1371 if (hdr_data)1372 stbi__float_postprocess(hdr_data,x,y,comp,req_comp);1373 return hdr_data;1374 }1375 #endif1376 data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp);1377 if (data)1378 return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);1379 return stbi__errpf("unknown image type", "Image not of any known type, or corrupt");1380}13811382STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)1383{1384 stbi__context s;1385 stbi__start_mem(&s,buffer,len);1386 return stbi__loadf_main(&s,x,y,comp,req_comp);1387}13881389STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)1390{1391 stbi__context s;1392 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);1393 return stbi__loadf_main(&s,x,y,comp,req_comp);1394}13951396#ifndef STBI_NO_STDIO1397STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)1398{1399 float *result;1400 FILE *f = stbi__fopen(filename, "rb");1401 if (!f) return stbi__errpf("can't fopen", "Unable to open file");1402 result = stbi_loadf_from_file(f,x,y,comp,req_comp);1403 fclose(f);1404 return result;1405}14061407STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)1408{1409 stbi__context s;1410 stbi__start_file(&s,f);1411 return stbi__loadf_main(&s,x,y,comp,req_comp);1412}1413#endif // !STBI_NO_STDIO14141415#endif // !STBI_NO_LINEAR14161417// these is-hdr-or-not is defined independent of whether STBI_NO_LINEAR is1418// defined, for API simplicity; if STBI_NO_LINEAR is defined, it always1419// reports false!14201421STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)1422{1423 #ifndef STBI_NO_HDR1424 stbi__context s;1425 stbi__start_mem(&s,buffer,len);1426 return stbi__hdr_test(&s);1427 #else1428 STBI_NOTUSED(buffer);1429 STBI_NOTUSED(len);1430 return 0;1431 #endif1432}14331434#ifndef STBI_NO_STDIO1435STBIDEF int stbi_is_hdr (char const *filename)1436{1437 FILE *f = stbi__fopen(filename, "rb");1438 int result=0;1439 if (f) {1440 result = stbi_is_hdr_from_file(f);1441 fclose(f);1442 }1443 return result;1444}1445stb_image: fix warningsSean Barrettbefore adoptionccee11a
line 1446why, one step back1446STBIDEF int stbi_is_hdr_from_file(FILE *f)tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1447–1448why, one step back1447{1448 #ifndef STBI_NO_HDRstb_image: fix warningsSean Barrettbefore adoptionccee11a
lines 1449–1450why, one step back1449 long pos = ftell(f);1450 int res;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1451–1452why, one step back1451 stbi__context s;1452 stbi__start_file(&s,f);stbi_is_hdr_from_file needs to reset file positionSean Barrettbefore adoption9b2522c
lines 1453–1455why, one step back1453 res = stbi__hdr_test(&s);1454 fseek(f, pos, SEEK_SET);1455 return res;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1456–1527why, one step back1456 #else1457 STBI_NOTUSED(f);1458 return 0;1459 #endif1460}1461#endif // !STBI_NO_STDIO14621463STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)1464{1465 #ifndef STBI_NO_HDR1466 stbi__context s;1467 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);1468 return stbi__hdr_test(&s);1469 #else1470 STBI_NOTUSED(clbk);1471 STBI_NOTUSED(user);1472 return 0;1473 #endif1474}14751476#ifndef STBI_NO_LINEAR1477static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f;14781479STBIDEF void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; }1480STBIDEF void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; }1481#endif14821483static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f;14841485STBIDEF void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; }1486STBIDEF void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; }148714881489//////////////////////////////////////////////////////////////////////////////1490//1491// Common code used by all image loaders1492//14931494enum1495{1496 STBI__SCAN_load=0,1497 STBI__SCAN_type,1498 STBI__SCAN_header1499};15001501static void stbi__refill_buffer(stbi__context *s)1502{1503 int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);1504 if (n == 0) {1505 // at end of file, treat same as if from memory, but need to handle case1506 // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file1507 s->read_from_callbacks = 0;1508 s->img_buffer = s->buffer_start;1509 s->img_buffer_end = s->buffer_start+1;1510 *s->img_buffer = 0;1511 } else {1512 s->img_buffer = s->buffer_start;1513 s->img_buffer_end = s->buffer_start + n;1514 }1515}15161517stbi_inline static stbi_uc stbi__get8(stbi__context *s)1518{1519 if (s->img_buffer < s->img_buffer_end)1520 return *s->img_buffer++;1521 if (s->read_from_callbacks) {1522 stbi__refill_buffer(s);1523 return *s->img_buffer++;1524 }1525 return 0;1526}1527Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1528–1530why, one step back1528#if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)1529// nothing1530#elsetweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1531–1541why, one step back1531stbi_inline static int stbi__at_eof(stbi__context *s)1532{1533 if (s->io.read) {1534 if (!(s->io.eof)(s->io_user_data)) return 0;1535 // if feof() is true, check if buffer = end1536 // special case: we've only got the special 0 character at the end1537 if (s->read_from_callbacks == 0) return 1;1538 }15391540 return s->img_buffer >= s->img_buffer_end;1541}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1542why, one step back1542#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1543why, one step back1543Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1544–1546why, one step back1544#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)1545// nothing1546#elsetweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1547–1562why, one step back1547static void stbi__skip(stbi__context *s, int n)1548{1549 if (n < 0) {1550 s->img_buffer = s->img_buffer_end;1551 return;1552 }1553 if (s->io.read) {1554 int blen = (int) (s->img_buffer_end - s->img_buffer);1555 if (blen < n) {1556 s->img_buffer = s->img_buffer_end;1557 (s->io.skip)(s->io_user_data, n - blen);1558 return;1559 }1560 }1561 s->img_buffer += n;1562}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1563why, one step back1563#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1564why, one step back1564Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1565–1567why, one step back1565#if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM)1566// nothing1567#elsetweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1568–1590why, one step back1568static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)1569{1570 if (s->io.read) {1571 int blen = (int) (s->img_buffer_end - s->img_buffer);1572 if (blen < n) {1573 int res, count;15741575 memcpy(buffer, s->img_buffer, blen);15761577 count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen);1578 res = (count == (n-blen));1579 s->img_buffer = s->img_buffer_end;1580 return res;1581 }1582 }15831584 if (s->img_buffer+n <= s->img_buffer_end) {1585 memcpy(buffer, s->img_buffer, n);1586 s->img_buffer += n;1587 return 1;1588 } else1589 return 0;1590}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1591why, one step back1591#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1592why, one step back1592Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1593–1595why, one step back1593#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)1594// nothing1595#elsetweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1596–1600why, one step back1596static int stbi__get16be(stbi__context *s)1597{1598 int z = stbi__get8(s);1599 return (z << 8) + stbi__get8(s);1600}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1601why, one step back1601#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1602why, one step back1602Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1603–1605why, one step back1603#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)1604// nothing1605#elsetweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1606–1610why, one step back1606static stbi__uint32 stbi__get32be(stbi__context *s)1607{1608 stbi__uint32 z = stbi__get16be(s);1609 return (z << 16) + stbi__get16be(s);1610}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1611why, one step back1611#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1612–1632why, one step back16121613#if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF)1614// nothing1615#else1616static int stbi__get16le(stbi__context *s)1617{1618 int z = stbi__get8(s);1619 return z + (stbi__get8(s) << 8);1620}1621#endif16221623#ifndef STBI_NO_BMP1624static stbi__uint32 stbi__get32le(stbi__context *s)1625{1626 stbi__uint32 z = stbi__get16le(s);1627 return z + (stbi__get16le(s) << 16);1628}1629#endif16301631#define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings1632Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1633–1635why, one step back1633#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)1634// nothing1635#elsetweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1636–1650why, one step back1636//////////////////////////////////////////////////////////////////////////////1637//1638// generic converter from built-in img_n to req_comp1639// individual types do this automatically as much as possible (e.g. jpeg1640// does all cases internally since it needs to colorspace convert anyway,1641// and it never has alpha, so very few cases ). png can automatically1642// interleave an alpha=255 channel, but falls back to this for other cases1643//1644// assume data buffer is malloced, so malloc a new one and free that one1645// only failure mode is malloc failing16461647static stbi_uc stbi__compute_y(int r, int g, int b)1648{1649 return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8);1650}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1651why, one step back1651#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1652why, one step back1652Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1653–1655why, one step back1653#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)1654// nothing1655#elsetweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1656–1678why, one step back1656static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)1657{1658 int i,j;1659 unsigned char *good;16601661 if (req_comp == img_n) return data;1662 STBI_ASSERT(req_comp >= 1 && req_comp <= 4);16631664 good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0);1665 if (good == NULL) {1666 STBI_FREE(data);1667 return stbi__errpuc("outofmem", "Out of memory");1668 }16691670 for (j=0; j < (int) y; ++j) {1671 unsigned char *src = data + j * x * img_n ;1672 unsigned char *dest = good + j * x * req_comp;16731674 #define STBI__COMBO(a,b) ((a)*8+(b))1675 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)1676 // convert source image with img_n components to one with req_comp components;1677 // avoid switch per pixel, so use switch per scanline and massive macros1678 switch (STBI__COMBO(img_n, req_comp)) {Merge branch 'master' of https://github.com/WARP-LAB/stb into workingSean Barrettbefore adoptionba5fc49
line 1679why, one step back1679 STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1680why, one step back1680 STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;Merge branch 'master' of https://github.com/WARP-LAB/stb into workingSean Barrettbefore adoptionba5fc49
line 1681why, one step back1681 STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1682–1683why, one step back1682 STBI__CASE(2,1) { dest[0]=src[0]; } break;1683 STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;Merge branch 'master' of https://github.com/WARP-LAB/stb into workingSean Barrettbefore adoptionba5fc49
lines 1684–1685why, one step back1684 STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;1685 STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1686why, one step back1686 STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;Merge branch 'master' of https://github.com/WARP-LAB/stb into workingSean Barrettbefore adoptionba5fc49
line 1687why, one step back1687 STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1688why, one step back1688 STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;Merge branch 'master' of https://github.com/WARP-LAB/stb into workingSean Barrettbefore adoptionba5fc49
lines 1689–1690why, one step back1689 STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break;1690 STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1691–1698why, one step back1691 default: STBI_ASSERT(0);1692 }1693 #undef STBI__CASE1694 }16951696 STBI_FREE(data);1697 return good;1698}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1699why, one step back1699#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
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)1702// nothing1703#elsetweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1704–1707why, one step back1704static stbi__uint16 stbi__compute_y_16(int r, int g, int b)1705{1706 return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8);1707}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1708why, one step back1708#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1709why, one step back1709Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
lines 1710–1712why, one step back1710#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)1711// nothing1712#elsetweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1713–1735why, one step back1713static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y)1714{1715 int i,j;1716 stbi__uint16 *good;17171718 if (req_comp == img_n) return data;1719 STBI_ASSERT(req_comp >= 1 && req_comp <= 4);17201721 good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2);1722 if (good == NULL) {1723 STBI_FREE(data);1724 return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");1725 }17261727 for (j=0; j < (int) y; ++j) {1728 stbi__uint16 *src = data + j * x * img_n ;1729 stbi__uint16 *dest = good + j * x * req_comp;17301731 #define STBI__COMBO(a,b) ((a)*8+(b))1732 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)1733 // convert source image with img_n components to one with req_comp components;1734 // avoid switch per pixel, so use switch per scanline and massive macros1735 switch (STBI__COMBO(img_n, req_comp)) {Merge branch 'master' of https://github.com/WARP-LAB/stb into workingSean Barrettbefore adoptionba5fc49
line 1736why, one step back1736 STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1737why, one step back1737 STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;Merge branch 'master' of https://github.com/WARP-LAB/stb into workingSean Barrettbefore adoptionba5fc49
line 1738why, one step back1738 STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1739–1740why, one step back1739 STBI__CASE(2,1) { dest[0]=src[0]; } break;1740 STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;Merge branch 'master' of https://github.com/WARP-LAB/stb into workingSean Barrettbefore adoptionba5fc49
lines 1741–1742why, one step back1741 STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;1742 STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1743why, one step back1743 STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;Merge branch 'master' of https://github.com/WARP-LAB/stb into workingSean Barrettbefore adoptionba5fc49
line 1744why, one step back1744 STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
line 1745why, one step back1745 STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;Merge branch 'master' of https://github.com/WARP-LAB/stb into workingSean Barrettbefore adoptionba5fc49
lines 1746–1747why, one step back1746 STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break;1747 STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1748–1755why, one step back1748 default: STBI_ASSERT(0);1749 }1750 #undef STBI__CASE1751 }17521753 STBI_FREE(data);1754 return good;1755}Merge branch 'unused-function-warnings' of https://github.com/BradleyMarie/stb into work2Sean Barrettbefore adoption1ced9fa
line 1756why, one step back1756#endiftweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1757–1771why, one step back17571758#ifndef STBI_NO_LINEAR1759static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)1760{1761 int i,k,n;1762 float *output;1763 if (!data) return NULL;1764 output = (float *) stbi__malloc_mad4(x, y, comp, sizeof(float), 0);1765 if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem", "Out of memory"); }1766 // compute number of non-alpha components1767 if (comp & 1) n = comp; else n = comp-1;1768 for (i=0; i < x*y; ++i) {1769 for (k=0; k < n; ++k) {1770 output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);1771 }Merge branch 'optimize_stbi__ldr_to_hdr' of https://github.com/technik/stb into workingSean Barrettbefore adoptionf9d9a41
lines 1772–1776why, one step back1772 }1773 if (n < comp) {1774 for (i=0; i < x*y; ++i) {1775 output[i*comp + n] = data[i*comp + n]/255.0f;1776 }tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1777–1905why, one step back1777 }1778 STBI_FREE(data);1779 return output;1780}1781#endif17821783#ifndef STBI_NO_HDR1784#define stbi__float2int(x) ((int) (x))1785static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp)1786{1787 int i,k,n;1788 stbi_uc *output;1789 if (!data) return NULL;1790 output = (stbi_uc *) stbi__malloc_mad3(x, y, comp, 0);1791 if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem", "Out of memory"); }1792 // compute number of non-alpha components1793 if (comp & 1) n = comp; else n = comp-1;1794 for (i=0; i < x*y; ++i) {1795 for (k=0; k < n; ++k) {1796 float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f;1797 if (z < 0) z = 0;1798 if (z > 255) z = 255;1799 output[i*comp + k] = (stbi_uc) stbi__float2int(z);1800 }1801 if (k < comp) {1802 float z = data[i*comp+k] * 255 + 0.5f;1803 if (z < 0) z = 0;1804 if (z > 255) z = 255;1805 output[i*comp + k] = (stbi_uc) stbi__float2int(z);1806 }1807 }1808 STBI_FREE(data);1809 return output;1810}1811#endif18121813//////////////////////////////////////////////////////////////////////////////1814//1815// "baseline" JPEG/JFIF decoder1816//1817// simple implementation1818// - doesn't support delayed output of y-dimension1819// - simple interface (only one output format: 8-bit interleaved RGB)1820// - doesn't try to recover corrupt jpegs1821// - doesn't allow partial loading, loading multiple at once1822// - still fast on x86 (copying globals into locals doesn't help x86)1823// - allocates lots of intermediate memory (full size of all components)1824// - non-interleaved case requires this anyway1825// - allows good upsampling (see next)1826// high-quality1827// - upsampled channels are bilinearly interpolated, even across blocks1828// - quality integer IDCT derived from IJG's 'slow'1829// performance1830// - fast huffman; reasonable integer IDCT1831// - some SIMD kernels for common paths on targets with SSE2/NEON1832// - uses a lot of intermediate memory, could cache poorly18331834#ifndef STBI_NO_JPEG18351836// huffman decoding acceleration1837#define FAST_BITS 9 // larger handles more cases; smaller stomps less cache18381839typedef struct1840{1841 stbi_uc fast[1 << FAST_BITS];1842 // weirdly, repacking this into AoS is a 10% speed loss, instead of a win1843 stbi__uint16 code[256];1844 stbi_uc values[256];1845 stbi_uc size[257];1846 unsigned int maxcode[18];1847 int delta[17]; // old 'firstsymbol' - old 'firstcode'1848} stbi__huffman;18491850typedef struct1851{1852 stbi__context *s;1853 stbi__huffman huff_dc[4];1854 stbi__huffman huff_ac[4];1855 stbi__uint16 dequant[4][64];1856 stbi__int16 fast_ac[4][1 << FAST_BITS];18571858// sizes for components, interleaved MCUs1859 int img_h_max, img_v_max;1860 int img_mcu_x, img_mcu_y;1861 int img_mcu_w, img_mcu_h;18621863// definition of jpeg image component1864 struct1865 {1866 int id;1867 int h,v;1868 int tq;1869 int hd,ha;1870 int dc_pred;18711872 int x,y,w2,h2;1873 stbi_uc *data;1874 void *raw_data, *raw_coeff;1875 stbi_uc *linebuf;1876 short *coeff; // progressive only1877 int coeff_w, coeff_h; // number of 8x8 coefficient blocks1878 } img_comp[4];18791880 stbi__uint32 code_buffer; // jpeg entropy-coded buffer1881 int code_bits; // number of valid bits1882 unsigned char marker; // marker seen while filling entropy buffer1883 int nomore; // flag if we saw a marker so must stop18841885 int progressive;1886 int spec_start;1887 int spec_end;1888 int succ_high;1889 int succ_low;1890 int eob_run;1891 int jfif;1892 int app14_color_transform; // Adobe APP14 tag1893 int rgb;18941895 int scan_n, order[4];1896 int restart_interval, todo;18971898// kernels1899 void (*idct_block_kernel)(stbi_uc *out, int out_stride, short data[64]);1900 void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step);1901 stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs);1902} stbi__jpeg;19031904static int stbi__build_huffman(stbi__huffman *h, int *count)1905{fix compiler warningsSean Barrettbefore adoption74a21dc
lines 1906–1907why, one step back1906 int i,j,k=0;1907 unsigned int code;tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1908–1922why, one step back1908 // build size list for each symbol (from JPEG spec)1909 for (i=0; i < 16; ++i)1910 for (j=0; j < count[i]; ++j)1911 h->size[k++] = (stbi_uc) (i+1);1912 h->size[k] = 0;19131914 // compute actual symbols (from jpeg spec)1915 code = 0;1916 k = 0;1917 for(j=1; j <= 16; ++j) {1918 // compute delta to add to code to compute symbol id1919 h->delta[j] = k - code;1920 if (h->size[k] == j) {1921 while (h->size[k] == j)1922 h->code[k++] = (stbi__uint16) (code++);stb_image: avoid signed left shiftsSean Barrettbefore adoption4fd9019
line 1923why, one step back1923 if (code-1 >= (1u << j)) return stbi__err("bad code lengths","Corrupt JPEG");tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1924–1966why, one step back1924 }1925 // compute largest code + 1 for this size, preshifted as needed later1926 h->maxcode[j] = code << (16-j);1927 code <<= 1;1928 }1929 h->maxcode[j] = 0xffffffff;19301931 // build non-spec acceleration table; 255 is flag for not-accelerated1932 memset(h->fast, 255, 1 << FAST_BITS);1933 for (i=0; i < k; ++i) {1934 int s = h->size[i];1935 if (s <= FAST_BITS) {1936 int c = h->code[i] << (FAST_BITS-s);1937 int m = 1 << (FAST_BITS-s);1938 for (j=0; j < m; ++j) {1939 h->fast[c+j] = (stbi_uc) i;1940 }1941 }1942 }1943 return 1;1944}19451946// build a table that decodes both magnitude and value of small ACs in1947// one go.1948static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h)1949{1950 int i;1951 for (i=0; i < (1 << FAST_BITS); ++i) {1952 stbi_uc fast = h->fast[i];1953 fast_ac[i] = 0;1954 if (fast < 255) {1955 int rs = h->values[fast];1956 int run = (rs >> 4) & 15;1957 int magbits = rs & 15;1958 int len = h->size[fast];19591960 if (magbits && len + magbits <= FAST_BITS) {1961 // magnitude code followed by receive_extend code1962 int k = ((i << len) & ((1 << FAST_BITS) - 1)) >> (FAST_BITS - magbits);1963 int m = 1 << (magbits - 1);1964 if (k < m) k += (~0U << magbits) + 1;1965 // if the result is small enough, we can fit it in fast_ac table1966 if (k >= -128 && k <= 127)stb_image: avoid signed left shiftsSean Barrettbefore adoption4fd9019
line 1967why, one step back1967 fast_ac[i] = (stbi__int16) ((k * 256) + (run * 16) + (len + magbits));tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1968–1975why, one step back1968 }1969 }1970 }1971}19721973static void stbi__grow_buffer_unsafe(stbi__jpeg *j)1974{1975 do {stb_image: avoid signed left shiftsSean Barrettbefore adoption4fd9019
line 1976why, one step back1976 unsigned int b = j->nomore ? 0 : stbi__get8(j->s);tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1977–1991why, one step back1977 if (b == 0xff) {1978 int c = stbi__get8(j->s);1979 while (c == 0xff) c = stbi__get8(j->s); // consume fill bytes1980 if (c != 0) {1981 j->marker = (unsigned char) c;1982 j->nomore = 1;1983 return;1984 }1985 }1986 j->code_buffer |= b << (24 - j->code_bits);1987 j->code_bits += 8;1988 } while (j->code_bits <= 24);1989}19901991// (1 << n) - 1Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1992why, one step back1992static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};tweaks to previous mergeSean Barrettbefore adoptionc79fa78
lines 1993–2044why, one step back19931994// decode a jpeg huffman value from the bitstream1995stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)1996{1997 unsigned int temp;1998 int c,k;19992000 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);The first 2,000 of 7,658 lines.