From 76a555596c2b09de668aa9a17324fed1b1d0ab9a Mon Sep 17 00:00:00 2001 From: Sander Hautvast Date: Thu, 27 Jan 2022 17:34:36 +0100 Subject: [PATCH] small improvements --- Cargo.lock | 2 +- Cargo.toml | 5 +---- src/lib.rs | 39 ++++++++++++++++----------------------- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13b3c04..f44ee69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -371,7 +371,7 @@ dependencies = [ ] [[package]] -name = "octo" +name = "octtreequantizer" version = "0.1.0" dependencies = [ "image", diff --git a/Cargo.toml b/Cargo.toml index 358f696..c153276 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 61f09a2..cfc24ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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,37 +41,30 @@ impl OctTreeQuantizer { where P: Pixel + '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); - //reduce sets to None and the code below actually removes nodes from the list - for level in &mut self.color_list { - level.retain(|c| c.is_some()); - } + if self.colors > self.reduce_colors { + self.reduce_tree(self.reduce_colors); + //reduce sets to None and the code below actually removes nodes from the list + for level in &mut self.color_list { + level.retain(|c| c.is_some()); } } } 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(); - if let Some(color) = color { - out.put_pixel(x, y, *color); - } - } + 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 { + imgbuf.put_pixel(x, y, *color); } } } - out + + imgbuf } fn get_index_for_color

(&self, color: &P, node: &Rc>) -> Option