image.process\color\color_convert.mbt
92.5% · 136 / 147 points · 466 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 色彩转换 color convert - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// color_convert.mbt:色彩空间与通道操作 - 7
// 本文件包含两类操作: - 8
// 1. 色彩空间转换:rgb_to_hsv/hsv_to_rgb、rgb_to_xyz/xyz_to_rgb、rgb_to_lab/lab_to_rgb、 - 9
// rgb_to_ycbcr/ycbcr_to_rgb、rgb_to_cmyk/cmyk_to_rgb 等 - 10
// 2. 通道操作:to_grayscale/to_rgb/to_rgba、premultiply_alpha/unpremultiply_alpha 等 - 11
- 12
///| - 13
/// 转为灰度图(channels=1) - 14
/// 使用 ITU-R BT.601 亮度公式: Y = 0.299R + 0.587G + 0.114B - 15
/// 优化:直接从 Bytes 读取,避免额外数组分配 - 16
pub fn to_grayscale(img : @types.Image) -> @types.Image raise @types.LoadError { - 17
// 输入验证 - 18●
if img.width <= 0 || img.height <= 0 { - 19●
raise @types.LoadError::DecodeFailed( - 20
"to_grayscale: 图像尺寸必须大于0", - 21
) - 22
} - 23
if img.channels < 1 || img.channels > 4 { - 24●
raise @types.LoadError::DecodeFailed( - 25
"to_grayscale: 通道数必须在1-4之间", - 26
) - 27
} - 28
let expected_size = img.width * img.height * img.channels - 29●
if img.data.length() != expected_size { - 30●
raise @types.LoadError::DecodeFailed( - 31
"to_grayscale: 数据大小不匹配,期望 " + - 32●
expected_size.to_string() + - 33
",实际 " + - 34●
img.data.length().to_string(), - 35
) - 36
} - 37
if img.channels == 1 { - 38●
return img - 39
} - 40
let pixel_count = img.width * img.height - 41
let ch = img.channels - 42●
let out : Array[Byte] = Array::make(pixel_count, b'\x00') - 43
let data = img.data - 44
for i in 0..<pixel_count { - 45●
let base = i * ch - 46●
let r = data[base].to_int() - 47●
let g = data[base + 1].to_int() - 48●
let b = data[base + 2].to_int() - 49
let gray = (r * 299 + g * 587 + b * 114) / 1000 - 50●
out[i] = gray.to_byte() - 51
} - 52
@types.Image::{ - 53
width: img.width, - 54
height: img.height, - 55
channels: 1, - 56●
data: Bytes::from_array(out), - 57
} - 58
} - 59
- 60
///| - 61
/// 去除 alpha 通道(RGBA → RGB,channels=3) - 62
pub fn to_rgb(img : @types.Image) -> @types.Image raise @types.LoadError { - 63
// 输入验证 - 64●
if img.width <= 0 || img.height <= 0 { - 65●
raise @types.LoadError::DecodeFailed("to_rgb: 图像尺寸必须大于0") - 66
} - 67
if img.channels < 1 || img.channels > 4 { - 68○
raise @types.LoadError::DecodeFailed("to_rgb: 通道数必须在1-4之间") - 69
} - 70
let expected_size = img.width * img.height * img.channels - 71●
if img.data.length() != expected_size { - 72●
raise @types.LoadError::DecodeFailed( - 73
"to_rgb: 数据大小不匹配,期望 " + - 74●
expected_size.to_string() + - 75
",实际 " + - 76●
img.data.length().to_string(), - 77
) - 78
} - 79
if img.channels == 3 { - 80●
return img - 81
} - 82
if img.channels < 3 { - 83●
return img - 84
} - 85
let pixel_count = img.width * img.height - 86●
let out : Array[Byte] = Array::make(pixel_count * 3, b'\x00') - 87
for i in 0..<pixel_count { - 88●
out[i * 3] = img.data[i * img.channels] - 89
out[i * 3 + 1] = img.data[i * img.channels + 1] - 90
out[i * 3 + 2] = img.data[i * img.channels + 2] - 91
} - 92
@types.Image::{ - 93
width: img.width, - 94
height: img.height, - 95
channels: 3, - 96●
data: Bytes::from_array(out), - 97
} - 98
} - 99
- 100
///| - 101
/// 添加 alpha 通道(RGB → RGBA,channels=4,alpha=255) - 102
pub fn to_rgba(img : @types.Image) -> @types.Image raise @types.LoadError { - 103
// 输入验证 - 104●
if img.width <= 0 || img.height <= 0 { - 105●
raise @types.LoadError::DecodeFailed("to_rgba: 图像尺寸必须大于0") - 106
} - 107
if img.channels < 1 || img.channels > 4 { - 108○
raise @types.LoadError::DecodeFailed("to_rgba: 通道数必须在1-4之间") - 109
} - 110
let expected_size = img.width * img.height * img.channels - 111●
if img.data.length() != expected_size { - 112●
raise @types.LoadError::DecodeFailed( - 113
"to_rgba: 数据大小不匹配,期望 " + - 114●
expected_size.to_string() + - 115
",实际 " + - 116●
img.data.length().to_string(), - 117
) - 118
} - 119
if img.channels == 4 { - 120●
return img - 121
} - 122
let pixel_count = img.width * img.height - 123●
let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00') - 124
for i in 0..<pixel_count { - 125●
let src_ch = img.channels - 126
out[i * 4] = img.data[i * src_ch] - 127
out[i * 4 + 1] = if src_ch >= 2 { - 128●
img.data[i * src_ch + 1] - 129
} else { - 130●
b'\x00' - 131
} - 132
out[i * 4 + 2] = if src_ch >= 3 { - 133●
img.data[i * src_ch + 2] - 134
} else { - 135●
b'\x00' - 136
} - 137
out[i * 4 + 3] = b'\xFF' - 138
} - 139
@types.Image::{ - 140
width: img.width, - 141
height: img.height, - 142
channels: 4, - 143●
data: Bytes::from_array(out), - 144
} - 145
} - 146
- 147
///| - 148
/// 预乘 alpha(RGBA → RGBA premultiplied) - 149
/// 要求 channels=4 - 150
pub fn premultiply_alpha( - 151
img : @types.Image, - 152
) -> @types.Image raise @types.LoadError { - 153
// 输入验证 - 154●
if img.width <= 0 || img.height <= 0 { - 155●
raise @types.LoadError::DecodeFailed( - 156
"premultiply_alpha: 图像尺寸必须大于0", - 157
) - 158
} - 159
if img.channels != 4 { - 160●
raise @types.LoadError::DecodeFailed( - 161
"premultiply_alpha 需要 channels=4 (RGBA)", - 162
) - 163
} - 164
let expected_size = img.width * img.height * img.channels - 165●
if img.data.length() != expected_size { - 166○
raise @types.LoadError::DecodeFailed( - 167
"premultiply_alpha: 数据大小不匹配,期望 " + - 168○
expected_size.to_string() + - 169
",实际 " + - 170○
img.data.length().to_string(), - 171
) - 172
} - 173
let pixel_count = img.width * img.height - 174●
let data_int : Array[Int32] = Array::make(pixel_count * 4, 0) - 175
for i in 0..<(pixel_count * 4) { - 176●
data_int[i] = img.data[i].to_int() - 177
} - 178●
let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00') - 179
for i in 0..<pixel_count { - 180●
let r = data_int[i * 4] - 181
let g = data_int[i * 4 + 1] - 182
let b = data_int[i * 4 + 2] - 183
let a = data_int[i * 4 + 3] - 184●
out[i * 4] = (r * a / 255).to_byte() - 185●
out[i * 4 + 1] = (g * a / 255).to_byte() - 186●
out[i * 4 + 2] = (b * a / 255).to_byte() - 187●
out[i * 4 + 3] = a.to_byte() - 188
} - 189
@types.Image::{ - 190
width: img.width, - 191
height: img.height, - 192
channels: 4, - 193●
data: Bytes::from_array(out), - 194
} - 195
} - 196
- 197
///| - 198
/// 反预乘 alpha(RGBA premA → RGBA unpremultiplied) - 199
/// 要求 channels=4 - 200
pub fn unpremultiply_alpha( - 201
img : @types.Image, - 202
) -> @types.Image raise @types.LoadError { - 203
// 输入验证 - 204●
if img.width <= 0 || img.height <= 0 { - 205●
raise @types.LoadError::DecodeFailed( - 206
"unpremultiply_alpha: 图像尺寸必须大于0", - 207
) - 208
} - 209
if img.channels != 4 { - 210●
raise @types.LoadError::DecodeFailed( - 211
"unpremultiply_alpha 需要 channels=4 (RGBA)", - 212
) - 213
} - 214
let expected_size = img.width * img.height * img.channels - 215●
if img.data.length() != expected_size { - 216○
raise @types.LoadError::DecodeFailed( - 217
"unpremultiply_alpha: 数据大小不匹配,期望 " + - 218○
expected_size.to_string() + - 219
",实际 " + - 220○
img.data.length().to_string(), - 221
) - 222
} - 223
let pixel_count = img.width * img.height - 224●
let data_int : Array[Int32] = Array::make(pixel_count * 4, 0) - 225
for i in 0..<(pixel_count * 4) { - 226●
data_int[i] = img.data[i].to_int() - 227
} - 228●
let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00') - 229
for i in 0..<pixel_count { - 230●
let r = data_int[i * 4] - 231
let g = data_int[i * 4 + 1] - 232
let b = data_int[i * 4 + 2] - 233
let a = data_int[i * 4 + 3] - 234
if a == 0 { - 235●
out[i * 4] = b'\x00' - 236
out[i * 4 + 1] = b'\x00' - 237
out[i * 4 + 2] = b'\x00' - 238
out[i * 4 + 3] = b'\x00' - 239
} else { - 240●
out[i * 4] = (r * 255 / a).to_byte() - 241●
out[i * 4 + 1] = (g * 255 / a).to_byte() - 242●
out[i * 4 + 2] = (b * 255 / a).to_byte() - 243●
out[i * 4 + 3] = a.to_byte() - 244
} - 245
} - 246
@types.Image::{ - 247
width: img.width, - 248
height: img.height, - 249
channels: 4, - 250●
data: Bytes::from_array(out), - 251
} - 252
} - 253
- 254
///| - 255
/// RGB → YCbCr(ITU-R BT.601)。返回 (Y, Cb, Cr),范围 0-255。 - 256
pub fn rgb_to_ycbcr( - 257
r : Int32, - 258
g : Int32, - 259
b : Int32, - 260
) -> (Int32, Int32, Int32) raise @types.LoadError { - 261
// 输入验证 - 262●
if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 { - 263●
raise @types.LoadError::DecodeFailed( - 264
"rgb_to_ycbcr: r,g,b必须在[0,255]范围内", - 265
) - 266
} - 267
let y = (299 * r + 587 * g + 114 * b) / 1000 - 268
let cb = (-169 * r - 331 * g + 500 * b) / 1000 + 128 - 269
let cr = (500 * r - 419 * g - 81 * b) / 1000 + 128 - 270●
(y.clamp(min=0, max=255), cb.clamp(min=0, max=255), cr.clamp(min=0, max=255)) - 271
} - 272
- 273
///| - 274
/// YCbCr → RGB(ITU-R BT.601)。输入范围 0-255。 - 275
pub fn ycbcr_to_rgb( - 276
y : Int32, - 277
cb : Int32, - 278
cr : Int32, - 279
) -> (Int32, Int32, Int32) raise @types.LoadError { - 280
// 输入验证 - 281●
if y < 0 || y > 255 || cb < 0 || cb > 255 || cr < 0 || cr > 255 { - 282●
raise @types.LoadError::DecodeFailed( - 283
"ycbcr_to_rgb: y,cb,cr必须在[0,255]范围内", - 284
) - 285
} - 286
let cb0 = cb - 128 - 287
let cr0 = cr - 128 - 288
let r = y + 1402 * cr0 / 1000 - 289
let g = y - 344 * cb0 / 1000 - 714 * cr0 / 1000 - 290
let b = y + 1772 * cb0 / 1000 - 291●
(r.clamp(min=0, max=255), g.clamp(min=0, max=255), b.clamp(min=0, max=255)) - 292
} - 293
- 294
///| - 295
/// RGB → XYZ(sRGB, D65 白点)。返回 (X, Y, Z),范围 0.0-1.0。 - 296
pub fn rgb_to_xyz( - 297
r : Int32, - 298
g : Int32, - 299
b : Int32, - 300
) -> (Float, Float, Float) raise @types.LoadError { - 301
// 输入验证 - 302●
if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 { - 303●
raise @types.LoadError::DecodeFailed( - 304
"rgb_to_xyz: r,g,b必须在[0,255]范围内", - 305
) - 306
} - 307
fn linearize(c : Int32) -> Float { - 308●
let cf = Float::from_int(c) / 255.0F - 309
if cf <= 0.04045F { - 310●
cf / 12.92F - 311
} else { - 312●
@math.powf((cf + 0.055F) / 1.055F, 2.4F) - 313
} - 314
} - 315●
let rl = linearize(r) - 316●
let gl = linearize(g) - 317●
let bl = linearize(b) - 318
let x = 0.4124F * rl + 0.3576F * gl + 0.1805F * bl - 319
let y = 0.2126F * rl + 0.7152F * gl + 0.0722F * bl - 320
let z = 0.0193F * rl + 0.1192F * gl + 0.9505F * bl - 321
(x, y, z) - 322
} - 323
- 324
///| - 325
/// XYZ → RGB(sRGB, D65 白点)。输入范围 0.0-1.0。 - 326
pub fn xyz_to_rgb( - 327
x : Float, - 328
y : Float, - 329
z : Float, - 330
) -> (Int32, Int32, Int32) raise @types.LoadError { - 331
// 输入验证 - 332●
if x < 0.0F || x > 1.0F || y < 0.0F || y > 1.0F || z < 0.0F || z > 1.0F { - 333●
raise @types.LoadError::DecodeFailed( - 334
"xyz_to_rgb: x,y,z必须在[0,1]范围内", - 335
) - 336
} - 337
let rl = 3.2406F * x - 1.5372F * y - 0.4986F * z - 338
let gl = -0.9689F * x + 1.8758F * y + 0.0415F * z - 339
let bl = 0.0557F * x - 0.2040F * y + 1.0570F * z - 340
fn srgb(c : Float) -> Int32 { - 341●
let v = if c <= 0.0031308F { - 342●
12.92F * c - 343
} else { - 344●
1.055F * @math.powf(c, 1.0F / 2.4F) - 0.055F - 345
} - 346●
(v * 255.0F + 0.5F).to_int().clamp(min=0, max=255) - 347
} - 348●
(srgb(rl), srgb(gl), srgb(bl)) - 349
} - 350
- 351
///| - 352
/// RGB → Lab(CIELAB, D65 白点)。返回 (L, a, b),L 范围 0-100,a/b 范围 -128-127。 - 353
pub fn rgb_to_lab( - 354
r : Int32, - 355
g : Int32, - 356
b : Int32, - 357
) -> (Float, Float, Float) raise @types.LoadError { - 358
// 输入验证 - 359●
if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 { - 360●
raise @types.LoadError::DecodeFailed( - 361
"rgb_to_lab: r,g,b必须在[0,255]范围内", - 362
) - 363
} - 364●
let (x, y, z) = rgb_to_xyz(r, g, b) - 365
let xn = 0.9505F - 366
let yn = 1.0F - 367
let zn = 1.089F - 368
fn f(t : Float) -> Float { - 369●
if t > 0.008856F { - 370●
@math.powf(t, 1.0F / 3.0F) - 371
} else { - 372●
7.787F * t + 16.0F / 116.0F - 373
} - 374
} - 375●
let fx = f(x / xn) - 376●
let fy = f(y / yn) - 377●
let fz = f(z / zn) - 378
let l = 116.0F * fy - 16.0F - 379
let a = 500.0F * (fx - fy) - 380
let b_val = 200.0F * (fy - fz) - 381
(l, a, b_val) - 382
} - 383
- 384
///| - 385
/// Lab → RGB(CIELAB, D65 白点)。输入 L 范围 0-100,a/b 范围 -128-127。 - 386
pub fn lab_to_rgb( - 387
l : Float, - 388
a : Float, - 389
b : Float, - 390
) -> (Int32, Int32, Int32) raise @types.LoadError { - 391
// 输入验证 - 392●
if l < 0.0F || l > 100.0F { - 393●
raise @types.LoadError::DecodeFailed( - 394
"lab_to_rgb: L必须在[0,100]范围内", - 395
) - 396
} - 397
let yn = 1.0F - 398
let xn = 0.9505F - 399
let zn = 1.089F - 400
let fy = (l + 16.0F) / 116.0F - 401
let fx = a / 500.0F + fy - 402
let fz = fy - b / 200.0F - 403
fn f_inv(t : Float) -> Float { - 404●
if @math.powf(t, 3.0F) > 0.008856F { - 405●
@math.powf(t, 3.0F) - 406
} else { - 407●
(t - 16.0F / 116.0F) / 7.787F - 408
} - 409
} - 410●
let x = xn * f_inv(fx) - 411●
let y = yn * f_inv(fy) - 412●
let z = zn * f_inv(fz) - 413●
xyz_to_rgb(x, y, z) - 414
} - 415
- 416
///| - 417
/// RGB → CMYK。返回 (C, M, Y, K),范围 0-255。 - 418
pub fn rgb_to_cmyk( - 419
r : Int32, - 420
g : Int32, - 421
b : Int32, - 422
) -> (Int32, Int32, Int32, Int32) raise @types.LoadError { - 423
// 输入验证 - 424●
if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 { - 425●
raise @types.LoadError::DecodeFailed( - 426
"rgb_to_cmyk: r,g,b必须在[0,255]范围内", - 427
) - 428
} - 429
let c = 255 - r - 430
let m = 255 - g - 431
let y = 255 - b - 432◐
let k = if c < m { if c < y { c } else { y } } else if m < y { m } else { y } - 433
if k == 255 { - 434●
(0, 0, 0, 255) - 435
} else { - 436●
let denom = 255 - k - 437
((c - k) * 255 / denom, (m - k) * 255 / denom, (y - k) * 255 / denom, k) - 438
} - 439
} - 440
- 441
///| - 442
/// CMYK → RGB。输入范围 0-255。 - 443
pub fn cmyk_to_rgb( - 444
c : Int32, - 445
m : Int32, - 446
y : Int32, - 447
k : Int32, - 448
) -> (Int32, Int32, Int32) raise @types.LoadError { - 449
// 输入验证 - 450●
if c < 0 || - 451
c > 255 || - 452
m < 0 || - 453
m > 255 || - 454
y < 0 || - 455
y > 255 || - 456
k < 0 || - 457
k > 255 { - 458●
raise @types.LoadError::DecodeFailed( - 459
"cmyk_to_rgb: c,m,y,k必须在[0,255]范围内", - 460
) - 461
} - 462
let r = 255 - (c * (255 - k) / 255 + k) - 463
let g = 255 - (m * (255 - k) / 255 + k) - 464
let b = 255 - (y * (255 - k) / 255 + k) - 465●
(r.clamp(min=0, max=255), g.clamp(min=0, max=255), b.clamp(min=0, max=255)) - 466
}