decode one 64-entry block--
| 2346 | |
| 2347 | // decode one 64-entry block-- |
| 2348 | static int stbi__jpeg_decode_block( |
| 2349 | stbi__jpeg* j, short data[64], stbi__huffman* hdc, stbi__huffman* hac, |
| 2350 | stbi__int16* fac, int b, stbi__uint16* dequant) { |
| 2351 | int diff, dc, k; |
| 2352 | int t; |
| 2353 | |
| 2354 | if (j->code_bits < 16) |
| 2355 | stbi__grow_buffer_unsafe(j); |
| 2356 | t = stbi__jpeg_huff_decode(j, hdc); |
| 2357 | if (t < 0 || t > 15) |
| 2358 | return stbi__err("bad huffman code", "Corrupt JPEG"); |
| 2359 | |
| 2360 | // 0 all the ac values now so we can do it 32-bits at a time |
| 2361 | memset(data, 0, 64 * sizeof(data[0])); |
| 2362 | |
| 2363 | diff = t ? stbi__extend_receive(j, t) : 0; |
| 2364 | dc = j->img_comp[b].dc_pred + diff; |
| 2365 | j->img_comp[b].dc_pred = dc; |
| 2366 | data[0] = (short)(dc * dequant[0]); |
| 2367 | |
| 2368 | // decode AC components, see JPEG spec |
| 2369 | k = 1; |
| 2370 | do { |
| 2371 | unsigned int zig; |
| 2372 | int c, r, s; |
| 2373 | if (j->code_bits < 16) |
| 2374 | stbi__grow_buffer_unsafe(j); |
| 2375 | c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS) - 1); |
| 2376 | r = fac[c]; |
| 2377 | if (r) { // fast-AC path |
| 2378 | k += (r >> 4) & 15; // run |
| 2379 | s = r & 15; // combined length |
| 2380 | j->code_buffer <<= s; |
| 2381 | j->code_bits -= s; |
| 2382 | // decode into unzigzag'd location |
| 2383 | zig = stbi__jpeg_dezigzag[k++]; |
| 2384 | data[zig] = (short)((r >> 8) * dequant[zig]); |
| 2385 | } else { |
| 2386 | int rs = stbi__jpeg_huff_decode(j, hac); |
| 2387 | if (rs < 0) |
| 2388 | return stbi__err("bad huffman code", "Corrupt JPEG"); |
| 2389 | s = rs & 15; |
| 2390 | r = rs >> 4; |
| 2391 | if (s == 0) { |
| 2392 | if (rs != 0xf0) |
| 2393 | break; // end block |
| 2394 | k += 16; |
| 2395 | } else { |
| 2396 | k += r; |
| 2397 | // decode into unzigzag'd location |
| 2398 | zig = stbi__jpeg_dezigzag[k++]; |
| 2399 | data[zig] = (short)(stbi__extend_receive(j, s) * dequant[zig]); |
| 2400 | } |
| 2401 | } |
| 2402 | } while (k < 64); |
| 2403 | return 1; |
| 2404 | } |
| 2405 |
no test coverage detected