image.process\transform\pyramid_16f.mbt
96.6% · 172 / 178 points · 456 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 变体 Image pyramid 16-bit/float variants - 3
/// - 4
/// 16位高斯/拉普拉斯金字塔 gaussian_pyramid_16 / laplacian_pyramid_16 - 5
/// 16位/浮点高斯/拉普拉斯金字塔 16-bit/float Gaussian/Laplacian pyramid - 6
// 图像金字塔 16-bit/float 变体 - 7
// pyr_down / pyr_up - 8
- 9
///| - 10
fn read_u16_p(data : Bytes, offset : Int32) -> Int32 { - 11●
data[offset].to_int() | (data[offset + 1].to_int() << 8) - 12
} - 13
- 14
///| - 15
fn write_u16_p(data : Array[Byte], offset : Int32, v : Int32) -> Unit { - 16●
data[offset] = (v & 0xFF).to_byte() - 17●
data[offset + 1] = ((v >> 8) & 0xFF).to_byte() - 18
} - 19
- 20
///| - 21
fn read_f32_p(data : Bytes, offset : Int32) -> Float { - 22●
let b0 = data[offset].to_int() - 23●
let b1 = data[offset + 1].to_int() - 24●
let b2 = data[offset + 2].to_int() - 25●
let b3 = data[offset + 3].to_int() - 26
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 27●
Float::reinterpret_from_int(bits) - 28
} - 29
- 30
///| - 31
fn write_f32_p(data : Array[Byte], offset : Int32, v : Float) -> Unit { - 32●
let bits = v.reinterpret_as_int() - 33●
data[offset] = (bits & 0xFF).to_byte() - 34●
data[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 35●
data[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 36●
data[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 37
} - 38
- 39
// ========== pyr_down 16-bit ========== - 40
- 41
///| - 42
/// 高斯金字塔下采样 16-bit 版本。 - 43
/// @see pyr_down 8-bit 版本,本函数为 Image16 变体 - 44
pub fn pyr_down_16( - 45
img : @types.Image16, - 46
) -> @types.Image16 raise @types.LoadError { - 47●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 48●
raise @types.LoadError::DecodeFailed("pyr_down_16 仅支持 channels=1/3/4") - 49
} - 50
let ch = img.channels - 51
let w = img.width - 52
let h = img.height - 53
let new_w = w / 2 - 54
let new_h = h / 2 - 55
if new_w == 0 || new_h == 0 { - 56●
raise @types.LoadError::DecodeFailed("图像太小,无法下采样") - 57
} - 58●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 59
for i in 0..<(w * h * ch) { - 60●
data_int[i] = read_u16_p(img.data, i * 2) - 61
} - 62●
let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00') - 63
for y in 0..<new_h { - 64●
for x in 0..<new_w { - 65●
for c in 0..<ch { - 66●
let mut sum = 0 - 67
let mut count = 0 - 68
for dy in 0..<2 { - 69●
for dx in 0..<2 { - 70●
let sx = x * 2 + dx - 71
let sy = y * 2 + dy - 72
if sx < w && sy < h { - 73●
sum += data_int[(sy * w + sx) * ch + c] - 74
count += 1 - 75
} - 76
} - 77
} - 78●
write_u16_p(out, ((y * new_w + x) * ch + c) * 2, sum / count) - 79
} - 80
} - 81
} - 82
@types.Image16::{ - 83
width: new_w, - 84
height: new_h, - 85
channels: ch, - 86●
data: Bytes::from_array(out), - 87
} - 88
} - 89
- 90
// ========== pyr_down float ========== - 91
- 92
///| - 93
/// 高斯金字塔下采样 float 版本。 - 94
/// @see pyr_down 8-bit 版本,本函数为 ImageF 变体 - 95
pub fn pyr_down_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError { - 96●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 97●
raise @types.LoadError::DecodeFailed("pyr_down_f 仅支持 channels=1/3/4") - 98
} - 99
let ch = img.channels - 100
let w = img.width - 101
let h = img.height - 102
let new_w = w / 2 - 103
let new_h = h / 2 - 104
if new_w == 0 || new_h == 0 { - 105●
raise @types.LoadError::DecodeFailed("图像太小,无法下采样") - 106
} - 107●
let data_f : Array[Float] = Array::make(w * h * ch, 0.0F) - 108
for i in 0..<(w * h * ch) { - 109●
data_f[i] = read_f32_p(img.data, i * 4) - 110
} - 111●
let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00') - 112
for y in 0..<new_h { - 113●
for x in 0..<new_w { - 114●
for c in 0..<ch { - 115●
let mut sum = 0.0F - 116
let mut count = 0 - 117
for dy in 0..<2 { - 118●
for dx in 0..<2 { - 119●
let sx = x * 2 + dx - 120
let sy = y * 2 + dy - 121
if sx < w && sy < h { - 122●
sum += data_f[(sy * w + sx) * ch + c] - 123
count += 1 - 124
} - 125
} - 126
} - 127●
write_f32_p( - 128
out, - 129
((y * new_w + x) * ch + c) * 4, - 130●
sum / Float::from_int(count), - 131
) - 132
} - 133
} - 134
} - 135
@types.ImageF::{ - 136
width: new_w, - 137
height: new_h, - 138
channels: ch, - 139●
data: Bytes::from_array(out), - 140
} - 141
} - 142
- 143
// ========== pyr_up 16-bit ========== - 144
- 145
///| - 146
/// 拉普拉斯金字塔上采样 16-bit 版本。 - 147
/// @see pyr_up 8-bit 版本,本函数为 Image16 变体 - 148
pub fn pyr_up_16(img : @types.Image16) -> @types.Image16 raise @types.LoadError { - 149●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 150●
raise @types.LoadError::DecodeFailed("pyr_up_16 仅支持 channels=1/3/4") - 151
} - 152
let ch = img.channels - 153
let w = img.width - 154
let h = img.height - 155
let new_w = w * 2 - 156
let new_h = h * 2 - 157●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 158
for i in 0..<(w * h * ch) { - 159●
data_int[i] = read_u16_p(img.data, i * 2) - 160
} - 161●
let x0_arr : Array[Int32] = Array::make(new_w, 0) - 162●
let x1_arr : Array[Int32] = Array::make(new_w, 0) - 163●
let wx_arr : Array[Float] = Array::make(new_w, 0.0F) - 164
for x in 0..<new_w { - 165●
let fx = (Float::from_int(x) - 0.5F) / 2.0F - 166●
let x0 = @types.clamp_int(fx.to_int(), 0, w - 1) - 167
x0_arr[x] = x0 - 168●
x1_arr[x] = @types.clamp_int(x0 + 1, 0, w - 1) - 169●
wx_arr[x] = fx - Float::from_int(x0) - 170
} - 171●
let y0_arr : Array[Int32] = Array::make(new_h, 0) - 172●
let y1_arr : Array[Int32] = Array::make(new_h, 0) - 173●
let wy_arr : Array[Float] = Array::make(new_h, 0.0F) - 174
for y in 0..<new_h { - 175●
let fy = (Float::from_int(y) - 0.5F) / 2.0F - 176●
let y0 = @types.clamp_int(fy.to_int(), 0, h - 1) - 177
y0_arr[y] = y0 - 178●
y1_arr[y] = @types.clamp_int(y0 + 1, 0, h - 1) - 179●
wy_arr[y] = fy - Float::from_int(y0) - 180
} - 181●
let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00') - 182
for y in 0..<new_h { - 183●
let y0 = y0_arr[y] - 184
let y1 = y1_arr[y] - 185
let wy = wy_arr[y] - 186
let wy_inv = 1.0F - wy - 187
let row0 = y0 * w - 188
let row1 = y1 * w - 189
for x in 0..<new_w { - 190●
let x0 = x0_arr[x] - 191
let x1 = x1_arr[x] - 192
let wx = wx_arr[x] - 193
let wx_inv = 1.0F - wx - 194
for c in 0..<ch { - 195●
let v00 = Float::from_int(data_int[(row0 + x0) * ch + c]) - 196●
let v01 = Float::from_int(data_int[(row0 + x1) * ch + c]) - 197●
let v10 = Float::from_int(data_int[(row1 + x0) * ch + c]) - 198●
let v11 = Float::from_int(data_int[(row1 + x1) * ch + c]) - 199
let top = v00 * wx_inv + v01 * wx - 200
let bot = v10 * wx_inv + v11 * wx - 201
let v = top * wy_inv + bot * wy - 202
let clamped = if v < 0.0F { - 203●
0 - 204●
} else if v > 65535.0F { - 205○
65535 - 206
} else { - 207●
(v + 0.5F).to_int() - 208
} - 209●
write_u16_p(out, ((y * new_w + x) * ch + c) * 2, clamped) - 210
} - 211
} - 212
} - 213
@types.Image16::{ - 214
width: new_w, - 215
height: new_h, - 216
channels: ch, - 217●
data: Bytes::from_array(out), - 218
} - 219
} - 220
- 221
// ========== pyr_up float ========== - 222
- 223
///| - 224
/// 拉普拉斯金字塔上采样 float 版本。 - 225
/// @see pyr_up 8-bit 版本,本函数为 ImageF 变体 - 226
pub fn pyr_up_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError { - 227●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 228●
raise @types.LoadError::DecodeFailed("pyr_up_f 仅支持 channels=1/3/4") - 229
} - 230
let ch = img.channels - 231
let w = img.width - 232
let h = img.height - 233
let new_w = w * 2 - 234
let new_h = h * 2 - 235●
let data_f : Array[Float] = Array::make(w * h * ch, 0.0F) - 236
for i in 0..<(w * h * ch) { - 237●
data_f[i] = read_f32_p(img.data, i * 4) - 238
} - 239●
let x0_arr : Array[Int32] = Array::make(new_w, 0) - 240●
let x1_arr : Array[Int32] = Array::make(new_w, 0) - 241●
let wx_arr : Array[Float] = Array::make(new_w, 0.0F) - 242
for x in 0..<new_w { - 243●
let fx = (Float::from_int(x) - 0.5F) / 2.0F - 244●
let x0 = @types.clamp_int(fx.to_int(), 0, w - 1) - 245
x0_arr[x] = x0 - 246●
x1_arr[x] = @types.clamp_int(x0 + 1, 0, w - 1) - 247●
wx_arr[x] = fx - Float::from_int(x0) - 248
} - 249●
let y0_arr : Array[Int32] = Array::make(new_h, 0) - 250●
let y1_arr : Array[Int32] = Array::make(new_h, 0) - 251●
let wy_arr : Array[Float] = Array::make(new_h, 0.0F) - 252
for y in 0..<new_h { - 253●
let fy = (Float::from_int(y) - 0.5F) / 2.0F - 254●
let y0 = @types.clamp_int(fy.to_int(), 0, h - 1) - 255
y0_arr[y] = y0 - 256●
y1_arr[y] = @types.clamp_int(y0 + 1, 0, h - 1) - 257●
wy_arr[y] = fy - Float::from_int(y0) - 258
} - 259●
let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00') - 260
for y in 0..<new_h { - 261●
let y0 = y0_arr[y] - 262
let y1 = y1_arr[y] - 263
let wy = wy_arr[y] - 264
let wy_inv = 1.0F - wy - 265
let row0 = y0 * w - 266
let row1 = y1 * w - 267
for x in 0..<new_w { - 268●
let x0 = x0_arr[x] - 269
let x1 = x1_arr[x] - 270
let wx = wx_arr[x] - 271
let wx_inv = 1.0F - wx - 272
for c in 0..<ch { - 273●
let v00 = data_f[(row0 + x0) * ch + c] - 274
let v01 = data_f[(row0 + x1) * ch + c] - 275
let v10 = data_f[(row1 + x0) * ch + c] - 276
let v11 = data_f[(row1 + x1) * ch + c] - 277
let top = v00 * wx_inv + v01 * wx - 278
let bot = v10 * wx_inv + v11 * wx - 279
let v = top * wy_inv + bot * wy - 280●
write_f32_p(out, ((y * new_w + x) * ch + c) * 4, v) - 281
} - 282
} - 283
} - 284
@types.ImageF::{ - 285
width: new_w, - 286
height: new_h, - 287
channels: ch, - 288●
data: Bytes::from_array(out), - 289
} - 290
} - 291
- 292
// ========== laplacian_diff 16-bit ========== - 293
- 294
///| - 295
fn laplacian_diff_p16(a : @types.Image16, b : @types.Image16) -> @types.Image16 { - 296●
let ch = a.channels - 297
let w = a.width - 298
let h = a.height - 299●
let a_int : Array[Int32] = Array::make(w * h * ch, 0) - 300
for i in 0..<(w * h * ch) { - 301●
a_int[i] = read_u16_p(a.data, i * 2) - 302
} - 303
let bw = b.width - 304
let bh = b.height - 305●
let b_int : Array[Int32] = Array::make(bw * bh * ch, 0) - 306
for i in 0..<(bw * bh * ch) { - 307●
b_int[i] = read_u16_p(b.data, i * 2) - 308
} - 309●
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 310
for y in 0..<h { - 311●
let by = y * bh / h - 312
for x in 0..<w { - 313●
let bx = x * bw / w - 314
for c in 0..<ch { - 315●
let va = a_int[(y * w + x) * ch + c] - 316
let vb = b_int[(by * bw + bx) * ch + c] - 317
let diff = va - vb + 32768 - 318
let clamped = if diff < 0 { - 319●
0 - 320●
} else if diff > 65535 { - 321○
65535 - 322
} else { - 323●
diff - 324
} - 325●
write_u16_p(out, ((y * w + x) * ch + c) * 2, clamped) - 326
} - 327
} - 328
} - 329
@types.Image16::{ - 330
width: w, - 331
height: h, - 332
channels: ch, - 333●
data: Bytes::from_array(out), - 334
} - 335
} - 336
- 337
// ========== laplacian_diff float ========== - 338
- 339
///| - 340
fn laplacian_diff_pf(a : @types.ImageF, b : @types.ImageF) -> @types.ImageF { - 341●
let ch = a.channels - 342
let w = a.width - 343
let h = a.height - 344●
let a_f : Array[Float] = Array::make(w * h * ch, 0.0F) - 345
for i in 0..<(w * h * ch) { - 346●
a_f[i] = read_f32_p(a.data, i * 4) - 347
} - 348
let bw = b.width - 349
let bh = b.height - 350●
let b_f : Array[Float] = Array::make(bw * bh * ch, 0.0F) - 351
for i in 0..<(bw * bh * ch) { - 352●
b_f[i] = read_f32_p(b.data, i * 4) - 353
} - 354●
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 355
for y in 0..<h { - 356●
let by = y * bh / h - 357
for x in 0..<w { - 358●
let bx = x * bw / w - 359
for c in 0..<ch { - 360●
let va = a_f[(y * w + x) * ch + c] - 361
let vb = b_f[(by * bw + bx) * ch + c] - 362
let diff = va - vb + 0.5F - 363●
write_f32_p(out, ((y * w + x) * ch + c) * 4, diff) - 364
} - 365
} - 366
} - 367
@types.ImageF::{ - 368
width: w, - 369
height: h, - 370
channels: ch, - 371●
data: Bytes::from_array(out), - 372
} - 373
} - 374
- 375
// ========== build_gaussian_pyramid 16-bit ========== - 376
- 377
///| - 378
/// 构建高斯金字塔 16-bit 版本。 - 379
/// @see build_gaussian_pyramid 8-bit 版本,本函数为 Image16 变体 - 380
pub fn build_gaussian_pyramid_16( - 381
img : @types.Image16, - 382
levels : Int32, - 383
) -> Array[@types.Image16] raise @types.LoadError { - 384●
let result : Array[@types.Image16] = [img] - 385
let mut current = img - 386◐
let max_levels = if levels == 0 { 100 } else { levels } - 387
for _ in 1..<max_levels { - 388●
if current.width < 2 || current.height < 2 { - 389○
break - 390
} - 391●
current = pyr_down_16(current) - 392●
result.push(current) - 393
} - 394
result - 395
} - 396
- 397
// ========== build_gaussian_pyramid float ========== - 398
- 399
///| - 400
/// 构建高斯金字塔 float 版本。 - 401
/// @see build_gaussian_pyramid 8-bit 版本,本函数为 ImageF 变体 - 402
pub fn build_gaussian_pyramid_f( - 403
img : @types.ImageF, - 404
levels : Int32, - 405
) -> Array[@types.ImageF] raise @types.LoadError { - 406●
let result : Array[@types.ImageF] = [img] - 407
let mut current = img - 408◐
let max_levels = if levels == 0 { 100 } else { levels } - 409
for _ in 1..<max_levels { - 410●
if current.width < 2 || current.height < 2 { - 411○
break - 412
} - 413●
current = pyr_down_f(current) - 414●
result.push(current) - 415
} - 416
result - 417
} - 418
- 419
// ========== build_laplacian_pyramid 16-bit ========== - 420
- 421
///| - 422
/// 构建拉普拉斯金字塔 16-bit 版本。 - 423
/// @see build_laplacian_pyramid 8-bit 版本,本函数为 Image16 变体 - 424
pub fn build_laplacian_pyramid_16( - 425
img : @types.Image16, - 426
levels : Int32, - 427
) -> Array[@types.Image16] raise @types.LoadError { - 428●
let gaussian = build_gaussian_pyramid_16(img, levels) - 429
let result : Array[@types.Image16] = [] - 430●
for i in 0..<(gaussian.length() - 1) { - 431●
let upsampled = pyr_up_16(gaussian[i + 1]) - 432●
let lap = laplacian_diff_p16(gaussian[i], upsampled) - 433●
result.push(lap) - 434
} - 435●
result.push(gaussian[gaussian.length() - 1]) - 436
result - 437
} - 438
- 439
// ========== build_laplacian_pyramid float ========== - 440
- 441
///| - 442
/// 构建拉普拉斯金字塔 float 版本。 - 443
pub fn build_laplacian_pyramid_float( - 444
img : @types.ImageF, - 445
levels : Int32, - 446
) -> Array[@types.ImageF] raise @types.LoadError { - 447●
let gaussian = build_gaussian_pyramid_f(img, levels) - 448
let result : Array[@types.ImageF] = [] - 449●
for i in 0..<(gaussian.length() - 1) { - 450●
let upsampled = pyr_up_f(gaussian[i + 1]) - 451●
let lap = laplacian_diff_pf(gaussian[i], upsampled) - 452●
result.push(lap) - 453
} - 454●
result.push(gaussian[gaussian.length() - 1]) - 455
result - 456
}