From def44c6e969f1027da82e4130a82db7e4916ba86 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 12 Jan 2023 22:35:44 +0100 Subject: clean up some unused code Some of it was written because it fit the API, but we didn't end up using it in main.rs. --- src/layer.rs | 116 ++++---------------------------------------------------- src/renderer.rs | 40 ++++++------------- 2 files changed, 19 insertions(+), 137 deletions(-) diff --git a/src/layer.rs b/src/layer.rs index 74c36e0..a8057a0 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -3,60 +3,36 @@ //! This supports OSM-style "tiled" images, but not all of the tiles have to be present. If a tile //! is not present, a default pixel is returned. The tile is allocated with the first call to a //! mutating operation. -use std::{ - fs::{self, File}, - io::BufWriter, - path::Path, -}; +use std::{fs::File, io::BufWriter, path::Path}; -use color_eyre::eyre::{bail, Result}; +use color_eyre::eyre::Result; use fnv::FnvHashMap; use image::{ codecs::png::{CompressionType, FilterType, PngEncoder}, - ColorType, ImageBuffer, ImageEncoder, Pixel, Rgba, RgbaImage, + ColorType, ImageBuffer, ImageEncoder, Pixel, RgbaImage, }; use num_traits::Zero; pub const TILE_HEIGHT: u64 = 256; pub const TILE_WIDTH: u64 = 256; +type TileIndex = (u64, u64); + /// Main "lazy image buffer" struct. #[derive(Debug, Clone)] pub struct TileLayer { - tiles: FnvHashMap<(u64, u64), ImageBuffer>>, + tiles: FnvHashMap>>, default_pixel: P, - width: u64, - height: u64, } impl TileLayer

{ - pub fn from_pixel(width: u64, height: u64, pixel: P) -> Self { + pub fn from_pixel(pixel: P) -> Self { TileLayer { tiles: Default::default(), default_pixel: pixel, - width, - height, } } - pub fn width(&self) -> u64 { - self.width - } - - pub fn height(&self) -> u64 { - self.height - } - - fn index(&self, x: u64, y: u64) -> ((u64, u64), (u32, u32)) { - ( - (x / TILE_WIDTH, y / TILE_HEIGHT), - ( - (x % TILE_WIDTH).try_into().unwrap(), - (y % TILE_HEIGHT).try_into().unwrap(), - ), - ) - } - pub fn enumerate_tiles( &self, ) -> impl Iterator>)> { @@ -69,45 +45,6 @@ impl TileLayer

{ }) } - pub fn tile_for_mut(&mut self, x: u64, y: u64) -> &mut ImageBuffer> { - let ((tx, ty), _) = self.index(x, y); - self.tile_mut(tx, ty) - } - - pub fn get_pixel_checked(&self, x: u64, y: u64) -> Option<&P> { - if x >= self.width || y >= self.height { - return None; - } - - let (outer_idx, (inner_x, inner_y)) = self.index(x, y); - self.tiles - .get(&outer_idx) - .map(|tile| tile.get_pixel(inner_x, inner_y)) - .or(Some(&self.default_pixel)) - } - - pub fn get_pixel(&self, x: u64, y: u64) -> &P { - // This is kinda cheating, but we care about the API for now, not the speed. We can - // optimize this later. - self.get_pixel_checked(x, y).unwrap() - } - - pub fn get_pixel_mut_checked(&mut self, x: u64, y: u64) -> Option<&mut P> { - if x >= self.width || y >= self.height { - return None; - } - - let ((outer_x, outer_y), (inner_x, inner_y)) = self.index(x, y); - Some( - self.tile_mut(outer_x, outer_y) - .get_pixel_mut(inner_x, inner_y), - ) - } - - pub fn get_pixel_mut(&mut self, x: u64, y: u64) -> &mut P { - self.get_pixel_mut_checked(x, y).unwrap() - } - /// Enumerate all pixels that are explicitely set in this layer. pub fn enumerate_pixels(&self) -> impl Iterator { self.tiles.iter().flat_map(|((tx, ty), tile)| { @@ -121,27 +58,10 @@ impl TileLayer

{ }) } - /// Mutably enumerate all pixels that are explicitely set in this layer. - pub fn enumerate_pixels_mut(&mut self) -> impl Iterator { - self.tiles.iter_mut().flat_map(|((tx, ty), tile)| { - tile.enumerate_pixels_mut().map(move |(x, y, p)| { - ( - u64::from(x) + tx * TILE_WIDTH, - u64::from(y) + ty * TILE_HEIGHT, - p, - ) - }) - }) - } - pub fn pixels(&self) -> impl Iterator { self.enumerate_pixels().map(|x| x.2) } - pub fn pixels_mut(&mut self) -> impl Iterator { - self.enumerate_pixels_mut().map(|x| x.2) - } - pub fn tile_count(&self) -> usize { self.tiles.len() } @@ -184,32 +104,12 @@ impl TileLayer

{ } } -impl TileLayer> { - pub fn save_to_directory>(&self, path: S) -> Result<()> { - let path = path.as_ref(); - - for ((x, y), tile) in self.tiles.iter() { - let folder = path.join(&x.to_string()); - let metadata = folder.metadata(); - match metadata { - Err(_) => fs::create_dir(&folder)?, - Ok(m) if !m.is_dir() => bail!("Output path is not a directory"), - _ => {} - } - let file = folder.join(&format!("{y}.png")); - compress_png(tile, file)?; - } - - Ok(()) - } -} - pub fn compress_png>(image: &RgbaImage, path: P) -> Result<()> { let outstream = BufWriter::new(File::create(path)?); let encoder = PngEncoder::new_with_quality(outstream, CompressionType::Best, FilterType::Adaptive); - encoder.write_image(&image, image.width(), image.height(), ColorType::Rgba8)?; + encoder.write_image(image, image.width(), image.height(), ColorType::Rgba8)?; Ok(()) } diff --git a/src/renderer.rs b/src/renderer.rs index 6e17304..cece8f3 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,9 +1,8 @@ -use std::{fs, mem, path::Path}; +use std::{fs, path::Path}; use color_eyre::eyre::{bail, Result}; -use image::{ImageBuffer, Luma, Pixel, Rgba, RgbaImage}; +use image::{ImageBuffer, Luma, Pixel, RgbaImage}; use nalgebra::{vector, Vector2}; -use num_traits::identities::Zero; use super::{ gpx::Coordinates, @@ -12,8 +11,6 @@ use super::{ pub type HeatCounter = TileLayer>; -pub type HeatMap = TileLayer>; - fn render_circle(layer: &mut TileLayer

, center: (u64, u64), radius: u64, pixel: P) { let topleft = (center.0 - radius, center.1 - radius); let rad_32: u32 = radius.try_into().unwrap(); @@ -59,14 +56,14 @@ fn render_line( fn unsigned_add(a: Vector2, b: Vector2) -> Vector2 { let x = if b[0] < 0 { - a[0] - b[0].abs() as u64 + a[0] - u64::from(b[0].unsigned_abs()) } else { - a[0] + b[0] as u64 + a[0] + u64::try_from(b[0]).unwrap() }; let y = if b[1] < 0 { - a[1] - b[1].abs() as u64 + a[1] - u64::from(b[1].unsigned_abs()) } else { - a[1] + b[1] as u64 + a[1] + u64::try_from(b[1]).unwrap() }; vector![x, y] } @@ -130,19 +127,6 @@ fn colorize_tile(tile: &ImageBuffer, Vec>, max: u32) -> RgbaImage { result } -pub fn colorize_heatcounter(layer: &HeatCounter) -> HeatMap { - let max = layer.pixels().map(|l| l.0[0]).max().unwrap_or_default(); - let mut result = TileLayer::from_pixel(layer.width(), layer.height(), [0, 0, 0, 0].into()); - if max == 0 { - return result; - } - for (tile_x, tile_y, tile) in layer.enumerate_tiles() { - let colorized = colorize_tile(&tile, max.into()); - *result.tile_mut(tile_x, tile_y) = colorized; - } - result -} - /// Lazily colorizes a [`HeatCounter`] by colorizing it tile-by-tile and saving a tile before /// rendering the next one. /// @@ -159,15 +143,15 @@ pub fn lazy_colorization, F: FnMut(usize)>( } for (tile_x, tile_y, tile) in layer.enumerate_tiles() { - let colorized = colorize_tile(&tile, max.into()); - let folder = base_dir.join(&tile_x.to_string()); + let colorized = colorize_tile(tile, max.into()); + let folder = base_dir.join(tile_x.to_string()); let metadata = folder.metadata(); match metadata { Err(_) => fs::create_dir(&folder)?, Ok(m) if !m.is_dir() => bail!("Output path is not a directory"), _ => {} } - let file = folder.join(&format!("{tile_y}.png")); + let file = folder.join(format!("{tile_y}.png")); layer::compress_png(&colorized, file)?; progress_callback(1); } @@ -180,12 +164,10 @@ pub fn render_heatcounter( tracks: &[Vec], mut progress_callback: F, ) -> HeatCounter { - let size = 256 * 2u64.pow(zoom); - - let mut heatcounter = TileLayer::from_pixel(size, size, [0].into()); + let mut heatcounter = TileLayer::from_pixel([0].into()); for track in tracks { - let mut layer = TileLayer::from_pixel(size, size, [0].into()); + let mut layer = TileLayer::from_pixel([0].into()); let points = track .iter() -- cgit v1.2.3