image.codec\color\color_convert.mbt
100% · 77 / 77 points · 258 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 色彩转换 color/color_convert - 3
/// - 4
/// 编解码工具模块 Codec utility module - 5
- 6
// 纯 MoonBit 色彩模型转换(全目标可用:native/wasm/js) - 7
// 仅依赖 @types,不依赖 @core/FFI - 8
// 移植自 src/process/color/color_convert.mbt:1-162 的 5 个 8-bit Image 色彩转换函数 - 9
- 10
///| - 11
/// 转为灰度图(channels=1,纯 MoonBit,全目标可用) Convert to grayscale (channels=1, pure MoonBit, all targets) - 12
/// 使用 ITU-R BT.601 亮度公式: Y = 0.299R + 0.587G + 0.114B Uses ITU-R BT.601 luma formula: Y = 0.299R + 0.587G + 0.114B - 13
/// - 14
/// ```mbt check - 15
/// test "doc_to_grayscale_pure" { - 16
/// let data = Bytes::from_array([b'\xff', b'\x00', b'\x00']) - 17
/// let img = @types.Image::{ width: 1, height: 1, channels: 3, data, } - 18
/// let gray = @color.to_grayscale_pure(img) - 19
/// assert_eq(gray.channels, 1) - 20
/// assert_eq(gray.width, 1) - 21
/// } - 22
/// ``` - 23
pub fn to_grayscale_pure(img : @types.Image) -> @types.Image { - 24●
if img.channels == 1 { - 25●
return img - 26
} - 27
let pixel_count = img.width * img.height - 28
let ch = img.channels - 29●
let data_int : Array[Int32] = Array::make(pixel_count * ch, 0) - 30
for i in 0..<(pixel_count * ch) { - 31●
data_int[i] = img.data[i].to_int() - 32
} - 33●
let out : Array[Byte] = Array::make(pixel_count, b'\x00') - 34
for i in 0..<pixel_count { - 35●
let r = data_int[i * ch] - 36
let g = data_int[i * ch + 1] - 37
let b = data_int[i * ch + 2] - 38
let gray = (r * 299 + g * 587 + b * 114) / 1000 - 39●
out[i] = gray.to_byte() - 40
} - 41
@types.Image::{ - 42
width: img.width, - 43
height: img.height, - 44
channels: 1, - 45●
data: Bytes::from_array(out), - 46
} - 47
} - 48
- 49
///| - 50
/// 去除 alpha 通道(RGBA → RGB,channels=3,纯 MoonBit,全目标可用) - 51
pub fn to_rgb_pure(img : @types.Image) -> @types.Image { - 52●
if img.channels == 3 { - 53●
return img - 54
} - 55
if img.channels < 3 { - 56●
return img - 57
} - 58
let pixel_count = img.width * img.height - 59●
let out : Array[Byte] = Array::make(pixel_count * 3, b'\x00') - 60
for i in 0..<pixel_count { - 61●
out[i * 3] = img.data[i * img.channels] - 62
out[i * 3 + 1] = img.data[i * img.channels + 1] - 63
out[i * 3 + 2] = img.data[i * img.channels + 2] - 64
} - 65
@types.Image::{ - 66
width: img.width, - 67
height: img.height, - 68
channels: 3, - 69●
data: Bytes::from_array(out), - 70
} - 71
} - 72
- 73
///| - 74
/// 添加 alpha 通道(RGB → RGBA,channels=4,alpha=255,纯 MoonBit,全目标可用) - 75
pub fn to_rgba_pure(img : @types.Image) -> @types.Image { - 76●
if img.channels == 4 { - 77●
return img - 78
} - 79
let pixel_count = img.width * img.height - 80●
let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00') - 81
for i in 0..<pixel_count { - 82●
let src_ch = img.channels - 83
out[i * 4] = img.data[i * src_ch] - 84
out[i * 4 + 1] = if src_ch >= 2 { - 85●
img.data[i * src_ch + 1] - 86
} else { - 87●
b'\x00' - 88
} - 89
out[i * 4 + 2] = if src_ch >= 3 { - 90●
img.data[i * src_ch + 2] - 91
} else { - 92●
b'\x00' - 93
} - 94
out[i * 4 + 3] = b'\xFF' - 95
} - 96
@types.Image::{ - 97
width: img.width, - 98
height: img.height, - 99
channels: 4, - 100●
data: Bytes::from_array(out), - 101
} - 102
} - 103
- 104
///| - 105
/// 按 stb_image 语义转换通道数(纯 MoonBit,全目标可用) Convert channels following stb_image semantics (pure MoonBit, all targets) - 106
/// - 1: 灰度(BT.601 亮度公式) Grayscale (BT.601 luma formula) - 107
/// - 2: 灰度 + alpha(alpha=255 若源无 alpha) Grayscale + alpha (alpha=255 if source has no alpha) - 108
/// - 3: RGB(丢弃 alpha,灰度展开为 R=G=B) RGB (discard alpha, grayscale expanded to R=G=B) - 109
/// - 4: RGBA(alpha=255 若源无 alpha,灰度展开为 R=G=B) RGBA (alpha=255 if source has no alpha, grayscale expanded to R=G=B) - 110
/// - 111
/// ```mbt check - 112
/// test "doc_convert_channels_pure" { - 113
/// try { - 114
/// let data = Bytes::from_array([b'\xff', b'\x00', b'\x00']) - 115
/// let img = @types.Image::{ width: 1, height: 1, channels: 3, data, } - 116
/// let rgba = @color.convert_channels_pure(img, 4) - 117
/// assert_eq(rgba.channels, 4) - 118
/// } catch { - 119
/// _ => assert_true(false) - 120
/// } - 121
/// } - 122
/// ``` - 123
pub fn convert_channels_pure( - 124
img : @types.Image, - 125
req_channels : Int32, - 126
) -> @types.Image raise @types.LoadError { - 127●
if req_channels == img.channels { - 128●
return img - 129
} - 130
let pixel_count = img.width * img.height - 131
let src_ch = img.channels - 132●
match req_channels { - 133●
1 => to_grayscale_pure(img) - 134●
2 => { - 135●
let out : Array[Byte] = Array::make(pixel_count * 2, b'\x00') - 136
for i in 0..<pixel_count { - 137●
let gray = if src_ch >= 3 { - 138●
let r = img.data[i * src_ch].to_int() - 139●
let g = img.data[i * src_ch + 1].to_int() - 140●
let b = img.data[i * src_ch + 2].to_int() - 141
(r * 299 + g * 587 + b * 114) / 1000 - 142
} else { - 143●
img.data[i * src_ch].to_int() - 144
} - 145
let alpha = if src_ch == 2 || src_ch == 4 { - 146●
img.data[i * src_ch + src_ch - 1].to_int() - 147
} else { - 148●
255 - 149
} - 150●
out[i * 2] = gray.to_byte() - 151●
out[i * 2 + 1] = alpha.to_byte() - 152
} - 153
@types.Image::{ - 154
width: img.width, - 155
height: img.height, - 156
channels: 2, - 157●
data: Bytes::from_array(out), - 158
} - 159
} - 160
3 => - 161●
if src_ch == 1 { - 162●
let out : Array[Byte] = Array::make(pixel_count * 3, b'\x00') - 163
for i in 0..<pixel_count { - 164●
let v = img.data[i] - 165
out[i * 3] = v - 166
out[i * 3 + 1] = v - 167
out[i * 3 + 2] = v - 168
} - 169
@types.Image::{ - 170
width: img.width, - 171
height: img.height, - 172
channels: 3, - 173●
data: Bytes::from_array(out), - 174
} - 175
} else { - 176●
to_rgb_pure(img) - 177
} - 178●
4 => to_rgba_pure(img) - 179●
_ => raise @types.LoadError::DecodeFailed("不支持的 req_channels 值") - 180
} - 181
} - 182
- 183
///| - 184
/// 预乘 alpha(RGBA → RGBA premultiplied,纯 MoonBit,全目标可用) - 185
/// 要求 channels=4 - 186
pub fn premultiply_alpha_pure( - 187
img : @types.Image, - 188
) -> @types.Image raise @types.LoadError { - 189●
if img.channels != 4 { - 190●
raise @types.LoadError::DecodeFailed( - 191
"premultiply_alpha_pure 需要 channels=4 (RGBA)", - 192
) - 193
} - 194
let pixel_count = img.width * img.height - 195●
let data_int : Array[Int32] = Array::make(pixel_count * 4, 0) - 196
for i in 0..<(pixel_count * 4) { - 197●
data_int[i] = img.data[i].to_int() - 198
} - 199●
let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00') - 200
for i in 0..<pixel_count { - 201●
let r = data_int[i * 4] - 202
let g = data_int[i * 4 + 1] - 203
let b = data_int[i * 4 + 2] - 204
let a = data_int[i * 4 + 3] - 205●
out[i * 4] = (r * a / 255).to_byte() - 206●
out[i * 4 + 1] = (g * a / 255).to_byte() - 207●
out[i * 4 + 2] = (b * a / 255).to_byte() - 208●
out[i * 4 + 3] = a.to_byte() - 209
} - 210
@types.Image::{ - 211
width: img.width, - 212
height: img.height, - 213
channels: 4, - 214●
data: Bytes::from_array(out), - 215
} - 216
} - 217
- 218
///| - 219
/// 反预乘 alpha(RGBA premA → RGBA unpremultiplied,纯 MoonBit,全目标可用) - 220
/// 要求 channels=4 - 221
pub fn unpremultiply_alpha_pure( - 222
img : @types.Image, - 223
) -> @types.Image raise @types.LoadError { - 224●
if img.channels != 4 { - 225●
raise @types.LoadError::DecodeFailed( - 226
"unpremultiply_alpha_pure 需要 channels=4 (RGBA)", - 227
) - 228
} - 229
let pixel_count = img.width * img.height - 230●
let data_int : Array[Int32] = Array::make(pixel_count * 4, 0) - 231
for i in 0..<(pixel_count * 4) { - 232●
data_int[i] = img.data[i].to_int() - 233
} - 234●
let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00') - 235
for i in 0..<pixel_count { - 236●
let r = data_int[i * 4] - 237
let g = data_int[i * 4 + 1] - 238
let b = data_int[i * 4 + 2] - 239
let a = data_int[i * 4 + 3] - 240
if a == 0 { - 241●
out[i * 4] = b'\x00' - 242
out[i * 4 + 1] = b'\x00' - 243
out[i * 4 + 2] = b'\x00' - 244
out[i * 4 + 3] = b'\x00' - 245
} else { - 246●
out[i * 4] = (r * 255 / a).to_byte() - 247●
out[i * 4 + 1] = (g * 255 / a).to_byte() - 248●
out[i * 4 + 2] = (b * 255 / a).to_byte() - 249●
out[i * 4 + 3] = a.to_byte() - 250
} - 251
} - 252
@types.Image::{ - 253
width: img.width, - 254
height: img.height, - 255
channels: 4, - 256●
data: Bytes::from_array(out), - 257
} - 258
}