image.codec\lib\unified_info.mbt
75.5% · 40 / 53 points · 269 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 统一信息 lib/unified_info - 3
/// - 4
/// 编解码工具模块 - 5
/// 编解码工具模块 Codec utility module - 6
// v246.0.0 统一 info API:整合基本信息 + 格式特定扩展信息 - 7
- 8
///| - 9
/// 统一图像信息(跨格式通用字段) - 10
#derive(Eq, @debug.Debug) - 11
pub(all) struct UnifiedImageInfo { - 12
format : @types.ImageFormat - 13
format_name : String - 14
width : Int32 - 15
height : Int32 - 16
channels : Int32 - 17
bit_depth : Int32 - 18
has_alpha : Bool - 19
} - 20
- 21
///| - 22
pub extend UnifiedImageInfo with @debug.Debug::{to_repr} - 23
- 24
///| - 25
pub extend UnifiedImageInfo with Eq::{not_equal, equal} - 26
- 27
///| - 28
/// 统一图像信息探测 - 29
/// 自动检测格式并返回跨格式通用信息(尺寸/通道/位深/alpha) - 30
/// 未知格式 raise LoadError::UnsupportedFormat - 31
pub fn get_image_info(data : Bytes) -> UnifiedImageInfo raise @types.LoadError { - 32●
let format = detect_format(data) - 33●
let ext = @codec.get_image_info_extended(data) - 34●
let format_name = match format { - 35●
@types.ImageFormat::Png => "PNG" - 36●
@types.ImageFormat::Jpeg => "JPEG" - 37●
@types.ImageFormat::Bmp => "BMP" - 38●
@types.ImageFormat::Gif => "GIF" - 39●
@types.ImageFormat::Tga => "TGA" - 40○
@types.ImageFormat::Psd => "PSD" - 41●
@types.ImageFormat::Hdr => "HDR" - 42●
@types.ImageFormat::Pnm => "PNM" - 43●
@types.ImageFormat::Qoi => "QOI" - 44●
@types.ImageFormat::Webp => "WebP" - 45●
@types.ImageFormat::Tiff => "TIFF" - 46●
@types.ImageFormat::Ico => "ICO" - 47○
@types.ImageFormat::Cur => "CUR" - 48●
@types.ImageFormat::Icns => "ICNS" - 49●
@types.ImageFormat::Apng => "APNG" - 50○
@types.ImageFormat::Avif => "AVIF" - 51○
@types.ImageFormat::Heif => "HEIF" - 52○
@types.ImageFormat::JpegXl => "JPEG-XL" - 53○
@types.ImageFormat::Unknown => "unknown" - 54○
@types.ImageFormat::.. => "unknown" - 55
} - 56●
match ext { - 57
@codec.ImageInfoExtended::Avif(e) => - 58○
UnifiedImageInfo::{ - 59
format, - 60
format_name, - 61
width: e.width, - 62
height: e.height, - 63
channels: e.num_channels, - 64
bit_depth: e.bit_depth, - 65
has_alpha: e.has_alpha, - 66
} - 67
@codec.ImageInfoExtended::Heif(e) => - 68○
UnifiedImageInfo::{ - 69
format, - 70
format_name, - 71
width: e.width, - 72
height: e.height, - 73
channels: e.num_channels, - 74
bit_depth: e.bit_depth, - 75
has_alpha: e.has_alpha, - 76
} - 77
@codec.ImageInfoExtended::JpegXl(e) => - 78○
UnifiedImageInfo::{ - 79
format, - 80
format_name, - 81
width: e.width, - 82
height: e.height, - 83
channels: e.num_channels, - 84
bit_depth: e.bit_depth, - 85
has_alpha: e.has_alpha, - 86
} - 87
@codec.ImageInfoExtended::Webp(e) => - 88●
UnifiedImageInfo::{ - 89
format, - 90
format_name, - 91
width: e.width, - 92
height: e.height, - 93
channels: e.num_channels, - 94
bit_depth: 8, - 95
has_alpha: e.has_alpha, - 96
} - 97
@codec.ImageInfoExtended::Tiff(e) => - 98●
UnifiedImageInfo::{ - 99
format, - 100
format_name, - 101
width: e.width, - 102
height: e.height, - 103
channels: e.num_channels, - 104
bit_depth: e.bit_depth, - 105
has_alpha: e.has_alpha, - 106
} - 107
@codec.ImageInfoExtended::Psd(e) => - 108○
UnifiedImageInfo::{ - 109
format, - 110
format_name, - 111
width: e.width, - 112
height: e.height, - 113
channels: e.num_channels, - 114
bit_depth: e.bit_depth, - 115
has_alpha: e.has_alpha, - 116
} - 117
@codec.ImageInfoExtended::Bmp(e) => - 118●
UnifiedImageInfo::{ - 119
format, - 120
format_name, - 121
width: e.width, - 122
height: e.height, - 123
channels: e.num_channels, - 124
bit_depth: e.bit_depth, - 125
has_alpha: e.has_alpha, - 126
} - 127
@codec.ImageInfoExtended::Gif(e) => - 128●
UnifiedImageInfo::{ - 129
format, - 130
format_name, - 131
width: e.width, - 132
height: e.height, - 133
channels: e.num_channels, - 134
bit_depth: 8, - 135
has_alpha: e.has_alpha, - 136
} - 137
@codec.ImageInfoExtended::Png(e) => - 138●
UnifiedImageInfo::{ - 139
format, - 140
format_name, - 141
width: e.width, - 142
height: e.height, - 143
channels: e.num_channels, - 144
bit_depth: e.bit_depth, - 145
has_alpha: e.has_alpha, - 146
} - 147
@codec.ImageInfoExtended::Jpeg(e) => - 148●
UnifiedImageInfo::{ - 149
format, - 150
format_name, - 151
width: e.width, - 152
height: e.height, - 153
channels: e.num_channels, - 154
bit_depth: e.precision, - 155
has_alpha: e.has_alpha, - 156
} - 157
@codec.ImageInfoExtended::Qoi(e) => - 158●
UnifiedImageInfo::{ - 159
format, - 160
format_name, - 161
width: e.width, - 162
height: e.height, - 163
channels: e.num_channels, - 164
bit_depth: 8, - 165
has_alpha: e.has_alpha, - 166
} - 167
@codec.ImageInfoExtended::Tga(e) => - 168●
UnifiedImageInfo::{ - 169
format, - 170
format_name, - 171
width: e.width, - 172
height: e.height, - 173
channels: e.num_channels, - 174
bit_depth: e.bit_depth, - 175
has_alpha: e.has_alpha, - 176
} - 177
@codec.ImageInfoExtended::Hdr(e) => - 178●
UnifiedImageInfo::{ - 179
format, - 180
format_name, - 181
width: e.width, - 182
height: e.height, - 183
channels: e.num_channels, - 184
bit_depth: 32, - 185
has_alpha: e.has_alpha, - 186
} - 187
@codec.ImageInfoExtended::Pnm(e) => - 188●
UnifiedImageInfo::{ - 189
format, - 190
format_name, - 191
width: e.width, - 192
height: e.height, - 193
channels: e.num_channels, - 194
bit_depth: e.bit_depth, - 195
has_alpha: e.has_alpha, - 196
} - 197
@codec.ImageInfoExtended::Ico(e) => - 198●
UnifiedImageInfo::{ - 199
format, - 200
format_name, - 201
width: e.width, - 202
height: e.height, - 203
channels: e.num_channels, - 204●
bit_depth: if e.bit_depths.length() > 0 { - 205●
e.bit_depths[0] - 206
} else { - 207○
0 - 208
}, - 209
has_alpha: e.has_alpha, - 210
} - 211
@codec.ImageInfoExtended::Cur(e) => - 212●
UnifiedImageInfo::{ - 213
format, - 214
format_name, - 215
width: e.width, - 216
height: e.height, - 217
channels: e.num_channels, - 218
bit_depth: 8, - 219
has_alpha: e.has_alpha, - 220
} - 221
@codec.ImageInfoExtended::Icns(e) => - 222●
UnifiedImageInfo::{ - 223
format, - 224
format_name, - 225
width: e.width, - 226
height: e.height, - 227
channels: e.num_channels, - 228
bit_depth: 32, - 229
has_alpha: e.has_alpha, - 230
} - 231
@codec.ImageInfoExtended::Apng(e) => - 232●
UnifiedImageInfo::{ - 233
format, - 234
format_name, - 235
width: e.width, - 236
height: e.height, - 237
channels: e.num_channels, - 238
bit_depth: e.bit_depth, - 239
has_alpha: e.has_alpha, - 240
} - 241
@codec.ImageInfoExtended::.. => - 242○
UnifiedImageInfo::{ - 243
format, - 244
format_name: "unknown", - 245
width: 0, - 246
height: 0, - 247
channels: 0, - 248
bit_depth: 0, - 249
has_alpha: false, - 250
} - 251
} - 252
} - 253
- 254
///| - 255
/// 人类可读的图像信息摘要 - 256
/// 返回格式如 "PNG 4x4 3ch 8bit" - 257
pub fn get_image_summary(data : Bytes) -> String raise @types.LoadError { - 258●
let info = get_image_info(data) - 259
info.format_name + - 260
" " + - 261●
info.width.to_string() + - 262
"x" + - 263●
info.height.to_string() + - 264
" " + - 265●
info.channels.to_string() + - 266
"ch " + - 267●
info.bit_depth.to_string() + - 268
"bit" - 269
}