image.codec\codec\jpeg_progressive_script.mbt
95.9% · 283 / 295 points · 747 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # progressive JPEG · 扫描脚本可配置(`encode_jpeg_progressive_420_scripted`) - 3
/// - 4
/// 把"固定 8 条扫描"升级成**按脚本驱动**:每条扫描给出分量集、`Ss/Se`(spectral selection) - 5
/// 与 `Ah/Al`(successive approximation),逐条写自己的 DHT + SOS。 - 6
/// - 7
/// 默认脚本 `jpeg_progressive_script_libjpeg()` 复刻 libjpeg `jpeg_simple_progression()` - 8
/// 对 YCbCr 的 10 条扫描:DC 首扫 → Y `1–5`(Al=2) → Cr/Cb 全带(Al=1) → Y `6–63`(Al=2) → - 9
/// DC 细化 → Y 全带 `Ah=2 Al=1` → Cr/Cb 全带细化 → Y 全带细化(亮度最低位最后发)。 - 10
/// - 11
/// 与 4:2:0 固定 8 扫描版共用同一套系数平面(Y `stride_y`、Cb/Cr `stride_c`)与 - 12
/// `prog420_pixel_ycbcr` / `jpeg_progressive_quantize_block`;差别只在扫描的组织方式, - 13
/// 顺带把 AC 表改成**逐扫描按频次重建**(libjpeg `optimize_coding` 同口径)。 - 14
- 15
///| - 16
/// 一条 progressive 扫描(字段与 libjpeg `jpeg_scan_info` 对齐)。 - 17
/// `comps` 用本库的分量序号:0 = Y、1 = Cb、2 = Cr。 - 18
pub(all) struct JpegProgScan { - 19
comps : Array[Int32] - 20
ss : Int32 - 21
se : Int32 - 22
ah : Int32 - 23
al : Int32 - 24
} - 25
- 26
///| - 27
/// libjpeg `jpeg_simple_progression()` 的 YCbCr 默认 10 条扫描。 - 28
pub fn jpeg_progressive_script_libjpeg() -> Array[JpegProgScan] { - 29●
[ - 30
JpegProgScan::{ comps: [0, 1, 2], ss: 0, se: 0, ah: 0, al: 1, }, - 31
JpegProgScan::{ comps: [0], ss: 1, se: 5, ah: 0, al: 2, }, - 32
JpegProgScan::{ comps: [2], ss: 1, se: 63, ah: 0, al: 1, }, - 33
JpegProgScan::{ comps: [1], ss: 1, se: 63, ah: 0, al: 1, }, - 34
JpegProgScan::{ comps: [0], ss: 6, se: 63, ah: 0, al: 2, }, - 35
JpegProgScan::{ comps: [0, 1, 2], ss: 0, se: 0, ah: 1, al: 0, }, - 36
JpegProgScan::{ comps: [0], ss: 1, se: 63, ah: 2, al: 1, }, - 37
JpegProgScan::{ comps: [2], ss: 1, se: 63, ah: 1, al: 0, }, - 38
JpegProgScan::{ comps: [1], ss: 1, se: 63, ah: 1, al: 0, }, - 39
JpegProgScan::{ comps: [0], ss: 1, se: 63, ah: 1, al: 0, }, - 40
] - 41
} - 42
- 43
///| - 44
/// 0.1.30 固定 8 条扫描等价脚本(DC 首扫 Al=1 → 3×AC 首扫 Al=1 → DC 细化 → 3×AC 细化)。 - 45
pub fn jpeg_progressive_script_eight() -> Array[JpegProgScan] { - 46●
[ - 47
JpegProgScan::{ comps: [0, 1, 2], ss: 0, se: 0, ah: 0, al: 1, }, - 48
JpegProgScan::{ comps: [0], ss: 1, se: 63, ah: 0, al: 1, }, - 49
JpegProgScan::{ comps: [1], ss: 1, se: 63, ah: 0, al: 1, }, - 50
JpegProgScan::{ comps: [2], ss: 1, se: 63, ah: 0, al: 1, }, - 51
JpegProgScan::{ comps: [0, 1, 2], ss: 0, se: 0, ah: 1, al: 0, }, - 52
JpegProgScan::{ comps: [0], ss: 1, se: 63, ah: 1, al: 0, }, - 53
JpegProgScan::{ comps: [2], ss: 1, se: 63, ah: 1, al: 0, }, - 54
JpegProgScan::{ comps: [1], ss: 1, se: 63, ah: 1, al: 0, }, - 55
] - 56
} - 57
- 58
///| - 59
/// 发挂起的 EOBRUN(不带修正位缓冲;AC 首扫用)。 - 60
fn prog_emit_eobrun( - 61
w : JpegBitWriter, - 62
enc : JpegHuffmanEncoder, - 63
freq : Array[Int32], - 64
eobrun : Int32, - 65
) -> Unit { - 66●
if eobrun > 0 { - 67●
let nbits = jpeg_category(eobrun) - 1 - 68●
jpeg_prog_emit_sym(w, enc, freq, nbits << 4) - 69
if nbits > 0 { - 70●
jpeg_prog_emit_value(w, freq, eobrun - (1 << nbits), nbits) - 71
} - 72
} - 73
} - 74
- 75
///| - 76
/// 一条 AC 首扫(单分量平面,band `Ss..Se`,点变换 `Al`)。`freq` 非空时只计频。 - 77
fn prog_ac_first_scan( - 78
w : JpegBitWriter, - 79
enc : JpegHuffmanEncoder, - 80
freq : Array[Int32], - 81
plane : Array[Int32], - 82
stride : Int32, - 83
bx_max : Int32, - 84
by_max : Int32, - 85
ss : Int32, - 86
se : Int32, - 87
al : Int32, - 88
) -> Unit { - 89●
let mut eobrun = 0 - 90
for by in 0..<by_max { - 91●
for bx in 0..<bx_max { - 92●
let base = (by * stride + bx) * 64 - 93
let mut has_nz = false - 94
let mut k = ss - 95
while k <= se { - 96●
if jpeg_prog_shift(plane[base + k], al) != 0 { - 97●
has_nz = true - 98
k = se - 99
} - 100
k = k + 1 - 101
} - 102
if !has_nz { - 103●
eobrun += 1 - 104
if eobrun == PROGRESSIVE_MAX_EOBRUN { - 105○
prog_emit_eobrun(w, enc, freq, eobrun) - 106
eobrun = 0 - 107
} - 108
continue - 109
} - 110
if eobrun > 0 { - 111●
prog_emit_eobrun(w, enc, freq, eobrun) - 112
eobrun = 0 - 113
} - 114
let mut run = 0 - 115
k = ss - 116
while k <= se { - 117●
let c = jpeg_prog_shift(plane[base + k], al) - 118
if c == 0 { - 119●
run += 1 - 120
} else { - 121●
while run > 15 { - 122●
jpeg_prog_emit_sym(w, enc, freq, 0xF0) - 123
run -= 16 - 124
} - 125●
let size = jpeg_category(c) - 126●
jpeg_prog_emit_sym(w, enc, freq, (run << 4) | size) - 127●
jpeg_prog_emit_value(w, freq, c, size) - 128
run = 0 - 129
} - 130
k = k + 1 - 131
} - 132
if run > 0 { - 133
// 块尾残余零:立即 EOB 收尾(不进 EOBRUN),与 0.1.30 固定脚本同口径 - 134●
jpeg_prog_emit_sym(w, enc, freq, 0) - 135
} - 136
} - 137
} - 138
if eobrun > 0 { - 139●
prog_emit_eobrun(w, enc, freq, eobrun) - 140
} - 141
} - 142
- 143
///| - 144
/// 一条 AC 细化扫描(单分量平面,band `Ss..Se`,`Ah>0`,点变换 `Al`)。 - 145
/// 新非零判据是 `|c|>>Al == 1`(libjpeg `encode_mcu_AC_refine` 的 `temp == 1`), - 146
/// `|c|>>Al > 1` 的系数是"更高位已发过",只补一位修正位;修正位取 `|c|>>Al` 的最低位。 - 147
fn prog_ac_refine_scan( - 148
w : JpegBitWriter, - 149
enc : JpegHuffmanEncoder, - 150
freq : Array[Int32], - 151
plane : Array[Int32], - 152
stride : Int32, - 153
bx_max : Int32, - 154
by_max : Int32, - 155
ss : Int32, - 156
se : Int32, - 157
al : Int32, - 158
) -> Unit { - 159●
let absv = Array::make(64, 0) - 160
let be : Array[Int32] = [] - 161
let br : Array[Int32] = [] - 162
let mut eobrun = 0 - 163
for by in 0..<by_max { - 164●
for bx in 0..<bx_max { - 165●
let base = (by * stride + bx) * 64 - 166
let mut has_nz = false - 167
let mut eob = ss - 1 - 168
let mut k = ss - 169
while k <= se { - 170●
let c = plane[base + k] - 171●
let a = if c < 0 { -c } else { c } - 172
let av = a >> al - 173
absv[k] = av - 174
if av != 0 { - 175●
has_nz = true - 176
} - 177
if av == 1 { - 178●
eob = k - 179
} - 180
k = k + 1 - 181
} - 182
if has_nz && eobrun > 0 { - 183●
jpeg_prog_flush_eobrun(w, enc, freq, eobrun, be) - 184
eobrun = 0 - 185
} - 186
let mut r = 0 - 187
k = ss - 188
while k <= se { - 189●
let av = absv[k] - 190
if av == 0 { - 191●
r += 1 - 192
k = k + 1 - 193
continue - 194
} - 195
while r > 15 && k <= eob { - 196●
jpeg_prog_flush_eobrun(w, enc, freq, eobrun, be) - 197
eobrun = 0 - 198●
jpeg_prog_emit_sym(w, enc, freq, 0xF0) - 199
r -= 16 - 200●
jpeg_prog_flush_corr(w, freq, br) - 201
} - 202
if av > 1 { - 203●
br.push(av & 1) - 204
} else { - 205●
jpeg_prog_flush_eobrun(w, enc, freq, eobrun, be) - 206
eobrun = 0 - 207●
jpeg_prog_emit_sym(w, enc, freq, (r << 4) | 1) - 208●
jpeg_prog_emit_bit(w, freq, if plane[base + k] < 0 { 0 } else { 1 }) - 209●
jpeg_prog_flush_corr(w, freq, br) - 210
r = 0 - 211
} - 212
k = k + 1 - 213
} - 214●
if r > 0 || br.length() > 0 { - 215●
eobrun += 1 - 216
for b in br { - 217●
be.push(b) - 218
} - 219●
br.clear() - 220
if eobrun == PROGRESSIVE_MAX_EOBRUN { - 221○
jpeg_prog_flush_eobrun(w, enc, freq, eobrun, be) - 222
eobrun = 0 - 223
} - 224
} - 225
} - 226
} - 227○
if eobrun > 0 || be.length() > 0 { - 228●
jpeg_prog_flush_eobrun(w, enc, freq, eobrun, be) - 229
} - 230●
jpeg_prog_flush_corr(w, freq, br) - 231
} - 232
- 233
///| - 234
/// 一条交错 DC 首扫(4:2:0 的 MCU 顺序:4 个 Y 块 + Cb + Cr)。 - 235
fn prog420_dc_first( - 236
w : JpegBitWriter, - 237
enc_lum : JpegHuffmanEncoder, - 238
enc_chr : JpegHuffmanEncoder, - 239
coefs_y : Array[Int32], - 240
coefs_cb : Array[Int32], - 241
coefs_cr : Array[Int32], - 242
stride_y : Int32, - 243
stride_c : Int32, - 244
mcus_x : Int32, - 245
mcus_y : Int32, - 246
al : Int32, - 247
) -> Unit { - 248●
let prev : Array[Ref[Int32]] = [Ref(0), Ref(0), Ref(0)] - 249
for my in 0..<mcus_y { - 250●
for mx in 0..<mcus_x { - 251●
for bi in 0..<4 { - 252●
let gx = mx * 2 + bi % 2 - 253
let gy = my * 2 + bi / 2 - 254
let dc = coefs_y[(gy * stride_y + gx) * 64] >> al - 255
let diff = dc - prev[0].val - 256
prev[0].val = dc - 257●
let cat = jpeg_category(diff) - 258●
jpeg_write_bits(w, enc_lum.codes[cat], enc_lum.lengths[cat]) - 259
if cat > 0 { - 260●
jpeg_write_bits(w, jpeg_value_bits(diff, cat), cat) - 261
} - 262
} - 263
let cbase = (my * stride_c + mx) * 64 - 264
let dc_cb = coefs_cb[cbase] >> al - 265
let diff_cb = dc_cb - prev[1].val - 266
prev[1].val = dc_cb - 267●
let cat_cb = jpeg_category(diff_cb) - 268●
jpeg_write_bits(w, enc_chr.codes[cat_cb], enc_chr.lengths[cat_cb]) - 269
if cat_cb > 0 { - 270●
jpeg_write_bits(w, jpeg_value_bits(diff_cb, cat_cb), cat_cb) - 271
} - 272
let dc_cr = coefs_cr[cbase] >> al - 273
let diff_cr = dc_cr - prev[2].val - 274
prev[2].val = dc_cr - 275●
let cat_cr = jpeg_category(diff_cr) - 276●
jpeg_write_bits(w, enc_chr.codes[cat_cr], enc_chr.lengths[cat_cr]) - 277
if cat_cr > 0 { - 278●
jpeg_write_bits(w, jpeg_value_bits(diff_cr, cat_cr), cat_cr) - 279
} - 280
} - 281
} - 282
} - 283
- 284
///| - 285
/// 一条交错 DC 细化扫描:每块只发 `(DC >> Al) & 1`。 - 286
fn prog420_dc_refine( - 287
w : JpegBitWriter, - 288
coefs_y : Array[Int32], - 289
coefs_cb : Array[Int32], - 290
coefs_cr : Array[Int32], - 291
stride_y : Int32, - 292
stride_c : Int32, - 293
mcus_x : Int32, - 294
mcus_y : Int32, - 295
al : Int32, - 296
) -> Unit { - 297●
for my in 0..<mcus_y { - 298●
for mx in 0..<mcus_x { - 299●
for bi in 0..<4 { - 300●
let gx = mx * 2 + bi % 2 - 301
let gy = my * 2 + bi / 2 - 302●
jpeg_write_bits(w, (coefs_y[(gy * stride_y + gx) * 64] >> al) & 1, 1) - 303
} - 304
let cbase = (my * stride_c + mx) * 64 - 305●
jpeg_write_bits(w, (coefs_cb[cbase] >> al) & 1, 1) - 306●
jpeg_write_bits(w, (coefs_cr[cbase] >> al) & 1, 1) - 307
} - 308
} - 309
} - 310
- 311
///| - 312
/// 校验脚本合法性(照 ITU-T T.81 对 progressive 参数的约束 + 本库 3 分量布局)。 - 313
fn prog_script_check( - 314
script : Array[JpegProgScan], - 315
) -> Unit raise @types.LoadError { - 316●
if script.length() == 0 { - 317○
raise @types.LoadError::EncodeFailed("progressive 扫描脚本为空") - 318
} - 319
for s in script { - 320●
if s.ss < 0 || s.se > 63 || s.ss > s.se { - 321○
raise @types.LoadError::EncodeFailed( - 322
"progressive 扫描脚本 Ss/Se 越界", - 323
) - 324
} - 325
if s.ah < 0 || s.ah > 13 || s.al < 0 || s.al > 13 { - 326○
raise @types.LoadError::EncodeFailed( - 327
"progressive 扫描脚本 Ah/Al 越界", - 328
) - 329
} - 330●
if s.comps.length() == 0 || s.comps.length() > 3 { - 331○
raise @types.LoadError::EncodeFailed( - 332
"progressive 扫描脚本分量数无效", - 333
) - 334
} - 335●
if s.ss != 0 && s.comps.length() != 1 { - 336○
raise @types.LoadError::EncodeFailed( - 337
"progressive 的 AC 扫描只能是单分量", - 338
) - 339
} - 340
if s.ss == 0 && s.se != 0 { - 341○
raise @types.LoadError::EncodeFailed( - 342
"progressive 的 DC 扫描必须 Ss=Se=0", - 343
) - 344
} - 345
for ci in s.comps { - 346●
if ci < 0 || ci > 2 { - 347○
raise @types.LoadError::EncodeFailed( - 348
"progressive 扫描脚本分量序号越界(仅 0/1/2)", - 349
) - 350
} - 351
} - 352
} - 353
} - 354
- 355
///| - 356
/// 4:2:0 渐进式编码 · 扫描脚本可配置。 - 357
/// - 358
/// 参数 Parameters: - 359
/// - `img` - 输入图像(1 或 3 通道) - 360
/// - `quality` - 质量 1–100 - 361
/// - `script` - 扫描脚本(`jpeg_progressive_script_libjpeg()` 为 libjpeg 默认 10 段) - 362
/// - 363
/// 返回 Return: - 364
/// - progressive JPEG(SOF2,Y `2×2`、Cb/Cr `1×1`)字节流 - 365
/// - 366
/// 错误 Errors: - 367
/// - `LoadError::EncodeFailed` - 尺寸无效、像素数超上限、脚本非法 - 368
pub fn encode_jpeg_progressive_420_scripted( - 369
img : @types.Image, - 370
quality : Int32, - 371
script : Array[JpegProgScan], - 372
) -> Bytes raise @types.LoadError { - 373●
let width = img.width - 374
let height = img.height - 375
if width <= 0 || height <= 0 { - 376○
raise @types.LoadError::EncodeFailed("JPEG 尺寸无效") - 377
} - 378
if height > PROGRESSIVE_MAX_PIXELS / width { - 379○
raise @types.LoadError::EncodeFailed( - 380
"progressive 编码暂支持 ≤ 2,000,000 像素(量化系数缓存上限)", - 381
) - 382
} - 383●
prog_script_check(script) - 384
let channels = img.channels - 385
let data = img.data - 386●
let zz = jpeg_zigzag() - 387●
let quant_lum = jpeg_scaled_quant(std_quant_luminance(), quality) - 388●
let quant_chr = jpeg_scaled_quant(std_quant_chrominance(), quality) - 389●
let dc_lum_enc = build_huffman_encoder(std_dc_lum_bits(), std_dc_lum_vals()) - 390●
let dc_chr_enc = build_huffman_encoder(std_dc_chr_bits(), std_dc_chr_vals()) - 391
let mcus_x = (width + 15) / 16 - 392
let mcus_y = (height + 15) / 16 - 393
let stride_y = mcus_x * 2 - 394
let stride_c = mcus_x - 395
let y_fx = (width + 7) / 8 - 396
let y_fy = (height + 7) / 8 - 397
let c_fx = (width + 15) / 16 - 398
let c_fy = (height + 15) / 16 - 399●
let coefs_y : Array[Int32] = Array::make(stride_y * (mcus_y * 2) * 64, 0) - 400●
let coefs_cb : Array[Int32] = Array::make(stride_c * c_fy * 64, 0) - 401●
let coefs_cr : Array[Int32] = Array::make(stride_c * c_fy * 64, 0) - 402●
let inv_lum : Array[Float] = Array::make(64, 0.0F) - 403●
let inv_chr : Array[Float] = Array::make(64, 0.0F) - 404
for i in 0..<64 { - 405●
inv_lum[i] = 1.0F / (Float::from_int(quant_lum[i]) * 8.0F) - 406●
inv_chr[i] = 1.0F / (Float::from_int(quant_chr[i]) * 8.0F) - 407
} - 408●
let tmp_i : Array[Int32] = Array::make(64, 0) - 409●
let shifted : Array[Int32] = Array::make(64, 0) - 410●
let dct_out : Array[Int32] = Array::make(64, 0) - 411●
let block : Array[Int32] = Array::make(64, 128) - 412●
let block_cr : Array[Int32] = Array::make(64, 128) - 413
for by in 0..<(mcus_y * 2) { - 414●
for bx in 0..<stride_y { - 415●
for py in 0..<8 { - 416●
for px in 0..<8 { - 417●
let (yv, _, _) = prog420_pixel_ycbcr( - 418
data, - 419
channels, - 420
width, - 421
height, - 422
bx * 8 + px, - 423
by * 8 + py, - 424
) - 425
block[py * 8 + px] = yv - 426
} - 427
} - 428●
jpeg_progressive_quantize_block( - 429
block, - 430
inv_lum, - 431
zz, - 432
tmp_i, - 433
shifted, - 434
dct_out, - 435
coefs_y, - 436
(by * stride_y + bx) * 64, - 437
) - 438
} - 439
} - 440
for by in 0..<c_fy { - 441●
for bx in 0..<c_fx { - 442●
for py in 0..<8 { - 443●
for px in 0..<8 { - 444●
let sx = bx * 16 + px * 2 - 445
let sy = by * 16 + py * 2 - 446●
let (_, cb0, cr0) = prog420_pixel_ycbcr( - 447
data, channels, width, height, sx, sy, - 448
) - 449●
let (_, cb1, cr1) = prog420_pixel_ycbcr( - 450
data, - 451
channels, - 452
width, - 453
height, - 454
sx + 1, - 455
sy, - 456
) - 457●
let (_, cb2, cr2) = prog420_pixel_ycbcr( - 458
data, - 459
channels, - 460
width, - 461
height, - 462
sx, - 463
sy + 1, - 464
) - 465●
let (_, cb3, cr3) = prog420_pixel_ycbcr( - 466
data, - 467
channels, - 468
width, - 469
height, - 470
sx + 1, - 471
sy + 1, - 472
) - 473
let o = py * 8 + px - 474
block[o] = (cb0 + cb1 + cb2 + cb3) / 4 - 475
block_cr[o] = (cr0 + cr1 + cr2 + cr3) / 4 - 476
} - 477
} - 478
let cbase = (by * stride_c + bx) * 64 - 479●
jpeg_progressive_quantize_block( - 480
block, inv_chr, zz, tmp_i, shifted, dct_out, coefs_cb, cbase, - 481
) - 482●
jpeg_progressive_quantize_block( - 483
block_cr, inv_chr, zz, tmp_i, shifted, dct_out, coefs_cr, cbase, - 484
) - 485
} - 486
} - 487
let out : Array[Byte] = [] - 488●
out.push(b'\xFF') - 489●
out.push(b'\xD8') - 490
// ---- DQT - 491●
out.push(b'\xFF') - 492●
out.push(b'\xDB') - 493●
out.push(b'\x00') - 494●
out.push(b'\x84') - 495●
out.push(b'\x00') - 496
for k in 0..<64 { - 497●
out.push(quant_lum[zz[k]].to_byte()) - 498
} - 499●
out.push(b'\x01') - 500
for k in 0..<64 { - 501●
out.push(quant_chr[zz[k]].to_byte()) - 502
} - 503
// ---- SOF2(Y 2×2、Cb/Cr 1×1) - 504●
out.push(b'\xFF') - 505●
out.push(b'\xC2') - 506●
out.push(b'\x00') - 507●
out.push(b'\x11') - 508●
out.push(b'\x08') - 509●
out.push(((height >> 8) & 0xFF).to_byte()) - 510●
out.push((height & 0xFF).to_byte()) - 511●
out.push(((width >> 8) & 0xFF).to_byte()) - 512●
out.push((width & 0xFF).to_byte()) - 513●
out.push(b'\x03') - 514●
out.push(b'\x01') - 515●
out.push(b'\x22') - 516●
out.push(b'\x00') - 517●
out.push(b'\x02') - 518●
out.push(b'\x11') - 519●
out.push(b'\x01') - 520●
out.push(b'\x03') - 521●
out.push(b'\x11') - 522●
out.push(b'\x01') - 523
// ---- DHT:先给 DC 标准两张(AC 表按扫描逐条重建,紧跟各自 SOS) - 524
let dc_dht : Array[Byte] = [] - 525●
dc_dht.push(b'\x00') - 526●
for b in std_dc_lum_bits() { - 527●
dc_dht.push(b.to_byte()) - 528
} - 529●
for v in std_dc_lum_vals() { - 530●
dc_dht.push(v.to_byte()) - 531
} - 532●
dc_dht.push(b'\x01') - 533●
for b in std_dc_chr_bits() { - 534●
dc_dht.push(b.to_byte()) - 535
} - 536●
for v in std_dc_chr_vals() { - 537●
dc_dht.push(v.to_byte()) - 538
} - 539●
let dc_dht_len = 2 + dc_dht.length() - 540●
out.push(b'\xFF') - 541●
out.push(b'\xC4') - 542●
out.push(((dc_dht_len >> 8) & 0xFF).to_byte()) - 543●
out.push((dc_dht_len & 0xFF).to_byte()) - 544
for b in dc_dht { - 545●
out.push(b) - 546
} - 547
// ---- 逐扫描:AC 扫描两遍(计频 → 建表 → 写 DHT → SOS → 位流) - 548●
let scratch_out : Array[Byte] = Array::make(1, b'\x00') - 549
for scan in script { - 550●
let is_dc = scan.ss == 0 - 551
if is_dc && scan.ah == 0 { - 552●
let ids : Array[Byte] = [] - 553
let slots : Array[Byte] = [] - 554
for ci in scan.comps { - 555●
ids.push((ci + 1).to_byte()) - 556●
slots.push(if ci == 0 { b'\x00' } else { b'\x11' }) - 557
} - 558●
jpeg_push_sos( - 559
out, - 560●
ids.length(), - 561
ids, - 562
slots, - 563
0, - 564
0, - 565
(scan.ah << 4) | scan.al, - 566
) - 567
let w = JpegBitWriter::{ out, bit_buf: 0, bit_cnt: 0, } - 568●
prog420_dc_first( - 569
w, - 570
dc_lum_enc, - 571
dc_chr_enc, - 572
coefs_y, - 573
coefs_cb, - 574
coefs_cr, - 575
stride_y, - 576
stride_c, - 577
mcus_x, - 578
mcus_y, - 579
scan.al, - 580
) - 581●
jpeg_flush_bits(w) - 582●
} else if is_dc { - 583●
let ids : Array[Byte] = [] - 584
let slots : Array[Byte] = [] - 585
for ci in scan.comps { - 586●
ids.push((ci + 1).to_byte()) - 587●
slots.push(if ci == 0 { b'\x00' } else { b'\x11' }) - 588
} - 589●
jpeg_push_sos( - 590
out, - 591●
ids.length(), - 592
ids, - 593
slots, - 594
0, - 595
0, - 596
(scan.ah << 4) | scan.al, - 597
) - 598
let w = JpegBitWriter::{ out, bit_buf: 0, bit_cnt: 0, } - 599●
prog420_dc_refine( - 600
w, - 601
coefs_y, - 602
coefs_cb, - 603
coefs_cr, - 604
stride_y, - 605
stride_c, - 606
mcus_x, - 607
mcus_y, - 608
scan.al, - 609
) - 610●
jpeg_flush_bits(w) - 611
} else { - 612●
let ci = scan.comps[0] - 613
let (plane, stride, bx_max, by_max) = if ci == 0 { - 614●
(coefs_y, stride_y, y_fx, y_fy) - 615●
} else if ci == 1 { - 616●
(coefs_cb, stride_c, c_fx, c_fy) - 617
} else { - 618●
(coefs_cr, stride_c, c_fx, c_fy) - 619
} - 620●
let slot : Byte = if ci == 0 { b'\x00' } else { b'\x11' } - 621●
let id : Array[Byte] = [(ci + 1).to_byte()] - 622
let slots : Array[Byte] = [slot] - 623
if scan.ah == 0 { - 624●
let freq = Array::make(257, 0) - 625●
prog_ac_first_scan( - 626
JpegBitWriter::{ out: scratch_out, bit_buf: 0, bit_cnt: 0, }, - 627
if ci == 0 { - 628●
dc_lum_enc - 629
} else { - 630●
dc_chr_enc - 631
}, - 632
freq, - 633
plane, - 634
stride, - 635
bx_max, - 636
by_max, - 637
scan.ss, - 638
scan.se, - 639
scan.al, - 640
) - 641●
let (bits, vals) = jpeg_gen_optimal_table(freq) - 642●
let enc = build_huffman_encoder(bits, vals) - 643
let dht_data : Array[Byte] = [] - 644●
dht_data.push(if ci == 0 { b'\x10' } else { b'\x11' }) - 645
for b in bits { - 646●
dht_data.push(b.to_byte()) - 647
} - 648
for v in vals { - 649●
dht_data.push(v.to_byte()) - 650
} - 651●
let dht_len = 2 + dht_data.length() - 652●
out.push(b'\xFF') - 653●
out.push(b'\xC4') - 654●
out.push(((dht_len >> 8) & 0xFF).to_byte()) - 655●
out.push((dht_len & 0xFF).to_byte()) - 656
for b in dht_data { - 657●
out.push(b) - 658
} - 659●
jpeg_push_sos( - 660
out, - 661
1, - 662
id, - 663
slots, - 664
scan.ss, - 665
scan.se, - 666
(scan.ah << 4) | scan.al, - 667
) - 668
let w = JpegBitWriter::{ out, bit_buf: 0, bit_cnt: 0, } - 669●
prog_ac_first_scan( - 670
w, - 671
enc, - 672
[], - 673
plane, - 674
stride, - 675
bx_max, - 676
by_max, - 677
scan.ss, - 678
scan.se, - 679
scan.al, - 680
) - 681●
jpeg_flush_bits(w) - 682
} else { - 683●
let freq = Array::make(257, 0) - 684●
prog_ac_refine_scan( - 685
JpegBitWriter::{ out: scratch_out, bit_buf: 0, bit_cnt: 0, }, - 686
if ci == 0 { - 687●
dc_lum_enc - 688
} else { - 689●
dc_chr_enc - 690
}, - 691
freq, - 692
plane, - 693
stride, - 694
bx_max, - 695
by_max, - 696
scan.ss, - 697
scan.se, - 698
scan.al, - 699
) - 700●
let (bits, vals) = jpeg_gen_optimal_table(freq) - 701●
let enc = build_huffman_encoder(bits, vals) - 702
let dht_data : Array[Byte] = [] - 703●
dht_data.push(if ci == 0 { b'\x10' } else { b'\x11' }) - 704
for b in bits { - 705●
dht_data.push(b.to_byte()) - 706
} - 707
for v in vals { - 708●
dht_data.push(v.to_byte()) - 709
} - 710●
let dht_len = 2 + dht_data.length() - 711●
out.push(b'\xFF') - 712●
out.push(b'\xC4') - 713●
out.push(((dht_len >> 8) & 0xFF).to_byte()) - 714●
out.push((dht_len & 0xFF).to_byte()) - 715
for b in dht_data { - 716●
out.push(b) - 717
} - 718●
jpeg_push_sos( - 719
out, - 720
1, - 721
id, - 722
slots, - 723
scan.ss, - 724
scan.se, - 725
(scan.ah << 4) | scan.al, - 726
) - 727
let w = JpegBitWriter::{ out, bit_buf: 0, bit_cnt: 0, } - 728●
prog_ac_refine_scan( - 729
w, - 730
enc, - 731
[], - 732
plane, - 733
stride, - 734
bx_max, - 735
by_max, - 736
scan.ss, - 737
scan.se, - 738
scan.al, - 739
) - 740●
jpeg_flush_bits(w) - 741
} - 742
} - 743
} - 744●
out.push(b'\xFF') - 745●
out.push(b'\xD9') - 746●
Bytes::from_array(out) - 747
}