image.codec\codec\jpeg_progressive_preview.mbt

87.1% · 27 / 31 points · 155 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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