image.process\transform\resize_cover.mbt

95% · 38 / 40 points · 96 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/// # 缩放覆盖 resize cover
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 高级 Resize 操作(纯 MoonBit):resize_to_cover / resize_to_contain
  7. 7// 含裁剪和填充逻辑,属于高级算法层
  8. 8
  9. 9///|
  10. 10/// Resize 并裁剪到精确尺寸(cover 模式)。
  11. 11/// 先按宽高比缩放使图像"覆盖"目标尺寸,然后居中裁剪多余部分。
  12. 12pub fn resize_to_cover(
  13. 13 img : @types.Image,
  14. 14 target_w : Int32,
  15. 15 target_h : Int32,
  16. 16) -> @types.Image raise @types.LoadError {
  17. 17● if target_w <= 0 || target_h <= 0 {
  18. 18● raise @types.LoadError::DecodeFailed("目标尺寸必须为正数")
  19. 19 }
  20. 20● let scale_w = Float::from_int(target_w) / Float::from_int(img.width)
  21. 21● let scale_h = Float::from_int(target_h) / Float::from_int(img.height)
  22. 22◐ let scale = if scale_w > scale_h { scale_w } else { scale_h }
  23. 23● let scaled_w = (Float::from_int(img.width) * scale).to_int()
  24. 24● let scaled_h = (Float::from_int(img.height) * scale).to_int()
  25. 25● let scaled = @pure_util.resize_pure(img, scaled_w, scaled_h)
  26. 26 let x = (scaled_w - target_w) / 2
  27. 27 let y = (scaled_h - target_h) / 2
  28. 28● crop(scaled, x, y, target_w, target_h)
  29. 29}
  30. 30
  31. 31///|
  32. 32/// Resize 到目标尺寸内(contain 模式),保持宽高比,不足部分用 fill_color 填充。
  33. 33pub fn resize_to_contain(
  34. 34 img : @types.Image,
  35. 35 target_w : Int32,
  36. 36 target_h : Int32,
  37. 37 fill_color : Array[Byte],
  38. 38) -> @types.Image raise @types.LoadError {
  39. 39● if target_w <= 0 || target_h <= 0 {
  40. 40● raise @types.LoadError::DecodeFailed("目标尺寸必须为正数")
  41. 41 }
  42. 42● let scale_w = Float::from_int(target_w) / Float::from_int(img.width)
  43. 43● let scale_h = Float::from_int(target_h) / Float::from_int(img.height)
  44. 44● let scale = if scale_w < scale_h { scale_w } else { scale_h }
  45. 45● let scaled_w = (Float::from_int(img.width) * scale).to_int()
  46. 46● let scaled_h = (Float::from_int(img.height) * scale).to_int()
  47. 47● let scaled = @pure_util.resize_pure(img, scaled_w, scaled_h)
  48. 48 let px = (target_w - scaled_w) / 2
  49. 49 let py = (target_h - scaled_h) / 2
  50. 50● pad_image(scaled, px, py, fill_color)
  51. 51}
  52. 52
  53. 53///|
  54. 54/// 内部 pad 辅助函数:在图像四周填充指定颜色,生成新尺寸 (width+2*px, height+2*py) 的新图像。
  55. 55fn pad_image(
  56. 56 img : @types.Image,
  57. 57 px : Int32,
  58. 58 py : Int32,
  59. 59 pad_color : Array[Byte],
  60. 60) -> @types.Image raise @types.LoadError {
  61. 61● if px < 0 || py < 0 {
  62. 62○ raise @types.LoadError::DecodeFailed("pad 填充必须非负")
  63. 63 }
  64. 64 let ch = img.channels
  65. 65 let new_w = img.width + 2 * px
  66. 66 let new_h = img.height + 2 * py
  67. 67● let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
  68. 68● let color_len = pad_color.length()
  69. 69 if color_len == 0 {
  70. 70● raise @types.LoadError::DecodeFailed("pad_color 不能为空")
  71. 71 }
  72. 72 // 填充
  73. 73 for y in 0..<new_h {
  74. 74● for x in 0..<new_w {
  75. 75● let dst_off = (y * new_w + x) * ch
  76. 76 if x >= px && x < px + img.width && y >= py && y < py + img.height {
  77. 77 // 拷贝原始像素
  78. 78● let src_off = ((y - py) * img.width + (x - px)) * ch
  79. 79 for c in 0..<ch {
  80. 80● out[dst_off + c] = img.data[src_off + c]
  81. 81 }
  82. 82 } else {
  83. 83 // 填充颜色
  84. 84● for c in 0..<ch {
  85. 85● out[dst_off + c] = pad_color[c % color_len]
  86. 86 }
  87. 87 }
  88. 88 }
  89. 89 }
  90. 90 @types.Image::{
  91. 91 width: new_w,
  92. 92 height: new_h,
  93. 93 channels: ch,
  94. 94● data: Bytes::from_array(out),
  95. 95 }
  96. 96}