image.codec\codec\codec_trait.mbt
95.2% · 178 / 187 points · 277 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 编解码特征 Codec Trait - 3
/// - 4
/// 编解码统一接口定义 - 5
/// 统一编解码接口定义 Unified codec interface definitions - 6
- 7
///| - 8
/// 编解码器统一接口 trait - 9
/// - 10
/// 为 ImageFormat 枚举实现,提供统一的 encode / decode 方法 - 11
pub trait Codec { - 12
fn format_name(Self) -> String - 13
fn can_encode(Self) -> Bool - 14
fn can_decode(Self) -> Bool - 15
fn is_lossless(Self) -> Bool - 16
fn encode(Self, @types.AnyImage) -> Bytes raise @types.LoadError - 17
fn decode(Self, Bytes) -> @types.AnyImage raise @types.LoadError - 18
} - 19
- 20
///| - 21
pub impl Codec for @types.ImageFormat with fn format_name(self) { - 22●
match self { - 23●
@types.ImageFormat::Png => "PNG" - 24●
@types.ImageFormat::Jpeg => "JPEG" - 25●
@types.ImageFormat::Bmp => "BMP" - 26●
@types.ImageFormat::Gif => "GIF" - 27●
@types.ImageFormat::Tga => "TGA" - 28●
@types.ImageFormat::Psd => "PSD" - 29●
@types.ImageFormat::Hdr => "HDR" - 30●
@types.ImageFormat::Pnm => "PNM" - 31●
@types.ImageFormat::Qoi => "QOI" - 32●
@types.ImageFormat::Webp => "WebP" - 33●
@types.ImageFormat::Tiff => "TIFF" - 34●
@types.ImageFormat::Ico => "ICO" - 35●
@types.ImageFormat::Cur => "CUR" - 36●
@types.ImageFormat::Icns => "ICNS" - 37●
@types.ImageFormat::Apng => "APNG" - 38●
@types.ImageFormat::Avif => "AVIF" - 39●
@types.ImageFormat::Heif => "HEIF" - 40●
@types.ImageFormat::JpegXl => "JPEG-XL" - 41●
@types.ImageFormat::Unknown => "Unknown" - 42○
@types.ImageFormat::.. => "Unknown" - 43
} - 44
} - 45
- 46
///| - 47
pub impl Codec for @types.ImageFormat with fn can_encode(self) { - 48●
match self { - 49●
@types.ImageFormat::Png => true - 50●
@types.ImageFormat::Jpeg => true - 51●
@types.ImageFormat::Bmp => true - 52●
@types.ImageFormat::Gif => true - 53●
@types.ImageFormat::Tga => true - 54●
@types.ImageFormat::Psd => false - 55●
@types.ImageFormat::Hdr => true - 56●
@types.ImageFormat::Pnm => true - 57●
@types.ImageFormat::Qoi => true - 58●
@types.ImageFormat::Webp => true - 59●
@types.ImageFormat::Tiff => true - 60●
@types.ImageFormat::Ico => true - 61●
@types.ImageFormat::Cur => true - 62●
@types.ImageFormat::Icns => true - 63●
@types.ImageFormat::Apng => true - 64●
@types.ImageFormat::Avif => false - 65●
@types.ImageFormat::Heif => false - 66●
@types.ImageFormat::JpegXl => false - 67●
@types.ImageFormat::Unknown => false - 68○
@types.ImageFormat::.. => false - 69
} - 70
} - 71
- 72
///| - 73
pub impl Codec for @types.ImageFormat with fn can_decode(self) { - 74●
match self { - 75●
@types.ImageFormat::Unknown => false - 76●
_ => true - 77
} - 78
} - 79
- 80
///| - 81
pub impl Codec for @types.ImageFormat with fn is_lossless(self) { - 82●
match self { - 83●
@types.ImageFormat::Jpeg => false - 84●
@types.ImageFormat::Webp => false - 85●
@types.ImageFormat::Avif => false - 86●
@types.ImageFormat::Heif => false - 87●
@types.ImageFormat::JpegXl => false - 88●
_ => true - 89
} - 90
} - 91
- 92
///| - 93
#warnings("-deprecated") - 94
pub impl Codec for @types.ImageFormat with fn encode(self, img) { - 95●
match self { - 96●
@types.ImageFormat::Png => encode_png_pure(@types.any_image_to_u8(img)) - 97●
@types.ImageFormat::Jpeg => encode_jpeg_pure(@types.any_image_to_u8(img)) - 98●
@types.ImageFormat::Bmp => encode_bmp_pure(@types.any_image_to_u8(img)) - 99●
@types.ImageFormat::Gif => encode_gif_pure(@types.any_image_to_u8(img)) - 100●
@types.ImageFormat::Tga => encode_tga_pure(@types.any_image_to_u8(img)) - 101
@types.ImageFormat::Hdr => - 102●
match img { - 103○
@types.AnyImage::F32(im) => encode_hdr_pure(im) - 104●
_ => encode_hdr_pure(@types.hdr_from_u8(@types.any_image_to_u8(img))) - 105
} - 106●
@types.ImageFormat::Pnm => encode_pnm_pure(@types.any_image_to_u8(img)) - 107●
@types.ImageFormat::Qoi => encode_qoi_pure(@types.any_image_to_u8(img)) - 108
@types.ImageFormat::Webp => - 109●
encode_webp_lossy_pure(@types.any_image_to_u8(img)) - 110●
@types.ImageFormat::Tiff => encode_tiff_pure(@types.any_image_to_u8(img)) - 111●
@types.ImageFormat::Ico => encode_ico_pure(@types.any_image_to_u8(img)) - 112●
@types.ImageFormat::Cur => encode_cur_pure(@types.any_image_to_u8(img)) - 113●
@types.ImageFormat::Icns => encode_icns_pure(@types.any_image_to_u8(img)) - 114●
@types.ImageFormat::Apng => { - 115●
let im = @types.any_image_to_u8(img) - 116●
encode_apng_pure(@types.PngAnimation::{ frames: [im], delays: [0], }) - 117
} - 118
_ => - 119●
raise @types.LoadError::EncodeFailed("Format not supported for encoding") - 120
} - 121
} - 122
- 123
///| - 124
pub impl Codec for @types.ImageFormat with fn decode(self, data) { - 125●
match self { - 126●
@types.ImageFormat::Png => @types.AnyImage::U8(decode_png_pure(data)) - 127○
@types.ImageFormat::Jpeg => @types.AnyImage::U8(decode_jpeg_pure(data)) - 128●
@types.ImageFormat::Bmp => @types.AnyImage::U8(decode_bmp_pure(data)) - 129●
@types.ImageFormat::Gif => @types.AnyImage::U8(decode_gif_pure(data)) - 130●
@types.ImageFormat::Tga => @types.AnyImage::U8(decode_tga_pure(data)) - 131●
@types.ImageFormat::Psd => @types.AnyImage::U8(decode_psd_pure(data)) - 132●
@types.ImageFormat::Hdr => @types.AnyImage::F32(decode_hdr_pure(data)) - 133●
@types.ImageFormat::Pnm => @types.AnyImage::U8(decode_pnm_pure(data)) - 134●
@types.ImageFormat::Qoi => @types.AnyImage::U8(decode_qoi_pure(data)) - 135○
@types.ImageFormat::Webp => @types.AnyImage::U8(decode_webp_pure(data)) - 136●
@types.ImageFormat::Tiff => @types.AnyImage::U8(decode_tiff_pure(data)) - 137●
@types.ImageFormat::Ico => @types.AnyImage::U8(decode_ico_pure(data)) - 138●
@types.ImageFormat::Cur => @types.AnyImage::U8(decode_cur_pure(data)) - 139●
@types.ImageFormat::Icns => @types.AnyImage::U8(decode_icns_pure(data)) - 140●
@types.ImageFormat::Apng => { - 141●
let anim = decode_apng_pure(data) - 142●
match anim.frames { - 143●
[first, ..] => @types.AnyImage::U8(first) - 144○
[] => raise @types.LoadError::DecodeFailed("APNG has no frames") - 145
} - 146
} - 147●
@types.ImageFormat::Avif => @types.AnyImage::U8(avif_decode(data)) - 148●
@types.ImageFormat::Heif => @types.AnyImage::U8(heif_decode(data)) - 149●
@types.ImageFormat::JpegXl => @types.AnyImage::U8(jpegxl_decode(data)) - 150
@types.ImageFormat::Unknown => - 151●
raise @types.LoadError::DecodeFailed("Unknown format") - 152
@types.ImageFormat::.. => - 153○
raise @types.LoadError::DecodeFailed("Unsupported format") - 154
} - 155
} - 156
- 157
///| - 158
- 159
///| - 160
/// 通过 Codec trait 统一解码:自动检测格式并解码 - 161
pub fn codec_decode_auto( - 162
data : Bytes, - 163
) -> @types.AnyImage raise @types.LoadError { - 164●
let format = codec_detect_format(data) - 165●
Codec::decode(format, data) - 166
} - 167
- 168
///| - 169
/// 检测图像格式(基于 magic bytes) - 170
pub fn codec_detect_format(data : Bytes) -> @types.ImageFormat { - 171●
if data.length() < 2 { - 172●
return @types.ImageFormat::Unknown - 173
} - 174●
let b0 = data[0].to_int() - 175●
let b1 = data[1].to_int() - 176
// PNG: 89 50 4E 47 - 177
if b0 == 0x89 && b1 == 0x50 { - 178●
return @types.ImageFormat::Png - 179
} - 180
// JPEG: FF D8 - 181
if b0 == 0xFF && b1 == 0xD8 { - 182●
return @types.ImageFormat::Jpeg - 183
} - 184
// GIF: 47 49 46 - 185
if b0 == 0x47 && b1 == 0x49 { - 186●
return @types.ImageFormat::Gif - 187
} - 188
// BMP: 42 4D - 189
if b0 == 0x42 && b1 == 0x4D { - 190●
return @types.ImageFormat::Bmp - 191
} - 192
// QOI: 71 6F 69 66 - 193
if b0 == 0x71 && b1 == 0x6F { - 194●
return @types.ImageFormat::Qoi - 195
} - 196
// WebP: 52 49 46 46 ... 57 45 42 50 - 197●
if b0 == 0x52 && b1 == 0x49 && data.length() >= 12 { - 198●
let b8 = data[8].to_int() - 199●
let b9 = data[9].to_int() - 200●
let b10 = data[10].to_int() - 201●
let b11 = data[11].to_int() - 202
if b8 == 0x57 && b9 == 0x45 && b10 == 0x42 && b11 == 0x50 { - 203●
return @types.ImageFormat::Webp - 204
} - 205
} - 206
// TIFF: 49 49 or 4D 4D - 207
if (b0 == 0x49 && b1 == 0x49) || (b0 == 0x4D && b1 == 0x4D) { - 208●
return @types.ImageFormat::Tiff - 209
} - 210
// ICO/CUR:交给各自的严格探测(仅看前 4 字节会误判 24/32-bit TGA) - 211●
if is_ico_format(data) { - 212●
return @types.ImageFormat::Ico - 213
} - 214●
if is_cur_format(data) { - 215●
return @types.ImageFormat::Cur - 216
} - 217
// TGA:无强魔数,按头字段(cmaptype/image_type/bpp)判定 - 218●
if is_tga_format(data) { - 219●
return @types.ImageFormat::Tga - 220
} - 221
// HDR: #?RADIANCE or #?RGBE - 222
if b0 == 0x23 && b1 == 0x3F { - 223●
return @types.ImageFormat::Hdr - 224
} - 225
// PNM: P1-P6 - 226
if b0 == 0x50 && b1 >= 0x31 && b1 <= 0x36 { - 227●
return @types.ImageFormat::Pnm - 228
} - 229
// PSD: 38 42 50 53 - 230
if b0 == 0x38 && b1 == 0x42 { - 231●
return @types.ImageFormat::Psd - 232
} - 233
// ICNS: 69 63 6E 73 - 234
if b0 == 0x69 && b1 == 0x63 { - 235●
return @types.ImageFormat::Icns - 236
} - 237
// AVIF/HEIF: ftyp box - 238●
if b0 == 0x00 && data.length() >= 12 { - 239●
let b4 = data[4].to_int() - 240●
let b5 = data[5].to_int() - 241●
let b6 = data[6].to_int() - 242●
let b7 = data[7].to_int() - 243
if b4 == 0x66 && b5 == 0x74 && b6 == 0x79 && b7 == 0x70 { - 244●
let b8 = data[8].to_int() - 245●
let b9 = data[9].to_int() - 246●
let b10 = data[10].to_int() - 247●
let b11 = data[11].to_int() - 248
// avif/mif/avis - 249
if (b8 == 0x61 && b9 == 0x76 && b10 == 0x69 && b11 == 0x66) || - 250
(b8 == 0x6D && b9 == 0x69 && b10 == 0x66 && b11 == 0x31) || - 251
(b8 == 0x61 && b9 == 0x76 && b10 == 0x69 && b11 == 0x73) { - 252●
return @types.ImageFormat::Avif - 253
} - 254
// heic/heix/heif/mif - 255
if (b8 == 0x68 && b9 == 0x65 && b10 == 0x69 && b11 == 0x63) || - 256
(b8 == 0x68 && b9 == 0x65 && b10 == 0x69 && b11 == 0x78) || - 257
(b8 == 0x68 && b9 == 0x65 && b10 == 0x69 && b11 == 0x66) || - 258
(b8 == 0x6D && b9 == 0x69 && b10 == 0x66 && b11 == 0x31) { - 259●
return @types.ImageFormat::Heif - 260
} - 261
} - 262
} - 263
// JPEG-XL: FF 0A (codestream) or 00 00 00 0C 4A 58 4C 20 (container) - 264
if b0 == 0xFF && b1 == 0x0A { - 265●
return @types.ImageFormat::JpegXl - 266
} - 267●
if b0 == 0x00 && b1 == 0x00 && data.length() >= 8 { - 268●
let b4 = data[4].to_int() - 269●
let b5 = data[5].to_int() - 270●
let b6 = data[6].to_int() - 271●
let b7 = data[7].to_int() - 272
if b4 == 0x4A && b5 == 0x58 && b6 == 0x4C && b7 == 0x20 { - 273●
return @types.ImageFormat::JpegXl - 274
} - 275
} - 276
@types.ImageFormat::Unknown - 277
}