image.core\types\region.mbt
92.7% · 38 / 41 points · 180 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 区域视图 Region / ImageRegion - 3
/// - 4
/// 一张大图上的矩形兴趣区(ROI)。`ImageRegion` 只持有 `Image` 的引用与坐标, - 5
/// **不复制像素**:`base.data` 的底层缓冲区被共享(`Bytes` 为引用计数类型)。 - 6
/// 需要独立像素时才调用 `ImageRegion::to_image()` 物化一次。 - 7
/// - 8
/// 用途:超大图的局部读、局部算子、局部编码,避免为了一块区域再复制整幅图。 - 9
- 10
///| - 11
/// 矩形区域(左上角 + 宽高,单位像素)。坐标可为负或越界, - 12
/// 由 `Region::clamp` / `Image::region` 负责裁到图像边界内。 - 13
#derive(Eq, @debug.Debug) - 14
pub(all) struct Region { - 15
x : Int32 - 16
y : Int32 - 17
w : Int32 - 18
h : Int32 - 19
} - 20
- 21
///| - 22
/// 构造区域。不做边界检查(越界由使用方或 `clamp` 处理)。 - 23
pub fn Region::new(x : Int32, y : Int32, w : Int32, h : Int32) -> Region { - 24●
{ x, y, w, h, } - 25
} - 26
- 27
///| - 28
/// 区域像素数(宽 * 高)。非正尺寸时返回 0。 - 29
pub fn Region::area(self : Region) -> Int32 { - 30●
if self.w <= 0 || self.h <= 0 { - 31●
0 - 32
} else { - 33●
self.w * self.h - 34
} - 35
} - 36
- 37
///| - 38
/// 区域是否为空(宽或高非正)。 - 39
pub fn Region::is_empty(self : Region) -> Bool { - 40●
self.w <= 0 || self.h <= 0 - 41
} - 42
- 43
///| - 44
/// 把区域裁到 `width * height` 的画布内,返回可能缩小甚至为空的区域。 - 45
/// 负宽/负高视为空;部分越界时保留交集。 - 46
pub fn Region::clamp(self : Region, width : Int32, height : Int32) -> Region { - 47●
if self.w <= 0 || self.h <= 0 || width <= 0 || height <= 0 { - 48●
return { x: 0, y: 0, w: 0, h: 0, } - 49
} - 50●
let x0 = if self.x < 0 { 0 } else { self.x } - 51●
let y0 = if self.y < 0 { 0 } else { self.y } - 52●
let x1 = if self.x + self.w > width { width } else { self.x + self.w } - 53●
let y1 = if self.y + self.h > height { height } else { self.y + self.h } - 54
if x1 <= x0 || y1 <= y0 { - 55●
{ x: 0, y: 0, w: 0, h: 0, } - 56
} else { - 57●
{ x: x0, y: y0, w: x1 - x0, h: y1 - y0, } - 58
} - 59
} - 60
- 61
///| - 62
/// 图像上的区域视图。持有 `base` 的共享引用,不复制像素。 - 63
#derive(Eq, @debug.Debug) - 64
pub(all) struct ImageRegion { - 65
base : Image - 66
region : Region - 67
} - 68
- 69
///| - 70
/// 取图像的一块区域视图(越界部分自动裁掉,不报错)。 - 71
/// 区域为空时 `width()` / `height()` 为 0,`to_image()` 得到 0×0 图。 - 72
pub fn Image::region( - 73
self : Image, - 74
x : Int32, - 75
y : Int32, - 76
w : Int32, - 77
h : Int32, - 78
) -> ImageRegion { - 79●
let r = Region::new(x, y, w, h).clamp(self.width, self.height) - 80
{ base: self, region: r, } - 81
} - 82
- 83
///| - 84
/// 用现成的 `Region` 取区域视图(同样自动裁剪)。 - 85
pub fn Image::region_with(self : Image, region : Region) -> ImageRegion { - 86○
{ base: self, region: region.clamp(self.width, self.height), } - 87
} - 88
- 89
///| - 90
/// 区域视图的宽(可能因裁剪小于请求值)。 - 91
pub fn ImageRegion::width(self : ImageRegion) -> Int32 { - 92●
self.region.w - 93
} - 94
- 95
///| - 96
/// 区域视图的高(可能因裁剪小于请求值)。 - 97
pub fn ImageRegion::height(self : ImageRegion) -> Int32 { - 98●
self.region.h - 99
} - 100
- 101
///| - 102
/// 区域视图的通道数(与原图一致)。 - 103
pub fn ImageRegion::channels(self : ImageRegion) -> Int32 { - 104●
self.base.channels - 105
} - 106
- 107
///| - 108
/// 区域视图对应的 `Region`(已裁剪到图像内)。 - 109
pub fn ImageRegion::region_of(self : ImageRegion) -> Region { - 110○
self.region - 111
} - 112
- 113
///| - 114
/// 区域视图是否为空。 - 115
pub fn ImageRegion::is_empty(self : ImageRegion) -> Bool { - 116●
self.region.is_empty() - 117
} - 118
- 119
///| - 120
/// 读区域内的单通道字节;坐标相对区域左上角。越界返回 0(不 panic),便于算子边界处理。 - 121
pub fn ImageRegion::get( - 122
self : ImageRegion, - 123
x : Int32, - 124
y : Int32, - 125
channel : Int32, - 126
) -> Byte { - 127●
if x < 0 || - 128
y < 0 || - 129
x >= self.region.w || - 130
y >= self.region.h || - 131
channel < 0 || - 132
channel >= self.base.channels { - 133●
return b'\x00' - 134
} - 135
let idx = ((self.region.y + y) * self.base.width + self.region.x + x) * - 136
self.base.channels + - 137
channel - 138
self.base.data[idx] - 139
} - 140
- 141
///| - 142
/// 读区域内一个像素的全部通道,返回长度 `channels` 的数组。 - 143
pub fn ImageRegion::pixel( - 144
self : ImageRegion, - 145
x : Int32, - 146
y : Int32, - 147
) -> Array[Byte] { - 148●
let ch = self.base.channels - 149●
let out : Array[Byte] = Array::make(ch, b'\x00') - 150
for c in 0..<ch { - 151●
out[c] = self.get(x, y, c) - 152
} - 153
out - 154
} - 155
- 156
///| - 157
/// 把区域视图物化为独立 `Image`(这一步才真正分配像素)。 - 158
/// 与 `crop` 的差别:越界不报错,按裁剪后的有效区域输出。 - 159
pub fn ImageRegion::to_image(self : ImageRegion) -> Image { - 160●
let ch = self.base.channels - 161
let w = self.region.w - 162
let h = self.region.h - 163
if w <= 0 || h <= 0 { - 164●
return Image::{ - 165
width: 0, - 166
height: 0, - 167
channels: ch, - 168●
data: Bytes::make(0, b'\x00'), - 169
} - 170
} - 171●
let out : Array[Byte] = Array::make(w * h * ch, b'\x00') - 172
for row in 0..<h { - 173●
let src_off = ((self.region.y + row) * self.base.width + self.region.x) * ch - 174
let dst_off = row * w * ch - 175
for i in 0..<(w * ch) { - 176●
out[dst_off + i] = self.base.data[src_off + i] - 177
} - 178
} - 179●
Image::{ width: w, height: h, channels: ch, data: Bytes::from_array(out), } - 180
}