image.codec\codec\jpeg_progressive_preview.mbt
87.1% · 27 / 31 points · 155 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 渐进式 JPEG 逐扫描预览(0.1.33) - 3
/// - 4
/// progressive 编码的价值在于「先粗后细」:第 1 条扫描(DC 首扫)就能给出 - 5
/// 整幅可辨认的低频轮廓,后续扫描逐位补细节。此前的解码只输出终态, - 6
/// 本模块把「每条扫描解完即成片」的能力开出来,供渐进加载 / 预览场景使用。 - 7
- 8
///| - 9
/// progressive 解码上下文:系数缓冲与块网格参数(scan 之间共享) - 10
pub struct JpegProgCtx { - 11
coefs : Array[Array[Int32]] - 12
bw : Array[Int32] - 13
bh : Array[Int32] - 14
stride_x : Array[Int32] - 15
mcus_x : Int32 - 16
mcus_y : Int32 - 17
max_h : Int32 - 18
max_v : Int32 - 19
} - 20
- 21
///| - 22
/// 建上下文:校验分量数与采样因子,按 MCU 网格容量分配系数缓冲 - 23
fn jpeg_progressive_ctx( - 24
header : JpegHeader, - 25
) -> JpegProgCtx raise @types.LoadError { - 26●
let width = header.width - 27
let height = header.height - 28
let nc = header.num_components - 29
if nc != 1 && nc != 3 { - 30○
raise @types.LoadError::DecodeFailed( - 31
"JPEG progressive 仅支持 1 或 3 分量", - 32
) - 33
} - 34●
if header.scans.length() == 0 { - 35○
raise @types.LoadError::DecodeFailed("JPEG progressive 缺少扫描数据") - 36
} - 37
let mut max_h = 1 - 38
let mut max_v = 1 - 39
for i in 0..<nc { - 40●
if header.component_h[i] > max_h { - 41●
max_h = header.component_h[i] - 42
} - 43
if header.component_v[i] > max_v { - 44●
max_v = header.component_v[i] - 45
} - 46
} - 47
if max_h < 1 || max_v < 1 { - 48○
raise @types.LoadError::DecodeFailed("JPEG 采样因子无效") - 49
} - 50
let mcu_w = max_h * 8 - 51
let mcu_h = max_v * 8 - 52
let mcus_x = (width + mcu_w - 1) / mcu_w - 53
let mcus_y = (height + mcu_h - 1) / mcu_h - 54●
let bw : Array[Int32] = Array::make(4, 0) - 55●
let bh : Array[Int32] = Array::make(4, 0) - 56●
let stride_x : Array[Int32] = Array::make(4, 0) - 57
let coefs : Array[Array[Int32]] = [] - 58
for ci in 0..<nc { - 59
// 实际块网格(输出用):ceil(w*h_i/(8*max_h)) × ceil(h*v_i/(8*max_v)) - 60●
bw[ci] = (width * header.component_h[ci] + mcu_w - 1) / mcu_w - 61
bh[ci] = (height * header.component_v[ci] + mcu_h - 1) / mcu_h - 62
// 系数缓冲按 MCU 网格容量分配:交错扫描会把 padding 块也编进码流 - 63
stride_x[ci] = mcus_x * header.component_h[ci] - 64●
coefs.push( - 65●
Array::make(stride_x[ci] * mcus_y * header.component_v[ci] * 64, 0), - 66
) - 67
} - 68
JpegProgCtx::{ coefs, bw, bh, stride_x, mcus_x, mcus_y, max_h, max_v, } - 69
} - 70
- 71
///| - 72
/// 把当前累积到的系数成型为一帧(未到的系数位面按 0 处理,即"粗版") - 73
fn jpeg_progressive_render( - 74
header : JpegHeader, - 75
ctx : JpegProgCtx, - 76
) -> @types.Image { - 77●
jpeg_progressive_output( - 78
header, - 79
ctx.coefs, - 80
ctx.bw, - 81
ctx.bh, - 82
ctx.stride_x, - 83
ctx.max_h, - 84
ctx.max_v, - 85
) - 86
} - 87
- 88
///| - 89
/// 渐进式 JPEG 的一帧预览:扫描序号(1 起)与当时的图像 - 90
pub(all) struct JpegProgPreview { - 91
scan_index : Int32 - 92
image : @types.Image - 93
} - 94
- 95
///| - 96
/// 逐扫描解码 progressive JPEG,在每 `every_scans` 条扫描后产出一帧。 - 97
/// - 98
/// 参数 Parameters: - 99
/// - `data` - progressive JPEG(SOF2)字节流 - 100
/// - `every_scans` - 每解完多少条扫描出一帧;`< 1` 视为 1(最长每扫描一帧) - 101
/// - 102
/// 返回 Return: - 103
/// - 预览帧数组,按扫描进度递增排列;**末帧恒为完整解码结果** - 104
pub fn decode_jpeg_progressive_previews( - 105
data : Bytes, - 106
every_scans : Int32, - 107
) -> Array[JpegProgPreview] raise @types.LoadError { - 108●
let header = jpeg_parse_header(data) - 109
if !header.progressive { - 110●
raise @types.LoadError::DecodeFailed( - 111
"不是 progressive JPEG(SOF2),逐扫描预览不适用", - 112
) - 113
} - 114●
@types.check_dims(header.width, header.height, header.num_components) - 115●
let ctx = jpeg_progressive_ctx(header) - 116●
let every = if every_scans < 1 { 1 } else { every_scans } - 117●
let total = header.scans.length() - 118
let frames : Array[JpegProgPreview] = [] - 119
let mut idx = 0 - 120
for scan in header.scans { - 121●
jpeg_progressive_scan( - 122
header, - 123
data, - 124
scan, - 125
ctx.coefs, - 126
ctx.bw, - 127
ctx.bh, - 128
ctx.stride_x, - 129
ctx.mcus_x, - 130
ctx.mcus_y, - 131
) - 132
idx = idx + 1 - 133
if idx % every == 0 || idx == total { - 134●
frames.push(JpegProgPreview::{ - 135
scan_index: idx, - 136●
image: jpeg_progressive_render(header, ctx), - 137
}) - 138
} - 139
} - 140
frames - 141
} - 142
- 143
///| - 144
/// 按文件字节解的扫描总数与最终帧(不额外成型中间帧,等价 `decode_jpeg_pure`) - 145
pub fn jpeg_progressive_scan_count( - 146
data : Bytes, - 147
) -> Int32 raise @types.LoadError { - 148●
let header = jpeg_parse_header(data) - 149
if !header.progressive { - 150○
raise @types.LoadError::DecodeFailed( - 151
"不是 progressive JPEG(SOF2),逐扫描预览不适用", - 152
) - 153
} - 154●
header.scans.length() - 155
}