image.codec\codec\bmp_decode.mbt

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