diff options
Diffstat (limited to 'src/renderer.rs')
-rw-r--r-- | src/renderer.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/renderer.rs b/src/renderer.rs index 74a321c..d7aefe4 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,3 +1,11 @@ +//! Actual rendering functions for heatmaps. +//! +//! We begin the rendering by using [`render_heatcounter`] to turn a list of GPX tracks into a +//! [`HeatCounter`], which is basically a grayscale heatmap, where each pixel represents the number +//! of tracks that goes through this pixel. +//! +//! We then render the colored heatmap tiles using [`lazy_colorization`], which provides us with +//! colorful PNG data. use std::thread; use color_eyre::{eyre::Result, Report}; @@ -10,13 +18,18 @@ use super::{ layer::{self, TileLayer}, }; +/// Represents a fully rendered tile. #[derive(Debug, Clone)] pub struct RenderedTile { + /// The `x` coordinate of the tile. pub x: u64, + /// The `y` coordinate of the tile. pub y: u64, + /// The encoded (PNG) image data, ready to be saved to disk. pub data: Vec<u8>, } +/// Type for the intermediate heat counters. pub type HeatCounter = TileLayer<Luma<u8>>; fn render_circle<P: Pixel>(layer: &mut TileLayer<P>, center: (u64, u64), radius: u64, pixel: P) { @@ -138,7 +151,11 @@ fn colorize_tile(tile: &ImageBuffer<Luma<u8>, Vec<u8>>, max: u32) -> RgbaImage { /// Lazily colorizes a [`HeatCounter`] by colorizing it tile-by-tile and saving a tile before /// rendering the next one. /// -/// This has a way lower memory usage than [`colorize_heatcounter`]. +/// This function calls the given callback with each rendered tile, and the function is responsible +/// for saving it. If the callback returns an `Err(...)`, the error is passed through. +/// +/// Note that this function internally uses `rayon` for parallization. If you want to limit the +/// number of threads used, set up the global [`rayon::ThreadPool`] first. pub fn lazy_colorization<F: FnMut(RenderedTile) -> Result<()> + Send>( layer: HeatCounter, mut save_callback: F, @@ -177,6 +194,10 @@ pub fn lazy_colorization<F: FnMut(RenderedTile) -> Result<()> + Send>( } /// Renders the heat counter for the given zoom level and track points. +/// +/// The given callback will be called when a track has been rendered and merged into the +/// accumulator, to allow for UI feedback. The passed parameter is the number of tracks that have +/// been rendered since the last call. pub fn render_heatcounter<F: Fn(usize) + Send + Sync>( zoom: u32, tracks: &[Vec<Coordinates>], |