image.codec\codec\bmp_decode.mbt
97.3% · 36 / 37 points · 116 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # BMP 解码 BMP Decode - 3
/// - 4
/// BMP 图像格式解码 BMP image format decoding - 5
// 纯 MoonBit BMP 解码器 - 6
// 支持 24-bit / 32-bit 无压缩 BMP(BI_RGB,compression=0) - 7
// 规范: https://en.wikipedia.org/wiki/BMP_file_format - 8
- 9
///| - 10
/// 解码 BMP 字节流为 Image(24-bit -> channels=3, 32-bit -> channels=4)仅支持 BITMAPINFOHEADER(DIB 头 40 字节)+ BI_RGB(无压缩) Decode BMP byte stream to Image (24-bit -> channels=3, 32-bit -> channels=4) Only supports BITMAPINFOHEADER (DIB header 40 bytes) + BI_RGB (uncompressed) - 11
- 12
///| - 13
/// 参数 Parameters: - 14
/// - `data` - BMP 字节流 BMP byte stream - 15
/// - 16
/// 返回 Return: - 17
/// - 解码后的 8-bit Image Decoded 8-bit Image - 18
/// - 19
/// 错误 Errors: - 20
/// - `LoadError::DecodeFailed` - 数据过短、magic 不匹配、不支持的位深/压缩/尺寸、像素越界 Data too short, magic mismatch, unsupported bit depth/compression/dimensions, pixel out of bounds - 21
/// - 22
/// ```mbt check - 23
/// test "doc_decode_bmp_pure" { - 24
/// try { - 25
/// let img = @codec.decode_bmp_pure(@types.DOC_TEST_BMP_1X1) - 26
/// assert_eq(img.width, 1) - 27
/// assert_eq(img.height, 1) - 28
/// assert_eq(img.channels, 3) - 29
/// } catch { - 30
/// _ => assert_true(false) - 31
/// } - 32
/// } - 33
/// ``` - 34
pub fn decode_bmp_pure(data : Bytes) -> @types.Image raise @types.LoadError { - 35●
let len = data.length() - 36
// BMP 文件头 14 字节 + DIB 头 40 字节 = 54 字节最小 - 37
if len < 54 { - 38●
raise @types.LoadError::DecodeFailed("BMP 数据过短") - 39
} - 40
// 检查 magic "BM" - 41●
if data[0].to_int() != 0x42 || data[1].to_int() != 0x4D { - 42●
raise @types.LoadError::DecodeFailed("BMP magic 不匹配") - 43
} - 44●
let data_offset = read_u32_le(data, 10) - 45●
let dib_size = read_u32_le(data, 14) - 46
// 仅支持 BITMAPINFOHEADER(40 字节) - 47
if dib_size != 40 { - 48●
raise @types.LoadError::DecodeFailed( - 49
"不支持的 BMP DIB 头大小,仅支持 BITMAPINFOHEADER(40 字节)", - 50
) - 51
} - 52●
let width = read_u32_le(data, 18) - 53●
let height = read_u32_le(data, 22) - 54●
let bpp = read_u16_le(data, 28) - 55●
let compression = read_u32_le(data, 30) - 56
// 仅支持 24-bit 和 32-bit - 57
if bpp != 24 && bpp != 32 { - 58●
raise @types.LoadError::DecodeFailed( - 59
"不支持的 BMP 位深,仅支持 24-bit 和 32-bit", - 60
) - 61
} - 62
// 仅支持 BI_RGB(compression=0) - 63
if compression != 0 { - 64●
raise @types.LoadError::DecodeFailed( - 65
"不支持的 BMP 压缩方式,仅支持 BI_RGB(无压缩)", - 66
) - 67
} - 68
// 尺寸校验 - 69
if width == 0 || height == 0 { - 70●
raise @types.LoadError::DecodeFailed("BMP 尺寸无效") - 71
} - 72◐
let abs_width = if width < 0 { -width } else { width } - 73●
let abs_height = if height < 0 { -height } else { height } - 74
// 行序:height > 0 自下而上,height < 0 自上而下 - 75
let bottom_up = height > 0 - 76
// 每像素字节数 - 77
let bytes_per_pixel = bpp / 8 - 78
// 行数据字节数(4 字节对齐) - 79
let row_size = (abs_width * bytes_per_pixel + 3) / 4 * 4 - 80
// 输出 channels - 81
let out_channels = bytes_per_pixel - 82●
@types.check_dims(abs_width, abs_height, out_channels) - 83
// 输出像素数据 - 84
let pixel_count = abs_width * abs_height - 85●
let pixels : Array[Byte] = Array::make(pixel_count * out_channels, b'\x00') - 86
// 逐行读取 - 87
for file_row in 0..<abs_height { - 88
// 计算输出行号 - 89●
let out_row = if bottom_up { abs_height - 1 - file_row } else { file_row } - 90
let row_start = data_offset + file_row * row_size - 91
for x in 0..<abs_width { - 92●
let pixel_offset = row_start + x * bytes_per_pixel - 93
if pixel_offset > len - bytes_per_pixel { - 94●
raise @types.LoadError::DecodeFailed("BMP 像素数据越界") - 95
} - 96
// BGR(A) -> RGB(A) - 97●
let b = data[pixel_offset].to_int() - 98●
let g = data[pixel_offset + 1].to_int() - 99●
let r = data[pixel_offset + 2].to_int() - 100
let out_idx = (out_row * abs_width + x) * out_channels - 101●
pixels[out_idx] = r.to_byte() - 102●
pixels[out_idx + 1] = g.to_byte() - 103●
pixels[out_idx + 2] = b.to_byte() - 104
if out_channels == 4 { - 105●
let a = data[pixel_offset + 3].to_int() - 106●
pixels[out_idx + 3] = a.to_byte() - 107
} - 108
} - 109
} - 110
@types.Image::{ - 111
width: abs_width, - 112
height: abs_height, - 113
channels: out_channels, - 114●
data: Bytes::from_array(pixels), - 115
} - 116
}