image.process\transform\tile.mbt

81.6% · 40 / 49 points · 144 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/// # 分块 / 预算:大图直接出缩略图
  3. 3///
  4. 4/// 目标:**不为缩略图分配与底图同量级的内存**。
  5. 5/// `resize_subsample` 直接按面积平均从底图读数,中间只用一个长度为 `channels` 的累加器;
  6. 6/// 唯一分配是输出图(`out_w × out_h × channels`)。
  7. 7/// 20000×20000 灰度图出 512×512 缩略图,额外内存约 786 KB,而不是再开一张 400 MB 的中间图。
  8. 8
  9. 9///|
  10. 10/// 面积平均重采样(下采样首选,上采样退化为最近邻)。
  11. 11/// 每格把源图对应矩形内的像素求平均后写一个输出像素——**不建中间缓冲**。
  12. 12/// 失败条件:输入/输出尺寸非正、数据长度与 `width*height*channels` 不符。
  13. 13pub fn resize_subsample(
  14. 14 img : @types.Image,
  15. 15 out_w : Int32,
  16. 16 out_h : Int32,
  17. 17) -> @types.Image raise @types.LoadError {
  18. 18● let ch = img.channels
  19. 19 if out_w <= 0 || out_h <= 0 || img.width <= 0 || img.height <= 0 || ch <= 0 {
  20. 20● raise @types.LoadError::DecodeFailed("resize_subsample: 尺寸无效")
  21. 21 }
  22. 22● if img.data.length() < img.width * img.height * ch {
  23. 23○ raise @types.LoadError::DecodeFailed(
  24. 24 "resize_subsample: 像素数据长度不足",
  25. 25 )
  26. 26 }
  27. 27● let out : Array[Byte] = Array::make(out_w * out_h * ch, b'\x00')
  28. 28● let acc : Array[Double] = Array::make(ch, 0.0)
  29. 29 for oy in 0..<out_h {
  30. 30● let y0 = oy * img.height / out_h
  31. 31 let y1_raw = (oy + 1) * img.height / out_h
  32. 32● let y1 = if y1_raw <= y0 { y0 + 1 } else { y1_raw }
  33. 33 for ox in 0..<out_w {
  34. 34● let x0 = ox * img.width / out_w
  35. 35 let x1_raw = (ox + 1) * img.width / out_w
  36. 36● let x1 = if x1_raw <= x0 { x0 + 1 } else { x1_raw }
  37. 37 for c in 0..<ch {
  38. 38● acc[c] = 0.0
  39. 39 }
  40. 40 let mut count = 0
  41. 41 for sy in y0..<y1 {
  42. 42● let row_off = sy * img.width * ch
  43. 43 for sx in x0..<x1 {
  44. 44● let off = row_off + sx * ch
  45. 45 for c in 0..<ch {
  46. 46● acc[c] = acc[c] + img.data[off + c].to_double()
  47. 47 }
  48. 48 }
  49. 49 count = count + (x1 - x0)
  50. 50 }
  51. 51◐ let d = if count <= 0 { 1 } else { count }
  52. 52 let dst = (oy * out_w + ox) * ch
  53. 53 for c in 0..<ch {
  54. 54● let v = acc[c] / d.to_double()
  55. 55● out[dst + c] = v.round().to_int().to_byte()
  56. 56 }
  57. 57 }
  58. 58 }
  59. 59 @types.Image::{
  60. 60 width: out_w,
  61. 61 height: out_h,
  62. 62 channels: ch,
  63. 63● data: Bytes::from_array(out),
  64. 64 }
  65. 65}
  66. 66
  67. 67///|
  68. 68/// 等比缩略图:长边压到 `max_side`,短边按比例(四舍五入,至少 1 像素)。
  69. 69/// 原图两边都不超过 `max_side` 时原样返回(零分配)。
  70. 70pub fn thumbnail(
  71. 71 img : @types.Image,
  72. 72 max_side : Int32,
  73. 73) -> @types.Image raise @types.LoadError {
  74. 74● if max_side <= 0 {
  75. 75○ raise @types.LoadError::DecodeFailed("thumbnail: max_side 必须为正")
  76. 76 }
  77. 77 let w = img.width
  78. 78 let h = img.height
  79. 79 if w <= max_side && h <= max_side {
  80. 80● return img
  81. 81 }
  82. 82● let (ow, oh) = thumbnail_dims(w, h, max_side)
  83. 83● resize_subsample(img, ow, oh)
  84. 84}
  85. 85
  86. 86///|
  87. 87/// 等比缩略图的输出尺寸(不分配内存,供预算检查先算)。
  88. 88pub fn thumbnail_dims(
  89. 89 width : Int32,
  90. 90 height : Int32,
  91. 91 max_side : Int32,
  92. 92) -> (Int32, Int32) {
  93. 93● if width <= 0 || height <= 0 || max_side <= 0 {
  94. 94○ return (0, 0)
  95. 95 }
  96. 96 if width >= height {
  97. 97● let oh_raw = (height * max_side + width / 2) / width
  98. 98● let oh = if oh_raw < 1 { 1 } else { oh_raw }
  99. 99 (max_side, oh)
  100. 100 } else {
  101. 101● let ow_raw = (width * max_side + height / 2) / height
  102. 102◐ let ow = if ow_raw < 1 { 1 } else { ow_raw }
  103. 103 (ow, max_side)
  104. 104 }
  105. 105}
  106. 106
  107. 107///|
  108. 108/// 带分配预算的缩略图:先按尺寸算输出字节,超出 `budget` 直接拒(不分配)。
  109. 109/// 这是"大图出小图"的推荐入口:预算按**输出**计,底图内存由解码期 `DecodeLimits` 把关。
  110. 110pub fn thumbnail_budgeted(
  111. 111 img : @types.Image,
  112. 112 max_side : Int32,
  113. 113 budget : @types.AllocBudget,
  114. 114) -> @types.Image raise @types.ImageError {
  115. 115● if max_side <= 0 {
  116. 116○ raise @types.ImageError::Process(
  117. 117 @types.ErrorCode::InvalidArgument,
  118. 118 "thumbnail: max_side 必须为正",
  119. 119 )
  120. 120 }
  121. 121● let (ow, oh) = thumbnail_dims(img.width, img.height, max_side)
  122. 122 if ow <= 0 || oh <= 0 {
  123. 123○ raise @types.ImageError::Process(
  124. 124 @types.ErrorCode::InvalidArgument,
  125. 125 "thumbnail: 图像尺寸无效",
  126. 126 )
  127. 127 }
  128. 128● budget.check_image(
  129. 129● ow.to_int64(),
  130. 130● oh.to_int64(),
  131. 131● img.channels.to_int64(),
  132. 132 "thumbnail",
  133. 133 )
  134. 134● let out = resize_subsample(img, ow, oh) catch {
  135. 135 @types.LoadError::DecodeFailed(msg) =>
  136. 136○ raise @types.ImageError::Process(@types.ErrorCode::InvalidArgument, msg)
  137. 137 _ =>
  138. 138○ raise @types.ImageError::Process(
  139. 139 @types.ErrorCode::ProcessFailed,
  140. 140 "thumbnail: 重采样失败",
  141. 141 )
  142. 142 }
  143. 143 out
  144. 144}