Skip to content

Commit

Permalink
♻️ Turn image decoding into a Vec code into a method call
Browse files Browse the repository at this point in the history
Making the decoder.read_image() code an impl method under the CogReader struct. Reworded the previous panic message into an unimplemented error with a more helpful message that dtypes other than float32 are not yet supported.
  • Loading branch information
weiji14 committed Mar 14, 2024
1 parent d02dd13 commit 54858db
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/io/geotiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ impl<R: Read + Seek> CogReader<R> {
Ok(Self { decoder })
}

/// Decode GeoTIFF image to a Vec
fn as_vec(&mut self) -> TiffResult<Vec<f32>> {
let decode_result = self.decoder.read_image()?;
let image_data: Vec<f32> = match decode_result {
DecodingResult::F32(img_data) => img_data,
_ => unimplemented!("Data types other than float32 are not yet supported."),
};
Ok(image_data)
}

/// Affine transformation for 2D matrix extracted from TIFF tag metadata, used to transform
/// image pixel (row, col) coordinates to and from geographic/projected (x, y) coordinates.
///
Expand Down Expand Up @@ -80,9 +90,7 @@ pub fn read_geotiff<R: Read + Seek>(stream: R) -> TiffResult<Array2<f32>> {
let (width, height): (u32, u32) = reader.decoder.dimensions()?;

// Get image pixel data
let DecodingResult::F32(img_data) = reader.decoder.read_image()? else {
panic!("Cannot read band data")
};
let img_data: Vec<f32> = reader.as_vec()?;

// Put image pixel data into an ndarray
let vec_data = Array2::from_shape_vec((height as usize, width as usize), img_data)
Expand Down

0 comments on commit 54858db

Please sign in to comment.