image.domain\util\color_map.mbt
98.3% · 57 / 58 points · 179 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 色彩映射 util/color_map - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 色彩映射与 alpha 操作(纯 MoonBit):apply_lut / gradient_map / set_alpha / fill_alpha - 7
- 8
///| - 9
- 10
///| - 11
/// 应用查找表(LUT)。lut 为 256 个 Byte 的映射表,将每个像素值 v 替换为 lut[v]。 - 12
/// 仅影响颜色通道,alpha 通道不变。 - 13
pub fn apply_lut( - 14
img : @types.Image, - 15
lut : Array[Byte], - 16
) -> @types.Image raise @types.LoadError { - 17●
if lut.length() < 256 { - 18●
raise @types.LoadError::DecodeFailed("LUT 必须有 256 个条目") - 19
} - 20
let n = img.width * img.height - 21
let ch = img.channels - 22●
let color_ch = if ch >= 3 { 3 } else { ch } - 23●
let out : Array[Byte] = Array::make(n * ch, b'\x00') - 24
for i in 0..<n { - 25●
for c in 0..<color_ch { - 26●
out[i * ch + c] = lut[img.data[i * ch + c].to_int()] - 27
} - 28
if ch == 4 { - 29●
out[i * ch + 3] = img.data[i * ch + 3] - 30
} - 31
} - 32
@types.Image::{ - 33
width: img.width, - 34
height: img.height, - 35
channels: ch, - 36●
data: Bytes::from_array(out), - 37
} - 38
} - 39
- 40
///| - 41
/// 渐变映射。将图像亮度映射到 color_stops 定义的颜色渐变。 - 42
/// color_stops 为渐变颜色点,每个点是 (position, r, g, b),position ∈ [0, 255]。 - 43
/// 返回 RGB 图像(channels=3)。 - 44
pub fn gradient_map( - 45
img : @types.Image, - 46
color_stops : Array[(Int32, Byte, Byte, Byte)], - 47
) -> @types.Image raise @types.LoadError { - 48●
if color_stops.length() < 2 { - 49●
raise @types.LoadError::DecodeFailed( - 50
"gradient_map 至少需要 2 个颜色点", - 51
) - 52
} - 53
let n = img.width * img.height - 54●
let out : Array[Byte] = Array::make(n * 3, b'\x00') - 55
// 预计算 256 个亮度的映射 - 56●
let lut : Array[(Byte, Byte, Byte)] = Array::make( - 57
256, - 58
(b'\x00', b'\x00', b'\x00'), - 59
) - 60
for v in 0..<256 { - 61●
lut[v] = interp_gradient(v, color_stops) - 62
} - 63
for i in 0..<n { - 64●
let ch = img.channels - 65
let luminance = if ch >= 3 { - 66
// RGB -> luminance (0.299R + 0.587G + 0.114B) - 67●
let r = img.data[i * ch].to_int() - 68●
let g = img.data[i * ch + 1].to_int() - 69●
let b = img.data[i * ch + 2].to_int() - 70
(r * 77 + g * 150 + b * 29) / 256 - 71
} else { - 72●
img.data[i * ch].to_int() - 73
} - 74
let (r, g, b) = lut[luminance] - 75
out[i * 3] = r - 76
out[i * 3 + 1] = g - 77
out[i * 3 + 2] = b - 78
} - 79
@types.Image::{ - 80
width: img.width, - 81
height: img.height, - 82
channels: 3, - 83●
data: Bytes::from_array(out), - 84
} - 85
} - 86
- 87
///| - 88
fn interp_gradient( - 89
v : Int32, - 90
stops : Array[(Int32, Byte, Byte, Byte)], - 91
) -> (Byte, Byte, Byte) { - 92●
let len = stops.length() - 93
if v <= stops[0].0 { - 94●
return (stops[0].1, stops[0].2, stops[0].3) - 95
} - 96
if v >= stops[len - 1].0 { - 97●
return (stops[len - 1].1, stops[len - 1].2, stops[len - 1].3) - 98
} - 99
for i in 0..<(len - 1) { - 100●
let (p0, r0, g0, b0) = stops[i] - 101
let (p1, r1, g1, b1) = stops[i + 1] - 102
if v >= p0 && v <= p1 { - 103●
let range = p1 - p0 - 104
if range == 0 { - 105○
return (r0, g0, b0) - 106
} - 107
let t = v - p0 - 108●
let r = r0.to_int() + (r1.to_int() - r0.to_int()) * t / range - 109●
let g = g0.to_int() + (g1.to_int() - g0.to_int()) * t / range - 110●
let b = b0.to_int() + (b1.to_int() - b0.to_int()) * t / range - 111●
return (@types.clamp_byte(r), @types.clamp_byte(g), @types.clamp_byte(b)) - 112
} - 113
} - 114
(b'\x00', b'\x00', b'\x00') - 115
} - 116
- 117
///| - 118
/// 设置所有像素的 alpha 通道为指定值。图像必须是 RGBA(channels=4)。 - 119
pub fn set_alpha( - 120
img : @types.Image, - 121
alpha : Byte, - 122
) -> @types.Image raise @types.LoadError { - 123●
if img.channels != 4 { - 124●
raise @types.LoadError::DecodeFailed( - 125
"set_alpha 需要 RGBA 图像(channels=4)", - 126
) - 127
} - 128
let n = img.width * img.height - 129●
let out : Array[Byte] = Array::make(n * 4, b'\x00') - 130
for i in 0..<n { - 131●
out[i * 4] = img.data[i * 4] - 132
out[i * 4 + 1] = img.data[i * 4 + 1] - 133
out[i * 4 + 2] = img.data[i * 4 + 2] - 134
out[i * 4 + 3] = alpha - 135
} - 136
@types.Image::{ - 137
width: img.width, - 138
height: img.height, - 139
channels: 4, - 140●
data: Bytes::from_array(out), - 141
} - 142
} - 143
- 144
///| - 145
/// 用指定颜色填充 alpha 通道。将 alpha < threshold 的像素的 RGB 设为指定颜色。 - 146
pub fn fill_alpha( - 147
img : @types.Image, - 148
threshold : Int32, - 149
fill_r : Byte, - 150
fill_g : Byte, - 151
fill_b : Byte, - 152
) -> @types.Image raise @types.LoadError { - 153●
if img.channels != 4 { - 154●
raise @types.LoadError::DecodeFailed( - 155
"fill_alpha 需要 RGBA 图像(channels=4)", - 156
) - 157
} - 158
let n = img.width * img.height - 159●
let out : Array[Byte] = Array::make(n * 4, b'\x00') - 160
for i in 0..<n { - 161●
let a = img.data[i * 4 + 3].to_int() - 162
if a < threshold { - 163●
out[i * 4] = fill_r - 164
out[i * 4 + 1] = fill_g - 165
out[i * 4 + 2] = fill_b - 166
} else { - 167●
out[i * 4] = img.data[i * 4] - 168
out[i * 4 + 1] = img.data[i * 4 + 1] - 169
out[i * 4 + 2] = img.data[i * 4 + 2] - 170
} - 171
out[i * 4 + 3] = img.data[i * 4 + 3] - 172
} - 173
@types.Image::{ - 174
width: img.width, - 175
height: img.height, - 176
channels: 4, - 177●
data: Bytes::from_array(out), - 178
} - 179
}