image.codec\codec\png_decode_16.mbt

99% · 103 / 104 points · 217 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/// # PNG 16-bit 解码 PNG 16-bit Decode
  3. 3///
  4. 4/// PNG 16-bit 高精度解码
  5. 5/// PNG 16-bit 高精度解码 PNG 16-bit high-precision decoding
  6. 6// 纯 MoonBit PNG 16-bit 解码(全目标可用:native/wasm/js)
  7. 7// 支持 8-bit PNG 上采样为 16-bit,以及原生 16-bit PNG
  8. 8// 输出 Image16(little-endian uint16 per sample)
  9. 9
  10. 10///|
  11. 11/// PNG 16-bit 解码(纯 MoonBit,全目标可用)。
  12. 12/// 支持 bit depth 8(上采样为 16-bit)和 16(原生)。
  13. 13/// color type 0/2/3/4/6,不支持 interlace。
  14. 14/// req_channels: 0/None=原色, 1=灰度, 2=灰度+alpha, 3=RGB, 4=RGBA
  15. 15pub fn decode_png_16_pure(
  16. 16 data : Bytes,
  17. 17 req_channels~ : Int32? = None,
  18. 18) -> @types.Image16 raise @types.LoadError {
  19. 19● match req_channels {
  20. 20 Some(n) =>
  21. 21● if n < 1 || n > 4 {
  22. 22● raise @types.LoadError::DecodeFailed("req_channels 必须为 1-4")
  23. 23 }
  24. 24● None => ()
  25. 25 }
  26. 26● let len = data.length()
  27. 27● if len < 8 || !is_png_signature(data, 0) {
  28. 28● raise @types.LoadError::DecodeFailed("PNG signature 错误")
  29. 29 }
  30. 30 let mut pos = 8
  31. 31 let mut width = 0
  32. 32 let mut height = 0
  33. 33 let mut bit_depth = 0
  34. 34 let mut color_type = 0
  35. 35 let mut interlace = 0
  36. 36 let palette : Array[(Int32, Int32, Int32)] = []
  37. 37 let idat_data : Array[Byte] = []
  38. 38 let mut found_ihdr = false
  39. 39 let mut found_iend = false
  40. 40 while pos < len && !found_iend {
  41. 41● if pos + 8 > len {
  42. 42● raise @types.LoadError::DecodeFailed("PNG chunk 头部过短")
  43. 43 }
  44. 44● let chunk_len = read_u32_be(data, pos)
  45. 45● let chunk_key = read_u32_be(data, pos + 4)
  46. 46 pos += 8
  47. 47 // 溢出安全:以 `len - pos` 为界,避免 pos+chunk_len 在 Int32 上回绕
  48. 48 if chunk_len < 0 || chunk_len > len - pos {
  49. 49● raise @types.LoadError::DecodeFailed("PNG chunk 数据越界")
  50. 50 }
  51. 51● match chunk_key {
  52. 52● 0x49484452 => {
  53. 53 if chunk_len < 13 {
  54. 54● raise @types.LoadError::DecodeFailed("PNG IHDR 过短")
  55. 55 }
  56. 56● width = read_u32_be(data, pos)
  57. 57● height = read_u32_be(data, pos + 4)
  58. 58● bit_depth = data[pos + 8].to_int()
  59. 59● color_type = data[pos + 9].to_int()
  60. 60● interlace = data[pos + 12].to_int()
  61. 61 found_ihdr = true
  62. 62 }
  63. 63● 0x504C5445 => {
  64. 64 let num_colors = chunk_len / 3
  65. 65 for i in 0..<num_colors {
  66. 66● palette.push(
  67. 67 (
  68. 68● data[pos + i * 3].to_int(),
  69. 69● data[pos + i * 3 + 1].to_int(),
  70. 70● data[pos + i * 3 + 2].to_int(),
  71. 71 ),
  72. 72 )
  73. 73 }
  74. 74 }
  75. 75 0x49444154 =>
  76. 76● for i in 0..<chunk_len {
  77. 77● idat_data.push(data[pos + i])
  78. 78 }
  79. 79● 0x49454E44 => found_iend = true
  80. 80● _ => ()
  81. 81 }
  82. 82 pos += chunk_len + 4
  83. 83 }
  84. 84 if !found_ihdr {
  85. 85● raise @types.LoadError::DecodeFailed("PNG 无 IHDR chunk")
  86. 86 }
  87. 87 if width <= 0 || height <= 0 {
  88. 88● raise @types.LoadError::DecodeFailed("PNG 尺寸无效")
  89. 89 }
  90. 90 if interlace != 0 {
  91. 91● raise @types.LoadError::DecodeFailed("PNG interlace 不支持")
  92. 92 }
  93. 93 if bit_depth != 8 && bit_depth != 16 {
  94. 94● raise @types.LoadError::DecodeFailed(
  95. 95 "PNG 16-bit 解码仅支持 bit depth 8 或 16",
  96. 96 )
  97. 97 }
  98. 98● let ch = match png_channels(color_type) {
  99. 99● Some(c) => c
  100. 100● None => raise @types.LoadError::DecodeFailed("PNG 无效 color type")
  101. 101 }
  102. 102● let raw = @util.zlib_inflate(Bytes::from_array(idat_data))
  103. 103 // bytes per pixel for filtering
  104. 104 let bpp = ch * (bit_depth / 8)
  105. 105 let stride = width * bpp
  106. 106● let unfiltered : Array[Byte] = Array::make(height * stride, b'\x00')
  107. 107 let mut raw_pos = 0
  108. 108 for y in 0..<height {
  109. 109● if raw_pos >= raw.length() {
  110. 110○ raise @types.LoadError::DecodeFailed("PNG 解压数据过短")
  111. 111 }
  112. 112● let filter_type = raw[raw_pos].to_int()
  113. 113 raw_pos += 1
  114. 114 for x in 0..<stride {
  115. 115● if raw_pos >= raw.length() {
  116. 116● raise @types.LoadError::DecodeFailed("PNG 解压数据过短")
  117. 117 }
  118. 118● let raw_byte = raw[raw_pos].to_int()
  119. 119 raw_pos += 1
  120. 120 let a = if x >= bpp {
  121. 121● unfiltered[y * stride + x - bpp].to_int()
  122. 122 } else {
  123. 123● 0
  124. 124 }
  125. 125● let b = if y > 0 { unfiltered[(y - 1) * stride + x].to_int() } else { 0 }
  126. 126 let c = if x >= bpp && y > 0 {
  127. 127● unfiltered[(y - 1) * stride + x - bpp].to_int()
  128. 128 } else {
  129. 129● 0
  130. 130 }
  131. 131● let val = match filter_type {
  132. 132● 0 => raw_byte
  133. 133● 1 => (raw_byte + a) & 0xFF
  134. 134● 2 => (raw_byte + b) & 0xFF
  135. 135● 3 => (raw_byte + (a + b) / 2) & 0xFF
  136. 136● 4 => (raw_byte + paeth_predict(a, b, c)) & 0xFF
  137. 137● _ => raise @types.LoadError::DecodeFailed("PNG 无效 filter type")
  138. 138 }
  139. 139● unfiltered[y * stride + x] = val.to_byte()
  140. 140 }
  141. 141 }
  142. 142 // 转换为 16-bit samples (little-endian)
  143. 143 let pixel_count = width * height
  144. 144● let out_ch = match req_channels {
  145. 145● Some(n) => n
  146. 146● None => ch
  147. 147 }
  148. 148 // 先提取原生 16-bit samples
  149. 149● let samples16 : Array[Int32] = Array::make(pixel_count * ch, 0)
  150. 150 if bit_depth == 16 {
  151. 151 // big-endian → native int
  152. 152● for i in 0..<(pixel_count * ch) {
  153. 153● let hi = unfiltered[i * 2].to_int()
  154. 154● let lo = unfiltered[i * 2 + 1].to_int()
  155. 155 samples16[i] = hi * 256 + lo
  156. 156 }
  157. 157 } else {
  158. 158 // 8-bit → upscale: v * 257
  159. 159● for i in 0..<(pixel_count * ch) {
  160. 160● samples16[i] = unfiltered[i].to_int() * 257
  161. 161 }
  162. 162 }
  163. 163 // 通道转换
  164. 164● let out_data : Array[Byte] = Array::make(pixel_count * out_ch * 2, b'\x00')
  165. 165 for i in 0..<pixel_count {
  166. 166● let (r16, g16, b16, a16) = if ch == 1 {
  167. 167● let v = samples16[i]
  168. 168 (v, v, v, 65535)
  169. 169● } else if ch == 2 {
  170. 170● let v = samples16[i * 2]
  171. 171 let a = samples16[i * 2 + 1]
  172. 172 (v, v, v, a)
  173. 173● } else if ch == 3 {
  174. 174● (samples16[i * 3], samples16[i * 3 + 1], samples16[i * 3 + 2], 65535)
  175. 175 } else {
  176. 176● (
  177. 177 samples16[i * 4],
  178. 178 samples16[i * 4 + 1],
  179. 179 samples16[i * 4 + 2],
  180. 180 samples16[i * 4 + 3],
  181. 181 )
  182. 182 }
  183. 183● let (or16, og16, ob16, oa16) = match out_ch {
  184. 184● 1 => {
  185. 185 let gray = (r16 * 299 + g16 * 587 + b16 * 114) / 1000
  186. 186 (gray, gray, gray, 65535)
  187. 187 }
  188. 188● 2 => {
  189. 189 let gray = (r16 * 299 + g16 * 587 + b16 * 114) / 1000
  190. 190 (gray, gray, gray, a16)
  191. 191 }
  192. 192● 3 => (r16, g16, b16, 65535)
  193. 193● _ => (r16, g16, b16, a16)
  194. 194 }
  195. 195 // little-endian uint16
  196. 196● out_data[i * out_ch * 2] = (or16 & 0xFF).to_byte()
  197. 197● out_data[i * out_ch * 2 + 1] = ((or16 >> 8) & 0xFF).to_byte()
  198. 198 if out_ch >= 2 {
  199. 199● out_data[i * out_ch * 2 + 2] = (og16 & 0xFF).to_byte()
  200. 200● out_data[i * out_ch * 2 + 3] = ((og16 >> 8) & 0xFF).to_byte()
  201. 201 }
  202. 202 if out_ch >= 3 {
  203. 203● out_data[i * out_ch * 2 + 4] = (ob16 & 0xFF).to_byte()
  204. 204● out_data[i * out_ch * 2 + 5] = ((ob16 >> 8) & 0xFF).to_byte()
  205. 205 }
  206. 206 if out_ch >= 4 {
  207. 207● out_data[i * out_ch * 2 + 6] = (oa16 & 0xFF).to_byte()
  208. 208● out_data[i * out_ch * 2 + 7] = ((oa16 >> 8) & 0xFF).to_byte()
  209. 209 }
  210. 210 }
  211. 211 @types.Image16::{
  212. 212 width,
  213. 213 height,
  214. 214 channels: out_ch,
  215. 215● data: Bytes::from_array(out_data),
  216. 216 }
  217. 217}