MCPcopy Create free account
hub / github.com/OpenFodder/openfodder / stbi__process_frame_header

Function stbi__process_frame_header

Source/Utils/stb_image.h:3263–3353  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3261}
3262
3263static int stbi__process_frame_header(stbi__jpeg *z, int scan)
3264{
3265 stbi__context *s = z->s;
3266 int Lf,p,i,q, h_max=1,v_max=1,c;
3267 Lf = stbi__get16be(s); if (Lf < 11) return stbi__err("bad SOF len","Corrupt JPEG"); // JPEG
3268 p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
3269 s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG
3270 s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires
3271 if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
3272 if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)");
3273 c = stbi__get8(s);
3274 if (c != 3 && c != 1 && c != 4) return stbi__err("bad component count","Corrupt JPEG");
3275 s->img_n = c;
3276 for (i=0; i < c; ++i) {
3277 z->img_comp[i].data = NULL;
3278 z->img_comp[i].linebuf = NULL;
3279 }
3280
3281 if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len","Corrupt JPEG");
3282
3283 z->rgb = 0;
3284 for (i=0; i < s->img_n; ++i) {
3285 static const unsigned char rgb[3] = { 'R', 'G', 'B' };
3286 z->img_comp[i].id = stbi__get8(s);
3287 if (s->img_n == 3 && z->img_comp[i].id == rgb[i])
3288 ++z->rgb;
3289 q = stbi__get8(s);
3290 z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return stbi__err("bad H","Corrupt JPEG");
3291 z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return stbi__err("bad V","Corrupt JPEG");
3292 z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ","Corrupt JPEG");
3293 }
3294
3295 if (scan != STBI__SCAN_load) return 1;
3296
3297 if (!stbi__mad3sizes_valid(s->img_x, s->img_y, s->img_n, 0)) return stbi__err("too large", "Image too large to decode");
3298
3299 for (i=0; i < s->img_n; ++i) {
3300 if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;
3301 if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
3302 }
3303
3304 // check that plane subsampling factors are integer ratios; our resamplers can't deal with fractional ratios
3305 // and I've never seen a non-corrupted JPEG file actually use them
3306 for (i=0; i < s->img_n; ++i) {
3307 if (h_max % z->img_comp[i].h != 0) return stbi__err("bad H","Corrupt JPEG");
3308 if (v_max % z->img_comp[i].v != 0) return stbi__err("bad V","Corrupt JPEG");
3309 }
3310
3311 // compute interleaved mcu info
3312 z->img_h_max = h_max;
3313 z->img_v_max = v_max;
3314 z->img_mcu_w = h_max * 8;
3315 z->img_mcu_h = v_max * 8;
3316 // these sizes can't be more than 17 bits
3317 z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
3318 z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
3319
3320 for (i=0; i < s->img_n; ++i) {

Callers 1

stbi__decode_jpeg_headerFunction · 0.85

Calls 7

stbi__get16beFunction · 0.85
stbi__errFunction · 0.85
stbi__get8Function · 0.85
stbi__mad3sizes_validFunction · 0.85
stbi__malloc_mad2Function · 0.85
stbi__malloc_mad3Function · 0.85

Tested by

no test coverage detected