1 /// PNG image loading. 2 /// D translation of stb_image-2.27 3 /// This port only support PNG loading, 8-bit and 16-bit. 4 module gamut.codecs.pngload; 5 6 version(decodePNG): 7 8 /* stb_image - v2.27 - public domain image loader - http://nothings.org/stb 9 no warranty implied; use at your own risk 10 11 12 QUICK NOTES: 13 Primarily of interest to game developers and other people who can 14 avoid problematic images and only need the trivial interface 15 16 JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib) 17 PNG 1/2/4/8/16-bit-per-channel 18 19 TGA (not sure what subset, if a subset) 20 BMP non-1bpp, non-RLE 21 PSD (composited view only, no extra channels, 8/16 bit-per-channel) 22 23 GIF (*comp always reports as 4-channel) 24 HDR (radiance rgbE format) 25 PIC (Softimage PIC) 26 PNM (PPM and PGM binary only) 27 28 Animated GIF still needs a proper API, but here's one way to do it: 29 http://gist.github.com/urraka/685d9a6340b26b830d49 30 31 - decode from memory 32 - decode from arbitrary I/O callbacks 33 - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON) 34 35 Full documentation under "DOCUMENTATION" below. 36 37 38 LICENSE 39 40 See end of file for license information. 41 42 RECENT REVISION HISTORY: 43 44 2.27 (2021-07-11) document stbi_info better, 16-bit PNM support, bug fixes 45 2.26 (2020-07-13) many minor fixes 46 2.25 (2020-02-02) fix warnings 47 2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically 48 2.23 (2019-08-11) fix clang static analysis warning 49 2.22 (2019-03-04) gif fixes, fix warnings 50 2.21 (2019-02-25) fix typo in comment 51 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs 52 2.19 (2018-02-11) fix warning 53 2.18 (2018-01-30) fix warnings 54 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings 55 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes 56 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC 57 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs 58 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes 59 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes 60 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64 61 RGB-format JPEG; remove white matting in PSD; 62 allocate large structures on the stack; 63 correct channel count for PNG & BMP 64 2.10 (2016-01-22) avoid warning introduced in 2.09 65 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED 66 67 See end of file for full revision history. 68 69 70 ============================ Contributors ========================= 71 72 Image formats Extensions, features 73 Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info) 74 Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info) 75 Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG) 76 Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks) 77 Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG) 78 Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip) 79 Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD) 80 github:urraka (animated gif) Junggon Kim (PNM comments) 81 Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA) 82 socks-the-fox (16-bit PNG) 83 Jeremy Sawicki (handle all ImageNet JPGs) 84 Optimizations & bugfixes Mikhail Morozov (1-bit BMP) 85 Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query) 86 Arseny Kapoulkine Simon Breuss (16-bit PNM) 87 John-Mark Allen 88 Carmelo J Fdez-Aguera 89 90 Bug & warning fixes 91 Marc LeBlanc David Woo Guillaume George Martins Mozeiko 92 Christpher Lloyd Jerry Jansson Joseph Thomson Blazej Dariusz Roszkowski 93 Phil Jordan Dave Moore Roy Eltham 94 Hayaki Saito Nathan Reed Won Chun 95 Luke Graham Johan Duparc Nick Verigakis the Horde3D community 96 Thomas Ruf Ronny Chevalier github:rlyeh 97 Janez Zemva John Bartholomew Michal Cichon github:romigrou 98 Jonathan Blow Ken Hamada Tero Hanninen github:svdijk 99 Eugene Golushkov Laurent Gomila Cort Stratton github:snagar 100 Aruelien Pocheville Sergio Gonzalez Thibault Reuille github:Zelex 101 Cass Everitt Ryamond Barbiero github:grim210 102 Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw 103 Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus 104 Josh Tobin Matthew Gregan github:poppolopoppo 105 Julian Raschke Gregory Mullen Christian Floisand github:darealshinji 106 Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007 107 Brad Weinberger Matvey Cherevko github:mosra 108 Luca Sas Alexander Veselov Zack Middleton [reserved] 109 Ryan C. Gordon [reserved] [reserved] 110 DO NOT ADD YOUR NAME HERE 111 112 Jacko Dirks 113 114 To add your name to the credits, pick a random blank space in the middle and fill it. 115 80% of merge conflicts on stb PRs are due to people adding their name at the end 116 of the credits. 117 */ 118 119 import core.stdc.string: memcpy, memset; 120 import core.stdc.stdlib: malloc, free, realloc; 121 import core.atomic; 122 123 import std.math: ldexp, pow, abs; 124 //import dplug.core.vec; 125 126 127 128 nothrow @nogc: 129 130 import inteli.emmintrin; 131 enum stbi__sse2_available = true; // because always available with intel-intrinsics 132 133 // DOCUMENTATION 134 // 135 // Limitations: 136 // - no 12-bit-per-channel JPEG 137 // - no JPEGs with arithmetic coding 138 // - GIF always returns *comp=4 139 // 140 // Basic usage (see HDR discussion below for HDR usage): 141 // int x,y,n; 142 // unsigned char *data = stbi_load(filename, &x, &y, &n, 0); 143 // // ... process data if not null ... 144 // // ... x = width, y = height, n = # 8-bit components per pixel ... 145 // // ... replace '0' with '1'..'4' to force that many components per pixel 146 // // ... but 'n' will always be the number that it would have been if you said 0 147 // stbi_image_free(data) 148 // 149 // Standard parameters: 150 // int *x -- outputs image width in pixels 151 // int *y -- outputs image height in pixels 152 // int *channels_in_file -- outputs # of image components in image file 153 // int desired_channels -- if non-zero, # of image components requested in result 154 // 155 // The return value from an image loader is an 'unsigned char *' which points 156 // to the pixel data, or null on an allocation failure or if the image is 157 // corrupt or invalid. The pixel data consists of *y scanlines of *x pixels, 158 // with each pixel consisting of N interleaved 8-bit components; the first 159 // pixel pointed to is top-left-most in the image. There is no padding between 160 // image scanlines or between pixels, regardless of format. The number of 161 // components N is 'desired_channels' if desired_channels is non-zero, or 162 // *channels_in_file otherwise. If desired_channels is non-zero, 163 // *channels_in_file has the number of components that _would_ have been 164 // output otherwise. E.g. if you set desired_channels to 4, you will always 165 // get RGBA output, but you can check *channels_in_file to see if it's trivially 166 // opaque because e.g. there were only 3 channels in the source image. 167 // 168 // An output image with N components has the following components interleaved 169 // in this order in each pixel: 170 // 171 // N=#comp components 172 // 1 grey 173 // 2 grey, alpha 174 // 3 red, green, blue 175 // 4 red, green, blue, alpha 176 // 177 // If image loading fails for any reason, the return value will be null, 178 // and *x, *y, *channels_in_file will be unchanged. The function 179 // stbi_failure_reason() can be queried for an extremely brief, end-user 180 // unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS 181 // to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly 182 // more user-friendly ones. 183 // 184 // Paletted PNG, BMP, GIF, and PIC images are automatically depalettized. 185 // 186 // To query the width, height and component count of an image without having to 187 // decode the full file, you can use the stbi_info family of functions: 188 // 189 // int x,y,n,ok; 190 // ok = stbi_info(filename, &x, &y, &n); 191 // // returns ok=1 and sets x, y, n if image is a supported format, 192 // // 0 otherwise. 193 // 194 // Note that stb_image pervasively uses ints in its public API for sizes, 195 // including sizes of memory buffers. This is now part of the API and thus 196 // hard to change without causing breakage. As a result, the various image 197 // loaders all have certain limits on image size; these differ somewhat 198 // by format but generally boil down to either just under 2GB or just under 199 // 1GB. When the decoded image would be larger than this, stb_image decoding 200 // will fail. 201 // 202 // Additionally, stb_image will reject image files that have any of their 203 // dimensions set to a larger value than the configurable STBI_MAX_DIMENSIONS, 204 // which defaults to 2**24 = 16777216 pixels. Due to the above memory limit, 205 // the only way to have an image with such dimensions load correctly 206 // is for it to have a rather extreme aspect ratio. Either way, the 207 // assumption here is that such larger images are likely to be malformed 208 // or malicious. If you do need to load an image with individual dimensions 209 // larger than that, and it still fits in the overall size limit, you can 210 // #define STBI_MAX_DIMENSIONS on your own to be something larger. 211 // 212 // 213 // Philosophy 214 // 215 // stb libraries are designed with the following priorities: 216 // 217 // 1. easy to use 218 // 2. easy to maintain 219 // 3. good performance 220 // 221 // Sometimes I let "good performance" creep up in priority over "easy to maintain", 222 // and for best performance I may provide less-easy-to-use APIs that give higher 223 // performance, in addition to the easy-to-use ones. Nevertheless, it's important 224 // to keep in mind that from the standpoint of you, a client of this library, 225 // all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all. 226 // 227 // Some secondary priorities arise directly from the first two, some of which 228 // provide more explicit reasons why performance can't be emphasized. 229 // 230 // - Portable ("ease of use") 231 // - Small source code footprint ("easy to maintain") 232 // - No dependencies ("ease of use") 233 // 234 // =========================================================================== 235 // 236 // I/O callbacks 237 // 238 // I/O callbacks allow you to read from arbitrary sources, like packaged 239 // files or some other source. Data read from callbacks are processed 240 // through a small internal buffer (currently 128 bytes) to try to reduce 241 // overhead. 242 // 243 // The three functions you must define are "read" (reads some bytes of data), 244 // "skip" (skips some bytes of data), "eof" (reports if the stream is at the end). 245 // 246 // =========================================================================== 247 // 248 // SIMD support 249 // 250 // The JPEG decoder will try to automatically use SIMD kernels on x86 when 251 // supported by the compiler. For ARM Neon support, you must explicitly 252 // request it. 253 // 254 // (The old do-it-yourself SIMD API is no longer supported in the current 255 // code.) 256 // 257 // On x86, SSE2 will automatically be used when available based on a run-time 258 // test; if not, the generic C versions are used as a fall-back. On ARM targets, 259 // the typical path is to have separate builds for NEON and non-NEON devices 260 // (at least this is true for iOS and Android). Therefore, the NEON support is 261 // toggled by a build flag: define STBI_NEON to get NEON loops. 262 // 263 // If for some reason you do not want to use any of SIMD code, or if 264 // you have issues compiling it, you can disable it entirely by 265 // defining STBI_NO_SIMD. 266 // 267 // =========================================================================== 268 // 269 // HDR image support (disable by defining STBI_NO_HDR) 270 // 271 // stb_image supports loading HDR images in general, and currently the Radiance 272 // .HDR file format specifically. You can still load any file through the existing 273 // interface; if you attempt to load an HDR file, it will be automatically remapped 274 // to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1; 275 // both of these constants can be reconfigured through this interface: 276 // 277 // stbi_hdr_to_ldr_gamma(2.2f); 278 // stbi_hdr_to_ldr_scale(1.0f); 279 // 280 // (note, do not use _inverse_ constants; stbi_image will invert them 281 // appropriately). 282 // 283 // Additionally, there is a new, parallel interface for loading files as 284 // (linear) floats to preserve the full dynamic range: 285 // 286 // float *data = stbi_loadf(filename, &x, &y, &n, 0); 287 // 288 // If you load LDR images through this interface, those images will 289 // be promoted to floating point values, run through the inverse of 290 // constants corresponding to the above: 291 // 292 // stbi_ldr_to_hdr_scale(1.0f); 293 // stbi_ldr_to_hdr_gamma(2.2f); 294 // 295 // Finally, given a filename (or an open file or memory block--see header 296 // file for details) containing image data, you can query for the "most 297 // appropriate" interface to use (that is, whether the image is HDR or 298 // not), using: 299 // 300 // stbi_is_hdr(char *filename); 301 // 302 // =========================================================================== 303 // 304 // iPhone PNG support: 305 // 306 // We optionally support converting iPhone-formatted PNGs (which store 307 // premultiplied BGRA) back to RGB, even though they're internally encoded 308 // differently. To enable this conversion, call 309 // stbi_convert_iphone_png_to_rgb(1). 310 // 311 // Call stbi_set_unpremultiply_on_load(1) as well to force a divide per 312 // pixel to remove any premultiplied alpha *only* if the image file explicitly 313 // says there's premultiplied data (currently only happens in iPhone images, 314 // and only if iPhone convert-to-rgb processing is on). 315 // 316 // =========================================================================== 317 // 318 // ADDITIONAL CONFIGURATION 319 // 320 // - You can suppress implementation of any of the decoders to reduce 321 // your code footprint by #defining one or more of the following 322 // symbols before creating the implementation. 323 // 324 // STBI_NO_JPEG 325 // STBI_NO_PNG 326 // STBI_NO_BMP 327 // STBI_NO_PSD 328 // STBI_NO_TGA 329 // STBI_NO_GIF 330 // STBI_NO_HDR 331 // STBI_NO_PIC 332 // STBI_NO_PNM (.ppm and .pgm) 333 // 334 // 335 // - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still 336 // want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB 337 // 338 // - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater 339 // than that size (in either width or height) without further processing. 340 // This is to let programs in the wild set an upper bound to prevent 341 // denial-of-service attacks on untrusted data, as one could generate a 342 // valid image of gigantic dimensions and force stb_image to allocate a 343 // huge block of memory and spend disproportionate time decoding it. By 344 // default this is set to (1 << 24), which is 16777216, but that's still 345 // very big. 346 347 348 version = decodePNG; 349 //version = decodeBMP; 350 //version = decodePSD; 351 //version = decodeTGA; 352 //version = decodeGIF; 353 //version = decodeHDR; 354 //version = decodePIC; 355 //version = decodePNM; 356 357 //version = enableLinear; // STBI_NO_LINEAR 358 //version = enableFailureStrings; 359 //version = enableFailureStringsUser; // prettier error messages 360 361 enum STBI_VERSION = 1; 362 363 enum 364 { 365 STBI_default = 0, // only used for desired_channels 366 367 STBI_grey = 1, 368 STBI_grey_alpha = 2, 369 STBI_rgb = 3, 370 STBI_rgb_alpha = 4 371 } 372 373 alias stbi_uc = ubyte; 374 alias stbi_us = ushort; 375 376 ////////////////////////////////////////////////////////////////////////////// 377 // 378 // PRIMARY API - works on images of any type 379 // 380 381 // 382 // load image by filename, open file, or memory buffer 383 // 384 385 struct stbi_io_callbacks 386 { 387 nothrow @nogc @system: 388 // fill 'data' with 'size' bytes. return number of bytes actually read 389 int function(void *user,char *data,int size) read; 390 391 // skip the next 'n' bytes, or 'unget' the last -n bytes if negative 392 void function(void *user,int n) skip; 393 394 // returns nonzero if we are at end of file/data 395 int function(void *user) eof; 396 } 397 398 // <Implementation> 399 400 version = STBI_NO_THREAD_LOCALS; 401 402 alias stbi__uint16 = ushort; 403 alias stbi__int16 = short; 404 alias stbi__uint32 = uint; 405 alias stbi__int32 = int; 406 407 uint stbi_lrot(uint x, int y) 408 { 409 return (x << y) | (x >> (-y & 31)); 410 } 411 412 void* STBI_MALLOC(size_t size) 413 { 414 return malloc(size); 415 } 416 417 void* STBI_REALLOC(void* p, size_t new_size) 418 { 419 return realloc(p, new_size); 420 } 421 422 void* STBI_REALLOC_SIZED(void *ptr, size_t old_size, size_t new_size) 423 { 424 return realloc(ptr, new_size); 425 } 426 427 void STBI_FREE(void* p) 428 { 429 free(p); 430 } 431 432 //alias STBI_MALLOC = malloc; 433 //alias STBI_REALLOC = realloc; 434 //alias STBI_FREE = free; 435 436 enum STBI_MAX_DIMENSIONS = (1 << 24); 437 438 /////////////////////////////////////////////// 439 // 440 // stbi__context struct and start_xxx functions 441 442 // stbi__context structure is our basic context used by all images, so it 443 // contains all the IO context, plus some basic image information 444 struct stbi__context 445 { 446 stbi__uint32 img_x, img_y; 447 int img_n, img_out_n; 448 449 stbi_io_callbacks io; 450 void *io_user_data; 451 452 int read_from_callbacks; 453 int buflen; 454 stbi_uc[128] buffer_start; 455 int callback_already_read; 456 457 stbi_uc *img_buffer, img_buffer_end; 458 stbi_uc *img_buffer_original, img_buffer_original_end; 459 460 float ppmX; 461 float ppmY; 462 float pixelAspectRatio; 463 } 464 465 466 // initialize a callback-based context 467 void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user) 468 { 469 s.io = *c; 470 s.io_user_data = user; 471 s.buflen = s.buffer_start.sizeof; 472 s.read_from_callbacks = 1; 473 s.callback_already_read = 0; 474 s.img_buffer = s.img_buffer_original = s.buffer_start.ptr; 475 stbi__refill_buffer(s); 476 s.img_buffer_original_end = s.img_buffer_end; 477 } 478 479 void stbi__rewind(stbi__context *s) 480 { 481 // conceptually rewind SHOULD rewind to the beginning of the stream, 482 // but we just rewind to the beginning of the initial buffer, because 483 // we only use it after doing 'test', which only ever looks at at most 92 bytes 484 s.img_buffer = s.img_buffer_original; 485 s.img_buffer_end = s.img_buffer_original_end; 486 } 487 488 enum 489 { 490 STBI_ORDER_RGB, 491 STBI_ORDER_BGR 492 } 493 494 struct stbi__result_info 495 { 496 int bits_per_channel; 497 int num_channels; 498 int channel_order; 499 } 500 501 version(enableFailureStrings) 502 { 503 // Note: this is a global, so if multiple image loads happen at once, the reason given might be racey. 504 __gshared const(char)* stbi__g_failure_reason; 505 506 const(char*) stbi_failure_reason() 507 { 508 return stbi__g_failure_reason; 509 } 510 511 int stbi_err(const(char)* str) 512 { 513 stbi__g_failure_reason = str; 514 return 0; 515 } 516 } 517 518 alias stbi__malloc = STBI_MALLOC; 519 520 // stb_image uses ints pervasively, including for offset calculations. 521 // therefore the largest decoded image size we can support with the 522 // current code, even on 64-bit targets, is INT_MAX. this is not a 523 // significant limitation for the intended use case. 524 // 525 // we do, however, need to make sure our size calculations don't 526 // overflow. hence a few helper functions for size calculations that 527 // multiply integers together, making sure that they're non-negative 528 // and no overflow occurs. 529 530 // return 1 if the sum is valid, 0 on overflow. 531 // negative terms are considered invalid. 532 int stbi__addsizes_valid(int a, int b) 533 { 534 if (b < 0) return 0; 535 // now 0 <= b <= INT_MAX, hence also 536 // 0 <= INT_MAX - b <= INTMAX. 537 // And "a + b <= INT_MAX" (which might overflow) is the 538 // same as a <= INT_MAX - b (no overflow) 539 return a <= int.max - b; 540 } 541 542 // returns 1 if the product is valid, 0 on overflow. 543 // negative factors are considered invalid. 544 int stbi__mul2sizes_valid(int a, int b) 545 { 546 if (a < 0 || b < 0) return 0; 547 if (b == 0) return 1; // mul-by-0 is always safe 548 // portable way to check for no overflows in a*b 549 return a <= int.max/b; 550 } 551 552 int stbi__mad2sizes_valid(int a, int b, int add) 553 { 554 return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add); 555 } 556 557 // returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow 558 int stbi__mad3sizes_valid(int a, int b, int c, int add) 559 { 560 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && 561 stbi__addsizes_valid(a*b*c, add); 562 } 563 564 // returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow 565 int stbi__mad4sizes_valid(int a, int b, int c, int d, int add) 566 { 567 return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && 568 stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add); 569 } 570 571 void *stbi__malloc_mad2(int a, int b, int add) 572 { 573 if (!stbi__mad2sizes_valid(a, b, add)) return null; 574 return stbi__malloc(a*b + add); 575 } 576 577 void *stbi__malloc_mad3(int a, int b, int c, int add) 578 { 579 if (!stbi__mad3sizes_valid(a, b, c, add)) return null; 580 return stbi__malloc(a*b*c + add); 581 } 582 583 void *stbi__malloc_mad4(int a, int b, int c, int d, int add) 584 { 585 if (!stbi__mad4sizes_valid(a, b, c, d, add)) return null; 586 return stbi__malloc(a*b*c*d + add); 587 } 588 589 // stbi__err - error 590 591 version(enableFailureStrings) 592 { 593 int stbi__err(const(char)* msg, const(char)* msgUser) 594 { 595 return stbi_err(msg); 596 } 597 } 598 else version(enableFailureStringsUser) 599 { 600 int stbi__err(const(char)* msg, const(char)* msgUser) 601 { 602 return stbi_err(msgUser); 603 } 604 } 605 else 606 { 607 deprecated int stbi__err(const(char)* msg, const(char)* msgUser) 608 { 609 return 0; 610 } 611 } 612 613 // stbi__errpf - error returning pointer to float 614 // stbi__errpuc - error returning pointer to unsigned char 615 deprecated float* stbi__errpf(const(char)* msg, const(char)* msgUser) 616 { 617 return cast(float*) (cast(size_t) stbi__err(msg, msgUser)); 618 } 619 620 deprecated ubyte* stbi__errpuc(const(char)* msg, const(char)* msgUser) 621 { 622 return cast(ubyte*) (cast(size_t) stbi__err(msg, msgUser)); 623 } 624 625 void stbi_image_free(void *retval_from_stbi_load) @trusted // TODO: make it @safe by changing stbi_load to return a slice 626 { 627 STBI_FREE(retval_from_stbi_load); 628 } 629 630 void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc) 631 { 632 memset(ri, 0, (*ri).sizeof); // make sure it's initialized if we add new fields 633 ri.bits_per_channel = 8; // default is 8 so most paths don't have to be changed 634 ri.channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order 635 ri.num_channels = 0; 636 637 // test the formats with a very explicit header first (at least a FOURCC 638 // or distinctive magic number first) 639 version(decodePNG) 640 { 641 if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, ri); 642 } 643 return null; 644 } 645 646 stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels) 647 { 648 int img_len = w * h * channels; 649 stbi_uc *reduced; 650 651 reduced = cast(stbi_uc *) stbi__malloc(img_len); 652 if (reduced == null) 653 return null; 654 655 for (int i = 0; i < img_len; ++i) 656 reduced[i] = cast(stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16.8 bit scaling 657 658 STBI_FREE(orig); 659 return reduced; 660 } 661 662 stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels) 663 { 664 int i; 665 int img_len = w * h * channels; 666 stbi__uint16 *enlarged; 667 668 enlarged = cast(stbi__uint16 *) stbi__malloc(img_len*2); 669 if (enlarged == null) 670 return null; 671 672 for (i = 0; i < img_len; ++i) 673 enlarged[i] = (orig[i] << 8) + orig[i]; // replicate to high and low byte, maps 0.0, 255.0xffff 674 675 STBI_FREE(orig); 676 return enlarged; 677 } 678 679 680 ubyte *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) 681 { 682 stbi__result_info ri; 683 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8); 684 685 if (result == null) 686 return null; 687 688 // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. 689 assert(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); 690 691 if (ri.bits_per_channel != 8) { 692 result = stbi__convert_16_to_8(cast(stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp); 693 ri.bits_per_channel = 8; 694 } 695 696 // @TODO: move stbi__convert_format to here 697 698 return cast(ubyte*) result; 699 } 700 701 stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) 702 { 703 stbi__result_info ri; 704 void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16); 705 706 if (result == null) 707 return null; 708 709 // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. 710 assert(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); 711 712 if (ri.bits_per_channel != 16) { 713 result = stbi__convert_8_to_16(cast(stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp); 714 ri.bits_per_channel = 16; 715 } 716 717 return cast(stbi__uint16 *) result; 718 } 719 720 void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp) 721 { 722 } 723 724 stbi_us *stbi_load_16_from_callbacks(const(stbi_io_callbacks)*clbk, void *user, int *x, int *y, int *channels_in_file, 725 int desired_channels,float* ppmX, float* ppmY, float* pixelRatio) 726 { 727 stbi__context s; 728 stbi__start_callbacks(&s, cast(stbi_io_callbacks *)clbk, user); // const_cast here 729 stbi_us* res = stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); 730 *ppmX = s.ppmX; 731 *ppmY = s.ppmY; 732 *pixelRatio = s.pixelAspectRatio; 733 return res; 734 } 735 736 stbi_uc *stbi_load_from_callbacks(const(stbi_io_callbacks)*clbk, void *user, int *x, int *y, int *comp, int req_comp, 737 float* ppmX, float* ppmY, float* pixelRatio) 738 { 739 stbi__context s; 740 stbi__start_callbacks(&s, cast(stbi_io_callbacks *) clbk, user); // const_cast here 741 stbi_uc* res = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); 742 *ppmX = s.ppmX; 743 *ppmY = s.ppmY; 744 *pixelRatio = s.pixelAspectRatio; 745 return res; 746 } 747 748 version(enableLinear) 749 { 750 __gshared stbi__l2h_gamma = 2.2f; 751 __gshared stbi__l2h_scale = 1.0f; 752 753 void stbi_ldr_to_hdr_gamma(float gamma) 754 { 755 atomicStore(stbi__l2h_gamma, gamma); 756 } 757 758 void stbi_ldr_to_hdr_scale(float scale) 759 { 760 atomicStore(stbi__l2h_scale, scale); 761 } 762 } 763 764 765 shared(float) stbi__h2l_gamma_i = 1.0f / 2.2f, 766 stbi__h2l_scale_i = 1.0f; 767 768 void stbi_hdr_to_ldr_gamma(float gamma) 769 { 770 atomicStore(stbi__h2l_gamma_i, 1 / gamma); 771 } 772 773 void stbi_hdr_to_ldr_scale(float scale) 774 { 775 atomicStore(stbi__h2l_scale_i, 1 / scale); 776 } 777 778 779 ////////////////////////////////////////////////////////////////////////////// 780 // 781 // Common code used by all image loaders 782 // 783 784 enum 785 { 786 STBI__SCAN_load = 0, 787 STBI__SCAN_type, 788 STBI__SCAN_header 789 } 790 791 void stbi__refill_buffer(stbi__context *s) 792 { 793 int n = s.io.read(s.io_user_data, cast(char*)s.buffer_start, s.buflen); 794 s.callback_already_read += cast(int) (s.img_buffer - s.img_buffer_original); 795 if (n == 0) { 796 // at end of file, treat same as if from memory, but need to handle case 797 // where s.img_buffer isn't pointing to safe memory, e.g. 0-byte file 798 s.read_from_callbacks = 0; 799 s.img_buffer = s.buffer_start.ptr; 800 s.img_buffer_end = s.buffer_start.ptr+1; 801 *s.img_buffer = 0; 802 } else { 803 s.img_buffer = s.buffer_start.ptr; 804 s.img_buffer_end = s.buffer_start.ptr + n; 805 } 806 } 807 808 stbi_uc stbi__get8(stbi__context *s) 809 { 810 if (s.img_buffer < s.img_buffer_end) 811 return *s.img_buffer++; 812 if (s.read_from_callbacks) { 813 stbi__refill_buffer(s); 814 return *s.img_buffer++; 815 } 816 return 0; 817 } 818 819 int stbi__at_eof(stbi__context *s) 820 { 821 if (s.io.read) 822 { 823 if (!s.io.eof(s.io_user_data)) 824 return 0; 825 // if feof() is true, check if buffer = end 826 // special case: we've only got the special 0 character at the end 827 if (s.read_from_callbacks == 0) 828 return 1; 829 } 830 return s.img_buffer >= s.img_buffer_end; 831 } 832 833 void stbi__skip(stbi__context *s, int n) 834 { 835 if (n == 0) 836 return; // already there! 837 if (n < 0) 838 { 839 s.img_buffer = s.img_buffer_end; 840 return; 841 } 842 if (s.io.read) 843 { 844 int blen = cast(int) (s.img_buffer_end - s.img_buffer); 845 if (blen < n) 846 { 847 s.img_buffer = s.img_buffer_end; 848 s.io.skip(s.io_user_data, n - blen); 849 return; 850 } 851 } 852 s.img_buffer += n; 853 } 854 855 int stbi__getn(stbi__context *s, stbi_uc *buffer, int n) 856 { 857 if (s.io.read) 858 { 859 int blen = cast(int) (s.img_buffer_end - s.img_buffer); 860 if (blen < n) 861 { 862 int res, count; 863 memcpy(buffer, s.img_buffer, blen); 864 count = s.io.read(s.io_user_data, cast(char*) buffer + blen, n - blen); 865 res = (count == (n-blen)); 866 s.img_buffer = s.img_buffer_end; 867 return res; 868 } 869 } 870 871 if (s.img_buffer+n <= s.img_buffer_end) 872 { 873 memcpy(buffer, s.img_buffer, n); 874 s.img_buffer += n; 875 return 1; 876 } 877 else 878 return 0; 879 } 880 881 int stbi__get16be(stbi__context *s) 882 { 883 int z = stbi__get8(s); 884 return (z << 8) + stbi__get8(s); 885 } 886 887 stbi__uint32 stbi__get32be(stbi__context *s) 888 { 889 stbi__uint32 z = stbi__get16be(s); 890 return (z << 16) + stbi__get16be(s); 891 } 892 893 int stbi__get16le(stbi__context *s) 894 { 895 int z = stbi__get8(s); 896 return z + (stbi__get8(s) << 8); 897 } 898 899 stbi__uint32 stbi__get32le(stbi__context *s) 900 { 901 stbi__uint32 z = stbi__get16le(s); 902 z += cast(stbi__uint32)stbi__get16le(s) << 16; 903 return z; 904 } 905 906 ubyte STBI__BYTECAST(T)(T x) 907 { 908 return cast(ubyte)(x & 255); 909 } 910 911 ////////////////////////////////////////////////////////////////////////////// 912 // 913 // generic converter from built-in img_n to req_comp 914 // individual types do this automatically as much as possible (e.g. jpeg 915 // does all cases internally since it needs to colorspace convert anyway, 916 // and it never has alpha, so very few cases ). png can automatically 917 // interleave an alpha=255 channel, but falls back to this for other cases 918 // 919 // assume data buffer is malloced, so malloc a new one and free that one 920 // only failure mode is malloc failing 921 922 stbi_uc stbi__compute_y(int r, int g, int b) 923 { 924 return cast(ubyte)(((r * 77) + (g * 150) + (29 * b)) >> 8); 925 } 926 927 ubyte *stbi__convert_format(ubyte *data, int img_n, int req_comp, uint x, uint y) 928 { 929 int i,j; 930 ubyte *good; 931 932 if (req_comp == img_n) 933 return data; 934 assert(req_comp >= 1 && req_comp <= 4); 935 936 good = cast(ubyte*) stbi__malloc_mad3(req_comp, x, y, 0); 937 if (good == null) 938 { 939 STBI_FREE(data); 940 return null; 941 } 942 943 for (j = 0; j < cast(int) y; ++j) 944 { 945 ubyte *src = data + j * x * img_n ; 946 ubyte *dest = good + j * x * req_comp; 947 948 // convert source image with img_n components to one with req_comp components; 949 // avoid switch per pixel, so use switch per scanline and massive macros 950 switch (img_n * 8 + req_comp) 951 { 952 case 1 * 8 + 2: 953 { 954 for(i = x - 1; i >= 0; --i, src += 1, dest += 2) 955 { 956 dest[0] = src[0]; 957 dest[1] = 255; 958 } 959 } 960 break; 961 case 1 * 8 + 3: 962 { 963 for(i = x - 1; i >= 0; --i, src += 1, dest += 3) 964 { 965 dest[0] = dest[1] = dest[2] = src[0]; 966 } 967 } 968 break; 969 case 1 * 8 + 4: 970 for(i = x - 1; i >= 0; --i, src += 1, dest += 4) 971 { 972 dest[0] = dest[1] = dest[2] = src[0]; 973 dest[3] = 255; 974 } 975 break; 976 case 2 * 8 + 1: 977 { 978 for(i = x - 1; i >= 0; --i, src += 2, dest += 1) 979 { 980 dest[0] = src[0]; 981 } 982 } 983 break; 984 case 2 * 8 + 3: 985 { 986 for(i = x - 1; i >= 0; --i, src += 2, dest += 3) 987 { 988 dest[0] = dest[1] = dest[2] = src[0]; 989 } 990 } 991 break; 992 case 2 * 8 + 4: 993 { 994 for(i = x - 1; i >= 0; --i, src += 2, dest += 4) 995 { 996 dest[0] = dest[1] = dest[2] = src[0]; 997 dest[3] = src[1]; 998 } 999 } 1000 break; 1001 case 3 * 8 + 4: 1002 { 1003 for(i = x - 1; i >= 0; --i, src += 3, dest += 4) 1004 { 1005 dest[0] = src[0]; 1006 dest[1] = src[1]; 1007 dest[2] = src[2]; 1008 dest[3] = 255; 1009 } 1010 } 1011 break; 1012 case 3 * 8 + 1: 1013 { 1014 for(i = x - 1; i >= 0; --i, src += 3, dest += 1) 1015 { 1016 dest[0] = stbi__compute_y(src[0],src[1],src[2]); 1017 } 1018 } 1019 break; 1020 case 3 * 8 + 2: 1021 { 1022 for(i = x - 1; i >= 0; --i, src += 3, dest += 2) 1023 { 1024 dest[0] = stbi__compute_y(src[0],src[1],src[2]); 1025 dest[1] = 255; 1026 } 1027 } 1028 break; 1029 1030 case 4 * 8 + 1: 1031 { 1032 for(i = x - 1; i >= 0; --i, src += 4, dest += 1) 1033 { 1034 dest[0] = stbi__compute_y(src[0],src[1],src[2]); 1035 } 1036 } 1037 break; 1038 1039 case 4 * 8 + 2: 1040 { 1041 for(i = x - 1; i >= 0; --i, src += 4, dest += 2) 1042 { 1043 dest[0] = stbi__compute_y(src[0],src[1],src[2]); 1044 dest[1] = src[3]; 1045 } 1046 } 1047 break; 1048 case 4 * 8 + 3: 1049 { 1050 for(i = x - 1; i >= 0; --i, src += 4, dest += 3) 1051 { 1052 dest[0] = src[0]; 1053 dest[1] = src[1]; 1054 dest[2] = src[2]; 1055 } 1056 } 1057 break; 1058 default: 1059 assert(0); 1060 } 1061 } 1062 1063 STBI_FREE(data); 1064 return good; 1065 } 1066 1067 stbi__uint16 stbi__compute_y_16(int r, int g, int b) 1068 { 1069 return cast(stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8); 1070 } 1071 1072 stbi__uint16* stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, uint x, uint y) 1073 { 1074 int i,j; 1075 stbi__uint16 *good; 1076 1077 if (req_comp == img_n) 1078 return data; 1079 assert(req_comp >= 1 && req_comp <= 4); 1080 1081 good = cast(stbi__uint16 *) stbi__malloc(req_comp * x * y * 2); 1082 if (good == null) 1083 { 1084 STBI_FREE(data); 1085 return null; 1086 } 1087 1088 for (j = 0; j < cast(int) y; ++j) 1089 { 1090 stbi__uint16 *src = data + j * x * img_n ; 1091 stbi__uint16 *dest = good + j * x * req_comp; 1092 1093 // convert source image with img_n components to one with req_comp components; 1094 // avoid switch per pixel, so use switch per scanline and massive macros 1095 switch (img_n * 8 + req_comp) 1096 { 1097 case 1 * 8 + 2: 1098 { 1099 for(i = x - 1; i >= 0; --i, src += 1, dest += 2) 1100 { 1101 dest[0] = src[0]; 1102 dest[1] = 0xffff; 1103 } 1104 } 1105 break; 1106 case 1 * 8 + 3: 1107 { 1108 for(i = x - 1; i >= 0; --i, src += 1, dest += 3) 1109 { 1110 dest[0] = dest[1] = dest[2] = src[0]; 1111 } 1112 } 1113 break; 1114 case 1 * 8 + 4: 1115 for(i = x - 1; i >= 0; --i, src += 1, dest += 4) 1116 { 1117 dest[0] = dest[1] = dest[2] = src[0]; 1118 dest[3] = 0xffff; 1119 } 1120 break; 1121 case 2 * 8 + 1: 1122 { 1123 for(i = x - 1; i >= 0; --i, src += 2, dest += 1) 1124 { 1125 dest[0] = src[0]; 1126 } 1127 } 1128 break; 1129 case 2 * 8 + 3: 1130 { 1131 for(i = x - 1; i >= 0; --i, src += 2, dest += 3) 1132 { 1133 dest[0] = dest[1] = dest[2] = src[0]; 1134 } 1135 } 1136 break; 1137 case 2 * 8 + 4: 1138 { 1139 for(i = x - 1; i >= 0; --i, src += 2, dest += 4) 1140 { 1141 dest[0] = dest[1] = dest[2] = src[0]; 1142 dest[3] = src[1]; 1143 } 1144 } 1145 break; 1146 case 3 * 8 + 4: 1147 { 1148 for(i = x - 1; i >= 0; --i, src += 3, dest += 4) 1149 { 1150 dest[0] = src[0]; 1151 dest[1] = src[1]; 1152 dest[2] = src[2]; 1153 dest[3] = 0xffff; 1154 } 1155 } 1156 break; 1157 case 3 * 8 + 1: 1158 { 1159 for(i = x - 1; i >= 0; --i, src += 3, dest += 1) 1160 { 1161 dest[0] = stbi__compute_y_16(src[0],src[1],src[2]); 1162 } 1163 } 1164 break; 1165 case 3 * 8 + 2: 1166 { 1167 for(i = x - 1; i >= 0; --i, src += 3, dest += 2) 1168 { 1169 dest[0] = stbi__compute_y_16(src[0],src[1],src[2]); 1170 dest[1] = 0xffff; 1171 } 1172 } 1173 break; 1174 1175 case 4 * 8 + 1: 1176 { 1177 for(i = x - 1; i >= 0; --i, src += 4, dest += 1) 1178 { 1179 dest[0] = stbi__compute_y_16(src[0],src[1],src[2]); 1180 } 1181 } 1182 break; 1183 1184 case 4 * 8 + 2: 1185 { 1186 for(i = x - 1; i >= 0; --i, src += 4, dest += 2) 1187 { 1188 dest[0] = stbi__compute_y_16(src[0],src[1],src[2]); 1189 dest[1] = src[3]; 1190 } 1191 } 1192 break; 1193 case 4 * 8 + 3: 1194 { 1195 for(i = x - 1; i >= 0; --i, src += 4, dest += 3) 1196 { 1197 dest[0] = src[0]; 1198 dest[1] = src[1]; 1199 dest[2] = src[2]; 1200 } 1201 } 1202 break; 1203 default: 1204 assert(0); 1205 } 1206 } 1207 1208 STBI_FREE(data); 1209 return good; 1210 } 1211 1212 version(enableLinear) 1213 { 1214 float* stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp) 1215 { 1216 int i,k,n; 1217 float *output; 1218 if (!data) return null; 1219 output = cast(float *) stbi__malloc_mad4(x, y, comp, float.sizeof, 0); 1220 if (output == null) 1221 { 1222 STBI_FREE(data); 1223 return null; 1224 } 1225 // compute number of non-alpha components 1226 if (comp & 1) 1227 n = comp; 1228 else 1229 n = comp - 1; 1230 for (i = 0; i < x*y; ++i) 1231 { 1232 for (k = 0; k < n; ++k) 1233 { 1234 output[i*comp + k] = cast(float) (pow(data[i*comp+k] / 255.0f, stbi__l2h_gamma) * stbi__l2h_scale); 1235 } 1236 } 1237 if (n < comp) 1238 { 1239 for (i=0; i < x*y; ++i) 1240 { 1241 output[i*comp + n] = data[i*comp + n] / 255.0f; 1242 } 1243 } 1244 STBI_FREE(data); 1245 return output; 1246 } 1247 } 1248 1249 int stbi__float2int(float x) 1250 { 1251 return cast(int)x; 1252 } 1253 1254 // public domain zlib decode v0.2 Sean Barrett 2006-11-18 1255 // simple implementation 1256 // - all input must be provided in an upfront buffer 1257 // - all output is written to a single output buffer (can malloc/realloc) 1258 // performance 1259 // - fast huffman 1260 1261 // fast-way is faster to check than jpeg huffman, but slow way is slower 1262 enum STBI__ZFAST_BITS = 9; // accelerate all cases in default tables 1263 enum STBI__ZFAST_MASK = ((1 << STBI__ZFAST_BITS) - 1); 1264 enum STBI__ZNSYMS = 288; // number of symbols in literal/length alphabet 1265 1266 // zlib-style huffman encoding 1267 // (jpegs packs from left, zlib from right, so can't share code) 1268 struct stbi__zhuffman 1269 { 1270 stbi__uint16[1 << STBI__ZFAST_BITS] fast; 1271 stbi__uint16[16] firstcode; 1272 int[17] maxcode; 1273 stbi__uint16[16] firstsymbol; 1274 stbi_uc[STBI__ZNSYMS] size; 1275 stbi__uint16[STBI__ZNSYMS] value; 1276 } 1277 1278 int stbi__bitreverse16(int n) 1279 { 1280 n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1); 1281 n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2); 1282 n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4); 1283 n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8); 1284 return n; 1285 } 1286 1287 int stbi__bit_reverse(int v, int bits) 1288 { 1289 assert(bits <= 16); 1290 // to bit reverse n bits, reverse 16 and shift 1291 // e.g. 11 bits, bit reverse and shift away 5 1292 return stbi__bitreverse16(v) >> (16-bits); 1293 } 1294 1295 int stbi__zbuild_huffman(stbi__zhuffman *z, const stbi_uc *sizelist, int num) 1296 { 1297 int i,k=0; 1298 int code; 1299 int[16] next_code; 1300 int[17] sizes; 1301 1302 // DEFLATE spec for generating codes 1303 memset(sizes.ptr, 0, sizes.sizeof); 1304 memset(z.fast.ptr, 0, z.fast.sizeof); 1305 for (i=0; i < num; ++i) 1306 ++sizes[sizelist[i]]; 1307 sizes[0] = 0; 1308 for (i=1; i < 16; ++i) 1309 if (sizes[i] > (1 << i)) 1310 return 0; // stbi__err("bad sizes", "Corrupt PNG"); 1311 code = 0; 1312 for (i=1; i < 16; ++i) { 1313 next_code[i] = code; 1314 z.firstcode[i] = cast(stbi__uint16) code; 1315 z.firstsymbol[i] = cast(stbi__uint16) k; 1316 code = (code + sizes[i]); 1317 if (sizes[i]) 1318 if (code-1 >= (1 << i)) return 0; // stbi__err("bad codelengths","Corrupt PNG"); 1319 z.maxcode[i] = code << (16-i); // preshift for inner loop 1320 code <<= 1; 1321 k += sizes[i]; 1322 } 1323 z.maxcode[16] = 0x10000; // sentinel 1324 for (i=0; i < num; ++i) { 1325 int s = sizelist[i]; 1326 if (s) { 1327 int c = next_code[s] - z.firstcode[s] + z.firstsymbol[s]; 1328 stbi__uint16 fastv = cast(stbi__uint16) ((s << 9) | i); 1329 z.size [c] = cast(stbi_uc ) s; 1330 z.value[c] = cast(stbi__uint16) i; 1331 if (s <= STBI__ZFAST_BITS) { 1332 int j = stbi__bit_reverse(next_code[s],s); 1333 while (j < (1 << STBI__ZFAST_BITS)) { 1334 z.fast[j] = fastv; 1335 j += (1 << s); 1336 } 1337 } 1338 ++next_code[s]; 1339 } 1340 } 1341 return 1; 1342 } 1343 1344 // zlib-from-memory implementation for PNG reading 1345 // because PNG allows splitting the zlib stream arbitrarily, 1346 // and it's annoying structurally to have PNG call ZLIB call PNG, 1347 // we require PNG read all the IDATs and combine them into a single 1348 // memory buffer 1349 1350 struct stbi__zbuf 1351 { 1352 stbi_uc *zbuffer, zbuffer_end; 1353 int num_bits; 1354 stbi__uint32 code_buffer; 1355 1356 ubyte *zout; 1357 ubyte *zout_start; 1358 ubyte *zout_end; 1359 int z_expandable; 1360 1361 stbi__zhuffman z_length, z_distance; 1362 } 1363 1364 int stbi__zeof(stbi__zbuf *z) 1365 { 1366 return (z.zbuffer >= z.zbuffer_end); 1367 } 1368 1369 stbi_uc stbi__zget8(stbi__zbuf *z) 1370 { 1371 return stbi__zeof(z) ? 0 : *z.zbuffer++; 1372 } 1373 1374 void stbi__fill_bits(stbi__zbuf *z) 1375 { 1376 do { 1377 if (z.code_buffer >= (1U << z.num_bits)) { 1378 z.zbuffer = z.zbuffer_end; /* treat this as EOF so we fail. */ 1379 return; 1380 } 1381 z.code_buffer |= cast(uint) stbi__zget8(z) << z.num_bits; 1382 z.num_bits += 8; 1383 } while (z.num_bits <= 24); 1384 } 1385 1386 uint stbi__zreceive(stbi__zbuf *z, int n) 1387 { 1388 uint k; 1389 if (z.num_bits < n) stbi__fill_bits(z); 1390 k = z.code_buffer & ((1 << n) - 1); 1391 z.code_buffer >>= n; 1392 z.num_bits -= n; 1393 return k; 1394 } 1395 1396 int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z) 1397 { 1398 int b,s,k; 1399 // not resolved by fast table, so compute it the slow way 1400 // use jpeg approach, which requires MSbits at top 1401 k = stbi__bit_reverse(a.code_buffer, 16); 1402 for (s=STBI__ZFAST_BITS+1; ; ++s) 1403 if (k < z.maxcode[s]) 1404 break; 1405 if (s >= 16) return -1; // invalid code! 1406 // code size is s, so: 1407 b = (k >> (16-s)) - z.firstcode[s] + z.firstsymbol[s]; 1408 if (b >= STBI__ZNSYMS) return -1; // some data was corrupt somewhere! 1409 if (z.size[b] != s) return -1; // was originally an assert, but report failure instead. 1410 a.code_buffer >>= s; 1411 a.num_bits -= s; 1412 return z.value[b]; 1413 } 1414 1415 int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z) 1416 { 1417 int b,s; 1418 if (a.num_bits < 16) { 1419 if (stbi__zeof(a)) { 1420 return -1; /* report error for unexpected end of data. */ 1421 } 1422 stbi__fill_bits(a); 1423 } 1424 b = z.fast[a.code_buffer & STBI__ZFAST_MASK]; 1425 if (b) { 1426 s = b >> 9; 1427 a.code_buffer >>= s; 1428 a.num_bits -= s; 1429 return b & 511; 1430 } 1431 return stbi__zhuffman_decode_slowpath(a, z); 1432 } 1433 1434 int stbi__zexpand(stbi__zbuf *z, ubyte *zout, int n) // need to make room for n bytes 1435 { 1436 ubyte *q; 1437 uint cur, limit, old_limit; 1438 z.zout = zout; 1439 if (!z.z_expandable) return 0; // stbi__err("output buffer limit","Corrupt PNG"); 1440 cur = cast(uint) (z.zout - z.zout_start); 1441 limit = old_limit = cast(uint) (z.zout_end - z.zout_start); 1442 if (uint.max - cur < cast(uint) n) return 0; //stbi__err("outofmem", "Out of memory"); 1443 while (cur + n > limit) { 1444 if(limit > uint.max / 2) return 0; //stbi__err("outofmem", "Out of memory"); 1445 limit *= 2; 1446 } 1447 q = cast(ubyte *) STBI_REALLOC_SIZED(z.zout_start, old_limit, limit); 1448 if (q == null) return 0; //stbi__err("outofmem", "Out of memory"); 1449 z.zout_start = q; 1450 z.zout = q + cur; 1451 z.zout_end = q + limit; 1452 return 1; 1453 } 1454 1455 static immutable int[31] stbi__zlength_base = [ 1456 3,4,5,6,7,8,9,10,11,13, 1457 15,17,19,23,27,31,35,43,51,59, 1458 67,83,99,115,131,163,195,227,258,0,0 ]; 1459 1460 static immutable int[31] stbi__zlength_extra= 1461 [ 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 ]; 1462 1463 static immutable int[32] stbi__zdist_base = [ 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, 1464 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0]; 1465 1466 static immutable int[32] stbi__zdist_extra = 1467 [ 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]; 1468 1469 int stbi__parse_huffman_block(stbi__zbuf *a) 1470 { 1471 ubyte *zout = a.zout; 1472 for(;;) { 1473 int z = stbi__zhuffman_decode(a, &a.z_length); 1474 if (z < 256) 1475 { 1476 if (z < 0) 1477 return 0; //stbi__err("bad huffman code","Corrupt PNG"); // error in huffman codes 1478 if (zout >= a.zout_end) { 1479 if (!stbi__zexpand(a, zout, 1)) return 0; 1480 zout = a.zout; 1481 } 1482 *zout++ = cast(char) z; 1483 } else { 1484 stbi_uc *p; 1485 int len,dist; 1486 if (z == 256) { 1487 a.zout = zout; 1488 return 1; 1489 } 1490 z -= 257; 1491 len = stbi__zlength_base[z]; 1492 if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]); 1493 z = stbi__zhuffman_decode(a, &a.z_distance); 1494 if (z < 0) return 0; //stbi__err("bad huffman code","Corrupt PNG"); 1495 dist = stbi__zdist_base[z]; 1496 if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]); 1497 if (zout - a.zout_start < dist) return 0; //stbi__err("bad dist","Corrupt PNG"); 1498 if (zout + len > a.zout_end) { 1499 if (!stbi__zexpand(a, zout, len)) return 0; 1500 zout = a.zout; 1501 } 1502 p = cast(stbi_uc *) (zout - dist); 1503 if (dist == 1) { // run of one byte; common in images. 1504 stbi_uc v = *p; 1505 if (len) { do *zout++ = v; while (--len); } 1506 } else { 1507 if (len) { do *zout++ = *p++; while (--len); } 1508 } 1509 } 1510 } 1511 } 1512 1513 int stbi__compute_huffman_codes(stbi__zbuf *a) 1514 { 1515 static immutable stbi_uc[19] length_dezigzag = [ 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 ]; 1516 stbi__zhuffman z_codelength; 1517 stbi_uc[286+32+137] lencodes;//padding for maximum single op 1518 stbi_uc[19] codelength_sizes; 1519 int i,n; 1520 1521 int hlit = stbi__zreceive(a,5) + 257; 1522 int hdist = stbi__zreceive(a,5) + 1; 1523 int hclen = stbi__zreceive(a,4) + 4; 1524 int ntot = hlit + hdist; 1525 1526 memset(codelength_sizes.ptr, 0, codelength_sizes.sizeof); 1527 for (i=0; i < hclen; ++i) { 1528 int s = stbi__zreceive(a,3); 1529 codelength_sizes[length_dezigzag[i]] = cast(stbi_uc) s; 1530 } 1531 if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes.ptr, 19)) return 0; 1532 1533 n = 0; 1534 while (n < ntot) { 1535 int c = stbi__zhuffman_decode(a, &z_codelength); 1536 if (c < 0 || c >= 19) return 0; //stbi__err("bad codelengths", "Corrupt PNG"); 1537 if (c < 16) 1538 lencodes[n++] = cast(stbi_uc) c; 1539 else { 1540 stbi_uc fill = 0; 1541 if (c == 16) { 1542 c = stbi__zreceive(a,2)+3; 1543 if (n == 0) return 0; //stbi__err("bad codelengths", "Corrupt PNG"); 1544 fill = lencodes[n-1]; 1545 } else if (c == 17) { 1546 c = stbi__zreceive(a,3)+3; 1547 } else if (c == 18) { 1548 c = stbi__zreceive(a,7)+11; 1549 } else { 1550 return 0; //stbi__err("bad codelengths", "Corrupt PNG"); 1551 } 1552 if (ntot - n < c) return 0; //stbi__err("bad codelengths", "Corrupt PNG"); 1553 memset(lencodes.ptr+n, fill, c); 1554 n += c; 1555 } 1556 } 1557 if (n != ntot) return 0; //stbi__err("bad codelengths","Corrupt PNG"); 1558 if (!stbi__zbuild_huffman(&a.z_length, lencodes.ptr, hlit)) return 0; 1559 if (!stbi__zbuild_huffman(&a.z_distance, lencodes.ptr+hlit, hdist)) return 0; 1560 return 1; 1561 } 1562 1563 int stbi__parse_uncompressed_block(stbi__zbuf *a) 1564 { 1565 stbi_uc[4] header; 1566 int len,nlen,k; 1567 if (a.num_bits & 7) 1568 stbi__zreceive(a, a.num_bits & 7); // discard 1569 // drain the bit-packed data into header 1570 k = 0; 1571 while (a.num_bits > 0) { 1572 header[k++] = cast(stbi_uc) (a.code_buffer & 255); // suppress MSVC run-time check 1573 a.code_buffer >>= 8; 1574 a.num_bits -= 8; 1575 } 1576 if (a.num_bits < 0) return 0; //stbi__err("zlib corrupt","Corrupt PNG"); 1577 // now fill header the normal way 1578 while (k < 4) 1579 header[k++] = stbi__zget8(a); 1580 len = header[1] * 256 + header[0]; 1581 nlen = header[3] * 256 + header[2]; 1582 if (nlen != (len ^ 0xffff)) return 0; //stbi__err("zlib corrupt","Corrupt PNG"); 1583 if (a.zbuffer + len > a.zbuffer_end) return 0; //stbi__err("read past buffer","Corrupt PNG"); 1584 if (a.zout + len > a.zout_end) 1585 if (!stbi__zexpand(a, a.zout, len)) return 0; 1586 memcpy(a.zout, a.zbuffer, len); 1587 a.zbuffer += len; 1588 a.zout += len; 1589 return 1; 1590 } 1591 1592 int stbi__parse_zlib_header(stbi__zbuf *a) 1593 { 1594 int cmf = stbi__zget8(a); 1595 int cm = cmf & 15; 1596 /* int cinfo = cmf >> 4; */ 1597 int flg = stbi__zget8(a); 1598 if (stbi__zeof(a)) return 0; //stbi__err("bad zlib header","Corrupt PNG"); // zlib spec 1599 if ((cmf*256+flg) % 31 != 0) return 0; //stbi__err("bad zlib header","Corrupt PNG"); // zlib spec 1600 if (flg & 32) return 0; //stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png 1601 if (cm != 8) return 0; //stbi__err("bad compression","Corrupt PNG"); // DEFLATE required for png 1602 // window = 1 << (8 + cinfo)... but who cares, we fully buffer output 1603 return 1; 1604 } 1605 1606 static immutable stbi_uc[STBI__ZNSYMS] stbi__zdefault_length = 1607 [ 1608 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, 1609 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, 1610 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, 1611 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, 1612 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, 1613 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, 1614 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, 1615 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, 1616 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 1617 ]; 1618 static immutable stbi_uc[32] stbi__zdefault_distance = 1619 [ 1620 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 1621 ]; 1622 /* 1623 Init algorithm: 1624 { 1625 int i; // use <= to match clearly with spec 1626 for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8; 1627 for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9; 1628 for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7; 1629 for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8; 1630 1631 for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5; 1632 } 1633 */ 1634 1635 int stbi__parse_zlib(stbi__zbuf *a, int parse_header) 1636 { 1637 int final_, type; 1638 if (parse_header) 1639 if (!stbi__parse_zlib_header(a)) return 0; 1640 a.num_bits = 0; 1641 a.code_buffer = 0; 1642 do { 1643 final_ = stbi__zreceive(a,1); 1644 type = stbi__zreceive(a,2); 1645 if (type == 0) { 1646 if (!stbi__parse_uncompressed_block(a)) return 0; 1647 } else if (type == 3) { 1648 return 0; 1649 } else { 1650 if (type == 1) { 1651 // use fixed code lengths 1652 if (!stbi__zbuild_huffman(&a.z_length , stbi__zdefault_length.ptr , STBI__ZNSYMS)) return 0; 1653 if (!stbi__zbuild_huffman(&a.z_distance, stbi__zdefault_distance.ptr, 32)) return 0; 1654 } else { 1655 if (!stbi__compute_huffman_codes(a)) return 0; 1656 } 1657 if (!stbi__parse_huffman_block(a)) return 0; 1658 } 1659 } while (!final_); 1660 return 1; 1661 } 1662 1663 int stbi__do_zlib(stbi__zbuf *a, ubyte *obuf, int olen, int exp, int parse_header) 1664 { 1665 a.zout_start = obuf; 1666 a.zout = obuf; 1667 a.zout_end = obuf + olen; 1668 a.z_expandable = exp; 1669 1670 return stbi__parse_zlib(a, parse_header); 1671 } 1672 1673 ubyte *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen) 1674 { 1675 stbi__zbuf a; 1676 ubyte *p = cast(ubyte *) stbi__malloc(initial_size); 1677 if (p == null) return null; 1678 a.zbuffer = cast(stbi_uc *) buffer; 1679 a.zbuffer_end = cast(stbi_uc *) buffer + len; 1680 if (stbi__do_zlib(&a, p, initial_size, 1, 1)) { 1681 if (outlen) *outlen = cast(int) (a.zout - a.zout_start); 1682 return a.zout_start; 1683 } else { 1684 STBI_FREE(a.zout_start); 1685 return null; 1686 } 1687 } 1688 1689 ubyte *stbi_zlib_decode_malloc(const(char)*buffer, int len, int *outlen) 1690 { 1691 return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen); 1692 } 1693 1694 ubyte *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header) 1695 { 1696 stbi__zbuf a; 1697 ubyte *p = cast(ubyte *) stbi__malloc(initial_size); 1698 if (p == null) return null; 1699 a.zbuffer = cast(stbi_uc *) buffer; 1700 a.zbuffer_end = cast(stbi_uc *) buffer + len; 1701 if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) { 1702 if (outlen) *outlen = cast(int) (a.zout - a.zout_start); 1703 return a.zout_start; 1704 } else { 1705 STBI_FREE(a.zout_start); 1706 return null; 1707 } 1708 } 1709 1710 1711 // public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18 1712 // simple implementation 1713 // - only 8-bit samples 1714 // - no CRC checking 1715 // - allocates lots of intermediate memory 1716 // - avoids problem of streaming data between subsystems 1717 // - avoids explicit window management 1718 // performance 1719 // - uses stb_zlib, a PD zlib implementation with fast huffman decoding 1720 1721 version(decodePNG) 1722 { 1723 struct stbi__pngchunk 1724 { 1725 stbi__uint32 length; 1726 stbi__uint32 type; 1727 } 1728 1729 stbi__pngchunk stbi__get_chunk_header(stbi__context *s) 1730 { 1731 stbi__pngchunk c; 1732 c.length = stbi__get32be(s); 1733 c.type = stbi__get32be(s); 1734 return c; 1735 } 1736 1737 int stbi__check_png_header(stbi__context *s) 1738 { 1739 static immutable stbi_uc[8] png_sig = [ 137,80,78,71,13,10,26,10 ]; 1740 int i; 1741 for (i=0; i < 8; ++i) 1742 if (stbi__get8(s) != png_sig[i]) 1743 return 0; //stbi__err("bad png sig","Not a PNG"); 1744 return 1; 1745 } 1746 1747 struct stbi__png 1748 { 1749 stbi__context *s; 1750 stbi_uc* idata; 1751 stbi_uc* expanded; 1752 stbi_uc* out_; 1753 int depth; 1754 } 1755 1756 enum 1757 { 1758 STBI__F_none=0, 1759 STBI__F_sub=1, 1760 STBI__F_up=2, 1761 STBI__F_avg=3, 1762 STBI__F_paeth=4, 1763 // synthetic filters used for first scanline to avoid needing a dummy row of 0s 1764 STBI__F_avg_first, 1765 STBI__F_paeth_first 1766 } 1767 1768 static immutable stbi_uc[5] first_row_filter = 1769 [ 1770 STBI__F_none, 1771 STBI__F_sub, 1772 STBI__F_none, 1773 STBI__F_avg_first, 1774 STBI__F_paeth_first 1775 ]; 1776 1777 int stbi__paeth(int a, int b, int c) 1778 { 1779 int p = a + b - c; 1780 int pa = abs(p-a); 1781 int pb = abs(p-b); 1782 int pc = abs(p-c); 1783 if (pa <= pb && pa <= pc) 1784 return a; 1785 if (pb <= pc) 1786 return b; 1787 return c; 1788 } 1789 1790 static immutable stbi_uc[9] stbi__depth_scale_table = [ 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 ]; 1791 1792 // create the png data from post-deflated data 1793 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) 1794 { 1795 int bytes = (depth == 16? 2 : 1); 1796 stbi__context *s = a.s; 1797 stbi__uint32 i,j,stride = x*out_n*bytes; 1798 stbi__uint32 img_len, img_width_bytes; 1799 int k; 1800 int img_n = s.img_n; // copy it into a local for later 1801 1802 int output_bytes = out_n*bytes; 1803 int filter_bytes = img_n*bytes; 1804 int width = x; 1805 1806 assert(out_n == s.img_n || out_n == s.img_n+1); 1807 a.out_ = cast(stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into 1808 if (!a.out_) return 0; //stbi__err("outofmem", "Out of memory"); 1809 1810 if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return 0; //stbi__err("too large", "Corrupt PNG"); 1811 img_width_bytes = (((img_n * x * depth) + 7) >> 3); 1812 img_len = (img_width_bytes + 1) * y; 1813 1814 // we used to check for exact match between raw_len and img_len on non-interlaced PNGs, 1815 // but issue #276 reported a PNG in the wild that had extra data at the end (all zeros), 1816 // so just check for raw_len < img_len always. 1817 if (raw_len < img_len) return 0; //stbi__err("not enough pixels","Corrupt PNG"); 1818 1819 for (j=0; j < y; ++j) 1820 { 1821 stbi_uc *cur = a.out_ + stride*j; 1822 stbi_uc *prior; 1823 int filter = *raw++; 1824 1825 if (filter > 4) 1826 return 0; //stbi__err("invalid filter","Corrupt PNG"); 1827 1828 if (depth < 8) { 1829 if (img_width_bytes > x) return 0; //stbi__err("invalid width","Corrupt PNG"); 1830 cur += x*out_n - img_width_bytes; // store output to the rightmost img_len bytes, so we can decode in place 1831 filter_bytes = 1; 1832 width = img_width_bytes; 1833 } 1834 prior = cur - stride; // bugfix: need to compute this after 'cur +=' computation above 1835 1836 // if first row, use special filter that doesn't sample previous row 1837 if (j == 0) filter = first_row_filter[filter]; 1838 1839 // handle first byte explicitly 1840 for (k=0; k < filter_bytes; ++k) 1841 { 1842 switch (filter) { 1843 case STBI__F_none : cur[k] = raw[k]; break; 1844 case STBI__F_sub : cur[k] = raw[k]; break; 1845 case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break; 1846 case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); break; 1847 case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0)); break; 1848 case STBI__F_avg_first : cur[k] = raw[k]; break; 1849 case STBI__F_paeth_first: cur[k] = raw[k]; break; 1850 default: assert(false); 1851 } 1852 } 1853 1854 if (depth == 8) { 1855 if (img_n != out_n) 1856 cur[img_n] = 255; // first pixel 1857 raw += img_n; 1858 cur += out_n; 1859 prior += out_n; 1860 } else if (depth == 16) { 1861 if (img_n != out_n) { 1862 cur[filter_bytes] = 255; // first pixel top byte 1863 cur[filter_bytes+1] = 255; // first pixel bottom byte 1864 } 1865 raw += filter_bytes; 1866 cur += output_bytes; 1867 prior += output_bytes; 1868 } else { 1869 raw += 1; 1870 cur += 1; 1871 prior += 1; 1872 } 1873 1874 // this is a little gross, so that we don't switch per-pixel or per-component 1875 if (depth < 8 || img_n == out_n) { 1876 int nk = (width - 1)*filter_bytes; 1877 switch (filter) { 1878 // "none" filter turns into a memcpy here; make that explicit. 1879 case STBI__F_none: 1880 memcpy(cur, raw, nk); 1881 break; 1882 case STBI__F_sub: for (k=0; k < nk; ++k) { cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); } break; 1883 case STBI__F_up: for (k=0; k < nk; ++k) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break; 1884 case STBI__F_avg: for (k=0; k < nk; ++k) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); } break; 1885 case STBI__F_paeth: for (k=0; k < nk; ++k) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); } break; 1886 case STBI__F_avg_first: for (k=0; k < nk; ++k) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); } break; 1887 case STBI__F_paeth_first: for (k=0; k < nk; ++k) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); } break; 1888 default: assert(0); 1889 } 1890 raw += nk; 1891 } else { 1892 assert(img_n+1 == out_n); 1893 switch (filter) { 1894 case STBI__F_none: 1895 for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) 1896 for (k=0; k < filter_bytes; ++k) 1897 { cur[k] = raw[k]; } break; 1898 case STBI__F_sub: 1899 for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) 1900 for (k=0; k < filter_bytes; ++k) 1901 { cur[k] = STBI__BYTECAST(raw[k] + cur[k- output_bytes]); } break; 1902 case STBI__F_up: 1903 for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) 1904 for (k=0; k < filter_bytes; ++k) 1905 { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break; 1906 case STBI__F_avg: 1907 for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) 1908 for (k=0; k < filter_bytes; ++k) 1909 { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k- output_bytes])>>1)); } break; 1910 case STBI__F_paeth: 1911 for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) 1912 for (k=0; k < filter_bytes; ++k) 1913 { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],prior[k],prior[k- output_bytes])); } break; 1914 case STBI__F_avg_first: 1915 for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) 1916 for (k=0; k < filter_bytes; ++k) 1917 { cur[k] = STBI__BYTECAST(raw[k] + (cur[k- output_bytes] >> 1)); } break; 1918 case STBI__F_paeth_first: 1919 for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) 1920 for (k=0; k < filter_bytes; ++k) 1921 { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],0,0)); } break; 1922 default: assert(0); 1923 } 1924 1925 // the loop above sets the high byte of the pixels' alpha, but for 1926 // 16 bit png files we also need the low byte set. we'll do that here. 1927 if (depth == 16) { 1928 cur = a.out_ + stride*j; // start at the beginning of the row again 1929 for (i=0; i < x; ++i,cur+=output_bytes) { 1930 cur[filter_bytes+1] = 255; 1931 } 1932 } 1933 } 1934 } 1935 1936 // we make a separate pass to expand bits to pixels; for performance, 1937 // this could run two scanlines behind the above code, so it won't 1938 // intefere with filtering but will still be in the cache. 1939 if (depth < 8) { 1940 for (j=0; j < y; ++j) { 1941 stbi_uc *cur = a.out_ + stride*j; 1942 stbi_uc *in_ = a.out_ + stride*j + x*out_n - img_width_bytes; 1943 // unpack 1/2/4-bit into a 8-bit buffer. allows us to keep the common 8-bit path optimal at minimal cost for 1/2/4-bit 1944 // png guarante byte alignment, if width is not multiple of 8/4/2 we'll decode dummy trailing data that will be skipped in the later loop 1945 stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1; // scale grayscale values to 0..255 range 1946 1947 // note that the final byte might overshoot and write more data than desired. 1948 // we can allocate enough data that this never writes out of memory, but it 1949 // could also overwrite the next scanline. can it overwrite non-empty data 1950 // on the next scanline? yes, consider 1-pixel-wide scanlines with 1-bit-per-pixel. 1951 // so we need to explicitly clamp the final ones 1952 1953 if (depth == 4) { 1954 for (k=x*img_n; k >= 2; k-=2, ++in_) { 1955 *cur++ = cast(ubyte)(scale * ((*in_ >> 4)) ); 1956 *cur++ = cast(ubyte)(scale * ((*in_ ) & 0x0f)); 1957 } 1958 if (k > 0) *cur++ = cast(ubyte)(scale * ((*in_ >> 4) )); 1959 } else if (depth == 2) { 1960 for (k=x*img_n; k >= 4; k-=4, ++in_) { 1961 *cur++ = cast(ubyte)(scale * ((*in_ >> 6) )); 1962 *cur++ = cast(ubyte)(scale * ((*in_ >> 4) & 0x03)); 1963 *cur++ = cast(ubyte)(scale * ((*in_ >> 2) & 0x03)); 1964 *cur++ = cast(ubyte)(scale * ((*in_ ) & 0x03)); 1965 } 1966 if (k > 0) *cur++ = cast(ubyte)(scale * ((*in_ >> 6) )); 1967 if (k > 1) *cur++ = cast(ubyte)(scale * ((*in_ >> 4) & 0x03)); 1968 if (k > 2) *cur++ = cast(ubyte)(scale * ((*in_ >> 2) & 0x03)); 1969 } else if (depth == 1) { 1970 for (k=x*img_n; k >= 8; k-=8, ++in_) { 1971 *cur++ = cast(ubyte)(scale * ((*in_ >> 7) )); 1972 *cur++ = cast(ubyte)(scale * ((*in_ >> 6) & 0x01)); 1973 *cur++ = cast(ubyte)(scale * ((*in_ >> 5) & 0x01)); 1974 *cur++ = cast(ubyte)(scale * ((*in_ >> 4) & 0x01)); 1975 *cur++ = cast(ubyte)(scale * ((*in_ >> 3) & 0x01)); 1976 *cur++ = cast(ubyte)(scale * ((*in_ >> 2) & 0x01)); 1977 *cur++ = cast(ubyte)(scale * ((*in_ >> 1) & 0x01)); 1978 *cur++ = cast(ubyte)(scale * ((*in_ ) & 0x01)); 1979 } 1980 if (k > 0) *cur++ = cast(ubyte)(scale * ((*in_ >> 7) )); 1981 if (k > 1) *cur++ = cast(ubyte)(scale * ((*in_ >> 6) & 0x01)); 1982 if (k > 2) *cur++ = cast(ubyte)(scale * ((*in_ >> 5) & 0x01)); 1983 if (k > 3) *cur++ = cast(ubyte)(scale * ((*in_ >> 4) & 0x01)); 1984 if (k > 4) *cur++ = cast(ubyte)(scale * ((*in_ >> 3) & 0x01)); 1985 if (k > 5) *cur++ = cast(ubyte)(scale * ((*in_ >> 2) & 0x01)); 1986 if (k > 6) *cur++ = cast(ubyte)(scale * ((*in_ >> 1) & 0x01)); 1987 } 1988 if (img_n != out_n) { 1989 int q; 1990 // insert alpha = 255 1991 cur = a.out_ + stride*j; 1992 if (img_n == 1) { 1993 for (q=x-1; q >= 0; --q) { 1994 cur[q*2+1] = 255; 1995 cur[q*2+0] = cur[q]; 1996 } 1997 } else { 1998 assert(img_n == 3); 1999 for (q=x-1; q >= 0; --q) { 2000 cur[q*4+3] = 255; 2001 cur[q*4+2] = cur[q*3+2]; 2002 cur[q*4+1] = cur[q*3+1]; 2003 cur[q*4+0] = cur[q*3+0]; 2004 } 2005 } 2006 } 2007 } 2008 } else if (depth == 16) { 2009 // force the image data from big-endian to platform-native. 2010 // this is done in a separate pass due to the decoding relying 2011 // on the data being untouched, but could probably be done 2012 // per-line during decode if care is taken. 2013 stbi_uc *cur = a.out_; 2014 stbi__uint16 *cur16 = cast(stbi__uint16*)cur; 2015 2016 for(i=0; i < x*y*out_n; ++i,cur16++,cur+=2) { 2017 *cur16 = (cur[0] << 8) | cur[1]; 2018 } 2019 } 2020 2021 return 1; 2022 } 2023 2024 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) 2025 { 2026 int bytes = (depth == 16 ? 2 : 1); 2027 int out_bytes = out_n * bytes; 2028 stbi_uc *final_; 2029 int p; 2030 if (!interlaced) 2031 return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a.s.img_x, a.s.img_y, depth, color); 2032 2033 // de-interlacing 2034 final_ = cast(stbi_uc *) stbi__malloc_mad3(a.s.img_x, a.s.img_y, out_bytes, 0); 2035 if (!final_) return 0; //stbi__err("outofmem", "Out of memory"); 2036 for (p=0; p < 7; ++p) { 2037 static immutable int[7] xorig = [ 0,4,0,2,0,1,0 ]; 2038 static immutable int[7] yorig = [ 0,0,4,0,2,0,1 ]; 2039 static immutable int[7] xspc = [ 8,8,4,4,2,2,1 ]; 2040 static immutable int[7] yspc = [ 8,8,8,4,4,2,2 ]; 2041 int i,j,x,y; 2042 // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1 2043 x = (a.s.img_x - xorig[p] + xspc[p]-1) / xspc[p]; 2044 y = (a.s.img_y - yorig[p] + yspc[p]-1) / yspc[p]; 2045 if (x && y) { 2046 stbi__uint32 img_len = ((((a.s.img_n * x * depth) + 7) >> 3) + 1) * y; 2047 if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) { 2048 STBI_FREE(final_); 2049 return 0; 2050 } 2051 for (j=0; j < y; ++j) { 2052 for (i=0; i < x; ++i) { 2053 int out_y = j*yspc[p]+yorig[p]; 2054 int out_x = i*xspc[p]+xorig[p]; 2055 memcpy(final_ + out_y*a.s.img_x*out_bytes + out_x*out_bytes, 2056 a.out_ + (j*x+i)*out_bytes, out_bytes); 2057 } 2058 } 2059 STBI_FREE(a.out_); 2060 image_data += img_len; 2061 image_data_len -= img_len; 2062 } 2063 } 2064 a.out_ = final_; 2065 2066 return 1; 2067 } 2068 2069 int stbi__compute_transparency(stbi__png *z, stbi_uc* tc, int out_n) 2070 { 2071 stbi__context *s = z.s; 2072 stbi__uint32 i, pixel_count = s.img_x * s.img_y; 2073 stbi_uc *p = z.out_; 2074 2075 // compute color-based transparency, assuming we've 2076 // already got 255 as the alpha value in the output 2077 assert(out_n == 2 || out_n == 4); 2078 2079 if (out_n == 2) { 2080 for (i=0; i < pixel_count; ++i) { 2081 p[1] = (p[0] == tc[0] ? 0 : 255); 2082 p += 2; 2083 } 2084 } else { 2085 for (i=0; i < pixel_count; ++i) { 2086 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) 2087 p[3] = 0; 2088 p += 4; 2089 } 2090 } 2091 return 1; 2092 } 2093 2094 int stbi__compute_transparency16(stbi__png *z, stbi__uint16* tc, int out_n) 2095 { 2096 stbi__context *s = z.s; 2097 stbi__uint32 i, pixel_count = s.img_x * s.img_y; 2098 stbi__uint16 *p = cast(stbi__uint16*) z.out_; 2099 2100 // compute color-based transparency, assuming we've 2101 // already got 65535 as the alpha value in the output 2102 assert(out_n == 2 || out_n == 4); 2103 2104 if (out_n == 2) { 2105 for (i = 0; i < pixel_count; ++i) { 2106 p[1] = (p[0] == tc[0] ? 0 : 65535); 2107 p += 2; 2108 } 2109 } else { 2110 for (i = 0; i < pixel_count; ++i) { 2111 if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) 2112 p[3] = 0; 2113 p += 4; 2114 } 2115 } 2116 return 1; 2117 } 2118 2119 int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n) 2120 { 2121 stbi__uint32 i, pixel_count = a.s.img_x * a.s.img_y; 2122 stbi_uc* p, temp_out, orig = a.out_; 2123 2124 p = cast(stbi_uc *) stbi__malloc_mad2(pixel_count, pal_img_n, 0); 2125 if (p == null) return 0; //stbi__err("outofmem", "Out of memory"); 2126 2127 // between here and free(out) below, exitting would leak 2128 temp_out = p; 2129 2130 if (pal_img_n == 3) { 2131 for (i=0; i < pixel_count; ++i) { 2132 int n = orig[i]*4; 2133 p[0] = palette[n ]; 2134 p[1] = palette[n+1]; 2135 p[2] = palette[n+2]; 2136 p += 3; 2137 } 2138 } else { 2139 for (i=0; i < pixel_count; ++i) { 2140 int n = orig[i]*4; 2141 p[0] = palette[n ]; 2142 p[1] = palette[n+1]; 2143 p[2] = palette[n+2]; 2144 p[3] = palette[n+3]; 2145 p += 4; 2146 } 2147 } 2148 STBI_FREE(a.out_); 2149 a.out_ = temp_out; 2150 2151 return 1; 2152 } 2153 2154 enum stbi__unpremultiply_on_load = 1; 2155 2156 uint STBI__PNG_TYPE(char a, char b, char c, char d) 2157 { 2158 return ( (cast(uint)a) << 24 ) 2159 + ( (cast(uint)b) << 16 ) 2160 + ( (cast(uint)c) << 8 ) 2161 + ( (cast(uint)d) << 0 ); 2162 } 2163 2164 int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) 2165 { 2166 stbi_uc[1024] palette; 2167 stbi_uc pal_img_n=0; 2168 stbi_uc has_trans = 0; 2169 stbi_uc[3] tc = [0, 0, 0]; 2170 stbi__uint16[3] tc16; 2171 stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0; 2172 int first=1,k,interlace=0, color=0, is_iphone=0; 2173 stbi__context *s = z.s; 2174 2175 z.expanded = null; 2176 z.idata = null; 2177 z.out_ = null; 2178 2179 s.ppmX = -1; 2180 s.ppmY = -1; 2181 s.pixelAspectRatio = -1; 2182 2183 if (!stbi__check_png_header(s)) return 0; 2184 2185 if (scan == STBI__SCAN_type) return 1; 2186 2187 for (;;) { 2188 stbi__pngchunk c = stbi__get_chunk_header(s); 2189 uint aaaa = c.type; 2190 switch (c.type) { 2191 case STBI__PNG_TYPE('C','g','B','I'): 2192 is_iphone = 1; 2193 stbi__skip(s, c.length); 2194 break; 2195 2196 case STBI__PNG_TYPE('p','H','Y','s'): 2197 s.ppmX = stbi__get32be(s); 2198 s.ppmY = stbi__get32be(s); 2199 s.pixelAspectRatio = s.ppmX / s.ppmY; 2200 ubyte unit = stbi__get8(s); 2201 if (unit != 1) 2202 { 2203 s.ppmX = -1; // only contains an aspect ratio, but no physical resolution 2204 s.ppmY = -1; 2205 } 2206 break; 2207 2208 case STBI__PNG_TYPE('I','H','D','R'): { 2209 int comp,filter; 2210 if (!first) return 0; //stbi__err("multiple IHDR","Corrupt PNG"); 2211 first = 0; 2212 if (c.length != 13) return 0; //stbi__err("bad IHDR len","Corrupt PNG"); 2213 s.img_x = stbi__get32be(s); 2214 s.img_y = stbi__get32be(s); 2215 if (s.img_y > STBI_MAX_DIMENSIONS) return 0; //stbi__err("too large","Very large image (corrupt?)"); 2216 if (s.img_x > STBI_MAX_DIMENSIONS) return 0; //stbi__err("too large","Very large image (corrupt?)"); 2217 z.depth = stbi__get8(s); if (z.depth != 1 && z.depth != 2 && z.depth != 4 && z.depth != 8 && z.depth != 16) return 0; //stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only"); 2218 color = stbi__get8(s); if (color > 6) return 0; //stbi__err("bad ctype","Corrupt PNG"); 2219 if (color == 3 && z.depth == 16) return 0; //stbi__err("bad ctype","Corrupt PNG"); 2220 if (color == 3) pal_img_n = 3; else if (color & 1) return 0; //stbi__err("bad ctype","Corrupt PNG"); 2221 comp = stbi__get8(s); if (comp) return 0; //stbi__err("bad comp method","Corrupt PNG"); 2222 filter= stbi__get8(s); if (filter) return 0; //stbi__err("bad filter method","Corrupt PNG"); 2223 interlace = stbi__get8(s); if (interlace>1) return 0; //stbi__err("bad interlace method","Corrupt PNG"); 2224 if (!s.img_x || !s.img_y) return 0; //stbi__err("0-pixel image","Corrupt PNG"); 2225 if (!pal_img_n) { 2226 s.img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0); 2227 if ((1 << 30) / s.img_x / s.img_n < s.img_y) return 0; //stbi__err("too large", "Image too large to decode"); 2228 if (scan == STBI__SCAN_header) return 1; 2229 } else { 2230 // if paletted, then pal_n is our final components, and 2231 // img_n is # components to decompress/filter. 2232 s.img_n = 1; 2233 if ((1 << 30) / s.img_x / 4 < s.img_y) return 0; //stbi__err("too large","Corrupt PNG"); 2234 // if SCAN_header, have to scan to see if we have a tRNS 2235 } 2236 break; 2237 } 2238 2239 case STBI__PNG_TYPE('P','L','T','E'): { 2240 if (first) return 0; //stbi__err("first not IHDR", "Corrupt PNG"); 2241 if (c.length > 256*3) return 0; //stbi__err("invalid PLTE","Corrupt PNG"); 2242 pal_len = c.length / 3; 2243 if (pal_len * 3 != c.length) return 0; //stbi__err("invalid PLTE","Corrupt PNG"); 2244 for (i=0; i < pal_len; ++i) { 2245 palette[i*4+0] = stbi__get8(s); 2246 palette[i*4+1] = stbi__get8(s); 2247 palette[i*4+2] = stbi__get8(s); 2248 palette[i*4+3] = 255; 2249 } 2250 break; 2251 } 2252 2253 case STBI__PNG_TYPE('t','R','N','S'): { 2254 if (first) return 0; //stbi__err("first not IHDR", "Corrupt PNG"); 2255 if (z.idata) return 0; //stbi__err("tRNS after IDAT","Corrupt PNG"); 2256 if (pal_img_n) { 2257 if (scan == STBI__SCAN_header) { s.img_n = 4; return 1; } 2258 if (pal_len == 0) return 0; //stbi__err("tRNS before PLTE","Corrupt PNG"); 2259 if (c.length > pal_len) return 0; //stbi__err("bad tRNS len","Corrupt PNG"); 2260 pal_img_n = 4; 2261 for (i=0; i < c.length; ++i) 2262 palette[i*4+3] = stbi__get8(s); 2263 } else { 2264 if (!(s.img_n & 1)) return 0; //stbi__err("tRNS with alpha","Corrupt PNG"); 2265 if (c.length != cast(stbi__uint32) s.img_n*2) return 0; //stbi__err("bad tRNS len","Corrupt PNG"); 2266 has_trans = 1; 2267 if (z.depth == 16) { 2268 for (k = 0; k < s.img_n; ++k) tc16[k] = cast(stbi__uint16)stbi__get16be(s); // copy the values as-is 2269 } else { 2270 for (k = 0; k < s.img_n; ++k) 2271 { 2272 tc[k] = cast(ubyte)( cast(stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z.depth]); // non 8-bit images will be larger 2273 } 2274 } 2275 } 2276 break; 2277 } 2278 2279 case STBI__PNG_TYPE('I','D','A','T'): { 2280 if (first) 2281 { 2282 return 0; //stbi__err("first not IHDR", "Corrupt PNG"); 2283 } 2284 if (pal_img_n && !pal_len) 2285 { 2286 return 0; //stbi__err("no PLTE","Corrupt PNG"); 2287 } 2288 if (scan == STBI__SCAN_header) 2289 { 2290 s.img_n = pal_img_n; 2291 return 1; 2292 } 2293 if (cast(int)(ioff + c.length) < cast(int)ioff) 2294 { 2295 return 0; 2296 } 2297 if (ioff + c.length > idata_limit) { 2298 stbi__uint32 idata_limit_old = idata_limit; 2299 stbi_uc *p; 2300 if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096; 2301 while (ioff + c.length > idata_limit) 2302 idata_limit *= 2; 2303 p = cast(stbi_uc *) STBI_REALLOC_SIZED(z.idata, idata_limit_old, idata_limit); 2304 if (p == null) 2305 { 2306 return 0; //stbi__err("outofmem", "Out of memory"); 2307 } 2308 z.idata = p; 2309 } 2310 if (!stbi__getn(s, z.idata+ioff,c.length)) 2311 { 2312 return 0; //stbi__err("outofdata","Corrupt PNG"); 2313 } 2314 ioff += c.length; 2315 break; 2316 } 2317 2318 case STBI__PNG_TYPE('I','E','N','D'): { 2319 stbi__uint32 raw_len, bpl; 2320 if (first) return 0; //stbi__err("first not IHDR", "Corrupt PNG"); 2321 if (scan != STBI__SCAN_load) return 1; 2322 if (z.idata == null) 2323 { 2324 return 0; //stbi__err("no IDAT","Corrupt PNG"); 2325 } 2326 // initial guess for decoded data size to avoid unnecessary reallocs 2327 bpl = (s.img_x * z.depth + 7) / 8; // bytes per line, per component 2328 raw_len = bpl * s.img_y * s.img_n /* pixels */ + s.img_y /* filter mode per row */; 2329 z.expanded = cast(stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag(cast(char *) z.idata, ioff, raw_len, cast(int *) &raw_len, !is_iphone); 2330 if (z.expanded == null) 2331 { 2332 return 0; // zlib should set error 2333 } 2334 STBI_FREE(z.idata); z.idata = null; 2335 if ((req_comp == s.img_n+1 && req_comp != 3 && !pal_img_n) || has_trans) 2336 s.img_out_n = s.img_n+1; 2337 else 2338 s.img_out_n = s.img_n; 2339 if (!stbi__create_png_image(z, z.expanded, raw_len, s.img_out_n, z.depth, color, interlace)) 2340 { 2341 return 0; 2342 } 2343 if (has_trans) { 2344 if (z.depth == 16) { 2345 if (!stbi__compute_transparency16(z, tc16.ptr, s.img_out_n)) 2346 { 2347 return 0; 2348 } 2349 } else { 2350 if (!stbi__compute_transparency(z, tc.ptr, s.img_out_n)) 2351 { 2352 return 0; 2353 } 2354 } 2355 } 2356 2357 if (pal_img_n) { 2358 // pal_img_n == 3 or 4 2359 s.img_n = pal_img_n; // record the actual colors we had 2360 s.img_out_n = pal_img_n; 2361 if (req_comp >= 3) s.img_out_n = req_comp; 2362 if (!stbi__expand_png_palette(z, palette.ptr, pal_len, s.img_out_n)) 2363 { 2364 return 0; 2365 } 2366 } else if (has_trans) { 2367 // non-paletted image with tRNS . source image has (constant) alpha 2368 ++s.img_n; 2369 } 2370 STBI_FREE(z.expanded); z.expanded = null; 2371 // end of PNG chunk, read and skip CRC 2372 stbi__get32be(s); 2373 return 1; 2374 } 2375 2376 default: 2377 // if critical, fail 2378 if (first) 2379 { 2380 return 0; //stbi__err("first not IHDR", "Corrupt PNG"); 2381 } 2382 if ((c.type & (1 << 29)) == 0) 2383 { 2384 return 0; //stbi__err("invalid_chunk", "PNG not supported: unknown PNG chunk type"); 2385 } 2386 stbi__skip(s, c.length); 2387 break; 2388 } 2389 // end of PNG chunk, read and skip CRC 2390 stbi__get32be(s); 2391 } 2392 } 2393 2394 void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, stbi__result_info *ri) 2395 { 2396 void *result=null; 2397 if (req_comp < 0 || req_comp > 4) return null; //stbi__errpuc("bad req_comp", "Internal error"); 2398 if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) { 2399 if (p.depth <= 8) 2400 ri.bits_per_channel = 8; 2401 else if (p.depth == 16) 2402 ri.bits_per_channel = 16; 2403 else 2404 return null; //stbi__errpuc("bad bits_per_channel", "PNG not supported: unsupported color depth"); 2405 result = p.out_; 2406 p.out_ = null; 2407 if (req_comp && req_comp != p.s.img_out_n) { 2408 if (ri.bits_per_channel == 8) 2409 result = stbi__convert_format(cast(ubyte*) result, p.s.img_out_n, req_comp, p.s.img_x, p.s.img_y); 2410 else 2411 result = stbi__convert_format16(cast(stbi__uint16 *) result, p.s.img_out_n, req_comp, p.s.img_x, p.s.img_y); 2412 p.s.img_out_n = req_comp; 2413 if (result == null) return result; 2414 } 2415 *x = p.s.img_x; 2416 *y = p.s.img_y; 2417 if (n) *n = p.s.img_n; 2418 } 2419 STBI_FREE(p.out_); p.out_ = null; 2420 STBI_FREE(p.expanded); p.expanded = null; 2421 STBI_FREE(p.idata); p.idata = null; 2422 2423 return result; 2424 } 2425 2426 void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) 2427 { 2428 stbi__png p; 2429 p.s = s; 2430 return stbi__do_png(&p, x,y,comp,req_comp, ri); 2431 } 2432 2433 int stbi__png_test(stbi__context *s) 2434 { 2435 int r; 2436 r = stbi__check_png_header(s); 2437 stbi__rewind(s); 2438 return r; 2439 } 2440 2441 int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp) 2442 { 2443 if (!stbi__parse_png_file(p, STBI__SCAN_header, 0)) { 2444 stbi__rewind( p.s ); 2445 return 0; 2446 } 2447 if (x) *x = p.s.img_x; 2448 if (y) *y = p.s.img_y; 2449 if (comp) *comp = p.s.img_n; 2450 return 1; 2451 } 2452 2453 int stbi__png_info(stbi__context *s, int *x, int *y, int *comp) 2454 { 2455 stbi__png p; 2456 p.s = s; 2457 return stbi__png_info_raw(&p, x, y, comp); 2458 } 2459 2460 int stbi__png_is16(stbi__context *s) 2461 { 2462 stbi__png p; 2463 p.s = s; 2464 if (!stbi__png_info_raw(&p, null, null, null)) 2465 return 0; 2466 if (p.depth != 16) { 2467 stbi__rewind(p.s); 2468 return 0; 2469 } 2470 return 1; 2471 } 2472 2473 bool stbi__png_is16(stbi_io_callbacks* clbk, void* user) // #BONUS 2474 { 2475 stbi__context s; 2476 stbi__start_callbacks(&s, clbk, user); 2477 return stbi__png_is16(&s) != 0; 2478 } 2479 }