image.process\color\dehaze_16f.mbt
98.9% · 90 / 91 points · 262 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 变体 Dehaze 16-bit/float variants - 3
/// - 4
/// 16位/浮点去雾 dehaze_16 / dehaze_f - 5
/// 16位/浮点去雾 16-bit/float dehazing - 6
// 去雾 16-bit/float 变体(color 包) - 7
// dehaze / guided_filter 支持 Image16 / ImageF - 8
- 9
// ========== 辅助函数 ========== - 10
- 11
///| - 12
fn read_u16_le_dh(data : Bytes, offset : Int32) -> Int32 { - 13●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 14
} - 15
- 16
///| - 17
fn write_u16_le_dh(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_dh(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_dh(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 img16_to_img8_dh(img : @types.Image16) -> @types.Image { - 43●
let w = img.width - 44
let h = img.height - 45
let ch = img.channels - 46●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 47
for i in 0..<(w * h * ch) { - 48●
let v16 = read_u16_le_dh(img.data, i * 2) - 49●
data8[i] = (v16 / 257).to_byte() - 50
} - 51
@types.Image::{ - 52
width: w, - 53
height: h, - 54
channels: ch, - 55●
data: Bytes::from_array(data8), - 56
} - 57
} - 58
- 59
///| - 60
fn imgf_to_img8_dh(img : @types.ImageF) -> @types.Image { - 61●
let w = img.width - 62
let h = img.height - 63
let ch = img.channels - 64●
let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00') - 65
for i in 0..<(w * h * ch) { - 66●
let vf = read_float_le_dh(img.data, i * 4) - 67●
let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255) - 68●
data8[i] = v8.to_byte() - 69
} - 70
@types.Image::{ - 71
width: w, - 72
height: h, - 73
channels: ch, - 74●
data: Bytes::from_array(data8), - 75
} - 76
} - 77
- 78
///| - 79
fn img8_to_img16_dh(img : @types.Image) -> @types.Image16 { - 80●
let w = img.width - 81
let h = img.height - 82
let ch = img.channels - 83●
let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00') - 84
for i in 0..<(w * h * ch) { - 85●
let v8 = img.data[i].to_int() - 86●
write_u16_le_dh(out, i * 2, v8 * 257) - 87
} - 88
@types.Image16::{ - 89
width: w, - 90
height: h, - 91
channels: ch, - 92●
data: Bytes::from_array(out), - 93
} - 94
} - 95
- 96
///| - 97
fn img8_to_imgf_dh(img : @types.Image) -> @types.ImageF { - 98●
let w = img.width - 99
let h = img.height - 100
let ch = img.channels - 101●
let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00') - 102
for i in 0..<(w * h * ch) { - 103●
let v8 = img.data[i].to_int() - 104●
write_f32_le_dh(out, i * 4, Float::from_int(v8) / 255.0F) - 105
} - 106
@types.ImageF::{ - 107
width: w, - 108
height: h, - 109
channels: ch, - 110●
data: Bytes::from_array(out), - 111
} - 112
} - 113
- 114
// ========== Dehaze 16-bit ========== - 115
- 116
///| - 117
/// 16-bit 图像去雾。 - 118
/// @see dehaze 8-bit 版本,本函数为 Image16 变体 - 119
pub fn dehaze_16( - 120
img : @types.Image16, - 121
patch_size~ : Int32 = 7, - 122
omega~ : Float = 0.95F, - 123
t0~ : Float = 0.1F, - 124
) -> @types.Image16 raise @types.LoadError { - 125
// 输入验证 - 126●
if img.width <= 0 || img.height <= 0 { - 127●
raise @types.LoadError::DecodeFailed("dehaze_16: 图像尺寸必须大于0") - 128
} - 129
if img.channels != 3 && img.channels != 4 { - 130●
raise @types.LoadError::DecodeFailed( - 131
"dehaze_16: 仅支持RGB/RGBA图像(channels=3/4)", - 132
) - 133
} - 134
let expected_size = img.width * img.height * img.channels * 2 - 135●
if img.data.length() != expected_size { - 136●
raise @types.LoadError::DecodeFailed( - 137
"dehaze_16: 数据大小不匹配,期望 " + - 138●
expected_size.to_string() + - 139
",实际 " + - 140●
img.data.length().to_string(), - 141
) - 142
} - 143●
let img8 = img16_to_img8_dh(img) - 144●
let out8 = dehaze(img8, patch_size~, omega~, t0~) - 145●
img8_to_img16_dh(out8) - 146
} - 147
- 148
// ========== Dehaze float ========== - 149
- 150
///| - 151
/// float 图像去雾。 - 152
/// @see dehaze 8-bit 版本,本函数为 ImageF 变体 - 153
pub fn dehaze_f( - 154
img : @types.ImageF, - 155
patch_size~ : Int32 = 7, - 156
omega~ : Float = 0.95F, - 157
t0~ : Float = 0.1F, - 158
) -> @types.ImageF raise @types.LoadError { - 159
// 输入验证 - 160●
if img.width <= 0 || img.height <= 0 { - 161●
raise @types.LoadError::DecodeFailed("dehaze_f: 图像尺寸必须大于0") - 162
} - 163
if img.channels != 3 && img.channels != 4 { - 164○
raise @types.LoadError::DecodeFailed( - 165
"dehaze_f: 仅支持RGB/RGBA图像(channels=3/4)", - 166
) - 167
} - 168
let expected_size = img.width * img.height * img.channels * 4 - 169●
if img.data.length() != expected_size { - 170●
raise @types.LoadError::DecodeFailed( - 171
"dehaze_f: 数据大小不匹配,期望 " + - 172●
expected_size.to_string() + - 173
",实际 " + - 174●
img.data.length().to_string(), - 175
) - 176
} - 177●
let img8 = imgf_to_img8_dh(img) - 178●
let out8 = dehaze(img8, patch_size~, omega~, t0~) - 179●
img8_to_imgf_dh(out8) - 180
} - 181
- 182
// ========== Guided Filter 16-bit ========== - 183
- 184
///| - 185
/// 16-bit 引导滤波。 - 186
/// @see guided_filter 8-bit 版本,本函数为 Image16 变体 - 187
pub fn guided_filter_16( - 188
img : @types.Image16, - 189
input : Array[Float], - 190
radius~ : Int32 = 20, - 191
eps~ : Float = 0.001F, - 192
) -> Array[Float] raise @types.LoadError { - 193
// 输入验证 - 194●
if img.width <= 0 || img.height <= 0 { - 195●
raise @types.LoadError::DecodeFailed( - 196
"guided_filter_16: 图像尺寸必须大于0", - 197
) - 198
} - 199
if img.channels < 1 || img.channels > 4 { - 200●
raise @types.LoadError::DecodeFailed( - 201
"guided_filter_16: 通道数必须在1-4之间", - 202
) - 203
} - 204
let expected_size = img.width * img.height * img.channels * 2 - 205●
if img.data.length() != expected_size { - 206●
raise @types.LoadError::DecodeFailed( - 207
"guided_filter_16: 数据大小不匹配,期望 " + - 208●
expected_size.to_string() + - 209
",实际 " + - 210●
img.data.length().to_string(), - 211
) - 212
} - 213
if radius < 1 { - 214●
raise @types.LoadError::DecodeFailed("guided_filter_16: radius必须>=1") - 215
} - 216
if eps <= 0.0F { - 217●
raise @types.LoadError::DecodeFailed("guided_filter_16: eps必须大于0") - 218
} - 219●
let img8 = img16_to_img8_dh(img) - 220●
guided_filter(img8, input, radius~, eps~) - 221
} - 222
- 223
// ========== Guided Filter float ========== - 224
- 225
///| - 226
/// float 引导滤波。 - 227
/// @see guided_filter 8-bit 版本,本函数为 ImageF 变体 - 228
pub fn guided_filter_f( - 229
img : @types.ImageF, - 230
input : Array[Float], - 231
radius~ : Int32 = 20, - 232
eps~ : Float = 0.001F, - 233
) -> Array[Float] raise @types.LoadError { - 234
// 输入验证 - 235●
if img.width <= 0 || img.height <= 0 { - 236●
raise @types.LoadError::DecodeFailed( - 237
"guided_filter_f: 图像尺寸必须大于0", - 238
) - 239
} - 240
if img.channels < 1 || img.channels > 4 { - 241●
raise @types.LoadError::DecodeFailed( - 242
"guided_filter_f: 通道数必须在1-4之间", - 243
) - 244
} - 245
let expected_size = img.width * img.height * img.channels * 4 - 246●
if img.data.length() != expected_size { - 247●
raise @types.LoadError::DecodeFailed( - 248
"guided_filter_f: 数据大小不匹配,期望 " + - 249●
expected_size.to_string() + - 250
",实际 " + - 251●
img.data.length().to_string(), - 252
) - 253
} - 254
if radius < 1 { - 255●
raise @types.LoadError::DecodeFailed("guided_filter_f: radius必须>=1") - 256
} - 257
if eps <= 0.0F { - 258●
raise @types.LoadError::DecodeFailed("guided_filter_f: eps必须大于0") - 259
} - 260●
let img8 = imgf_to_img8_dh(img) - 261●
guided_filter(img8, input, radius~, eps~) - 262
}