image.codec\lib\stream.mbt

96.3% · 26 / 27 points · 111 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/// # 流式处理 lib/stream
  3. 3///
  4. 4/// 编解码工具模块
  5. 5/// 编解码工具模块 Codec utility module
  6. 6// 流式解码接口(纯 MoonBit)
  7. 7// 提供逐行/分块回调式解码接口(当前为全量解码后逐行分发,未来支持增量解码)
  8. 8
  9. 9///|
  10. 10/// 流式解码信息
  11. 11#derive(Eq, Default, @debug.Debug)
  12. 12pub(all) struct StreamInfo {
  13. 13 width : Int32
  14. 14 height : Int32
  15. 15 channels : Int32
  16. 16}
  17. 17
  18. 18///|
  19. 19pub extend StreamInfo with Eq::{not_equal, equal}
  20. 20
  21. 21///|
  22. 22pub extend StreamInfo with Default::{default}
  23. 23
  24. 24///|
  25. 25pub extend StreamInfo with @debug.Debug::{to_repr}
  26. 26
  27. 27///|
  28. 28/// 流式解码:逐行回调处理。
  29. 29/// on_row 回调接收 (行号, 行数据) 参数。
  30. 30/// 行数据为 w*channels 个字节,按 RGB/RGBA 顺序排列。
  31. 31pub fn decode_stream(
  32. 32 data : Bytes,
  33. 33 on_row : (Int32, Array[Byte]) -> Unit,
  34. 34) -> StreamInfo raise @types.LoadError {
  35. 35● let img = load_from_bytes_auto(data)
  36. 36 let w = img.width
  37. 37 let h = img.height
  38. 38 let ch = img.channels
  39. 39● let row_data : Array[Byte] = Array::make(w * ch, b'\x00')
  40. 40 for y in 0..<h {
  41. 41● for x in 0..<w {
  42. 42● for c in 0..<ch {
  43. 43● row_data[x * ch + c] = img.data[(y * w + x) * ch + c]
  44. 44 }
  45. 45 }
  46. 46● on_row(y, row_data)
  47. 47 }
  48. 48 StreamInfo::{ width: w, height: h, channels: ch, }
  49. 49}
  50. 50
  51. 51///|
  52. 52/// 分块流式解码:每 chunk_height 行回调一次。
  53. 53/// on_chunk 回调接收 (起始行号, 块图像) 参数。
  54. 54/// 适用于分块处理大图,减少回调开销。
  55. 55pub fn decode_stream_chunked(
  56. 56 data : Bytes,
  57. 57 chunk_height : Int32,
  58. 58 on_chunk : (Int32, @types.Image) -> Unit,
  59. 59) -> StreamInfo raise @types.LoadError {
  60. 60● let img = load_from_bytes_auto(data)
  61. 61 let w = img.width
  62. 62 let h = img.height
  63. 63 let ch = img.channels
  64. 64◐ let chunk_h = if chunk_height < 1 { 1 } else { chunk_height }
  65. 65 let mut y = 0
  66. 66 while y < h {
  67. 67● let end_y = if y + chunk_h > h { h } else { y + chunk_h }
  68. 68 let block_h = end_y - y
  69. 69● let block_data : Array[Byte] = Array::make(w * block_h * ch, b'\x00')
  70. 70 for by in 0..<block_h {
  71. 71● for x in 0..<w {
  72. 72● for c in 0..<ch {
  73. 73● block_data[(by * w + x) * ch + c] = img.data[((y + by) * w + x) * ch +
  74. 74 c]
  75. 75 }
  76. 76 }
  77. 77 }
  78. 78 let block_img = @types.Image::{
  79. 79 width: w,
  80. 80 height: block_h,
  81. 81 channels: ch,
  82. 82● data: Bytes::from_array(block_data),
  83. 83 }
  84. 84● on_chunk(y, block_img)
  85. 85 y = end_y
  86. 86 }
  87. 87 StreamInfo::{ width: w, height: h, channels: ch, }
  88. 88}
  89. 89
  90. 90///|
  91. 91/// 流式解码(带 req_channels):指定输出通道数。
  92. 92pub fn decode_stream_channels(
  93. 93 data : Bytes,
  94. 94 req_channels : Int32,
  95. 95 on_row : (Int32, Array[Byte]) -> Unit,
  96. 96) -> StreamInfo raise @types.LoadError {
  97. 97● let img = load_from_bytes_auto(data, req_channels=Some(req_channels))
  98. 98 let w = img.width
  99. 99 let h = img.height
  100. 100 let ch = img.channels
  101. 101● let row_data : Array[Byte] = Array::make(w * ch, b'\x00')
  102. 102 for y in 0..<h {
  103. 103● for x in 0..<w {
  104. 104● for c in 0..<ch {
  105. 105● row_data[x * ch + c] = img.data[(y * w + x) * ch + c]
  106. 106 }
  107. 107 }
  108. 108● on_row(y, row_data)
  109. 109 }
  110. 110 StreamInfo::{ width: w, height: h, channels: ch, }
  111. 111}