///|
/// # 量化 quantize
///
/// 图像处理算法模块
/// 图像处理算法模块 Image processing algorithm module
// 色彩量化(纯 MoonBit):Floyd-Steinberg 抖动 + Median Cut + K-means 量化
///|
/// Floyd-Steinberg 误差扩散抖动量化。
/// levels 为每通道量化级别数(如 levels=2 → 0/255,levels=4 → 0/85/170/255)。
/// 仅处理颜色通道,alpha 不变。
pub fn floyd_steinberg(
img : @types.Image,
levels : Int32,
) -> @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"floyd_steinberg: 图像尺寸必须大于0",
)
}
if img.channels < 1 || img.channels > 4 {
raise @types.LoadError::DecodeFailed(
"floyd_steinberg: 通道数必须在1-4之间",
)
}
let expected_size = img.width * img.height * img.channels
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"floyd_steinberg: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if levels < 2 {
raise @types.LoadError::DecodeFailed("floyd_steinberg: levels必须>=2")
}
let ch = img.channels
let color_ch = if ch >= 3 { 3 } else { ch }
let w = img.width
let h = img.height
let n = w * h
// 可变像素缓冲
let buf : Array[Int32] = Array::make(n * color_ch, 0)
for i in 0.. 0 {
buf[((y + 1) * w + x - 1) * color_ch + c] += err * 3 / 16
}
buf[((y + 1) * w + x) * color_ch + c] += err * 5 / 16
if x + 1 < w {
buf[((y + 1) * w + x + 1) * color_ch + c] += err / 16
}
}
}
}
}
// 输出
let out : Array[Byte] = Array::make(n * ch, b'\x00')
for i in 0.. @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"median_cut: 图像尺寸必须大于0",
)
}
if img.channels < 1 || img.channels > 4 {
raise @types.LoadError::DecodeFailed(
"median_cut: 通道数必须在1-4之间",
)
}
let expected_size = img.width * img.height * img.channels
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"median_cut: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if num_colors < 2 {
return to_rgb(img)
}
let w = img.width
let h = img.height
let n = w * h
// 收集所有 RGB 像素
let pixels : Array[(Int32, Int32, Int32)] = Array::make(n, (0, 0, 0))
let ch = img.channels
let data_int : Array[Int32] = Array::make(n * ch, 0)
for i in 0..<(n * ch) {
data_int[i] = img.data[i].to_int()
}
for i in 0..= 2 { data_int[i * ch + 1] } else { r }
let b = if ch >= 3 { data_int[i * ch + 2] } else { r }
pixels[i] = (r, g, b)
}
// 初始 bucket 包含所有像素索引
let initial_bucket : Array[Int32] = Array::make(n, 0)
for i in 0.. best_range {
best_range = range
best_bucket_idx = bi
}
}
if best_bucket_idx < 0 {
break
}
// 分割最佳 bucket
let bucket = buckets[best_bucket_idx]
let (_, split_ch) = bucket_max_range(pixels, bucket)
// 按分割通道排序
let sorted = bucket.copy()
sorted.sort_by(fn(a, b) {
let va = channel_value(pixels[a], split_ch)
let vb = channel_value(pixels[b], split_ch)
va.compare(vb)
})
let mid = sorted.length() / 2
let left : Array[Int32] = Array::make(mid, 0)
let right : Array[Int32] = Array::make(sorted.length() - mid, 0)
for i in 0.. @types.Image raise @types.LoadError {
if k < 1 || k > 256 {
raise @types.LoadError::DecodeFailed("k 必须在 1..256 之间")
}
if img.channels != 3 && img.channels != 4 {
raise @types.LoadError::DecodeFailed(
"kmeans_quantize 需要 RGB/RGBA 图像",
)
}
let ch = img.channels
let n = img.width * img.height
if n == 0 {
return img
}
// 初始化聚类中心:均匀采样
let centroids_r : Array[Int32] = Array::make(k, 0)
let centroids_g : Array[Int32] = Array::make(k, 0)
let centroids_b : Array[Int32] = Array::make(k, 0)
let data_int : Array[Int32] = Array::make(n * ch, 0)
for i in 0..<(n * ch) {
data_int[i] = img.data[i].to_int()
}
let step = if n > k { n / k } else { 1 }
for i in 0.. 0 {
centroids_r[j] = sum_r[j] / cnt[j]
centroids_g[j] = sum_g[j] / cnt[j]
centroids_b[j] = sum_b[j] / cnt[j]
}
}
}
// 生成输出
let out : Array[Byte] = Array::make(n * ch, b'\x00')
for i in 0.. Int32 {
match ch {
0 => pixel.0
1 => pixel.1
_ => pixel.2
}
}
///|
fn bucket_max_range(
pixels : Array[(Int32, Int32, Int32)],
bucket : Array[Int32],
) -> (Int32, Int32) {
let mut min_r = 255
let mut max_r = 0
let mut min_g = 255
let mut max_g = 0
let mut min_b = 255
let mut max_b = 0
for i in 0.. max_r {
max_r = r
}
if g < min_g {
min_g = g
}
if g > max_g {
max_g = g
}
if b < min_b {
min_b = b
}
if b > max_b {
max_b = b
}
}
let range_r = max_r - min_r
let range_g = max_g - min_g
let range_b = max_b - min_b
if range_r >= range_g && range_r >= range_b {
(range_r, 0)
} else if range_g >= range_b {
(range_g, 1)
} else {
(range_b, 2)
}
}
///|
fn bucket_average(
pixels : Array[(Int32, Int32, Int32)],
bucket : Array[Int32],
) -> (Int32, Int32, Int32) {
let mut sum_r = 0
let mut sum_g = 0
let mut sum_b = 0
for i in 0.. Int32 {
let dr = r1 - r2
let dg = g1 - g2
let db = b1 - b2
dr * dr + dg * dg + db * db
}