image.codec\codec\qoi_extended.mbt

88.9% · 16 / 18 points · 62 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 Extended
  3. 3///
  4. 4/// QOI 扩展功能 QOI extended features
  5. 5
  6. 6///|
  7. 7/// QOI 扩展信息(包含通道数、色彩空间等)
  8. 8pub struct QoiInfoExtended {
  9. 9 width : Int32
  10. 10 height : Int32
  11. 11 channels : Int32
  12. 12 colorspace : Int32
  13. 13 colorspace_name : String
  14. 14 num_channels : Int32
  15. 15 has_alpha : Bool
  16. 16}
  17. 17
  18. 18///|
  19. 19/// 检测数据是否为 QOI 格式
  20. 20pub fn is_qoi_format(data : Bytes) -> Bool {
  21. 21● let len = data.length()
  22. 22 if len < 14 {
  23. 23● return false
  24. 24 }
  25. 25● data[0].to_int() == 0x71 &&
  26. 26● data[1].to_int() == 0x6F &&
  27. 27● data[2].to_int() == 0x69 &&
  28. 28● data[3].to_int() == 0x66
  29. 29}
  30. 30
  31. 31///|
  32. 32/// 解析 QOI 扩展头部信息
  33. 33pub fn qoi_info_extended(
  34. 34 data : Bytes,
  35. 35) -> QoiInfoExtended raise @types.LoadError {
  36. 36● if !is_qoi_format(data) {
  37. 37● raise @types.LoadError::UnsupportedFormat("Not a QOI file")
  38. 38 }
  39. 39● let width = read_u32_be(data, 4)
  40. 40● let height = read_u32_be(data, 8)
  41. 41● let channels = data[12].to_int()
  42. 42● let colorspace = data[13].to_int()
  43. 43 if width == 0 || height == 0 {
  44. 44○ raise @types.LoadError::DecodeFailed("QOI 尺寸无效")
  45. 45 }
  46. 46 if channels != 3 && channels != 4 {
  47. 47○ raise @types.LoadError::DecodeFailed("QOI 通道数无效")
  48. 48 }
  49. 49 QoiInfoExtended::{
  50. 50 width,
  51. 51 height,
  52. 52 channels,
  53. 53 colorspace,
  54. 54 colorspace_name: if colorspace == 0 {
  55. 55● "sRGB with linear alpha"
  56. 56 } else {
  57. 57● "all linear"
  58. 58 },
  59. 59 num_channels: channels,
  60. 60 has_alpha: channels == 4,
  61. 61 }
  62. 62}