Skip to main content

ImageLoad

Trait ImageLoad 

Source
pub trait ImageLoad {
    // Required methods
    fn load_image(
        &mut self,
        decoder: &mut ImageDecoder,
        data: &[u8],
    ) -> Result<ImageInfo>;
    fn load_image_read<R: Read>(
        &mut self,
        decoder: &mut ImageDecoder,
        reader: R,
    ) -> Result<ImageInfo>;
    fn load_image_file(
        &mut self,
        decoder: &mut ImageDecoder,
        path: impl AsRef<Path>,
    ) -> Result<ImageInfo>;
}
Expand description

Extension trait for loading images into pre-allocated tensors.

Import this trait to add load_image, load_image_read, and load_image_file methods to Tensor<T> and TensorDyn.

The decoder configures the tensor’s dimensions and pixel format to the decoded native format (JPEG → Nv12/Grey, PNG → Rgb/Rgba/Grey). Allocate the tensor at the maximum expected image size; smaller images reconfigure it within the same allocation.

§Example

use edgefirst_codec::{ImageDecoder, ImageLoad};
use edgefirst_tensor::{CpuAccess, Tensor, TensorTrait, TensorMemory, PixelFormat};

let mut tensor = Tensor::<u8>::image(1920, 1080, PixelFormat::Nv12, Some(TensorMemory::Mem),
                                      CpuAccess::Write)
    .expect("alloc");
let mut decoder = ImageDecoder::new();

let jpeg = std::fs::read("image.jpg").unwrap();
let info = tensor.load_image(&mut decoder, &jpeg).unwrap();
println!("Decoded {}×{} {:?}", info.width, info.height, info.format);

let info = tensor.load_image_file(&mut decoder, "image.jpg").unwrap();

Required Methods§

Source

fn load_image( &mut self, decoder: &mut ImageDecoder, data: &[u8], ) -> Result<ImageInfo>

Decode image bytes (&[u8]) into this tensor, configuring its dimensions and format to the decoded native format.

The image format (JPEG or PNG) is detected from magic bytes.

§Errors

Returns CodecError::InsufficientCapacity if the decoded image is larger than the tensor’s allocation.

Source

fn load_image_read<R: Read>( &mut self, decoder: &mut ImageDecoder, reader: R, ) -> Result<ImageInfo>

Decode image data from a Read source into this tensor.

Source

fn load_image_file( &mut self, decoder: &mut ImageDecoder, path: impl AsRef<Path>, ) -> Result<ImageInfo>

Decode an image file into this tensor.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ImageLoad for TensorDyn

Source§

fn load_image( &mut self, decoder: &mut ImageDecoder, data: &[u8], ) -> Result<ImageInfo>

Source§

fn load_image_read<R: Read>( &mut self, decoder: &mut ImageDecoder, reader: R, ) -> Result<ImageInfo>

Source§

fn load_image_file( &mut self, decoder: &mut ImageDecoder, path: impl AsRef<Path>, ) -> Result<ImageInfo>

Source§

impl<T: ImagePixel> ImageLoad for Tensor<T>

Source§

fn load_image( &mut self, decoder: &mut ImageDecoder, data: &[u8], ) -> Result<ImageInfo>

Source§

fn load_image_read<R: Read>( &mut self, decoder: &mut ImageDecoder, reader: R, ) -> Result<ImageInfo>

Source§

fn load_image_file( &mut self, decoder: &mut ImageDecoder, path: impl AsRef<Path>, ) -> Result<ImageInfo>

Implementors§