image.process\color\colormap.mbt
92.3% · 60 / 65 points · 141 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 色彩映射 colormap - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 伪彩色映射(纯 MoonBit):apply_colormap + 预设色彩映射表 - 7
- 8
///| - 9
/// 色彩映射预设 - 10
/// `#non_exhaustive` 表示未来可能添加新构造器 - 11
#non_exhaustive - 12
pub(all) enum Colormap { - 13
JET // 蓝→青→绿→黄→红 - 14
HOT // 黑→红→黄→白 - 15
COOL // 青→品红 - 16
GRAY // 灰度 - 17
BONE // 偏蓝灰 - 18
COPPER // 偏铜色 - 19
} - 20
- 21
///| - 22
fn get_colormap_lut(map : Colormap) -> Array[(Byte, Byte, Byte)] { - 23●
let lut : Array[(Byte, Byte, Byte)] = Array::make( - 24
256, - 25
(b'\x00', b'\x00', b'\x00'), - 26
) - 27
for i in 0..<256 { - 28●
let t = Float::from_int(i) / 255.0F - 29●
let (r, g, b) = match map { - 30●
JET => { - 31
// JET: 4 段线性插值 - 32
let r_val = if t < 0.375F { - 33●
0.0F - 34●
} else if t < 0.625F { - 35●
(t - 0.375F) / 0.25F - 36
} else { - 37●
1.0F - 38
} - 39
let g_val = if t < 0.125F { - 40●
0.0F - 41●
} else if t < 0.375F { - 42●
(t - 0.125F) / 0.25F - 43●
} else if t < 0.625F { - 44●
1.0F - 45●
} else if t < 0.875F { - 46●
1.0F - (t - 0.625F) / 0.25F - 47
} else { - 48●
0.0F - 49
} - 50
let b_val = if t < 0.125F { - 51●
0.5F + t / 0.25F * 0.5F - 52●
} else if t < 0.375F { - 53●
1.0F - 54●
} else if t < 0.625F { - 55●
1.0F - (t - 0.375F) / 0.25F - 56
} else { - 57●
0.0F - 58
} - 59
(r_val, g_val, b_val) - 60
} - 61●
HOT => { - 62●
let r_val = if t < 0.333F { t / 0.333F } else { 1.0F } - 63
let g_val = if t < 0.333F { - 64●
0.0F - 65●
} else if t < 0.666F { - 66●
(t - 0.333F) / 0.333F - 67
} else { - 68●
1.0F - 69
} - 70●
let b_val = if t < 0.666F { 0.0F } else { (t - 0.666F) / 0.334F } - 71
(r_val, g_val, b_val) - 72
} - 73●
COOL => (0.0F, t, 1.0F - t) - 74●
GRAY => (t, t, t) - 75
BONE => - 76
// 偏蓝灰 - 77●
(t * 0.93F, t * 0.88F, t * 1.0F) - 78
COPPER => - 79
// 偏铜色 - 80●
((t * 1.25F).clamp(min=0.0F, max=1.0F), t * 0.78F, t * 0.5F) - 81
} - 82
lut[i] = ( - 83●
(r * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte(), - 84●
(g * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte(), - 85●
(b * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte(), - 86
) - 87
} - 88
lut - 89
} - 90
- 91
///| - 92
/// 伪彩色映射。将灰度图或彩色图(先转灰度)映射为 RGB 伪彩色图像。 - 93
pub fn apply_colormap( - 94
img : @types.Image, - 95
map : Colormap, - 96
) -> @types.Image raise @types.LoadError { - 97
// 输入验证 - 98●
if img.width <= 0 || img.height <= 0 { - 99●
raise @types.LoadError::DecodeFailed( - 100
"apply_colormap: 图像尺寸必须大于0", - 101
) - 102
} - 103
if img.channels < 1 || img.channels > 4 { - 104○
raise @types.LoadError::DecodeFailed( - 105
"apply_colormap: 通道数必须在1-4之间", - 106
) - 107
} - 108
let expected_size = img.width * img.height * img.channels - 109●
if img.data.length() != expected_size { - 110○
raise @types.LoadError::DecodeFailed( - 111
"apply_colormap: 数据大小不匹配,期望 " + - 112○
expected_size.to_string() + - 113
",实际 " + - 114○
img.data.length().to_string(), - 115
) - 116
} - 117●
let lut = get_colormap_lut(map) - 118
let n = img.width * img.height - 119
let ch = img.channels - 120●
let out : Array[Byte] = Array::make(n * 3, b'\x00') - 121
for i in 0..<n { - 122●
let gray = if ch == 1 { - 123●
img.data[i].to_int() - 124
} else { - 125●
let r = img.data[i * ch].to_int() - 126●
let g = img.data[i * ch + 1].to_int() - 127●
let b = img.data[i * ch + 2].to_int() - 128
(r * 299 + g * 587 + b * 114) / 1000 - 129
} - 130
let (cr, cg, cb) = lut[gray] - 131
out[i * 3] = cr - 132
out[i * 3 + 1] = cg - 133
out[i * 3 + 2] = cb - 134
} - 135
@types.Image::{ - 136
width: img.width, - 137
height: img.height, - 138
channels: 3, - 139●
data: Bytes::from_array(out), - 140
} - 141
}