use exr::prelude::{ReadChannels, ReadLayers};
use nalgebra::DMatrix;

pub fn load_heightfield_from_exr(path: &str) -> anyhow::Result<DMatrix<f32>>
{
    let image = exr::prelude::read()
        .no_deep_data()
        .largest_resolution_level()
        .all_channels()
        .all_layers()
        .all_attributes()
        .from_file(path)?;

    let layer = &image.layer_data[0];
    let channel = &layer.channel_data.list[0];

    let width = layer.size.width();
    let height = layer.size.height();

    let heights: Vec<f32> = channel.sample_data.values_as_f32().collect();

    Ok(DMatrix::from_row_slice(height, width, &heights))
}
