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