image.core\types\traits.mbt
89.5% · 77 / 86 points · 330 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 类型特征 types/traits - 3
/// - 4
/// 图像处理库子模块 Image processing library submodule - 5
- 6
///| - 7
/// 图像统一接口 trait,抽象 8-bit / 16-bit / float 三种像素精度 - 8
/// - 9
/// 所有图像类型(Image / Image16 / ImageF / AnyImage)均实现此 trait, - 10
/// 允许泛型算法以统一方式访问图像几何信息与像素数据 - 11
pub(open) trait ImageLike { - 12
fn width(Self) -> Int32 - 13
fn height(Self) -> Int32 - 14
fn channels(Self) -> Int32 - 15
fn data(Self) -> Bytes - 16
fn bytes_per_pixel(Self) -> Int32 - 17
fn pixel_count(Self) -> Int32 = _ - 18
fn byte_count(Self) -> Int32 = _ - 19
fn is_grayscale(Self) -> Bool = _ - 20
fn is_rgb(Self) -> Bool = _ - 21
fn is_rgba(Self) -> Bool = _ - 22
} - 23
- 24
///| - 25
impl ImageLike with fn pixel_count(self) { - 26●
ImageLike::width(self) * ImageLike::height(self) * ImageLike::channels(self) - 27
} - 28
- 29
///| - 30
impl ImageLike with fn byte_count(self) { - 31●
ImageLike::pixel_count(self) * ImageLike::bytes_per_pixel(self) - 32
} - 33
- 34
///| - 35
impl ImageLike with fn is_grayscale(self) { - 36●
ImageLike::channels(self) == 1 - 37
} - 38
- 39
///| - 40
impl ImageLike with fn is_rgb(self) { - 41●
ImageLike::channels(self) == 3 - 42
} - 43
- 44
///| - 45
impl ImageLike with fn is_rgba(self) { - 46●
ImageLike::channels(self) == 4 - 47
} - 48
- 49
// Image (8-bit) 实现 ImageLike - 50
- 51
///| - 52
pub impl ImageLike for Image with fn width(self) { - 53●
self.width - 54
} - 55
- 56
///| - 57
pub impl ImageLike for Image with fn height(self) { - 58●
self.height - 59
} - 60
- 61
///| - 62
pub impl ImageLike for Image with fn channels(self) { - 63●
self.channels - 64
} - 65
- 66
///| - 67
pub impl ImageLike for Image with fn data(self) { - 68○
self.data - 69
} - 70
- 71
///| - 72
pub impl ImageLike for Image with fn bytes_per_pixel(_) { - 73●
1 - 74
} - 75
- 76
///| - 77
pub extend Image with ImageLike::{ - 78
width, - 79
height, - 80
channels, - 81
data, - 82
bytes_per_pixel, - 83
pixel_count, - 84
byte_count, - 85
is_grayscale, - 86
is_rgb, - 87
is_rgba, - 88
} - 89
- 90
// Image16 (16-bit) 实现 ImageLike - 91
- 92
///| - 93
pub impl ImageLike for Image16 with fn width(self) { - 94●
self.width - 95
} - 96
- 97
///| - 98
pub impl ImageLike for Image16 with fn height(self) { - 99●
self.height - 100
} - 101
- 102
///| - 103
pub impl ImageLike for Image16 with fn channels(self) { - 104●
self.channels - 105
} - 106
- 107
///| - 108
pub impl ImageLike for Image16 with fn data(self) { - 109○
self.data - 110
} - 111
- 112
///| - 113
pub impl ImageLike for Image16 with fn bytes_per_pixel(_) { - 114●
2 - 115
} - 116
- 117
///| - 118
pub extend Image16 with ImageLike::{ - 119
width, - 120
height, - 121
channels, - 122
data, - 123
bytes_per_pixel, - 124
pixel_count, - 125
byte_count, - 126
is_grayscale, - 127
is_rgb, - 128
is_rgba, - 129
} - 130
- 131
// ImageF (32-bit float) 实现 ImageLike - 132
- 133
///| - 134
pub impl ImageLike for ImageF with fn width(self) { - 135●
self.width - 136
} - 137
- 138
///| - 139
pub impl ImageLike for ImageF with fn height(self) { - 140●
self.height - 141
} - 142
- 143
///| - 144
pub impl ImageLike for ImageF with fn channels(self) { - 145●
self.channels - 146
} - 147
- 148
///| - 149
pub impl ImageLike for ImageF with fn data(self) { - 150○
self.data - 151
} - 152
- 153
///| - 154
pub impl ImageLike for ImageF with fn bytes_per_pixel(_) { - 155●
4 - 156
} - 157
- 158
///| - 159
pub extend ImageF with ImageLike::{ - 160
width, - 161
height, - 162
channels, - 163
data, - 164
bytes_per_pixel, - 165
pixel_count, - 166
byte_count, - 167
is_grayscale, - 168
is_rgb, - 169
is_rgba, - 170
} - 171
- 172
///| - 173
/// 统一图像容器,包装三种像素精度的图像 - 174
/// - 175
/// 用于需要统一处理不同精度图像的场景,如编解码器接口 - 176
/// `#non_exhaustive` 表示未来可能添加新构造器 - 177
#derive(Eq, @debug.Debug) - 178
#non_exhaustive - 179
pub(all) enum AnyImage { - 180
U8(Image) - 181
U16(Image16) - 182
F32(ImageF) - 183
} - 184
- 185
///| - 186
pub extend AnyImage with @debug.Debug::{to_repr} - 187
- 188
///| - 189
pub extend AnyImage with Eq::{not_equal, equal} - 190
- 191
// AnyImage 实现 ImageLike - 192
- 193
///| - 194
pub impl ImageLike for AnyImage with fn width(self) { - 195●
match self { - 196●
U8(img) => img.width - 197●
U16(img) => img.width - 198●
F32(img) => img.width - 199
} - 200
} - 201
- 202
///| - 203
pub impl ImageLike for AnyImage with fn height(self) { - 204●
match self { - 205●
U8(img) => img.height - 206○
U16(img) => img.height - 207●
F32(img) => img.height - 208
} - 209
} - 210
- 211
///| - 212
pub impl ImageLike for AnyImage with fn channels(self) { - 213●
match self { - 214●
U8(img) => img.channels - 215●
U16(img) => img.channels - 216●
F32(img) => img.channels - 217
} - 218
} - 219
- 220
///| - 221
pub impl ImageLike for AnyImage with fn data(self) { - 222○
match self { - 223○
U8(img) => img.data - 224○
U16(img) => img.data - 225○
F32(img) => img.data - 226
} - 227
} - 228
- 229
///| - 230
pub impl ImageLike for AnyImage with fn bytes_per_pixel(self) { - 231●
match self { - 232●
U8(_) => 1 - 233●
U16(_) => 2 - 234●
F32(_) => 4 - 235
} - 236
} - 237
- 238
///| - 239
pub extend AnyImage with ImageLike::{ - 240
width, - 241
height, - 242
channels, - 243
data, - 244
bytes_per_pixel, - 245
pixel_count, - 246
byte_count, - 247
is_grayscale, - 248
is_rgb, - 249
is_rgba, - 250
} - 251
- 252
///| - 253
/// 将 Image 升级为 AnyImage::U8 - 254
pub fn image_to_any(img : Image) -> AnyImage { - 255●
U8(img) - 256
} - 257
- 258
///| - 259
/// 将 Image16 升级为 AnyImage::U16 - 260
pub fn image16_to_any(img : Image16) -> AnyImage { - 261●
U16(img) - 262
} - 263
- 264
///| - 265
/// 将 ImageF 升级为 AnyImage::F32 - 266
pub fn imagef_to_any(img : ImageF) -> AnyImage { - 267●
F32(img) - 268
} - 269
- 270
///| - 271
/// 将 AnyImage 转换为 8-bit Image - 272
/// - 273
/// U8 直接返回;U16 截断高位;F32 量化到 [0,255] - 274
pub fn any_image_to_u8(img : AnyImage) -> Image { - 275●
match img { - 276●
U8(im) => im - 277●
U16(im) => { - 278
let w = im.width - 279
let h = im.height - 280
let c = im.channels - 281
let n = w * h * c - 282
let buf : Array[Byte] = [] - 283
for i in 0..<n { - 284●
let lo = im.data[i * 2].to_int() - 285●
let hi = im.data[i * 2 + 1].to_int() - 286
let v = (hi << 8) | lo - 287●
let clamped = if v > 255 { 255 } else { v } - 288●
buf.push(clamped.to_byte()) - 289
} - 290●
Image::{ width: w, height: h, channels: c, data: Bytes::from_array(buf), } - 291
} - 292●
F32(im) => { - 293
let w = im.width - 294
let h = im.height - 295
let c = im.channels - 296
let n = w * h * c - 297
let buf : Array[Byte] = [] - 298
for i in 0..<n { - 299●
let idx = i * 4 - 300●
let bits = (im.data[idx + 3].to_int() << 24) | - 301●
(im.data[idx + 2].to_int() << 16) | - 302●
(im.data[idx + 1].to_int() << 8) | - 303●
im.data[idx].to_int() - 304●
let f = Float::reinterpret_from_int(bits) - 305●
let v = (f * 255.0F + 0.5F).to_int() - 306●
let clamped = if v > 255 { 255 } else if v < 0 { 0 } else { v } - 307●
buf.push(clamped.to_byte()) - 308
} - 309●
Image::{ width: w, height: h, channels: c, data: Bytes::from_array(buf), } - 310
} - 311
} - 312
} - 313
- 314
///| - 315
/// 将 8-bit Image 转换为 ImageF(归一化到 [0,1]) - 316
pub fn hdr_from_u8(img : Image) -> ImageF { - 317●
let w = img.width - 318
let h = img.height - 319
let c = img.channels - 320
let n = w * h * c - 321
let buf : Array[Byte] = [] - 322
for i in 0..<n { - 323●
let f = Float::from_int(img.data[i].to_int()) / 255.0F - 324●
let bytes = f.to_le_bytes() - 325
for j in 0..<4 { - 326●
buf.push(bytes[j]) - 327
} - 328
} - 329●
ImageF::{ width: w, height: h, channels: c, data: Bytes::from_array(buf), } - 330
}