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