Changes in stb lib to avoid returning alpha bitmaps when no pixel has alpha

This commit is contained in:
Bertrand Songis
2016-02-29 18:41:35 +01:00
parent d28c71db86
commit 301cd0538b
+16 -7
View File
@@ -4045,6 +4045,7 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r
a->out = (stbi_uc *) stbi__malloc(x * y * out_n); // extra bytes to write off the end into a->out = (stbi_uc *) stbi__malloc(x * y * out_n); // extra bytes to write off the end into
if (!a->out) return stbi__err("outofmem", "Out of memory"); if (!a->out) return stbi__err("outofmem", "Out of memory");
s->img_out_n = 3;
img_width_bytes = (((img_n * x * depth) + 7) >> 3); img_width_bytes = (((img_n * x * depth) + 7) >> 3);
img_len = (img_width_bytes + 1) * y; img_len = (img_width_bytes + 1) * y;
if (s->img_x == x && s->img_y == y) { if (s->img_x == x && s->img_y == y) {
@@ -4106,12 +4107,12 @@ static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 r
switch (filter) { switch (filter) {
// "none" filter turns into a memcpy here; make that explicit. // "none" filter turns into a memcpy here; make that explicit.
case STBI__F_none: memcpy(cur, raw, nk); break; case STBI__F_none: memcpy(cur, raw, nk); break;
CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); break; CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); if ((k&0x03) == 0x03 && cur[k]!=255) s->img_out_n = 4; } break;
CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break; CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); if ((k&0x03) == 0x03 && cur[k]!=255) s->img_out_n = 4; } break;
CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); break; CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); if ((k&0x03) == 0x03 && cur[k]!=255) s->img_out_n = 4; } break;
CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); break; CASE(STBI__F_paeth) {cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); if ((k&0x03) == 0x03 && cur[k]!=255) s->img_out_n = 4; } break;
CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); break; CASE(STBI__F_avg_first) {cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); if ((k&0x03) == 0x03 && cur[k]!=255) s->img_out_n = 4; } break;
CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); break; CASE(STBI__F_paeth_first) {cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); if ((k&0x03) == 0x03 && cur[k]!=255) s->img_out_n = 4; } break;
} }
#undef CASE #undef CASE
raw += nk; raw += nk;
@@ -4538,11 +4539,13 @@ static unsigned char *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req
if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) { if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) {
result = p->out; result = p->out;
p->out = NULL; p->out = NULL;
#if 0
if (req_comp && req_comp != p->s->img_out_n) { if (req_comp && req_comp != p->s->img_out_n) {
result = stbi__convert_format(result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y); result = stbi__convert_format(result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
p->s->img_out_n = req_comp; p->s->img_out_n = req_comp;
if (result == NULL) return result; if (result == NULL) return result;
} }
#endif
*x = p->s->img_x; *x = p->s->img_x;
*y = p->s->img_y; *y = p->s->img_y;
if (n) *n = p->s->img_out_n; if (n) *n = p->s->img_out_n;
@@ -4785,6 +4788,7 @@ static stbi_uc *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int
out = (stbi_uc *) stbi__malloc(target * s->img_x * s->img_y); out = (stbi_uc *) stbi__malloc(target * s->img_x * s->img_y);
if (!out) return stbi__errpuc("outofmem", "Out of memory"); if (!out) return stbi__errpuc("outofmem", "Out of memory");
if (info.bpp < 16) { if (info.bpp < 16) {
s->img_n = 3;
int z=0; int z=0;
if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); } if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); }
for (i=0; i < psize; ++i) { for (i=0; i < psize; ++i) {
@@ -4856,6 +4860,7 @@ static stbi_uc *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int
} }
} else { } else {
int bpp = info.bpp; int bpp = info.bpp;
s->img_n = 3;
for (i=0; i < (int) s->img_x; ++i) { for (i=0; i < (int) s->img_x; ++i) {
stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s)); stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s));
int a; int a;
@@ -4864,7 +4869,11 @@ static stbi_uc *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int
out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount)); out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount));
a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255); a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255);
all_a |= a; all_a |= a;
if (target == 4) out[z++] = STBI__BYTECAST(a); if (target == 4) {
uint8_t alpha = STBI__BYTECAST(a);
if (alpha != 255) s->img_n = 4;
out[z++] = alpha;
}
} }
} }
stbi__skip(s, pad); stbi__skip(s, pad);