Skip to content

Commit

Permalink
implement read mono16
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarriba committed Jan 10, 2025
1 parent af0d244 commit 14a29fb
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions crates/kornia-io/src/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ pub fn read_image_png_rgba8(file_path: impl AsRef<Path>) -> Result<Image<u8, 4>,
Ok(Image::new(size.into(), buf)?)
}

/// Read a PNG image with a single channel (mono16).
///
/// # Arguments
///
/// * `file_path` - The path to the PNG file.
///
/// # Returns
///
/// A grayscale image with a single channel (mono16).
pub fn read_image_png_mono16(file_path: impl AsRef<Path>) -> Result<Image<u16, 1>, IoError> {
let (buf, size) = read_png_impl(file_path)?;

// convert the buffer to u16
let mut buf_u16 = Vec::with_capacity(buf.len() / 2);
for chunk in buf.chunks_exact(2) {
buf_u16.push(u16::from_be_bytes([chunk[0], chunk[1]]));
}

Ok(Image::new(size.into(), buf_u16)?)
}

// utility function to read the png file
fn read_png_impl(file_path: impl AsRef<Path>) -> Result<(Vec<u8>, [usize; 2]), IoError> {
// verify the file exists
Expand Down

0 comments on commit 14a29fb

Please sign in to comment.