image.codec\codec\png_extended.mbt

85.1% · 40 / 47 points · 112 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/// # PNG 扩展 PNG Extended
  3. 3///
  4. 4/// PNG 扩展功能 PNG extended features
  5. 5
  6. 6///|
  7. 7/// PNG 扩展信息(包含位深、颜色类型、隔行扫描等)
  8. 8pub struct PngInfoExtended {
  9. 9 width : Int32
  10. 10 height : Int32
  11. 11 bit_depth : Int32
  12. 12 color_type : Int32
  13. 13 color_type_name : String
  14. 14 num_channels : Int32
  15. 15 has_alpha : Bool
  16. 16 is_palette : Bool
  17. 17 compression_method : Int32
  18. 18 filter_method : Int32
  19. 19 interlace_method : Int32
  20. 20}
  21. 21
  22. 22///|
  23. 23/// 检测数据是否为 PNG 格式
  24. 24pub fn is_png_format(data : Bytes) -> Bool {
  25. 25● let len = data.length()
  26. 26 if len < 8 {
  27. 27● return false
  28. 28 }
  29. 29● data[0].to_int() == 0x89 &&
  30. 30● data[1].to_int() == 0x50 &&
  31. 31● data[2].to_int() == 0x4E &&
  32. 32● data[3].to_int() == 0x47 &&
  33. 33● data[4].to_int() == 0x0D &&
  34. 34● data[5].to_int() == 0x0A &&
  35. 35● data[6].to_int() == 0x1A &&
  36. 36● data[7].to_int() == 0x0A
  37. 37}
  38. 38
  39. 39///|
  40. 40fn png_color_type_name(ct : Int32) -> String {
  41. 41● match ct {
  42. 42● 0 => "Grayscale"
  43. 43● 2 => "RGB"
  44. 44● 3 => "Palette"
  45. 45○ 4 => "Grayscale+Alpha"
  46. 46● 6 => "RGBA"
  47. 47○ _ => "unknown"
  48. 48 }
  49. 49}
  50. 50
  51. 51///|
  52. 52fn png_channels_count(ct : Int32) -> Int32 {
  53. 53● match ct {
  54. 54● 0 => 1
  55. 55● 2 => 3
  56. 56● 3 => 1
  57. 57○ 4 => 2
  58. 58● 6 => 4
  59. 59○ _ => 0
  60. 60 }
  61. 61}
  62. 62
  63. 63///|
  64. 64/// 解析 PNG 扩展头部信息
  65. 65///
  66. 66/// 解析 IHDR chunk,提取尺寸、位深、颜色类型、压缩/滤波/隔行方式
  67. 67pub fn png_info_extended(
  68. 68 data : Bytes,
  69. 69) -> PngInfoExtended raise @types.LoadError {
  70. 70● if !is_png_format(data) {
  71. 71● raise @types.LoadError::UnsupportedFormat("Not a PNG file")
  72. 72 }
  73. 73● let len = data.length()
  74. 74 // IHDR chunk starts at offset 8
  75. 75 // length(4) + "IHDR"(4) + width(4) + height(4) + bit_depth(1) + color_type(1) + compression(1) + filter(1) + interlace(1)
  76. 76 if len < 29 {
  77. 77○ raise @types.LoadError::DecodeFailed("PNG 数据过短,无法读取 IHDR")
  78. 78 }
  79. 79 // 验证 IHDR chunk type
  80. 80● if data[12].to_int() != 0x49 ||
  81. 81● data[13].to_int() != 0x48 ||
  82. 82● data[14].to_int() != 0x44 ||
  83. 83● data[15].to_int() != 0x52 {
  84. 84○ raise @types.LoadError::DecodeFailed("PNG IHDR chunk 不存在")
  85. 85 }
  86. 86● let width = read_u32_be(data, 16)
  87. 87● let height = read_u32_be(data, 20)
  88. 88● let bit_depth = data[24].to_int()
  89. 89● let color_type = data[25].to_int()
  90. 90● let compression_method = data[26].to_int()
  91. 91● let filter_method = data[27].to_int()
  92. 92● let interlace_method = data[28].to_int()
  93. 93 if width == 0 || height == 0 {
  94. 94○ raise @types.LoadError::DecodeFailed("PNG 尺寸无效")
  95. 95 }
  96. 96● let num_channels = png_channels_count(color_type)
  97. 97 let has_alpha = color_type == 4 || color_type == 6
  98. 98 let is_palette = color_type == 3
  99. 99 PngInfoExtended::{
  100. 100 width,
  101. 101 height,
  102. 102 bit_depth,
  103. 103 color_type,
  104. 104● color_type_name: png_color_type_name(color_type),
  105. 105 num_channels,
  106. 106 has_alpha,
  107. 107 is_palette,
  108. 108 compression_method,
  109. 109 filter_method,
  110. 110 interlace_method,
  111. 111 }
  112. 112}