| 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 | |
| 45 | LICENSE |
| 46 | |
| 47 | See end of file for license information. |
| 48 | |
| 49 | RECENT 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 Dave Moore Roy Eltham |
| 104 | Hayaki Saito Nathan Reed Won Chun |
| 105 | Luke Graham Johan Duparc Nick Verigakis the Horde3D community |
| 106 | Thomas Ruf Ronny Chevalier github:rlyeh |
| 107 | Janez Zemva John Bartholomew Michal Cichon github:romigrou |
| 108 | Jonathan Blow Ken Hamada Tero Hanninen github:svdijk |
| 109 | Eugene Golushkov Laurent Gomila Cort Stratton github:snagar |
| 110 | Aruelien Pocheville Sergio Gonzalez Thibault Reuille github:Zelex |
| 111 | Cass Everitt Ryamond Barbiero github:grim210 |
| 112 | Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw |
| 113 | Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus |
| 114 | Josh Tobin Neil Bickford Matthew Gregan github:poppolopoppo |
| 115 | Julian Raschke Gregory Mullen Christian Floisand github:darealshinji |
| 116 | Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007 |
| 117 | Brad Weinberger Matvey Cherevko github:mosra |
| 118 | Luca Sas Alexander Veselov Zack Middleton [reserved] |
| 119 | Ryan C. Gordon [reserved] [reserved] |
| 120 | DO NOT ADD YOUR NAME HERE |
| 121 | |
| 122 | Jacko Dirks |
| 123 | |
| 124 | To add your name to the credits, pick a random blank space in the middle and fill it. |
| 125 | 80% of merge conflicts on stb PRs are due to people adding their name at the end |
| 126 | of the credits. |
| 127 | */ |
| 128 | |
| 129 | #ifndef STBI_INCLUDE_STB_IMAGE_H |
| 130 | #define STBI_INCLUDE_STB_IMAGE_H |
| 131 | |
| 132 | // DOCUMENTATION |
| 133 | // |
| 134 | // Limitations: |
| 135 | // - no 12-bit-per-channel JPEG |
| 136 | // - no JPEGs with arithmetic coding |
| 137 | // - GIF always returns *comp=4 |
| 138 | // |
| 139 | // Basic usage (see HDR discussion below for HDR usage): |
| 140 | // int x,y,n; |
| 141 | // unsigned char *data = stbi_load(filename, &x, &y, &n, 0); |
| 142 | // // ... process data if not NULL ... |
| 143 | // // ... x = width, y = height, n = # 8-bit components per pixel ... |
| 144 | // // ... replace '0' with '1'..'4' to force that many components per pixel |
| 145 | // // ... but 'n' will always be the number that it would have been if you said 0 |
| 146 | // stbi_image_free(data); |
| 147 | // |
| 148 | // Standard parameters: |
| 149 | // int *x -- outputs image width in pixels |
| 150 | // int *y -- outputs image height in pixels |
| 151 | // int *channels_in_file -- outputs # of image components in image file |
| 152 | // int desired_channels -- if non-zero, # of image components requested in result |
| 153 | // |
| 154 | // The return value from an image loader is an 'unsigned char *' which points |
| 155 | // to the pixel data, or NULL on an allocation failure or if the image is |
| 156 | // corrupt or invalid. The pixel data consists of *y scanlines of *x pixels, |
| 157 | // with each pixel consisting of N interleaved 8-bit components; the first |
| 158 | // pixel pointed to is top-left-most in the image. There is no padding between |
| 159 | // image scanlines or between pixels, regardless of format. The number of |
| 160 | // components N is 'desired_channels' if desired_channels is non-zero, or |
| 161 | // *channels_in_file otherwise. If desired_channels is non-zero, |
| 162 | // *channels_in_file has the number of components that _would_ have been |
| 163 | // output otherwise. E.g. if you set desired_channels to 4, you will always |
| 164 | // get RGBA output, but you can check *channels_in_file to see if it's trivially |
| 165 | // opaque because e.g. there were only 3 channels in the source image. |
| 166 | // |
| 167 | // An output image with N components has the following components interleaved |
| 168 | // in this order in each pixel: |
| 169 | // |
| 170 | // N=#comp components |
| 171 | // 1 grey |
| 172 | // 2 grey, alpha |
| 173 | // 3 red, green, blue |
| 174 | // 4 red, green, blue, alpha |
| 175 | // |
| 176 | // If image loading fails for any reason, the return value will be NULL, |
| 177 | // and *x, *y, *channels_in_file will be unchanged. The function |
| 178 | // stbi_failure_reason() can be queried for an extremely brief, end-user |
| 179 | // unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS |
| 180 | // to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly |
| 181 | // more user-friendly ones. |
| 182 | // |
| 183 | // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized. |
| 184 | // |
| 185 | // To query the width, height and component count of an image without having to |
| 186 | // decode the full file, you can use the stbi_info family of functions: |
| 187 | // |
| 188 | // int x,y,n,ok; |
| 189 | // ok = stbi_info(filename, &x, &y, &n); |
| 190 | // // returns ok=1 and sets x, y, n if image is a supported format, |
| 191 | // // 0 otherwise. |
| 192 | // |
| 193 | // Note that stb_image pervasively uses ints in its public API for sizes, |
| 194 | // including sizes of memory buffers. This is now part of the API and thus |
| 195 | // hard to change without causing breakage. As a result, the various image |
| 196 | // loaders all have certain limits on image size; these differ somewhat |
| 197 | // by format but generally boil down to either just under 2GB or just under |
| 198 | // 1GB. When the decoded image would be larger than this, stb_image decoding |
| 199 | // will fail. |
| 200 | // |
| 201 | // Additionally, stb_image will reject image files that have any of their |
| 202 | // dimensions set to a larger value than the configurable STBI_MAX_DIMENSIONS, |
| 203 | // which defaults to 2**24 = 16777216 pixels. Due to the above memory limit, |
| 204 | // the only way to have an image with such dimensions load correctly |
| 205 | // is for it to have a rather extreme aspect ratio. Either way, the |
| 206 | // assumption here is that such larger images are likely to be malformed |
| 207 | // or malicious. If you do need to load an image with individual dimensions |
| 208 | // larger than that, and it still fits in the overall size limit, you can |
| 209 | // #define STBI_MAX_DIMENSIONS on your own to be something larger. |
| 210 | // |
| 211 | // =========================================================================== |
| 212 | // |
| 213 | // UNICODE: |
| 214 | // |
| 215 | // If compiling for Windows and you wish to use Unicode filenames, compile |
| 216 | // with |
| 217 | // #define STBI_WINDOWS_UTF8 |
| 218 | // and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert |
| 219 | // Windows wchar_t filenames to utf8. |
| 220 | // |
| 221 | // =========================================================================== |
| 222 | // |
| 223 | // Philosophy |
| 224 | // |
| 225 | // stb libraries are designed with the following priorities: |
| 226 | // |
| 227 | // 1. easy to use |
| 228 | // 2. easy to maintain |
| 229 | // 3. good performance |
| 230 | // |
| 231 | // Sometimes I let "good performance" creep up in priority over "easy to maintain", |
| 232 | // and for best performance I may provide less-easy-to-use APIs that give higher |
| 233 | // performance, in addition to the easy-to-use ones. Nevertheless, it's important |
| 234 | // to keep in mind that from the standpoint of you, a client of this library, |
| 235 | // all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all. |
| 236 | // |
| 237 | // Some secondary priorities arise directly from the first two, some of which |
| 238 | // provide more explicit reasons why performance can't be emphasized. |
| 239 | // |
| 240 | // - Portable ("ease of use") |
| 241 | // - Small source code footprint ("easy to maintain") |
| 242 | // - No dependencies ("ease of use") |
| 243 | // |
| 244 | // =========================================================================== |
| 245 | // |
| 246 | // I/O callbacks |
| 247 | // |
| 248 | // I/O callbacks allow you to read from arbitrary sources, like packaged |
| 249 | // files or some other source. Data read from callbacks are processed |
| 250 | // through a small internal buffer (currently 128 bytes) to try to reduce |
| 251 | // overhead. |
| 252 | // |
| 253 | // The three functions you must define are "read" (reads some bytes of data), |
| 254 | // "skip" (skips some bytes of data), "eof" (reports if the stream is at the end). |
| 255 | // |
| 256 | // =========================================================================== |
| 257 | // |
| 258 | // SIMD support |
| 259 | // |
| 260 | // The JPEG decoder will try to automatically use SIMD kernels on x86 when |
| 261 | // supported by the compiler. For ARM Neon support, you must explicitly |
| 262 | // request it. |
| 263 | // |
| 264 | // (The old do-it-yourself SIMD API is no longer supported in the current |
| 265 | // code.) |
| 266 | // |
| 267 | // On x86, SSE2 will automatically be used when available based on a run-time |
| 268 | // test; if not, the generic C versions are used as a fall-back. On ARM targets, |
| 269 | // the typical path is to have separate builds for NEON and non-NEON devices |
| 270 | // (at least this is true for iOS and Android). Therefore, the NEON support is |
| 271 | // toggled by a build flag: define STBI_NEON to get NEON loops. |
| 272 | // |
| 273 | // If for some reason you do not want to use any of SIMD code, or if |
| 274 | // you have issues compiling it, you can disable it entirely by |
| 275 | // defining STBI_NO_SIMD. |
| 276 | // |
| 277 | // =========================================================================== |
| 278 | // |
| 279 | // HDR image support (disable by defining STBI_NO_HDR) |
| 280 | // |
| 281 | // stb_image supports loading HDR images in general, and currently the Radiance |
| 282 | // .HDR file format specifically. You can still load any file through the existing |
| 283 | // interface; if you attempt to load an HDR file, it will be automatically remapped |
| 284 | // to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1; |
| 285 | // both of these constants can be reconfigured through this interface: |
| 286 | // |
| 287 | // stbi_hdr_to_ldr_gamma(2.2f); |
| 288 | // stbi_hdr_to_ldr_scale(1.0f); |
| 289 | // |
| 290 | // (note, do not use _inverse_ constants; stbi_image will invert them |
| 291 | // appropriately). |
| 292 | // |
| 293 | // Additionally, there is a new, parallel interface for loading files as |
| 294 | // (linear) floats to preserve the full dynamic range: |
| 295 | // |
| 296 | // float *data = stbi_loadf(filename, &x, &y, &n, 0); |
| 297 | // |
| 298 | // If you load LDR images through this interface, those images will |
| 299 | // be promoted to floating point values, run through the inverse of |
| 300 | // constants corresponding to the above: |
| 301 | // |
| 302 | // stbi_ldr_to_hdr_scale(1.0f); |
| 303 | // stbi_ldr_to_hdr_gamma(2.2f); |
| 304 | // |
| 305 | // Finally, given a filename (or an open file or memory block--see header |
| 306 | // file for details) containing image data, you can query for the "most |
| 307 | // appropriate" interface to use (that is, whether the image is HDR or |
| 308 | // not), using: |
| 309 | // |
| 310 | // stbi_is_hdr(char *filename); |
| 311 | // |
| 312 | // =========================================================================== |
| 313 | // |
| 314 | // iPhone PNG support: |
| 315 | // |
| 316 | // We optionally support converting iPhone-formatted PNGs (which store |
| 317 | // premultiplied BGRA) back to RGB, even though they're internally encoded |
| 318 | // differently. To enable this conversion, call |
| 319 | // stbi_convert_iphone_png_to_rgb(1). |
| 320 | // |
| 321 | // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per |
| 322 | // pixel to remove any premultiplied alpha *only* if the image file explicitly |
| 323 | // says there's premultiplied data (currently only happens in iPhone images, |
| 324 | // and only if iPhone convert-to-rgb processing is on). |
| 325 | // |
| 326 | // =========================================================================== |
| 327 | // |
| 328 | // ADDITIONAL CONFIGURATION |
| 329 | // |
| 330 | // - You can suppress implementation of any of the decoders to reduce |
| 331 | // your code footprint by #defining one or more of the following |
| 332 | // symbols before creating the implementation. |
| 333 | // |
| 334 | // STBI_NO_JPEG |
| 335 | // STBI_NO_PNG |
| 336 | // STBI_NO_BMP |
| 337 | // STBI_NO_PSD |
| 338 | // STBI_NO_TGA |
| 339 | // STBI_NO_GIF |
| 340 | // STBI_NO_HDR |
| 341 | // STBI_NO_PIC |
| 342 | // STBI_NO_PNM (.ppm and .pgm) |
| 343 | // |
| 344 | // - You can request *only* certain decoders and suppress all other ones |
| 345 | // (this will be more forward-compatible, as addition of new decoders |
| 346 | // doesn't require you to disable them explicitly): |
| 347 | // |
| 348 | // STBI_ONLY_JPEG |
| 349 | // STBI_ONLY_PNG |
| 350 | // STBI_ONLY_BMP |
| 351 | // STBI_ONLY_PSD |
| 352 | // STBI_ONLY_TGA |
| 353 | // STBI_ONLY_GIF |
| 354 | // STBI_ONLY_HDR |
| 355 | // STBI_ONLY_PIC |
| 356 | // STBI_ONLY_PNM (.ppm and .pgm) |
| 357 | // |
| 358 | // - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still |
| 359 | // want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB |
| 360 | // |
| 361 | // - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater |
| 362 | // than that size (in either width or height) without further processing. |
| 363 | // This is to let programs in the wild set an upper bound to prevent |
| 364 | // denial-of-service attacks on untrusted data, as one could generate a |
| 365 | // valid image of gigantic dimensions and force stb_image to allocate a |
| 366 | // huge block of memory and spend disproportionate time decoding it. By |
| 367 | // default this is set to (1 << 24), which is 16777216, but that's still |
| 368 | // very big. |
| 369 | |
| 370 | #ifndef STBI_NO_STDIO |
| 371 | #include <stdio.h> |
| 372 | #endif // STBI_NO_STDIO |
| 373 | |
| 374 | #define STBI_VERSION 1 |
| 375 | |
| 376 | enum |
| 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> |
| 388 | typedef unsigned char stbi_uc; |
| 389 | typedef unsigned short stbi_us; |
| 390 | #else |
| 391 | typedef Uint8 stbi_uc; |
| 392 | typedef Uint16 stbi_us; |
| 393 | #endif |
| 394 | |
| 395 | #ifdef __cplusplus |
| 396 | extern "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 | |
| 416 | typedef 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 | |
| 428 | STBIDEF 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 */ |
| 430 | STBIDEF 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 |
| 434 | STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); |
| 435 | STBIDEF 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 |
| 440 | STBIDEF 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 |
| 444 | STBIDEF 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 */ |
| 455 | STBIDEF 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 | STBIDEF 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); |
| 457 | #endif |
| 458 | |
| 459 | //////////////////////////////////// |
| 460 | // |
| 461 | // 16-bits-per-channel interface |
| 462 | // |
| 463 | |
| 464 | #if 0 /* not used in SDL */ |
| 465 | STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels); |
| 466 | STBIDEF 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 |
| 470 | STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); |
| 471 | STBIDEF 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 |
| 500 | STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user); |
| 501 | STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len); |
| 502 | #endif |
| 503 | #ifndef STBI_NO_STDIO |
| 504 | STBIDEF int stbi_is_hdr (char const *filename); |
| 505 | STBIDEF 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 |
| 512 | STBIDEF const char *stbi_failure_reason (void); |
| 513 | #endif |
| 514 | |
| 515 | // free the loaded image -- this is just free() |
| 516 | STBIDEF 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 |
| 520 | STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); |
| 521 | STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp); |
| 522 | STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len); |
| 523 | STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user); |
| 524 | #endif |
| 525 | |
| 526 | #ifndef STBI_NO_STDIO |
| 527 | STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp); |
| 528 | STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp); |
| 529 | STBIDEF int stbi_is_16_bit (char const *filename); |
| 530 | STBIDEF 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. |
| 539 | STBIDEF 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" |
| 543 | STBIDEF 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 |
| 546 | STBIDEF 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 |
| 553 | STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply); |
| 554 | STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert); |
| 555 | STBIDEF 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 | STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen); |
| 563 | STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header); |
| 564 | STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen); |
| 565 | STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); |
| 566 | |
| 567 | STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen); |
| 568 | STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); |
| 569 | #endif |
| 570 | |
| 571 | |
| 572 | #ifdef __cplusplus |
| 573 | } |
| 574 | #endif |
| 575 | |
| 576 | // |
| 577 | // |
| 578 | //// end header file ///////////////////////////////////////////////////// |
| 579 | #endif // STBI_INCLUDE_STB_IMAGE_H |
| 580 | |
| 581 | #ifdef STB_IMAGE_IMPLEMENTATION |
| 582 | |
| 583 | #if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \ |
| 584 | || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \ |
| 585 | || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \ |
| 586 | || defined(STBI_ONLY_ZLIB) |
| 587 | #ifndef STBI_ONLY_JPEG |
| 588 | #define STBI_NO_JPEG |
| 589 | #endif |
| 590 | #ifndef STBI_ONLY_PNG |
| 591 | #define STBI_NO_PNG |
| 592 | #endif |
| 593 | #ifndef STBI_ONLY_BMP |
| 594 | #define STBI_NO_BMP |
| 595 | #endif |
| 596 | #ifndef STBI_ONLY_PSD |
| 597 | #define STBI_NO_PSD |
| 598 | #endif |
| 599 | #ifndef STBI_ONLY_TGA |
| 600 | #define STBI_NO_TGA |
| 601 | #endif |
| 602 | #ifndef STBI_ONLY_GIF |
| 603 | #define STBI_NO_GIF |
| 604 | #endif |
| 605 | #ifndef STBI_ONLY_HDR |
| 606 | #define STBI_NO_HDR |
| 607 | #endif |
| 608 | #ifndef STBI_ONLY_PIC |
| 609 | #define STBI_NO_PIC |
| 610 | #endif |
| 611 | #ifndef STBI_ONLY_PNM |
| 612 | #define STBI_NO_PNM |
| 613 | #endif |
| 614 | #endif |
| 615 | |
| 616 | #if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB) |
| 617 | #define STBI_NO_ZLIB |
| 618 | #endif |
| 619 | |
| 620 | |
| 621 | #if 0 /* SDL change */ |
| 622 | #include <stdarg.h> |
| 623 | #include <stddef.h> // ptrdiff_t on osx |
| 624 | #include <stdlib.h> |
| 625 | #include <string.h> |
| 626 | #include <limits.h> |
| 627 | |
| 628 | #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) |
| 629 | #include <math.h> // ldexp, pow |
| 630 | #endif |
| 631 | #else /* SDL change */ |
| 632 | #ifndef UINT_MAX |
| 633 | #define UINT_MAX SDL_MAX_UINT32 |
| 634 | #endif |
| 635 | #ifndef INT_MAX |
| 636 | #define INT_MAX SDL_MAX_SINT32 |
| 637 | #endif |
| 638 | #ifndef INT_MIN |
| 639 | #define INT_MIN SDL_MIN_SINT32 |
| 640 | #endif |
| 641 | #ifndef SHRT_MAX |
| 642 | #define SHRT_MAX SDL_MAX_SINT16 |
| 643 | #endif |
| 644 | #ifndef SHRT_MIN |
| 645 | #define SHRT_MIN SDL_MIN_SINT16 |
| 646 | #endif |
| 647 | #endif |
| 648 | |
| 649 | #ifndef STBI_NO_STDIO |
| 650 | #include <stdio.h> |
| 651 | #endif |
| 652 | |
| 653 | #ifndef STBI_ASSERT |
| 654 | #include <assert.h> |
| 655 | #define STBI_ASSERT(x) assert(x) |
| 656 | #endif |
| 657 | |
| 658 | #ifdef __cplusplus |
| 659 | #define STBI_EXTERN extern "C" |
| 660 | #else |
| 661 | #define STBI_EXTERN extern |
| 662 | #endif |
| 663 | |
| 664 | |
| 665 | #ifndef _MSC_VER |
| 666 | #ifdef __cplusplus |
| 667 | #define stbi_inline inline |
| 668 | #else |
| 669 | #define stbi_inline |
| 670 | #endif |
| 671 | #else |
| 672 | #define stbi_inline __forceinline |
| 673 | #endif |
| 674 | |
| 675 | #ifndef STBI_NO_THREAD_LOCALS |
| 676 | #if defined(__cplusplus) && __cplusplus >= 201103L |
| 677 | #define STBI_THREAD_LOCAL thread_local |
| 678 | #elif defined(__GNUC__) && __GNUC__ < 5 |
| 679 | #define STBI_THREAD_LOCAL __thread |
| 680 | #elif defined(_MSC_VER) |
| 681 | #define STBI_THREAD_LOCAL __declspec(thread) |
| 682 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__) |
| 683 | #define STBI_THREAD_LOCAL _Thread_local |
| 684 | #endif |
| 685 | |
| 686 | #ifndef STBI_THREAD_LOCAL |
| 687 | #if defined(__GNUC__) |
| 688 | #define STBI_THREAD_LOCAL __thread |
| 689 | #endif |
| 690 | #endif |
| 691 | #endif |
| 692 | |
| 693 | #if 0 /* SDL change */ |
| 694 | #if defined(_MSC_VER) || defined(__SYMBIAN32__) |
| 695 | typedef unsigned short stbi__uint16; |
| 696 | typedef signed short stbi__int16; |
| 697 | typedef unsigned int stbi__uint32; |
| 698 | typedef signed int stbi__int32; |
| 699 | #else |
| 700 | #include <stdint.h> |
| 701 | typedef uint16_t stbi__uint16; |
| 702 | typedef int16_t stbi__int16; |
| 703 | typedef uint32_t stbi__uint32; |
| 704 | typedef int32_t stbi__int32; |
| 705 | #endif |
| 706 | #else |
| 707 | typedef Uint16 stbi__uint16; |
| 708 | typedef Sint16 stbi__int16; |
| 709 | typedef Uint32 stbi__uint32; |
| 710 | typedef Sint32 stbi__int32; |
| 711 | #endif |
| 712 | |
| 713 | #ifndef STBI_BUFFER_SIZE |
| 714 | #define STBI_BUFFER_SIZE 128 |
| 715 | #endif |
| 716 | |
| 717 | // should produce compiler error if size is wrong |
| 718 | typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1]; |
| 719 | |
| 720 | #ifdef _MSC_VER |
| 721 | #define STBI_NOTUSED(v) (void)(v) |
| 722 | #else |
| 723 | #define STBI_NOTUSED(v) (void)sizeof(v) |
| 724 | #endif |
| 725 | |
| 726 | #if 0 /* SDL change: */ |
| 727 | #ifdef _MSC_VER |
| 728 | #define STBI_HAS_LROTL |
| 729 | #endif |
| 730 | #endif |
| 731 | |
| 732 | #ifdef STBI_HAS_LROTL |
| 733 | #define stbi_lrot(x,y) _lrotl(x,y) |
| 734 | #else |
| 735 | #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (-(y) & 31))) |
| 736 | #endif |
| 737 | |
| 738 | #if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED)) |
| 739 | // ok |
| 740 | #elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED) |
| 741 | // ok |
| 742 | #else |
| 743 | #error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)." |
| 744 | #endif |
| 745 | |
| 746 | #ifndef STBI_MALLOC |
| 747 | #define STBI_MALLOC(sz) malloc(sz) |
| 748 | #define STBI_REALLOC(p,newsz) realloc(p,newsz) |
| 749 | #define STBI_FREE(p) free(p) |
| 750 | #endif |
| 751 | |
| 752 | #ifndef STBI_REALLOC_SIZED |
| 753 | #define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz) |
| 754 | #endif |
| 755 | |
| 756 | // x86/x64 detection |
| 757 | #if defined(__x86_64__) || defined(_M_X64) |
| 758 | #define STBI__X64_TARGET |
| 759 | #elif defined(__i386) || defined(_M_IX86) |
| 760 | #define STBI__X86_TARGET |
| 761 | #endif |
| 762 | |
| 763 | #if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD) |
| 764 | // gcc doesn't support sse2 intrinsics unless you compile with -msse2, |
| 765 | // which in turn means it gets to use SSE2 everywhere. This is unfortunate, |
| 766 | // but previous attempts to provide the SSE2 functions with runtime |
| 767 | // detection caused numerous issues. The way architecture extensions are |
| 768 | // exposed in GCC/Clang is, sadly, not really suited for one-file libs. |
| 769 | // New behavior: if compiled with -msse2, we use SSE2 without any |
| 770 | // detection; if not, we don't use it at all. |
| 771 | #define STBI_NO_SIMD |
| 772 | #endif |
| 773 | |
| 774 | #if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD) |
| 775 | // Note that __MINGW32__ doesn't actually mean 32-bit, so we have to avoid STBI__X64_TARGET |
| 776 | // |
| 777 | // 32-bit MinGW wants ESP to be 16-byte aligned, but this is not in the |
| 778 | // Windows ABI and VC++ as well as Windows DLLs don't maintain that invariant. |
| 779 | // As a result, enabling SSE2 on 32-bit MinGW is dangerous when not |
| 780 | // simultaneously enabling "-mstackrealign". |
| 781 | // |
| 782 | // See https://github.com/nothings/stb/issues/81 for more information. |
| 783 | // |
| 784 | // So default to no SSE2 on 32-bit MinGW. If you've read this far and added |
| 785 | // -mstackrealign to your build settings, feel free to #define STBI_MINGW_ENABLE_SSE2. |
| 786 | #define STBI_NO_SIMD |
| 787 | #endif |
| 788 | |
| 789 | #if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET)) |
| 790 | #define STBI_SSE2 |
| 791 | #include <emmintrin.h> |
| 792 | |
| 793 | #ifdef _MSC_VER |
| 794 | |
| 795 | #if _MSC_VER >= 1400 // not VC6 |
| 796 | #include <intrin.h> // __cpuid |
| 797 | static int stbi__cpuid3(void) |
| 798 | { |
| 799 | int info[4]; |
| 800 | __cpuid(info,1); |
| 801 | return info[3]; |
| 802 | } |
| 803 | #else |
| 804 | static int stbi__cpuid3(void) |
| 805 | { |
| 806 | int res; |
| 807 | __asm { |
| 808 | mov eax,1 |
| 809 | cpuid |
| 810 | mov res,edx |
| 811 | } |
| 812 | return res; |
| 813 | } |
| 814 | #endif |
| 815 | |
| 816 | #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name |
| 817 | |
| 818 | #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) |
| 819 | static int stbi__sse2_available(void) |
| 820 | { |
| 821 | int info3 = stbi__cpuid3(); |
| 822 | return ((info3 >> 26) & 1) != 0; |
| 823 | } |
| 824 | #endif |
| 825 | |
| 826 | #else // assume GCC-style if not VC++ |
| 827 | #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16))) |
| 828 | |
| 829 | #if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) |
| 830 | static int stbi__sse2_available(void) |
| 831 | { |
| 832 | // If we're even attempting to compile this on GCC/Clang, that means |
| 833 | // -msse2 is on, which means the compiler is allowed to use SSE2 |
| 834 | // instructions at will, and so are we. |
| 835 | return 1; |
| 836 | } |
| 837 | #endif |
| 838 | |
| 839 | #endif |
| 840 | #endif |
| 841 | |
| 842 | // ARM NEON |
| 843 | #if defined(STBI_NO_SIMD) && defined(STBI_NEON) |
| 844 | #undef STBI_NEON |
| 845 | #endif |
| 846 | |
| 847 | #ifdef STBI_NEON |
| 848 | #include <arm_neon.h> |
| 849 | #ifdef _MSC_VER |
| 850 | #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name |
| 851 | #else |
| 852 | #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16))) |
| 853 | #endif |
| 854 | #endif |
| 855 | |
| 856 | #ifndef STBI_SIMD_ALIGN |
| 857 | #define STBI_SIMD_ALIGN(type, name) type name |
| 858 | #endif |
| 859 | |
| 860 | #ifndef STBI_MAX_DIMENSIONS |
| 861 | #define STBI_MAX_DIMENSIONS (1 << 24) |
| 862 | #endif |
| 863 | |
| 864 | /////////////////////////////////////////////// |
| 865 | // |
| 866 | // stbi__context struct and start_xxx functions |
| 867 | |
| 868 | // stbi__context structure is our basic context used by all images, so it |
| 869 | // contains all the IO context, plus some basic image information |
| 870 | typedef struct |
| 871 | { |
| 872 | stbi__uint32 img_x, img_y; |
| 873 | int img_n, img_out_n; |
| 874 | |
| 875 | stbi_io_callbacks io; |
| 876 | void *io_user_data; |
| 877 | |
| 878 | int read_from_callbacks; |
| 879 | int buflen; |
| 880 | stbi_uc buffer_start[128]; |
| 881 | int callback_already_read; |
| 882 | |
| 883 | stbi_uc *img_buffer, *img_buffer_end; |
| 884 | stbi_uc *img_buffer_original, *img_buffer_original_end; |
| 885 | } stbi__context; |
| 886 | |
| 887 | |
| 888 | static void stbi__refill_buffer(stbi__context *s); |
| 889 | |
| 890 | // initialize a memory-decode context |
| 891 | static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len) |
| 892 | { |
| 893 | s->io.read = NULL; |
| 894 | s->read_from_callbacks = 0; |
| 895 | s->callback_already_read = 0; |
| 896 | s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer; |
| 897 | s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len; |
| 898 | } |
| 899 | |
| 900 | #if 0 /* not used in SDL */ |
| 901 | // initialize a callback-based context |
| 902 | static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user) |
| 903 | { |
| 904 | s->io = *c; |
| 905 | s->io_user_data = user; |
| 906 | s->buflen = sizeof(s->buffer_start); |
| 907 | s->read_from_callbacks = 1; |
| 908 | s->callback_already_read = 0; |
| 909 | s->img_buffer = s->img_buffer_original = s->buffer_start; |
| 910 | stbi__refill_buffer(s); |
| 911 | s->img_buffer_original_end = s->img_buffer_end; |
| 912 | } |
| 913 | #endif |
| 914 | |
| 915 | #ifndef STBI_NO_STDIO |
| 916 | |
| 917 | static int stbi__stdio_read(void *user, char *data, int size) |
| 918 | { |
| 919 | return (int) fread(data,1,size,(FILE*) user); |
| 920 | } |
| 921 | |
| 922 | static void stbi__stdio_skip(void *user, int n) |
| 923 | { |
| 924 | int ch; |
| 925 | fseek((FILE*) user, n, SEEK_CUR); |
| 926 | ch = fgetc((FILE*) user); /* have to read a byte to reset feof()'s flag */ |
| 927 | if (ch != EOF) { |
| 928 | ungetc(ch, (FILE *) user); /* push byte back onto stream if valid. */ |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | static int stbi__stdio_eof(void *user) |
| 933 | { |
| 934 | return feof((FILE*) user) || ferror((FILE *) user); |
| 935 | } |
| 936 | |
| 937 | static stbi_io_callbacks stbi__stdio_callbacks = |
| 938 | { |
| 939 | stbi__stdio_read, |
| 940 | stbi__stdio_skip, |
| 941 | stbi__stdio_eof, |
| 942 | }; |
| 943 | |
| 944 | static void stbi__start_file(stbi__context *s, FILE *f) |
| 945 | { |
| 946 | stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f); |
| 947 | } |
| 948 | |
| 949 | //static void stop_file(stbi__context *s) { } |
| 950 | |
| 951 | #endif // !STBI_NO_STDIO |
| 952 | |
| 953 | static void stbi__rewind(stbi__context *s) |
| 954 | { |
| 955 | // conceptually rewind SHOULD rewind to the beginning of the stream, |
| 956 | // but we just rewind to the beginning of the initial buffer, because |
| 957 | // we only use it after doing 'test', which only ever looks at at most 92 bytes |
| 958 | s->img_buffer = s->img_buffer_original; |
| 959 | s->img_buffer_end = s->img_buffer_original_end; |
| 960 | } |
| 961 | |
| 962 | enum |
| 963 | { |
| 964 | STBI_ORDER_RGB, |
| 965 | STBI_ORDER_BGR |
| 966 | }; |
| 967 | |
| 968 | typedef struct |
| 969 | { |
| 970 | int w; |
| 971 | int h; |
| 972 | int pitch; |
| 973 | stbi_uc *y; |
| 974 | stbi_uc *uv; |
| 975 | } stbi__nv12; |
| 976 | |
| 977 | typedef struct |
| 978 | { |
| 979 | int bits_per_channel; |
| 980 | int num_channels; |
| 981 | int channel_order; |
| 982 | } stbi__result_info; |
| 983 | |
| 984 | #ifndef STBI_NO_JPEG |
| 985 | static int stbi__jpeg_test(stbi__context *s); |
| 986 | static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__nv12 *nv12, stbi__result_info *ri); |
| 987 | #if 0 /* not used in SDL */ |
| 988 | static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp); |
| 989 | #endif |
| 990 | #endif |
| 991 | |
| 992 | #ifndef STBI_NO_PNG |
| 993 | static int stbi__png_test(stbi__context *s); |
| 994 | static 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); |
| 995 | #if 0 /* not used in SDL */ |
| 996 | static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp); |
| 997 | static int stbi__png_is16(stbi__context *s); |
| 998 | #endif |
| 999 | #endif |
| 1000 | |
| 1001 | #ifndef STBI_NO_BMP |
| 1002 | static int stbi__bmp_test(stbi__context *s); |
| 1003 | static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); |
| 1004 | static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp); |
| 1005 | #endif |
| 1006 | |
| 1007 | #ifndef STBI_NO_TGA |
| 1008 | static int stbi__tga_test(stbi__context *s); |
| 1009 | static 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); |
| 1010 | static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp); |
| 1011 | #endif |
| 1012 | |
| 1013 | #ifndef STBI_NO_PSD |
| 1014 | static int stbi__psd_test(stbi__context *s); |
| 1015 | static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc); |
| 1016 | static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp); |
| 1017 | static int stbi__psd_is16(stbi__context *s); |
| 1018 | #endif |
| 1019 | |
| 1020 | #ifndef STBI_NO_HDR |
| 1021 | static int stbi__hdr_test(stbi__context *s); |
| 1022 | static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); |
| 1023 | static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp); |
| 1024 | #endif |
| 1025 | |
| 1026 | #ifndef STBI_NO_PIC |
| 1027 | static int stbi__pic_test(stbi__context *s); |
| 1028 | static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); |
| 1029 | static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp); |
| 1030 | #endif |
| 1031 | |
| 1032 | #ifndef STBI_NO_GIF |
| 1033 | static int stbi__gif_test(stbi__context *s); |
| 1034 | static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); |
| 1035 | static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp); |
| 1036 | static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp); |
| 1037 | #endif |
| 1038 | |
| 1039 | #ifndef STBI_NO_PNM |
| 1040 | static int stbi__pnm_test(stbi__context *s); |
| 1041 | static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); |
| 1042 | static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp); |
| 1043 | static int stbi__pnm_is16(stbi__context *s); |
| 1044 | #endif |
| 1045 | |
| 1046 | #ifndef STBI_NO_FAILURE_STRINGS |
| 1047 | #if 1 /* SDL change: */ |
| 1048 | static int stbi__err(const char *str) |
| 1049 | { |
| 1050 | SDL_SetError("%s" , str); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | #else /* SDL change. */ |
| 1054 | static |
| 1055 | #ifdef STBI_THREAD_LOCAL |
| 1056 | STBI_THREAD_LOCAL |
| 1057 | #endif |
| 1058 | const char *stbi__g_failure_reason; |
| 1059 | |
| 1060 | STBIDEF const char *stbi_failure_reason(void) |
| 1061 | { |
| 1062 | return stbi__g_failure_reason; |
| 1063 | } |
| 1064 | |
| 1065 | static int stbi__err(const char *str) |
| 1066 | { |
| 1067 | stbi__g_failure_reason = str; |
| 1068 | return 0; |
| 1069 | } |
| 1070 | #endif /**/ |
| 1071 | #endif |
| 1072 | |
| 1073 | static void *stbi__malloc(size_t size) |
| 1074 | { |
| 1075 | return STBI_MALLOC(size); |
| 1076 | } |
| 1077 | |
| 1078 | // stb_image uses ints pervasively, including for offset calculations. |
| 1079 | // therefore the largest decoded image size we can support with the |
| 1080 | // current code, even on 64-bit targets, is INT_MAX. this is not a |
| 1081 | // significant limitation for the intended use case. |
| 1082 | // |
| 1083 | // we do, however, need to make sure our size calculations don't |
| 1084 | // overflow. hence a few helper functions for size calculations that |
| 1085 | // multiply integers together, making sure that they're non-negative |
| 1086 | // and no overflow occurs. |
| 1087 | |
| 1088 | // return 1 if the sum is valid, 0 on overflow. |
| 1089 | // negative terms are considered invalid. |
| 1090 | static int stbi__addsizes_valid(int a, int b) |
| 1091 | { |
| 1092 | if (b < 0) return 0; |
| 1093 | // now 0 <= b <= INT_MAX, hence also |
| 1094 | // 0 <= INT_MAX - b <= INTMAX. |
| 1095 | // And "a + b <= INT_MAX" (which might overflow) is the |
| 1096 | // same as a <= INT_MAX - b (no overflow) |
| 1097 | return a <= INT_MAX - b; |
| 1098 | } |
| 1099 | |
| 1100 | // returns 1 if the product is valid, 0 on overflow. |
| 1101 | // negative factors are considered invalid. |
| 1102 | static int stbi__mul2sizes_valid(int a, int b) |
| 1103 | { |
| 1104 | if (a < 0 || b < 0) return 0; |
| 1105 | if (b == 0) return 1; // mul-by-0 is always safe |
| 1106 | // portable way to check for no overflows in a*b |
| 1107 | return a <= INT_MAX/b; |
| 1108 | } |
| 1109 | |
| 1110 | #if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) |
| 1111 | // returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow |
| 1112 | static int stbi__mad2sizes_valid(int a, int b, int add) |
| 1113 | { |
| 1114 | return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add); |
| 1115 | } |
| 1116 | #endif |
| 1117 | |
| 1118 | // returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow |
| 1119 | static int stbi__mad3sizes_valid(int a, int b, int c, int add) |
| 1120 | { |
| 1121 | return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && |
| 1122 | stbi__addsizes_valid(a*b*c, add); |
| 1123 | } |
| 1124 | |
| 1125 | // returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow |
| 1126 | #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) |
| 1127 | static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add) |
| 1128 | { |
| 1129 | return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && |
| 1130 | stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add); |
| 1131 | } |
| 1132 | #endif |
| 1133 | |
| 1134 | #if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) |
| 1135 | // mallocs with size overflow checking |
| 1136 | static void *stbi__malloc_mad2(int a, int b, int add) |
| 1137 | { |
| 1138 | if (!stbi__mad2sizes_valid(a, b, add)) return NULL; |
| 1139 | return stbi__malloc(a*b + add); |
| 1140 | } |
| 1141 | #endif |
| 1142 | |
| 1143 | static void *stbi__malloc_mad3(int a, int b, int c, int add) |
| 1144 | { |
| 1145 | if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL; |
| 1146 | return stbi__malloc(a*b*c + add); |
| 1147 | } |
| 1148 | |
| 1149 | #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) |
| 1150 | static void *stbi__malloc_mad4(int a, int b, int c, int d, int add) |
| 1151 | { |
| 1152 | if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL; |
| 1153 | return stbi__malloc(a*b*c*d + add); |
| 1154 | } |
| 1155 | #endif |
| 1156 | |
| 1157 | // returns 1 if the sum of two signed ints is valid (between -2^31 and 2^31-1 inclusive), 0 on overflow. |
| 1158 | static int stbi__addints_valid(int a, int b) |
| 1159 | { |
| 1160 | if ((a >= 0) != (b >= 0)) return 1; // a and b have different signs, so no overflow |
| 1161 | if (a < 0 && b < 0) return a >= INT_MIN - b; // same as a + b >= INT_MIN; INT_MIN - b cannot overflow since b < 0. |
| 1162 | return a <= INT_MAX - b; |
| 1163 | } |
| 1164 | |
| 1165 | // returns 1 if the product of two ints fits in a signed short, 0 on overflow. |
| 1166 | static int stbi__mul2shorts_valid(int a, int b) |
| 1167 | { |
| 1168 | if (b == 0 || b == -1) return 1; // multiplication by 0 is always 0; check for -1 so SHRT_MIN/b doesn't overflow |
| 1169 | if ((a >= 0) == (b >= 0)) return a <= SHRT_MAX/b; // product is positive, so similar to mul2sizes_valid |
| 1170 | if (b < 0) return a <= SHRT_MIN / b; // same as a * b >= SHRT_MIN |
| 1171 | return a >= SHRT_MIN / b; |
| 1172 | } |
| 1173 | |
| 1174 | // stbi__err - error |
| 1175 | // stbi__errpf - error returning pointer to float |
| 1176 | // stbi__errpuc - error returning pointer to unsigned char |
| 1177 | |
| 1178 | #ifdef STBI_NO_FAILURE_STRINGS |
| 1179 | #define stbi__err(x,y) 0 |
| 1180 | #elif defined(STBI_FAILURE_USERMSG) |
| 1181 | #define stbi__err(x,y) stbi__err(y) |
| 1182 | #else |
| 1183 | #define stbi__err(x,y) stbi__err(x) |
| 1184 | #endif |
| 1185 | |
| 1186 | #define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL)) |
| 1187 | #define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL)) |
| 1188 | |
| 1189 | STBIDEF void stbi_image_free(void *retval_from_stbi_load) |
| 1190 | { |
| 1191 | STBI_FREE(retval_from_stbi_load); |
| 1192 | } |
| 1193 | |
| 1194 | #ifndef STBI_NO_LINEAR |
| 1195 | static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp); |
| 1196 | #endif |
| 1197 | |
| 1198 | #ifndef STBI_NO_HDR |
| 1199 | static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp); |
| 1200 | #endif |
| 1201 | |
| 1202 | static int stbi__vertically_flip_on_load_global = 0; |
| 1203 | |
| 1204 | #ifndef STBI_NO_PNG |
| 1205 | #if 0 /* not used in SDL */ |
| 1206 | STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip) |
| 1207 | { |
| 1208 | stbi__vertically_flip_on_load_global = flag_true_if_should_flip; |
| 1209 | } |
| 1210 | #endif /**/ |
| 1211 | #endif |
| 1212 | |
| 1213 | #ifndef STBI_THREAD_LOCAL |
| 1214 | #define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global |
| 1215 | #else |
| 1216 | static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set; |
| 1217 | |
| 1218 | #ifndef STBI_NO_PNG |
| 1219 | STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip) |
| 1220 | { |
| 1221 | stbi__vertically_flip_on_load_local = flag_true_if_should_flip; |
| 1222 | stbi__vertically_flip_on_load_set = 1; |
| 1223 | } |
| 1224 | #endif |
| 1225 | |
| 1226 | #define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \ |
| 1227 | ? stbi__vertically_flip_on_load_local \ |
| 1228 | : stbi__vertically_flip_on_load_global) |
| 1229 | #endif // STBI_THREAD_LOCAL |
| 1230 | |
| 1231 | static 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) |
| 1232 | { |
| 1233 | memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields |
| 1234 | ri->bits_per_channel = 8; // default is 8 so most paths don't have to be changed |
| 1235 | ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order |
| 1236 | ri->num_channels = 0; |
| 1237 | |
| 1238 | // test the formats with a very explicit header first (at least a FOURCC |
| 1239 | // or distinctive magic number first) |
| 1240 | #ifndef STBI_NO_PNG |
| 1241 | if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, palette_buffer, palette_buffer_len, ri); |
| 1242 | #endif |
| 1243 | #ifndef STBI_NO_BMP |
| 1244 | if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp, ri); |
| 1245 | #endif |
| 1246 | #ifndef STBI_NO_GIF |
| 1247 | if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp, ri); |
| 1248 | #endif |
| 1249 | #ifndef STBI_NO_PSD |
| 1250 | if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc); |
| 1251 | #else |
| 1252 | STBI_NOTUSED(bpc); |
| 1253 | #endif |
| 1254 | #ifndef STBI_NO_PIC |
| 1255 | if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri); |
| 1256 | #endif |
| 1257 | |
| 1258 | // then the formats that can end up attempting to load with just 1 or 2 |
| 1259 | // bytes matching expectations; these are prone to false positives, so |
| 1260 | // try them later |
| 1261 | #ifndef STBI_NO_JPEG |
| 1262 | if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp,NULL, ri); |
| 1263 | #endif |
| 1264 | #ifndef STBI_NO_PNM |
| 1265 | if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp, ri); |
| 1266 | #endif |
| 1267 | |
| 1268 | #ifndef STBI_NO_HDR |
| 1269 | if (stbi__hdr_test(s)) { |
| 1270 | float *hdr = stbi__hdr_load(s, x,y,comp,req_comp, ri); |
| 1271 | return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp); |
| 1272 | } |
| 1273 | #endif |
| 1274 | |
| 1275 | #ifndef STBI_NO_TGA |
| 1276 | // test tga last because it's a crappy test! |
| 1277 | if (stbi__tga_test(s)) |
| 1278 | return stbi__tga_load(s,x,y,comp,req_comp, palette_buffer, palette_buffer_len, ri); |
| 1279 | #endif |
| 1280 | |
| 1281 | return stbi__errpuc("unknown image type" , "Image not of any known type, or corrupt" ); |
| 1282 | } |
| 1283 | |
| 1284 | static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels) |
| 1285 | { |
| 1286 | int i; |
| 1287 | int img_len = w * h * channels; |
| 1288 | stbi_uc *reduced; |
| 1289 | |
| 1290 | reduced = (stbi_uc *) stbi__malloc(img_len); |
| 1291 | if (reduced == NULL) return stbi__errpuc("outofmem" , "Out of memory" ); |
| 1292 | |
| 1293 | for (i = 0; i < img_len; ++i) |
| 1294 | reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling |
| 1295 | |
| 1296 | STBI_FREE(orig); |
| 1297 | return reduced; |
| 1298 | } |
| 1299 | |
| 1300 | #if 0 /* not used in SDL */ |
| 1301 | static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels) |
| 1302 | { |
| 1303 | int i; |
| 1304 | int img_len = w * h * channels; |
| 1305 | stbi__uint16 *enlarged; |
| 1306 | |
| 1307 | enlarged = (stbi__uint16 *) stbi__malloc(img_len*2); |
| 1308 | if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem" , "Out of memory" ); |
| 1309 | |
| 1310 | for (i = 0; i < img_len; ++i) |
| 1311 | enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff |
| 1312 | |
| 1313 | STBI_FREE(orig); |
| 1314 | return enlarged; |
| 1315 | } |
| 1316 | #endif |
| 1317 | |
| 1318 | static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel) |
| 1319 | { |
| 1320 | int row; |
| 1321 | size_t bytes_per_row = (size_t)w * bytes_per_pixel; |
| 1322 | stbi_uc temp[2048]; |
| 1323 | stbi_uc *bytes = (stbi_uc *)image; |
| 1324 | |
| 1325 | for (row = 0; row < (h>>1); row++) { |
| 1326 | stbi_uc *row0 = bytes + row*bytes_per_row; |
| 1327 | stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row; |
| 1328 | // swap row0 with row1 |
| 1329 | size_t bytes_left = bytes_per_row; |
| 1330 | while (bytes_left) { |
| 1331 | size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp); |
| 1332 | memcpy(temp, row0, bytes_copy); |
| 1333 | memcpy(row0, row1, bytes_copy); |
| 1334 | memcpy(row1, temp, bytes_copy); |
| 1335 | row0 += bytes_copy; |
| 1336 | row1 += bytes_copy; |
| 1337 | bytes_left -= bytes_copy; |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | #ifndef STBI_NO_GIF |
| 1343 | static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel) |
| 1344 | { |
| 1345 | int slice; |
| 1346 | int slice_size = w * h * bytes_per_pixel; |
| 1347 | |
| 1348 | stbi_uc *bytes = (stbi_uc *)image; |
| 1349 | for (slice = 0; slice < z; ++slice) { |
| 1350 | stbi__vertical_flip(bytes, w, h, bytes_per_pixel); |
| 1351 | bytes += slice_size; |
| 1352 | } |
| 1353 | } |
| 1354 | #endif |
| 1355 | |
| 1356 | #if 0 /* not used in SDL */ |
| 1357 | static unsigned char *stbi__load_indexed(stbi__context *s, int *x, int *y, unsigned int *palette_buffer, int palette_buffer_len) |
| 1358 | { |
| 1359 | stbi__result_info ri; |
| 1360 | int comp; |
| 1361 | void *result; |
| 1362 | |
| 1363 | if (!palette_buffer) |
| 1364 | return NULL; |
| 1365 | |
| 1366 | result = stbi__load_main(s, x, y, &comp, 1, &ri, 8, palette_buffer, palette_buffer_len); |
| 1367 | if (result == NULL) |
| 1368 | return NULL; |
| 1369 | |
| 1370 | if (comp != 1) { |
| 1371 | stbi_image_free(result); |
| 1372 | return NULL; |
| 1373 | } |
| 1374 | |
| 1375 | if (ri.bits_per_channel != 8) { |
| 1376 | stbi_image_free(result); |
| 1377 | return NULL; |
| 1378 | } |
| 1379 | |
| 1380 | // @TODO: move stbi__convert_format to here |
| 1381 | |
| 1382 | if (stbi__vertically_flip_on_load) { |
| 1383 | int channels = 1; |
| 1384 | stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc)); |
| 1385 | } |
| 1386 | |
| 1387 | return (unsigned char *) result; |
| 1388 | } |
| 1389 | #endif /**/ |
| 1390 | |
| 1391 | static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) |
| 1392 | { |
| 1393 | stbi__result_info ri; |
| 1394 | void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8, NULL, 0); |
| 1395 | |
| 1396 | if (result == NULL) |
| 1397 | return NULL; |
| 1398 | |
| 1399 | // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. |
| 1400 | STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); |
| 1401 | |
| 1402 | if (ri.bits_per_channel != 8) { |
| 1403 | result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp); |
| 1404 | ri.bits_per_channel = 8; |
| 1405 | } |
| 1406 | |
| 1407 | // @TODO: move stbi__convert_format to here |
| 1408 | |
| 1409 | if (stbi__vertically_flip_on_load) { |
| 1410 | int channels = req_comp ? req_comp : *comp; |
| 1411 | stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc)); |
| 1412 | } |
| 1413 | |
| 1414 | return (unsigned char *) result; |
| 1415 | } |
| 1416 | |
| 1417 | #if 0 /* not used in SDL */ |
| 1418 | static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) |
| 1419 | { |
| 1420 | stbi__result_info ri; |
| 1421 | void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16, NULL, 0); |
| 1422 | |
| 1423 | if (result == NULL) |
| 1424 | return NULL; |
| 1425 | |
| 1426 | // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. |
| 1427 | STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); |
| 1428 | |
| 1429 | if (ri.bits_per_channel != 16) { |
| 1430 | result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp); |
| 1431 | ri.bits_per_channel = 16; |
| 1432 | } |
| 1433 | |
| 1434 | // @TODO: move stbi__convert_format16 to here |
| 1435 | // @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision |
| 1436 | |
| 1437 | if (stbi__vertically_flip_on_load) { |
| 1438 | int channels = req_comp ? req_comp : *comp; |
| 1439 | stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16)); |
| 1440 | } |
| 1441 | |
| 1442 | return (stbi__uint16 *) result; |
| 1443 | } |
| 1444 | #endif /**/ |
| 1445 | |
| 1446 | #if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR) |
| 1447 | static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp) |
| 1448 | { |
| 1449 | if (stbi__vertically_flip_on_load && result != NULL) { |
| 1450 | int channels = req_comp ? req_comp : *comp; |
| 1451 | stbi__vertical_flip(result, *x, *y, channels * sizeof(float)); |
| 1452 | } |
| 1453 | } |
| 1454 | #endif |
| 1455 | |
| 1456 | #ifndef STBI_NO_STDIO |
| 1457 | |
| 1458 | #if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) |
| 1459 | STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide); |
| 1460 | STBI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default); |
| 1461 | #endif |
| 1462 | |
| 1463 | #if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) |
| 1464 | STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input) |
| 1465 | { |
| 1466 | return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL); |
| 1467 | } |
| 1468 | #endif |
| 1469 | |
| 1470 | static FILE *stbi__fopen(char const *filename, char const *mode) |
| 1471 | { |
| 1472 | FILE *f; |
| 1473 | #if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) |
| 1474 | wchar_t wMode[64]; |
| 1475 | wchar_t wFilename[1024]; |
| 1476 | if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename))) |
| 1477 | return 0; |
| 1478 | |
| 1479 | if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode))) |
| 1480 | return 0; |
| 1481 | |
| 1482 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 1483 | if (0 != _wfopen_s(&f, wFilename, wMode)) |
| 1484 | f = 0; |
| 1485 | #else |
| 1486 | f = _wfopen(wFilename, wMode); |
| 1487 | #endif |
| 1488 | |
| 1489 | #elif defined(_MSC_VER) && _MSC_VER >= 1400 |
| 1490 | if (0 != fopen_s(&f, filename, mode)) |
| 1491 | f=0; |
| 1492 | #else |
| 1493 | f = fopen(filename, mode); |
| 1494 | #endif |
| 1495 | return f; |
| 1496 | } |
| 1497 | |
| 1498 | |
| 1499 | STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp) |
| 1500 | { |
| 1501 | FILE *f = stbi__fopen(filename, "rb" ); |
| 1502 | unsigned char *result; |
| 1503 | if (!f) return stbi__errpuc("can't fopen" , "Unable to open file" ); |
| 1504 | result = stbi_load_from_file(f,x,y,comp,req_comp); |
| 1505 | fclose(f); |
| 1506 | return result; |
| 1507 | } |
| 1508 | |
| 1509 | STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) |
| 1510 | { |
| 1511 | unsigned char *result; |
| 1512 | stbi__context s; |
| 1513 | stbi__start_file(&s,f); |
| 1514 | result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); |
| 1515 | if (result) { |
| 1516 | // need to 'unget' all the characters in the IO buffer |
| 1517 | fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR); |
| 1518 | } |
| 1519 | return result; |
| 1520 | } |
| 1521 | |
| 1522 | STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp, int req_comp) |
| 1523 | { |
| 1524 | stbi__uint16 *result; |
| 1525 | stbi__context s; |
| 1526 | stbi__start_file(&s,f); |
| 1527 | result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp); |
| 1528 | if (result) { |
| 1529 | // need to 'unget' all the characters in the IO buffer |
| 1530 | fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR); |
| 1531 | } |
| 1532 | return result; |
| 1533 | } |
| 1534 | |
| 1535 | STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *comp, int req_comp) |
| 1536 | { |
| 1537 | FILE *f = stbi__fopen(filename, "rb" ); |
| 1538 | stbi__uint16 *result; |
| 1539 | if (!f) return (stbi_us *) stbi__errpuc("can't fopen" , "Unable to open file" ); |
| 1540 | result = stbi_load_from_file_16(f,x,y,comp,req_comp); |
| 1541 | fclose(f); |
| 1542 | return result; |
| 1543 | } |
| 1544 | |
| 1545 | |
| 1546 | #endif //!STBI_NO_STDIO |
| 1547 | |
| 1548 | #if 0 /* not used in SDL */ |
| 1549 | STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels) |
| 1550 | { |
| 1551 | stbi__context s; |
| 1552 | stbi__start_mem(&s,buffer,len); |
| 1553 | return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); |
| 1554 | } |
| 1555 | |
| 1556 | STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels) |
| 1557 | { |
| 1558 | stbi__context s; |
| 1559 | stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user); |
| 1560 | return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); |
| 1561 | } |
| 1562 | #endif /**/ |
| 1563 | |
| 1564 | STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) |
| 1565 | { |
| 1566 | stbi__context s; |
| 1567 | stbi__start_mem(&s,buffer,len); |
| 1568 | return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); |
| 1569 | } |
| 1570 | |
| 1571 | #if 0 /* not used in SDL */ |
| 1572 | STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp) |
| 1573 | { |
| 1574 | stbi__context s; |
| 1575 | stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); |
| 1576 | return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); |
| 1577 | } |
| 1578 | |
| 1579 | STBIDEF stbi_uc *stbi_load_from_memory_with_palette(stbi_uc const *buffer, int len, int *x, int *y, unsigned int *palette_buffer, int palette_buffer_len) |
| 1580 | { |
| 1581 | stbi__context s; |
| 1582 | stbi__start_mem(&s, buffer, len); |
| 1583 | return stbi__load_indexed(&s, x, y, palette_buffer, palette_buffer_len); |
| 1584 | } |
| 1585 | |
| 1586 | STBIDEF 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) |
| 1587 | { |
| 1588 | stbi__context s; |
| 1589 | stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user); |
| 1590 | return stbi__load_indexed(&s, x, y, palette_buffer, palette_buffer_len); |
| 1591 | } |
| 1592 | #endif |
| 1593 | |
| 1594 | #ifndef STBI_NO_GIF |
| 1595 | STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp) |
| 1596 | { |
| 1597 | unsigned char *result; |
| 1598 | stbi__context s; |
| 1599 | stbi__start_mem(&s,buffer,len); |
| 1600 | |
| 1601 | result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp); |
| 1602 | if (stbi__vertically_flip_on_load) { |
| 1603 | stbi__vertical_flip_slices( result, *x, *y, *z, *comp ); |
| 1604 | } |
| 1605 | |
| 1606 | return result; |
| 1607 | } |
| 1608 | #endif |
| 1609 | |
| 1610 | #ifndef STBI_NO_LINEAR |
| 1611 | static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp) |
| 1612 | { |
| 1613 | unsigned char *data; |
| 1614 | #ifndef STBI_NO_HDR |
| 1615 | if (stbi__hdr_test(s)) { |
| 1616 | stbi__result_info ri; |
| 1617 | float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp, &ri); |
| 1618 | if (hdr_data) |
| 1619 | stbi__float_postprocess(hdr_data,x,y,comp,req_comp); |
| 1620 | return hdr_data; |
| 1621 | } |
| 1622 | #endif |
| 1623 | data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp); |
| 1624 | if (data) |
| 1625 | return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp); |
| 1626 | return stbi__errpf("unknown image type" , "Image not of any known type, or corrupt" ); |
| 1627 | } |
| 1628 | |
| 1629 | STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) |
| 1630 | { |
| 1631 | stbi__context s; |
| 1632 | stbi__start_mem(&s,buffer,len); |
| 1633 | return stbi__loadf_main(&s,x,y,comp,req_comp); |
| 1634 | } |
| 1635 | |
| 1636 | STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp) |
| 1637 | { |
| 1638 | stbi__context s; |
| 1639 | stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); |
| 1640 | return stbi__loadf_main(&s,x,y,comp,req_comp); |
| 1641 | } |
| 1642 | |
| 1643 | #ifndef STBI_NO_STDIO |
| 1644 | STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp) |
| 1645 | { |
| 1646 | float *result; |
| 1647 | FILE *f = stbi__fopen(filename, "rb" ); |
| 1648 | if (!f) return stbi__errpf("can't fopen" , "Unable to open file" ); |
| 1649 | result = stbi_loadf_from_file(f,x,y,comp,req_comp); |
| 1650 | fclose(f); |
| 1651 | return result; |
| 1652 | } |
| 1653 | |
| 1654 | STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) |
| 1655 | { |
| 1656 | stbi__context s; |
| 1657 | stbi__start_file(&s,f); |
| 1658 | return stbi__loadf_main(&s,x,y,comp,req_comp); |
| 1659 | } |
| 1660 | #endif // !STBI_NO_STDIO |
| 1661 | |
| 1662 | #endif // !STBI_NO_LINEAR |
| 1663 | |
| 1664 | #if 0 /* not used in SDL */ |
| 1665 | // these is-hdr-or-not is defined independent of whether STBI_NO_LINEAR is |
| 1666 | // defined, for API simplicity; if STBI_NO_LINEAR is defined, it always |
| 1667 | // reports false! |
| 1668 | |
| 1669 | STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len) |
| 1670 | { |
| 1671 | #ifndef STBI_NO_HDR |
| 1672 | stbi__context s; |
| 1673 | stbi__start_mem(&s,buffer,len); |
| 1674 | return stbi__hdr_test(&s); |
| 1675 | #else |
| 1676 | STBI_NOTUSED(buffer); |
| 1677 | STBI_NOTUSED(len); |
| 1678 | return 0; |
| 1679 | #endif |
| 1680 | } |
| 1681 | #endif |
| 1682 | |
| 1683 | #ifndef STBI_NO_STDIO |
| 1684 | STBIDEF int stbi_is_hdr (char const *filename) |
| 1685 | { |
| 1686 | FILE *f = stbi__fopen(filename, "rb" ); |
| 1687 | int result=0; |
| 1688 | if (f) { |
| 1689 | result = stbi_is_hdr_from_file(f); |
| 1690 | fclose(f); |
| 1691 | } |
| 1692 | return result; |
| 1693 | } |
| 1694 | |
| 1695 | STBIDEF int stbi_is_hdr_from_file(FILE *f) |
| 1696 | { |
| 1697 | #ifndef STBI_NO_HDR |
| 1698 | long pos = ftell(f); |
| 1699 | int res; |
| 1700 | stbi__context s; |
| 1701 | stbi__start_file(&s,f); |
| 1702 | res = stbi__hdr_test(&s); |
| 1703 | fseek(f, pos, SEEK_SET); |
| 1704 | return res; |
| 1705 | #else |
| 1706 | STBI_NOTUSED(f); |
| 1707 | return 0; |
| 1708 | #endif |
| 1709 | } |
| 1710 | #endif // !STBI_NO_STDIO |
| 1711 | |
| 1712 | #if 0 /* not used in SDL */ |
| 1713 | STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user) |
| 1714 | { |
| 1715 | #ifndef STBI_NO_HDR |
| 1716 | stbi__context s; |
| 1717 | stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); |
| 1718 | return stbi__hdr_test(&s); |
| 1719 | #else |
| 1720 | STBI_NOTUSED(clbk); |
| 1721 | STBI_NOTUSED(user); |
| 1722 | return 0; |
| 1723 | #endif |
| 1724 | } |
| 1725 | #endif |
| 1726 | |
| 1727 | #ifndef STBI_NO_LINEAR |
| 1728 | static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f; |
| 1729 | |
| 1730 | STBIDEF void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; } |
| 1731 | STBIDEF void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; } |
| 1732 | #endif |
| 1733 | |
| 1734 | #ifndef STBI_NO_HDR |
| 1735 | static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f; |
| 1736 | |
| 1737 | STBIDEF void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; } |
| 1738 | STBIDEF void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; } |
| 1739 | #endif |
| 1740 | |
| 1741 | |
| 1742 | ////////////////////////////////////////////////////////////////////////////// |
| 1743 | // |
| 1744 | // Common code used by all image loaders |
| 1745 | // |
| 1746 | |
| 1747 | enum |
| 1748 | { |
| 1749 | STBI__SCAN_load=0, |
| 1750 | STBI__SCAN_type, |
| 1751 | |
| 1752 | }; |
| 1753 | |
| 1754 | static void stbi__refill_buffer(stbi__context *s) |
| 1755 | { |
| 1756 | int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen); |
| 1757 | s->callback_already_read += (int) (s->img_buffer - s->img_buffer_original); |
| 1758 | if (n == 0) { |
| 1759 | // at end of file, treat same as if from memory, but need to handle case |
| 1760 | // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file |
| 1761 | s->read_from_callbacks = 0; |
| 1762 | s->img_buffer = s->buffer_start; |
| 1763 | s->img_buffer_end = s->buffer_start+1; |
| 1764 | *s->img_buffer = 0; |
| 1765 | } else { |
| 1766 | s->img_buffer = s->buffer_start; |
| 1767 | s->img_buffer_end = s->buffer_start + n; |
| 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | stbi_inline static stbi_uc stbi__get8(stbi__context *s) |
| 1772 | { |
| 1773 | if (s->img_buffer < s->img_buffer_end) |
| 1774 | return *s->img_buffer++; |
| 1775 | if (s->read_from_callbacks) { |
| 1776 | stbi__refill_buffer(s); |
| 1777 | return *s->img_buffer++; |
| 1778 | } |
| 1779 | return 0; |
| 1780 | } |
| 1781 | |
| 1782 | #if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) |
| 1783 | // nothing |
| 1784 | #else |
| 1785 | stbi_inline static int stbi__at_eof(stbi__context *s) |
| 1786 | { |
| 1787 | if (s->io.read) { |
| 1788 | if (!(s->io.eof)(s->io_user_data)) return 0; |
| 1789 | // if feof() is true, check if buffer = end |
| 1790 | // special case: we've only got the special 0 character at the end |
| 1791 | if (s->read_from_callbacks == 0) return 1; |
| 1792 | } |
| 1793 | |
| 1794 | return s->img_buffer >= s->img_buffer_end; |
| 1795 | } |
| 1796 | #endif |
| 1797 | |
| 1798 | #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) |
| 1799 | // nothing |
| 1800 | #else |
| 1801 | static void stbi__skip(stbi__context *s, int n) |
| 1802 | { |
| 1803 | if (n == 0) return; // already there! |
| 1804 | if (n < 0) { |
| 1805 | s->img_buffer = s->img_buffer_end; |
| 1806 | return; |
| 1807 | } |
| 1808 | if (s->io.read) { |
| 1809 | int blen = (int) (s->img_buffer_end - s->img_buffer); |
| 1810 | if (blen < n) { |
| 1811 | s->img_buffer = s->img_buffer_end; |
| 1812 | (s->io.skip)(s->io_user_data, n - blen); |
| 1813 | return; |
| 1814 | } |
| 1815 | } |
| 1816 | s->img_buffer += n; |
| 1817 | } |
| 1818 | #endif |
| 1819 | |
| 1820 | #if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM) |
| 1821 | // nothing |
| 1822 | #else |
| 1823 | static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n) |
| 1824 | { |
| 1825 | if (s->io.read) { |
| 1826 | int blen = (int) (s->img_buffer_end - s->img_buffer); |
| 1827 | if (blen < n) { |
| 1828 | int res, count; |
| 1829 | |
| 1830 | memcpy(buffer, s->img_buffer, blen); |
| 1831 | |
| 1832 | count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen); |
| 1833 | res = (count == (n-blen)); |
| 1834 | s->img_buffer = s->img_buffer_end; |
| 1835 | return res; |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | if (s->img_buffer+n <= s->img_buffer_end) { |
| 1840 | memcpy(buffer, s->img_buffer, n); |
| 1841 | s->img_buffer += n; |
| 1842 | return 1; |
| 1843 | } else |
| 1844 | return 0; |
| 1845 | } |
| 1846 | #endif |
| 1847 | |
| 1848 | #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) |
| 1849 | // nothing |
| 1850 | #else |
| 1851 | static int stbi__get16be(stbi__context *s) |
| 1852 | { |
| 1853 | int z = stbi__get8(s); |
| 1854 | return (z << 8) + stbi__get8(s); |
| 1855 | } |
| 1856 | #endif |
| 1857 | |
| 1858 | #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) |
| 1859 | // nothing |
| 1860 | #else |
| 1861 | static stbi__uint32 stbi__get32be(stbi__context *s) |
| 1862 | { |
| 1863 | stbi__uint32 z = stbi__get16be(s); |
| 1864 | return (z << 16) + stbi__get16be(s); |
| 1865 | } |
| 1866 | #endif |
| 1867 | |
| 1868 | #if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) |
| 1869 | // nothing |
| 1870 | #else |
| 1871 | static int stbi__get16le(stbi__context *s) |
| 1872 | { |
| 1873 | int z = stbi__get8(s); |
| 1874 | return z + (stbi__get8(s) << 8); |
| 1875 | } |
| 1876 | #endif |
| 1877 | |
| 1878 | #ifndef STBI_NO_BMP |
| 1879 | static stbi__uint32 stbi__get32le(stbi__context *s) |
| 1880 | { |
| 1881 | stbi__uint32 z = stbi__get16le(s); |
| 1882 | z += (stbi__uint32)stbi__get16le(s) << 16; |
| 1883 | return z; |
| 1884 | } |
| 1885 | #endif |
| 1886 | |
| 1887 | #define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings |
| 1888 | |
| 1889 | #if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) |
| 1890 | // nothing |
| 1891 | #else |
| 1892 | ////////////////////////////////////////////////////////////////////////////// |
| 1893 | // |
| 1894 | // generic converter from built-in img_n to req_comp |
| 1895 | // individual types do this automatically as much as possible (e.g. jpeg |
| 1896 | // does all cases internally since it needs to colorspace convert anyway, |
| 1897 | // and it never has alpha, so very few cases ). png can automatically |
| 1898 | // interleave an alpha=255 channel, but falls back to this for other cases |
| 1899 | // |
| 1900 | // assume data buffer is malloced, so malloc a new one and free that one |
| 1901 | // only failure mode is malloc failing |
| 1902 | |
| 1903 | static stbi_uc stbi__compute_y(int r, int g, int b) |
| 1904 | { |
| 1905 | return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8); |
| 1906 | } |
| 1907 | #endif |
| 1908 | |
| 1909 | #if defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) |
| 1910 | // nothing |
| 1911 | #else |
| 1912 | static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y) |
| 1913 | { |
| 1914 | int i,j; |
| 1915 | unsigned char *good; |
| 1916 | |
| 1917 | if (req_comp == img_n) return data; |
| 1918 | STBI_ASSERT(req_comp >= 1 && req_comp <= 4); |
| 1919 | |
| 1920 | good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0); |
| 1921 | if (good == NULL) { |
| 1922 | STBI_FREE(data); |
| 1923 | return stbi__errpuc("outofmem" , "Out of memory" ); |
| 1924 | } |
| 1925 | |
| 1926 | for (j=0; j < (int) y; ++j) { |
| 1927 | unsigned char *src = data + j * x * img_n ; |
| 1928 | unsigned char *dest = good + j * x * req_comp; |
| 1929 | |
| 1930 | #define STBI__COMBO(a,b) ((a)*8+(b)) |
| 1931 | #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) |
| 1932 | // convert source image with img_n components to one with req_comp components; |
| 1933 | // avoid switch per pixel, so use switch per scanline and massive macros |
| 1934 | switch (STBI__COMBO(img_n, req_comp)) { |
| 1935 | STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break; |
| 1936 | STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; |
| 1937 | STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break; |
| 1938 | STBI__CASE(2,1) { dest[0]=src[0]; } break; |
| 1939 | STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; |
| 1940 | STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; |
| 1941 | STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break; |
| 1942 | STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; |
| 1943 | STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break; |
| 1944 | STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; |
| 1945 | STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break; |
| 1946 | STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; |
| 1947 | default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return stbi__errpuc("unsupported" , "Unsupported format conversion" ); |
| 1948 | } |
| 1949 | #undef STBI__CASE |
| 1950 | } |
| 1951 | |
| 1952 | STBI_FREE(data); |
| 1953 | return good; |
| 1954 | } |
| 1955 | #endif |
| 1956 | |
| 1957 | #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) |
| 1958 | // nothing |
| 1959 | #else |
| 1960 | static stbi__uint16 stbi__compute_y_16(int r, int g, int b) |
| 1961 | { |
| 1962 | return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8); |
| 1963 | } |
| 1964 | #endif |
| 1965 | |
| 1966 | #if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) |
| 1967 | // nothing |
| 1968 | #else |
| 1969 | static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y) |
| 1970 | { |
| 1971 | int i,j; |
| 1972 | stbi__uint16 *good; |
| 1973 | |
| 1974 | if (req_comp == img_n) return data; |
| 1975 | STBI_ASSERT(req_comp >= 1 && req_comp <= 4); |
| 1976 | |
| 1977 | good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2); |
| 1978 | if (good == NULL) { |
| 1979 | STBI_FREE(data); |
| 1980 | return (stbi__uint16 *) stbi__errpuc("outofmem" , "Out of memory" ); |
| 1981 | } |
| 1982 | |
| 1983 | for (j=0; j < (int) y; ++j) { |
| 1984 | stbi__uint16 *src = data + j * x * img_n ; |
| 1985 | stbi__uint16 *dest = good + j * x * req_comp; |
| 1986 | |
| 1987 | #define STBI__COMBO(a,b) ((a)*8+(b)) |
| 1988 | #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) |
| 1989 | // convert source image with img_n components to one with req_comp components; |
| 1990 | // avoid switch per pixel, so use switch per scanline and massive macros |
| 1991 | switch (STBI__COMBO(img_n, req_comp)) { |
| 1992 | STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break; |
| 1993 | STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; |
| 1994 | STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break; |
| 1995 | STBI__CASE(2,1) { dest[0]=src[0]; } break; |
| 1996 | STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; |
| 1997 | STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; |
| 1998 | STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break; |
| 1999 | STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; |
| 2000 | STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break; |
| 2001 | STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; |
| 2002 | STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break; |
| 2003 | STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; |
| 2004 | default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return (stbi__uint16*) stbi__errpuc("unsupported" , "Unsupported format conversion" ); |
| 2005 | } |
| 2006 | #undef STBI__CASE |
| 2007 | } |
| 2008 | |
| 2009 | STBI_FREE(data); |
| 2010 | return good; |
| 2011 | } |
| 2012 | #endif |
| 2013 | |
| 2014 | #ifndef STBI_NO_LINEAR |
| 2015 | static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp) |
| 2016 | { |
| 2017 | int i,k,n; |
| 2018 | float *output; |
| 2019 | if (!data) return NULL; |
| 2020 | output = (float *) stbi__malloc_mad4(x, y, comp, sizeof(float), 0); |
| 2021 | if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem" , "Out of memory" ); } |
| 2022 | // compute number of non-alpha components |
| 2023 | if (comp & 1) n = comp; else n = comp-1; |
| 2024 | for (i=0; i < x*y; ++i) { |
| 2025 | for (k=0; k < n; ++k) { |
| 2026 | output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale); |
| 2027 | } |
| 2028 | } |
| 2029 | if (n < comp) { |
| 2030 | for (i=0; i < x*y; ++i) { |
| 2031 | output[i*comp + n] = data[i*comp + n]/255.0f; |
| 2032 | } |
| 2033 | } |
| 2034 | STBI_FREE(data); |
| 2035 | return output; |
| 2036 | } |
| 2037 | #endif |
| 2038 | |
| 2039 | #ifndef STBI_NO_HDR |
| 2040 | #define stbi__float2int(x) ((int) (x)) |
| 2041 | static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp) |
| 2042 | { |
| 2043 | int i,k,n; |
| 2044 | stbi_uc *output; |
| 2045 | if (!data) return NULL; |
| 2046 | output = (stbi_uc *) stbi__malloc_mad3(x, y, comp, 0); |
| 2047 | if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem" , "Out of memory" ); } |
| 2048 | // compute number of non-alpha components |
| 2049 | if (comp & 1) n = comp; else n = comp-1; |
| 2050 | for (i=0; i < x*y; ++i) { |
| 2051 | for (k=0; k < n; ++k) { |
| 2052 | float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f; |
| 2053 | if (z < 0) z = 0; |
| 2054 | if (z > 255) z = 255; |
| 2055 | output[i*comp + k] = (stbi_uc) stbi__float2int(z); |
| 2056 | } |
| 2057 | if (k < comp) { |
| 2058 | float z = data[i*comp+k] * 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 | } |
| 2064 | STBI_FREE(data); |
| 2065 | return output; |
| 2066 | } |
| 2067 | #endif |
| 2068 | |
| 2069 | ////////////////////////////////////////////////////////////////////////////// |
| 2070 | // |
| 2071 | // "baseline" JPEG/JFIF decoder |
| 2072 | // |
| 2073 | // simple implementation |
| 2074 | // - doesn't support delayed output of y-dimension |
| 2075 | // - simple interface (only one output format: 8-bit interleaved RGB) |
| 2076 | // - doesn't try to recover corrupt jpegs |
| 2077 | // - doesn't allow partial loading, loading multiple at once |
| 2078 | // - still fast on x86 (copying globals into locals doesn't help x86) |
| 2079 | // - allocates lots of intermediate memory (full size of all components) |
| 2080 | // - non-interleaved case requires this anyway |
| 2081 | // - allows good upsampling (see next) |
| 2082 | // high-quality |
| 2083 | // - upsampled channels are bilinearly interpolated, even across blocks |
| 2084 | // - quality integer IDCT derived from IJG's 'slow' |
| 2085 | // performance |
| 2086 | // - fast huffman; reasonable integer IDCT |
| 2087 | // - some SIMD kernels for common paths on targets with SSE2/NEON |
| 2088 | // - uses a lot of intermediate memory, could cache poorly |
| 2089 | |
| 2090 | #ifndef STBI_NO_JPEG |
| 2091 | |
| 2092 | // huffman decoding acceleration |
| 2093 | #define FAST_BITS 9 // larger handles more cases; smaller stomps less cache |
| 2094 | |
| 2095 | typedef struct |
| 2096 | { |
| 2097 | stbi_uc fast[1 << FAST_BITS]; |
| 2098 | // weirdly, repacking this into AoS is a 10% speed loss, instead of a win |
| 2099 | stbi__uint16 code[256]; |
| 2100 | stbi_uc values[256]; |
| 2101 | stbi_uc size[257]; |
| 2102 | unsigned int maxcode[18]; |
| 2103 | int delta[17]; // old 'firstsymbol' - old 'firstcode' |
| 2104 | } stbi__huffman; |
| 2105 | |
| 2106 | typedef struct |
| 2107 | { |
| 2108 | stbi__context *s; |
| 2109 | stbi__huffman huff_dc[4]; |
| 2110 | stbi__huffman huff_ac[4]; |
| 2111 | stbi__uint16 dequant[4][64]; |
| 2112 | stbi__int16 fast_ac[4][1 << FAST_BITS]; |
| 2113 | |
| 2114 | // sizes for components, interleaved MCUs |
| 2115 | int img_h_max, img_v_max; |
| 2116 | int img_mcu_x, img_mcu_y; |
| 2117 | int img_mcu_w, img_mcu_h; |
| 2118 | |
| 2119 | // definition of jpeg image component |
| 2120 | struct |
| 2121 | { |
| 2122 | int id; |
| 2123 | int h,v; |
| 2124 | int tq; |
| 2125 | int hd,ha; |
| 2126 | int dc_pred; |
| 2127 | |
| 2128 | int x,y,w2,h2; |
| 2129 | stbi_uc *data; |
| 2130 | void *raw_data, *raw_coeff; |
| 2131 | stbi_uc *linebuf; |
| 2132 | short *coeff; // progressive only |
| 2133 | int coeff_w, coeff_h; // number of 8x8 coefficient blocks |
| 2134 | } img_comp[4]; |
| 2135 | |
| 2136 | stbi__uint32 code_buffer; // jpeg entropy-coded buffer |
| 2137 | int code_bits; // number of valid bits |
| 2138 | unsigned char marker; // marker seen while filling entropy buffer |
| 2139 | int nomore; // flag if we saw a marker so must stop |
| 2140 | |
| 2141 | int progressive; |
| 2142 | int spec_start; |
| 2143 | int spec_end; |
| 2144 | int succ_high; |
| 2145 | int succ_low; |
| 2146 | int eob_run; |
| 2147 | int jfif; |
| 2148 | int app14_color_transform; // Adobe APP14 tag |
| 2149 | int rgb; |
| 2150 | |
| 2151 | int scan_n, order[4]; |
| 2152 | int restart_interval, todo; |
| 2153 | |
| 2154 | // kernels |
| 2155 | void (*idct_block_kernel)(stbi_uc *out, int out_stride, short data[64]); |
| 2156 | void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step); |
| 2157 | stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs); |
| 2158 | } stbi__jpeg; |
| 2159 | |
| 2160 | static int stbi__build_huffman(stbi__huffman *h, int *count) |
| 2161 | { |
| 2162 | int i,j,k=0; |
| 2163 | unsigned int code; |
| 2164 | // build size list for each symbol (from JPEG spec) |
| 2165 | for (i=0; i < 16; ++i) { |
| 2166 | for (j=0; j < count[i]; ++j) { |
| 2167 | h->size[k++] = (stbi_uc) (i+1); |
| 2168 | if(k >= 257) return stbi__err("bad size list" ,"Corrupt JPEG" ); |
| 2169 | } |
| 2170 | } |
| 2171 | h->size[k] = 0; |
| 2172 | |
| 2173 | // compute actual symbols (from jpeg spec) |
| 2174 | code = 0; |
| 2175 | k = 0; |
| 2176 | for(j=1; j <= 16; ++j) { |
| 2177 | // compute delta to add to code to compute symbol id |
| 2178 | h->delta[j] = k - code; |
| 2179 | if (h->size[k] == j) { |
| 2180 | while (h->size[k] == j) |
| 2181 | h->code[k++] = (stbi__uint16) (code++); |
| 2182 | if (code-1 >= (1u << j)) return stbi__err("bad code lengths" ,"Corrupt JPEG" ); |
| 2183 | } |
| 2184 | // compute largest code + 1 for this size, preshifted as needed later |
| 2185 | h->maxcode[j] = code << (16-j); |
| 2186 | code <<= 1; |
| 2187 | } |
| 2188 | h->maxcode[j] = 0xffffffff; |
| 2189 | |
| 2190 | // build non-spec acceleration table; 255 is flag for not-accelerated |
| 2191 | memset(h->fast, 255, 1 << FAST_BITS); |
| 2192 | for (i=0; i < k; ++i) { |
| 2193 | int s = h->size[i]; |
| 2194 | if (s <= FAST_BITS) { |
| 2195 | int c = h->code[i] << (FAST_BITS-s); |
| 2196 | int m = 1 << (FAST_BITS-s); |
| 2197 | for (j=0; j < m; ++j) { |
| 2198 | h->fast[c+j] = (stbi_uc) i; |
| 2199 | } |
| 2200 | } |
| 2201 | } |
| 2202 | return 1; |
| 2203 | } |
| 2204 | |
| 2205 | // build a table that decodes both magnitude and value of small ACs in |
| 2206 | // one go. |
| 2207 | static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h) |
| 2208 | { |
| 2209 | int i; |
| 2210 | for (i=0; i < (1 << FAST_BITS); ++i) { |
| 2211 | stbi_uc fast = h->fast[i]; |
| 2212 | fast_ac[i] = 0; |
| 2213 | if (fast < 255) { |
| 2214 | int rs = h->values[fast]; |
| 2215 | int run = (rs >> 4) & 15; |
| 2216 | int magbits = rs & 15; |
| 2217 | int len = h->size[fast]; |
| 2218 | |
| 2219 | if (magbits && len + magbits <= FAST_BITS) { |
| 2220 | // magnitude code followed by receive_extend code |
| 2221 | int k = ((i << len) & ((1 << FAST_BITS) - 1)) >> (FAST_BITS - magbits); |
| 2222 | int m = 1 << (magbits - 1); |
| 2223 | if (k < m) k += (~0U << magbits) + 1; |
| 2224 | // if the result is small enough, we can fit it in fast_ac table |
| 2225 | if (k >= -128 && k <= 127) |
| 2226 | fast_ac[i] = (stbi__int16) ((k * 256) + (run * 16) + (len + magbits)); |
| 2227 | } |
| 2228 | } |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | static void stbi__grow_buffer_unsafe(stbi__jpeg *j) |
| 2233 | { |
| 2234 | do { |
| 2235 | unsigned int b = j->nomore ? 0 : stbi__get8(j->s); |
| 2236 | if (b == 0xff) { |
| 2237 | int c = stbi__get8(j->s); |
| 2238 | while (c == 0xff) c = stbi__get8(j->s); // consume fill bytes |
| 2239 | if (c != 0) { |
| 2240 | j->marker = (unsigned char) c; |
| 2241 | j->nomore = 1; |
| 2242 | return; |
| 2243 | } |
| 2244 | } |
| 2245 | j->code_buffer |= b << (24 - j->code_bits); |
| 2246 | j->code_bits += 8; |
| 2247 | } while (j->code_bits <= 24); |
| 2248 | } |
| 2249 | |
| 2250 | // (1 << n) - 1 |
| 2251 | static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535}; |
| 2252 | |
| 2253 | // decode a jpeg huffman value from the bitstream |
| 2254 | stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h) |
| 2255 | { |
| 2256 | unsigned int temp; |
| 2257 | int c,k; |
| 2258 | |
| 2259 | if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); |
| 2260 | |
| 2261 | // look at the top FAST_BITS and determine what symbol ID it is, |
| 2262 | // if the code is <= FAST_BITS |
| 2263 | c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); |
| 2264 | k = h->fast[c]; |
| 2265 | if (k < 255) { |
| 2266 | int s = h->size[k]; |
| 2267 | if (s > j->code_bits) |
| 2268 | return -1; |
| 2269 | j->code_buffer <<= s; |
| 2270 | j->code_bits -= s; |
| 2271 | return h->values[k]; |
| 2272 | } |
| 2273 | |
| 2274 | // naive test is to shift the code_buffer down so k bits are |
| 2275 | // valid, then test against maxcode. To speed this up, we've |
| 2276 | // preshifted maxcode left so that it has (16-k) 0s at the |
| 2277 | // end; in other words, regardless of the number of bits, it |
| 2278 | // wants to be compared against something shifted to have 16; |
| 2279 | // that way we don't need to shift inside the loop. |
| 2280 | temp = j->code_buffer >> 16; |
| 2281 | for (k=FAST_BITS+1 ; ; ++k) |
| 2282 | if (temp < h->maxcode[k]) |
| 2283 | break; |
| 2284 | if (k == 17) { |
| 2285 | // error! code not found |
| 2286 | j->code_bits -= 16; |
| 2287 | return -1; |
| 2288 | } |
| 2289 | |
| 2290 | if (k > j->code_bits) |
| 2291 | return -1; |
| 2292 | |
| 2293 | // convert the huffman code to the symbol id |
| 2294 | c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k]; |
| 2295 | if(c < 0 || c >= 256) // symbol id out of bounds! |
| 2296 | return -1; |
| 2297 | STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]); |
| 2298 | |
| 2299 | // convert the id to a symbol |
| 2300 | j->code_bits -= k; |
| 2301 | j->code_buffer <<= k; |
| 2302 | return h->values[c]; |
| 2303 | } |
| 2304 | |
| 2305 | // bias[n] = (-1<<n) + 1 |
| 2306 | static const int stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767}; |
| 2307 | |
| 2308 | // combined JPEG 'receive' and JPEG 'extend', since baseline |
| 2309 | // always extends everything it receives. |
| 2310 | stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n) |
| 2311 | { |
| 2312 | unsigned int k; |
| 2313 | int sgn; |
| 2314 | if (j->code_bits < n) stbi__grow_buffer_unsafe(j); |
| 2315 | if (j->code_bits < n) return 0; // ran out of bits from stream, return 0s intead of continuing |
| 2316 | |
| 2317 | sgn = j->code_buffer >> 31; // sign bit always in MSB; 0 if MSB clear (positive), 1 if MSB set (negative) |
| 2318 | k = stbi_lrot(j->code_buffer, n); |
| 2319 | j->code_buffer = k & ~stbi__bmask[n]; |
| 2320 | k &= stbi__bmask[n]; |
| 2321 | j->code_bits -= n; |
| 2322 | return k + (stbi__jbias[n] & (sgn - 1)); |
| 2323 | } |
| 2324 | |
| 2325 | // get some unsigned bits |
| 2326 | stbi_inline static int stbi__jpeg_get_bits(stbi__jpeg *j, int n) |
| 2327 | { |
| 2328 | unsigned int k; |
| 2329 | if (j->code_bits < n) stbi__grow_buffer_unsafe(j); |
| 2330 | if (j->code_bits < n) return 0; // ran out of bits from stream, return 0s intead of continuing |
| 2331 | k = stbi_lrot(j->code_buffer, n); |
| 2332 | j->code_buffer = k & ~stbi__bmask[n]; |
| 2333 | k &= stbi__bmask[n]; |
| 2334 | j->code_bits -= n; |
| 2335 | return k; |
| 2336 | } |
| 2337 | |
| 2338 | stbi_inline static int stbi__jpeg_get_bit(stbi__jpeg *j) |
| 2339 | { |
| 2340 | unsigned int k; |
| 2341 | if (j->code_bits < 1) stbi__grow_buffer_unsafe(j); |
| 2342 | if (j->code_bits < 1) return 0; // ran out of bits from stream, return 0s intead of continuing |
| 2343 | k = j->code_buffer; |
| 2344 | j->code_buffer <<= 1; |
| 2345 | --j->code_bits; |
| 2346 | return k & 0x80000000; |
| 2347 | } |
| 2348 | |
| 2349 | // given a value that's at position X in the zigzag stream, |
| 2350 | // where does it appear in the 8x8 matrix coded as row-major? |
| 2351 | static const stbi_uc stbi__jpeg_dezigzag[64+15] = |
| 2352 | { |
| 2353 | 0, 1, 8, 16, 9, 2, 3, 10, |
| 2354 | 17, 24, 32, 25, 18, 11, 4, 5, |
| 2355 | 12, 19, 26, 33, 40, 48, 41, 34, |
| 2356 | 27, 20, 13, 6, 7, 14, 21, 28, |
| 2357 | 35, 42, 49, 56, 57, 50, 43, 36, |
| 2358 | 29, 22, 15, 23, 30, 37, 44, 51, |
| 2359 | 58, 59, 52, 45, 38, 31, 39, 46, |
| 2360 | 53, 60, 61, 54, 47, 55, 62, 63, |
| 2361 | // let corrupt input sample past end |
| 2362 | 63, 63, 63, 63, 63, 63, 63, 63, |
| 2363 | 63, 63, 63, 63, 63, 63, 63 |
| 2364 | }; |
| 2365 | |
| 2366 | // decode one 64-entry block-- |
| 2367 | static 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) |
| 2368 | { |
| 2369 | int diff,dc,k; |
| 2370 | int t; |
| 2371 | |
| 2372 | if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); |
| 2373 | t = stbi__jpeg_huff_decode(j, hdc); |
| 2374 | if (t < 0 || t > 15) return stbi__err("bad huffman code" ,"Corrupt JPEG" ); |
| 2375 | |
| 2376 | // 0 all the ac values now so we can do it 32-bits at a time |
| 2377 | memset(data,0,64*sizeof(data[0])); |
| 2378 | |
| 2379 | diff = t ? stbi__extend_receive(j, t) : 0; |
| 2380 | if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta" ,"Corrupt JPEG" ); |
| 2381 | dc = j->img_comp[b].dc_pred + diff; |
| 2382 | j->img_comp[b].dc_pred = dc; |
| 2383 | if (!stbi__mul2shorts_valid(dc, dequant[0])) return stbi__err("can't merge dc and ac" , "Corrupt JPEG" ); |
| 2384 | data[0] = (short) (dc * dequant[0]); |
| 2385 | |
| 2386 | // decode AC components, see JPEG spec |
| 2387 | k = 1; |
| 2388 | do { |
| 2389 | unsigned int zig; |
| 2390 | int c,r,s; |
| 2391 | if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); |
| 2392 | c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); |
| 2393 | r = fac[c]; |
| 2394 | if (r) { // fast-AC path |
| 2395 | k += (r >> 4) & 15; // run |
| 2396 | s = r & 15; // combined length |
| 2397 | if (s > j->code_bits) return stbi__err("bad huffman code" , "Combined length longer than code bits available" ); |
| 2398 | j->code_buffer <<= s; |
| 2399 | j->code_bits -= s; |
| 2400 | // decode into unzigzag'd location |
| 2401 | zig = stbi__jpeg_dezigzag[k++]; |
| 2402 | data[zig] = (short) ((r >> 8) * dequant[zig]); |
| 2403 | } else { |
| 2404 | int rs = stbi__jpeg_huff_decode(j, hac); |
| 2405 | if (rs < 0) return stbi__err("bad huffman code" ,"Corrupt JPEG" ); |
| 2406 | s = rs & 15; |
| 2407 | r = rs >> 4; |
| 2408 | if (s == 0) { |
| 2409 | if (rs != 0xf0) break; // end block |
| 2410 | k += 16; |
| 2411 | } else { |
| 2412 | k += r; |
| 2413 | // decode into unzigzag'd location |
| 2414 | zig = stbi__jpeg_dezigzag[k++]; |
| 2415 | data[zig] = (short) (stbi__extend_receive(j,s) * dequant[zig]); |
| 2416 | } |
| 2417 | } |
| 2418 | } while (k < 64); |
| 2419 | return 1; |
| 2420 | } |
| 2421 | |
| 2422 | static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__huffman *hdc, int b) |
| 2423 | { |
| 2424 | int diff,dc; |
| 2425 | int t; |
| 2426 | if (j->spec_end != 0) return stbi__err("can't merge dc and ac" , "Corrupt JPEG" ); |
| 2427 | |
| 2428 | if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); |
| 2429 | |
| 2430 | if (j->succ_high == 0) { |
| 2431 | // first scan for DC coefficient, must be first |
| 2432 | memset(data,0,64*sizeof(data[0])); // 0 all the ac values now |
| 2433 | t = stbi__jpeg_huff_decode(j, hdc); |
| 2434 | if (t < 0 || t > 15) return stbi__err("can't merge dc and ac" , "Corrupt JPEG" ); |
| 2435 | diff = t ? stbi__extend_receive(j, t) : 0; |
| 2436 | |
| 2437 | if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta" , "Corrupt JPEG" ); |
| 2438 | dc = j->img_comp[b].dc_pred + diff; |
| 2439 | j->img_comp[b].dc_pred = dc; |
| 2440 | if (!stbi__mul2shorts_valid(dc, 1 << j->succ_low)) return stbi__err("can't merge dc and ac" , "Corrupt JPEG" ); |
| 2441 | data[0] = (short) (dc * (1 << j->succ_low)); |
| 2442 | } else { |
| 2443 | // refinement scan for DC coefficient |
| 2444 | if (stbi__jpeg_get_bit(j)) |
| 2445 | data[0] += (short) (1 << j->succ_low); |
| 2446 | } |
| 2447 | return 1; |
| 2448 | } |
| 2449 | |
| 2450 | // @OPTIMIZE: store non-zigzagged during the decode passes, |
| 2451 | // and only de-zigzag when dequantizing |
| 2452 | static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__huffman *hac, stbi__int16 *fac) |
| 2453 | { |
| 2454 | int k; |
| 2455 | if (j->spec_start == 0) return stbi__err("can't merge dc and ac" , "Corrupt JPEG" ); |
| 2456 | |
| 2457 | if (j->succ_high == 0) { |
| 2458 | int shift = j->succ_low; |
| 2459 | |
| 2460 | if (j->eob_run) { |
| 2461 | --j->eob_run; |
| 2462 | return 1; |
| 2463 | } |
| 2464 | |
| 2465 | k = j->spec_start; |
| 2466 | do { |
| 2467 | unsigned int zig; |
| 2468 | int c,r,s; |
| 2469 | if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); |
| 2470 | c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); |
| 2471 | r = fac[c]; |
| 2472 | if (r) { // fast-AC path |
| 2473 | k += (r >> 4) & 15; // run |
| 2474 | s = r & 15; // combined length |
| 2475 | if (s > j->code_bits) return stbi__err("bad huffman code" , "Combined length longer than code bits available" ); |
| 2476 | j->code_buffer <<= s; |
| 2477 | j->code_bits -= s; |
| 2478 | zig = stbi__jpeg_dezigzag[k++]; |
| 2479 | data[zig] = (short) ((r >> 8) * (1 << shift)); |
| 2480 | } else { |
| 2481 | int rs = stbi__jpeg_huff_decode(j, hac); |
| 2482 | if (rs < 0) return stbi__err("bad huffman code" ,"Corrupt JPEG" ); |
| 2483 | s = rs & 15; |
| 2484 | r = rs >> 4; |
| 2485 | if (s == 0) { |
| 2486 | if (r < 15) { |
| 2487 | j->eob_run = (1 << r); |
| 2488 | if (r) |
| 2489 | j->eob_run += stbi__jpeg_get_bits(j, r); |
| 2490 | --j->eob_run; |
| 2491 | break; |
| 2492 | } |
| 2493 | k += 16; |
| 2494 | } else { |
| 2495 | k += r; |
| 2496 | zig = stbi__jpeg_dezigzag[k++]; |
| 2497 | data[zig] = (short) (stbi__extend_receive(j,s) * (1 << shift)); |
| 2498 | } |
| 2499 | } |
| 2500 | } while (k <= j->spec_end); |
| 2501 | } else { |
| 2502 | // refinement scan for these AC coefficients |
| 2503 | |
| 2504 | short bit = (short) (1 << j->succ_low); |
| 2505 | |
| 2506 | if (j->eob_run) { |
| 2507 | --j->eob_run; |
| 2508 | for (k = j->spec_start; k <= j->spec_end; ++k) { |
| 2509 | short *p = &data[stbi__jpeg_dezigzag[k]]; |
| 2510 | if (*p != 0) |
| 2511 | if (stbi__jpeg_get_bit(j)) |
| 2512 | if ((*p & bit)==0) { |
| 2513 | if (*p > 0) |
| 2514 | *p += bit; |
| 2515 | else |
| 2516 | *p -= bit; |
| 2517 | } |
| 2518 | } |
| 2519 | } else { |
| 2520 | k = j->spec_start; |
| 2521 | do { |
| 2522 | int r,s; |
| 2523 | 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 |
| 2524 | if (rs < 0) return stbi__err("bad huffman code" ,"Corrupt JPEG" ); |
| 2525 | s = rs & 15; |
| 2526 | r = rs >> 4; |
| 2527 | if (s == 0) { |
| 2528 | if (r < 15) { |
| 2529 | j->eob_run = (1 << r) - 1; |
| 2530 | if (r) |
| 2531 | j->eob_run += stbi__jpeg_get_bits(j, r); |
| 2532 | r = 64; // force end of block |
| 2533 | } else { |
| 2534 | // r=15 s=0 should write 16 0s, so we just do |
| 2535 | // a run of 15 0s and then write s (which is 0), |
| 2536 | // so we don't have to do anything special here |
| 2537 | } |
| 2538 | } else { |
| 2539 | if (s != 1) return stbi__err("bad huffman code" , "Corrupt JPEG" ); |
| 2540 | // sign bit |
| 2541 | if (stbi__jpeg_get_bit(j)) |
| 2542 | s = bit; |
| 2543 | else |
| 2544 | s = -bit; |
| 2545 | } |
| 2546 | |
| 2547 | // advance by r |
| 2548 | while (k <= j->spec_end) { |
| 2549 | short *p = &data[stbi__jpeg_dezigzag[k++]]; |
| 2550 | if (*p != 0) { |
| 2551 | if (stbi__jpeg_get_bit(j)) |
| 2552 | if ((*p & bit)==0) { |
| 2553 | if (*p > 0) |
| 2554 | *p += bit; |
| 2555 | else |
| 2556 | *p -= bit; |
| 2557 | } |
| 2558 | } else { |
| 2559 | if (r == 0) { |
| 2560 | *p = (short) s; |
| 2561 | break; |
| 2562 | } |
| 2563 | --r; |
| 2564 | } |
| 2565 | } |
| 2566 | } while (k <= j->spec_end); |
| 2567 | } |
| 2568 | } |
| 2569 | return 1; |
| 2570 | } |
| 2571 | |
| 2572 | // take a -128..127 value and stbi__clamp it and convert to 0..255 |
| 2573 | stbi_inline static stbi_uc stbi__clamp(int x) |
| 2574 | { |
| 2575 | // trick to use a single test to catch both cases |
| 2576 | if ((unsigned int) x > 255) { |
| 2577 | if (x < 0) return 0; |
| 2578 | if (x > 255) return 255; |
| 2579 | } |
| 2580 | return (stbi_uc) x; |
| 2581 | } |
| 2582 | |
| 2583 | #define stbi__f2f(x) ((int) (((x) * 4096 + 0.5))) |
| 2584 | #define stbi__fsh(x) ((x) * 4096) |
| 2585 | |
| 2586 | // derived from jidctint -- DCT_ISLOW |
| 2587 | #define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \ |
| 2588 | int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \ |
| 2589 | p2 = s2; \ |
| 2590 | p3 = s6; \ |
| 2591 | p1 = (p2+p3) * stbi__f2f(0.5411961f); \ |
| 2592 | t2 = p1 + p3*stbi__f2f(-1.847759065f); \ |
| 2593 | t3 = p1 + p2*stbi__f2f( 0.765366865f); \ |
| 2594 | p2 = s0; \ |
| 2595 | p3 = s4; \ |
| 2596 | t0 = stbi__fsh(p2+p3); \ |
| 2597 | t1 = stbi__fsh(p2-p3); \ |
| 2598 | x0 = t0+t3; \ |
| 2599 | x3 = t0-t3; \ |
| 2600 | x1 = t1+t2; \ |
| 2601 | x2 = t1-t2; \ |
| 2602 | t0 = s7; \ |
| 2603 | t1 = s5; \ |
| 2604 | t2 = s3; \ |
| 2605 | t3 = s1; \ |
| 2606 | p3 = t0+t2; \ |
| 2607 | p4 = t1+t3; \ |
| 2608 | p1 = t0+t3; \ |
| 2609 | p2 = t1+t2; \ |
| 2610 | p5 = (p3+p4)*stbi__f2f( 1.175875602f); \ |
| 2611 | t0 = t0*stbi__f2f( 0.298631336f); \ |
| 2612 | t1 = t1*stbi__f2f( 2.053119869f); \ |
| 2613 | t2 = t2*stbi__f2f( 3.072711026f); \ |
| 2614 | t3 = t3*stbi__f2f( 1.501321110f); \ |
| 2615 | p1 = p5 + p1*stbi__f2f(-0.899976223f); \ |
| 2616 | p2 = p5 + p2*stbi__f2f(-2.562915447f); \ |
| 2617 | p3 = p3*stbi__f2f(-1.961570560f); \ |
| 2618 | p4 = p4*stbi__f2f(-0.390180644f); \ |
| 2619 | t3 += p1+p4; \ |
| 2620 | t2 += p2+p3; \ |
| 2621 | t1 += p2+p4; \ |
| 2622 | t0 += p1+p3; |
| 2623 | |
| 2624 | static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64]) |
| 2625 | { |
| 2626 | int i,val[64],*v=val; |
| 2627 | stbi_uc *o; |
| 2628 | short *d = data; |
| 2629 | |
| 2630 | // columns |
| 2631 | for (i=0; i < 8; ++i,++d, ++v) { |
| 2632 | // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing |
| 2633 | if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0 |
| 2634 | && d[40]==0 && d[48]==0 && d[56]==0) { |
| 2635 | // no shortcut 0 seconds |
| 2636 | // (1|2|3|4|5|6|7)==0 0 seconds |
| 2637 | // all separate -0.047 seconds |
| 2638 | // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds |
| 2639 | int dcterm = d[0]*4; |
| 2640 | v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm; |
| 2641 | } else { |
| 2642 | STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56]) |
| 2643 | // constants scaled things up by 1<<12; let's bring them back |
| 2644 | // down, but keep 2 extra bits of precision |
| 2645 | x0 += 512; x1 += 512; x2 += 512; x3 += 512; |
| 2646 | v[ 0] = (x0+t3) >> 10; |
| 2647 | v[56] = (x0-t3) >> 10; |
| 2648 | v[ 8] = (x1+t2) >> 10; |
| 2649 | v[48] = (x1-t2) >> 10; |
| 2650 | v[16] = (x2+t1) >> 10; |
| 2651 | v[40] = (x2-t1) >> 10; |
| 2652 | v[24] = (x3+t0) >> 10; |
| 2653 | v[32] = (x3-t0) >> 10; |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) { |
| 2658 | // no fast case since the first 1D IDCT spread components out |
| 2659 | STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7]) |
| 2660 | // constants scaled things up by 1<<12, plus we had 1<<2 from first |
| 2661 | // loop, plus horizontal and vertical each scale by sqrt(8) so together |
| 2662 | // we've got an extra 1<<3, so 1<<17 total we need to remove. |
| 2663 | // so we want to round that, which means adding 0.5 * 1<<17, |
| 2664 | // aka 65536. Also, we'll end up with -128 to 127 that we want |
| 2665 | // to encode as 0..255 by adding 128, so we'll add that before the shift |
| 2666 | x0 += 65536 + (128<<17); |
| 2667 | x1 += 65536 + (128<<17); |
| 2668 | x2 += 65536 + (128<<17); |
| 2669 | x3 += 65536 + (128<<17); |
| 2670 | // tried computing the shifts into temps, or'ing the temps to see |
| 2671 | // if any were out of range, but that was slower |
| 2672 | o[0] = stbi__clamp((x0+t3) >> 17); |
| 2673 | o[7] = stbi__clamp((x0-t3) >> 17); |
| 2674 | o[1] = stbi__clamp((x1+t2) >> 17); |
| 2675 | o[6] = stbi__clamp((x1-t2) >> 17); |
| 2676 | o[2] = stbi__clamp((x2+t1) >> 17); |
| 2677 | o[5] = stbi__clamp((x2-t1) >> 17); |
| 2678 | o[3] = stbi__clamp((x3+t0) >> 17); |
| 2679 | o[4] = stbi__clamp((x3-t0) >> 17); |
| 2680 | } |
| 2681 | } |
| 2682 | |
| 2683 | #ifdef STBI_SSE2 |
| 2684 | // sse2 integer IDCT. not the fastest possible implementation but it |
| 2685 | // produces bit-identical results to the generic C version so it's |
| 2686 | // fully "transparent". |
| 2687 | static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64]) |
| 2688 | { |
| 2689 | // This is constructed to match our regular (generic) integer IDCT exactly. |
| 2690 | __m128i row0, row1, row2, row3, row4, row5, row6, row7; |
| 2691 | __m128i tmp; |
| 2692 | |
| 2693 | // dot product constant: even elems=x, odd elems=y |
| 2694 | #define dct_const(x,y) _mm_setr_epi16((x),(y),(x),(y),(x),(y),(x),(y)) |
| 2695 | |
| 2696 | // out(0) = c0[even]*x + c0[odd]*y (c0, x, y 16-bit, out 32-bit) |
| 2697 | // out(1) = c1[even]*x + c1[odd]*y |
| 2698 | #define dct_rot(out0,out1, x,y,c0,c1) \ |
| 2699 | __m128i c0##lo = _mm_unpacklo_epi16((x),(y)); \ |
| 2700 | __m128i c0##hi = _mm_unpackhi_epi16((x),(y)); \ |
| 2701 | __m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \ |
| 2702 | __m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \ |
| 2703 | __m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \ |
| 2704 | __m128i out1##_h = _mm_madd_epi16(c0##hi, c1) |
| 2705 | |
| 2706 | // out = in << 12 (in 16-bit, out 32-bit) |
| 2707 | #define dct_widen(out, in) \ |
| 2708 | __m128i out##_l = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \ |
| 2709 | __m128i out##_h = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4) |
| 2710 | |
| 2711 | // wide add |
| 2712 | #define dct_wadd(out, a, b) \ |
| 2713 | __m128i out##_l = _mm_add_epi32(a##_l, b##_l); \ |
| 2714 | __m128i out##_h = _mm_add_epi32(a##_h, b##_h) |
| 2715 | |
| 2716 | // wide sub |
| 2717 | #define dct_wsub(out, a, b) \ |
| 2718 | __m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \ |
| 2719 | __m128i out##_h = _mm_sub_epi32(a##_h, b##_h) |
| 2720 | |
| 2721 | // butterfly a/b, add bias, then shift by "s" and pack |
| 2722 | #define dct_bfly32o(out0, out1, a,b,bias,s) \ |
| 2723 | { \ |
| 2724 | __m128i abiased_l = _mm_add_epi32(a##_l, bias); \ |
| 2725 | __m128i abiased_h = _mm_add_epi32(a##_h, bias); \ |
| 2726 | dct_wadd(sum, abiased, b); \ |
| 2727 | dct_wsub(dif, abiased, b); \ |
| 2728 | out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), _mm_srai_epi32(sum_h, s)); \ |
| 2729 | out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), _mm_srai_epi32(dif_h, s)); \ |
| 2730 | } |
| 2731 | |
| 2732 | // 8-bit interleave step (for transposes) |
| 2733 | #define dct_interleave8(a, b) \ |
| 2734 | tmp = a; \ |
| 2735 | a = _mm_unpacklo_epi8(a, b); \ |
| 2736 | b = _mm_unpackhi_epi8(tmp, b) |
| 2737 | |
| 2738 | // 16-bit interleave step (for transposes) |
| 2739 | #define dct_interleave16(a, b) \ |
| 2740 | tmp = a; \ |
| 2741 | a = _mm_unpacklo_epi16(a, b); \ |
| 2742 | b = _mm_unpackhi_epi16(tmp, b) |
| 2743 | |
| 2744 | #define dct_pass(bias,shift) \ |
| 2745 | do { \ |
| 2746 | /* even part */ \ |
| 2747 | dct_rot(t2e,t3e, row2,row6, rot0_0,rot0_1); \ |
| 2748 | __m128i sum04 = _mm_add_epi16(row0, row4); \ |
| 2749 | __m128i dif04 = _mm_sub_epi16(row0, row4); \ |
| 2750 | dct_widen(t0e, sum04); \ |
| 2751 | dct_widen(t1e, dif04); \ |
| 2752 | dct_wadd(x0, t0e, t3e); \ |
| 2753 | dct_wsub(x3, t0e, t3e); \ |
| 2754 | dct_wadd(x1, t1e, t2e); \ |
| 2755 | dct_wsub(x2, t1e, t2e); \ |
| 2756 | /* odd part */ \ |
| 2757 | dct_rot(y0o,y2o, row7,row3, rot2_0,rot2_1); \ |
| 2758 | dct_rot(y1o,y3o, row5,row1, rot3_0,rot3_1); \ |
| 2759 | __m128i sum17 = _mm_add_epi16(row1, row7); \ |
| 2760 | __m128i sum35 = _mm_add_epi16(row3, row5); \ |
| 2761 | dct_rot(y4o,y5o, sum17,sum35, rot1_0,rot1_1); \ |
| 2762 | dct_wadd(x4, y0o, y4o); \ |
| 2763 | dct_wadd(x5, y1o, y5o); \ |
| 2764 | dct_wadd(x6, y2o, y5o); \ |
| 2765 | dct_wadd(x7, y3o, y4o); \ |
| 2766 | dct_bfly32o(row0,row7, x0,x7,bias,shift); \ |
| 2767 | dct_bfly32o(row1,row6, x1,x6,bias,shift); \ |
| 2768 | dct_bfly32o(row2,row5, x2,x5,bias,shift); \ |
| 2769 | dct_bfly32o(row3,row4, x3,x4,bias,shift); \ |
| 2770 | } while ( 0 ) |
| 2771 | |
| 2772 | __m128i rot0_0 = dct_const(stbi__f2f(0.5411961f), stbi__f2f(0.5411961f) + stbi__f2f(-1.847759065f)); |
| 2773 | __m128i rot0_1 = dct_const(stbi__f2f(0.5411961f) + stbi__f2f( 0.765366865f), stbi__f2f(0.5411961f)); |
| 2774 | __m128i rot1_0 = dct_const(stbi__f2f(1.175875602f) + stbi__f2f(-0.899976223f), stbi__f2f(1.175875602f)); |
| 2775 | __m128i rot1_1 = dct_const(stbi__f2f(1.175875602f), stbi__f2f(1.175875602f) + stbi__f2f(-2.562915447f)); |
| 2776 | __m128i rot2_0 = dct_const(stbi__f2f(-1.961570560f) + stbi__f2f( 0.298631336f), stbi__f2f(-1.961570560f)); |
| 2777 | __m128i rot2_1 = dct_const(stbi__f2f(-1.961570560f), stbi__f2f(-1.961570560f) + stbi__f2f( 3.072711026f)); |
| 2778 | __m128i rot3_0 = dct_const(stbi__f2f(-0.390180644f) + stbi__f2f( 2.053119869f), stbi__f2f(-0.390180644f)); |
| 2779 | __m128i rot3_1 = dct_const(stbi__f2f(-0.390180644f), stbi__f2f(-0.390180644f) + stbi__f2f( 1.501321110f)); |
| 2780 | |
| 2781 | // rounding biases in column/row passes, see stbi__idct_block for explanation. |
| 2782 | __m128i bias_0 = _mm_set1_epi32(512); |
| 2783 | __m128i bias_1 = _mm_set1_epi32(65536 + (128<<17)); |
| 2784 | |
| 2785 | // load |
| 2786 | row0 = _mm_load_si128((const __m128i *) (data + 0*8)); |
| 2787 | row1 = _mm_load_si128((const __m128i *) (data + 1*8)); |
| 2788 | row2 = _mm_load_si128((const __m128i *) (data + 2*8)); |
| 2789 | row3 = _mm_load_si128((const __m128i *) (data + 3*8)); |
| 2790 | row4 = _mm_load_si128((const __m128i *) (data + 4*8)); |
| 2791 | row5 = _mm_load_si128((const __m128i *) (data + 5*8)); |
| 2792 | row6 = _mm_load_si128((const __m128i *) (data + 6*8)); |
| 2793 | row7 = _mm_load_si128((const __m128i *) (data + 7*8)); |
| 2794 | |
| 2795 | // column pass |
| 2796 | dct_pass(bias_0, 10); |
| 2797 | |
| 2798 | { |
| 2799 | // 16bit 8x8 transpose pass 1 |
| 2800 | dct_interleave16(row0, row4); |
| 2801 | dct_interleave16(row1, row5); |
| 2802 | dct_interleave16(row2, row6); |
| 2803 | dct_interleave16(row3, row7); |
| 2804 | |
| 2805 | // transpose pass 2 |
| 2806 | dct_interleave16(row0, row2); |
| 2807 | dct_interleave16(row1, row3); |
| 2808 | dct_interleave16(row4, row6); |
| 2809 | dct_interleave16(row5, row7); |
| 2810 | |
| 2811 | // transpose pass 3 |
| 2812 | dct_interleave16(row0, row1); |
| 2813 | dct_interleave16(row2, row3); |
| 2814 | dct_interleave16(row4, row5); |
| 2815 | dct_interleave16(row6, row7); |
| 2816 | } |
| 2817 | |
| 2818 | // row pass |
| 2819 | dct_pass(bias_1, 17); |
| 2820 | |
| 2821 | { |
| 2822 | // pack |
| 2823 | __m128i p0 = _mm_packus_epi16(row0, row1); // a0a1a2a3...a7b0b1b2b3...b7 |
| 2824 | __m128i p1 = _mm_packus_epi16(row2, row3); |
| 2825 | __m128i p2 = _mm_packus_epi16(row4, row5); |
| 2826 | __m128i p3 = _mm_packus_epi16(row6, row7); |
| 2827 | |
| 2828 | // 8bit 8x8 transpose pass 1 |
| 2829 | dct_interleave8(p0, p2); // a0e0a1e1... |
| 2830 | dct_interleave8(p1, p3); // c0g0c1g1... |
| 2831 | |
| 2832 | // transpose pass 2 |
| 2833 | dct_interleave8(p0, p1); // a0c0e0g0... |
| 2834 | dct_interleave8(p2, p3); // b0d0f0h0... |
| 2835 | |
| 2836 | // transpose pass 3 |
| 2837 | dct_interleave8(p0, p2); // a0b0c0d0... |
| 2838 | dct_interleave8(p1, p3); // a4b4c4d4... |
| 2839 | |
| 2840 | // store |
| 2841 | _mm_storel_epi64((__m128i *) out, p0); out += out_stride; |
| 2842 | _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p0, 0x4e)); out += out_stride; |
| 2843 | _mm_storel_epi64((__m128i *) out, p2); out += out_stride; |
| 2844 | _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p2, 0x4e)); out += out_stride; |
| 2845 | _mm_storel_epi64((__m128i *) out, p1); out += out_stride; |
| 2846 | _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p1, 0x4e)); out += out_stride; |
| 2847 | _mm_storel_epi64((__m128i *) out, p3); out += out_stride; |
| 2848 | _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p3, 0x4e)); |
| 2849 | } |
| 2850 | |
| 2851 | #undef dct_const |
| 2852 | #undef dct_rot |
| 2853 | #undef dct_widen |
| 2854 | #undef dct_wadd |
| 2855 | #undef dct_wsub |
| 2856 | #undef dct_bfly32o |
| 2857 | #undef dct_interleave8 |
| 2858 | #undef dct_interleave16 |
| 2859 | #undef dct_pass |
| 2860 | } |
| 2861 | |
| 2862 | #endif // STBI_SSE2 |
| 2863 | |
| 2864 | #ifdef STBI_NEON |
| 2865 | |
| 2866 | // NEON integer IDCT. should produce bit-identical |
| 2867 | // results to the generic C version. |
| 2868 | static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64]) |
| 2869 | { |
| 2870 | int16x8_t row0, row1, row2, row3, row4, row5, row6, row7; |
| 2871 | |
| 2872 | int16x4_t rot0_0 = vdup_n_s16(stbi__f2f(0.5411961f)); |
| 2873 | int16x4_t rot0_1 = vdup_n_s16(stbi__f2f(-1.847759065f)); |
| 2874 | int16x4_t rot0_2 = vdup_n_s16(stbi__f2f( 0.765366865f)); |
| 2875 | int16x4_t rot1_0 = vdup_n_s16(stbi__f2f( 1.175875602f)); |
| 2876 | int16x4_t rot1_1 = vdup_n_s16(stbi__f2f(-0.899976223f)); |
| 2877 | int16x4_t rot1_2 = vdup_n_s16(stbi__f2f(-2.562915447f)); |
| 2878 | int16x4_t rot2_0 = vdup_n_s16(stbi__f2f(-1.961570560f)); |
| 2879 | int16x4_t rot2_1 = vdup_n_s16(stbi__f2f(-0.390180644f)); |
| 2880 | int16x4_t rot3_0 = vdup_n_s16(stbi__f2f( 0.298631336f)); |
| 2881 | int16x4_t rot3_1 = vdup_n_s16(stbi__f2f( 2.053119869f)); |
| 2882 | int16x4_t rot3_2 = vdup_n_s16(stbi__f2f( 3.072711026f)); |
| 2883 | int16x4_t rot3_3 = vdup_n_s16(stbi__f2f( 1.501321110f)); |
| 2884 | |
| 2885 | #define dct_long_mul(out, inq, coeff) \ |
| 2886 | int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \ |
| 2887 | int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff) |
| 2888 | |
| 2889 | #define dct_long_mac(out, acc, inq, coeff) \ |
| 2890 | int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \ |
| 2891 | int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff) |
| 2892 | |
| 2893 | #define dct_widen(out, inq) \ |
| 2894 | int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \ |
| 2895 | int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12) |
| 2896 | |
| 2897 | // wide add |
| 2898 | #define dct_wadd(out, a, b) \ |
| 2899 | int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \ |
| 2900 | int32x4_t out##_h = vaddq_s32(a##_h, b##_h) |
| 2901 | |
| 2902 | // wide sub |
| 2903 | #define dct_wsub(out, a, b) \ |
| 2904 | int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \ |
| 2905 | int32x4_t out##_h = vsubq_s32(a##_h, b##_h) |
| 2906 | |
| 2907 | // butterfly a/b, then shift using "shiftop" by "s" and pack |
| 2908 | #define dct_bfly32o(out0,out1, a,b,shiftop,s) \ |
| 2909 | do { \ |
| 2910 | dct_wadd(sum, a, b); \ |
| 2911 | dct_wsub(dif, a, b); \ |
| 2912 | out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \ |
| 2913 | out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \ |
| 2914 | } while ( 0 ) |
| 2915 | |
| 2916 | #define dct_pass(shiftop, shift) \ |
| 2917 | do { \ |
| 2918 | /* even part */ \ |
| 2919 | int16x8_t sum26 = vaddq_s16(row2, row6); \ |
| 2920 | dct_long_mul(p1e, sum26, rot0_0); \ |
| 2921 | dct_long_mac(t2e, p1e, row6, rot0_1); \ |
| 2922 | dct_long_mac(t3e, p1e, row2, rot0_2); \ |
| 2923 | int16x8_t sum04 = vaddq_s16(row0, row4); \ |
| 2924 | int16x8_t dif04 = vsubq_s16(row0, row4); \ |
| 2925 | dct_widen(t0e, sum04); \ |
| 2926 | dct_widen(t1e, dif04); \ |
| 2927 | dct_wadd(x0, t0e, t3e); \ |
| 2928 | dct_wsub(x3, t0e, t3e); \ |
| 2929 | dct_wadd(x1, t1e, t2e); \ |
| 2930 | dct_wsub(x2, t1e, t2e); \ |
| 2931 | /* odd part */ \ |
| 2932 | int16x8_t sum15 = vaddq_s16(row1, row5); \ |
| 2933 | int16x8_t sum17 = vaddq_s16(row1, row7); \ |
| 2934 | int16x8_t sum35 = vaddq_s16(row3, row5); \ |
| 2935 | int16x8_t sum37 = vaddq_s16(row3, row7); \ |
| 2936 | int16x8_t sumodd = vaddq_s16(sum17, sum35); \ |
| 2937 | dct_long_mul(p5o, sumodd, rot1_0); \ |
| 2938 | dct_long_mac(p1o, p5o, sum17, rot1_1); \ |
| 2939 | dct_long_mac(p2o, p5o, sum35, rot1_2); \ |
| 2940 | dct_long_mul(p3o, sum37, rot2_0); \ |
| 2941 | dct_long_mul(p4o, sum15, rot2_1); \ |
| 2942 | dct_wadd(sump13o, p1o, p3o); \ |
| 2943 | dct_wadd(sump24o, p2o, p4o); \ |
| 2944 | dct_wadd(sump23o, p2o, p3o); \ |
| 2945 | dct_wadd(sump14o, p1o, p4o); \ |
| 2946 | dct_long_mac(x4, sump13o, row7, rot3_0); \ |
| 2947 | dct_long_mac(x5, sump24o, row5, rot3_1); \ |
| 2948 | dct_long_mac(x6, sump23o, row3, rot3_2); \ |
| 2949 | dct_long_mac(x7, sump14o, row1, rot3_3); \ |
| 2950 | dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \ |
| 2951 | dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \ |
| 2952 | dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \ |
| 2953 | dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \ |
| 2954 | } while ( 0 ) |
| 2955 | |
| 2956 | // load |
| 2957 | row0 = vld1q_s16(data + 0*8); |
| 2958 | row1 = vld1q_s16(data + 1*8); |
| 2959 | row2 = vld1q_s16(data + 2*8); |
| 2960 | row3 = vld1q_s16(data + 3*8); |
| 2961 | row4 = vld1q_s16(data + 4*8); |
| 2962 | row5 = vld1q_s16(data + 5*8); |
| 2963 | row6 = vld1q_s16(data + 6*8); |
| 2964 | row7 = vld1q_s16(data + 7*8); |
| 2965 | |
| 2966 | // add DC bias |
| 2967 | row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0)); |
| 2968 | |
| 2969 | // column pass |
| 2970 | dct_pass(vrshrn_n_s32, 10); |
| 2971 | |
| 2972 | // 16bit 8x8 transpose |
| 2973 | { |
| 2974 | // these three map to a single VTRN.16, VTRN.32, and VSWP, respectively. |
| 2975 | // whether compilers actually get this is another story, sadly. |
| 2976 | #define dct_trn16(x, y) do { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; } while ( 0 ) |
| 2977 | #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 ) |
| 2978 | #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 ) |
| 2979 | |
| 2980 | // pass 1 |
| 2981 | dct_trn16(row0, row1); // a0b0a2b2a4b4a6b6 |
| 2982 | dct_trn16(row2, row3); |
| 2983 | dct_trn16(row4, row5); |
| 2984 | dct_trn16(row6, row7); |
| 2985 | |
| 2986 | // pass 2 |
| 2987 | dct_trn32(row0, row2); // a0b0c0d0a4b4c4d4 |
| 2988 | dct_trn32(row1, row3); |
| 2989 | dct_trn32(row4, row6); |
| 2990 | dct_trn32(row5, row7); |
| 2991 | |
| 2992 | // pass 3 |
| 2993 | dct_trn64(row0, row4); // a0b0c0d0e0f0g0h0 |
| 2994 | dct_trn64(row1, row5); |
| 2995 | dct_trn64(row2, row6); |
| 2996 | dct_trn64(row3, row7); |
| 2997 | |
| 2998 | #undef dct_trn16 |
| 2999 | #undef dct_trn32 |
| 3000 | #undef dct_trn64 |
| 3001 | } |
| 3002 | |
| 3003 | // row pass |
| 3004 | // vrshrn_n_s32 only supports shifts up to 16, we need |
| 3005 | // 17. so do a non-rounding shift of 16 first then follow |
| 3006 | // up with a rounding shift by 1. |
| 3007 | dct_pass(vshrn_n_s32, 16); |
| 3008 | |
| 3009 | { |
| 3010 | // pack and round |
| 3011 | uint8x8_t p0 = vqrshrun_n_s16(row0, 1); |
| 3012 | uint8x8_t p1 = vqrshrun_n_s16(row1, 1); |
| 3013 | uint8x8_t p2 = vqrshrun_n_s16(row2, 1); |
| 3014 | uint8x8_t p3 = vqrshrun_n_s16(row3, 1); |
| 3015 | uint8x8_t p4 = vqrshrun_n_s16(row4, 1); |
| 3016 | uint8x8_t p5 = vqrshrun_n_s16(row5, 1); |
| 3017 | uint8x8_t p6 = vqrshrun_n_s16(row6, 1); |
| 3018 | uint8x8_t p7 = vqrshrun_n_s16(row7, 1); |
| 3019 | |
| 3020 | // again, these can translate into one instruction, but often don't. |
| 3021 | #define dct_trn8_8(x, y) do { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; } while ( 0 ) |
| 3022 | #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 ) |
| 3023 | #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 ) |
| 3024 | |
| 3025 | // sadly can't use interleaved stores here since we only write |
| 3026 | // 8 bytes to each scan line! |
| 3027 | |
| 3028 | // 8x8 8-bit transpose pass 1 |
| 3029 | dct_trn8_8(p0, p1); |
| 3030 | dct_trn8_8(p2, p3); |
| 3031 | dct_trn8_8(p4, p5); |
| 3032 | dct_trn8_8(p6, p7); |
| 3033 | |
| 3034 | // pass 2 |
| 3035 | dct_trn8_16(p0, p2); |
| 3036 | dct_trn8_16(p1, p3); |
| 3037 | dct_trn8_16(p4, p6); |
| 3038 | dct_trn8_16(p5, p7); |
| 3039 | |
| 3040 | // pass 3 |
| 3041 | dct_trn8_32(p0, p4); |
| 3042 | dct_trn8_32(p1, p5); |
| 3043 | dct_trn8_32(p2, p6); |
| 3044 | dct_trn8_32(p3, p7); |
| 3045 | |
| 3046 | // store |
| 3047 | vst1_u8(out, p0); out += out_stride; |
| 3048 | vst1_u8(out, p1); out += out_stride; |
| 3049 | vst1_u8(out, p2); out += out_stride; |
| 3050 | vst1_u8(out, p3); out += out_stride; |
| 3051 | vst1_u8(out, p4); out += out_stride; |
| 3052 | vst1_u8(out, p5); out += out_stride; |
| 3053 | vst1_u8(out, p6); out += out_stride; |
| 3054 | vst1_u8(out, p7); |
| 3055 | |
| 3056 | #undef dct_trn8_8 |
| 3057 | #undef dct_trn8_16 |
| 3058 | #undef dct_trn8_32 |
| 3059 | } |
| 3060 | |
| 3061 | #undef dct_long_mul |
| 3062 | #undef dct_long_mac |
| 3063 | #undef dct_widen |
| 3064 | #undef dct_wadd |
| 3065 | #undef dct_wsub |
| 3066 | #undef dct_bfly32o |
| 3067 | #undef dct_pass |
| 3068 | } |
| 3069 | |
| 3070 | #endif // STBI_NEON |
| 3071 | |
| 3072 | #define STBI__MARKER_none 0xff |
| 3073 | // if there's a pending marker from the entropy stream, return that |
| 3074 | // otherwise, fetch from the stream and get a marker. if there's no |
| 3075 | // marker, return 0xff, which is never a valid marker value |
| 3076 | static stbi_uc stbi__get_marker(stbi__jpeg *j) |
| 3077 | { |
| 3078 | stbi_uc x; |
| 3079 | if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none; return x; } |
| 3080 | x = stbi__get8(j->s); |
| 3081 | if (x != 0xff) return STBI__MARKER_none; |
| 3082 | while (x == 0xff) |
| 3083 | x = stbi__get8(j->s); // consume repeated 0xff fill bytes |
| 3084 | return x; |
| 3085 | } |
| 3086 | |
| 3087 | // in each scan, we'll have scan_n components, and the order |
| 3088 | // of the components is specified by order[] |
| 3089 | #define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7) |
| 3090 | |
| 3091 | // after a restart interval, stbi__jpeg_reset the entropy decoder and |
| 3092 | // the dc prediction |
| 3093 | static void stbi__jpeg_reset(stbi__jpeg *j) |
| 3094 | { |
| 3095 | j->code_bits = 0; |
| 3096 | j->code_buffer = 0; |
| 3097 | j->nomore = 0; |
| 3098 | 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; |
| 3099 | j->marker = STBI__MARKER_none; |
| 3100 | j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff; |
| 3101 | j->eob_run = 0; |
| 3102 | // no more than 1<<31 MCUs if no restart_interal? that's plenty safe, |
| 3103 | // since we don't even allow 1<<30 pixels |
| 3104 | } |
| 3105 | |
| 3106 | static int stbi__parse_entropy_coded_data(stbi__jpeg *z) |
| 3107 | { |
| 3108 | stbi__jpeg_reset(z); |
| 3109 | if (!z->progressive) { |
| 3110 | if (z->scan_n == 1) { |
| 3111 | int i,j; |
| 3112 | STBI_SIMD_ALIGN(short, data[64]); |
| 3113 | int n = z->order[0]; |
| 3114 | // non-interleaved data, we just need to process one block at a time, |
| 3115 | // in trivial scanline order |
| 3116 | // number of blocks to do just depends on how many actual "pixels" this |
| 3117 | // component has, independent of interleaved MCU blocking and such |
| 3118 | int w = (z->img_comp[n].x+7) >> 3; |
| 3119 | int h = (z->img_comp[n].y+7) >> 3; |
| 3120 | for (j=0; j < h; ++j) { |
| 3121 | for (i=0; i < w; ++i) { |
| 3122 | int ha = z->img_comp[n].ha; |
| 3123 | 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; |
| 3124 | z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data); |
| 3125 | // every data block is an MCU, so countdown the restart interval |
| 3126 | if (--z->todo <= 0) { |
| 3127 | if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); |
| 3128 | // if it's NOT a restart, then just bail, so we get corrupt data |
| 3129 | // rather than no data |
| 3130 | if (!STBI__RESTART(z->marker)) return 1; |
| 3131 | stbi__jpeg_reset(z); |
| 3132 | } |
| 3133 | } |
| 3134 | } |
| 3135 | return 1; |
| 3136 | } else { // interleaved |
| 3137 | int i,j,k,x,y; |
| 3138 | STBI_SIMD_ALIGN(short, data[64]); |
| 3139 | for (j=0; j < z->img_mcu_y; ++j) { |
| 3140 | for (i=0; i < z->img_mcu_x; ++i) { |
| 3141 | // scan an interleaved mcu... process scan_n components in order |
| 3142 | for (k=0; k < z->scan_n; ++k) { |
| 3143 | int n = z->order[k]; |
| 3144 | // scan out an mcu's worth of this component; that's just determined |
| 3145 | // by the basic H and V specified for the component |
| 3146 | for (y=0; y < z->img_comp[n].v; ++y) { |
| 3147 | for (x=0; x < z->img_comp[n].h; ++x) { |
| 3148 | int x2 = (i*z->img_comp[n].h + x)*8; |
| 3149 | int y2 = (j*z->img_comp[n].v + y)*8; |
| 3150 | int ha = z->img_comp[n].ha; |
| 3151 | 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; |
| 3152 | z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data); |
| 3153 | } |
| 3154 | } |
| 3155 | } |
| 3156 | // after all interleaved components, that's an interleaved MCU, |
| 3157 | // so now count down the restart interval |
| 3158 | if (--z->todo <= 0) { |
| 3159 | if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); |
| 3160 | if (!STBI__RESTART(z->marker)) return 1; |
| 3161 | stbi__jpeg_reset(z); |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | return 1; |
| 3166 | } |
| 3167 | } else { |
| 3168 | if (z->scan_n == 1) { |
| 3169 | int i,j; |
| 3170 | int n = z->order[0]; |
| 3171 | // non-interleaved data, we just need to process one block at a time, |
| 3172 | // in trivial scanline order |
| 3173 | // number of blocks to do just depends on how many actual "pixels" this |
| 3174 | // component has, independent of interleaved MCU blocking and such |
| 3175 | int w = (z->img_comp[n].x+7) >> 3; |
| 3176 | int h = (z->img_comp[n].y+7) >> 3; |
| 3177 | for (j=0; j < h; ++j) { |
| 3178 | for (i=0; i < w; ++i) { |
| 3179 | short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w); |
| 3180 | if (z->spec_start == 0) { |
| 3181 | if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n)) |
| 3182 | return 0; |
| 3183 | } else { |
| 3184 | int ha = z->img_comp[n].ha; |
| 3185 | if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha])) |
| 3186 | return 0; |
| 3187 | } |
| 3188 | // every data block is an MCU, so countdown the restart interval |
| 3189 | if (--z->todo <= 0) { |
| 3190 | if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); |
| 3191 | if (!STBI__RESTART(z->marker)) return 1; |
| 3192 | stbi__jpeg_reset(z); |
| 3193 | } |
| 3194 | } |
| 3195 | } |
| 3196 | return 1; |
| 3197 | } else { // interleaved |
| 3198 | int i,j,k,x,y; |
| 3199 | for (j=0; j < z->img_mcu_y; ++j) { |
| 3200 | for (i=0; i < z->img_mcu_x; ++i) { |
| 3201 | // scan an interleaved mcu... process scan_n components in order |
| 3202 | for (k=0; k < z->scan_n; ++k) { |
| 3203 | int n = z->order[k]; |
| 3204 | // scan out an mcu's worth of this component; that's just determined |
| 3205 | // by the basic H and V specified for the component |
| 3206 | for (y=0; y < z->img_comp[n].v; ++y) { |
| 3207 | for (x=0; x < z->img_comp[n].h; ++x) { |
| 3208 | int x2 = (i*z->img_comp[n].h + x); |
| 3209 | int y2 = (j*z->img_comp[n].v + y); |
| 3210 | short *data = z->img_comp[n].coeff + 64 * (x2 + y2 * z->img_comp[n].coeff_w); |
| 3211 | if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n)) |
| 3212 | return 0; |
| 3213 | } |
| 3214 | } |
| 3215 | } |
| 3216 | // after all interleaved components, that's an interleaved MCU, |
| 3217 | // so now count down the restart interval |
| 3218 | if (--z->todo <= 0) { |
| 3219 | if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); |
| 3220 | if (!STBI__RESTART(z->marker)) return 1; |
| 3221 | stbi__jpeg_reset(z); |
| 3222 | } |
| 3223 | } |
| 3224 | } |
| 3225 | return 1; |
| 3226 | } |
| 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | static void stbi__jpeg_dequantize(short *data, stbi__uint16 *dequant) |
| 3231 | { |
| 3232 | int i; |
| 3233 | for (i=0; i < 64; ++i) |
| 3234 | data[i] *= dequant[i]; |
| 3235 | } |
| 3236 | |
| 3237 | static void stbi__jpeg_finish(stbi__jpeg *z) |
| 3238 | { |
| 3239 | if (z->progressive) { |
| 3240 | // dequantize and idct the data |
| 3241 | int i,j,n; |
| 3242 | for (n=0; n < z->s->img_n; ++n) { |
| 3243 | int w = (z->img_comp[n].x+7) >> 3; |
| 3244 | int h = (z->img_comp[n].y+7) >> 3; |
| 3245 | for (j=0; j < h; ++j) { |
| 3246 | for (i=0; i < w; ++i) { |
| 3247 | short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w); |
| 3248 | stbi__jpeg_dequantize(data, z->dequant[z->img_comp[n].tq]); |
| 3249 | z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data); |
| 3250 | } |
| 3251 | } |
| 3252 | } |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | static int stbi__process_marker(stbi__jpeg *z, int m) |
| 3257 | { |
| 3258 | int L; |
| 3259 | switch (m) { |
| 3260 | case STBI__MARKER_none: // no marker found |
| 3261 | return stbi__err("expected marker" ,"Corrupt JPEG" ); |
| 3262 | |
| 3263 | case 0xDD: // DRI - specify restart interval |
| 3264 | if (stbi__get16be(z->s) != 4) return stbi__err("bad DRI len" ,"Corrupt JPEG" ); |
| 3265 | z->restart_interval = stbi__get16be(z->s); |
| 3266 | return 1; |
| 3267 | |
| 3268 | case 0xDB: // DQT - define quantization table |
| 3269 | L = stbi__get16be(z->s)-2; |
| 3270 | while (L > 0) { |
| 3271 | int q = stbi__get8(z->s); |
| 3272 | int p = q >> 4, sixteen = (p != 0); |
| 3273 | int t = q & 15,i; |
| 3274 | if (p != 0 && p != 1) return stbi__err("bad DQT type" ,"Corrupt JPEG" ); |
| 3275 | if (t > 3) return stbi__err("bad DQT table" ,"Corrupt JPEG" ); |
| 3276 | |
| 3277 | for (i=0; i < 64; ++i) |
| 3278 | z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s)); |
| 3279 | L -= (sixteen ? 129 : 65); |
| 3280 | } |
| 3281 | return L==0; |
| 3282 | |
| 3283 | case 0xC4: // DHT - define huffman table |
| 3284 | L = stbi__get16be(z->s)-2; |
| 3285 | while (L > 0) { |
| 3286 | stbi_uc *v; |
| 3287 | int sizes[16],i,n=0; |
| 3288 | int q = stbi__get8(z->s); |
| 3289 | int tc = q >> 4; |
| 3290 | int th = q & 15; |
| 3291 | if (tc > 1 || th > 3) return stbi__err("bad DHT header" ,"Corrupt JPEG" ); |
| 3292 | for (i=0; i < 16; ++i) { |
| 3293 | sizes[i] = stbi__get8(z->s); |
| 3294 | n += sizes[i]; |
| 3295 | } |
| 3296 | if(n > 256) return stbi__err("bad DHT header" ,"Corrupt JPEG" ); // Loop over i < n would write past end of values! |
| 3297 | L -= 17; |
| 3298 | if (tc == 0) { |
| 3299 | if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0; |
| 3300 | v = z->huff_dc[th].values; |
| 3301 | } else { |
| 3302 | if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0; |
| 3303 | v = z->huff_ac[th].values; |
| 3304 | } |
| 3305 | for (i=0; i < n; ++i) |
| 3306 | v[i] = stbi__get8(z->s); |
| 3307 | if (tc != 0) |
| 3308 | stbi__build_fast_ac(z->fast_ac[th], z->huff_ac + th); |
| 3309 | L -= n; |
| 3310 | } |
| 3311 | return L==0; |
| 3312 | } |
| 3313 | |
| 3314 | // check for comment block or APP blocks |
| 3315 | if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) { |
| 3316 | L = stbi__get16be(z->s); |
| 3317 | if (L < 2) { |
| 3318 | if (m == 0xFE) |
| 3319 | return stbi__err("bad COM len" ,"Corrupt JPEG" ); |
| 3320 | else |
| 3321 | return stbi__err("bad APP len" ,"Corrupt JPEG" ); |
| 3322 | } |
| 3323 | L -= 2; |
| 3324 | |
| 3325 | if (m == 0xE0 && L >= 5) { // JFIF APP0 segment |
| 3326 | static const unsigned char tag[5] = {'J','F','I','F','\0'}; |
| 3327 | int ok = 1; |
| 3328 | int i; |
| 3329 | for (i=0; i < 5; ++i) |
| 3330 | if (stbi__get8(z->s) != tag[i]) |
| 3331 | ok = 0; |
| 3332 | L -= 5; |
| 3333 | if (ok) |
| 3334 | z->jfif = 1; |
| 3335 | } else if (m == 0xEE && L >= 12) { // Adobe APP14 segment |
| 3336 | static const unsigned char tag[6] = {'A','d','o','b','e','\0'}; |
| 3337 | int ok = 1; |
| 3338 | int i; |
| 3339 | for (i=0; i < 6; ++i) |
| 3340 | if (stbi__get8(z->s) != tag[i]) |
| 3341 | ok = 0; |
| 3342 | L -= 6; |
| 3343 | if (ok) { |
| 3344 | stbi__get8(z->s); // version |
| 3345 | stbi__get16be(z->s); // flags0 |
| 3346 | stbi__get16be(z->s); // flags1 |
| 3347 | z->app14_color_transform = stbi__get8(z->s); // color transform |
| 3348 | L -= 6; |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | stbi__skip(z->s, L); |
| 3353 | return 1; |
| 3354 | } |
| 3355 | |
| 3356 | return stbi__err("unknown marker" ,"Corrupt JPEG" ); |
| 3357 | } |
| 3358 | |
| 3359 | // after we see SOS |
| 3360 | static int (stbi__jpeg *z) |
| 3361 | { |
| 3362 | int i; |
| 3363 | int Ls = stbi__get16be(z->s); |
| 3364 | z->scan_n = stbi__get8(z->s); |
| 3365 | 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" ); |
| 3366 | if (Ls != 6+2*z->scan_n) return stbi__err("bad SOS len" ,"Corrupt JPEG" ); |
| 3367 | for (i=0; i < z->scan_n; ++i) { |
| 3368 | int id = stbi__get8(z->s), which; |
| 3369 | int q = stbi__get8(z->s); |
| 3370 | for (which = 0; which < z->s->img_n; ++which) |
| 3371 | if (z->img_comp[which].id == id) |
| 3372 | break; |
| 3373 | if (which == z->s->img_n) return 0; // no match |
| 3374 | z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return stbi__err("bad DC huff" ,"Corrupt JPEG" ); |
| 3375 | z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return stbi__err("bad AC huff" ,"Corrupt JPEG" ); |
| 3376 | z->order[i] = which; |
| 3377 | } |
| 3378 | |
| 3379 | { |
| 3380 | int aa; |
| 3381 | z->spec_start = stbi__get8(z->s); |
| 3382 | z->spec_end = stbi__get8(z->s); // should be 63, but might be 0 |
| 3383 | aa = stbi__get8(z->s); |
| 3384 | z->succ_high = (aa >> 4); |
| 3385 | z->succ_low = (aa & 15); |
| 3386 | if (z->progressive) { |
| 3387 | if (z->spec_start > 63 || z->spec_end > 63 || z->spec_start > z->spec_end || z->succ_high > 13 || z->succ_low > 13) |
| 3388 | return stbi__err("bad SOS" , "Corrupt JPEG" ); |
| 3389 | } else { |
| 3390 | if (z->spec_start != 0) return stbi__err("bad SOS" ,"Corrupt JPEG" ); |
| 3391 | if (z->succ_high != 0 || z->succ_low != 0) return stbi__err("bad SOS" ,"Corrupt JPEG" ); |
| 3392 | z->spec_end = 63; |
| 3393 | } |
| 3394 | } |
| 3395 | |
| 3396 | return 1; |
| 3397 | } |
| 3398 | |
| 3399 | static int stbi__free_jpeg_components(stbi__jpeg *z, int ncomp, int why) |
| 3400 | { |
| 3401 | int i; |
| 3402 | for (i=0; i < ncomp; ++i) { |
| 3403 | if (z->img_comp[i].raw_data) { |
| 3404 | STBI_FREE(z->img_comp[i].raw_data); |
| 3405 | z->img_comp[i].raw_data = NULL; |
| 3406 | z->img_comp[i].data = NULL; |
| 3407 | } |
| 3408 | if (z->img_comp[i].raw_coeff) { |
| 3409 | STBI_FREE(z->img_comp[i].raw_coeff); |
| 3410 | z->img_comp[i].raw_coeff = 0; |
| 3411 | z->img_comp[i].coeff = 0; |
| 3412 | } |
| 3413 | if (z->img_comp[i].linebuf) { |
| 3414 | STBI_FREE(z->img_comp[i].linebuf); |
| 3415 | z->img_comp[i].linebuf = NULL; |
| 3416 | } |
| 3417 | } |
| 3418 | return why; |
| 3419 | } |
| 3420 | |
| 3421 | static int (stbi__jpeg *z, int scan) |
| 3422 | { |
| 3423 | stbi__context *s = z->s; |
| 3424 | int Lf,p,i,q, h_max=1,v_max=1,c; |
| 3425 | Lf = stbi__get16be(s); if (Lf < 11) return stbi__err("bad SOF len" ,"Corrupt JPEG" ); // JPEG |
| 3426 | p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit" ,"JPEG format not supported: 8-bit only" ); // JPEG baseline |
| 3427 | 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 |
| 3428 | s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width" ,"Corrupt JPEG" ); // JPEG requires |
| 3429 | if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large" ,"Very large image (corrupt?)" ); |
| 3430 | if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large" ,"Very large image (corrupt?)" ); |
| 3431 | c = stbi__get8(s); |
| 3432 | if (c != 3 && c != 1 && c != 4) return stbi__err("bad component count" ,"Corrupt JPEG" ); |
| 3433 | s->img_n = c; |
| 3434 | for (i=0; i < c; ++i) { |
| 3435 | z->img_comp[i].data = NULL; |
| 3436 | z->img_comp[i].linebuf = NULL; |
| 3437 | } |
| 3438 | |
| 3439 | if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len" ,"Corrupt JPEG" ); |
| 3440 | |
| 3441 | z->rgb = 0; |
| 3442 | for (i=0; i < s->img_n; ++i) { |
| 3443 | static const unsigned char rgb[3] = { 'R', 'G', 'B' }; |
| 3444 | z->img_comp[i].id = stbi__get8(s); |
| 3445 | if (s->img_n == 3 && z->img_comp[i].id == rgb[i]) |
| 3446 | ++z->rgb; |
| 3447 | q = stbi__get8(s); |
| 3448 | 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" ); |
| 3449 | 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" ); |
| 3450 | z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ" ,"Corrupt JPEG" ); |
| 3451 | } |
| 3452 | |
| 3453 | if (scan != STBI__SCAN_load) return 1; |
| 3454 | |
| 3455 | if (!stbi__mad3sizes_valid(s->img_x, s->img_y, s->img_n, 0)) return stbi__err("too large" , "Image too large to decode" ); |
| 3456 | |
| 3457 | for (i=0; i < s->img_n; ++i) { |
| 3458 | if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h; |
| 3459 | if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v; |
| 3460 | } |
| 3461 | |
| 3462 | // check that plane subsampling factors are integer ratios; our resamplers can't deal with fractional ratios |
| 3463 | // and I've never seen a non-corrupted JPEG file actually use them |
| 3464 | for (i=0; i < s->img_n; ++i) { |
| 3465 | if (h_max % z->img_comp[i].h != 0) return stbi__err("bad H" ,"Corrupt JPEG" ); |
| 3466 | if (v_max % z->img_comp[i].v != 0) return stbi__err("bad V" ,"Corrupt JPEG" ); |
| 3467 | } |
| 3468 | |
| 3469 | // compute interleaved mcu info |
| 3470 | z->img_h_max = h_max; |
| 3471 | z->img_v_max = v_max; |
| 3472 | z->img_mcu_w = h_max * 8; |
| 3473 | z->img_mcu_h = v_max * 8; |
| 3474 | // these sizes can't be more than 17 bits |
| 3475 | z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w; |
| 3476 | z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h; |
| 3477 | |
| 3478 | for (i=0; i < s->img_n; ++i) { |
| 3479 | // number of effective pixels (e.g. for non-interleaved MCU) |
| 3480 | z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max; |
| 3481 | z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max; |
| 3482 | // to simplify generation, we'll allocate enough memory to decode |
| 3483 | // the bogus oversized data from using interleaved MCUs and their |
| 3484 | // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't |
| 3485 | // discard the extra data until colorspace conversion |
| 3486 | // |
| 3487 | // img_mcu_x, img_mcu_y: <=17 bits; comp[i].h and .v are <=4 (checked earlier) |
| 3488 | // so these muls can't overflow with 32-bit ints (which we require) |
| 3489 | z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8; |
| 3490 | z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8; |
| 3491 | z->img_comp[i].coeff = 0; |
| 3492 | z->img_comp[i].raw_coeff = 0; |
| 3493 | z->img_comp[i].linebuf = NULL; |
| 3494 | z->img_comp[i].raw_data = stbi__malloc_mad2(z->img_comp[i].w2, z->img_comp[i].h2, 15); |
| 3495 | if (z->img_comp[i].raw_data == NULL) |
| 3496 | return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem" , "Out of memory" )); |
| 3497 | // align blocks for idct using mmx/sse |
| 3498 | z->img_comp[i].data = (stbi_uc*) (((size_t) z->img_comp[i].raw_data + 15) & ~15); |
| 3499 | if (z->progressive) { |
| 3500 | // w2, h2 are multiples of 8 (see above) |
| 3501 | z->img_comp[i].coeff_w = z->img_comp[i].w2 / 8; |
| 3502 | z->img_comp[i].coeff_h = z->img_comp[i].h2 / 8; |
| 3503 | z->img_comp[i].raw_coeff = stbi__malloc_mad3(z->img_comp[i].w2, z->img_comp[i].h2, sizeof(short), 15); |
| 3504 | if (z->img_comp[i].raw_coeff == NULL) |
| 3505 | return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem" , "Out of memory" )); |
| 3506 | z->img_comp[i].coeff = (short*) (((size_t) z->img_comp[i].raw_coeff + 15) & ~15); |
| 3507 | } |
| 3508 | } |
| 3509 | |
| 3510 | return 1; |
| 3511 | } |
| 3512 | |
| 3513 | // use comparisons since in some cases we handle more than one case (e.g. SOF) |
| 3514 | #define stbi__DNL(x) ((x) == 0xdc) |
| 3515 | #define stbi__SOI(x) ((x) == 0xd8) |
| 3516 | #define stbi__EOI(x) ((x) == 0xd9) |
| 3517 | #define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2) |
| 3518 | #define stbi__SOS(x) ((x) == 0xda) |
| 3519 | |
| 3520 | #define stbi__SOF_progressive(x) ((x) == 0xc2) |
| 3521 | |
| 3522 | static int (stbi__jpeg *z, int scan) |
| 3523 | { |
| 3524 | int m; |
| 3525 | z->jfif = 0; |
| 3526 | z->app14_color_transform = -1; // valid values are 0,1,2 |
| 3527 | z->marker = STBI__MARKER_none; // initialize cached marker to empty |
| 3528 | m = stbi__get_marker(z); |
| 3529 | if (!stbi__SOI(m)) return stbi__err("no SOI" ,"Corrupt JPEG" ); |
| 3530 | if (scan == STBI__SCAN_type) return 1; |
| 3531 | m = stbi__get_marker(z); |
| 3532 | while (!stbi__SOF(m)) { |
| 3533 | if (!stbi__process_marker(z,m)) return 0; |
| 3534 | m = stbi__get_marker(z); |
| 3535 | while (m == STBI__MARKER_none) { |
| 3536 | // some files have extra padding after their blocks, so ok, we'll scan |
| 3537 | if (stbi__at_eof(z->s)) return stbi__err("no SOF" , "Corrupt JPEG" ); |
| 3538 | m = stbi__get_marker(z); |
| 3539 | } |
| 3540 | } |
| 3541 | z->progressive = stbi__SOF_progressive(m); |
| 3542 | if (!stbi__process_frame_header(z, scan)) return 0; |
| 3543 | return 1; |
| 3544 | } |
| 3545 | |
| 3546 | static stbi_uc stbi__skip_jpeg_junk_at_end(stbi__jpeg *j) |
| 3547 | { |
| 3548 | // some JPEGs have junk at end, skip over it but if we find what looks |
| 3549 | // like a valid marker, resume there |
| 3550 | while (!stbi__at_eof(j->s)) { |
| 3551 | stbi_uc x = stbi__get8(j->s); |
| 3552 | while (x == 0xff) { // might be a marker |
| 3553 | if (stbi__at_eof(j->s)) return STBI__MARKER_none; |
| 3554 | x = stbi__get8(j->s); |
| 3555 | if (x != 0x00 && x != 0xff) { |
| 3556 | // not a stuffed zero or lead-in to another marker, looks |
| 3557 | // like an actual marker, return it |
| 3558 | return x; |
| 3559 | } |
| 3560 | // stuffed zero has x=0 now which ends the loop, meaning we go |
| 3561 | // back to regular scan loop. |
| 3562 | // repeated 0xff keeps trying to read the next byte of the marker. |
| 3563 | } |
| 3564 | } |
| 3565 | return STBI__MARKER_none; |
| 3566 | } |
| 3567 | |
| 3568 | // decode image to YCbCr format |
| 3569 | static int stbi__decode_jpeg_image(stbi__jpeg *j) |
| 3570 | { |
| 3571 | int m; |
| 3572 | for (m = 0; m < 4; m++) { |
| 3573 | j->img_comp[m].raw_data = NULL; |
| 3574 | j->img_comp[m].raw_coeff = NULL; |
| 3575 | } |
| 3576 | j->restart_interval = 0; |
| 3577 | if (!stbi__decode_jpeg_header(j, STBI__SCAN_load)) return 0; |
| 3578 | m = stbi__get_marker(j); |
| 3579 | while (!stbi__EOI(m)) { |
| 3580 | if (stbi__SOS(m)) { |
| 3581 | if (!stbi__process_scan_header(j)) return 0; |
| 3582 | if (!stbi__parse_entropy_coded_data(j)) return 0; |
| 3583 | if (j->marker == STBI__MARKER_none ) { |
| 3584 | j->marker = stbi__skip_jpeg_junk_at_end(j); |
| 3585 | // if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0 |
| 3586 | } |
| 3587 | m = stbi__get_marker(j); |
| 3588 | if (STBI__RESTART(m)) |
| 3589 | m = stbi__get_marker(j); |
| 3590 | } else if (stbi__DNL(m)) { |
| 3591 | int Ld = stbi__get16be(j->s); |
| 3592 | stbi__uint32 NL = stbi__get16be(j->s); |
| 3593 | if (Ld != 4) return stbi__err("bad DNL len" , "Corrupt JPEG" ); |
| 3594 | if (NL != j->s->img_y) return stbi__err("bad DNL height" , "Corrupt JPEG" ); |
| 3595 | m = stbi__get_marker(j); |
| 3596 | } else { |
| 3597 | if (!stbi__process_marker(j, m)) return 1; |
| 3598 | m = stbi__get_marker(j); |
| 3599 | } |
| 3600 | } |
| 3601 | if (j->progressive) |
| 3602 | stbi__jpeg_finish(j); |
| 3603 | return 1; |
| 3604 | } |
| 3605 | |
| 3606 | // static jfif-centered resampling (across block boundaries) |
| 3607 | |
| 3608 | typedef stbi_uc *(*resample_row_func)(stbi_uc *out, stbi_uc *in0, stbi_uc *in1, |
| 3609 | int w, int hs); |
| 3610 | |
| 3611 | #define stbi__div4(x) ((stbi_uc) ((x) >> 2)) |
| 3612 | |
| 3613 | static stbi_uc *resample_row_1(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) |
| 3614 | { |
| 3615 | STBI_NOTUSED(out); |
| 3616 | STBI_NOTUSED(in_far); |
| 3617 | STBI_NOTUSED(w); |
| 3618 | STBI_NOTUSED(hs); |
| 3619 | return in_near; |
| 3620 | } |
| 3621 | |
| 3622 | static stbi_uc* stbi__resample_row_v_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) |
| 3623 | { |
| 3624 | // need to generate two samples vertically for every one in input |
| 3625 | int i; |
| 3626 | STBI_NOTUSED(hs); |
| 3627 | for (i=0; i < w; ++i) |
| 3628 | out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2); |
| 3629 | return out; |
| 3630 | } |
| 3631 | |
| 3632 | static stbi_uc* stbi__resample_row_h_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) |
| 3633 | { |
| 3634 | // need to generate two samples horizontally for every one in input |
| 3635 | int i; |
| 3636 | stbi_uc *input = in_near; |
| 3637 | |
| 3638 | if (w == 1) { |
| 3639 | // if only one sample, can't do any interpolation |
| 3640 | out[0] = out[1] = input[0]; |
| 3641 | return out; |
| 3642 | } |
| 3643 | |
| 3644 | out[0] = input[0]; |
| 3645 | out[1] = stbi__div4(input[0]*3 + input[1] + 2); |
| 3646 | for (i=1; i < w-1; ++i) { |
| 3647 | int n = 3*input[i]+2; |
| 3648 | out[i*2+0] = stbi__div4(n+input[i-1]); |
| 3649 | out[i*2+1] = stbi__div4(n+input[i+1]); |
| 3650 | } |
| 3651 | out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2); |
| 3652 | out[i*2+1] = input[w-1]; |
| 3653 | |
| 3654 | STBI_NOTUSED(in_far); |
| 3655 | STBI_NOTUSED(hs); |
| 3656 | |
| 3657 | return out; |
| 3658 | } |
| 3659 | |
| 3660 | #define stbi__div16(x) ((stbi_uc) ((x) >> 4)) |
| 3661 | |
| 3662 | static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) |
| 3663 | { |
| 3664 | // need to generate 2x2 samples for every one in input |
| 3665 | int i,t0,t1; |
| 3666 | if (w == 1) { |
| 3667 | out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2); |
| 3668 | return out; |
| 3669 | } |
| 3670 | |
| 3671 | t1 = 3*in_near[0] + in_far[0]; |
| 3672 | out[0] = stbi__div4(t1+2); |
| 3673 | for (i=1; i < w; ++i) { |
| 3674 | t0 = t1; |
| 3675 | t1 = 3*in_near[i]+in_far[i]; |
| 3676 | out[i*2-1] = stbi__div16(3*t0 + t1 + 8); |
| 3677 | out[i*2 ] = stbi__div16(3*t1 + t0 + 8); |
| 3678 | } |
| 3679 | out[w*2-1] = stbi__div4(t1+2); |
| 3680 | |
| 3681 | STBI_NOTUSED(hs); |
| 3682 | |
| 3683 | return out; |
| 3684 | } |
| 3685 | |
| 3686 | #if defined(STBI_SSE2) || defined(STBI_NEON) |
| 3687 | static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) |
| 3688 | { |
| 3689 | // need to generate 2x2 samples for every one in input |
| 3690 | int i=0,t0,t1; |
| 3691 | |
| 3692 | if (w == 1) { |
| 3693 | out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2); |
| 3694 | return out; |
| 3695 | } |
| 3696 | |
| 3697 | t1 = 3*in_near[0] + in_far[0]; |
| 3698 | // process groups of 8 pixels for as long as we can. |
| 3699 | // note we can't handle the last pixel in a row in this loop |
| 3700 | // because we need to handle the filter boundary conditions. |
| 3701 | for (; i < ((w-1) & ~7); i += 8) { |
| 3702 | #if defined(STBI_SSE2) |
| 3703 | // load and perform the vertical filtering pass |
| 3704 | // this uses 3*x + y = 4*x + (y - x) |
| 3705 | __m128i zero = _mm_setzero_si128(); |
| 3706 | __m128i farb = _mm_loadl_epi64((__m128i *) (in_far + i)); |
| 3707 | __m128i nearb = _mm_loadl_epi64((__m128i *) (in_near + i)); |
| 3708 | __m128i farw = _mm_unpacklo_epi8(farb, zero); |
| 3709 | __m128i nearw = _mm_unpacklo_epi8(nearb, zero); |
| 3710 | __m128i diff = _mm_sub_epi16(farw, nearw); |
| 3711 | __m128i nears = _mm_slli_epi16(nearw, 2); |
| 3712 | __m128i curr = _mm_add_epi16(nears, diff); // current row |
| 3713 | |
| 3714 | // horizontal filter works the same based on shifted vers of current |
| 3715 | // row. "prev" is current row shifted right by 1 pixel; we need to |
| 3716 | // insert the previous pixel value (from t1). |
| 3717 | // "next" is current row shifted left by 1 pixel, with first pixel |
| 3718 | // of next block of 8 pixels added in. |
| 3719 | __m128i prv0 = _mm_slli_si128(curr, 2); |
| 3720 | __m128i nxt0 = _mm_srli_si128(curr, 2); |
| 3721 | __m128i prev = _mm_insert_epi16(prv0, t1, 0); |
| 3722 | __m128i next = _mm_insert_epi16(nxt0, 3*in_near[i+8] + in_far[i+8], 7); |
| 3723 | |
| 3724 | // horizontal filter, polyphase implementation since it's convenient: |
| 3725 | // even pixels = 3*cur + prev = cur*4 + (prev - cur) |
| 3726 | // odd pixels = 3*cur + next = cur*4 + (next - cur) |
| 3727 | // note the shared term. |
| 3728 | __m128i bias = _mm_set1_epi16(8); |
| 3729 | __m128i curs = _mm_slli_epi16(curr, 2); |
| 3730 | __m128i prvd = _mm_sub_epi16(prev, curr); |
| 3731 | __m128i nxtd = _mm_sub_epi16(next, curr); |
| 3732 | __m128i curb = _mm_add_epi16(curs, bias); |
| 3733 | __m128i even = _mm_add_epi16(prvd, curb); |
| 3734 | __m128i odd = _mm_add_epi16(nxtd, curb); |
| 3735 | |
| 3736 | // interleave even and odd pixels, then undo scaling. |
| 3737 | __m128i int0 = _mm_unpacklo_epi16(even, odd); |
| 3738 | __m128i int1 = _mm_unpackhi_epi16(even, odd); |
| 3739 | __m128i de0 = _mm_srli_epi16(int0, 4); |
| 3740 | __m128i de1 = _mm_srli_epi16(int1, 4); |
| 3741 | |
| 3742 | // pack and write output |
| 3743 | __m128i outv = _mm_packus_epi16(de0, de1); |
| 3744 | _mm_storeu_si128((__m128i *) (out + i*2), outv); |
| 3745 | #elif defined(STBI_NEON) |
| 3746 | // load and perform the vertical filtering pass |
| 3747 | // this uses 3*x + y = 4*x + (y - x) |
| 3748 | uint8x8_t farb = vld1_u8(in_far + i); |
| 3749 | uint8x8_t nearb = vld1_u8(in_near + i); |
| 3750 | int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb)); |
| 3751 | int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2)); |
| 3752 | int16x8_t curr = vaddq_s16(nears, diff); // current row |
| 3753 | |
| 3754 | // horizontal filter works the same based on shifted vers of current |
| 3755 | // row. "prev" is current row shifted right by 1 pixel; we need to |
| 3756 | // insert the previous pixel value (from t1). |
| 3757 | // "next" is current row shifted left by 1 pixel, with first pixel |
| 3758 | // of next block of 8 pixels added in. |
| 3759 | int16x8_t prv0 = vextq_s16(curr, curr, 7); |
| 3760 | int16x8_t nxt0 = vextq_s16(curr, curr, 1); |
| 3761 | int16x8_t prev = vsetq_lane_s16(t1, prv0, 0); |
| 3762 | int16x8_t next = vsetq_lane_s16(3*in_near[i+8] + in_far[i+8], nxt0, 7); |
| 3763 | |
| 3764 | // horizontal filter, polyphase implementation since it's convenient: |
| 3765 | // even pixels = 3*cur + prev = cur*4 + (prev - cur) |
| 3766 | // odd pixels = 3*cur + next = cur*4 + (next - cur) |
| 3767 | // note the shared term. |
| 3768 | int16x8_t curs = vshlq_n_s16(curr, 2); |
| 3769 | int16x8_t prvd = vsubq_s16(prev, curr); |
| 3770 | int16x8_t nxtd = vsubq_s16(next, curr); |
| 3771 | int16x8_t even = vaddq_s16(curs, prvd); |
| 3772 | int16x8_t odd = vaddq_s16(curs, nxtd); |
| 3773 | |
| 3774 | // undo scaling and round, then store with even/odd phases interleaved |
| 3775 | uint8x8x2_t o; |
| 3776 | o.val[0] = vqrshrun_n_s16(even, 4); |
| 3777 | o.val[1] = vqrshrun_n_s16(odd, 4); |
| 3778 | vst2_u8(out + i*2, o); |
| 3779 | #endif |
| 3780 | |
| 3781 | // "previous" value for next iter |
| 3782 | t1 = 3*in_near[i+7] + in_far[i+7]; |
| 3783 | } |
| 3784 | |
| 3785 | t0 = t1; |
| 3786 | t1 = 3*in_near[i] + in_far[i]; |
| 3787 | out[i*2] = stbi__div16(3*t1 + t0 + 8); |
| 3788 | |
| 3789 | for (++i; i < w; ++i) { |
| 3790 | t0 = t1; |
| 3791 | t1 = 3*in_near[i]+in_far[i]; |
| 3792 | out[i*2-1] = stbi__div16(3*t0 + t1 + 8); |
| 3793 | out[i*2 ] = stbi__div16(3*t1 + t0 + 8); |
| 3794 | } |
| 3795 | out[w*2-1] = stbi__div4(t1+2); |
| 3796 | |
| 3797 | STBI_NOTUSED(hs); |
| 3798 | |
| 3799 | return out; |
| 3800 | } |
| 3801 | #endif |
| 3802 | |
| 3803 | static stbi_uc *stbi__resample_row_generic(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) |
| 3804 | { |
| 3805 | // resample with nearest-neighbor |
| 3806 | int i,j; |
| 3807 | STBI_NOTUSED(in_far); |
| 3808 | for (i=0; i < w; ++i) |
| 3809 | for (j=0; j < hs; ++j) |
| 3810 | out[i*hs+j] = in_near[i]; |
| 3811 | return out; |
| 3812 | } |
| 3813 | |
| 3814 | // this is a reduced-precision calculation of YCbCr-to-RGB introduced |
| 3815 | // to make sure the code produces the same results in both SIMD and scalar |
| 3816 | #define stbi__float2fixed(x) (((int) ((x) * 4096.0f + 0.5f)) << 8) |
| 3817 | static 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) |
| 3818 | { |
| 3819 | int i; |
| 3820 | for (i=0; i < count; ++i) { |
| 3821 | int y_fixed = (y[i] << 20) + (1<<19); // rounding |
| 3822 | int r,g,b; |
| 3823 | int cr = pcr[i] - 128; |
| 3824 | int cb = pcb[i] - 128; |
| 3825 | r = y_fixed + cr* stbi__float2fixed(1.40200f); |
| 3826 | g = y_fixed + (cr*-stbi__float2fixed(0.71414f)) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000); |
| 3827 | b = y_fixed + cb* stbi__float2fixed(1.77200f); |
| 3828 | r >>= 20; |
| 3829 | g >>= 20; |
| 3830 | b >>= 20; |
| 3831 | if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; } |
| 3832 | if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; } |
| 3833 | if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; } |
| 3834 | out[0] = (stbi_uc)r; |
| 3835 | out[1] = (stbi_uc)g; |
| 3836 | out[2] = (stbi_uc)b; |
| 3837 | out[3] = 255; |
| 3838 | out += step; |
| 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | #if defined(STBI_SSE2) || defined(STBI_NEON) |
| 3843 | static void stbi__YCbCr_to_RGB_simd(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step) |
| 3844 | { |
| 3845 | int i = 0; |
| 3846 | |
| 3847 | #ifdef STBI_SSE2 |
| 3848 | // step == 3 is pretty ugly on the final interleave, and i'm not convinced |
| 3849 | // it's useful in practice (you wouldn't use it for textures, for example). |
| 3850 | // so just accelerate step == 4 case. |
| 3851 | if (step == 4) { |
| 3852 | // this is a fairly straightforward implementation and not super-optimized. |
| 3853 | __m128i signflip = _mm_set1_epi8(-0x80); |
| 3854 | __m128i cr_const0 = _mm_set1_epi16( (short) ( 1.40200f*4096.0f+0.5f)); |
| 3855 | __m128i cr_const1 = _mm_set1_epi16( - (short) ( 0.71414f*4096.0f+0.5f)); |
| 3856 | __m128i cb_const0 = _mm_set1_epi16( - (short) ( 0.34414f*4096.0f+0.5f)); |
| 3857 | __m128i cb_const1 = _mm_set1_epi16( (short) ( 1.77200f*4096.0f+0.5f)); |
| 3858 | __m128i y_bias = _mm_set1_epi8((char) (unsigned char) 128); |
| 3859 | __m128i xw = _mm_set1_epi16(255); // alpha channel |
| 3860 | |
| 3861 | for (; i+7 < count; i += 8) { |
| 3862 | // load |
| 3863 | __m128i y_bytes = _mm_loadl_epi64((__m128i *) (y+i)); |
| 3864 | __m128i cr_bytes = _mm_loadl_epi64((__m128i *) (pcr+i)); |
| 3865 | __m128i cb_bytes = _mm_loadl_epi64((__m128i *) (pcb+i)); |
| 3866 | __m128i cr_biased = _mm_xor_si128(cr_bytes, signflip); // -128 |
| 3867 | __m128i cb_biased = _mm_xor_si128(cb_bytes, signflip); // -128 |
| 3868 | |
| 3869 | // unpack to short (and left-shift cr, cb by 8) |
| 3870 | __m128i yw = _mm_unpacklo_epi8(y_bias, y_bytes); |
| 3871 | __m128i crw = _mm_unpacklo_epi8(_mm_setzero_si128(), cr_biased); |
| 3872 | __m128i cbw = _mm_unpacklo_epi8(_mm_setzero_si128(), cb_biased); |
| 3873 | |
| 3874 | // color transform |
| 3875 | __m128i yws = _mm_srli_epi16(yw, 4); |
| 3876 | __m128i cr0 = _mm_mulhi_epi16(cr_const0, crw); |
| 3877 | __m128i cb0 = _mm_mulhi_epi16(cb_const0, cbw); |
| 3878 | __m128i cb1 = _mm_mulhi_epi16(cbw, cb_const1); |
| 3879 | __m128i cr1 = _mm_mulhi_epi16(crw, cr_const1); |
| 3880 | __m128i rws = _mm_add_epi16(cr0, yws); |
| 3881 | __m128i gwt = _mm_add_epi16(cb0, yws); |
| 3882 | __m128i bws = _mm_add_epi16(yws, cb1); |
| 3883 | __m128i gws = _mm_add_epi16(gwt, cr1); |
| 3884 | |
| 3885 | // descale |
| 3886 | __m128i rw = _mm_srai_epi16(rws, 4); |
| 3887 | __m128i bw = _mm_srai_epi16(bws, 4); |
| 3888 | __m128i gw = _mm_srai_epi16(gws, 4); |
| 3889 | |
| 3890 | // back to byte, set up for transpose |
| 3891 | __m128i brb = _mm_packus_epi16(rw, bw); |
| 3892 | __m128i gxb = _mm_packus_epi16(gw, xw); |
| 3893 | |
| 3894 | // transpose to interleave channels |
| 3895 | __m128i t0 = _mm_unpacklo_epi8(brb, gxb); |
| 3896 | __m128i t1 = _mm_unpackhi_epi8(brb, gxb); |
| 3897 | __m128i o0 = _mm_unpacklo_epi16(t0, t1); |
| 3898 | __m128i o1 = _mm_unpackhi_epi16(t0, t1); |
| 3899 | |
| 3900 | // store |
| 3901 | _mm_storeu_si128((__m128i *) (out + 0), o0); |
| 3902 | _mm_storeu_si128((__m128i *) (out + 16), o1); |
| 3903 | out += 32; |
| 3904 | } |
| 3905 | } |
| 3906 | #endif |
| 3907 | |
| 3908 | #ifdef STBI_NEON |
| 3909 | // in this version, step=3 support would be easy to add. but is there demand? |
| 3910 | if (step == 4) { |
| 3911 | // this is a fairly straightforward implementation and not super-optimized. |
| 3912 | uint8x8_t signflip = vdup_n_u8(0x80); |
| 3913 | int16x8_t cr_const0 = vdupq_n_s16( (short) ( 1.40200f*4096.0f+0.5f)); |
| 3914 | int16x8_t cr_const1 = vdupq_n_s16( - (short) ( 0.71414f*4096.0f+0.5f)); |
| 3915 | int16x8_t cb_const0 = vdupq_n_s16( - (short) ( 0.34414f*4096.0f+0.5f)); |
| 3916 | int16x8_t cb_const1 = vdupq_n_s16( (short) ( 1.77200f*4096.0f+0.5f)); |
| 3917 | |
| 3918 | for (; i+7 < count; i += 8) { |
| 3919 | // load |
| 3920 | uint8x8_t y_bytes = vld1_u8(y + i); |
| 3921 | uint8x8_t cr_bytes = vld1_u8(pcr + i); |
| 3922 | uint8x8_t cb_bytes = vld1_u8(pcb + i); |
| 3923 | int8x8_t cr_biased = vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip)); |
| 3924 | int8x8_t cb_biased = vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip)); |
| 3925 | |
| 3926 | // expand to s16 |
| 3927 | int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4)); |
| 3928 | int16x8_t crw = vshll_n_s8(cr_biased, 7); |
| 3929 | int16x8_t cbw = vshll_n_s8(cb_biased, 7); |
| 3930 | |
| 3931 | // color transform |
| 3932 | int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0); |
| 3933 | int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0); |
| 3934 | int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1); |
| 3935 | int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1); |
| 3936 | int16x8_t rws = vaddq_s16(yws, cr0); |
| 3937 | int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1); |
| 3938 | int16x8_t bws = vaddq_s16(yws, cb1); |
| 3939 | |
| 3940 | // undo scaling, round, convert to byte |
| 3941 | uint8x8x4_t o; |
| 3942 | o.val[0] = vqrshrun_n_s16(rws, 4); |
| 3943 | o.val[1] = vqrshrun_n_s16(gws, 4); |
| 3944 | o.val[2] = vqrshrun_n_s16(bws, 4); |
| 3945 | o.val[3] = vdup_n_u8(255); |
| 3946 | |
| 3947 | // store, interleaving r/g/b/a |
| 3948 | vst4_u8(out, o); |
| 3949 | out += 8*4; |
| 3950 | } |
| 3951 | } |
| 3952 | #endif |
| 3953 | |
| 3954 | for (; i < count; ++i) { |
| 3955 | int y_fixed = (y[i] << 20) + (1<<19); // rounding |
| 3956 | int r,g,b; |
| 3957 | int cr = pcr[i] - 128; |
| 3958 | int cb = pcb[i] - 128; |
| 3959 | r = y_fixed + cr* stbi__float2fixed(1.40200f); |
| 3960 | g = y_fixed + cr*-stbi__float2fixed(0.71414f) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000); |
| 3961 | b = y_fixed + cb* stbi__float2fixed(1.77200f); |
| 3962 | r >>= 20; |
| 3963 | g >>= 20; |
| 3964 | b >>= 20; |
| 3965 | if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; } |
| 3966 | if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; } |
| 3967 | if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; } |
| 3968 | out[0] = (stbi_uc)r; |
| 3969 | out[1] = (stbi_uc)g; |
| 3970 | out[2] = (stbi_uc)b; |
| 3971 | out[3] = 255; |
| 3972 | out += step; |
| 3973 | } |
| 3974 | } |
| 3975 | #endif |
| 3976 | |
| 3977 | // set up the kernels |
| 3978 | static void stbi__setup_jpeg(stbi__jpeg *j) |
| 3979 | { |
| 3980 | j->idct_block_kernel = stbi__idct_block; |
| 3981 | j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row; |
| 3982 | j->resample_row_hv_2_kernel = stbi__resample_row_hv_2; |
| 3983 | |
| 3984 | #ifdef STBI_SSE2 |
| 3985 | if (stbi__sse2_available()) { |
| 3986 | j->idct_block_kernel = stbi__idct_simd; |
| 3987 | j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd; |
| 3988 | j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd; |
| 3989 | } |
| 3990 | #endif |
| 3991 | |
| 3992 | #ifdef STBI_NEON |
| 3993 | if (SDL_HasNEON()) { /* SDL change */ |
| 3994 | j->idct_block_kernel = stbi__idct_simd; |
| 3995 | j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd; |
| 3996 | j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd; |
| 3997 | } /**/ |
| 3998 | #endif |
| 3999 | } |
| 4000 | |
| 4001 | // clean up the temporary component buffers |
| 4002 | static void stbi__cleanup_jpeg(stbi__jpeg *j) |
| 4003 | { |
| 4004 | stbi__free_jpeg_components(j, j->s->img_n, 0); |
| 4005 | } |
| 4006 | |
| 4007 | typedef struct |
| 4008 | { |
| 4009 | resample_row_func resample; |
| 4010 | stbi_uc *line0,*line1; |
| 4011 | int hs,vs; // expansion factor in each axis |
| 4012 | int w_lores; // horizontal pixels pre-expansion |
| 4013 | int ystep; // how far through vertical expansion we are |
| 4014 | int ypos; // which pre-expansion row we're on |
| 4015 | } stbi__resample; |
| 4016 | |
| 4017 | // fast 0..255 * 0..255 => 0..255 rounded multiplication |
| 4018 | static stbi_uc stbi__blinn_8x8(stbi_uc x, stbi_uc y) |
| 4019 | { |
| 4020 | unsigned int t = x*y + 128; |
| 4021 | return (stbi_uc) ((t + (t >>8)) >> 8); |
| 4022 | } |
| 4023 | |
| 4024 | static stbi_uc *output_jpeg_nv12(stbi__jpeg *z, stbi__nv12 *nv12) |
| 4025 | { |
| 4026 | unsigned int i,j; |
| 4027 | |
| 4028 | // Copy the Y plane |
| 4029 | if (nv12->pitch == (int)z->s->img_x) { |
| 4030 | memcpy(nv12->y, z->img_comp[0].data, z->s->img_y * z->s->img_x); |
| 4031 | } else { |
| 4032 | for (i=0; i < z->s->img_y; ++i) { |
| 4033 | memcpy(nv12->y + i * nv12->pitch, z->img_comp[0].data + i * z->s->img_x, z->s->img_x); |
| 4034 | } |
| 4035 | } |
| 4036 | |
| 4037 | if (z->s->img_n == 3) { |
| 4038 | // NV12: U and V are interleaved, each subsampled by 2 |
| 4039 | const int nv12_hs = 2; |
| 4040 | const int nv12_vs = 2; |
| 4041 | const int u_hs = (z->img_h_max / z->img_comp[1].h); |
| 4042 | const int u_vs = (z->img_v_max / z->img_comp[1].v); |
| 4043 | const int v_hs = (z->img_h_max / z->img_comp[2].h); |
| 4044 | const int v_vs = (z->img_v_max / z->img_comp[2].v); |
| 4045 | for (i=0; i < (z->s->img_y + 1) / 2; ++i) { |
| 4046 | stbi_uc *src_u = z->img_comp[1].data + i * (1 + (nv12_vs - u_vs)) * z->img_comp[1].x; |
| 4047 | stbi_uc *src_v = z->img_comp[2].data + i * (1 + (nv12_vs - v_vs)) * z->img_comp[2].x; |
| 4048 | stbi_uc *dst = nv12->uv + i * nv12->pitch; |
| 4049 | for (j=0; j < (z->s->img_x + 1) / 2; ++j) { |
| 4050 | *dst++ = *src_u; |
| 4051 | src_u += 1 + (nv12_hs - u_hs); |
| 4052 | *dst++ = *src_v; |
| 4053 | src_v += 1 + (nv12_hs - v_hs); |
| 4054 | } |
| 4055 | } |
| 4056 | } else { |
| 4057 | // Grayscale |
| 4058 | for (i=0; i < (z->s->img_y + 1) / 2; ++i) { |
| 4059 | memset(nv12->uv + i * nv12->pitch, 0x80808080, ((z->s->img_x + 1) / 2) * 2); |
| 4060 | } |
| 4061 | } |
| 4062 | |
| 4063 | return nv12->y; |
| 4064 | } |
| 4065 | |
| 4066 | static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp, stbi__nv12 *nv12) |
| 4067 | { |
| 4068 | int n, decode_n, is_rgb; |
| 4069 | z->s->img_n = 0; // make stbi__cleanup_jpeg safe |
| 4070 | |
| 4071 | // validate req_comp |
| 4072 | if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp" , "Internal error" ); |
| 4073 | |
| 4074 | // load a jpeg image from whichever source, but leave in YCbCr format |
| 4075 | if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; } |
| 4076 | |
| 4077 | // determine actual number of components to generate |
| 4078 | n = req_comp ? req_comp : z->s->img_n >= 3 ? 3 : 1; |
| 4079 | |
| 4080 | is_rgb = z->s->img_n == 3 && (z->rgb == 3 || (z->app14_color_transform == 0 && !z->jfif)); |
| 4081 | |
| 4082 | if (z->s->img_n == 3 && n < 3 && !is_rgb) |
| 4083 | decode_n = 1; |
| 4084 | else |
| 4085 | decode_n = z->s->img_n; |
| 4086 | |
| 4087 | // nothing to do if no components requested; check this now to avoid |
| 4088 | // accessing uninitialized coutput[0] later |
| 4089 | if (decode_n <= 0) { stbi__cleanup_jpeg(z); return NULL; } |
| 4090 | |
| 4091 | // resample and color-convert |
| 4092 | { |
| 4093 | int k; |
| 4094 | unsigned int i,j; |
| 4095 | stbi_uc *output; |
| 4096 | stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL }; |
| 4097 | |
| 4098 | stbi__resample res_comp[4]; |
| 4099 | |
| 4100 | if (nv12) { |
| 4101 | if (nv12->w != (int)z->s->img_x || nv12->h != (int)z->s->img_y) { |
| 4102 | stbi__cleanup_jpeg(z); |
| 4103 | return stbi__errpuc("badsize" , "Unexpected size" ); |
| 4104 | } |
| 4105 | |
| 4106 | if (is_rgb) { |
| 4107 | stbi__cleanup_jpeg(z); |
| 4108 | return stbi__errpuc("rgbtonv12" , "Can't convert RGB to NV12" ); |
| 4109 | } |
| 4110 | |
| 4111 | output = output_jpeg_nv12(z, nv12); |
| 4112 | } else { |
| 4113 | for (k=0; k < decode_n; ++k) { |
| 4114 | stbi__resample *r = &res_comp[k]; |
| 4115 | |
| 4116 | // allocate line buffer big enough for upsampling off the edges |
| 4117 | // with upsample factor of 4 |
| 4118 | z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3); |
| 4119 | if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem" , "Out of memory" ); } |
| 4120 | |
| 4121 | r->hs = z->img_h_max / z->img_comp[k].h; |
| 4122 | r->vs = z->img_v_max / z->img_comp[k].v; |
| 4123 | r->ystep = r->vs >> 1; |
| 4124 | r->w_lores = (z->s->img_x + r->hs-1) / r->hs; |
| 4125 | r->ypos = 0; |
| 4126 | r->line0 = r->line1 = z->img_comp[k].data; |
| 4127 | |
| 4128 | if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1; |
| 4129 | else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2; |
| 4130 | else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2; |
| 4131 | else if (r->hs == 2 && r->vs == 2) r->resample = z->resample_row_hv_2_kernel; |
| 4132 | else r->resample = stbi__resample_row_generic; |
| 4133 | } |
| 4134 | |
| 4135 | // can't error after this so, this is safe |
| 4136 | output = (stbi_uc *) stbi__malloc_mad3(n, z->s->img_x, z->s->img_y, 1); |
| 4137 | if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem" , "Out of memory" ); } |
| 4138 | |
| 4139 | // now go ahead and resample |
| 4140 | for (j=0; j < z->s->img_y; ++j) { |
| 4141 | stbi_uc *out = output + n * z->s->img_x * j; |
| 4142 | for (k=0; k < decode_n; ++k) { |
| 4143 | stbi__resample *r = &res_comp[k]; |
| 4144 | int y_bot = r->ystep >= (r->vs >> 1); |
| 4145 | coutput[k] = r->resample(z->img_comp[k].linebuf, |
| 4146 | y_bot ? r->line1 : r->line0, |
| 4147 | y_bot ? r->line0 : r->line1, |
| 4148 | r->w_lores, r->hs); |
| 4149 | if (++r->ystep >= r->vs) { |
| 4150 | r->ystep = 0; |
| 4151 | r->line0 = r->line1; |
| 4152 | if (++r->ypos < z->img_comp[k].y) |
| 4153 | r->line1 += z->img_comp[k].w2; |
| 4154 | } |
| 4155 | } |
| 4156 | if (n >= 3) { |
| 4157 | stbi_uc *y = coutput[0]; |
| 4158 | if (z->s->img_n == 3) { |
| 4159 | if (is_rgb) { |
| 4160 | for (i=0; i < z->s->img_x; ++i) { |
| 4161 | out[0] = y[i]; |
| 4162 | out[1] = coutput[1][i]; |
| 4163 | out[2] = coutput[2][i]; |
| 4164 | out[3] = 255; |
| 4165 | out += n; |
| 4166 | } |
| 4167 | } else { |
| 4168 | z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); |
| 4169 | } |
| 4170 | } else if (z->s->img_n == 4) { |
| 4171 | if (z->app14_color_transform == 0) { // CMYK |
| 4172 | for (i=0; i < z->s->img_x; ++i) { |
| 4173 | stbi_uc m = coutput[3][i]; |
| 4174 | out[0] = stbi__blinn_8x8(coutput[0][i], m); |
| 4175 | out[1] = stbi__blinn_8x8(coutput[1][i], m); |
| 4176 | out[2] = stbi__blinn_8x8(coutput[2][i], m); |
| 4177 | out[3] = 255; |
| 4178 | out += n; |
| 4179 | } |
| 4180 | } else if (z->app14_color_transform == 2) { // YCCK |
| 4181 | z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); |
| 4182 | for (i=0; i < z->s->img_x; ++i) { |
| 4183 | stbi_uc m = coutput[3][i]; |
| 4184 | out[0] = stbi__blinn_8x8(255 - out[0], m); |
| 4185 | out[1] = stbi__blinn_8x8(255 - out[1], m); |
| 4186 | out[2] = stbi__blinn_8x8(255 - out[2], m); |
| 4187 | out += n; |
| 4188 | } |
| 4189 | } else { // YCbCr + alpha? Ignore the fourth channel for now |
| 4190 | z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); |
| 4191 | } |
| 4192 | } else |
| 4193 | for (i=0; i < z->s->img_x; ++i) { |
| 4194 | out[0] = out[1] = out[2] = y[i]; |
| 4195 | out[3] = 255; // not used if n==3 |
| 4196 | out += n; |
| 4197 | } |
| 4198 | } else { |
| 4199 | if (is_rgb) { |
| 4200 | if (n == 1) |
| 4201 | for (i=0; i < z->s->img_x; ++i) |
| 4202 | *out++ = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]); |
| 4203 | else { |
| 4204 | for (i=0; i < z->s->img_x; ++i, out += 2) { |
| 4205 | out[0] = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]); |
| 4206 | out[1] = 255; |
| 4207 | } |
| 4208 | } |
| 4209 | } else if (z->s->img_n == 4 && z->app14_color_transform == 0) { |
| 4210 | for (i=0; i < z->s->img_x; ++i) { |
| 4211 | stbi_uc m = coutput[3][i]; |
| 4212 | stbi_uc r = stbi__blinn_8x8(coutput[0][i], m); |
| 4213 | stbi_uc g = stbi__blinn_8x8(coutput[1][i], m); |
| 4214 | stbi_uc b = stbi__blinn_8x8(coutput[2][i], m); |
| 4215 | out[0] = stbi__compute_y(r, g, b); |
| 4216 | out[1] = 255; |
| 4217 | out += n; |
| 4218 | } |
| 4219 | } else if (z->s->img_n == 4 && z->app14_color_transform == 2) { |
| 4220 | for (i=0; i < z->s->img_x; ++i) { |
| 4221 | out[0] = stbi__blinn_8x8(255 - coutput[0][i], coutput[3][i]); |
| 4222 | out[1] = 255; |
| 4223 | out += n; |
| 4224 | } |
| 4225 | } else { |
| 4226 | stbi_uc *y = coutput[0]; |
| 4227 | if (n == 1) |
| 4228 | for (i=0; i < z->s->img_x; ++i) out[i] = y[i]; |
| 4229 | else |
| 4230 | for (i=0; i < z->s->img_x; ++i) { *out++ = y[i]; *out++ = 255; } |
| 4231 | } |
| 4232 | } |
| 4233 | } |
| 4234 | } |
| 4235 | stbi__cleanup_jpeg(z); |
| 4236 | *out_x = z->s->img_x; |
| 4237 | *out_y = z->s->img_y; |
| 4238 | if (comp) *comp = z->s->img_n >= 3 ? 3 : 1; // report original components, not output |
| 4239 | return output; |
| 4240 | } |
| 4241 | } |
| 4242 | |
| 4243 | static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__nv12 *nv12, stbi__result_info *ri) |
| 4244 | { |
| 4245 | unsigned char* result; |
| 4246 | stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg)); |
| 4247 | if (!j) return stbi__errpuc("outofmem" , "Out of memory" ); |
| 4248 | memset(j, 0, sizeof(stbi__jpeg)); |
| 4249 | STBI_NOTUSED(ri); |
| 4250 | j->s = s; |
| 4251 | stbi__setup_jpeg(j); |
| 4252 | result = load_jpeg_image(j, x,y,comp,req_comp,nv12); |
| 4253 | STBI_FREE(j); |
| 4254 | return result; |
| 4255 | } |
| 4256 | |
| 4257 | static int stbi__jpeg_test(stbi__context *s) |
| 4258 | { |
| 4259 | int r; |
| 4260 | stbi__jpeg* j = (stbi__jpeg*)stbi__malloc(sizeof(stbi__jpeg)); |
| 4261 | if (!j) return stbi__err("outofmem" , "Out of memory" ); |
| 4262 | memset(j, 0, sizeof(stbi__jpeg)); |
| 4263 | j->s = s; |
| 4264 | stbi__setup_jpeg(j); |
| 4265 | r = stbi__decode_jpeg_header(j, STBI__SCAN_type); |
| 4266 | stbi__rewind(s); |
| 4267 | STBI_FREE(j); |
| 4268 | return r; |
| 4269 | } |
| 4270 | |
| 4271 | #if 0 /* not used in SDL */ |
| 4272 | static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp) |
| 4273 | { |
| 4274 | if (!stbi__decode_jpeg_header(j, STBI__SCAN_header)) { |
| 4275 | stbi__rewind( j->s ); |
| 4276 | return 0; |
| 4277 | } |
| 4278 | if (x) *x = j->s->img_x; |
| 4279 | if (y) *y = j->s->img_y; |
| 4280 | if (comp) *comp = j->s->img_n >= 3 ? 3 : 1; |
| 4281 | return 1; |
| 4282 | } |
| 4283 | |
| 4284 | static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp) |
| 4285 | { |
| 4286 | int result; |
| 4287 | stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg))); |
| 4288 | if (!j) return stbi__err("outofmem" , "Out of memory" ); |
| 4289 | memset(j, 0, sizeof(stbi__jpeg)); |
| 4290 | j->s = s; |
| 4291 | result = stbi__jpeg_info_raw(j, x, y, comp); |
| 4292 | STBI_FREE(j); |
| 4293 | return result; |
| 4294 | } |
| 4295 | #endif /**/ |
| 4296 | #endif |
| 4297 | |
| 4298 | // public domain zlib decode v0.2 Sean Barrett 2006-11-18 |
| 4299 | // simple implementation |
| 4300 | // - all input must be provided in an upfront buffer |
| 4301 | // - all output is written to a single output buffer (can malloc/realloc) |
| 4302 | // performance |
| 4303 | // - fast huffman |
| 4304 | |
| 4305 | #ifndef STBI_NO_ZLIB |
| 4306 | |
| 4307 | // fast-way is faster to check than jpeg huffman, but slow way is slower |
| 4308 | #define STBI__ZFAST_BITS 9 // accelerate all cases in default tables |
| 4309 | #define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1) |
| 4310 | #define STBI__ZNSYMS 288 // number of symbols in literal/length alphabet |
| 4311 | |
| 4312 | // zlib-style huffman encoding |
| 4313 | // (jpegs packs from left, zlib from right, so can't share code) |
| 4314 | typedef struct |
| 4315 | { |
| 4316 | stbi__uint16 fast[1 << STBI__ZFAST_BITS]; |
| 4317 | stbi__uint16 firstcode[16]; |
| 4318 | int maxcode[17]; |
| 4319 | stbi__uint16 firstsymbol[16]; |
| 4320 | stbi_uc size[STBI__ZNSYMS]; |
| 4321 | stbi__uint16 value[STBI__ZNSYMS]; |
| 4322 | } stbi__zhuffman; |
| 4323 | |
| 4324 | stbi_inline static int stbi__bitreverse16(int n) |
| 4325 | { |
| 4326 | n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1); |
| 4327 | n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2); |
| 4328 | n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4); |
| 4329 | n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8); |
| 4330 | return n; |
| 4331 | } |
| 4332 | |
| 4333 | stbi_inline static int stbi__bit_reverse(int v, int bits) |
| 4334 | { |
| 4335 | STBI_ASSERT(bits <= 16); |
| 4336 | // to bit reverse n bits, reverse 16 and shift |
| 4337 | // e.g. 11 bits, bit reverse and shift away 5 |
| 4338 | return stbi__bitreverse16(v) >> (16-bits); |
| 4339 | } |
| 4340 | |
| 4341 | static int stbi__zbuild_huffman(stbi__zhuffman *z, const stbi_uc *sizelist, int num) |
| 4342 | { |
| 4343 | int i,k=0; |
| 4344 | int code, next_code[16], sizes[17]; |
| 4345 | |
| 4346 | // DEFLATE spec for generating codes |
| 4347 | memset(sizes, 0, sizeof(sizes)); |
| 4348 | memset(z->fast, 0, sizeof(z->fast)); |
| 4349 | for (i=0; i < num; ++i) |
| 4350 | ++sizes[sizelist[i]]; |
| 4351 | sizes[0] = 0; |
| 4352 | for (i=1; i < 16; ++i) |
| 4353 | if (sizes[i] > (1 << i)) |
| 4354 | return stbi__err("bad sizes" , "Corrupt PNG" ); |
| 4355 | code = 0; |
| 4356 | for (i=1; i < 16; ++i) { |
| 4357 | next_code[i] = code; |
| 4358 | z->firstcode[i] = (stbi__uint16) code; |
| 4359 | z->firstsymbol[i] = (stbi__uint16) k; |
| 4360 | code = (code + sizes[i]); |
| 4361 | if (sizes[i]) |
| 4362 | if (code-1 >= (1 << i)) return stbi__err("bad codelengths" ,"Corrupt PNG" ); |
| 4363 | z->maxcode[i] = code << (16-i); // preshift for inner loop |
| 4364 | code <<= 1; |
| 4365 | k += sizes[i]; |
| 4366 | } |
| 4367 | z->maxcode[16] = 0x10000; // sentinel |
| 4368 | for (i=0; i < num; ++i) { |
| 4369 | int s = sizelist[i]; |
| 4370 | if (s) { |
| 4371 | int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s]; |
| 4372 | stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i); |
| 4373 | z->size [c] = (stbi_uc ) s; |
| 4374 | z->value[c] = (stbi__uint16) i; |
| 4375 | if (s <= STBI__ZFAST_BITS) { |
| 4376 | int j = stbi__bit_reverse(next_code[s],s); |
| 4377 | while (j < (1 << STBI__ZFAST_BITS)) { |
| 4378 | z->fast[j] = fastv; |
| 4379 | j += (1 << s); |
| 4380 | } |
| 4381 | } |
| 4382 | ++next_code[s]; |
| 4383 | } |
| 4384 | } |
| 4385 | return 1; |
| 4386 | } |
| 4387 | |
| 4388 | // zlib-from-memory implementation for PNG reading |
| 4389 | // because PNG allows splitting the zlib stream arbitrarily, |
| 4390 | // and it's annoying structurally to have PNG call ZLIB call PNG, |
| 4391 | // we require PNG read all the IDATs and combine them into a single |
| 4392 | // memory buffer |
| 4393 | |
| 4394 | typedef struct |
| 4395 | { |
| 4396 | stbi_uc *zbuffer, *zbuffer_end; |
| 4397 | int num_bits; |
| 4398 | int hit_zeof_once; |
| 4399 | stbi__uint32 code_buffer; |
| 4400 | |
| 4401 | char *zout; |
| 4402 | char *zout_start; |
| 4403 | char *zout_end; |
| 4404 | int z_expandable; |
| 4405 | |
| 4406 | stbi__zhuffman z_length, z_distance; |
| 4407 | } stbi__zbuf; |
| 4408 | |
| 4409 | stbi_inline static int stbi__zeof(stbi__zbuf *z) |
| 4410 | { |
| 4411 | return (z->zbuffer >= z->zbuffer_end); |
| 4412 | } |
| 4413 | |
| 4414 | stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z) |
| 4415 | { |
| 4416 | return stbi__zeof(z) ? 0 : *z->zbuffer++; |
| 4417 | } |
| 4418 | |
| 4419 | static void stbi__fill_bits(stbi__zbuf *z) |
| 4420 | { |
| 4421 | do { |
| 4422 | if (z->code_buffer >= (1U << z->num_bits)) { |
| 4423 | z->zbuffer = z->zbuffer_end; /* treat this as EOF so we fail. */ |
| 4424 | return; |
| 4425 | } |
| 4426 | z->code_buffer |= (unsigned int) stbi__zget8(z) << z->num_bits; |
| 4427 | z->num_bits += 8; |
| 4428 | } while (z->num_bits <= 24); |
| 4429 | } |
| 4430 | |
| 4431 | stbi_inline static unsigned int stbi__zreceive(stbi__zbuf *z, int n) |
| 4432 | { |
| 4433 | unsigned int k; |
| 4434 | if (z->num_bits < n) stbi__fill_bits(z); |
| 4435 | k = z->code_buffer & ((1 << n) - 1); |
| 4436 | z->code_buffer >>= n; |
| 4437 | z->num_bits -= n; |
| 4438 | return k; |
| 4439 | } |
| 4440 | |
| 4441 | static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z) |
| 4442 | { |
| 4443 | int b,s,k; |
| 4444 | // not resolved by fast table, so compute it the slow way |
| 4445 | // use jpeg approach, which requires MSbits at top |
| 4446 | k = stbi__bit_reverse(a->code_buffer, 16); |
| 4447 | for (s=STBI__ZFAST_BITS+1; ; ++s) |
| 4448 | if (k < z->maxcode[s]) |
| 4449 | break; |
| 4450 | if (s >= 16) return -1; // invalid code! |
| 4451 | // code size is s, so: |
| 4452 | b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s]; |
| 4453 | if (b >= STBI__ZNSYMS) return -1; // some data was corrupt somewhere! |
| 4454 | if (z->size[b] != s) return -1; // was originally an assert, but report failure instead. |
| 4455 | a->code_buffer >>= s; |
| 4456 | a->num_bits -= s; |
| 4457 | return z->value[b]; |
| 4458 | } |
| 4459 | |
| 4460 | stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z) |
| 4461 | { |
| 4462 | int b,s; |
| 4463 | if (a->num_bits < 16) { |
| 4464 | if (stbi__zeof(a)) { |
| 4465 | if (!a->hit_zeof_once) { |
| 4466 | // This is the first time we hit eof, insert 16 extra padding btis |
| 4467 | // to allow us to keep going; if we actually consume any of them |
| 4468 | // though, that is invalid data. This is caught later. |
| 4469 | a->hit_zeof_once = 1; |
| 4470 | a->num_bits += 16; // add 16 implicit zero bits |
| 4471 | } else { |
| 4472 | // We already inserted our extra 16 padding bits and are again |
| 4473 | // out, this stream is actually prematurely terminated. |
| 4474 | return -1; |
| 4475 | } |
| 4476 | } else { |
| 4477 | stbi__fill_bits(a); |
| 4478 | } |
| 4479 | } |
| 4480 | b = z->fast[a->code_buffer & STBI__ZFAST_MASK]; |
| 4481 | if (b) { |
| 4482 | s = b >> 9; |
| 4483 | a->code_buffer >>= s; |
| 4484 | a->num_bits -= s; |
| 4485 | return b & 511; |
| 4486 | } |
| 4487 | return stbi__zhuffman_decode_slowpath(a, z); |
| 4488 | } |
| 4489 | |
| 4490 | static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes |
| 4491 | { |
| 4492 | char *q; |
| 4493 | unsigned int cur, limit, old_limit; |
| 4494 | z->zout = zout; |
| 4495 | if (!z->z_expandable) return stbi__err("output buffer limit" ,"Corrupt PNG" ); |
| 4496 | cur = (unsigned int) (z->zout - z->zout_start); |
| 4497 | limit = old_limit = (unsigned) (z->zout_end - z->zout_start); |
| 4498 | if (UINT_MAX - cur < (unsigned) n) return stbi__err("outofmem" , "Out of memory" ); |
| 4499 | while (cur + n > limit) { |
| 4500 | if(limit > UINT_MAX / 2) return stbi__err("outofmem" , "Out of memory" ); |
| 4501 | limit *= 2; |
| 4502 | } |
| 4503 | q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit); |
| 4504 | STBI_NOTUSED(old_limit); |
| 4505 | if (q == NULL) return stbi__err("outofmem" , "Out of memory" ); |
| 4506 | z->zout_start = q; |
| 4507 | z->zout = q + cur; |
| 4508 | z->zout_end = q + limit; |
| 4509 | return 1; |
| 4510 | } |
| 4511 | |
| 4512 | static const int stbi__zlength_base[31] = { |
| 4513 | 3,4,5,6,7,8,9,10,11,13, |
| 4514 | 15,17,19,23,27,31,35,43,51,59, |
| 4515 | 67,83,99,115,131,163,195,227,258,0,0 }; |
| 4516 | |
| 4517 | static const int stbi__zlength_extra[31]= |
| 4518 | { 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 }; |
| 4519 | |
| 4520 | static const int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, |
| 4521 | 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0}; |
| 4522 | |
| 4523 | static const int stbi__zdist_extra[32] = |
| 4524 | { 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}; |
| 4525 | |
| 4526 | static int stbi__parse_huffman_block(stbi__zbuf *a) |
| 4527 | { |
| 4528 | char *zout = a->zout; |
| 4529 | for(;;) { |
| 4530 | int z = stbi__zhuffman_decode(a, &a->z_length); |
| 4531 | if (z < 256) { |
| 4532 | if (z < 0) return stbi__err("bad huffman code" ,"Corrupt PNG" ); // error in huffman codes |
| 4533 | if (zout >= a->zout_end) { |
| 4534 | if (!stbi__zexpand(a, zout, 1)) return 0; |
| 4535 | zout = a->zout; |
| 4536 | } |
| 4537 | *zout++ = (char) z; |
| 4538 | } else { |
| 4539 | stbi_uc *p; |
| 4540 | int len,dist; |
| 4541 | if (z == 256) { |
| 4542 | a->zout = zout; |
| 4543 | if (a->hit_zeof_once && a->num_bits < 16) { |
| 4544 | // The first time we hit zeof, we inserted 16 extra zero bits into our bit |
| 4545 | // buffer so the decoder can just do its speculative decoding. But if we |
| 4546 | // actually consumed any of those bits (which is the case when num_bits < 16), |
| 4547 | // the stream actually read past the end so it is malformed. |
| 4548 | return stbi__err("unexpected end" ,"Corrupt PNG" ); |
| 4549 | } |
| 4550 | return 1; |
| 4551 | } |
| 4552 | if (z >= 286) return stbi__err("bad huffman code" ,"Corrupt PNG" ); // per DEFLATE, length codes 286 and 287 must not appear in compressed data |
| 4553 | z -= 257; |
| 4554 | len = stbi__zlength_base[z]; |
| 4555 | if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]); |
| 4556 | z = stbi__zhuffman_decode(a, &a->z_distance); |
| 4557 | 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 |
| 4558 | dist = stbi__zdist_base[z]; |
| 4559 | if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]); |
| 4560 | if (zout - a->zout_start < dist) return stbi__err("bad dist" ,"Corrupt PNG" ); |
| 4561 | if (len > a->zout_end - zout) { |
| 4562 | if (!stbi__zexpand(a, zout, len)) return 0; |
| 4563 | zout = a->zout; |
| 4564 | } |
| 4565 | p = (stbi_uc *) (zout - dist); |
| 4566 | if (dist == 1) { // run of one byte; common in images. |
| 4567 | stbi_uc v = *p; |
| 4568 | if (len) { do *zout++ = v; while (--len); } |
| 4569 | } else { |
| 4570 | if (len) { do *zout++ = *p++; while (--len); } |
| 4571 | } |
| 4572 | } |
| 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | static int stbi__compute_huffman_codes(stbi__zbuf *a) |
| 4577 | { |
| 4578 | 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 }; |
| 4579 | stbi__zhuffman z_codelength; |
| 4580 | stbi_uc lencodes[286+32+137];//padding for maximum single op |
| 4581 | stbi_uc codelength_sizes[19]; |
| 4582 | int i,n; |
| 4583 | |
| 4584 | int hlit = stbi__zreceive(a,5) + 257; |
| 4585 | int hdist = stbi__zreceive(a,5) + 1; |
| 4586 | int hclen = stbi__zreceive(a,4) + 4; |
| 4587 | int ntot = hlit + hdist; |
| 4588 | |
| 4589 | memset(codelength_sizes, 0, sizeof(codelength_sizes)); |
| 4590 | for (i=0; i < hclen; ++i) { |
| 4591 | int s = stbi__zreceive(a,3); |
| 4592 | codelength_sizes[length_dezigzag[i]] = (stbi_uc) s; |
| 4593 | } |
| 4594 | if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0; |
| 4595 | |
| 4596 | n = 0; |
| 4597 | while (n < ntot) { |
| 4598 | int c = stbi__zhuffman_decode(a, &z_codelength); |
| 4599 | if (c < 0 || c >= 19) return stbi__err("bad codelengths" , "Corrupt PNG" ); |
| 4600 | if (c < 16) |
| 4601 | lencodes[n++] = (stbi_uc) c; |
| 4602 | else { |
| 4603 | stbi_uc fill = 0; |
| 4604 | if (c == 16) { |
| 4605 | c = stbi__zreceive(a,2)+3; |
| 4606 | if (n == 0) return stbi__err("bad codelengths" , "Corrupt PNG" ); |
| 4607 | fill = lencodes[n-1]; |
| 4608 | } else if (c == 17) { |
| 4609 | c = stbi__zreceive(a,3)+3; |
| 4610 | } else if (c == 18) { |
| 4611 | c = stbi__zreceive(a,7)+11; |
| 4612 | } else { |
| 4613 | return stbi__err("bad codelengths" , "Corrupt PNG" ); |
| 4614 | } |
| 4615 | if (ntot - n < c) return stbi__err("bad codelengths" , "Corrupt PNG" ); |
| 4616 | memset(lencodes+n, fill, c); |
| 4617 | n += c; |
| 4618 | } |
| 4619 | } |
| 4620 | if (n != ntot) return stbi__err("bad codelengths" ,"Corrupt PNG" ); |
| 4621 | if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0; |
| 4622 | if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0; |
| 4623 | return 1; |
| 4624 | } |
| 4625 | |
| 4626 | static int stbi__parse_uncompressed_block(stbi__zbuf *a) |
| 4627 | { |
| 4628 | stbi_uc header[4]; |
| 4629 | int len,nlen,k; |
| 4630 | if (a->num_bits & 7) |
| 4631 | stbi__zreceive(a, a->num_bits & 7); // discard |
| 4632 | // drain the bit-packed data into header |
| 4633 | k = 0; |
| 4634 | while (a->num_bits > 0) { |
| 4635 | header[k++] = (stbi_uc) (a->code_buffer & 255); // suppress MSVC run-time check |
| 4636 | a->code_buffer >>= 8; |
| 4637 | a->num_bits -= 8; |
| 4638 | } |
| 4639 | if (a->num_bits < 0) return stbi__err("zlib corrupt" ,"Corrupt PNG" ); |
| 4640 | // now fill header the normal way |
| 4641 | while (k < 4) |
| 4642 | header[k++] = stbi__zget8(a); |
| 4643 | len = header[1] * 256 + header[0]; |
| 4644 | nlen = header[3] * 256 + header[2]; |
| 4645 | if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt" ,"Corrupt PNG" ); |
| 4646 | if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer" ,"Corrupt PNG" ); |
| 4647 | if (a->zout + len > a->zout_end) |
| 4648 | if (!stbi__zexpand(a, a->zout, len)) return 0; |
| 4649 | memcpy(a->zout, a->zbuffer, len); |
| 4650 | a->zbuffer += len; |
| 4651 | a->zout += len; |
| 4652 | return 1; |
| 4653 | } |
| 4654 | |
| 4655 | static int stbi__parse_zlib_header(stbi__zbuf *a) |
| 4656 | { |
| 4657 | int cmf = stbi__zget8(a); |
| 4658 | int cm = cmf & 15; |
| 4659 | /* int cinfo = cmf >> 4; */ |
| 4660 | int flg = stbi__zget8(a); |
| 4661 | if (stbi__zeof(a)) return stbi__err("bad zlib header" ,"Corrupt PNG" ); // zlib spec |
| 4662 | if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header" ,"Corrupt PNG" ); // zlib spec |
| 4663 | if (flg & 32) return stbi__err("no preset dict" ,"Corrupt PNG" ); // preset dictionary not allowed in png |
| 4664 | if (cm != 8) return stbi__err("bad compression" ,"Corrupt PNG" ); // DEFLATE required for png |
| 4665 | // window = 1 << (8 + cinfo)... but who cares, we fully buffer output |
| 4666 | return 1; |
| 4667 | } |
| 4668 | |
| 4669 | static const stbi_uc stbi__zdefault_length[STBI__ZNSYMS] = |
| 4670 | { |
| 4671 | 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, |
| 4672 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 4673 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 4674 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 4675 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, |
| 4676 | 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, |
| 4677 | 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, |
| 4678 | 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, |
| 4679 | 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 |
| 4680 | }; |
| 4681 | static const stbi_uc stbi__zdefault_distance[32] = |
| 4682 | { |
| 4683 | 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 |
| 4684 | }; |
| 4685 | /* |
| 4686 | Init algorithm: |
| 4687 | { |
| 4688 | int i; // use <= to match clearly with spec |
| 4689 | for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8; |
| 4690 | for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9; |
| 4691 | for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7; |
| 4692 | for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8; |
| 4693 | |
| 4694 | for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5; |
| 4695 | } |
| 4696 | */ |
| 4697 | |
| 4698 | static int stbi__parse_zlib(stbi__zbuf *a, int parse_header) |
| 4699 | { |
| 4700 | int final, type; |
| 4701 | if (parse_header) |
| 4702 | if (!stbi__parse_zlib_header(a)) return 0; |
| 4703 | a->num_bits = 0; |
| 4704 | a->code_buffer = 0; |
| 4705 | a->hit_zeof_once = 0; |
| 4706 | do { |
| 4707 | final = stbi__zreceive(a,1); |
| 4708 | type = stbi__zreceive(a,2); |
| 4709 | if (type == 0) { |
| 4710 | if (!stbi__parse_uncompressed_block(a)) return 0; |
| 4711 | } else if (type == 3) { |
| 4712 | return 0; |
| 4713 | } else { |
| 4714 | if (type == 1) { |
| 4715 | // use fixed code lengths |
| 4716 | if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , STBI__ZNSYMS)) return 0; |
| 4717 | if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0; |
| 4718 | } else { |
| 4719 | if (!stbi__compute_huffman_codes(a)) return 0; |
| 4720 | } |
| 4721 | if (!stbi__parse_huffman_block(a)) return 0; |
| 4722 | } |
| 4723 | } while (!final); |
| 4724 | return 1; |
| 4725 | } |
| 4726 | |
| 4727 | static int stbi__do_zlib(stbi__zbuf *a, char *obuf, int olen, int exp, int parse_header) |
| 4728 | { |
| 4729 | a->zout_start = obuf; |
| 4730 | a->zout = obuf; |
| 4731 | a->zout_end = obuf + olen; |
| 4732 | a->z_expandable = exp; |
| 4733 | |
| 4734 | return stbi__parse_zlib(a, parse_header); |
| 4735 | } |
| 4736 | |
| 4737 | STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen) |
| 4738 | { |
| 4739 | stbi__zbuf a; |
| 4740 | char *p = (char *) stbi__malloc(initial_size); |
| 4741 | if (p == NULL) return NULL; |
| 4742 | a.zbuffer = (stbi_uc *) buffer; |
| 4743 | a.zbuffer_end = (stbi_uc *) buffer + len; |
| 4744 | if (stbi__do_zlib(&a, p, initial_size, 1, 1)) { |
| 4745 | if (outlen) *outlen = (int) (a.zout - a.zout_start); |
| 4746 | return a.zout_start; |
| 4747 | } else { |
| 4748 | STBI_FREE(a.zout_start); |
| 4749 | return NULL; |
| 4750 | } |
| 4751 | } |
| 4752 | |
| 4753 | STBIDEF char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen) |
| 4754 | { |
| 4755 | return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen); |
| 4756 | } |
| 4757 | |
| 4758 | STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header) |
| 4759 | { |
| 4760 | stbi__zbuf a; |
| 4761 | char *p = (char *) stbi__malloc(initial_size); |
| 4762 | if (p == NULL) return NULL; |
| 4763 | a.zbuffer = (stbi_uc *) buffer; |
| 4764 | a.zbuffer_end = (stbi_uc *) buffer + len; |
| 4765 | if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) { |
| 4766 | if (outlen) *outlen = (int) (a.zout - a.zout_start); |
| 4767 | return a.zout_start; |
| 4768 | } else { |
| 4769 | STBI_FREE(a.zout_start); |
| 4770 | return NULL; |
| 4771 | } |
| 4772 | } |
| 4773 | |
| 4774 | STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen) |
| 4775 | { |
| 4776 | stbi__zbuf a; |
| 4777 | a.zbuffer = (stbi_uc *) ibuffer; |
| 4778 | a.zbuffer_end = (stbi_uc *) ibuffer + ilen; |
| 4779 | if (stbi__do_zlib(&a, obuffer, olen, 0, 1)) |
| 4780 | return (int) (a.zout - a.zout_start); |
| 4781 | else |
| 4782 | return -1; |
| 4783 | } |
| 4784 | |
| 4785 | STBIDEF char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen) |
| 4786 | { |
| 4787 | stbi__zbuf a; |
| 4788 | char *p = (char *) stbi__malloc(16384); |
| 4789 | if (p == NULL) return NULL; |
| 4790 | a.zbuffer = (stbi_uc *) buffer; |
| 4791 | a.zbuffer_end = (stbi_uc *) buffer+len; |
| 4792 | if (stbi__do_zlib(&a, p, 16384, 1, 0)) { |
| 4793 | if (outlen) *outlen = (int) (a.zout - a.zout_start); |
| 4794 | return a.zout_start; |
| 4795 | } else { |
| 4796 | STBI_FREE(a.zout_start); |
| 4797 | return NULL; |
| 4798 | } |
| 4799 | } |
| 4800 | |
| 4801 | STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen) |
| 4802 | { |
| 4803 | stbi__zbuf a; |
| 4804 | a.zbuffer = (stbi_uc *) ibuffer; |
| 4805 | a.zbuffer_end = (stbi_uc *) ibuffer + ilen; |
| 4806 | if (stbi__do_zlib(&a, obuffer, olen, 0, 0)) |
| 4807 | return (int) (a.zout - a.zout_start); |
| 4808 | else |
| 4809 | return -1; |
| 4810 | } |
| 4811 | #endif |
| 4812 | |
| 4813 | // public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18 |
| 4814 | // simple implementation |
| 4815 | // - only 8-bit samples |
| 4816 | // - no CRC checking |
| 4817 | // - allocates lots of intermediate memory |
| 4818 | // - avoids problem of streaming data between subsystems |
| 4819 | // - avoids explicit window management |
| 4820 | // performance |
| 4821 | // - uses stb_zlib, a PD zlib implementation with fast huffman decoding |
| 4822 | |
| 4823 | #ifndef STBI_NO_PNG |
| 4824 | typedef struct |
| 4825 | { |
| 4826 | stbi__uint32 length; |
| 4827 | stbi__uint32 type; |
| 4828 | } stbi__pngchunk; |
| 4829 | |
| 4830 | static stbi__pngchunk stbi__get_chunk_header(stbi__context *s) |
| 4831 | { |
| 4832 | stbi__pngchunk c; |
| 4833 | c.length = stbi__get32be(s); |
| 4834 | c.type = stbi__get32be(s); |
| 4835 | return c; |
| 4836 | } |
| 4837 | |
| 4838 | static int stbi__check_png_header(stbi__context *s) |
| 4839 | { |
| 4840 | static const stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 }; |
| 4841 | int i; |
| 4842 | for (i=0; i < 8; ++i) |
| 4843 | if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig" ,"Not a PNG" ); |
| 4844 | return 1; |
| 4845 | } |
| 4846 | |
| 4847 | typedef struct |
| 4848 | { |
| 4849 | stbi__context *s; |
| 4850 | stbi_uc *idata, *expanded, *out; |
| 4851 | int depth; |
| 4852 | } stbi__png; |
| 4853 | |
| 4854 | |
| 4855 | enum { |
| 4856 | STBI__F_none=0, |
| 4857 | STBI__F_sub=1, |
| 4858 | STBI__F_up=2, |
| 4859 | STBI__F_avg=3, |
| 4860 | STBI__F_paeth=4, |
| 4861 | // synthetic filter used for first scanline to avoid needing a dummy row of 0s |
| 4862 | STBI__F_avg_first |
| 4863 | }; |
| 4864 | |
| 4865 | static stbi_uc first_row_filter[5] = |
| 4866 | { |
| 4867 | STBI__F_none, |
| 4868 | STBI__F_sub, |
| 4869 | STBI__F_none, |
| 4870 | STBI__F_avg_first, |
| 4871 | STBI__F_sub // Paeth with b=c=0 turns out to be equivalent to sub |
| 4872 | }; |
| 4873 | |
| 4874 | static int stbi__paeth(int a, int b, int c) |
| 4875 | { |
| 4876 | // This formulation looks very different from the reference in the PNG spec, but is |
| 4877 | // actually equivalent and has favorable data dependencies and admits straightforward |
| 4878 | // generation of branch-free code, which helps performance significantly. |
| 4879 | int thresh = c*3 - (a + b); |
| 4880 | int lo = a < b ? a : b; |
| 4881 | int hi = a < b ? b : a; |
| 4882 | int t0 = (hi <= thresh) ? lo : c; |
| 4883 | int t1 = (thresh <= lo) ? hi : t0; |
| 4884 | return t1; |
| 4885 | } |
| 4886 | |
| 4887 | static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 }; |
| 4888 | |
| 4889 | // adds an extra all-255 alpha channel |
| 4890 | // dest == src is legal |
| 4891 | // img_n must be 1 or 3 |
| 4892 | static void stbi__create_png_alpha_expand8(stbi_uc *dest, stbi_uc *src, stbi__uint32 x, int img_n) |
| 4893 | { |
| 4894 | int i; |
| 4895 | // must process data backwards since we allow dest==src |
| 4896 | if (img_n == 1) { |
| 4897 | for (i=x-1; i >= 0; --i) { |
| 4898 | dest[i*2+1] = 255; |
| 4899 | dest[i*2+0] = src[i]; |
| 4900 | } |
| 4901 | } else { |
| 4902 | STBI_ASSERT(img_n == 3); |
| 4903 | for (i=x-1; i >= 0; --i) { |
| 4904 | dest[i*4+3] = 255; |
| 4905 | dest[i*4+2] = src[i*3+2]; |
| 4906 | dest[i*4+1] = src[i*3+1]; |
| 4907 | dest[i*4+0] = src[i*3+0]; |
| 4908 | } |
| 4909 | } |
| 4910 | } |
| 4911 | |
| 4912 | // create the png data from post-deflated data |
| 4913 | static 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) |
| 4914 | { |
| 4915 | int bytes = (depth == 16 ? 2 : 1); |
| 4916 | stbi__context *s = a->s; |
| 4917 | stbi__uint32 i,j,stride = x*out_n*bytes; |
| 4918 | stbi__uint32 img_len, img_width_bytes; |
| 4919 | stbi_uc *filter_buf; |
| 4920 | int all_ok = 1; |
| 4921 | int k; |
| 4922 | int img_n = s->img_n; // copy it into a local for later |
| 4923 | |
| 4924 | int output_bytes = out_n*bytes; |
| 4925 | int filter_bytes = img_n*bytes; |
| 4926 | int width = x; |
| 4927 | |
| 4928 | STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1); |
| 4929 | a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into |
| 4930 | if (!a->out) return stbi__err("outofmem" , "Out of memory" ); |
| 4931 | |
| 4932 | // note: error exits here don't need to clean up a->out individually, |
| 4933 | // stbi__do_png always does on error. |
| 4934 | if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large" , "Corrupt PNG" ); |
| 4935 | img_width_bytes = (((img_n * x * depth) + 7) >> 3); |
| 4936 | if (!stbi__mad2sizes_valid(img_width_bytes, y, img_width_bytes)) return stbi__err("too large" , "Corrupt PNG" ); |
| 4937 | img_len = (img_width_bytes + 1) * y; |
| 4938 | |
| 4939 | // we used to check for exact match between raw_len and img_len on non-interlaced PNGs, |
| 4940 | // but issue #276 reported a PNG in the wild that had extra data at the end (all zeros), |
| 4941 | // so just check for raw_len < img_len always. |
| 4942 | if (raw_len < img_len) return stbi__err("not enough pixels" ,"Corrupt PNG" ); |
| 4943 | |
| 4944 | // Allocate two scan lines worth of filter workspace buffer. |
| 4945 | filter_buf = (stbi_uc *) stbi__malloc_mad2(img_width_bytes, 2, 0); |
| 4946 | if (!filter_buf) return stbi__err("outofmem" , "Out of memory" ); |
| 4947 | |
| 4948 | // Filtering for low-bit-depth images |
| 4949 | if (depth < 8) { |
| 4950 | filter_bytes = 1; |
| 4951 | width = img_width_bytes; |
| 4952 | } |
| 4953 | |
| 4954 | for (j=0; j < y; ++j) { |
| 4955 | // cur/prior filter buffers alternate |
| 4956 | stbi_uc *cur = filter_buf + (j & 1)*img_width_bytes; |
| 4957 | stbi_uc *prior = filter_buf + (~j & 1)*img_width_bytes; |
| 4958 | stbi_uc *dest = a->out + stride*j; |
| 4959 | int nk = width * filter_bytes; |
| 4960 | int filter = *raw++; |
| 4961 | |
| 4962 | // check filter type |
| 4963 | if (filter > 4) { |
| 4964 | all_ok = stbi__err("invalid filter" ,"Corrupt PNG" ); |
| 4965 | break; |
| 4966 | } |
| 4967 | |
| 4968 | // if first row, use special filter that doesn't sample previous row |
| 4969 | if (j == 0) filter = first_row_filter[filter]; |
| 4970 | |
| 4971 | // perform actual filtering |
| 4972 | switch (filter) { |
| 4973 | case STBI__F_none: |
| 4974 | memcpy(cur, raw, nk); |
| 4975 | break; |
| 4976 | case STBI__F_sub: |
| 4977 | memcpy(cur, raw, filter_bytes); |
| 4978 | for (k = filter_bytes; k < nk; ++k) |
| 4979 | cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); |
| 4980 | break; |
| 4981 | case STBI__F_up: |
| 4982 | for (k = 0; k < nk; ++k) |
| 4983 | cur[k] = STBI__BYTECAST(raw[k] + prior[k]); |
| 4984 | break; |
| 4985 | case STBI__F_avg: |
| 4986 | for (k = 0; k < filter_bytes; ++k) |
| 4987 | cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); |
| 4988 | for (k = filter_bytes; k < nk; ++k) |
| 4989 | cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); |
| 4990 | break; |
| 4991 | case STBI__F_paeth: |
| 4992 | for (k = 0; k < filter_bytes; ++k) |
| 4993 | cur[k] = STBI__BYTECAST(raw[k] + prior[k]); // prior[k] == stbi__paeth(0,prior[k],0) |
| 4994 | for (k = filter_bytes; k < nk; ++k) |
| 4995 | cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes], prior[k], prior[k-filter_bytes])); |
| 4996 | break; |
| 4997 | case STBI__F_avg_first: |
| 4998 | memcpy(cur, raw, filter_bytes); |
| 4999 | for (k = filter_bytes; k < nk; ++k) |
| 5000 | cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); |
| 5001 | break; |
| 5002 | } |
| 5003 | |
| 5004 | raw += nk; |
| 5005 | |
| 5006 | // expand decoded bits in cur to dest, also adding an extra alpha channel if desired |
| 5007 | if (depth < 8) { |
| 5008 | stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1; // scale grayscale values to 0..255 range |
| 5009 | stbi_uc *in = cur; |
| 5010 | stbi_uc *out = dest; |
| 5011 | stbi_uc inb = 0; |
| 5012 | stbi__uint32 nsmp = x*img_n; |
| 5013 | |
| 5014 | // expand bits to bytes first |
| 5015 | if (depth == 4) { |
| 5016 | for (i=0; i < nsmp; ++i) { |
| 5017 | if ((i & 1) == 0) inb = *in++; |
| 5018 | *out++ = scale * (inb >> 4); |
| 5019 | inb <<= 4; |
| 5020 | } |
| 5021 | } else if (depth == 2) { |
| 5022 | for (i=0; i < nsmp; ++i) { |
| 5023 | if ((i & 3) == 0) inb = *in++; |
| 5024 | *out++ = scale * (inb >> 6); |
| 5025 | inb <<= 2; |
| 5026 | } |
| 5027 | } else { |
| 5028 | STBI_ASSERT(depth == 1); |
| 5029 | for (i=0; i < nsmp; ++i) { |
| 5030 | if ((i & 7) == 0) inb = *in++; |
| 5031 | *out++ = scale * (inb >> 7); |
| 5032 | inb <<= 1; |
| 5033 | } |
| 5034 | } |
| 5035 | |
| 5036 | // insert alpha=255 values if desired |
| 5037 | if (img_n != out_n) |
| 5038 | stbi__create_png_alpha_expand8(dest, dest, x, img_n); |
| 5039 | } else if (depth == 8) { |
| 5040 | if (img_n == out_n) |
| 5041 | memcpy(dest, cur, x*img_n); |
| 5042 | else |
| 5043 | stbi__create_png_alpha_expand8(dest, cur, x, img_n); |
| 5044 | } else if (depth == 16) { |
| 5045 | // convert the image data from big-endian to platform-native |
| 5046 | stbi__uint16 *dest16 = (stbi__uint16*)dest; |
| 5047 | stbi__uint32 nsmp = x*img_n; |
| 5048 | |
| 5049 | if (img_n == out_n) { |
| 5050 | for (i = 0; i < nsmp; ++i, ++dest16, cur += 2) |
| 5051 | *dest16 = (cur[0] << 8) | cur[1]; |
| 5052 | } else { |
| 5053 | STBI_ASSERT(img_n+1 == out_n); |
| 5054 | if (img_n == 1) { |
| 5055 | for (i = 0; i < x; ++i, dest16 += 2, cur += 2) { |
| 5056 | dest16[0] = (cur[0] << 8) | cur[1]; |
| 5057 | dest16[1] = 0xffff; |
| 5058 | } |
| 5059 | } else { |
| 5060 | STBI_ASSERT(img_n == 3); |
| 5061 | for (i = 0; i < x; ++i, dest16 += 4, cur += 6) { |
| 5062 | dest16[0] = (cur[0] << 8) | cur[1]; |
| 5063 | dest16[1] = (cur[2] << 8) | cur[3]; |
| 5064 | dest16[2] = (cur[4] << 8) | cur[5]; |
| 5065 | dest16[3] = 0xffff; |
| 5066 | } |
| 5067 | } |
| 5068 | } |
| 5069 | } |
| 5070 | } |
| 5071 | |
| 5072 | STBI_FREE(filter_buf); |
| 5073 | if (!all_ok) return 0; |
| 5074 | |
| 5075 | return 1; |
| 5076 | } |
| 5077 | |
| 5078 | static 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) |
| 5079 | { |
| 5080 | int bytes = (depth == 16 ? 2 : 1); |
| 5081 | int out_bytes = out_n * bytes; |
| 5082 | stbi_uc *final; |
| 5083 | int p; |
| 5084 | if (!interlaced) |
| 5085 | return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color); |
| 5086 | |
| 5087 | // de-interlacing |
| 5088 | final = (stbi_uc *) stbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0); |
| 5089 | if (!final) return stbi__err("outofmem" , "Out of memory" ); |
| 5090 | for (p=0; p < 7; ++p) { |
| 5091 | int xorig[] = { 0,4,0,2,0,1,0 }; |
| 5092 | int yorig[] = { 0,0,4,0,2,0,1 }; |
| 5093 | int xspc[] = { 8,8,4,4,2,2,1 }; |
| 5094 | int yspc[] = { 8,8,8,4,4,2,2 }; |
| 5095 | int i,j,x,y; |
| 5096 | // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1 |
| 5097 | x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p]; |
| 5098 | y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p]; |
| 5099 | if (x && y) { |
| 5100 | stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y; |
| 5101 | if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) { |
| 5102 | STBI_FREE(final); |
| 5103 | return 0; |
| 5104 | } |
| 5105 | for (j=0; j < y; ++j) { |
| 5106 | for (i=0; i < x; ++i) { |
| 5107 | int out_y = j*yspc[p]+yorig[p]; |
| 5108 | int out_x = i*xspc[p]+xorig[p]; |
| 5109 | memcpy(final + out_y*a->s->img_x*out_bytes + out_x*out_bytes, |
| 5110 | a->out + (j*x+i)*out_bytes, out_bytes); |
| 5111 | } |
| 5112 | } |
| 5113 | STBI_FREE(a->out); |
| 5114 | image_data += img_len; |
| 5115 | image_data_len -= img_len; |
| 5116 | } |
| 5117 | } |
| 5118 | a->out = final; |
| 5119 | |
| 5120 | return 1; |
| 5121 | } |
| 5122 | |
| 5123 | static int stbi__compute_transparency(stbi__png *z, stbi_uc tc[3], int out_n) |
| 5124 | { |
| 5125 | stbi__context *s = z->s; |
| 5126 | stbi__uint32 i, pixel_count = s->img_x * s->img_y; |
| 5127 | stbi_uc *p = z->out; |
| 5128 | |
| 5129 | // compute color-based transparency, assuming we've |
| 5130 | // already got 255 as the alpha value in the output |
| 5131 | STBI_ASSERT(out_n == 2 || out_n == 4); |
| 5132 | |
| 5133 | if (out_n == 2) { |
| 5134 | for (i=0; i < pixel_count; ++i) { |
| 5135 | p[1] = (p[0] == tc[0] ? 0 : 255); |
| 5136 | p += 2; |
| 5137 | } |
| 5138 | } else { |
| 5139 | for (i=0; i < pixel_count; ++i) { |
| 5140 | if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) |
| 5141 | p[3] = 0; |
| 5142 | p += 4; |
| 5143 | } |
| 5144 | } |
| 5145 | return 1; |
| 5146 | } |
| 5147 | |
| 5148 | static int stbi__compute_transparency16(stbi__png *z, stbi__uint16 tc[3], int out_n) |
| 5149 | { |
| 5150 | stbi__context *s = z->s; |
| 5151 | stbi__uint32 i, pixel_count = s->img_x * s->img_y; |
| 5152 | stbi__uint16 *p = (stbi__uint16*) z->out; |
| 5153 | |
| 5154 | // compute color-based transparency, assuming we've |
| 5155 | // already got 65535 as the alpha value in the output |
| 5156 | STBI_ASSERT(out_n == 2 || out_n == 4); |
| 5157 | |
| 5158 | if (out_n == 2) { |
| 5159 | for (i = 0; i < pixel_count; ++i) { |
| 5160 | p[1] = (p[0] == tc[0] ? 0 : 65535); |
| 5161 | p += 2; |
| 5162 | } |
| 5163 | } else { |
| 5164 | for (i = 0; i < pixel_count; ++i) { |
| 5165 | if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) |
| 5166 | p[3] = 0; |
| 5167 | p += 4; |
| 5168 | } |
| 5169 | } |
| 5170 | return 1; |
| 5171 | } |
| 5172 | |
| 5173 | static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n) |
| 5174 | { |
| 5175 | stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y; |
| 5176 | stbi_uc *p, *temp_out, *orig = a->out; |
| 5177 | |
| 5178 | p = (stbi_uc *) stbi__malloc_mad2(pixel_count, pal_img_n, 0); |
| 5179 | if (p == NULL) return stbi__err("outofmem" , "Out of memory" ); |
| 5180 | |
| 5181 | // between here and free(out) below, exitting would leak |
| 5182 | temp_out = p; |
| 5183 | |
| 5184 | if (pal_img_n == 3) { |
| 5185 | for (i=0; i < pixel_count; ++i) { |
| 5186 | int n = orig[i]*4; |
| 5187 | p[0] = palette[n ]; |
| 5188 | p[1] = palette[n+1]; |
| 5189 | p[2] = palette[n+2]; |
| 5190 | p += 3; |
| 5191 | } |
| 5192 | } else { |
| 5193 | for (i=0; i < pixel_count; ++i) { |
| 5194 | int n = orig[i]*4; |
| 5195 | p[0] = palette[n ]; |
| 5196 | p[1] = palette[n+1]; |
| 5197 | p[2] = palette[n+2]; |
| 5198 | p[3] = palette[n+3]; |
| 5199 | p += 4; |
| 5200 | } |
| 5201 | } |
| 5202 | STBI_FREE(a->out); |
| 5203 | a->out = temp_out; |
| 5204 | |
| 5205 | STBI_NOTUSED(len); |
| 5206 | |
| 5207 | return 1; |
| 5208 | } |
| 5209 | |
| 5210 | static int stbi__unpremultiply_on_load_global = 0; |
| 5211 | static int stbi__de_iphone_flag_global = 0; |
| 5212 | |
| 5213 | #if 0 /* not used in SDL */ |
| 5214 | STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply) |
| 5215 | { |
| 5216 | stbi__unpremultiply_on_load_global = flag_true_if_should_unpremultiply; |
| 5217 | } |
| 5218 | |
| 5219 | STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert) |
| 5220 | { |
| 5221 | stbi__de_iphone_flag_global = flag_true_if_should_convert; |
| 5222 | } |
| 5223 | #endif |
| 5224 | |
| 5225 | #ifndef STBI_THREAD_LOCAL |
| 5226 | #define stbi__unpremultiply_on_load stbi__unpremultiply_on_load_global |
| 5227 | #define stbi__de_iphone_flag stbi__de_iphone_flag_global |
| 5228 | #else |
| 5229 | static STBI_THREAD_LOCAL int stbi__unpremultiply_on_load_local, stbi__unpremultiply_on_load_set; |
| 5230 | static STBI_THREAD_LOCAL int stbi__de_iphone_flag_local, stbi__de_iphone_flag_set; |
| 5231 | |
| 5232 | STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply) |
| 5233 | { |
| 5234 | stbi__unpremultiply_on_load_local = flag_true_if_should_unpremultiply; |
| 5235 | stbi__unpremultiply_on_load_set = 1; |
| 5236 | } |
| 5237 | |
| 5238 | STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert) |
| 5239 | { |
| 5240 | stbi__de_iphone_flag_local = flag_true_if_should_convert; |
| 5241 | stbi__de_iphone_flag_set = 1; |
| 5242 | } |
| 5243 | |
| 5244 | #define stbi__unpremultiply_on_load (stbi__unpremultiply_on_load_set \ |
| 5245 | ? stbi__unpremultiply_on_load_local \ |
| 5246 | : stbi__unpremultiply_on_load_global) |
| 5247 | #define stbi__de_iphone_flag (stbi__de_iphone_flag_set \ |
| 5248 | ? stbi__de_iphone_flag_local \ |
| 5249 | : stbi__de_iphone_flag_global) |
| 5250 | #endif // STBI_THREAD_LOCAL |
| 5251 | |
| 5252 | static void stbi__de_iphone(stbi__png *z) |
| 5253 | { |
| 5254 | stbi__context *s = z->s; |
| 5255 | stbi__uint32 i, pixel_count = s->img_x * s->img_y; |
| 5256 | stbi_uc *p = z->out; |
| 5257 | |
| 5258 | if (s->img_out_n == 3) { // convert bgr to rgb |
| 5259 | for (i=0; i < pixel_count; ++i) { |
| 5260 | stbi_uc t = p[0]; |
| 5261 | p[0] = p[2]; |
| 5262 | p[2] = t; |
| 5263 | p += 3; |
| 5264 | } |
| 5265 | } else { |
| 5266 | STBI_ASSERT(s->img_out_n == 4); |
| 5267 | if (stbi__unpremultiply_on_load) { |
| 5268 | // convert bgr to rgb and unpremultiply |
| 5269 | for (i=0; i < pixel_count; ++i) { |
| 5270 | stbi_uc a = p[3]; |
| 5271 | stbi_uc t = p[0]; |
| 5272 | if (a) { |
| 5273 | stbi_uc half = a / 2; |
| 5274 | p[0] = (p[2] * 255 + half) / a; |
| 5275 | p[1] = (p[1] * 255 + half) / a; |
| 5276 | p[2] = ( t * 255 + half) / a; |
| 5277 | } else { |
| 5278 | p[0] = p[2]; |
| 5279 | p[2] = t; |
| 5280 | } |
| 5281 | p += 4; |
| 5282 | } |
| 5283 | } else { |
| 5284 | // convert bgr to rgb |
| 5285 | for (i=0; i < pixel_count; ++i) { |
| 5286 | stbi_uc t = p[0]; |
| 5287 | p[0] = p[2]; |
| 5288 | p[2] = t; |
| 5289 | p += 4; |
| 5290 | } |
| 5291 | } |
| 5292 | } |
| 5293 | } |
| 5294 | |
| 5295 | #define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d)) |
| 5296 | |
| 5297 | static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp, unsigned int *palette_buffer, int palette_buffer_len) |
| 5298 | { |
| 5299 | stbi_uc _palette[1024], pal_img_n=0; |
| 5300 | stbi_uc *palette = _palette; |
| 5301 | stbi_uc has_trans=0, tc[3]={0}; |
| 5302 | stbi__uint16 tc16[3]; |
| 5303 | stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0; |
| 5304 | int first=1,k,interlace=0, color=0, is_iphone=0; |
| 5305 | stbi__context *s = z->s; |
| 5306 | |
| 5307 | if (palette_buffer) { |
| 5308 | if (palette_buffer_len < 256) |
| 5309 | return stbi__err("palette buffer too small" , "palette buffer len must be 256" ); |
| 5310 | else if (req_comp != 1) |
| 5311 | return stbi__err("invalid req_comp" , "req_comp must be 1 when loading paletted" ); |
| 5312 | else |
| 5313 | palette = (stbi_uc *)(void *)palette_buffer; |
| 5314 | } |
| 5315 | |
| 5316 | z->expanded = NULL; |
| 5317 | z->idata = NULL; |
| 5318 | z->out = NULL; |
| 5319 | |
| 5320 | if (!stbi__check_png_header(s)) return 0; |
| 5321 | |
| 5322 | if (scan == STBI__SCAN_type) return 1; |
| 5323 | |
| 5324 | for (;;) { |
| 5325 | stbi__pngchunk c = stbi__get_chunk_header(s); |
| 5326 | switch (c.type) { |
| 5327 | case STBI__PNG_TYPE('C','g','B','I'): |
| 5328 | is_iphone = 1; |
| 5329 | stbi__skip(s, c.length); |
| 5330 | break; |
| 5331 | case STBI__PNG_TYPE('I','H','D','R'): { |
| 5332 | int comp,filter; |
| 5333 | if (!first) return stbi__err("multiple IHDR" ,"Corrupt PNG" ); |
| 5334 | first = 0; |
| 5335 | if (c.length != 13) return stbi__err("bad IHDR len" ,"Corrupt PNG" ); |
| 5336 | s->img_x = stbi__get32be(s); |
| 5337 | s->img_y = stbi__get32be(s); |
| 5338 | if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large" ,"Very large image (corrupt?)" ); |
| 5339 | if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large" ,"Very large image (corrupt?)" ); |
| 5340 | 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" ); |
| 5341 | color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype" ,"Corrupt PNG" ); |
| 5342 | if (color == 3 && z->depth == 16) return stbi__err("bad ctype" ,"Corrupt PNG" ); |
| 5343 | if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype" ,"Corrupt PNG" ); |
| 5344 | comp = stbi__get8(s); if (comp) return stbi__err("bad comp method" ,"Corrupt PNG" ); |
| 5345 | filter= stbi__get8(s); if (filter) return stbi__err("bad filter method" ,"Corrupt PNG" ); |
| 5346 | interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method" ,"Corrupt PNG" ); |
| 5347 | if (!s->img_x || !s->img_y) return stbi__err("0-pixel image" ,"Corrupt PNG" ); |
| 5348 | if (!pal_img_n) { |
| 5349 | s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0); |
| 5350 | if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large" , "Image too large to decode" ); |
| 5351 | } else { |
| 5352 | // if paletted, then pal_n is our final components, and |
| 5353 | // img_n is # components to decompress/filter. |
| 5354 | s->img_n = 1; |
| 5355 | if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large" ,"Corrupt PNG" ); |
| 5356 | } |
| 5357 | // even with SCAN_header, have to scan to see if we have a tRNS |
| 5358 | break; |
| 5359 | } |
| 5360 | |
| 5361 | case STBI__PNG_TYPE('P','L','T','E'): { |
| 5362 | if (first) return stbi__err("first not IHDR" , "Corrupt PNG" ); |
| 5363 | if (c.length > 256*3) return stbi__err("invalid PLTE" ,"Corrupt PNG" ); |
| 5364 | pal_len = c.length / 3; |
| 5365 | if (pal_len * 3 != c.length) return stbi__err("invalid PLTE" ,"Corrupt PNG" ); |
| 5366 | for (i=0; i < pal_len; ++i) { |
| 5367 | palette[i*4+0] = stbi__get8(s); |
| 5368 | palette[i*4+1] = stbi__get8(s); |
| 5369 | palette[i*4+2] = stbi__get8(s); |
| 5370 | palette[i*4+3] = 255; |
| 5371 | } |
| 5372 | break; |
| 5373 | } |
| 5374 | |
| 5375 | case STBI__PNG_TYPE('t','R','N','S'): { |
| 5376 | if (first) return stbi__err("first not IHDR" , "Corrupt PNG" ); |
| 5377 | if (z->idata) return stbi__err("tRNS after IDAT" ,"Corrupt PNG" ); |
| 5378 | if (pal_img_n) { |
| 5379 | if (scan == STBI__SCAN_header) { s->img_n = 4; return 1; } |
| 5380 | if (pal_len == 0) return stbi__err("tRNS before PLTE" ,"Corrupt PNG" ); |
| 5381 | if (c.length > pal_len) return stbi__err("bad tRNS len" ,"Corrupt PNG" ); |
| 5382 | pal_img_n = 4; |
| 5383 | for (i=0; i < c.length; ++i) |
| 5384 | palette[i*4+3] = stbi__get8(s); |
| 5385 | } else { |
| 5386 | if (!(s->img_n & 1)) return stbi__err("tRNS with alpha" ,"Corrupt PNG" ); |
| 5387 | if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len" ,"Corrupt PNG" ); |
| 5388 | has_trans = 1; |
| 5389 | // non-paletted with tRNS = constant alpha. if header-scanning, we can stop now. |
| 5390 | if (scan == STBI__SCAN_header) { ++s->img_n; return 1; } |
| 5391 | if (z->depth == 16) { |
| 5392 | for (k = 0; k < s->img_n && k < 3; ++k) // extra loop test to suppress false GCC warning |
| 5393 | tc16[k] = (stbi__uint16)stbi__get16be(s); // copy the values as-is |
| 5394 | } else { |
| 5395 | for (k = 0; k < s->img_n && k < 3; ++k) |
| 5396 | tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth]; // non 8-bit images will be larger |
| 5397 | } |
| 5398 | } |
| 5399 | break; |
| 5400 | } |
| 5401 | |
| 5402 | case STBI__PNG_TYPE('I','D','A','T'): { |
| 5403 | if (first) return stbi__err("first not IHDR" , "Corrupt PNG" ); |
| 5404 | if (pal_img_n && !pal_len) return stbi__err("no PLTE" ,"Corrupt PNG" ); |
| 5405 | if (scan == STBI__SCAN_header) { |
| 5406 | // header scan definitely stops at first IDAT |
| 5407 | if (pal_img_n) |
| 5408 | s->img_n = pal_img_n; |
| 5409 | return 1; |
| 5410 | } |
| 5411 | if (c.length > (1u << 30)) return stbi__err("IDAT size limit" , "IDAT section larger than 2^30 bytes" ); |
| 5412 | if ((int)(ioff + c.length) < (int)ioff) return 0; |
| 5413 | if (ioff + c.length > idata_limit) { |
| 5414 | stbi__uint32 idata_limit_old = idata_limit; |
| 5415 | stbi_uc *p; |
| 5416 | if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096; |
| 5417 | while (ioff + c.length > idata_limit) |
| 5418 | idata_limit *= 2; |
| 5419 | STBI_NOTUSED(idata_limit_old); |
| 5420 | p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem" , "Out of memory" ); |
| 5421 | z->idata = p; |
| 5422 | } |
| 5423 | if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata" ,"Corrupt PNG" ); |
| 5424 | ioff += c.length; |
| 5425 | break; |
| 5426 | } |
| 5427 | |
| 5428 | case STBI__PNG_TYPE('I','E','N','D'): { |
| 5429 | stbi__uint32 raw_len, bpl; |
| 5430 | if (first) return stbi__err("first not IHDR" , "Corrupt PNG" ); |
| 5431 | if (scan != STBI__SCAN_load) return 1; |
| 5432 | if (z->idata == NULL) return stbi__err("no IDAT" ,"Corrupt PNG" ); |
| 5433 | // initial guess for decoded data size to avoid unnecessary reallocs |
| 5434 | bpl = (s->img_x * z->depth + 7) / 8; // bytes per line, per component |
| 5435 | raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */; |
| 5436 | z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone); |
| 5437 | if (z->expanded == NULL) return 0; // zlib should set error |
| 5438 | STBI_FREE(z->idata); z->idata = NULL; |
| 5439 | if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans) |
| 5440 | s->img_out_n = s->img_n+1; |
| 5441 | else |
| 5442 | s->img_out_n = s->img_n; |
| 5443 | if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0; |
| 5444 | if (has_trans) { |
| 5445 | if (z->depth == 16) { |
| 5446 | if (!stbi__compute_transparency16(z, tc16, s->img_out_n)) return 0; |
| 5447 | } else { |
| 5448 | if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0; |
| 5449 | } |
| 5450 | } |
| 5451 | if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2) |
| 5452 | stbi__de_iphone(z); |
| 5453 | if (pal_img_n) { |
| 5454 | // pal_img_n == 3 or 4 |
| 5455 | s->img_n = pal_img_n; // record the actual colors we had |
| 5456 | s->img_out_n = pal_img_n; |
| 5457 | if (req_comp >= 3) s->img_out_n = req_comp; |
| 5458 | if (!palette_buffer) |
| 5459 | if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n)) |
| 5460 | return 0; |
| 5461 | } else if (has_trans) { |
| 5462 | // non-paletted image with tRNS -> source image has (constant) alpha |
| 5463 | ++s->img_n; |
| 5464 | } |
| 5465 | STBI_FREE(z->expanded); z->expanded = NULL; |
| 5466 | // end of PNG chunk, read and skip CRC |
| 5467 | stbi__get32be(s); |
| 5468 | if (s->io.skip && s->img_buffer_end > s->img_buffer) { |
| 5469 | // rewind the additional bytes that have been read to the buffer |
| 5470 | (s->io.skip)(s->io_user_data, (int)(s->img_buffer - s->img_buffer_end)); |
| 5471 | } |
| 5472 | return 1; |
| 5473 | } |
| 5474 | |
| 5475 | default: |
| 5476 | // if critical, fail |
| 5477 | if (first) return stbi__err("first not IHDR" , "Corrupt PNG" ); |
| 5478 | if ((c.type & (1 << 29)) == 0) { |
| 5479 | #ifndef STBI_NO_FAILURE_STRINGS |
| 5480 | // not threadsafe |
| 5481 | static char invalid_chunk[] = "XXXX PNG chunk not known" ; |
| 5482 | invalid_chunk[0] = STBI__BYTECAST(c.type >> 24); |
| 5483 | invalid_chunk[1] = STBI__BYTECAST(c.type >> 16); |
| 5484 | invalid_chunk[2] = STBI__BYTECAST(c.type >> 8); |
| 5485 | invalid_chunk[3] = STBI__BYTECAST(c.type >> 0); |
| 5486 | (void)invalid_chunk; |
| 5487 | #endif |
| 5488 | return stbi__err(invalid_chunk, "PNG not supported: unknown PNG chunk type" ); |
| 5489 | } |
| 5490 | stbi__skip(s, c.length); |
| 5491 | break; |
| 5492 | } |
| 5493 | // end of PNG chunk, read and skip CRC |
| 5494 | stbi__get32be(s); |
| 5495 | } |
| 5496 | } |
| 5497 | |
| 5498 | static 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) |
| 5499 | { |
| 5500 | void *result=NULL; |
| 5501 | if (palette_buffer && req_comp != 1) { |
| 5502 | stbi__err("bad req_comp" , "req_comp must be 1 if loading paletted image without expansion" ); |
| 5503 | return NULL; |
| 5504 | } |
| 5505 | if (req_comp < 0 || req_comp > 4) { |
| 5506 | stbi__err("bad req_comp" , "Internal error" ); |
| 5507 | return NULL; |
| 5508 | } |
| 5509 | if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp, palette_buffer, palette_buffer_len)) { |
| 5510 | if (p->depth <= 8) |
| 5511 | ri->bits_per_channel = 8; |
| 5512 | else if (p->depth == 16) |
| 5513 | ri->bits_per_channel = 16; |
| 5514 | else |
| 5515 | return stbi__errpuc("bad bits_per_channel" , "PNG not supported: unsupported color depth" ); |
| 5516 | result = p->out; |
| 5517 | p->out = NULL; |
| 5518 | if (req_comp && req_comp != p->s->img_out_n) { |
| 5519 | if (palette_buffer) |
| 5520 | ; |
| 5521 | else if (ri->bits_per_channel == 8) |
| 5522 | result = stbi__convert_format((unsigned char *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y); |
| 5523 | else |
| 5524 | result = stbi__convert_format16((stbi__uint16 *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y); |
| 5525 | p->s->img_out_n = req_comp; |
| 5526 | if (result == NULL) return result; |
| 5527 | } |
| 5528 | *x = p->s->img_x; |
| 5529 | *y = p->s->img_y; |
| 5530 | if (n) { |
| 5531 | if (palette_buffer) |
| 5532 | *n = 1; |
| 5533 | else |
| 5534 | *n = p->s->img_n; |
| 5535 | } |
| 5536 | } |
| 5537 | STBI_FREE(p->out); p->out = NULL; |
| 5538 | STBI_FREE(p->expanded); p->expanded = NULL; |
| 5539 | STBI_FREE(p->idata); p->idata = NULL; |
| 5540 | |
| 5541 | return result; |
| 5542 | } |
| 5543 | |
| 5544 | static 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) |
| 5545 | { |
| 5546 | stbi__png p; |
| 5547 | p.s = s; |
| 5548 | return stbi__do_png(&p, x,y,comp,req_comp, palette_buffer, palette_buffer_len, ri); |
| 5549 | } |
| 5550 | |
| 5551 | static int stbi__png_test(stbi__context *s) |
| 5552 | { |
| 5553 | int r; |
| 5554 | r = stbi__check_png_header(s); |
| 5555 | stbi__rewind(s); |
| 5556 | return r; |
| 5557 | } |
| 5558 | |
| 5559 | #if 0 /* not used in SDL */ |
| 5560 | static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp) |
| 5561 | { |
| 5562 | if (!stbi__parse_png_file(p, STBI__SCAN_header, NULL, 0, NULL)) { |
| 5563 | stbi__rewind( p->s ); |
| 5564 | return 0; |
| 5565 | } |
| 5566 | if (x) *x = p->s->img_x; |
| 5567 | if (y) *y = p->s->img_y; |
| 5568 | if (comp) *comp = p->s->img_n; |
| 5569 | return 1; |
| 5570 | } |
| 5571 | |
| 5572 | static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp) |
| 5573 | { |
| 5574 | stbi__png p; |
| 5575 | p.s = s; |
| 5576 | return stbi__png_info_raw(&p, x, y, comp); |
| 5577 | } |
| 5578 | |
| 5579 | static int stbi__png_is16(stbi__context *s) |
| 5580 | { |
| 5581 | stbi__png p; |
| 5582 | p.s = s; |
| 5583 | if (!stbi__png_info_raw(&p, NULL, NULL, NULL)) |
| 5584 | return 0; |
| 5585 | if (p.depth != 16) { |
| 5586 | stbi__rewind(p.s); |
| 5587 | return 0; |
| 5588 | } |
| 5589 | return 1; |
| 5590 | } |
| 5591 | #endif /**/ |
| 5592 | #endif |
| 5593 | |
| 5594 | // Microsoft/Windows BMP image |
| 5595 | |
| 5596 | #ifndef STBI_NO_BMP |
| 5597 | static int stbi__bmp_test_raw(stbi__context *s) |
| 5598 | { |
| 5599 | int r; |
| 5600 | int sz; |
| 5601 | if (stbi__get8(s) != 'B') return 0; |
| 5602 | if (stbi__get8(s) != 'M') return 0; |
| 5603 | stbi__get32le(s); // discard filesize |
| 5604 | stbi__get16le(s); // discard reserved |
| 5605 | stbi__get16le(s); // discard reserved |
| 5606 | stbi__get32le(s); // discard data offset |
| 5607 | sz = stbi__get32le(s); |
| 5608 | r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124); |
| 5609 | return r; |
| 5610 | } |
| 5611 | |
| 5612 | static int stbi__bmp_test(stbi__context *s) |
| 5613 | { |
| 5614 | int r = stbi__bmp_test_raw(s); |
| 5615 | stbi__rewind(s); |
| 5616 | return r; |
| 5617 | } |
| 5618 | |
| 5619 | |
| 5620 | // returns 0..31 for the highest set bit |
| 5621 | static int stbi__high_bit(unsigned int z) |
| 5622 | { |
| 5623 | int n=0; |
| 5624 | if (z == 0) return -1; |
| 5625 | if (z >= 0x10000) { n += 16; z >>= 16; } |
| 5626 | if (z >= 0x00100) { n += 8; z >>= 8; } |
| 5627 | if (z >= 0x00010) { n += 4; z >>= 4; } |
| 5628 | if (z >= 0x00004) { n += 2; z >>= 2; } |
| 5629 | if (z >= 0x00002) { n += 1;/* >>= 1;*/ } |
| 5630 | return n; |
| 5631 | } |
| 5632 | |
| 5633 | static int stbi__bitcount(unsigned int a) |
| 5634 | { |
| 5635 | a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2 |
| 5636 | a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4 |
| 5637 | a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits |
| 5638 | a = (a + (a >> 8)); // max 16 per 8 bits |
| 5639 | a = (a + (a >> 16)); // max 32 per 8 bits |
| 5640 | return a & 0xff; |
| 5641 | } |
| 5642 | |
| 5643 | // extract an arbitrarily-aligned N-bit value (N=bits) |
| 5644 | // from v, and then make it 8-bits long and fractionally |
| 5645 | // extend it to full full range. |
| 5646 | static int stbi__shiftsigned(unsigned int v, int shift, int bits) |
| 5647 | { |
| 5648 | static unsigned int mul_table[9] = { |
| 5649 | 0, |
| 5650 | 0xff/*0b11111111*/, 0x55/*0b01010101*/, 0x49/*0b01001001*/, 0x11/*0b00010001*/, |
| 5651 | 0x21/*0b00100001*/, 0x41/*0b01000001*/, 0x81/*0b10000001*/, 0x01/*0b00000001*/, |
| 5652 | }; |
| 5653 | static unsigned int shift_table[9] = { |
| 5654 | 0, 0,0,1,0,2,4,6,0, |
| 5655 | }; |
| 5656 | if (shift < 0) |
| 5657 | v <<= -shift; |
| 5658 | else |
| 5659 | v >>= shift; |
| 5660 | STBI_ASSERT(v < 256); |
| 5661 | v >>= (8-bits); |
| 5662 | STBI_ASSERT(bits >= 0 && bits <= 8); |
| 5663 | return (int) ((unsigned) v * mul_table[bits]) >> shift_table[bits]; |
| 5664 | } |
| 5665 | |
| 5666 | typedef struct |
| 5667 | { |
| 5668 | int bpp, offset, hsz; |
| 5669 | unsigned int mr,mg,mb,ma, all_a; |
| 5670 | int extra_read; |
| 5671 | } stbi__bmp_data; |
| 5672 | |
| 5673 | static int stbi__bmp_set_mask_defaults(stbi__bmp_data *info, int compress) |
| 5674 | { |
| 5675 | // BI_BITFIELDS specifies masks explicitly, don't override |
| 5676 | if (compress == 3) |
| 5677 | return 1; |
| 5678 | |
| 5679 | if (compress == 0) { |
| 5680 | if (info->bpp == 16) { |
| 5681 | info->mr = 31u << 10; |
| 5682 | info->mg = 31u << 5; |
| 5683 | info->mb = 31u << 0; |
| 5684 | } else if (info->bpp == 32) { |
| 5685 | info->mr = 0xffu << 16; |
| 5686 | info->mg = 0xffu << 8; |
| 5687 | info->mb = 0xffu << 0; |
| 5688 | info->ma = 0xffu << 24; |
| 5689 | info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0 |
| 5690 | } else { |
| 5691 | // otherwise, use defaults, which is all-0 |
| 5692 | info->mr = info->mg = info->mb = info->ma = 0; |
| 5693 | } |
| 5694 | return 1; |
| 5695 | } |
| 5696 | return 0; // error |
| 5697 | } |
| 5698 | |
| 5699 | static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) |
| 5700 | { |
| 5701 | int hsz; |
| 5702 | if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP" , "Corrupt BMP" ); |
| 5703 | stbi__get32le(s); // discard filesize |
| 5704 | stbi__get16le(s); // discard reserved |
| 5705 | stbi__get16le(s); // discard reserved |
| 5706 | info->offset = stbi__get32le(s); |
| 5707 | info->hsz = hsz = stbi__get32le(s); |
| 5708 | info->mr = info->mg = info->mb = info->ma = 0; |
| 5709 | info->extra_read = 14; |
| 5710 | |
| 5711 | if (info->offset < 0) return stbi__errpuc("bad BMP" , "bad BMP" ); |
| 5712 | |
| 5713 | if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP" , "BMP type not supported: unknown" ); |
| 5714 | if (hsz == 12) { |
| 5715 | s->img_x = stbi__get16le(s); |
| 5716 | s->img_y = stbi__get16le(s); |
| 5717 | } else { |
| 5718 | s->img_x = stbi__get32le(s); |
| 5719 | s->img_y = stbi__get32le(s); |
| 5720 | } |
| 5721 | if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP" , "bad BMP" ); |
| 5722 | info->bpp = stbi__get16le(s); |
| 5723 | if (hsz != 12) { |
| 5724 | int compress = stbi__get32le(s); |
| 5725 | if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE" , "BMP type not supported: RLE" ); |
| 5726 | if (compress >= 4) return stbi__errpuc("BMP JPEG/PNG" , "BMP type not supported: unsupported compression" ); // this includes PNG/JPEG modes |
| 5727 | if (compress == 3 && info->bpp != 16 && info->bpp != 32) return stbi__errpuc("bad BMP" , "bad BMP" ); // bitfields requires 16 or 32 bits/pixel |
| 5728 | stbi__get32le(s); // discard sizeof |
| 5729 | stbi__get32le(s); // discard hres |
| 5730 | stbi__get32le(s); // discard vres |
| 5731 | stbi__get32le(s); // discard colorsused |
| 5732 | stbi__get32le(s); // discard max important |
| 5733 | if (hsz == 40 || hsz == 56) { |
| 5734 | if (hsz == 56) { |
| 5735 | stbi__get32le(s); |
| 5736 | stbi__get32le(s); |
| 5737 | stbi__get32le(s); |
| 5738 | stbi__get32le(s); |
| 5739 | } |
| 5740 | if (info->bpp == 16 || info->bpp == 32) { |
| 5741 | if (compress == 0) { |
| 5742 | stbi__bmp_set_mask_defaults(info, compress); |
| 5743 | } else if (compress == 3) { |
| 5744 | info->mr = stbi__get32le(s); |
| 5745 | info->mg = stbi__get32le(s); |
| 5746 | info->mb = stbi__get32le(s); |
| 5747 | info->extra_read += 12; |
| 5748 | // not documented, but generated by photoshop and handled by mspaint |
| 5749 | if (info->mr == info->mg && info->mg == info->mb) { |
| 5750 | // ?!?!? |
| 5751 | return stbi__errpuc("bad BMP" , "bad BMP" ); |
| 5752 | } |
| 5753 | } else |
| 5754 | return stbi__errpuc("bad BMP" , "bad BMP" ); |
| 5755 | } |
| 5756 | } else { |
| 5757 | // V4/V5 header |
| 5758 | int i; |
| 5759 | if (hsz != 108 && hsz != 124) |
| 5760 | return stbi__errpuc("bad BMP" , "bad BMP" ); |
| 5761 | info->mr = stbi__get32le(s); |
| 5762 | info->mg = stbi__get32le(s); |
| 5763 | info->mb = stbi__get32le(s); |
| 5764 | info->ma = stbi__get32le(s); |
| 5765 | if (compress != 3) // override mr/mg/mb unless in BI_BITFIELDS mode, as per docs |
| 5766 | stbi__bmp_set_mask_defaults(info, compress); |
| 5767 | stbi__get32le(s); // discard color space |
| 5768 | for (i=0; i < 12; ++i) |
| 5769 | stbi__get32le(s); // discard color space parameters |
| 5770 | if (hsz == 124) { |
| 5771 | stbi__get32le(s); // discard rendering intent |
| 5772 | stbi__get32le(s); // discard offset of profile data |
| 5773 | stbi__get32le(s); // discard size of profile data |
| 5774 | stbi__get32le(s); // discard reserved |
| 5775 | } |
| 5776 | } |
| 5777 | } |
| 5778 | return (void *) 1; |
| 5779 | } |
| 5780 | |
| 5781 | |
| 5782 | static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) |
| 5783 | { |
| 5784 | stbi_uc *out; |
| 5785 | unsigned int mr=0,mg=0,mb=0,ma=0, all_a; |
| 5786 | stbi_uc pal[256][4]; |
| 5787 | int psize=0,i,j,width; |
| 5788 | int flip_vertically, pad, target; |
| 5789 | stbi__bmp_data info; |
| 5790 | STBI_NOTUSED(ri); |
| 5791 | |
| 5792 | info.all_a = 255; |
| 5793 | if (stbi__bmp_parse_header(s, &info) == NULL) |
| 5794 | return NULL; // error code already set |
| 5795 | |
| 5796 | flip_vertically = ((int) s->img_y) > 0; |
| 5797 | s->img_y = abs((int) s->img_y); |
| 5798 | |
| 5799 | if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large" ,"Very large image (corrupt?)" ); |
| 5800 | if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large" ,"Very large image (corrupt?)" ); |
| 5801 | |
| 5802 | mr = info.mr; |
| 5803 | mg = info.mg; |
| 5804 | mb = info.mb; |
| 5805 | ma = info.ma; |
| 5806 | all_a = info.all_a; |
| 5807 | |
| 5808 | if (info.hsz == 12) { |
| 5809 | if (info.bpp < 24) |
| 5810 | psize = (info.offset - info.extra_read - 24) / 3; |
| 5811 | } else { |
| 5812 | if (info.bpp < 16) |
| 5813 | psize = (info.offset - info.extra_read - info.hsz) >> 2; |
| 5814 | } |
| 5815 | if (psize == 0) { |
| 5816 | // accept some number of extra bytes after the header, but if the offset points either to before |
| 5817 | // the header ends or implies a large amount of extra data, reject the file as malformed |
| 5818 | int bytes_read_so_far = s->callback_already_read + (int)(s->img_buffer - s->img_buffer_original); |
| 5819 | int header_limit = 1024; // max we actually read is below 256 bytes currently. |
| 5820 | int extra_data_limit = 256*4; // what ordinarily goes here is a palette; 256 entries*4 bytes is its max size. |
| 5821 | if (bytes_read_so_far <= 0 || bytes_read_so_far > header_limit) { |
| 5822 | return stbi__errpuc("bad header" , "Corrupt BMP" ); |
| 5823 | } |
| 5824 | // we established that bytes_read_so_far is positive and sensible. |
| 5825 | // the first half of this test rejects offsets that are either too small positives, or |
| 5826 | // negative, and guarantees that info.offset >= bytes_read_so_far > 0. this in turn |
| 5827 | // ensures the number computed in the second half of the test can't overflow. |
| 5828 | if (info.offset < bytes_read_so_far || info.offset - bytes_read_so_far > extra_data_limit) { |
| 5829 | return stbi__errpuc("bad offset" , "Corrupt BMP" ); |
| 5830 | } else { |
| 5831 | stbi__skip(s, info.offset - bytes_read_so_far); |
| 5832 | } |
| 5833 | } |
| 5834 | |
| 5835 | if (info.bpp == 24 && ma == 0xff000000) |
| 5836 | s->img_n = 3; |
| 5837 | else |
| 5838 | s->img_n = ma ? 4 : 3; |
| 5839 | if (req_comp && req_comp >= 3) // we can directly decode 3 or 4 |
| 5840 | target = req_comp; |
| 5841 | else |
| 5842 | target = s->img_n; // if they want monochrome, we'll post-convert |
| 5843 | |
| 5844 | // sanity-check size |
| 5845 | if (!stbi__mad3sizes_valid(target, s->img_x, s->img_y, 0)) |
| 5846 | return stbi__errpuc("too large" , "Corrupt BMP" ); |
| 5847 | |
| 5848 | out = (stbi_uc *) stbi__malloc_mad3(target, s->img_x, s->img_y, 0); |
| 5849 | if (!out) return stbi__errpuc("outofmem" , "Out of memory" ); |
| 5850 | if (info.bpp < 16) { |
| 5851 | int z=0; |
| 5852 | if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid" , "Corrupt BMP" ); } |
| 5853 | for (i=0; i < psize; ++i) { |
| 5854 | pal[i][2] = stbi__get8(s); |
| 5855 | pal[i][1] = stbi__get8(s); |
| 5856 | pal[i][0] = stbi__get8(s); |
| 5857 | if (info.hsz != 12) stbi__get8(s); |
| 5858 | pal[i][3] = 255; |
| 5859 | } |
| 5860 | stbi__skip(s, info.offset - info.extra_read - info.hsz - psize * (info.hsz == 12 ? 3 : 4)); |
| 5861 | if (info.bpp == 1) width = (s->img_x + 7) >> 3; |
| 5862 | else if (info.bpp == 4) width = (s->img_x + 1) >> 1; |
| 5863 | else if (info.bpp == 8) width = s->img_x; |
| 5864 | else { STBI_FREE(out); return stbi__errpuc("bad bpp" , "Corrupt BMP" ); } |
| 5865 | pad = (-width)&3; |
| 5866 | if (info.bpp == 1) { |
| 5867 | for (j=0; j < (int) s->img_y; ++j) { |
| 5868 | int bit_offset = 7, v = stbi__get8(s); |
| 5869 | for (i=0; i < (int) s->img_x; ++i) { |
| 5870 | int color = (v>>bit_offset)&0x1; |
| 5871 | out[z++] = pal[color][0]; |
| 5872 | out[z++] = pal[color][1]; |
| 5873 | out[z++] = pal[color][2]; |
| 5874 | if (target == 4) out[z++] = 255; |
| 5875 | if (i+1 == (int) s->img_x) break; |
| 5876 | if((--bit_offset) < 0) { |
| 5877 | bit_offset = 7; |
| 5878 | v = stbi__get8(s); |
| 5879 | } |
| 5880 | } |
| 5881 | stbi__skip(s, pad); |
| 5882 | } |
| 5883 | } else { |
| 5884 | for (j=0; j < (int) s->img_y; ++j) { |
| 5885 | for (i=0; i < (int) s->img_x; i += 2) { |
| 5886 | int v=stbi__get8(s),v2=0; |
| 5887 | if (info.bpp == 4) { |
| 5888 | v2 = v & 15; |
| 5889 | v >>= 4; |
| 5890 | } |
| 5891 | out[z++] = pal[v][0]; |
| 5892 | out[z++] = pal[v][1]; |
| 5893 | out[z++] = pal[v][2]; |
| 5894 | if (target == 4) out[z++] = 255; |
| 5895 | if (i+1 == (int) s->img_x) break; |
| 5896 | v = (info.bpp == 8) ? stbi__get8(s) : v2; |
| 5897 | out[z++] = pal[v][0]; |
| 5898 | out[z++] = pal[v][1]; |
| 5899 | out[z++] = pal[v][2]; |
| 5900 | if (target == 4) out[z++] = 255; |
| 5901 | } |
| 5902 | stbi__skip(s, pad); |
| 5903 | } |
| 5904 | } |
| 5905 | } else { |
| 5906 | int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0; |
| 5907 | int z = 0; |
| 5908 | int easy=0; |
| 5909 | stbi__skip(s, info.offset - info.extra_read - info.hsz); |
| 5910 | if (info.bpp == 24) width = 3 * s->img_x; |
| 5911 | else if (info.bpp == 16) width = 2*s->img_x; |
| 5912 | else /* bpp = 32 and pad = 0 */ width=0; |
| 5913 | pad = (-width) & 3; |
| 5914 | if (info.bpp == 24) { |
| 5915 | easy = 1; |
| 5916 | } else if (info.bpp == 32) { |
| 5917 | if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000) |
| 5918 | easy = 2; |
| 5919 | } |
| 5920 | if (!easy) { |
| 5921 | if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks" , "Corrupt BMP" ); } |
| 5922 | // right shift amt to put high bit in position #7 |
| 5923 | rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr); |
| 5924 | gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg); |
| 5925 | bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb); |
| 5926 | ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma); |
| 5927 | if (rcount > 8 || gcount > 8 || bcount > 8 || acount > 8) { STBI_FREE(out); return stbi__errpuc("bad masks" , "Corrupt BMP" ); } |
| 5928 | } |
| 5929 | for (j=0; j < (int) s->img_y; ++j) { |
| 5930 | if (easy) { |
| 5931 | for (i=0; i < (int) s->img_x; ++i) { |
| 5932 | unsigned char a; |
| 5933 | out[z+2] = stbi__get8(s); |
| 5934 | out[z+1] = stbi__get8(s); |
| 5935 | out[z+0] = stbi__get8(s); |
| 5936 | z += 3; |
| 5937 | a = (easy == 2 ? stbi__get8(s) : 255); |
| 5938 | all_a |= a; |
| 5939 | if (target == 4) out[z++] = a; |
| 5940 | } |
| 5941 | } else { |
| 5942 | int bpp = info.bpp; |
| 5943 | for (i=0; i < (int) s->img_x; ++i) { |
| 5944 | stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s)); |
| 5945 | unsigned int a; |
| 5946 | out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount)); |
| 5947 | out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount)); |
| 5948 | out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount)); |
| 5949 | a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255); |
| 5950 | all_a |= a; |
| 5951 | if (target == 4) out[z++] = STBI__BYTECAST(a); |
| 5952 | } |
| 5953 | } |
| 5954 | stbi__skip(s, pad); |
| 5955 | } |
| 5956 | } |
| 5957 | |
| 5958 | // if alpha channel is all 0s, replace with all 255s |
| 5959 | if (target == 4 && all_a == 0) |
| 5960 | for (i=4*s->img_x*s->img_y-1; i >= 0; i -= 4) |
| 5961 | out[i] = 255; |
| 5962 | |
| 5963 | if (flip_vertically) { |
| 5964 | stbi_uc t; |
| 5965 | for (j=0; j < (int) s->img_y>>1; ++j) { |
| 5966 | stbi_uc *p1 = out + j *s->img_x*target; |
| 5967 | stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target; |
| 5968 | for (i=0; i < (int) s->img_x*target; ++i) { |
| 5969 | t = p1[i]; p1[i] = p2[i]; p2[i] = t; |
| 5970 | } |
| 5971 | } |
| 5972 | } |
| 5973 | |
| 5974 | if (req_comp && req_comp != target) { |
| 5975 | out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y); |
| 5976 | if (out == NULL) return out; // stbi__convert_format frees input on failure |
| 5977 | } |
| 5978 | |
| 5979 | *x = s->img_x; |
| 5980 | *y = s->img_y; |
| 5981 | if (comp) *comp = s->img_n; |
| 5982 | return out; |
| 5983 | } |
| 5984 | #endif |
| 5985 | |
| 5986 | // Targa Truevision - TGA |
| 5987 | // by Jonathan Dummer |
| 5988 | #ifndef STBI_NO_TGA |
| 5989 | // returns STBI_rgb or whatever, 0 on error |
| 5990 | static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16) |
| 5991 | { |
| 5992 | // only RGB or RGBA (incl. 16bit) or grey allowed |
| 5993 | if (is_rgb16) *is_rgb16 = 0; |
| 5994 | switch(bits_per_pixel) { |
| 5995 | case 8: return STBI_grey; |
| 5996 | case 16: if(is_grey) return STBI_grey_alpha; |
| 5997 | // fallthrough |
| 5998 | case 15: if(is_rgb16) *is_rgb16 = 1; |
| 5999 | return STBI_rgb; |
| 6000 | case 24: // fallthrough |
| 6001 | case 32: return bits_per_pixel/8; |
| 6002 | default: return 0; |
| 6003 | } |
| 6004 | } |
| 6005 | |
| 6006 | static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp) |
| 6007 | { |
| 6008 | int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp; |
| 6009 | int sz, tga_colormap_type; |
| 6010 | stbi__get8(s); // discard Offset |
| 6011 | tga_colormap_type = stbi__get8(s); // colormap type |
| 6012 | if( tga_colormap_type > 1 ) { |
| 6013 | stbi__rewind(s); |
| 6014 | return 0; // only RGB or indexed allowed |
| 6015 | } |
| 6016 | tga_image_type = stbi__get8(s); // image type |
| 6017 | if ( tga_colormap_type == 1 ) { // colormapped (paletted) image |
| 6018 | if (tga_image_type != 1 && tga_image_type != 9) { |
| 6019 | stbi__rewind(s); |
| 6020 | return 0; |
| 6021 | } |
| 6022 | stbi__skip(s,4); // skip index of first colormap entry and number of entries |
| 6023 | sz = stbi__get8(s); // check bits per palette color entry |
| 6024 | if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) { |
| 6025 | stbi__rewind(s); |
| 6026 | return 0; |
| 6027 | } |
| 6028 | stbi__skip(s,4); // skip image x and y origin |
| 6029 | tga_colormap_bpp = sz; |
| 6030 | } else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE |
| 6031 | if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) { |
| 6032 | stbi__rewind(s); |
| 6033 | return 0; // only RGB or grey allowed, +/- RLE |
| 6034 | } |
| 6035 | stbi__skip(s,9); // skip colormap specification and image x/y origin |
| 6036 | tga_colormap_bpp = 0; |
| 6037 | } |
| 6038 | tga_w = stbi__get16le(s); |
| 6039 | if( tga_w < 1 ) { |
| 6040 | stbi__rewind(s); |
| 6041 | return 0; // test width |
| 6042 | } |
| 6043 | tga_h = stbi__get16le(s); |
| 6044 | if( tga_h < 1 ) { |
| 6045 | stbi__rewind(s); |
| 6046 | return 0; // test height |
| 6047 | } |
| 6048 | tga_bits_per_pixel = stbi__get8(s); // bits per pixel |
| 6049 | stbi__get8(s); // ignore alpha bits |
| 6050 | if (tga_colormap_bpp != 0) { |
| 6051 | if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) { |
| 6052 | // when using a colormap, tga_bits_per_pixel is the size of the indexes |
| 6053 | // I don't think anything but 8 or 16bit indexes makes sense |
| 6054 | stbi__rewind(s); |
| 6055 | return 0; |
| 6056 | } |
| 6057 | tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL); |
| 6058 | } else { |
| 6059 | tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL); |
| 6060 | } |
| 6061 | if(!tga_comp) { |
| 6062 | stbi__rewind(s); |
| 6063 | return 0; |
| 6064 | } |
| 6065 | if (x) *x = tga_w; |
| 6066 | if (y) *y = tga_h; |
| 6067 | if (comp) *comp = tga_comp; |
| 6068 | return 1; // seems to have passed everything |
| 6069 | } |
| 6070 | |
| 6071 | static int stbi__tga_test(stbi__context *s) |
| 6072 | { |
| 6073 | int res = 0; |
| 6074 | int sz, tga_color_type; |
| 6075 | stbi__get8(s); // discard Offset |
| 6076 | tga_color_type = stbi__get8(s); // color type |
| 6077 | if ( tga_color_type > 1 ) goto errorEnd; // only RGB or indexed allowed |
| 6078 | sz = stbi__get8(s); // image type |
| 6079 | if ( tga_color_type == 1 ) { // colormapped (paletted) image |
| 6080 | if (sz != 1 && sz != 9) goto errorEnd; // colortype 1 demands image type 1 or 9 |
| 6081 | stbi__skip(s,4); // skip index of first colormap entry and number of entries |
| 6082 | sz = stbi__get8(s); // check bits per palette color entry |
| 6083 | if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd; |
| 6084 | stbi__skip(s,4); // skip image x and y origin |
| 6085 | } else { // "normal" image w/o colormap |
| 6086 | if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) ) goto errorEnd; // only RGB or grey allowed, +/- RLE |
| 6087 | stbi__skip(s,9); // skip colormap specification and image x/y origin |
| 6088 | } |
| 6089 | if ( stbi__get16le(s) < 1 ) goto errorEnd; // test width |
| 6090 | if ( stbi__get16le(s) < 1 ) goto errorEnd; // test height |
| 6091 | sz = stbi__get8(s); // bits per pixel |
| 6092 | if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) ) goto errorEnd; // for colormapped images, bpp is size of an index |
| 6093 | if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd; |
| 6094 | |
| 6095 | res = 1; // if we got this far, everything's good and we can return 1 instead of 0 |
| 6096 | |
| 6097 | errorEnd: |
| 6098 | stbi__rewind(s); |
| 6099 | return res; |
| 6100 | } |
| 6101 | |
| 6102 | // read 16bit value and convert to 24bit RGB |
| 6103 | static void stbi__tga_read_rgb16(stbi__context *s, stbi_uc* out) |
| 6104 | { |
| 6105 | stbi__uint16 px = (stbi__uint16)stbi__get16le(s); |
| 6106 | stbi__uint16 fiveBitMask = 31; |
| 6107 | // we have 3 channels with 5bits each |
| 6108 | int r = (px >> 10) & fiveBitMask; |
| 6109 | int g = (px >> 5) & fiveBitMask; |
| 6110 | int b = px & fiveBitMask; |
| 6111 | // Note that this saves the data in RGB(A) order, so it doesn't need to be swapped later |
| 6112 | out[0] = (stbi_uc)((r * 255)/31); |
| 6113 | out[1] = (stbi_uc)((g * 255)/31); |
| 6114 | out[2] = (stbi_uc)((b * 255)/31); |
| 6115 | |
| 6116 | // some people claim that the most significant bit might be used for alpha |
| 6117 | // (possibly if an alpha-bit is set in the "image descriptor byte") |
| 6118 | // but that only made 16bit test images completely translucent.. |
| 6119 | // so let's treat all 15 and 16bit TGAs as RGB with no alpha. |
| 6120 | } |
| 6121 | |
| 6122 | static 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) |
| 6123 | { |
| 6124 | // read in the TGA header stuff |
| 6125 | int tga_offset = stbi__get8(s); |
| 6126 | int tga_indexed = stbi__get8(s); |
| 6127 | int tga_image_type = stbi__get8(s); |
| 6128 | int tga_is_RLE = 0; |
| 6129 | int tga_palette_start = stbi__get16le(s); |
| 6130 | int tga_palette_len = stbi__get16le(s); |
| 6131 | int tga_palette_bits = stbi__get8(s); |
| 6132 | int tga_x_origin = stbi__get16le(s); |
| 6133 | int tga_y_origin = stbi__get16le(s); |
| 6134 | int tga_width = stbi__get16le(s); |
| 6135 | int tga_height = stbi__get16le(s); |
| 6136 | int tga_bits_per_pixel = stbi__get8(s); |
| 6137 | int tga_comp, tga_rgb16=0; |
| 6138 | int tga_inverted = stbi__get8(s); |
| 6139 | // int tga_alpha_bits = tga_inverted & 15; // the 4 lowest bits - unused (useless?) |
| 6140 | // image data |
| 6141 | unsigned char *tga_data; |
| 6142 | unsigned char *tga_palette = NULL; |
| 6143 | int i, j; |
| 6144 | unsigned char raw_data[4] = {0}; |
| 6145 | int RLE_count = 0; |
| 6146 | int RLE_repeating = 0; |
| 6147 | int read_next_pixel = 1; |
| 6148 | STBI_NOTUSED(ri); |
| 6149 | STBI_NOTUSED(tga_x_origin); // @TODO |
| 6150 | STBI_NOTUSED(tga_y_origin); // @TODO |
| 6151 | |
| 6152 | if (tga_height > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large" ,"Very large image (corrupt?)" ); |
| 6153 | if (tga_width > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large" ,"Very large image (corrupt?)" ); |
| 6154 | |
| 6155 | // do a tiny bit of precessing |
| 6156 | if ( tga_image_type >= 8 ) |
| 6157 | { |
| 6158 | tga_image_type -= 8; |
| 6159 | tga_is_RLE = 1; |
| 6160 | } |
| 6161 | tga_inverted = 1 - ((tga_inverted >> 5) & 1); |
| 6162 | |
| 6163 | // If I'm paletted, then I'll use the number of bits from the palette |
| 6164 | if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16); |
| 6165 | else tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16); |
| 6166 | |
| 6167 | if(!tga_comp) // shouldn't really happen, stbi__tga_test() should have ensured basic consistency |
| 6168 | return stbi__errpuc("bad format" , "Can't find out TGA pixelformat" ); |
| 6169 | |
| 6170 | // tga info |
| 6171 | *x = tga_width; |
| 6172 | *y = tga_height; |
| 6173 | if (comp) *comp = tga_comp; |
| 6174 | |
| 6175 | if (!stbi__mad3sizes_valid(tga_width, tga_height, tga_comp, 0)) |
| 6176 | return stbi__errpuc("too large" , "Corrupt TGA" ); |
| 6177 | |
| 6178 | tga_data = (unsigned char*)stbi__malloc_mad3(tga_width, tga_height, tga_comp, 0); |
| 6179 | if (!tga_data) return stbi__errpuc("outofmem" , "Out of memory" ); |
| 6180 | |
| 6181 | // skip to the data's starting position (offset usually = 0) |
| 6182 | stbi__skip(s, tga_offset ); |
| 6183 | |
| 6184 | if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) { |
| 6185 | for (i=0; i < tga_height; ++i) { |
| 6186 | int row = tga_inverted ? tga_height -i - 1 : i; |
| 6187 | stbi_uc *tga_row = tga_data + row*tga_width*tga_comp; |
| 6188 | stbi__getn(s, tga_row, tga_width * tga_comp); |
| 6189 | } |
| 6190 | } else { |
| 6191 | // do I need to load a palette? |
| 6192 | if ( tga_indexed) |
| 6193 | { |
| 6194 | if (tga_palette_len == 0) { /* you have to have at least one entry! */ |
| 6195 | STBI_FREE(tga_data); |
| 6196 | return stbi__errpuc("bad palette" , "Corrupt TGA" ); |
| 6197 | } |
| 6198 | |
| 6199 | // any data to skip? (offset usually = 0) |
| 6200 | stbi__skip(s, tga_palette_start ); |
| 6201 | // load the palette |
| 6202 | if (palette_buffer) { |
| 6203 | if (palette_buffer_len < tga_palette_len * tga_comp) { |
| 6204 | STBI_FREE(tga_data); |
| 6205 | return stbi__errpuc("buffer too small" , "Palette buffer too small" ); |
| 6206 | } |
| 6207 | tga_palette = (unsigned char*)(void*)palette_buffer; |
| 6208 | } else { |
| 6209 | tga_palette = (unsigned char*)stbi__malloc_mad2(tga_palette_len, tga_comp, 0); |
| 6210 | if (!tga_palette) { |
| 6211 | STBI_FREE(tga_data); |
| 6212 | return stbi__errpuc("outofmem" , "Out of memory" ); |
| 6213 | } |
| 6214 | } |
| 6215 | if (tga_rgb16) { |
| 6216 | stbi_uc *pal_entry = tga_palette; |
| 6217 | STBI_ASSERT(tga_comp == STBI_rgb); |
| 6218 | for (i=0; i < tga_palette_len; ++i) { |
| 6219 | stbi__tga_read_rgb16(s, pal_entry); |
| 6220 | pal_entry += tga_comp; |
| 6221 | } |
| 6222 | } else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) { |
| 6223 | STBI_FREE(tga_data); |
| 6224 | if (!palette_buffer) |
| 6225 | STBI_FREE(tga_palette); |
| 6226 | return stbi__errpuc("bad palette" , "Corrupt TGA" ); |
| 6227 | } |
| 6228 | } |
| 6229 | // load the data |
| 6230 | for (i=0; i < tga_width * tga_height; ++i) |
| 6231 | { |
| 6232 | // if I'm in RLE mode, do I need to get a RLE stbi__pngchunk? |
| 6233 | if ( tga_is_RLE ) |
| 6234 | { |
| 6235 | if ( RLE_count == 0 ) |
| 6236 | { |
| 6237 | // yep, get the next byte as a RLE command |
| 6238 | int RLE_cmd = stbi__get8(s); |
| 6239 | RLE_count = 1 + (RLE_cmd & 127); |
| 6240 | RLE_repeating = RLE_cmd >> 7; |
| 6241 | read_next_pixel = 1; |
| 6242 | } else if ( !RLE_repeating ) |
| 6243 | { |
| 6244 | read_next_pixel = 1; |
| 6245 | } |
| 6246 | } else |
| 6247 | { |
| 6248 | read_next_pixel = 1; |
| 6249 | } |
| 6250 | // OK, if I need to read a pixel, do it now |
| 6251 | if ( read_next_pixel ) |
| 6252 | { |
| 6253 | // load however much data we did have |
| 6254 | if ( tga_indexed && !palette_buffer ) |
| 6255 | { |
| 6256 | // read in index, then perform the lookup |
| 6257 | int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s); |
| 6258 | if ( pal_idx >= tga_palette_len ) { |
| 6259 | // invalid index |
| 6260 | pal_idx = 0; |
| 6261 | } |
| 6262 | pal_idx *= tga_comp; |
| 6263 | for (j = 0; j < tga_comp; ++j) { |
| 6264 | raw_data[j] = tga_palette[pal_idx+j]; |
| 6265 | } |
| 6266 | } else if(tga_rgb16) { |
| 6267 | STBI_ASSERT(tga_comp == STBI_rgb); |
| 6268 | stbi__tga_read_rgb16(s, raw_data); |
| 6269 | } else { |
| 6270 | // read in the data raw |
| 6271 | for (j = 0; j < tga_comp; ++j) { |
| 6272 | raw_data[j] = stbi__get8(s); |
| 6273 | } |
| 6274 | } |
| 6275 | // clear the reading flag for the next pixel |
| 6276 | read_next_pixel = 0; |
| 6277 | } // end of reading a pixel |
| 6278 | |
| 6279 | // copy data |
| 6280 | for (j = 0; j < tga_comp; ++j) |
| 6281 | tga_data[i*tga_comp+j] = raw_data[j]; |
| 6282 | |
| 6283 | // in case we're in RLE mode, keep counting down |
| 6284 | --RLE_count; |
| 6285 | } |
| 6286 | // do I need to invert the image? |
| 6287 | if ( tga_inverted ) |
| 6288 | { |
| 6289 | for (j = 0; j*2 < tga_height; ++j) |
| 6290 | { |
| 6291 | int index1 = j * tga_width * tga_comp; |
| 6292 | int index2 = (tga_height - 1 - j) * tga_width * tga_comp; |
| 6293 | for (i = tga_width * tga_comp; i > 0; --i) |
| 6294 | { |
| 6295 | unsigned char temp = tga_data[index1]; |
| 6296 | tga_data[index1] = tga_data[index2]; |
| 6297 | tga_data[index2] = temp; |
| 6298 | ++index1; |
| 6299 | ++index2; |
| 6300 | } |
| 6301 | } |
| 6302 | } |
| 6303 | // clear my palette, if I had one |
| 6304 | if ( tga_palette != NULL && !palette_buffer ) |
| 6305 | { |
| 6306 | STBI_FREE( tga_palette ); |
| 6307 | } |
| 6308 | } |
| 6309 | |
| 6310 | // swap RGB - if the source data was RGB16, it already is in the right order |
| 6311 | if (tga_comp >= 3 && !tga_rgb16) |
| 6312 | { |
| 6313 | unsigned char* tga_pixel = tga_data; |
| 6314 | for (i=0; i < tga_width * tga_height; ++i) |
| 6315 | { |
| 6316 | unsigned char temp = tga_pixel[0]; |
| 6317 | tga_pixel[0] = tga_pixel[2]; |
| 6318 | tga_pixel[2] = temp; |
| 6319 | tga_pixel += tga_comp; |
| 6320 | } |
| 6321 | } |
| 6322 | |
| 6323 | // convert to target component count |
| 6324 | if (req_comp && req_comp != tga_comp) |
| 6325 | tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height); |
| 6326 | |
| 6327 | // the things I do to get rid of an error message, and yet keep |
| 6328 | // Microsoft's C compilers happy... [8^( |
| 6329 | tga_palette_start = tga_palette_len = tga_palette_bits = |
| 6330 | tga_x_origin = tga_y_origin = 0; |
| 6331 | STBI_NOTUSED(tga_palette_start); |
| 6332 | // OK, done |
| 6333 | return tga_data; |
| 6334 | } |
| 6335 | #endif |
| 6336 | |
| 6337 | // ************************************************************************************************* |
| 6338 | // Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB |
| 6339 | |
| 6340 | #ifndef STBI_NO_PSD |
| 6341 | static int stbi__psd_test(stbi__context *s) |
| 6342 | { |
| 6343 | int r = (stbi__get32be(s) == 0x38425053); |
| 6344 | stbi__rewind(s); |
| 6345 | return r; |
| 6346 | } |
| 6347 | |
| 6348 | static int stbi__psd_decode_rle(stbi__context *s, stbi_uc *p, int pixelCount) |
| 6349 | { |
| 6350 | int count, nleft, len; |
| 6351 | |
| 6352 | count = 0; |
| 6353 | while ((nleft = pixelCount - count) > 0) { |
| 6354 | len = stbi__get8(s); |
| 6355 | if (len == 128) { |
| 6356 | // No-op. |
| 6357 | } else if (len < 128) { |
| 6358 | // Copy next len+1 bytes literally. |
| 6359 | len++; |
| 6360 | if (len > nleft) return 0; // corrupt data |
| 6361 | count += len; |
| 6362 | while (len) { |
| 6363 | *p = stbi__get8(s); |
| 6364 | p += 4; |
| 6365 | len--; |
| 6366 | } |
| 6367 | } else if (len > 128) { |
| 6368 | stbi_uc val; |
| 6369 | // Next -len+1 bytes in the dest are replicated from next source byte. |
| 6370 | // (Interpret len as a negative 8-bit int.) |
| 6371 | len = 257 - len; |
| 6372 | if (len > nleft) return 0; // corrupt data |
| 6373 | val = stbi__get8(s); |
| 6374 | count += len; |
| 6375 | while (len) { |
| 6376 | *p = val; |
| 6377 | p += 4; |
| 6378 | len--; |
| 6379 | } |
| 6380 | } |
| 6381 | } |
| 6382 | |
| 6383 | return 1; |
| 6384 | } |
| 6385 | |
| 6386 | static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc) |
| 6387 | { |
| 6388 | int pixelCount; |
| 6389 | int channelCount, compression; |
| 6390 | int channel, i; |
| 6391 | int bitdepth; |
| 6392 | int w,h; |
| 6393 | stbi_uc *out; |
| 6394 | STBI_NOTUSED(ri); |
| 6395 | |
| 6396 | // Check identifier |
| 6397 | if (stbi__get32be(s) != 0x38425053) // "8BPS" |
| 6398 | return stbi__errpuc("not PSD" , "Corrupt PSD image" ); |
| 6399 | |
| 6400 | // Check file type version. |
| 6401 | if (stbi__get16be(s) != 1) |
| 6402 | return stbi__errpuc("wrong version" , "Unsupported version of PSD image" ); |
| 6403 | |
| 6404 | // Skip 6 reserved bytes. |
| 6405 | stbi__skip(s, 6 ); |
| 6406 | |
| 6407 | // Read the number of channels (R, G, B, A, etc). |
| 6408 | channelCount = stbi__get16be(s); |
| 6409 | if (channelCount < 0 || channelCount > 16) |
| 6410 | return stbi__errpuc("wrong channel count" , "Unsupported number of channels in PSD image" ); |
| 6411 | |
| 6412 | // Read the rows and columns of the image. |
| 6413 | h = stbi__get32be(s); |
| 6414 | w = stbi__get32be(s); |
| 6415 | |
| 6416 | if (h > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large" ,"Very large image (corrupt?)" ); |
| 6417 | if (w > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large" ,"Very large image (corrupt?)" ); |
| 6418 | |
| 6419 | // Make sure the depth is 8 bits. |
| 6420 | bitdepth = stbi__get16be(s); |
| 6421 | if (bitdepth != 8 && bitdepth != 16) |
| 6422 | return stbi__errpuc("unsupported bit depth" , "PSD bit depth is not 8 or 16 bit" ); |
| 6423 | |
| 6424 | // Make sure the color mode is RGB. |
| 6425 | // Valid options are: |
| 6426 | // 0: Bitmap |
| 6427 | // 1: Grayscale |
| 6428 | // 2: Indexed color |
| 6429 | // 3: RGB color |
| 6430 | // 4: CMYK color |
| 6431 | // 7: Multichannel |
| 6432 | // 8: Duotone |
| 6433 | // 9: Lab color |
| 6434 | if (stbi__get16be(s) != 3) |
| 6435 | return stbi__errpuc("wrong color format" , "PSD is not in RGB color format" ); |
| 6436 | |
| 6437 | // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.) |
| 6438 | stbi__skip(s,stbi__get32be(s) ); |
| 6439 | |
| 6440 | // Skip the image resources. (resolution, pen tool paths, etc) |
| 6441 | stbi__skip(s, stbi__get32be(s) ); |
| 6442 | |
| 6443 | // Skip the reserved data. |
| 6444 | stbi__skip(s, stbi__get32be(s) ); |
| 6445 | |
| 6446 | // Find out if the data is compressed. |
| 6447 | // Known values: |
| 6448 | // 0: no compression |
| 6449 | // 1: RLE compressed |
| 6450 | compression = stbi__get16be(s); |
| 6451 | if (compression > 1) |
| 6452 | return stbi__errpuc("bad compression" , "PSD has an unknown compression format" ); |
| 6453 | |
| 6454 | // Check size |
| 6455 | if (!stbi__mad3sizes_valid(4, w, h, 0)) |
| 6456 | return stbi__errpuc("too large" , "Corrupt PSD" ); |
| 6457 | |
| 6458 | // Create the destination image. |
| 6459 | |
| 6460 | if (!compression && bitdepth == 16 && bpc == 16) { |
| 6461 | out = (stbi_uc *) stbi__malloc_mad3(8, w, h, 0); |
| 6462 | ri->bits_per_channel = 16; |
| 6463 | } else |
| 6464 | out = (stbi_uc *) stbi__malloc(4 * w*h); |
| 6465 | |
| 6466 | if (!out) return stbi__errpuc("outofmem" , "Out of memory" ); |
| 6467 | pixelCount = w*h; |
| 6468 | |
| 6469 | // Initialize the data to zero. |
| 6470 | //memset( out, 0, pixelCount * 4 ); |
| 6471 | |
| 6472 | // Finally, the image data. |
| 6473 | if (compression) { |
| 6474 | // RLE as used by .PSD and .TIFF |
| 6475 | // Loop until you get the number of unpacked bytes you are expecting: |
| 6476 | // Read the next source byte into n. |
| 6477 | // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally. |
| 6478 | // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times. |
| 6479 | // Else if n is 128, noop. |
| 6480 | // Endloop |
| 6481 | |
| 6482 | // The RLE-compressed data is preceded by a 2-byte data count for each row in the data, |
| 6483 | // which we're going to just skip. |
| 6484 | stbi__skip(s, h * channelCount * 2 ); |
| 6485 | |
| 6486 | // Read the RLE data by channel. |
| 6487 | for (channel = 0; channel < 4; channel++) { |
| 6488 | stbi_uc *p; |
| 6489 | |
| 6490 | p = out+channel; |
| 6491 | if (channel >= channelCount) { |
| 6492 | // Fill this channel with default data. |
| 6493 | for (i = 0; i < pixelCount; i++, p += 4) |
| 6494 | *p = (channel == 3 ? 255 : 0); |
| 6495 | } else { |
| 6496 | // Read the RLE data. |
| 6497 | if (!stbi__psd_decode_rle(s, p, pixelCount)) { |
| 6498 | STBI_FREE(out); |
| 6499 | return stbi__errpuc("corrupt" , "bad RLE data" ); |
| 6500 | } |
| 6501 | } |
| 6502 | } |
| 6503 | |
| 6504 | } else { |
| 6505 | // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...) |
| 6506 | // where each channel consists of an 8-bit (or 16-bit) value for each pixel in the image. |
| 6507 | |
| 6508 | // Read the data by channel. |
| 6509 | for (channel = 0; channel < 4; channel++) { |
| 6510 | if (channel >= channelCount) { |
| 6511 | // Fill this channel with default data. |
| 6512 | if (bitdepth == 16 && bpc == 16) { |
| 6513 | stbi__uint16 *q = ((stbi__uint16 *) out) + channel; |
| 6514 | stbi__uint16 val = channel == 3 ? 65535 : 0; |
| 6515 | for (i = 0; i < pixelCount; i++, q += 4) |
| 6516 | *q = val; |
| 6517 | } else { |
| 6518 | stbi_uc *p = out+channel; |
| 6519 | stbi_uc val = channel == 3 ? 255 : 0; |
| 6520 | for (i = 0; i < pixelCount; i++, p += 4) |
| 6521 | *p = val; |
| 6522 | } |
| 6523 | } else { |
| 6524 | if (ri->bits_per_channel == 16) { // output bpc |
| 6525 | stbi__uint16 *q = ((stbi__uint16 *) out) + channel; |
| 6526 | for (i = 0; i < pixelCount; i++, q += 4) |
| 6527 | *q = (stbi__uint16) stbi__get16be(s); |
| 6528 | } else { |
| 6529 | stbi_uc *p = out+channel; |
| 6530 | if (bitdepth == 16) { // input bpc |
| 6531 | for (i = 0; i < pixelCount; i++, p += 4) |
| 6532 | *p = (stbi_uc) (stbi__get16be(s) >> 8); |
| 6533 | } else { |
| 6534 | for (i = 0; i < pixelCount; i++, p += 4) |
| 6535 | *p = stbi__get8(s); |
| 6536 | } |
| 6537 | } |
| 6538 | } |
| 6539 | } |
| 6540 | } |
| 6541 | |
| 6542 | // remove weird white matte from PSD |
| 6543 | if (channelCount >= 4) { |
| 6544 | if (ri->bits_per_channel == 16) { |
| 6545 | for (i=0; i < w*h; ++i) { |
| 6546 | stbi__uint16 *pixel = (stbi__uint16 *) out + 4*i; |
| 6547 | if (pixel[3] != 0 && pixel[3] != 65535) { |
| 6548 | float a = pixel[3] / 65535.0f; |
| 6549 | float ra = 1.0f / a; |
| 6550 | float inv_a = 65535.0f * (1 - ra); |
| 6551 | pixel[0] = (stbi__uint16) (pixel[0]*ra + inv_a); |
| 6552 | pixel[1] = (stbi__uint16) (pixel[1]*ra + inv_a); |
| 6553 | pixel[2] = (stbi__uint16) (pixel[2]*ra + inv_a); |
| 6554 | } |
| 6555 | } |
| 6556 | } else { |
| 6557 | for (i=0; i < w*h; ++i) { |
| 6558 | unsigned char *pixel = out + 4*i; |
| 6559 | if (pixel[3] != 0 && pixel[3] != 255) { |
| 6560 | float a = pixel[3] / 255.0f; |
| 6561 | float ra = 1.0f / a; |
| 6562 | float inv_a = 255.0f * (1 - ra); |
| 6563 | pixel[0] = (unsigned char) (pixel[0]*ra + inv_a); |
| 6564 | pixel[1] = (unsigned char) (pixel[1]*ra + inv_a); |
| 6565 | pixel[2] = (unsigned char) (pixel[2]*ra + inv_a); |
| 6566 | } |
| 6567 | } |
| 6568 | } |
| 6569 | } |
| 6570 | |
| 6571 | // convert to desired output format |
| 6572 | if (req_comp && req_comp != 4) { |
| 6573 | if (ri->bits_per_channel == 16) |
| 6574 | out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, 4, req_comp, w, h); |
| 6575 | else |
| 6576 | out = stbi__convert_format(out, 4, req_comp, w, h); |
| 6577 | if (out == NULL) return out; // stbi__convert_format frees input on failure |
| 6578 | } |
| 6579 | |
| 6580 | if (comp) *comp = 4; |
| 6581 | *y = h; |
| 6582 | *x = w; |
| 6583 | |
| 6584 | return out; |
| 6585 | } |
| 6586 | #endif |
| 6587 | |
| 6588 | // ************************************************************************************************* |
| 6589 | // Softimage PIC loader |
| 6590 | // by Tom Seddon |
| 6591 | // |
| 6592 | // See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format |
| 6593 | // See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/ |
| 6594 | |
| 6595 | #ifndef STBI_NO_PIC |
| 6596 | static int stbi__pic_is4(stbi__context *s,const char *str) |
| 6597 | { |
| 6598 | int i; |
| 6599 | for (i=0; i<4; ++i) |
| 6600 | if (stbi__get8(s) != (stbi_uc)str[i]) |
| 6601 | return 0; |
| 6602 | |
| 6603 | return 1; |
| 6604 | } |
| 6605 | |
| 6606 | static int stbi__pic_test_core(stbi__context *s) |
| 6607 | { |
| 6608 | int i; |
| 6609 | |
| 6610 | if (!stbi__pic_is4(s,"\x53\x80\xF6\x34" )) |
| 6611 | return 0; |
| 6612 | |
| 6613 | for(i=0;i<84;++i) |
| 6614 | stbi__get8(s); |
| 6615 | |
| 6616 | if (!stbi__pic_is4(s,"PICT" )) |
| 6617 | return 0; |
| 6618 | |
| 6619 | return 1; |
| 6620 | } |
| 6621 | |
| 6622 | typedef struct |
| 6623 | { |
| 6624 | stbi_uc size,type,channel; |
| 6625 | } stbi__pic_packet; |
| 6626 | |
| 6627 | static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest) |
| 6628 | { |
| 6629 | int mask=0x80, i; |
| 6630 | |
| 6631 | for (i=0; i<4; ++i, mask>>=1) { |
| 6632 | if (channel & mask) { |
| 6633 | if (stbi__at_eof(s)) return stbi__errpuc("bad file" ,"PIC file too short" ); |
| 6634 | dest[i]=stbi__get8(s); |
| 6635 | } |
| 6636 | } |
| 6637 | |
| 6638 | return dest; |
| 6639 | } |
| 6640 | |
| 6641 | static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src) |
| 6642 | { |
| 6643 | int mask=0x80,i; |
| 6644 | |
| 6645 | for (i=0;i<4; ++i, mask>>=1) |
| 6646 | if (channel&mask) |
| 6647 | dest[i]=src[i]; |
| 6648 | } |
| 6649 | |
| 6650 | static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result) |
| 6651 | { |
| 6652 | int act_comp=0,num_packets=0,y,chained; |
| 6653 | stbi__pic_packet packets[10]; |
| 6654 | |
| 6655 | // this will (should...) cater for even some bizarre stuff like having data |
| 6656 | // for the same channel in multiple packets. |
| 6657 | do { |
| 6658 | stbi__pic_packet *packet; |
| 6659 | |
| 6660 | if (num_packets==sizeof(packets)/sizeof(packets[0])) |
| 6661 | return stbi__errpuc("bad format" ,"too many packets" ); |
| 6662 | |
| 6663 | packet = &packets[num_packets++]; |
| 6664 | |
| 6665 | chained = stbi__get8(s); |
| 6666 | packet->size = stbi__get8(s); |
| 6667 | packet->type = stbi__get8(s); |
| 6668 | packet->channel = stbi__get8(s); |
| 6669 | |
| 6670 | act_comp |= packet->channel; |
| 6671 | |
| 6672 | if (stbi__at_eof(s)) return stbi__errpuc("bad file" ,"file too short (reading packets)" ); |
| 6673 | if (packet->size != 8) return stbi__errpuc("bad format" ,"packet isn't 8bpp" ); |
| 6674 | } while (chained); |
| 6675 | |
| 6676 | *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel? |
| 6677 | |
| 6678 | for(y=0; y<height; ++y) { |
| 6679 | int packet_idx; |
| 6680 | |
| 6681 | for(packet_idx=0; packet_idx < num_packets; ++packet_idx) { |
| 6682 | stbi__pic_packet *packet = &packets[packet_idx]; |
| 6683 | stbi_uc *dest = result+y*width*4; |
| 6684 | |
| 6685 | switch (packet->type) { |
| 6686 | default: |
| 6687 | return stbi__errpuc("bad format" ,"packet has bad compression type" ); |
| 6688 | |
| 6689 | case 0: {//uncompressed |
| 6690 | int x; |
| 6691 | |
| 6692 | for(x=0;x<width;++x, dest+=4) |
| 6693 | if (!stbi__readval(s,packet->channel,dest)) |
| 6694 | return 0; |
| 6695 | break; |
| 6696 | } |
| 6697 | |
| 6698 | case 1://Pure RLE |
| 6699 | { |
| 6700 | int left=width, i; |
| 6701 | |
| 6702 | while (left>0) { |
| 6703 | stbi_uc count,value[4]; |
| 6704 | |
| 6705 | count=stbi__get8(s); |
| 6706 | if (stbi__at_eof(s)) return stbi__errpuc("bad file" ,"file too short (pure read count)" ); |
| 6707 | |
| 6708 | if (count > left) |
| 6709 | count = (stbi_uc) left; |
| 6710 | |
| 6711 | if (!stbi__readval(s,packet->channel,value)) return 0; |
| 6712 | |
| 6713 | for(i=0; i<count; ++i,dest+=4) |
| 6714 | stbi__copyval(packet->channel,dest,value); |
| 6715 | left -= count; |
| 6716 | } |
| 6717 | } |
| 6718 | break; |
| 6719 | |
| 6720 | case 2: {//Mixed RLE |
| 6721 | int left=width; |
| 6722 | while (left>0) { |
| 6723 | int count = stbi__get8(s), i; |
| 6724 | if (stbi__at_eof(s)) return stbi__errpuc("bad file" ,"file too short (mixed read count)" ); |
| 6725 | |
| 6726 | if (count >= 128) { // Repeated |
| 6727 | stbi_uc value[4]; |
| 6728 | |
| 6729 | if (count==128) |
| 6730 | count = stbi__get16be(s); |
| 6731 | else |
| 6732 | count -= 127; |
| 6733 | if (count > left) |
| 6734 | return stbi__errpuc("bad file" ,"scanline overrun" ); |
| 6735 | |
| 6736 | if (!stbi__readval(s,packet->channel,value)) |
| 6737 | return 0; |
| 6738 | |
| 6739 | for(i=0;i<count;++i, dest += 4) |
| 6740 | stbi__copyval(packet->channel,dest,value); |
| 6741 | } else { // Raw |
| 6742 | ++count; |
| 6743 | if (count>left) return stbi__errpuc("bad file" ,"scanline overrun" ); |
| 6744 | |
| 6745 | for(i=0;i<count;++i, dest+=4) |
| 6746 | if (!stbi__readval(s,packet->channel,dest)) |
| 6747 | return 0; |
| 6748 | } |
| 6749 | left-=count; |
| 6750 | } |
| 6751 | break; |
| 6752 | } |
| 6753 | } |
| 6754 | } |
| 6755 | } |
| 6756 | |
| 6757 | return result; |
| 6758 | } |
| 6759 | |
| 6760 | static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp, stbi__result_info *ri) |
| 6761 | { |
| 6762 | stbi_uc *result; |
| 6763 | int i, x,y, internal_comp; |
| 6764 | STBI_NOTUSED(ri); |
| 6765 | |
| 6766 | if (!comp) comp = &internal_comp; |
| 6767 | |
| 6768 | for (i=0; i<92; ++i) |
| 6769 | stbi__get8(s); |
| 6770 | |
| 6771 | x = stbi__get16be(s); |
| 6772 | y = stbi__get16be(s); |
| 6773 | |
| 6774 | if (y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large" ,"Very large image (corrupt?)" ); |
| 6775 | if (x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large" ,"Very large image (corrupt?)" ); |
| 6776 | |
| 6777 | if (stbi__at_eof(s)) return stbi__errpuc("bad file" ,"file too short (pic header)" ); |
| 6778 | if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large" , "PIC image too large to decode" ); |
| 6779 | |
| 6780 | stbi__get32be(s); //skip `ratio' |
| 6781 | stbi__get16be(s); //skip `fields' |
| 6782 | stbi__get16be(s); //skip `pad' |
| 6783 | |
| 6784 | // intermediate buffer is RGBA |
| 6785 | result = (stbi_uc *) stbi__malloc_mad3(x, y, 4, 0); |
| 6786 | if (!result) return stbi__errpuc("outofmem" , "Out of memory" ); |
| 6787 | memset(result, 0xff, x*y*4); |
| 6788 | |
| 6789 | if (!stbi__pic_load_core(s,x,y,comp, result)) { |
| 6790 | STBI_FREE(result); |
| 6791 | return 0; |
| 6792 | } |
| 6793 | *px = x; |
| 6794 | *py = y; |
| 6795 | if (req_comp == 0) req_comp = *comp; |
| 6796 | result=stbi__convert_format(result,4,req_comp,x,y); |
| 6797 | |
| 6798 | return result; |
| 6799 | } |
| 6800 | |
| 6801 | static int stbi__pic_test(stbi__context *s) |
| 6802 | { |
| 6803 | int r = stbi__pic_test_core(s); |
| 6804 | stbi__rewind(s); |
| 6805 | return r; |
| 6806 | } |
| 6807 | #endif |
| 6808 | |
| 6809 | // ************************************************************************************************* |
| 6810 | // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb |
| 6811 | |
| 6812 | #ifndef STBI_NO_GIF |
| 6813 | typedef struct |
| 6814 | { |
| 6815 | stbi__int16 prefix; |
| 6816 | stbi_uc first; |
| 6817 | stbi_uc suffix; |
| 6818 | } stbi__gif_lzw; |
| 6819 | |
| 6820 | typedef struct |
| 6821 | { |
| 6822 | int w,h; |
| 6823 | stbi_uc *out; // output buffer (always 4 components) |
| 6824 | stbi_uc *background; // The current "background" as far as a gif is concerned |
| 6825 | stbi_uc *history; |
| 6826 | int flags, bgindex, ratio, transparent, eflags; |
| 6827 | stbi_uc pal[256][4]; |
| 6828 | stbi_uc lpal[256][4]; |
| 6829 | stbi__gif_lzw codes[8192]; |
| 6830 | stbi_uc *color_table; |
| 6831 | int parse, step; |
| 6832 | int lflags; |
| 6833 | int start_x, start_y; |
| 6834 | int max_x, max_y; |
| 6835 | int cur_x, cur_y; |
| 6836 | int line_size; |
| 6837 | int delay; |
| 6838 | } stbi__gif; |
| 6839 | |
| 6840 | static int stbi__gif_test_raw(stbi__context *s) |
| 6841 | { |
| 6842 | int sz; |
| 6843 | if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0; |
| 6844 | sz = stbi__get8(s); |
| 6845 | if (sz != '9' && sz != '7') return 0; |
| 6846 | if (stbi__get8(s) != 'a') return 0; |
| 6847 | return 1; |
| 6848 | } |
| 6849 | |
| 6850 | static int stbi__gif_test(stbi__context *s) |
| 6851 | { |
| 6852 | int r = stbi__gif_test_raw(s); |
| 6853 | stbi__rewind(s); |
| 6854 | return r; |
| 6855 | } |
| 6856 | |
| 6857 | static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp) |
| 6858 | { |
| 6859 | int i; |
| 6860 | for (i=0; i < num_entries; ++i) { |
| 6861 | pal[i][2] = stbi__get8(s); |
| 6862 | pal[i][1] = stbi__get8(s); |
| 6863 | pal[i][0] = stbi__get8(s); |
| 6864 | pal[i][3] = transp == i ? 0 : 255; |
| 6865 | } |
| 6866 | } |
| 6867 | |
| 6868 | static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_info) |
| 6869 | { |
| 6870 | stbi_uc version; |
| 6871 | if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') |
| 6872 | return stbi__err("not GIF" , "Corrupt GIF" ); |
| 6873 | |
| 6874 | version = stbi__get8(s); |
| 6875 | if (version != '7' && version != '9') return stbi__err("not GIF" , "Corrupt GIF" ); |
| 6876 | if (stbi__get8(s) != 'a') return stbi__err("not GIF" , "Corrupt GIF" ); |
| 6877 | |
| 6878 | stbi__g_failure_reason = "" ; |
| 6879 | g->w = stbi__get16le(s); |
| 6880 | g->h = stbi__get16le(s); |
| 6881 | g->flags = stbi__get8(s); |
| 6882 | g->bgindex = stbi__get8(s); |
| 6883 | g->ratio = stbi__get8(s); |
| 6884 | g->transparent = -1; |
| 6885 | |
| 6886 | if (g->w > STBI_MAX_DIMENSIONS) return stbi__err("too large" ,"Very large image (corrupt?)" ); |
| 6887 | if (g->h > STBI_MAX_DIMENSIONS) return stbi__err("too large" ,"Very large image (corrupt?)" ); |
| 6888 | |
| 6889 | if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments |
| 6890 | |
| 6891 | if (is_info) return 1; |
| 6892 | |
| 6893 | if (g->flags & 0x80) |
| 6894 | stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1); |
| 6895 | |
| 6896 | return 1; |
| 6897 | } |
| 6898 | |
| 6899 | static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp) |
| 6900 | { |
| 6901 | stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif)); |
| 6902 | if (!g) return stbi__err("outofmem" , "Out of memory" ); |
| 6903 | if (!stbi__gif_header(s, g, comp, 1)) { |
| 6904 | STBI_FREE(g); |
| 6905 | stbi__rewind( s ); |
| 6906 | return 0; |
| 6907 | } |
| 6908 | if (x) *x = g->w; |
| 6909 | if (y) *y = g->h; |
| 6910 | STBI_FREE(g); |
| 6911 | return 1; |
| 6912 | } |
| 6913 | |
| 6914 | static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code) |
| 6915 | { |
| 6916 | stbi_uc *p, *c; |
| 6917 | int idx; |
| 6918 | |
| 6919 | // recurse to decode the prefixes, since the linked-list is backwards, |
| 6920 | // and working backwards through an interleaved image would be nasty |
| 6921 | if (g->codes[code].prefix >= 0) |
| 6922 | stbi__out_gif_code(g, g->codes[code].prefix); |
| 6923 | |
| 6924 | if (g->cur_y >= g->max_y) return; |
| 6925 | |
| 6926 | idx = g->cur_x + g->cur_y; |
| 6927 | p = &g->out[idx]; |
| 6928 | g->history[idx / 4] = 1; |
| 6929 | |
| 6930 | c = &g->color_table[g->codes[code].suffix * 4]; |
| 6931 | if (c[3] > 128) { // don't render transparent pixels; |
| 6932 | p[0] = c[2]; |
| 6933 | p[1] = c[1]; |
| 6934 | p[2] = c[0]; |
| 6935 | p[3] = c[3]; |
| 6936 | } |
| 6937 | g->cur_x += 4; |
| 6938 | |
| 6939 | if (g->cur_x >= g->max_x) { |
| 6940 | g->cur_x = g->start_x; |
| 6941 | g->cur_y += g->step; |
| 6942 | |
| 6943 | while (g->cur_y >= g->max_y && g->parse > 0) { |
| 6944 | g->step = (1 << g->parse) * g->line_size; |
| 6945 | g->cur_y = g->start_y + (g->step >> 1); |
| 6946 | --g->parse; |
| 6947 | } |
| 6948 | } |
| 6949 | } |
| 6950 | |
| 6951 | static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g) |
| 6952 | { |
| 6953 | stbi_uc lzw_cs; |
| 6954 | stbi__int32 len, init_code; |
| 6955 | stbi__uint32 first; |
| 6956 | stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear; |
| 6957 | stbi__gif_lzw *p; |
| 6958 | |
| 6959 | lzw_cs = stbi__get8(s); |
| 6960 | if (lzw_cs > 12) return NULL; |
| 6961 | clear = 1 << lzw_cs; |
| 6962 | first = 1; |
| 6963 | codesize = lzw_cs + 1; |
| 6964 | codemask = (1 << codesize) - 1; |
| 6965 | bits = 0; |
| 6966 | valid_bits = 0; |
| 6967 | for (init_code = 0; init_code < clear; init_code++) { |
| 6968 | g->codes[init_code].prefix = -1; |
| 6969 | g->codes[init_code].first = (stbi_uc) init_code; |
| 6970 | g->codes[init_code].suffix = (stbi_uc) init_code; |
| 6971 | } |
| 6972 | |
| 6973 | // support no starting clear code |
| 6974 | avail = clear+2; |
| 6975 | oldcode = -1; |
| 6976 | |
| 6977 | len = 0; |
| 6978 | for(;;) { |
| 6979 | if (valid_bits < codesize) { |
| 6980 | if (len == 0) { |
| 6981 | len = stbi__get8(s); // start new block |
| 6982 | if (len == 0) |
| 6983 | return g->out; |
| 6984 | } |
| 6985 | --len; |
| 6986 | bits |= (stbi__int32) stbi__get8(s) << valid_bits; |
| 6987 | valid_bits += 8; |
| 6988 | } else { |
| 6989 | stbi__int32 code = bits & codemask; |
| 6990 | bits >>= codesize; |
| 6991 | valid_bits -= codesize; |
| 6992 | // @OPTIMIZE: is there some way we can accelerate the non-clear path? |
| 6993 | if (code == clear) { // clear code |
| 6994 | codesize = lzw_cs + 1; |
| 6995 | codemask = (1 << codesize) - 1; |
| 6996 | avail = clear + 2; |
| 6997 | oldcode = -1; |
| 6998 | first = 0; |
| 6999 | } else if (code == clear + 1) { // end of stream code |
| 7000 | stbi__skip(s, len); |
| 7001 | while ((len = stbi__get8(s)) > 0) |
| 7002 | stbi__skip(s,len); |
| 7003 | return g->out; |
| 7004 | } else if (code <= avail) { |
| 7005 | if (first) { |
| 7006 | return stbi__errpuc("no clear code" , "Corrupt GIF" ); |
| 7007 | } |
| 7008 | |
| 7009 | if (oldcode >= 0) { |
| 7010 | p = &g->codes[avail++]; |
| 7011 | if (avail > 8192) { |
| 7012 | return stbi__errpuc("too many codes" , "Corrupt GIF" ); |
| 7013 | } |
| 7014 | |
| 7015 | p->prefix = (stbi__int16) oldcode; |
| 7016 | p->first = g->codes[oldcode].first; |
| 7017 | p->suffix = (code == avail) ? p->first : g->codes[code].first; |
| 7018 | } else if (code == avail) |
| 7019 | return stbi__errpuc("illegal code in raster" , "Corrupt GIF" ); |
| 7020 | |
| 7021 | stbi__out_gif_code(g, (stbi__uint16) code); |
| 7022 | |
| 7023 | if ((avail & codemask) == 0 && avail <= 0x0FFF) { |
| 7024 | codesize++; |
| 7025 | codemask = (1 << codesize) - 1; |
| 7026 | } |
| 7027 | |
| 7028 | oldcode = code; |
| 7029 | } else { |
| 7030 | return stbi__errpuc("illegal code in raster" , "Corrupt GIF" ); |
| 7031 | } |
| 7032 | } |
| 7033 | } |
| 7034 | } |
| 7035 | |
| 7036 | // this function is designed to support animated gifs, although stb_image doesn't support it |
| 7037 | // two back is the image from two frames ago, used for a very specific disposal format |
| 7038 | static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back) |
| 7039 | { |
| 7040 | int dispose; |
| 7041 | int first_frame; |
| 7042 | int pi; |
| 7043 | int pcount; |
| 7044 | STBI_NOTUSED(req_comp); |
| 7045 | |
| 7046 | // on first frame, any non-written pixels get the background colour (non-transparent) |
| 7047 | first_frame = 0; |
| 7048 | if (g->out == 0) { |
| 7049 | if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header |
| 7050 | if (!stbi__mad3sizes_valid(4, g->w, g->h, 0)) |
| 7051 | return stbi__errpuc("too large" , "GIF image is too large" ); |
| 7052 | pcount = g->w * g->h; |
| 7053 | g->out = (stbi_uc *) stbi__malloc(4 * pcount); |
| 7054 | g->background = (stbi_uc *) stbi__malloc(4 * pcount); |
| 7055 | g->history = (stbi_uc *) stbi__malloc(pcount); |
| 7056 | if (!g->out || !g->background || !g->history) |
| 7057 | return stbi__errpuc("outofmem" , "Out of memory" ); |
| 7058 | |
| 7059 | // image is treated as "transparent" at the start - ie, nothing overwrites the current background; |
| 7060 | // background colour is only used for pixels that are not rendered first frame, after that "background" |
| 7061 | // color refers to the color that was there the previous frame. |
| 7062 | memset(g->out, 0x00, 4 * pcount); |
| 7063 | memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent) |
| 7064 | memset(g->history, 0x00, pcount); // pixels that were affected previous frame |
| 7065 | first_frame = 1; |
| 7066 | } else { |
| 7067 | // second frame - how do we dispose of the previous one? |
| 7068 | dispose = (g->eflags & 0x1C) >> 2; |
| 7069 | pcount = g->w * g->h; |
| 7070 | |
| 7071 | if ((dispose == 3) && (two_back == 0)) { |
| 7072 | dispose = 2; // if I don't have an image to revert back to, default to the old background |
| 7073 | } |
| 7074 | |
| 7075 | if (dispose == 3) { // use previous graphic |
| 7076 | for (pi = 0; pi < pcount; ++pi) { |
| 7077 | if (g->history[pi]) { |
| 7078 | memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 ); |
| 7079 | } |
| 7080 | } |
| 7081 | } else if (dispose == 2) { |
| 7082 | // restore what was changed last frame to background before that frame; |
| 7083 | for (pi = 0; pi < pcount; ++pi) { |
| 7084 | if (g->history[pi]) { |
| 7085 | memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 ); |
| 7086 | } |
| 7087 | } |
| 7088 | } else { |
| 7089 | // This is a non-disposal case eithe way, so just |
| 7090 | // leave the pixels as is, and they will become the new background |
| 7091 | // 1: do not dispose |
| 7092 | // 0: not specified. |
| 7093 | } |
| 7094 | |
| 7095 | // background is what out is after the undoing of the previou frame; |
| 7096 | memcpy( g->background, g->out, 4 * g->w * g->h ); |
| 7097 | } |
| 7098 | |
| 7099 | // clear my history; |
| 7100 | memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame |
| 7101 | |
| 7102 | for (;;) { |
| 7103 | int tag = stbi__get8(s); |
| 7104 | switch (tag) { |
| 7105 | case 0x2C: /* Image Descriptor */ |
| 7106 | { |
| 7107 | stbi__int32 x, y, w, h; |
| 7108 | stbi_uc *o; |
| 7109 | |
| 7110 | x = stbi__get16le(s); |
| 7111 | y = stbi__get16le(s); |
| 7112 | w = stbi__get16le(s); |
| 7113 | h = stbi__get16le(s); |
| 7114 | if (((x + w) > (g->w)) || ((y + h) > (g->h))) |
| 7115 | return stbi__errpuc("bad Image Descriptor" , "Corrupt GIF" ); |
| 7116 | |
| 7117 | g->line_size = g->w * 4; |
| 7118 | g->start_x = x * 4; |
| 7119 | g->start_y = y * g->line_size; |
| 7120 | g->max_x = g->start_x + w * 4; |
| 7121 | g->max_y = g->start_y + h * g->line_size; |
| 7122 | g->cur_x = g->start_x; |
| 7123 | g->cur_y = g->start_y; |
| 7124 | |
| 7125 | // if the width of the specified rectangle is 0, that means |
| 7126 | // we may not see *any* pixels or the image is malformed; |
| 7127 | // to make sure this is caught, move the current y down to |
| 7128 | // max_y (which is what out_gif_code checks). |
| 7129 | if (w == 0) |
| 7130 | g->cur_y = g->max_y; |
| 7131 | |
| 7132 | g->lflags = stbi__get8(s); |
| 7133 | |
| 7134 | if (g->lflags & 0x40) { |
| 7135 | g->step = 8 * g->line_size; // first interlaced spacing |
| 7136 | g->parse = 3; |
| 7137 | } else { |
| 7138 | g->step = g->line_size; |
| 7139 | g->parse = 0; |
| 7140 | } |
| 7141 | |
| 7142 | if (g->lflags & 0x80) { |
| 7143 | stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1); |
| 7144 | g->color_table = (stbi_uc *) g->lpal; |
| 7145 | } else if (g->flags & 0x80) { |
| 7146 | g->color_table = (stbi_uc *) g->pal; |
| 7147 | } else |
| 7148 | return stbi__errpuc("missing color table" , "Corrupt GIF" ); |
| 7149 | |
| 7150 | o = stbi__process_gif_raster(s, g); |
| 7151 | if (!o) return NULL; |
| 7152 | |
| 7153 | // if this was the first frame, |
| 7154 | pcount = g->w * g->h; |
| 7155 | if (first_frame && (g->bgindex > 0)) { |
| 7156 | // if first frame, any pixel not drawn to gets the background color |
| 7157 | for (pi = 0; pi < pcount; ++pi) { |
| 7158 | if (g->history[pi] == 0) { |
| 7159 | g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be; |
| 7160 | memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 ); |
| 7161 | } |
| 7162 | } |
| 7163 | } |
| 7164 | |
| 7165 | return o; |
| 7166 | } |
| 7167 | |
| 7168 | case 0x21: // Comment Extension. |
| 7169 | { |
| 7170 | int len; |
| 7171 | int ext = stbi__get8(s); |
| 7172 | if (ext == 0xF9) { // Graphic Control Extension. |
| 7173 | len = stbi__get8(s); |
| 7174 | if (len == 4) { |
| 7175 | g->eflags = stbi__get8(s); |
| 7176 | g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths. |
| 7177 | |
| 7178 | // unset old transparent |
| 7179 | if (g->transparent >= 0) { |
| 7180 | g->pal[g->transparent][3] = 255; |
| 7181 | } |
| 7182 | if (g->eflags & 0x01) { |
| 7183 | g->transparent = stbi__get8(s); |
| 7184 | if (g->transparent >= 0) { |
| 7185 | g->pal[g->transparent][3] = 0; |
| 7186 | } |
| 7187 | } else { |
| 7188 | // don't need transparent |
| 7189 | stbi__skip(s, 1); |
| 7190 | g->transparent = -1; |
| 7191 | } |
| 7192 | } else { |
| 7193 | stbi__skip(s, len); |
| 7194 | break; |
| 7195 | } |
| 7196 | } |
| 7197 | while ((len = stbi__get8(s)) != 0) { |
| 7198 | stbi__skip(s, len); |
| 7199 | } |
| 7200 | break; |
| 7201 | } |
| 7202 | |
| 7203 | case 0x3B: // gif stream termination code |
| 7204 | return (stbi_uc *) s; // using '1' causes warning on some compilers |
| 7205 | |
| 7206 | default: |
| 7207 | return stbi__errpuc("unknown code" , "Corrupt GIF" ); |
| 7208 | } |
| 7209 | } |
| 7210 | } |
| 7211 | |
| 7212 | static void *stbi__load_gif_main_outofmem(stbi__gif *g, stbi_uc *out, int **delays) |
| 7213 | { |
| 7214 | STBI_FREE(g->out); |
| 7215 | STBI_FREE(g->history); |
| 7216 | STBI_FREE(g->background); |
| 7217 | |
| 7218 | if (out) STBI_FREE(out); |
| 7219 | if (delays && *delays) STBI_FREE(*delays); |
| 7220 | return stbi__errpuc("outofmem" , "Out of memory" ); |
| 7221 | } |
| 7222 | |
| 7223 | static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp) |
| 7224 | { |
| 7225 | if (stbi__gif_test(s)) { |
| 7226 | int layers = 0; |
| 7227 | stbi_uc *u = 0; |
| 7228 | stbi_uc *out = 0; |
| 7229 | stbi_uc *two_back = 0; |
| 7230 | stbi__gif g; |
| 7231 | int stride; |
| 7232 | int out_size = 0; |
| 7233 | int delays_size = 0; |
| 7234 | |
| 7235 | STBI_NOTUSED(out_size); |
| 7236 | STBI_NOTUSED(delays_size); |
| 7237 | |
| 7238 | memset(&g, 0, sizeof(g)); |
| 7239 | if (delays) { |
| 7240 | *delays = 0; |
| 7241 | } |
| 7242 | |
| 7243 | do { |
| 7244 | u = stbi__gif_load_next(s, &g, comp, req_comp, two_back); |
| 7245 | if (u == (stbi_uc *) s) u = 0; // end of animated gif marker |
| 7246 | |
| 7247 | if (u) { |
| 7248 | *x = g.w; |
| 7249 | *y = g.h; |
| 7250 | ++layers; |
| 7251 | stride = g.w * g.h * 4; |
| 7252 | |
| 7253 | if (out) { |
| 7254 | void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride ); |
| 7255 | if (!tmp) |
| 7256 | return stbi__load_gif_main_outofmem(&g, out, delays); |
| 7257 | else { |
| 7258 | out = (stbi_uc*) tmp; |
| 7259 | out_size = layers * stride; |
| 7260 | } |
| 7261 | |
| 7262 | if (delays) { |
| 7263 | int *new_delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers ); |
| 7264 | if (!new_delays) |
| 7265 | return stbi__load_gif_main_outofmem(&g, out, delays); |
| 7266 | *delays = new_delays; |
| 7267 | delays_size = layers * sizeof(int); |
| 7268 | } |
| 7269 | } else { |
| 7270 | out = (stbi_uc*)stbi__malloc( layers * stride ); |
| 7271 | if (!out) |
| 7272 | return stbi__load_gif_main_outofmem(&g, out, delays); |
| 7273 | out_size = layers * stride; |
| 7274 | if (delays) { |
| 7275 | *delays = (int*) stbi__malloc( layers * sizeof(int) ); |
| 7276 | if (!*delays) |
| 7277 | return stbi__load_gif_main_outofmem(&g, out, delays); |
| 7278 | delays_size = layers * sizeof(int); |
| 7279 | } |
| 7280 | } |
| 7281 | memcpy( out + ((layers - 1) * stride), u, stride ); |
| 7282 | if (layers >= 2) { |
| 7283 | two_back = out - 2 * stride; |
| 7284 | } |
| 7285 | |
| 7286 | if (delays) { |
| 7287 | (*delays)[layers - 1U] = g.delay; |
| 7288 | } |
| 7289 | } |
| 7290 | } while (u != 0); |
| 7291 | |
| 7292 | // free temp buffer; |
| 7293 | STBI_FREE(g.out); |
| 7294 | STBI_FREE(g.history); |
| 7295 | STBI_FREE(g.background); |
| 7296 | |
| 7297 | // do the final conversion after loading everything; |
| 7298 | if (req_comp && req_comp != 4) |
| 7299 | out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h); |
| 7300 | |
| 7301 | *z = layers; |
| 7302 | return out; |
| 7303 | } else { |
| 7304 | return stbi__errpuc("not GIF" , "Image was not as a gif type." ); |
| 7305 | } |
| 7306 | } |
| 7307 | |
| 7308 | static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) |
| 7309 | { |
| 7310 | stbi_uc *u = 0; |
| 7311 | stbi__gif g; |
| 7312 | memset(&g, 0, sizeof(g)); |
| 7313 | STBI_NOTUSED(ri); |
| 7314 | |
| 7315 | u = stbi__gif_load_next(s, &g, comp, req_comp, 0); |
| 7316 | if (u == (stbi_uc *) s) u = 0; // end of animated gif marker |
| 7317 | if (u) { |
| 7318 | *x = g.w; |
| 7319 | *y = g.h; |
| 7320 | |
| 7321 | // moved conversion to after successful load so that the same |
| 7322 | // can be done for multiple frames. |
| 7323 | if (req_comp && req_comp != 4) |
| 7324 | u = stbi__convert_format(u, 4, req_comp, g.w, g.h); |
| 7325 | } else if (g.out) { |
| 7326 | // if there was an error and we allocated an image buffer, free it! |
| 7327 | STBI_FREE(g.out); |
| 7328 | } |
| 7329 | |
| 7330 | // free buffers needed for multiple frame loading; |
| 7331 | STBI_FREE(g.history); |
| 7332 | STBI_FREE(g.background); |
| 7333 | |
| 7334 | return u; |
| 7335 | } |
| 7336 | |
| 7337 | static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp) |
| 7338 | { |
| 7339 | return stbi__gif_info_raw(s,x,y,comp); |
| 7340 | } |
| 7341 | #endif |
| 7342 | |
| 7343 | // ************************************************************************************************* |
| 7344 | // Radiance RGBE HDR loader |
| 7345 | // originally by Nicolas Schulz |
| 7346 | #ifndef STBI_NO_HDR |
| 7347 | static int stbi__hdr_test_core(stbi__context *s, const char *signature) |
| 7348 | { |
| 7349 | int i; |
| 7350 | for (i=0; signature[i]; ++i) |
| 7351 | if (stbi__get8(s) != signature[i]) |
| 7352 | return 0; |
| 7353 | stbi__rewind(s); |
| 7354 | return 1; |
| 7355 | } |
| 7356 | |
| 7357 | static int stbi__hdr_test(stbi__context* s) |
| 7358 | { |
| 7359 | int r = stbi__hdr_test_core(s, "#?RADIANCE\n" ); |
| 7360 | stbi__rewind(s); |
| 7361 | if(!r) { |
| 7362 | r = stbi__hdr_test_core(s, "#?RGBE\n" ); |
| 7363 | stbi__rewind(s); |
| 7364 | } |
| 7365 | return r; |
| 7366 | } |
| 7367 | |
| 7368 | #define STBI__HDR_BUFLEN 1024 |
| 7369 | static char *stbi__hdr_gettoken(stbi__context *z, char *buffer) |
| 7370 | { |
| 7371 | int len=0; |
| 7372 | char c = '\0'; |
| 7373 | |
| 7374 | c = (char) stbi__get8(z); |
| 7375 | |
| 7376 | while (!stbi__at_eof(z) && c != '\n') { |
| 7377 | buffer[len++] = c; |
| 7378 | if (len == STBI__HDR_BUFLEN-1) { |
| 7379 | // flush to end of line |
| 7380 | while (!stbi__at_eof(z) && stbi__get8(z) != '\n') |
| 7381 | ; |
| 7382 | break; |
| 7383 | } |
| 7384 | c = (char) stbi__get8(z); |
| 7385 | } |
| 7386 | |
| 7387 | buffer[len] = 0; |
| 7388 | return buffer; |
| 7389 | } |
| 7390 | |
| 7391 | static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp) |
| 7392 | { |
| 7393 | if ( input[3] != 0 ) { |
| 7394 | float f1; |
| 7395 | // Exponent |
| 7396 | f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8)); |
| 7397 | if (req_comp <= 2) |
| 7398 | output[0] = (input[0] + input[1] + input[2]) * f1 / 3; |
| 7399 | else { |
| 7400 | output[0] = input[0] * f1; |
| 7401 | output[1] = input[1] * f1; |
| 7402 | output[2] = input[2] * f1; |
| 7403 | } |
| 7404 | if (req_comp == 2) output[1] = 1; |
| 7405 | if (req_comp == 4) output[3] = 1; |
| 7406 | } else { |
| 7407 | switch (req_comp) { |
| 7408 | case 4: output[3] = 1; /* fallthrough */ |
| 7409 | case 3: output[0] = output[1] = output[2] = 0; |
| 7410 | break; |
| 7411 | case 2: output[1] = 1; /* fallthrough */ |
| 7412 | case 1: output[0] = 0; |
| 7413 | break; |
| 7414 | } |
| 7415 | } |
| 7416 | } |
| 7417 | |
| 7418 | static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) |
| 7419 | { |
| 7420 | char buffer[STBI__HDR_BUFLEN]; |
| 7421 | char *token; |
| 7422 | int valid = 0; |
| 7423 | int width, height; |
| 7424 | stbi_uc *scanline; |
| 7425 | float *hdr_data; |
| 7426 | int len; |
| 7427 | unsigned char count, value; |
| 7428 | int i, j, k, c1,c2, z; |
| 7429 | const char *headerToken; |
| 7430 | STBI_NOTUSED(ri); |
| 7431 | |
| 7432 | // Check identifier |
| 7433 | headerToken = stbi__hdr_gettoken(s,buffer); |
| 7434 | if (strcmp(headerToken, "#?RADIANCE" ) != 0 && strcmp(headerToken, "#?RGBE" ) != 0) |
| 7435 | return stbi__errpf("not HDR" , "Corrupt HDR image" ); |
| 7436 | |
| 7437 | // Parse header |
| 7438 | for(;;) { |
| 7439 | token = stbi__hdr_gettoken(s,buffer); |
| 7440 | if (token[0] == 0) break; |
| 7441 | if (strcmp(token, "FORMAT=32-bit_rle_rgbe" ) == 0) valid = 1; |
| 7442 | } |
| 7443 | |
| 7444 | if (!valid) return stbi__errpf("unsupported format" , "Unsupported HDR format" ); |
| 7445 | |
| 7446 | // Parse width and height |
| 7447 | // can't use sscanf() if we're not using stdio! |
| 7448 | token = stbi__hdr_gettoken(s,buffer); |
| 7449 | if (strncmp(token, "-Y " , 3)) return stbi__errpf("unsupported data layout" , "Unsupported HDR format" ); |
| 7450 | token += 3; |
| 7451 | height = (int) strtol(token, &token, 10); |
| 7452 | while (*token == ' ') ++token; |
| 7453 | if (strncmp(token, "+X " , 3)) return stbi__errpf("unsupported data layout" , "Unsupported HDR format" ); |
| 7454 | token += 3; |
| 7455 | width = (int) strtol(token, NULL, 10); |
| 7456 | |
| 7457 | if (height > STBI_MAX_DIMENSIONS) return stbi__errpf("too large" ,"Very large image (corrupt?)" ); |
| 7458 | if (width > STBI_MAX_DIMENSIONS) return stbi__errpf("too large" ,"Very large image (corrupt?)" ); |
| 7459 | |
| 7460 | *x = width; |
| 7461 | *y = height; |
| 7462 | |
| 7463 | if (comp) *comp = 3; |
| 7464 | if (req_comp == 0) req_comp = 3; |
| 7465 | |
| 7466 | if (!stbi__mad4sizes_valid(width, height, req_comp, sizeof(float), 0)) |
| 7467 | return stbi__errpf("too large" , "HDR image is too large" ); |
| 7468 | |
| 7469 | // Read data |
| 7470 | hdr_data = (float *) stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0); |
| 7471 | if (!hdr_data) |
| 7472 | return stbi__errpf("outofmem" , "Out of memory" ); |
| 7473 | |
| 7474 | // Load image data |
| 7475 | // image data is stored as some number of sca |
| 7476 | if ( width < 8 || width >= 32768) { |
| 7477 | // Read flat data |
| 7478 | for (j=0; j < height; ++j) { |
| 7479 | for (i=0; i < width; ++i) { |
| 7480 | stbi_uc rgbe[4]; |
| 7481 | main_decode_loop: |
| 7482 | stbi__getn(s, rgbe, 4); |
| 7483 | stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp); |
| 7484 | } |
| 7485 | } |
| 7486 | } else { |
| 7487 | // Read RLE-encoded data |
| 7488 | scanline = NULL; |
| 7489 | |
| 7490 | for (j = 0; j < height; ++j) { |
| 7491 | c1 = stbi__get8(s); |
| 7492 | c2 = stbi__get8(s); |
| 7493 | len = stbi__get8(s); |
| 7494 | if (c1 != 2 || c2 != 2 || (len & 0x80)) { |
| 7495 | // not run-length encoded, so we have to actually use THIS data as a decoded |
| 7496 | // pixel (note this can't be a valid pixel--one of RGB must be >= 128) |
| 7497 | stbi_uc rgbe[4]; |
| 7498 | rgbe[0] = (stbi_uc) c1; |
| 7499 | rgbe[1] = (stbi_uc) c2; |
| 7500 | rgbe[2] = (stbi_uc) len; |
| 7501 | rgbe[3] = (stbi_uc) stbi__get8(s); |
| 7502 | stbi__hdr_convert(hdr_data, rgbe, req_comp); |
| 7503 | i = 1; |
| 7504 | j = 0; |
| 7505 | STBI_FREE(scanline); |
| 7506 | goto main_decode_loop; // yes, this makes no sense |
| 7507 | } |
| 7508 | len <<= 8; |
| 7509 | len |= stbi__get8(s); |
| 7510 | if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length" , "corrupt HDR" ); } |
| 7511 | if (scanline == NULL) { |
| 7512 | scanline = (stbi_uc *) stbi__malloc_mad2(width, 4, 0); |
| 7513 | if (!scanline) { |
| 7514 | STBI_FREE(hdr_data); |
| 7515 | return stbi__errpf("outofmem" , "Out of memory" ); |
| 7516 | } |
| 7517 | } |
| 7518 | |
| 7519 | for (k = 0; k < 4; ++k) { |
| 7520 | int nleft; |
| 7521 | i = 0; |
| 7522 | while ((nleft = width - i) > 0) { |
| 7523 | count = stbi__get8(s); |
| 7524 | if (count > 128) { |
| 7525 | // Run |
| 7526 | value = stbi__get8(s); |
| 7527 | count -= 128; |
| 7528 | if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt" , "bad RLE data in HDR" ); } |
| 7529 | for (z = 0; z < count; ++z) |
| 7530 | scanline[i++ * 4 + k] = value; |
| 7531 | } else { |
| 7532 | // Dump |
| 7533 | if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt" , "bad RLE data in HDR" ); } |
| 7534 | for (z = 0; z < count; ++z) |
| 7535 | scanline[i++ * 4 + k] = stbi__get8(s); |
| 7536 | } |
| 7537 | } |
| 7538 | } |
| 7539 | for (i=0; i < width; ++i) |
| 7540 | stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp); |
| 7541 | } |
| 7542 | if (scanline) |
| 7543 | STBI_FREE(scanline); |
| 7544 | } |
| 7545 | |
| 7546 | return hdr_data; |
| 7547 | } |
| 7548 | |
| 7549 | static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp) |
| 7550 | { |
| 7551 | char buffer[STBI__HDR_BUFLEN]; |
| 7552 | char *token; |
| 7553 | int valid = 0; |
| 7554 | int dummy; |
| 7555 | |
| 7556 | if (!x) x = &dummy; |
| 7557 | if (!y) y = &dummy; |
| 7558 | if (!comp) comp = &dummy; |
| 7559 | |
| 7560 | if (stbi__hdr_test(s) == 0) { |
| 7561 | stbi__rewind( s ); |
| 7562 | return 0; |
| 7563 | } |
| 7564 | |
| 7565 | for(;;) { |
| 7566 | token = stbi__hdr_gettoken(s,buffer); |
| 7567 | if (token[0] == 0) break; |
| 7568 | if (strcmp(token, "FORMAT=32-bit_rle_rgbe" ) == 0) valid = 1; |
| 7569 | } |
| 7570 | |
| 7571 | if (!valid) { |
| 7572 | stbi__rewind( s ); |
| 7573 | return 0; |
| 7574 | } |
| 7575 | token = stbi__hdr_gettoken(s,buffer); |
| 7576 | if (strncmp(token, "-Y " , 3)) { |
| 7577 | stbi__rewind( s ); |
| 7578 | return 0; |
| 7579 | } |
| 7580 | token += 3; |
| 7581 | *y = (int) strtol(token, &token, 10); |
| 7582 | while (*token == ' ') ++token; |
| 7583 | if (strncmp(token, "+X " , 3)) { |
| 7584 | stbi__rewind( s ); |
| 7585 | return 0; |
| 7586 | } |
| 7587 | token += 3; |
| 7588 | *x = (int) strtol(token, NULL, 10); |
| 7589 | *comp = 3; |
| 7590 | return 1; |
| 7591 | } |
| 7592 | #endif // STBI_NO_HDR |
| 7593 | |
| 7594 | #ifndef STBI_NO_BMP |
| 7595 | static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp) |
| 7596 | { |
| 7597 | void *p; |
| 7598 | stbi__bmp_data info; |
| 7599 | |
| 7600 | info.all_a = 255; |
| 7601 | p = stbi__bmp_parse_header(s, &info); |
| 7602 | if (p == NULL) { |
| 7603 | stbi__rewind( s ); |
| 7604 | return 0; |
| 7605 | } |
| 7606 | if (x) *x = s->img_x; |
| 7607 | if (y) *y = s->img_y; |
| 7608 | if (comp) { |
| 7609 | if (info.bpp == 24 && info.ma == 0xff000000) |
| 7610 | *comp = 3; |
| 7611 | else |
| 7612 | *comp = info.ma ? 4 : 3; |
| 7613 | } |
| 7614 | return 1; |
| 7615 | } |
| 7616 | #endif |
| 7617 | |
| 7618 | #ifndef STBI_NO_PSD |
| 7619 | static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp) |
| 7620 | { |
| 7621 | int channelCount, dummy, depth; |
| 7622 | if (!x) x = &dummy; |
| 7623 | if (!y) y = &dummy; |
| 7624 | if (!comp) comp = &dummy; |
| 7625 | if (stbi__get32be(s) != 0x38425053) { |
| 7626 | stbi__rewind( s ); |
| 7627 | return 0; |
| 7628 | } |
| 7629 | if (stbi__get16be(s) != 1) { |
| 7630 | stbi__rewind( s ); |
| 7631 | return 0; |
| 7632 | } |
| 7633 | stbi__skip(s, 6); |
| 7634 | channelCount = stbi__get16be(s); |
| 7635 | if (channelCount < 0 || channelCount > 16) { |
| 7636 | stbi__rewind( s ); |
| 7637 | return 0; |
| 7638 | } |
| 7639 | *y = stbi__get32be(s); |
| 7640 | *x = stbi__get32be(s); |
| 7641 | depth = stbi__get16be(s); |
| 7642 | if (depth != 8 && depth != 16) { |
| 7643 | stbi__rewind( s ); |
| 7644 | return 0; |
| 7645 | } |
| 7646 | if (stbi__get16be(s) != 3) { |
| 7647 | stbi__rewind( s ); |
| 7648 | return 0; |
| 7649 | } |
| 7650 | *comp = 4; |
| 7651 | return 1; |
| 7652 | } |
| 7653 | |
| 7654 | static int stbi__psd_is16(stbi__context *s) |
| 7655 | { |
| 7656 | int channelCount, depth; |
| 7657 | if (stbi__get32be(s) != 0x38425053) { |
| 7658 | stbi__rewind( s ); |
| 7659 | return 0; |
| 7660 | } |
| 7661 | if (stbi__get16be(s) != 1) { |
| 7662 | stbi__rewind( s ); |
| 7663 | return 0; |
| 7664 | } |
| 7665 | stbi__skip(s, 6); |
| 7666 | channelCount = stbi__get16be(s); |
| 7667 | if (channelCount < 0 || channelCount > 16) { |
| 7668 | stbi__rewind( s ); |
| 7669 | return 0; |
| 7670 | } |
| 7671 | STBI_NOTUSED(stbi__get32be(s)); |
| 7672 | STBI_NOTUSED(stbi__get32be(s)); |
| 7673 | depth = stbi__get16be(s); |
| 7674 | if (depth != 16) { |
| 7675 | stbi__rewind( s ); |
| 7676 | return 0; |
| 7677 | } |
| 7678 | return 1; |
| 7679 | } |
| 7680 | #endif |
| 7681 | |
| 7682 | #ifndef STBI_NO_PIC |
| 7683 | static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp) |
| 7684 | { |
| 7685 | int act_comp=0,num_packets=0,chained,dummy; |
| 7686 | stbi__pic_packet packets[10]; |
| 7687 | |
| 7688 | if (!x) x = &dummy; |
| 7689 | if (!y) y = &dummy; |
| 7690 | if (!comp) comp = &dummy; |
| 7691 | |
| 7692 | if (!stbi__pic_is4(s,"\x53\x80\xF6\x34" )) { |
| 7693 | stbi__rewind(s); |
| 7694 | return 0; |
| 7695 | } |
| 7696 | |
| 7697 | stbi__skip(s, 88); |
| 7698 | |
| 7699 | *x = stbi__get16be(s); |
| 7700 | *y = stbi__get16be(s); |
| 7701 | if (stbi__at_eof(s)) { |
| 7702 | stbi__rewind( s); |
| 7703 | return 0; |
| 7704 | } |
| 7705 | if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) { |
| 7706 | stbi__rewind( s ); |
| 7707 | return 0; |
| 7708 | } |
| 7709 | |
| 7710 | stbi__skip(s, 8); |
| 7711 | |
| 7712 | do { |
| 7713 | stbi__pic_packet *packet; |
| 7714 | |
| 7715 | if (num_packets==sizeof(packets)/sizeof(packets[0])) |
| 7716 | return 0; |
| 7717 | |
| 7718 | packet = &packets[num_packets++]; |
| 7719 | chained = stbi__get8(s); |
| 7720 | packet->size = stbi__get8(s); |
| 7721 | packet->type = stbi__get8(s); |
| 7722 | packet->channel = stbi__get8(s); |
| 7723 | act_comp |= packet->channel; |
| 7724 | |
| 7725 | if (stbi__at_eof(s)) { |
| 7726 | stbi__rewind( s ); |
| 7727 | return 0; |
| 7728 | } |
| 7729 | if (packet->size != 8) { |
| 7730 | stbi__rewind( s ); |
| 7731 | return 0; |
| 7732 | } |
| 7733 | } while (chained); |
| 7734 | |
| 7735 | *comp = (act_comp & 0x10 ? 4 : 3); |
| 7736 | |
| 7737 | return 1; |
| 7738 | } |
| 7739 | #endif |
| 7740 | |
| 7741 | // ************************************************************************************************* |
| 7742 | // Portable Gray Map and Portable Pixel Map loader |
| 7743 | // by Ken Miller |
| 7744 | // |
| 7745 | // PGM: http://netpbm.sourceforge.net/doc/pgm.html |
| 7746 | // PPM: http://netpbm.sourceforge.net/doc/ppm.html |
| 7747 | // |
| 7748 | // Known limitations: |
| 7749 | // Does not support comments in the header section |
| 7750 | // Does not support ASCII image data (formats P2 and P3) |
| 7751 | |
| 7752 | #ifndef STBI_NO_PNM |
| 7753 | |
| 7754 | static int stbi__pnm_test(stbi__context *s) |
| 7755 | { |
| 7756 | char p, t; |
| 7757 | p = (char) stbi__get8(s); |
| 7758 | t = (char) stbi__get8(s); |
| 7759 | if (p != 'P' || (t != '5' && t != '6')) { |
| 7760 | stbi__rewind( s ); |
| 7761 | return 0; |
| 7762 | } |
| 7763 | return 1; |
| 7764 | } |
| 7765 | |
| 7766 | static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) |
| 7767 | { |
| 7768 | stbi_uc *out; |
| 7769 | STBI_NOTUSED(ri); |
| 7770 | |
| 7771 | ri->bits_per_channel = stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n); |
| 7772 | if (ri->bits_per_channel == 0) |
| 7773 | return 0; |
| 7774 | |
| 7775 | if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large" ,"Very large image (corrupt?)" ); |
| 7776 | if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large" ,"Very large image (corrupt?)" ); |
| 7777 | |
| 7778 | *x = s->img_x; |
| 7779 | *y = s->img_y; |
| 7780 | if (comp) *comp = s->img_n; |
| 7781 | |
| 7782 | if (!stbi__mad4sizes_valid(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0)) |
| 7783 | return stbi__errpuc("too large" , "PNM too large" ); |
| 7784 | |
| 7785 | out = (stbi_uc *) stbi__malloc_mad4(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0); |
| 7786 | if (!out) return stbi__errpuc("outofmem" , "Out of memory" ); |
| 7787 | if (!stbi__getn(s, out, s->img_n * s->img_x * s->img_y * (ri->bits_per_channel / 8))) { |
| 7788 | STBI_FREE(out); |
| 7789 | return stbi__errpuc("bad PNM" , "PNM file truncated" ); |
| 7790 | } |
| 7791 | |
| 7792 | if (req_comp && req_comp != s->img_n) { |
| 7793 | if (ri->bits_per_channel == 16) { |
| 7794 | out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, s->img_n, req_comp, s->img_x, s->img_y); |
| 7795 | } else { |
| 7796 | out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y); |
| 7797 | } |
| 7798 | if (out == NULL) return out; // stbi__convert_format frees input on failure |
| 7799 | } |
| 7800 | return out; |
| 7801 | } |
| 7802 | |
| 7803 | static int stbi__pnm_isspace(char c) |
| 7804 | { |
| 7805 | return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r'; |
| 7806 | } |
| 7807 | |
| 7808 | static void stbi__pnm_skip_whitespace(stbi__context *s, char *c) |
| 7809 | { |
| 7810 | for (;;) { |
| 7811 | while (!stbi__at_eof(s) && stbi__pnm_isspace(*c)) |
| 7812 | *c = (char) stbi__get8(s); |
| 7813 | |
| 7814 | if (stbi__at_eof(s) || *c != '#') |
| 7815 | break; |
| 7816 | |
| 7817 | while (!stbi__at_eof(s) && *c != '\n' && *c != '\r' ) |
| 7818 | *c = (char) stbi__get8(s); |
| 7819 | } |
| 7820 | } |
| 7821 | |
| 7822 | static int stbi__pnm_isdigit(char c) |
| 7823 | { |
| 7824 | return c >= '0' && c <= '9'; |
| 7825 | } |
| 7826 | |
| 7827 | static int stbi__pnm_getinteger(stbi__context *s, char *c) |
| 7828 | { |
| 7829 | int value = 0; |
| 7830 | |
| 7831 | while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) { |
| 7832 | value = value*10 + (*c - '0'); |
| 7833 | *c = (char) stbi__get8(s); |
| 7834 | if((value > 214748364) || (value == 214748364 && *c > '7')) |
| 7835 | return stbi__err("integer parse overflow" , "Parsing an integer in the PPM header overflowed a 32-bit int" ); |
| 7836 | } |
| 7837 | |
| 7838 | return value; |
| 7839 | } |
| 7840 | |
| 7841 | static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp) |
| 7842 | { |
| 7843 | int maxv, dummy; |
| 7844 | char c, p, t; |
| 7845 | |
| 7846 | if (!x) x = &dummy; |
| 7847 | if (!y) y = &dummy; |
| 7848 | if (!comp) comp = &dummy; |
| 7849 | |
| 7850 | stbi__rewind(s); |
| 7851 | |
| 7852 | // Get identifier |
| 7853 | p = (char) stbi__get8(s); |
| 7854 | t = (char) stbi__get8(s); |
| 7855 | if (p != 'P' || (t != '5' && t != '6')) { |
| 7856 | stbi__rewind(s); |
| 7857 | return 0; |
| 7858 | } |
| 7859 | |
| 7860 | *comp = (t == '6') ? 3 : 1; // '5' is 1-component .pgm; '6' is 3-component .ppm |
| 7861 | |
| 7862 | c = (char) stbi__get8(s); |
| 7863 | stbi__pnm_skip_whitespace(s, &c); |
| 7864 | |
| 7865 | *x = stbi__pnm_getinteger(s, &c); // read width |
| 7866 | if(*x == 0) |
| 7867 | return stbi__err("invalid width" , "PPM image header had zero or overflowing width" ); |
| 7868 | stbi__pnm_skip_whitespace(s, &c); |
| 7869 | |
| 7870 | *y = stbi__pnm_getinteger(s, &c); // read height |
| 7871 | if (*y == 0) |
| 7872 | return stbi__err("invalid width" , "PPM image header had zero or overflowing width" ); |
| 7873 | stbi__pnm_skip_whitespace(s, &c); |
| 7874 | |
| 7875 | maxv = stbi__pnm_getinteger(s, &c); // read max value |
| 7876 | if (maxv > 65535) |
| 7877 | return stbi__err("max value > 65535" , "PPM image supports only 8-bit and 16-bit images" ); |
| 7878 | else if (maxv > 255) |
| 7879 | return 16; |
| 7880 | else |
| 7881 | return 8; |
| 7882 | } |
| 7883 | |
| 7884 | static int stbi__pnm_is16(stbi__context *s) |
| 7885 | { |
| 7886 | if (stbi__pnm_info(s, NULL, NULL, NULL) == 16) |
| 7887 | return 1; |
| 7888 | return 0; |
| 7889 | } |
| 7890 | #endif |
| 7891 | |
| 7892 | #if 0 /* not used in SDL */ |
| 7893 | static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp) |
| 7894 | { |
| 7895 | #ifndef STBI_NO_JPEG |
| 7896 | if (stbi__jpeg_info(s, x, y, comp)) return 1; |
| 7897 | #endif |
| 7898 | |
| 7899 | #ifndef STBI_NO_PNG |
| 7900 | if (stbi__png_info(s, x, y, comp)) return 1; |
| 7901 | #endif |
| 7902 | |
| 7903 | #ifndef STBI_NO_GIF |
| 7904 | if (stbi__gif_info(s, x, y, comp)) return 1; |
| 7905 | #endif |
| 7906 | |
| 7907 | #ifndef STBI_NO_BMP |
| 7908 | if (stbi__bmp_info(s, x, y, comp)) return 1; |
| 7909 | #endif |
| 7910 | |
| 7911 | #ifndef STBI_NO_PSD |
| 7912 | if (stbi__psd_info(s, x, y, comp)) return 1; |
| 7913 | #endif |
| 7914 | |
| 7915 | #ifndef STBI_NO_PIC |
| 7916 | if (stbi__pic_info(s, x, y, comp)) return 1; |
| 7917 | #endif |
| 7918 | |
| 7919 | #ifndef STBI_NO_PNM |
| 7920 | if (stbi__pnm_info(s, x, y, comp)) return 1; |
| 7921 | #endif |
| 7922 | |
| 7923 | #ifndef STBI_NO_HDR |
| 7924 | if (stbi__hdr_info(s, x, y, comp)) return 1; |
| 7925 | #endif |
| 7926 | |
| 7927 | // test tga last because it's a crappy test! |
| 7928 | #ifndef STBI_NO_TGA |
| 7929 | if (stbi__tga_info(s, x, y, comp)) |
| 7930 | return 1; |
| 7931 | #endif |
| 7932 | return stbi__err("unknown image type" , "Image not of any known type, or corrupt" ); |
| 7933 | } |
| 7934 | |
| 7935 | static int stbi__is_16_main(stbi__context *s) |
| 7936 | { |
| 7937 | #ifndef STBI_NO_PNG |
| 7938 | if (stbi__png_is16(s)) return 1; |
| 7939 | #endif |
| 7940 | |
| 7941 | #ifndef STBI_NO_PSD |
| 7942 | if (stbi__psd_is16(s)) return 1; |
| 7943 | #endif |
| 7944 | |
| 7945 | #ifndef STBI_NO_PNM |
| 7946 | if (stbi__pnm_is16(s)) return 1; |
| 7947 | #endif |
| 7948 | return 0; |
| 7949 | } |
| 7950 | #endif /**/ |
| 7951 | |
| 7952 | #ifndef STBI_NO_STDIO |
| 7953 | STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp) |
| 7954 | { |
| 7955 | FILE *f = stbi__fopen(filename, "rb" ); |
| 7956 | int result; |
| 7957 | if (!f) return stbi__err("can't fopen" , "Unable to open file" ); |
| 7958 | result = stbi_info_from_file(f, x, y, comp); |
| 7959 | fclose(f); |
| 7960 | return result; |
| 7961 | } |
| 7962 | |
| 7963 | STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp) |
| 7964 | { |
| 7965 | int r; |
| 7966 | stbi__context s; |
| 7967 | long pos = ftell(f); |
| 7968 | stbi__start_file(&s, f); |
| 7969 | r = stbi__info_main(&s,x,y,comp); |
| 7970 | fseek(f,pos,SEEK_SET); |
| 7971 | return r; |
| 7972 | } |
| 7973 | |
| 7974 | STBIDEF int stbi_is_16_bit(char const *filename) |
| 7975 | { |
| 7976 | FILE *f = stbi__fopen(filename, "rb" ); |
| 7977 | int result; |
| 7978 | if (!f) return stbi__err("can't fopen" , "Unable to open file" ); |
| 7979 | result = stbi_is_16_bit_from_file(f); |
| 7980 | fclose(f); |
| 7981 | return result; |
| 7982 | } |
| 7983 | |
| 7984 | STBIDEF int stbi_is_16_bit_from_file(FILE *f) |
| 7985 | { |
| 7986 | int r; |
| 7987 | stbi__context s; |
| 7988 | long pos = ftell(f); |
| 7989 | stbi__start_file(&s, f); |
| 7990 | r = stbi__is_16_main(&s); |
| 7991 | fseek(f,pos,SEEK_SET); |
| 7992 | return r; |
| 7993 | } |
| 7994 | #endif // !STBI_NO_STDIO |
| 7995 | |
| 7996 | #if 0 /* not used in SDL */ |
| 7997 | STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp) |
| 7998 | { |
| 7999 | stbi__context s; |
| 8000 | stbi__start_mem(&s,buffer,len); |
| 8001 | return stbi__info_main(&s,x,y,comp); |
| 8002 | } |
| 8003 | |
| 8004 | STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp) |
| 8005 | { |
| 8006 | stbi__context s; |
| 8007 | stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user); |
| 8008 | return stbi__info_main(&s,x,y,comp); |
| 8009 | } |
| 8010 | |
| 8011 | STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len) |
| 8012 | { |
| 8013 | stbi__context s; |
| 8014 | stbi__start_mem(&s,buffer,len); |
| 8015 | return stbi__is_16_main(&s); |
| 8016 | } |
| 8017 | |
| 8018 | STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user) |
| 8019 | { |
| 8020 | stbi__context s; |
| 8021 | stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user); |
| 8022 | return stbi__is_16_main(&s); |
| 8023 | } |
| 8024 | #endif /**/ |
| 8025 | |
| 8026 | #endif // STB_IMAGE_IMPLEMENTATION |
| 8027 | |
| 8028 | /* |
| 8029 | revision history: |
| 8030 | 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs |
| 8031 | 2.19 (2018-02-11) fix warning |
| 8032 | 2.18 (2018-01-30) fix warnings |
| 8033 | 2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug |
| 8034 | 1-bit BMP |
| 8035 | *_is_16_bit api |
| 8036 | avoid warnings |
| 8037 | 2.16 (2017-07-23) all functions have 16-bit variants; |
| 8038 | STBI_NO_STDIO works again; |
| 8039 | compilation fixes; |
| 8040 | fix rounding in unpremultiply; |
| 8041 | optimize vertical flip; |
| 8042 | disable raw_len validation; |
| 8043 | documentation fixes |
| 8044 | 2.15 (2017-03-18) fix png-1,2,4 bug; now all Imagenet JPGs decode; |
| 8045 | warning fixes; disable run-time SSE detection on gcc; |
| 8046 | uniform handling of optional "return" values; |
| 8047 | thread-safe initialization of zlib tables |
| 8048 | 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs |
| 8049 | 2.13 (2016-11-29) add 16-bit API, only supported for PNG right now |
| 8050 | 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes |
| 8051 | 2.11 (2016-04-02) allocate large structures on the stack |
| 8052 | remove white matting for transparent PSD |
| 8053 | fix reported channel count for PNG & BMP |
| 8054 | re-enable SSE2 in non-gcc 64-bit |
| 8055 | support RGB-formatted JPEG |
| 8056 | read 16-bit PNGs (only as 8-bit) |
| 8057 | 2.10 (2016-01-22) avoid warning introduced in 2.09 by STBI_REALLOC_SIZED |
| 8058 | 2.09 (2016-01-16) allow comments in PNM files |
| 8059 | 16-bit-per-pixel TGA (not bit-per-component) |
| 8060 | info() for TGA could break due to .hdr handling |
| 8061 | info() for BMP to shares code instead of sloppy parse |
| 8062 | can use STBI_REALLOC_SIZED if allocator doesn't support realloc |
| 8063 | code cleanup |
| 8064 | 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA |
| 8065 | 2.07 (2015-09-13) fix compiler warnings |
| 8066 | partial animated GIF support |
| 8067 | limited 16-bpc PSD support |
| 8068 | #ifdef unused functions |
| 8069 | bug with < 92 byte PIC,PNM,HDR,TGA |
| 8070 | 2.06 (2015-04-19) fix bug where PSD returns wrong '*comp' value |
| 8071 | 2.05 (2015-04-19) fix bug in progressive JPEG handling, fix warning |
| 8072 | 2.04 (2015-04-15) try to re-enable SIMD on MinGW 64-bit |
| 8073 | 2.03 (2015-04-12) extra corruption checking (mmozeiko) |
| 8074 | stbi_set_flip_vertically_on_load (nguillemot) |
| 8075 | fix NEON support; fix mingw support |
| 8076 | 2.02 (2015-01-19) fix incorrect assert, fix warning |
| 8077 | 2.01 (2015-01-17) fix various warnings; suppress SIMD on gcc 32-bit without -msse2 |
| 8078 | 2.00b (2014-12-25) fix STBI_MALLOC in progressive JPEG |
| 8079 | 2.00 (2014-12-25) optimize JPG, including x86 SSE2 & NEON SIMD (ryg) |
| 8080 | progressive JPEG (stb) |
| 8081 | PGM/PPM support (Ken Miller) |
| 8082 | STBI_MALLOC,STBI_REALLOC,STBI_FREE |
| 8083 | GIF bugfix -- seemingly never worked |
| 8084 | STBI_NO_*, STBI_ONLY_* |
| 8085 | 1.48 (2014-12-14) fix incorrectly-named assert() |
| 8086 | 1.47 (2014-12-14) 1/2/4-bit PNG support, both direct and paletted (Omar Cornut & stb) |
| 8087 | optimize PNG (ryg) |
| 8088 | fix bug in interlaced PNG with user-specified channel count (stb) |
| 8089 | 1.46 (2014-08-26) |
| 8090 | fix broken tRNS chunk (colorkey-style transparency) in non-paletted PNG |
| 8091 | 1.45 (2014-08-16) |
| 8092 | fix MSVC-ARM internal compiler error by wrapping malloc |
| 8093 | 1.44 (2014-08-07) |
| 8094 | various warning fixes from Ronny Chevalier |
| 8095 | 1.43 (2014-07-15) |
| 8096 | fix MSVC-only compiler problem in code changed in 1.42 |
| 8097 | 1.42 (2014-07-09) |
| 8098 | don't define _CRT_SECURE_NO_WARNINGS (affects user code) |
| 8099 | fixes to stbi__cleanup_jpeg path |
| 8100 | added STBI_ASSERT to avoid requiring assert.h |
| 8101 | 1.41 (2014-06-25) |
| 8102 | fix search&replace from 1.36 that messed up comments/error messages |
| 8103 | 1.40 (2014-06-22) |
| 8104 | fix gcc struct-initialization warning |
| 8105 | 1.39 (2014-06-15) |
| 8106 | fix to TGA optimization when req_comp != number of components in TGA; |
| 8107 | fix to GIF loading because BMP wasn't rewinding (whoops, no GIFs in my test suite) |
| 8108 | add support for BMP version 5 (more ignored fields) |
| 8109 | 1.38 (2014-06-06) |
| 8110 | suppress MSVC warnings on integer casts truncating values |
| 8111 | fix accidental rename of 'skip' field of I/O |
| 8112 | 1.37 (2014-06-04) |
| 8113 | remove duplicate typedef |
| 8114 | 1.36 (2014-06-03) |
| 8115 | convert to header file single-file library |
| 8116 | if de-iphone isn't set, load iphone images color-swapped instead of returning NULL |
| 8117 | 1.35 (2014-05-27) |
| 8118 | various warnings |
| 8119 | fix broken STBI_SIMD path |
| 8120 | fix bug where stbi_load_from_file no longer left file pointer in correct place |
| 8121 | fix broken non-easy path for 32-bit BMP (possibly never used) |
| 8122 | TGA optimization by Arseny Kapoulkine |
| 8123 | 1.34 (unknown) |
| 8124 | use STBI_NOTUSED in stbi__resample_row_generic(), fix one more leak in tga failure case |
| 8125 | 1.33 (2011-07-14) |
| 8126 | make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements |
| 8127 | 1.32 (2011-07-13) |
| 8128 | support for "info" function for all supported filetypes (SpartanJ) |
| 8129 | 1.31 (2011-06-20) |
| 8130 | a few more leak fixes, bug in PNG handling (SpartanJ) |
| 8131 | 1.30 (2011-06-11) |
| 8132 | added ability to load files via callbacks to accomidate custom input streams (Ben Wenger) |
| 8133 | removed deprecated format-specific test/load functions |
| 8134 | removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway |
| 8135 | error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha) |
| 8136 | fix inefficiency in decoding 32-bit BMP (David Woo) |
| 8137 | 1.29 (2010-08-16) |
| 8138 | various warning fixes from Aurelien Pocheville |
| 8139 | 1.28 (2010-08-01) |
| 8140 | fix bug in GIF palette transparency (SpartanJ) |
| 8141 | 1.27 (2010-08-01) |
| 8142 | cast-to-stbi_uc to fix warnings |
| 8143 | 1.26 (2010-07-24) |
| 8144 | fix bug in file buffering for PNG reported by SpartanJ |
| 8145 | 1.25 (2010-07-17) |
| 8146 | refix trans_data warning (Won Chun) |
| 8147 | 1.24 (2010-07-12) |
| 8148 | perf improvements reading from files on platforms with lock-heavy fgetc() |
| 8149 | minor perf improvements for jpeg |
| 8150 | deprecated type-specific functions so we'll get feedback if they're needed |
| 8151 | attempt to fix trans_data warning (Won Chun) |
| 8152 | 1.23 fixed bug in iPhone support |
| 8153 | 1.22 (2010-07-10) |
| 8154 | removed image *writing* support |
| 8155 | stbi_info support from Jetro Lauha |
| 8156 | GIF support from Jean-Marc Lienher |
| 8157 | iPhone PNG-extensions from James Brown |
| 8158 | warning-fixes from Nicolas Schulz and Janez Zemva (i.stbi__err. Janez (U+017D)emva) |
| 8159 | 1.21 fix use of 'stbi_uc' in header (reported by jon blow) |
| 8160 | 1.20 added support for Softimage PIC, by Tom Seddon |
| 8161 | 1.19 bug in interlaced PNG corruption check (found by ryg) |
| 8162 | 1.18 (2008-08-02) |
| 8163 | fix a threading bug (local mutable static) |
| 8164 | 1.17 support interlaced PNG |
| 8165 | 1.16 major bugfix - stbi__convert_format converted one too many pixels |
| 8166 | 1.15 initialize some fields for thread safety |
| 8167 | 1.14 fix threadsafe conversion bug |
| 8168 | header-file-only version (#define STBI_HEADER_FILE_ONLY before including) |
| 8169 | 1.13 threadsafe |
| 8170 | 1.12 const qualifiers in the API |
| 8171 | 1.11 Support installable IDCT, colorspace conversion routines |
| 8172 | 1.10 Fixes for 64-bit (don't use "unsigned long") |
| 8173 | optimized upsampling by Fabian "ryg" Giesen |
| 8174 | 1.09 Fix format-conversion for PSD code (bad global variables!) |
| 8175 | 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz |
| 8176 | 1.07 attempt to fix C++ warning/errors again |
| 8177 | 1.06 attempt to fix C++ warning/errors again |
| 8178 | 1.05 fix TGA loading to return correct *comp and use good luminance calc |
| 8179 | 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free |
| 8180 | 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR |
| 8181 | 1.02 support for (subset of) HDR files, float interface for preferred access to them |
| 8182 | 1.01 fix bug: possible bug in handling right-side up bmps... not sure |
| 8183 | fix bug: the stbi__bmp_load() and stbi__tga_load() functions didn't work at all |
| 8184 | 1.00 interface to zlib that skips zlib header |
| 8185 | 0.99 correct handling of alpha in palette |
| 8186 | 0.98 TGA loader by lonesock; dynamically add loaders (untested) |
| 8187 | 0.97 jpeg errors on too large a file; also catch another malloc failure |
| 8188 | 0.96 fix detection of invalid v value - particleman@mollyrocket forum |
| 8189 | 0.95 during header scan, seek to markers in case of padding |
| 8190 | 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same |
| 8191 | 0.93 handle jpegtran output; verbose errors |
| 8192 | 0.92 read 4,8,16,24,32-bit BMP files of several formats |
| 8193 | 0.91 output 24-bit Windows 3.0 BMP files |
| 8194 | 0.90 fix a few more warnings; bump version number to approach 1.0 |
| 8195 | 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd |
| 8196 | 0.60 fix compiling as c++ |
| 8197 | 0.59 fix warnings: merge Dave Moore's -Wall fixes |
| 8198 | 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian |
| 8199 | 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available |
| 8200 | 0.56 fix bug: zlib uncompressed mode len vs. nlen |
| 8201 | 0.55 fix bug: restart_interval not initialized to 0 |
| 8202 | 0.54 allow NULL for 'int *comp' |
| 8203 | 0.53 fix bug in png 3->4; speedup png decoding |
| 8204 | 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments |
| 8205 | 0.51 obey req_comp requests, 1-component jpegs return as 1-component, |
| 8206 | on 'test' only check type, not whether we support this variant |
| 8207 | 0.50 (2006-11-19) |
| 8208 | first released version |
| 8209 | */ |
| 8210 | |
| 8211 | |
| 8212 | /* |
| 8213 | ------------------------------------------------------------------------------ |
| 8214 | This software is available under 2 licenses -- choose whichever you prefer. |
| 8215 | ------------------------------------------------------------------------------ |
| 8216 | ALTERNATIVE A - MIT License |
| 8217 | Copyright (c) 2017 Sean Barrett |
| 8218 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 8219 | this software and associated documentation files (the "Software"), to deal in |
| 8220 | the Software without restriction, including without limitation the rights to |
| 8221 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8222 | of the Software, and to permit persons to whom the Software is furnished to do |
| 8223 | so, subject to the following conditions: |
| 8224 | The above copyright notice and this permission notice shall be included in all |
| 8225 | copies or substantial portions of the Software. |
| 8226 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8227 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 8228 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 8229 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 8230 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 8231 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 8232 | SOFTWARE. |
| 8233 | ------------------------------------------------------------------------------ |
| 8234 | ALTERNATIVE B - Public Domain (www.unlicense.org) |
| 8235 | This is free and unencumbered software released into the public domain. |
| 8236 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this |
| 8237 | software, either in source code form or as a compiled binary, for any purpose, |
| 8238 | commercial or non-commercial, and by any means. |
| 8239 | In jurisdictions that recognize copyright laws, the author or authors of this |
| 8240 | software dedicate any and all copyright interest in the software to the public |
| 8241 | domain. We make this dedication for the benefit of the public at large and to |
| 8242 | the detriment of our heirs and successors. We intend this dedication to be an |
| 8243 | overt act of relinquishment in perpetuity of all present and future rights to |
| 8244 | this software under copyright law. |
| 8245 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8246 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 8247 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 8248 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 8249 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 8250 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 8251 | ------------------------------------------------------------------------------ |
| 8252 | */ |
| 8253 | |