stb_image: JPEG: Improved detection of RGB images
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jeremy Sawicki · committed by Jeremy Sawicki · parent 6f6e11f · Browse files · 1 file·+50 added−5 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+50 added−5 removed
- other:+50 added−5 removed
Provenance
- Commit
9e76bb5- Tree
638e6ea- Author
Jeremy Sawicki <jeremysawicki>- Committer
Jeremy Sawicki <jeremysawicki>- Parent
6f6e11f
1 file changed+50 added−5 removed
modified:stb_image.h+50 added-5 removed
1689 unmodified lines1690 int spec_end;1691 int succ_high;1692 int succ_low;1693 int eob_run;1694+ int jfif;1695+ int app14;1696 int rgb;1697 1698 int scan_n, order[4];1699 int restart_interval, todo;1140 unmodified lines2840 L -= n;2841 }2842 return L==0;2843 }2844+2845 // check for comment block or APP blocks2846 if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {2844- stbi__skip(z->s, stbi__get16be(z->s)-2);2847+ L = stbi__get16be(z->s);2848+ if (L < 2) {2849+ if (m == 0xFE)2850+ return stbi__err("bad COM len","Corrupt JPEG");2851+ else2852+ return stbi__err("bad APP len","Corrupt JPEG");2853+ }2854+ L -= 2;2855+2856+ if (m == 0xE0 && L >= 5) { // JFIF APP0 segment2857+ static const unsigned char tag[5] = {'J','F','I','F','\0'};2858+ int ok = 1;2859+ int i;2860+ for (i=0; i < 5; ++i)2861+ if (stbi__get8(z->s) != tag[i])2862+ ok = 0;2863+ L -= 5;2864+ if (ok) {2865+ z->jfif = 1;2866+ }2867+ } else if (m == 0xEE && L >= 12) { // Adobe APP14 segment2868+ static const unsigned char tag[6] = {'A','d','o','b','e','\0'};2869+ int ok = 1;2870+ int i;2871+ for (i=0; i < 6; ++i)2872+ if (stbi__get8(z->s) != tag[i])2873+ ok = 0;2874+ L -= 6;2875+ if (ok) {2876+ stbi__get8(z->s); // version2877+ stbi__get16be(z->s); // flags02878+ stbi__get16be(z->s); // flags12879+ z->app14 = stbi__get8(z->s); // color transform2880+ L -= 6;2881+ }2882+ }2883+2884+ stbi__skip(z->s, L);2885 return 1;2886 }2887+2888 return stbi__err("unknown marker","Corrupt JPEG");2889 }2890 2891 // after we see SOS152 unmodified lines3044 3045 static int stbi__decode_jpeg_header(stbi__jpeg *z, int scan)3046 {3047 int m;3048+ z->jfif = 0;3049+ z->app14 = -1;3050 z->marker = STBI__MARKER_none; // initialize cached marker to empty3051 m = stbi__get_marker(z);3052 if (!stbi__SOI(m)) return stbi__err("no SOI","Corrupt JPEG");3053 if (scan == STBI__SCAN_type) return 1;463 unmodified lines3517 } stbi__resample;3518 3519 static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)3520 {3478- int n, decode_n;3521+ int n, decode_n, is_rgb;3522 z->s->img_n = 0; // make stbi__cleanup_jpeg safe3523 3524 // validate req_comp3525 if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");3 unmodified lines3529 3530 // determine actual number of components to generate3531 n = req_comp ? req_comp : z->s->img_n;3532 3533+ is_rgb = z->s->img_n == 3 && (z->rgb == 3 || (z->app14 == 0 && !z->jfif));3534+3490- if (z->s->img_n == 3 && n < 3 && z->rgb != 3)3535+ if (z->s->img_n == 3 && n < 3 && !is_rgb)3536 decode_n = 1;3537 else3538 decode_n = z->s->img_n;3539 51 unmodified lines3591 }3592 if (n >= 3) {3593 stbi_uc *y = coutput[0];3594 if (z->s->img_n == 3) {3550- if (z->rgb == 3) {3595+ if (is_rgb) {3596 for (i=0; i < z->s->img_x; ++i) {3597 out[0] = y[i];3598 out[1] = coutput[1][i];3599 out[2] = coutput[2][i];9 unmodified lines3609 out[3] = 255; // not used if n==33610 out += n;3611 }3612 } else {3568- if (z->rgb == 3) {3613+ if (is_rgb) {3614 if (n == 1)3615 for (i=0; i < z->s->img_x; ++i)3616 *out++ = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]);3617 else {3477 unmodified lines