image.process\frequency\frequency_16f.mbt
98.8% · 159 / 161 points · 450 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 变体 Frequency domain transforms 16-bit/float variants - 3
/// - 4
/// 16位 FFT/DCT/Haar 小波 fft_2d_16 / dct_2d_16 / haar_wavelet_16 - 5
/// 16位/浮点 FFT/DCT/Haar 小波 16-bit/float FFT/DCT/Haar wavelet - 6
// 频域变换 16-bit/float 变体 - 7
// FFT / DCT / Haar 支持 Image16 / ImageF - 8
- 9
// ========== 辅助函数 ========== - 10
- 11
///| - 12
fn read_u16_le_freq(data : Bytes, offset : Int32) -> Int32 { - 13●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 14
} - 15
- 16
///| - 17
fn write_u16_le_freq(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_freq(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_freq(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
// ========== FFT 16-bit ========== - 42
- 43
///| - 44
/// 16-bit 2D FFT。输入归一化到 0-1 后变换。 - 45
/// @see fft_2d 8-bit 版本,本函数为 Image16 变体 - 46
pub fn fft_2d_16( - 47
img : @types.Image16, - 48
) -> Array[FFTResult] raise @types.LoadError { - 49●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 50●
raise @types.LoadError::DecodeFailed("fft_2d_16 仅支持 channels=1/3/4") - 51
} - 52
let ch = img.channels - 53◐
let color_ch = if ch >= 3 { 3 } else { 1 } - 54
let w = img.width - 55
let h = img.height - 56
let bpp = ch * 2 - 57
// 转换为 8-bit 灰度图(归一化) - 58●
let data8 : Array[Byte] = Array::make(w * h * color_ch, b'\x00') - 59
for i in 0..<(w * h) { - 60●
for c in 0..<color_ch { - 61●
let v16 = read_u16_le_freq(img.data, i * bpp + c * 2) - 62●
data8[i * color_ch + c] = (v16 / 257).to_byte() - 63
} - 64
} - 65
let img8 = @types.Image::{ - 66
width: w, - 67
height: h, - 68
channels: color_ch, - 69●
data: Bytes::from_array(data8), - 70
} - 71●
fft_2d(img8) - 72
} - 73
- 74
///| - 75
/// 16-bit 逆 FFT。输出 Image16(0-65535)。 - 76
/// @see ifft_2d 8-bit 版本,本函数为 Image16 变体 - 77
pub fn ifft_2d_16(fft : FFTResult) -> @types.Image16 { - 78●
let img8 = ifft_2d(fft) - 79
let w = img8.width - 80
let h = img8.height - 81
let ch = img8.channels - 82●
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 83
for i in 0..<(w * h * ch) { - 84●
let v8 = img8.data[i].to_int() - 85●
write_u16_le_freq(out, i * 2, v8 * 257) - 86
} - 87
@types.Image16::{ - 88
width: w, - 89
height: h, - 90
channels: ch, - 91●
data: Bytes::from_array(out), - 92
} - 93
} - 94
- 95
///| - 96
/// 16-bit FFT 幅度谱。输出 Image16。 - 97
/// @see fft_magnitude 8-bit 版本,本函数为 Image16 变体 - 98
pub fn fft_magnitude_16(fft : FFTResult, log_scale : Bool) -> @types.Image16 { - 99●
let img8 = fft_magnitude(fft, log_scale) - 100
let w = img8.width - 101
let h = img8.height - 102
let ch = img8.channels - 103●
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 104
for i in 0..<(w * h * ch) { - 105●
let v8 = img8.data[i].to_int() - 106●
write_u16_le_freq(out, i * 2, v8 * 257) - 107
} - 108
@types.Image16::{ - 109
width: w, - 110
height: h, - 111
channels: ch, - 112●
data: Bytes::from_array(out), - 113
} - 114
} - 115
- 116
// ========== FFT float ========== - 117
- 118
///| - 119
/// float 2D FFT。输入直接使用 float 值。 - 120
/// @see fft_2d 8-bit 版本,本函数为 ImageF 变体 - 121
pub fn fft_2d_f(img : @types.ImageF) -> Array[FFTResult] raise @types.LoadError { - 122●
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 123●
raise @types.LoadError::DecodeFailed("fft_2d_f 仅支持 channels=1/3/4") - 124
} - 125
let ch = img.channels - 126◐
let color_ch = if ch >= 3 { 3 } else { 1 } - 127
let w = img.width - 128
let h = img.height - 129
let bpp = ch * 4 - 130
// 转换为 8-bit(归一化到 0-255) - 131●
let data8 : Array[Byte] = Array::make(w * h * color_ch, b'\x00') - 132
for i in 0..<(w * h) { - 133●
for c in 0..<color_ch { - 134●
let vf = read_float_le_freq(img.data, i * bpp + c * 4) - 135●
let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255) - 136●
data8[i * color_ch + c] = v8.to_byte() - 137
} - 138
} - 139
let img8 = @types.Image::{ - 140
width: w, - 141
height: h, - 142
channels: color_ch, - 143●
data: Bytes::from_array(data8), - 144
} - 145●
fft_2d(img8) - 146
} - 147
- 148
///| - 149
/// float 逆 FFT。输出 ImageF(0-1)。 - 150
/// @see ifft_2d 8-bit 版本,本函数为 ImageF 变体 - 151
pub fn ifft_2d_f(fft : FFTResult) -> @types.ImageF { - 152●
let img8 = ifft_2d(fft) - 153
let w = img8.width - 154
let h = img8.height - 155
let ch = img8.channels - 156●
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 157
for i in 0..<(w * h * ch) { - 158●
let v8 = img8.data[i].to_int() - 159●
write_f32_le_freq(out, i * 4, Float::from_int(v8) / 255.0F) - 160
} - 161
@types.ImageF::{ - 162
width: w, - 163
height: h, - 164
channels: ch, - 165●
data: Bytes::from_array(out), - 166
} - 167
} - 168
- 169
///| - 170
/// float FFT 幅度谱。输出 ImageF。 - 171
/// @see fft_magnitude 8-bit 版本,本函数为 ImageF 变体 - 172
pub fn fft_magnitude_f(fft : FFTResult, log_scale : Bool) -> @types.ImageF { - 173●
let img8 = fft_magnitude(fft, log_scale) - 174
let w = img8.width - 175
let h = img8.height - 176
let ch = img8.channels - 177●
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 178
for i in 0..<(w * h * ch) { - 179●
let v8 = img8.data[i].to_int() - 180●
write_f32_le_freq(out, i * 4, Float::from_int(v8) / 255.0F) - 181
} - 182
@types.ImageF::{ - 183
width: w, - 184
height: h, - 185
channels: ch, - 186●
data: Bytes::from_array(out), - 187
} - 188
} - 189
- 190
// ========== DCT 16-bit ========== - 191
- 192
///| - 193
/// 16-bit 2D DCT。输入归一化后变换。 - 194
/// @see dct_2d 8-bit 版本,本函数为 Image16 变体 - 195
pub fn dct_2d_16(img : @types.Image16) -> Array[Array[Double]] { - 196●
let w = img.width - 197
let h = img.height - 198
let ch = img.channels - 199
// 转换为 8-bit - 200●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 201
for i in 0..<(w * h * ch) { - 202●
let v16 = read_u16_le_freq(img.data, i * 2) - 203●
data8[i] = (v16 / 257).to_byte() - 204
} - 205
let img8 = @types.Image::{ - 206
width: w, - 207
height: h, - 208
channels: ch, - 209●
data: Bytes::from_array(data8), - 210
} - 211●
dct_2d(img8) - 212
} - 213
- 214
///| - 215
/// 16-bit 逆 DCT。输出 Image16。 - 216
/// @see idct_2d 8-bit 版本,本函数为 Image16 变体 - 217
pub fn idct_2d_16(coeffs : Array[Array[Double]]) -> @types.Image16 { - 218●
let img8 = idct_2d(coeffs) - 219
let w = img8.width - 220
let h = img8.height - 221
let ch = img8.channels - 222●
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 223
for i in 0..<(w * h * ch) { - 224●
let v8 = img8.data[i].to_int() - 225●
write_u16_le_freq(out, i * 2, v8 * 257) - 226
} - 227
@types.Image16::{ - 228
width: w, - 229
height: h, - 230
channels: ch, - 231●
data: Bytes::from_array(out), - 232
} - 233
} - 234
- 235
// ========== DCT float ========== - 236
- 237
///| - 238
/// float 2D DCT。 - 239
/// @see dct_2d 8-bit 版本,本函数为 ImageF 变体 - 240
pub fn dct_2d_f(img : @types.ImageF) -> Array[Array[Double]] { - 241●
let w = img.width - 242
let h = img.height - 243
let ch = img.channels - 244●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 245
for i in 0..<(w * h * ch) { - 246●
let vf = read_float_le_freq(img.data, i * 4) - 247●
let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255) - 248●
data8[i] = v8.to_byte() - 249
} - 250
let img8 = @types.Image::{ - 251
width: w, - 252
height: h, - 253
channels: ch, - 254●
data: Bytes::from_array(data8), - 255
} - 256●
dct_2d(img8) - 257
} - 258
- 259
///| - 260
/// float 逆 DCT。输出 ImageF。 - 261
/// @see idct_2d 8-bit 版本,本函数为 ImageF 变体 - 262
pub fn idct_2d_f(coeffs : Array[Array[Double]]) -> @types.ImageF { - 263●
let img8 = idct_2d(coeffs) - 264
let w = img8.width - 265
let h = img8.height - 266
let ch = img8.channels - 267●
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 268
for i in 0..<(w * h * ch) { - 269●
let v8 = img8.data[i].to_int() - 270●
write_f32_le_freq(out, i * 4, Float::from_int(v8) / 255.0F) - 271
} - 272
@types.ImageF::{ - 273
width: w, - 274
height: h, - 275
channels: ch, - 276●
data: Bytes::from_array(out), - 277
} - 278
} - 279
- 280
// ========== Haar 16-bit ========== - 281
- 282
///| - 283
/// 16-bit 2D Haar 小波变换。 - 284
/// @see haar_transform_2d 8-bit 版本,本函数为 Image16 变体 - 285
pub fn haar_transform_2d_16( - 286
img : @types.Image16, - 287
levels~ : Int32 = 1, - 288
) -> HaarWaveletResult raise @types.LoadError { - 289●
let w = img.width - 290
let h = img.height - 291
let ch = img.channels - 292●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 293
for i in 0..<(w * h * ch) { - 294●
let v16 = read_u16_le_freq(img.data, i * 2) - 295●
data8[i] = (v16 / 257).to_byte() - 296
} - 297
let img8 = @types.Image::{ - 298
width: w, - 299
height: h, - 300
channels: ch, - 301●
data: Bytes::from_array(data8), - 302
} - 303●
haar_transform_2d(img8, levels~) - 304
} - 305
- 306
///| - 307
/// 16-bit 逆 Haar 小波变换。输出 Image16。 - 308
/// @see haar_inverse_transform_2d 8-bit 版本,本函数为 Image16 变体 - 309
pub fn haar_inverse_transform_2d_16( - 310
result : HaarWaveletResult, - 311
) -> @types.Image16 { - 312●
let img8 = haar_inverse_transform_2d(result) - 313
let w = img8.width - 314
let h = img8.height - 315
let ch = img8.channels - 316●
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 317
for i in 0..<(w * h * ch) { - 318●
let v8 = img8.data[i].to_int() - 319●
write_u16_le_freq(out, i * 2, v8 * 257) - 320
} - 321
@types.Image16::{ - 322
width: w, - 323
height: h, - 324
channels: ch, - 325●
data: Bytes::from_array(out), - 326
} - 327
} - 328
- 329
///| - 330
/// 16-bit Haar 去噪。 - 331
/// @see haar_denoise 8-bit 版本,本函数为 Image16 变体 - 332
pub fn haar_denoise_16( - 333
img : @types.Image16, - 334
threshold~ : Float = 10.0F, - 335
soft~ : Bool = true, - 336
levels~ : Int32 = 3, - 337
) -> @types.Image16 raise @types.LoadError { - 338●
let w = img.width - 339
let h = img.height - 340
let ch = img.channels - 341●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 342
for i in 0..<(w * h * ch) { - 343●
let v16 = read_u16_le_freq(img.data, i * 2) - 344●
data8[i] = (v16 / 257).to_byte() - 345
} - 346
let img8 = @types.Image::{ - 347
width: w, - 348
height: h, - 349
channels: ch, - 350●
data: Bytes::from_array(data8), - 351
} - 352●
let out8 = haar_denoise(img8, threshold~, soft~, levels~) - 353●
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 354
for i in 0..<(w * h * ch) { - 355●
let v8 = out8.data[i].to_int() - 356●
write_u16_le_freq(out, i * 2, v8 * 257) - 357
} - 358
@types.Image16::{ - 359
width: w, - 360
height: h, - 361
channels: ch, - 362●
data: Bytes::from_array(out), - 363
} - 364
} - 365
- 366
// ========== Haar float ========== - 367
- 368
///| - 369
/// float 2D Haar 小波变换。 - 370
/// @see haar_transform_2d 8-bit 版本,本函数为 ImageF 变体 - 371
pub fn haar_transform_2d_f( - 372
img : @types.ImageF, - 373
levels~ : Int32 = 1, - 374
) -> HaarWaveletResult raise @types.LoadError { - 375●
let w = img.width - 376
let h = img.height - 377
let ch = img.channels - 378●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 379
for i in 0..<(w * h * ch) { - 380●
let vf = read_float_le_freq(img.data, i * 4) - 381●
let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255) - 382●
data8[i] = v8.to_byte() - 383
} - 384
let img8 = @types.Image::{ - 385
width: w, - 386
height: h, - 387
channels: ch, - 388●
data: Bytes::from_array(data8), - 389
} - 390●
haar_transform_2d(img8, levels~) - 391
} - 392
- 393
///| - 394
/// float 逆 Haar 小波变换。输出 ImageF。 - 395
/// @see haar_inverse_transform_2d 8-bit 版本,本函数为 ImageF 变体 - 396
pub fn haar_inverse_transform_2d_f(result : HaarWaveletResult) -> @types.ImageF { - 397●
let img8 = haar_inverse_transform_2d(result) - 398
let w = img8.width - 399
let h = img8.height - 400
let ch = img8.channels - 401●
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 402
for i in 0..<(w * h * ch) { - 403●
let v8 = img8.data[i].to_int() - 404●
write_f32_le_freq(out, i * 4, Float::from_int(v8) / 255.0F) - 405
} - 406
@types.ImageF::{ - 407
width: w, - 408
height: h, - 409
channels: ch, - 410●
data: Bytes::from_array(out), - 411
} - 412
} - 413
- 414
///| - 415
/// float Haar 去噪。 - 416
/// @see haar_denoise 8-bit 版本,本函数为 ImageF 变体 - 417
pub fn haar_denoise_f( - 418
img : @types.ImageF, - 419
threshold~ : Float = 10.0F, - 420
soft~ : Bool = true, - 421
levels~ : Int32 = 3, - 422
) -> @types.ImageF raise @types.LoadError { - 423●
let w = img.width - 424
let h = img.height - 425
let ch = img.channels - 426●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 427
for i in 0..<(w * h * ch) { - 428●
let vf = read_float_le_freq(img.data, i * 4) - 429●
let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255) - 430●
data8[i] = v8.to_byte() - 431
} - 432
let img8 = @types.Image::{ - 433
width: w, - 434
height: h, - 435
channels: ch, - 436●
data: Bytes::from_array(data8), - 437
} - 438●
let out8 = haar_denoise(img8, threshold~, soft~, levels~) - 439●
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 440
for i in 0..<(w * h * ch) { - 441●
let v8 = out8.data[i].to_int() - 442●
write_f32_le_freq(out, i * 4, Float::from_int(v8) / 255.0F) - 443
} - 444
@types.ImageF::{ - 445
width: w, - 446
height: h, - 447
channels: ch, - 448●
data: Bytes::from_array(out), - 449
} - 450
}