Skip to content
Myr

stb_image: fix reading BMP with explicit masks

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 · parent d693c61 · Browse files · 1 file+10 added−4 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+10 added−4 removed

  • other:+10 added−4 removed

1 file+10 added−4 removed

modified:stb_image.h+10 added-4 removed
5119 unmodified lines
5120typedef struct5121{5122 int bpp, offset, hsz;5123 unsigned int mr,mg,mb,ma, all_a;5124 int extra_read;5125} stbi__bmp_data;51265127static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)5128{
4 unmodified lines
5133 stbi__get16le(s); // discard reserved5134 info->offset = stbi__get32le(s);5135 info->hsz = hsz = stbi__get32le(s);5136 info->mr = info->mg = info->mb = info->ma = 0;5137 info->extra_read = 14;51385139 if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown");5140 if (hsz == 12) {5141 s->img_x = stbi__get16le(s);
35 unmodified lines
5177 } else if (compress == 3) {5178 info->mr = stbi__get32le(s);5179 info->mg = stbi__get32le(s);5180 info->mb = stbi__get32le(s);5181 info->extra_read += 12;5182 // not documented, but generated by photoshop and handled by mspaint5183 if (info->mr == info->mg && info->mg == info->mb) {5184 // ?!?!?5185 return stbi__errpuc("bad BMP", "bad BMP");
48 unmodified lines
5234 all_a = info.all_a;52355236 if (info.hsz == 12) {5237 if (info.bpp < 24)5235 psize = (info.offset - 14 - 24) / 3;5238 psize = (info.offset - info.extra_read - 24) / 3;5239 } else {5240 if (info.bpp < 16)5238 psize = (info.offset - 14 - info.hsz) >> 2;5241 psize = (info.offset - info.extra_read - info.hsz) >> 2;5242 }5243 if (psize == 0) {5244 STBI_ASSERT(info.offset == (s->img_buffer - s->buffer_start));5245 }52465247 if (info.bpp == 24 && ma == 0xff000000)5248 s->img_n = 3;5249 else
18 unmodified lines
5268 pal[i][0] = stbi__get8(s);5269 if (info.hsz != 12) stbi__get8(s);5270 pal[i][3] = 255;5271 }5266 stbi__skip(s, info.offset - 14 - info.hsz - psize * (info.hsz == 12 ? 3 : 4));5272 stbi__skip(s, info.offset - info.extra_read - info.hsz - psize * (info.hsz == 12 ? 3 : 4));5273 if (info.bpp == 1) width = (s->img_x + 7) >> 3;5274 else if (info.bpp == 4) width = (s->img_x + 1) >> 1;5275 else if (info.bpp == 8) width = s->img_x;5276 else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); }
40 unmodified lines
5317 } else {5318 int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;5319 int z = 0;5320 int easy=0;5315 stbi__skip(s, info.offset - 14 - info.hsz);5321 stbi__skip(s, info.offset - info.extra_read - info.hsz);5322 if (info.bpp == 24) width = 3 * s->img_x;5323 else if (info.bpp == 16) width = 2*s->img_x;5324 else /* bpp = 32 and pad = 0 */ width=0;5325 pad = (-width) & 3;
2240 unmodified lines