small improvements

This commit is contained in:
Sander Hautvast 2022-01-27 17:34:36 +01:00
parent 9e42ad0f36
commit 76a555596c
3 changed files with 18 additions and 28 deletions

2
Cargo.lock generated
View file

@ -371,7 +371,7 @@ dependencies = [
]
[[package]]
name = "octo"
name = "octtreequantizer"
version = "0.1.0"
dependencies = [
"image",

View file

@ -1,5 +1,5 @@
[package]
name = "octo"
name = "octtreequantizer"
version = "0.1.0"
edition = "2021"
@ -8,6 +8,3 @@ edition = "2021"
[dependencies]
image="0.23.14"
imageproc="0.22.0"
[profile.release]
debug = true

View file

@ -1,6 +1,6 @@
use std::{cell::RefCell, rc::Rc};
use image::{GenericImageView, Pixel, RgbImage, Rgb};
use image::{Pixel, Rgb, RgbImage};
use imageproc::definitions::Image;
const MAX_LEVEL: usize = 5;
@ -41,10 +41,8 @@ impl OctTreeQuantizer {
where
P: Pixel<Subpixel = u8> + 'static,
{
for y in 0..image.height() {
for x in 0..image.width() {
let p = image.get_pixel(x, y);
self.insert_color(p, Rc::clone(&self.root));
for pixel in image.pixels() {
self.insert_color(pixel, Rc::clone(&self.root));
if self.colors > self.reduce_colors {
self.reduce_tree(self.reduce_colors);
@ -54,24 +52,19 @@ impl OctTreeQuantizer {
}
}
}
}
let table = self.build_color_table();
let mut out = RgbImage::new(image.width(), image.height());
for y in 0..image.height() {
for x in 0..image.width() {
unsafe { //safe because bounds are checked
let pixel = image.unsafe_get_pixel(x, y);
if let Some(index) = self.get_index_for_color(&pixel, &self.root) {
let color = table.get(index).unwrap();
let mut imgbuf = RgbImage::new(image.width(), image.height());
for (x, y, pixel) in image.enumerate_pixels() {
if let Some(index) = self.get_index_for_color(pixel, &self.root) {
let color = &table[index];
if let Some(color) = color {
out.put_pixel(x, y, *color);
imgbuf.put_pixel(x, y, *color);
}
}
}
}
}
out
imgbuf
}
fn get_index_for_color<P>(&self, color: &P, node: &Rc<RefCell<OctTreeNode>>) -> Option<usize>