image.codec\codec\qoi_decode.mbt
93.8% · 60 / 64 points · 159 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 Decoder - 3
/// - 4
/// 纯 MoonBit QOI 解码器,支持全部 QOI 标签(INDEX/DIFF/LUMA/RUN/RGB/RGBA)。 Pure MoonBit QOI decoder, supporting all QOI tags (INDEX/DIFF/LUMA/RUN/RGB/RGBA). - 5
/// - 6
/// 核心函数 Core functions: - 7
/// - `decode_qoi_pure` - 解码 QOI 字节流为 Image Decode QOI byte stream to Image - 8
- 9
// 纯 MoonBit QOI 解码器 - 10
// 规范: https://qoiformat.org/qoi-specification.pdf - 11
// 移植自 src/format/qoi.mbt 的 decode_qoi,仅替换类型引用: - 12
// @core.Image -> @types.Image, @core.LoadError -> @types.LoadError - 13
- 14
// QOI 哈希函数: (r*3 + g*5 + b*7 + a*11) % 64 - 15
- 16
///| - 17
fn qoi_hash(r : Int32, g : Int32, b : Int32, a : Int32) -> Int32 { - 18●
(r * 3 + g * 5 + b * 7 + a * 11) % 64 - 19
} - 20
- 21
///| - 22
/// 解码 QOI 字节流为 Image(channels=3 或 4)支持全部 QOI 操作标签 Supports all QOI operation tags - 23
- 24
///| - 25
/// 参数 Parameters: - 26
/// - `data` - QOI 字节流 QOI byte stream - 27
/// - 28
/// 返回 Return: - 29
/// - 解码后的 8-bit Image Decoded 8-bit Image - 30
/// - 31
/// 错误 Errors: - 32
/// - `LoadError::DecodeFailed` - 数据过短、magic 不匹配、尺寸无效 Data too short, magic mismatch, invalid dimensions - 33
/// - 34
/// ```mbt check - 35
/// test "doc_decode_qoi_pure" { - 36
/// try { - 37
/// let img = @codec.decode_qoi_pure(@types.DOC_TEST_QOI_1X1) - 38
/// assert_eq(img.width, 1) - 39
/// assert_eq(img.height, 1) - 40
/// assert_eq(img.channels, 4) - 41
/// } catch { - 42
/// _ => assert_true(false) - 43
/// } - 44
/// } - 45
/// ``` - 46
pub fn decode_qoi_pure(data : Bytes) -> @types.Image raise @types.LoadError { - 47●
let len = data.length() - 48
if len < 14 { - 49●
raise @types.LoadError::DecodeFailed("QOI 数据过短") - 50
} - 51●
if data[0].to_int() != 0x71 || - 52●
data[1].to_int() != 0x6F || - 53●
data[2].to_int() != 0x69 || - 54●
data[3].to_int() != 0x66 { - 55●
raise @types.LoadError::DecodeFailed("QOI magic 不匹配") - 56
} - 57●
let width = (data[4].to_int() << 24) | - 58●
(data[5].to_int() << 16) | - 59●
(data[6].to_int() << 8) | - 60●
data[7].to_int() - 61●
let height = (data[8].to_int() << 24) | - 62●
(data[9].to_int() << 16) | - 63●
(data[10].to_int() << 8) | - 64●
data[11].to_int() - 65●
let channels = data[12].to_int() - 66
if width <= 0 || height <= 0 { - 67●
raise @types.LoadError::DecodeFailed("QOI 尺寸无效") - 68
} - 69●
let out_channels = if channels == 3 { 3 } else { 4 } - 70●
@types.check_dims(width, height, out_channels) - 71
let pixel_count = width * height - 72●
let pixels : Array[Byte] = Array::make(pixel_count * out_channels, b'\x00') - 73●
let index_r : Array[Int32] = Array::make(64, 0) - 74●
let index_g : Array[Int32] = Array::make(64, 0) - 75●
let index_b : Array[Int32] = Array::make(64, 0) - 76●
let index_a : Array[Int32] = Array::make(64, 0) - 77
let px_r = Ref(0) - 78
let px_g = Ref(0) - 79
let px_b = Ref(0) - 80
let px_a = Ref(255) - 81
let pos = Ref(14) - 82
let pixel_idx = Ref(0) - 83
while pixel_idx.val < pixel_count && pos.val < len { - 84●
let tag = data[pos.val].to_int() - 85
pos.val += 1 - 86
if tag == 0xFE { - 87●
if pos.val + 2 >= len { - 88●
raise @types.LoadError::DecodeFailed("QOI 数据截断: OP_RGB") - 89
} - 90●
px_r.val = data[pos.val].to_int() - 91●
px_g.val = data[pos.val + 1].to_int() - 92●
px_b.val = data[pos.val + 2].to_int() - 93
pos.val += 3 - 94●
} else if tag == 0xFF { - 95●
if pos.val + 3 >= len { - 96○
raise @types.LoadError::DecodeFailed("QOI 数据截断: OP_RGBA") - 97
} - 98●
px_r.val = data[pos.val].to_int() - 99●
px_g.val = data[pos.val + 1].to_int() - 100●
px_b.val = data[pos.val + 2].to_int() - 101●
px_a.val = data[pos.val + 3].to_int() - 102
pos.val += 4 - 103●
} else if (tag & 0xC0) == 0x00 { - 104●
let idx = tag & 0x3F - 105
px_r.val = index_r[idx] - 106
px_g.val = index_g[idx] - 107
px_b.val = index_b[idx] - 108
px_a.val = index_a[idx] - 109●
} else if (tag & 0xC0) == 0x40 { - 110●
px_r.val = (px_r.val + ((tag >> 4) & 0x03) - 2) & 0xFF - 111
px_g.val = (px_g.val + ((tag >> 2) & 0x03) - 2) & 0xFF - 112
px_b.val = (px_b.val + (tag & 0x03) - 2) & 0xFF - 113●
} else if (tag & 0xC0) == 0x80 { - 114●
if pos.val >= len { - 115○
raise @types.LoadError::DecodeFailed("QOI 数据截断: OP_LUMA") - 116
} - 117
let dg = (tag & 0x3F) - 32 - 118
px_g.val = (px_g.val + dg) & 0xFF - 119●
let b2 = data[pos.val].to_int() - 120
pos.val += 1 - 121
let dr_dg = (b2 >> 4) - 8 - 122
let db_dg = (b2 & 0x0F) - 8 - 123
px_r.val = (px_r.val + dr_dg + dg) & 0xFF - 124
px_b.val = (px_b.val + db_dg + dg) & 0xFF - 125
} else { - 126●
let run = (tag & 0x3F) + 1 - 127
for _ in 0..<run { - 128●
if pixel_idx.val < pixel_count { - 129●
pixels[pixel_idx.val * out_channels] = px_r.val.to_byte() - 130●
pixels[pixel_idx.val * out_channels + 1] = px_g.val.to_byte() - 131●
pixels[pixel_idx.val * out_channels + 2] = px_b.val.to_byte() - 132
if out_channels == 4 { - 133○
pixels[pixel_idx.val * out_channels + 3] = px_a.val.to_byte() - 134
} - 135
pixel_idx.val += 1 - 136
} - 137
} - 138
continue - 139
} - 140●
pixels[pixel_idx.val * out_channels] = px_r.val.to_byte() - 141●
pixels[pixel_idx.val * out_channels + 1] = px_g.val.to_byte() - 142●
pixels[pixel_idx.val * out_channels + 2] = px_b.val.to_byte() - 143
if out_channels == 4 { - 144●
pixels[pixel_idx.val * out_channels + 3] = px_a.val.to_byte() - 145
} - 146
pixel_idx.val += 1 - 147●
let h = qoi_hash(px_r.val, px_g.val, px_b.val, px_a.val) - 148
index_r[h] = px_r.val - 149
index_g[h] = px_g.val - 150
index_b[h] = px_b.val - 151
index_a[h] = px_a.val - 152
} - 153
@types.Image::{ - 154
width, - 155
height, - 156
channels: out_channels, - 157●
data: Bytes::from_array(pixels), - 158
} - 159
}