Skip to main content

TensorImage

Struct TensorImage 

Source
pub struct TensorImage { /* private fields */ }
Expand description

An image represented as a tensor with associated format information.

Implementations§

Source§

impl TensorImage

Source

pub fn new( width: usize, height: usize, fourcc: FourCharCode, memory: Option<TensorMemory>, ) -> Result<Self>

Creates a new TensorImage with the specified width, height, format, and memory type.

§Examples
use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::TensorMemory;
let img = TensorImage::new(640, 480, RGB, Some(TensorMemory::Mem))?;
assert_eq!(img.width(), 640);
assert_eq!(img.height(), 480);
assert_eq!(img.fourcc(), RGB);
assert!(!img.is_planar());
Source

pub fn from_tensor(tensor: Tensor<u8>, fourcc: FourCharCode) -> Result<Self>

Creates a new TensorImage from an existing tensor and specified format.

The required tensor shape depends on the pixel format:

FormatShapeDescription
RGB[H, W, 3]3-channel interleaved
RGBA[H, W, 4]4-channel interleaved
GREY[H, W, 1]Single-channel grayscale
YUYV[H, W, 2]YUV 4:2:2 interleaved
PLANAR_RGB[3, H, W]Channels-first (3 planes)
PLANAR_RGBA[4, H, W]Channels-first (4 planes)
RGB_INT8[H, W, 3]Packed RGB, int8 via XOR 0x80
PLANAR_RGB_INT8[3, H, W]Planar RGB, int8 via XOR 0x80
NV12[H*3/2, W]Semi-planar YUV 4:2:0 (2D)
NV16[H*2, W]Semi-planar YUV 4:2:2 (2D)

Most formats use a 3D tensor where the channel dimension matches the format’s channel count. The semi-planar formats NV12 and NV16 are special: the Y and UV planes have different heights, so the data cannot be described as [H, W, C]. Instead the contiguous memory is represented as a 2D tensor whose first dimension encodes the total byte height (Y rows + UV rows).

§Examples

RGB (3D interleaved):

use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::Tensor;
let tensor = Tensor::new(&[720, 1280, 3], None, None)?;
let img = TensorImage::from_tensor(tensor, RGB)?;
assert_eq!(img.width(), 1280);
assert_eq!(img.height(), 720);
assert_eq!(img.fourcc(), RGB);

GREY (3D with 1 channel):

use edgefirst_image::{GREY, TensorImage};
use edgefirst_tensor::Tensor;
let tensor = Tensor::new(&[480, 640, 1], None, None)?;
let img = TensorImage::from_tensor(tensor, GREY)?;
assert_eq!(img.width(), 640);
assert_eq!(img.height(), 480);

NV12 (2D semi-planar, height*3/2 rows):

use edgefirst_image::{NV12, TensorImage};
use edgefirst_tensor::Tensor;
// 1080p NV12: 1080 Y rows + 540 UV rows = 1620 total rows
let tensor = Tensor::new(&[1620, 1920], None, None)?;
let img = TensorImage::from_tensor(tensor, NV12)?;
assert_eq!(img.width(), 1920);
assert_eq!(img.height(), 1080);
Source

pub fn load( image: &[u8], format: Option<FourCharCode>, memory: Option<TensorMemory>, ) -> Result<Self>

Loads an image from the given byte slice, attempting to decode it as JPEG or PNG format. Exif orientation is supported. The default format is RGB.

§Examples
use edgefirst_image::{RGBA, TensorImage};
use edgefirst_tensor::TensorMemory;
let jpeg_bytes = include_bytes!("../../../testdata/zidane.png");
let img = TensorImage::load(jpeg_bytes, Some(RGBA), Some(TensorMemory::Mem))?;
assert_eq!(img.width(), 1280);
assert_eq!(img.height(), 720);
assert_eq!(img.fourcc(), RGBA);
Source

pub fn load_jpeg( image: &[u8], format: Option<FourCharCode>, memory: Option<TensorMemory>, ) -> Result<Self>

Loads a JPEG image from the given byte slice. Supports EXIF orientation. The default format is RGB.

§Examples
use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::TensorMemory;
let jpeg_bytes = include_bytes!("../../../testdata/zidane.jpg");
let img = TensorImage::load_jpeg(jpeg_bytes, Some(RGB), Some(TensorMemory::Mem))?;
assert_eq!(img.width(), 1280);
assert_eq!(img.height(), 720);
assert_eq!(img.fourcc(), RGB);
Source

pub fn load_png( image: &[u8], format: Option<FourCharCode>, memory: Option<TensorMemory>, ) -> Result<Self>

Loads a PNG image from the given byte slice. Supports EXIF orientation. The default format is RGB.

§Examples
use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::TensorMemory;
let png_bytes = include_bytes!("../../../testdata/zidane.png");
let img = TensorImage::load_png(png_bytes, Some(RGB), Some(TensorMemory::Mem))?;
assert_eq!(img.width(), 1280);
assert_eq!(img.height(), 720);
assert_eq!(img.fourcc(), RGB);
Source

pub fn save_jpeg(&self, path: &str, quality: u8) -> Result<()>

Saves the image as a JPEG file at the specified path with the given quality. Only RGB and RGBA formats are supported.

§Examples
use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::Tensor;
let tensor = Tensor::new(&[720, 1280, 3], None, None)?;
let img = TensorImage::from_tensor(tensor, RGB)?;
let save_path = "/tmp/output.jpg";
img.save_jpeg(save_path, 90)?;
Source

pub fn tensor(&self) -> &Tensor<u8>

Returns a reference to the underlying tensor.

§Examples
use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::{Tensor, TensorTrait};
let tensor = Tensor::new(&[720, 1280, 3], None, Some("Tensor"))?;
let img = TensorImage::from_tensor(tensor, RGB)?;
let underlying_tensor = img.tensor();
assert_eq!(underlying_tensor.name(), "Tensor");
Source

pub fn fourcc(&self) -> FourCharCode

Returns the FourCC code representing the image format.

§Examples
use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::{Tensor, TensorTrait};
let tensor = Tensor::new(&[720, 1280, 3], None, Some("Tensor"))?;
let img = TensorImage::from_tensor(tensor, RGB)?;
assert_eq!(img.fourcc(), RGB);
Source

pub fn is_planar(&self) -> bool

§Examples
use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::{Tensor, TensorTrait};
let tensor = Tensor::new(&[720, 1280, 3], None, Some("Tensor"))?;
let img = TensorImage::from_tensor(tensor, RGB)?;
assert!(!img.is_planar());
Source

pub fn width(&self) -> usize

§Examples
use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::{Tensor, TensorTrait};
let tensor = Tensor::new(&[720, 1280, 3], None, Some("Tensor"))?;
let img = TensorImage::from_tensor(tensor, RGB)?;
assert_eq!(img.width(), 1280);
Source

pub fn height(&self) -> usize

§Examples
use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::{Tensor, TensorTrait};
let tensor = Tensor::new(&[720, 1280, 3], None, Some("Tensor"))?;
let img = TensorImage::from_tensor(tensor, RGB)?;
assert_eq!(img.height(), 720);
Source

pub fn channels(&self) -> usize

§Examples
use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::{Tensor, TensorTrait};
let tensor = Tensor::new(&[720, 1280, 3], None, Some("Tensor"))?;
let img = TensorImage::from_tensor(tensor, RGB)?;
assert_eq!(img.channels(), 3);
Source

pub fn row_stride(&self) -> usize

§Examples
use edgefirst_image::{RGB, TensorImage};
use edgefirst_tensor::{Tensor, TensorTrait};
let tensor = Tensor::new(&[720, 1280, 3], None, Some("Tensor"))?;
let img = TensorImage::from_tensor(tensor, RGB)?;
assert_eq!(img.row_stride(), 1280*3);
Source

pub fn buffer_identity(&self) -> &BufferIdentity

Returns the buffer identity of the underlying tensor.

Trait Implementations§

Source§

impl Debug for TensorImage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl TensorImageDst for TensorImage

Source§

fn tensor(&self) -> &Tensor<u8>

Returns a reference to the underlying tensor.
Source§

fn tensor_mut(&mut self) -> &mut Tensor<u8>

Returns a mutable reference to the underlying tensor.
Source§

fn fourcc(&self) -> FourCharCode

Returns the FourCC code representing the image format.
Source§

fn is_planar(&self) -> bool

Returns whether the image is in planar format.
Source§

fn width(&self) -> usize

Returns the width of the image in pixels.
Source§

fn height(&self) -> usize

Returns the height of the image in pixels.
Source§

fn channels(&self) -> usize

Returns the number of channels in the image.
Source§

fn row_stride(&self) -> usize

Returns the row stride in bytes.
Source§

fn buffer_identity(&self) -> &BufferIdentity

Returns the buffer identity of the underlying tensor.
Source§

impl TryFrom<&TensorImage> for G2DSurface

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(img: &TensorImage) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&mut TensorImage> for G2DSurface

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(img: &mut TensorImage) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V