rgba -> rgb
This commit is contained in:
parent
e792087ebc
commit
9e42ad0f36
1 changed files with 9 additions and 10 deletions
19
src/lib.rs
19
src/lib.rs
|
|
@ -1,11 +1,11 @@
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
use image::{GenericImageView, Pixel, Rgba, RgbaImage};
|
use image::{GenericImageView, Pixel, RgbImage, Rgb};
|
||||||
use imageproc::definitions::Image;
|
use imageproc::definitions::Image;
|
||||||
|
|
||||||
const MAX_LEVEL: usize = 5;
|
const MAX_LEVEL: usize = 5;
|
||||||
|
|
||||||
pub fn quantize<P>(image: &Image<P>, num_colors: usize) -> RgbaImage
|
pub fn quantize<P>(image: &Image<P>, num_colors: usize) -> RgbImage
|
||||||
where
|
where
|
||||||
P: Pixel<Subpixel = u8> + 'static,
|
P: Pixel<Subpixel = u8> + 'static,
|
||||||
{
|
{
|
||||||
|
|
@ -37,7 +37,7 @@ impl OctTreeQuantizer {
|
||||||
new_quantizer
|
new_quantizer
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn quantize<P>(&mut self, image: &Image<P>) -> RgbaImage
|
pub fn quantize<P>(&mut self, image: &Image<P>) -> RgbImage
|
||||||
where
|
where
|
||||||
P: Pixel<Subpixel = u8> + 'static,
|
P: Pixel<Subpixel = u8> + 'static,
|
||||||
{
|
{
|
||||||
|
|
@ -57,7 +57,7 @@ impl OctTreeQuantizer {
|
||||||
}
|
}
|
||||||
let table = self.build_color_table();
|
let table = self.build_color_table();
|
||||||
|
|
||||||
let mut out = RgbaImage::new(image.width(), image.height());
|
let mut out = RgbImage::new(image.width(), image.height());
|
||||||
for y in 0..image.height() {
|
for y in 0..image.height() {
|
||||||
for x in 0..image.width() {
|
for x in 0..image.width() {
|
||||||
unsafe { //safe because bounds are checked
|
unsafe { //safe because bounds are checked
|
||||||
|
|
@ -111,12 +111,12 @@ impl OctTreeQuantizer {
|
||||||
get_index_for_color(&self, color, 0, node)
|
get_index_for_color(&self, color, 0, node)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_color_table(&mut self) -> Vec<Option<Rgba<u8>>> {
|
fn build_color_table(&mut self) -> Vec<Option<Rgb<u8>>> {
|
||||||
//nested function that is called recursively
|
//nested function that is called recursively
|
||||||
fn build_color_table(
|
fn build_color_table(
|
||||||
quantizer: &mut OctTreeQuantizer,
|
quantizer: &mut OctTreeQuantizer,
|
||||||
node: &Rc<RefCell<OctTreeNode>>,
|
node: &Rc<RefCell<OctTreeNode>>,
|
||||||
table: &mut Vec<Option<Rgba<u8>>>,
|
table: &mut Vec<Option<Rgb<u8>>>,
|
||||||
index: usize,
|
index: usize,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
if quantizer.colors > quantizer.maximum_colors {
|
if quantizer.colors > quantizer.maximum_colors {
|
||||||
|
|
@ -126,11 +126,10 @@ impl OctTreeQuantizer {
|
||||||
{
|
{
|
||||||
let node = node.borrow();
|
let node = node.borrow();
|
||||||
let count = node.count;
|
let count = node.count;
|
||||||
table[index] = Some(Rgba::from([
|
table[index] = Some(Rgb::from([
|
||||||
(node.total_red / count as u32) as u8,
|
(node.total_red / count as u32) as u8,
|
||||||
(node.total_green / count as u32) as u8,
|
(node.total_green / count as u32) as u8,
|
||||||
(node.total_blue / count as u32) as u8,
|
(node.total_blue / count as u32) as u8,
|
||||||
0xFF,
|
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
node.borrow_mut().index = index;
|
node.borrow_mut().index = index;
|
||||||
|
|
@ -153,7 +152,7 @@ impl OctTreeQuantizer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut table: Vec<Option<Rgba<u8>>> = vec![None; self.colors];
|
let mut table: Vec<Option<Rgb<u8>>> = vec![None; self.colors];
|
||||||
let node = Rc::clone(&self.root);
|
let node = Rc::clone(&self.root);
|
||||||
build_color_table(self, &node, &mut table, 0);
|
build_color_table(self, &node, &mut table, 0);
|
||||||
table
|
table
|
||||||
|
|
@ -370,7 +369,7 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_big_image() -> Result<(), ImageError> {
|
fn test_big_image() -> Result<(), ImageError> {
|
||||||
let src: RgbaImage = image::open("testdata/input.jpg").unwrap().into_rgba8();
|
let src: RgbImage = image::open("testdata/input.jpg").unwrap().into_rgb8();
|
||||||
|
|
||||||
let out = quantize(&src, 256);
|
let out = quantize(&src, 256);
|
||||||
out.save_with_format("output.jpg", image::ImageFormat::Jpeg)?;
|
out.save_with_format("output.jpg", image::ImageFormat::Jpeg)?;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue