Atlas - stb_image.h

Home / ext / SDL / src / video Lines: 7 | Size: 291733 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* stb_image - v2.30 - public domain image loader - http://nothings.org/stb 2 no warranty implied; use at your own risk 3 4 Do this: 5 #define STB_IMAGE_IMPLEMENTATION 6 before you include this file in *one* C or C++ file to create the implementation. 7 8 // i.e. it should look like this: 9 #include ... 10 #include ... 11 #include ... 12 #define STB_IMAGE_IMPLEMENTATION 13 #include "stb_image.h" 14 15 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,free 17 18 19 QUICK NOTES: 20 Primarily of interest to game developers and other people who can 21 avoid problematic images and only need the trivial interface 22 23 JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib) 24 PNG 1/2/4/8/16-bit-per-channel 25 26 TGA (not sure what subset, if a subset) 27 BMP non-1bpp, non-RLE 28 PSD (composited view only, no extra channels, 8/16 bit-per-channel) 29 30 GIF (*comp always reports as 4-channel) 31 HDR (radiance rgbE format) 32 PIC (Softimage PIC) 33 PNM (PPM and PGM binary only) 34 35 Animated GIF still needs a proper API, but here's one way to do it: 36 http://gist.github.com/urraka/685d9a6340b26b830d49 37 38 - decode from memory or through FILE (define STBI_NO_STDIO to remove code) 39 - decode from arbitrary I/O callbacks 40 - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON) 41 42 Full documentation under "DOCUMENTATION" below. 43 44 45LICENSE 46 47 See end of file for license information. 48 49RECENT REVISION HISTORY: 50 51 2.30 (2024-05-31) avoid erroneous gcc warning 52 2.29 (2023-05-xx) optimizations 53 2.28 (2023-01-29) many error fixes, security errors, just tons of stuff 54 2.27 (2021-07-11) document stbi_info better, 16-bit PNM support, bug fixes 55 2.26 (2020-07-13) many minor fixes 56 2.25 (2020-02-02) fix warnings 57 2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically 58 2.23 (2019-08-11) fix clang static analysis warning 59 2.22 (2019-03-04) gif fixes, fix warnings 60 2.21 (2019-02-25) fix typo in comment 61 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs 62 2.19 (2018-02-11) fix warning 63 2.18 (2018-01-30) fix warnings 64 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings 65 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes 66 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC 67 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs 68 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes 69 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes 70 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64 71 RGB-format JPEG; remove white matting in PSD; 72 allocate large structures on the stack; 73 correct channel count for PNG & BMP 74 2.10 (2016-01-22) avoid warning introduced in 2.09 75 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED 76 77 See end of file for full revision history. 78 79 80 ============================ Contributors ========================= 81 82 Image formats Extensions, features 83 Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info) 84 Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info) 85 Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG) 86 Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks) 87 Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG) 88 Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip) 89 Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD) 90 github:urraka (animated gif) Junggon Kim (PNM comments) 91 Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA) 92 socks-the-fox (16-bit PNG) 93 Jeremy Sawicki (handle all ImageNet JPGs) 94 Optimizations & bugfixes Mikhail Morozov (1-bit BMP) 95 Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query) 96 Arseny Kapoulkine Simon Breuss (16-bit PNM) 97 John-Mark Allen Katelyn Gadd (indexed color loading) 98 Carmelo J Fdez-Aguera 99 100 Bug & warning fixes 101 Marc LeBlanc David Woo Guillaume George Martins Mozeiko 102 Christpher Lloyd Jerry Jansson Joseph Thomson Blazej Dariusz Roszkowski 103 Phil Jordan Henner Zeller Dave Moore Roy Eltham 104 Hayaki Saito Nathan Reed Won Chun 105 Luke Graham Johan Duparc Nick Verigakis the Horde3D community 106 Thomas Ruf Ronny Chevalier github:rlyeh 107 Janez Zemva John Bartholomew Michal Cichon github:romigrou 108 Jonathan Blow Ken Hamada Tero Hanninen github:svdijk 109 Eugene Golushkov Laurent Gomila Cort Stratton github:snagar 110 Aruelien Pocheville Sergio Gonzalez Thibault Reuille github:Zelex 111 Cass Everitt Ryamond Barbiero github:grim210 112 Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw 113 Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus 114 Josh Tobin Neil Bickford Matthew Gregan github:poppolopoppo 115 Julian Raschke Gregory Mullen Christian Floisand github:darealshinji 116 Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007 117 Brad Weinberger Matvey Cherevko github:mosra 118 Luca Sas Alexander Veselov Zack Middleton [reserved] 119 Ryan C. Gordon [reserved] [reserved] 120 DO NOT ADD YOUR NAME HERE 121 122 Jacko Dirks 123 124 To add your name to the credits, pick a random blank space in the middle and fill it. 125 80% of merge conflicts on stb PRs are due to people adding their name at the end 126 of the credits. 127*/ 128 129#ifndef STBI_INCLUDE_STB_IMAGE_H 130#define STBI_INCLUDE_STB_IMAGE_H 131 132// DOCUMENTATION 133// 134// Limitations: 135// - no 12-bit-per-channel JPEG 136// - no JPEGs with arithmetic coding 137// - GIF always returns *comp=4 138// 139// Basic usage (see HDR discussion below for HDR usage): 140// int x,y,n; 141// unsigned char *data = stbi_load(filename, &x, &y, &n, 0); 142// // ... process data if not NULL ... 143// // ... x = width, y = height, n = # 8-bit components per pixel ... 144// // ... replace '0' with '1'..'4' to force that many components per pixel 145// // ... but 'n' will always be the number that it would have been if you said 0 146// stbi_image_free(data); 147// 148// Standard parameters: 149// int *x -- outputs image width in pixels 150// int *y -- outputs image height in pixels 151// int *channels_in_file -- outputs # of image components in image file 152// int desired_channels -- if non-zero, # of image components requested in result 153// 154// The return value from an image loader is an 'unsigned char *' which points 155// to the pixel data, or NULL on an allocation failure or if the image is 156// corrupt or invalid. The pixel data consists of *y scanlines of *x pixels, 157// with each pixel consisting of N interleaved 8-bit components; the first 158// pixel pointed to is top-left-most in the image. There is no padding between 159// image scanlines or between pixels, regardless of format. The number of 160// components N is 'desired_channels' if desired_channels is non-zero, or 161// *channels_in_file otherwise. If desired_channels is non-zero, 162// *channels_in_file has the number of components that _would_ have been 163// output otherwise. E.g. if you set desired_channels to 4, you will always 164// get RGBA output, but you can check *channels_in_file to see if it's trivially 165// opaque because e.g. there were only 3 channels in the source image. 166// 167// An output image with N components has the following components interleaved 168// in this order in each pixel: 169// 170// N=#comp components 171// 1 grey 172// 2 grey, alpha 173// 3 red, green, blue 174// 4 red, green, blue, alpha 175// 176// If image loading fails for any reason, the return value will be NULL, 177// and *x, *y, *channels_in_file will be unchanged. The function 178// stbi_failure_reason() can be queried for an extremely brief, end-user 179// unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS 180// to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly 181// more user-friendly ones. 182// 183// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized. 184// 185// To query the width, height and component count of an image without having to 186// decode the full file, you can use the stbi_info family of functions: 187// 188// int x,y,n,ok; 189// ok = stbi_info(filename, &x, &y, &n); 190// // returns ok=1 and sets x, y, n if image is a supported format, 191// // 0 otherwise. 192// 193// Note that stb_image pervasively uses ints in its public API for sizes, 194// including sizes of memory buffers. This is now part of the API and thus 195// hard to change without causing breakage. As a result, the various image 196// loaders all have certain limits on image size; these differ somewhat 197// by format but generally boil down to either just under 2GB or just under 198// 1GB. When the decoded image would be larger than this, stb_image decoding 199// will fail. 200// 201// Additionally, stb_image will reject image files that have any of their 202// dimensions set to a larger value than the configurable STBI_MAX_DIMENSIONS, 203// which defaults to 2**24 = 16777216 pixels. Due to the above memory limit, 204// the only way to have an image with such dimensions load correctly 205// is for it to have a rather extreme aspect ratio. Either way, the 206// assumption here is that such larger images are likely to be malformed 207// or malicious. If you do need to load an image with individual dimensions 208// larger than that, and it still fits in the overall size limit, you can 209// #define STBI_MAX_DIMENSIONS on your own to be something larger. 210// 211// =========================================================================== 212// 213// UNICODE: 214// 215// If compiling for Windows and you wish to use Unicode filenames, compile 216// with 217// #define STBI_WINDOWS_UTF8 218// and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert 219// Windows wchar_t filenames to utf8. 220// 221// =========================================================================== 222// 223// Philosophy 224// 225// stb libraries are designed with the following priorities: 226// 227// 1. easy to use 228// 2. easy to maintain 229// 3. good performance 230// 231// Sometimes I let "good performance" creep up in priority over "easy to maintain", 232// and for best performance I may provide less-easy-to-use APIs that give higher 233// performance, in addition to the easy-to-use ones. Nevertheless, it's important 234// to keep in mind that from the standpoint of you, a client of this library, 235// all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all. 236// 237// Some secondary priorities arise directly from the first two, some of which 238// provide more explicit reasons why performance can't be emphasized. 239// 240// - Portable ("ease of use") 241// - Small source code footprint ("easy to maintain") 242// - No dependencies ("ease of use") 243// 244// =========================================================================== 245// 246// I/O callbacks 247// 248// I/O callbacks allow you to read from arbitrary sources, like packaged 249// files or some other source. Data read from callbacks are processed 250// through a small internal buffer (currently 128 bytes) to try to reduce 251// overhead. 252// 253// The three functions you must define are "read" (reads some bytes of data), 254// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end). 255// 256// =========================================================================== 257// 258// SIMD support 259// 260// The JPEG decoder will try to automatically use SIMD kernels on x86 when 261// supported by the compiler. For ARM Neon support, you must explicitly 262// request it. 263// 264// (The old do-it-yourself SIMD API is no longer supported in the current 265// code.) 266// 267// On x86, SSE2 will automatically be used when available based on a run-time 268// test; if not, the generic C versions are used as a fall-back. On ARM targets, 269// the typical path is to have separate builds for NEON and non-NEON devices 270// (at least this is true for iOS and Android). Therefore, the NEON support is 271// toggled by a build flag: define STBI_NEON to get NEON loops. 272// 273// If for some reason you do not want to use any of SIMD code, or if 274// you have issues compiling it, you can disable it entirely by 275// defining STBI_NO_SIMD. 276// 277// =========================================================================== 278// 279// HDR image support (disable by defining STBI_NO_HDR) 280// 281// stb_image supports loading HDR images in general, and currently the Radiance 282// .HDR file format specifically. You can still load any file through the existing 283// interface; if you attempt to load an HDR file, it will be automatically remapped 284// to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1; 285// both of these constants can be reconfigured through this interface: 286// 287// stbi_hdr_to_ldr_gamma(2.2f); 288// stbi_hdr_to_ldr_scale(1.0f); 289// 290// (note, do not use _inverse_ constants; stbi_image will invert them 291// appropriately). 292// 293// Additionally, there is a new, parallel interface for loading files as 294// (linear) floats to preserve the full dynamic range: 295// 296// float *data = stbi_loadf(filename, &x, &y, &n, 0); 297// 298// If you load LDR images through this interface, those images will 299// be promoted to floating point values, run through the inverse of 300// constants corresponding to the above: 301// 302// stbi_ldr_to_hdr_scale(1.0f); 303// stbi_ldr_to_hdr_gamma(2.2f); 304// 305// Finally, given a filename (or an open file or memory block--see header 306// file for details) containing image data, you can query for the "most 307// appropriate" interface to use (that is, whether the image is HDR or 308// not), using: 309// 310// stbi_is_hdr(char *filename); 311// 312// =========================================================================== 313// 314// iPhone PNG support: 315// 316// We optionally support converting iPhone-formatted PNGs (which store 317// premultiplied BGRA) back to RGB, even though they're internally encoded 318// differently. To enable this conversion, call 319// stbi_convert_iphone_png_to_rgb(1). 320// 321// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per 322// pixel to remove any premultiplied alpha *only* if the image file explicitly 323// says there's premultiplied data (currently only happens in iPhone images, 324// and only if iPhone convert-to-rgb processing is on). 325// 326// =========================================================================== 327// 328// ADDITIONAL CONFIGURATION 329// 330// - You can suppress implementation of any of the decoders to reduce 331// your code footprint by #defining one or more of the following 332// symbols before creating the implementation. 333// 334// STBI_NO_JPEG 335// STBI_NO_PNG 336// STBI_NO_BMP 337// STBI_NO_PSD 338// STBI_NO_TGA 339// STBI_NO_GIF 340// STBI_NO_HDR 341// STBI_NO_PIC 342// STBI_NO_PNM (.ppm and .pgm) 343// 344// - You can request *only* certain decoders and suppress all other ones 345// (this will be more forward-compatible, as addition of new decoders 346// doesn't require you to disable them explicitly): 347// 348// STBI_ONLY_JPEG 349// STBI_ONLY_PNG 350// STBI_ONLY_BMP 351// STBI_ONLY_PSD 352// STBI_ONLY_TGA 353// STBI_ONLY_GIF 354// STBI_ONLY_HDR 355// STBI_ONLY_PIC 356// STBI_ONLY_PNM (.ppm and .pgm) 357// 358// - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still 359// want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB 360// 361// - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater 362// than that size (in either width or height) without further processing. 363// This is to let programs in the wild set an upper bound to prevent 364// denial-of-service attacks on untrusted data, as one could generate a 365// valid image of gigantic dimensions and force stb_image to allocate a 366// huge block of memory and spend disproportionate time decoding it. By 367// default this is set to (1 << 24), which is 16777216, but that's still 368// very big. 369 370#ifndef STBI_NO_STDIO 371#include <stdio.h> 372#endif // STBI_NO_STDIO 373 374#define STBI_VERSION 1 375 376enum 377{ 378 STBI_default = 0, // only used for desired_channels 379 380 STBI_grey = 1, 381 STBI_grey_alpha = 2, 382 STBI_rgb = 3, 383 STBI_rgb_alpha = 4 384}; 385 386#if 0 /* SDL change */ 387#include <stdlib.h> 388typedef unsigned char stbi_uc; 389typedef unsigned short stbi_us; 390#else 391typedef Uint8 stbi_uc; 392typedef Uint16 stbi_us; 393#endif 394 395#ifdef __cplusplus 396extern "C" { 397#endif 398 399#ifndef STBIDEF 400#ifdef STB_IMAGE_STATIC 401#define STBIDEF static 402#else 403#define STBIDEF extern 404#endif 405#endif 406 407////////////////////////////////////////////////////////////////////////////// 408// 409// PRIMARY API - works on images of any type 410// 411 412// 413// load image by filename, open file, or memory buffer 414// 415 416typedef struct 417{ 418 int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read 419 void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative 420 int (*eof) (void *user); // returns nonzero if we are at end of file/data 421} stbi_io_callbacks; 422 423//////////////////////////////////// 424// 425// 8-bits-per-channel interface 426// 427 428STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels); 429#if 0 /* not used in SDL */ 430STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels); 431#endif 432 433#ifndef STBI_NO_STDIO 434STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); 435STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); 436// for stbi_load_from_file, file pointer is left pointing immediately after image 437#endif 438 439#ifndef STBI_NO_GIF 440STBIDEF 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); 441#endif 442 443#ifdef STBI_WINDOWS_UTF8 444STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input); 445#endif 446 447//////////////////////////////////// 448// 449// 8-bits-per-channel indexed color 450// Will fail if image is not an 8-bit PNG or TGA with a palette. 451// Palette buffer needs to be at least 256 entries for PNG. 452// 453 454#if 0 /* not used in SDL */ 455STBIDEF stbi_uc *stbi_load_from_memory_with_palette (stbi_uc const *buffer, int len , int *x, int *y, unsigned int *palette_buffer, int palette_buffer_len); 456#endif 457STBIDEF stbi_uc *stbi_load_from_callbacks_with_palette(stbi_io_callbacks const *clbk, void *user, int *x, int *y, unsigned int *palette_buffer, int palette_buffer_len); 458 459//////////////////////////////////// 460// 461// 16-bits-per-channel interface 462// 463 464#if 0 /* not used in SDL */ 465STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels); 466STBIDEF 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); 467#endif 468 469#ifndef STBI_NO_STDIO 470STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); 471STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); 472#endif 473 474//////////////////////////////////// 475// 476// float-per-channel interface 477// 478#ifndef STBI_NO_LINEAR 479 STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels); 480 STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels); 481 482 #ifndef STBI_NO_STDIO 483 STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); 484 STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); 485 #endif 486#endif 487 488#ifndef STBI_NO_HDR 489 STBIDEF void stbi_hdr_to_ldr_gamma(float gamma); 490 STBIDEF void stbi_hdr_to_ldr_scale(float scale); 491#endif // STBI_NO_HDR 492 493#ifndef STBI_NO_LINEAR 494 STBIDEF void stbi_ldr_to_hdr_gamma(float gamma); 495 STBIDEF void stbi_ldr_to_hdr_scale(float scale); 496#endif // STBI_NO_LINEAR 497 498#if 0 /* not used in SDL */ 499// stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR 500STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user); 501STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len); 502#endif 503#ifndef STBI_NO_STDIO 504STBIDEF int stbi_is_hdr (char const *filename); 505STBIDEF int stbi_is_hdr_from_file(FILE *f); 506#endif // STBI_NO_STDIO 507 508 509#if 0 /* not used in SDL */ 510// get a VERY brief reason for failure 511// on most compilers (and ALL modern mainstream compilers) this is threadsafe 512STBIDEF const char *stbi_failure_reason (void); 513#endif 514 515// free the loaded image -- this is just free() 516STBIDEF void stbi_image_free (void *retval_from_stbi_load); 517 518#if 0 /* not used in SDL */ 519// get image dimensions & components without fully decoding 520STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); 521STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp); 522STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len); 523STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user); 524#endif 525 526#ifndef STBI_NO_STDIO 527STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp); 528STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp); 529STBIDEF int stbi_is_16_bit (char const *filename); 530STBIDEF int stbi_is_16_bit_from_file(FILE *f); 531#endif 532 533 534#ifndef STBI_NO_PNG 535#if 0 /* not used in SDL */ 536// for image formats that explicitly notate that they have premultiplied alpha, 537// we just return the colors as stored in the file. set this flag to force 538// unpremultiplication. results are undefined if the unpremultiply overflow. 539STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply); 540 541// indicate whether we should process iphone images back to canonical format, 542// or just pass them through "as-is" 543STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert); 544 545// flip the image vertically, so the first pixel in the output array is the bottom left 546STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip); 547#endif /**/ 548 549#ifndef STBI_NO_THREAD_LOCALS /**/ 550// as above, but only applies to images loaded on the thread that calls the function 551// this function is only available if your compiler supports thread-local variables; 552// calling it will fail to link if your compiler doesn't 553STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply); 554STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert); 555STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip); 556#endif 557#endif 558 559// ZLIB client - used by PNG, available for other purposes 560 561#ifndef STBI_NO_ZLIB 562#if 0 /* not used in SDL */ 563STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen); 564#endif 565STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header); 566#if 0 /* not used in SDL */ 567STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen); 568STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); 569 570STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen); 571STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); 572#endif 573#endif 574 575 576#ifdef __cplusplus 577} 578#endif 579 580// 581// 582//// end header file ///////////////////////////////////////////////////// 583#endif // STBI_INCLUDE_STB_IMAGE_H 584 585#ifdef STB_IMAGE_IMPLEMENTATION 586 587#if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \ 588 || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \ 589 || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \ 590 || defined(STBI_ONLY_ZLIB) 591 #ifndef STBI_ONLY_JPEG 592 #define STBI_NO_JPEG 593 #endif 594 #ifndef STBI_ONLY_PNG 595 #define STBI_NO_PNG 596 #endif 597 #ifndef STBI_ONLY_BMP 598 #define STBI_NO_BMP 599 #endif 600 #ifndef STBI_ONLY_PSD 601 #define STBI_NO_PSD 602 #endif 603 #ifndef STBI_ONLY_TGA 604 #define STBI_NO_TGA 605 #endif 606 #ifndef STBI_ONLY_GIF 607 #define STBI_NO_GIF 608 #endif 609 #ifndef STBI_ONLY_HDR 610 #define STBI_NO_HDR 611 #endif 612 #ifndef STBI_ONLY_PIC 613 #define STBI_NO_PIC 614 #endif 615 #ifndef STBI_ONLY_PNM 616 #define STBI_NO_PNM 617 #endif 618#endif 619 620#if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB) 621#define STBI_NO_ZLIB 622#endif 623 624 625#if 0 /* SDL change */ 626#include <stdarg.h> 627#include <stddef.h> // ptrdiff_t on osx 628#include <stdlib.h> 629#include <string.h> 630#include <limits.h> 631 632#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) 633#include <math.h> // ldexp, pow 634#endif 635#else /* SDL change */ 636#ifndef UINT_MAX 637#define UINT_MAX SDL_MAX_UINT32 638#endif 639#ifndef INT_MAX 640#define INT_MAX SDL_MAX_SINT32 641#endif 642#ifndef INT_MIN 643#define INT_MIN SDL_MIN_SINT32 644#endif 645#ifndef SHRT_MAX 646#define SHRT_MAX SDL_MAX_SINT16 647#endif 648#ifndef SHRT_MIN 649#define SHRT_MIN SDL_MIN_SINT16 650#endif 651#endif 652 653#ifndef STBI_NO_STDIO 654#include <stdio.h> 655#endif 656 657#ifndef STBI_ASSERT 658#include <assert.h> 659#define STBI_ASSERT(x) assert(x) 660#endif 661 662#ifdef __cplusplus 663#define STBI_EXTERN extern "C" 664#else 665#define STBI_EXTERN extern 666#endif 667 668 669#ifndef _MSC_VER 670 #ifdef __cplusplus 671 #define stbi_inline inline 672 #else 673 #define stbi_inline 674 #endif 675#else 676 #define stbi_inline __forceinline 677#endif 678 679#ifndef STBI_NO_THREAD_LOCALS 680 #if defined(__cplusplus) && __cplusplus >= 201103L 681 #define STBI_THREAD_LOCAL thread_local 682 #elif defined(__GNUC__) && __GNUC__ < 5 683 #define STBI_THREAD_LOCAL __thread 684 #elif defined(_MSC_VER) 685 #define STBI_THREAD_LOCAL __declspec(thread) 686 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__) 687 #define STBI_THREAD_LOCAL _Thread_local 688 #endif 689 690 #ifndef STBI_THREAD_LOCAL 691 #if defined(__GNUC__) 692 #define STBI_THREAD_LOCAL __thread 693 #endif 694 #endif 695#endif 696 697#if 0 /* SDL change */ 698#if defined(_MSC_VER) || defined(__SYMBIAN32__) 699typedef unsigned short stbi__uint16; 700typedef signed short stbi__int16; 701typedef unsigned int stbi__uint32; 702typedef signed int stbi__int32; 703#else 704#include <stdint.h> 705typedef uint16_t stbi__uint16; 706typedef int16_t stbi__int16; 707typedef uint32_t stbi__uint32; 708typedef int32_t stbi__int32; 709#endif 710#else 711typedef Uint16 stbi__uint16; 712typedef Sint16 stbi__int16; 713typedef Uint32 stbi__uint32; 714typedef Sint32 stbi__int32; 715#endif 716 717#ifndef STBI_BUFFER_SIZE 718#define STBI_BUFFER_SIZE 128 719#endif 720 721// should produce compiler error if size is wrong 722typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1]; 723 724#ifdef _MSC_VER 725#define STBI_NOTUSED(v) (void)(v) 726#else 727#define STBI_NOTUSED(v) (void)sizeof(v) 728#endif 729 730#if 0 /* SDL change: */ 731#ifdef _MSC_VER 732#define STBI_HAS_LROTL 733#endif 734#endif 735 736#ifdef STBI_HAS_LROTL 737 #define stbi_lrot(x,y) _lrotl(x,y) 738#else 739 #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (-(y) & 31))) 740#endif 741 742#if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED)) 743// ok 744#elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED) 745// ok 746#else 747#error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)." 748#endif 749 750#ifndef STBI_MALLOC 751#define STBI_MALLOC(sz) malloc(sz) 752#define STBI_REALLOC(p,newsz) realloc(p,newsz) 753#define STBI_FREE(p) free(p) 754#endif 755 756#ifndef STBI_REALLOC_SIZED 757#define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz) 758#endif 759 760// x86/x64 detection 761#if defined(__x86_64__) || defined(_M_X64) 762#define STBI__X64_TARGET 763#elif defined(__i386) || defined(_M_IX86) 764#define STBI__X86_TARGET 765#endif 766 767#if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD) 768// gcc doesn't support sse2 intrinsics unless you compile with -msse2, 769// which in turn means it gets to use SSE2 everywhere. This is unfortunate, 770// but previous attempts to provide the SSE2 functions with runtime 771// detection caused numerous issues. The way architecture extensions are 772// exposed in GCC/Clang is, sadly, not really suited for one-file libs. 773// New behavior: if compiled with -msse2, we use SSE2 without any 774// detection; if not, we don't use it at all. 775#define STBI_NO_SIMD 776#endif 777 778#if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD) 779// Note that __MINGW32__ doesn't actually mean 32-bit, so we have to avoid STBI__X64_TARGET 780// 781// 32-bit MinGW wants ESP to be 16-byte aligned, but this is not in the 782// Windows ABI and VC++ as well as Windows DLLs don't maintain that invariant. 783// As a result, enabling SSE2 on 32-bit MinGW is dangerous when not 784// simultaneously enabling "-mstackrealign". 785// 786// See https://github.com/nothings/stb/issues/81 for more information. 787// 788// So default to no SSE2 on 32-bit MinGW. If you've read this far and added 789// -mstackrealign to your build settings, feel free to #define STBI_MINGW_ENABLE_SSE2. 790#define STBI_NO_SIMD 791#endif 792 793#if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET)) 794#define STBI_SSE2 795#include <emmintrin.h> 796 797#ifdef _MSC_VER 798 799#if _MSC_VER >= 1400 // not VC6 800#include <intrin.h> // __cpuid 801static int stbi__cpuid3(void) 802{ 803 int info[4]; 804 __cpuid(info,1); 805 return info[3]; 806} 807#else 808static int stbi__cpuid3(void) 809{ 810 int res; 811 __asm { 812 mov eax,1 813 cpuid 814 mov res,edx 815 } 816 return res; 817} 818#endif 819 820#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name 821 822#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) 823static int stbi__sse2_available(void) 824{ 825 int info3 = stbi__cpuid3(); 826 return ((info3 >> 26) & 1) != 0; 827} 828#endif 829 830#else // assume GCC-style if not VC++ 831#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16))) 832 833#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) 834static int stbi__sse2_available(void) 835{ 836 // If we're even attempting to compile this on GCC/Clang, that means 837 // -msse2 is on, which means the compiler is allowed to use SSE2 838 // instructions at will, and so are we. 839 return 1; 840} 841#endif 842 843#endif 844#endif 845 846// ARM NEON 847#if defined(STBI_NO_SIMD) && defined(STBI_NEON) 848#undef STBI_NEON 849#endif 850 851#ifdef STBI_NEON 852#include <arm_neon.h> 853#ifdef _MSC_VER 854#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name 855#else 856#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16))) 857#endif 858#endif 859 860#ifndef STBI_SIMD_ALIGN 861#define STBI_SIMD_ALIGN(type, name) type name 862#endif 863 864#ifndef STBI_MAX_DIMENSIONS 865#define STBI_MAX_DIMENSIONS (1 << 24) 866#endif 867 868/////////////////////////////////////////////// 869// 870// stbi__context struct and start_xxx functions 871 872// stbi__context structure is our basic context used by all images, so it 873// contains all the IO context, plus some basic image information 874typedef struct 875{ 876 stbi__uint32 img_x, img_y; 877 int img_n, img_out_n; 878 879 stbi_io_callbacks io; 880 void *io_user_data; 881 882 int read_from_callbacks; 883 int buflen; 884 stbi_uc buffer_start[128]; 885 int callback_already_read; 886 887 stbi_uc *img_buffer, *img_buffer_end; 888 stbi_uc *img_buffer_original, *img_buffer_original_end; 889} stbi__context; 890 891 892static void stbi__refill_buffer(stbi__context *s); 893 894// initialize a memory-decode context 895static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len) 896{ 897 s->io.read = NULL; 898 s->read_from_callbacks = 0; 899 s->callback_already_read = 0; 900 s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer; 901 s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len; 902} 903 904// initialize a callback-based context 905static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user) 906{ 907 s->io = *c; 908 s->io_user_data = user; 909 s->buflen = sizeof(s->buffer_start); 910 s->read_from_callbacks = 1; 911 s->callback_already_read = 0; 912 s->img_buffer = s->img_buffer_original = s->buffer_start; 913 stbi__refill_buffer(s); 914 s->img_buffer_original_end = s->img_buffer_end; 915} 916 917#ifndef STBI_NO_STDIO 918 919static int stbi__stdio_read(void *user, char *data, int size) 920{ 921 return (int) fread(data,1,size,(FILE*) user); 922} 923 924static void stbi__stdio_skip(void *user, int n) 925{ 926 int ch; 927 fseek((FILE*) user, n, SEEK_CUR); 928 ch = fgetc((FILE*) user); /* have to read a byte to reset feof()'s flag */ 929 if (ch != EOF) { 930 ungetc(ch, (FILE *) user); /* push byte back onto stream if valid. */ 931 } 932} 933 934static int stbi__stdio_eof(void *user) 935{ 936 return feof((FILE*) user) || ferror((FILE *) user); 937} 938 939static stbi_io_callbacks stbi__stdio_callbacks = 940{ 941 stbi__stdio_read, 942 stbi__stdio_skip, 943 stbi__stdio_eof, 944}; 945 946static void stbi__start_file(stbi__context *s, FILE *f) 947{ 948 stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f); 949} 950 951//static void stop_file(stbi__context *s) { } 952 953#endif // !STBI_NO_STDIO 954 955static void stbi__rewind(stbi__context *s) 956{ 957 // conceptually rewind SHOULD rewind to the beginning of the stream, 958 // but we just rewind to the beginning of the initial buffer, because 959 // we only use it after doing 'test', which only ever looks at at most 92 bytes 960 s->img_buffer = s->img_buffer_original; 961 s->img_buffer_end = s->img_buffer_original_end; 962} 963 964enum 965{ 966 STBI_ORDER_RGB, 967 STBI_ORDER_BGR 968}; 969 970typedef struct 971{ 972 int w; 973 int h; 974 int pitch; 975 stbi_uc *y; 976 stbi_uc *uv; 977} stbi__nv12; 978 979typedef struct 980{ 981 int bits_per_channel; 982 int num_channels; 983 int channel_order; 984} stbi__result_info; 985 986#ifndef STBI_NO_JPEG 987static int stbi__jpeg_test(stbi__context *s); 988static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__nv12 *nv12, stbi__result_info *ri); 989#if 0 /* not used in SDL */ 990static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp); 991#endif 992#endif 993 994#ifndef STBI_NO_PNG 995static int stbi__png_test(stbi__context *s); 996static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, unsigned int *palette_buffer, int palette_buffer_len, stbi__result_info *ri); 997#if 0 /* not used in SDL */ 998static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp); 999static int stbi__png_is16(stbi__context *s); 1000#endif 1001#endif 1002 1003#ifndef STBI_NO_BMP 1004static int stbi__bmp_test(stbi__context *s); 1005static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); 1006static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp); 1007#endif 1008 1009#ifndef STBI_NO_TGA 1010static int stbi__tga_test(stbi__context *s); 1011static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, unsigned int *palette_buffer, int palette_buffer_len, stbi__result_info *ri); 1012static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp); 1013#endif 1014 1015#ifndef STBI_NO_PSD 1016static int stbi__psd_test(stbi__context *s); 1017static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc); 1018static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp); 1019static int stbi__psd_is16(stbi__context *s); 1020#endif 1021 1022#ifndef STBI_NO_HDR 1023static int stbi__hdr_test(stbi__context *s); 1024static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); 1025static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp); 1026#endif 1027 1028#ifndef STBI_NO_PIC 1029static int stbi__pic_test(stbi__context *s); 1030static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); 1031static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp); 1032#endif 1033 1034#ifndef STBI_NO_GIF 1035static int stbi__gif_test(stbi__context *s); 1036static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); 1037static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp); 1038static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp); 1039#endif 1040 1041#ifndef STBI_NO_PNM 1042static int stbi__pnm_test(stbi__context *s); 1043static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); 1044static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp); 1045static int stbi__pnm_is16(stbi__context *s); 1046#endif 1047 1048#ifndef STBI_NO_FAILURE_STRINGS 1049#if 1 /* SDL change: */ 1050static int stbi__err(const char *str) 1051{ 1052 SDL_SetError("%s", str); 1053 return 0; 1054} 1055#else /* SDL change. */ 1056static 1057#ifdef STBI_THREAD_LOCAL 1058STBI_THREAD_LOCAL 1059#endif 1060const char *stbi__g_failure_reason; 1061 1062STBIDEF const char *stbi_failure_reason(void) 1063{ 1064 return stbi__g_failure_reason; 1065} 1066 1067static int stbi__err(const char *str) 1068{ 1069 stbi__g_failure_reason = str; 1070 return 0; 1071} 1072#endif /**/ 1073#endif 1074 1075static void *stbi__malloc(size_t size) 1076{ 1077 return STBI_MALLOC(size); 1078} 1079 1080// stb_image uses ints pervasively, including for offset calculations. 1081// therefore the largest decoded image size we can support with the 1082// current code, even on 64-bit targets, is INT_MAX. this is not a 1083// significant limitation for the intended use case. 1084// 1085// we do, however, need to make sure our size calculations don't 1086// overflow. hence a few helper functions for size calculations that 1087// multiply integers together, making sure that they're non-negative 1088// and no overflow occurs. 1089 1090// return 1 if the sum is valid, 0 on overflow. 1091// negative terms are considered invalid. 1092static int stbi__addsizes_valid(int a, int b) 1093{ 1094 if (b < 0) return 0; 1095 // now 0 <= b <= INT_MAX, hence also 1096 // 0 <= INT_MAX - b <= INTMAX. 1097 // And "a + b <= INT_MAX" (which might overflow) is the 1098 // same as a <= INT_MAX - b (no overflow) 1099 return a <= INT_MAX - b; 1100} 1101 1102// returns 1 if the product is valid, 0 on overflow. 1103// negative factors are considered invalid. 1104static int stbi__mul2sizes_valid(int a, int b) 1105{ 1106 if (a < 0 || b < 0) return 0; 1107 if (b == 0) return 1; // mul-by-0 is always safe 1108 // portable way to check for no overflows in a*b 1109 return a <= INT_MAX/b; 1110} 1111 1112#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) 1113// returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow 1114static int stbi__mad2sizes_valid(int a, int b, int add) 1115{ 1116 return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add); 1117} 1118#endif 1119 1120// returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow 1121static int stbi__mad3sizes_valid(int a, int b, int c, int add) 1122{ 1123 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && 1124 stbi__addsizes_valid(a*b*c, add); 1125} 1126 1127// returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow 1128#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) || !defined(STBI_NO_PNG) || !defined(STBI_NO_PSD) 1129static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add) 1130{ 1131 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && 1132 stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add); 1133} 1134#endif 1135 1136#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) 1137// mallocs with size overflow checking 1138static void *stbi__malloc_mad2(int a, int b, int add) 1139{ 1140 if (!stbi__mad2sizes_valid(a, b, add)) return NULL; 1141 return stbi__malloc(a*b + add); 1142} 1143#endif 1144 1145static void *stbi__malloc_mad3(int a, int b, int c, int add) 1146{ 1147 if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL; 1148 return stbi__malloc(a*b*c + add); 1149} 1150 1151#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) || !defined(STBI_NO_PNG) || !defined(STBI_NO_PSD) 1152static void *stbi__malloc_mad4(int a, int b, int c, int d, int add) 1153{ 1154 if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL; 1155 return stbi__malloc(a*b*c*d + add); 1156} 1157#endif 1158 1159// returns 1 if the sum of two signed ints is valid (between -2^31 and 2^31-1 inclusive), 0 on overflow. 1160static int stbi__addints_valid(int a, int b) 1161{ 1162 if ((a >= 0) != (b >= 0)) return 1; // a and b have different signs, so no overflow 1163 if (a < 0 && b < 0) return a >= INT_MIN - b; // same as a + b >= INT_MIN; INT_MIN - b cannot overflow since b < 0. 1164 return a <= INT_MAX - b; 1165} 1166 1167// returns 1 if the product of two ints fits in a signed short, 0 on overflow. 1168static int stbi__mul2shorts_valid(int a, int b) 1169{ 1170 if (b == 0 || b == -1) return 1; // multiplication by 0 is always 0; check for -1 so SHRT_MIN/b doesn't overflow 1171 if ((a >= 0) == (b >= 0)) return a <= SHRT_MAX/b; // product is positive, so similar to mul2sizes_valid 1172 if (b < 0) return a <= SHRT_MIN / b; // same as a * b >= SHRT_MIN 1173 return a >= SHRT_MIN / b; 1174} 1175 1176// stbi__err - error 1177// stbi__errpf - error returning pointer to float 1178// stbi__errpuc - error returning pointer to unsigned char 1179 1180#ifdef STBI_NO_FAILURE_STRINGS 1181 #define stbi__err(x,y) 0 1182#elif defined(STBI_FAILURE_USERMSG) 1183 #define stbi__err(x,y) stbi__err(y) 1184#else 1185 #define stbi__err(x,y) stbi__err(x) 1186#endif 1187 1188#define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL)) 1189#define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL)) 1190 1191STBIDEF void stbi_image_free(void *retval_from_stbi_load) 1192{ 1193 STBI_FREE(retval_from_stbi_load); 1194} 1195 1196#ifndef STBI_NO_LINEAR 1197static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp); 1198#endif 1199 1200#ifndef STBI_NO_HDR 1201static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp); 1202#endif 1203 1204static int stbi__vertically_flip_on_load_global = 0; 1205 1206#ifndef STBI_NO_PNG 1207#if 0 /* not used in SDL */ 1208STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip) 1209{ 1210 stbi__vertically_flip_on_load_global = flag_true_if_should_flip; 1211} 1212#endif /**/ 1213#endif 1214 1215#ifndef STBI_THREAD_LOCAL 1216#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global 1217#else 1218static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set; 1219 1220#ifndef STBI_NO_PNG 1221STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip) 1222{ 1223 stbi__vertically_flip_on_load_local = flag_true_if_should_flip; 1224 stbi__vertically_flip_on_load_set = 1; 1225} 1226#endif 1227 1228#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \ 1229 ? stbi__vertically_flip_on_load_local \ 1230 : stbi__vertically_flip_on_load_global) 1231#endif // STBI_THREAD_LOCAL 1232 1233static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc, unsigned int *palette_buffer, int palette_buffer_len) 1234{ 1235 memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields 1236 ri->bits_per_channel = 8; // default is 8 so most paths don't have to be changed 1237 ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order 1238 ri->num_channels = 0; 1239 1240 // test the formats with a very explicit header first (at least a FOURCC 1241 // or distinctive magic number first) 1242 #ifndef STBI_NO_PNG 1243 if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, palette_buffer, palette_buffer_len, ri); 1244 #endif 1245 #ifndef STBI_NO_BMP 1246 if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp, ri); 1247 #endif 1248 #ifndef STBI_NO_GIF 1249 if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp, ri); 1250 #endif 1251 #ifndef STBI_NO_PSD 1252 if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc); 1253 #else 1254 STBI_NOTUSED(bpc); 1255 #endif 1256 #ifndef STBI_NO_PIC 1257 if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri); 1258 #endif 1259 1260 // then the formats that can end up attempting to load with just 1 or 2 1261 // bytes matching expectations; these are prone to false positives, so 1262 // try them later 1263 #ifndef STBI_NO_JPEG 1264 if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp,NULL, ri); 1265 #endif 1266 #ifndef STBI_NO_PNM 1267 if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp, ri); 1268 #endif 1269 1270 #ifndef STBI_NO_HDR 1271 if (stbi__hdr_test(s)) { 1272 float *hdr = stbi__hdr_load(s, x,y,comp,req_comp, ri); 1273 return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp); 1274 } 1275 #endif 1276 1277 #ifndef STBI_NO_TGA 1278 // test tga last because it's a crappy test! 1279 if (stbi__tga_test(s)) 1280 return stbi__tga_load(s,x,y,comp,req_comp, palette_buffer, palette_buffer_len, ri); 1281 #endif 1282 1283 return stbi__errpuc("unknown image type", "Image not of any known type, or corrupt"); 1284} 1285 1286static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels) 1287{ 1288 int i; 1289 int img_len = w * h * channels; 1290 stbi_uc *reduced; 1291 1292 reduced = (stbi_uc *) stbi__malloc(img_len); 1293 if (reduced == NULL) return stbi__errpuc("outofmem", "Out of memory"); 1294 1295 for (i = 0; i < img_len; ++i) 1296 reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling 1297 1298 STBI_FREE(orig); 1299 return reduced; 1300} 1301 1302#if 0 /* not used in SDL */ 1303static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels) 1304{ 1305 int i; 1306 int img_len = w * h * channels; 1307 stbi__uint16 *enlarged; 1308 1309 enlarged = (stbi__uint16 *) stbi__malloc_mad2(img_len, 2, 0); 1310 if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory"); 1311 1312 for (i = 0; i < img_len; ++i) 1313 enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff 1314 1315 STBI_FREE(orig); 1316 return enlarged; 1317} 1318#endif 1319 1320static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel) 1321{ 1322 int row; 1323 size_t bytes_per_row = (size_t)w * bytes_per_pixel; 1324 stbi_uc temp[2048]; 1325 stbi_uc *bytes = (stbi_uc *)image; 1326 1327 for (row = 0; row < (h>>1); row++) { 1328 stbi_uc *row0 = bytes + row*bytes_per_row; 1329 stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row; 1330 // swap row0 with row1 1331 size_t bytes_left = bytes_per_row; 1332 while (bytes_left) { 1333 size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp); 1334 memcpy(temp, row0, bytes_copy); 1335 memcpy(row0, row1, bytes_copy); 1336 memcpy(row1, temp, bytes_copy); 1337 row0 += bytes_copy; 1338 row1 += bytes_copy; 1339 bytes_left -= bytes_copy; 1340 } 1341 } 1342} 1343 1344#ifndef STBI_NO_GIF 1345static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel) 1346{ 1347 int slice; 1348 int slice_size = w * h * bytes_per_pixel; 1349 1350 stbi_uc *bytes = (stbi_uc *)image; 1351 for (slice = 0; slice < z; ++slice) { 1352 stbi__vertical_flip(bytes, w, h, bytes_per_pixel); 1353 bytes += slice_size; 1354 } 1355} 1356#endif 1357 1358static unsigned char *stbi__load_indexed(stbi__context *s, int *x, int *y, unsigned int *palette_buffer, int palette_buffer_len) 1359{ 1360 stbi__result_info ri; 1361 int comp; 1362 void *result; 1363 1364 if (!palette_buffer) 1365 return NULL; 1366 1367 result = stbi__load_main(s, x, y, &comp, 1, &ri, 8, palette_buffer, palette_buffer_len); 1368 if (result == NULL) 1369 return NULL; 1370 1371 if (comp != 1) { 1372 stbi_image_free(result); 1373 return NULL; 1374 } 1375 1376 if (ri.bits_per_channel != 8) { 1377 stbi_image_free(result); 1378 return NULL; 1379 } 1380 1381 // @TODO: move stbi__convert_format to here 1382 1383 if (stbi__vertically_flip_on_load) { 1384 int channels = 1; 1385 stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc)); 1386 } 1387 1388 return (unsigned char *) result; 1389} 1390 1391static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) 1392{ 1393 stbi__result_info ri; 1394 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8, NULL, 0); 1395 1396 if (result == NULL) 1397 return NULL; 1398 1399 // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. 1400 STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); 1401 1402 if (ri.bits_per_channel != 8) { 1403 result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp); 1404 ri.bits_per_channel = 8; 1405 } 1406 1407 // @TODO: move stbi__convert_format to here 1408 1409 if (stbi__vertically_flip_on_load) { 1410 int channels = req_comp ? req_comp : *comp; 1411 stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc)); 1412 } 1413 1414 return (unsigned char *) result; 1415} 1416 1417#if 0 /* not used in SDL */ 1418static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) 1419{ 1420 stbi__result_info ri; 1421 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16, NULL, 0); 1422 1423 if (result == NULL) 1424 return NULL; 1425 1426 // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. 1427 STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); 1428 1429 if (ri.bits_per_channel != 16) { 1430 result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp); 1431 ri.bits_per_channel = 16; 1432 } 1433 1434 // @TODO: move stbi__convert_format16 to here 1435 // @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision 1436 1437 if (stbi__vertically_flip_on_load) { 1438 int channels = req_comp ? req_comp : *comp; 1439 stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16)); 1440 } 1441 1442 return (stbi__uint16 *) result; 1443} 1444#endif /**/ 1445 1446#if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR) 1447static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp) 1448{ 1449 if (stbi__vertically_flip_on_load && result != NULL) { 1450 int channels = req_comp ? req_comp : *comp; 1451 stbi__vertical_flip(result, *x, *y, channels * sizeof(float)); 1452 } 1453} 1454#endif 1455 1456#ifndef STBI_NO_STDIO 1457 1458#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) 1459STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide); 1460STBI_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); 1461#endif 1462 1463#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) 1464STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input) 1465{ 1466 return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL); 1467} 1468#endif 1469 1470static FILE *stbi__fopen(char const *filename, char const *mode) 1471{ 1472 FILE *f; 1473#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) 1474 wchar_t wMode[64]; 1475 wchar_t wFilename[1024]; 1476 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename))) 1477 return 0; 1478 1479 if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode))) 1480 return 0; 1481 1482#if defined(_MSC_VER) && _MSC_VER >= 1400 1483 if (0 != _wfopen_s(&f, wFilename, wMode)) 1484 f = 0; 1485#else 1486 f = _wfopen(wFilename, wMode); 1487#endif 1488 1489#elif defined(_MSC_VER) && _MSC_VER >= 1400 1490 if (0 != fopen_s(&f, filename, mode)) 1491 f=0; 1492#else 1493 f = fopen(filename, mode); 1494#endif 1495 return f; 1496} 1497 1498 1499STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp) 1500{ 1501 FILE *f = stbi__fopen(filename, "rb"); 1502 unsigned char *result; 1503 if (!f) return stbi__errpuc("can't fopen", "Unable to open file"); 1504 result = stbi_load_from_file(f,x,y,comp,req_comp); 1505 fclose(f); 1506 return result; 1507} 1508 1509STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) 1510{ 1511 unsigned char *result; 1512 stbi__context s; 1513 stbi__start_file(&s,f); 1514 result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); 1515 if (result) { 1516 // need to 'unget' all the characters in the IO buffer 1517 fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR); 1518 } 1519 return result; 1520} 1521 1522STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp, int req_comp) 1523{ 1524 stbi__uint16 *result; 1525 stbi__context s; 1526 stbi__start_file(&s,f); 1527 result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp); 1528 if (result) { 1529 // need to 'unget' all the characters in the IO buffer 1530 fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR); 1531 } 1532 return result; 1533} 1534 1535STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *comp, int req_comp) 1536{ 1537 FILE *f = stbi__fopen(filename, "rb"); 1538 stbi__uint16 *result; 1539 if (!f) return (stbi_us *) stbi__errpuc("can't fopen", "Unable to open file"); 1540 result = stbi_load_from_file_16(f,x,y,comp,req_comp); 1541 fclose(f); 1542 return result; 1543} 1544 1545 1546#endif //!STBI_NO_STDIO 1547 1548#if 0 /* not used in SDL */ 1549STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels) 1550{ 1551 stbi__context s; 1552 stbi__start_mem(&s,buffer,len); 1553 return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); 1554} 1555 1556STBIDEF 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) 1557{ 1558 stbi__context s; 1559 stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user); 1560 return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); 1561} 1562#endif /**/ 1563 1564STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) 1565{ 1566 stbi__context s; 1567 stbi__start_mem(&s,buffer,len); 1568 return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); 1569} 1570 1571STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp) 1572{ 1573 stbi__context s; 1574 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); 1575 return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); 1576} 1577 1578#if 0 /* not used in SDL */ 1579STBIDEF stbi_uc *stbi_load_from_memory_with_palette(stbi_uc const *buffer, int len, int *x, int *y, unsigned int *palette_buffer, int palette_buffer_len) 1580{ 1581 stbi__context s; 1582 stbi__start_mem(&s, buffer, len); 1583 return stbi__load_indexed(&s, x, y, palette_buffer, palette_buffer_len); 1584} 1585#endif 1586 1587STBIDEF stbi_uc *stbi_load_from_callbacks_with_palette(stbi_io_callbacks const *clbk, void *user, int *x, int *y, unsigned int *palette_buffer, int palette_buffer_len) 1588{ 1589 stbi__context s; 1590 stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user); 1591 return stbi__load_indexed(&s, x, y, palette_buffer, palette_buffer_len); 1592} 1593 1594#ifndef STBI_NO_GIF 1595STBIDEF 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) 1596{ 1597 unsigned char *result; 1598 stbi__context s; 1599 stbi__start_mem(&s,buffer,len); 1600 1601 result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp); 1602 if (stbi__vertically_flip_on_load) { 1603 stbi__vertical_flip_slices( result, *x, *y, *z, *comp ); 1604 } 1605 1606 return result; 1607} 1608#endif 1609 1610#ifndef STBI_NO_LINEAR 1611static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp) 1612{ 1613 unsigned char *data; 1614 #ifndef STBI_NO_HDR 1615 if (stbi__hdr_test(s)) { 1616 stbi__result_info ri; 1617 float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp, &ri); 1618 if (hdr_data) 1619 stbi__float_postprocess(hdr_data,x,y,comp,req_comp); 1620 return hdr_data; 1621 } 1622 #endif 1623 data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp); 1624 if (data) 1625 return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp); 1626 return stbi__errpf("unknown image type", "Image not of any known type, or corrupt"); 1627} 1628 1629STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) 1630{ 1631 stbi__context s; 1632 stbi__start_mem(&s,buffer,len); 1633 return stbi__loadf_main(&s,x,y,comp,req_comp); 1634} 1635 1636STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp) 1637{ 1638 stbi__context s; 1639 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); 1640 return stbi__loadf_main(&s,x,y,comp,req_comp); 1641} 1642 1643#ifndef STBI_NO_STDIO 1644STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp) 1645{ 1646 float *result; 1647 FILE *f = stbi__fopen(filename, "rb"); 1648 if (!f) return stbi__errpf("can't fopen", "Unable to open file"); 1649 result = stbi_loadf_from_file(f,x,y,comp,req_comp); 1650 fclose(f); 1651 return result; 1652} 1653 1654STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) 1655{ 1656 stbi__context s; 1657 stbi__start_file(&s,f); 1658 return stbi__loadf_main(&s,x,y,comp,req_comp); 1659} 1660#endif // !STBI_NO_STDIO 1661 1662#endif // !STBI_NO_LINEAR 1663 1664#if 0 /* not used in SDL */ 1665// these is-hdr-or-not is defined independent of whether STBI_NO_LINEAR is 1666// defined, for API simplicity; if STBI_NO_LINEAR is defined, it always 1667// reports false! 1668 1669STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len) 1670{ 1671 #ifndef STBI_NO_HDR 1672 stbi__context s; 1673 stbi__start_mem(&s,buffer,len); 1674 return stbi__hdr_test(&s); 1675 #else 1676 STBI_NOTUSED(buffer); 1677 STBI_NOTUSED(len); 1678 return 0; 1679 #endif 1680} 1681#endif 1682 1683#ifndef STBI_NO_STDIO 1684STBIDEF int stbi_is_hdr (char const *filename) 1685{ 1686 FILE *f = stbi__fopen(filename, "rb"); 1687 int result=0; 1688 if (f) { 1689 result = stbi_is_hdr_from_file(f); 1690 fclose(f); 1691 } 1692 return result; 1693} 1694 1695STBIDEF int stbi_is_hdr_from_file(FILE *f) 1696{ 1697 #ifndef STBI_NO_HDR 1698 long pos = ftell(f); 1699 int res; 1700 stbi__context s; 1701 stbi__start_file(&s,f); 1702 res = stbi__hdr_test(&s); 1703 fseek(f, pos, SEEK_SET); 1704 return res; 1705 #else 1706 STBI_NOTUSED(f); 1707 return 0; 1708 #endif 1709} 1710#endif // !STBI_NO_STDIO 1711 1712#if 0 /* not used in SDL */ 1713STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user) 1714{ 1715 #ifndef STBI_NO_HDR 1716 stbi__context s; 1717 stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); 1718 return stbi__hdr_test(&s); 1719 #else 1720 STBI_NOTUSED(clbk); 1721 STBI_NOTUSED(user); 1722 return 0; 1723 #endif 1724} 1725#endif 1726 1727#ifndef STBI_NO_LINEAR 1728static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f; 1729 1730STBIDEF void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; } 1731STBIDEF void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; } 1732#endif 1733 1734#ifndef STBI_NO_HDR 1735static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f; 1736 1737STBIDEF void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; } 1738STBIDEF void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; } 1739#endif 1740 1741 1742////////////////////////////////////////////////////////////////////////////// 1743// 1744// Common code used by all image loaders 1745// 1746 1747enum 1748{ 1749 STBI__SCAN_load=0, 1750 STBI__SCAN_type, 1751 STBI__SCAN_header 1752}; 1753 1754static void stbi__refill_buffer(stbi__context *s) 1755{ 1756 int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen); 1757 s->callback_already_read += (int) (s->img_buffer - s->img_buffer_original); 1758 if (n == 0) { 1759 // at end of file, treat same as if from memory, but need to handle case 1760 // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file 1761 s->read_from_callbacks = 0; 1762 s->img_buffer = s->buffer_start; 1763 s->img_buffer_end = s->buffer_start+1; 1764 *s->img_buffer = 0; 1765 } else { 1766 s->img_buffer = s->buffer_start; 1767 s->img_buffer_end = s->buffer_start + n; 1768 } 1769} 1770 1771stbi_inline static stbi_uc stbi__get8(stbi__context *s) 1772{ 1773 if (s->img_buffer < s->img_buffer_end) 1774 return *s->img_buffer++; 1775 if (s->read_from_callbacks) { 1776 stbi__refill_buffer(s); 1777 return *s->img_buffer++; 1778 } 1779 return 0; 1780} 1781 1782#if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) 1783// nothing 1784#else 1785stbi_inline static int stbi__at_eof(stbi__context *s) 1786{ 1787 if (s->io.read) { 1788 if (!(s->io.eof)(s->io_user_data)) return 0; 1789 // if feof() is true, check if buffer = end 1790 // special case: we've only got the special 0 character at the end 1791 if (s->read_from_callbacks == 0) return 1; 1792 } 1793 1794 return s->img_buffer >= s->img_buffer_end; 1795} 1796#endif 1797 1798#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) 1799// nothing 1800#else 1801static void stbi__skip(stbi__context *s, int n) 1802{ 1803 if (n == 0) return; // already there! 1804 if (n < 0) { 1805 s->img_buffer = s->img_buffer_end; 1806 return; 1807 } 1808 if (s->io.read) { 1809 int blen = (int) (s->img_buffer_end - s->img_buffer); 1810 if (blen < n) { 1811 s->img_buffer = s->img_buffer_end; 1812 (s->io.skip)(s->io_user_data, n - blen); 1813 return; 1814 } 1815 } 1816 s->img_buffer += n; 1817} 1818#endif 1819 1820#if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM) 1821// nothing 1822#else 1823static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n) 1824{ 1825 if (s->io.read) { 1826 int blen = (int) (s->img_buffer_end - s->img_buffer); 1827 if (blen < n) { 1828 int res, count; 1829 1830 memcpy(buffer, s->img_buffer, blen); 1831 1832 count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen); 1833 res = (count == (n-blen)); 1834 s->img_buffer = s->img_buffer_end; 1835 return res; 1836 } 1837 } 1838 1839 if (s->img_buffer+n <= s->img_buffer_end) { 1840 memcpy(buffer, s->img_buffer, n); 1841 s->img_buffer += n; 1842 return 1; 1843 } else 1844 return 0; 1845} 1846#endif 1847 1848#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) 1849// nothing 1850#else 1851static int stbi__get16be(stbi__context *s) 1852{ 1853 int z = stbi__get8(s); 1854 return (z << 8) + stbi__get8(s); 1855} 1856#endif 1857 1858#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) 1859// nothing 1860#else 1861static stbi__uint32 stbi__get32be(stbi__context *s) 1862{ 1863 stbi__uint32 z = stbi__get16be(s); 1864 return (z << 16) + stbi__get16be(s); 1865} 1866#endif 1867 1868#if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) 1869// nothing 1870#else 1871static int stbi__get16le(stbi__context *s) 1872{ 1873 int z = stbi__get8(s); 1874 return z + (stbi__get8(s) << 8); 1875} 1876#endif 1877 1878#ifndef STBI_NO_BMP 1879static stbi__uint32 stbi__get32le(stbi__context *s) 1880{ 1881 stbi__uint32 z = stbi__get16le(s); 1882 z += (stbi__uint32)stbi__get16le(s) << 16; 1883 return z; 1884} 1885#endif 1886 1887#define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings 1888 1889#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) 1890// nothing 1891#else 1892////////////////////////////////////////////////////////////////////////////// 1893// 1894// generic converter from built-in img_n to req_comp 1895// individual types do this automatically as much as possible (e.g. jpeg 1896// does all cases internally since it needs to colorspace convert anyway, 1897// and it never has alpha, so very few cases ). png can automatically 1898// interleave an alpha=255 channel, but falls back to this for other cases 1899// 1900// assume data buffer is malloced, so malloc a new one and free that one 1901// only failure mode is malloc failing 1902 1903static stbi_uc stbi__compute_y(int r, int g, int b) 1904{ 1905 return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8); 1906} 1907#endif 1908 1909#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) 1910// nothing 1911#else 1912static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y) 1913{ 1914 int i,j; 1915 unsigned char *good; 1916 1917 if (data == NULL) return data; 1918 if (req_comp == img_n) return data; 1919 STBI_ASSERT(req_comp >= 1 && req_comp <= 4); 1920 1921 good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0); 1922 if (good == NULL) { 1923 STBI_FREE(data); 1924 return stbi__errpuc("outofmem", "Out of memory"); 1925 } 1926 1927 for (j=0; j < (int) y; ++j) { 1928 unsigned char *src = data + j * x * img_n ; 1929 unsigned char *dest = good + j * x * req_comp; 1930 1931 #define STBI__COMBO(a,b) ((a)*8+(b)) 1932 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) 1933 // convert source image with img_n components to one with req_comp components; 1934 // avoid switch per pixel, so use switch per scanline and massive macros 1935 switch (STBI__COMBO(img_n, req_comp)) { 1936 STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break; 1937 STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; 1938 STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break; 1939 STBI__CASE(2,1) { dest[0]=src[0]; } break; 1940 STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; 1941 STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; 1942 STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break; 1943 STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; 1944 STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break; 1945 STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; 1946 STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break; 1947 STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; 1948 default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return stbi__errpuc("unsupported", "Unsupported format conversion"); 1949 } 1950 #undef STBI__CASE 1951 } 1952 1953 STBI_FREE(data); 1954 return good; 1955} 1956#endif 1957 1958#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) 1959// nothing 1960#else 1961static stbi__uint16 stbi__compute_y_16(int r, int g, int b) 1962{ 1963 return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8); 1964} 1965#endif 1966 1967#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) 1968// nothing 1969#else 1970static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y) 1971{ 1972 int i,j; 1973 stbi__uint16 *good; 1974 1975 if (req_comp == img_n) return data; 1976 STBI_ASSERT(req_comp >= 1 && req_comp <= 4); 1977 1978 good = (stbi__uint16 *) stbi__malloc_mad4(req_comp, x, y, 2, 0); 1979 if (good == NULL) { 1980 STBI_FREE(data); 1981 return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory"); 1982 } 1983 1984 for (j=0; j < (int) y; ++j) { 1985 stbi__uint16 *src = data + j * x * img_n ; 1986 stbi__uint16 *dest = good + j * x * req_comp; 1987 1988 #define STBI__COMBO(a,b) ((a)*8+(b)) 1989 #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) 1990 // convert source image with img_n components to one with req_comp components; 1991 // avoid switch per pixel, so use switch per scanline and massive macros 1992 switch (STBI__COMBO(img_n, req_comp)) { 1993 STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break; 1994 STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; 1995 STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break; 1996 STBI__CASE(2,1) { dest[0]=src[0]; } break; 1997 STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; 1998 STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; 1999 STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break; 2000 STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; 2001 STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break; 2002 STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; 2003 STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break; 2004 STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; 2005 default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return (stbi__uint16*) stbi__errpuc("unsupported", "Unsupported format conversion"); 2006 } 2007 #undef STBI__CASE 2008 } 2009 2010 STBI_FREE(data); 2011 return good; 2012} 2013#endif 2014 2015#ifndef STBI_NO_LINEAR 2016static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp) 2017{ 2018 int i,k,n; 2019 float *output; 2020 if (!data) return NULL; 2021 output = (float *) stbi__malloc_mad4(x, y, comp, sizeof(float), 0); 2022 if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem", "Out of memory"); } 2023 // compute number of non-alpha components 2024 if (comp & 1) n = comp; else n = comp-1; 2025 for (i=0; i < x*y; ++i) { 2026 for (k=0; k < n; ++k) { 2027 output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale); 2028 } 2029 } 2030 if (n < comp) { 2031 for (i=0; i < x*y; ++i) { 2032 output[i*comp + n] = data[i*comp + n]/255.0f; 2033 } 2034 } 2035 STBI_FREE(data); 2036 return output; 2037} 2038#endif 2039 2040#ifndef STBI_NO_HDR 2041#define stbi__float2int(x) ((int) (x)) 2042static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp) 2043{ 2044 int i,k,n; 2045 stbi_uc *output; 2046 if (!data) return NULL; 2047 output = (stbi_uc *) stbi__malloc_mad3(x, y, comp, 0); 2048 if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem", "Out of memory"); } 2049 // compute number of non-alpha components 2050 if (comp & 1) n = comp; else n = comp-1; 2051 for (i=0; i < x*y; ++i) { 2052 for (k=0; k < n; ++k) { 2053 float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f; 2054 if (z < 0) z = 0; 2055 if (z > 255) z = 255; 2056 output[i*comp + k] = (stbi_uc) stbi__float2int(z); 2057 } 2058 if (k < comp) { 2059 float z = data[i*comp+k] * 255 + 0.5f; 2060 if (z < 0) z = 0; 2061 if (z > 255) z = 255; 2062 output[i*comp + k] = (stbi_uc) stbi__float2int(z); 2063 } 2064 } 2065 STBI_FREE(data); 2066 return output; 2067} 2068#endif 2069 2070////////////////////////////////////////////////////////////////////////////// 2071// 2072// "baseline" JPEG/JFIF decoder 2073// 2074// simple implementation 2075// - doesn't support delayed output of y-dimension 2076// - simple interface (only one output format: 8-bit interleaved RGB) 2077// - doesn't try to recover corrupt jpegs 2078// - doesn't allow partial loading, loading multiple at once 2079// - still fast on x86 (copying globals into locals doesn't help x86) 2080// - allocates lots of intermediate memory (full size of all components) 2081// - non-interleaved case requires this anyway 2082// - allows good upsampling (see next) 2083// high-quality 2084// - upsampled channels are bilinearly interpolated, even across blocks 2085// - quality integer IDCT derived from IJG's 'slow' 2086// performance 2087// - fast huffman; reasonable integer IDCT 2088// - some SIMD kernels for common paths on targets with SSE2/NEON 2089// - uses a lot of intermediate memory, could cache poorly 2090 2091#ifndef STBI_NO_JPEG 2092 2093// huffman decoding acceleration 2094#define FAST_BITS 9 // larger handles more cases; smaller stomps less cache 2095 2096typedef struct 2097{ 2098 stbi_uc fast[1 << FAST_BITS]; 2099 // weirdly, repacking this into AoS is a 10% speed loss, instead of a win 2100 stbi__uint16 code[256]; 2101 stbi_uc values[256]; 2102 stbi_uc size[257]; 2103 unsigned int maxcode[18]; 2104 int delta[17]; // old 'firstsymbol' - old 'firstcode' 2105} stbi__huffman; 2106 2107typedef struct 2108{ 2109 stbi__context *s; 2110 stbi__huffman huff_dc[4]; 2111 stbi__huffman huff_ac[4]; 2112 stbi__uint16 dequant[4][64]; 2113 stbi__int16 fast_ac[4][1 << FAST_BITS]; 2114 2115// sizes for components, interleaved MCUs 2116 int img_h_max, img_v_max; 2117 int img_mcu_x, img_mcu_y; 2118 int img_mcu_w, img_mcu_h; 2119 2120// definition of jpeg image component 2121 struct 2122 { 2123 int id; 2124 int h,v; 2125 int tq; 2126 int hd,ha; 2127 int dc_pred; 2128 2129 int x,y,w2,h2; 2130 stbi_uc *data; 2131 void *raw_data, *raw_coeff; 2132 stbi_uc *linebuf; 2133 short *coeff; // progressive only 2134 int coeff_w, coeff_h; // number of 8x8 coefficient blocks 2135 } img_comp[4]; 2136 2137 stbi__uint32 code_buffer; // jpeg entropy-coded buffer 2138 int code_bits; // number of valid bits 2139 unsigned char marker; // marker seen while filling entropy buffer 2140 int nomore; // flag if we saw a marker so must stop 2141 2142 int progressive; 2143 int spec_start; 2144 int spec_end; 2145 int succ_high; 2146 int succ_low; 2147 int eob_run; 2148 int jfif; 2149 int app14_color_transform; // Adobe APP14 tag 2150 int rgb; 2151 2152 int scan_n, order[4]; 2153 int restart_interval, todo; 2154 2155// kernels 2156 void (*idct_block_kernel)(stbi_uc *out, int out_stride, short data[64]); 2157 void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step); 2158 stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs); 2159} stbi__jpeg; 2160 2161static int stbi__build_huffman(stbi__huffman *h, int *count) 2162{ 2163 int i,j,k=0; 2164 unsigned int code; 2165 // build size list for each symbol (from JPEG spec) 2166 for (i=0; i < 16; ++i) { 2167 for (j=0; j < count[i]; ++j) { 2168 h->size[k++] = (stbi_uc) (i+1); 2169 if(k >= 257) return stbi__err("bad size list","Corrupt JPEG"); 2170 } 2171 } 2172 h->size[k] = 0; 2173 2174 // compute actual symbols (from jpeg spec) 2175 code = 0; 2176 k = 0; 2177 for(j=1; j <= 16; ++j) { 2178 // compute delta to add to code to compute symbol id 2179 h->delta[j] = k - code; 2180 if (h->size[k] == j) { 2181 while (h->size[k] == j) 2182 h->code[k++] = (stbi__uint16) (code++); 2183 if (code-1 >= (1u << j)) return stbi__err("bad code lengths","Corrupt JPEG"); 2184 } 2185 // compute largest code + 1 for this size, preshifted as needed later 2186 h->maxcode[j] = code << (16-j); 2187 code <<= 1; 2188 } 2189 h->maxcode[j] = 0xffffffff; 2190 2191 // build non-spec acceleration table; 255 is flag for not-accelerated 2192 memset(h->fast, 255, 1 << FAST_BITS); 2193 for (i=0; i < k; ++i) { 2194 int s = h->size[i]; 2195 if (s <= FAST_BITS) { 2196 int c = h->code[i] << (FAST_BITS-s); 2197 int m = 1 << (FAST_BITS-s); 2198 for (j=0; j < m; ++j) { 2199 h->fast[c+j] = (stbi_uc) i; 2200 } 2201 } 2202 } 2203 return 1; 2204} 2205 2206// build a table that decodes both magnitude and value of small ACs in 2207// one go. 2208static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h) 2209{ 2210 int i; 2211 for (i=0; i < (1 << FAST_BITS); ++i) { 2212 stbi_uc fast = h->fast[i]; 2213 fast_ac[i] = 0; 2214 if (fast < 255) { 2215 int rs = h->values[fast]; 2216 int run = (rs >> 4) & 15; 2217 int magbits = rs & 15; 2218 int len = h->size[fast]; 2219 2220 if (magbits && len + magbits <= FAST_BITS) { 2221 // magnitude code followed by receive_extend code 2222 int k = ((i << len) & ((1 << FAST_BITS) - 1)) >> (FAST_BITS - magbits); 2223 int m = 1 << (magbits - 1); 2224 if (k < m) k += (~0U << magbits) + 1; 2225 // if the result is small enough, we can fit it in fast_ac table 2226 if (k >= -128 && k <= 127) 2227 fast_ac[i] = (stbi__int16) ((k * 256) + (run * 16) + (len + magbits)); 2228 } 2229 } 2230 } 2231} 2232 2233static void stbi__grow_buffer_unsafe(stbi__jpeg *j) 2234{ 2235 do { 2236 unsigned int b = j->nomore ? 0 : stbi__get8(j->s); 2237 if (b == 0xff) { 2238 int c = stbi__get8(j->s); 2239 while (c == 0xff) c = stbi__get8(j->s); // consume fill bytes 2240 if (c != 0) { 2241 j->marker = (unsigned char) c; 2242 j->nomore = 1; 2243 return; 2244 } 2245 } 2246 j->code_buffer |= b << (24 - j->code_bits); 2247 j->code_bits += 8; 2248 } while (j->code_bits <= 24); 2249} 2250 2251// (1 << n) - 1 2252static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535}; 2253 2254// decode a jpeg huffman value from the bitstream 2255stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h) 2256{ 2257 unsigned int temp; 2258 int c,k; 2259 2260 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); 2261 2262 // look at the top FAST_BITS and determine what symbol ID it is, 2263 // if the code is <= FAST_BITS 2264 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); 2265 k = h->fast[c]; 2266 if (k < 255) { 2267 int s = h->size[k]; 2268 if (s > j->code_bits) 2269 return -1; 2270 j->code_buffer <<= s; 2271 j->code_bits -= s; 2272 return h->values[k]; 2273 } 2274 2275 // naive test is to shift the code_buffer down so k bits are 2276 // valid, then test against maxcode. To speed this up, we've 2277 // preshifted maxcode left so that it has (16-k) 0s at the 2278 // end; in other words, regardless of the number of bits, it 2279 // wants to be compared against something shifted to have 16; 2280 // that way we don't need to shift inside the loop. 2281 temp = j->code_buffer >> 16; 2282 for (k=FAST_BITS+1 ; ; ++k) 2283 if (temp < h->maxcode[k]) 2284 break; 2285 if (k == 17) { 2286 // error! code not found 2287 j->code_bits -= 16; 2288 return -1; 2289 } 2290 2291 if (k > j->code_bits) 2292 return -1; 2293 2294 // convert the huffman code to the symbol id 2295 c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k]; 2296 if(c < 0 || c >= 256) // symbol id out of bounds! 2297 return -1; 2298 STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]); 2299 2300 // convert the id to a symbol 2301 j->code_bits -= k; 2302 j->code_buffer <<= k; 2303 return h->values[c]; 2304} 2305 2306// bias[n] = (-1<<n) + 1 2307static const int stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767}; 2308 2309// combined JPEG 'receive' and JPEG 'extend', since baseline 2310// always extends everything it receives. 2311stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n) 2312{ 2313 unsigned int k; 2314 int sgn; 2315 if (j->code_bits < n) stbi__grow_buffer_unsafe(j); 2316 if (j->code_bits < n) return 0; // ran out of bits from stream, return 0s intead of continuing 2317 2318 sgn = j->code_buffer >> 31; // sign bit always in MSB; 0 if MSB clear (positive), 1 if MSB set (negative) 2319 k = stbi_lrot(j->code_buffer, n); 2320 j->code_buffer = k & ~stbi__bmask[n]; 2321 k &= stbi__bmask[n]; 2322 j->code_bits -= n; 2323 return k + (stbi__jbias[n] & (sgn - 1)); 2324} 2325 2326// get some unsigned bits 2327stbi_inline static int stbi__jpeg_get_bits(stbi__jpeg *j, int n) 2328{ 2329 unsigned int k; 2330 if (j->code_bits < n) stbi__grow_buffer_unsafe(j); 2331 if (j->code_bits < n) return 0; // ran out of bits from stream, return 0s intead of continuing 2332 k = stbi_lrot(j->code_buffer, n); 2333 j->code_buffer = k & ~stbi__bmask[n]; 2334 k &= stbi__bmask[n]; 2335 j->code_bits -= n; 2336 return k; 2337} 2338 2339stbi_inline static int stbi__jpeg_get_bit(stbi__jpeg *j) 2340{ 2341 unsigned int k; 2342 if (j->code_bits < 1) stbi__grow_buffer_unsafe(j); 2343 if (j->code_bits < 1) return 0; // ran out of bits from stream, return 0s intead of continuing 2344 k = j->code_buffer; 2345 j->code_buffer <<= 1; 2346 --j->code_bits; 2347 return k & 0x80000000; 2348} 2349 2350// given a value that's at position X in the zigzag stream, 2351// where does it appear in the 8x8 matrix coded as row-major? 2352static const stbi_uc stbi__jpeg_dezigzag[64+15] = 2353{ 2354 0, 1, 8, 16, 9, 2, 3, 10, 2355 17, 24, 32, 25, 18, 11, 4, 5, 2356 12, 19, 26, 33, 40, 48, 41, 34, 2357 27, 20, 13, 6, 7, 14, 21, 28, 2358 35, 42, 49, 56, 57, 50, 43, 36, 2359 29, 22, 15, 23, 30, 37, 44, 51, 2360 58, 59, 52, 45, 38, 31, 39, 46, 2361 53, 60, 61, 54, 47, 55, 62, 63, 2362 // let corrupt input sample past end 2363 63, 63, 63, 63, 63, 63, 63, 63, 2364 63, 63, 63, 63, 63, 63, 63 2365}; 2366 2367// decode one 64-entry block-- 2368static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman *hdc, stbi__huffman *hac, stbi__int16 *fac, int b, stbi__uint16 *dequant) 2369{ 2370 int diff,dc,k; 2371 int t; 2372 2373 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); 2374 t = stbi__jpeg_huff_decode(j, hdc); 2375 if (t < 0 || t > 15) return stbi__err("bad huffman code","Corrupt JPEG"); 2376 2377 // 0 all the ac values now so we can do it 32-bits at a time 2378 memset(data,0,64*sizeof(data[0])); 2379 2380 diff = t ? stbi__extend_receive(j, t) : 0; 2381 if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta","Corrupt JPEG"); 2382 dc = j->img_comp[b].dc_pred + diff; 2383 j->img_comp[b].dc_pred = dc; 2384 if (!stbi__mul2shorts_valid(dc, dequant[0])) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); 2385 data[0] = (short) (dc * dequant[0]); 2386 2387 // decode AC components, see JPEG spec 2388 k = 1; 2389 do { 2390 unsigned int zig; 2391 int c,r,s; 2392 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); 2393 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); 2394 r = fac[c]; 2395 if (r) { // fast-AC path 2396 k += (r >> 4) & 15; // run 2397 s = r & 15; // combined length 2398 if (s > j->code_bits) return stbi__err("bad huffman code", "Combined length longer than code bits available"); 2399 j->code_buffer <<= s; 2400 j->code_bits -= s; 2401 // decode into unzigzag'd location 2402 zig = stbi__jpeg_dezigzag[k++]; 2403 data[zig] = (short) ((r >> 8) * dequant[zig]); 2404 } else { 2405 int rs = stbi__jpeg_huff_decode(j, hac); 2406 if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); 2407 s = rs & 15; 2408 r = rs >> 4; 2409 if (s == 0) { 2410 if (rs != 0xf0) break; // end block 2411 k += 16; 2412 } else { 2413 k += r; 2414 // decode into unzigzag'd location 2415 zig = stbi__jpeg_dezigzag[k++]; 2416 data[zig] = (short) (stbi__extend_receive(j,s) * dequant[zig]); 2417 } 2418 } 2419 } while (k < 64); 2420 return 1; 2421} 2422 2423static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__huffman *hdc, int b) 2424{ 2425 int diff,dc; 2426 int t; 2427 if (j->spec_end != 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); 2428 2429 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); 2430 2431 if (j->succ_high == 0) { 2432 // first scan for DC coefficient, must be first 2433 memset(data,0,64*sizeof(data[0])); // 0 all the ac values now 2434 t = stbi__jpeg_huff_decode(j, hdc); 2435 if (t < 0 || t > 15) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); 2436 diff = t ? stbi__extend_receive(j, t) : 0; 2437 2438 if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta", "Corrupt JPEG"); 2439 dc = j->img_comp[b].dc_pred + diff; 2440 j->img_comp[b].dc_pred = dc; 2441 if (!stbi__mul2shorts_valid(dc, 1 << j->succ_low)) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); 2442 data[0] = (short) (dc * (1 << j->succ_low)); 2443 } else { 2444 // refinement scan for DC coefficient 2445 if (stbi__jpeg_get_bit(j)) 2446 data[0] += (short) (1 << j->succ_low); 2447 } 2448 return 1; 2449} 2450 2451// @OPTIMIZE: store non-zigzagged during the decode passes, 2452// and only de-zigzag when dequantizing 2453static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__huffman *hac, stbi__int16 *fac) 2454{ 2455 int k; 2456 if (j->spec_start == 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); 2457 2458 if (j->succ_high == 0) { 2459 int shift = j->succ_low; 2460 2461 if (j->eob_run) { 2462 --j->eob_run; 2463 return 1; 2464 } 2465 2466 k = j->spec_start; 2467 do { 2468 unsigned int zig; 2469 int c,r,s; 2470 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); 2471 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); 2472 r = fac[c]; 2473 if (r) { // fast-AC path 2474 k += (r >> 4) & 15; // run 2475 s = r & 15; // combined length 2476 if (s > j->code_bits) return stbi__err("bad huffman code", "Combined length longer than code bits available"); 2477 j->code_buffer <<= s; 2478 j->code_bits -= s; 2479 zig = stbi__jpeg_dezigzag[k++]; 2480 data[zig] = (short) ((r >> 8) * (1 << shift)); 2481 } else { 2482 int rs = stbi__jpeg_huff_decode(j, hac); 2483 if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); 2484 s = rs & 15; 2485 r = rs >> 4; 2486 if (s == 0) { 2487 if (r < 15) { 2488 j->eob_run = (1 << r); 2489 if (r) 2490 j->eob_run += stbi__jpeg_get_bits(j, r); 2491 --j->eob_run; 2492 break; 2493 } 2494 k += 16; 2495 } else { 2496 k += r; 2497 zig = stbi__jpeg_dezigzag[k++]; 2498 data[zig] = (short) (stbi__extend_receive(j,s) * (1 << shift)); 2499 } 2500 } 2501 } while (k <= j->spec_end); 2502 } else { 2503 // refinement scan for these AC coefficients 2504 2505 short bit = (short) (1 << j->succ_low); 2506 2507 if (j->eob_run) { 2508 --j->eob_run; 2509 for (k = j->spec_start; k <= j->spec_end; ++k) { 2510 short *p = &data[stbi__jpeg_dezigzag[k]]; 2511 if (*p != 0) 2512 if (stbi__jpeg_get_bit(j)) 2513 if ((*p & bit)==0) { 2514 if (*p > 0) 2515 *p += bit; 2516 else 2517 *p -= bit; 2518 } 2519 } 2520 } else { 2521 k = j->spec_start; 2522 do { 2523 int r,s; 2524 int rs = stbi__jpeg_huff_decode(j, hac); // @OPTIMIZE see if we can use the fast path here, advance-by-r is so slow, eh 2525 if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); 2526 s = rs & 15; 2527 r = rs >> 4; 2528 if (s == 0) { 2529 if (r < 15) { 2530 j->eob_run = (1 << r) - 1; 2531 if (r) 2532 j->eob_run += stbi__jpeg_get_bits(j, r); 2533 r = 64; // force end of block 2534 } else { 2535 // r=15 s=0 should write 16 0s, so we just do 2536 // a run of 15 0s and then write s (which is 0), 2537 // so we don't have to do anything special here 2538 } 2539 } else { 2540 if (s != 1) return stbi__err("bad huffman code", "Corrupt JPEG"); 2541 // sign bit 2542 if (stbi__jpeg_get_bit(j)) 2543 s = bit; 2544 else 2545 s = -bit; 2546 } 2547 2548 // advance by r 2549 while (k <= j->spec_end) { 2550 short *p = &data[stbi__jpeg_dezigzag[k++]]; 2551 if (*p != 0) { 2552 if (stbi__jpeg_get_bit(j)) 2553 if ((*p & bit)==0) { 2554 if (*p > 0) 2555 *p += bit; 2556 else 2557 *p -= bit; 2558 } 2559 } else { 2560 if (r == 0) { 2561 *p = (short) s; 2562 break; 2563 } 2564 --r; 2565 } 2566 } 2567 } while (k <= j->spec_end); 2568 } 2569 } 2570 return 1; 2571} 2572 2573// take a -128..127 value and stbi__clamp it and convert to 0..255 2574stbi_inline static stbi_uc stbi__clamp(int x) 2575{ 2576 // trick to use a single test to catch both cases 2577 if ((unsigned int) x > 255) { 2578 if (x < 0) return 0; 2579 if (x > 255) return 255; 2580 } 2581 return (stbi_uc) x; 2582} 2583 2584#define stbi__f2f(x) ((int) (((x) * 4096 + 0.5))) 2585#define stbi__fsh(x) ((x) * 4096) 2586 2587// derived from jidctint -- DCT_ISLOW 2588#define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \ 2589 int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \ 2590 p2 = s2; \ 2591 p3 = s6; \ 2592 p1 = (p2+p3) * stbi__f2f(0.5411961f); \ 2593 t2 = p1 + p3*stbi__f2f(-1.847759065f); \ 2594 t3 = p1 + p2*stbi__f2f( 0.765366865f); \ 2595 p2 = s0; \ 2596 p3 = s4; \ 2597 t0 = stbi__fsh(p2+p3); \ 2598 t1 = stbi__fsh(p2-p3); \ 2599 x0 = t0+t3; \ 2600 x3 = t0-t3; \ 2601 x1 = t1+t2; \ 2602 x2 = t1-t2; \ 2603 t0 = s7; \ 2604 t1 = s5; \ 2605 t2 = s3; \ 2606 t3 = s1; \ 2607 p3 = t0+t2; \ 2608 p4 = t1+t3; \ 2609 p1 = t0+t3; \ 2610 p2 = t1+t2; \ 2611 p5 = (p3+p4)*stbi__f2f( 1.175875602f); \ 2612 t0 = t0*stbi__f2f( 0.298631336f); \ 2613 t1 = t1*stbi__f2f( 2.053119869f); \ 2614 t2 = t2*stbi__f2f( 3.072711026f); \ 2615 t3 = t3*stbi__f2f( 1.501321110f); \ 2616 p1 = p5 + p1*stbi__f2f(-0.899976223f); \ 2617 p2 = p5 + p2*stbi__f2f(-2.562915447f); \ 2618 p3 = p3*stbi__f2f(-1.961570560f); \ 2619 p4 = p4*stbi__f2f(-0.390180644f); \ 2620 t3 += p1+p4; \ 2621 t2 += p2+p3; \ 2622 t1 += p2+p4; \ 2623 t0 += p1+p3; 2624 2625static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64]) 2626{ 2627 int i,val[64],*v=val; 2628 stbi_uc *o; 2629 short *d = data; 2630 2631 // columns 2632 for (i=0; i < 8; ++i,++d, ++v) { 2633 // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing 2634 if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0 2635 && d[40]==0 && d[48]==0 && d[56]==0) { 2636 // no shortcut 0 seconds 2637 // (1|2|3|4|5|6|7)==0 0 seconds 2638 // all separate -0.047 seconds 2639 // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds 2640 int dcterm = d[0]*4; 2641 v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm; 2642 } else { 2643 STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56]) 2644 // constants scaled things up by 1<<12; let's bring them back 2645 // down, but keep 2 extra bits of precision 2646 x0 += 512; x1 += 512; x2 += 512; x3 += 512; 2647 v[ 0] = (x0+t3) >> 10; 2648 v[56] = (x0-t3) >> 10; 2649 v[ 8] = (x1+t2) >> 10; 2650 v[48] = (x1-t2) >> 10; 2651 v[16] = (x2+t1) >> 10; 2652 v[40] = (x2-t1) >> 10; 2653 v[24] = (x3+t0) >> 10; 2654 v[32] = (x3-t0) >> 10; 2655 } 2656 } 2657 2658 for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) { 2659 // no fast case since the first 1D IDCT spread components out 2660 STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7]) 2661 // constants scaled things up by 1<<12, plus we had 1<<2 from first 2662 // loop, plus horizontal and vertical each scale by sqrt(8) so together 2663 // we've got an extra 1<<3, so 1<<17 total we need to remove. 2664 // so we want to round that, which means adding 0.5 * 1<<17, 2665 // aka 65536. Also, we'll end up with -128 to 127 that we want 2666 // to encode as 0..255 by adding 128, so we'll add that before the shift 2667 x0 += 65536 + (128<<17); 2668 x1 += 65536 + (128<<17); 2669 x2 += 65536 + (128<<17); 2670 x3 += 65536 + (128<<17); 2671 // tried computing the shifts into temps, or'ing the temps to see 2672 // if any were out of range, but that was slower 2673 o[0] = stbi__clamp((x0+t3) >> 17); 2674 o[7] = stbi__clamp((x0-t3) >> 17); 2675 o[1] = stbi__clamp((x1+t2) >> 17); 2676 o[6] = stbi__clamp((x1-t2) >> 17); 2677 o[2] = stbi__clamp((x2+t1) >> 17); 2678 o[5] = stbi__clamp((x2-t1) >> 17); 2679 o[3] = stbi__clamp((x3+t0) >> 17); 2680 o[4] = stbi__clamp((x3-t0) >> 17); 2681 } 2682} 2683 2684#ifdef STBI_SSE2 2685// sse2 integer IDCT. not the fastest possible implementation but it 2686// produces bit-identical results to the generic C version so it's 2687// fully "transparent". 2688static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64]) 2689{ 2690 // This is constructed to match our regular (generic) integer IDCT exactly. 2691 __m128i row0, row1, row2, row3, row4, row5, row6, row7; 2692 __m128i tmp; 2693 2694 // dot product constant: even elems=x, odd elems=y 2695 #define dct_const(x,y) _mm_setr_epi16((x),(y),(x),(y),(x),(y),(x),(y)) 2696 2697 // out(0) = c0[even]*x + c0[odd]*y (c0, x, y 16-bit, out 32-bit) 2698 // out(1) = c1[even]*x + c1[odd]*y 2699 #define dct_rot(out0,out1, x,y,c0,c1) \ 2700 __m128i c0##lo = _mm_unpacklo_epi16((x),(y)); \ 2701 __m128i c0##hi = _mm_unpackhi_epi16((x),(y)); \ 2702 __m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \ 2703 __m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \ 2704 __m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \ 2705 __m128i out1##_h = _mm_madd_epi16(c0##hi, c1) 2706 2707 // out = in << 12 (in 16-bit, out 32-bit) 2708 #define dct_widen(out, in) \ 2709 __m128i out##_l = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \ 2710 __m128i out##_h = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4) 2711 2712 // wide add 2713 #define dct_wadd(out, a, b) \ 2714 __m128i out##_l = _mm_add_epi32(a##_l, b##_l); \ 2715 __m128i out##_h = _mm_add_epi32(a##_h, b##_h) 2716 2717 // wide sub 2718 #define dct_wsub(out, a, b) \ 2719 __m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \ 2720 __m128i out##_h = _mm_sub_epi32(a##_h, b##_h) 2721 2722 // butterfly a/b, add bias, then shift by "s" and pack 2723 #define dct_bfly32o(out0, out1, a,b,bias,s) \ 2724 { \ 2725 __m128i abiased_l = _mm_add_epi32(a##_l, bias); \ 2726 __m128i abiased_h = _mm_add_epi32(a##_h, bias); \ 2727 dct_wadd(sum, abiased, b); \ 2728 dct_wsub(dif, abiased, b); \ 2729 out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), _mm_srai_epi32(sum_h, s)); \ 2730 out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), _mm_srai_epi32(dif_h, s)); \ 2731 } 2732 2733 // 8-bit interleave step (for transposes) 2734 #define dct_interleave8(a, b) \ 2735 tmp = a; \ 2736 a = _mm_unpacklo_epi8(a, b); \ 2737 b = _mm_unpackhi_epi8(tmp, b) 2738 2739 // 16-bit interleave step (for transposes) 2740 #define dct_interleave16(a, b) \ 2741 tmp = a; \ 2742 a = _mm_unpacklo_epi16(a, b); \ 2743 b = _mm_unpackhi_epi16(tmp, b) 2744 2745 #define dct_pass(bias,shift) \ 2746 do { \ 2747 /* even part */ \ 2748 dct_rot(t2e,t3e, row2,row6, rot0_0,rot0_1); \ 2749 __m128i sum04 = _mm_add_epi16(row0, row4); \ 2750 __m128i dif04 = _mm_sub_epi16(row0, row4); \ 2751 dct_widen(t0e, sum04); \ 2752 dct_widen(t1e, dif04); \ 2753 dct_wadd(x0, t0e, t3e); \ 2754 dct_wsub(x3, t0e, t3e); \ 2755 dct_wadd(x1, t1e, t2e); \ 2756 dct_wsub(x2, t1e, t2e); \ 2757 /* odd part */ \ 2758 dct_rot(y0o,y2o, row7,row3, rot2_0,rot2_1); \ 2759 dct_rot(y1o,y3o, row5,row1, rot3_0,rot3_1); \ 2760 __m128i sum17 = _mm_add_epi16(row1, row7); \ 2761 __m128i sum35 = _mm_add_epi16(row3, row5); \ 2762 dct_rot(y4o,y5o, sum17,sum35, rot1_0,rot1_1); \ 2763 dct_wadd(x4, y0o, y4o); \ 2764 dct_wadd(x5, y1o, y5o); \ 2765 dct_wadd(x6, y2o, y5o); \ 2766 dct_wadd(x7, y3o, y4o); \ 2767 dct_bfly32o(row0,row7, x0,x7,bias,shift); \ 2768 dct_bfly32o(row1,row6, x1,x6,bias,shift); \ 2769 dct_bfly32o(row2,row5, x2,x5,bias,shift); \ 2770 dct_bfly32o(row3,row4, x3,x4,bias,shift); \ 2771 } while ( 0 ) 2772 2773 __m128i rot0_0 = dct_const(stbi__f2f(0.5411961f), stbi__f2f(0.5411961f) + stbi__f2f(-1.847759065f)); 2774 __m128i rot0_1 = dct_const(stbi__f2f(0.5411961f) + stbi__f2f( 0.765366865f), stbi__f2f(0.5411961f)); 2775 __m128i rot1_0 = dct_const(stbi__f2f(1.175875602f) + stbi__f2f(-0.899976223f), stbi__f2f(1.175875602f)); 2776 __m128i rot1_1 = dct_const(stbi__f2f(1.175875602f), stbi__f2f(1.175875602f) + stbi__f2f(-2.562915447f)); 2777 __m128i rot2_0 = dct_const(stbi__f2f(-1.961570560f) + stbi__f2f( 0.298631336f), stbi__f2f(-1.961570560f)); 2778 __m128i rot2_1 = dct_const(stbi__f2f(-1.961570560f), stbi__f2f(-1.961570560f) + stbi__f2f( 3.072711026f)); 2779 __m128i rot3_0 = dct_const(stbi__f2f(-0.390180644f) + stbi__f2f( 2.053119869f), stbi__f2f(-0.390180644f)); 2780 __m128i rot3_1 = dct_const(stbi__f2f(-0.390180644f), stbi__f2f(-0.390180644f) + stbi__f2f( 1.501321110f)); 2781 2782 // rounding biases in column/row passes, see stbi__idct_block for explanation. 2783 __m128i bias_0 = _mm_set1_epi32(512); 2784 __m128i bias_1 = _mm_set1_epi32(65536 + (128<<17)); 2785 2786 // load 2787 row0 = _mm_load_si128((const __m128i *) (data + 0*8)); 2788 row1 = _mm_load_si128((const __m128i *) (data + 1*8)); 2789 row2 = _mm_load_si128((const __m128i *) (data + 2*8)); 2790 row3 = _mm_load_si128((const __m128i *) (data + 3*8)); 2791 row4 = _mm_load_si128((const __m128i *) (data + 4*8)); 2792 row5 = _mm_load_si128((const __m128i *) (data + 5*8)); 2793 row6 = _mm_load_si128((const __m128i *) (data + 6*8)); 2794 row7 = _mm_load_si128((const __m128i *) (data + 7*8)); 2795 2796 // column pass 2797 dct_pass(bias_0, 10); 2798 2799 { 2800 // 16bit 8x8 transpose pass 1 2801 dct_interleave16(row0, row4); 2802 dct_interleave16(row1, row5); 2803 dct_interleave16(row2, row6); 2804 dct_interleave16(row3, row7); 2805 2806 // transpose pass 2 2807 dct_interleave16(row0, row2); 2808 dct_interleave16(row1, row3); 2809 dct_interleave16(row4, row6); 2810 dct_interleave16(row5, row7); 2811 2812 // transpose pass 3 2813 dct_interleave16(row0, row1); 2814 dct_interleave16(row2, row3); 2815 dct_interleave16(row4, row5); 2816 dct_interleave16(row6, row7); 2817 } 2818 2819 // row pass 2820 dct_pass(bias_1, 17); 2821 2822 { 2823 // pack 2824 __m128i p0 = _mm_packus_epi16(row0, row1); // a0a1a2a3...a7b0b1b2b3...b7 2825 __m128i p1 = _mm_packus_epi16(row2, row3); 2826 __m128i p2 = _mm_packus_epi16(row4, row5); 2827 __m128i p3 = _mm_packus_epi16(row6, row7); 2828 2829 // 8bit 8x8 transpose pass 1 2830 dct_interleave8(p0, p2); // a0e0a1e1... 2831 dct_interleave8(p1, p3); // c0g0c1g1... 2832 2833 // transpose pass 2 2834 dct_interleave8(p0, p1); // a0c0e0g0... 2835 dct_interleave8(p2, p3); // b0d0f0h0... 2836 2837 // transpose pass 3 2838 dct_interleave8(p0, p2); // a0b0c0d0... 2839 dct_interleave8(p1, p3); // a4b4c4d4... 2840 2841 // store 2842 _mm_storel_epi64((__m128i *) out, p0); out += out_stride; 2843 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p0, 0x4e)); out += out_stride; 2844 _mm_storel_epi64((__m128i *) out, p2); out += out_stride; 2845 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p2, 0x4e)); out += out_stride; 2846 _mm_storel_epi64((__m128i *) out, p1); out += out_stride; 2847 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p1, 0x4e)); out += out_stride; 2848 _mm_storel_epi64((__m128i *) out, p3); out += out_stride; 2849 _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p3, 0x4e)); 2850 } 2851 2852#undef dct_const 2853#undef dct_rot 2854#undef dct_widen 2855#undef dct_wadd 2856#undef dct_wsub 2857#undef dct_bfly32o 2858#undef dct_interleave8 2859#undef dct_interleave16 2860#undef dct_pass 2861} 2862 2863#endif // STBI_SSE2 2864 2865#ifdef STBI_NEON 2866 2867// NEON integer IDCT. should produce bit-identical 2868// results to the generic C version. 2869static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64]) 2870{ 2871 int16x8_t row0, row1, row2, row3, row4, row5, row6, row7; 2872 2873 int16x4_t rot0_0 = vdup_n_s16(stbi__f2f(0.5411961f)); 2874 int16x4_t rot0_1 = vdup_n_s16(stbi__f2f(-1.847759065f)); 2875 int16x4_t rot0_2 = vdup_n_s16(stbi__f2f( 0.765366865f)); 2876 int16x4_t rot1_0 = vdup_n_s16(stbi__f2f( 1.175875602f)); 2877 int16x4_t rot1_1 = vdup_n_s16(stbi__f2f(-0.899976223f)); 2878 int16x4_t rot1_2 = vdup_n_s16(stbi__f2f(-2.562915447f)); 2879 int16x4_t rot2_0 = vdup_n_s16(stbi__f2f(-1.961570560f)); 2880 int16x4_t rot2_1 = vdup_n_s16(stbi__f2f(-0.390180644f)); 2881 int16x4_t rot3_0 = vdup_n_s16(stbi__f2f( 0.298631336f)); 2882 int16x4_t rot3_1 = vdup_n_s16(stbi__f2f( 2.053119869f)); 2883 int16x4_t rot3_2 = vdup_n_s16(stbi__f2f( 3.072711026f)); 2884 int16x4_t rot3_3 = vdup_n_s16(stbi__f2f( 1.501321110f)); 2885 2886#define dct_long_mul(out, inq, coeff) \ 2887 int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \ 2888 int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff) 2889 2890#define dct_long_mac(out, acc, inq, coeff) \ 2891 int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \ 2892 int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff) 2893 2894#define dct_widen(out, inq) \ 2895 int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \ 2896 int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12) 2897 2898// wide add 2899#define dct_wadd(out, a, b) \ 2900 int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \ 2901 int32x4_t out##_h = vaddq_s32(a##_h, b##_h) 2902 2903// wide sub 2904#define dct_wsub(out, a, b) \ 2905 int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \ 2906 int32x4_t out##_h = vsubq_s32(a##_h, b##_h) 2907 2908// butterfly a/b, then shift using "shiftop" by "s" and pack 2909#define dct_bfly32o(out0,out1, a,b,shiftop,s) \ 2910 do { \ 2911 dct_wadd(sum, a, b); \ 2912 dct_wsub(dif, a, b); \ 2913 out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \ 2914 out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \ 2915 } while ( 0 ) 2916 2917#define dct_pass(shiftop, shift) \ 2918 do { \ 2919 /* even part */ \ 2920 int16x8_t sum26 = vaddq_s16(row2, row6); \ 2921 dct_long_mul(p1e, sum26, rot0_0); \ 2922 dct_long_mac(t2e, p1e, row6, rot0_1); \ 2923 dct_long_mac(t3e, p1e, row2, rot0_2); \ 2924 int16x8_t sum04 = vaddq_s16(row0, row4); \ 2925 int16x8_t dif04 = vsubq_s16(row0, row4); \ 2926 dct_widen(t0e, sum04); \ 2927 dct_widen(t1e, dif04); \ 2928 dct_wadd(x0, t0e, t3e); \ 2929 dct_wsub(x3, t0e, t3e); \ 2930 dct_wadd(x1, t1e, t2e); \ 2931 dct_wsub(x2, t1e, t2e); \ 2932 /* odd part */ \ 2933 int16x8_t sum15 = vaddq_s16(row1, row5); \ 2934 int16x8_t sum17 = vaddq_s16(row1, row7); \ 2935 int16x8_t sum35 = vaddq_s16(row3, row5); \ 2936 int16x8_t sum37 = vaddq_s16(row3, row7); \ 2937 int16x8_t sumodd = vaddq_s16(sum17, sum35); \ 2938 dct_long_mul(p5o, sumodd, rot1_0); \ 2939 dct_long_mac(p1o, p5o, sum17, rot1_1); \ 2940 dct_long_mac(p2o, p5o, sum35, rot1_2); \ 2941 dct_long_mul(p3o, sum37, rot2_0); \ 2942 dct_long_mul(p4o, sum15, rot2_1); \ 2943 dct_wadd(sump13o, p1o, p3o); \ 2944 dct_wadd(sump24o, p2o, p4o); \ 2945 dct_wadd(sump23o, p2o, p3o); \ 2946 dct_wadd(sump14o, p1o, p4o); \ 2947 dct_long_mac(x4, sump13o, row7, rot3_0); \ 2948 dct_long_mac(x5, sump24o, row5, rot3_1); \ 2949 dct_long_mac(x6, sump23o, row3, rot3_2); \ 2950 dct_long_mac(x7, sump14o, row1, rot3_3); \ 2951 dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \ 2952 dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \ 2953 dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \ 2954 dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \ 2955 } while ( 0 ) 2956 2957 // load 2958 row0 = vld1q_s16(data + 0*8); 2959 row1 = vld1q_s16(data + 1*8); 2960 row2 = vld1q_s16(data + 2*8); 2961 row3 = vld1q_s16(data + 3*8); 2962 row4 = vld1q_s16(data + 4*8); 2963 row5 = vld1q_s16(data + 5*8); 2964 row6 = vld1q_s16(data + 6*8); 2965 row7 = vld1q_s16(data + 7*8); 2966 2967 // add DC bias 2968 row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0)); 2969 2970 // column pass 2971 dct_pass(vrshrn_n_s32, 10); 2972 2973 // 16bit 8x8 transpose 2974 { 2975// these three map to a single VTRN.16, VTRN.32, and VSWP, respectively. 2976// whether compilers actually get this is another story, sadly. 2977#define dct_trn16(x, y) do { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; } while ( 0 ) 2978#define dct_trn32(x, y) do { int32x4x2_t t = vtrnq_s32(vreinterpretq_s32_s16(x), vreinterpretq_s32_s16(y)); x = vreinterpretq_s16_s32(t.val[0]); y = vreinterpretq_s16_s32(t.val[1]); } while ( 0 ) 2979#define dct_trn64(x, y) do { int16x8_t x0 = x; int16x8_t y0 = y; x = vcombine_s16(vget_low_s16(x0), vget_low_s16(y0)); y = vcombine_s16(vget_high_s16(x0), vget_high_s16(y0)); } while ( 0 ) 2980 2981 // pass 1 2982 dct_trn16(row0, row1); // a0b0a2b2a4b4a6b6 2983 dct_trn16(row2, row3); 2984 dct_trn16(row4, row5); 2985 dct_trn16(row6, row7); 2986 2987 // pass 2 2988 dct_trn32(row0, row2); // a0b0c0d0a4b4c4d4 2989 dct_trn32(row1, row3); 2990 dct_trn32(row4, row6); 2991 dct_trn32(row5, row7); 2992 2993 // pass 3 2994 dct_trn64(row0, row4); // a0b0c0d0e0f0g0h0 2995 dct_trn64(row1, row5); 2996 dct_trn64(row2, row6); 2997 dct_trn64(row3, row7); 2998 2999#undef dct_trn16 3000#undef dct_trn32 3001#undef dct_trn64 3002 } 3003 3004 // row pass 3005 // vrshrn_n_s32 only supports shifts up to 16, we need 3006 // 17. so do a non-rounding shift of 16 first then follow 3007 // up with a rounding shift by 1. 3008 dct_pass(vshrn_n_s32, 16); 3009 3010 { 3011 // pack and round 3012 uint8x8_t p0 = vqrshrun_n_s16(row0, 1); 3013 uint8x8_t p1 = vqrshrun_n_s16(row1, 1); 3014 uint8x8_t p2 = vqrshrun_n_s16(row2, 1); 3015 uint8x8_t p3 = vqrshrun_n_s16(row3, 1); 3016 uint8x8_t p4 = vqrshrun_n_s16(row4, 1); 3017 uint8x8_t p5 = vqrshrun_n_s16(row5, 1); 3018 uint8x8_t p6 = vqrshrun_n_s16(row6, 1); 3019 uint8x8_t p7 = vqrshrun_n_s16(row7, 1); 3020 3021 // again, these can translate into one instruction, but often don't. 3022#define dct_trn8_8(x, y) do { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; } while ( 0 ) 3023#define dct_trn8_16(x, y) do { uint16x4x2_t t = vtrn_u16(vreinterpret_u16_u8(x), vreinterpret_u16_u8(y)); x = vreinterpret_u8_u16(t.val[0]); y = vreinterpret_u8_u16(t.val[1]); } while ( 0 ) 3024#define dct_trn8_32(x, y) do { uint32x2x2_t t = vtrn_u32(vreinterpret_u32_u8(x), vreinterpret_u32_u8(y)); x = vreinterpret_u8_u32(t.val[0]); y = vreinterpret_u8_u32(t.val[1]); } while ( 0 ) 3025 3026 // sadly can't use interleaved stores here since we only write 3027 // 8 bytes to each scan line! 3028 3029 // 8x8 8-bit transpose pass 1 3030 dct_trn8_8(p0, p1); 3031 dct_trn8_8(p2, p3); 3032 dct_trn8_8(p4, p5); 3033 dct_trn8_8(p6, p7); 3034 3035 // pass 2 3036 dct_trn8_16(p0, p2); 3037 dct_trn8_16(p1, p3); 3038 dct_trn8_16(p4, p6); 3039 dct_trn8_16(p5, p7); 3040 3041 // pass 3 3042 dct_trn8_32(p0, p4); 3043 dct_trn8_32(p1, p5); 3044 dct_trn8_32(p2, p6); 3045 dct_trn8_32(p3, p7); 3046 3047 // store 3048 vst1_u8(out, p0); out += out_stride; 3049 vst1_u8(out, p1); out += out_stride; 3050 vst1_u8(out, p2); out += out_stride; 3051 vst1_u8(out, p3); out += out_stride; 3052 vst1_u8(out, p4); out += out_stride; 3053 vst1_u8(out, p5); out += out_stride; 3054 vst1_u8(out, p6); out += out_stride; 3055 vst1_u8(out, p7); 3056 3057#undef dct_trn8_8 3058#undef dct_trn8_16 3059#undef dct_trn8_32 3060 } 3061 3062#undef dct_long_mul 3063#undef dct_long_mac 3064#undef dct_widen 3065#undef dct_wadd 3066#undef dct_wsub 3067#undef dct_bfly32o 3068#undef dct_pass 3069} 3070 3071#endif // STBI_NEON 3072 3073#define STBI__MARKER_none 0xff 3074// if there's a pending marker from the entropy stream, return that 3075// otherwise, fetch from the stream and get a marker. if there's no 3076// marker, return 0xff, which is never a valid marker value 3077static stbi_uc stbi__get_marker(stbi__jpeg *j) 3078{ 3079 stbi_uc x; 3080 if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none; return x; } 3081 x = stbi__get8(j->s); 3082 if (x != 0xff) return STBI__MARKER_none; 3083 while (x == 0xff) 3084 x = stbi__get8(j->s); // consume repeated 0xff fill bytes 3085 return x; 3086} 3087 3088// in each scan, we'll have scan_n components, and the order 3089// of the components is specified by order[] 3090#define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7) 3091 3092// after a restart interval, stbi__jpeg_reset the entropy decoder and 3093// the dc prediction 3094static void stbi__jpeg_reset(stbi__jpeg *j) 3095{ 3096 j->code_bits = 0; 3097 j->code_buffer = 0; 3098 j->nomore = 0; 3099 j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = j->img_comp[3].dc_pred = 0; 3100 j->marker = STBI__MARKER_none; 3101 j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff; 3102 j->eob_run = 0; 3103 // no more than 1<<31 MCUs if no restart_interal? that's plenty safe, 3104 // since we don't even allow 1<<30 pixels 3105} 3106 3107static int stbi__parse_entropy_coded_data(stbi__jpeg *z) 3108{ 3109 stbi__jpeg_reset(z); 3110 if (!z->progressive) { 3111 if (z->scan_n == 1) { 3112 int i,j; 3113 STBI_SIMD_ALIGN(short, data[64]); 3114 int n = z->order[0]; 3115 // non-interleaved data, we just need to process one block at a time, 3116 // in trivial scanline order 3117 // number of blocks to do just depends on how many actual "pixels" this 3118 // component has, independent of interleaved MCU blocking and such 3119 int w = (z->img_comp[n].x+7) >> 3; 3120 int h = (z->img_comp[n].y+7) >> 3; 3121 for (j=0; j < h; ++j) { 3122 for (i=0; i < w; ++i) { 3123 int ha = z->img_comp[n].ha; 3124 if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0; 3125 z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data); 3126 // every data block is an MCU, so countdown the restart interval 3127 if (--z->todo <= 0) { 3128 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); 3129 // if it's NOT a restart, then just bail, so we get corrupt data 3130 // rather than no data 3131 if (!STBI__RESTART(z->marker)) return 1; 3132 stbi__jpeg_reset(z); 3133 } 3134 } 3135 } 3136 return 1; 3137 } else { // interleaved 3138 int i,j,k,x,y; 3139 STBI_SIMD_ALIGN(short, data[64]); 3140 for (j=0; j < z->img_mcu_y; ++j) { 3141 for (i=0; i < z->img_mcu_x; ++i) { 3142 // scan an interleaved mcu... process scan_n components in order 3143 for (k=0; k < z->scan_n; ++k) { 3144 int n = z->order[k]; 3145 // scan out an mcu's worth of this component; that's just determined 3146 // by the basic H and V specified for the component 3147 for (y=0; y < z->img_comp[n].v; ++y) { 3148 for (x=0; x < z->img_comp[n].h; ++x) { 3149 int x2 = (i*z->img_comp[n].h + x)*8; 3150 int y2 = (j*z->img_comp[n].v + y)*8; 3151 int ha = z->img_comp[n].ha; 3152 if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0; 3153 z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data); 3154 } 3155 } 3156 } 3157 // after all interleaved components, that's an interleaved MCU, 3158 // so now count down the restart interval 3159 if (--z->todo <= 0) { 3160 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); 3161 if (!STBI__RESTART(z->marker)) return 1; 3162 stbi__jpeg_reset(z); 3163 } 3164 } 3165 } 3166 return 1; 3167 } 3168 } else { 3169 if (z->scan_n == 1) { 3170 int i,j; 3171 int n = z->order[0]; 3172 // non-interleaved data, we just need to process one block at a time, 3173 // in trivial scanline order 3174 // number of blocks to do just depends on how many actual "pixels" this 3175 // component has, independent of interleaved MCU blocking and such 3176 int w = (z->img_comp[n].x+7) >> 3; 3177 int h = (z->img_comp[n].y+7) >> 3; 3178 for (j=0; j < h; ++j) { 3179 for (i=0; i < w; ++i) { 3180 short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w); 3181 if (z->spec_start == 0) { 3182 if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n)) 3183 return 0; 3184 } else { 3185 int ha = z->img_comp[n].ha; 3186 if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha])) 3187 return 0; 3188 } 3189 // every data block is an MCU, so countdown the restart interval 3190 if (--z->todo <= 0) { 3191 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); 3192 if (!STBI__RESTART(z->marker)) return 1; 3193 stbi__jpeg_reset(z); 3194 } 3195 } 3196 } 3197 return 1; 3198 } else { // interleaved 3199 int i,j,k,x,y; 3200 for (j=0; j < z->img_mcu_y; ++j) { 3201 for (i=0; i < z->img_mcu_x; ++i) { 3202 // scan an interleaved mcu... process scan_n components in order 3203 for (k=0; k < z->scan_n; ++k) { 3204 int n = z->order[k]; 3205 // scan out an mcu's worth of this component; that's just determined 3206 // by the basic H and V specified for the component 3207 for (y=0; y < z->img_comp[n].v; ++y) { 3208 for (x=0; x < z->img_comp[n].h; ++x) { 3209 int x2 = (i*z->img_comp[n].h + x); 3210 int y2 = (j*z->img_comp[n].v + y); 3211 short *data = z->img_comp[n].coeff + 64 * (x2 + y2 * z->img_comp[n].coeff_w); 3212 if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n)) 3213 return 0; 3214 } 3215 } 3216 } 3217 // after all interleaved components, that's an interleaved MCU, 3218 // so now count down the restart interval 3219 if (--z->todo <= 0) { 3220 if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); 3221 if (!STBI__RESTART(z->marker)) return 1; 3222 stbi__jpeg_reset(z); 3223 } 3224 } 3225 } 3226 return 1; 3227 } 3228 } 3229} 3230 3231static void stbi__jpeg_dequantize(short *data, stbi__uint16 *dequant) 3232{ 3233 int i; 3234 for (i=0; i < 64; ++i) 3235 data[i] *= dequant[i]; 3236} 3237 3238static void stbi__jpeg_finish(stbi__jpeg *z) 3239{ 3240 if (z->progressive) { 3241 // dequantize and idct the data 3242 int i,j,n; 3243 for (n=0; n < z->s->img_n; ++n) { 3244 int w = (z->img_comp[n].x+7) >> 3; 3245 int h = (z->img_comp[n].y+7) >> 3; 3246 for (j=0; j < h; ++j) { 3247 for (i=0; i < w; ++i) { 3248 short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w); 3249 stbi__jpeg_dequantize(data, z->dequant[z->img_comp[n].tq]); 3250 z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data); 3251 } 3252 } 3253 } 3254 } 3255} 3256 3257static int stbi__process_marker(stbi__jpeg *z, int m) 3258{ 3259 int L; 3260 switch (m) { 3261 case STBI__MARKER_none: // no marker found 3262 return stbi__err("expected marker","Corrupt JPEG"); 3263 3264 case 0xDD: // DRI - specify restart interval 3265 if (stbi__get16be(z->s) != 4) return stbi__err("bad DRI len","Corrupt JPEG"); 3266 z->restart_interval = stbi__get16be(z->s); 3267 return 1; 3268 3269 case 0xDB: // DQT - define quantization table 3270 L = stbi__get16be(z->s)-2; 3271 while (L > 0) { 3272 int q = stbi__get8(z->s); 3273 int p = q >> 4, sixteen = (p != 0); 3274 int t = q & 15,i; 3275 if (p != 0 && p != 1) return stbi__err("bad DQT type","Corrupt JPEG"); 3276 if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG"); 3277 3278 for (i=0; i < 64; ++i) 3279 z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s)); 3280 L -= (sixteen ? 129 : 65); 3281 } 3282 return L==0; 3283 3284 case 0xC4: // DHT - define huffman table 3285 L = stbi__get16be(z->s)-2; 3286 while (L > 0) { 3287 stbi_uc *v; 3288 int sizes[16],i,n=0; 3289 int q = stbi__get8(z->s); 3290 int tc = q >> 4; 3291 int th = q & 15; 3292 if (tc > 1 || th > 3) return stbi__err("bad DHT header","Corrupt JPEG"); 3293 for (i=0; i < 16; ++i) { 3294 sizes[i] = stbi__get8(z->s); 3295 n += sizes[i]; 3296 } 3297 if(n > 256) return stbi__err("bad DHT header","Corrupt JPEG"); // Loop over i < n would write past end of values! 3298 L -= 17; 3299 if (tc == 0) { 3300 if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0; 3301 v = z->huff_dc[th].values; 3302 } else { 3303 if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0; 3304 v = z->huff_ac[th].values; 3305 } 3306 for (i=0; i < n; ++i) 3307 v[i] = stbi__get8(z->s); 3308 if (tc != 0) 3309 stbi__build_fast_ac(z->fast_ac[th], z->huff_ac + th); 3310 L -= n; 3311 } 3312 return L==0; 3313 } 3314 3315 // check for comment block or APP blocks 3316 if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) { 3317 L = stbi__get16be(z->s); 3318 if (L < 2) { 3319 if (m == 0xFE) 3320 return stbi__err("bad COM len","Corrupt JPEG"); 3321 else 3322 return stbi__err("bad APP len","Corrupt JPEG"); 3323 } 3324 L -= 2; 3325 3326 if (m == 0xE0 && L >= 5) { // JFIF APP0 segment 3327 static const unsigned char tag[5] = {'J','F','I','F','\0'}; 3328 int ok = 1; 3329 int i; 3330 for (i=0; i < 5; ++i) 3331 if (stbi__get8(z->s) != tag[i]) 3332 ok = 0; 3333 L -= 5; 3334 if (ok) 3335 z->jfif = 1; 3336 } else if (m == 0xEE && L >= 12) { // Adobe APP14 segment 3337 static const unsigned char tag[6] = {'A','d','o','b','e','\0'}; 3338 int ok = 1; 3339 int i; 3340 for (i=0; i < 6; ++i) 3341 if (stbi__get8(z->s) != tag[i]) 3342 ok = 0; 3343 L -= 6; 3344 if (ok) { 3345 stbi__get8(z->s); // version 3346 stbi__get16be(z->s); // flags0 3347 stbi__get16be(z->s); // flags1 3348 z->app14_color_transform = stbi__get8(z->s); // color transform 3349 L -= 6; 3350 } 3351 } 3352 3353 stbi__skip(z->s, L); 3354 return 1; 3355 } 3356 3357 return stbi__err("unknown marker","Corrupt JPEG"); 3358} 3359 3360// after we see SOS 3361static int stbi__process_scan_header(stbi__jpeg *z) 3362{ 3363 int i; 3364 int Ls = stbi__get16be(z->s); 3365 z->scan_n = stbi__get8(z->s); 3366 if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s->img_n) return stbi__err("bad SOS component count","Corrupt JPEG"); 3367 if (Ls != 6+2*z->scan_n) return stbi__err("bad SOS len","Corrupt JPEG"); 3368 for (i=0; i < z->scan_n; ++i) { 3369 int id = stbi__get8(z->s), which; 3370 int q = stbi__get8(z->s); 3371 for (which = 0; which < z->s->img_n; ++which) 3372 if (z->img_comp[which].id == id) 3373 break; 3374 if (which == z->s->img_n) return 0; // no match 3375 z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return stbi__err("bad DC huff","Corrupt JPEG"); 3376 z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return stbi__err("bad AC huff","Corrupt JPEG"); 3377 z->order[i] = which; 3378 } 3379 3380 { 3381 int aa; 3382 z->spec_start = stbi__get8(z->s); 3383 z->spec_end = stbi__get8(z->s); // should be 63, but might be 0 3384 aa = stbi__get8(z->s); 3385 z->succ_high = (aa >> 4); 3386 z->succ_low = (aa & 15); 3387 if (z->progressive) { 3388 if (z->spec_start > 63 || z->spec_end > 63 || z->spec_start > z->spec_end || z->succ_high > 13 || z->succ_low > 13) 3389 return stbi__err("bad SOS", "Corrupt JPEG"); 3390 } else { 3391 if (z->spec_start != 0) return stbi__err("bad SOS","Corrupt JPEG"); 3392 if (z->succ_high != 0 || z->succ_low != 0) return stbi__err("bad SOS","Corrupt JPEG"); 3393 z->spec_end = 63; 3394 } 3395 } 3396 3397 return 1; 3398} 3399 3400static int stbi__free_jpeg_components(stbi__jpeg *z, int ncomp, int why) 3401{ 3402 int i; 3403 for (i=0; i < ncomp; ++i) { 3404 if (z->img_comp[i].raw_data) { 3405 STBI_FREE(z->img_comp[i].raw_data); 3406 z->img_comp[i].raw_data = NULL; 3407 z->img_comp[i].data = NULL; 3408 } 3409 if (z->img_comp[i].raw_coeff) { 3410 STBI_FREE(z->img_comp[i].raw_coeff); 3411 z->img_comp[i].raw_coeff = NULL; 3412 z->img_comp[i].coeff = NULL; 3413 } 3414 if (z->img_comp[i].linebuf) { 3415 STBI_FREE(z->img_comp[i].linebuf); 3416 z->img_comp[i].linebuf = NULL; 3417 } 3418 } 3419 return why; 3420} 3421 3422static int stbi__process_frame_header(stbi__jpeg *z, int scan) 3423{ 3424 stbi__context *s = z->s; 3425 int Lf,p,i,q, h_max=1,v_max=1,c; 3426 Lf = stbi__get16be(s); if (Lf < 11) return stbi__err("bad SOF len","Corrupt JPEG"); // JPEG 3427 p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline 3428 s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG 3429 s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires 3430 if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); 3431 if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); 3432 c = stbi__get8(s); 3433 if (c != 3 && c != 1 && c != 4) return stbi__err("bad component count","Corrupt JPEG"); 3434 s->img_n = c; 3435 for (i=0; i < c; ++i) { 3436 z->img_comp[i].data = NULL; 3437 z->img_comp[i].linebuf = NULL; 3438 } 3439 3440 if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len","Corrupt JPEG"); 3441 3442 z->rgb = 0; 3443 for (i=0; i < s->img_n; ++i) { 3444 static const unsigned char rgb[3] = { 'R', 'G', 'B' }; 3445 z->img_comp[i].id = stbi__get8(s); 3446 if (s->img_n == 3 && z->img_comp[i].id == rgb[i]) 3447 ++z->rgb; 3448 q = stbi__get8(s); 3449 z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return stbi__err("bad H","Corrupt JPEG"); 3450 z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return stbi__err("bad V","Corrupt JPEG"); 3451 z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ","Corrupt JPEG"); 3452 } 3453 3454 if (scan != STBI__SCAN_load) return 1; 3455 3456 if (!stbi__mad3sizes_valid(s->img_x, s->img_y, s->img_n, 0)) return stbi__err("too large", "Image too large to decode"); 3457 3458 for (i=0; i < s->img_n; ++i) { 3459 if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h; 3460 if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v; 3461 } 3462 3463 // check that plane subsampling factors are integer ratios; our resamplers can't deal with fractional ratios 3464 // and I've never seen a non-corrupted JPEG file actually use them 3465 for (i=0; i < s->img_n; ++i) { 3466 if (h_max % z->img_comp[i].h != 0) return stbi__err("bad H","Corrupt JPEG"); 3467 if (v_max % z->img_comp[i].v != 0) return stbi__err("bad V","Corrupt JPEG"); 3468 } 3469 3470 // compute interleaved mcu info 3471 z->img_h_max = h_max; 3472 z->img_v_max = v_max; 3473 z->img_mcu_w = h_max * 8; 3474 z->img_mcu_h = v_max * 8; 3475 // these sizes can't be more than 17 bits 3476 z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w; 3477 z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h; 3478 3479 for (i=0; i < s->img_n; ++i) { 3480 // number of effective pixels (e.g. for non-interleaved MCU) 3481 z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max; 3482 z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max; 3483 // to simplify generation, we'll allocate enough memory to decode 3484 // the bogus oversized data from using interleaved MCUs and their 3485 // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't 3486 // discard the extra data until colorspace conversion 3487 // 3488 // img_mcu_x, img_mcu_y: <=17 bits; comp[i].h and .v are <=4 (checked earlier) 3489 // so these muls can't overflow with 32-bit ints (which we require) 3490 z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8; 3491 z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8; 3492 z->img_comp[i].coeff = NULL; 3493 z->img_comp[i].raw_coeff = NULL; 3494 z->img_comp[i].linebuf = NULL; 3495 z->img_comp[i].raw_data = stbi__malloc_mad2(z->img_comp[i].w2, z->img_comp[i].h2, 15); 3496 if (z->img_comp[i].raw_data == NULL) 3497 return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory")); 3498 // align blocks for idct using mmx/sse 3499 z->img_comp[i].data = (stbi_uc*) (((size_t) z->img_comp[i].raw_data + 15) & ~15); 3500 if (z->progressive) { 3501 // w2, h2 are multiples of 8 (see above) 3502 z->img_comp[i].coeff_w = z->img_comp[i].w2 / 8; 3503 z->img_comp[i].coeff_h = z->img_comp[i].h2 / 8; 3504 z->img_comp[i].raw_coeff = stbi__malloc_mad3(z->img_comp[i].w2, z->img_comp[i].h2, sizeof(short), 15); 3505 if (z->img_comp[i].raw_coeff == NULL) 3506 return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory")); 3507 z->img_comp[i].coeff = (short*) (((size_t) z->img_comp[i].raw_coeff + 15) & ~15); 3508 } 3509 } 3510 3511 return 1; 3512} 3513 3514// use comparisons since in some cases we handle more than one case (e.g. SOF) 3515#define stbi__DNL(x) ((x) == 0xdc) 3516#define stbi__SOI(x) ((x) == 0xd8) 3517#define stbi__EOI(x) ((x) == 0xd9) 3518#define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2) 3519#define stbi__SOS(x) ((x) == 0xda) 3520 3521#define stbi__SOF_progressive(x) ((x) == 0xc2) 3522 3523static int stbi__decode_jpeg_header(stbi__jpeg *z, int scan) 3524{ 3525 int m; 3526 z->jfif = 0; 3527 z->app14_color_transform = -1; // valid values are 0,1,2 3528 z->marker = STBI__MARKER_none; // initialize cached marker to empty 3529 m = stbi__get_marker(z); 3530 if (!stbi__SOI(m)) return stbi__err("no SOI","Corrupt JPEG"); 3531 if (scan == STBI__SCAN_type) return 1; 3532 m = stbi__get_marker(z); 3533 while (!stbi__SOF(m)) { 3534 if (!stbi__process_marker(z,m)) return 0; 3535 m = stbi__get_marker(z); 3536 while (m == STBI__MARKER_none) { 3537 // some files have extra padding after their blocks, so ok, we'll scan 3538 if (stbi__at_eof(z->s)) return stbi__err("no SOF", "Corrupt JPEG"); 3539 m = stbi__get_marker(z); 3540 } 3541 } 3542 z->progressive = stbi__SOF_progressive(m); 3543 if (!stbi__process_frame_header(z, scan)) return 0; 3544 return 1; 3545} 3546 3547static stbi_uc stbi__skip_jpeg_junk_at_end(stbi__jpeg *j) 3548{ 3549 // some JPEGs have junk at end, skip over it but if we find what looks 3550 // like a valid marker, resume there 3551 while (!stbi__at_eof(j->s)) { 3552 stbi_uc x = stbi__get8(j->s); 3553 while (x == 0xff) { // might be a marker 3554 if (stbi__at_eof(j->s)) return STBI__MARKER_none; 3555 x = stbi__get8(j->s); 3556 if (x != 0x00 && x != 0xff) { 3557 // not a stuffed zero or lead-in to another marker, looks 3558 // like an actual marker, return it 3559 return x; 3560 } 3561 // stuffed zero has x=0 now which ends the loop, meaning we go 3562 // back to regular scan loop. 3563 // repeated 0xff keeps trying to read the next byte of the marker. 3564 } 3565 } 3566 return STBI__MARKER_none; 3567} 3568 3569// decode image to YCbCr format 3570static int stbi__decode_jpeg_image(stbi__jpeg *j) 3571{ 3572 int m; 3573 for (m = 0; m < 4; m++) { 3574 j->img_comp[m].raw_data = NULL; 3575 j->img_comp[m].raw_coeff = NULL; 3576 } 3577 j->restart_interval = 0; 3578 if (!stbi__decode_jpeg_header(j, STBI__SCAN_load)) return 0; 3579 m = stbi__get_marker(j); 3580 while (!stbi__EOI(m)) { 3581 if (stbi__SOS(m)) { 3582 if (!stbi__process_scan_header(j)) return 0; 3583 if (!stbi__parse_entropy_coded_data(j)) return 0; 3584 if (j->marker == STBI__MARKER_none ) { 3585 j->marker = stbi__skip_jpeg_junk_at_end(j); 3586 // if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0 3587 } 3588 m = stbi__get_marker(j); 3589 if (STBI__RESTART(m)) 3590 m = stbi__get_marker(j); 3591 } else if (stbi__DNL(m)) { 3592 int Ld = stbi__get16be(j->s); 3593 stbi__uint32 NL = stbi__get16be(j->s); 3594 if (Ld != 4) return stbi__err("bad DNL len", "Corrupt JPEG"); 3595 if (NL != j->s->img_y) return stbi__err("bad DNL height", "Corrupt JPEG"); 3596 m = stbi__get_marker(j); 3597 } else { 3598 if (!stbi__process_marker(j, m)) return 1; 3599 m = stbi__get_marker(j); 3600 } 3601 } 3602 if (j->progressive) 3603 stbi__jpeg_finish(j); 3604 return 1; 3605} 3606 3607// static jfif-centered resampling (across block boundaries) 3608 3609typedef stbi_uc *(*resample_row_func)(stbi_uc *out, stbi_uc *in0, stbi_uc *in1, 3610 int w, int hs); 3611 3612#define stbi__div4(x) ((stbi_uc) ((x) >> 2)) 3613 3614static stbi_uc *resample_row_1(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) 3615{ 3616 STBI_NOTUSED(out); 3617 STBI_NOTUSED(in_far); 3618 STBI_NOTUSED(w); 3619 STBI_NOTUSED(hs); 3620 return in_near; 3621} 3622 3623static stbi_uc* stbi__resample_row_v_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) 3624{ 3625 // need to generate two samples vertically for every one in input 3626 int i; 3627 STBI_NOTUSED(hs); 3628 for (i=0; i < w; ++i) 3629 out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2); 3630 return out; 3631} 3632 3633static stbi_uc* stbi__resample_row_h_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) 3634{ 3635 // need to generate two samples horizontally for every one in input 3636 int i; 3637 stbi_uc *input = in_near; 3638 3639 if (w == 1) { 3640 // if only one sample, can't do any interpolation 3641 out[0] = out[1] = input[0]; 3642 return out; 3643 } 3644 3645 out[0] = input[0]; 3646 out[1] = stbi__div4(input[0]*3 + input[1] + 2); 3647 for (i=1; i < w-1; ++i) { 3648 int n = 3*input[i]+2; 3649 out[i*2+0] = stbi__div4(n+input[i-1]); 3650 out[i*2+1] = stbi__div4(n+input[i+1]); 3651 } 3652 out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2); 3653 out[i*2+1] = input[w-1]; 3654 3655 STBI_NOTUSED(in_far); 3656 STBI_NOTUSED(hs); 3657 3658 return out; 3659} 3660 3661#define stbi__div16(x) ((stbi_uc) ((x) >> 4)) 3662 3663static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) 3664{ 3665 // need to generate 2x2 samples for every one in input 3666 int i,t0,t1; 3667 if (w == 1) { 3668 out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2); 3669 return out; 3670 } 3671 3672 t1 = 3*in_near[0] + in_far[0]; 3673 out[0] = stbi__div4(t1+2); 3674 for (i=1; i < w; ++i) { 3675 t0 = t1; 3676 t1 = 3*in_near[i]+in_far[i]; 3677 out[i*2-1] = stbi__div16(3*t0 + t1 + 8); 3678 out[i*2 ] = stbi__div16(3*t1 + t0 + 8); 3679 } 3680 out[w*2-1] = stbi__div4(t1+2); 3681 3682 STBI_NOTUSED(hs); 3683 3684 return out; 3685} 3686 3687#if defined(STBI_SSE2) || defined(STBI_NEON) 3688static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) 3689{ 3690 // need to generate 2x2 samples for every one in input 3691 int i=0,t0,t1; 3692 3693 if (w == 1) { 3694 out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2); 3695 return out; 3696 } 3697 3698 t1 = 3*in_near[0] + in_far[0]; 3699 // process groups of 8 pixels for as long as we can. 3700 // note we can't handle the last pixel in a row in this loop 3701 // because we need to handle the filter boundary conditions. 3702 for (; i < ((w-1) & ~7); i += 8) { 3703#if defined(STBI_SSE2) 3704 // load and perform the vertical filtering pass 3705 // this uses 3*x + y = 4*x + (y - x) 3706 __m128i zero = _mm_setzero_si128(); 3707 __m128i farb = _mm_loadl_epi64((__m128i *) (in_far + i)); 3708 __m128i nearb = _mm_loadl_epi64((__m128i *) (in_near + i)); 3709 __m128i farw = _mm_unpacklo_epi8(farb, zero); 3710 __m128i nearw = _mm_unpacklo_epi8(nearb, zero); 3711 __m128i diff = _mm_sub_epi16(farw, nearw); 3712 __m128i nears = _mm_slli_epi16(nearw, 2); 3713 __m128i curr = _mm_add_epi16(nears, diff); // current row 3714 3715 // horizontal filter works the same based on shifted vers of current 3716 // row. "prev" is current row shifted right by 1 pixel; we need to 3717 // insert the previous pixel value (from t1). 3718 // "next" is current row shifted left by 1 pixel, with first pixel 3719 // of next block of 8 pixels added in. 3720 __m128i prv0 = _mm_slli_si128(curr, 2); 3721 __m128i nxt0 = _mm_srli_si128(curr, 2); 3722 __m128i prev = _mm_insert_epi16(prv0, t1, 0); 3723 __m128i next = _mm_insert_epi16(nxt0, 3*in_near[i+8] + in_far[i+8], 7); 3724 3725 // horizontal filter, polyphase implementation since it's convenient: 3726 // even pixels = 3*cur + prev = cur*4 + (prev - cur) 3727 // odd pixels = 3*cur + next = cur*4 + (next - cur) 3728 // note the shared term. 3729 __m128i bias = _mm_set1_epi16(8); 3730 __m128i curs = _mm_slli_epi16(curr, 2); 3731 __m128i prvd = _mm_sub_epi16(prev, curr); 3732 __m128i nxtd = _mm_sub_epi16(next, curr); 3733 __m128i curb = _mm_add_epi16(curs, bias); 3734 __m128i even = _mm_add_epi16(prvd, curb); 3735 __m128i odd = _mm_add_epi16(nxtd, curb); 3736 3737 // interleave even and odd pixels, then undo scaling. 3738 __m128i int0 = _mm_unpacklo_epi16(even, odd); 3739 __m128i int1 = _mm_unpackhi_epi16(even, odd); 3740 __m128i de0 = _mm_srli_epi16(int0, 4); 3741 __m128i de1 = _mm_srli_epi16(int1, 4); 3742 3743 // pack and write output 3744 __m128i outv = _mm_packus_epi16(de0, de1); 3745 _mm_storeu_si128((__m128i *) (out + i*2), outv); 3746#elif defined(STBI_NEON) 3747 // load and perform the vertical filtering pass 3748 // this uses 3*x + y = 4*x + (y - x) 3749 uint8x8_t farb = vld1_u8(in_far + i); 3750 uint8x8_t nearb = vld1_u8(in_near + i); 3751 int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb)); 3752 int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2)); 3753 int16x8_t curr = vaddq_s16(nears, diff); // current row 3754 3755 // horizontal filter works the same based on shifted vers of current 3756 // row. "prev" is current row shifted right by 1 pixel; we need to 3757 // insert the previous pixel value (from t1). 3758 // "next" is current row shifted left by 1 pixel, with first pixel 3759 // of next block of 8 pixels added in. 3760 int16x8_t prv0 = vextq_s16(curr, curr, 7); 3761 int16x8_t nxt0 = vextq_s16(curr, curr, 1); 3762 int16x8_t prev = vsetq_lane_s16(t1, prv0, 0); 3763 int16x8_t next = vsetq_lane_s16(3*in_near[i+8] + in_far[i+8], nxt0, 7); 3764 3765 // horizontal filter, polyphase implementation since it's convenient: 3766 // even pixels = 3*cur + prev = cur*4 + (prev - cur) 3767 // odd pixels = 3*cur + next = cur*4 + (next - cur) 3768 // note the shared term. 3769 int16x8_t curs = vshlq_n_s16(curr, 2); 3770 int16x8_t prvd = vsubq_s16(prev, curr); 3771 int16x8_t nxtd = vsubq_s16(next, curr); 3772 int16x8_t even = vaddq_s16(curs, prvd); 3773 int16x8_t odd = vaddq_s16(curs, nxtd); 3774 3775 // undo scaling and round, then store with even/odd phases interleaved 3776 uint8x8x2_t o; 3777 o.val[0] = vqrshrun_n_s16(even, 4); 3778 o.val[1] = vqrshrun_n_s16(odd, 4); 3779 vst2_u8(out + i*2, o); 3780#endif 3781 3782 // "previous" value for next iter 3783 t1 = 3*in_near[i+7] + in_far[i+7]; 3784 } 3785 3786 t0 = t1; 3787 t1 = 3*in_near[i] + in_far[i]; 3788 out[i*2] = stbi__div16(3*t1 + t0 + 8); 3789 3790 for (++i; i < w; ++i) { 3791 t0 = t1; 3792 t1 = 3*in_near[i]+in_far[i]; 3793 out[i*2-1] = stbi__div16(3*t0 + t1 + 8); 3794 out[i*2 ] = stbi__div16(3*t1 + t0 + 8); 3795 } 3796 out[w*2-1] = stbi__div4(t1+2); 3797 3798 STBI_NOTUSED(hs); 3799 3800 return out; 3801} 3802#endif 3803 3804static stbi_uc *stbi__resample_row_generic(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) 3805{ 3806 // resample with nearest-neighbor 3807 int i,j; 3808 STBI_NOTUSED(in_far); 3809 for (i=0; i < w; ++i) 3810 for (j=0; j < hs; ++j) 3811 out[i*hs+j] = in_near[i]; 3812 return out; 3813} 3814 3815// this is a reduced-precision calculation of YCbCr-to-RGB introduced 3816// to make sure the code produces the same results in both SIMD and scalar 3817#define stbi__float2fixed(x) (((int) ((x) * 4096.0f + 0.5f)) << 8) 3818static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step) 3819{ 3820 int i; 3821 for (i=0; i < count; ++i) { 3822 int y_fixed = (y[i] << 20) + (1<<19); // rounding 3823 int r,g,b; 3824 int cr = pcr[i] - 128; 3825 int cb = pcb[i] - 128; 3826 r = y_fixed + cr* stbi__float2fixed(1.40200f); 3827 g = y_fixed + (cr*-stbi__float2fixed(0.71414f)) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000); 3828 b = y_fixed + cb* stbi__float2fixed(1.77200f); 3829 r >>= 20; 3830 g >>= 20; 3831 b >>= 20; 3832 if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; } 3833 if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; } 3834 if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; } 3835 out[0] = (stbi_uc)r; 3836 out[1] = (stbi_uc)g; 3837 out[2] = (stbi_uc)b; 3838 out[3] = 255; 3839 out += step; 3840 } 3841} 3842 3843#if defined(STBI_SSE2) || defined(STBI_NEON) 3844static void stbi__YCbCr_to_RGB_simd(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step) 3845{ 3846 int i = 0; 3847 3848#ifdef STBI_SSE2 3849 // step == 3 is pretty ugly on the final interleave, and i'm not convinced 3850 // it's useful in practice (you wouldn't use it for textures, for example). 3851 // so just accelerate step == 4 case. 3852 if (step == 4) { 3853 // this is a fairly straightforward implementation and not super-optimized. 3854 __m128i signflip = _mm_set1_epi8(-0x80); 3855 __m128i cr_const0 = _mm_set1_epi16( (short) ( 1.40200f*4096.0f+0.5f)); 3856 __m128i cr_const1 = _mm_set1_epi16( - (short) ( 0.71414f*4096.0f+0.5f)); 3857 __m128i cb_const0 = _mm_set1_epi16( - (short) ( 0.34414f*4096.0f+0.5f)); 3858 __m128i cb_const1 = _mm_set1_epi16( (short) ( 1.77200f*4096.0f+0.5f)); 3859 __m128i y_bias = _mm_set1_epi8((char) (unsigned char) 128); 3860 __m128i xw = _mm_set1_epi16(255); // alpha channel 3861 3862 for (; i+7 < count; i += 8) { 3863 // load 3864 __m128i y_bytes = _mm_loadl_epi64((__m128i *) (y+i)); 3865 __m128i cr_bytes = _mm_loadl_epi64((__m128i *) (pcr+i)); 3866 __m128i cb_bytes = _mm_loadl_epi64((__m128i *) (pcb+i)); 3867 __m128i cr_biased = _mm_xor_si128(cr_bytes, signflip); // -128 3868 __m128i cb_biased = _mm_xor_si128(cb_bytes, signflip); // -128 3869 3870 // unpack to short (and left-shift cr, cb by 8) 3871 __m128i yw = _mm_unpacklo_epi8(y_bias, y_bytes); 3872 __m128i crw = _mm_unpacklo_epi8(_mm_setzero_si128(), cr_biased); 3873 __m128i cbw = _mm_unpacklo_epi8(_mm_setzero_si128(), cb_biased); 3874 3875 // color transform 3876 __m128i yws = _mm_srli_epi16(yw, 4); 3877 __m128i cr0 = _mm_mulhi_epi16(cr_const0, crw); 3878 __m128i cb0 = _mm_mulhi_epi16(cb_const0, cbw); 3879 __m128i cb1 = _mm_mulhi_epi16(cbw, cb_const1); 3880 __m128i cr1 = _mm_mulhi_epi16(crw, cr_const1); 3881 __m128i rws = _mm_add_epi16(cr0, yws); 3882 __m128i gwt = _mm_add_epi16(cb0, yws); 3883 __m128i bws = _mm_add_epi16(yws, cb1); 3884 __m128i gws = _mm_add_epi16(gwt, cr1); 3885 3886 // descale 3887 __m128i rw = _mm_srai_epi16(rws, 4); 3888 __m128i bw = _mm_srai_epi16(bws, 4); 3889 __m128i gw = _mm_srai_epi16(gws, 4); 3890 3891 // back to byte, set up for transpose 3892 __m128i brb = _mm_packus_epi16(rw, bw); 3893 __m128i gxb = _mm_packus_epi16(gw, xw); 3894 3895 // transpose to interleave channels 3896 __m128i t0 = _mm_unpacklo_epi8(brb, gxb); 3897 __m128i t1 = _mm_unpackhi_epi8(brb, gxb); 3898 __m128i o0 = _mm_unpacklo_epi16(t0, t1); 3899 __m128i o1 = _mm_unpackhi_epi16(t0, t1); 3900 3901 // store 3902 _mm_storeu_si128((__m128i *) (out + 0), o0); 3903 _mm_storeu_si128((__m128i *) (out + 16), o1); 3904 out += 32; 3905 } 3906 } 3907#endif 3908 3909#ifdef STBI_NEON 3910 // in this version, step=3 support would be easy to add. but is there demand? 3911 if (step == 4) { 3912 // this is a fairly straightforward implementation and not super-optimized. 3913 uint8x8_t signflip = vdup_n_u8(0x80); 3914 int16x8_t cr_const0 = vdupq_n_s16( (short) ( 1.40200f*4096.0f+0.5f)); 3915 int16x8_t cr_const1 = vdupq_n_s16( - (short) ( 0.71414f*4096.0f+0.5f)); 3916 int16x8_t cb_const0 = vdupq_n_s16( - (short) ( 0.34414f*4096.0f+0.5f)); 3917 int16x8_t cb_const1 = vdupq_n_s16( (short) ( 1.77200f*4096.0f+0.5f)); 3918 3919 for (; i+7 < count; i += 8) { 3920 // load 3921 uint8x8_t y_bytes = vld1_u8(y + i); 3922 uint8x8_t cr_bytes = vld1_u8(pcr + i); 3923 uint8x8_t cb_bytes = vld1_u8(pcb + i); 3924 int8x8_t cr_biased = vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip)); 3925 int8x8_t cb_biased = vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip)); 3926 3927 // expand to s16 3928 int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4)); 3929 int16x8_t crw = vshll_n_s8(cr_biased, 7); 3930 int16x8_t cbw = vshll_n_s8(cb_biased, 7); 3931 3932 // color transform 3933 int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0); 3934 int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0); 3935 int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1); 3936 int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1); 3937 int16x8_t rws = vaddq_s16(yws, cr0); 3938 int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1); 3939 int16x8_t bws = vaddq_s16(yws, cb1); 3940 3941 // undo scaling, round, convert to byte 3942 uint8x8x4_t o; 3943 o.val[0] = vqrshrun_n_s16(rws, 4); 3944 o.val[1] = vqrshrun_n_s16(gws, 4); 3945 o.val[2] = vqrshrun_n_s16(bws, 4); 3946 o.val[3] = vdup_n_u8(255); 3947 3948 // store, interleaving r/g/b/a 3949 vst4_u8(out, o); 3950 out += 8*4; 3951 } 3952 } 3953#endif 3954 3955 for (; i < count; ++i) { 3956 int y_fixed = (y[i] << 20) + (1<<19); // rounding 3957 int r,g,b; 3958 int cr = pcr[i] - 128; 3959 int cb = pcb[i] - 128; 3960 r = y_fixed + cr* stbi__float2fixed(1.40200f); 3961 g = y_fixed + cr*-stbi__float2fixed(0.71414f) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000); 3962 b = y_fixed + cb* stbi__float2fixed(1.77200f); 3963 r >>= 20; 3964 g >>= 20; 3965 b >>= 20; 3966 if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; } 3967 if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; } 3968 if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; } 3969 out[0] = (stbi_uc)r; 3970 out[1] = (stbi_uc)g; 3971 out[2] = (stbi_uc)b; 3972 out[3] = 255; 3973 out += step; 3974 } 3975} 3976#endif 3977 3978// set up the kernels 3979static void stbi__setup_jpeg(stbi__jpeg *j) 3980{ 3981 j->idct_block_kernel = stbi__idct_block; 3982 j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row; 3983 j->resample_row_hv_2_kernel = stbi__resample_row_hv_2; 3984 3985#ifdef STBI_SSE2 3986 if (stbi__sse2_available()) { 3987 j->idct_block_kernel = stbi__idct_simd; 3988 j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd; 3989 j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd; 3990 } 3991#endif 3992 3993#ifdef STBI_NEON 3994 if (SDL_HasNEON()) { /* SDL change */ 3995 j->idct_block_kernel = stbi__idct_simd; 3996 j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd; 3997 j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd; 3998 } /**/ 3999#endif 4000} 4001 4002// clean up the temporary component buffers 4003static void stbi__cleanup_jpeg(stbi__jpeg *j) 4004{ 4005 stbi__free_jpeg_components(j, j->s->img_n, 0); 4006} 4007 4008typedef struct 4009{ 4010 resample_row_func resample; 4011 stbi_uc *line0,*line1; 4012 int hs,vs; // expansion factor in each axis 4013 int w_lores; // horizontal pixels pre-expansion 4014 int ystep; // how far through vertical expansion we are 4015 int ypos; // which pre-expansion row we're on 4016} stbi__resample; 4017 4018// fast 0..255 * 0..255 => 0..255 rounded multiplication 4019static stbi_uc stbi__blinn_8x8(stbi_uc x, stbi_uc y) 4020{ 4021 unsigned int t = x*y + 128; 4022 return (stbi_uc) ((t + (t >>8)) >> 8); 4023} 4024 4025static stbi_uc *output_jpeg_nv12(stbi__jpeg *z, stbi__nv12 *nv12) 4026{ 4027 unsigned int i,j; 4028 4029 // Copy the Y plane 4030 if (nv12->pitch == (int)z->s->img_x) { 4031 memcpy(nv12->y, z->img_comp[0].data, z->s->img_y * z->s->img_x); 4032 } else { 4033 for (i=0; i < z->s->img_y; ++i) { 4034 memcpy(nv12->y + i * nv12->pitch, z->img_comp[0].data + i * z->s->img_x, z->s->img_x); 4035 } 4036 } 4037 4038 if (z->s->img_n == 3) { 4039 // NV12: U and V are interleaved, each subsampled by 2 4040 const int nv12_hs = 2; 4041 const int nv12_vs = 2; 4042 const int u_hs = (z->img_h_max / z->img_comp[1].h); 4043 const int u_vs = (z->img_v_max / z->img_comp[1].v); 4044 const int v_hs = (z->img_h_max / z->img_comp[2].h); 4045 const int v_vs = (z->img_v_max / z->img_comp[2].v); 4046 for (i=0; i < (z->s->img_y + 1) / 2; ++i) { 4047 stbi_uc *src_u = z->img_comp[1].data + i * (1 + (nv12_vs - u_vs)) * z->img_comp[1].x; 4048 stbi_uc *src_v = z->img_comp[2].data + i * (1 + (nv12_vs - v_vs)) * z->img_comp[2].x; 4049 stbi_uc *dst = nv12->uv + i * nv12->pitch; 4050 for (j=0; j < (z->s->img_x + 1) / 2; ++j) { 4051 *dst++ = *src_u; 4052 src_u += 1 + (nv12_hs - u_hs); 4053 *dst++ = *src_v; 4054 src_v += 1 + (nv12_hs - v_hs); 4055 } 4056 } 4057 } else { 4058 // Grayscale 4059 for (i=0; i < (z->s->img_y + 1) / 2; ++i) { 4060 memset(nv12->uv + i * nv12->pitch, 0x80808080, ((z->s->img_x + 1) / 2) * 2); 4061 } 4062 } 4063 4064 return nv12->y; 4065} 4066 4067static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp, stbi__nv12 *nv12) 4068{ 4069 int n, decode_n, is_rgb; 4070 z->s->img_n = 0; // make stbi__cleanup_jpeg safe 4071 4072 // validate req_comp 4073 if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error"); 4074 4075 // load a jpeg image from whichever source, but leave in YCbCr format 4076 if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; } 4077 4078 // determine actual number of components to generate 4079 n = req_comp ? req_comp : z->s->img_n >= 3 ? 3 : 1; 4080 4081 is_rgb = z->s->img_n == 3 && (z->rgb == 3 || (z->app14_color_transform == 0 && !z->jfif)); 4082 4083 if (z->s->img_n == 3 && n < 3 && !is_rgb) 4084 decode_n = 1; 4085 else 4086 decode_n = z->s->img_n; 4087 4088 // nothing to do if no components requested; check this now to avoid 4089 // accessing uninitialized coutput[0] later 4090 if (decode_n <= 0) { stbi__cleanup_jpeg(z); return NULL; } 4091 4092 // resample and color-convert 4093 { 4094 int k; 4095 unsigned int i,j; 4096 stbi_uc *output; 4097 stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL }; 4098 4099 stbi__resample res_comp[4]; 4100 4101 if (nv12) { 4102 if (nv12->w != (int)z->s->img_x || nv12->h != (int)z->s->img_y) { 4103 stbi__cleanup_jpeg(z); 4104 return stbi__errpuc("badsize", "Unexpected size"); 4105 } 4106 4107 if (is_rgb) { 4108 stbi__cleanup_jpeg(z); 4109 return stbi__errpuc("rgbtonv12", "Can't convert RGB to NV12"); 4110 } 4111 4112 output = output_jpeg_nv12(z, nv12); 4113 } else { 4114 for (k=0; k < decode_n; ++k) { 4115 stbi__resample *r = &res_comp[k]; 4116 4117 // allocate line buffer big enough for upsampling off the edges 4118 // with upsample factor of 4 4119 z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3); 4120 if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); } 4121 4122 r->hs = z->img_h_max / z->img_comp[k].h; 4123 r->vs = z->img_v_max / z->img_comp[k].v; 4124 r->ystep = r->vs >> 1; 4125 r->w_lores = (z->s->img_x + r->hs-1) / r->hs; 4126 r->ypos = 0; 4127 r->line0 = r->line1 = z->img_comp[k].data; 4128 4129 if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1; 4130 else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2; 4131 else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2; 4132 else if (r->hs == 2 && r->vs == 2) r->resample = z->resample_row_hv_2_kernel; 4133 else r->resample = stbi__resample_row_generic; 4134 } 4135 4136 // can't error after this so, this is safe 4137 output = (stbi_uc *) stbi__malloc_mad3(n, z->s->img_x, z->s->img_y, 1); 4138 if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); } 4139 4140 // now go ahead and resample 4141 for (j=0; j < z->s->img_y; ++j) { 4142 stbi_uc *out = output + n * z->s->img_x * j; 4143 for (k=0; k < decode_n; ++k) { 4144 stbi__resample *r = &res_comp[k]; 4145 int y_bot = r->ystep >= (r->vs >> 1); 4146 coutput[k] = r->resample(z->img_comp[k].linebuf, 4147 y_bot ? r->line1 : r->line0, 4148 y_bot ? r->line0 : r->line1, 4149 r->w_lores, r->hs); 4150 if (++r->ystep >= r->vs) { 4151 r->ystep = 0; 4152 r->line0 = r->line1; 4153 if (++r->ypos < z->img_comp[k].y) 4154 r->line1 += z->img_comp[k].w2; 4155 } 4156 } 4157 if (n >= 3) { 4158 stbi_uc *y = coutput[0]; 4159 if (z->s->img_n == 3) { 4160 if (is_rgb) { 4161 for (i=0; i < z->s->img_x; ++i) { 4162 out[0] = y[i]; 4163 out[1] = coutput[1][i]; 4164 out[2] = coutput[2][i]; 4165 out[3] = 255; 4166 out += n; 4167 } 4168 } else { 4169 z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); 4170 } 4171 } else if (z->s->img_n == 4) { 4172 if (z->app14_color_transform == 0) { // CMYK 4173 for (i=0; i < z->s->img_x; ++i) { 4174 stbi_uc m = coutput[3][i]; 4175 out[0] = stbi__blinn_8x8(coutput[0][i], m); 4176 out[1] = stbi__blinn_8x8(coutput[1][i], m); 4177 out[2] = stbi__blinn_8x8(coutput[2][i], m); 4178 out[3] = 255; 4179 out += n; 4180 } 4181 } else if (z->app14_color_transform == 2) { // YCCK 4182 z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); 4183 for (i=0; i < z->s->img_x; ++i) { 4184 stbi_uc m = coutput[3][i]; 4185 out[0] = stbi__blinn_8x8(255 - out[0], m); 4186 out[1] = stbi__blinn_8x8(255 - out[1], m); 4187 out[2] = stbi__blinn_8x8(255 - out[2], m); 4188 out += n; 4189 } 4190 } else { // YCbCr + alpha? Ignore the fourth channel for now 4191 z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); 4192 } 4193 } else 4194 for (i=0; i < z->s->img_x; ++i) { 4195 out[0] = out[1] = out[2] = y[i]; 4196 out[3] = 255; // not used if n==3 4197 out += n; 4198 } 4199 } else { 4200 if (is_rgb) { 4201 if (n == 1) 4202 for (i=0; i < z->s->img_x; ++i) 4203 *out++ = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]); 4204 else { 4205 for (i=0; i < z->s->img_x; ++i, out += 2) { 4206 out[0] = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]); 4207 out[1] = 255; 4208 } 4209 } 4210 } else if (z->s->img_n == 4 && z->app14_color_transform == 0) { 4211 for (i=0; i < z->s->img_x; ++i) { 4212 stbi_uc m = coutput[3][i]; 4213 stbi_uc r = stbi__blinn_8x8(coutput[0][i], m); 4214 stbi_uc g = stbi__blinn_8x8(coutput[1][i], m); 4215 stbi_uc b = stbi__blinn_8x8(coutput[2][i], m); 4216 out[0] = stbi__compute_y(r, g, b); 4217 out[1] = 255; 4218 out += n; 4219 } 4220 } else if (z->s->img_n == 4 && z->app14_color_transform == 2) { 4221 for (i=0; i < z->s->img_x; ++i) { 4222 out[0] = stbi__blinn_8x8(255 - coutput[0][i], coutput[3][i]); 4223 out[1] = 255; 4224 out += n; 4225 } 4226 } else { 4227 stbi_uc *y = coutput[0]; 4228 if (n == 1) 4229 for (i=0; i < z->s->img_x; ++i) out[i] = y[i]; 4230 else 4231 for (i=0; i < z->s->img_x; ++i) { *out++ = y[i]; *out++ = 255; } 4232 } 4233 } 4234 } 4235 } 4236 stbi__cleanup_jpeg(z); 4237 *out_x = z->s->img_x; 4238 *out_y = z->s->img_y; 4239 if (comp) *comp = z->s->img_n >= 3 ? 3 : 1; // report original components, not output 4240 return output; 4241 } 4242} 4243 4244static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__nv12 *nv12, stbi__result_info *ri) 4245{ 4246 unsigned char* result; 4247 stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg)); 4248 if (!j) return stbi__errpuc("outofmem", "Out of memory"); 4249 memset(j, 0, sizeof(stbi__jpeg)); 4250 STBI_NOTUSED(ri); 4251 j->s = s; 4252 stbi__setup_jpeg(j); 4253 result = load_jpeg_image(j, x,y,comp,req_comp,nv12); 4254 STBI_FREE(j); 4255 return result; 4256} 4257 4258static int stbi__jpeg_test(stbi__context *s) 4259{ 4260 int r; 4261 stbi__jpeg* j = (stbi__jpeg*)stbi__malloc(sizeof(stbi__jpeg)); 4262 if (!j) return stbi__err("outofmem", "Out of memory"); 4263 memset(j, 0, sizeof(stbi__jpeg)); 4264 j->s = s; 4265 stbi__setup_jpeg(j); 4266 r = stbi__decode_jpeg_header(j, STBI__SCAN_type); 4267 stbi__rewind(s); 4268 STBI_FREE(j); 4269 return r; 4270} 4271 4272#if 0 /* not used in SDL */ 4273static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp) 4274{ 4275 if (!stbi__decode_jpeg_header(j, STBI__SCAN_header)) { 4276 stbi__rewind( j->s ); 4277 return 0; 4278 } 4279 if (x) *x = j->s->img_x; 4280 if (y) *y = j->s->img_y; 4281 if (comp) *comp = j->s->img_n >= 3 ? 3 : 1; 4282 return 1; 4283} 4284 4285static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp) 4286{ 4287 int result; 4288 stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg))); 4289 if (!j) return stbi__err("outofmem", "Out of memory"); 4290 memset(j, 0, sizeof(stbi__jpeg)); 4291 j->s = s; 4292 result = stbi__jpeg_info_raw(j, x, y, comp); 4293 STBI_FREE(j); 4294 return result; 4295} 4296#endif /**/ 4297#endif 4298 4299// public domain zlib decode v0.2 Sean Barrett 2006-11-18 4300// simple implementation 4301// - all input must be provided in an upfront buffer 4302// - all output is written to a single output buffer (can malloc/realloc) 4303// performance 4304// - fast huffman 4305 4306#ifndef STBI_NO_ZLIB 4307 4308// fast-way is faster to check than jpeg huffman, but slow way is slower 4309#define STBI__ZFAST_BITS 9 // accelerate all cases in default tables 4310#define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1) 4311#define STBI__ZNSYMS 288 // number of symbols in literal/length alphabet 4312 4313// zlib-style huffman encoding 4314// (jpegs packs from left, zlib from right, so can't share code) 4315typedef struct 4316{ 4317 stbi__uint16 fast[1 << STBI__ZFAST_BITS]; 4318 stbi__uint16 firstcode[16]; 4319 int maxcode[17]; 4320 stbi__uint16 firstsymbol[16]; 4321 stbi_uc size[STBI__ZNSYMS]; 4322 stbi__uint16 value[STBI__ZNSYMS]; 4323} stbi__zhuffman; 4324 4325stbi_inline static int stbi__bitreverse16(int n) 4326{ 4327 n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1); 4328 n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2); 4329 n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4); 4330 n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8); 4331 return n; 4332} 4333 4334stbi_inline static int stbi__bit_reverse(int v, int bits) 4335{ 4336 STBI_ASSERT(bits <= 16); 4337 // to bit reverse n bits, reverse 16 and shift 4338 // e.g. 11 bits, bit reverse and shift away 5 4339 return stbi__bitreverse16(v) >> (16-bits); 4340} 4341 4342static int stbi__zbuild_huffman(stbi__zhuffman *z, const stbi_uc *sizelist, int num) 4343{ 4344 int i,k=0; 4345 int code, next_code[16], sizes[17]; 4346 4347 // DEFLATE spec for generating codes 4348 memset(sizes, 0, sizeof(sizes)); 4349 memset(z->fast, 0, sizeof(z->fast)); 4350 for (i=0; i < num; ++i) 4351 ++sizes[sizelist[i]]; 4352 sizes[0] = 0; 4353 for (i=1; i < 16; ++i) 4354 if (sizes[i] > (1 << i)) 4355 return stbi__err("bad sizes", "Corrupt PNG"); 4356 code = 0; 4357 for (i=1; i < 16; ++i) { 4358 next_code[i] = code; 4359 z->firstcode[i] = (stbi__uint16) code; 4360 z->firstsymbol[i] = (stbi__uint16) k; 4361 code = (code + sizes[i]); 4362 if (sizes[i]) 4363 if (code-1 >= (1 << i)) return stbi__err("bad codelengths","Corrupt PNG"); 4364 z->maxcode[i] = code << (16-i); // preshift for inner loop 4365 code <<= 1; 4366 k += sizes[i]; 4367 } 4368 z->maxcode[16] = 0x10000; // sentinel 4369 for (i=0; i < num; ++i) { 4370 int s = sizelist[i]; 4371 if (s) { 4372 int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s]; 4373 stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i); 4374 z->size [c] = (stbi_uc ) s; 4375 z->value[c] = (stbi__uint16) i; 4376 if (s <= STBI__ZFAST_BITS) { 4377 int j = stbi__bit_reverse(next_code[s],s); 4378 while (j < (1 << STBI__ZFAST_BITS)) { 4379 z->fast[j] = fastv; 4380 j += (1 << s); 4381 } 4382 } 4383 ++next_code[s]; 4384 } 4385 } 4386 return 1; 4387} 4388 4389// zlib-from-memory implementation for PNG reading 4390// because PNG allows splitting the zlib stream arbitrarily, 4391// and it's annoying structurally to have PNG call ZLIB call PNG, 4392// we require PNG read all the IDATs and combine them into a single 4393// memory buffer 4394 4395typedef struct 4396{ 4397 stbi_uc *zbuffer, *zbuffer_end; 4398 int num_bits; 4399 int hit_zeof_once; 4400 stbi__uint32 code_buffer; 4401 4402 char *zout; 4403 char *zout_start; 4404 char *zout_end; 4405 int z_expandable; 4406 4407 stbi__zhuffman z_length, z_distance; 4408} stbi__zbuf; 4409 4410stbi_inline static int stbi__zeof(stbi__zbuf *z) 4411{ 4412 return (z->zbuffer >= z->zbuffer_end); 4413} 4414 4415stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z) 4416{ 4417 return stbi__zeof(z) ? 0 : *z->zbuffer++; 4418} 4419 4420static void stbi__fill_bits(stbi__zbuf *z) 4421{ 4422 do { 4423 if (z->code_buffer >= (1U << z->num_bits)) { 4424 z->zbuffer = z->zbuffer_end; /* treat this as EOF so we fail. */ 4425 return; 4426 } 4427 z->code_buffer |= (unsigned int) stbi__zget8(z) << z->num_bits; 4428 z->num_bits += 8; 4429 } while (z->num_bits <= 24); 4430} 4431 4432stbi_inline static unsigned int stbi__zreceive(stbi__zbuf *z, int n) 4433{ 4434 unsigned int k; 4435 if (z->num_bits < n) stbi__fill_bits(z); 4436 k = z->code_buffer & ((1 << n) - 1); 4437 z->code_buffer >>= n; 4438 z->num_bits -= n; 4439 return k; 4440} 4441 4442static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z) 4443{ 4444 int b,s,k; 4445 // not resolved by fast table, so compute it the slow way 4446 // use jpeg approach, which requires MSbits at top 4447 k = stbi__bit_reverse(a->code_buffer, 16); 4448 for (s=STBI__ZFAST_BITS+1; ; ++s) 4449 if (k < z->maxcode[s]) 4450 break; 4451 if (s >= 16) return -1; // invalid code! 4452 // code size is s, so: 4453 b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s]; 4454 if (b >= STBI__ZNSYMS) return -1; // some data was corrupt somewhere! 4455 if (z->size[b] != s) return -1; // was originally an assert, but report failure instead. 4456 a->code_buffer >>= s; 4457 a->num_bits -= s; 4458 return z->value[b]; 4459} 4460 4461stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z) 4462{ 4463 int b,s; 4464 if (a->num_bits < 16) { 4465 if (stbi__zeof(a)) { 4466 if (!a->hit_zeof_once) { 4467 // This is the first time we hit eof, insert 16 extra padding btis 4468 // to allow us to keep going; if we actually consume any of them 4469 // though, that is invalid data. This is caught later. 4470 a->hit_zeof_once = 1; 4471 a->num_bits += 16; // add 16 implicit zero bits 4472 } else { 4473 // We already inserted our extra 16 padding bits and are again 4474 // out, this stream is actually prematurely terminated. 4475 return -1; 4476 } 4477 } else { 4478 stbi__fill_bits(a); 4479 } 4480 } 4481 b = z->fast[a->code_buffer & STBI__ZFAST_MASK]; 4482 if (b) { 4483 s = b >> 9; 4484 a->code_buffer >>= s; 4485 a->num_bits -= s; 4486 return b & 511; 4487 } 4488 return stbi__zhuffman_decode_slowpath(a, z); 4489} 4490 4491static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes 4492{ 4493 char *q; 4494 unsigned int cur, limit, old_limit; 4495 z->zout = zout; 4496 if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG"); 4497 cur = (unsigned int) (z->zout - z->zout_start); 4498 limit = old_limit = (unsigned) (z->zout_end - z->zout_start); 4499 if (UINT_MAX - cur < (unsigned) n) return stbi__err("outofmem", "Out of memory"); 4500 while (cur + n > limit) { 4501 if(limit > UINT_MAX / 2) return stbi__err("outofmem", "Out of memory"); 4502 limit *= 2; 4503 } 4504 q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit); 4505 STBI_NOTUSED(old_limit); 4506 if (q == NULL) return stbi__err("outofmem", "Out of memory"); 4507 z->zout_start = q; 4508 z->zout = q + cur; 4509 z->zout_end = q + limit; 4510 return 1; 4511} 4512 4513static const int stbi__zlength_base[31] = { 4514 3,4,5,6,7,8,9,10,11,13, 4515 15,17,19,23,27,31,35,43,51,59, 4516 67,83,99,115,131,163,195,227,258,0,0 }; 4517 4518static const int stbi__zlength_extra[31]= 4519{ 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 }; 4520 4521static const int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, 4522257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0}; 4523 4524static const int stbi__zdist_extra[32] = 4525{ 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; 4526 4527static int stbi__parse_huffman_block(stbi__zbuf *a) 4528{ 4529 char *zout = a->zout; 4530 for(;;) { 4531 int z = stbi__zhuffman_decode(a, &a->z_length); 4532 if (z < 256) { 4533 if (z < 0) return stbi__err("bad huffman code","Corrupt PNG"); // error in huffman codes 4534 if (zout >= a->zout_end) { 4535 if (!stbi__zexpand(a, zout, 1)) return 0; 4536 zout = a->zout; 4537 } 4538 *zout++ = (char) z; 4539 } else { 4540 stbi_uc *p; 4541 int len,dist; 4542 if (z == 256) { 4543 a->zout = zout; 4544 if (a->hit_zeof_once && a->num_bits < 16) { 4545 // The first time we hit zeof, we inserted 16 extra zero bits into our bit 4546 // buffer so the decoder can just do its speculative decoding. But if we 4547 // actually consumed any of those bits (which is the case when num_bits < 16), 4548 // the stream actually read past the end so it is malformed. 4549 return stbi__err("unexpected end","Corrupt PNG"); 4550 } 4551 return 1; 4552 } 4553 if (z >= 286) return stbi__err("bad huffman code","Corrupt PNG"); // per DEFLATE, length codes 286 and 287 must not appear in compressed data 4554 z -= 257; 4555 len = stbi__zlength_base[z]; 4556 if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]); 4557 z = stbi__zhuffman_decode(a, &a->z_distance); 4558 if (z < 0 || z >= 30) return stbi__err("bad huffman code","Corrupt PNG"); // per DEFLATE, distance codes 30 and 31 must not appear in compressed data 4559 dist = stbi__zdist_base[z]; 4560 if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]); 4561 if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG"); 4562 if (len > a->zout_end - zout) { 4563 if (!stbi__zexpand(a, zout, len)) return 0; 4564 zout = a->zout; 4565 } 4566 p = (stbi_uc *) (zout - dist); 4567 if (dist == 1) { // run of one byte; common in images. 4568 stbi_uc v = *p; 4569 if (len) { do *zout++ = v; while (--len); } 4570 } else { 4571 if (len) { do *zout++ = *p++; while (--len); } 4572 } 4573 } 4574 } 4575} 4576 4577static int stbi__compute_huffman_codes(stbi__zbuf *a) 4578{ 4579 static const stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 }; 4580 stbi__zhuffman z_codelength; 4581 stbi_uc lencodes[286+32+137];//padding for maximum single op 4582 stbi_uc codelength_sizes[19]; 4583 int i,n; 4584 4585 int hlit = stbi__zreceive(a,5) + 257; 4586 int hdist = stbi__zreceive(a,5) + 1; 4587 int hclen = stbi__zreceive(a,4) + 4; 4588 int ntot = hlit + hdist; 4589 4590 memset(codelength_sizes, 0, sizeof(codelength_sizes)); 4591 for (i=0; i < hclen; ++i) { 4592 int s = stbi__zreceive(a,3); 4593 codelength_sizes[length_dezigzag[i]] = (stbi_uc) s; 4594 } 4595 if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0; 4596 4597 n = 0; 4598 while (n < ntot) { 4599 int c = stbi__zhuffman_decode(a, &z_codelength); 4600 if (c < 0 || c >= 19) return stbi__err("bad codelengths", "Corrupt PNG"); 4601 if (c < 16) 4602 lencodes[n++] = (stbi_uc) c; 4603 else { 4604 stbi_uc fill = 0; 4605 if (c == 16) { 4606 c = stbi__zreceive(a,2)+3; 4607 if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG"); 4608 fill = lencodes[n-1]; 4609 } else if (c == 17) { 4610 c = stbi__zreceive(a,3)+3; 4611 } else if (c == 18) { 4612 c = stbi__zreceive(a,7)+11; 4613 } else { 4614 return stbi__err("bad codelengths", "Corrupt PNG"); 4615 } 4616 if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG"); 4617 memset(lencodes+n, fill, c); 4618 n += c; 4619 } 4620 } 4621 if (n != ntot) return stbi__err("bad codelengths","Corrupt PNG"); 4622 if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0; 4623 if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0; 4624 return 1; 4625} 4626 4627static int stbi__parse_uncompressed_block(stbi__zbuf *a) 4628{ 4629 stbi_uc header[4]; 4630 int len,nlen,k; 4631 if (a->num_bits & 7) 4632 stbi__zreceive(a, a->num_bits & 7); // discard 4633 // drain the bit-packed data into header 4634 k = 0; 4635 while (a->num_bits > 0) { 4636 header[k++] = (stbi_uc) (a->code_buffer & 255); // suppress MSVC run-time check 4637 a->code_buffer >>= 8; 4638 a->num_bits -= 8; 4639 } 4640 if (a->num_bits < 0) return stbi__err("zlib corrupt","Corrupt PNG"); 4641 // now fill header the normal way 4642 while (k < 4) 4643 header[k++] = stbi__zget8(a); 4644 len = header[1] * 256 + header[0]; 4645 nlen = header[3] * 256 + header[2]; 4646 if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt","Corrupt PNG"); 4647 if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer","Corrupt PNG"); 4648 if (a->zout + len > a->zout_end) 4649 if (!stbi__zexpand(a, a->zout, len)) return 0; 4650 memcpy(a->zout, a->zbuffer, len); 4651 a->zbuffer += len; 4652 a->zout += len; 4653 return 1; 4654} 4655 4656static int stbi__parse_zlib_header(stbi__zbuf *a) 4657{ 4658 int cmf = stbi__zget8(a); 4659 int cm = cmf & 15; 4660 /* int cinfo = cmf >> 4; */ 4661 int flg = stbi__zget8(a); 4662 if (stbi__zeof(a)) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec 4663 if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec 4664 if (flg & 32) return stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png 4665 if (cm != 8) return stbi__err("bad compression","Corrupt PNG"); // DEFLATE required for png 4666 // window = 1 << (8 + cinfo)... but who cares, we fully buffer output 4667 return 1; 4668} 4669 4670static const stbi_uc stbi__zdefault_length[STBI__ZNSYMS] = 4671{ 4672 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 4673 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 4674 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 4675 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 4676 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 4677 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 4678 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 4679 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 4680 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8 4681}; 4682static const stbi_uc stbi__zdefault_distance[32] = 4683{ 4684 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5 4685}; 4686/* 4687Init algorithm: 4688{ 4689 int i; // use <= to match clearly with spec 4690 for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8; 4691 for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9; 4692 for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7; 4693 for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8; 4694 4695 for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5; 4696} 4697*/ 4698 4699static int stbi__parse_zlib(stbi__zbuf *a, int parse_header) 4700{ 4701 int final, type; 4702 if (parse_header) 4703 if (!stbi__parse_zlib_header(a)) return 0; 4704 a->num_bits = 0; 4705 a->code_buffer = 0; 4706 a->hit_zeof_once = 0; 4707 do { 4708 final = stbi__zreceive(a,1); 4709 type = stbi__zreceive(a,2); 4710 if (type == 0) { 4711 if (!stbi__parse_uncompressed_block(a)) return 0; 4712 } else if (type == 3) { 4713 return 0; 4714 } else { 4715 if (type == 1) { 4716 // use fixed code lengths 4717 if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , STBI__ZNSYMS)) return 0; 4718 if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0; 4719 } else { 4720 if (!stbi__compute_huffman_codes(a)) return 0; 4721 } 4722 if (!stbi__parse_huffman_block(a)) return 0; 4723 } 4724 } while (!final); 4725 return 1; 4726} 4727 4728static int stbi__do_zlib(stbi__zbuf *a, char *obuf, int olen, int exp, int parse_header) 4729{ 4730 a->zout_start = obuf; 4731 a->zout = obuf; 4732 a->zout_end = obuf + olen; 4733 a->z_expandable = exp; 4734 4735 return stbi__parse_zlib(a, parse_header); 4736} 4737 4738#if 0 /* not used in SDL */ 4739STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen) 4740{ 4741 stbi__zbuf a; 4742 char *p = (char *) stbi__malloc(initial_size); 4743 if (p == NULL) return NULL; 4744 a.zbuffer = (stbi_uc *) buffer; 4745 a.zbuffer_end = (stbi_uc *) buffer + len; 4746 if (stbi__do_zlib(&a, p, initial_size, 1, 1)) { 4747 if (outlen) *outlen = (int) (a.zout - a.zout_start); 4748 return a.zout_start; 4749 } else { 4750 STBI_FREE(a.zout_start); 4751 return NULL; 4752 } 4753} 4754 4755STBIDEF char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen) 4756{ 4757 return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen); 4758} 4759#endif /* */ 4760 4761STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header) 4762{ 4763 stbi__zbuf a; 4764 char *p = (char *) stbi__malloc(initial_size); 4765 if (p == NULL) return NULL; 4766 a.zbuffer = (stbi_uc *) buffer; 4767 a.zbuffer_end = (stbi_uc *) buffer + len; 4768 if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) { 4769 if (outlen) *outlen = (int) (a.zout - a.zout_start); 4770 return a.zout_start; 4771 } else { 4772 STBI_FREE(a.zout_start); 4773 return NULL; 4774 } 4775} 4776 4777#if 0 /* not used in SDL */ 4778STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen) 4779{ 4780 stbi__zbuf a; 4781 a.zbuffer = (stbi_uc *) ibuffer; 4782 a.zbuffer_end = (stbi_uc *) ibuffer + ilen; 4783 if (stbi__do_zlib(&a, obuffer, olen, 0, 1)) 4784 return (int) (a.zout - a.zout_start); 4785 else 4786 return -1; 4787} 4788 4789STBIDEF char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen) 4790{ 4791 stbi__zbuf a; 4792 char *p = (char *) stbi__malloc(16384); 4793 if (p == NULL) return NULL; 4794 a.zbuffer = (stbi_uc *) buffer; 4795 a.zbuffer_end = (stbi_uc *) buffer+len; 4796 if (stbi__do_zlib(&a, p, 16384, 1, 0)) { 4797 if (outlen) *outlen = (int) (a.zout - a.zout_start); 4798 return a.zout_start; 4799 } else { 4800 STBI_FREE(a.zout_start); 4801 return NULL; 4802 } 4803} 4804 4805STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen) 4806{ 4807 stbi__zbuf a; 4808 a.zbuffer = (stbi_uc *) ibuffer; 4809 a.zbuffer_end = (stbi_uc *) ibuffer + ilen; 4810 if (stbi__do_zlib(&a, obuffer, olen, 0, 0)) 4811 return (int) (a.zout - a.zout_start); 4812 else 4813 return -1; 4814} 4815#endif /* */ 4816#endif 4817 4818// public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18 4819// simple implementation 4820// - only 8-bit samples 4821// - no CRC checking 4822// - allocates lots of intermediate memory 4823// - avoids problem of streaming data between subsystems 4824// - avoids explicit window management 4825// performance 4826// - uses stb_zlib, a PD zlib implementation with fast huffman decoding 4827 4828#ifndef STBI_NO_PNG 4829typedef struct 4830{ 4831 stbi__uint32 length; 4832 stbi__uint32 type; 4833} stbi__pngchunk; 4834 4835static stbi__pngchunk stbi__get_chunk_header(stbi__context *s) 4836{ 4837 stbi__pngchunk c; 4838 c.length = stbi__get32be(s); 4839 c.type = stbi__get32be(s); 4840 return c; 4841} 4842 4843static int stbi__check_png_header(stbi__context *s) 4844{ 4845 static const stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 }; 4846 int i; 4847 for (i=0; i < 8; ++i) 4848 if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG"); 4849 return 1; 4850} 4851 4852typedef struct 4853{ 4854 stbi__context *s; 4855 stbi_uc *idata, *expanded, *out; 4856 int depth; 4857} stbi__png; 4858 4859 4860enum { 4861 STBI__F_none=0, 4862 STBI__F_sub=1, 4863 STBI__F_up=2, 4864 STBI__F_avg=3, 4865 STBI__F_paeth=4, 4866 // synthetic filter used for first scanline to avoid needing a dummy row of 0s 4867 STBI__F_avg_first 4868}; 4869 4870static stbi_uc first_row_filter[5] = 4871{ 4872 STBI__F_none, 4873 STBI__F_sub, 4874 STBI__F_none, 4875 STBI__F_avg_first, 4876 STBI__F_sub // Paeth with b=c=0 turns out to be equivalent to sub 4877}; 4878 4879static int stbi__paeth(int a, int b, int c) 4880{ 4881 // This formulation looks very different from the reference in the PNG spec, but is 4882 // actually equivalent and has favorable data dependencies and admits straightforward 4883 // generation of branch-free code, which helps performance significantly. 4884 int thresh = c*3 - (a + b); 4885 int lo = a < b ? a : b; 4886 int hi = a < b ? b : a; 4887 int t0 = (hi <= thresh) ? lo : c; 4888 int t1 = (thresh <= lo) ? hi : t0; 4889 return t1; 4890} 4891 4892static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 }; 4893 4894// adds an extra all-255 alpha channel 4895// dest == src is legal 4896// img_n must be 1 or 3 4897static void stbi__create_png_alpha_expand8(stbi_uc *dest, stbi_uc *src, stbi__uint32 x, int img_n) 4898{ 4899 int i; 4900 // must process data backwards since we allow dest==src 4901 if (img_n == 1) { 4902 for (i=x-1; i >= 0; --i) { 4903 dest[i*2+1] = 255; 4904 dest[i*2+0] = src[i]; 4905 } 4906 } else { 4907 STBI_ASSERT(img_n == 3); 4908 for (i=x-1; i >= 0; --i) { 4909 dest[i*4+3] = 255; 4910 dest[i*4+2] = src[i*3+2]; 4911 dest[i*4+1] = src[i*3+1]; 4912 dest[i*4+0] = src[i*3+0]; 4913 } 4914 } 4915} 4916 4917// create the png data from post-deflated data 4918static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color) 4919{ 4920 int bytes = (depth == 16 ? 2 : 1); 4921 stbi__context *s = a->s; 4922 stbi__uint32 i,j,stride = x*out_n*bytes; 4923 stbi__uint32 img_len, img_width_bytes; 4924 stbi_uc *filter_buf; 4925 int all_ok = 1; 4926 int k; 4927 int img_n = s->img_n; // copy it into a local for later 4928 4929 int output_bytes = out_n*bytes; 4930 int filter_bytes = img_n*bytes; 4931 int width = x; 4932 4933 STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1); 4934 a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into 4935 if (!a->out) return stbi__err("outofmem", "Out of memory"); 4936 4937 // note: error exits here don't need to clean up a->out individually, 4938 // stbi__do_png always does on error. 4939 if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large", "Corrupt PNG"); 4940 img_width_bytes = (((img_n * x * depth) + 7) >> 3); 4941 if (!stbi__mad2sizes_valid(img_width_bytes, y, img_width_bytes)) return stbi__err("too large", "Corrupt PNG"); 4942 img_len = (img_width_bytes + 1) * y; 4943 4944 // we used to check for exact match between raw_len and img_len on non-interlaced PNGs, 4945 // but issue #276 reported a PNG in the wild that had extra data at the end (all zeros), 4946 // so just check for raw_len < img_len always. 4947 if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG"); 4948 4949 // Allocate two scan lines worth of filter workspace buffer. 4950 filter_buf = (stbi_uc *) stbi__malloc_mad2(img_width_bytes, 2, 0); 4951 if (!filter_buf) return stbi__err("outofmem", "Out of memory"); 4952 4953 // Filtering for low-bit-depth images 4954 if (depth < 8) { 4955 filter_bytes = 1; 4956 width = img_width_bytes; 4957 } 4958 4959 for (j=0; j < y; ++j) { 4960 // cur/prior filter buffers alternate 4961 stbi_uc *cur = filter_buf + (j & 1)*img_width_bytes; 4962 stbi_uc *prior = filter_buf + (~j & 1)*img_width_bytes; 4963 stbi_uc *dest = a->out + stride*j; 4964 int nk = width * filter_bytes; 4965 int filter = *raw++; 4966 4967 // check filter type 4968 if (filter > 4) { 4969 all_ok = stbi__err("invalid filter","Corrupt PNG"); 4970 break; 4971 } 4972 4973 // if first row, use special filter that doesn't sample previous row 4974 if (j == 0) filter = first_row_filter[filter]; 4975 4976 // perform actual filtering 4977 switch (filter) { 4978 case STBI__F_none: 4979 memcpy(cur, raw, nk); 4980 break; 4981 case STBI__F_sub: 4982 memcpy(cur, raw, filter_bytes); 4983 for (k = filter_bytes; k < nk; ++k) 4984 cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); 4985 break; 4986 case STBI__F_up: 4987 for (k = 0; k < nk; ++k) 4988 cur[k] = STBI__BYTECAST(raw[k] + prior[k]); 4989 break; 4990 case STBI__F_avg: 4991 for (k = 0; k < filter_bytes; ++k) 4992 cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); 4993 for (k = filter_bytes; k < nk; ++k) 4994 cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); 4995 break; 4996 case STBI__F_paeth: 4997 for (k = 0; k < filter_bytes; ++k) 4998 cur[k] = STBI__BYTECAST(raw[k] + prior[k]); // prior[k] == stbi__paeth(0,prior[k],0) 4999 for (k = filter_bytes; k < nk; ++k) 5000 cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes], prior[k], prior[k-filter_bytes])); 5001 break; 5002 case STBI__F_avg_first: 5003 memcpy(cur, raw, filter_bytes); 5004 for (k = filter_bytes; k < nk; ++k) 5005 cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); 5006 break; 5007 } 5008 5009 raw += nk; 5010 5011 // expand decoded bits in cur to dest, also adding an extra alpha channel if desired 5012 if (depth < 8) { 5013 stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1; // scale grayscale values to 0..255 range 5014 stbi_uc *in = cur; 5015 stbi_uc *out = dest; 5016 stbi_uc inb = 0; 5017 stbi__uint32 nsmp = x*img_n; 5018 5019 // expand bits to bytes first 5020 if (depth == 4) { 5021 for (i=0; i < nsmp; ++i) { 5022 if ((i & 1) == 0) inb = *in++; 5023 *out++ = scale * (inb >> 4); 5024 inb <<= 4; 5025 } 5026 } else if (depth == 2) { 5027 for (i=0; i < nsmp; ++i) { 5028 if ((i & 3) == 0) inb = *in++; 5029 *out++ = scale * (inb >> 6); 5030 inb <<= 2; 5031 } 5032 } else { 5033 STBI_ASSERT(depth == 1); 5034 for (i=0; i < nsmp; ++i) { 5035 if ((i & 7) == 0) inb = *in++; 5036 *out++ = scale * (inb >> 7); 5037 inb <<= 1; 5038 } 5039 } 5040 5041 // insert alpha=255 values if desired 5042 if (img_n != out_n) 5043 stbi__create_png_alpha_expand8(dest, dest, x, img_n); 5044 } else if (depth == 8) { 5045 if (img_n == out_n) 5046 memcpy(dest, cur, x*img_n); 5047 else 5048 stbi__create_png_alpha_expand8(dest, cur, x, img_n); 5049 } else if (depth == 16) { 5050 // convert the image data from big-endian to platform-native 5051 stbi__uint16 *dest16 = (stbi__uint16*)dest; 5052 stbi__uint32 nsmp = x*img_n; 5053 5054 if (img_n == out_n) { 5055 for (i = 0; i < nsmp; ++i, ++dest16, cur += 2) 5056 *dest16 = (cur[0] << 8) | cur[1]; 5057 } else { 5058 STBI_ASSERT(img_n+1 == out_n); 5059 if (img_n == 1) { 5060 for (i = 0; i < x; ++i, dest16 += 2, cur += 2) { 5061 dest16[0] = (cur[0] << 8) | cur[1]; 5062 dest16[1] = 0xffff; 5063 } 5064 } else { 5065 STBI_ASSERT(img_n == 3); 5066 for (i = 0; i < x; ++i, dest16 += 4, cur += 6) { 5067 dest16[0] = (cur[0] << 8) | cur[1]; 5068 dest16[1] = (cur[2] << 8) | cur[3]; 5069 dest16[2] = (cur[4] << 8) | cur[5]; 5070 dest16[3] = 0xffff; 5071 } 5072 } 5073 } 5074 } 5075 } 5076 5077 STBI_FREE(filter_buf); 5078 if (!all_ok) return 0; 5079 5080 return 1; 5081} 5082 5083static int stbi__create_png_image(stbi__png *a, stbi_uc *image_data, stbi__uint32 image_data_len, int out_n, int depth, int color, int interlaced) 5084{ 5085 int bytes = (depth == 16 ? 2 : 1); 5086 int out_bytes = out_n * bytes; 5087 stbi_uc *final; 5088 int p; 5089 if (!interlaced) 5090 return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color); 5091 5092 // de-interlacing 5093 final = (stbi_uc *) stbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0); 5094 if (!final) return stbi__err("outofmem", "Out of memory"); 5095 for (p=0; p < 7; ++p) { 5096 int xorig[] = { 0,4,0,2,0,1,0 }; 5097 int yorig[] = { 0,0,4,0,2,0,1 }; 5098 int xspc[] = { 8,8,4,4,2,2,1 }; 5099 int yspc[] = { 8,8,8,4,4,2,2 }; 5100 int i,j,x,y; 5101 // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1 5102 x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p]; 5103 y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p]; 5104 if (x && y) { 5105 stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y; 5106 if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) { 5107 STBI_FREE(final); 5108 return 0; 5109 } 5110 for (j=0; j < y; ++j) { 5111 for (i=0; i < x; ++i) { 5112 int out_y = j*yspc[p]+yorig[p]; 5113 int out_x = i*xspc[p]+xorig[p]; 5114 memcpy(final + out_y*a->s->img_x*out_bytes + out_x*out_bytes, 5115 a->out + (j*x+i)*out_bytes, out_bytes); 5116 } 5117 } 5118 STBI_FREE(a->out); 5119 image_data += img_len; 5120 image_data_len -= img_len; 5121 } 5122 } 5123 a->out = final; 5124 5125 return 1; 5126} 5127 5128static int stbi__compute_transparency(stbi__png *z, stbi_uc tc[3], int out_n) 5129{ 5130 stbi__context *s = z->s; 5131 stbi__uint32 i, pixel_count = s->img_x * s->img_y; 5132 stbi_uc *p = z->out; 5133 5134 // compute color-based transparency, assuming we've 5135 // already got 255 as the alpha value in the output 5136 STBI_ASSERT(out_n == 2 || out_n == 4); 5137 5138 if (out_n == 2) { 5139 for (i=0; i < pixel_count; ++i) { 5140 p[1] = (p[0] == tc[0] ? 0 : 255); 5141 p += 2; 5142 } 5143 } else { 5144 for (i=0; i < pixel_count; ++i) { 5145 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) 5146 p[3] = 0; 5147 p += 4; 5148 } 5149 } 5150 return 1; 5151} 5152 5153static int stbi__compute_transparency16(stbi__png *z, stbi__uint16 tc[3], int out_n) 5154{ 5155 stbi__context *s = z->s; 5156 stbi__uint32 i, pixel_count = s->img_x * s->img_y; 5157 stbi__uint16 *p = (stbi__uint16*) z->out; 5158 5159 // compute color-based transparency, assuming we've 5160 // already got 65535 as the alpha value in the output 5161 STBI_ASSERT(out_n == 2 || out_n == 4); 5162 5163 if (out_n == 2) { 5164 for (i = 0; i < pixel_count; ++i) { 5165 p[1] = (p[0] == tc[0] ? 0 : 65535); 5166 p += 2; 5167 } 5168 } else { 5169 for (i = 0; i < pixel_count; ++i) { 5170 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) 5171 p[3] = 0; 5172 p += 4; 5173 } 5174 } 5175 return 1; 5176} 5177 5178static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n) 5179{ 5180 stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y; 5181 stbi_uc *p, *temp_out, *orig = a->out; 5182 5183 p = (stbi_uc *) stbi__malloc_mad2(pixel_count, pal_img_n, 0); 5184 if (p == NULL) return stbi__err("outofmem", "Out of memory"); 5185 5186 // between here and free(out) below, exitting would leak 5187 temp_out = p; 5188 5189 if (pal_img_n == 3) { 5190 for (i=0; i < pixel_count; ++i) { 5191 int n = orig[i]*4; 5192 p[0] = palette[n ]; 5193 p[1] = palette[n+1]; 5194 p[2] = palette[n+2]; 5195 p += 3; 5196 } 5197 } else { 5198 for (i=0; i < pixel_count; ++i) { 5199 int n = orig[i]*4; 5200 p[0] = palette[n ]; 5201 p[1] = palette[n+1]; 5202 p[2] = palette[n+2]; 5203 p[3] = palette[n+3]; 5204 p += 4; 5205 } 5206 } 5207 STBI_FREE(a->out); 5208 a->out = temp_out; 5209 5210 STBI_NOTUSED(len); 5211 5212 return 1; 5213} 5214 5215static int stbi__unpremultiply_on_load_global = 0; 5216static int stbi__de_iphone_flag_global = 0; 5217 5218#if 0 /* not used in SDL */ 5219STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply) 5220{ 5221 stbi__unpremultiply_on_load_global = flag_true_if_should_unpremultiply; 5222} 5223 5224STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert) 5225{ 5226 stbi__de_iphone_flag_global = flag_true_if_should_convert; 5227} 5228#endif 5229 5230#ifndef STBI_THREAD_LOCAL 5231#define stbi__unpremultiply_on_load stbi__unpremultiply_on_load_global 5232#define stbi__de_iphone_flag stbi__de_iphone_flag_global 5233#else 5234static STBI_THREAD_LOCAL int stbi__unpremultiply_on_load_local, stbi__unpremultiply_on_load_set; 5235static STBI_THREAD_LOCAL int stbi__de_iphone_flag_local, stbi__de_iphone_flag_set; 5236 5237STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply) 5238{ 5239 stbi__unpremultiply_on_load_local = flag_true_if_should_unpremultiply; 5240 stbi__unpremultiply_on_load_set = 1; 5241} 5242 5243STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert) 5244{ 5245 stbi__de_iphone_flag_local = flag_true_if_should_convert; 5246 stbi__de_iphone_flag_set = 1; 5247} 5248 5249#define stbi__unpremultiply_on_load (stbi__unpremultiply_on_load_set \ 5250 ? stbi__unpremultiply_on_load_local \ 5251 : stbi__unpremultiply_on_load_global) 5252#define stbi__de_iphone_flag (stbi__de_iphone_flag_set \ 5253 ? stbi__de_iphone_flag_local \ 5254 : stbi__de_iphone_flag_global) 5255#endif // STBI_THREAD_LOCAL 5256 5257static void stbi__de_iphone(stbi__png *z) 5258{ 5259 stbi__context *s = z->s; 5260 stbi__uint32 i, pixel_count = s->img_x * s->img_y; 5261 stbi_uc *p = z->out; 5262 5263 if (s->img_out_n == 3) { // convert bgr to rgb 5264 for (i=0; i < pixel_count; ++i) { 5265 stbi_uc t = p[0]; 5266 p[0] = p[2]; 5267 p[2] = t; 5268 p += 3; 5269 } 5270 } else { 5271 STBI_ASSERT(s->img_out_n == 4); 5272 if (stbi__unpremultiply_on_load) { 5273 // convert bgr to rgb and unpremultiply 5274 for (i=0; i < pixel_count; ++i) { 5275 stbi_uc a = p[3]; 5276 stbi_uc t = p[0]; 5277 if (a) { 5278 stbi_uc half = a / 2; 5279 p[0] = (p[2] * 255 + half) / a; 5280 p[1] = (p[1] * 255 + half) / a; 5281 p[2] = ( t * 255 + half) / a; 5282 } else { 5283 p[0] = p[2]; 5284 p[2] = t; 5285 } 5286 p += 4; 5287 } 5288 } else { 5289 // convert bgr to rgb 5290 for (i=0; i < pixel_count; ++i) { 5291 stbi_uc t = p[0]; 5292 p[0] = p[2]; 5293 p[2] = t; 5294 p += 4; 5295 } 5296 } 5297 } 5298} 5299 5300#define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d)) 5301 5302static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp, unsigned int *palette_buffer, int palette_buffer_len) 5303{ 5304 stbi_uc _palette[1024]={0}, pal_img_n=0; 5305 stbi_uc *palette = _palette; 5306 stbi_uc has_trans=0, tc[3]={0}; 5307 stbi__uint16 tc16[3]={0}; 5308 stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0; 5309 int first=1,k,interlace=0, color=0, is_iphone=0; 5310 stbi__context *s = z->s; 5311 5312 if (palette_buffer) { 5313 if (palette_buffer_len < 256) 5314 return stbi__err("palette buffer too small", "palette buffer len must be 256"); 5315 else if (req_comp != 1) 5316 return stbi__err("invalid req_comp", "req_comp must be 1 when loading paletted"); 5317 else 5318 palette = (stbi_uc *)(void *)palette_buffer; 5319 } 5320 5321 z->expanded = NULL; 5322 z->idata = NULL; 5323 z->out = NULL; 5324 5325 if (!stbi__check_png_header(s)) return 0; 5326 5327 if (scan == STBI__SCAN_type) return 1; 5328 5329 for (;;) { 5330 stbi__pngchunk c = stbi__get_chunk_header(s); 5331 switch (c.type) { 5332 case STBI__PNG_TYPE('C','g','B','I'): 5333 is_iphone = 1; 5334 stbi__skip(s, c.length); 5335 break; 5336 case STBI__PNG_TYPE('I','H','D','R'): { 5337 int comp,filter; 5338 if (!first) return stbi__err("multiple IHDR","Corrupt PNG"); 5339 first = 0; 5340 if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG"); 5341 s->img_x = stbi__get32be(s); 5342 s->img_y = stbi__get32be(s); 5343 if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); 5344 if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); 5345 z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only"); 5346 color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG"); 5347 if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG"); 5348 if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG"); 5349 comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG"); 5350 filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG"); 5351 interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG"); 5352 if (!s->img_x || !s->img_y) return stbi__err("0-pixel image","Corrupt PNG"); 5353 if (!pal_img_n) { 5354 s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0); 5355 if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode"); 5356 } else { 5357 // if paletted, then pal_n is our final components, and 5358 // img_n is # components to decompress/filter. 5359 s->img_n = 1; 5360 if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG"); 5361 } 5362 // even with SCAN_header, have to scan to see if we have a tRNS 5363 break; 5364 } 5365 5366 case STBI__PNG_TYPE('P','L','T','E'): { 5367 if (first) return stbi__err("first not IHDR", "Corrupt PNG"); 5368 if (c.length > 256*3) return stbi__err("invalid PLTE","Corrupt PNG"); 5369 pal_len = c.length / 3; 5370 if (pal_len * 3 != c.length) return stbi__err("invalid PLTE","Corrupt PNG"); 5371 for (i=0; i < pal_len; ++i) { 5372 palette[i*4+0] = stbi__get8(s); 5373 palette[i*4+1] = stbi__get8(s); 5374 palette[i*4+2] = stbi__get8(s); 5375 palette[i*4+3] = 255; 5376 } 5377 break; 5378 } 5379 5380 case STBI__PNG_TYPE('t','R','N','S'): { 5381 if (first) return stbi__err("first not IHDR", "Corrupt PNG"); 5382 if (z->idata) return stbi__err("tRNS after IDAT","Corrupt PNG"); 5383 if (pal_img_n) { 5384 if (scan == STBI__SCAN_header) { s->img_n = 4; return 1; } 5385 if (pal_len == 0) return stbi__err("tRNS before PLTE","Corrupt PNG"); 5386 if (c.length > pal_len) return stbi__err("bad tRNS len","Corrupt PNG"); 5387 pal_img_n = 4; 5388 for (i=0; i < c.length; ++i) 5389 palette[i*4+3] = stbi__get8(s); 5390 } else { 5391 if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG"); 5392 if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG"); 5393 has_trans = 1; 5394 // non-paletted with tRNS = constant alpha. if header-scanning, we can stop now. 5395 if (scan == STBI__SCAN_header) { ++s->img_n; return 1; } 5396 if (z->depth == 16) { 5397 for (k = 0; k < s->img_n && k < 3; ++k) // extra loop test to suppress false GCC warning 5398 tc16[k] = (stbi__uint16)stbi__get16be(s); // copy the values as-is 5399 } else { 5400 for (k = 0; k < s->img_n && k < 3; ++k) 5401 tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth]; // non 8-bit images will be larger 5402 } 5403 } 5404 break; 5405 } 5406 5407 case STBI__PNG_TYPE('I','D','A','T'): { 5408 if (first) return stbi__err("first not IHDR", "Corrupt PNG"); 5409 if (pal_img_n && !pal_len) return stbi__err("no PLTE","Corrupt PNG"); 5410 if (scan == STBI__SCAN_header) { 5411 // header scan definitely stops at first IDAT 5412 if (pal_img_n) 5413 s->img_n = pal_img_n; 5414 return 1; 5415 } 5416 if (c.length > (1u << 30)) return stbi__err("IDAT size limit", "IDAT section larger than 2^30 bytes"); 5417 if ((int)(ioff + c.length) < (int)ioff) return 0; 5418 if (ioff + c.length > idata_limit) { 5419 stbi__uint32 idata_limit_old = idata_limit; 5420 stbi_uc *p; 5421 if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096; 5422 while (ioff + c.length > idata_limit) 5423 idata_limit *= 2; 5424 STBI_NOTUSED(idata_limit_old); 5425 p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory"); 5426 z->idata = p; 5427 } 5428 if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG"); 5429 ioff += c.length; 5430 break; 5431 } 5432 5433 case STBI__PNG_TYPE('I','E','N','D'): { 5434 stbi__uint32 raw_len, bpl; 5435 if (first) return stbi__err("first not IHDR", "Corrupt PNG"); 5436 if (scan != STBI__SCAN_load) return 1; 5437 if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG"); 5438 // initial guess for decoded data size to avoid unnecessary reallocs 5439 bpl = (s->img_x * z->depth + 7) / 8; // bytes per line, per component 5440 raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */; 5441 z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone); 5442 if (z->expanded == NULL) return 0; // zlib should set error 5443 STBI_FREE(z->idata); z->idata = NULL; 5444 if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans) 5445 s->img_out_n = s->img_n+1; 5446 else 5447 s->img_out_n = s->img_n; 5448 if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0; 5449 if (has_trans) { 5450 if (z->depth == 16) { 5451 if (!stbi__compute_transparency16(z, tc16, s->img_out_n)) return 0; 5452 } else { 5453 if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0; 5454 } 5455 } 5456 if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2) 5457 stbi__de_iphone(z); 5458 if (pal_img_n) { 5459 // pal_img_n == 3 or 4 5460 s->img_n = pal_img_n; // record the actual colors we had 5461 s->img_out_n = pal_img_n; 5462 if (req_comp >= 3) s->img_out_n = req_comp; 5463 if (!palette_buffer) 5464 if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n)) 5465 return 0; 5466 } else if (has_trans) { 5467 // non-paletted image with tRNS -> source image has (constant) alpha 5468 ++s->img_n; 5469 } 5470 STBI_FREE(z->expanded); z->expanded = NULL; 5471 // end of PNG chunk, read and skip CRC 5472 stbi__get32be(s); 5473 if (s->io.skip && s->img_buffer_end > s->img_buffer) { 5474 // rewind the additional bytes that have been read to the buffer 5475 (s->io.skip)(s->io_user_data, (int)(s->img_buffer - s->img_buffer_end)); 5476 } 5477 return 1; 5478 } 5479 5480 default: 5481 // if critical, fail 5482 if (first) return stbi__err("first not IHDR", "Corrupt PNG"); 5483 if ((c.type & (1 << 29)) == 0) { 5484 #ifndef STBI_NO_FAILURE_STRINGS 5485 // not threadsafe 5486 static char invalid_chunk[] = "XXXX PNG chunk not known"; 5487 invalid_chunk[0] = STBI__BYTECAST(c.type >> 24); 5488 invalid_chunk[1] = STBI__BYTECAST(c.type >> 16); 5489 invalid_chunk[2] = STBI__BYTECAST(c.type >> 8); 5490 invalid_chunk[3] = STBI__BYTECAST(c.type >> 0); 5491 (void)invalid_chunk; 5492 #endif 5493 return stbi__err(invalid_chunk, "PNG not supported: unknown PNG chunk type"); 5494 } 5495 stbi__skip(s, c.length); 5496 break; 5497 } 5498 // end of PNG chunk, read and skip CRC 5499 stbi__get32be(s); 5500 } 5501} 5502 5503static void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, unsigned int *palette_buffer, int palette_buffer_len, stbi__result_info *ri) 5504{ 5505 void *result=NULL; 5506 if (palette_buffer && req_comp != 1) { 5507 stbi__err("bad req_comp", "req_comp must be 1 if loading paletted image without expansion"); 5508 return NULL; 5509 } 5510 if (req_comp < 0 || req_comp > 4) { 5511 stbi__err("bad req_comp", "Internal error"); 5512 return NULL; 5513 } 5514 if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp, palette_buffer, palette_buffer_len)) { 5515 if (p->depth <= 8) 5516 ri->bits_per_channel = 8; 5517 else if (p->depth == 16) 5518 ri->bits_per_channel = 16; 5519 else 5520 return stbi__errpuc("bad bits_per_channel", "PNG not supported: unsupported color depth"); 5521 result = p->out; 5522 p->out = NULL; 5523 if (req_comp && req_comp != p->s->img_out_n) { 5524 if (palette_buffer) 5525 ; 5526 else if (ri->bits_per_channel == 8) 5527 result = stbi__convert_format((unsigned char *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y); 5528 else 5529 result = stbi__convert_format16((stbi__uint16 *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y); 5530 p->s->img_out_n = req_comp; 5531 if (result == NULL) return result; 5532 } 5533 *x = p->s->img_x; 5534 *y = p->s->img_y; 5535 if (n) { 5536 if (palette_buffer) 5537 *n = 1; 5538 else 5539 *n = p->s->img_n; 5540 } 5541 } 5542 STBI_FREE(p->out); p->out = NULL; 5543 STBI_FREE(p->expanded); p->expanded = NULL; 5544 STBI_FREE(p->idata); p->idata = NULL; 5545 5546 return result; 5547} 5548 5549static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, unsigned int *palette_buffer, int palette_buffer_len, stbi__result_info *ri) 5550{ 5551 stbi__png p; 5552 p.s = s; 5553 return stbi__do_png(&p, x,y,comp,req_comp, palette_buffer, palette_buffer_len, ri); 5554} 5555 5556static int stbi__png_test(stbi__context *s) 5557{ 5558 int r; 5559 r = stbi__check_png_header(s); 5560 stbi__rewind(s); 5561 return r; 5562} 5563 5564#if 0 /* not used in SDL */ 5565static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp) 5566{ 5567 if (!stbi__parse_png_file(p, STBI__SCAN_header, NULL, 0, NULL)) { 5568 stbi__rewind( p->s ); 5569 return 0; 5570 } 5571 if (x) *x = p->s->img_x; 5572 if (y) *y = p->s->img_y; 5573 if (comp) *comp = p->s->img_n; 5574 return 1; 5575} 5576 5577static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp) 5578{ 5579 stbi__png p; 5580 p.s = s; 5581 return stbi__png_info_raw(&p, x, y, comp); 5582} 5583 5584static int stbi__png_is16(stbi__context *s) 5585{ 5586 stbi__png p; 5587 p.s = s; 5588 if (!stbi__png_info_raw(&p, NULL, NULL, NULL)) 5589 return 0; 5590 if (p.depth != 16) { 5591 stbi__rewind(p.s); 5592 return 0; 5593 } 5594 return 1; 5595} 5596#endif /**/ 5597#endif 5598 5599// Microsoft/Windows BMP image 5600 5601#ifndef STBI_NO_BMP 5602static int stbi__bmp_test_raw(stbi__context *s) 5603{ 5604 int r; 5605 int sz; 5606 if (stbi__get8(s) != 'B') return 0; 5607 if (stbi__get8(s) != 'M') return 0; 5608 stbi__get32le(s); // discard filesize 5609 stbi__get16le(s); // discard reserved 5610 stbi__get16le(s); // discard reserved 5611 stbi__get32le(s); // discard data offset 5612 sz = stbi__get32le(s); 5613 r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124); 5614 return r; 5615} 5616 5617static int stbi__bmp_test(stbi__context *s) 5618{ 5619 int r = stbi__bmp_test_raw(s); 5620 stbi__rewind(s); 5621 return r; 5622} 5623 5624 5625// returns 0..31 for the highest set bit 5626static int stbi__high_bit(unsigned int z) 5627{ 5628 int n=0; 5629 if (z == 0) return -1; 5630 if (z >= 0x10000) { n += 16; z >>= 16; } 5631 if (z >= 0x00100) { n += 8; z >>= 8; } 5632 if (z >= 0x00010) { n += 4; z >>= 4; } 5633 if (z >= 0x00004) { n += 2; z >>= 2; } 5634 if (z >= 0x00002) { n += 1;/* >>= 1;*/ } 5635 return n; 5636} 5637 5638static int stbi__bitcount(unsigned int a) 5639{ 5640 a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2 5641 a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4 5642 a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits 5643 a = (a + (a >> 8)); // max 16 per 8 bits 5644 a = (a + (a >> 16)); // max 32 per 8 bits 5645 return a & 0xff; 5646} 5647 5648// extract an arbitrarily-aligned N-bit value (N=bits) 5649// from v, and then make it 8-bits long and fractionally 5650// extend it to full full range. 5651static int stbi__shiftsigned(unsigned int v, int shift, int bits) 5652{ 5653 static unsigned int mul_table[9] = { 5654 0, 5655 0xff/*0b11111111*/, 0x55/*0b01010101*/, 0x49/*0b01001001*/, 0x11/*0b00010001*/, 5656 0x21/*0b00100001*/, 0x41/*0b01000001*/, 0x81/*0b10000001*/, 0x01/*0b00000001*/, 5657 }; 5658 static unsigned int shift_table[9] = { 5659 0, 0,0,1,0,2,4,6,0, 5660 }; 5661 if (shift < 0) 5662 v <<= -shift; 5663 else 5664 v >>= shift; 5665 STBI_ASSERT(v < 256); 5666 v >>= (8-bits); 5667 STBI_ASSERT(bits >= 0 && bits <= 8); 5668 return (int) ((unsigned) v * mul_table[bits]) >> shift_table[bits]; 5669} 5670 5671typedef struct 5672{ 5673 int bpp, offset, hsz; 5674 unsigned int mr,mg,mb,ma, all_a; 5675 int extra_read; 5676} stbi__bmp_data; 5677 5678static int stbi__bmp_set_mask_defaults(stbi__bmp_data *info, int compress) 5679{ 5680 // BI_BITFIELDS specifies masks explicitly, don't override 5681 if (compress == 3) 5682 return 1; 5683 5684 if (compress == 0) { 5685 if (info->bpp == 16) { 5686 info->mr = 31u << 10; 5687 info->mg = 31u << 5; 5688 info->mb = 31u << 0; 5689 } else if (info->bpp == 32) { 5690 info->mr = 0xffu << 16; 5691 info->mg = 0xffu << 8; 5692 info->mb = 0xffu << 0; 5693 info->ma = 0xffu << 24; 5694 info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0 5695 } else { 5696 // otherwise, use defaults, which is all-0 5697 info->mr = info->mg = info->mb = info->ma = 0; 5698 } 5699 return 1; 5700 } 5701 return 0; // error 5702} 5703 5704static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) 5705{ 5706 int hsz; 5707 if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP"); 5708 stbi__get32le(s); // discard filesize 5709 stbi__get16le(s); // discard reserved 5710 stbi__get16le(s); // discard reserved 5711 info->offset = stbi__get32le(s); 5712 info->hsz = hsz = stbi__get32le(s); 5713 info->mr = info->mg = info->mb = info->ma = 0; 5714 info->extra_read = 14; 5715 5716 if (info->offset < 0) return stbi__errpuc("bad BMP", "bad BMP"); 5717 5718 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown"); 5719 if (hsz == 12) { 5720 s->img_x = stbi__get16le(s); 5721 s->img_y = stbi__get16le(s); 5722 } else { 5723 s->img_x = stbi__get32le(s); 5724 s->img_y = stbi__get32le(s); 5725 } 5726 if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP"); 5727 info->bpp = stbi__get16le(s); 5728 if (hsz != 12) { 5729 int compress = stbi__get32le(s); 5730 if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE"); 5731 if (compress >= 4) return stbi__errpuc("BMP JPEG/PNG", "BMP type not supported: unsupported compression"); // this includes PNG/JPEG modes 5732 if (compress == 3 && info->bpp != 16 && info->bpp != 32) return stbi__errpuc("bad BMP", "bad BMP"); // bitfields requires 16 or 32 bits/pixel 5733 stbi__get32le(s); // discard sizeof 5734 stbi__get32le(s); // discard hres 5735 stbi__get32le(s); // discard vres 5736 stbi__get32le(s); // discard colorsused 5737 stbi__get32le(s); // discard max important 5738 if (hsz == 40 || hsz == 56) { 5739 if (hsz == 56) { 5740 stbi__get32le(s); 5741 stbi__get32le(s); 5742 stbi__get32le(s); 5743 stbi__get32le(s); 5744 } 5745 if (info->bpp == 16 || info->bpp == 32) { 5746 if (compress == 0) { 5747 stbi__bmp_set_mask_defaults(info, compress); 5748 } else if (compress == 3) { 5749 info->mr = stbi__get32le(s); 5750 info->mg = stbi__get32le(s); 5751 info->mb = stbi__get32le(s); 5752 info->extra_read += 12; 5753 // not documented, but generated by photoshop and handled by mspaint 5754 if (info->mr == info->mg && info->mg == info->mb) { 5755 // ?!?!? 5756 return stbi__errpuc("bad BMP", "bad BMP"); 5757 } 5758 } else 5759 return stbi__errpuc("bad BMP", "bad BMP"); 5760 } 5761 } else { 5762 // V4/V5 header 5763 int i; 5764 if (hsz != 108 && hsz != 124) 5765 return stbi__errpuc("bad BMP", "bad BMP"); 5766 info->mr = stbi__get32le(s); 5767 info->mg = stbi__get32le(s); 5768 info->mb = stbi__get32le(s); 5769 info->ma = stbi__get32le(s); 5770 if (compress != 3) // override mr/mg/mb unless in BI_BITFIELDS mode, as per docs 5771 stbi__bmp_set_mask_defaults(info, compress); 5772 stbi__get32le(s); // discard color space 5773 for (i=0; i < 12; ++i) 5774 stbi__get32le(s); // discard color space parameters 5775 if (hsz == 124) { 5776 stbi__get32le(s); // discard rendering intent 5777 stbi__get32le(s); // discard offset of profile data 5778 stbi__get32le(s); // discard size of profile data 5779 stbi__get32le(s); // discard reserved 5780 } 5781 } 5782 } 5783 return (void *) 1; 5784} 5785 5786 5787static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) 5788{ 5789 stbi_uc *out; 5790 unsigned int mr=0,mg=0,mb=0,ma=0, all_a; 5791 stbi_uc pal[256][4]; 5792 int psize=0,i,j,width; 5793 int flip_vertically, pad, target; 5794 stbi__bmp_data info; 5795 STBI_NOTUSED(ri); 5796 5797 info.all_a = 255; 5798 if (stbi__bmp_parse_header(s, &info) == NULL) 5799 return NULL; // error code already set 5800 5801 flip_vertically = ((int) s->img_y) > 0; 5802 s->img_y = abs((int) s->img_y); 5803 5804 if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); 5805 if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); 5806 5807 mr = info.mr; 5808 mg = info.mg; 5809 mb = info.mb; 5810 ma = info.ma; 5811 all_a = info.all_a; 5812 5813 if (info.hsz == 12) { 5814 if (info.bpp < 24) 5815 psize = (info.offset - info.extra_read - 24) / 3; 5816 } else { 5817 if (info.bpp < 16) 5818 psize = (info.offset - info.extra_read - info.hsz) >> 2; 5819 } 5820 if (psize == 0) { 5821 // accept some number of extra bytes after the header, but if the offset points either to before 5822 // the header ends or implies a large amount of extra data, reject the file as malformed 5823 int bytes_read_so_far = s->callback_already_read + (int)(s->img_buffer - s->img_buffer_original); 5824 int header_limit = 1024; // max we actually read is below 256 bytes currently. 5825 int extra_data_limit = 256*4; // what ordinarily goes here is a palette; 256 entries*4 bytes is its max size. 5826 if (bytes_read_so_far <= 0 || bytes_read_so_far > header_limit) { 5827 return stbi__errpuc("bad header", "Corrupt BMP"); 5828 } 5829 // we established that bytes_read_so_far is positive and sensible. 5830 // the first half of this test rejects offsets that are either too small positives, or 5831 // negative, and guarantees that info.offset >= bytes_read_so_far > 0. this in turn 5832 // ensures the number computed in the second half of the test can't overflow. 5833 if (info.offset < bytes_read_so_far || info.offset - bytes_read_so_far > extra_data_limit) { 5834 return stbi__errpuc("bad offset", "Corrupt BMP"); 5835 } else { 5836 stbi__skip(s, info.offset - bytes_read_so_far); 5837 } 5838 } 5839 5840 if (info.bpp == 24 && ma == 0xff000000) 5841 s->img_n = 3; 5842 else 5843 s->img_n = ma ? 4 : 3; 5844 if (req_comp && req_comp >= 3) // we can directly decode 3 or 4 5845 target = req_comp; 5846 else 5847 target = s->img_n; // if they want monochrome, we'll post-convert 5848 5849 // sanity-check size 5850 if (!stbi__mad3sizes_valid(target, s->img_x, s->img_y, 0)) 5851 return stbi__errpuc("too large", "Corrupt BMP"); 5852 5853 out = (stbi_uc *) stbi__malloc_mad3(target, s->img_x, s->img_y, 0); 5854 if (!out) return stbi__errpuc("outofmem", "Out of memory"); 5855 if (info.bpp < 16) { 5856 int z=0; 5857 if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); } 5858 for (i=0; i < psize; ++i) { 5859 pal[i][2] = stbi__get8(s); 5860 pal[i][1] = stbi__get8(s); 5861 pal[i][0] = stbi__get8(s); 5862 if (info.hsz != 12) stbi__get8(s); 5863 pal[i][3] = 255; 5864 } 5865 stbi__skip(s, info.offset - info.extra_read - info.hsz - psize * (info.hsz == 12 ? 3 : 4)); 5866 if (info.bpp == 1) width = (s->img_x + 7) >> 3; 5867 else if (info.bpp == 4) width = (s->img_x + 1) >> 1; 5868 else if (info.bpp == 8) width = s->img_x; 5869 else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); } 5870 pad = (-width)&3; 5871 if (info.bpp == 1) { 5872 for (j=0; j < (int) s->img_y; ++j) { 5873 int bit_offset = 7, v = stbi__get8(s); 5874 for (i=0; i < (int) s->img_x; ++i) { 5875 int color = (v>>bit_offset)&0x1; 5876 out[z++] = pal[color][0]; 5877 out[z++] = pal[color][1]; 5878 out[z++] = pal[color][2]; 5879 if (target == 4) out[z++] = 255; 5880 if (i+1 == (int) s->img_x) break; 5881 if((--bit_offset) < 0) { 5882 bit_offset = 7; 5883 v = stbi__get8(s); 5884 } 5885 } 5886 stbi__skip(s, pad); 5887 } 5888 } else { 5889 for (j=0; j < (int) s->img_y; ++j) { 5890 for (i=0; i < (int) s->img_x; i += 2) { 5891 int v=stbi__get8(s),v2=0; 5892 if (info.bpp == 4) { 5893 v2 = v & 15; 5894 v >>= 4; 5895 } 5896 out[z++] = pal[v][0]; 5897 out[z++] = pal[v][1]; 5898 out[z++] = pal[v][2]; 5899 if (target == 4) out[z++] = 255; 5900 if (i+1 == (int) s->img_x) break; 5901 v = (info.bpp == 8) ? stbi__get8(s) : v2; 5902 out[z++] = pal[v][0]; 5903 out[z++] = pal[v][1]; 5904 out[z++] = pal[v][2]; 5905 if (target == 4) out[z++] = 255; 5906 } 5907 stbi__skip(s, pad); 5908 } 5909 } 5910 } else { 5911 int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0; 5912 int z = 0; 5913 int easy=0; 5914 stbi__skip(s, info.offset - info.extra_read - info.hsz); 5915 if (info.bpp == 24) width = 3 * s->img_x; 5916 else if (info.bpp == 16) width = 2*s->img_x; 5917 else /* bpp = 32 and pad = 0 */ width=0; 5918 pad = (-width) & 3; 5919 if (info.bpp == 24) { 5920 easy = 1; 5921 } else if (info.bpp == 32) { 5922 if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000) 5923 easy = 2; 5924 } 5925 if (!easy) { 5926 if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); } 5927 // right shift amt to put high bit in position #7 5928 rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr); 5929 gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg); 5930 bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb); 5931 ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma); 5932 if (rcount > 8 || gcount > 8 || bcount > 8 || acount > 8) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); } 5933 } 5934 for (j=0; j < (int) s->img_y; ++j) { 5935 if (easy) { 5936 for (i=0; i < (int) s->img_x; ++i) { 5937 unsigned char a; 5938 out[z+2] = stbi__get8(s); 5939 out[z+1] = stbi__get8(s); 5940 out[z+0] = stbi__get8(s); 5941 z += 3; 5942 a = (easy == 2 ? stbi__get8(s) : 255); 5943 all_a |= a; 5944 if (target == 4) out[z++] = a; 5945 } 5946 } else { 5947 int bpp = info.bpp; 5948 for (i=0; i < (int) s->img_x; ++i) { 5949 stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s)); 5950 unsigned int a; 5951 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount)); 5952 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount)); 5953 out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount)); 5954 a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255); 5955 all_a |= a; 5956 if (target == 4) out[z++] = STBI__BYTECAST(a); 5957 } 5958 } 5959 stbi__skip(s, pad); 5960 } 5961 } 5962 5963 // if alpha channel is all 0s, replace with all 255s 5964 if (target == 4 && all_a == 0) 5965 for (i=4*s->img_x*s->img_y-1; i >= 0; i -= 4) 5966 out[i] = 255; 5967 5968 if (flip_vertically) { 5969 stbi_uc t; 5970 for (j=0; j < (int) s->img_y>>1; ++j) { 5971 stbi_uc *p1 = out + j *s->img_x*target; 5972 stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target; 5973 for (i=0; i < (int) s->img_x*target; ++i) { 5974 t = p1[i]; p1[i] = p2[i]; p2[i] = t; 5975 } 5976 } 5977 } 5978 5979 if (req_comp && req_comp != target) { 5980 out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y); 5981 if (out == NULL) return out; // stbi__convert_format frees input on failure 5982 } 5983 5984 *x = s->img_x; 5985 *y = s->img_y; 5986 if (comp) *comp = s->img_n; 5987 return out; 5988} 5989#endif 5990 5991// Targa Truevision - TGA 5992// by Jonathan Dummer 5993#ifndef STBI_NO_TGA 5994// returns STBI_rgb or whatever, 0 on error 5995static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16) 5996{ 5997 // only RGB or RGBA (incl. 16bit) or grey allowed 5998 if (is_rgb16) *is_rgb16 = 0; 5999 switch(bits_per_pixel) { 6000 case 8: return STBI_grey; 6001 case 16: if(is_grey) return STBI_grey_alpha; 6002 // fallthrough 6003 case 15: if(is_rgb16) *is_rgb16 = 1; 6004 return STBI_rgb; 6005 case 24: // fallthrough 6006 case 32: return bits_per_pixel/8; 6007 default: return 0; 6008 } 6009} 6010 6011static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp) 6012{ 6013 int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp; 6014 int sz, tga_colormap_type; 6015 stbi__get8(s); // discard Offset 6016 tga_colormap_type = stbi__get8(s); // colormap type 6017 if( tga_colormap_type > 1 ) { 6018 stbi__rewind(s); 6019 return 0; // only RGB or indexed allowed 6020 } 6021 tga_image_type = stbi__get8(s); // image type 6022 if ( tga_colormap_type == 1 ) { // colormapped (paletted) image 6023 if (tga_image_type != 1 && tga_image_type != 9) { 6024 stbi__rewind(s); 6025 return 0; 6026 } 6027 stbi__skip(s,4); // skip index of first colormap entry and number of entries 6028 sz = stbi__get8(s); // check bits per palette color entry 6029 if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) { 6030 stbi__rewind(s); 6031 return 0; 6032 } 6033 stbi__skip(s,4); // skip image x and y origin 6034 tga_colormap_bpp = sz; 6035 } else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE 6036 if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) { 6037 stbi__rewind(s); 6038 return 0; // only RGB or grey allowed, +/- RLE 6039 } 6040 stbi__skip(s,9); // skip colormap specification and image x/y origin 6041 tga_colormap_bpp = 0; 6042 } 6043 tga_w = stbi__get16le(s); 6044 if( tga_w < 1 ) { 6045 stbi__rewind(s); 6046 return 0; // test width 6047 } 6048 tga_h = stbi__get16le(s); 6049 if( tga_h < 1 ) { 6050 stbi__rewind(s); 6051 return 0; // test height 6052 } 6053 tga_bits_per_pixel = stbi__get8(s); // bits per pixel 6054 stbi__get8(s); // ignore alpha bits 6055 if (tga_colormap_bpp != 0) { 6056 if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) { 6057 // when using a colormap, tga_bits_per_pixel is the size of the indexes 6058 // I don't think anything but 8 or 16bit indexes makes sense 6059 stbi__rewind(s); 6060 return 0; 6061 } 6062 tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL); 6063 } else { 6064 tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL); 6065 } 6066 if(!tga_comp) { 6067 stbi__rewind(s); 6068 return 0; 6069 } 6070 if (x) *x = tga_w; 6071 if (y) *y = tga_h; 6072 if (comp) *comp = tga_comp; 6073 return 1; // seems to have passed everything 6074} 6075 6076static int stbi__tga_test(stbi__context *s) 6077{ 6078 int res = 0; 6079 int sz, tga_color_type; 6080 stbi__get8(s); // discard Offset 6081 tga_color_type = stbi__get8(s); // color type 6082 if ( tga_color_type > 1 ) goto errorEnd; // only RGB or indexed allowed 6083 sz = stbi__get8(s); // image type 6084 if ( tga_color_type == 1 ) { // colormapped (paletted) image 6085 if (sz != 1 && sz != 9) goto errorEnd; // colortype 1 demands image type 1 or 9 6086 stbi__skip(s,4); // skip index of first colormap entry and number of entries 6087 sz = stbi__get8(s); // check bits per palette color entry 6088 if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd; 6089 stbi__skip(s,4); // skip image x and y origin 6090 } else { // "normal" image w/o colormap 6091 if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) ) goto errorEnd; // only RGB or grey allowed, +/- RLE 6092 stbi__skip(s,9); // skip colormap specification and image x/y origin 6093 } 6094 if ( stbi__get16le(s) < 1 ) goto errorEnd; // test width 6095 if ( stbi__get16le(s) < 1 ) goto errorEnd; // test height 6096 sz = stbi__get8(s); // bits per pixel 6097 if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) ) goto errorEnd; // for colormapped images, bpp is size of an index 6098 if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd; 6099 6100 res = 1; // if we got this far, everything's good and we can return 1 instead of 0 6101 6102errorEnd: 6103 stbi__rewind(s); 6104 return res; 6105} 6106 6107// read 16bit value and convert to 24bit RGB 6108static void stbi__tga_read_rgb16(stbi__context *s, stbi_uc* out) 6109{ 6110 stbi__uint16 px = (stbi__uint16)stbi__get16le(s); 6111 stbi__uint16 fiveBitMask = 31; 6112 // we have 3 channels with 5bits each 6113 int r = (px >> 10) & fiveBitMask; 6114 int g = (px >> 5) & fiveBitMask; 6115 int b = px & fiveBitMask; 6116 // Note that this saves the data in RGB(A) order, so it doesn't need to be swapped later 6117 out[0] = (stbi_uc)((r * 255)/31); 6118 out[1] = (stbi_uc)((g * 255)/31); 6119 out[2] = (stbi_uc)((b * 255)/31); 6120 6121 // some people claim that the most significant bit might be used for alpha 6122 // (possibly if an alpha-bit is set in the "image descriptor byte") 6123 // but that only made 16bit test images completely translucent.. 6124 // so let's treat all 15 and 16bit TGAs as RGB with no alpha. 6125} 6126 6127static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, unsigned int *palette_buffer, int palette_buffer_len, stbi__result_info *ri) 6128{ 6129 // read in the TGA header stuff 6130 int tga_offset = stbi__get8(s); 6131 int tga_indexed = stbi__get8(s); 6132 int tga_image_type = stbi__get8(s); 6133 int tga_is_RLE = 0; 6134 int tga_palette_start = stbi__get16le(s); 6135 int tga_palette_len = stbi__get16le(s); 6136 int tga_palette_bits = stbi__get8(s); 6137 int tga_x_origin = stbi__get16le(s); 6138 int tga_y_origin = stbi__get16le(s); 6139 int tga_width = stbi__get16le(s); 6140 int tga_height = stbi__get16le(s); 6141 int tga_bits_per_pixel = stbi__get8(s); 6142 int tga_comp, tga_rgb16=0; 6143 int tga_inverted = stbi__get8(s); 6144 // int tga_alpha_bits = tga_inverted & 15; // the 4 lowest bits - unused (useless?) 6145 // image data 6146 unsigned char *tga_data; 6147 unsigned char *tga_palette = NULL; 6148 int i, j; 6149 unsigned char raw_data[4] = {0}; 6150 int RLE_count = 0; 6151 int RLE_repeating = 0; 6152 int read_next_pixel = 1; 6153 STBI_NOTUSED(ri); 6154 STBI_NOTUSED(tga_x_origin); // @TODO 6155 STBI_NOTUSED(tga_y_origin); // @TODO 6156 6157 if (tga_height > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); 6158 if (tga_width > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); 6159 6160 // do a tiny bit of precessing 6161 if ( tga_image_type >= 8 ) 6162 { 6163 tga_image_type -= 8; 6164 tga_is_RLE = 1; 6165 } 6166 tga_inverted = 1 - ((tga_inverted >> 5) & 1); 6167 6168 // If I'm paletted, then I'll use the number of bits from the palette 6169 if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16); 6170 else tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16); 6171 6172 if(!tga_comp) // shouldn't really happen, stbi__tga_test() should have ensured basic consistency 6173 return stbi__errpuc("bad format", "Can't find out TGA pixelformat"); 6174 6175 // tga info 6176 *x = tga_width; 6177 *y = tga_height; 6178 if (comp) *comp = tga_comp; 6179 6180 if (!stbi__mad3sizes_valid(tga_width, tga_height, tga_comp, 0)) 6181 return stbi__errpuc("too large", "Corrupt TGA"); 6182 6183 tga_data = (unsigned char*)stbi__malloc_mad3(tga_width, tga_height, tga_comp, 0); 6184 if (!tga_data) return stbi__errpuc("outofmem", "Out of memory"); 6185 6186 // skip to the data's starting position (offset usually = 0) 6187 stbi__skip(s, tga_offset ); 6188 6189 if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) { 6190 for (i=0; i < tga_height; ++i) { 6191 int row = tga_inverted ? tga_height -i - 1 : i; 6192 stbi_uc *tga_row = tga_data + row*tga_width*tga_comp; 6193 stbi__getn(s, tga_row, tga_width * tga_comp); 6194 } 6195 } else { 6196 // do I need to load a palette? 6197 if ( tga_indexed) 6198 { 6199 if (tga_palette_len == 0) { /* you have to have at least one entry! */ 6200 STBI_FREE(tga_data); 6201 return stbi__errpuc("bad palette", "Corrupt TGA"); 6202 } 6203 6204 // any data to skip? (offset usually = 0) 6205 stbi__skip(s, tga_palette_start ); 6206 // load the palette 6207 if (palette_buffer) { 6208 if (palette_buffer_len < tga_palette_len * tga_comp) { 6209 STBI_FREE(tga_data); 6210 return stbi__errpuc("buffer too small", "Palette buffer too small"); 6211 } 6212 tga_palette = (unsigned char*)(void*)palette_buffer; 6213 } else { 6214 tga_palette = (unsigned char*)stbi__malloc_mad2(tga_palette_len, tga_comp, 0); 6215 if (!tga_palette) { 6216 STBI_FREE(tga_data); 6217 return stbi__errpuc("outofmem", "Out of memory"); 6218 } 6219 } 6220 if (tga_rgb16) { 6221 stbi_uc *pal_entry = tga_palette; 6222 STBI_ASSERT(tga_comp == STBI_rgb); 6223 for (i=0; i < tga_palette_len; ++i) { 6224 stbi__tga_read_rgb16(s, pal_entry); 6225 pal_entry += tga_comp; 6226 } 6227 } else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) { 6228 STBI_FREE(tga_data); 6229 if (!palette_buffer) 6230 STBI_FREE(tga_palette); 6231 return stbi__errpuc("bad palette", "Corrupt TGA"); 6232 } 6233 } 6234 // load the data 6235 for (i=0; i < tga_width * tga_height; ++i) 6236 { 6237 // if I'm in RLE mode, do I need to get a RLE stbi__pngchunk? 6238 if ( tga_is_RLE ) 6239 { 6240 if ( RLE_count == 0 ) 6241 { 6242 // yep, get the next byte as a RLE command 6243 int RLE_cmd = stbi__get8(s); 6244 RLE_count = 1 + (RLE_cmd & 127); 6245 RLE_repeating = RLE_cmd >> 7; 6246 read_next_pixel = 1; 6247 } else if ( !RLE_repeating ) 6248 { 6249 read_next_pixel = 1; 6250 } 6251 } else 6252 { 6253 read_next_pixel = 1; 6254 } 6255 // OK, if I need to read a pixel, do it now 6256 if ( read_next_pixel ) 6257 { 6258 // load however much data we did have 6259 if ( tga_indexed && !palette_buffer ) 6260 { 6261 // read in index, then perform the lookup 6262 int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s); 6263 if ( pal_idx >= tga_palette_len ) { 6264 // invalid index 6265 pal_idx = 0; 6266 } 6267 pal_idx *= tga_comp; 6268 for (j = 0; j < tga_comp; ++j) { 6269 raw_data[j] = tga_palette[pal_idx+j]; 6270 } 6271 } else if(tga_rgb16) { 6272 STBI_ASSERT(tga_comp == STBI_rgb); 6273 stbi__tga_read_rgb16(s, raw_data); 6274 } else { 6275 // read in the data raw 6276 for (j = 0; j < tga_comp; ++j) { 6277 raw_data[j] = stbi__get8(s); 6278 } 6279 } 6280 // clear the reading flag for the next pixel 6281 read_next_pixel = 0; 6282 } // end of reading a pixel 6283 6284 // copy data 6285 for (j = 0; j < tga_comp; ++j) 6286 tga_data[i*tga_comp+j] = raw_data[j]; 6287 6288 // in case we're in RLE mode, keep counting down 6289 --RLE_count; 6290 } 6291 // do I need to invert the image? 6292 if ( tga_inverted ) 6293 { 6294 for (j = 0; j*2 < tga_height; ++j) 6295 { 6296 int index1 = j * tga_width * tga_comp; 6297 int index2 = (tga_height - 1 - j) * tga_width * tga_comp; 6298 for (i = tga_width * tga_comp; i > 0; --i) 6299 { 6300 unsigned char temp = tga_data[index1]; 6301 tga_data[index1] = tga_data[index2]; 6302 tga_data[index2] = temp; 6303 ++index1; 6304 ++index2; 6305 } 6306 } 6307 } 6308 // clear my palette, if I had one 6309 if ( tga_palette != NULL && !palette_buffer ) 6310 { 6311 STBI_FREE( tga_palette ); 6312 } 6313 } 6314 6315 // swap RGB - if the source data was RGB16, it already is in the right order 6316 if (tga_comp >= 3 && !tga_rgb16) 6317 { 6318 unsigned char* tga_pixel = tga_data; 6319 for (i=0; i < tga_width * tga_height; ++i) 6320 { 6321 unsigned char temp = tga_pixel[0]; 6322 tga_pixel[0] = tga_pixel[2]; 6323 tga_pixel[2] = temp; 6324 tga_pixel += tga_comp; 6325 } 6326 } 6327 6328 // convert to target component count 6329 if (req_comp && req_comp != tga_comp) 6330 tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height); 6331 6332 // the things I do to get rid of an error message, and yet keep 6333 // Microsoft's C compilers happy... [8^( 6334 tga_palette_start = tga_palette_len = tga_palette_bits = 6335 tga_x_origin = tga_y_origin = 0; 6336 STBI_NOTUSED(tga_palette_start); 6337 // OK, done 6338 return tga_data; 6339} 6340#endif 6341 6342// ************************************************************************************************* 6343// Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB 6344 6345#ifndef STBI_NO_PSD 6346static int stbi__psd_test(stbi__context *s) 6347{ 6348 int r = (stbi__get32be(s) == 0x38425053); 6349 stbi__rewind(s); 6350 return r; 6351} 6352 6353static int stbi__psd_decode_rle(stbi__context *s, stbi_uc *p, int pixelCount) 6354{ 6355 int count, nleft, len; 6356 6357 count = 0; 6358 while ((nleft = pixelCount - count) > 0) { 6359 len = stbi__get8(s); 6360 if (len == 128) { 6361 // No-op. 6362 } else if (len < 128) { 6363 // Copy next len+1 bytes literally. 6364 len++; 6365 if (len > nleft) return 0; // corrupt data 6366 count += len; 6367 while (len) { 6368 *p = stbi__get8(s); 6369 p += 4; 6370 len--; 6371 } 6372 } else if (len > 128) { 6373 stbi_uc val; 6374 // Next -len+1 bytes in the dest are replicated from next source byte. 6375 // (Interpret len as a negative 8-bit int.) 6376 len = 257 - len; 6377 if (len > nleft) return 0; // corrupt data 6378 val = stbi__get8(s); 6379 count += len; 6380 while (len) { 6381 *p = val; 6382 p += 4; 6383 len--; 6384 } 6385 } 6386 } 6387 6388 return 1; 6389} 6390 6391static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc) 6392{ 6393 int pixelCount; 6394 int channelCount, compression; 6395 int channel, i; 6396 int bitdepth; 6397 int w,h; 6398 stbi_uc *out; 6399 STBI_NOTUSED(ri); 6400 6401 // Check identifier 6402 if (stbi__get32be(s) != 0x38425053) // "8BPS" 6403 return stbi__errpuc("not PSD", "Corrupt PSD image"); 6404 6405 // Check file type version. 6406 if (stbi__get16be(s) != 1) 6407 return stbi__errpuc("wrong version", "Unsupported version of PSD image"); 6408 6409 // Skip 6 reserved bytes. 6410 stbi__skip(s, 6 ); 6411 6412 // Read the number of channels (R, G, B, A, etc). 6413 channelCount = stbi__get16be(s); 6414 if (channelCount < 0 || channelCount > 16) 6415 return stbi__errpuc("wrong channel count", "Unsupported number of channels in PSD image"); 6416 6417 // Read the rows and columns of the image. 6418 h = stbi__get32be(s); 6419 w = stbi__get32be(s); 6420 6421 if (h > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); 6422 if (w > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); 6423 6424 // Make sure the depth is 8 bits. 6425 bitdepth = stbi__get16be(s); 6426 if (bitdepth != 8 && bitdepth != 16) 6427 return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 or 16 bit"); 6428 6429 // Make sure the color mode is RGB. 6430 // Valid options are: 6431 // 0: Bitmap 6432 // 1: Grayscale 6433 // 2: Indexed color 6434 // 3: RGB color 6435 // 4: CMYK color 6436 // 7: Multichannel 6437 // 8: Duotone 6438 // 9: Lab color 6439 if (stbi__get16be(s) != 3) 6440 return stbi__errpuc("wrong color format", "PSD is not in RGB color format"); 6441 6442 // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.) 6443 stbi__skip(s,stbi__get32be(s) ); 6444 6445 // Skip the image resources. (resolution, pen tool paths, etc) 6446 stbi__skip(s, stbi__get32be(s) ); 6447 6448 // Skip the reserved data. 6449 stbi__skip(s, stbi__get32be(s) ); 6450 6451 // Find out if the data is compressed. 6452 // Known values: 6453 // 0: no compression 6454 // 1: RLE compressed 6455 compression = stbi__get16be(s); 6456 if (compression > 1) 6457 return stbi__errpuc("bad compression", "PSD has an unknown compression format"); 6458 6459 // Check size 6460 if (!stbi__mad3sizes_valid(4, w, h, 0)) 6461 return stbi__errpuc("too large", "Corrupt PSD"); 6462 6463 // Create the destination image. 6464 6465 if (!compression && bitdepth == 16 && bpc == 16) { 6466 out = (stbi_uc *) stbi__malloc_mad3(8, w, h, 0); 6467 ri->bits_per_channel = 16; 6468 } else 6469 out = (stbi_uc *) stbi__malloc(4 * w*h); 6470 6471 if (!out) return stbi__errpuc("outofmem", "Out of memory"); 6472 pixelCount = w*h; 6473 6474 // Initialize the data to zero. 6475 //memset( out, 0, pixelCount * 4 ); 6476 6477 // Finally, the image data. 6478 if (compression) { 6479 // RLE as used by .PSD and .TIFF 6480 // Loop until you get the number of unpacked bytes you are expecting: 6481 // Read the next source byte into n. 6482 // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally. 6483 // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times. 6484 // Else if n is 128, noop. 6485 // Endloop 6486 6487 // The RLE-compressed data is preceded by a 2-byte data count for each row in the data, 6488 // which we're going to just skip. 6489 stbi__skip(s, h * channelCount * 2 ); 6490 6491 // Read the RLE data by channel. 6492 for (channel = 0; channel < 4; channel++) { 6493 stbi_uc *p; 6494 6495 p = out+channel; 6496 if (channel >= channelCount) { 6497 // Fill this channel with default data. 6498 for (i = 0; i < pixelCount; i++, p += 4) 6499 *p = (channel == 3 ? 255 : 0); 6500 } else { 6501 // Read the RLE data. 6502 if (!stbi__psd_decode_rle(s, p, pixelCount)) { 6503 STBI_FREE(out); 6504 return stbi__errpuc("corrupt", "bad RLE data"); 6505 } 6506 } 6507 } 6508 6509 } else { 6510 // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...) 6511 // where each channel consists of an 8-bit (or 16-bit) value for each pixel in the image. 6512 6513 // Read the data by channel. 6514 for (channel = 0; channel < 4; channel++) { 6515 if (channel >= channelCount) { 6516 // Fill this channel with default data. 6517 if (bitdepth == 16 && bpc == 16) { 6518 stbi__uint16 *q = ((stbi__uint16 *) out) + channel; 6519 stbi__uint16 val = channel == 3 ? 65535 : 0; 6520 for (i = 0; i < pixelCount; i++, q += 4) 6521 *q = val; 6522 } else { 6523 stbi_uc *p = out+channel; 6524 stbi_uc val = channel == 3 ? 255 : 0; 6525 for (i = 0; i < pixelCount; i++, p += 4) 6526 *p = val; 6527 } 6528 } else { 6529 if (ri->bits_per_channel == 16) { // output bpc 6530 stbi__uint16 *q = ((stbi__uint16 *) out) + channel; 6531 for (i = 0; i < pixelCount; i++, q += 4) 6532 *q = (stbi__uint16) stbi__get16be(s); 6533 } else { 6534 stbi_uc *p = out+channel; 6535 if (bitdepth == 16) { // input bpc 6536 for (i = 0; i < pixelCount; i++, p += 4) 6537 *p = (stbi_uc) (stbi__get16be(s) >> 8); 6538 } else { 6539 for (i = 0; i < pixelCount; i++, p += 4) 6540 *p = stbi__get8(s); 6541 } 6542 } 6543 } 6544 } 6545 } 6546 6547 // remove weird white matte from PSD 6548 if (channelCount >= 4) { 6549 if (ri->bits_per_channel == 16) { 6550 for (i=0; i < w*h; ++i) { 6551 stbi__uint16 *pixels = (stbi__uint16 *) out + 4*i; 6552 if (pixels[3] != 0 && pixels[3] != 65535) { 6553 float a = pixels[3] / 65535.0f; 6554 float ra = 1.0f / a; 6555 float inv_a = 65535.0f * (1 - ra); 6556 pixels[0] = (stbi__uint16) (pixels[0]*ra + inv_a); 6557 pixels[1] = (stbi__uint16) (pixels[1]*ra + inv_a); 6558 pixels[2] = (stbi__uint16) (pixels[2]*ra + inv_a); 6559 } 6560 } 6561 } else { 6562 for (i=0; i < w*h; ++i) { 6563 unsigned char *pixels = out + 4*i; 6564 if (pixels[3] != 0 && pixels[3] != 255) { 6565 float a = pixels[3] / 255.0f; 6566 float ra = 1.0f / a; 6567 float inv_a = 255.0f * (1 - ra); 6568 pixels[0] = (unsigned char) (pixels[0]*ra + inv_a); 6569 pixels[1] = (unsigned char) (pixels[1]*ra + inv_a); 6570 pixels[2] = (unsigned char) (pixels[2]*ra + inv_a); 6571 } 6572 } 6573 } 6574 } 6575 6576 // convert to desired output format 6577 if (req_comp && req_comp != 4) { 6578 if (ri->bits_per_channel == 16) 6579 out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, 4, req_comp, w, h); 6580 else 6581 out = stbi__convert_format(out, 4, req_comp, w, h); 6582 if (out == NULL) return out; // stbi__convert_format frees input on failure 6583 } 6584 6585 if (comp) *comp = 4; 6586 *y = h; 6587 *x = w; 6588 6589 return out; 6590} 6591#endif 6592 6593// ************************************************************************************************* 6594// Softimage PIC loader 6595// by Tom Seddon 6596// 6597// See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format 6598// See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/ 6599 6600#ifndef STBI_NO_PIC 6601static int stbi__pic_is4(stbi__context *s,const char *str) 6602{ 6603 int i; 6604 for (i=0; i<4; ++i) 6605 if (stbi__get8(s) != (stbi_uc)str[i]) 6606 return 0; 6607 6608 return 1; 6609} 6610 6611static int stbi__pic_test_core(stbi__context *s) 6612{ 6613 int i; 6614 6615 if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) 6616 return 0; 6617 6618 for(i=0;i<84;++i) 6619 stbi__get8(s); 6620 6621 if (!stbi__pic_is4(s,"PICT")) 6622 return 0; 6623 6624 return 1; 6625} 6626 6627typedef struct 6628{ 6629 stbi_uc size,type,channel; 6630} stbi__pic_packet; 6631 6632static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest) 6633{ 6634 int mask=0x80, i; 6635 6636 for (i=0; i<4; ++i, mask>>=1) { 6637 if (channel & mask) { 6638 if (stbi__at_eof(s)) return stbi__errpuc("bad file","PIC file too short"); 6639 dest[i]=stbi__get8(s); 6640 } 6641 } 6642 6643 return dest; 6644} 6645 6646static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src) 6647{ 6648 int mask=0x80,i; 6649 6650 for (i=0;i<4; ++i, mask>>=1) 6651 if (channel&mask) 6652 dest[i]=src[i]; 6653} 6654 6655static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result) 6656{ 6657 int act_comp=0,num_packets=0,y,chained; 6658 stbi__pic_packet packets[10]; 6659 6660 // this will (should...) cater for even some bizarre stuff like having data 6661 // for the same channel in multiple packets. 6662 do { 6663 stbi__pic_packet *packet; 6664 6665 if (num_packets==sizeof(packets)/sizeof(packets[0])) 6666 return stbi__errpuc("bad format","too many packets"); 6667 6668 packet = &packets[num_packets++]; 6669 6670 chained = stbi__get8(s); 6671 packet->size = stbi__get8(s); 6672 packet->type = stbi__get8(s); 6673 packet->channel = stbi__get8(s); 6674 6675 act_comp |= packet->channel; 6676 6677 if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (reading packets)"); 6678 if (packet->size != 8) return stbi__errpuc("bad format","packet isn't 8bpp"); 6679 } while (chained); 6680 6681 *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel? 6682 6683 for(y=0; y<height; ++y) { 6684 int packet_idx; 6685 6686 for(packet_idx=0; packet_idx < num_packets; ++packet_idx) { 6687 stbi__pic_packet *packet = &packets[packet_idx]; 6688 stbi_uc *dest = result+y*width*4; 6689 6690 switch (packet->type) { 6691 default: 6692 return stbi__errpuc("bad format","packet has bad compression type"); 6693 6694 case 0: {//uncompressed 6695 int x; 6696 6697 for(x=0;x<width;++x, dest+=4) 6698 if (!stbi__readval(s,packet->channel,dest)) 6699 return 0; 6700 break; 6701 } 6702 6703 case 1://Pure RLE 6704 { 6705 int left=width, i; 6706 6707 while (left>0) { 6708 stbi_uc count,value[4]; 6709 6710 count=stbi__get8(s); 6711 if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pure read count)"); 6712 6713 if (count > left) 6714 count = (stbi_uc) left; 6715 6716 if (!stbi__readval(s,packet->channel,value)) return 0; 6717 6718 for(i=0; i<count; ++i,dest+=4) 6719 stbi__copyval(packet->channel,dest,value); 6720 left -= count; 6721 } 6722 } 6723 break; 6724 6725 case 2: {//Mixed RLE 6726 int left=width; 6727 while (left>0) { 6728 int count = stbi__get8(s), i; 6729 if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (mixed read count)"); 6730 6731 if (count >= 128) { // Repeated 6732 stbi_uc value[4]; 6733 6734 if (count==128) 6735 count = stbi__get16be(s); 6736 else 6737 count -= 127; 6738 if (count > left) 6739 return stbi__errpuc("bad file","scanline overrun"); 6740 6741 if (!stbi__readval(s,packet->channel,value)) 6742 return 0; 6743 6744 for(i=0;i<count;++i, dest += 4) 6745 stbi__copyval(packet->channel,dest,value); 6746 } else { // Raw 6747 ++count; 6748 if (count>left) return stbi__errpuc("bad file","scanline overrun"); 6749 6750 for(i=0;i<count;++i, dest+=4) 6751 if (!stbi__readval(s,packet->channel,dest)) 6752 return 0; 6753 } 6754 left-=count; 6755 } 6756 break; 6757 } 6758 } 6759 } 6760 } 6761 6762 return result; 6763} 6764 6765static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp, stbi__result_info *ri) 6766{ 6767 stbi_uc *result; 6768 int i, x,y, internal_comp; 6769 STBI_NOTUSED(ri); 6770 6771 if (!comp) comp = &internal_comp; 6772 6773 for (i=0; i<92; ++i) 6774 stbi__get8(s); 6775 6776 x = stbi__get16be(s); 6777 y = stbi__get16be(s); 6778 6779 if (y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); 6780 if (x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); 6781 6782 if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)"); 6783 if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode"); 6784 6785 stbi__get32be(s); //skip `ratio' 6786 stbi__get16be(s); //skip `fields' 6787 stbi__get16be(s); //skip `pad' 6788 6789 // intermediate buffer is RGBA 6790 result = (stbi_uc *) stbi__malloc_mad3(x, y, 4, 0); 6791 if (!result) return stbi__errpuc("outofmem", "Out of memory"); 6792 memset(result, 0xff, x*y*4); 6793 6794 if (!stbi__pic_load_core(s,x,y,comp, result)) { 6795 STBI_FREE(result); 6796 return 0; 6797 } 6798 *px = x; 6799 *py = y; 6800 if (req_comp == 0) req_comp = *comp; 6801 result=stbi__convert_format(result,4,req_comp,x,y); 6802 6803 return result; 6804} 6805 6806static int stbi__pic_test(stbi__context *s) 6807{ 6808 int r = stbi__pic_test_core(s); 6809 stbi__rewind(s); 6810 return r; 6811} 6812#endif 6813 6814// ************************************************************************************************* 6815// GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb 6816 6817#ifndef STBI_NO_GIF 6818typedef struct 6819{ 6820 stbi__int16 prefix; 6821 stbi_uc first; 6822 stbi_uc suffix; 6823} stbi__gif_lzw; 6824 6825typedef struct 6826{ 6827 int w,h; 6828 stbi_uc *out; // output buffer (always 4 components) 6829 stbi_uc *background; // The current "background" as far as a gif is concerned 6830 stbi_uc *history; 6831 int flags, bgindex, ratio, transparent, eflags; 6832 stbi_uc pal[256][4]; 6833 stbi_uc lpal[256][4]; 6834 stbi__gif_lzw codes[8192]; 6835 stbi_uc *color_table; 6836 int parse, step; 6837 int lflags; 6838 int start_x, start_y; 6839 int max_x, max_y; 6840 int cur_x, cur_y; 6841 int line_size; 6842 int delay; 6843} stbi__gif; 6844 6845static int stbi__gif_test_raw(stbi__context *s) 6846{ 6847 int sz; 6848 if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0; 6849 sz = stbi__get8(s); 6850 if (sz != '9' && sz != '7') return 0; 6851 if (stbi__get8(s) != 'a') return 0; 6852 return 1; 6853} 6854 6855static int stbi__gif_test(stbi__context *s) 6856{ 6857 int r = stbi__gif_test_raw(s); 6858 stbi__rewind(s); 6859 return r; 6860} 6861 6862static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp) 6863{ 6864 int i; 6865 for (i=0; i < num_entries; ++i) { 6866 pal[i][2] = stbi__get8(s); 6867 pal[i][1] = stbi__get8(s); 6868 pal[i][0] = stbi__get8(s); 6869 pal[i][3] = transp == i ? 0 : 255; 6870 } 6871} 6872 6873static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_info) 6874{ 6875 stbi_uc version; 6876 if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') 6877 return stbi__err("not GIF", "Corrupt GIF"); 6878 6879 version = stbi__get8(s); 6880 if (version != '7' && version != '9') return stbi__err("not GIF", "Corrupt GIF"); 6881 if (stbi__get8(s) != 'a') return stbi__err("not GIF", "Corrupt GIF"); 6882 6883 stbi__g_failure_reason = ""; 6884 g->w = stbi__get16le(s); 6885 g->h = stbi__get16le(s); 6886 g->flags = stbi__get8(s); 6887 g->bgindex = stbi__get8(s); 6888 g->ratio = stbi__get8(s); 6889 g->transparent = -1; 6890 6891 if (g->w > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); 6892 if (g->h > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); 6893 6894 if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments 6895 6896 if (is_info) return 1; 6897 6898 if (g->flags & 0x80) 6899 stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1); 6900 6901 return 1; 6902} 6903 6904static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp) 6905{ 6906 stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif)); 6907 if (!g) return stbi__err("outofmem", "Out of memory"); 6908 if (!stbi__gif_header(s, g, comp, 1)) { 6909 STBI_FREE(g); 6910 stbi__rewind( s ); 6911 return 0; 6912 } 6913 if (x) *x = g->w; 6914 if (y) *y = g->h; 6915 STBI_FREE(g); 6916 return 1; 6917} 6918 6919static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code) 6920{ 6921 stbi_uc *p, *c; 6922 int idx; 6923 6924 // recurse to decode the prefixes, since the linked-list is backwards, 6925 // and working backwards through an interleaved image would be nasty 6926 if (g->codes[code].prefix >= 0) 6927 stbi__out_gif_code(g, g->codes[code].prefix); 6928 6929 if (g->cur_y >= g->max_y) return; 6930 6931 idx = g->cur_x + g->cur_y; 6932 p = &g->out[idx]; 6933 g->history[idx / 4] = 1; 6934 6935 c = &g->color_table[g->codes[code].suffix * 4]; 6936 if (c[3] > 128) { // don't render transparent pixels; 6937 p[0] = c[2]; 6938 p[1] = c[1]; 6939 p[2] = c[0]; 6940 p[3] = c[3]; 6941 } 6942 g->cur_x += 4; 6943 6944 if (g->cur_x >= g->max_x) { 6945 g->cur_x = g->start_x; 6946 g->cur_y += g->step; 6947 6948 while (g->cur_y >= g->max_y && g->parse > 0) { 6949 g->step = (1 << g->parse) * g->line_size; 6950 g->cur_y = g->start_y + (g->step >> 1); 6951 --g->parse; 6952 } 6953 } 6954} 6955 6956static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g) 6957{ 6958 stbi_uc lzw_cs; 6959 stbi__int32 len, init_code; 6960 stbi__uint32 first; 6961 stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear; 6962 stbi__gif_lzw *p; 6963 6964 lzw_cs = stbi__get8(s); 6965 if (lzw_cs > 12) return NULL; 6966 clear = 1 << lzw_cs; 6967 first = 1; 6968 codesize = lzw_cs + 1; 6969 codemask = (1 << codesize) - 1; 6970 bits = 0; 6971 valid_bits = 0; 6972 for (init_code = 0; init_code < clear; init_code++) { 6973 g->codes[init_code].prefix = -1; 6974 g->codes[init_code].first = (stbi_uc) init_code; 6975 g->codes[init_code].suffix = (stbi_uc) init_code; 6976 } 6977 6978 // support no starting clear code 6979 avail = clear+2; 6980 oldcode = -1; 6981 6982 len = 0; 6983 for(;;) { 6984 if (valid_bits < codesize) { 6985 if (len == 0) { 6986 len = stbi__get8(s); // start new block 6987 if (len == 0) 6988 return g->out; 6989 } 6990 --len; 6991 bits |= (stbi__int32) stbi__get8(s) << valid_bits; 6992 valid_bits += 8; 6993 } else { 6994 stbi__int32 code = bits & codemask; 6995 bits >>= codesize; 6996 valid_bits -= codesize; 6997 // @OPTIMIZE: is there some way we can accelerate the non-clear path? 6998 if (code == clear) { // clear code 6999 codesize = lzw_cs + 1; 7000 codemask = (1 << codesize) - 1; 7001 avail = clear + 2; 7002 oldcode = -1; 7003 first = 0; 7004 } else if (code == clear + 1) { // end of stream code 7005 stbi__skip(s, len); 7006 while ((len = stbi__get8(s)) > 0) 7007 stbi__skip(s,len); 7008 return g->out; 7009 } else if (code <= avail) { 7010 if (first) { 7011 return stbi__errpuc("no clear code", "Corrupt GIF"); 7012 } 7013 7014 if (oldcode >= 0) { 7015 p = &g->codes[avail++]; 7016 if (avail > 8192) { 7017 return stbi__errpuc("too many codes", "Corrupt GIF"); 7018 } 7019 7020 p->prefix = (stbi__int16) oldcode; 7021 p->first = g->codes[oldcode].first; 7022 p->suffix = (code == avail) ? p->first : g->codes[code].first; 7023 } else if (code == avail) 7024 return stbi__errpuc("illegal code in raster", "Corrupt GIF"); 7025 7026 stbi__out_gif_code(g, (stbi__uint16) code); 7027 7028 if ((avail & codemask) == 0 && avail <= 0x0FFF) { 7029 codesize++; 7030 codemask = (1 << codesize) - 1; 7031 } 7032 7033 oldcode = code; 7034 } else { 7035 return stbi__errpuc("illegal code in raster", "Corrupt GIF"); 7036 } 7037 } 7038 } 7039} 7040 7041// this function is designed to support animated gifs, although stb_image doesn't support it 7042// two back is the image from two frames ago, used for a very specific disposal format 7043static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back) 7044{ 7045 int dispose; 7046 int first_frame; 7047 int pi; 7048 int pcount; 7049 STBI_NOTUSED(req_comp); 7050 7051 // on first frame, any non-written pixels get the background colour (non-transparent) 7052 first_frame = 0; 7053 if (g->out == 0) { 7054 if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header 7055 if (!stbi__mad3sizes_valid(4, g->w, g->h, 0)) 7056 return stbi__errpuc("too large", "GIF image is too large"); 7057 pcount = g->w * g->h; 7058 g->out = (stbi_uc *) stbi__malloc(4 * pcount); 7059 g->background = (stbi_uc *) stbi__malloc(4 * pcount); 7060 g->history = (stbi_uc *) stbi__malloc(pcount); 7061 if (!g->out || !g->background || !g->history) 7062 return stbi__errpuc("outofmem", "Out of memory"); 7063 7064 // image is treated as "transparent" at the start - ie, nothing overwrites the current background; 7065 // background colour is only used for pixels that are not rendered first frame, after that "background" 7066 // color refers to the color that was there the previous frame. 7067 memset(g->out, 0x00, 4 * pcount); 7068 memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent) 7069 memset(g->history, 0x00, pcount); // pixels that were affected previous frame 7070 first_frame = 1; 7071 } else { 7072 // second frame - how do we dispose of the previous one? 7073 dispose = (g->eflags & 0x1C) >> 2; 7074 pcount = g->w * g->h; 7075 7076 if ((dispose == 3) && (two_back == 0)) { 7077 dispose = 2; // if I don't have an image to revert back to, default to the old background 7078 } 7079 7080 if (dispose == 3) { // use previous graphic 7081 for (pi = 0; pi < pcount; ++pi) { 7082 if (g->history[pi]) { 7083 memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 ); 7084 } 7085 } 7086 } else if (dispose == 2) { 7087 // restore what was changed last frame to background before that frame; 7088 for (pi = 0; pi < pcount; ++pi) { 7089 if (g->history[pi]) { 7090 memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 ); 7091 } 7092 } 7093 } else { 7094 // This is a non-disposal case eithe way, so just 7095 // leave the pixels as is, and they will become the new background 7096 // 1: do not dispose 7097 // 0: not specified. 7098 } 7099 7100 // background is what out is after the undoing of the previou frame; 7101 memcpy( g->background, g->out, 4 * g->w * g->h ); 7102 } 7103 7104 // clear my history; 7105 memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame 7106 7107 for (;;) { 7108 int tag = stbi__get8(s); 7109 switch (tag) { 7110 case 0x2C: /* Image Descriptor */ 7111 { 7112 stbi__int32 x, y, w, h; 7113 stbi_uc *o; 7114 7115 x = stbi__get16le(s); 7116 y = stbi__get16le(s); 7117 w = stbi__get16le(s); 7118 h = stbi__get16le(s); 7119 if (((x + w) > (g->w)) || ((y + h) > (g->h))) 7120 return stbi__errpuc("bad Image Descriptor", "Corrupt GIF"); 7121 7122 g->line_size = g->w * 4; 7123 g->start_x = x * 4; 7124 g->start_y = y * g->line_size; 7125 g->max_x = g->start_x + w * 4; 7126 g->max_y = g->start_y + h * g->line_size; 7127 g->cur_x = g->start_x; 7128 g->cur_y = g->start_y; 7129 7130 // if the width of the specified rectangle is 0, that means 7131 // we may not see *any* pixels or the image is malformed; 7132 // to make sure this is caught, move the current y down to 7133 // max_y (which is what out_gif_code checks). 7134 if (w == 0) 7135 g->cur_y = g->max_y; 7136 7137 g->lflags = stbi__get8(s); 7138 7139 if (g->lflags & 0x40) { 7140 g->step = 8 * g->line_size; // first interlaced spacing 7141 g->parse = 3; 7142 } else { 7143 g->step = g->line_size; 7144 g->parse = 0; 7145 } 7146 7147 if (g->lflags & 0x80) { 7148 stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1); 7149 g->color_table = (stbi_uc *) g->lpal; 7150 } else if (g->flags & 0x80) { 7151 g->color_table = (stbi_uc *) g->pal; 7152 } else 7153 return stbi__errpuc("missing color table", "Corrupt GIF"); 7154 7155 o = stbi__process_gif_raster(s, g); 7156 if (!o) return NULL; 7157 7158 // if this was the first frame, 7159 pcount = g->w * g->h; 7160 if (first_frame && (g->bgindex > 0)) { 7161 // if first frame, any pixel not drawn to gets the background color 7162 for (pi = 0; pi < pcount; ++pi) { 7163 if (g->history[pi] == 0) { 7164 g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be; 7165 memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 ); 7166 } 7167 } 7168 } 7169 7170 return o; 7171 } 7172 7173 case 0x21: // Comment Extension. 7174 { 7175 int len; 7176 int ext = stbi__get8(s); 7177 if (ext == 0xF9) { // Graphic Control Extension. 7178 len = stbi__get8(s); 7179 if (len == 4) { 7180 g->eflags = stbi__get8(s); 7181 g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths. 7182 7183 // unset old transparent 7184 if (g->transparent >= 0) { 7185 g->pal[g->transparent][3] = 255; 7186 } 7187 if (g->eflags & 0x01) { 7188 g->transparent = stbi__get8(s); 7189 if (g->transparent >= 0) { 7190 g->pal[g->transparent][3] = 0; 7191 } 7192 } else { 7193 // don't need transparent 7194 stbi__skip(s, 1); 7195 g->transparent = -1; 7196 } 7197 } else { 7198 stbi__skip(s, len); 7199 break; 7200 } 7201 } 7202 while ((len = stbi__get8(s)) != 0) { 7203 stbi__skip(s, len); 7204 } 7205 break; 7206 } 7207 7208 case 0x3B: // gif stream termination code 7209 return (stbi_uc *) s; // using '1' causes warning on some compilers 7210 7211 default: 7212 return stbi__errpuc("unknown code", "Corrupt GIF"); 7213 } 7214 } 7215} 7216 7217static void *stbi__load_gif_main_outofmem(stbi__gif *g, stbi_uc *out, int **delays) 7218{ 7219 STBI_FREE(g->out); 7220 STBI_FREE(g->history); 7221 STBI_FREE(g->background); 7222 7223 if (out) STBI_FREE(out); 7224 if (delays && *delays) { 7225 STBI_FREE(*delays); 7226 *delays = NULL; 7227 } 7228 return stbi__errpuc("outofmem", "Out of memory"); 7229} 7230 7231static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp) 7232{ 7233 if (stbi__gif_test(s)) { 7234 int layers = 0; 7235 stbi_uc *u = 0; 7236 stbi_uc *out = 0; 7237 stbi_uc *two_back = 0; 7238 stbi__gif g; 7239 int stride; 7240 int out_size = 0; 7241 int delays_size = 0; 7242 7243 STBI_NOTUSED(out_size); 7244 STBI_NOTUSED(delays_size); 7245 7246 memset(&g, 0, sizeof(g)); 7247 if (delays) { 7248 *delays = 0; 7249 } 7250 7251 do { 7252 u = stbi__gif_load_next(s, &g, comp, req_comp, two_back); 7253 if (u == (stbi_uc *) s) u = 0; // end of animated gif marker 7254 7255 if (u) { 7256 *x = g.w; 7257 *y = g.h; 7258 ++layers; 7259 stride = g.w * g.h * 4; 7260 7261 if (out) { 7262 void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride ); 7263 if (!tmp) 7264 return stbi__load_gif_main_outofmem(&g, out, delays); 7265 else { 7266 out = (stbi_uc*) tmp; 7267 out_size = layers * stride; 7268 } 7269 7270 if (delays) { 7271 int *new_delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers ); 7272 if (!new_delays) 7273 return stbi__load_gif_main_outofmem(&g, out, delays); 7274 *delays = new_delays; 7275 delays_size = layers * sizeof(int); 7276 } 7277 } else { 7278 out = (stbi_uc*)stbi__malloc( layers * stride ); 7279 if (!out) 7280 return stbi__load_gif_main_outofmem(&g, out, delays); 7281 out_size = layers * stride; 7282 if (delays) { 7283 *delays = (int*) stbi__malloc( layers * sizeof(int) ); 7284 if (!*delays) 7285 return stbi__load_gif_main_outofmem(&g, out, delays); 7286 delays_size = layers * sizeof(int); 7287 } 7288 } 7289 memcpy( out + ((layers - 1) * stride), u, stride ); 7290 if (layers >= 2) { 7291 two_back = out - 2 * stride; 7292 } 7293 7294 if (delays) { 7295 (*delays)[layers - 1U] = g.delay; 7296 } 7297 } 7298 } while (u != 0); 7299 7300 // free temp buffer; 7301 STBI_FREE(g.out); 7302 STBI_FREE(g.history); 7303 STBI_FREE(g.background); 7304 7305 // do the final conversion after loading everything; 7306 if (req_comp && req_comp != 4) 7307 out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h); 7308 7309 *z = layers; 7310 return out; 7311 } else { 7312 return stbi__errpuc("not GIF", "Image was not as a gif type."); 7313 } 7314} 7315 7316static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) 7317{ 7318 stbi_uc *u = 0; 7319 stbi__gif g; 7320 memset(&g, 0, sizeof(g)); 7321 STBI_NOTUSED(ri); 7322 7323 u = stbi__gif_load_next(s, &g, comp, req_comp, 0); 7324 if (u == (stbi_uc *) s) u = 0; // end of animated gif marker 7325 if (u) { 7326 *x = g.w; 7327 *y = g.h; 7328 7329 // moved conversion to after successful load so that the same 7330 // can be done for multiple frames. 7331 if (req_comp && req_comp != 4) 7332 u = stbi__convert_format(u, 4, req_comp, g.w, g.h); 7333 } else if (g.out) { 7334 // if there was an error and we allocated an image buffer, free it! 7335 STBI_FREE(g.out); 7336 } 7337 7338 // free buffers needed for multiple frame loading; 7339 STBI_FREE(g.history); 7340 STBI_FREE(g.background); 7341 7342 return u; 7343} 7344 7345static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp) 7346{ 7347 return stbi__gif_info_raw(s,x,y,comp); 7348} 7349#endif 7350 7351// ************************************************************************************************* 7352// Radiance RGBE HDR loader 7353// originally by Nicolas Schulz 7354#ifndef STBI_NO_HDR 7355static int stbi__hdr_test_core(stbi__context *s, const char *signature) 7356{ 7357 int i; 7358 for (i=0; signature[i]; ++i) 7359 if (stbi__get8(s) != signature[i]) 7360 return 0; 7361 stbi__rewind(s); 7362 return 1; 7363} 7364 7365static int stbi__hdr_test(stbi__context* s) 7366{ 7367 int r = stbi__hdr_test_core(s, "#?RADIANCE\n"); 7368 stbi__rewind(s); 7369 if(!r) { 7370 r = stbi__hdr_test_core(s, "#?RGBE\n"); 7371 stbi__rewind(s); 7372 } 7373 return r; 7374} 7375 7376#define STBI__HDR_BUFLEN 1024 7377static char *stbi__hdr_gettoken(stbi__context *z, char *buffer) 7378{ 7379 int len=0; 7380 char c = '\0'; 7381 7382 c = (char) stbi__get8(z); 7383 7384 while (!stbi__at_eof(z) && c != '\n') { 7385 buffer[len++] = c; 7386 if (len == STBI__HDR_BUFLEN-1) { 7387 // flush to end of line 7388 while (!stbi__at_eof(z) && stbi__get8(z) != '\n') 7389 ; 7390 break; 7391 } 7392 c = (char) stbi__get8(z); 7393 } 7394 7395 buffer[len] = 0; 7396 return buffer; 7397} 7398 7399static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp) 7400{ 7401 if ( input[3] != 0 ) { 7402 float f1; 7403 // Exponent 7404 f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8)); 7405 if (req_comp <= 2) 7406 output[0] = (input[0] + input[1] + input[2]) * f1 / 3; 7407 else { 7408 output[0] = input[0] * f1; 7409 output[1] = input[1] * f1; 7410 output[2] = input[2] * f1; 7411 } 7412 if (req_comp == 2) output[1] = 1; 7413 if (req_comp == 4) output[3] = 1; 7414 } else { 7415 switch (req_comp) { 7416 case 4: output[3] = 1; /* fallthrough */ 7417 case 3: output[0] = output[1] = output[2] = 0; 7418 break; 7419 case 2: output[1] = 1; /* fallthrough */ 7420 case 1: output[0] = 0; 7421 break; 7422 } 7423 } 7424} 7425 7426static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) 7427{ 7428 char buffer[STBI__HDR_BUFLEN]; 7429 char *token; 7430 int valid = 0; 7431 int width, height; 7432 stbi_uc *scanline; 7433 float *hdr_data; 7434 int len; 7435 unsigned char count, value; 7436 int i, j, k, c1,c2, z; 7437 const char *headerToken; 7438 STBI_NOTUSED(ri); 7439 7440 // Check identifier 7441 headerToken = stbi__hdr_gettoken(s,buffer); 7442 if (strcmp(headerToken, "#?RADIANCE") != 0 && strcmp(headerToken, "#?RGBE") != 0) 7443 return stbi__errpf("not HDR", "Corrupt HDR image"); 7444 7445 // Parse header 7446 for(;;) { 7447 token = stbi__hdr_gettoken(s,buffer); 7448 if (token[0] == 0) break; 7449 if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1; 7450 } 7451 7452 if (!valid) return stbi__errpf("unsupported format", "Unsupported HDR format"); 7453 7454 // Parse width and height 7455 // can't use sscanf() if we're not using stdio! 7456 token = stbi__hdr_gettoken(s,buffer); 7457 if (strncmp(token, "-Y ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format"); 7458 token += 3; 7459 height = (int) strtol(token, &token, 10); 7460 while (*token == ' ') ++token; 7461 if (strncmp(token, "+X ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format"); 7462 token += 3; 7463 width = (int) strtol(token, NULL, 10); 7464 7465 if (height > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)"); 7466 if (width > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)"); 7467 7468 *x = width; 7469 *y = height; 7470 7471 if (comp) *comp = 3; 7472 if (req_comp == 0) req_comp = 3; 7473 7474 if (!stbi__mad4sizes_valid(width, height, req_comp, sizeof(float), 0)) 7475 return stbi__errpf("too large", "HDR image is too large"); 7476 7477 // Read data 7478 hdr_data = (float *) stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0); 7479 if (!hdr_data) 7480 return stbi__errpf("outofmem", "Out of memory"); 7481 7482 // Load image data 7483 // image data is stored as some number of sca 7484 if ( width < 8 || width >= 32768) { 7485 // Read flat data 7486 for (j=0; j < height; ++j) { 7487 for (i=0; i < width; ++i) { 7488 stbi_uc rgbe[4]; 7489 main_decode_loop: 7490 stbi__getn(s, rgbe, 4); 7491 stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp); 7492 } 7493 } 7494 } else { 7495 // Read RLE-encoded data 7496 scanline = NULL; 7497 7498 for (j = 0; j < height; ++j) { 7499 c1 = stbi__get8(s); 7500 c2 = stbi__get8(s); 7501 len = stbi__get8(s); 7502 if (c1 != 2 || c2 != 2 || (len & 0x80)) { 7503 // not run-length encoded, so we have to actually use THIS data as a decoded 7504 // pixel (note this can't be a valid pixel--one of RGB must be >= 128) 7505 stbi_uc rgbe[4]; 7506 rgbe[0] = (stbi_uc) c1; 7507 rgbe[1] = (stbi_uc) c2; 7508 rgbe[2] = (stbi_uc) len; 7509 rgbe[3] = (stbi_uc) stbi__get8(s); 7510 stbi__hdr_convert(hdr_data, rgbe, req_comp); 7511 i = 1; 7512 j = 0; 7513 STBI_FREE(scanline); 7514 goto main_decode_loop; // yes, this makes no sense 7515 } 7516 len <<= 8; 7517 len |= stbi__get8(s); 7518 if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); } 7519 if (scanline == NULL) { 7520 scanline = (stbi_uc *) stbi__malloc_mad2(width, 4, 0); 7521 if (!scanline) { 7522 STBI_FREE(hdr_data); 7523 return stbi__errpf("outofmem", "Out of memory"); 7524 } 7525 } 7526 7527 for (k = 0; k < 4; ++k) { 7528 int nleft; 7529 i = 0; 7530 while ((nleft = width - i) > 0) { 7531 count = stbi__get8(s); 7532 if (count > 128) { 7533 // Run 7534 value = stbi__get8(s); 7535 count -= 128; 7536 if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); } 7537 for (z = 0; z < count; ++z) 7538 scanline[i++ * 4 + k] = value; 7539 } else { 7540 // Dump 7541 if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); } 7542 for (z = 0; z < count; ++z) 7543 scanline[i++ * 4 + k] = stbi__get8(s); 7544 } 7545 } 7546 } 7547 for (i=0; i < width; ++i) 7548 stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp); 7549 } 7550 if (scanline) 7551 STBI_FREE(scanline); 7552 } 7553 7554 return hdr_data; 7555} 7556 7557static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp) 7558{ 7559 char buffer[STBI__HDR_BUFLEN]; 7560 char *token; 7561 int valid = 0; 7562 int dummy; 7563 7564 if (!x) x = &dummy; 7565 if (!y) y = &dummy; 7566 if (!comp) comp = &dummy; 7567 7568 if (stbi__hdr_test(s) == 0) { 7569 stbi__rewind( s ); 7570 return 0; 7571 } 7572 7573 for(;;) { 7574 token = stbi__hdr_gettoken(s,buffer); 7575 if (token[0] == 0) break; 7576 if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1; 7577 } 7578 7579 if (!valid) { 7580 stbi__rewind( s ); 7581 return 0; 7582 } 7583 token = stbi__hdr_gettoken(s,buffer); 7584 if (strncmp(token, "-Y ", 3)) { 7585 stbi__rewind( s ); 7586 return 0; 7587 } 7588 token += 3; 7589 *y = (int) strtol(token, &token, 10); 7590 while (*token == ' ') ++token; 7591 if (strncmp(token, "+X ", 3)) { 7592 stbi__rewind( s ); 7593 return 0; 7594 } 7595 token += 3; 7596 *x = (int) strtol(token, NULL, 10); 7597 *comp = 3; 7598 return 1; 7599} 7600#endif // STBI_NO_HDR 7601 7602#ifndef STBI_NO_BMP 7603static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp) 7604{ 7605 void *p; 7606 stbi__bmp_data info; 7607 7608 info.all_a = 255; 7609 p = stbi__bmp_parse_header(s, &info); 7610 if (p == NULL) { 7611 stbi__rewind( s ); 7612 return 0; 7613 } 7614 if (x) *x = s->img_x; 7615 if (y) *y = s->img_y; 7616 if (comp) { 7617 if (info.bpp == 24 && info.ma == 0xff000000) 7618 *comp = 3; 7619 else 7620 *comp = info.ma ? 4 : 3; 7621 } 7622 return 1; 7623} 7624#endif 7625 7626#ifndef STBI_NO_PSD 7627static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp) 7628{ 7629 int channelCount, dummy, depth; 7630 if (!x) x = &dummy; 7631 if (!y) y = &dummy; 7632 if (!comp) comp = &dummy; 7633 if (stbi__get32be(s) != 0x38425053) { 7634 stbi__rewind( s ); 7635 return 0; 7636 } 7637 if (stbi__get16be(s) != 1) { 7638 stbi__rewind( s ); 7639 return 0; 7640 } 7641 stbi__skip(s, 6); 7642 channelCount = stbi__get16be(s); 7643 if (channelCount < 0 || channelCount > 16) { 7644 stbi__rewind( s ); 7645 return 0; 7646 } 7647 *y = stbi__get32be(s); 7648 *x = stbi__get32be(s); 7649 depth = stbi__get16be(s); 7650 if (depth != 8 && depth != 16) { 7651 stbi__rewind( s ); 7652 return 0; 7653 } 7654 if (stbi__get16be(s) != 3) { 7655 stbi__rewind( s ); 7656 return 0; 7657 } 7658 *comp = 4; 7659 return 1; 7660} 7661 7662static int stbi__psd_is16(stbi__context *s) 7663{ 7664 int channelCount, depth; 7665 if (stbi__get32be(s) != 0x38425053) { 7666 stbi__rewind( s ); 7667 return 0; 7668 } 7669 if (stbi__get16be(s) != 1) { 7670 stbi__rewind( s ); 7671 return 0; 7672 } 7673 stbi__skip(s, 6); 7674 channelCount = stbi__get16be(s); 7675 if (channelCount < 0 || channelCount > 16) { 7676 stbi__rewind( s ); 7677 return 0; 7678 } 7679 STBI_NOTUSED(stbi__get32be(s)); 7680 STBI_NOTUSED(stbi__get32be(s)); 7681 depth = stbi__get16be(s); 7682 if (depth != 16) { 7683 stbi__rewind( s ); 7684 return 0; 7685 } 7686 return 1; 7687} 7688#endif 7689 7690#ifndef STBI_NO_PIC 7691static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp) 7692{ 7693 int act_comp=0,num_packets=0,chained,dummy; 7694 stbi__pic_packet packets[10]; 7695 7696 if (!x) x = &dummy; 7697 if (!y) y = &dummy; 7698 if (!comp) comp = &dummy; 7699 7700 if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) { 7701 stbi__rewind(s); 7702 return 0; 7703 } 7704 7705 stbi__skip(s, 88); 7706 7707 *x = stbi__get16be(s); 7708 *y = stbi__get16be(s); 7709 if (stbi__at_eof(s)) { 7710 stbi__rewind( s); 7711 return 0; 7712 } 7713 if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) { 7714 stbi__rewind( s ); 7715 return 0; 7716 } 7717 7718 stbi__skip(s, 8); 7719 7720 do { 7721 stbi__pic_packet *packet; 7722 7723 if (num_packets==sizeof(packets)/sizeof(packets[0])) 7724 return 0; 7725 7726 packet = &packets[num_packets++]; 7727 chained = stbi__get8(s); 7728 packet->size = stbi__get8(s); 7729 packet->type = stbi__get8(s); 7730 packet->channel = stbi__get8(s); 7731 act_comp |= packet->channel; 7732 7733 if (stbi__at_eof(s)) { 7734 stbi__rewind( s ); 7735 return 0; 7736 } 7737 if (packet->size != 8) { 7738 stbi__rewind( s ); 7739 return 0; 7740 } 7741 } while (chained); 7742 7743 *comp = (act_comp & 0x10 ? 4 : 3); 7744 7745 return 1; 7746} 7747#endif 7748 7749// ************************************************************************************************* 7750// Portable Gray Map and Portable Pixel Map loader 7751// by Ken Miller 7752// 7753// PGM: http://netpbm.sourceforge.net/doc/pgm.html 7754// PPM: http://netpbm.sourceforge.net/doc/ppm.html 7755// 7756// Known limitations: 7757// Does not support comments in the header section 7758// Does not support ASCII image data (formats P2 and P3) 7759 7760#ifndef STBI_NO_PNM 7761 7762static int stbi__pnm_test(stbi__context *s) 7763{ 7764 char p, t; 7765 p = (char) stbi__get8(s); 7766 t = (char) stbi__get8(s); 7767 if (p != 'P' || (t != '5' && t != '6')) { 7768 stbi__rewind( s ); 7769 return 0; 7770 } 7771 return 1; 7772} 7773 7774static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) 7775{ 7776 stbi_uc *out; 7777 STBI_NOTUSED(ri); 7778 7779 ri->bits_per_channel = stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n); 7780 if (ri->bits_per_channel == 0) 7781 return 0; 7782 7783 if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); 7784 if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); 7785 7786 *x = s->img_x; 7787 *y = s->img_y; 7788 if (comp) *comp = s->img_n; 7789 7790 if (!stbi__mad4sizes_valid(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0)) 7791 return stbi__errpuc("too large", "PNM too large"); 7792 7793 out = (stbi_uc *) stbi__malloc_mad4(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0); 7794 if (!out) return stbi__errpuc("outofmem", "Out of memory"); 7795 if (!stbi__getn(s, out, s->img_n * s->img_x * s->img_y * (ri->bits_per_channel / 8))) { 7796 STBI_FREE(out); 7797 return stbi__errpuc("bad PNM", "PNM file truncated"); 7798 } 7799 7800 if (req_comp && req_comp != s->img_n) { 7801 if (ri->bits_per_channel == 16) { 7802 out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, s->img_n, req_comp, s->img_x, s->img_y); 7803 } else { 7804 out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y); 7805 } 7806 if (out == NULL) return out; // stbi__convert_format frees input on failure 7807 } 7808 return out; 7809} 7810 7811static int stbi__pnm_isspace(char c) 7812{ 7813 return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r'; 7814} 7815 7816static void stbi__pnm_skip_whitespace(stbi__context *s, char *c) 7817{ 7818 for (;;) { 7819 while (!stbi__at_eof(s) && stbi__pnm_isspace(*c)) 7820 *c = (char) stbi__get8(s); 7821 7822 if (stbi__at_eof(s) || *c != '#') 7823 break; 7824 7825 while (!stbi__at_eof(s) && *c != '\n' && *c != '\r' ) 7826 *c = (char) stbi__get8(s); 7827 } 7828} 7829 7830static int stbi__pnm_isdigit(char c) 7831{ 7832 return c >= '0' && c <= '9'; 7833} 7834 7835static int stbi__pnm_getinteger(stbi__context *s, char *c) 7836{ 7837 int value = 0; 7838 7839 while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) { 7840 value = value*10 + (*c - '0'); 7841 *c = (char) stbi__get8(s); 7842 if((value > 214748364) || (value == 214748364 && *c > '7')) 7843 return stbi__err("integer parse overflow", "Parsing an integer in the PPM header overflowed a 32-bit int"); 7844 } 7845 7846 return value; 7847} 7848 7849static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp) 7850{ 7851 int maxv, dummy; 7852 char c, p, t; 7853 7854 if (!x) x = &dummy; 7855 if (!y) y = &dummy; 7856 if (!comp) comp = &dummy; 7857 7858 stbi__rewind(s); 7859 7860 // Get identifier 7861 p = (char) stbi__get8(s); 7862 t = (char) stbi__get8(s); 7863 if (p != 'P' || (t != '5' && t != '6')) { 7864 stbi__rewind(s); 7865 return 0; 7866 } 7867 7868 *comp = (t == '6') ? 3 : 1; // '5' is 1-component .pgm; '6' is 3-component .ppm 7869 7870 c = (char) stbi__get8(s); 7871 stbi__pnm_skip_whitespace(s, &c); 7872 7873 *x = stbi__pnm_getinteger(s, &c); // read width 7874 if(*x == 0) 7875 return stbi__err("invalid width", "PPM image header had zero or overflowing width"); 7876 stbi__pnm_skip_whitespace(s, &c); 7877 7878 *y = stbi__pnm_getinteger(s, &c); // read height 7879 if (*y == 0) 7880 return stbi__err("invalid width", "PPM image header had zero or overflowing width"); 7881 stbi__pnm_skip_whitespace(s, &c); 7882 7883 maxv = stbi__pnm_getinteger(s, &c); // read max value 7884 if (maxv > 65535) 7885 return stbi__err("max value > 65535", "PPM image supports only 8-bit and 16-bit images"); 7886 else if (maxv > 255) 7887 return 16; 7888 else 7889 return 8; 7890} 7891 7892static int stbi__pnm_is16(stbi__context *s) 7893{ 7894 if (stbi__pnm_info(s, NULL, NULL, NULL) == 16) 7895 return 1; 7896 return 0; 7897} 7898#endif 7899 7900#if 0 /* not used in SDL */ 7901static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp) 7902{ 7903 #ifndef STBI_NO_JPEG 7904 if (stbi__jpeg_info(s, x, y, comp)) return 1; 7905 #endif 7906 7907 #ifndef STBI_NO_PNG 7908 if (stbi__png_info(s, x, y, comp)) return 1; 7909 #endif 7910 7911 #ifndef STBI_NO_GIF 7912 if (stbi__gif_info(s, x, y, comp)) return 1; 7913 #endif 7914 7915 #ifndef STBI_NO_BMP 7916 if (stbi__bmp_info(s, x, y, comp)) return 1; 7917 #endif 7918 7919 #ifndef STBI_NO_PSD 7920 if (stbi__psd_info(s, x, y, comp)) return 1; 7921 #endif 7922 7923 #ifndef STBI_NO_PIC 7924 if (stbi__pic_info(s, x, y, comp)) return 1; 7925 #endif 7926 7927 #ifndef STBI_NO_PNM 7928 if (stbi__pnm_info(s, x, y, comp)) return 1; 7929 #endif 7930 7931 #ifndef STBI_NO_HDR 7932 if (stbi__hdr_info(s, x, y, comp)) return 1; 7933 #endif 7934 7935 // test tga last because it's a crappy test! 7936 #ifndef STBI_NO_TGA 7937 if (stbi__tga_info(s, x, y, comp)) 7938 return 1; 7939 #endif 7940 return stbi__err("unknown image type", "Image not of any known type, or corrupt"); 7941} 7942 7943static int stbi__is_16_main(stbi__context *s) 7944{ 7945 #ifndef STBI_NO_PNG 7946 if (stbi__png_is16(s)) return 1; 7947 #endif 7948 7949 #ifndef STBI_NO_PSD 7950 if (stbi__psd_is16(s)) return 1; 7951 #endif 7952 7953 #ifndef STBI_NO_PNM 7954 if (stbi__pnm_is16(s)) return 1; 7955 #endif 7956 return 0; 7957} 7958#endif /**/ 7959 7960#ifndef STBI_NO_STDIO 7961STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp) 7962{ 7963 FILE *f = stbi__fopen(filename, "rb"); 7964 int result; 7965 if (!f) return stbi__err("can't fopen", "Unable to open file"); 7966 result = stbi_info_from_file(f, x, y, comp); 7967 fclose(f); 7968 return result; 7969} 7970 7971STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp) 7972{ 7973 int r; 7974 stbi__context s; 7975 long pos = ftell(f); 7976 stbi__start_file(&s, f); 7977 r = stbi__info_main(&s,x,y,comp); 7978 fseek(f,pos,SEEK_SET); 7979 return r; 7980} 7981 7982STBIDEF int stbi_is_16_bit(char const *filename) 7983{ 7984 FILE *f = stbi__fopen(filename, "rb"); 7985 int result; 7986 if (!f) return stbi__err("can't fopen", "Unable to open file"); 7987 result = stbi_is_16_bit_from_file(f); 7988 fclose(f); 7989 return result; 7990} 7991 7992STBIDEF int stbi_is_16_bit_from_file(FILE *f) 7993{ 7994 int r; 7995 stbi__context s; 7996 long pos = ftell(f); 7997 stbi__start_file(&s, f); 7998 r = stbi__is_16_main(&s); 7999 fseek(f,pos,SEEK_SET); 8000 return r; 8001} 8002#endif // !STBI_NO_STDIO 8003 8004#if 0 /* not used in SDL */ 8005STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp) 8006{ 8007 stbi__context s; 8008 stbi__start_mem(&s,buffer,len); 8009 return stbi__info_main(&s,x,y,comp); 8010} 8011 8012STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp) 8013{ 8014 stbi__context s; 8015 stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user); 8016 return stbi__info_main(&s,x,y,comp); 8017} 8018 8019STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len) 8020{ 8021 stbi__context s; 8022 stbi__start_mem(&s,buffer,len); 8023 return stbi__is_16_main(&s); 8024} 8025 8026STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user) 8027{ 8028 stbi__context s; 8029 stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user); 8030 return stbi__is_16_main(&s); 8031} 8032#endif /**/ 8033 8034#endif // STB_IMAGE_IMPLEMENTATION 8035 8036/* 8037 revision history: 8038 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs 8039 2.19 (2018-02-11) fix warning 8040 2.18 (2018-01-30) fix warnings 8041 2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug 8042 1-bit BMP 8043 *_is_16_bit api 8044 avoid warnings 8045 2.16 (2017-07-23) all functions have 16-bit variants; 8046 STBI_NO_STDIO works again; 8047 compilation fixes; 8048 fix rounding in unpremultiply; 8049 optimize vertical flip; 8050 disable raw_len validation; 8051 documentation fixes 8052 2.15 (2017-03-18) fix png-1,2,4 bug; now all Imagenet JPGs decode; 8053 warning fixes; disable run-time SSE detection on gcc; 8054 uniform handling of optional "return" values; 8055 thread-safe initialization of zlib tables 8056 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs 8057 2.13 (2016-11-29) add 16-bit API, only supported for PNG right now 8058 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes 8059 2.11 (2016-04-02) allocate large structures on the stack 8060 remove white matting for transparent PSD 8061 fix reported channel count for PNG & BMP 8062 re-enable SSE2 in non-gcc 64-bit 8063 support RGB-formatted JPEG 8064 read 16-bit PNGs (only as 8-bit) 8065 2.10 (2016-01-22) avoid warning introduced in 2.09 by STBI_REALLOC_SIZED 8066 2.09 (2016-01-16) allow comments in PNM files 8067 16-bit-per-pixel TGA (not bit-per-component) 8068 info() for TGA could break due to .hdr handling 8069 info() for BMP to shares code instead of sloppy parse 8070 can use STBI_REALLOC_SIZED if allocator doesn't support realloc 8071 code cleanup 8072 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA 8073 2.07 (2015-09-13) fix compiler warnings 8074 partial animated GIF support 8075 limited 16-bpc PSD support 8076 #ifdef unused functions 8077 bug with < 92 byte PIC,PNM,HDR,TGA 8078 2.06 (2015-04-19) fix bug where PSD returns wrong '*comp' value 8079 2.05 (2015-04-19) fix bug in progressive JPEG handling, fix warning 8080 2.04 (2015-04-15) try to re-enable SIMD on MinGW 64-bit 8081 2.03 (2015-04-12) extra corruption checking (mmozeiko) 8082 stbi_set_flip_vertically_on_load (nguillemot) 8083 fix NEON support; fix mingw support 8084 2.02 (2015-01-19) fix incorrect assert, fix warning 8085 2.01 (2015-01-17) fix various warnings; suppress SIMD on gcc 32-bit without -msse2 8086 2.00b (2014-12-25) fix STBI_MALLOC in progressive JPEG 8087 2.00 (2014-12-25) optimize JPG, including x86 SSE2 & NEON SIMD (ryg) 8088 progressive JPEG (stb) 8089 PGM/PPM support (Ken Miller) 8090 STBI_MALLOC,STBI_REALLOC,STBI_FREE 8091 GIF bugfix -- seemingly never worked 8092 STBI_NO_*, STBI_ONLY_* 8093 1.48 (2014-12-14) fix incorrectly-named assert() 8094 1.47 (2014-12-14) 1/2/4-bit PNG support, both direct and paletted (Omar Cornut & stb) 8095 optimize PNG (ryg) 8096 fix bug in interlaced PNG with user-specified channel count (stb) 8097 1.46 (2014-08-26) 8098 fix broken tRNS chunk (colorkey-style transparency) in non-paletted PNG 8099 1.45 (2014-08-16) 8100 fix MSVC-ARM internal compiler error by wrapping malloc 8101 1.44 (2014-08-07) 8102 various warning fixes from Ronny Chevalier 8103 1.43 (2014-07-15) 8104 fix MSVC-only compiler problem in code changed in 1.42 8105 1.42 (2014-07-09) 8106 don't define _CRT_SECURE_NO_WARNINGS (affects user code) 8107 fixes to stbi__cleanup_jpeg path 8108 added STBI_ASSERT to avoid requiring assert.h 8109 1.41 (2014-06-25) 8110 fix search&replace from 1.36 that messed up comments/error messages 8111 1.40 (2014-06-22) 8112 fix gcc struct-initialization warning 8113 1.39 (2014-06-15) 8114 fix to TGA optimization when req_comp != number of components in TGA; 8115 fix to GIF loading because BMP wasn't rewinding (whoops, no GIFs in my test suite) 8116 add support for BMP version 5 (more ignored fields) 8117 1.38 (2014-06-06) 8118 suppress MSVC warnings on integer casts truncating values 8119 fix accidental rename of 'skip' field of I/O 8120 1.37 (2014-06-04) 8121 remove duplicate typedef 8122 1.36 (2014-06-03) 8123 convert to header file single-file library 8124 if de-iphone isn't set, load iphone images color-swapped instead of returning NULL 8125 1.35 (2014-05-27) 8126 various warnings 8127 fix broken STBI_SIMD path 8128 fix bug where stbi_load_from_file no longer left file pointer in correct place 8129 fix broken non-easy path for 32-bit BMP (possibly never used) 8130 TGA optimization by Arseny Kapoulkine 8131 1.34 (unknown) 8132 use STBI_NOTUSED in stbi__resample_row_generic(), fix one more leak in tga failure case 8133 1.33 (2011-07-14) 8134 make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements 8135 1.32 (2011-07-13) 8136 support for "info" function for all supported filetypes (SpartanJ) 8137 1.31 (2011-06-20) 8138 a few more leak fixes, bug in PNG handling (SpartanJ) 8139 1.30 (2011-06-11) 8140 added ability to load files via callbacks to accomidate custom input streams (Ben Wenger) 8141 removed deprecated format-specific test/load functions 8142 removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway 8143 error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha) 8144 fix inefficiency in decoding 32-bit BMP (David Woo) 8145 1.29 (2010-08-16) 8146 various warning fixes from Aurelien Pocheville 8147 1.28 (2010-08-01) 8148 fix bug in GIF palette transparency (SpartanJ) 8149 1.27 (2010-08-01) 8150 cast-to-stbi_uc to fix warnings 8151 1.26 (2010-07-24) 8152 fix bug in file buffering for PNG reported by SpartanJ 8153 1.25 (2010-07-17) 8154 refix trans_data warning (Won Chun) 8155 1.24 (2010-07-12) 8156 perf improvements reading from files on platforms with lock-heavy fgetc() 8157 minor perf improvements for jpeg 8158 deprecated type-specific functions so we'll get feedback if they're needed 8159 attempt to fix trans_data warning (Won Chun) 8160 1.23 fixed bug in iPhone support 8161 1.22 (2010-07-10) 8162 removed image *writing* support 8163 stbi_info support from Jetro Lauha 8164 GIF support from Jean-Marc Lienher 8165 iPhone PNG-extensions from James Brown 8166 warning-fixes from Nicolas Schulz and Janez Zemva (i.stbi__err. Janez (U+017D)emva) 8167 1.21 fix use of 'stbi_uc' in header (reported by jon blow) 8168 1.20 added support for Softimage PIC, by Tom Seddon 8169 1.19 bug in interlaced PNG corruption check (found by ryg) 8170 1.18 (2008-08-02) 8171 fix a threading bug (local mutable static) 8172 1.17 support interlaced PNG 8173 1.16 major bugfix - stbi__convert_format converted one too many pixels 8174 1.15 initialize some fields for thread safety 8175 1.14 fix threadsafe conversion bug 8176 header-file-only version (#define STBI_HEADER_FILE_ONLY before including) 8177 1.13 threadsafe 8178 1.12 const qualifiers in the API 8179 1.11 Support installable IDCT, colorspace conversion routines 8180 1.10 Fixes for 64-bit (don't use "unsigned long") 8181 optimized upsampling by Fabian "ryg" Giesen 8182 1.09 Fix format-conversion for PSD code (bad global variables!) 8183 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz 8184 1.07 attempt to fix C++ warning/errors again 8185 1.06 attempt to fix C++ warning/errors again 8186 1.05 fix TGA loading to return correct *comp and use good luminance calc 8187 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free 8188 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR 8189 1.02 support for (subset of) HDR files, float interface for preferred access to them 8190 1.01 fix bug: possible bug in handling right-side up bmps... not sure 8191 fix bug: the stbi__bmp_load() and stbi__tga_load() functions didn't work at all 8192 1.00 interface to zlib that skips zlib header 8193 0.99 correct handling of alpha in palette 8194 0.98 TGA loader by lonesock; dynamically add loaders (untested) 8195 0.97 jpeg errors on too large a file; also catch another malloc failure 8196 0.96 fix detection of invalid v value - particleman@mollyrocket forum 8197 0.95 during header scan, seek to markers in case of padding 8198 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same 8199 0.93 handle jpegtran output; verbose errors 8200 0.92 read 4,8,16,24,32-bit BMP files of several formats 8201 0.91 output 24-bit Windows 3.0 BMP files 8202 0.90 fix a few more warnings; bump version number to approach 1.0 8203 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd 8204 0.60 fix compiling as c++ 8205 0.59 fix warnings: merge Dave Moore's -Wall fixes 8206 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian 8207 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available 8208 0.56 fix bug: zlib uncompressed mode len vs. nlen 8209 0.55 fix bug: restart_interval not initialized to 0 8210 0.54 allow NULL for 'int *comp' 8211 0.53 fix bug in png 3->4; speedup png decoding 8212 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments 8213 0.51 obey req_comp requests, 1-component jpegs return as 1-component, 8214 on 'test' only check type, not whether we support this variant 8215 0.50 (2006-11-19) 8216 first released version 8217*/ 8218 8219 8220/* 8221------------------------------------------------------------------------------ 8222This software is available under 2 licenses -- choose whichever you prefer. 8223------------------------------------------------------------------------------ 8224ALTERNATIVE A - MIT License 8225Copyright (c) 2017 Sean Barrett 8226Permission is hereby granted, free of charge, to any person obtaining a copy of 8227this software and associated documentation files (the "Software"), to deal in 8228the Software without restriction, including without limitation the rights to 8229use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 8230of the Software, and to permit persons to whom the Software is furnished to do 8231so, subject to the following conditions: 8232The above copyright notice and this permission notice shall be included in all 8233copies or substantial portions of the Software. 8234THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 8235IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 8236FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 8237AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 8238LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 8239OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 8240SOFTWARE. 8241------------------------------------------------------------------------------ 8242ALTERNATIVE B - Public Domain (www.unlicense.org) 8243This is free and unencumbered software released into the public domain. 8244Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 8245software, either in source code form or as a compiled binary, for any purpose, 8246commercial or non-commercial, and by any means. 8247In jurisdictions that recognize copyright laws, the author or authors of this 8248software dedicate any and all copyright interest in the software to the public 8249domain. We make this dedication for the benefit of the public at large and to 8250the detriment of our heirs and successors. We intend this dedication to be an 8251overt act of relinquishment in perpetuity of all present and future rights to 8252this software under copyright law. 8253THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 8254IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 8255FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 8256AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 8257ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 8258WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8259------------------------------------------------------------------------------ 8260*/ 8261
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.