Skip to main content

ImagePixel

Trait ImagePixel 

Source
pub trait ImagePixel: Element {
    // Required methods
    fn from_u8(v: u8) -> Self;
    fn dtype() -> DType;

    // Provided method
    fn from_u16(v: u16) -> Self { ... }
}
Expand description

Marker trait for pixel element types supported by image decoding.

Provides conversions from decoded u8 and u16 pixel values into the tensor’s native element type. The from_u16 path is used for 16-bit PNG images; JPEG always decodes to u8.

§Supported types and conversions

Typefrom_u8from_u16Notes
u8identity>> 8
u16* 257identity0..255 → 0..65535
i8XOR 0x80(>> 8) XOR 0x80unsigned-to-signed via sign-bit flip
i16* 257 XOR 0x8000XOR 0x8000unsigned-to-signed via sign-bit flip
f32/ 255.0/ 65535.0normalised to [0.0, 1.0]

edgefirst_tensor::Element is a supertrait: under the dynamic tensor backend, Tensor<T>’s own constructors need T::DTYPE at compile time (there is no per-variant enum to allocate through instead), so any T this crate builds a Tensor<T> over must already be one of the eleven element types Element is sealed to. Every ImagePixel impl below is one of those types, so this costs nothing real – it just makes the existing constraint visible at the trait level instead of failing much later at a Tensor<T> call site.

Required Methods§

Source

fn from_u8(v: u8) -> Self

Convert a [0, 255] byte value to this pixel type.

Source

fn dtype() -> DType

The DType that corresponds to this Rust type.

Provided Methods§

Source

fn from_u16(v: u16) -> Self

Convert a [0, 65535] 16-bit value to this pixel type.

Used for 16-bit PNG images. The default implementation converts via from_u8(v >> 8) which discards the low byte.

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 ImagePixel for f32

Source§

fn from_u8(v: u8) -> Self

Source§

fn from_u16(v: u16) -> Self

Source§

fn dtype() -> DType

Source§

impl ImagePixel for i8

Source§

fn from_u8(v: u8) -> Self

Source§

fn from_u16(v: u16) -> Self

Source§

fn dtype() -> DType

Source§

impl ImagePixel for i16

Source§

fn from_u8(v: u8) -> Self

Source§

fn from_u16(v: u16) -> Self

Source§

fn dtype() -> DType

Source§

impl ImagePixel for u8

Source§

fn from_u8(v: u8) -> Self

Source§

fn from_u16(v: u16) -> Self

Source§

fn dtype() -> DType

Source§

impl ImagePixel for u16

Source§

fn from_u8(v: u8) -> Self

Source§

fn from_u16(v: u16) -> Self

Source§

fn dtype() -> DType

Implementors§