image.process\feature\histogram_16f.mbt
95.8% · 252 / 263 points · 674 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 变体 Histogram operations 16-bit/float variants - 3
/// - 4
/// 16位直方图操作 histogram_16 / histogram_equalize_16 - 5
/// 16位/浮点直方图操作 16-bit/float histogram operations - 6
// 直方图操作 16-bit/float 变体 - 7
// 支持 Image16 / ImageF 两种类型 - 8
- 9
// ========== 辅助函数 ========== - 10
- 11
///| - 12
fn read_u16_le_h(data : Bytes, offset : Int32) -> Int32 { - 13●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 14
} - 15
- 16
///| - 17
fn write_u16_le_h(buf : Array[Byte], offset : Int32, val : Int32) -> Unit { - 18●
buf[offset] = (val & 0xFF).to_byte() - 19●
buf[offset + 1] = ((val >> 8) & 0xFF).to_byte() - 20
} - 21
- 22
///| - 23
fn read_float_le_h(data : Bytes, offset : Int32) -> Float { - 24●
let b0 = data[offset].to_int() - 25●
let b1 = data[offset + 1].to_int() - 26●
let b2 = data[offset + 2].to_int() - 27●
let b3 = data[offset + 3].to_int() - 28
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 29●
Float::reinterpret_from_int(bits) - 30
} - 31
- 32
///| - 33
fn write_f32_le_h(buf : Array[Byte], offset : Int32, val : Float) -> Unit { - 34●
let bits = val.reinterpret_as_int() - 35●
buf[offset] = (bits & 0xFF).to_byte() - 36●
buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 37●
buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 38●
buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 39
} - 40
- 41
// ========== histogram ========== - 42
- 43
///| - 44
/// 16-bit 直方图。返回 65536 个 bin 的计数数组。彩色图像先转灰度(BT.601)。 - 45
/// @see histogram 8-bit 版本,本函数为 Image16 变体 - 46
pub fn histogram_16( - 47
img : @types.Image16, - 48
) -> Array[Int32] raise @types.LoadError { - 49
// 输入验证 - 50●
if img.width <= 0 || img.height <= 0 { - 51●
raise @types.LoadError::DecodeFailed( - 52
"histogram_16: 图像尺寸必须大于0", - 53
) - 54
} - 55
if img.channels < 1 || img.channels > 4 { - 56●
raise @types.LoadError::DecodeFailed( - 57
"histogram_16: 通道数必须在1-4之间", - 58
) - 59
} - 60
let expected_size = img.width * img.height * img.channels * 2 - 61●
if img.data.length() != expected_size { - 62●
raise @types.LoadError::DecodeFailed( - 63
"histogram_16: 数据大小不匹配,期望 " + - 64●
expected_size.to_string() + - 65
",实际 " + - 66●
img.data.length().to_string(), - 67
) - 68
} - 69
- 70●
let hist : Array[Int32] = Array::make(65536, 0) - 71
let n = img.width * img.height - 72
let ch = img.channels - 73
for i in 0..<n { - 74●
let v = if ch == 1 { - 75●
read_u16_le_h(img.data, i * 2) - 76
} else { - 77●
let r = read_u16_le_h(img.data, i * ch * 2) - 78●
let g = read_u16_le_h(img.data, i * ch * 2 + 2) - 79●
let b = read_u16_le_h(img.data, i * ch * 2 + 4) - 80
(r * 299 + g * 587 + b * 114) / 1000 - 81
} - 82
hist[v] += 1 - 83
} - 84
hist - 85
} - 86
- 87
///| - 88
/// float 直方图。将值归一化到 [0,1] 后分 256 个 bin。彩色图像先转灰度。 - 89
/// @see histogram 8-bit 版本,本函数为 ImageF 变体 - 90
pub fn histogram_f(img : @types.ImageF) -> Array[Int32] raise @types.LoadError { - 91
// 输入验证 - 92●
if img.width <= 0 || img.height <= 0 { - 93●
raise @types.LoadError::DecodeFailed( - 94
"histogram_f: 图像尺寸必须大于0", - 95
) - 96
} - 97
if img.channels < 1 || img.channels > 4 { - 98●
raise @types.LoadError::DecodeFailed( - 99
"histogram_f: 通道数必须在1-4之间", - 100
) - 101
} - 102
let expected_size = img.width * img.height * img.channels * 4 - 103●
if img.data.length() != expected_size { - 104●
raise @types.LoadError::DecodeFailed( - 105
"histogram_f: 数据大小不匹配,期望 " + - 106●
expected_size.to_string() + - 107
",实际 " + - 108●
img.data.length().to_string(), - 109
) - 110
} - 111
- 112●
let hist : Array[Int32] = Array::make(256, 0) - 113
let n = img.width * img.height - 114
let ch = img.channels - 115
for i in 0..<n { - 116●
let v = if ch == 1 { - 117●
read_float_le_h(img.data, i * 4) - 118
} else { - 119●
let r = read_float_le_h(img.data, i * ch * 4) - 120●
let g = read_float_le_h(img.data, i * ch * 4 + 4) - 121●
let b = read_float_le_h(img.data, i * ch * 4 + 8) - 122
r * 0.299F + g * 0.587F + b * 0.114F - 123
} - 124
let bin = if v < 0.0F { - 125●
0 - 126●
} else if v >= 1.0F { - 127●
255 - 128
} else { - 129●
(v * 255.0F + 0.5F).to_int() - 130
} - 131
hist[bin] += 1 - 132
} - 133
hist - 134
} - 135
- 136
// ========== histogram_equalize ========== - 137
- 138
///| - 139
/// 16-bit 直方图均衡化。基于灰度直方图计算映射 LUT,应用到所有颜色通道。 - 140
/// @see histogram_equalize 8-bit 版本,本函数为 Image16 变体 - 141
pub fn histogram_equalize_16( - 142
img : @types.Image16, - 143
) -> @types.Image16 raise @types.LoadError { - 144●
let hist = histogram_16(img) - 145
let n = img.width * img.height - 146
let ch = img.channels - 147
let bpp = ch * 2 - 148
// 计算 CDF - 149●
let cdf : Array[Int32] = Array::make(65536, 0) - 150
let mut cum = 0 - 151
for i in 0..<65536 { - 152●
cum += hist[i] - 153
cdf[i] = cum - 154
} - 155
let mut cdf_min = 0 - 156
for i in 0..<65536 { - 157●
if cdf[i] > 0 { - 158●
cdf_min = cdf[i] - 159
break - 160
} - 161
} - 162
let denom = n - cdf_min - 163
if denom <= 0 { - 164●
return img - 165
} - 166
// 计算映射 LUT - 167●
let lut : Array[Int32] = Array::make(65536, 0) - 168
for i in 0..<65536 { - 169●
let mapped = (cdf[i] - cdf_min) * 65535 / denom - 170●
lut[i] = @types.clamp_int(mapped, 0, 65535) - 171
} - 172
// 应用 LUT - 173●
let color_ch = if ch >= 3 { 3 } else { ch } - 174
let n_total = n * ch - 175●
let src : Array[Int32] = Array::make(n_total, 0) - 176
for i in 0..<n_total { - 177●
src[i] = read_u16_le_h(img.data, i * 2) - 178
} - 179●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 180
for i in 0..<n { - 181●
for c in 0..<color_ch { - 182●
write_u16_le_h(out, i * bpp + c * 2, lut[src[i * ch + c]]) - 183
} - 184
if ch == 4 { - 185○
let alpha_off = i * bpp + 6 - 186
out[alpha_off] = img.data[alpha_off] - 187
out[alpha_off + 1] = img.data[alpha_off + 1] - 188
} - 189
} - 190
@types.Image16::{ - 191
width: img.width, - 192
height: img.height, - 193
channels: ch, - 194●
data: Bytes::from_array(out), - 195
} - 196
} - 197
- 198
///| - 199
/// float 直方图均衡化。基于灰度直方图计算映射 LUT,应用到所有颜色通道。 - 200
/// @see histogram_equalize 8-bit 版本,本函数为 ImageF 变体 - 201
pub fn histogram_equalize_f( - 202
img : @types.ImageF, - 203
) -> @types.ImageF raise @types.LoadError { - 204●
let hist = histogram_f(img) - 205
let n = img.width * img.height - 206
let ch = img.channels - 207
let bpp = ch * 4 - 208●
let cdf : Array[Int32] = Array::make(256, 0) - 209
let mut cum = 0 - 210
for i in 0..<256 { - 211●
cum += hist[i] - 212
cdf[i] = cum - 213
} - 214
let mut cdf_min = 0 - 215
for i in 0..<256 { - 216●
if cdf[i] > 0 { - 217●
cdf_min = cdf[i] - 218
break - 219
} - 220
} - 221
let denom = n - cdf_min - 222
if denom <= 0 { - 223●
return img - 224
} - 225●
let lut : Array[Float] = Array::make(256, 0.0F) - 226
for i in 0..<256 { - 227●
let mapped = Float::from_int(cdf[i] - cdf_min) / Float::from_int(denom) - 228●
lut[i] = @types.clamp_float(mapped, 0.0F, 1.0F) - 229
} - 230◐
let color_ch = if ch >= 3 { 3 } else { ch } - 231
let n_total = n * ch - 232●
let src : Array[Float] = Array::make(n_total, 0.0F) - 233
for i in 0..<n_total { - 234●
src[i] = read_float_le_h(img.data, i * 4) - 235
} - 236●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 237
for i in 0..<n { - 238●
for c in 0..<color_ch { - 239●
let v = src[i * ch + c] - 240
let bin = if v < 0.0F { - 241●
0 - 242●
} else if v >= 1.0F { - 243●
255 - 244
} else { - 245●
(v * 255.0F + 0.5F).to_int() - 246
} - 247●
write_f32_le_h(out, i * bpp + c * 4, lut[bin]) - 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
// ========== histogram_normalize ========== - 265
- 266
///| - 267
/// 16-bit 直方图归一化(线性拉伸)。将像素值范围线性映射到 [0, 65535]。 - 268
/// @see histogram_normalize 8-bit 版本,本函数为 Image16 变体 - 269
pub fn histogram_normalize_16( - 270
img : @types.Image16, - 271
) -> @types.Image16 raise @types.LoadError { - 272
// 输入验证 - 273●
if img.width <= 0 || img.height <= 0 { - 274●
raise @types.LoadError::DecodeFailed( - 275
"histogram_normalize_16: 图像尺寸必须大于0", - 276
) - 277
} - 278
if img.channels < 1 || img.channels > 4 { - 279●
raise @types.LoadError::DecodeFailed( - 280
"histogram_normalize_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
"histogram_normalize_16: 数据大小不匹配,期望 " + - 287●
expected_size.to_string() + - 288
",实际 " + - 289●
img.data.length().to_string(), - 290
) - 291
} - 292
- 293
let ch = img.channels - 294●
let color_ch = if ch >= 3 { 3 } else { ch } - 295
let n = img.width * img.height - 296
let bpp = ch * 2 - 297
let n_total = n * ch - 298●
let src : Array[Int32] = Array::make(n_total, 0) - 299
for i in 0..<n_total { - 300●
src[i] = read_u16_le_h(img.data, i * 2) - 301
} - 302
let mut min_val = 65535 - 303
let mut max_val = 0 - 304
for i in 0..<n { - 305●
for c in 0..<color_ch { - 306●
let v = src[i * ch + c] - 307
if v < min_val { - 308●
min_val = v - 309
} - 310
if v > max_val { - 311●
max_val = v - 312
} - 313
} - 314
} - 315
let range = max_val - min_val - 316
if range == 0 { - 317●
return img - 318
} - 319●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 320
for i in 0..<n { - 321●
for c in 0..<color_ch { - 322●
let v = src[i * ch + c] - 323
let scaled = (v - min_val) * 65535 / range - 324●
write_u16_le_h(out, i * bpp + c * 2, @types.clamp_int(scaled, 0, 65535)) - 325
} - 326
if ch == 4 { - 327●
let alpha_off = i * bpp + 6 - 328
out[alpha_off] = img.data[alpha_off] - 329
out[alpha_off + 1] = img.data[alpha_off + 1] - 330
} - 331
} - 332
@types.Image16::{ - 333
width: img.width, - 334
height: img.height, - 335
channels: ch, - 336●
data: Bytes::from_array(out), - 337
} - 338
} - 339
- 340
///| - 341
/// float 直方图归一化(线性拉伸)。将像素值范围线性映射到 [0.0, 1.0]。 - 342
/// @see histogram_normalize 8-bit 版本,本函数为 ImageF 变体 - 343
pub fn histogram_normalize_f( - 344
img : @types.ImageF, - 345
) -> @types.ImageF raise @types.LoadError { - 346
// 输入验证 - 347●
if img.width <= 0 || img.height <= 0 { - 348●
raise @types.LoadError::DecodeFailed( - 349
"histogram_normalize_f: 图像尺寸必须大于0", - 350
) - 351
} - 352
if img.channels < 1 || img.channels > 4 { - 353●
raise @types.LoadError::DecodeFailed( - 354
"histogram_normalize_f: 通道数必须在1-4之间", - 355
) - 356
} - 357
let expected_size = img.width * img.height * img.channels * 4 - 358●
if img.data.length() != expected_size { - 359●
raise @types.LoadError::DecodeFailed( - 360
"histogram_normalize_f: 数据大小不匹配,期望 " + - 361●
expected_size.to_string() + - 362
",实际 " + - 363●
img.data.length().to_string(), - 364
) - 365
} - 366
- 367
let ch = img.channels - 368●
let color_ch = if ch >= 3 { 3 } else { ch } - 369
let n = img.width * img.height - 370
let bpp = ch * 4 - 371
let n_total = n * ch - 372●
let src : Array[Float] = Array::make(n_total, 0.0F) - 373
for i in 0..<n_total { - 374●
src[i] = read_float_le_h(img.data, i * 4) - 375
} - 376
let mut min_val = 1.0F - 377
let mut max_val = 0.0F - 378
for i in 0..<n { - 379●
for c in 0..<color_ch { - 380●
let v = src[i * ch + c] - 381
if v < min_val { - 382●
min_val = v - 383
} - 384
if v > max_val { - 385●
max_val = v - 386
} - 387
} - 388
} - 389
let range = max_val - min_val - 390
if range <= 0.000001F { - 391●
return img - 392
} - 393●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 394
for i in 0..<n { - 395●
for c in 0..<color_ch { - 396●
let v = src[i * ch + c] - 397
let scaled = (v - min_val) / range - 398●
write_f32_le_h( - 399
out, - 400
i * bpp + c * 4, - 401●
@types.clamp_float(scaled, 0.0F, 1.0F), - 402
) - 403
} - 404
if ch == 4 { - 405●
let alpha_off = i * bpp + 12 - 406
for b in 0..<4 { - 407●
out[alpha_off + b] = img.data[alpha_off + b] - 408
} - 409
} - 410
} - 411
@types.ImageF::{ - 412
width: img.width, - 413
height: img.height, - 414
channels: ch, - 415●
data: Bytes::from_array(out), - 416
} - 417
} - 418
- 419
// ========== histogram_matching ========== - 420
- 421
///| - 422
/// 16-bit 直方图匹配(规定化)。将输入图像的直方图匹配到参考图像的直方图。 - 423
/// @see histogram_matching 8-bit 版本,本函数为 Image16 变体 - 424
pub fn histogram_matching_16( - 425
img : @types.Image16, - 426
reference : @types.Image16, - 427
) -> @types.Image16 raise @types.LoadError { - 428
// 输入验证 - 429●
if img.width <= 0 || img.height <= 0 { - 430●
raise @types.LoadError::DecodeFailed( - 431
"histogram_matching_16: 图像尺寸必须大于0", - 432
) - 433
} - 434
if img.channels < 1 || img.channels > 4 { - 435●
raise @types.LoadError::DecodeFailed( - 436
"histogram_matching_16: 通道数必须在1-4之间", - 437
) - 438
} - 439
let expected_size = img.width * img.height * img.channels * 2 - 440●
if img.data.length() != expected_size { - 441●
raise @types.LoadError::DecodeFailed( - 442
"histogram_matching_16: 数据大小不匹配,期望 " + - 443●
expected_size.to_string() + - 444
",实际 " + - 445●
img.data.length().to_string(), - 446
) - 447
} - 448
if reference.width <= 0 || reference.height <= 0 { - 449○
raise @types.LoadError::DecodeFailed( - 450
"histogram_matching_16: 参考图像尺寸必须大于0", - 451
) - 452
} - 453
if reference.channels < 1 || reference.channels > 4 { - 454●
raise @types.LoadError::DecodeFailed( - 455
"histogram_matching_16: 参考图像通道数必须在1-4之间", - 456
) - 457
} - 458
let ref_expected_size = reference.width * - 459
reference.height * - 460
reference.channels * - 461
2 - 462●
if reference.data.length() != ref_expected_size { - 463●
raise @types.LoadError::DecodeFailed( - 464
"histogram_matching_16: 参考图像数据大小不匹配,期望 " + - 465●
ref_expected_size.to_string() + - 466
",实际 " + - 467●
reference.data.length().to_string(), - 468
) - 469
} - 470
- 471
let ch = img.channels - 472●
let color_ch = if ch >= 3 { 3 } else { ch } - 473
let n = img.width * img.height - 474
let bpp = ch * 2 - 475●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 476
let n_ref = reference.width * reference.height - 477
let ref_ch = reference.channels - 478●
let ref_color_ch = if ref_ch >= 3 { 3 } else { ref_ch } - 479
for c in 0..<color_ch { - 480●
let hist_in : Array[Int32] = Array::make(65536, 0) - 481●
let hist_ref : Array[Int32] = Array::make(65536, 0) - 482
for i in 0..<n { - 483●
hist_in[read_u16_le_h(img.data, i * bpp + c * 2)] += 1 - 484
} - 485
if c < ref_color_ch { - 486●
for i in 0..<n_ref { - 487●
hist_ref[read_u16_le_h(reference.data, i * ref_ch * 2 + c * 2)] += 1 - 488
} - 489
} - 490●
let cdf_in : Array[Float] = Array::make(65536, 0.0F) - 491●
let cdf_ref : Array[Float] = Array::make(65536, 0.0F) - 492
let mut cum_in = 0 - 493
let mut cum_ref = 0 - 494
for i in 0..<65536 { - 495●
cum_in += hist_in[i] - 496●
cdf_in[i] = Float::from_int(cum_in) / Float::from_int(n) - 497
cum_ref += hist_ref[i] - 498●
cdf_ref[i] = Float::from_int(cum_ref) / Float::from_int(n_ref) - 499
} - 500●
let lut : Array[Int32] = Array::make(65536, 0) - 501
for i in 0..<65536 { - 502●
let target = cdf_in[i] - 503
let mut best_j = 0 - 504
let mut best_diff = 1.0F - 505
for j in 0..<65536 { - 506○
let diff = (cdf_ref[j] - target).abs() - 507
if diff < best_diff { - 508●
best_diff = diff - 509
best_j = j - 510
} - 511
} - 512
lut[i] = best_j - 513
} - 514
for i in 0..<n { - 515●
let v = read_u16_le_h(img.data, i * bpp + c * 2) - 516●
write_u16_le_h(out, i * bpp + c * 2, lut[v]) - 517
} - 518
} - 519
if ch == 4 { - 520●
for i in 0..<n { - 521●
let alpha_off = i * bpp + 6 - 522
out[alpha_off] = img.data[alpha_off] - 523
out[alpha_off + 1] = img.data[alpha_off + 1] - 524
} - 525
} - 526
@types.Image16::{ - 527
width: img.width, - 528
height: img.height, - 529
channels: ch, - 530●
data: Bytes::from_array(out), - 531
} - 532
} - 533
- 534
///| - 535
/// float 直方图匹配(规定化)。将输入图像的直方图匹配到参考图像的直方图。 - 536
/// @see histogram_matching 8-bit 版本,本函数为 ImageF 变体 - 537
pub fn histogram_matching_f( - 538
img : @types.ImageF, - 539
reference : @types.ImageF, - 540
) -> @types.ImageF raise @types.LoadError { - 541
// 输入验证 - 542●
if img.width <= 0 || img.height <= 0 { - 543●
raise @types.LoadError::DecodeFailed( - 544
"histogram_matching_f: 图像尺寸必须大于0", - 545
) - 546
} - 547
if img.channels < 1 || img.channels > 4 { - 548●
raise @types.LoadError::DecodeFailed( - 549
"histogram_matching_f: 通道数必须在1-4之间", - 550
) - 551
} - 552
let expected_size = img.width * img.height * img.channels * 4 - 553●
if img.data.length() != expected_size { - 554●
raise @types.LoadError::DecodeFailed( - 555
"histogram_matching_f: 数据大小不匹配,期望 " + - 556●
expected_size.to_string() + - 557
",实际 " + - 558●
img.data.length().to_string(), - 559
) - 560
} - 561
if reference.width <= 0 || reference.height <= 0 { - 562○
raise @types.LoadError::DecodeFailed( - 563
"histogram_matching_f: 参考图像尺寸必须大于0", - 564
) - 565
} - 566
if reference.channels < 1 || reference.channels > 4 { - 567●
raise @types.LoadError::DecodeFailed( - 568
"histogram_matching_f: 参考图像通道数必须在1-4之间", - 569
) - 570
} - 571
let ref_expected_size = reference.width * - 572
reference.height * - 573
reference.channels * - 574
4 - 575●
if reference.data.length() != ref_expected_size { - 576●
raise @types.LoadError::DecodeFailed( - 577
"histogram_matching_f: 参考图像数据大小不匹配,期望 " + - 578●
ref_expected_size.to_string() + - 579
",实际 " + - 580●
reference.data.length().to_string(), - 581
) - 582
} - 583
- 584
let ch = img.channels - 585●
let color_ch = if ch >= 3 { 3 } else { ch } - 586
let n = img.width * img.height - 587
let bpp = ch * 4 - 588●
let out : Array[Byte] = Array::make(n * bpp, b'\x00') - 589
let n_ref = reference.width * reference.height - 590
let ref_ch = reference.channels - 591●
let ref_color_ch = if ref_ch >= 3 { 3 } else { ref_ch } - 592
for c in 0..<color_ch { - 593●
let hist_in : Array[Int32] = Array::make(256, 0) - 594●
let hist_ref : Array[Int32] = Array::make(256, 0) - 595
for i in 0..<n { - 596●
let v = read_float_le_h(img.data, i * bpp + c * 4) - 597
let bin = if v < 0.0F { - 598●
0 - 599●
} else if v >= 1.0F { - 600●
255 - 601
} else { - 602●
(v * 255.0F + 0.5F).to_int() - 603
} - 604
hist_in[bin] += 1 - 605
} - 606
if c < ref_color_ch { - 607●
for i in 0..<n_ref { - 608●
let v = read_float_le_h(reference.data, i * ref_ch * 4 + c * 4) - 609
let bin = if v < 0.0F { - 610●
0 - 611●
} else if v >= 1.0F { - 612●
255 - 613
} else { - 614●
(v * 255.0F + 0.5F).to_int() - 615
} - 616
hist_ref[bin] += 1 - 617
} - 618
} - 619●
let cdf_in : Array[Float] = Array::make(256, 0.0F) - 620●
let cdf_ref : Array[Float] = Array::make(256, 0.0F) - 621
let mut cum_in = 0 - 622
let mut cum_ref = 0 - 623
for i in 0..<256 { - 624●
cum_in += hist_in[i] - 625●
cdf_in[i] = Float::from_int(cum_in) / Float::from_int(n) - 626
cum_ref += hist_ref[i] - 627●
cdf_ref[i] = Float::from_int(cum_ref) / Float::from_int(n_ref) - 628
} - 629●
let lut : Array[Int32] = Array::make(256, 0) - 630
for i in 0..<256 { - 631●
let target = cdf_in[i] - 632
let mut best_j = 0 - 633
let mut best_diff = 1.0F - 634
for j in 0..<256 { - 635●
let diff = (cdf_ref[j] - target).abs() - 636
if diff < best_diff { - 637●
best_diff = diff - 638
best_j = j - 639
} - 640
} - 641
lut[i] = best_j - 642
} - 643
for i in 0..<n { - 644●
let v = read_float_le_h(img.data, i * bpp + c * 4) - 645
let bin = if v < 0.0F { - 646●
0 - 647●
} else if v >= 1.0F { - 648●
255 - 649
} else { - 650●
(v * 255.0F + 0.5F).to_int() - 651
} - 652●
let mapped = Float::from_int(lut[bin]) / 255.0F - 653●
write_f32_le_h( - 654
out, - 655
i * bpp + c * 4, - 656●
@types.clamp_float(mapped, 0.0F, 1.0F), - 657
) - 658
} - 659
} - 660
if ch == 4 { - 661○
for i in 0..<n { - 662○
let alpha_off = i * bpp + 12 - 663
for b in 0..<4 { - 664○
out[alpha_off + b] = img.data[alpha_off + b] - 665
} - 666
} - 667
} - 668
@types.ImageF::{ - 669
width: img.width, - 670
height: img.height, - 671
channels: ch, - 672●
data: Bytes::from_array(out), - 673
} - 674
}