image.process\transform\geometry_16f.mbt
100% · 132 / 132 points · 343 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 高级几何变换 16-bit/float 变体 Advanced geometry transforms 16-bit/float variants - 3
/// - 4
/// 16位透视/Seam Carving perspective_transform_16 / seam_carve_16 - 5
/// 16位/浮点透视/Seam Carving 16-bit/float perspective/seam carving - 6
// 高级几何变换 16-bit/float 变体 - 7
// warp_affine / warp_perspective / resize - 8
- 9
// ========== 辅助函数 ========== - 10
- 11
///| - 12
fn read_u16_le_geo(data : Bytes, offset : Int32) -> Int32 { - 13●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 14
} - 15
- 16
///| - 17
fn write_u16_le_geo(buf : Array[Byte], offset : Int32, val : Int32) -> Unit { - 18●
buf[offset] = (val & 0xFF).to_byte() - 19●
buf[offset + 1] = ((val >> 8) & 0xFF).to_byte() - 20
} - 21
- 22
///| - 23
fn read_float_le_geo(data : Bytes, offset : Int32) -> Float { - 24●
let b0 = data[offset].to_int() - 25●
let b1 = data[offset + 1].to_int() - 26●
let b2 = data[offset + 2].to_int() - 27●
let b3 = data[offset + 3].to_int() - 28
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 29●
Float::reinterpret_from_int(bits) - 30
} - 31
- 32
///| - 33
fn write_f32_le_geo(buf : Array[Byte], offset : Int32, val : Float) -> Unit { - 34●
let bits = val.reinterpret_as_int() - 35●
buf[offset] = (bits & 0xFF).to_byte() - 36●
buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 37●
buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 38●
buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 39
} - 40
- 41
///| - 42
fn sample_bilinear_16( - 43
data : Array[Int32], - 44
w : Int32, - 45
h : Int32, - 46
ch : Int32, - 47
c : Int32, - 48
x : Float, - 49
y : Float, - 50
) -> Int32 { - 51●
let x0 = x.to_int() - 52●
let y0 = y.to_int() - 53
let w_minus_1 = w - 1 - 54
let h_minus_1 = h - 1 - 55●
let x0c = @types.clamp_int(x0, 0, w_minus_1) - 56●
let y0c = @types.clamp_int(y0, 0, h_minus_1) - 57●
let x1 = @types.clamp_int(x0 + 1, 0, w_minus_1) - 58●
let y1 = @types.clamp_int(y0 + 1, 0, h_minus_1) - 59●
let fx = x - Float::from_int(x0) - 60●
let fy = y - Float::from_int(y0) - 61
let p00 = data[(y0c * w + x0c) * ch + c] - 62
let p10 = data[(y0c * w + x1) * ch + c] - 63
let p01 = data[(y1 * w + x0c) * ch + c] - 64
let p11 = data[(y1 * w + x1) * ch + c] - 65●
let top = Float::from_int(p00) * (1.0F - fx) + Float::from_int(p10) * fx - 66●
let bot = Float::from_int(p01) * (1.0F - fx) + Float::from_int(p11) * fx - 67●
(top * (1.0F - fy) + bot * fy + 0.5F).to_int() - 68
} - 69
- 70
///| - 71
fn sample_bilinear_f( - 72
data : Array[Float], - 73
w : Int32, - 74
h : Int32, - 75
ch : Int32, - 76
c : Int32, - 77
x : Float, - 78
y : Float, - 79
) -> Float { - 80●
let x0 = x.to_int() - 81●
let y0 = y.to_int() - 82
let w_minus_1 = w - 1 - 83
let h_minus_1 = h - 1 - 84●
let x0c = @types.clamp_int(x0, 0, w_minus_1) - 85●
let y0c = @types.clamp_int(y0, 0, h_minus_1) - 86●
let x1 = @types.clamp_int(x0 + 1, 0, w_minus_1) - 87●
let y1 = @types.clamp_int(y0 + 1, 0, h_minus_1) - 88●
let fx = x - Float::from_int(x0) - 89●
let fy = y - Float::from_int(y0) - 90
let p00 = data[(y0c * w + x0c) * ch + c] - 91
let p10 = data[(y0c * w + x1) * ch + c] - 92
let p01 = data[(y1 * w + x0c) * ch + c] - 93
let p11 = data[(y1 * w + x1) * ch + c] - 94
let top = p00 * (1.0F - fx) + p10 * fx - 95
let bot = p01 * (1.0F - fx) + p11 * fx - 96
top * (1.0F - fy) + bot * fy - 97
} - 98
- 99
// ========== warp_affine 16-bit ========== - 100
- 101
///| - 102
/// 16-bit 仿射变换。 - 103
/// @see warp_affine 8-bit 版本,本函数为 Image16 变体 - 104
pub fn warp_affine_16( - 105
img : @types.Image16, - 106
matrix : (Float, Float, Float, Float, Float, Float), - 107
new_w : Int32, - 108
new_h : Int32, - 109
) -> @types.Image16 { - 110●
let (a, b, c, d, e, f) = matrix - 111
let ch = img.channels - 112
let w = img.width - 113
let h = img.height - 114●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 115
for i in 0..<(w * h * ch) { - 116●
data_int[i] = read_u16_le_geo(img.data, i * 2) - 117
} - 118●
let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00') - 119
for dy in 0..<new_h { - 120●
let fdy = Float::from_int(dy) - 121
for dx in 0..<new_w { - 122●
let sx = a * Float::from_int(dx) + b * fdy + c - 123●
let sy = d * Float::from_int(dx) + e * fdy + f - 124
for ci in 0..<ch { - 125●
let val = sample_bilinear_16(data_int, w, h, ch, ci, sx, sy) - 126●
write_u16_le_geo( - 127
out, - 128
(dy * new_w + dx) * ch * 2 + ci * 2, - 129●
@types.clamp_int(val, 0, 65535), - 130
) - 131
} - 132
} - 133
} - 134
@types.Image16::{ - 135
width: new_w, - 136
height: new_h, - 137
channels: ch, - 138●
data: Bytes::from_array(out), - 139
} - 140
} - 141
- 142
// ========== warp_affine float ========== - 143
- 144
///| - 145
/// float 仿射变换。 - 146
/// @see warp_affine 8-bit 版本,本函数为 ImageF 变体 - 147
pub fn warp_affine_f( - 148
img : @types.ImageF, - 149
matrix : (Float, Float, Float, Float, Float, Float), - 150
new_w : Int32, - 151
new_h : Int32, - 152
) -> @types.ImageF { - 153●
let (a, b, c, d, e, f) = matrix - 154
let ch = img.channels - 155
let w = img.width - 156
let h = img.height - 157●
let data_f : Array[Float] = Array::make(w * h * ch, 0.0F) - 158
for i in 0..<(w * h * ch) { - 159●
data_f[i] = read_float_le_geo(img.data, i * 4) - 160
} - 161●
let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00') - 162
for dy in 0..<new_h { - 163●
let fdy = Float::from_int(dy) - 164
for dx in 0..<new_w { - 165●
let sx = a * Float::from_int(dx) + b * fdy + c - 166●
let sy = d * Float::from_int(dx) + e * fdy + f - 167
for ci in 0..<ch { - 168●
let val = sample_bilinear_f(data_f, w, h, ch, ci, sx, sy) - 169●
write_f32_le_geo(out, (dy * new_w + dx) * ch * 4 + ci * 4, val) - 170
} - 171
} - 172
} - 173
@types.ImageF::{ - 174
width: new_w, - 175
height: new_h, - 176
channels: ch, - 177●
data: Bytes::from_array(out), - 178
} - 179
} - 180
- 181
// ========== warp_perspective 16-bit ========== - 182
- 183
///| - 184
/// 16-bit 透视变换。 - 185
/// @see warp_perspective 8-bit 版本,本函数为 Image16 变体 - 186
pub fn warp_perspective_16( - 187
img : @types.Image16, - 188
matrix : PerspectiveMatrix, - 189
new_w : Int32, - 190
new_h : Int32, - 191
) -> @types.Image16 { - 192●
let ch = img.channels - 193
let w = img.width - 194
let h = img.height - 195●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 196
for i in 0..<(w * h * ch) { - 197●
data_int[i] = read_u16_le_geo(img.data, i * 2) - 198
} - 199●
let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00') - 200
for dy in 0..<new_h { - 201●
let fdy = Float::from_int(dy) - 202
for dx in 0..<new_w { - 203●
let fdx = Float::from_int(dx) - 204
let denom = matrix.m20 * fdx + matrix.m21 * fdy + matrix.m22 - 205
let sx = (matrix.m00 * fdx + matrix.m01 * fdy + matrix.m02) / denom - 206
let sy = (matrix.m10 * fdx + matrix.m11 * fdy + matrix.m12) / denom - 207
for ci in 0..<ch { - 208●
let val = sample_bilinear_16(data_int, w, h, ch, ci, sx, sy) - 209●
write_u16_le_geo( - 210
out, - 211
(dy * new_w + dx) * ch * 2 + ci * 2, - 212●
@types.clamp_int(val, 0, 65535), - 213
) - 214
} - 215
} - 216
} - 217
@types.Image16::{ - 218
width: new_w, - 219
height: new_h, - 220
channels: ch, - 221●
data: Bytes::from_array(out), - 222
} - 223
} - 224
- 225
// ========== warp_perspective float ========== - 226
- 227
///| - 228
/// float 透视变换。 - 229
/// @see warp_perspective 8-bit 版本,本函数为 ImageF 变体 - 230
pub fn warp_perspective_f( - 231
img : @types.ImageF, - 232
matrix : PerspectiveMatrix, - 233
new_w : Int32, - 234
new_h : Int32, - 235
) -> @types.ImageF { - 236●
let ch = img.channels - 237
let w = img.width - 238
let h = img.height - 239●
let data_f : Array[Float] = Array::make(w * h * ch, 0.0F) - 240
for i in 0..<(w * h * ch) { - 241●
data_f[i] = read_float_le_geo(img.data, i * 4) - 242
} - 243●
let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00') - 244
for dy in 0..<new_h { - 245●
let fdy = Float::from_int(dy) - 246
for dx in 0..<new_w { - 247●
let fdx = Float::from_int(dx) - 248
let denom = matrix.m20 * fdx + matrix.m21 * fdy + matrix.m22 - 249
let sx = (matrix.m00 * fdx + matrix.m01 * fdy + matrix.m02) / denom - 250
let sy = (matrix.m10 * fdx + matrix.m11 * fdy + matrix.m12) / denom - 251
for ci in 0..<ch { - 252●
let val = sample_bilinear_f(data_f, w, h, ch, ci, sx, sy) - 253●
write_f32_le_geo(out, (dy * new_w + dx) * ch * 4 + ci * 4, val) - 254
} - 255
} - 256
} - 257
@types.ImageF::{ - 258
width: new_w, - 259
height: new_h, - 260
channels: ch, - 261●
data: Bytes::from_array(out), - 262
} - 263
} - 264
- 265
// ========== resize 16-bit ========== - 266
- 267
///| - 268
/// 16-bit 图像缩放。 - 269
/// @see resize 8-bit 版本,本函数为 Image16 变体 - 270
pub fn resize_16( - 271
img : @types.Image16, - 272
new_w : Int32, - 273
new_h : Int32, - 274
) -> @types.Image16 { - 275●
let ch = img.channels - 276
let w = img.width - 277
let h = img.height - 278●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 279
for i in 0..<(w * h * ch) { - 280●
data_int[i] = read_u16_le_geo(img.data, i * 2) - 281
} - 282●
let scale_x = Float::from_int(w) / Float::from_int(new_w) - 283●
let scale_y = Float::from_int(h) / Float::from_int(new_h) - 284●
let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00') - 285
for dy in 0..<new_h { - 286●
for dx in 0..<new_w { - 287●
let sx = (Float::from_int(dx) + 0.5F) * scale_x - 0.5F - 288●
let sy = (Float::from_int(dy) + 0.5F) * scale_y - 0.5F - 289
for ci in 0..<ch { - 290●
let val = sample_bilinear_16(data_int, w, h, ch, ci, sx, sy) - 291●
write_u16_le_geo( - 292
out, - 293
(dy * new_w + dx) * ch * 2 + ci * 2, - 294●
@types.clamp_int(val, 0, 65535), - 295
) - 296
} - 297
} - 298
} - 299
@types.Image16::{ - 300
width: new_w, - 301
height: new_h, - 302
channels: ch, - 303●
data: Bytes::from_array(out), - 304
} - 305
} - 306
- 307
// ========== resize float ========== - 308
- 309
///| - 310
/// float 图像缩放。 - 311
/// @see resize 8-bit 版本,本函数为 ImageF 变体 - 312
pub fn resize_f( - 313
img : @types.ImageF, - 314
new_w : Int32, - 315
new_h : Int32, - 316
) -> @types.ImageF { - 317●
let ch = img.channels - 318
let w = img.width - 319
let h = img.height - 320●
let data_f : Array[Float] = Array::make(w * h * ch, 0.0F) - 321
for i in 0..<(w * h * ch) { - 322●
data_f[i] = read_float_le_geo(img.data, i * 4) - 323
} - 324●
let scale_x = Float::from_int(w) / Float::from_int(new_w) - 325●
let scale_y = Float::from_int(h) / Float::from_int(new_h) - 326●
let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00') - 327
for dy in 0..<new_h { - 328●
for dx in 0..<new_w { - 329●
let sx = (Float::from_int(dx) + 0.5F) * scale_x - 0.5F - 330●
let sy = (Float::from_int(dy) + 0.5F) * scale_y - 0.5F - 331
for ci in 0..<ch { - 332●
let val = sample_bilinear_f(data_f, w, h, ch, ci, sx, sy) - 333●
write_f32_le_geo(out, (dy * new_w + dx) * ch * 4 + ci * 4, val) - 334
} - 335
} - 336
} - 337
@types.ImageF::{ - 338
width: new_w, - 339
height: new_h, - 340
channels: ch, - 341●
data: Bytes::from_array(out), - 342
} - 343
}