image.process\color\color_adjust.mbt
100% · 233 / 233 points · 547 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 adjust - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 色彩调整操作(纯 MoonBit) - 7
// 亮度/对比度/gamma/反色 + HSV/HSL 色彩空间转换 - 8
- 9
///| - 10
/// 亮度调整。delta ∈ [-255, 255],正值变亮,负值变暗。 - 11
/// 仅影响颜色通道(前 min(channels,3) 个),alpha 通道不变。 - 12
/// 亮度调整。delta ∈ [-255, 255],正值增亮,负值减暗。 Brightness adjustment. delta ∈ [-255, 255], positive=brighter, negative=darker. - 13
/// 仅影响颜色通道(前 min(channels,3) 个),alpha 通道不变。 Only affects color channels (first min(channels,3)), alpha channel unchanged. - 14
/// - 15
/// ```mbt check - 16
/// test "doc_adjust_brightness" { - 17
/// try { - 18
/// let data = Bytes::from_array([b'\x80', b'\x80', b'\x80']) - 19
/// let img = @types.Image::{ width: 1, height: 1, channels: 3, data, } - 20
/// let brightened = @color.adjust_brightness(img, 50) - 21
/// assert_eq(brightened.width, 1) - 22
/// assert_eq(brightened.channels, 3) - 23
/// } catch { - 24
/// _ => assert_true(false) - 25
/// } - 26
/// } - 27
/// ``` - 28
pub fn adjust_brightness( - 29
img : @types.Image, - 30
delta : Int32, - 31
) -> @types.Image raise @types.LoadError { - 32
// 输入验证 - 33●
if img.width <= 0 || img.height <= 0 { - 34●
raise @types.LoadError::DecodeFailed( - 35
"adjust_brightness: 图像尺寸必须大于0", - 36
) - 37
} - 38
if img.channels < 1 || img.channels > 4 { - 39●
raise @types.LoadError::DecodeFailed( - 40
"adjust_brightness: 通道数必须在1-4之间", - 41
) - 42
} - 43
let expected_size = img.width * img.height * img.channels - 44●
if img.data.length() != expected_size { - 45●
raise @types.LoadError::DecodeFailed( - 46
"adjust_brightness: 数据大小不匹配,期望 " + - 47●
expected_size.to_string() + - 48
",实际 " + - 49●
img.data.length().to_string(), - 50
) - 51
} - 52
let n = img.width * img.height - 53
let ch = img.channels - 54●
let color_ch = if ch >= 3 { 3 } else { ch } - 55●
let lut : Array[Byte] = Array::make(256, b'\x00') - 56
for i in 0..<256 { - 57●
lut[i] = @types.clamp_byte(i + delta) - 58
} - 59●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 60
for i in 0..<n { - 61●
for c in 0..<color_ch { - 62●
out[i * ch + c] = lut[img.data[i * ch + c].to_int()] - 63
} - 64
if ch == 4 { - 65●
out[i * ch + 3] = img.data[i * ch + 3] - 66
} - 67
} - 68
@types.Image::{ - 69
width: img.width, - 70
height: img.height, - 71
channels: ch, - 72●
data: Bytes::from_array(out), - 73
} - 74
} - 75
- 76
///| - 77
/// 对比度调整。factor=1.0 为原图,>1 增加对比度,<1 降低。 - 78
/// 以 128 为中心缩放。仅影响颜色通道,alpha 不变。 - 79
/// 优化:直接处理所有像素,避免内层循环 - 80
pub fn adjust_contrast( - 81
img : @types.Image, - 82
factor : Float, - 83
) -> @types.Image raise @types.LoadError { - 84
// 输入验证 - 85●
if img.width <= 0 || img.height <= 0 { - 86●
raise @types.LoadError::DecodeFailed( - 87
"adjust_contrast: 图像尺寸必须大于0", - 88
) - 89
} - 90
if img.channels < 1 || img.channels > 4 { - 91●
raise @types.LoadError::DecodeFailed( - 92
"adjust_contrast: 通道数必须在1-4之间", - 93
) - 94
} - 95
let expected_size = img.width * img.height * img.channels - 96●
if img.data.length() != expected_size { - 97●
raise @types.LoadError::DecodeFailed( - 98
"adjust_contrast: 数据大小不匹配,期望 " + - 99●
expected_size.to_string() + - 100
",实际 " + - 101●
img.data.length().to_string(), - 102
) - 103
} - 104
if factor < 0.0 { - 105●
raise @types.LoadError::DecodeFailed( - 106
"adjust_contrast: factor必须大于等于0", - 107
) - 108
} - 109
let n = img.width * img.height - 110
let ch = img.channels - 111●
let _ = if ch >= 3 { 3 } else { ch } - 112●
let lut : Array[Byte] = Array::make(256, b'\x00') - 113
for i in 0..<256 { - 114●
let adjusted = (Float::from_int(i - 128) * factor + 128.0F).to_int() - 115●
lut[i] = @types.clamp_byte(adjusted) - 116
} - 117
let total = n * ch - 118●
let out : Array[Byte] = Array::make(total, b'\x00') - 119
let data = img.data - 120
if ch == 4 { - 121
// RGBA:处理 RGB,保留 A - 122●
for i in 0..<n { - 123●
let base = i * 4 - 124●
out[base] = lut[data[base].to_int()] - 125●
out[base + 1] = lut[data[base + 1].to_int()] - 126●
out[base + 2] = lut[data[base + 2].to_int()] - 127
out[base + 3] = data[base + 3] - 128
} - 129●
} else if ch == 3 { - 130
// RGB - 131●
for i in 0..<n { - 132●
let base = i * 3 - 133●
out[base] = lut[data[base].to_int()] - 134●
out[base + 1] = lut[data[base + 1].to_int()] - 135●
out[base + 2] = lut[data[base + 2].to_int()] - 136
} - 137
} else { - 138
// 单通道 - 139●
for i in 0..<n { - 140●
out[i] = lut[data[i].to_int()] - 141
} - 142
} - 143
@types.Image::{ - 144
width: img.width, - 145
height: img.height, - 146
channels: ch, - 147●
data: Bytes::from_array(out), - 148
} - 149
} - 150
- 151
///| - 152
/// Gamma 校正。gamma=1.0 为原图,>1 变暗,<1 变亮。 - 153
/// 使用查找表加速。仅影响颜色通道,alpha 不变。 - 154
pub fn adjust_gamma( - 155
img : @types.Image, - 156
gamma : Float, - 157
) -> @types.Image raise @types.LoadError { - 158
// 输入验证 - 159●
if img.width <= 0 || img.height <= 0 { - 160●
raise @types.LoadError::DecodeFailed( - 161
"adjust_gamma: 图像尺寸必须大于0", - 162
) - 163
} - 164
if img.channels < 1 || img.channels > 4 { - 165●
raise @types.LoadError::DecodeFailed( - 166
"adjust_gamma: 通道数必须在1-4之间", - 167
) - 168
} - 169
let expected_size = img.width * img.height * img.channels - 170●
if img.data.length() != expected_size { - 171●
raise @types.LoadError::DecodeFailed( - 172
"adjust_gamma: 数据大小不匹配,期望 " + - 173●
expected_size.to_string() + - 174
",实际 " + - 175●
img.data.length().to_string(), - 176
) - 177
} - 178
if gamma <= 0.0 { - 179●
raise @types.LoadError::DecodeFailed("adjust_gamma: gamma必须大于0") - 180
} - 181
// 预计算 256 项查找表 - 182●
let lut : Array[Byte] = Array::make(256, b'\x00') - 183
for i in 0..<256 { - 184●
let normalized = Float::from_int(i) / 255.0F - 185●
let corrected = @math.powf(normalized, gamma) - 186●
let val = (corrected * 255.0F + 0.5F).to_int() - 187●
lut[i] = @types.clamp_byte(val) - 188
} - 189
let n = img.width * img.height - 190
let ch = img.channels - 191●
let color_ch = if ch >= 3 { 3 } else { ch } - 192●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 193
for i in 0..<n { - 194●
for c in 0..<color_ch { - 195●
out[i * ch + c] = lut[img.data[i * ch + c].to_int()] - 196
} - 197
if ch == 4 { - 198●
out[i * ch + 3] = img.data[i * ch + 3] - 199
} - 200
} - 201
@types.Image::{ - 202
width: img.width, - 203
height: img.height, - 204
channels: ch, - 205●
data: Bytes::from_array(out), - 206
} - 207
} - 208
- 209
///| - 210
/// 反色(负片)。new = 255 - old。仅影响颜色通道,alpha 不变。 - 211
pub fn invert(img : @types.Image) -> @types.Image raise @types.LoadError { - 212
// 输入验证 - 213●
if img.width <= 0 || img.height <= 0 { - 214●
raise @types.LoadError::DecodeFailed("invert: 图像尺寸必须大于0") - 215
} - 216
if img.channels < 1 || img.channels > 4 { - 217●
raise @types.LoadError::DecodeFailed("invert: 通道数必须在1-4之间") - 218
} - 219
let expected_size = img.width * img.height * img.channels - 220●
if img.data.length() != expected_size { - 221●
raise @types.LoadError::DecodeFailed( - 222
"invert: 数据大小不匹配,期望 " + - 223●
expected_size.to_string() + - 224
",实际 " + - 225●
img.data.length().to_string(), - 226
) - 227
} - 228
let n = img.width * img.height - 229
let ch = img.channels - 230●
let color_ch = if ch >= 3 { 3 } else { ch } - 231●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 232
for i in 0..<n { - 233●
for c in 0..<color_ch { - 234●
out[i * ch + c] = (255 - img.data[i * ch + c].to_int()).to_byte() - 235
} - 236
if ch == 4 { - 237●
out[i * ch + 3] = img.data[i * ch + 3] - 238
} - 239
} - 240
@types.Image::{ - 241
width: img.width, - 242
height: img.height, - 243
channels: ch, - 244●
data: Bytes::from_array(out), - 245
} - 246
} - 247
- 248
///| - 249
/// RGB → HSV 色彩空间转换。 - 250
/// 输入 r,g,b ∈ [0,255],输出 h ∈ [0,360), s ∈ [0,1], v ∈ [0,1] - 251
pub fn rgb_to_hsv( - 252
r : Int32, - 253
g : Int32, - 254
b : Int32, - 255
) -> (Float, Float, Float) raise @types.LoadError { - 256
// 输入验证 - 257●
if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 { - 258●
raise @types.LoadError::DecodeFailed( - 259
"rgb_to_hsv: r,g,b必须在[0,255]范围内", - 260
) - 261
} - 262●
let rf = Float::from_int(r) / 255.0F - 263●
let gf = Float::from_int(g) / 255.0F - 264●
let bf = Float::from_int(b) / 255.0F - 265●
let mx = rf.max(gf).max(bf) - 266●
let mn = rf.min(gf).min(bf) - 267
let delta = mx - mn - 268
let v = mx - 269●
let s = if mx == 0.0F { 0.0F } else { delta / mx } - 270
let h = if delta == 0.0F { - 271●
0.0F - 272●
} else if mx == rf { - 273●
60.0F * ((gf - bf) / delta) - 274●
} else if mx == gf { - 275●
60.0F * ((bf - rf) / delta + 2.0F) - 276
} else { - 277●
60.0F * ((rf - gf) / delta + 4.0F) - 278
} - 279●
let h_norm = if h < 0.0F { h + 360.0F } else { h } - 280
(h_norm, s, v) - 281
} - 282
- 283
///| - 284
/// HSV → RGB 色彩空间转换。 - 285
/// 输入 h ∈ [0,360), s ∈ [0,1], v ∈ [0,1],输出 r,g,b ∈ [0,255] - 286
pub fn hsv_to_rgb( - 287
h : Float, - 288
s : Float, - 289
v : Float, - 290
) -> (Int32, Int32, Int32) raise @types.LoadError { - 291
// 输入验证 - 292●
if s < 0.0F || s > 1.0F || v < 0.0F || v > 1.0F { - 293●
raise @types.LoadError::DecodeFailed( - 294
"hsv_to_rgb: s,v必须在[0,1]范围内", - 295
) - 296
} - 297
let c = v * s - 298
// 归一化 h 到 [0,6) - 299
let mut hp = h / 60.0F - 300
while hp >= 6.0F { - 301●
hp = hp - 6.0F - 302
} - 303
while hp < 0.0F { - 304●
hp = hp + 6.0F - 305
} - 306
// hp % 2.0 - 307
let hp_mod2 = if hp < 2.0F { - 308●
hp - 309●
} else if hp < 4.0F { - 310●
hp - 2.0F - 311
} else { - 312●
hp - 4.0F - 313
} - 314●
let x = c * (1.0F - (hp_mod2 - 1.0F).abs()) - 315
let m = v - c - 316
let (r1, g1, b1) = if hp < 1.0F { - 317●
(c, x, 0.0F) - 318●
} else if hp < 2.0F { - 319●
(x, c, 0.0F) - 320●
} else if hp < 3.0F { - 321●
(0.0F, c, x) - 322●
} else if hp < 4.0F { - 323●
(0.0F, x, c) - 324●
} else if hp < 5.0F { - 325●
(x, 0.0F, c) - 326
} else { - 327●
(c, 0.0F, x) - 328
} - 329
( - 330●
((r1 + m) * 255.0F + 0.5F).to_int(), - 331●
((g1 + m) * 255.0F + 0.5F).to_int(), - 332●
((b1 + m) * 255.0F + 0.5F).to_int(), - 333
) - 334
} - 335
- 336
///| - 337
/// RGB → HSL 色彩空间转换。 - 338
/// 输入 r,g,b ∈ [0,255],输出 h ∈ [0,360), s ∈ [0,1], l ∈ [0,1] - 339
pub fn rgb_to_hsl( - 340
r : Int32, - 341
g : Int32, - 342
b : Int32, - 343
) -> (Float, Float, Float) raise @types.LoadError { - 344
// 输入验证 - 345●
if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 { - 346●
raise @types.LoadError::DecodeFailed( - 347
"rgb_to_hsl: r,g,b必须在[0,255]范围内", - 348
) - 349
} - 350●
let rf = Float::from_int(r) / 255.0F - 351●
let gf = Float::from_int(g) / 255.0F - 352●
let bf = Float::from_int(b) / 255.0F - 353●
let mx = rf.max(gf).max(bf) - 354●
let mn = rf.min(gf).min(bf) - 355
let delta = mx - mn - 356
let l = (mx + mn) / 2.0F - 357
let s = if delta == 0.0F { - 358●
0.0F - 359
} else { - 360●
delta / (1.0F - (2.0F * l - 1.0F).abs()) - 361
} - 362
let h = if delta == 0.0F { - 363●
0.0F - 364●
} else if mx == rf { - 365●
60.0F * ((gf - bf) / delta) - 366●
} else if mx == gf { - 367●
60.0F * ((bf - rf) / delta + 2.0F) - 368
} else { - 369●
60.0F * ((rf - gf) / delta + 4.0F) - 370
} - 371●
let h_norm = if h < 0.0F { h + 360.0F } else { h } - 372
(h_norm, s, l) - 373
} - 374
- 375
///| - 376
/// HSL → RGB 色彩空间转换。 - 377
/// 输入 h ∈ [0,360), s ∈ [0,1], l ∈ [0,1],输出 r,g,b ∈ [0,255] - 378
pub fn hsl_to_rgb( - 379
h : Float, - 380
s : Float, - 381
l : Float, - 382
) -> (Int32, Int32, Int32) raise @types.LoadError { - 383
// 输入验证 - 384●
if s < 0.0F || s > 1.0F || l < 0.0F || l > 1.0F { - 385●
raise @types.LoadError::DecodeFailed( - 386
"hsl_to_rgb: s,l必须在[0,1]范围内", - 387
) - 388
} - 389●
let c = (1.0F - (2.0F * l - 1.0F).abs()) * s - 390
// 归一化 h 到 [0,6) - 391
let mut hp = h / 60.0F - 392
while hp >= 6.0F { - 393●
hp = hp - 6.0F - 394
} - 395
while hp < 0.0F { - 396●
hp = hp + 6.0F - 397
} - 398
let hp_mod2 = if hp < 2.0F { - 399●
hp - 400●
} else if hp < 4.0F { - 401●
hp - 2.0F - 402
} else { - 403●
hp - 4.0F - 404
} - 405●
let x = c * (1.0F - (hp_mod2 - 1.0F).abs()) - 406
let m = l - c / 2.0F - 407
let (r1, g1, b1) = if hp < 1.0F { - 408●
(c, x, 0.0F) - 409●
} else if hp < 2.0F { - 410●
(x, c, 0.0F) - 411●
} else if hp < 3.0F { - 412●
(0.0F, c, x) - 413●
} else if hp < 4.0F { - 414●
(0.0F, x, c) - 415●
} else if hp < 5.0F { - 416●
(x, 0.0F, c) - 417
} else { - 418●
(c, 0.0F, x) - 419
} - 420
( - 421●
((r1 + m) * 255.0F + 0.5F).to_int(), - 422●
((g1 + m) * 255.0F + 0.5F).to_int(), - 423●
((b1 + m) * 255.0F + 0.5F).to_int(), - 424
) - 425
} - 426
- 427
///| - 428
/// 饱和度调整。factor: 0.0 = 灰度, 1.0 = 原始, >1.0 = 增强饱和度 - 429
pub fn adjust_saturation( - 430
img : @types.Image, - 431
factor : Float, - 432
) -> @types.Image raise @types.LoadError { - 433
// 输入验证 - 434●
if img.width <= 0 || img.height <= 0 { - 435●
raise @types.LoadError::DecodeFailed( - 436
"adjust_saturation: 图像尺寸必须大于0", - 437
) - 438
} - 439
if img.channels < 1 || img.channels > 4 { - 440●
raise @types.LoadError::DecodeFailed( - 441
"adjust_saturation: 通道数必须在1-4之间", - 442
) - 443
} - 444
let expected_size = img.width * img.height * img.channels - 445●
if img.data.length() != expected_size { - 446●
raise @types.LoadError::DecodeFailed( - 447
"adjust_saturation: 数据大小不匹配,期望 " + - 448●
expected_size.to_string() + - 449
",实际 " + - 450●
img.data.length().to_string(), - 451
) - 452
} - 453
let n = img.width * img.height - 454
let ch = img.channels - 455
if ch < 3 { - 456●
return img - 457
} - 458●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 459●
let f = factor.clamp(min=0.0F, max=10.0F) - 460
for i in 0..<n { - 461●
let r = Float::from_int(img.data[i * ch].to_int()) / 255.0F - 462●
let g = Float::from_int(img.data[i * ch + 1].to_int()) / 255.0F - 463●
let b = Float::from_int(img.data[i * ch + 2].to_int()) / 255.0F - 464
let gray = 0.299F * r + 0.587F * g + 0.114F * b - 465●
let nr = (gray + (r - gray) * f).clamp(min=0.0F, max=1.0F) - 466●
let ng = (gray + (g - gray) * f).clamp(min=0.0F, max=1.0F) - 467●
let nb = (gray + (b - gray) * f).clamp(min=0.0F, max=1.0F) - 468●
out[i * ch] = (nr * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte() - 469
out[i * ch + 1] = (ng * 255.0F + 0.5F) - 470●
.to_int() - 471●
.clamp(min=0, max=255) - 472●
.to_byte() - 473
out[i * ch + 2] = (nb * 255.0F + 0.5F) - 474●
.to_int() - 475●
.clamp(min=0, max=255) - 476●
.to_byte() - 477
if ch == 4 { - 478●
out[i * ch + 3] = img.data[i * ch + 3] - 479
} - 480
} - 481
@types.Image::{ - 482
width: img.width, - 483
height: img.height, - 484
channels: ch, - 485●
data: Bytes::from_array(out), - 486
} - 487
} - 488
- 489
///| - 490
/// 色相调整。delta: 色相偏移量(度,0-360) - 491
pub fn adjust_hue( - 492
img : @types.Image, - 493
delta : Float, - 494
) -> @types.Image raise @types.LoadError { - 495
// 输入验证 - 496●
if img.width <= 0 || img.height <= 0 { - 497●
raise @types.LoadError::DecodeFailed( - 498
"adjust_hue: 图像尺寸必须大于0", - 499
) - 500
} - 501
if img.channels < 1 || img.channels > 4 { - 502●
raise @types.LoadError::DecodeFailed( - 503
"adjust_hue: 通道数必须在1-4之间", - 504
) - 505
} - 506
let expected_size = img.width * img.height * img.channels - 507●
if img.data.length() != expected_size { - 508●
raise @types.LoadError::DecodeFailed( - 509
"adjust_hue: 数据大小不匹配,期望 " + - 510●
expected_size.to_string() + - 511
",实际 " + - 512●
img.data.length().to_string(), - 513
) - 514
} - 515
let n = img.width * img.height - 516
let ch = img.channels - 517
if ch < 3 { - 518●
return img - 519
} - 520●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 521
for i in 0..<n { - 522●
let r = img.data[i * ch].to_int() - 523●
let g = img.data[i * ch + 1].to_int() - 524●
let b = img.data[i * ch + 2].to_int() - 525●
let (h, s, l) = rgb_to_hsl(r, g, b) - 526
let mut nh = h + delta - 527
while nh >= 360.0F { - 528●
nh = nh - 360.0F - 529
} - 530
while nh < 0.0F { - 531●
nh = nh + 360.0F - 532
} - 533●
let (nr, ng, nb) = hsl_to_rgb(nh, s, l) - 534●
out[i * ch] = nr.clamp(min=0, max=255).to_byte() - 535●
out[i * ch + 1] = ng.clamp(min=0, max=255).to_byte() - 536●
out[i * ch + 2] = nb.clamp(min=0, max=255).to_byte() - 537
if ch == 4 { - 538●
out[i * ch + 3] = img.data[i * ch + 3] - 539
} - 540
} - 541
@types.Image::{ - 542
width: img.width, - 543
height: img.height, - 544
channels: ch, - 545●
data: Bytes::from_array(out), - 546
} - 547
}