image.codec\util\image_info.mbt

93.4% · 171 / 183 points · 373 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/// # 图像信息查询 Image Info Query
  3. 3///
  4. 4/// 纯 MoonBit 图像信息查询(全目标可用),仅依赖 @types,不加载像素数据。
  5. 5/// 通过解析各格式头部获取 width/height/channels。
  6. 6///
  7. 7/// 纯 MoonBit 图像信息查询(全目标),仅依赖 @types,不加载像素数据。通过解析格式头部获取 width/height/channels。 Pure MoonBit image info query (all targets), depends only on @types, does not load pixel data.
  8. 8/// 通过解析格式头部获取 width/height/channels。 Obtains width/height/channels by parsing format headers.
  9. 9///
  10. 10/// 核心函数 Core functions:
  11. 11/// - `load_image_info_pure` - 加载图像信息(不含像素数据)Load image info (no pixel data)
  12. 12/// - `load_png_info` / `load_jpeg_info` / `load_bmp_info` - 格式特化信息查询 Format-specific info query
  13. 13
  14. 14// 纯 MoonBit 图像信息查询(全目标可用:native/wasm/js)
  15. 15// 仅依赖 @types,不依赖 @core/FFI
  16. 16// 通过解析各格式头部获取 width/height/channels,不加载像素数据
  17. 17
  18. 18///|
  19. 19fn info_read_u32_be(data : Bytes, offset : Int32) -> Int32 {
  20. 20● data[offset].to_int() * 16777216 +
  21. 21● data[offset + 1].to_int() * 65536 +
  22. 22● data[offset + 2].to_int() * 256 +
  23. 23● data[offset + 3].to_int()
  24. 24}
  25. 25
  26. 26///|
  27. 27fn info_read_u32_le(data : Bytes, offset : Int32) -> Int32 {
  28. 28● data[offset].to_int() +
  29. 29● data[offset + 1].to_int() * 256 +
  30. 30● data[offset + 2].to_int() * 65536 +
  31. 31● data[offset + 3].to_int() * 16777216
  32. 32}
  33. 33
  34. 34///|
  35. 35fn info_read_u16_be(data : Bytes, offset : Int32) -> Int32 {
  36. 36● data[offset].to_int() * 256 + data[offset + 1].to_int()
  37. 37}
  38. 38
  39. 39///|
  40. 40fn info_read_u16_le(data : Bytes, offset : Int32) -> Int32 {
  41. 41● data[offset].to_int() + data[offset + 1].to_int() * 256
  42. 42}
  43. 43
  44. 44///|
  45. 45/// 从 BMP 头部读取信息
  46. 46fn info_bmp(data : Bytes) -> @types.ImageInfo? {
  47. 47● if data.length() < 30 {
  48. 48● return None
  49. 49 }
  50. 50● let width = info_read_u32_le(data, 18)
  51. 51● let height = info_read_u32_le(data, 22)
  52. 52● let bpp = info_read_u16_le(data, 28)
  53. 53 let channels = bpp / 8
  54. 54 if width <= 0 || height <= 0 || channels <= 0 {
  55. 55● return None
  56. 56 }
  57. 57● Some(@types.ImageInfo::{ width, height: height.abs(), channels, })
  58. 58}
  59. 59
  60. 60///|
  61. 61/// 从 QOI 头部读取信息
  62. 62fn info_qoi(data : Bytes) -> @types.ImageInfo? {
  63. 63● if data.length() < 14 {
  64. 64● return None
  65. 65 }
  66. 66● let width = info_read_u32_be(data, 4)
  67. 67● let height = info_read_u32_be(data, 8)
  68. 68● let channels = data[12].to_int()
  69. 69 if width <= 0 || height <= 0 || channels <= 0 {
  70. 70● return None
  71. 71 }
  72. 72 Some(@types.ImageInfo::{ width, height, channels, })
  73. 73}
  74. 74
  75. 75///|
  76. 76/// 从 PSD 头部读取信息
  77. 77fn info_psd(data : Bytes) -> @types.ImageInfo? {
  78. 78● if data.length() < 16 {
  79. 79● return None
  80. 80 }
  81. 81● let channels = info_read_u16_be(data, 4)
  82. 82● let height = info_read_u32_be(data, 6)
  83. 83● let width = info_read_u32_be(data, 10)
  84. 84 if width <= 0 || height <= 0 || channels <= 0 {
  85. 85● return None
  86. 86 }
  87. 87 Some(@types.ImageInfo::{ width, height, channels, })
  88. 88}
  89. 89
  90. 90///|
  91. 91/// 从 GIF 头部读取信息
  92. 92fn info_gif(data : Bytes) -> @types.ImageInfo? {
  93. 93● if data.length() < 10 {
  94. 94● return None
  95. 95 }
  96. 96● let width = info_read_u16_le(data, 6)
  97. 97● let height = info_read_u16_le(data, 8)
  98. 98 if width <= 0 || height <= 0 {
  99. 99● return None
  100. 100 }
  101. 101 Some(@types.ImageInfo::{ width, height, channels: 3, })
  102. 102}
  103. 103
  104. 104///|
  105. 105/// 从 PNG 头部读取信息(IHDR chunk)
  106. 106fn info_png(data : Bytes) -> @types.ImageInfo? {
  107. 107 // PNG signature: 8 bytes
  108. 108 // IHDR: length(4) + "IHDR"(4) + width(4) + height(4) + bit_depth(1) + color_type(1) + ...
  109. 109 // width at offset 16, height at offset 20
  110. 110● if data.length() < 26 {
  111. 111● return None
  112. 112 }
  113. 113 // 验证 PNG signature
  114. 114● if data[0].to_int() != 0x89 ||
  115. 115● data[1].to_int() != 0x50 ||
  116. 116● data[2].to_int() != 0x4E ||
  117. 117● data[3].to_int() != 0x47 {
  118. 118○ return None
  119. 119 }
  120. 120● let width = info_read_u32_be(data, 16)
  121. 121● let height = info_read_u32_be(data, 20)
  122. 122● let bit_depth = data[24].to_int()
  123. 123● let color_type = data[25].to_int()
  124. 124● let channels = match color_type {
  125. 125● 0 => 1 // gray
  126. 126● 2 => 3 // rgb
  127. 127● 3 => 1 // palette (indexed)
  128. 128● 4 => 2 // gray + alpha
  129. 129● 6 => 4 // rgba
  130. 130● _ => return None
  131. 131 }
  132. 132 if width <= 0 || height <= 0 || bit_depth <= 0 {
  133. 133○ return None
  134. 134 }
  135. 135 Some(@types.ImageInfo::{ width, height, channels, })
  136. 136}
  137. 137
  138. 138///|
  139. 139/// 从 JPEG 头部读取信息(解析 SOF marker)
  140. 140fn info_jpeg(data : Bytes) -> @types.ImageInfo? {
  141. 141● let len = data.length()
  142. 142● if len < 4 || data[0].to_int() != 0xFF || data[1].to_int() != 0xD8 {
  143. 143○ return None
  144. 144 }
  145. 145 // 扫描 markers 寻找 SOF (0xFF 0xC0-0xCF, 排除 C4/C8/CC)
  146. 146 let mut pos = 2
  147. 147 while pos + 1 < len {
  148. 148● if data[pos].to_int() != 0xFF {
  149. 149○ pos += 1
  150. 150 continue
  151. 151 }
  152. 152● let marker = data[pos + 1].to_int()
  153. 153 // SOF markers: C0, C1, C2, C3, C5, C6, C7, C9, CA, CB, CD, CE, CF
  154. 154 // 排除 C4 (DHT), C8 (JPG), CC (DAC)
  155. 155 if marker >= 0xC0 &&
  156. 156 marker <= 0xCF &&
  157. 157 marker != 0xC4 &&
  158. 158 marker != 0xC8 &&
  159. 159 marker != 0xCC {
  160. 160 // SOF: length(2) + precision(1) + height(2) + width(2) + channels(1)
  161. 161● if pos + 9 >= len {
  162. 162● return None
  163. 163 }
  164. 164● let height = info_read_u16_be(data, pos + 5)
  165. 165● let width = info_read_u16_be(data, pos + 7)
  166. 166● let channels = data[pos + 9].to_int()
  167. 167 if width <= 0 || height <= 0 || channels <= 0 {
  168. 168○ return None
  169. 169 }
  170. 170 return Some(@types.ImageInfo::{ width, height, channels, })
  171. 171 }
  172. 172 // 跳过其他 marker
  173. 173 if pos + 3 >= len {
  174. 174○ return None
  175. 175 }
  176. 176● let seg_len = info_read_u16_be(data, pos + 2)
  177. 177 pos += 2 + seg_len
  178. 178 }
  179. 179 None
  180. 180}
  181. 181
  182. 182///|
  183. 183/// 从 PNM 头部读取信息(P5/P6 二进制格式)
  184. 184fn info_pnm(data : Bytes) -> @types.ImageInfo? {
  185. 185● let len = data.length()
  186. 186 if len < 4 {
  187. 187○ return None
  188. 188 }
  189. 189 // P5: gray (1ch), P6: rgb (3ch)
  190. 190● let channels = match data[1].to_int() {
  191. 191● 0x35 => 1 // P5
  192. 192● 0x36 => 3 // P6
  193. 193○ _ => return None
  194. 194 }
  195. 195 // 解析 ASCII header: P5/P6 + whitespace + width + whitespace + height + whitespace + maxval
  196. 196 let mut pos = 2
  197. 197 // 跳过空白和注释
  198. 198 fn skip_ws_comments(data : Bytes, pos : Int32, len : Int32) -> Int32 {
  199. 199● let mut p = pos
  200. 200 while p < len {
  201. 201● let b = data[p].to_int()
  202. 202 if b == 0x23 {
  203. 203 // # 注释行,跳过到行尾
  204. 204● while p < len && data[p].to_int() != 0x0A {
  205. 205● p += 1
  206. 206 }
  207. 207 p += 1
  208. 208● } else if b == 0x20 || b == 0x0A || b == 0x0D || b == 0x09 {
  209. 209● p += 1
  210. 210 } else {
  211. 211● break
  212. 212 }
  213. 213 }
  214. 214 p
  215. 215 }
  216. 216 fn read_ascii_int(data : Bytes, pos : Int32, len : Int32) -> (Int32, Int32)? {
  217. 217● let mut p = pos
  218. 218 let mut val = 0
  219. 219 let mut has_digit = false
  220. 220 while p < len {
  221. 221● let b = data[p].to_int()
  222. 222 if b >= 0x30 && b <= 0x39 {
  223. 223● val = val * 10 + (b - 0x30)
  224. 224 has_digit = true
  225. 225 p += 1
  226. 226 } else {
  227. 227● break
  228. 228 }
  229. 229 }
  230. 230 if has_digit {
  231. 231● Some((val, p))
  232. 232 } else {
  233. 233○ None
  234. 234 }
  235. 235 }
  236. 236● pos = skip_ws_comments(data, pos, len)
  237. 237● let (width, pos2) = match read_ascii_int(data, pos, len) {
  238. 238● Some(v) => v
  239. 239○ None => return None
  240. 240 }
  241. 241● pos = skip_ws_comments(data, pos2, len)
  242. 242● let (height, _) = match read_ascii_int(data, pos, len) {
  243. 243● Some(v) => v
  244. 244○ None => return None
  245. 245 }
  246. 246 if width <= 0 || height <= 0 {
  247. 247○ return None
  248. 248 }
  249. 249 Some(@types.ImageInfo::{ width, height, channels, })
  250. 250}
  251. 251
  252. 252///|
  253. 253/// 检测是否为 HDR 格式(magic: "#?RADIANCE" 或 "#?RGBE")
  254. 254pub fn is_hdr_from_bytes_pure(data : Bytes) -> Bool {
  255. 255● let len = data.length()
  256. 256● if len < 2 || data[0].to_int() != 0x23 || data[1].to_int() != 0x3F {
  257. 257● return false
  258. 258 }
  259. 259 // 检查 "#?RADIANCE" 或 "#?RGBE"
  260. 260 let radiance = [
  261. 261 b'\x52', b'\x41', b'\x44', b'\x49', b'\x41', b'\x4E', b'\x43', b'\x45',
  262. 262 ]
  263. 263 let rgbe = [b'\x52', b'\x47', b'\x42', b'\x45']
  264. 264 if len >= 10 {
  265. 265● let mut match_radiance = true
  266. 266 for i in 0..<8 {
  267. 267● if data[2 + i] != radiance[i] {
  268. 268● match_radiance = false
  269. 269 break
  270. 270 }
  271. 271 }
  272. 272 if match_radiance {
  273. 273● return true
  274. 274 }
  275. 275 }
  276. 276 if len >= 6 {
  277. 277● let mut match_rgbe = true
  278. 278 for i in 0..<4 {
  279. 279● if data[2 + i] != rgbe[i] {
  280. 280● match_rgbe = false
  281. 281 break
  282. 282 }
  283. 283 }
  284. 284 if match_rgbe {
  285. 285● return true
  286. 286 }
  287. 287 }
  288. 288 false
  289. 289}
  290. 290
  291. 291///|
  292. 292/// 检测是否为 16-bit 图像(仅 PNG 支持 16-bit)
  293. 293pub fn is_16_bit_from_bytes_pure(data : Bytes) -> Bool {
  294. 294● let len = data.length()
  295. 295 if len < 26 {
  296. 296● return false
  297. 297 }
  298. 298 // 检查 PNG signature
  299. 299● if data[0].to_int() != 0x89 ||
  300. 300● data[1].to_int() != 0x50 ||
  301. 301● data[2].to_int() != 0x4E ||
  302. 302● data[3].to_int() != 0x47 {
  303. 303● return false
  304. 304 }
  305. 305 // PNG bit depth 在 IHDR chunk 的 offset 24
  306. 306● data[24].to_int() == 16
  307. 307}
  308. 308
  309. 309///|
  310. 310/// 从字节流读取图像信息(纯 MoonBit,全目标可用)。
  311. 311/// 支持 BMP/QOI/PSD/GIF/PNG/JPEG/PNM(HDR 检测但不解析尺寸)。
  312. 312/// 返回 None 如果格式不支持或数据无效。
  313. 313pub fn info_from_bytes_pure(
  314. 314 data : Bytes,
  315. 315) -> @types.ImageInfo? raise @types.LoadError {
  316. 316● if data.length() == 0 {
  317. 317● raise @types.LoadError::DecodeFailed("输入数据为空")
  318. 318 }
  319. 319 // BMP
  320. 320● if data.length() >= 2 && data[0].to_int() == 0x42 && data[1].to_int() == 0x4D {
  321. 321● return info_bmp(data)
  322. 322 }
  323. 323 // QOI
  324. 324● if data.length() >= 4 &&
  325. 325● data[0].to_int() == 0x71 &&
  326. 326● data[1].to_int() == 0x6F &&
  327. 327● data[2].to_int() == 0x69 &&
  328. 328● data[3].to_int() == 0x66 {
  329. 329● return info_qoi(data)
  330. 330 }
  331. 331 // PSD
  332. 332● if data.length() >= 4 &&
  333. 333● data[0].to_int() == 0x38 &&
  334. 334● data[1].to_int() == 0x42 &&
  335. 335● data[2].to_int() == 0x50 &&
  336. 336● data[3].to_int() == 0x53 {
  337. 337● return info_psd(data)
  338. 338 }
  339. 339 // GIF
  340. 340● if data.length() >= 6 &&
  341. 341● data[0].to_int() == 0x47 &&
  342. 342● data[1].to_int() == 0x49 &&
  343. 343● data[2].to_int() == 0x46 {
  344. 344● return info_gif(data)
  345. 345 }
  346. 346 // PNG
  347. 347● if data.length() >= 8 &&
  348. 348● data[0].to_int() == 0x89 &&
  349. 349● data[1].to_int() == 0x50 &&
  350. 350● data[2].to_int() == 0x4E &&
  351. 351● data[3].to_int() == 0x47 {
  352. 352● return info_png(data)
  353. 353 }
  354. 354 // JPEG
  355. 355● if data.length() >= 2 && data[0].to_int() == 0xFF && data[1].to_int() == 0xD8 {
  356. 356● return info_jpeg(data)
  357. 357 }
  358. 358 // PNM (P5/P6)
  359. 359● if data.length() >= 2 && data[0].to_int() == 0x50 {
  360. 360● let p = data[1].to_int()
  361. 361 if p == 0x35 || p == 0x36 {
  362. 362● return info_pnm(data)
  363. 363 }
  364. 364 }
  365. 365 None
  366. 366}
  367. 367
  368. 368///|
  369. 369/// 纯 MoonBit 错误原因字符串(全目标可用)。
  370. 370/// 返回上次解码失败的错误描述。
  371. 371pub fn failure_reason_pure() -> String {
  372. 372● "pure MoonBit decoder error"
  373. 373}