image.process\color\retinex_16f.mbt
92.5% · 236 / 255 points · 524 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 多尺度 Retinex 16-bit/float 变体 Multi-scale Retinex 16-bit/float variants - 3
/// - 4
/// 16位/浮点多尺度 Retinex msrcr_16 / msrcr_f - 5
/// 16位/浮点多尺度 Retinex 色彩恢复 16-bit/float Multi-Scale Retinex with Color Restoration - 6
// 多尺度 Retinex 16-bit/float 变体 - 7
// SSR / MSR / MSRCR - 8
- 9
///| - 10
fn logf_r(x : Float) -> Float { - 11●
Float::from_double(@math.log2(x.to_double()) * 0.6931471805599453) - 12
} - 13
- 14
///| - 15
fn read_u16_r(data : Bytes, offset : Int32) -> Int32 { - 16●
data[offset].to_int() | (data[offset + 1].to_int() << 8) - 17
} - 18
- 19
///| - 20
fn write_u16_r(data : Array[Byte], offset : Int32, v : Int32) -> Unit { - 21●
data[offset] = (v & 0xFF).to_byte() - 22●
data[offset + 1] = ((v >> 8) & 0xFF).to_byte() - 23
} - 24
- 25
///| - 26
fn read_f32_r(data : Bytes, offset : Int32) -> Float { - 27●
let b0 = data[offset].to_int() - 28●
let b1 = data[offset + 1].to_int() - 29●
let b2 = data[offset + 2].to_int() - 30●
let b3 = data[offset + 3].to_int() - 31
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 32●
Float::reinterpret_from_int(bits) - 33
} - 34
- 35
///| - 36
fn write_f32_r(data : Array[Byte], offset : Int32, v : Float) -> Unit { - 37●
let bits = v.reinterpret_as_int() - 38●
data[offset] = (bits & 0xFF).to_byte() - 39●
data[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 40●
data[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 41●
data[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 42
} - 43
- 44
// ========== SSR 16-bit ========== - 45
- 46
///| - 47
/// 单尺度 Retinex(SSR)16-bit 版本。 - 48
/// @see ssr 8-bit 版本,本函数为 Image16 变体 - 49
pub fn ssr_16( - 50
img : @types.Image16, - 51
sigma~ : Float = 80.0F, - 52
gain~ : Float = 1.0F, - 53
offset~ : Float = 0.0F, - 54
) -> @types.Image16 raise @types.LoadError { - 55
// 输入验证 - 56●
if img.width == 0 || img.height == 0 { - 57●
return img - 58
} - 59
if img.width < 0 || img.height < 0 { - 60●
raise @types.LoadError::DecodeFailed("ssr_16: 图像尺寸不能为负") - 61
} - 62
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 63●
raise @types.LoadError::DecodeFailed("ssr_16 仅支持 channels=1/3/4") - 64
} - 65
let expected_size = img.width * img.height * img.channels * 2 - 66●
if img.data.length() != expected_size { - 67●
raise @types.LoadError::DecodeFailed( - 68
"ssr_16: 数据大小不匹配,期望 " + - 69●
expected_size.to_string() + - 70
",实际 " + - 71●
img.data.length().to_string(), - 72
) - 73
} - 74
if sigma <= 0.0F { - 75●
raise @types.LoadError::DecodeFailed("sigma 必须 > 0") - 76
} - 77
let ch = img.channels - 78
let w = img.width - 79
let h = img.height - 80
let n = w * h - 81
if n == 0 { - 82○
return img - 83
} - 84●
let out : Array[Byte] = Array::make(n * ch * 2, b'\x00') - 85
for c in 0..<ch { - 86●
if ch == 4 && c == 3 { - 87●
for i in 0..<n { - 88●
let v = read_u16_r(img.data, (i * ch + 3) * 2) - 89●
write_u16_r(out, (i * ch + 3) * 2, v) - 90
} - 91
continue - 92
} - 93●
let channel : Array[Float] = Array::make(n, 0.0F) - 94
for i in 0..<n { - 95●
let v = read_u16_r(img.data, (i * ch + c) * 2) - 96◐
channel[i] = Float::from_int(if v == 0 { 1 } else { v }) - 97
} - 98●
let blurred = gaussian_blur_channel(channel, w, h, sigma) - 99
for i in 0..<n { - 100●
let log_i = logf_r(channel[i]) - 101◐
let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F }) - 102
let retinex = (log_i - log_b) * gain + offset - 103●
let val = (retinex * 30.0F + 32768.0F).to_int() - 104◐
let clamped = if val < 0 { 0 } else if val > 65535 { 65535 } else { val } - 105●
write_u16_r(out, (i * ch + c) * 2, clamped) - 106
} - 107
} - 108
@types.Image16::{ - 109
width: w, - 110
height: h, - 111
channels: ch, - 112●
data: Bytes::from_array(out), - 113
} - 114
} - 115
- 116
// ========== SSR float ========== - 117
- 118
///| - 119
/// 单尺度 Retinex(SSR)float 版本。 - 120
/// @see ssr 8-bit 版本,本函数为 ImageF 变体 - 121
pub fn ssr_f( - 122
img : @types.ImageF, - 123
sigma~ : Float = 80.0F, - 124
gain~ : Float = 1.0F, - 125
offset~ : Float = 0.0F, - 126
) -> @types.ImageF raise @types.LoadError { - 127
// 输入验证 - 128●
if img.width == 0 || img.height == 0 { - 129●
return img - 130
} - 131
if img.width < 0 || img.height < 0 { - 132●
raise @types.LoadError::DecodeFailed("ssr_f: 图像尺寸不能为负") - 133
} - 134
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 135●
raise @types.LoadError::DecodeFailed("ssr_f 仅支持 channels=1/3/4") - 136
} - 137
let expected_size = img.width * img.height * img.channels * 4 - 138●
if img.data.length() != expected_size { - 139●
raise @types.LoadError::DecodeFailed( - 140
"ssr_f: 数据大小不匹配,期望 " + - 141●
expected_size.to_string() + - 142
",实际 " + - 143●
img.data.length().to_string(), - 144
) - 145
} - 146
if sigma <= 0.0F { - 147●
raise @types.LoadError::DecodeFailed("sigma 必须 > 0") - 148
} - 149
let ch = img.channels - 150
let w = img.width - 151
let h = img.height - 152
let n = w * h - 153
if n == 0 { - 154○
return img - 155
} - 156●
let out : Array[Byte] = Array::make(n * ch * 4, b'\x00') - 157
for c in 0..<ch { - 158●
if ch == 4 && c == 3 { - 159●
for i in 0..<n { - 160●
let v = read_f32_r(img.data, (i * ch + 3) * 4) - 161●
write_f32_r(out, (i * ch + 3) * 4, v) - 162
} - 163
continue - 164
} - 165●
let channel : Array[Float] = Array::make(n, 0.0F) - 166
for i in 0..<n { - 167●
let v = read_f32_r(img.data, (i * ch + c) * 4) - 168●
channel[i] = if v <= 0.0F { 0.0001F } else { v } - 169
} - 170●
let blurred = gaussian_blur_channel(channel, w, h, sigma) - 171
for i in 0..<n { - 172●
let log_i = logf_r(channel[i]) - 173◐
let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F }) - 174
let retinex = (log_i - log_b) * gain + offset - 175●
write_f32_r(out, (i * ch + c) * 4, retinex) - 176
} - 177
} - 178
@types.ImageF::{ - 179
width: w, - 180
height: h, - 181
channels: ch, - 182●
data: Bytes::from_array(out), - 183
} - 184
} - 185
- 186
// ========== MSR 16-bit ========== - 187
- 188
///| - 189
/// 多尺度 Retinex(MSR)16-bit 版本。 - 190
/// @see msr 8-bit 版本,本函数为 Image16 变体 - 191
pub fn msr_16( - 192
img : @types.Image16, - 193
sigmas~ : Array[Float] = [15.0F, 80.0F, 250.0F], - 194
gain~ : Float = 1.0F, - 195
offset~ : Float = 0.0F, - 196
) -> @types.Image16 raise @types.LoadError { - 197
// 输入验证 - 198●
if img.width <= 0 || img.height <= 0 { - 199●
raise @types.LoadError::DecodeFailed("msr_16: 图像尺寸必须大于0") - 200
} - 201
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 202●
raise @types.LoadError::DecodeFailed("msr_16 仅支持 channels=1/3/4") - 203
} - 204
let expected_size = img.width * img.height * img.channels * 2 - 205●
if img.data.length() != expected_size { - 206●
raise @types.LoadError::DecodeFailed( - 207
"msr_16: 数据大小不匹配,期望 " + - 208●
expected_size.to_string() + - 209
",实际 " + - 210●
img.data.length().to_string(), - 211
) - 212
} - 213●
if sigmas.length() == 0 { - 214●
raise @types.LoadError::DecodeFailed("sigmas 不能为空") - 215
} - 216
for s in sigmas { - 217●
if s <= 0.0F { - 218●
raise @types.LoadError::DecodeFailed("sigma 必须 > 0") - 219
} - 220
} - 221
let ch = img.channels - 222
let w = img.width - 223
let h = img.height - 224
let n = w * h - 225
if n == 0 { - 226○
return img - 227
} - 228●
let out : Array[Byte] = Array::make(n * ch * 2, b'\x00') - 229●
let num_scales = sigmas.length() - 230
for c in 0..<ch { - 231●
if ch == 4 && c == 3 { - 232●
for i in 0..<n { - 233●
let v = read_u16_r(img.data, (i * ch + 3) * 2) - 234●
write_u16_r(out, (i * ch + 3) * 2, v) - 235
} - 236
continue - 237
} - 238●
let channel : Array[Float] = Array::make(n, 0.0F) - 239
for i in 0..<n { - 240●
let v = read_u16_r(img.data, (i * ch + c) * 2) - 241◐
channel[i] = Float::from_int(if v == 0 { 1 } else { v }) - 242
} - 243●
let log_channel : Array[Float] = Array::make(n, 0.0F) - 244
for i in 0..<n { - 245●
log_channel[i] = logf_r(channel[i]) - 246
} - 247●
let retinex_sum : Array[Float] = Array::make(n, 0.0F) - 248
for sigma in sigmas { - 249●
let blurred = gaussian_blur_channel(channel, w, h, sigma) - 250
for i in 0..<n { - 251◐
let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F }) - 252
retinex_sum[i] += log_channel[i] - log_b - 253
} - 254
} - 255●
let inv_scales = gain / Float::from_int(num_scales) - 256
for i in 0..<n { - 257●
let retinex = retinex_sum[i] * inv_scales + offset - 258●
let val = (retinex * 30.0F + 32768.0F).to_int() - 259◐
let clamped = if val < 0 { 0 } else if val > 65535 { 65535 } else { val } - 260●
write_u16_r(out, (i * ch + c) * 2, clamped) - 261
} - 262
} - 263
@types.Image16::{ - 264
width: w, - 265
height: h, - 266
channels: ch, - 267●
data: Bytes::from_array(out), - 268
} - 269
} - 270
- 271
// ========== MSR float ========== - 272
- 273
///| - 274
/// 多尺度 Retinex(MSR)float 版本。 - 275
/// @see msr 8-bit 版本,本函数为 ImageF 变体 - 276
pub fn msr_f( - 277
img : @types.ImageF, - 278
sigmas~ : Array[Float] = [15.0F, 80.0F, 250.0F], - 279
gain~ : Float = 1.0F, - 280
offset~ : Float = 0.0F, - 281
) -> @types.ImageF raise @types.LoadError { - 282●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 283●
raise @types.LoadError::DecodeFailed("msr_f 仅支持 channels=1/3/4") - 284
} - 285●
if sigmas.length() == 0 { - 286●
raise @types.LoadError::DecodeFailed("sigmas 不能为空") - 287
} - 288
for s in sigmas { - 289●
if s <= 0.0F { - 290●
raise @types.LoadError::DecodeFailed("sigma 必须 > 0") - 291
} - 292
} - 293
let ch = img.channels - 294
let w = img.width - 295
let h = img.height - 296
let n = w * h - 297
if n == 0 { - 298●
return img - 299
} - 300●
let out : Array[Byte] = Array::make(n * ch * 4, b'\x00') - 301●
let num_scales = sigmas.length() - 302
for c in 0..<ch { - 303●
if ch == 4 && c == 3 { - 304●
for i in 0..<n { - 305●
let v = read_f32_r(img.data, (i * ch + 3) * 4) - 306●
write_f32_r(out, (i * ch + 3) * 4, v) - 307
} - 308
continue - 309
} - 310●
let channel : Array[Float] = Array::make(n, 0.0F) - 311
for i in 0..<n { - 312●
let v = read_f32_r(img.data, (i * ch + c) * 4) - 313●
channel[i] = if v <= 0.0F { 0.0001F } else { v } - 314
} - 315●
let log_channel : Array[Float] = Array::make(n, 0.0F) - 316
for i in 0..<n { - 317●
log_channel[i] = logf_r(channel[i]) - 318
} - 319●
let retinex_sum : Array[Float] = Array::make(n, 0.0F) - 320
for sigma in sigmas { - 321●
let blurred = gaussian_blur_channel(channel, w, h, sigma) - 322
for i in 0..<n { - 323◐
let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F }) - 324
retinex_sum[i] += log_channel[i] - log_b - 325
} - 326
} - 327●
let inv_scales = gain / Float::from_int(num_scales) - 328
for i in 0..<n { - 329●
let retinex = retinex_sum[i] * inv_scales + offset - 330●
write_f32_r(out, (i * ch + c) * 4, retinex) - 331
} - 332
} - 333
@types.ImageF::{ - 334
width: w, - 335
height: h, - 336
channels: ch, - 337●
data: Bytes::from_array(out), - 338
} - 339
} - 340
- 341
// ========== MSRCR 16-bit ========== - 342
- 343
///| - 344
/// 带颜色恢复的多尺度 Retinex(MSRCR)16-bit 版本。 - 345
/// @see msrcr 8-bit 版本,本函数为 Image16 变体 - 346
pub fn msrcr_16( - 347
img : @types.Image16, - 348
sigmas~ : Array[Float] = [15.0F, 80.0F, 250.0F], - 349
gain~ : Float = 1.0F, - 350
offset~ : Float = 0.0F, - 351
alpha~ : Float = 125.0F, - 352
beta~ : Float = 46.0F, - 353
) -> @types.Image16 raise @types.LoadError { - 354
// 输入验证 - 355●
if img.width <= 0 || img.height <= 0 { - 356●
raise @types.LoadError::DecodeFailed("msrcr_16: 图像尺寸必须大于0") - 357
} - 358
if img.channels != 3 && img.channels != 4 { - 359●
raise @types.LoadError::DecodeFailed("msrcr_16 需要 RGB/RGBA 图像") - 360
} - 361
let expected_size = img.width * img.height * img.channels * 2 - 362●
if img.data.length() != expected_size { - 363●
raise @types.LoadError::DecodeFailed( - 364
"msrcr_16: 数据大小不匹配,期望 " + - 365●
expected_size.to_string() + - 366
",实际 " + - 367●
img.data.length().to_string(), - 368
) - 369
} - 370●
if sigmas.length() == 0 { - 371●
raise @types.LoadError::DecodeFailed("sigmas 不能为空") - 372
} - 373
let ch = img.channels - 374
let w = img.width - 375
let h = img.height - 376
let n = w * h - 377
if n == 0 { - 378○
return img - 379
} - 380●
let out : Array[Byte] = Array::make(n * ch * 2, b'\x00') - 381●
let num_scales = sigmas.length() - 382
let channels : Array[Array[Float]] = [ - 383●
Array::make(n, 0.0F), - 384●
Array::make(n, 0.0F), - 385●
Array::make(n, 0.0F), - 386
] - 387
for c in 0..<3 { - 388●
for i in 0..<n { - 389●
let v = read_u16_r(img.data, (i * ch + c) * 2) - 390◐
channels[c][i] = Float::from_int(if v == 0 { 1 } else { v }) - 391
} - 392
} - 393
let msr_results : Array[Array[Float]] = [ - 394●
Array::make(n, 0.0F), - 395●
Array::make(n, 0.0F), - 396●
Array::make(n, 0.0F), - 397
] - 398
for c in 0..<3 { - 399●
let log_channel : Array[Float] = Array::make(n, 0.0F) - 400
for i in 0..<n { - 401●
log_channel[i] = logf_r(channels[c][i]) - 402
} - 403●
let retinex_sum : Array[Float] = Array::make(n, 0.0F) - 404
for sigma in sigmas { - 405●
let blurred = gaussian_blur_channel(channels[c], w, h, sigma) - 406
for i in 0..<n { - 407◐
let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F }) - 408
retinex_sum[i] += log_channel[i] - log_b - 409
} - 410
} - 411●
let inv_scales = 1.0F / Float::from_int(num_scales) - 412
for i in 0..<n { - 413●
msr_results[c][i] = retinex_sum[i] * inv_scales - 414
} - 415
} - 416
for i in 0..<n { - 417●
let sum = channels[0][i] + channels[1][i] + channels[2][i] - 418
for c in 0..<3 { - 419●
let crf = logf_r(alpha * channels[c][i] / (sum + 1.0F)) * beta - 420
let val = msr_results[c][i] * gain + offset + crf - 421●
let mapped = (val * 30.0F + 32768.0F).to_int() - 422
let clamped = if mapped < 0 { - 423○
0 - 424●
} else if mapped > 65535 { - 425○
65535 - 426
} else { - 427●
mapped - 428
} - 429●
write_u16_r(out, (i * ch + c) * 2, clamped) - 430
} - 431
if ch == 4 { - 432●
let v = read_u16_r(img.data, (i * ch + 3) * 2) - 433●
write_u16_r(out, (i * ch + 3) * 2, v) - 434
} - 435
} - 436
@types.Image16::{ - 437
width: w, - 438
height: h, - 439
channels: ch, - 440●
data: Bytes::from_array(out), - 441
} - 442
} - 443
- 444
// ========== MSRCR float ========== - 445
- 446
///| - 447
/// 带颜色恢复的多尺度 Retinex(MSRCR)float 版本。 - 448
/// @see msrcr 8-bit 版本,本函数为 ImageF 变体 - 449
pub fn msrcr_f( - 450
img : @types.ImageF, - 451
sigmas~ : Array[Float] = [15.0F, 80.0F, 250.0F], - 452
gain~ : Float = 1.0F, - 453
offset~ : Float = 0.0F, - 454
alpha~ : Float = 125.0F, - 455
beta~ : Float = 46.0F, - 456
) -> @types.ImageF raise @types.LoadError { - 457●
if img.channels != 3 && img.channels != 4 { - 458●
raise @types.LoadError::DecodeFailed("msrcr_f 需要 RGB/RGBA 图像") - 459
} - 460●
if sigmas.length() == 0 { - 461●
raise @types.LoadError::DecodeFailed("sigmas 不能为空") - 462
} - 463
let ch = img.channels - 464
let w = img.width - 465
let h = img.height - 466
let n = w * h - 467
if n == 0 { - 468●
return img - 469
} - 470●
let out : Array[Byte] = Array::make(n * ch * 4, b'\x00') - 471●
let num_scales = sigmas.length() - 472
let channels : Array[Array[Float]] = [ - 473●
Array::make(n, 0.0F), - 474●
Array::make(n, 0.0F), - 475●
Array::make(n, 0.0F), - 476
] - 477
for c in 0..<3 { - 478●
for i in 0..<n { - 479●
let v = read_f32_r(img.data, (i * ch + c) * 4) - 480●
channels[c][i] = if v <= 0.0F { 0.0001F } else { v } - 481
} - 482
} - 483
let msr_results : Array[Array[Float]] = [ - 484●
Array::make(n, 0.0F), - 485●
Array::make(n, 0.0F), - 486●
Array::make(n, 0.0F), - 487
] - 488
for c in 0..<3 { - 489●
let log_channel : Array[Float] = Array::make(n, 0.0F) - 490
for i in 0..<n { - 491●
log_channel[i] = logf_r(channels[c][i]) - 492
} - 493●
let retinex_sum : Array[Float] = Array::make(n, 0.0F) - 494
for sigma in sigmas { - 495●
let blurred = gaussian_blur_channel(channels[c], w, h, sigma) - 496
for i in 0..<n { - 497◐
let log_b = logf_r(if blurred[i] > 0.0F { blurred[i] } else { 1.0F }) - 498
retinex_sum[i] += log_channel[i] - log_b - 499
} - 500
} - 501●
let inv_scales = 1.0F / Float::from_int(num_scales) - 502
for i in 0..<n { - 503●
msr_results[c][i] = retinex_sum[i] * inv_scales - 504
} - 505
} - 506
for i in 0..<n { - 507●
let sum = channels[0][i] + channels[1][i] + channels[2][i] - 508
for c in 0..<3 { - 509●
let crf = logf_r(alpha * channels[c][i] / (sum + 1.0F)) * beta - 510
let val = msr_results[c][i] * gain + offset + crf - 511●
write_f32_r(out, (i * ch + c) * 4, val) - 512
} - 513
if ch == 4 { - 514●
let v = read_f32_r(img.data, (i * ch + 3) * 4) - 515●
write_f32_r(out, (i * ch + 3) * 4, v) - 516
} - 517
} - 518
@types.ImageF::{ - 519
width: w, - 520
height: h, - 521
channels: ch, - 522●
data: Bytes::from_array(out), - 523
} - 524
}