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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// 图像金字塔 16-bit/float 变体
// pyr_down / pyr_up

///|
fn clamp_i_p(v : Int, lo : Int, hi : Int) -> Int {
  if v < lo {
    lo
  } else if v > hi {
    hi
  } else {
    v
  }
}

///|
fn read_u16_p(data : Bytes, offset : Int) -> Int {
  data[offset].to_int() | (data[offset + 1].to_int() << 8)
}

///|
fn write_u16_p(data : Array[Byte], offset : Int, v : Int) -> Unit {
  data[offset] = (v & 0xFF).to_byte()
  data[offset + 1] = ((v >> 8) & 0xFF).to_byte()
}

///|
fn read_f32_p(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_p(data : Array[Byte], offset : Int, v : Float) -> Unit {
  let bits = v.reinterpret_as_int()
  data[offset] = (bits & 0xFF).to_byte()
  data[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  data[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  data[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
}

// ========== pyr_down 16-bit ==========

///|
/// 高斯金字塔下采样 16-bit 版本。
pub fn pyr_down_16(
  img : @types.Image16,
) -> @types.Image16 raise @types.LoadError {
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed("pyr_down_16 仅支持 channels=1/3/4")
  }
  let ch = img.channels
  let w = img.width
  let h = img.height
  let new_w = w / 2
  let new_h = h / 2
  if new_w == 0 || new_h == 0 {
    raise @types.LoadError::DecodeFailed("图像太小,无法下采样")
  }
  let data_int : Array[Int] = Array::make(w * h * ch, 0)
  for i in 0..<(w * h * ch) {
    data_int[i] = read_u16_p(img.data, i * 2)
  }
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00')
  for y in 0..<new_h {
    for x in 0..<new_w {
      for c in 0..<ch {
        let mut sum = 0
        let mut count = 0
        for dy in 0..<2 {
          for dx in 0..<2 {
            let sx = x * 2 + dx
            let sy = y * 2 + dy
            if sx < w && sy < h {
              sum += data_int[(sy * w + sx) * ch + c]
              count += 1
            }
          }
        }
        write_u16_p(out, ((y * new_w + x) * ch + c) * 2, sum / count)
      }
    }
  }
  @types.Image16::{
    width: new_w,
    height: new_h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

// ========== pyr_down float ==========

///|
/// 高斯金字塔下采样 float 版本。
pub fn pyr_down_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError {
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed("pyr_down_f 仅支持 channels=1/3/4")
  }
  let ch = img.channels
  let w = img.width
  let h = img.height
  let new_w = w / 2
  let new_h = h / 2
  if new_w == 0 || new_h == 0 {
    raise @types.LoadError::DecodeFailed("图像太小,无法下采样")
  }
  let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  for i in 0..<(w * h * ch) {
    data_f[i] = read_f32_p(img.data, i * 4)
  }
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00')
  for y in 0..<new_h {
    for x in 0..<new_w {
      for c in 0..<ch {
        let mut sum = 0.0F
        let mut count = 0
        for dy in 0..<2 {
          for dx in 0..<2 {
            let sx = x * 2 + dx
            let sy = y * 2 + dy
            if sx < w && sy < h {
              sum += data_f[(sy * w + sx) * ch + c]
              count += 1
            }
          }
        }
        write_f32_p(
          out,
          ((y * new_w + x) * ch + c) * 4,
          sum / Float::from_int(count),
        )
      }
    }
  }
  @types.ImageF::{
    width: new_w,
    height: new_h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

// ========== pyr_up 16-bit ==========

///|
/// 拉普拉斯金字塔上采样 16-bit 版本。
pub fn pyr_up_16(img : @types.Image16) -> @types.Image16 raise @types.LoadError {
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed("pyr_up_16 仅支持 channels=1/3/4")
  }
  let ch = img.channels
  let w = img.width
  let h = img.height
  let new_w = w * 2
  let new_h = h * 2
  let data_int : Array[Int] = Array::make(w * h * ch, 0)
  for i in 0..<(w * h * ch) {
    data_int[i] = read_u16_p(img.data, i * 2)
  }
  let x0_arr : Array[Int] = Array::make(new_w, 0)
  let x1_arr : Array[Int] = Array::make(new_w, 0)
  let wx_arr : Array[Float] = Array::make(new_w, 0.0F)
  for x in 0..<new_w {
    let fx = (Float::from_int(x) - 0.5F) / 2.0F
    let x0 = clamp_i_p(fx.to_int(), 0, w - 1)
    x0_arr[x] = x0
    x1_arr[x] = clamp_i_p(x0 + 1, 0, w - 1)
    wx_arr[x] = fx - Float::from_int(x0)
  }
  let y0_arr : Array[Int] = Array::make(new_h, 0)
  let y1_arr : Array[Int] = Array::make(new_h, 0)
  let wy_arr : Array[Float] = Array::make(new_h, 0.0F)
  for y in 0..<new_h {
    let fy = (Float::from_int(y) - 0.5F) / 2.0F
    let y0 = clamp_i_p(fy.to_int(), 0, h - 1)
    y0_arr[y] = y0
    y1_arr[y] = clamp_i_p(y0 + 1, 0, h - 1)
    wy_arr[y] = fy - Float::from_int(y0)
  }
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00')
  for y in 0..<new_h {
    let y0 = y0_arr[y]
    let y1 = y1_arr[y]
    let wy = wy_arr[y]
    let wy_inv = 1.0F - wy
    let row0 = y0 * w
    let row1 = y1 * w
    for x in 0..<new_w {
      let x0 = x0_arr[x]
      let x1 = x1_arr[x]
      let wx = wx_arr[x]
      let wx_inv = 1.0F - wx
      for c in 0..<ch {
        let v00 = Float::from_int(data_int[(row0 + x0) * ch + c])
        let v01 = Float::from_int(data_int[(row0 + x1) * ch + c])
        let v10 = Float::from_int(data_int[(row1 + x0) * ch + c])
        let v11 = Float::from_int(data_int[(row1 + x1) * ch + c])
        let top = v00 * wx_inv + v01 * wx
        let bot = v10 * wx_inv + v11 * wx
        let v = top * wy_inv + bot * wy
        let clamped = if v < 0.0F {
          0
        } else if v > 65535.0F {
          65535
        } else {
          (v + 0.5F).to_int()
        }
        write_u16_p(out, ((y * new_w + x) * ch + c) * 2, clamped)
      }
    }
  }
  @types.Image16::{
    width: new_w,
    height: new_h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

// ========== pyr_up float ==========

///|
/// 拉普拉斯金字塔上采样 float 版本。
pub fn pyr_up_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError {
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed("pyr_up_f 仅支持 channels=1/3/4")
  }
  let ch = img.channels
  let w = img.width
  let h = img.height
  let new_w = w * 2
  let new_h = h * 2
  let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  for i in 0..<(w * h * ch) {
    data_f[i] = read_f32_p(img.data, i * 4)
  }
  let x0_arr : Array[Int] = Array::make(new_w, 0)
  let x1_arr : Array[Int] = Array::make(new_w, 0)
  let wx_arr : Array[Float] = Array::make(new_w, 0.0F)
  for x in 0..<new_w {
    let fx = (Float::from_int(x) - 0.5F) / 2.0F
    let x0 = clamp_i_p(fx.to_int(), 0, w - 1)
    x0_arr[x] = x0
    x1_arr[x] = clamp_i_p(x0 + 1, 0, w - 1)
    wx_arr[x] = fx - Float::from_int(x0)
  }
  let y0_arr : Array[Int] = Array::make(new_h, 0)
  let y1_arr : Array[Int] = Array::make(new_h, 0)
  let wy_arr : Array[Float] = Array::make(new_h, 0.0F)
  for y in 0..<new_h {
    let fy = (Float::from_int(y) - 0.5F) / 2.0F
    let y0 = clamp_i_p(fy.to_int(), 0, h - 1)
    y0_arr[y] = y0
    y1_arr[y] = clamp_i_p(y0 + 1, 0, h - 1)
    wy_arr[y] = fy - Float::from_int(y0)
  }
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00')
  for y in 0..<new_h {
    let y0 = y0_arr[y]
    let y1 = y1_arr[y]
    let wy = wy_arr[y]
    let wy_inv = 1.0F - wy
    let row0 = y0 * w
    let row1 = y1 * w
    for x in 0..<new_w {
      let x0 = x0_arr[x]
      let x1 = x1_arr[x]
      let wx = wx_arr[x]
      let wx_inv = 1.0F - wx
      for c in 0..<ch {
        let v00 = data_f[(row0 + x0) * ch + c]
        let v01 = data_f[(row0 + x1) * ch + c]
        let v10 = data_f[(row1 + x0) * ch + c]
        let v11 = data_f[(row1 + x1) * ch + c]
        let top = v00 * wx_inv + v01 * wx
        let bot = v10 * wx_inv + v11 * wx
        let v = top * wy_inv + bot * wy
        write_f32_p(out, ((y * new_w + x) * ch + c) * 4, v)
      }
    }
  }
  @types.ImageF::{
    width: new_w,
    height: new_h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

// ========== laplacian_diff 16-bit ==========

///|
fn laplacian_diff_p16(a : @types.Image16, b : @types.Image16) -> @types.Image16 {
  let ch = a.channels
  let w = a.width
  let h = a.height
  let a_int : Array[Int] = Array::make(w * h * ch, 0)
  for i in 0..<(w * h * ch) {
    a_int[i] = read_u16_p(a.data, i * 2)
  }
  let bw = b.width
  let bh = b.height
  let b_int : Array[Int] = Array::make(bw * bh * ch, 0)
  for i in 0..<(bw * bh * ch) {
    b_int[i] = read_u16_p(b.data, i * 2)
  }
  let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  for y in 0..<h {
    let by = y * bh / h
    for x in 0..<w {
      let bx = x * bw / w
      for c in 0..<ch {
        let va = a_int[(y * w + x) * ch + c]
        let vb = b_int[(by * bw + bx) * ch + c]
        let diff = va - vb + 32768
        let clamped = if diff < 0 {
          0
        } else if diff > 65535 {
          65535
        } else {
          diff
        }
        write_u16_p(out, ((y * w + x) * ch + c) * 2, clamped)
      }
    }
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

// ========== laplacian_diff float ==========

///|
fn laplacian_diff_pf(a : @types.ImageF, b : @types.ImageF) -> @types.ImageF {
  let ch = a.channels
  let w = a.width
  let h = a.height
  let a_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  for i in 0..<(w * h * ch) {
    a_f[i] = read_f32_p(a.data, i * 4)
  }
  let bw = b.width
  let bh = b.height
  let b_f : Array[Float] = Array::make(bw * bh * ch, 0.0F)
  for i in 0..<(bw * bh * ch) {
    b_f[i] = read_f32_p(b.data, i * 4)
  }
  let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  for y in 0..<h {
    let by = y * bh / h
    for x in 0..<w {
      let bx = x * bw / w
      for c in 0..<ch {
        let va = a_f[(y * w + x) * ch + c]
        let vb = b_f[(by * bw + bx) * ch + c]
        let diff = va - vb + 0.5F
        write_f32_p(out, ((y * w + x) * ch + c) * 4, diff)
      }
    }
  }
  @types.ImageF::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

// ========== build_gaussian_pyramid 16-bit ==========

///|
/// 构建高斯金字塔 16-bit 版本。
pub fn build_gaussian_pyramid_16(
  img : @types.Image16,
  levels : Int,
) -> Array[@types.Image16] raise @types.LoadError {
  let result : Array[@types.Image16] = [img]
  let mut current = img
  let max_levels = if levels == 0 { 100 } else { levels }
  for _ in 1..<max_levels {
    if current.width < 2 || current.height < 2 {
      break
    }
    current = pyr_down_16(current)
    result.push(current)
  }
  result
}

// ========== build_gaussian_pyramid float ==========

///|
/// 构建高斯金字塔 float 版本。
pub fn build_gaussian_pyramid_f(
  img : @types.ImageF,
  levels : Int,
) -> Array[@types.ImageF] raise @types.LoadError {
  let result : Array[@types.ImageF] = [img]
  let mut current = img
  let max_levels = if levels == 0 { 100 } else { levels }
  for _ in 1..<max_levels {
    if current.width < 2 || current.height < 2 {
      break
    }
    current = pyr_down_f(current)
    result.push(current)
  }
  result
}

// ========== build_laplacian_pyramid 16-bit ==========

///|
/// 构建拉普拉斯金字塔 16-bit 版本。
pub fn build_laplacian_pyramid_16(
  img : @types.Image16,
  levels : Int,
) -> Array[@types.Image16] raise @types.LoadError {
  let gaussian = build_gaussian_pyramid_16(img, levels)
  let result : Array[@types.Image16] = []
  for i in 0..<(gaussian.length() - 1) {
    let upsampled = pyr_up_16(gaussian[i + 1])
    let lap = laplacian_diff_p16(gaussian[i], upsampled)
    result.push(lap)
  }
  result.push(gaussian[gaussian.length() - 1])
  result
}

// ========== build_laplacian_pyramid float ==========

///|
/// 构建拉普拉斯金字塔 float 版本。
pub fn build_laplacian_pyramid_float(
  img : @types.ImageF,
  levels : Int,
) -> Array[@types.ImageF] raise @types.LoadError {
  let gaussian = build_gaussian_pyramid_f(img, levels)
  let result : Array[@types.ImageF] = []
  for i in 0..<(gaussian.length() - 1) {
    let upsampled = pyr_up_f(gaussian[i + 1])
    let lap = laplacian_diff_pf(gaussian[i], upsampled)
    result.push(lap)
  }
  result.push(gaussian[gaussian.length() - 1])
  result
}