yif tach
yif tach

Reputation: 1

How to transform a tiff file to ndarray in rust?

I would like to create an ndarray in Rust containing data from a Tif image. I'm new to rust so it might be a silly question. I using this code:

let file = std::fs::File::open("some_image.tif").unwrap();
let mut decoder = tiff::decoder::Decoder::new(file).unwrap();
let image_data = decoder.read_image().unwrap();
let width = decoder.dimensions().unwrap().0 as usize;
let height = decoder.dimensions().unwrap().1 as usize;
let image_data = Array2::from_shape_vec((height, width), image_data).unwrap();

Now image is of type DecodingResult, variant U8 which hold a Vec. so the Array creator expects a vector, not a DecodingResult, which is just an enum wrapping a vector... How do I "access" the vector inside?

Upvotes: 0

Views: 333

Answers (1)

xamgore
xamgore

Reputation: 1872

It can be done with the help of pattern matching using either if let or match keywords.

use tiff::decoder::DecodingResult;

match image_data {
    DecodingResult::U32(data) => { /* data is Vec<u32> */ },
    _ => panic!(),
}

if let DecodingResult::U32(data) = image_data {
    // data is Vec<u32>
}

Upvotes: 0

Related Questions