image.process\color\color_convert_16f.mbt
95.9% · 187 / 195 points · 639 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 色彩转换 16f color convert 16f - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 颜色空间转换 16-bit/float 变体 - 7
// 支持 Image16 / ImageF 两种类型 - 8
- 9
// 复用 color_16f.mbt 中的 read_u16_le_c / write_u16_le_c / write_f32_le - 10
// 复用 tonemap.mbt 中的 read_float_le - 11
- 12
///| - 13
/// 16-bit 去除 alpha 通道(RGBA → RGB,channels=3) - 14
/// @see to_rgb 8-bit 版本,本函数为 Image16 变体 - 15
pub fn to_rgb_16(img : @types.Image16) -> @types.Image16 raise @types.LoadError { - 16
// 输入验证 - 17●
if img.width < 0 || img.height < 0 { - 18●
raise @types.LoadError::DecodeFailed("to_rgb_16: 图像尺寸不能为负") - 19
} - 20
if img.channels < 1 || img.channels > 4 { - 21●
raise @types.LoadError::DecodeFailed( - 22
"to_rgb_16: 通道数必须在1-4之间", - 23
) - 24
} - 25
let expected_size = img.width * img.height * img.channels * 2 - 26●
if img.data.length() != expected_size { - 27●
raise @types.LoadError::DecodeFailed( - 28
"to_rgb_16: 数据大小不匹配,期望 " + - 29●
expected_size.to_string() + - 30
",实际 " + - 31●
img.data.length().to_string(), - 32
) - 33
} - 34
if img.channels == 3 { - 35●
return img - 36
} - 37
if img.channels < 3 { - 38●
return img - 39
} - 40
let n = img.width * img.height - 41
let src_ch = img.channels - 42
let src_bpp = src_ch * 2 - 43
let out_bpp = 6 - 44●
let out : Array[Byte] = Array::make(n * out_bpp, b'\x00') - 45
for i in 0..<n { - 46●
for c in 0..<3 { - 47●
let src_off = i * src_bpp + c * 2 - 48
let dst_off = i * out_bpp + c * 2 - 49
out[dst_off] = img.data[src_off] - 50
out[dst_off + 1] = img.data[src_off + 1] - 51
} - 52
} - 53
@types.Image16::{ - 54
width: img.width, - 55
height: img.height, - 56
channels: 3, - 57●
data: Bytes::from_array(out), - 58
} - 59
} - 60
- 61
///| - 62
/// float 去除 alpha 通道(RGBA → RGB,channels=3) - 63
/// @see to_rgb 8-bit 版本,本函数为 ImageF 变体 - 64
pub fn to_rgb_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError { - 65
// 输入验证 - 66●
if img.width <= 0 || img.height <= 0 { - 67●
raise @types.LoadError::DecodeFailed("to_rgb_f: 图像尺寸必须大于0") - 68
} - 69
if img.channels < 1 || img.channels > 4 { - 70●
raise @types.LoadError::DecodeFailed( - 71
"to_rgb_f: 通道数必须在1-4之间", - 72
) - 73
} - 74
let expected_size = img.width * img.height * img.channels * 4 - 75●
if img.data.length() != expected_size { - 76●
raise @types.LoadError::DecodeFailed( - 77
"to_rgb_f: 数据大小不匹配,期望 " + - 78●
expected_size.to_string() + - 79
",实际 " + - 80●
img.data.length().to_string(), - 81
) - 82
} - 83
if img.channels == 3 { - 84○
return img - 85
} - 86
if img.channels < 3 { - 87○
return img - 88
} - 89
let n = img.width * img.height - 90
let src_ch = img.channels - 91
let src_bpp = src_ch * 4 - 92
let out_bpp = 12 - 93●
let out : Array[Byte] = Array::make(n * out_bpp, b'\x00') - 94
for i in 0..<n { - 95●
for c in 0..<3 { - 96●
let src_off = i * src_bpp + c * 4 - 97
let dst_off = i * out_bpp + c * 4 - 98
for b in 0..<4 { - 99●
out[dst_off + b] = img.data[src_off + b] - 100
} - 101
} - 102
} - 103
@types.ImageF::{ - 104
width: img.width, - 105
height: img.height, - 106
channels: 3, - 107●
data: Bytes::from_array(out), - 108
} - 109
} - 110
- 111
///| - 112
/// 16-bit 添加 alpha 通道(RGB → RGBA,channels=4,alpha=65535) - 113
/// @see to_rgba 8-bit 版本,本函数为 Image16 变体 - 114
pub fn to_rgba_16( - 115
img : @types.Image16, - 116
) -> @types.Image16 raise @types.LoadError { - 117
// 输入验证 - 118●
if img.width < 0 || img.height < 0 { - 119●
raise @types.LoadError::DecodeFailed("to_rgba_16: 图像尺寸不能为负") - 120
} - 121
if img.channels < 1 || img.channels > 4 { - 122●
raise @types.LoadError::DecodeFailed( - 123
"to_rgba_16: 通道数必须在1-4之间", - 124
) - 125
} - 126
let expected_size = img.width * img.height * img.channels * 2 - 127●
if img.data.length() != expected_size { - 128●
raise @types.LoadError::DecodeFailed( - 129
"to_rgba_16: 数据大小不匹配,期望 " + - 130●
expected_size.to_string() + - 131
",实际 " + - 132●
img.data.length().to_string(), - 133
) - 134
} - 135
if img.channels == 4 { - 136●
return img - 137
} - 138
let n = img.width * img.height - 139
let src_ch = img.channels - 140
let src_bpp = src_ch * 2 - 141
let out_bpp = 8 - 142●
let out : Array[Byte] = Array::make(n * out_bpp, b'\x00') - 143
for i in 0..<n { - 144●
for c in 0..<3 { - 145◐
let src_off = if c < src_ch { i * src_bpp + c * 2 } else { -1 } - 146
let dst_off = i * out_bpp + c * 2 - 147
if src_off >= 0 { - 148●
out[dst_off] = img.data[src_off] - 149
out[dst_off + 1] = img.data[src_off + 1] - 150
} - 151
} - 152
// alpha = 65535 - 153
let alpha_off = i * out_bpp + 6 - 154
out[alpha_off] = b'\xFF' - 155
out[alpha_off + 1] = b'\xFF' - 156
} - 157
@types.Image16::{ - 158
width: img.width, - 159
height: img.height, - 160
channels: 4, - 161●
data: Bytes::from_array(out), - 162
} - 163
} - 164
- 165
///| - 166
/// float 添加 alpha 通道(RGB → RGBA,channels=4,alpha=1.0) - 167
/// @see to_rgba 8-bit 版本,本函数为 ImageF 变体 - 168
pub fn to_rgba_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError { - 169
// 输入验证 - 170●
if img.width <= 0 || img.height <= 0 { - 171●
raise @types.LoadError::DecodeFailed("to_rgba_f: 图像尺寸必须大于0") - 172
} - 173
if img.channels < 1 || img.channels > 4 { - 174●
raise @types.LoadError::DecodeFailed( - 175
"to_rgba_f: 通道数必须在1-4之间", - 176
) - 177
} - 178
let expected_size = img.width * img.height * img.channels * 4 - 179●
if img.data.length() != expected_size { - 180●
raise @types.LoadError::DecodeFailed( - 181
"to_rgba_f: 数据大小不匹配,期望 " + - 182●
expected_size.to_string() + - 183
",实际 " + - 184●
img.data.length().to_string(), - 185
) - 186
} - 187
if img.channels == 4 { - 188○
return img - 189
} - 190
let n = img.width * img.height - 191
let src_ch = img.channels - 192
let src_bpp = src_ch * 4 - 193
let out_bpp = 16 - 194●
let out : Array[Byte] = Array::make(n * out_bpp, b'\x00') - 195
for i in 0..<n { - 196●
for c in 0..<3 { - 197●
if c < src_ch { - 198●
let src_off = i * src_bpp + c * 4 - 199
let dst_off = i * out_bpp + c * 4 - 200
for b in 0..<4 { - 201●
out[dst_off + b] = img.data[src_off + b] - 202
} - 203
} - 204
} - 205
// alpha = 1.0 - 206●
write_f32_le(out, i * out_bpp + 12, 1.0F) - 207
} - 208
@types.ImageF::{ - 209
width: img.width, - 210
height: img.height, - 211
channels: 4, - 212●
data: Bytes::from_array(out), - 213
} - 214
} - 215
- 216
///| - 217
/// 16-bit 预乘 alpha(RGBA → RGBA premultiplied)。要求 channels=4。 - 218
/// @see premultiply_alpha 8-bit 版本,本函数为 Image16 变体 - 219
pub fn premultiply_alpha_16( - 220
img : @types.Image16, - 221
) -> @types.Image16 raise @types.LoadError { - 222
// 输入验证 - 223●
if img.width <= 0 || img.height <= 0 { - 224●
raise @types.LoadError::DecodeFailed( - 225
"premultiply_alpha_16: 图像尺寸必须大于0", - 226
) - 227
} - 228
if img.channels != 4 { - 229●
raise @types.LoadError::DecodeFailed( - 230
"premultiply_alpha_16 需要 channels=4 (RGBA)", - 231
) - 232
} - 233
let expected_size = img.width * img.height * img.channels * 2 - 234●
if img.data.length() != expected_size { - 235●
raise @types.LoadError::DecodeFailed( - 236
"premultiply_alpha_16: 数据大小不匹配,期望 " + - 237●
expected_size.to_string() + - 238
",实际 " + - 239●
img.data.length().to_string(), - 240
) - 241
} - 242
let n = img.width * img.height - 243
let bpp = 8 - 244●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 245
for i in 0..<n { - 246●
let r = read_u16_le_c(img.data, i * bpp) - 247●
let g = read_u16_le_c(img.data, i * bpp + 2) - 248●
let b = read_u16_le_c(img.data, i * bpp + 4) - 249●
let a = read_u16_le_c(img.data, i * bpp + 6) - 250●
write_u16_le_c(out, i * bpp, r * a / 65535) - 251●
write_u16_le_c(out, i * bpp + 2, g * a / 65535) - 252●
write_u16_le_c(out, i * bpp + 4, b * a / 65535) - 253●
write_u16_le_c(out, i * bpp + 6, a) - 254
} - 255
@types.Image16::{ - 256
width: img.width, - 257
height: img.height, - 258
channels: 4, - 259●
data: Bytes::from_array(out), - 260
} - 261
} - 262
- 263
///| - 264
/// float 预乘 alpha(RGBA → RGBA premultiplied)。要求 channels=4。 - 265
/// @see premultiply_alpha 8-bit 版本,本函数为 ImageF 变体 - 266
pub fn premultiply_alpha_f( - 267
img : @types.ImageF, - 268
) -> @types.ImageF raise @types.LoadError { - 269
// 输入验证 - 270●
if img.width <= 0 || img.height <= 0 { - 271●
raise @types.LoadError::DecodeFailed( - 272
"premultiply_alpha_f: 图像尺寸必须大于0", - 273
) - 274
} - 275
if img.channels != 4 { - 276●
raise @types.LoadError::DecodeFailed( - 277
"premultiply_alpha_f 需要 channels=4 (RGBA)", - 278
) - 279
} - 280
let expected_size = img.width * img.height * img.channels * 4 - 281●
if img.data.length() != expected_size { - 282●
raise @types.LoadError::DecodeFailed( - 283
"premultiply_alpha_f: 数据大小不匹配,期望 " + - 284●
expected_size.to_string() + - 285
",实际 " + - 286●
img.data.length().to_string(), - 287
) - 288
} - 289
let n = img.width * img.height - 290
let bpp = 16 - 291●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 292
for i in 0..<n { - 293●
let r = read_float_le(img.data, i * bpp) - 294●
let g = read_float_le(img.data, i * bpp + 4) - 295●
let b = read_float_le(img.data, i * bpp + 8) - 296●
let a = read_float_le(img.data, i * bpp + 12) - 297●
write_f32_le(out, i * bpp, r * a) - 298●
write_f32_le(out, i * bpp + 4, g * a) - 299●
write_f32_le(out, i * bpp + 8, b * a) - 300●
write_f32_le(out, i * bpp + 12, a) - 301
} - 302
@types.ImageF::{ - 303
width: img.width, - 304
height: img.height, - 305
channels: 4, - 306●
data: Bytes::from_array(out), - 307
} - 308
} - 309
- 310
///| - 311
/// 16-bit 反预乘 alpha(RGBA premA → RGBA unpremultiplied)。要求 channels=4。 - 312
/// @see unpremultiply_alpha 8-bit 版本,本函数为 Image16 变体 - 313
pub fn unpremultiply_alpha_16( - 314
img : @types.Image16, - 315
) -> @types.Image16 raise @types.LoadError { - 316
// 输入验证 - 317●
if img.width <= 0 || img.height <= 0 { - 318●
raise @types.LoadError::DecodeFailed( - 319
"unpremultiply_alpha_16: 图像尺寸必须大于0", - 320
) - 321
} - 322
if img.channels != 4 { - 323●
raise @types.LoadError::DecodeFailed( - 324
"unpremultiply_alpha_16 需要 channels=4 (RGBA)", - 325
) - 326
} - 327
let expected_size = img.width * img.height * img.channels * 2 - 328●
if img.data.length() != expected_size { - 329●
raise @types.LoadError::DecodeFailed( - 330
"unpremultiply_alpha_16: 数据大小不匹配,期望 " + - 331●
expected_size.to_string() + - 332
",实际 " + - 333●
img.data.length().to_string(), - 334
) - 335
} - 336
let n = img.width * img.height - 337
let bpp = 8 - 338●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 339
for i in 0..<n { - 340●
let r = read_u16_le_c(img.data, i * bpp) - 341●
let g = read_u16_le_c(img.data, i * bpp + 2) - 342●
let b = read_u16_le_c(img.data, i * bpp + 4) - 343●
let a = read_u16_le_c(img.data, i * bpp + 6) - 344
if a == 0 { - 345●
write_u16_le_c(out, i * bpp, 0) - 346●
write_u16_le_c(out, i * bpp + 2, 0) - 347●
write_u16_le_c(out, i * bpp + 4, 0) - 348●
write_u16_le_c(out, i * bpp + 6, 0) - 349
} else { - 350●
write_u16_le_c(out, i * bpp, r * 65535 / a) - 351●
write_u16_le_c(out, i * bpp + 2, g * 65535 / a) - 352●
write_u16_le_c(out, i * bpp + 4, b * 65535 / a) - 353●
write_u16_le_c(out, i * bpp + 6, a) - 354
} - 355
} - 356
@types.Image16::{ - 357
width: img.width, - 358
height: img.height, - 359
channels: 4, - 360●
data: Bytes::from_array(out), - 361
} - 362
} - 363
- 364
///| - 365
/// float 反预乘 alpha(RGBA premA → RGBA unpremultiplied)。要求 channels=4。 - 366
/// @see unpremultiply_alpha 8-bit 版本,本函数为 ImageF 变体 - 367
pub fn unpremultiply_alpha_f( - 368
img : @types.ImageF, - 369
) -> @types.ImageF raise @types.LoadError { - 370
// 输入验证 - 371●
if img.width <= 0 || img.height <= 0 { - 372●
raise @types.LoadError::DecodeFailed( - 373
"unpremultiply_alpha_f: 图像尺寸必须大于0", - 374
) - 375
} - 376
if img.channels != 4 { - 377●
raise @types.LoadError::DecodeFailed( - 378
"unpremultiply_alpha_f 需要 channels=4 (RGBA)", - 379
) - 380
} - 381
let expected_size = img.width * img.height * img.channels * 4 - 382●
if img.data.length() != expected_size { - 383●
raise @types.LoadError::DecodeFailed( - 384
"unpremultiply_alpha_f: 数据大小不匹配,期望 " + - 385●
expected_size.to_string() + - 386
",实际 " + - 387●
img.data.length().to_string(), - 388
) - 389
} - 390
let n = img.width * img.height - 391
let bpp = 16 - 392●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 393
for i in 0..<n { - 394●
let r = read_float_le(img.data, i * bpp) - 395●
let g = read_float_le(img.data, i * bpp + 4) - 396●
let b = read_float_le(img.data, i * bpp + 8) - 397●
let a = read_float_le(img.data, i * bpp + 12) - 398
if a < 0.000001F { - 399●
write_f32_le(out, i * bpp, 0.0F) - 400●
write_f32_le(out, i * bpp + 4, 0.0F) - 401●
write_f32_le(out, i * bpp + 8, 0.0F) - 402●
write_f32_le(out, i * bpp + 12, 0.0F) - 403
} else { - 404●
write_f32_le(out, i * bpp, r / a) - 405●
write_f32_le(out, i * bpp + 4, g / a) - 406●
write_f32_le(out, i * bpp + 8, b / a) - 407●
write_f32_le(out, i * bpp + 12, a) - 408
} - 409
} - 410
@types.ImageF::{ - 411
width: img.width, - 412
height: img.height, - 413
channels: 4, - 414●
data: Bytes::from_array(out), - 415
} - 416
} - 417
- 418
// ===== 像素级颜色空间转换函数 ===== - 419
- 420
///| - 421
/// 16-bit RGB → YCbCr(ITU-R BT.601)。输入范围 0-65535,输出范围 0-65535。 - 422
/// @see rgb_to_ycbcr 8-bit 版本,本函数为 Image16 变体 - 423
pub fn rgb_to_ycbcr_16( - 424
r : Int32, - 425
g : Int32, - 426
b : Int32, - 427
) -> (Int32, Int32, Int32) raise @types.LoadError { - 428
// 输入验证 - 429●
if r < 0 || r > 65535 || g < 0 || g > 65535 || b < 0 || b > 65535 { - 430●
raise @types.LoadError::DecodeFailed( - 431
"rgb_to_ycbcr_16: r,g,b必须在[0,65535]范围内", - 432
) - 433
} - 434
let y = (299 * r + 587 * g + 114 * b) / 1000 - 435
let cb = (-169 * r - 331 * g + 500 * b) / 1000 + 32768 - 436
let cr = (500 * r - 419 * g - 81 * b) / 1000 + 32768 - 437
( - 438●
y.clamp(min=0, max=65535), - 439●
cb.clamp(min=0, max=65535), - 440●
cr.clamp(min=0, max=65535), - 441
) - 442
} - 443
- 444
///| - 445
/// float RGB → YCbCr(ITU-R BT.601)。输入范围 0.0-1.0,输出范围 0.0-1.0。 - 446
/// @see rgb_to_ycbcr 8-bit 版本,本函数为 ImageF 变体 - 447
pub fn rgb_to_ycbcr_f( - 448
r : Float, - 449
g : Float, - 450
b : Float, - 451
) -> (Float, Float, Float) raise @types.LoadError { - 452
// 输入验证 - 453●
if r < 0.0F || r > 1.0F || g < 0.0F || g > 1.0F || b < 0.0F || b > 1.0F { - 454●
raise @types.LoadError::DecodeFailed( - 455
"rgb_to_ycbcr_f: r,g,b必须在[0,1]范围内", - 456
) - 457
} - 458
let y = 0.299F * r + 0.587F * g + 0.114F * b - 459
let cb = -0.169F * r - 0.331F * g + 0.5F * b + 0.5F - 460
let cr = 0.5F * r - 0.419F * g - 0.081F * b + 0.5F - 461
( - 462●
@types.clamp_float(y, 0.0F, 1.0F), - 463●
@types.clamp_float(cb, 0.0F, 1.0F), - 464●
@types.clamp_float(cr, 0.0F, 1.0F), - 465
) - 466
} - 467
- 468
///| - 469
/// 16-bit YCbCr → RGB(ITU-R BT.601)。输入范围 0-65535。 - 470
/// @see ycbcr_to_rgb 8-bit 版本,本函数为 Image16 变体 - 471
pub fn ycbcr_to_rgb_16( - 472
y : Int32, - 473
cb : Int32, - 474
cr : Int32, - 475
) -> (Int32, Int32, Int32) raise @types.LoadError { - 476
// 输入验证 - 477●
if y < 0 || y > 65535 || cb < 0 || cb > 65535 || cr < 0 || cr > 65535 { - 478●
raise @types.LoadError::DecodeFailed( - 479
"ycbcr_to_rgb_16: y,cb,cr必须在[0,65535]范围内", - 480
) - 481
} - 482
let cb0 = cb - 32768 - 483
let cr0 = cr - 32768 - 484
let r = y + 1402 * cr0 / 1000 - 485
let g = y - 344 * cb0 / 1000 - 714 * cr0 / 1000 - 486
let b = y + 1772 * cb0 / 1000 - 487
( - 488●
r.clamp(min=0, max=65535), - 489●
g.clamp(min=0, max=65535), - 490●
b.clamp(min=0, max=65535), - 491
) - 492
} - 493
- 494
///| - 495
/// float YCbCr → RGB(ITU-R BT.601)。输入范围 0.0-1.0。 - 496
/// @see ycbcr_to_rgb 8-bit 版本,本函数为 ImageF 变体 - 497
pub fn ycbcr_to_rgb_f( - 498
y : Float, - 499
cb : Float, - 500
cr : Float, - 501
) -> (Float, Float, Float) raise @types.LoadError { - 502
// 输入验证 - 503●
if y < 0.0F || y > 1.0F || cb < 0.0F || cb > 1.0F || cr < 0.0F || cr > 1.0F { - 504●
raise @types.LoadError::DecodeFailed( - 505
"ycbcr_to_rgb_f: y,cb,cr必须在[0,1]范围内", - 506
) - 507
} - 508
let cb0 = cb - 0.5F - 509
let cr0 = cr - 0.5F - 510
let r = y + 1.402F * cr0 - 511
let g = y - 0.344F * cb0 - 0.714F * cr0 - 512
let b = y + 1.772F * cb0 - 513
( - 514●
@types.clamp_float(r, 0.0F, 1.0F), - 515●
@types.clamp_float(g, 0.0F, 1.0F), - 516●
@types.clamp_float(b, 0.0F, 1.0F), - 517
) - 518
} - 519
- 520
///| - 521
/// 16-bit RGB → CMYK。输入范围 0-65535,输出范围 0-65535。 - 522
/// @see rgb_to_cmyk 8-bit 版本,本函数为 Image16 变体 - 523
pub fn rgb_to_cmyk_16( - 524
r : Int32, - 525
g : Int32, - 526
b : Int32, - 527
) -> (Int32, Int32, Int32, Int32) raise @types.LoadError { - 528
// 输入验证 - 529●
if r < 0 || r > 65535 || g < 0 || g > 65535 || b < 0 || b > 65535 { - 530●
raise @types.LoadError::DecodeFailed( - 531
"rgb_to_cmyk_16: r,g,b必须在[0,65535]范围内", - 532
) - 533
} - 534
let c = 65535 - r - 535
let m = 65535 - g - 536
let y = 65535 - b - 537◐
let k = if c < m { if c < y { c } else { y } } else if m < y { m } else { y } - 538
if k == 65535 { - 539●
(0, 0, 0, 65535) - 540
} else { - 541●
let denom = 65535 - k - 542
( - 543
(c - k) * 65535 / denom, - 544
(m - k) * 65535 / denom, - 545
(y - k) * 65535 / denom, - 546
k, - 547
) - 548
} - 549
} - 550
- 551
///| - 552
/// float RGB → CMYK。输入范围 0.0-1.0,输出范围 0.0-1.0。 - 553
/// @see rgb_to_cmyk 8-bit 版本,本函数为 ImageF 变体 - 554
pub fn rgb_to_cmyk_f( - 555
r : Float, - 556
g : Float, - 557
b : Float, - 558
) -> (Float, Float, Float, Float) raise @types.LoadError { - 559
// 输入验证 - 560●
if r < 0.0F || r > 1.0F || g < 0.0F || g > 1.0F || b < 0.0F || b > 1.0F { - 561●
raise @types.LoadError::DecodeFailed( - 562
"rgb_to_cmyk_f: r,g,b必须在[0,1]范围内", - 563
) - 564
} - 565
let c = 1.0F - r - 566
let m = 1.0F - g - 567
let y = 1.0F - b - 568◐
let k = if c < m { if c < y { c } else { y } } else if m < y { m } else { y } - 569
if k > 0.999999F { - 570●
(0.0F, 0.0F, 0.0F, 1.0F) - 571
} else { - 572●
let denom = 1.0F - k - 573
((c - k) / denom, (m - k) / denom, (y - k) / denom, k) - 574
} - 575
} - 576
- 577
///| - 578
/// 16-bit CMYK → RGB。输入范围 0-65535。 - 579
/// @see cmyk_to_rgb 8-bit 版本,本函数为 Image16 变体 - 580
pub fn cmyk_to_rgb_16( - 581
c : Int32, - 582
m : Int32, - 583
y : Int32, - 584
k : Int32, - 585
) -> (Int32, Int32, Int32) raise @types.LoadError { - 586
// 输入验证 - 587●
if c < 0 || - 588
c > 65535 || - 589
m < 0 || - 590
m > 65535 || - 591
y < 0 || - 592
y > 65535 || - 593
k < 0 || - 594
k > 65535 { - 595●
raise @types.LoadError::DecodeFailed( - 596
"cmyk_to_rgb_16: c,m,y,k必须在[0,65535]范围内", - 597
) - 598
} - 599
let r = 65535 - (c * (65535 - k) / 65535 + k) - 600
let g = 65535 - (m * (65535 - k) / 65535 + k) - 601
let b = 65535 - (y * (65535 - k) / 65535 + k) - 602
( - 603●
r.clamp(min=0, max=65535), - 604●
g.clamp(min=0, max=65535), - 605●
b.clamp(min=0, max=65535), - 606
) - 607
} - 608
- 609
///| - 610
/// float CMYK → RGB。输入范围 0.0-1.0。 - 611
/// @see cmyk_to_rgb 8-bit 版本,本函数为 ImageF 变体 - 612
pub fn cmyk_to_rgb_f( - 613
c : Float, - 614
m : Float, - 615
y : Float, - 616
k : Float, - 617
) -> (Float, Float, Float) raise @types.LoadError { - 618
// 输入验证 - 619●
if c < 0.0F || - 620
c > 1.0F || - 621
m < 0.0F || - 622
m > 1.0F || - 623
y < 0.0F || - 624
y > 1.0F || - 625
k < 0.0F || - 626
k > 1.0F { - 627●
raise @types.LoadError::DecodeFailed( - 628
"cmyk_to_rgb_f: c,m,y,k必须在[0,1]范围内", - 629
) - 630
} - 631
let r = 1.0F - (c * (1.0F - k) + k) - 632
let g = 1.0F - (m * (1.0F - k) + k) - 633
let b = 1.0F - (y * (1.0F - k) + k) - 634
( - 635●
@types.clamp_float(r, 0.0F, 1.0F), - 636●
@types.clamp_float(g, 0.0F, 1.0F), - 637●
@types.clamp_float(b, 0.0F, 1.0F), - 638
) - 639
}