image.codec\codec\heif_decode.mbt

82.1% · 32 / 39 points · 143 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 Decode
  3. 3///
  4. 4/// HEIF 图像格式解码 HEIF image format decoding
  5. 5
  6. 6///|
  7. 7/// HEIF (High Efficiency Image File Format) 解码器
  8. 8///
  9. 9/// HEIF 基于 HEVC (H.265) 编码,使用 ISOBMFF 容器格式。
  10. 10/// 本模块提供格式检测和基本头部解析,完整解码需 HEVC 解码器支持。
  11. 11///
  12. 12/// Magic bytes: 0x00 0x00 0x00 0x?? 0x66 0x74 0x79 0x70 0x68 0x65 0x69 0x63(ftyp box,brand 为 "heic"、"heix"、"hevc" 或 "mif1")
  13. 13
  14. 14///|
  15. 15/// HEIF 图像信息
  16. 16pub struct HeifInfo {
  17. 17 width : Int32
  18. 18 height : Int32
  19. 19 bit_depth : Int32
  20. 20 num_channels : Int32
  21. 21 has_alpha : Bool
  22. 22}
  23. 23
  24. 24///|
  25. 25/// 检测数据是否为 HEIF 格式
  26. 26///
  27. 27/// HEIF 文件以 ftyp box 开头,brand 为 "heic", "heix", "hevc", "hevm", "heim", "heis", "mif1" 等
  28. 28pub fn is_heif_format(data : Bytes) -> Bool {
  29. 29● let len = data.length()
  30. 30 if len < 12 {
  31. 31● return false
  32. 32 }
  33. 33 // 检查 ftyp box: bytes 4-7 = "ftyp"
  34. 34 if data[4] != b'f' || data[5] != b't' || data[6] != b'y' || data[7] != b'p' {
  35. 35● return false
  36. 36 }
  37. 37 let b8 = data[8]
  38. 38 let b9 = data[9]
  39. 39 let b10 = data[10]
  40. 40 let b11 = data[11]
  41. 41 // "heic" = 0x68 0x65 0x69 0x63
  42. 42 if b8 == b'h' && b9 == b'e' && b10 == b'i' && b11 == b'c' {
  43. 43● return true
  44. 44 }
  45. 45 // "heix" = 0x68 0x65 0x69 0x78
  46. 46 if b8 == b'h' && b9 == b'e' && b10 == b'i' && b11 == b'x' {
  47. 47● return true
  48. 48 }
  49. 49 // "hevc" = 0x68 0x65 0x76 0x63
  50. 50 if b8 == b'h' && b9 == b'e' && b10 == b'v' && b11 == b'c' {
  51. 51● return true
  52. 52 }
  53. 53 // "mif1" = 0x6D 0x69 0x66 0x31
  54. 54 if b8 == b'm' && b9 == b'i' && b10 == b'f' && b11 == b'1' {
  55. 55● return true
  56. 56 }
  57. 57 false
  58. 58}
  59. 59
  60. 60///|
  61. 61/// 解析 HEIF 头部信息
  62. 62///
  63. 63/// 解析 ISOBMFF box 结构,提取图像尺寸和位深信息。
  64. 64pub fn heif_info(data : Bytes) -> HeifInfo raise @types.LoadError {
  65. 65● if !is_heif_format(data) {
  66. 66● raise @types.LoadError::UnsupportedFormat("Not a HEIF file")
  67. 67 }
  68. 68● let len = data.length()
  69. 69 let mut offset = 0
  70. 70 let mut width = 0
  71. 71 let mut height = 0
  72. 72 let mut bit_depth = 8
  73. 73 let mut has_alpha = false
  74. 74 // 遍历 ISOBMFF boxes
  75. 75 while offset + 8 <= len {
  76. 76● let box_size = (data[offset].to_int() << 24) |
  77. 77● (data[offset + 1].to_int() << 16) |
  78. 78● (data[offset + 2].to_int() << 8) |
  79. 79● data[offset + 3].to_int()
  80. 80 if box_size < 8 || box_size > len - offset {
  81. 81● break
  82. 82 }
  83. 83 let box_type_0 = data[offset + 4]
  84. 84 let box_type_1 = data[offset + 5]
  85. 85 let box_type_2 = data[offset + 6]
  86. 86 let box_type_3 = data[offset + 7]
  87. 87 // 检查 ispe box (ImageSpatialExtentsProperty)
  88. 88 if box_type_0 == b'i' &&
  89. 89 box_type_1 == b's' &&
  90. 90 box_type_2 == b'p' &&
  91. 91 box_type_3 == b'e' {
  92. 92● if offset + 20 <= len {
  93. 93● width = (data[offset + 12].to_int() << 24) |
  94. 94● (data[offset + 13].to_int() << 16) |
  95. 95● (data[offset + 14].to_int() << 8) |
  96. 96● data[offset + 15].to_int()
  97. 97● height = (data[offset + 16].to_int() << 24) |
  98. 98● (data[offset + 17].to_int() << 16) |
  99. 99● (data[offset + 18].to_int() << 8) |
  100. 100● data[offset + 19].to_int()
  101. 101 }
  102. 102 }
  103. 103 // 检查 auxC box for alpha
  104. 104 if box_type_0 == b'a' &&
  105. 105 box_type_1 == b'u' &&
  106. 106 box_type_2 == b'x' &&
  107. 107 box_type_3 == b'C' {
  108. 108○ has_alpha = true
  109. 109 }
  110. 110 // 检查 pixi box for bit depth
  111. 111 if box_type_0 == b'p' &&
  112. 112 box_type_1 == b'i' &&
  113. 113 box_type_2 == b'x' &&
  114. 114 box_type_3 == b'i' {
  115. 115○ if offset + 13 <= len {
  116. 116○ let num_ch = data[offset + 12].to_int()
  117. 117 if num_ch > 0 && offset + 13 + num_ch <= len {
  118. 118○ bit_depth = data[offset + 13].to_int()
  119. 119 }
  120. 120 }
  121. 121 }
  122. 122 offset += box_size
  123. 123 }
  124. 124 if width == 0 || height == 0 {
  125. 125● raise @types.LoadError::DecodeFailed(
  126. 126 "HEIF: could not determine image dimensions",
  127. 127 )
  128. 128 }
  129. 129◐ let num_channels = if has_alpha { 4 } else { 3 }
  130. 130 HeifInfo::{ width, height, bit_depth, num_channels, has_alpha, }
  131. 131}
  132. 132
  133. 133///|
  134. 134/// 解码 HEIF 图像
  135. 135///
  136. 136/// 注意:完整 HEIF 解码需要 HEVC 解码器,当前返回 UnsupportedFormat。
  137. 137/// 使用 heif_info 获取元数据。
  138. 138pub fn heif_decode(data : Bytes) -> @types.Image raise @types.LoadError {
  139. 139● let info = heif_info(data)
  140. 140 raise @types.LoadError::UnsupportedFormat(
  141. 141 "HEIF decode not yet implemented (HEVC decoder required). Image is \{info.width}x\{info.height} \{info.bit_depth}-bit",
  142. 142 )
  143. 143}