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