image.codec\codec\qoi_encode.mbt

96.5% · 83 / 86 points · 148 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/// # QOI 编码 QOI Encode
  3. 3///
  4. 4/// QOI 图像格式编码 QOI image format encoding
  5. 5// 纯 MoonBit QOI 编码器
  6. 6// 规范: https://qoiformat.org/qoi-specification.pdf
  7. 7// 移植自 src/format/qoi.mbt 的 encode_qoi,仅替换类型引用:
  8. 8// @core.Image -> @types.Image, @core.LoadError -> @types.LoadError
  9. 9// qoi_hash 复用 src/pure/qoi_decode.mbt 已有定义(同包私有函数跨文件可见)
  10. 10
  11. 11///|
  12. 12/// 将 Image 编码为 QOI 字节流,输入 Image 的 channels 必须为 3 (RGB) 或 4 (RGBA),输出: 14 字节 header + 像素编码 + 8 字节 padding Encode Image to QOI byte stream, Input Image channels must be 3 (RGB) or 4 (RGBA), Output: 14-byte header + pixel encoding + 8-byte padding
  13. 13
  14. 14///|
  15. 15/// 参数 Parameters:
  16. 16/// - `img` - 输入图像 Input image
  17. 17///
  18. 18/// 返回 Return:
  19. 19/// - QOI 字节流 QOI byte stream
  20. 20///
  21. 21/// 错误 Errors:
  22. 22/// - `LoadError::EncodeFailed` - channels 不为 3 或 4 Channels not 3 or 4
  23. 23///
  24. 24/// ```mbt check
  25. 25/// test "doc_encode_qoi_pure" {
  26. 26/// try {
  27. 27/// let data = Bytes::from_array([b'\xff', b'\x00', b'\x00'])
  28. 28/// let img = @types.Image::{ width: 1, height: 1, channels: 3, data, }
  29. 29/// let encoded = @codec.encode_qoi_pure(img)
  30. 30/// assert_true(encoded.length() > 0)
  31. 31/// let decoded = @codec.decode_qoi_pure(encoded)
  32. 32/// assert_eq(decoded.width, 1)
  33. 33/// } catch {
  34. 34/// _ => assert_true(false)
  35. 35/// }
  36. 36/// }
  37. 37/// ```
  38. 38pub fn encode_qoi_pure(img : @types.Image) -> Bytes raise @types.LoadError {
  39. 39● if img.channels != 3 && img.channels != 4 {
  40. 40● raise @types.LoadError::EncodeFailed(
  41. 41 "QOI 编码需要 channels 为 3 (RGB) 或 4 (RGBA)",
  42. 42 )
  43. 43 }
  44. 44 let out : Array[Byte] = []
  45. 45● out.push(b'\x71')
  46. 46● out.push(b'\x6F')
  47. 47● out.push(b'\x69')
  48. 48● out.push(b'\x66')
  49. 49● out.push(((img.width >> 24) & 0xFF).to_byte())
  50. 50● out.push(((img.width >> 16) & 0xFF).to_byte())
  51. 51● out.push(((img.width >> 8) & 0xFF).to_byte())
  52. 52● out.push((img.width & 0xFF).to_byte())
  53. 53● out.push(((img.height >> 24) & 0xFF).to_byte())
  54. 54● out.push(((img.height >> 16) & 0xFF).to_byte())
  55. 55● out.push(((img.height >> 8) & 0xFF).to_byte())
  56. 56● out.push((img.height & 0xFF).to_byte())
  57. 57● out.push(img.channels.to_byte())
  58. 58● out.push(b'\x00')
  59. 59● let index_r : Array[Int32] = Array::make(64, 0)
  60. 60● let index_g : Array[Int32] = Array::make(64, 0)
  61. 61● let index_b : Array[Int32] = Array::make(64, 0)
  62. 62● let index_a : Array[Int32] = Array::make(64, 0)
  63. 63 let prev_r = Ref(0)
  64. 64 let prev_g = Ref(0)
  65. 65 let prev_b = Ref(0)
  66. 66 let prev_a = Ref(255)
  67. 67 let has_alpha = img.channels == 4
  68. 68 let pixel_count = img.width * img.height
  69. 69 let run = Ref(0)
  70. 70 for i in 0..<pixel_count {
  71. 71● let r = img.data[i * img.channels].to_int()
  72. 72● let g = img.data[i * img.channels + 1].to_int()
  73. 73● let b = img.data[i * img.channels + 2].to_int()
  74. 74 let a = if has_alpha {
  75. 75● img.data[i * img.channels + 3].to_int()
  76. 76 } else {
  77. 77● 255
  78. 78 }
  79. 79 if r == prev_r.val && g == prev_g.val && b == prev_b.val && a == prev_a.val {
  80. 80● run.val += 1
  81. 81 if run.val == 62 {
  82. 82○ out.push((0xC0 | (run.val - 1)).to_byte())
  83. 83 run.val = 0
  84. 84 }
  85. 85 } else {
  86. 86● if run.val > 0 {
  87. 87● out.push((0xC0 | (run.val - 1)).to_byte())
  88. 88 run.val = 0
  89. 89 }
  90. 90● let h = qoi_hash(r, g, b, a)
  91. 91 if index_r[h] == r &&
  92. 92 index_g[h] == g &&
  93. 93 index_b[h] == b &&
  94. 94 index_a[h] == a {
  95. 95● out.push((h & 0x3F).to_byte())
  96. 96● } else if a == prev_a.val {
  97. 97● let dr = r - prev_r.val
  98. 98 let dg = g - prev_g.val
  99. 99 let db = b - prev_b.val
  100. 100 let dr_dg = dr - dg
  101. 101 let db_dg = db - dg
  102. 102 if dr >= -2 && dr <= 1 && dg >= -2 && dg <= 1 && db >= -2 && db <= 1 {
  103. 103● out.push(
  104. 104● (0x40 | ((dr + 2) << 4) | ((dg + 2) << 2) | (db + 2)).to_byte(),
  105. 105 )
  106. 106● } else if dg >= -32 &&
  107. 107 dg <= 31 &&
  108. 108 dr_dg >= -8 &&
  109. 109 dr_dg <= 7 &&
  110. 110 db_dg >= -8 &&
  111. 111 db_dg <= 7 {
  112. 112● out.push((0x80 | ((dg + 32) & 0x3F)).to_byte())
  113. 113● out.push((((dr_dg + 8) << 4) | (db_dg + 8)).to_byte())
  114. 114 } else {
  115. 115● out.push(b'\xFE')
  116. 116● out.push(r.to_byte())
  117. 117● out.push(g.to_byte())
  118. 118● out.push(b.to_byte())
  119. 119 }
  120. 120 } else {
  121. 121● out.push(b'\xFF')
  122. 122● out.push(r.to_byte())
  123. 123● out.push(g.to_byte())
  124. 124● out.push(b.to_byte())
  125. 125● out.push(a.to_byte())
  126. 126 }
  127. 127 index_r[h] = r
  128. 128 index_g[h] = g
  129. 129 index_b[h] = b
  130. 130 index_a[h] = a
  131. 131 }
  132. 132 prev_r.val = r
  133. 133 prev_g.val = g
  134. 134 prev_b.val = b
  135. 135 prev_a.val = a
  136. 136 }
  137. 137 if run.val > 0 {
  138. 138● out.push((0xC0 | (run.val - 1)).to_byte())
  139. 139 }
  140. 140 for i in 0..<8 {
  141. 141● if i == 7 {
  142. 142● out.push(b'\x01')
  143. 143 } else {
  144. 144● out.push(b'\x00')
  145. 145 }
  146. 146 }
  147. 147● Bytes::from_array(out)
  148. 148}