Skip to content

Commit

Permalink
[nasadem] make image a feautre
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Jan 15, 2025
1 parent de6991a commit 4ccdf82
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ clap = { version = "4.4.2", features = ["derive"] }
criterion = { version = "0.5", features = ["html_reports"] }
dashmap = "5.5.3"
geo = "0.26.0"
image = "0"
log = "0.4.20"
memmap2 = "0.7.1"
num-traits = "0.2.16"
Expand Down
5 changes: 5 additions & 0 deletions nasadem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ readme = "README.md"
repository = "https://github.com/jaykickliter/geoprof"
version = "0.1.0"

[features]
default = []
image = ["dep:image"]

[dependencies]
byteorder = { workspace = true }
geo = { workspace = true }
memmap2 = { workspace = true }
thiserror = { workspace = true }
image = { workspace = true, optional = true }
25 changes: 25 additions & 0 deletions nasadem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use geo::{
geometry::{Coord, Polygon},
polygon,
};
#[cfg(feature = "image")]
use image::{ImageBuffer, Luma};
use memmap2::Mmap;
use std::{
fs::File,
Expand Down Expand Up @@ -307,6 +309,29 @@ impl Tile {
(x: w, y: s),
]
}

/// Returns an [image] of this tile.
///
/// [image]: image::ImageBuffer
#[cfg(feature = "image")]
// https://docs.rs/image/latest/image/struct.ImageBuffer.html
pub fn to_image(&self) -> ImageBuffer<Luma<u16>, Vec<u16>> {
let (x_dim, y_dim) = self.dimensions();
let mut img = ImageBuffer::new(x_dim as u32, y_dim as u32);
let min_elev = self.min_elevation() as f32;
let max_elev = self.max_elevation() as f32;
let scale = |m: i16| {
let m = m as f32;
(m - min_elev) / (max_elev - min_elev) * u16::MAX as f32
};
for sample in self.iter() {
let (x, y) = sample.index();
let elev = sample.elevation();
let scaled_elev = scale(elev);
img.put_pixel(x as u32, (y_dim - 1 - y) as u32, Luma([scaled_elev as u16]))
}
img
}
}

/// Private API
Expand Down
4 changes: 2 additions & 2 deletions nasadem/zorderize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ edition = "2021"
[dependencies]
clap = { workspace = true }
glob = "0"
image = "0"
nasadem = { path = ".." }
image = { workspace = true }
nasadem = { path = "..", features = ["image"] }
20 changes: 1 addition & 19 deletions nasadem/zorderize/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,6 @@ struct Cmd {
dest: Option<PathBuf>,
}

fn tile_to_img(tile: &Tile) -> ImageBuffer<Luma<u16>, Vec<u16>> {
let (x_dim, y_dim) = tile.dimensions();
let mut img = ImageBuffer::new(x_dim as u32, y_dim as u32);
let min_elev = tile.min_elevation() as f32;
let max_elev = tile.max_elevation() as f32;
let scale = |m: i16| {
let m = m as f32;
(m - min_elev) / (max_elev - min_elev) * u16::MAX as f32
};
for sample in tile.iter() {
let (x, y) = sample.index();
let elev = sample.elevation();
let scaled_elev = scale(elev);
img.put_pixel(x as u32, (y_dim - 1 - y) as u32, Luma([scaled_elev as u16]))
}
img
}

fn scale_to_pow2(img: &ImageBuffer<Luma<u16>, Vec<u16>>) -> ImageBuffer<Luma<u16>, Vec<u16>> {
let (scaled_x, scaled_y) = match img.dimensions() {
(1201, 1201) => (1024, 1024),
Expand All @@ -44,7 +26,7 @@ fn scale_to_pow2(img: &ImageBuffer<Luma<u16>, Vec<u16>>) -> ImageBuffer<Luma<u16
fn main() {
let cli = Cmd::parse();
let tile = Tile::load(&cli.tile).unwrap();
let img = tile_to_img(&tile);
let img = tile.to_image();
let scaled = scale_to_pow2(&img);
let out = cli.dest.unwrap_or_else(|| {
let mut out = cli.tile.clone();
Expand Down

0 comments on commit 4ccdf82

Please sign in to comment.