aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/allRGB_alter.rs144
-rw-r--r--examples/allRGB_diamon.rs121
-rw-r--r--examples/allRGB_diamon_fixed.rs107
-rw-r--r--examples/allRGB_radial.rs109
-rw-r--r--examples/allRGB_randomwalk.rs108
-rw-r--r--examples/allRGB_square.rs117
6 files changed, 0 insertions, 706 deletions
diff --git a/examples/allRGB_alter.rs b/examples/allRGB_alter.rs
deleted file mode 100644
index 2326691..0000000
--- a/examples/allRGB_alter.rs
+++ /dev/null
@@ -1,144 +0,0 @@
-use coloctree::Point;
-use coloctree::{Color, OctreeNode};
-
-use image;
-
-fn mean_8neighbors_color(current_image: &Vec<Vec<Option<Color>>>, point: Point) -> Color {
- let mut nb_point = 0;
- let mut r: u64 = 0;
- let mut g: u64 = 0;
- let mut b: u64 = 0;
-
- for neighbors in point.neighbors_8() {
- if (neighbors.x as usize) < current_image.len()
- && (neighbors.y as usize) < current_image[0].len()
- {
- if let Some(c) = current_image[neighbors.x as usize][neighbors.y as usize] {
- nb_point += 1;
-
- r += c.r as u64;
- g += c.g as u64;
- b += c.b as u64;
- }
- }
- }
- if nb_point == 0 {
- Color { r: 0, g: 0, b: 0 }
- } else {
- Color {
- r: (r / nb_point) as u8,
- g: (g / nb_point) as u8,
- b: (b / nb_point) as u8,
- }
- }
-}
-fn to_index(point: &Point, size_y: usize) -> usize {
- point.x as usize * size_y + point.y as usize
-}
-
-fn main() {
- let img_x = 4096 as usize;
- let img_y = 4096 as usize;
-
- // Initialise the Octree
- let mut octree_root = OctreeNode::new(
- Color { r: 0, g: 0, b: 0 },
- Color {
- r: 255,
- g: 255,
- b: 255,
- },
- );
-
- let mut nb_points_done = 0;
-
- let mut image_colors: Vec<Vec<Option<Color>>> = vec![vec![None; img_x]; img_y];
- let mut points_done: Vec<bool> = vec![false; img_x * img_y];
- let mut points_todo: Vec<Point> = Vec::new();
-
- // The first color
- let init_color = Color {
- r: 255,
- g: 255,
- b: 255,
- };
- // The first point
- let init_point = Point { x: 2048, y: 2048 };
-
- // The final filename
- let output_filename = "all_rgb_alter.png";
-
- // Add the first point to the list of point to-do AND add it to the list of points "done" (to not do again) (and color it for the median color function)
- points_todo.push(init_point);
- points_done[init_point.x as usize * img_y + init_point.y as usize] = true;
- image_colors[init_point.x as usize][init_point.y as usize] = Some(init_color);
-
- // To make a all RGB image, add every color to the octree
- for r in 0..=255 {
- for g in 0..=255 {
- for b in 0..=255 {
- octree_root.insert(Color { r, g, b })
- }
- }
- }
-
- let mut img = image::RgbImage::new(img_x as u32, img_y as u32);
-
- /*
- * NOTE: The algorithm is:
- * 1) Using 2 buffers:
- * - Add the pixels to process to the temporary one
- * - Swap them when the active buffer run out of prixels to process
- * 2) Use the active buffer as a queue to select the nearest color available to the current mean color
- * - The current mean color is calculated as the mean of already put pixels
- * 3) Colorise that pixel
- * 4) Remove the used pixel to the available colors
- * 5) Add the 8-neighbors to the temporary buffer.
- */
-
- while !points_todo.is_empty() {
- let mut border = Vec::new();
- std::mem::swap(&mut border, &mut points_todo);
-
- while !border.is_empty() {
- let current_point = border.pop().unwrap();
-
- nb_points_done += 1;
-
- let mean_color_neighbors = mean_8neighbors_color(&image_colors, current_point);
- let color = octree_root.find_approximate_nearest_neighbor(&mean_color_neighbors);
- match color {
- Some(color) => {
- image_colors[current_point.x as usize][current_point.y as usize] = Some(color);
- octree_root.remove(color);
-
- for neighbors in current_point.neighbors_4() {
- let index = to_index(&neighbors, 4096);
- if neighbors.x < 4096 && neighbors.y < 4096 {
- if !points_done[index] {
- points_done[index] = true;
- points_todo.push(neighbors);
- }
- }
- }
- }
- None => {
- let finalname_bug = format!("{}.part.png", output_filename);
- img.save(finalname_bug).unwrap();
- println!("{} colors used", nb_points_done);
- panic!("No nearest point found!");
- }
- }
- }
- }
-
- for x in 0..img_x {
- for y in 0..img_y {
- if let Some(pixel) = image_colors[x][y] {
- img.put_pixel(x as u32, y as u32, image::Rgb([pixel.r, pixel.g, pixel.b]));
- }
- }
- }
-
- img.save(output_filename).unwrap();
-}
diff --git a/examples/allRGB_diamon.rs b/examples/allRGB_diamon.rs
deleted file mode 100644
index 996250d..0000000
--- a/examples/allRGB_diamon.rs
+++ /dev/null
@@ -1,121 +0,0 @@
-use coloctree::Point;
-use coloctree::{Color, OctreeNode};
-
-use rand::prelude::*;
-use rand::seq::SliceRandom;
-
-use image;
-
-fn to_index(point: &Point, size_y: usize) -> usize {
- point.x as usize * size_y + point.y as usize
-}
-fn main() {
- let img_x = 4096 as usize;
- let img_y = 4096 as usize;
-
- // Initialise the Octree
- let mut octree_root = OctreeNode::new(
- Color { r: 0, g: 0, b: 0 },
- Color {
- r: 255,
- g: 255,
- b: 255,
- },
- );
-
- let mut nb_points_done = 0;
-
- let seed = rand::random();
- let mut rng = SmallRng::seed_from_u64(seed);
- println!("Rng seed: {}", seed);
-
- let mut points_done: Vec<bool> = vec![false; img_x * img_y];
- let mut points_todo: Vec<(Point, Color)> = Vec::new();
-
- // The first color (default white)
- let init_color = Color {
- r: 255,
- g: 255,
- b: 255,
- };
-
- // The first point
- let init_point = Point { x: 2048, y: 2048 };
-
- // The final filename
- let output_filename = "all_rgb_diamon.png";
-
- // Add the first point to the list of point todo AND add it to the list of points "done"
- points_todo.push((init_point, init_color));
- points_done[init_point.x as usize * img_y + init_point.y as usize] = true;
-
- // To make a all RGB image, add every color to the octree
- for r in 0..=255 {
- for g in 0..=255 {
- for b in 0..=255 {
- octree_root.insert(Color { r, g, b })
- }
- }
- }
-
- let mut img = image::RgbImage::new(img_x as u32, img_y as u32);
-
- /*
- * NOTE: The algorithm is:
- * 1) Using 2 buffers:
- * - Add the pixels to process to the temporary one
- * - Swap them when the active buffer run out of prixels to process
- * - Optionnaly shuffle the active buffer after the swap
- * 2) Select a random pixel to process in the active buffers
- * 3) Select the nearest color available to parent pixel
- * 4) Colorise that pixel
- * 5) Remove the used pixel to the available colors
- * 6) Add the 4-neighbors to the temporary buffer.
- */
- while !points_todo.is_empty() {
- let mut border = Vec::new();
- std::mem::swap(&mut border, &mut points_todo);
-
- // NOTE: Shuffle it to add some variations, not mandatory
- border.shuffle(&mut rng);
-
- while !border.is_empty() {
- let index = (rng.random::<f32>() * border.len() as f32).floor() as usize;
- let last_index = border.len() - 1;
- border.swap(index, last_index);
- let (current_point, parent_color) = border.pop().unwrap();
-
- nb_points_done += 1;
-
- let color = octree_root.find_nearest_neighbor(&parent_color);
- match color {
- Some(color) => {
- img.put_pixel(
- current_point.x as u32,
- current_point.y as u32,
- image::Rgb([color.r, color.g, color.b]),
- );
- octree_root.remove(color);
-
- for neighbors in current_point.neighbors_4() {
- let index = to_index(&neighbors, 4096);
- if neighbors.x < 4096 && neighbors.y < 4096 {
- if !points_done[index] {
- points_done[index] = true;
- points_todo.push((neighbors, color));
- }
- }
- }
- }
- None => {
- let finalname_bug = format!("{}.part.png", output_filename);
- img.save(finalname_bug).unwrap();
- println!("{} colors used", nb_points_done);
- panic!("No nearest point found!");
- }
- }
- }
- }
-
- img.save(output_filename).unwrap();
-}
diff --git a/examples/allRGB_diamon_fixed.rs b/examples/allRGB_diamon_fixed.rs
deleted file mode 100644
index 627cff5..0000000
--- a/examples/allRGB_diamon_fixed.rs
+++ /dev/null
@@ -1,107 +0,0 @@
-use coloctree::Point;
-use coloctree::{Color, OctreeNode};
-
-use image;
-
-fn to_index(point: &Point, size_y: usize) -> usize {
- point.x as usize * size_y + point.y as usize
-}
-fn main() {
- let img_x = 4096 as usize;
- let img_y = 4096 as usize;
-
- // Initialise the Octree
- let mut octree_root = OctreeNode::new(
- Color { r: 0, g: 0, b: 0 },
- Color {
- r: 255,
- g: 255,
- b: 255,
- },
- );
-
- let mut nb_points_done = 0;
-
- let mut points_done: Vec<bool> = vec![false; img_x * img_y];
- let mut points_todo: Vec<(Point, Color)> = Vec::new();
-
- // The first color
- let init_color = Color {
- r: 255,
- g: 255,
- b: 255,
- };
- // The first point
- let init_point = Point { x: 2048, y: 2048 };
-
- // The final filename
- let output_filename = "all_rgb_diamon_fixed.png";
-
- // Add the first point to the list of point todo AND add it to the list of points "done"
- points_todo.push((init_point, init_color));
- points_done[init_point.x as usize * img_y + init_point.y as usize] = true;
-
- // To make a all RGB image, add every color to the octree
- for r in 0..=255 {
- for g in 0..=255 {
- for b in 0..=255 {
- octree_root.insert(Color { r, g, b })
- }
- }
- }
-
- let mut img = image::RgbImage::new(img_x as u32, img_y as u32);
-
- /*
- * NOTE: The algorithm is:
- * 1) Using 2 buffers:
- * - Add the pixels to process to the temporary one
- * - Swap them when the active buffer run out of prixels to process
- * 2) Use the active buffer as a stack of pixels
- * 3) For the current pixel, **select the approximate nearest color** available to parent pixel
- * 4) Colorise that pixel
- * 5) Remove the used pixel to the available colors
- * 6) Add the 4-neighbors to the temporary buffer.
- */
- while !points_todo.is_empty() {
- let mut border = Vec::new();
- std::mem::swap(&mut border, &mut points_todo);
-
- while !border.is_empty() {
- let (current_point, parent_color) = border.pop().unwrap();
-
- nb_points_done += 1;
-
- // NOTE: Use the approximate nearest color to not be exact and generate straight lines
- let color = octree_root.find_approximate_nearest_neighbor(&parent_color);
- match color {
- Some(color) => {
- img.put_pixel(
- current_point.x as u32,
- current_point.y as u32,
- image::Rgb([color.r, color.g, color.b]),
- );
- octree_root.remove(color);
-
- for neighbors in current_point.neighbors_4() {
- let index = to_index(&neighbors, 4096);
- if neighbors.x < 4096 && neighbors.y < 4096 {
- if !points_done[index] {
- points_done[index] = true;
- points_todo.push((neighbors, color));
- }
- }
- }
- }
- None => {
- let finalname_bug = format!("{}.part.png", output_filename);
- img.save(finalname_bug).unwrap();
- println!("{} colors used", nb_points_done);
- panic!("No nearest point found!");
- }
- }
- }
- }
-
- img.save(output_filename).unwrap();
-}
diff --git a/examples/allRGB_radial.rs b/examples/allRGB_radial.rs
deleted file mode 100644
index 663a60f..0000000
--- a/examples/allRGB_radial.rs
+++ /dev/null
@@ -1,109 +0,0 @@
-use coloctree::Point;
-use coloctree::{Color, OctreeNode};
-
-use rand::prelude::*;
-
-use image;
-
-fn to_index(point: &Point, size_y: usize) -> usize {
- point.x as usize * size_y + point.y as usize
-}
-fn main() {
- let img_x = 4096 as usize;
- let img_y = 4096 as usize;
-
- // Initialise the Octree
- let mut octree_root = OctreeNode::new(
- Color { r: 0, g: 0, b: 0 },
- Color {
- r: 255,
- g: 255,
- b: 255,
- },
- );
-
- let mut nb_points_done = 0;
-
- let seed = rand::random();
- let mut rng = SmallRng::seed_from_u64(seed);
- println!("Rng seed: {}", seed);
-
- let mut points_done: Vec<bool> = vec![false; img_x * img_y];
- let mut border: Vec<(Point, Color)> = Vec::new();
-
- // The first color
- let init_color = Color {
- r: 255,
- g: 255,
- b: 255,
- };
- // The first point
- let init_point = Point { x: 2048, y: 2048 };
-
- // The final filename
- let output_filename = "all_rgb_radial.png";
-
- // Add the first point to the list of point todo AND add it to the list of points "done"
- border.push((init_point, init_color));
- points_done[init_point.x as usize * img_y + init_point.y as usize] = true;
-
- // To make a all RGB image, add every color to the octree
- for r in 0..=255 {
- for g in 0..=255 {
- for b in 0..=255 {
- octree_root.insert(Color { r, g, b })
- }
- }
- }
-
- let mut img = image::RgbImage::new(img_x as u32, img_y as u32);
-
- /*
- * NOTE: The idea is simple:
- * 1) From the border while there is still pixel in the border
- * 2) Select the nearest color available to the "parent" pixel color
- * 3) Colorise that pixel
- * 4) Remove the used pixel to the available colors
- * 5) Add the 4-neighbors (can also us the 8-neighbors) to the borders list
- */
-
- while !border.is_empty() {
- let index = (rng.random::<f32>() * border.len() as f32).floor() as usize;
- let last_index = border.len() - 1;
- border.swap(index, last_index);
- let (current_point, parent_color) = border.pop().unwrap();
-
- nb_points_done += 1;
-
- let color = octree_root.find_nearest_neighbor(&parent_color);
- match color {
- Some(color) => {
- img.put_pixel(
- current_point.x as u32,
- current_point.y as u32,
- image::Rgb([color.r, color.g, color.b]),
- );
- octree_root.remove(color);
-
- // NOTE: Switch to neighbors_8 for another kind of "color explosion"
- for neighbors in current_point.neighbors_4() {
- let index = to_index(&neighbors, 4096);
- if neighbors.x < 4096 && neighbors.y < 4096 {
- if !points_done[index] {
- points_done[index] = true;
- border.push((neighbors, color));
- }
- }
- }
- }
- None => {
- let finalname_bug = format!("{}.part.png", output_filename);
- img.save(finalname_bug).unwrap();
- println!("{} colors used", nb_points_done);
- panic!("No nearest point found!");
- }
- }
- }
-
- img.save(output_filename).unwrap();
-}
diff --git a/examples/allRGB_randomwalk.rs b/examples/allRGB_randomwalk.rs
deleted file mode 100644
index f5d0d02..0000000
--- a/examples/allRGB_randomwalk.rs
+++ /dev/null
@@ -1,108 +0,0 @@
-use coloctree::Point;
-use coloctree::{Color, OctreeNode};
-
-use rand::prelude::*;
-use rand::seq::SliceRandom;
-
-// WARN: It takes some times to run!
-// About 100s on my computer
-
-use image;
-
-fn to_index(point: &Point, size_y: usize) -> usize {
- point.x as usize * size_y + point.y as usize
-}
-fn main() {
- let img_x = 4096 as usize;
- let img_y = 4096 as usize;
-
- // Initialise the Octree
- let mut octree_root = OctreeNode::new(
- Color { r: 0, g: 0, b: 0 },
- Color {
- r: 255,
- g: 255,
- b: 255,
- },
- );
-
- let mut nb_points_done = 0;
-
- let mut points_done: Vec<bool> = vec![false; img_x * img_y];
- let mut border: Vec<(Point, Color)> = Vec::new();
-
- // The first color
- let init_color = Color { r: 0, g: 0, b: 0 };
- // The first point
- let init_point = Point { x: 2048, y: 2048 };
-
- // The final filename
- let seed = rand::random();
- let mut rng = SmallRng::seed_from_u64(seed);
- println!("Rng seed: {}", seed);
-
- let output_filename = "all_rgb_randomwalk.png";
-
- // Add the first point to the list of point todo AND add it to the list of points "done"
- border.push((init_point, init_color));
- points_done[init_point.x as usize * img_y + init_point.y as usize] = true;
-
- // To make a all RGB image, fill the octree of every color
- for r in 0..=255 {
- for g in 0..=255 {
- for b in 0..=255 {
- octree_root.insert(Color { r, g, b })
- }
- }
- }
-
- // Initialise the output image
- let mut img = image::RgbImage::new(img_x as u32, img_y as u32);
-
- /*
- * NOTE: The idea is simple:
- * 1) We process the pixel of the image one by one from the border
- * 2) For that pixel, select the nearest color available to the "parent" pixel color
- * 3) Remove the used pixel to the available colors
- * 4) Add the 8-neighbors to the borders queue but **in a random order!**
- *
- * The is some subtilities to the last (add only neighbors that are valid in the image AND that
- * are not already done OR in the border queue.)
- */
- while !border.is_empty() {
- nb_points_done += 1;
- let (current_point, parent_color) = border.pop().unwrap();
-
- let color = octree_root.find_approximate_nearest_neighbor(&parent_color);
- match color {
- Some(color) => {
- img.put_pixel(
- current_point.x,
- current_point.y,
- image::Rgb([color.r, color.g, color.b]),
- );
- octree_root.remove(color);
-
- let mut neighbors_8 = current_point.neighbors_8();
- neighbors_8.shuffle(&mut rng);
- for neighbors in neighbors_8 {
- if neighbors.x < (img_x as u32) && neighbors.y < (img_y as u32) {
- let index = to_index(&neighbors, img_y);
- if !points_done[index] {
- points_done[index] = true;
- border.push((neighbors, color));
- }
- }
- }
- }
- None => {
- let finalname_bug = format!("{}.part.png", output_filename);
- img.save(finalname_bug).unwrap();
- println!("{} colors used", nb_points_done);
- panic!("No nearest point found!");
- }
- }
- }
-
- img.save(output_filename).unwrap();
-}
diff --git a/examples/allRGB_square.rs b/examples/allRGB_square.rs
deleted file mode 100644
index fb0211d..0000000
--- a/examples/allRGB_square.rs
+++ /dev/null
@@ -1,117 +0,0 @@
-use coloctree::Point;
-use coloctree::{Color, OctreeNode};
-
-use rand::prelude::*;
-
-use image;
-
-fn to_index(point: &Point, size_y: usize) -> usize {
- point.x as usize * size_y + point.y as usize
-}
-fn main() {
- let img_x = 4096 as usize;
- let img_y = 4096 as usize;
-
- // Initialise the Octree
- let mut octree_root = OctreeNode::new(
- Color { r: 0, g: 0, b: 0 },
- Color {
- r: 255,
- g: 255,
- b: 255,
- },
- );
-
- let mut nb_points_done = 0;
-
- let seed = rand::random();
- let mut rng = SmallRng::seed_from_u64(seed);
- println!("Rng seed: {}", seed);
-
- let mut points_done: Vec<bool> = vec![false; img_x * img_y];
- let mut points_todo: Vec<(Point, Color)> = Vec::new();
-
- // The first color (default white)
- let init_color = Color {
- r: 255,
- g: 255,
- b: 255,
- };
-
- // The first point
- let init_point = Point { x: 2048, y: 2048 };
-
- // The final filename
- let output_filename = "all_rgb_square.png";
-
- // Add the first point to the list of point todo AND add it to the list of points "done"
- points_todo.push((init_point, init_color));
- points_done[init_point.x as usize * img_y + init_point.y as usize] = true;
-
- // To make a all RGB image, add every color to the octree
- for r in 0..=255 {
- for g in 0..=255 {
- for b in 0..=255 {
- octree_root.insert(Color { r, g, b })
- }
- }
- }
-
- let mut img = image::RgbImage::new(img_x as u32, img_y as u32);
-
- /*
- * NOTE: The algorithm is:
- * 1) Using 2 buffers:
- * - Add the pixels to process to the temporary one
- * - Swap them when the active buffer run out of prixels to process
- * - Optionnaly shuffle the active buffer after the swap
- * 2) Select a random pixel to process in the active buffers
- * 3) Select the nearest color available to parent pixel
- * 4) Colorise that pixel
- * 5) Remove the used pixel to the available colors
- * 6) Add the **8-neighbors** to the temporary buffer.
- */
- while !points_todo.is_empty() {
- let mut border = Vec::new();
- std::mem::swap(&mut border, &mut points_todo);
-
- while !border.is_empty() {
- let index = (rng.random::<f32>() * border.len() as f32).floor() as usize;
- let last_index = border.len() - 1;
- border.swap(index, last_index);
- let (current_point, parent_color) = border.pop().unwrap();
-
- nb_points_done += 1;
-
- let color = octree_root.find_approximate_nearest_neighbor(&parent_color);
- match color {
- Some(color) => {
- img.put_pixel(
- current_point.x as u32,
- current_point.y as u32,
- image::Rgb([color.r, color.g, color.b]),
- );
- octree_root.remove(color);
-
- for neighbors in current_point.neighbors_8() {
- let index = to_index(&neighbors, 4096);
- if neighbors.x < 4096 && neighbors.y < 4096 {
- if !points_done[index] {
- points_done[index] = true;
- points_todo.push((neighbors, color));
- }
- }
- }
- }
- None => {
- let finalname_bug = format!("{}.part.png", output_filename);
- img.save(finalname_bug).unwrap();
- println!("{} colors used", nb_points_done);
- panic!("No nearest point found!");
- }
- }
- }
- }
-
- img.save(output_filename).unwrap();
-}