image.codec\codec\apng_extended.mbt

78.8% · 41 / 52 points · 137 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/// # APNG 扩展 APNG Extended
  3. 3///
  4. 4/// APNG 扩展功能 APNG extended features
  5. 5
  6. 6///|
  7. 7/// APNG 扩展信息(包含帧数、播放次数、动画标志等)
  8. 8pub struct ApngInfoExtended {
  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 num_frames : Int32
  17. 17 num_plays : Int32
  18. 18 is_animated : Bool
  19. 19}
  20. 20
  21. 21///|
  22. 22fn apng_color_type_name(ct : Int32) -> String {
  23. 23● match ct {
  24. 24○ 0 => "Grayscale"
  25. 25● 2 => "RGB"
  26. 26○ 3 => "Palette"
  27. 27○ 4 => "Grayscale+Alpha"
  28. 28● 6 => "RGBA"
  29. 29○ _ => "unknown"
  30. 30 }
  31. 31}
  32. 32
  33. 33///|
  34. 34fn apng_channels_count(ct : Int32) -> Int32 {
  35. 35● match ct {
  36. 36○ 0 => 1
  37. 37● 2 => 3
  38. 38○ 3 => 1
  39. 39○ 4 => 2
  40. 40● 6 => 4
  41. 41○ _ => 0
  42. 42 }
  43. 43}
  44. 44
  45. 45///|
  46. 46/// 检测数据是否为 APNG 格式(PNG signature + 含 acTL chunk)
  47. 47pub fn is_apng_format(data : Bytes) -> Bool {
  48. 48● let len = data.length()
  49. 49 if len < 8 {
  50. 50● return false
  51. 51 }
  52. 52● if !is_png_signature(data, 0) {
  53. 53● return false
  54. 54 }
  55. 55 let mut pos = 8
  56. 56 while pos + 8 <= len {
  57. 57● let chunk_len = read_u32_be(data, pos)
  58. 58● let b0 = data[pos + 4].to_int()
  59. 59● let b1 = data[pos + 5].to_int()
  60. 60● let b2 = data[pos + 6].to_int()
  61. 61● let b3 = data[pos + 7].to_int()
  62. 62 // 检查 "acTL" chunk
  63. 63 if b0 == 0x61 && b1 == 0x63 && b2 == 0x54 && b3 == 0x4C {
  64. 64● return true
  65. 65 }
  66. 66 if chunk_len < 0 || chunk_len > len - pos - 8 {
  67. 67● break
  68. 68 }
  69. 69 pos += 8 + chunk_len + 4
  70. 70 }
  71. 71 false
  72. 72}
  73. 73
  74. 74///|
  75. 75/// 解析 APNG 扩展头部信息
  76. 76///
  77. 77/// 遍历 PNG chunks,提取 IHDR(尺寸/位深/颜色类型) + acTL(帧数/播放次数)
  78. 78pub fn apng_info_extended(
  79. 79 data : Bytes,
  80. 80) -> ApngInfoExtended raise @types.LoadError {
  81. 81● let len = data.length()
  82. 82 if len < 8 {
  83. 83● raise @types.LoadError::DecodeFailed("APNG 数据过短")
  84. 84 }
  85. 85● if !is_png_signature(data, 0) {
  86. 86○ raise @types.LoadError::UnsupportedFormat("Not a PNG/APNG file")
  87. 87 }
  88. 88 let mut pos = 8
  89. 89 let mut width = 0
  90. 90 let mut height = 0
  91. 91 let mut bit_depth = 0
  92. 92 let mut color_type = 0
  93. 93 let mut num_frames = 0
  94. 94 let mut num_plays = 0
  95. 95 let mut found_ihdr = false
  96. 96 while pos + 8 <= len {
  97. 97● let chunk_len = read_u32_be(data, pos)
  98. 98● let b0 = data[pos + 4].to_int()
  99. 99● let b1 = data[pos + 5].to_int()
  100. 100● let b2 = data[pos + 6].to_int()
  101. 101● let b3 = data[pos + 7].to_int()
  102. 102 if pos + 8 + chunk_len + 4 > len {
  103. 103○ break
  104. 104 }
  105. 105 // IHDR
  106. 106 if b0 == 0x49 && b1 == 0x48 && b2 == 0x44 && b3 == 0x52 {
  107. 107● width = read_u32_be(data, pos + 8)
  108. 108● height = read_u32_be(data, pos + 12)
  109. 109● bit_depth = data[pos + 16].to_int()
  110. 110● color_type = data[pos + 17].to_int()
  111. 111 found_ihdr = true
  112. 112 }
  113. 113 // acTL
  114. 114 if b0 == 0x61 && b1 == 0x63 && b2 == 0x54 && b3 == 0x4C {
  115. 115● num_frames = read_u32_be(data, pos + 8)
  116. 116● num_plays = read_u32_be(data, pos + 12)
  117. 117 }
  118. 118 pos += 8 + chunk_len + 4
  119. 119 }
  120. 120 if !found_ihdr || width == 0 || height == 0 {
  121. 121○ raise @types.LoadError::DecodeFailed("APNG IHDR 不存在或尺寸无效")
  122. 122 }
  123. 123● let num_channels = apng_channels_count(color_type)
  124. 124 let has_alpha = color_type == 4 || color_type == 6
  125. 125 ApngInfoExtended::{
  126. 126 width,
  127. 127 height,
  128. 128 bit_depth,
  129. 129 color_type,
  130. 130● color_type_name: apng_color_type_name(color_type),
  131. 131 num_channels,
  132. 132 has_alpha,
  133. 133 num_frames,
  134. 134 num_plays,
  135. 135 is_animated: num_frames > 1,
  136. 136 }
  137. 137}