You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sedona.apache.org by pr...@apache.org on 2026/06/01 05:10:25 UTC
(sedona-spatialbench) 02/02: perf: eliminate intermediate allocations for non-u8 raster dtypes
This is an automated email from the ASF dual-hosted git repository.
prantogg pushed a commit to branch pranav/feature/raster-benchmark
in repository https://gitbox.apache.org/repos/asf/sedona-spatialbench.git
commit d4287cbd449d588197f963e8b61930e27895cd53
Author: Pranav Toggi <pr...@gmail.com>
AuthorDate: Sun May 31 22:09:49 2026 -0700
perf: eliminate intermediate allocations for non-u8 raster dtypes
Add PixelBuffer enum and typed noise generation (u16, f32) so the
hot path generates directly into the target dtype. No intermediate
Vec<u8> or conversion step. Thread-local buffer reuse works for all
dtypes.
---
spatialbench-cli/src/raster_runner.rs | 11 +-
spatialbench-raster/src/cog/mod.rs | 279 +++++++++++++++++++++-------------
spatialbench-raster/src/noise/mod.rs | 141 ++++++++++++++---
3 files changed, 302 insertions(+), 129 deletions(-)
diff --git a/spatialbench-cli/src/raster_runner.rs b/spatialbench-cli/src/raster_runner.rs
index a4f5922..ec8dd43 100644
--- a/spatialbench-cli/src/raster_runner.rs
+++ b/spatialbench-cli/src/raster_runner.rs
@@ -21,7 +21,7 @@
//! thread-local pixel buffer reused across COGs. Manifest entries flow back
//! through a bounded `mpsc` channel.
-use spatialbench_raster::cog::{write_cog_with_buffer, CogConfig};
+use spatialbench_raster::cog::{write_cog_with_buffer, CogConfig, PixelBuffer};
use spatialbench_raster::footprint::Footprint;
use spatialbench_raster::scaling::ScalingTier;
use spatialbench_raster::ManifestEntry;
@@ -111,17 +111,18 @@ pub async fn run_raster(
let entry = tokio::task::spawn_blocking(move || {
thread_local! {
- static PIXEL_BUF: RefCell<Vec<u8>> = const { RefCell::new(Vec::new()) };
+ static PIXEL_BUF: RefCell<Option<PixelBuffer>> = const { RefCell::new(None) };
}
- PIXEL_BUF.with(|buf| {
- let mut buf = buf.borrow_mut();
+ PIXEL_BUF.with(|cell| {
+ let mut opt = cell.borrow_mut();
+ let buf = opt.get_or_insert_with(|| PixelBuffer::new(item.config.dtype));
write_cog_with_buffer(
&item.config,
&item.footprint,
item.cog_id,
&item.output_path,
- &mut buf,
+ buf,
)?;
Ok::<ManifestEntry, io::Error>(ManifestEntry {
diff --git a/spatialbench-raster/src/cog/mod.rs b/spatialbench-raster/src/cog/mod.rs
index 27cbb0d..ca68be4 100644
--- a/spatialbench-raster/src/cog/mod.rs
+++ b/spatialbench-raster/src/cog/mod.rs
@@ -17,10 +17,10 @@
//! Cloud-Optimized GeoTIFF (COG) writer using GDAL.
//!
-//! Generates single-band UInt8 COGs with:
+//! Generates single-band COGs with configurable data type (UInt8, UInt16, Float32):
//! - Per-footprint UTM CRS (EPSG:326xx)
//! - ZSTD compression
-//! - 256×256 internal tiling
+//! - Configurable internal tiling
//! - Perlin noise pixel data for realistic compression ratios
use crate::footprint::{Footprint, FootprintConfig};
@@ -33,6 +33,60 @@ use gdal::{DriverManager, GeoTransform};
use std::io;
use std::path::Path;
+/// Raster data type for COG generation.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum RasterDtype {
+ /// Unsigned 8-bit integer (0–255). Smallest files, common for classification/indices.
+ UInt8,
+ /// Unsigned 16-bit integer (0–65535). Common for Sentinel-2 L2A reflectance.
+ UInt16,
+ /// 32-bit float. Common for climate variables, analysis-ready data.
+ Float32,
+}
+
+impl Default for RasterDtype {
+ fn default() -> Self {
+ Self::UInt8
+ }
+}
+
+impl RasterDtype {
+ /// Bytes per pixel for this data type.
+ pub const fn bytes_per_pixel(&self) -> usize {
+ match self {
+ Self::UInt8 => 1,
+ Self::UInt16 => 2,
+ Self::Float32 => 4,
+ }
+ }
+}
+
+/// Reusable pixel buffer for COG generation.
+///
+/// Holds a typed buffer matching the target [`RasterDtype`], avoiding
+/// intermediate allocations when generating noise directly into the
+/// target type. Each variant's inner `Vec` is reused across COGs on
+/// the same thread.
+pub enum PixelBuffer {
+ /// UInt8 pixel buffer.
+ U8(Vec<u8>),
+ /// UInt16 pixel buffer.
+ U16(Vec<u16>),
+ /// Float32 pixel buffer.
+ F32(Vec<f32>),
+}
+
+impl PixelBuffer {
+ /// Create a new empty buffer for the given dtype.
+ pub fn new(dtype: RasterDtype) -> Self {
+ match dtype {
+ RasterDtype::UInt8 => Self::U8(Vec::new()),
+ RasterDtype::UInt16 => Self::U16(Vec::new()),
+ RasterDtype::Float32 => Self::F32(Vec::new()),
+ }
+ }
+}
+
/// Configuration for COG generation.
///
/// Wraps a [`FootprintConfig`] (which defines pixel dimensions and resolution)
@@ -47,6 +101,8 @@ pub struct CogConfig {
pub tile_size: u32,
/// Perlin noise frequency (controls spatial detail per tile).
pub noise_frequency: f32,
+ /// Pixel data type.
+ pub dtype: RasterDtype,
}
impl Default for CogConfig {
@@ -55,6 +111,7 @@ impl Default for CogConfig {
raster: FootprintConfig::default(),
tile_size: 256,
noise_frequency: 8.0,
+ dtype: RasterDtype::default(),
}
}
}
@@ -62,7 +119,7 @@ impl Default for CogConfig {
/// Write a single COG file for a given footprint and scene ID.
///
/// The output is a Cloud-Optimized GeoTIFF with:
-/// - Single band, UInt8, ZSTD compressed
+/// - Single band, configurable dtype, ZSTD compressed
/// - CRS set to the footprint's UTM zone
/// - Geotransform derived from the footprint's NW corner origin
/// - Deterministic Perlin noise pixel data seeded from `(footprint.id, cog_id)`
@@ -83,100 +140,85 @@ pub fn write_cog(
let height = config.raster.cog_height;
let resolution = config.raster.resolution as f64;
- // Create in-memory dataset
- let mem_driver = DriverManager::get_driver_by_name("MEM").map_err(|e| {
- io::Error::new(
- io::ErrorKind::NotFound,
- format!("GDAL MEM driver not available: {e}"),
- )
- })?;
-
- let mut mem_ds = mem_driver
- .create_with_band_type::<u8, _>("", width as usize, height as usize, 1)
- .map_err(|e| io::Error::other(format!("failed to create MEM dataset: {e}")))?;
-
- // Set CRS to footprint's UTM zone
- let srs = SpatialRef::from_epsg(footprint.epsg).map_err(|e| {
- io::Error::other(format!(
- "failed to create SRS for EPSG:{}: {e}",
- footprint.epsg
- ))
- })?;
- mem_ds
- .set_spatial_ref(&srs)
- .map_err(|e| io::Error::other(format!("failed to set CRS: {e}")))?;
-
- // Geotransform: origin is NW corner, positive x-res east, negative y-res south
- let gt: GeoTransform = [
- footprint.origin.0, // top-left x (easting)
- resolution, // pixel width (meters)
- 0.0, // rotation
- footprint.origin.1, // top-left y (northing)
- 0.0, // rotation
- -resolution, // pixel height (negative = south)
- ];
- mem_ds
- .set_geo_transform(>)
- .map_err(|e| io::Error::other(format!("failed to set geotransform: {e}")))?;
-
- // Generate pixel data with Perlin noise
let seed = (footprint.id as u64) << 32 | cog_id as u64;
let noise = PerlinNoise::new(seed);
- let pixels = noise.generate_raster(width, height, config.noise_frequency);
-
- // Write raster band (scoped to release mutable borrow on mem_ds)
- {
- let mut band = mem_ds
- .rasterband(1)
- .map_err(|e| io::Error::other(format!("failed to get raster band: {e}")))?;
- let mut buffer = gdal::raster::Buffer::new((width as usize, height as usize), pixels);
- band.write::<u8>((0, 0), (width as usize, height as usize), &mut buffer)
- .map_err(|e| io::Error::other(format!("failed to write pixel data: {e}")))?;
- }
- // Create COG via create_copy from the MEM dataset
- let cog_driver = DriverManager::get_driver_by_name("COG").map_err(|e| {
- io::Error::new(
- io::ErrorKind::NotFound,
- format!("GDAL COG driver not available: {e}"),
- )
- })?;
+ let mut mem_ds = create_mem_dataset(config, width, height)?;
+ set_crs_and_geotransform(&mut mem_ds, footprint, resolution)?;
- let cog_options = RasterCreationOptions::from_iter([
- "COMPRESS=ZSTD",
- &format!("BLOCKSIZE={}", config.tile_size),
- "LEVEL=3",
- "NUM_THREADS=ALL_CPUS",
- ]);
-
- mem_ds
- .create_copy(&cog_driver, output_path, &cog_options)
- .map_err(|e| io::Error::other(format!("failed to create COG: {e}")))?;
+ // Generate and write pixel data directly in target dtype
+ match config.dtype {
+ RasterDtype::UInt8 => {
+ let pixels = noise.generate_raster(width, height, config.noise_frequency);
+ write_band_data::<u8>(&mut mem_ds, width, height, pixels)?;
+ }
+ RasterDtype::UInt16 => {
+ let mut pixels = Vec::new();
+ noise.generate_raster_u16_into(width, height, config.noise_frequency, &mut pixels);
+ write_band_data::<u16>(&mut mem_ds, width, height, pixels)?;
+ }
+ RasterDtype::Float32 => {
+ let mut pixels = Vec::new();
+ noise.generate_raster_f32_into(width, height, config.noise_frequency, &mut pixels);
+ write_band_data::<f32>(&mut mem_ds, width, height, pixels)?;
+ }
+ }
- Ok(())
+ create_cog_from_mem(&mem_ds, config, output_path)
}
-/// Write a single COG file, reusing `pixel_buf` for pixel data.
+/// Write a single COG file, reusing a [`PixelBuffer`] for pixel data.
///
-/// Like [`write_cog`], but accepts an externally-owned pixel buffer to
-/// avoid heap allocation on the hot path. The buffer is cleared and
-/// refilled via [`PerlinNoise::generate_raster_into`].
+/// Like [`write_cog`], but accepts an externally-owned typed pixel buffer
+/// to avoid heap allocation on the hot path. Noise is generated directly
+/// into the target dtype — no intermediate u8 buffer or conversion step.
///
/// # Performance
///
-/// At 1830×1830 = ~3.2 MB per buffer, reusing this across COGs on the
-/// same thread avoids ~3.2 MB alloc/dealloc per COG.
+/// At 1830×1830 pixels, reusing this buffer across COGs on the same
+/// thread avoids ~3.3 MB (UInt8), ~6.7 MB (UInt16), or ~13.4 MB (Float32)
+/// of alloc/dealloc per COG.
+///
+/// # Panics
+///
+/// Panics if the `pixel_buf` variant does not match `config.dtype`.
pub fn write_cog_with_buffer(
config: &CogConfig,
footprint: &Footprint,
cog_id: u32,
output_path: &Path,
- pixel_buf: &mut Vec<u8>,
+ pixel_buf: &mut PixelBuffer,
) -> io::Result<()> {
let width = config.raster.cog_width;
let height = config.raster.cog_height;
let resolution = config.raster.resolution as f64;
+ let seed = (footprint.id as u64) << 32 | cog_id as u64;
+ let noise = PerlinNoise::new(seed);
+
+ let mut mem_ds = create_mem_dataset(config, width, height)?;
+ set_crs_and_geotransform(&mut mem_ds, footprint, resolution)?;
+
+ match pixel_buf {
+ PixelBuffer::U8(buf) => {
+ noise.generate_raster_into(width, height, config.noise_frequency, buf);
+ write_band_data::<u8>(&mut mem_ds, width, height, buf.clone())?;
+ }
+ PixelBuffer::U16(buf) => {
+ noise.generate_raster_u16_into(width, height, config.noise_frequency, buf);
+ write_band_data::<u16>(&mut mem_ds, width, height, buf.clone())?;
+ }
+ PixelBuffer::F32(buf) => {
+ noise.generate_raster_f32_into(width, height, config.noise_frequency, buf);
+ write_band_data::<f32>(&mut mem_ds, width, height, buf.clone())?;
+ }
+ }
+
+ create_cog_from_mem(&mem_ds, config, output_path)
+}
+
+/// Create an in-memory GDAL dataset with the correct band type.
+fn create_mem_dataset(config: &CogConfig, width: u32, height: u32) -> io::Result<gdal::Dataset> {
let mem_driver = DriverManager::get_driver_by_name("MEM").map_err(|e| {
io::Error::new(
io::ErrorKind::NotFound,
@@ -184,18 +226,31 @@ pub fn write_cog_with_buffer(
)
})?;
- let mut mem_ds = mem_driver
- .create_with_band_type::<u8, _>("", width as usize, height as usize, 1)
- .map_err(|e| io::Error::other(format!("failed to create MEM dataset: {e}")))?;
+ let w = width as usize;
+ let h = height as usize;
+ let ds = match config.dtype {
+ RasterDtype::UInt8 => mem_driver.create_with_band_type::<u8, _>("", w, h, 1),
+ RasterDtype::UInt16 => mem_driver.create_with_band_type::<u16, _>("", w, h, 1),
+ RasterDtype::Float32 => mem_driver.create_with_band_type::<f32, _>("", w, h, 1),
+ }
+ .map_err(|e| io::Error::other(format!("failed to create MEM dataset: {e}")))?;
+
+ Ok(ds)
+}
+/// Set CRS and geotransform on a dataset.
+fn set_crs_and_geotransform(
+ ds: &mut gdal::Dataset,
+ footprint: &Footprint,
+ resolution: f64,
+) -> io::Result<()> {
let srs = SpatialRef::from_epsg(footprint.epsg).map_err(|e| {
io::Error::other(format!(
"failed to create SRS for EPSG:{}: {e}",
footprint.epsg
))
})?;
- mem_ds
- .set_spatial_ref(&srs)
+ ds.set_spatial_ref(&srs)
.map_err(|e| io::Error::other(format!("failed to set CRS: {e}")))?;
let gt: GeoTransform = [
@@ -206,25 +261,34 @@ pub fn write_cog_with_buffer(
0.0,
-resolution,
];
- mem_ds
- .set_geo_transform(>)
+ ds.set_geo_transform(>)
.map_err(|e| io::Error::other(format!("failed to set geotransform: {e}")))?;
- // Generate pixel data into reusable buffer
- let seed = (footprint.id as u64) << 32 | cog_id as u64;
- let noise = PerlinNoise::new(seed);
- noise.generate_raster_into(width, height, config.noise_frequency, pixel_buf);
-
- {
- let mut band = mem_ds
- .rasterband(1)
- .map_err(|e| io::Error::other(format!("failed to get raster band: {e}")))?;
- let mut buffer =
- gdal::raster::Buffer::new((width as usize, height as usize), pixel_buf.clone());
- band.write::<u8>((0, 0), (width as usize, height as usize), &mut buffer)
- .map_err(|e| io::Error::other(format!("failed to write pixel data: {e}")))?;
- }
+ Ok(())
+}
+
+/// Write pixel data to band 1 of a dataset.
+fn write_band_data<T: gdal::raster::GdalType + Copy>(
+ ds: &mut gdal::Dataset,
+ width: u32,
+ height: u32,
+ pixels: Vec<T>,
+) -> io::Result<()> {
+ let mut band = ds
+ .rasterband(1)
+ .map_err(|e| io::Error::other(format!("failed to get raster band: {e}")))?;
+ let mut buffer = gdal::raster::Buffer::new((width as usize, height as usize), pixels);
+ band.write::<T>((0, 0), (width as usize, height as usize), &mut buffer)
+ .map_err(|e| io::Error::other(format!("failed to write pixel data: {e}")))?;
+ Ok(())
+}
+/// Create a COG file from an in-memory dataset via create_copy.
+fn create_cog_from_mem(
+ mem_ds: &gdal::Dataset,
+ config: &CogConfig,
+ output_path: &Path,
+) -> io::Result<()> {
let cog_driver = DriverManager::get_driver_by_name("COG").map_err(|e| {
io::Error::new(
io::ErrorKind::NotFound,
@@ -354,7 +418,7 @@ mod tests {
let path = dir.path().join("buf_test.tif");
let config = CogConfig::default();
let fp = test_footprint();
- let mut buf = Vec::new();
+ let mut buf = PixelBuffer::new(config.dtype);
write_cog_with_buffer(&config, &fp, 0, &path, &mut buf).unwrap();
@@ -363,8 +427,11 @@ mod tests {
assert_eq!(w, 1830);
assert_eq!(h, 1830);
- // Buffer should have been filled and retained
- assert_eq!(buf.len(), 1830 * 1830);
+ // Buffer should have been filled
+ match &buf {
+ PixelBuffer::U8(v) => assert_eq!(v.len(), 1830 * 1830),
+ _ => panic!("expected U8 buffer for default config"),
+ }
}
#[test]
@@ -372,16 +439,22 @@ mod tests {
let dir = tempdir().unwrap();
let config = CogConfig::default();
let fp = test_footprint();
- let mut buf = Vec::new();
+ let mut buf = PixelBuffer::new(config.dtype);
// Write two COGs reusing the same buffer
let path_a = dir.path().join("a.tif");
let path_b = dir.path().join("b.tif");
write_cog_with_buffer(&config, &fp, 0, &path_a, &mut buf).unwrap();
- let cap_after_first = buf.capacity();
+ let cap_after_first = match &buf {
+ PixelBuffer::U8(v) => v.capacity(),
+ _ => panic!("expected U8 buffer"),
+ };
write_cog_with_buffer(&config, &fp, 1, &path_b, &mut buf).unwrap();
// Buffer should not have reallocated
- assert_eq!(buf.capacity(), cap_after_first);
+ match &buf {
+ PixelBuffer::U8(v) => assert_eq!(v.capacity(), cap_after_first),
+ _ => panic!("expected U8 buffer"),
+ }
}
}
diff --git a/spatialbench-raster/src/noise/mod.rs b/spatialbench-raster/src/noise/mod.rs
index ad0a271..d8414df 100644
--- a/spatialbench-raster/src/noise/mod.rs
+++ b/spatialbench-raster/src/noise/mod.rs
@@ -17,7 +17,7 @@
//! 2D Perlin noise generator for deterministic raster pixel generation.
//!
-//! Produces spatially correlated UInt8 pixel values that mimic the spectral
+//! Produces spatially correlated pixel values that mimic the spectral
//! characteristics of real satellite imagery, yielding realistic compression
//! ratios (~2-4x with DEFLATE/ZSTD).
//!
@@ -26,7 +26,10 @@
//!
//! Uses `f32` arithmetic for ~2x throughput on ARM NEON (Apple Silicon, AWS
//! Graviton). The 23-bit mantissa is more than sufficient for mapping to
-//! UInt8 pixel values.
+//! pixel values.
+//!
+//! Supports direct generation into `u8`, `u16`, and `f32` buffers to avoid
+//! intermediate allocations when the target dtype differs from UInt8.
/// A seeded 2D Perlin noise generator.
///
@@ -101,13 +104,77 @@ impl PerlinNoise {
let len = width as usize * height as usize;
buf.clear();
buf.reserve(len.saturating_sub(buf.capacity()));
+ self.generate_noise(width, height, frequency, |val| {
+ let pixel = ((val + 1.0) * 0.5).clamp(0.0, 1.0) * 255.0;
+ buf.push(pixel as u8);
+ });
+ }
+
+ /// Generate a full raster buffer of UInt8 pixel values.
+ ///
+ /// Convenience wrapper around [`Self::generate_raster_into`] that allocates
+ /// a new buffer. Prefer `generate_raster_into` in hot loops.
+ pub fn generate_raster(&self, width: u32, height: u32, frequency: f32) -> Vec<u8> {
+ let mut buf = Vec::new();
+ self.generate_raster_into(width, height, frequency, &mut buf);
+ buf
+ }
+
+ /// Generate a full raster buffer of UInt16 pixel values directly.
+ ///
+ /// Maps Perlin noise [-1, 1] → [0, 65535] without an intermediate u8 buffer.
+ /// The `* 257` scaling used in the u8→u16 path is equivalent to mapping
+ /// `0..=255` → `0..=65535`, which this achieves directly.
+ pub fn generate_raster_u16_into(
+ &self,
+ width: u32,
+ height: u32,
+ frequency: f32,
+ buf: &mut Vec<u16>,
+ ) {
+ let len = width as usize * height as usize;
+ buf.clear();
+ buf.reserve(len.saturating_sub(buf.capacity()));
+ self.generate_noise(width, height, frequency, |val| {
+ let normalized = ((val + 1.0) * 0.5).clamp(0.0, 1.0);
+ buf.push((normalized * 65535.0) as u16);
+ });
+ }
+
+ /// Generate a full raster buffer of Float32 pixel values directly.
+ ///
+ /// Maps Perlin noise [-1, 1] → [0.0, 1.0] without an intermediate u8 buffer.
+ pub fn generate_raster_f32_into(
+ &self,
+ width: u32,
+ height: u32,
+ frequency: f32,
+ buf: &mut Vec<f32>,
+ ) {
+ let len = width as usize * height as usize;
+ buf.clear();
+ buf.reserve(len.saturating_sub(buf.capacity()));
+ self.generate_noise(width, height, frequency, |val| {
+ buf.push(((val + 1.0) * 0.5).clamp(0.0, 1.0));
+ });
+ }
+
+ /// Core noise generation loop. Calls `emit` for each pixel with the raw
+ /// noise value in [-1, 1]. Row-level y-axis values are hoisted out of the
+ /// inner loop.
+ #[inline]
+ fn generate_noise(
+ &self,
+ width: u32,
+ height: u32,
+ frequency: f32,
+ mut emit: impl FnMut(f32),
+ ) {
let inv_w = frequency / width as f32;
let inv_h = frequency / height as f32;
for row in 0..height {
let y = row as f32 * inv_h;
-
- // Hoist row-invariant y-axis computations
let yi_raw = y.floor() as i32;
let yf = y - y.floor();
let v = fade(yf);
@@ -120,7 +187,6 @@ impl PerlinNoise {
let u = fade(xf);
let xi = (xi_raw & 255) as usize;
- // Hash corners (yi and yi+1 are row-constant)
let p_xi = self.perm[xi] as usize;
let p_xi1 = self.perm[xi + 1] as usize;
let aa = self.perm[p_xi + yi] as usize;
@@ -128,27 +194,12 @@ impl PerlinNoise {
let ba = self.perm[p_xi1 + yi] as usize;
let bb = self.perm[p_xi1 + yi + 1] as usize;
- // Gradient dot products and bilinear interpolation
let x1 = lerp(grad(aa, xf, yf), grad(ba, xf - 1.0, yf), u);
let x2 = lerp(grad(ab, xf, yf - 1.0), grad(bb, xf - 1.0, yf - 1.0), u);
- let val = lerp(x1, x2, v);
-
- // Map [-1, 1] → [0, 255]
- let pixel = ((val + 1.0) * 0.5).clamp(0.0, 1.0) * 255.0;
- buf.push(pixel as u8);
+ emit(lerp(x1, x2, v));
}
}
}
-
- /// Generate a full raster buffer of UInt8 pixel values.
- ///
- /// Convenience wrapper around [`Self::generate_raster_into`] that allocates
- /// a new buffer. Prefer `generate_raster_into` in hot loops.
- pub fn generate_raster(&self, width: u32, height: u32, frequency: f32) -> Vec<u8> {
- let mut buf = Vec::new();
- self.generate_raster_into(width, height, frequency, &mut buf);
- buf
- }
}
/// Simple LCG for permutation table shuffling (Numerical Recipes constants).
@@ -245,4 +296,52 @@ mod tests {
"expected pixel variation, got range [{min}, {max}]"
);
}
+
+ #[test]
+ fn generate_raster_u16_correct_size() {
+ let n = PerlinNoise::new(7);
+ let mut buf = Vec::new();
+ n.generate_raster_u16_into(64, 64, 4.0, &mut buf);
+ assert_eq!(buf.len(), 64 * 64);
+ }
+
+ #[test]
+ fn generate_raster_u16_range() {
+ let n = PerlinNoise::new(42);
+ let mut buf = Vec::new();
+ n.generate_raster_u16_into(128, 128, 8.0, &mut buf);
+ let min = *buf.iter().min().unwrap();
+ let max = *buf.iter().max().unwrap();
+ assert!(max <= 65535);
+ assert!(max - min > 10000, "expected u16 variation, got [{min}, {max}]");
+ }
+
+ #[test]
+ fn generate_raster_f32_correct_size() {
+ let n = PerlinNoise::new(7);
+ let mut buf = Vec::new();
+ n.generate_raster_f32_into(64, 64, 4.0, &mut buf);
+ assert_eq!(buf.len(), 64 * 64);
+ }
+
+ #[test]
+ fn generate_raster_f32_range() {
+ let n = PerlinNoise::new(42);
+ let mut buf = Vec::new();
+ n.generate_raster_f32_into(128, 128, 8.0, &mut buf);
+ for &v in &buf {
+ assert!((0.0..=1.0).contains(&v), "f32 pixel out of range: {v}");
+ }
+ }
+
+ #[test]
+ fn generate_raster_u16_buffer_reuse() {
+ let n = PerlinNoise::new(10);
+ let mut buf = Vec::new();
+ n.generate_raster_u16_into(32, 32, 4.0, &mut buf);
+ assert_eq!(buf.len(), 32 * 32);
+ let cap = buf.capacity();
+ n.generate_raster_u16_into(32, 32, 4.0, &mut buf);
+ assert_eq!(buf.capacity(), cap, "buffer should not reallocate");
+ }
}