image.codec\codec\png_encode.mbt

99.3% · 133 / 134 points · 198 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 编码 PNG Encode
  3. 3///
  4. 4/// PNG 图像格式编码
  5. 5/// PNG 图像格式编码 PNG image format encoding
  6. 6// 纯 MoonBit PNG 编码(全目标可用:native/wasm/js)
  7. 7// 使用 filter type 0 (None) + zlib stored blocks(无压缩但有效)
  8. 8
  9. 9///|
  10. 10/// PNG 行过滤(RFC 2083 §9):0 None / 1 Sub / 2 Up / 3 Average / 4 Paeth。
  11. 11fn png_filter_row(
  12. 12 raw : Array[Byte],
  13. 13 prev : Array[Byte],
  14. 14 bpp : Int32,
  15. 15 ft : Int32,
  16. 16) -> Array[Byte] {
  17. 17● let n = raw.length()
  18. 18● let out : Array[Byte] = Array::make(n, b'\x00')
  19. 19 for i in 0..<n {
  20. 20● let cur = raw[i].to_int()
  21. 21● let left = if i >= bpp { raw[i - bpp].to_int() } else { 0 }
  22. 22● let up = prev[i].to_int()
  23. 23● let ul = if i >= bpp { prev[i - bpp].to_int() } else { 0 }
  24. 24● let pred = match ft {
  25. 25● 1 => left
  26. 26● 2 => up
  27. 27● 3 => (left + up) / 2
  28. 28● 4 => {
  29. 29 // Paeth predictor
  30. 30 let p = left + up - ul
  31. 31● let pa = if p > left { p - left } else { left - p }
  32. 32● let pb = if p > up { p - up } else { up - p }
  33. 33● let pc = if p > ul { p - ul } else { ul - p }
  34. 34 if pa <= pb && pa <= pc {
  35. 35● left
  36. 36● } else if pb <= pc {
  37. 37● up
  38. 38 } else {
  39. 39● ul
  40. 40 }
  41. 41 }
  42. 42● _ => 0
  43. 43 }
  44. 44● out[i] = ((cur - pred) & 0xFF).to_byte()
  45. 45 }
  46. 46 out
  47. 47}
  48. 48
  49. 49///|
  50. 50/// 过滤结果的启发式代价:按「有符号字节」取绝对值求和(PNG 规范推荐)。
  51. 51fn png_filter_score(row : Array[Byte]) -> Int32 {
  52. 52● let mut s = 0
  53. 53● for i in 0..<row.length() {
  54. 54● let v = row[i].to_int()
  55. 55● s += if v < 128 { v } else { 256 - v }
  56. 56 }
  57. 57 s
  58. 58}
  59. 59
  60. 60///|
  61. 61/// 写入 PNG chunk
  62. 62fn write_chunk(out : Array[Byte], chunk_type : String, data : Bytes) -> Unit {
  63. 63● let len = data.length()
  64. 64 // length (big-endian)
  65. 65● out.push(((len >> 24) & 0xFF).to_byte())
  66. 66● out.push(((len >> 16) & 0xFF).to_byte())
  67. 67● out.push(((len >> 8) & 0xFF).to_byte())
  68. 68● out.push((len & 0xFF).to_byte())
  69. 69 // type
  70. 70● let type_bytes = @encoding/utf8.encode(chunk_type)
  71. 71 for i in 0..<4 {
  72. 72● out.push(type_bytes[i])
  73. 73 }
  74. 74 // data
  75. 75 for i in 0..<len {
  76. 76● out.push(data[i])
  77. 77 }
  78. 78 // CRC (over type + data)
  79. 79 let crc_data : Array[Byte] = []
  80. 80 for i in 0..<4 {
  81. 81● crc_data.push(type_bytes[i])
  82. 82 }
  83. 83 for i in 0..<len {
  84. 84● crc_data.push(data[i])
  85. 85 }
  86. 86● let crc = @util.crc32(Bytes::from_array(crc_data))
  87. 87● out.push(((crc >> 24) & 0xFF).to_byte())
  88. 88● out.push(((crc >> 16) & 0xFF).to_byte())
  89. 89● out.push(((crc >> 8) & 0xFF).to_byte())
  90. 90● out.push((crc & 0xFF).to_byte())
  91. 91}
  92. 92
  93. 93///|
  94. 94/// PNG 编码(纯 MoonBit,全目标可用)。
  95. 95/// 输入 Image(RGB),返回 PNG 格式字节流。
  96. 96/// 使用 filter type 0 (None) + zlib stored blocks(无压缩)。
  97. 97pub fn encode_png_pure(img : @types.Image) -> Bytes {
  98. 98● let width = img.width
  99. 99 let height = img.height
  100. 100 let ch = img.channels
  101. 101 // 确定输出通道数
  102. 102 let out_ch = if ch == 4 {
  103. 103● 4
  104. 104● } else if ch == 2 {
  105. 105● 2
  106. 106● } else if ch == 1 {
  107. 107● 1
  108. 108 } else {
  109. 109● 3
  110. 110 }
  111. 111 let color_type = if out_ch == 1 {
  112. 112● 0
  113. 113● } else if out_ch == 2 {
  114. 114● 4
  115. 115● } else if out_ch == 4 {
  116. 116● 6
  117. 117 } else {
  118. 118● 2
  119. 119 }
  120. 120 let out : Array[Byte] = []
  121. 121 // PNG signature
  122. 122● out.push(b'\x89')
  123. 123● out.push(b'\x50')
  124. 124● out.push(b'\x4E')
  125. 125● out.push(b'\x47')
  126. 126● out.push(b'\x0D')
  127. 127● out.push(b'\x0A')
  128. 128● out.push(b'\x1A')
  129. 129● out.push(b'\x0A')
  130. 130 // IHDR chunk
  131. 131 let ihdr : Array[Byte] = []
  132. 132 // width (big-endian)
  133. 133● ihdr.push(((width >> 24) & 0xFF).to_byte())
  134. 134● ihdr.push(((width >> 16) & 0xFF).to_byte())
  135. 135● ihdr.push(((width >> 8) & 0xFF).to_byte())
  136. 136● ihdr.push((width & 0xFF).to_byte())
  137. 137 // height (big-endian)
  138. 138● ihdr.push(((height >> 24) & 0xFF).to_byte())
  139. 139● ihdr.push(((height >> 16) & 0xFF).to_byte())
  140. 140● ihdr.push(((height >> 8) & 0xFF).to_byte())
  141. 141● ihdr.push((height & 0xFF).to_byte())
  142. 142● ihdr.push(b'\x08') // bit depth = 8
  143. 143● ihdr.push(color_type.to_byte())
  144. 144● ihdr.push(b'\x00') // compression = 0
  145. 145● ihdr.push(b'\x00') // filter = 0
  146. 146● ihdr.push(b'\x00') // interlace = 0
  147. 147● write_chunk(out, "IHDR", Bytes::from_array(ihdr))
  148. 148 // 构建过滤后的图像数据:逐行在 5 种 filter 里选绝对值和最小的那个
  149. 149 let bpp = out_ch
  150. 150 let row_len = width * out_ch
  151. 151 let filtered : Array[Byte] = []
  152. 152● let prev_row : Array[Byte] = Array::make(row_len, b'\x00')
  153. 153 for y in 0..<height {
  154. 154● let raw : Array[Byte] = Array::make(row_len, b'\x00')
  155. 155 for x in 0..<width {
  156. 156● let src_off = (y * width + x) * ch
  157. 157 if ch == out_ch {
  158. 158● for c in 0..<ch {
  159. 159● raw[x * out_ch + c] = img.data[src_off + c]
  160. 160 }
  161. 161 } else {
  162. 162● for c in 0..<out_ch {
  163. 163● raw[x * out_ch + c] = if c < ch {
  164. 164● img.data[src_off + c]
  165. 165 } else {
  166. 166○ b'\xFF'
  167. 167 }
  168. 168 }
  169. 169 }
  170. 170 }
  171. 171 let mut best_type = 0
  172. 172 let mut best_score = 0x7FFFFFFF
  173. 173 let mut best_row : Array[Byte] = raw
  174. 174 for ft in 0..<5 {
  175. 175● let cand = png_filter_row(raw, prev_row, bpp, ft)
  176. 176● let score = png_filter_score(cand)
  177. 177 if score < best_score {
  178. 178● best_score = score
  179. 179 best_type = ft
  180. 180 best_row = cand
  181. 181 }
  182. 182 }
  183. 183● filtered.push(best_type.to_byte())
  184. 184 for i in 0..<row_len {
  185. 185● filtered.push(best_row[i])
  186. 186 }
  187. 187 for i in 0..<row_len {
  188. 188● prev_row[i] = raw[i]
  189. 189 }
  190. 190 }
  191. 191 // zlib deflate(fixed Huffman + LZ77,RFC 1950/1951 合法流)
  192. 192● let compressed = zlib_compress(Bytes::from_array(filtered))
  193. 193 // IDAT chunk
  194. 194● write_chunk(out, "IDAT", compressed)
  195. 195 // IEND chunk
  196. 196● write_chunk(out, "IEND", Bytes::from_array([]))
  197. 197● Bytes::from_array(out)
  198. 198}