MCPcopy Create free account
hub / github.com/SFML/SFML / stbi__process_frame_header

Function stbi__process_frame_header

extlibs/headers/stb_image/stb_image.h:3264–3354  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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