image.codec\codec\jpegxl_extended.mbt

71.9% · 41 / 57 points · 160 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/// # JPEG-XL 扩展 JPEG-XL Extended
  3. 3///
  4. 4/// JPEG-XL 扩展功能 JPEG-XL extended features
  5. 5
  6. 6///|
  7. 7/// JPEG-XL 扩展信息
  8. 8pub struct JpegXlInfoExtended {
  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 is_lossless : Bool
  15. 15 is_codestream : Bool
  16. 16 orientation : Int32
  17. 17}
  18. 18
  19. 19///|
  20. 20/// 位流读取器
  21. 21priv struct JxlBitReader {
  22. 22 data : Bytes
  23. 23 mut byte_pos : Int32
  24. 24 mut bit_pos : Int32
  25. 25}
  26. 26
  27. 27///|
  28. 28fn JxlBitReader::new(data : Bytes, start : Int32) -> JxlBitReader {
  29. 29● JxlBitReader::{ data, byte_pos: start, bit_pos: 0, }
  30. 30}
  31. 31
  32. 32///|
  33. 33fn JxlBitReader::read_bit(self : JxlBitReader) -> Int32 {
  34. 34● if self.byte_pos >= self.data.length() {
  35. 35○ return 0
  36. 36 }
  37. 37● let byte = self.data[self.byte_pos].to_int()
  38. 38 let bit = (byte >> (7 - self.bit_pos)) & 1
  39. 39 self.bit_pos += 1
  40. 40 if self.bit_pos == 8 {
  41. 41● self.bit_pos = 0
  42. 42 self.byte_pos += 1
  43. 43 }
  44. 44 bit
  45. 45}
  46. 46
  47. 47///|
  48. 48fn JxlBitReader::read_bits(self : JxlBitReader, n : Int32) -> Int32 {
  49. 49● let mut result = 0
  50. 50 for _ in 0..<n {
  51. 51● result = (result << 1) | self.read_bit()
  52. 52 }
  53. 53 result
  54. 54}
  55. 55
  56. 56///|
  57. 57fn JxlBitReader::read_u32(
  58. 58 self : JxlBitReader,
  59. 59 d0 : Int32,
  60. 60 d1 : Int32,
  61. 61 d2 : Int32,
  62. 62 d3 : Int32,
  63. 63) -> Int32 {
  64. 64● let selector = self.read_bits(2)
  65. 65● match selector {
  66. 66● 0 => d0
  67. 67○ 1 => d1 + self.read_bits(8)
  68. 68○ 2 => d2 + self.read_bits(16)
  69. 69○ _ => d3 + self.read_bits(24)
  70. 70 }
  71. 71}
  72. 72
  73. 73///|
  74. 74/// 从宽高比计算宽度
  75. 75fn jxl_width_from_ratio(
  76. 76 height : Int32,
  77. 77 ratio : Int32,
  78. 78 reader : JxlBitReader,
  79. 79) -> Int32 {
  80. 80● match ratio {
  81. 81● 0 => height
  82. 82● 1 => height * 4 / 3
  83. 83● 2 => height * 3 / 2
  84. 84○ 3 => height * 16 / 9
  85. 85○ 4 => height * 5 / 4
  86. 86○ 5 => height * 2
  87. 87○ 6 => if height >= 2 { height / 2 } else { 1 }
  88. 88○ _ => reader.read_u32(1, 257, 65793, 0) + 1
  89. 89 }
  90. 90}
  91. 91
  92. 92///|
  93. 93/// 解析 JPEG-XL 裸码流 SizeHeader
  94. 94fn jxl_parse_size_header(data : Bytes, start : Int32) -> (Int32, Int32) {
  95. 95● let reader = JxlBitReader::new(data, start)
  96. 96● let small = reader.read_bit()
  97. 97 let height = if small == 1 {
  98. 98● reader.read_bits(5) + 1
  99. 99 } else {
  100. 100● reader.read_u32(1, 257, 65793, 0) + 1
  101. 101 }
  102. 102● let ratio = reader.read_bits(3)
  103. 103● let width = jxl_width_from_ratio(height, ratio, reader)
  104. 104 (width, height)
  105. 105}
  106. 106
  107. 107///|
  108. 108/// 在 ISOBMFF 容器中查找 codestream box 的偏移
  109. 109fn jxl_find_codestream(data : Bytes) -> Int32 {
  110. 110● let len = data.length()
  111. 111 let mut offset = 0
  112. 112 while offset + 8 <= len {
  113. 113● let box_size = read_u32_be(data, offset)
  114. 114 if box_size < 8 || box_size > len - offset {
  115. 115○ break
  116. 116 }
  117. 117● let btype = box_type_str(data, offset)
  118. 118 // jxlc = codestream box, jxlp = partial codestream
  119. 119 if btype == "jxlc" || btype == "jxlp" {
  120. 120● return offset + 8
  121. 121 }
  122. 122 offset += box_size
  123. 123 }
  124. 124 -1
  125. 125}
  126. 126
  127. 127///|
  128. 128/// 解析 JPEG-XL 扩展头部信息
  129. 129///
  130. 130/// 解析 SizeHeader 提取图像尺寸,支持裸码流和 ISOBMFF 容器两种格式
  131. 131pub fn jpegxl_info_extended(
  132. 132 data : Bytes,
  133. 133) -> JpegXlInfoExtended raise @types.LoadError {
  134. 134● if !is_jpegxl_format(data) {
  135. 135● raise @types.LoadError::UnsupportedFormat("Not a JPEG-XL file")
  136. 136 }
  137. 137● let is_cs = is_jpegxl_codestream(data)
  138. 138 let codestream_start = if is_cs {
  139. 139● 2
  140. 140 } else {
  141. 141● let cs = jxl_find_codestream(data)
  142. 142 if cs < 0 {
  143. 143● raise @types.LoadError::DecodeFailed(
  144. 144 "JPEG-XL: codestream box not found in container",
  145. 145 )
  146. 146 }
  147. 147 cs
  148. 148 }
  149. 149● let (width, height) = jxl_parse_size_header(data, codestream_start)
  150. 150 JpegXlInfoExtended::{
  151. 151 width,
  152. 152 height,
  153. 153 bit_depth: 8,
  154. 154 num_channels: 3,
  155. 155 has_alpha: false,
  156. 156 is_lossless: false,
  157. 157 is_codestream: is_cs,
  158. 158 orientation: 1,
  159. 159 }
  160. 160}