image.codec\codec\hdr_encode.mbt

98.4% · 63 / 64 points · 146 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/// # HDR 编码 HDR Encode
  3. 3///
  4. 4/// HDR Radiance 格式编码 HDR Radiance format encoding
  5. 5// 纯 MoonBit HDR 编码(全目标可用:native/wasm/js)
  6. 6// RGBE 格式编码,使用 RLE 压缩扫描线
  7. 7// 仅依赖 @types,不依赖 @core/FFI
  8. 8
  9. 9///|
  10. 10/// RLE 编码单个通道扫描线
  11. 11fn rle_encode_scanline_channel(
  12. 12 channel_data : Array[Byte],
  13. 13 scanline_len : Int32,
  14. 14) -> Array[Byte] {
  15. 15● let out : Array[Byte] = []
  16. 16 let mut pos = 0
  17. 17 while pos < scanline_len {
  18. 18 // 查找重复 run
  19. 19● let mut run_len = 1
  20. 20 while pos + run_len < scanline_len &&
  21. 21 channel_data[pos + run_len] == channel_data[pos] &&
  22. 22 run_len < 127 {
  23. 23● run_len += 1
  24. 24 }
  25. 25 if run_len >= 4 {
  26. 26 // RLE 编码
  27. 27● out.push((128 + run_len).to_byte())
  28. 28● out.push(channel_data[pos])
  29. 29 pos += run_len
  30. 30 } else {
  31. 31 // 查找非重复 run
  32. 32● let mut lit_len = 1
  33. 33 while pos + lit_len < scanline_len && lit_len < 128 {
  34. 34 // 检查是否开始新的重复 run
  35. 35● let mut next_run = 0
  36. 36 while pos + lit_len + next_run < scanline_len &&
  37. 37 channel_data[pos + lit_len + next_run] ==
  38. 38 channel_data[pos + lit_len] &&
  39. 39 next_run < 3 {
  40. 40● next_run += 1
  41. 41 }
  42. 42 if next_run >= 4 {
  43. 43○ break
  44. 44 }
  45. 45 lit_len += 1
  46. 46 }
  47. 47 // 非重复编码
  48. 48● out.push(lit_len.to_byte())
  49. 49 for i in 0..<lit_len {
  50. 50● out.push(channel_data[pos + i])
  51. 51 }
  52. 52 pos += lit_len
  53. 53 }
  54. 54 }
  55. 55 out
  56. 56}
  57. 57
  58. 58///|
  59. 59/// HDR 编码(纯 MoonBit,全目标可用)。输入 ImageF(32-bit float 像素数据),返回 HDR 格式字节流。 HDR encoding (pure MoonBit, all targets). Input ImageF (32-bit float pixel data), returns HDR format byte stream.
  60. 60
  61. 61///|
  62. 62/// 参数 Parameters:
  63. 63/// - `img` - 输入 ImageF Input ImageF
  64. 64///
  65. 65/// 返回 Return:
  66. 66/// - HDR 字节流 HDR byte stream
  67. 67///
  68. 68/// 错误 Errors:
  69. 69/// - `LoadError::EncodeFailed` - channels 不为 3 Channels not 3
  70. 70pub fn encode_hdr_pure(img : @types.ImageF) -> Bytes raise @types.LoadError {
  71. 71● if img.channels != 3 {
  72. 72● raise @types.LoadError::EncodeFailed(
  73. 73 "HDR 编码需要 3 通道 float 图像",
  74. 74 )
  75. 75 }
  76. 76 let width = img.width
  77. 77 let height = img.height
  78. 78 let pixel_count = width * height
  79. 79 // 构建输出
  80. 80 let out : Array[Byte] = []
  81. 81 // 写入 header
  82. 82 let header_str = "#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n"
  83. 83● let header_bytes = @encoding/utf8.encode(header_str)
  84. 84● for i in 0..<header_bytes.length() {
  85. 85● out.push(header_bytes[i])
  86. 86 }
  87. 87 // 写入尺寸行: "-Y HEIGHT +X WIDTH\n"
  88. 88● let dim_str = "-Y " + height.to_string() + " +X " + width.to_string() + "\n"
  89. 89● let dim_bytes = @encoding/utf8.encode(dim_str)
  90. 90● for i in 0..<dim_bytes.length() {
  91. 91● out.push(dim_bytes[i])
  92. 92 }
  93. 93 // 将 float 像素转换为 RGBE
  94. 94● let rgbe : Array[Byte] = Array::make(pixel_count * 4, b'\x00')
  95. 95 for i in 0..<pixel_count {
  96. 96 // 读取 little-endian float
  97. 97● let r_int = img.data[i * 12].to_int() |
  98. 98● (img.data[i * 12 + 1].to_int() << 8) |
  99. 99● (img.data[i * 12 + 2].to_int() << 16) |
  100. 100● (img.data[i * 12 + 3].to_int() << 24)
  101. 101● let g_int = img.data[i * 12 + 4].to_int() |
  102. 102● (img.data[i * 12 + 5].to_int() << 8) |
  103. 103● (img.data[i * 12 + 6].to_int() << 16) |
  104. 104● (img.data[i * 12 + 7].to_int() << 24)
  105. 105● let b_int = img.data[i * 12 + 8].to_int() |
  106. 106● (img.data[i * 12 + 9].to_int() << 8) |
  107. 107● (img.data[i * 12 + 10].to_int() << 16) |
  108. 108● (img.data[i * 12 + 11].to_int() << 24)
  109. 109● let r = Float::reinterpret_from_int(r_int)
  110. 110● let g = Float::reinterpret_from_int(g_int)
  111. 111● let b = Float::reinterpret_from_int(b_int)
  112. 112● let (rb, gb, bb, eb) = float_to_rgbe(r, g, b)
  113. 113 rgbe[i * 4] = rb
  114. 114 rgbe[i * 4 + 1] = gb
  115. 115 rgbe[i * 4 + 2] = bb
  116. 116 rgbe[i * 4 + 3] = eb
  117. 117 }
  118. 118 // RLE 编码每行
  119. 119 if width < 8 || width > 32767 {
  120. 120 // 小图或大图:不压缩
  121. 121● for i in 0..<(pixel_count * 4) {
  122. 122● out.push(rgbe[i])
  123. 123 }
  124. 124 } else {
  125. 125 // RLE 编码
  126. 126● for y in 0..<height {
  127. 127 // 写入行头 [2, 2, width_hi, width_lo]
  128. 128● out.push(b'\x02')
  129. 129● out.push(b'\x02')
  130. 130● out.push((width >> 8).to_byte())
  131. 131● out.push((width & 0xFF).to_byte())
  132. 132 // 4 通道分别 RLE 编码
  133. 133 for c in 0..<4 {
  134. 134● let channel_data : Array[Byte] = []
  135. 135 for x in 0..<width {
  136. 136● channel_data.push(rgbe[(y * width + x) * 4 + c])
  137. 137 }
  138. 138● let encoded = rle_encode_scanline_channel(channel_data, width)
  139. 139● for i in 0..<encoded.length() {
  140. 140● out.push(encoded[i])
  141. 141 }
  142. 142 }
  143. 143 }
  144. 144 }
  145. 145● Bytes::from_array(out)
  146. 146}