image.codec\codec\webp_extended.mbt

70.3% · 45 / 64 points · 166 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/// # WebP 扩展 WebP Extended
  3. 3///
  4. 4/// WebP 扩展功能 WebP extended features
  5. 5
  6. 6///|
  7. 7/// WebP 扩展信息(包含格式类型、alpha、动画、元数据标志等)
  8. 8pub struct WebpInfoExtended {
  9. 9 width : Int32
  10. 10 height : Int32
  11. 11 format : String
  12. 12 has_alpha : Bool
  13. 13 has_animation : Bool
  14. 14 has_icc_profile : Bool
  15. 15 has_exif : Bool
  16. 16 has_xmp : Bool
  17. 17 num_channels : Int32
  18. 18}
  19. 19
  20. 20///|
  21. 21/// 检测数据是否为 WebP 格式
  22. 22pub fn is_webp_format(data : Bytes) -> Bool {
  23. 23● let len = data.length()
  24. 24 if len < 12 {
  25. 25● return false
  26. 26 }
  27. 27 // RIFF magic
  28. 28● if data[0].to_int() != 0x52 ||
  29. 29● data[1].to_int() != 0x49 ||
  30. 30● data[2].to_int() != 0x46 ||
  31. 31● data[3].to_int() != 0x46 {
  32. 32● return false
  33. 33 }
  34. 34 // WEBP fourcc
  35. 35● data[8].to_int() == 0x57 &&
  36. 36● data[9].to_int() == 0x45 &&
  37. 37● data[10].to_int() == 0x42 &&
  38. 38● data[11].to_int() == 0x50
  39. 39}
  40. 40
  41. 41///|
  42. 42/// 解析 WebP 扩展头部信息
  43. 43///
  44. 44/// 遍历 RIFF chunk 结构,提取格式类型、尺寸、alpha、动画、ICC/EXIF/XMP 标志
  45. 45pub fn webp_info_extended(
  46. 46 data : Bytes,
  47. 47) -> WebpInfoExtended raise @types.LoadError {
  48. 48● if !is_webp_format(data) {
  49. 49● raise @types.LoadError::UnsupportedFormat("Not a WebP file")
  50. 50 }
  51. 51● let len = data.length()
  52. 52 let mut width = 0
  53. 53 let mut height = 0
  54. 54 let mut format = "unknown"
  55. 55 let mut has_alpha = false
  56. 56 let mut has_animation = false
  57. 57 let mut has_icc_profile = false
  58. 58 let mut has_exif = false
  59. 59 let mut has_xmp = false
  60. 60 // 遍历 RIFF chunks
  61. 61 let mut pos = 12
  62. 62 while pos + 8 <= len {
  63. 63● let chunk_id = read_4cc(data, pos)
  64. 64● let chunk_size = data[pos + 4].to_int() |
  65. 65● (data[pos + 5].to_int() << 8) |
  66. 66● (data[pos + 6].to_int() << 16) |
  67. 67● (data[pos + 7].to_int() << 24)
  68. 68 if chunk_size < 0 || pos + 8 + chunk_size > len {
  69. 69○ break
  70. 70 }
  71. 71 let chunk_data_start = pos + 8
  72. 72● match chunk_id {
  73. 73● "VP8X" => {
  74. 74 format = "extended"
  75. 75 if chunk_data_start + 10 <= len {
  76. 76● let flags = data[chunk_data_start].to_int()
  77. 77 has_alpha = (flags & 0x10) != 0
  78. 78 has_animation = (flags & 0x20) != 0
  79. 79 has_icc_profile = (flags & 0x01) != 0
  80. 80 has_exif = (flags & 0x08) != 0
  81. 81 has_xmp = (flags & 0x04) != 0
  82. 82 // canvas width/height: 24-bit LE + 1
  83. 83● width = data[chunk_data_start + 4].to_int() |
  84. 84● (data[chunk_data_start + 5].to_int() << 8) |
  85. 85● (data[chunk_data_start + 6].to_int() << 16)
  86. 86 width += 1
  87. 87● height = data[chunk_data_start + 7].to_int() |
  88. 88● (data[chunk_data_start + 8].to_int() << 8) |
  89. 89● (data[chunk_data_start + 9].to_int() << 16)
  90. 90 height += 1
  91. 91 }
  92. 92 }
  93. 93○ "VP8 " => {
  94. 94 if format != "extended" {
  95. 95○ format = "lossy"
  96. 96 }
  97. 97 // VP8 keyframe: 3 bytes tag + 3 bytes start code + 14 bits width + 2 bits + 14 bits height
  98. 98 if chunk_data_start + 10 <= len && width == 0 {
  99. 99○ let tag = data[chunk_data_start].to_int() |
  100. 100○ (data[chunk_data_start + 1].to_int() << 8) |
  101. 101○ (data[chunk_data_start + 2].to_int() << 16)
  102. 102 let is_keyframe = (tag & 1) == 0
  103. 103 if is_keyframe {
  104. 104 // start code: 0x9d 0x01 0x2a
  105. 105○ if chunk_data_start + 9 <= len {
  106. 106○ let w = data[chunk_data_start + 6].to_int() |
  107. 107○ (data[chunk_data_start + 7].to_int() << 8)
  108. 108○ let h = data[chunk_data_start + 8].to_int() |
  109. 109○ (data[chunk_data_start + 9].to_int() << 8)
  110. 110 width = w & 0x3FFF
  111. 111 height = h & 0x3FFF
  112. 112 }
  113. 113 }
  114. 114 }
  115. 115 }
  116. 116● "VP8L" => {
  117. 117 if format != "extended" {
  118. 118● format = "lossless"
  119. 119 }
  120. 120 // VP8L: 0x2F signature + 14 bits width + 14 bits height + 1 bit alpha
  121. 121 if chunk_data_start + 5 <= len && width == 0 {
  122. 122● if data[chunk_data_start].to_int() == 0x2F {
  123. 123● let b1 = data[chunk_data_start + 1].to_int()
  124. 124● let b2 = data[chunk_data_start + 2].to_int()
  125. 125● let b3 = data[chunk_data_start + 3].to_int()
  126. 126● let b4 = data[chunk_data_start + 4].to_int()
  127. 127 // width: 14 bits LE from b1 (8 bits) + b2 (6 bits)
  128. 128 width = (b1 | ((b2 & 0x3F) << 8)) + 1
  129. 129 // height: 14 bits from b2 (2 bits) + b3 (8 bits) + b4 (4 bits)
  130. 130 height = (((b2 >> 6) & 0x03) | (b3 << 2) | ((b4 & 0x0F) << 10)) + 1
  131. 131 // alpha: 1 bit from b4 bit 4
  132. 132 if format != "extended" {
  133. 133● has_alpha = ((b4 >> 4) & 1) == 1
  134. 134 }
  135. 135 }
  136. 136 }
  137. 137 }
  138. 138○ "ANIM" => has_animation = true
  139. 139○ "ICCP" => has_icc_profile = true
  140. 140○ "EXIF" => has_exif = true
  141. 141○ "XMP " => has_xmp = true
  142. 142○ _ => ()
  143. 143 }
  144. 144 pos += 8 + chunk_size
  145. 145 if chunk_size % 2 == 1 {
  146. 146● pos += 1 // padding
  147. 147 }
  148. 148 }
  149. 149 if width == 0 || height == 0 {
  150. 150○ raise @types.LoadError::DecodeFailed(
  151. 151 "WebP: could not determine image dimensions",
  152. 152 )
  153. 153 }
  154. 154● let num_channels = if has_alpha { 4 } else { 3 }
  155. 155 WebpInfoExtended::{
  156. 156 width,
  157. 157 height,
  158. 158 format,
  159. 159 has_alpha,
  160. 160 has_animation,
  161. 161 has_icc_profile,
  162. 162 has_exif,
  163. 163 has_xmp,
  164. 164 num_channels,
  165. 165 }
  166. 166}