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