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