code wiki / (root) / nx_jpeg_dequant.nx

nx_jpeg_dequant.nx source

↩ module page · 51 lines · 2196 B

1// nx_jpeg_dequant.nx -- dequantization + un-zigzag composer. 2// Per ITU-T Rec. T.81 sec A.3.4 / F.2.1.4. 3// 4// JPEG stores quantized DCT coefficients in zig-zag scan order so 5// the entropy-coded RLE clusters the typically-many trailing zeros. 6// To recover natural-order coefficients for the IDCT: 7// 8// 1. Dequantize: natural-magnitude_zz[k] = q_zz[k] * quant_zz[k] 9// (element-wise multiply in zig-zag order; legal because both 10// arrays are in the same order) 11// 2. Un-zigzag: natural[un_zigzag[k]] = natural-magnitude_zz[k] 12// 13// Composed in a single pass for efficiency: walk k = 0..63 once, 14// computing the natural position from the un_zigzag table and 15// writing the dequantized value directly to natural[un_zigzag[k]]. 16// 17// nx_safety_envelope: 18// intended_use: "Dequant + un-zigzag for JPEG decode pipeline." 19// sil_target: SIL1 20// evidence: [t81_section_a3_4_canonical_basis, 21// composes_zigzag_primitive, 22// composes_dqt_primitive] 23// hazard_register: [bug-tape-dequant-zero-divisor-not-applicable, 24// bug-tape-zigzag-permutation-table-corruption] 25// residual_risk: "Caller responsible for ensuring quant table 26// has 64 entries and is the table-id specified 27// by the SOF0 component descriptor." 28// verdict: NOT_YET_EVALUATED 29 30import "nx_syscalls.nx" 31import "nx_jpeg_zigzag.nx" 32 33// Dequantize + un-zigzag in a single pass. 34// 35// zz_coeffs -- 64 i64 entries, zig-zag order (from entropy decoder) 36// quant_zz -- 64 i64 entries, zig-zag order (from DQT parser) 37// natural_block -- 64 i64 entries, natural order (output) 38// 39// natural_block[un_zigzag[k]] = zz_coeffs[k] * quant_zz[k] for k = 0..63. 40func nx_jpeg_dequant_un_zigzag(zz_coeffs: *i64, 41 quant_zz: *i64, 42 natural_block: *i64) -> i64 { 43 let table: *i64 = sys_mmap(64 * 8) as *i64 44 nx_jpeg_zigzag_inverse_table(table) 45 var k: i64 = 0 46 while k < 64 { 47 natural_block[table[k]] = zz_coeffs[k] * quant_zz[k] 48 k = k + 1 49 } 50 return 0 51}