image.codec\lib\lib.mbt

96.5% · 192 / 199 points · 545 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 Load/Save Unified Entry
  3. 3///
  4. 4/// 通过 magic bytes 自动检测图像格式,并委托 codec/color 包进行解码和通道转换。 Automatically detects image format via magic bytes, and delegates to codec/color packages for decoding and channel conversion.
  5. 5///
  6. 6/// 核心函数 Core functions:
  7. 7/// - `detect_format` - 检测图像格式 Detect image format
  8. 8/// - `load_from_bytes` - 从字节流加载图像 Load image from byte stream
  9. 9/// - `save_to_bytes` - 保存图像为字节流 Save image to byte stream
  10. 10/// - `load_image_info` - 加载图像信息(不含像素数据)Load image info (no pixel data)
  11. 11///
  12. 12/// 注意 Notes:
  13. 13/// - TGA 无固定 magic bytes,需显式调用 @codec.decode_tga_pure TGA has no fixed magic bytes, must explicitly call @codec.decode_tga_pure
  14. 14/// - HDR 返回 ImageF(浮点图像),需显式调用 @codec.decode_hdr_pure HDR returns ImageF (float image), must explicitly call @codec.decode_hdr_pure
  15. 15
  16. 16// lib 包:统一分派入口(格式分派层)
  17. 17// 职责:通过 magic bytes 检测图像格式,并委托 pure/codec 进行解码、pure/color 进行通道转换
  18. 18// 注意:TGA 无固定 magic bytes,需显式调用 @codec.decode_tga_pure
  19. 19// HDR 返回 ImageF(浮点图像),需显式调用 @codec.decode_hdr_pure
  20. 20
  21. 21///|
  22. 22pub type ImageFormat = @types.ImageFormat
  23. 23
  24. 24///|
  25. 25/// 通过 magic bytes 检测图像格式 - BMP "BM"、QOI "qoif"、PNM "P5"/"P6"、PSD "8BPS"、GIF "GIF87a"/"GIF89a"
  26. 26
  27. 27/// - PNG 8字节签名、JPEG SOI 0xFF 0xD8 - WebP: RIFF....WEBP
  28. 28
  29. 29///|
  30. 30/// - ICNS: 前4字节 "icns"
  31. 31/// - TIFF: 字节序标记 + magic 0x002A
  32. 32/// - ICO/CUR: 前4字节 00 00 01 00
  33. 33/// - TGA 无固定 magic bytes,按头部字段(color map / image type / bpp / 宽高)保守判定
  34. 34pub fn detect_format(data : Bytes) -> @types.ImageFormat {
  35. 35● let len = data.length()
  36. 36 if len < 2 {
  37. 37● return @types.ImageFormat::Unknown
  38. 38 }
  39. 39 // PNG: 8字节签名
  40. 40 if len >= 8 &&
  41. 41● data[0].to_int() == 0x89 &&
  42. 42● data[1].to_int() == 0x50 &&
  43. 43● data[2].to_int() == 0x4E &&
  44. 44● data[3].to_int() == 0x47 &&
  45. 45● data[4].to_int() == 0x0D &&
  46. 46● data[5].to_int() == 0x0A &&
  47. 47● data[6].to_int() == 0x1A &&
  48. 48● data[7].to_int() == 0x0A {
  49. 49 // 检查是否 APNG(含 acTL chunk)
  50. 50● if @codec.is_apng_format(data) {
  51. 51● return @types.ImageFormat::Apng
  52. 52 }
  53. 53 return @types.ImageFormat::Png
  54. 54 }
  55. 55 // JPEG: SOI marker
  56. 56● if data[0].to_int() == 0xFF && data[1].to_int() == 0xD8 {
  57. 57● return @types.ImageFormat::Jpeg
  58. 58 }
  59. 59 // BMP: "BM"
  60. 60● if data[0].to_int() == 0x42 && data[1].to_int() == 0x4D {
  61. 61● return @types.ImageFormat::Bmp
  62. 62 }
  63. 63 // QOI: "qoif"
  64. 64 if len >= 4 &&
  65. 65● data[0].to_int() == 0x71 &&
  66. 66● data[1].to_int() == 0x6F &&
  67. 67● data[2].to_int() == 0x69 &&
  68. 68● data[3].to_int() == 0x66 {
  69. 69● return @types.ImageFormat::Qoi
  70. 70 }
  71. 71 // PNM: "P5"/"P6"
  72. 72● if data[0].to_int() == 0x50 &&
  73. 73● data[1].to_int() >= 0x35 &&
  74. 74● data[1].to_int() <= 0x36 {
  75. 75● return @types.ImageFormat::Pnm
  76. 76 }
  77. 77 // PSD: "8BPS"
  78. 78 if len >= 4 &&
  79. 79● data[0].to_int() == 0x38 &&
  80. 80● data[1].to_int() == 0x42 &&
  81. 81● data[2].to_int() == 0x50 &&
  82. 82● data[3].to_int() == 0x53 {
  83. 83● return @types.ImageFormat::Psd
  84. 84 }
  85. 85 // GIF: "GIF87a" or "GIF89a"
  86. 86 if len >= 6 &&
  87. 87● data[0].to_int() == 0x47 &&
  88. 88● data[1].to_int() == 0x49 &&
  89. 89● data[2].to_int() == 0x46 {
  90. 90● return @types.ImageFormat::Gif
  91. 91 }
  92. 92 // HDR: #?RADIANCE
  93. 93 if len >= 10 &&
  94. 94● data[0].to_int() == 0x23 &&
  95. 95● data[1].to_int() == 0x3F &&
  96. 96● data[2].to_int() == 0x52 {
  97. 97● return @types.ImageFormat::Hdr
  98. 98 }
  99. 99 // WebP: RIFF....WEBP
  100. 100 if len >= 12 &&
  101. 101● data[0].to_int() == 0x52 &&
  102. 102● data[1].to_int() == 0x49 &&
  103. 103● data[2].to_int() == 0x46 &&
  104. 104● data[3].to_int() == 0x46 &&
  105. 105● data[8].to_int() == 0x57 &&
  106. 106● data[9].to_int() == 0x45 &&
  107. 107● data[10].to_int() == 0x42 &&
  108. 108● data[11].to_int() == 0x50 {
  109. 109● return @types.ImageFormat::Webp
  110. 110 }
  111. 111 // ICNS: 前4字节 "icns"
  112. 112 if len >= 4 &&
  113. 113● data[0].to_int() == 0x69 &&
  114. 114● data[1].to_int() == 0x63 &&
  115. 115● data[2].to_int() == 0x6E &&
  116. 116● data[3].to_int() == 0x73 {
  117. 117● return @types.ImageFormat::Icns
  118. 118 }
  119. 119 // TIFF: 字节序标记 (II=0x4949 LE, MM=0x4D4D BE) + magic 42 (offset 2-3)
  120. 120● if (data[0].to_int() == 0x49 && data[1].to_int() == 0x49) ||
  121. 121● (data[0].to_int() == 0x4D && data[1].to_int() == 0x4D) {
  122. 122● if len >= 4 &&
  123. 123 (
  124. 124● (data[2].to_int() == 0x2A && data[3].to_int() == 0x00) ||
  125. 125● (data[2].to_int() == 0x00 && data[3].to_int() == 0x2A)
  126. 126 ) {
  127. 127● return @types.ImageFormat::Tiff
  128. 128 }
  129. 129 }
  130. 130 // ICO/CUR: 00 00 XX 00 + 图像数量 (≥1) + 目录条目
  131. 131 // XX = 01 (ICO) 或 02 (CUR),总长度至少 22 字节(6 头 + 16 目录条目)
  132. 132 if len >= 22 &&
  133. 133● data[0].to_int() == 0x00 &&
  134. 134● data[1].to_int() == 0x00 &&
  135. 135● (data[2].to_int() == 0x01 || data[2].to_int() == 0x02) &&
  136. 136● data[3].to_int() == 0x00 &&
  137. 137● data[4].to_int() != 0 { // 图像数量必须非零
  138. 138 // 读取第一个目录条目的 width/height
  139. 139● let width = data[6].to_int()
  140. 140● let height = data[7].to_int()
  141. 141 // CUR: width=0 且 height=0 表示热点坐标占位
  142. 142● if data[2].to_int() == 0x02 && width == 0 && height == 0 {
  143. 143● return @types.ImageFormat::Cur
  144. 144 }
  145. 145 return @types.ImageFormat::Ico
  146. 146 }
  147. 147 // JPEG-XL: 裸码流 0xFF 0x0A 或 ISOBMFF 容器
  148. 148● if len >= 2 && data[0].to_int() == 0xFF && data[1].to_int() == 0x0A {
  149. 149● return @types.ImageFormat::JpegXl
  150. 150 }
  151. 151 if len >= 12 &&
  152. 152● data[0].to_int() == 0x00 &&
  153. 153● data[1].to_int() == 0x00 &&
  154. 154● data[2].to_int() == 0x00 &&
  155. 155● data[3].to_int() == 0x0C &&
  156. 156● data[4].to_int() == 0x4A &&
  157. 157● data[5].to_int() == 0x58 &&
  158. 158● data[6].to_int() == 0x4C &&
  159. 159● data[7].to_int() == 0x20 {
  160. 160● return @types.ImageFormat::JpegXl
  161. 161 }
  162. 162 // AVIF/HEIF: ftyp box (bytes 4-7 = "ftyp")
  163. 163 if len >= 12 &&
  164. 164● data[4].to_int() == 0x66 &&
  165. 165● data[5].to_int() == 0x74 &&
  166. 166● data[6].to_int() == 0x79 &&
  167. 167● data[7].to_int() == 0x70 {
  168. 168● let b8 = data[8].to_int()
  169. 169● let b9 = data[9].to_int()
  170. 170● let b10 = data[10].to_int()
  171. 171● let b11 = data[11].to_int()
  172. 172 // AVIF: "avif" (0x61 0x76 0x69 0x66) or "avis" (0x61 0x76 0x69 0x73)
  173. 173 if b8 == 0x61 && b9 == 0x76 && b10 == 0x69 && (b11 == 0x66 || b11 == 0x73) {
  174. 174● return @types.ImageFormat::Avif
  175. 175 }
  176. 176 // HEIF: "heic", "heix", "hevc", "mif1"
  177. 177 if (b8 == 0x68 && b9 == 0x65 && b10 == 0x69 && (b11 == 0x63 || b11 == 0x78)) ||
  178. 178 (b8 == 0x68 && b9 == 0x65 && b10 == 0x76 && b11 == 0x63) ||
  179. 179 (b8 == 0x6D && b9 == 0x69 && b10 == 0x66 && b11 == 0x31) {
  180. 180● return @types.ImageFormat::Heif
  181. 181 }
  182. 182 }
  183. 183 // TGA:无强魔数,放最后用头部字段判定(前面所有 magic 都已排除)
  184. 184● if @codec.is_tga_format(data) {
  185. 185● return @types.ImageFormat::Tga
  186. 186 }
  187. 187 @types.ImageFormat::Unknown
  188. 188}
  189. 189
  190. 190///|
  191. 191/// 自动格式分派解码(pure 侧统一入口),根据 magic bytes 检测格式并委托对应 pure 解码器
  192. 192/// - TGA 无固定魔数,按头部字段保守判定(`@codec.is_tga_format`),命中则走 `decode_tga_pure`
  193. 193/// - HDR 返回 ImageF,需显式调用 @codec.decode_hdr_pure
  194. 194/// - APNG 与 PNG 共享签名,需显式调用 decode_apng_pure
  195. 195/// - 未识别格式 raise LoadError::DecodeFailed Auto-format dispatch decoding (pure side unified entry), Detects format from magic bytes and delegates to corresponding pure decoder
  196. 196
  197. 197///|
  198. 198/// ```mbt check
  199. 199/// test "doc_load_from_bytes_auto_bmp" {
  200. 200/// try {
  201. 201/// let img = @lib.load_from_bytes_auto(@types.DOC_TEST_BMP_1X1)
  202. 202/// assert_eq(img.width, 1)
  203. 203/// assert_eq(img.channels, 3)
  204. 204/// } catch {
  205. 205/// _ => assert_true(false)
  206. 206/// }
  207. 207/// }
  208. 208/// ```
  209. 209pub fn load_from_bytes_auto(
  210. 210 data : Bytes,
  211. 211 req_channels~ : Int32? = None,
  212. 212 deadline~ : @types.Deadline? = None,
  213. 213) -> @types.Image raise @types.LoadError {
  214. 214● let img = match detect_format(data) {
  215. 215 @types.ImageFormat::Png =>
  216. 216● @codec.decode_png_pure(data, req_channels~, deadline~)
  217. 217● @types.ImageFormat::Jpeg => @codec.decode_jpeg_pure(data)
  218. 218● @types.ImageFormat::Bmp => @codec.decode_bmp_pure(data)
  219. 219● @types.ImageFormat::Qoi => @codec.decode_qoi_pure(data)
  220. 220● @types.ImageFormat::Pnm => @codec.decode_pnm_pure(data, deadline~)
  221. 221● @types.ImageFormat::Psd => @codec.decode_psd_pure(data)
  222. 222● @types.ImageFormat::Gif => @codec.decode_gif_pure(data)
  223. 223● @types.ImageFormat::Webp => @codec.decode_webp_pure(data)
  224. 224 @types.ImageFormat::Hdr =>
  225. 225● raise @types.LoadError::DecodeFailed(
  226. 226 "HDR 格式需要显式调用 decode_hdr_pure",
  227. 227 )
  228. 228● @types.ImageFormat::Tga => @codec.decode_tga_pure(data)
  229. 229● @types.ImageFormat::Tiff => @codec.decode_tiff_pure(data)
  230. 230● @types.ImageFormat::Ico => @codec.decode_ico_pure(data)
  231. 231○ @types.ImageFormat::Cur => @codec.decode_cur_pure(data)
  232. 232● @types.ImageFormat::Icns => @codec.decode_icns_pure(data)
  233. 233● @types.ImageFormat::Apng => {
  234. 234● let anim = @codec.decode_apng_pure(data)
  235. 235● if anim.frames.length() == 0 {
  236. 236○ raise @types.LoadError::DecodeFailed("APNG 无帧")
  237. 237 }
  238. 238 anim.frames[0]
  239. 239 }
  240. 240○ @types.ImageFormat::Avif => @codec.avif_decode(data)
  241. 241○ @types.ImageFormat::Heif => @codec.heif_decode(data)
  242. 242○ @types.ImageFormat::JpegXl => @codec.jpegxl_decode(data)
  243. 243 @types.ImageFormat::Unknown =>
  244. 244● raise @types.LoadError::DecodeFailed(
  245. 245 "不支持或无法识别的格式(TGA 无 magic bytes,需显式调用 decode_tga_pure)",
  246. 246 )
  247. 247 @types.ImageFormat::.. =>
  248. 248○ raise @types.LoadError::DecodeFailed("不支持或无法识别的格式")
  249. 249 }
  250. 250● match req_channels {
  251. 251● Some(n) => @color.convert_channels_pure(img, n)
  252. 252● None => img
  253. 253 }
  254. 254}
  255. 255
  256. 256///|
  257. 257/// 16-bit 自动格式分派解码(纯 MoonBit,全目标可用)
  258. 258/// 根据格式委托对应 16-bit 解码器,输出 Image16(little-endian uint16)
  259. 259/// - PNG: 支持 8-bit 上采样和原生 16-bit
  260. 260/// - PNM: 支持 8-bit 上采样和原生 16-bit
  261. 261/// - 其他格式不支持 16-bit
  262. 262/// 注意:req_channels 仅对 PNG 生效,PNM 分支不传递 req_channels
  263. 263pub fn load_16_from_bytes_auto(
  264. 264 data : Bytes,
  265. 265 req_channels~ : Int32? = None,
  266. 266) -> @types.Image16 raise @types.LoadError {
  267. 267● match detect_format(data) {
  268. 268● @types.ImageFormat::Png => @codec.decode_png_16_pure(data, req_channels~)
  269. 269● @types.ImageFormat::Pnm => @codec.decode_pnm_16_pure(data)
  270. 270 _ =>
  271. 271● raise @types.LoadError::DecodeFailed(
  272. 272 "16-bit 解码不支持此格式,仅支持 PNG 和 PNM",
  273. 273 )
  274. 274 }
  275. 275}
  276. 276
  277. 277///|
  278. 278/// QOI 编码委托
  279. 279/// QOI 编码委托 QOI encoding delegation
  280. 280///
  281. 281/// 错误 Errors:
  282. 282/// - `LoadError::EncodeFailed` - channels 不为 3 或 4 Channels not 3 or 4
  283. 283pub fn encode_qoi(img : @types.Image) -> Bytes raise @types.LoadError {
  284. 284● @codec.encode_qoi_pure(img)
  285. 285}
  286. 286
  287. 287///|
  288. 288/// PNM 编码委托(channels<=1 → PGM else → PPM)
  289. 289pub fn encode_pnm_auto(img : @types.Image) -> Bytes {
  290. 290● @codec.encode_pnm_pure(img)
  291. 291}
  292. 292
  293. 293///|
  294. 294/// PPM 编码委托
  295. 295pub fn encode_ppm_auto(img : @types.Image) -> Bytes {
  296. 296● @codec.encode_ppm_pure(img)
  297. 297}
  298. 298
  299. 299///|
  300. 300/// PGM 编码委托
  301. 301pub fn encode_pgm_auto(img : @types.Image) -> Bytes {
  302. 302● @codec.encode_pgm_pure(img)
  303. 303}
  304. 304
  305. 305///|
  306. 306/// GIF 编码委托
  307. 307/// GIF 编码委托 GIF encoding delegation
  308. 308///
  309. 309/// 错误 Errors:
  310. 310/// - `LoadError::EncodeFailed` - channels 不为 3 或 4 Channels not 3 or 4
  311. 311pub fn encode_gif(img : @types.Image) -> Bytes raise @types.LoadError {
  312. 312● @codec.encode_gif_pure(img)
  313. 313}
  314. 314
  315. 315///|
  316. 316/// PNG 编码委托
  317. 317pub fn encode_png(img : @types.Image) -> Bytes {
  318. 318● @codec.encode_png_pure(img)
  319. 319}
  320. 320
  321. 321///|
  322. 322/// JPEG 编码委托
  323. 323/// JPEG 编码委托 JPEG encoding delegation
  324. 324///
  325. 325/// 错误 Errors:
  326. 326/// - `LoadError::EncodeFailed` - 尺寸无效、channels 不支持 Invalid dimensions, unsupported channels
  327. 327pub fn encode_jpeg(img : @types.Image) -> Bytes raise @types.LoadError {
  328. 328● @codec.encode_jpeg_pure(img)
  329. 329}
  330. 330
  331. 331///|
  332. 332/// 渐进式 JPEG 编码委托(SOF2)。
  333. 333/// Progressive JPEG encoding delegation (SOF2).
  334. 334///
  335. 335/// 参数 Parameters:
  336. 336/// - `img` - 输入图像;1 通道按灰度复制到三通道
  337. 337/// - `quality` - 1–100,50 等价标准量化表
  338. 338///
  339. 339/// 错误 Errors:
  340. 340/// - `LoadError::EncodeFailed` - 尺寸无效、或像素数超过 2,000,000(系数缓存上限)
  341. 341pub fn encode_jpeg_progressive(
  342. 342 img : @types.Image,
  343. 343 quality : Int32,
  344. 344) -> Bytes raise @types.LoadError {
  345. 345● @codec.encode_jpeg_progressive(img, quality)
  346. 346}
  347. 347
  348. 348///|
  349. 349/// 渐进式 JPEG 编码委托(SOF2,4:2:0 色度下采样)。
  350. 350/// Progressive JPEG encoding with 4:2:0 chroma subsampling.
  351. 351///
  352. 352/// 参数 Parameters:
  353. 353/// - `img` - 输入图像;1 通道按灰度处理
  354. 354/// - `quality` - 1–100,50 等价标准量化表
  355. 355///
  356. 356/// 错误 Errors:
  357. 357/// - `LoadError::EncodeFailed` - 尺寸无效、或像素数超过 2,000,000(系数缓存上限)
  358. 358pub fn encode_jpeg_progressive_420(
  359. 359 img : @types.Image,
  360. 360 quality : Int32,
  361. 361) -> Bytes raise @types.LoadError {
  362. 362● @codec.encode_jpeg_progressive_420(img, quality)
  363. 363}
  364. 364
  365. 365///|
  366. 366/// 渐进式 JPEG 编码委托(SOF2,4:2:0,**扫描脚本可配置**)。
  367. 367///
  368. 368/// `script` 给出每条扫描的分量集与 `Ss/Se/Ah/Al`;`@codec.jpeg_progressive_script_libjpeg()`
  369. 369/// 返回 libjpeg `jpeg_simple_progression()` 的 10 段默认脚本,
  370. 370/// `@codec.jpeg_progressive_script_eight()` 为 0.1.30 固定 8 段等价脚本。
  371. 371///
  372. 372/// 错误 Errors:
  373. 373/// - `LoadError::EncodeFailed` - 尺寸无效、像素数超上限、脚本非法
  374. 374pub fn encode_jpeg_progressive_420_scripted(
  375. 375 img : @types.Image,
  376. 376 quality : Int32,
  377. 377 script : Array[@codec.JpegProgScan],
  378. 378) -> Bytes raise @types.LoadError {
  379. 379● @codec.encode_jpeg_progressive_420_scripted(img, quality, script)
  380. 380}
  381. 381
  382. 382///|
  383. 383/// libjpeg 默认(10 段)progressive 扫描脚本。
  384. 384pub fn jpeg_progressive_script_libjpeg() -> Array[@codec.JpegProgScan] {
  385. 385● @codec.jpeg_progressive_script_libjpeg()
  386. 386}
  387. 387
  388. 388///|
  389. 389/// 0.1.30 固定 8 段扫描脚本(DC 首扫 Al=1 → 3×AC 首扫 → DC 细化 → 3×AC 细化)。
  390. 390pub fn jpeg_progressive_script_eight() -> Array[@codec.JpegProgScan] {
  391. 391○ @codec.jpeg_progressive_script_eight()
  392. 392}
  393. 393
  394. 394///|
  395. 395/// HDR 编码委托
  396. 396/// HDR 编码委托 HDR encoding delegation
  397. 397///
  398. 398/// 错误 Errors:
  399. 399/// - `LoadError::EncodeFailed` - channels 不为 3 Channels not 3
  400. 400pub fn encode_hdr(img : @types.ImageF) -> Bytes raise @types.LoadError {
  401. 401● @codec.encode_hdr_pure(img)
  402. 402}
  403. 403
  404. 404///|
  405. 405/// BMP 编码委托
  406. 406pub fn encode_bmp(img : @types.Image) -> Bytes {
  407. 407● @codec.encode_bmp_pure(img)
  408. 408}
  409. 409
  410. 410///|
  411. 411/// TGA 编码委托
  412. 412pub fn encode_tga(img : @types.Image) -> Bytes {
  413. 413● @codec.encode_tga_pure(img)
  414. 414}
  415. 415
  416. 416///|
  417. 417/// WebP 无损编码委托(VP8L)
  418. 418///
  419. 419/// 下游之所以需要这个别名:`save_image_auto(img, ImageFormat::Webp)` 能用,但很多人会先
  420. 420/// `import walkzzz/image.codec/lib` 写"只用 lib"的代码,再去文档里找 webp 编码函数——
  421. 421/// 而真正的实现在 `image.codec/codec`。这里给一条 `@lib` 就能拿到的入口,省一次 import。
  422. 422pub fn encode_webp_lossless(img : @types.Image) -> Bytes raise @types.LoadError {
  423. 423● @codec.encode_webp_lossless(img)
  424. 424}
  425. 425
  426. 426///|
  427. 427/// WebP 解码委托(统一走 RFC 9649 合规的 VP8L 路径,palette / predictor / cross-color 全支持;
  428. 428/// 有损 VP8 产物仍会报 `unsupported`,那是另一条待办)
  429. 429pub fn decode_webp(data : Bytes) -> @types.Image raise @types.LoadError {
  430. 430● @codec.decode_webp_pure(data)
  431. 431}
  432. 432
  433. 433///|
  434. 434/// 统一保存 API:按指定格式自动分派编码
  435. 435/// - 支持:PNG/JPEG/BMP/QOI/PNM/GIF/TGA/TIFF/ICO/CUR/ICNS/WebP
  436. 436/// - 不支持:HDR(需 ImageF,用 encode_hdr)、APNG(需 PngAnimation)、AVIF/HEIF/JPEG-XL/PSD(无编码器)
  437. 437pub fn save_image_auto(
  438. 438 img : @types.Image,
  439. 439 format : @types.ImageFormat,
  440. 440) -> Bytes raise @types.LoadError {
  441. 441● match format {
  442. 442● @types.ImageFormat::Png => @codec.encode_png_pure(img)
  443. 443● @types.ImageFormat::Jpeg => @codec.encode_jpeg_pure(img)
  444. 444● @types.ImageFormat::Bmp => @codec.encode_bmp_pure(img)
  445. 445● @types.ImageFormat::Qoi => @codec.encode_qoi_pure(img)
  446. 446● @types.ImageFormat::Pnm => @codec.encode_pnm_pure(img)
  447. 447● @types.ImageFormat::Gif => @codec.encode_gif_pure(img)
  448. 448● @types.ImageFormat::Tga => @codec.encode_tga_pure(img)
  449. 449● @types.ImageFormat::Tiff => @codec.encode_tiff_pure(img)
  450. 450● @types.ImageFormat::Ico => @codec.encode_ico_pure(img)
  451. 451● @types.ImageFormat::Cur => @codec.encode_cur_pure(img)
  452. 452● @types.ImageFormat::Icns => @codec.encode_icns_pure(img)
  453. 453● @types.ImageFormat::Webp => @codec.encode_webp_lossless(img)
  454. 454 _ =>
  455. 455● raise @types.LoadError::EncodeFailed(
  456. 456 "不支持此格式的编码(HDR 需 ImageF,APNG 需 PngAnimation,AVIF/HEIF/JPEG-XL/PSD 无编码器)",
  457. 457 )
  458. 458 }
  459. 459}
  460. 460
  461. 461///|
  462. 462/// 带质量参数的统一保存 API
  463. 463/// - WebP: 走 VP8L 无损编码,`quality` 参数被忽略(无损)
  464. 464/// - JPEG: `quality` 1–100 生效(50 = 标准量化表)
  465. 465/// - 其他格式: quality 参数被忽略,使用默认质量
  466. 466/// - 不支持的格式 raise
  467. 467pub fn save_image_with_quality(
  468. 468 img : @types.Image,
  469. 469 format : @types.ImageFormat,
  470. 470 quality : Int32,
  471. 471) -> Bytes raise @types.LoadError {
  472. 472● match format {
  473. 473● @types.ImageFormat::Webp => @codec.encode_webp_lossless(img)
  474. 474● @types.ImageFormat::Png => @codec.encode_png_pure(img)
  475. 475● @types.ImageFormat::Jpeg => @codec.encode_jpeg_with_quality(img, quality)
  476. 476● @types.ImageFormat::Bmp => @codec.encode_bmp_pure(img)
  477. 477● @types.ImageFormat::Qoi => @codec.encode_qoi_pure(img)
  478. 478● @types.ImageFormat::Pnm => @codec.encode_pnm_pure(img)
  479. 479● @types.ImageFormat::Gif => @codec.encode_gif_pure(img)
  480. 480● @types.ImageFormat::Tga => @codec.encode_tga_pure(img)
  481. 481● @types.ImageFormat::Tiff => @codec.encode_tiff_pure(img)
  482. 482● @types.ImageFormat::Ico => @codec.encode_ico_pure(img)
  483. 483● @types.ImageFormat::Cur => @codec.encode_cur_pure(img)
  484. 484● @types.ImageFormat::Icns => @codec.encode_icns_pure(img)
  485. 485● _ => raise @types.LoadError::EncodeFailed("不支持此格式的编码")
  486. 486 }
  487. 487}
  488. 488
  489. 489///|
  490. 490/// Float 自动格式分派加载,HDR → decode_hdr_pure 返回 ImageF,其他格式不支持 Float 加载 raise Float auto-format dispatch loading, HDR → decode_hdr_pure returns ImageF, Other formats do not support Float loading, raises
  491. 491
  492. 492///|
  493. 493/// 参数 Parameters:
  494. 494/// - `data` - 字节流 Byte stream
  495. 495///
  496. 496/// 返回 Return:
  497. 497/// - ImageF(HDR 解码结果)ImageF (HDR decode result)
  498. 498///
  499. 499/// 错误 Errors:
  500. 500/// - `LoadError::DecodeFailed` - 非 HDR 格式、HDR 头无效 Non-HDR format, invalid HDR header
  501. 501/// @see load_any 基础版本,本函数为 ImageF 变体
  502. 502pub fn load_any_f(data : Bytes) -> @types.ImageF raise @types.LoadError {
  503. 503● match detect_format(data) {
  504. 504● @types.ImageFormat::Hdr => @codec.decode_hdr_pure(data)
  505. 505 _ =>
  506. 506● raise @types.LoadError::DecodeFailed(
  507. 507 "Float 加载不支持此格式,仅支持 HDR",
  508. 508 )
  509. 509 }
  510. 510}
  511. 511
  512. 512///|
  513. 513/// Float 自动格式分派保存,HDR → encode_hdr_pure,其他格式不支持 Float 保存 raise
  514. 514/// @see save_any 基础版本,本函数为 ImageF 变体
  515. 515pub fn save_any_f(
  516. 516 img : @types.ImageF,
  517. 517 format : @types.ImageFormat,
  518. 518) -> Bytes raise @types.LoadError {
  519. 519● match format {
  520. 520● @types.ImageFormat::Hdr => @codec.encode_hdr_pure(img)
  521. 521 _ =>
  522. 522● raise @types.LoadError::EncodeFailed(
  523. 523 "Float 保存不支持此格式,仅支持 HDR",
  524. 524 )
  525. 525 }
  526. 526}
  527. 527
  528. 528///|
  529. 529/// 查询格式是否支持 16-bit 加载,PNG/PNM → true,其他 → false
  530. 530pub fn format_supports_16bit(format : @types.ImageFormat) -> Bool {
  531. 531● match format {
  532. 532● @types.ImageFormat::Png => true
  533. 533● @types.ImageFormat::Pnm => true
  534. 534● _ => false
  535. 535 }
  536. 536}
  537. 537
  538. 538///|
  539. 539/// 查询格式是否支持 Float 加载/保存,HDR → true,其他 → false
  540. 540pub fn format_supports_float(format : @types.ImageFormat) -> Bool {
  541. 541● match format {
  542. 542● @types.ImageFormat::Hdr => true
  543. 543● _ => false
  544. 544 }
  545. 545}