image.codec\codec\heif_extended.mbt

82% · 41 / 50 points · 161 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/// # HEIF 扩展 HEIF Extended
  3. 3///
  4. 4/// HEIF 扩展功能 HEIF extended features
  5. 5
  6. 6///|
  7. 7/// HEIF 扩展信息(包含色彩空间、旋转、HEVC 编码配置等)
  8. 8pub struct HeifInfoExtended {
  9. 9 width : Int32
  10. 10 height : Int32
  11. 11 bit_depth : Int32
  12. 12 num_channels : Int32
  13. 13 has_alpha : Bool
  14. 14 rotation : Int32
  15. 15 mirror : Int32
  16. 16 color_type : String
  17. 17 color_primaries : Int32
  18. 18 transfer_characteristics : Int32
  19. 19 matrix_coefficients : Int32
  20. 20 full_range : Bool
  21. 21 hevc_profile : Int32
  22. 22 hevc_level : Int32
  23. 23 hevc_tier : Int32
  24. 24 hevc_chroma_format : Int32
  25. 25 hevc_bit_depth_luma : Int32
  26. 26 hevc_bit_depth_chroma : Int32
  27. 27}
  28. 28
  29. 29///|
  30. 30/// 解析 HEIF 扩展头部信息
  31. 31///
  32. 32/// 递归遍历 ISOBMFF box 结构,提取色彩空间、旋转、镜像、HEVC 编码配置等
  33. 33pub fn heif_info_extended(
  34. 34 data : Bytes,
  35. 35) -> HeifInfoExtended raise @types.LoadError {
  36. 36● if !is_heif_format(data) {
  37. 37● raise @types.LoadError::UnsupportedFormat("Not a HEIF file")
  38. 38 }
  39. 39● let len = data.length()
  40. 40 let mut width = 0
  41. 41 let mut height = 0
  42. 42 let mut bit_depth = 8
  43. 43 let mut has_alpha = false
  44. 44 let mut rotation = 0
  45. 45 let mut mirror = 0
  46. 46 let mut color_type = "unknown"
  47. 47 let mut color_primaries = 0
  48. 48 let mut transfer_characteristics = 0
  49. 49 let mut matrix_coefficients = 0
  50. 50 let mut full_range = false
  51. 51 let mut hevc_profile = 0
  52. 52 let mut hevc_level = 0
  53. 53 let mut hevc_tier = 0
  54. 54 let mut hevc_chroma_format = 0
  55. 55 let mut hevc_bit_depth_luma = 8
  56. 56 let mut hevc_bit_depth_chroma = 8
  57. 57● heif_scan_boxes(data, 0, len, fn(box_off, _box_size, box_type) {
  58. 58● if box_type == "ispe" && box_off + 20 <= len {
  59. 59● width = read_u32_be(data, box_off + 12)
  60. 60● height = read_u32_be(data, box_off + 16)
  61. 61 }
  62. 62 if box_type == "auxC" {
  63. 63○ has_alpha = true
  64. 64 }
  65. 65 if box_type == "pixi" && box_off + 13 <= len {
  66. 66● let num_ch = data[box_off + 12].to_int()
  67. 67 if num_ch > 0 && box_off + 13 + num_ch <= len {
  68. 68● bit_depth = data[box_off + 13].to_int()
  69. 69 }
  70. 70 }
  71. 71 if box_type == "irot" && box_off + 9 <= len {
  72. 72● let angle_bits = data[box_off + 8].to_int() & 0x03
  73. 73 rotation = angle_bits * 90
  74. 74 }
  75. 75 if box_type == "imir" && box_off + 9 <= len {
  76. 76○ mirror = data[box_off + 8].to_int() & 0x01
  77. 77 }
  78. 78 if box_type == "colr" && box_off + 16 <= len {
  79. 79● let ctype = read_4cc(data, box_off + 8)
  80. 80 if ctype == "nclx" && box_off + 19 <= len {
  81. 81● color_type = "nclx"
  82. 82● color_primaries = read_u16_be(data, box_off + 12)
  83. 83● transfer_characteristics = read_u16_be(data, box_off + 14)
  84. 84● matrix_coefficients = read_u16_be(data, box_off + 16)
  85. 85● full_range = (data[box_off + 18].to_int() & 0x80) != 0
  86. 86○ } else if ctype == "rICC" {
  87. 87○ color_type = "rICC"
  88. 88○ } else if ctype == "prof" {
  89. 89○ color_type = "prof"
  90. 90 }
  91. 91 }
  92. 92 if box_type == "hvcC" && box_off + 22 <= len {
  93. 93● let b1 = data[box_off + 9].to_int()
  94. 94 hevc_tier = (b1 >> 6) & 0x01
  95. 95 hevc_profile = b1 & 0x1F
  96. 96● hevc_level = data[box_off + 13].to_int()
  97. 97● let chroma_byte = data[box_off + 21].to_int()
  98. 98 hevc_chroma_format = chroma_byte & 0x03
  99. 99 if box_off + 23 <= len {
  100. 100● hevc_bit_depth_luma = (data[box_off + 22].to_int() & 0x07) + 8
  101. 101 }
  102. 102 if box_off + 24 <= len {
  103. 103● hevc_bit_depth_chroma = (data[box_off + 23].to_int() & 0x07) + 8
  104. 104 }
  105. 105 }
  106. 106 })
  107. 107 if width == 0 || height == 0 {
  108. 108● raise @types.LoadError::DecodeFailed(
  109. 109 "HEIF: could not determine image dimensions",
  110. 110 )
  111. 111 }
  112. 112◐ let num_channels = if has_alpha { 4 } else { 3 }
  113. 113 HeifInfoExtended::{
  114. 114 width,
  115. 115 height,
  116. 116 bit_depth,
  117. 117 num_channels,
  118. 118 has_alpha,
  119. 119 rotation,
  120. 120 mirror,
  121. 121 color_type,
  122. 122 color_primaries,
  123. 123 transfer_characteristics,
  124. 124 matrix_coefficients,
  125. 125 full_range,
  126. 126 hevc_profile,
  127. 127 hevc_level,
  128. 128 hevc_tier,
  129. 129 hevc_chroma_format,
  130. 130 hevc_bit_depth_luma,
  131. 131 hevc_bit_depth_chroma,
  132. 132 }
  133. 133}
  134. 134
  135. 135///|
  136. 136fn heif_scan_boxes(
  137. 137 data : Bytes,
  138. 138 start : Int32,
  139. 139 end : Int32,
  140. 140 callback : (Int32, Int32, String) -> Unit,
  141. 141) -> Unit {
  142. 142● let mut offset = start
  143. 143 while offset + 8 <= end {
  144. 144● let box_size = read_u32_be(data, offset)
  145. 145 if box_size < 8 || box_size > end - offset {
  146. 146○ break
  147. 147 }
  148. 148● let btype = box_type_str(data, offset)
  149. 149● callback(offset, box_size, btype)
  150. 150 if btype == "meta" ||
  151. 151 btype == "iprp" ||
  152. 152 btype == "ipco" ||
  153. 153 btype == "moov" ||
  154. 154 btype == "trak" ||
  155. 155 btype == "mdia" {
  156. 156● let child_start = if btype == "meta" { offset + 12 } else { offset + 8 }
  157. 157● heif_scan_boxes(data, child_start, offset + box_size, callback)
  158. 158 }
  159. 159 offset += box_size
  160. 160 }
  161. 161}