image.process\color\color_16f.mbt
90.1% · 201 / 223 points · 593 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 色彩调整 16-bit/float 变体 Color adjustment 16-bit/float variants - 3
/// - 4
/// 16位亮度/对比度/伽马调整 adjust_brightness_16 / adjust_contrast_16 / adjust_gamma_16 - 5
/// 16位/浮点亮度/对比度/伽马调整变体 16-bit/float variants of brightness/contrast/gamma adjustment - 6
// 色彩调整 16-bit/float 变体 - 7
// 支持 Image16 / ImageF 两种类型 - 8
- 9
///| - 10
fn read_u16_le_c(data : Bytes, offset : Int32) -> Int32 { - 11●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 12
} - 13
- 14
///| - 15
fn write_u16_le_c(buf : Array[Byte], offset : Int32, val : Int32) -> Unit { - 16●
buf[offset] = (val & 0xFF).to_byte() - 17●
buf[offset + 1] = ((val >> 8) & 0xFF).to_byte() - 18
} - 19
- 20
///| - 21
fn write_f32_le(buf : Array[Byte], offset : Int32, val : Float) -> Unit { - 22●
let bits = val.reinterpret_as_int() - 23●
buf[offset] = (bits & 0xFF).to_byte() - 24●
buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 25●
buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 26●
buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 27
} - 28
- 29
///| - 30
/// 16-bit 亮度调整。delta ∈ [-65535, 65535] - 31
/// @see adjust_brightness 8-bit 版本,本函数为 Image16 变体 - 32
pub fn adjust_brightness_16( - 33
img : @types.Image16, - 34
delta : Int32, - 35
) -> @types.Image16 raise @types.LoadError { - 36
// 输入验证 - 37●
if img.width <= 0 || img.height <= 0 { - 38●
raise @types.LoadError::DecodeFailed( - 39
"adjust_brightness_16: 图像尺寸必须大于0", - 40
) - 41
} - 42
if img.channels < 1 || img.channels > 4 { - 43●
raise @types.LoadError::DecodeFailed( - 44
"adjust_brightness_16: 通道数必须在1-4之间", - 45
) - 46
} - 47
let expected_size = img.width * img.height * img.channels * 2 - 48●
if img.data.length() != expected_size { - 49●
raise @types.LoadError::DecodeFailed( - 50
"adjust_brightness_16: 数据大小不匹配,期望 " + - 51●
expected_size.to_string() + - 52
",实际 " + - 53●
img.data.length().to_string(), - 54
) - 55
} - 56
let n = img.width * img.height - 57
let ch = img.channels - 58●
let color_ch = if ch >= 3 { 3 } else { ch } - 59
let bpp = ch * 2 - 60●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 61
for i in 0..<n { - 62●
for c in 0..<color_ch { - 63●
let offset = i * bpp + c * 2 - 64●
let val = read_u16_le_c(img.data, offset) - 65
let adjusted = if val + delta < 0 { - 66●
0 - 67●
} else if val + delta > 65535 { - 68●
65535 - 69
} else { - 70●
val + delta - 71
} - 72●
write_u16_le_c(out, offset, adjusted) - 73
} - 74
if ch == 4 { - 75●
let alpha_off = i * bpp + 6 - 76
out[alpha_off] = img.data[alpha_off] - 77
out[alpha_off + 1] = img.data[alpha_off + 1] - 78
} - 79
} - 80
@types.Image16::{ - 81
width: img.width, - 82
height: img.height, - 83
channels: ch, - 84●
data: Bytes::from_array(out), - 85
} - 86
} - 87
- 88
///| - 89
/// float 亮度调整。delta 为浮点偏移量 - 90
/// @see adjust_brightness 8-bit 版本,本函数为 ImageF 变体 - 91
pub fn adjust_brightness_f( - 92
img : @types.ImageF, - 93
delta : Float, - 94
) -> @types.ImageF raise @types.LoadError { - 95
// 输入验证 - 96●
if img.width <= 0 || img.height <= 0 { - 97●
raise @types.LoadError::DecodeFailed( - 98
"adjust_brightness_f: 图像尺寸必须大于0", - 99
) - 100
} - 101
if img.channels < 1 || img.channels > 4 { - 102●
raise @types.LoadError::DecodeFailed( - 103
"adjust_brightness_f: 通道数必须在1-4之间", - 104
) - 105
} - 106
let expected_size = img.width * img.height * img.channels * 4 - 107●
if img.data.length() != expected_size { - 108●
raise @types.LoadError::DecodeFailed( - 109
"adjust_brightness_f: 数据大小不匹配,期望 " + - 110●
expected_size.to_string() + - 111
",实际 " + - 112●
img.data.length().to_string(), - 113
) - 114
} - 115
let n = img.width * img.height - 116
let ch = img.channels - 117◐
let color_ch = if ch >= 3 { 3 } else { ch } - 118
let bpp = ch * 4 - 119●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 120
for i in 0..<n { - 121●
for c in 0..<color_ch { - 122●
let offset = i * bpp + c * 4 - 123●
let val = read_float_le(img.data, offset) - 124●
write_f32_le(out, offset, val + delta) - 125
} - 126
if ch == 4 { - 127●
let alpha_off = i * bpp + 12 - 128
for b in 0..<4 { - 129●
out[alpha_off + b] = img.data[alpha_off + b] - 130
} - 131
} - 132
} - 133
@types.ImageF::{ - 134
width: img.width, - 135
height: img.height, - 136
channels: ch, - 137●
data: Bytes::from_array(out), - 138
} - 139
} - 140
- 141
///| - 142
/// 16-bit 对比度调整。factor=1.0 为原图,以 32768 为中心 - 143
/// @see adjust_contrast 8-bit 版本,本函数为 Image16 变体 - 144
pub fn adjust_contrast_16( - 145
img : @types.Image16, - 146
factor : Float, - 147
) -> @types.Image16 raise @types.LoadError { - 148
// 输入验证 - 149●
if img.width <= 0 || img.height <= 0 { - 150●
raise @types.LoadError::DecodeFailed( - 151
"adjust_contrast_16: 图像尺寸必须大于0", - 152
) - 153
} - 154
if img.channels < 1 || img.channels > 4 { - 155●
raise @types.LoadError::DecodeFailed( - 156
"adjust_contrast_16: 通道数必须在1-4之间", - 157
) - 158
} - 159
let expected_size = img.width * img.height * img.channels * 2 - 160●
if img.data.length() != expected_size { - 161●
raise @types.LoadError::DecodeFailed( - 162
"adjust_contrast_16: 数据大小不匹配,期望 " + - 163●
expected_size.to_string() + - 164
",实际 " + - 165●
img.data.length().to_string(), - 166
) - 167
} - 168
if factor < 0.0F { - 169○
raise @types.LoadError::DecodeFailed( - 170
"adjust_contrast_16: factor必须大于等于0", - 171
) - 172
} - 173
let n = img.width * img.height - 174
let ch = img.channels - 175●
let color_ch = if ch >= 3 { 3 } else { ch } - 176
let bpp = ch * 2 - 177●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 178
for i in 0..<n { - 179●
for c in 0..<color_ch { - 180●
let offset = i * bpp + c * 2 - 181●
let val = read_u16_le_c(img.data, offset) - 182●
let adjusted = (Float::from_int(val - 32768) * factor + 32768.0F).to_int() - 183
let clamped = if adjusted < 0 { - 184●
0 - 185●
} else if adjusted > 65535 { - 186●
65535 - 187
} else { - 188●
adjusted - 189
} - 190●
write_u16_le_c(out, offset, clamped) - 191
} - 192
if ch == 4 { - 193●
let alpha_off = i * bpp + 6 - 194
out[alpha_off] = img.data[alpha_off] - 195
out[alpha_off + 1] = img.data[alpha_off + 1] - 196
} - 197
} - 198
@types.Image16::{ - 199
width: img.width, - 200
height: img.height, - 201
channels: ch, - 202●
data: Bytes::from_array(out), - 203
} - 204
} - 205
- 206
///| - 207
/// float 对比度调整。factor=1.0 为原图,以 0.5 为中心 - 208
/// @see adjust_contrast 8-bit 版本,本函数为 ImageF 变体 - 209
pub fn adjust_contrast_f( - 210
img : @types.ImageF, - 211
factor : Float, - 212
) -> @types.ImageF raise @types.LoadError { - 213
// 输入验证 - 214●
if img.width <= 0 || img.height <= 0 { - 215●
raise @types.LoadError::DecodeFailed( - 216
"adjust_contrast_f: 图像尺寸必须大于0", - 217
) - 218
} - 219
if img.channels < 1 || img.channels > 4 { - 220●
raise @types.LoadError::DecodeFailed( - 221
"adjust_contrast_f: 通道数必须在1-4之间", - 222
) - 223
} - 224
let expected_size = img.width * img.height * img.channels * 4 - 225●
if img.data.length() != expected_size { - 226●
raise @types.LoadError::DecodeFailed( - 227
"adjust_contrast_f: 数据大小不匹配,期望 " + - 228●
expected_size.to_string() + - 229
",实际 " + - 230●
img.data.length().to_string(), - 231
) - 232
} - 233
if factor < 0.0F { - 234○
raise @types.LoadError::DecodeFailed( - 235
"adjust_contrast_f: factor必须大于等于0", - 236
) - 237
} - 238
let n = img.width * img.height - 239
let ch = img.channels - 240●
let color_ch = if ch >= 3 { 3 } else { ch } - 241
let bpp = ch * 4 - 242●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 243
for i in 0..<n { - 244●
for c in 0..<color_ch { - 245●
let offset = i * bpp + c * 4 - 246●
let val = read_float_le(img.data, offset) - 247●
write_f32_le(out, offset, (val - 0.5F) * factor + 0.5F) - 248
} - 249
if ch == 4 { - 250●
let alpha_off = i * bpp + 12 - 251
for b in 0..<4 { - 252●
out[alpha_off + b] = img.data[alpha_off + b] - 253
} - 254
} - 255
} - 256
@types.ImageF::{ - 257
width: img.width, - 258
height: img.height, - 259
channels: ch, - 260●
data: Bytes::from_array(out), - 261
} - 262
} - 263
- 264
///| - 265
/// 16-bit Gamma 校正。gamma=1.0 为原图,>1 变暗,<1 变亮。 - 266
/// 以 65535 为满量程。仅影响颜色通道,alpha 不变。 - 267
/// @see adjust_gamma 8-bit 版本,本函数为 Image16 变体 - 268
pub fn adjust_gamma_16( - 269
img : @types.Image16, - 270
gamma : Float, - 271
) -> @types.Image16 raise @types.LoadError { - 272
// 输入验证 - 273●
if img.width <= 0 || img.height <= 0 { - 274●
raise @types.LoadError::DecodeFailed( - 275
"adjust_gamma_16: 图像尺寸必须大于0", - 276
) - 277
} - 278
if img.channels < 1 || img.channels > 4 { - 279●
raise @types.LoadError::DecodeFailed( - 280
"adjust_gamma_16: 通道数必须在1-4之间", - 281
) - 282
} - 283
let expected_size = img.width * img.height * img.channels * 2 - 284●
if img.data.length() != expected_size { - 285●
raise @types.LoadError::DecodeFailed( - 286
"adjust_gamma_16: 数据大小不匹配,期望 " + - 287●
expected_size.to_string() + - 288
",实际 " + - 289●
img.data.length().to_string(), - 290
) - 291
} - 292
if gamma <= 0.0F { - 293●
raise @types.LoadError::DecodeFailed("adjust_gamma_16: gamma必须>0") - 294
} - 295
let n = img.width * img.height - 296
let ch = img.channels - 297●
let color_ch = if ch >= 3 { 3 } else { ch } - 298
let bpp = ch * 2 - 299●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 300
for i in 0..<n { - 301●
for c in 0..<color_ch { - 302●
let offset = i * bpp + c * 2 - 303●
let val = read_u16_le_c(img.data, offset) - 304●
let normalized = Float::from_int(val) / 65535.0F - 305●
let corrected = @math.powf(normalized, gamma) - 306●
let new_val = (corrected * 65535.0F + 0.5F).to_int() - 307
let clamped = if new_val < 0 { - 308○
0 - 309●
} else if new_val > 65535 { - 310○
65535 - 311
} else { - 312●
new_val - 313
} - 314●
write_u16_le_c(out, offset, clamped) - 315
} - 316
if ch == 4 { - 317○
let alpha_off = i * bpp + 6 - 318
out[alpha_off] = img.data[alpha_off] - 319
out[alpha_off + 1] = img.data[alpha_off + 1] - 320
} - 321
} - 322
@types.Image16::{ - 323
width: img.width, - 324
height: img.height, - 325
channels: ch, - 326●
data: Bytes::from_array(out), - 327
} - 328
} - 329
- 330
///| - 331
/// float Gamma 校正。gamma=1.0 为原图,>1 变暗,<1 变亮。 - 332
/// 以 1.0 为满量程。仅影响颜色通道,alpha 不变。 - 333
/// @see adjust_gamma 8-bit 版本,本函数为 ImageF 变体 - 334
pub fn adjust_gamma_f( - 335
img : @types.ImageF, - 336
gamma : Float, - 337
) -> @types.ImageF raise @types.LoadError { - 338
// 输入验证 - 339●
if img.width <= 0 || img.height <= 0 { - 340●
raise @types.LoadError::DecodeFailed( - 341
"adjust_gamma_f: 图像尺寸必须大于0", - 342
) - 343
} - 344
if img.channels < 1 || img.channels > 4 { - 345●
raise @types.LoadError::DecodeFailed( - 346
"adjust_gamma_f: 通道数必须在1-4之间", - 347
) - 348
} - 349
let expected_size = img.width * img.height * img.channels * 4 - 350●
if img.data.length() != expected_size { - 351●
raise @types.LoadError::DecodeFailed( - 352
"adjust_gamma_f: 数据大小不匹配,期望 " + - 353●
expected_size.to_string() + - 354
",实际 " + - 355●
img.data.length().to_string(), - 356
) - 357
} - 358
if gamma <= 0.0F { - 359●
raise @types.LoadError::DecodeFailed("adjust_gamma_f: gamma必须>0") - 360
} - 361
let n = img.width * img.height - 362
let ch = img.channels - 363◐
let color_ch = if ch >= 3 { 3 } else { ch } - 364
let bpp = ch * 4 - 365●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 366
for i in 0..<n { - 367●
for c in 0..<color_ch { - 368●
let offset = i * bpp + c * 4 - 369●
let val = read_float_le(img.data, offset) - 370
let corrected = if val < 0.0F { - 371○
0.0F - 372●
} else if val > 1.0F { - 373○
1.0F - 374
} else { - 375●
@math.powf(val, gamma) - 376
} - 377●
write_f32_le(out, offset, corrected) - 378
} - 379
if ch == 4 { - 380○
let alpha_off = i * bpp + 12 - 381
for b in 0..<4 { - 382○
out[alpha_off + b] = img.data[alpha_off + b] - 383
} - 384
} - 385
} - 386
@types.ImageF::{ - 387
width: img.width, - 388
height: img.height, - 389
channels: ch, - 390●
data: Bytes::from_array(out), - 391
} - 392
} - 393
- 394
///| - 395
/// 16-bit 反色(负片)。new = 65535 - old。仅影响颜色通道,alpha 不变。 - 396
/// @see invert 8-bit 版本,本函数为 Image16 变体 - 397
pub fn invert_16(img : @types.Image16) -> @types.Image16 raise @types.LoadError { - 398
// 输入验证 - 399●
if img.width <= 0 || img.height <= 0 { - 400●
raise @types.LoadError::DecodeFailed("invert_16: 图像尺寸必须大于0") - 401
} - 402
if img.channels < 1 || img.channels > 4 { - 403●
raise @types.LoadError::DecodeFailed( - 404
"invert_16: 通道数必须在1-4之间", - 405
) - 406
} - 407
let expected_size = img.width * img.height * img.channels * 2 - 408●
if img.data.length() != expected_size { - 409●
raise @types.LoadError::DecodeFailed( - 410
"invert_16: 数据大小不匹配,期望 " + - 411●
expected_size.to_string() + - 412
",实际 " + - 413●
img.data.length().to_string(), - 414
) - 415
} - 416
let n = img.width * img.height - 417
let ch = img.channels - 418●
let color_ch = if ch >= 3 { 3 } else { ch } - 419
let bpp = ch * 2 - 420●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 421
for i in 0..<n { - 422●
for c in 0..<color_ch { - 423●
let offset = i * bpp + c * 2 - 424●
let val = read_u16_le_c(img.data, offset) - 425●
write_u16_le_c(out, offset, 65535 - val) - 426
} - 427
if ch == 4 { - 428○
let alpha_off = i * bpp + 6 - 429
out[alpha_off] = img.data[alpha_off] - 430
out[alpha_off + 1] = img.data[alpha_off + 1] - 431
} - 432
} - 433
@types.Image16::{ - 434
width: img.width, - 435
height: img.height, - 436
channels: ch, - 437●
data: Bytes::from_array(out), - 438
} - 439
} - 440
- 441
///| - 442
/// float 反色(负片)。new = 1.0 - old。仅影响颜色通道,alpha 不变。 - 443
/// @see invert 8-bit 版本,本函数为 ImageF 变体 - 444
pub fn invert_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError { - 445
// 输入验证 - 446●
if img.width <= 0 || img.height <= 0 { - 447●
raise @types.LoadError::DecodeFailed("invert_f: 图像尺寸必须大于0") - 448
} - 449
if img.channels < 1 || img.channels > 4 { - 450●
raise @types.LoadError::DecodeFailed( - 451
"invert_f: 通道数必须在1-4之间", - 452
) - 453
} - 454
let expected_size = img.width * img.height * img.channels * 4 - 455●
if img.data.length() != expected_size { - 456●
raise @types.LoadError::DecodeFailed( - 457
"invert_f: 数据大小不匹配,期望 " + - 458●
expected_size.to_string() + - 459
",实际 " + - 460●
img.data.length().to_string(), - 461
) - 462
} - 463
let n = img.width * img.height - 464
let ch = img.channels - 465●
let color_ch = if ch >= 3 { 3 } else { ch } - 466
let bpp = ch * 4 - 467●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 468
for i in 0..<n { - 469●
for c in 0..<color_ch { - 470●
let offset = i * bpp + c * 4 - 471●
let val = read_float_le(img.data, offset) - 472●
write_f32_le(out, offset, 1.0F - val) - 473
} - 474
if ch == 4 { - 475○
let alpha_off = i * bpp + 12 - 476
for b in 0..<4 { - 477○
out[alpha_off + b] = img.data[alpha_off + b] - 478
} - 479
} - 480
} - 481
@types.ImageF::{ - 482
width: img.width, - 483
height: img.height, - 484
channels: ch, - 485●
data: Bytes::from_array(out), - 486
} - 487
} - 488
- 489
///| - 490
/// 16-bit 转灰度。ITU-R BT.601: Y = 0.299R + 0.587G + 0.114B。 - 491
/// 输出 channels=1 的 16-bit 灰度图。 - 492
/// @see to_grayscale 8-bit 版本,本函数为 Image16 变体 - 493
pub fn to_grayscale_16( - 494
img : @types.Image16, - 495
) -> @types.Image16 raise @types.LoadError { - 496
// 输入验证 - 497●
if img.width <= 0 || img.height <= 0 { - 498●
raise @types.LoadError::DecodeFailed( - 499
"to_grayscale_16: 图像尺寸必须大于0", - 500
) - 501
} - 502
if img.channels < 1 || img.channels > 4 { - 503●
raise @types.LoadError::DecodeFailed( - 504
"to_grayscale_16: 通道数必须在1-4之间", - 505
) - 506
} - 507
let expected_size = img.width * img.height * img.channels * 2 - 508●
if img.data.length() != expected_size { - 509●
raise @types.LoadError::DecodeFailed( - 510
"to_grayscale_16: 数据大小不匹配,期望 " + - 511●
expected_size.to_string() + - 512
",实际 " + - 513●
img.data.length().to_string(), - 514
) - 515
} - 516
let n = img.width * img.height - 517
let ch = img.channels - 518
let bpp = ch * 2 - 519●
let out : Array[Byte] = Array::make(n * 2, b'\x00') - 520
for i in 0..<n { - 521●
if ch >= 3 { - 522●
let r = read_u16_le_c(img.data, i * bpp) - 523●
let g = read_u16_le_c(img.data, i * bpp + 2) - 524●
let b = read_u16_le_c(img.data, i * bpp + 4) - 525●
let y = (Float::from_int(r) * 0.299F + - 526●
Float::from_int(g) * 0.587F + - 527●
Float::from_int(b) * 0.114F + - 528●
0.5F).to_int() - 529◐
let clamped = if y < 0 { 0 } else if y > 65535 { 65535 } else { y } - 530●
write_u16_le_c(out, i * 2, clamped) - 531
} else { - 532○
let val = read_u16_le_c(img.data, i * bpp) - 533○
write_u16_le_c(out, i * 2, val) - 534
} - 535
} - 536
@types.Image16::{ - 537
width: img.width, - 538
height: img.height, - 539
channels: 1, - 540●
data: Bytes::from_array(out), - 541
} - 542
} - 543
- 544
///| - 545
/// float 转灰度。ITU-R BT.601: Y = 0.299R + 0.587G + 0.114B。 - 546
/// 输出 channels=1 的 float 灰度图。 - 547
/// @see to_grayscale 8-bit 版本,本函数为 ImageF 变体 - 548
pub fn to_grayscale_f( - 549
img : @types.ImageF, - 550
) -> @types.ImageF raise @types.LoadError { - 551
// 输入验证 - 552●
if img.width <= 0 || img.height <= 0 { - 553●
raise @types.LoadError::DecodeFailed( - 554
"to_grayscale_f: 图像尺寸必须大于0", - 555
) - 556
} - 557
if img.channels < 1 || img.channels > 4 { - 558●
raise @types.LoadError::DecodeFailed( - 559
"to_grayscale_f: 通道数必须在1-4之间", - 560
) - 561
} - 562
let expected_size = img.width * img.height * img.channels * 4 - 563●
if img.data.length() != expected_size { - 564●
raise @types.LoadError::DecodeFailed( - 565
"to_grayscale_f: 数据大小不匹配,期望 " + - 566●
expected_size.to_string() + - 567
",实际 " + - 568●
img.data.length().to_string(), - 569
) - 570
} - 571
let n = img.width * img.height - 572
let ch = img.channels - 573
let bpp = ch * 4 - 574●
let out : Array[Byte] = Array::make(n * 4, b'\x00') - 575
for i in 0..<n { - 576●
if ch >= 3 { - 577●
let r = read_float_le(img.data, i * bpp) - 578●
let g = read_float_le(img.data, i * bpp + 4) - 579●
let b = read_float_le(img.data, i * bpp + 8) - 580
let y = r * 0.299F + g * 0.587F + b * 0.114F - 581●
write_f32_le(out, i * 4, y) - 582
} else { - 583○
let val = read_float_le(img.data, i * bpp) - 584○
write_f32_le(out, i * 4, val) - 585
} - 586
} - 587
@types.ImageF::{ - 588
width: img.width, - 589
height: img.height, - 590
channels: 1, - 591●
data: Bytes::from_array(out), - 592
} - 593
}