image.codec\util\resize.mbt

100% · 58 / 58 points · 115 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 缩放 util/resize
  3. 3///
  4. 4/// 编解码工具模块
  5. 5/// 编解码工具模块 Codec utility module
  6. 6// 纯 MoonBit 图像缩放(全目标可用:native/wasm/js)
  7. 7// 仅依赖 @types,不依赖 @core/FFI
  8. 8// 支持最近邻 + 双线性插值
  9. 9// v190.0.0 性能优化:预计算坐标权重 + 消除 data_int 拷贝
  10. 10
  11. 11///|
  12. 12/// resize 8-bit 图像(纯 MoonBit,全目标可用)。
  13. 13/// 使用双线性插值(filter = PointSample 时使用最近邻)。
  14. 14/// edge 参数目前统一按 Clamp 处理(边缘像素重复)。
  15. 15/// v190.0.0 优化:预计算坐标权重数组,消除 data_int 转换,
  16. 16/// 直接在 Bytes 上操作,减少 O(n) 内存拷贝和重复浮点运算。
  17. 17pub fn resize_pure(
  18. 18 img : @types.Image,
  19. 19 out_w : Int32,
  20. 20 out_h : Int32,
  21. 21 filter~ : @types.ResizeFilter = @types.ResizeFilter::Default,
  22. 22 edge~ : @types.ResizeEdge = @types.ResizeEdge::Clamp,
  23. 23) -> @types.Image raise @types.LoadError {
  24. 24● let _ = edge
  25. 25 if out_w <= 0 || out_h <= 0 {
  26. 26● raise @types.LoadError::DecodeFailed("resize 输出尺寸必须为正数")
  27. 27 }
  28. 28 let ch = img.channels
  29. 29 let w = img.width
  30. 30 let h = img.height
  31. 31● let scale_x = Float::from_int(w) / Float::from_int(out_w)
  32. 32● let scale_y = Float::from_int(h) / Float::from_int(out_h)
  33. 33● let use_nearest = match filter {
  34. 34● PointSample => true
  35. 35● _ => false
  36. 36 }
  37. 37 let w_minus_1 = w - 1
  38. 38 let h_minus_1 = h - 1
  39. 39● let out : Array[Byte] = Array::make(out_w * out_h * ch, b'\x00')
  40. 40 if use_nearest {
  41. 41● let sx_xi : Array[Int32] = Array::make(out_w, 0)
  42. 42 for dx in 0..<out_w {
  43. 43● let sx = (Float::from_int(dx) + 0.5F) * scale_x - 0.5F
  44. 44● sx_xi[dx] = (sx + 0.5F).to_int().clamp(min=0, max=w_minus_1)
  45. 45 }
  46. 46● let sy_yi : Array[Int32] = Array::make(out_h, 0)
  47. 47 for dy in 0..<out_h {
  48. 48● let sy = (Float::from_int(dy) + 0.5F) * scale_y - 0.5F
  49. 49● sy_yi[dy] = (sy + 0.5F).to_int().clamp(min=0, max=h_minus_1)
  50. 50 }
  51. 51 for dy in 0..<out_h {
  52. 52● let yi = sy_yi[dy]
  53. 53 for dx in 0..<out_w {
  54. 54● let xi = sx_xi[dx]
  55. 55 let src_idx = (yi * w + xi) * ch
  56. 56 let dst_idx = (dy * out_w + dx) * ch
  57. 57 for ci in 0..<ch {
  58. 58● out[dst_idx + ci] = img.data[src_idx + ci]
  59. 59 }
  60. 60 }
  61. 61 }
  62. 62 } else {
  63. 63● let sx_x0c : Array[Int32] = Array::make(out_w, 0)
  64. 64● let sx_x1 : Array[Int32] = Array::make(out_w, 0)
  65. 65● let sx_fx : Array[Float] = Array::make(out_w, 0.0F)
  66. 66 for dx in 0..<out_w {
  67. 67● let sx = (Float::from_int(dx) + 0.5F) * scale_x - 0.5F
  68. 68● let x0 = sx.to_int()
  69. 69● sx_x0c[dx] = x0.clamp(min=0, max=w_minus_1)
  70. 70● sx_x1[dx] = (x0 + 1).clamp(min=0, max=w_minus_1)
  71. 71● sx_fx[dx] = sx - Float::from_int(x0)
  72. 72 }
  73. 73● let sy_y0c : Array[Int32] = Array::make(out_h, 0)
  74. 74● let sy_y1 : Array[Int32] = Array::make(out_h, 0)
  75. 75● let sy_fy : Array[Float] = Array::make(out_h, 0.0F)
  76. 76 for dy in 0..<out_h {
  77. 77● let sy = (Float::from_int(dy) + 0.5F) * scale_y - 0.5F
  78. 78● let y0 = sy.to_int()
  79. 79● sy_y0c[dy] = y0.clamp(min=0, max=h_minus_1)
  80. 80● sy_y1[dy] = (y0 + 1).clamp(min=0, max=h_minus_1)
  81. 81● sy_fy[dy] = sy - Float::from_int(y0)
  82. 82 }
  83. 83 for dy in 0..<out_h {
  84. 84● let y0c = sy_y0c[dy]
  85. 85 let y1 = sy_y1[dy]
  86. 86 let fy = sy_fy[dy]
  87. 87 let one_minus_fy = 1.0F - fy
  88. 88 for dx in 0..<out_w {
  89. 89● let x0c = sx_x0c[dx]
  90. 90 let x1 = sx_x1[dx]
  91. 91 let fx = sx_fx[dx]
  92. 92 let one_minus_fx = 1.0F - fx
  93. 93 let dst_idx = (dy * out_w + dx) * ch
  94. 94 for ci in 0..<ch {
  95. 95● let p00 = img.data[(y0c * w + x0c) * ch + ci].to_int()
  96. 96● let p10 = img.data[(y0c * w + x1) * ch + ci].to_int()
  97. 97● let p01 = img.data[(y1 * w + x0c) * ch + ci].to_int()
  98. 98● let p11 = img.data[(y1 * w + x1) * ch + ci].to_int()
  99. 99● let top = Float::from_int(p00) * one_minus_fx +
  100. 100● Float::from_int(p10) * fx
  101. 101● let bot = Float::from_int(p01) * one_minus_fx +
  102. 102● Float::from_int(p11) * fx
  103. 103● let val = (top * one_minus_fy + bot * fy + 0.5F).to_int()
  104. 104● out[dst_idx + ci] = val.clamp(min=0, max=255).to_byte()
  105. 105 }
  106. 106 }
  107. 107 }
  108. 108 }
  109. 109 @types.Image::{
  110. 110 width: out_w,
  111. 111 height: out_h,
  112. 112 channels: ch,
  113. 113● data: Bytes::from_array(out),
  114. 114 }
  115. 115}