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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
// 形态学操作 16-bit/float 变体
// 支持 Image16 / ImageF 两种类型
// StructuringElement 复用 morphology.mbt 中的定义

// 复用 color_16f.mbt 中的 read_u16_le_m / write_u16_le_m / write_f32_le_m
// 复用 tonemap.mbt 中的 read_float_le_m

// ========== 基础操作:erode / dilate ==========

///|
/// 16-bit 腐蚀。每个像素取 3x3 邻域最小值。
pub fn erode_16(img : @types.Image16) -> @types.Image16 {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 2
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  let src : Array[Int] = Array::make(n * ch, 0)
  for i in 0..<(n * ch) {
    src[i] = read_u16_le_m(img.data, i * 2)
  }
  // 内部区域
  for y in 1..<(h - 1) {
    for x in 1..<(w - 1) {
      for c in 0..<ch {
        let base = ((y - 1) * w + (x - 1)) * ch + c
        let mut min_v = src[base]
        if src[base + ch] < min_v {
          min_v = src[base + ch]
        }
        if src[base + 2 * ch] < min_v {
          min_v = src[base + 2 * ch]
        }
        let row1 = base + w * ch
        if src[row1] < min_v {
          min_v = src[row1]
        }
        if src[row1 + ch] < min_v {
          min_v = src[row1 + ch]
        }
        if src[row1 + 2 * ch] < min_v {
          min_v = src[row1 + 2 * ch]
        }
        let row2 = base + 2 * w * ch
        if src[row2] < min_v {
          min_v = src[row2]
        }
        if src[row2 + ch] < min_v {
          min_v = src[row2 + ch]
        }
        if src[row2 + 2 * ch] < min_v {
          min_v = src[row2 + 2 * ch]
        }
        write_u16_le_m(out, (y * w + x) * bpp + c * 2, min_v)
      }
    }
  }
  // 边界区域
  for y in 0..<h {
    let y_inner = y >= 1 && y < h - 1
    for x in 0..<w {
      if y_inner && x >= 1 && x < w - 1 {
        continue
      }
      for c in 0..<ch {
        let mut min_v = 65535
        for dy in -1..<=1 {
          for dx in -1..<=1 {
            let sx = clamp_morph(x + dx, 0, w - 1)
            let sy = clamp_morph(y + dy, 0, h - 1)
            let v = src[(sy * w + sx) * ch + c]
            if v < min_v {
              min_v = v
            }
          }
        }
        write_u16_le_m(out, (y * w + x) * bpp + c * 2, min_v)
      }
    }
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// float 腐蚀。每个像素取 3x3 邻域最小值。
pub fn erode_f(img : @types.ImageF) -> @types.ImageF {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 4
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  let src : Array[Float] = Array::make(n * ch, 0.0F)
  for i in 0..<(n * ch) {
    src[i] = read_float_le_m(img.data, i * 4)
  }
  for y in 1..<(h - 1) {
    for x in 1..<(w - 1) {
      for c in 0..<ch {
        let base = ((y - 1) * w + (x - 1)) * ch + c
        let mut min_v = src[base]
        if src[base + ch] < min_v {
          min_v = src[base + ch]
        }
        if src[base + 2 * ch] < min_v {
          min_v = src[base + 2 * ch]
        }
        let row1 = base + w * ch
        if src[row1] < min_v {
          min_v = src[row1]
        }
        if src[row1 + ch] < min_v {
          min_v = src[row1 + ch]
        }
        if src[row1 + 2 * ch] < min_v {
          min_v = src[row1 + 2 * ch]
        }
        let row2 = base + 2 * w * ch
        if src[row2] < min_v {
          min_v = src[row2]
        }
        if src[row2 + ch] < min_v {
          min_v = src[row2 + ch]
        }
        if src[row2 + 2 * ch] < min_v {
          min_v = src[row2 + 2 * ch]
        }
        write_f32_le_m(out, (y * w + x) * bpp + c * 4, min_v)
      }
    }
  }
  for y in 0..<h {
    let y_inner = y >= 1 && y < h - 1
    for x in 0..<w {
      if y_inner && x >= 1 && x < w - 1 {
        continue
      }
      for c in 0..<ch {
        let mut min_v = 1.0F
        for dy in -1..<=1 {
          for dx in -1..<=1 {
            let sx = clamp_morph(x + dx, 0, w - 1)
            let sy = clamp_morph(y + dy, 0, h - 1)
            let v = src[(sy * w + sx) * ch + c]
            if v < min_v {
              min_v = v
            }
          }
        }
        write_f32_le_m(out, (y * w + x) * bpp + c * 4, min_v)
      }
    }
  }
  @types.ImageF::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// 16-bit 膨胀。每个像素取 3x3 邻域最大值。
pub fn dilate_16(img : @types.Image16) -> @types.Image16 {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 2
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  let src : Array[Int] = Array::make(n * ch, 0)
  for i in 0..<(n * ch) {
    src[i] = read_u16_le_m(img.data, i * 2)
  }
  for y in 1..<(h - 1) {
    for x in 1..<(w - 1) {
      for c in 0..<ch {
        let base = ((y - 1) * w + (x - 1)) * ch + c
        let mut max_v = src[base]
        if src[base + ch] > max_v {
          max_v = src[base + ch]
        }
        if src[base + 2 * ch] > max_v {
          max_v = src[base + 2 * ch]
        }
        let row1 = base + w * ch
        if src[row1] > max_v {
          max_v = src[row1]
        }
        if src[row1 + ch] > max_v {
          max_v = src[row1 + ch]
        }
        if src[row1 + 2 * ch] > max_v {
          max_v = src[row1 + 2 * ch]
        }
        let row2 = base + 2 * w * ch
        if src[row2] > max_v {
          max_v = src[row2]
        }
        if src[row2 + ch] > max_v {
          max_v = src[row2 + ch]
        }
        if src[row2 + 2 * ch] > max_v {
          max_v = src[row2 + 2 * ch]
        }
        write_u16_le_m(out, (y * w + x) * bpp + c * 2, max_v)
      }
    }
  }
  for y in 0..<h {
    let y_inner = y >= 1 && y < h - 1
    for x in 0..<w {
      if y_inner && x >= 1 && x < w - 1 {
        continue
      }
      for c in 0..<ch {
        let mut max_v = 0
        for dy in -1..<=1 {
          for dx in -1..<=1 {
            let sx = clamp_morph(x + dx, 0, w - 1)
            let sy = clamp_morph(y + dy, 0, h - 1)
            let v = src[(sy * w + sx) * ch + c]
            if v > max_v {
              max_v = v
            }
          }
        }
        write_u16_le_m(out, (y * w + x) * bpp + c * 2, max_v)
      }
    }
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// float 膨胀。每个像素取 3x3 邻域最大值。
pub fn dilate_f(img : @types.ImageF) -> @types.ImageF {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 4
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  let src : Array[Float] = Array::make(n * ch, 0.0F)
  for i in 0..<(n * ch) {
    src[i] = read_float_le_m(img.data, i * 4)
  }
  for y in 1..<(h - 1) {
    for x in 1..<(w - 1) {
      for c in 0..<ch {
        let base = ((y - 1) * w + (x - 1)) * ch + c
        let mut max_v = src[base]
        if src[base + ch] > max_v {
          max_v = src[base + ch]
        }
        if src[base + 2 * ch] > max_v {
          max_v = src[base + 2 * ch]
        }
        let row1 = base + w * ch
        if src[row1] > max_v {
          max_v = src[row1]
        }
        if src[row1 + ch] > max_v {
          max_v = src[row1 + ch]
        }
        if src[row1 + 2 * ch] > max_v {
          max_v = src[row1 + 2 * ch]
        }
        let row2 = base + 2 * w * ch
        if src[row2] > max_v {
          max_v = src[row2]
        }
        if src[row2 + ch] > max_v {
          max_v = src[row2 + ch]
        }
        if src[row2 + 2 * ch] > max_v {
          max_v = src[row2 + 2 * ch]
        }
        write_f32_le_m(out, (y * w + x) * bpp + c * 4, max_v)
      }
    }
  }
  for y in 0..<h {
    let y_inner = y >= 1 && y < h - 1
    for x in 0..<w {
      if y_inner && x >= 1 && x < w - 1 {
        continue
      }
      for c in 0..<ch {
        let mut max_v = 0.0F
        for dy in -1..<=1 {
          for dx in -1..<=1 {
            let sx = clamp_morph(x + dx, 0, w - 1)
            let sy = clamp_morph(y + dy, 0, h - 1)
            let v = src[(sy * w + sx) * ch + c]
            if v > max_v {
              max_v = v
            }
          }
        }
        write_f32_le_m(out, (y * w + x) * bpp + c * 4, max_v)
      }
    }
  }
  @types.ImageF::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

// ========== 组合操作 ==========

///|
/// 16-bit 开运算(先腐蚀后膨胀)。
pub fn morph_open_16(img : @types.Image16) -> @types.Image16 {
  dilate_16(erode_16(img))
}

///|
/// float 开运算(先腐蚀后膨胀)。
pub fn morph_open_f(img : @types.ImageF) -> @types.ImageF {
  dilate_f(erode_f(img))
}

///|
/// 16-bit 闭运算(先膨胀后腐蚀)。
pub fn morph_close_16(img : @types.Image16) -> @types.Image16 {
  erode_16(dilate_16(img))
}

///|
/// float 闭运算(先膨胀后腐蚀)。
pub fn morph_close_f(img : @types.ImageF) -> @types.ImageF {
  erode_f(dilate_f(img))
}

///|
/// 16-bit 形态学梯度(dilate - erode)。
pub fn morph_gradient_16(img : @types.Image16) -> @types.Image16 {
  let dilated = dilate_16(img)
  let eroded = erode_16(img)
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 2
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  for i in 0..<(n * ch) {
    let v = read_u16_le_m(dilated.data, i * 2) -
      read_u16_le_m(eroded.data, i * 2)
    write_u16_le_m(out, i * 2, if v < 0 { 0 } else { v })
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// float 形态学梯度(dilate - erode)。
pub fn morph_gradient_f(img : @types.ImageF) -> @types.ImageF {
  let dilated = dilate_f(img)
  let eroded = erode_f(img)
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 4
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  for i in 0..<(n * ch) {
    let v = read_float_le_m(dilated.data, i * 4) -
      read_float_le_m(eroded.data, i * 4)
    write_f32_le_m(out, i * 4, if v < 0.0F { 0.0F } else { v })
  }
  @types.ImageF::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// 16-bit 顶帽变换(original - open)。
pub fn morph_tophat_16(img : @types.Image16) -> @types.Image16 {
  let opened = morph_open_16(img)
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 2
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  for i in 0..<(n * ch) {
    let v = read_u16_le_m(img.data, i * 2) - read_u16_le_m(opened.data, i * 2)
    write_u16_le_m(out, i * 2, if v < 0 { 0 } else { v })
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// float 顶帽变换(original - open)。
pub fn morph_tophat_f(img : @types.ImageF) -> @types.ImageF {
  let opened = morph_open_f(img)
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 4
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  for i in 0..<(n * ch) {
    let v = read_float_le_m(img.data, i * 4) -
      read_float_le_m(opened.data, i * 4)
    write_f32_le_m(out, i * 4, if v < 0.0F { 0.0F } else { v })
  }
  @types.ImageF::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// 16-bit 黑帽变换(close - original)。
pub fn morph_blackhat_16(img : @types.Image16) -> @types.Image16 {
  let closed = morph_close_16(img)
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 2
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  for i in 0..<(n * ch) {
    let v = read_u16_le_m(closed.data, i * 2) - read_u16_le_m(img.data, i * 2)
    write_u16_le_m(out, i * 2, if v < 0 { 0 } else { v })
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// float 黑帽变换(close - original)。
pub fn morph_blackhat_f(img : @types.ImageF) -> @types.ImageF {
  let closed = morph_close_f(img)
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 4
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  for i in 0..<(n * ch) {
    let v = read_float_le_m(closed.data, i * 4) -
      read_float_le_m(img.data, i * 4)
    write_f32_le_m(out, i * 4, if v < 0.0F { 0.0F } else { v })
  }
  @types.ImageF::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

// ========== 自定义结构元素 ==========

///|
/// 16-bit 自定义结构元素腐蚀。
pub fn erode_custom_16(
  img : @types.Image16,
  se : StructuringElement,
) -> @types.Image16 {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 2
  let src : Array[Int] = Array::make(n * ch, 0)
  for i in 0..<(n * ch) {
    src[i] = read_u16_le_m(img.data, i * 2)
  }
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  let cx = se.width / 2
  let cy = se.height / 2
  for y in 0..<h {
    for x in 0..<w {
      for c in 0..<ch {
        let mut min_v = 65535
        for sy in 0..<se.height {
          for sx in 0..<se.width {
            if se.data[sy * se.width + sx] {
              let nx = clamp_morph(x + sx - cx, 0, w - 1)
              let ny = clamp_morph(y + sy - cy, 0, h - 1)
              let v = src[(ny * w + nx) * ch + c]
              if v < min_v {
                min_v = v
              }
            }
          }
        }
        write_u16_le_m(out, (y * w + x) * bpp + c * 2, min_v)
      }
    }
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// float 自定义结构元素腐蚀。
pub fn erode_custom_f(
  img : @types.ImageF,
  se : StructuringElement,
) -> @types.ImageF {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 4
  let src : Array[Float] = Array::make(n * ch, 0.0F)
  for i in 0..<(n * ch) {
    src[i] = read_float_le_m(img.data, i * 4)
  }
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  let cx = se.width / 2
  let cy = se.height / 2
  for y in 0..<h {
    for x in 0..<w {
      for c in 0..<ch {
        let mut min_v = 1.0F
        for sy in 0..<se.height {
          for sx in 0..<se.width {
            if se.data[sy * se.width + sx] {
              let nx = clamp_morph(x + sx - cx, 0, w - 1)
              let ny = clamp_morph(y + sy - cy, 0, h - 1)
              let v = src[(ny * w + nx) * ch + c]
              if v < min_v {
                min_v = v
              }
            }
          }
        }
        write_f32_le_m(out, (y * w + x) * bpp + c * 4, min_v)
      }
    }
  }
  @types.ImageF::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// 16-bit 自定义结构元素膨胀。
pub fn dilate_custom_16(
  img : @types.Image16,
  se : StructuringElement,
) -> @types.Image16 {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 2
  let src : Array[Int] = Array::make(n * ch, 0)
  for i in 0..<(n * ch) {
    src[i] = read_u16_le_m(img.data, i * 2)
  }
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  let cx = se.width / 2
  let cy = se.height / 2
  for y in 0..<h {
    for x in 0..<w {
      for c in 0..<ch {
        let mut max_v = 0
        for sy in 0..<se.height {
          for sx in 0..<se.width {
            if se.data[sy * se.width + sx] {
              let nx = clamp_morph(x + sx - cx, 0, w - 1)
              let ny = clamp_morph(y + sy - cy, 0, h - 1)
              let v = src[(ny * w + nx) * ch + c]
              if v > max_v {
                max_v = v
              }
            }
          }
        }
        write_u16_le_m(out, (y * w + x) * bpp + c * 2, max_v)
      }
    }
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// float 自定义结构元素膨胀。
pub fn dilate_custom_f(
  img : @types.ImageF,
  se : StructuringElement,
) -> @types.ImageF {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let n = w * h
  let bpp = ch * 4
  let src : Array[Float] = Array::make(n * ch, 0.0F)
  for i in 0..<(n * ch) {
    src[i] = read_float_le_m(img.data, i * 4)
  }
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  let cx = se.width / 2
  let cy = se.height / 2
  for y in 0..<h {
    for x in 0..<w {
      for c in 0..<ch {
        let mut max_v = 0.0F
        for sy in 0..<se.height {
          for sx in 0..<se.width {
            if se.data[sy * se.width + sx] {
              let nx = clamp_morph(x + sx - cx, 0, w - 1)
              let ny = clamp_morph(y + sy - cy, 0, h - 1)
              let v = src[(ny * w + nx) * ch + c]
              if v > max_v {
                max_v = v
              }
            }
          }
        }
        write_f32_le_m(out, (y * w + x) * bpp + c * 4, max_v)
      }
    }
  }
  @types.ImageF::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// 16-bit 自定义结构元素开运算。
pub fn morph_open_custom_16(
  img : @types.Image16,
  se : StructuringElement,
) -> @types.Image16 {
  dilate_custom_16(erode_custom_16(img, se), se)
}

///|
/// float 自定义结构元素开运算。
pub fn morph_open_custom_f(
  img : @types.ImageF,
  se : StructuringElement,
) -> @types.ImageF {
  dilate_custom_f(erode_custom_f(img, se), se)
}

///|
/// 16-bit 自定义结构元素闭运算。
pub fn morph_close_custom_16(
  img : @types.Image16,
  se : StructuringElement,
) -> @types.Image16 {
  erode_custom_16(dilate_custom_16(img, se), se)
}

///|
/// float 自定义结构元素闭运算。
pub fn morph_close_custom_f(
  img : @types.ImageF,
  se : StructuringElement,
) -> @types.ImageF {
  erode_custom_f(dilate_custom_f(img, se), se)
}

// ========== 辅助函数 ==========

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

///|
fn read_u16_le_m(data : Bytes, offset : Int) -> Int {
  data[offset].to_int() + data[offset + 1].to_int() * 256
}

///|
fn write_u16_le_m(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_m(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_m(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()
}