Skip to content
Myr

Merge branch 'master' of https://github.com/ocornut/stb into working

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parents 0096551, 1be86b3 · Browse files · 1 file+97 added−40 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+97 added−40 removed

  • other:+97 added−40 removed

1 file+97 added−40 removed

modified:stb_image.h+97 added-40 removed
11 unmodified lines
12 Primarily of interest to game developers and other people who can13 avoid problematic images and only need the trivial interface1415 JPEG baseline (no JPEG progressive)16 PNG 8-bit-per-channel only16 PNG 1/2/4/8-bit-per-channel (16 bpc not supported)1718 TGA (not sure what subset, if a subset)19 BMP non-1bpp, non-RLE20 PSD (composited view only, no extra channels)
6 unmodified lines
27 - decode from arbitrary I/O callbacks28 - overridable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)2930 Latest revisions:31 1.xx (2014-09-26) 1/2/4-bit PNG support (both grayscale and paletted)32 1.46 (2014-08-26) fix broken tRNS chunk in non-paletted PNG33 1.45 (2014-08-16) workaround MSVC-ARM internal compiler error by wrapping malloc34 1.44 (2014-08-07) warnings35 1.43 (2014-07-15) fix MSVC-only bug in 1.42
27 unmodified lines
63 Jetro Lauha (stbi_info) Ryamond Barbiero64 James "moose2000" Brown (iPhone PNG) David Woo65 Ben "Disch" Wenger (io callbacks) Roy Eltham66 Martin "SpartanJ" Golini Luke Graham66 Thomas Ruf67 Omar Cornut (1/2/4-bit png) Thomas Ruf68 John Bartholomew69 Optimizations & bugfixes Ken Hamada70 Fabian "ryg" Giesen Cort Stratton71 Arseny Kapoulkine Blazej Dariusz Roszkowski
2415 unmodified lines
24872488#define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings24892490// create the png data from post-deflated data2490static 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)2491static 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)2492{2493 stbi__context *s = a->s;2494 stbi__uint32 i,j,stride = x*out_n;2495 stbi__uint32 img_len;2496 int k;2497 int img_n = s->img_n; // copy it into a local for later2498 stbi_uc* line8 = NULL; // point into raw when depth==8 else temporary local buffer24992500 STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1);2501 a->out = (stbi_uc *) stbi__malloc(x * y * out_n);2502 if (!a->out) return stbi__err("outofmem", "Out of memory");25032504 img_len = ((((img_n * x * depth) + 7) >> 3) + 1) * y;2505 if (s->img_x == x && s->img_y == y) {2500 if (raw_len != (img_n * x + 1) * y) return stbi__err("not enough pixels","Corrupt PNG");2506 if (raw_len != img_len) return stbi__err("not enough pixels","Corrupt PNG");2507 } else { // interlaced:2502 if (raw_len < (img_n * x + 1) * y) return stbi__err("not enough pixels","Corrupt PNG");2508 if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG");2509 }25102511 if (depth != 8) {2512 line8 = (stbi_uc *) stbi__malloc((x+7) * out_n); // allocate buffer for one scanline2513 if (!line8) return stbi__err("outofmem", "Out of memory");2514 }25152516 for (j=0; j < y; ++j) {2517 stbi_uc *in;2518 stbi_uc *cur = a->out + stride*j;2519 stbi_uc *prior = cur - stride;2520 int filter = *raw++;2521 if (filter > 4) {2522 if (depth != 8) free(line8);2508 if (filter > 4) return stbi__err("invalid filter","Corrupt PNG");2523 return stbi__err("invalid filter","Corrupt PNG");2524 }25252526 if (depth == 8) {2527 in = raw;2528 raw += x*img_n;2529 }2530 else {2531 // 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-bit2532 // 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 loop2533 in = line8;2534 stbi_uc* decode_out = line8;2535 stbi_uc scale = (color == 0) ? 0xFF/((1<<depth)-1) : 1; // scale grayscale values to 0..255 range2536 if (depth == 4) {2537 for (k=x*img_n; k >= 1; k-=2, raw++) {2538 *decode_out++ = scale * ((*raw >> 4) );2539 *decode_out++ = scale * ((*raw ) & 0x0f);2540 }2541 } else if (depth == 2) {2542 for (k=x*img_n; k >= 1; k-=4, raw++) {2543 *decode_out++ = scale * ((*raw >> 6) );2544 *decode_out++ = scale * ((*raw >> 4) & 0x03);2545 *decode_out++ = scale * ((*raw >> 2) & 0x03);2546 *decode_out++ = scale * ((*raw ) & 0x03);2547 }2548 } else if (depth == 1) {2549 for (k=x*img_n; k >= 1; k-=8, raw++) {2550 *decode_out++ = scale * ((*raw >> 7) );2551 *decode_out++ = scale * ((*raw >> 6) & 0x01);2552 *decode_out++ = scale * ((*raw >> 5) & 0x01);2553 *decode_out++ = scale * ((*raw >> 4) & 0x01);2554 *decode_out++ = scale * ((*raw >> 3) & 0x01);2555 *decode_out++ = scale * ((*raw >> 2) & 0x01);2556 *decode_out++ = scale * ((*raw >> 1) & 0x01);2557 *decode_out++ = scale * ((*raw ) & 0x01);2558 }2559 }2560 }25612562 // if first row, use special filter that doesn't sample previous row2563 if (j == 0) filter = first_row_filter[filter];25642565 // handle first pixel explicitly2566 for (k=0; k < img_n; ++k) {2567 switch (filter) {2514 case STBI__F_none : cur[k] = raw[k]; break;2515 case STBI__F_sub : cur[k] = raw[k]; break;2516 case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;2517 case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); break;2518 case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0)); break;2519 case STBI__F_avg_first : cur[k] = raw[k]; break;2520 case STBI__F_paeth_first: cur[k] = raw[k]; break;2568 case STBI__F_none : cur[k] = in[k]; break;2569 case STBI__F_sub : cur[k] = in[k]; break;2570 case STBI__F_up : cur[k] = STBI__BYTECAST(in[k] + prior[k]); break;2571 case STBI__F_avg : cur[k] = STBI__BYTECAST(in[k] + (prior[k]>>1)); break;2572 case STBI__F_paeth : cur[k] = STBI__BYTECAST(in[k] + stbi__paeth(0,prior[k],0)); break;2573 case STBI__F_avg_first : cur[k] = in[k]; break;2574 case STBI__F_paeth_first: cur[k] = in[k]; break;2575 }2576 }2577 if (img_n != out_n) cur[img_n] = 255;2524 raw += img_n;2578 in += img_n;2579 cur += out_n;2580 prior += out_n;2581 // this is a little gross, so that we don't switch per-pixel or per-component2582 if (img_n == out_n) {2583 #define CASE(f) \2584 case f: \2531 for (i=x-1; i >= 1; --i, raw+=img_n,cur+=img_n,prior+=img_n) \2585 for (i=x-1; i >= 1; --i, in+=img_n,cur+=img_n,prior+=img_n) \2586 for (k=0; k < img_n; ++k)2587 switch (filter) {2534 CASE(STBI__F_none) cur[k] = raw[k]; break;2535 CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-img_n]); break;2536 CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;2537 CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-img_n])>>1)); break;2538 CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;2539 CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-img_n] >> 1)); break;2540 CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-img_n],0,0)); break;2588 CASE(STBI__F_none) cur[k] = in[k]; break;2589 CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(in[k] + cur[k-img_n]); break;2590 CASE(STBI__F_up) cur[k] = STBI__BYTECAST(in[k] + prior[k]); break;2591 CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(in[k] + ((prior[k] + cur[k-img_n])>>1)); break;2592 CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(in[k] + stbi__paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;2593 CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(in[k] + (cur[k-img_n] >> 1)); break;2594 CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(in[k] + stbi__paeth(cur[k-img_n],0,0)); break;2595 }2596 #undef CASE2597 } else {2598 STBI_ASSERT(img_n+1 == out_n);2599 #define CASE(f) \2600 case f: \2547 for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \2601 for (i=x-1; i >= 1; --i, cur[img_n]=255,in+=img_n,cur+=out_n,prior+=out_n) \2602 for (k=0; k < img_n; ++k)2603 switch (filter) {2550 CASE(STBI__F_none) cur[k] = raw[k]; break;2551 CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-out_n]); break;2552 CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;2553 CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-out_n])>>1)); break;2554 CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;2555 CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-out_n] >> 1)); break;2556 CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],0,0)); break;2604 CASE(STBI__F_none) cur[k] = in[k]; break;2605 CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(in[k] + cur[k-out_n]); break;2606 CASE(STBI__F_up) cur[k] = STBI__BYTECAST(in[k] + prior[k]); break;2607 CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(in[k] + ((prior[k] + cur[k-out_n])>>1)); break;2608 CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(in[k] + stbi__paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;2609 CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(in[k] + (cur[k-out_n] >> 1)); break;2610 CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(in[k] + stbi__paeth(cur[k-out_n],0,0)); break;2611 }2612 #undef CASE2613 }2614 }26152616 if (depth != 8) free(line8);2617 return 1;2618}26192564static int stbi__create_png_image(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, int interlaced)2620static int stbi__create_png_image(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, int depth, int color, int interlaced)2621{2622 stbi_uc *final;2623 int p;2624 if (!interlaced)2569 return stbi__create_png_image_raw(a, raw, raw_len, out_n, a->s->img_x, a->s->img_y);2625 return stbi__create_png_image_raw(a, raw, raw_len, out_n, a->s->img_x, a->s->img_y, depth, color);26262627 // de-interlacing2628 final = (stbi_uc *) stbi__malloc(a->s->img_x * a->s->img_y * out_n);2629 for (p=0; p < 7; ++p) {
5 unmodified lines
2635 // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 12636 x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p];2637 y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];2638 if (x && y) {2639 stbi__uint32 img_len = ((((out_n * x * depth) + 7) >> 3) + 1) * y;2583 if (!stbi__create_png_image_raw(a, raw, raw_len, out_n, x, y)) {2640 if (!stbi__create_png_image_raw(a, raw, raw_len, out_n, x, y, depth, color)) {2641 free(final);2642 return 0;2643 }2644 for (j=0; j < y; ++j)2645 for (i=0; i < x; ++i)2646 memcpy(final + (j*yspc[p]+yorig[p])*a->s->img_x*out_n + (i*xspc[p]+xorig[p])*out_n,2647 a->out + (j*x+i)*out_n, out_n);2648 free(a->out);2592 raw += (x*out_n+1)*y;2593 raw_len -= (x*out_n+1)*y;2649 raw += img_len;2650 raw_len -= img_len;2651 }2652 }2653 a->out = final;2654
121 unmodified lines
2776{2777 stbi_uc palette[1024], pal_img_n=0;2778 stbi_uc has_trans=0, tc[3];2779 stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;2723 int first=1,k,interlace=0, is_iphone=0;2780 int first=1,k,interlace=0, color=0, depth=0, is_iphone=0;2781 stbi__context *s = z->s;27822783 z->expanded = NULL;2784 z->idata = NULL;
10 unmodified lines
2795 is_iphone = 1;2796 stbi__skip(s, c.length);2797 break;2798 case PNG_TYPE('I','H','D','R'): {2742 int depth,color,comp,filter;2799 int comp,filter;2800 if (!first) return stbi__err("multiple IHDR","Corrupt PNG");2801 first = 0;2802 if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG");2803 s->img_x = stbi__get32be(s); if (s->img_x > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");2804 s->img_y = stbi__get32be(s); if (s->img_y > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");2748 depth = stbi__get8(s); if (depth != 8) return stbi__err("8bit only","PNG not supported: 8-bit only");2805 depth = stbi__get8(s); if (depth != 1 && depth != 2 && depth != 4 && depth != 8) return stbi__err("1/2/4/8-bit only","PNG not supported: 1/2/4/8-bit only");2806 color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG");2807 if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG");2808 comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG");2809 filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG");
75 unmodified lines
2885 if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)2886 s->img_out_n = s->img_n+1;2887 else2888 s->img_out_n = s->img_n;2832 if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace)) return 0;2889 if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, depth, color, interlace)) return 0;2890 if (has_trans)2891 if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0;2892 if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)2893 stbi__de_iphone(z);
1732 unmodified lines
4626 fix broken tRNS chunk (colorkey-style transparency) in non-paletted PNG4627 1.45 (2014-08-16)4628 fix MSVC-ARM internal compiler error by wrapping malloc4629 1.44 (2014-08-07)4573 various warning fixes from Ronny Chevalier4630 various warning fixes from Ronny Chevalier4631 1.43 (2014-07-15)4632 fix MSVC-only compiler problem in code changed in 1.424633 1.42 (2014-07-09)4634 don't define _CRT_SECURE_NO_WARNINGS (affects user code)
110 unmodified lines