image.vision\fieldimg\field_bind.mbt
100% · 68 / 68 points · 162 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 场绑定 fieldimg/field_bind - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 场与 Image 双向转换:Image ↔ ScalarField / VectorField。 - 7
// Field-Image bidirectional binding: Image ↔ ScalarField / VectorField. - 8
// - 9
// 映射关系: - 10
// 灰度 Image (channels=1) ↔ ScalarField - 11
// RGB Image (channels=3) ↔ VectorField (dim=3) - 12
// RGBA Image (channels=4) ↔ VectorField (dim=4) - 13
- 14
///| - 15
/// Image 转标量场:将灰度图(channels=1)转为标量场,像素值归一化到 [0, 1]。 - 16
/// 若 channels > 1,先转灰度(加权平均:0.299R + 0.587G + 0.114B)。 - 17
/// Image 转 ScalarField;灰度归一化到 [0, 1] Convert Image to ScalarField; grayscale (channels=1) normalized to [0, 1]. - 18
/// channels > 1 时 For channels > 1 - 19
pub fn image_to_scalar_field( - 20
img : @types.Image, - 21
) -> ScalarField raise @types.LoadError { - 22●
if img.width <= 0 || img.height <= 0 { - 23●
raise @types.LoadError::DecodeFailed( - 24
"image_to_scalar_field: 图像尺寸必须大于0", - 25
) - 26
} - 27
let w = img.width - 28
let h = img.height - 29
let ch = img.channels - 30
let n = w * h - 31●
let data = Array::make(n, 0.0F) - 32
for i in 0..<n { - 33●
let base = i * ch - 34
let v = if ch == 1 { - 35●
Float::from_int(img.data[base].to_int()) - 36●
} else if ch >= 3 { - 37●
let r = Float::from_int(img.data[base].to_int()) - 38●
let g = Float::from_int(img.data[base + 1].to_int()) - 39●
let b = Float::from_int(img.data[base + 2].to_int()) - 40
0.299F * r + 0.587F * g + 0.114F * b - 41
} else { - 42●
Float::from_int(img.data[base].to_int()) - 43
} - 44
data[i] = v * (1.0F / 255.0F) - 45
} - 46●
ScalarField::make(w, h, data) - 47
} - 48
- 49
///| - 50
/// 标量场转 Image:将标量场转为单通道灰度图,值钳制到 [0, 1] 后映射到 [0, 255]。 - 51
/// ScalarField 转单通道灰度 Image;值钳位到 [0,1] 再映射到 [0,255] Convert ScalarField to single-channel grayscale Image; values clamped to [0, 1] then mapped to [0, 255]. - 52
pub fn scalar_field_to_image(field : ScalarField) -> @types.Image { - 53●
let n = field.width * field.height - 54●
let buf : Array[Byte] = Array::make(n, b'0') - 55
for i in 0..<n { - 56●
let v = field.data[i] - 57●
let clamped = if v < 0.0F { 0.0F } else if v > 1.0F { 1.0F } else { v } - 58●
buf[i] = (clamped * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte() - 59
} - 60
{ - 61
width: field.width, - 62
height: field.height, - 63
channels: 1, - 64●
data: Bytes::from_array(buf), - 65
} - 66
} - 67
- 68
///| - 69
/// Image 转矢量场:将 RGB/RGBA 图转为矢量场(dim=channels),像素值归一化到 [0, 1]。 - 70
/// Image 转 VectorField;像素值归一化到 [0, 1] Convert Image to VectorField (dim=channels); pixel values normalized to [0, 1]. - 71
pub fn image_to_vector_field( - 72
img : @types.Image, - 73
) -> VectorField raise @types.LoadError { - 74●
if img.width <= 0 || img.height <= 0 || img.channels <= 0 { - 75●
raise @types.LoadError::DecodeFailed( - 76
"image_to_vector_field: 图像尺寸和通道数必须大于0", - 77
) - 78
} - 79
let w = img.width - 80
let h = img.height - 81
let ch = img.channels - 82
let n = w * h * ch - 83●
let data = Array::make(n, 0.0F) - 84
for i in 0..<n { - 85●
data[i] = Float::from_int(img.data[i].to_int()) * (1.0F / 255.0F) - 86
} - 87●
VectorField::make(w, h, ch, data) - 88
} - 89
- 90
///| - 91
/// 矢量场转 Image:将矢量场转为多通道图,值钳制到 [0, 1] 后映射到 [0, 255]。 - 92
/// VectorField 转多通道 Image;值钳位到 [0,1] 再映射到 [0,255] Convert VectorField to multi-channel Image; values clamped to [0, 1] then mapped to [0, 255]. - 93
pub fn vector_field_to_image(field : VectorField) -> @types.Image { - 94●
let n = field.width * field.height * field.dim - 95●
let buf : Array[Byte] = Array::make(n, b'0') - 96
for i in 0..<n { - 97●
let v = field.data[i] - 98●
let clamped = if v < 0.0F { 0.0F } else if v > 1.0F { 1.0F } else { v } - 99●
buf[i] = (clamped * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte() - 100
} - 101
{ - 102
width: field.width, - 103
height: field.height, - 104
channels: field.dim, - 105●
data: Bytes::from_array(buf), - 106
} - 107
} - 108
- 109
///| - 110
/// Image16 转标量场:16-bit 图转标量场,归一化到 [0, 1](除以 65535)。 - 111
/// Image16 转 ScalarField;归一化到 [0, 1](除以 65535) Convert Image16 to ScalarField; normalized to [0, 1] (divided by 65535). - 112
pub fn image16_to_scalar_field( - 113
img : @types.Image16, - 114
) -> ScalarField raise @types.LoadError { - 115●
if img.width <= 0 || img.height <= 0 { - 116●
raise @types.LoadError::DecodeFailed( - 117
"image16_to_scalar_field: 图像尺寸必须大于0", - 118
) - 119
} - 120
let w = img.width - 121
let h = img.height - 122
let ch = img.channels - 123
let n = w * h - 124●
let data = Array::make(n, 0.0F) - 125
let inv = 1.0F / 65535.0F - 126
for i in 0..<n { - 127●
let base = i * ch * 2 - 128●
let lo = img.data[base].to_int() - 129●
let hi = img.data[base + 1].to_int() - 130
let v = lo | (hi << 8) - 131●
data[i] = Float::from_int(v) * inv - 132
} - 133●
ScalarField::make(w, h, data) - 134
} - 135
- 136
///| - 137
/// ImageF 转标量场:HDR float 图转标量场,取第一通道,钳制到 [0, 1]。 - 138
/// ImageF 转 ScalarField;取首通道,钳位到 [0, 1] Convert ImageF to ScalarField; takes first channel, clamped to [0, 1]. - 139
pub fn imagef_to_scalar_field( - 140
img : @types.ImageF, - 141
) -> ScalarField raise @types.LoadError { - 142●
if img.width <= 0 || img.height <= 0 { - 143●
raise @types.LoadError::DecodeFailed( - 144
"imagef_to_scalar_field: 图像尺寸必须大于0", - 145
) - 146
} - 147
let w = img.width - 148
let h = img.height - 149
let ch = img.channels - 150
let n = w * h - 151●
let data = Array::make(n, 0.0F) - 152
for i in 0..<n { - 153●
let base = i * ch * 4 - 154●
let bits = img.data[base].to_int() | - 155●
(img.data[base + 1].to_int() << 8) | - 156●
(img.data[base + 2].to_int() << 16) | - 157●
(img.data[base + 3].to_int() << 24) - 158●
let v = Float::reinterpret_from_int(bits) - 159●
data[i] = if v < 0.0F { 0.0F } else if v > 1.0F { 1.0F } else { v } - 160
} - 161●
ScalarField::make(w, h, data) - 162
}