Skip to main content

RasterElement

Trait RasterElement 

Source
pub trait RasterElement:
    Copy
    + Default
    + Send
    + Sync
    + 'static
    + Sealed {
    type Bytes: AsRef<[u8]> + AsMut<[u8]> + Copy + Default + for<'a> TryFrom<&'a [u8]>;

    const DATA_TYPE: RasterDataType;
    const KIND: RasterElementKind;
    const SIZE: usize = _;

    // Required methods
    fn from_ne_bytes(bytes: Self::Bytes) -> Self;
    fn to_ne_bytes(self) -> Self::Bytes;
    fn to_raster_f64(self) -> f64;
    fn to_raster_i128(self) -> i128;
    fn from_raster_f64(value: f64) -> Self;
    fn from_raster_f64_truncating(value: f64) -> Self;
    fn from_raster_i128(value: i128) -> Self;

    // Provided method
    fn from_ne_slice(bytes: &[u8]) -> Self { ... }
}
Expand description

A Rust primitive that can appear as a raster sample.

Implemented for exactly the ten scalar raster types — u8, i8, u16, i16, u32, i32, u64, i64, f32 and f64 — and sealed, so the set cannot grow outside this crate.

§Thread safety

Every implementor is a primitive scalar, so Send + Sync is part of the contract. Generic code bounded on RasterElement may therefore split a &mut [T] across worker threads without restating the bound — which is what lets the GeoTIFF driver run its typed band reads on rayon workers, not just the raw-byte ones. Because the trait is sealed the guarantee costs nothing: no out-of-crate type can ever weaken it.

Use it as a bound to write code that is generic over the pixel type:

use oxigeo_core::buffer::{RasterElement, convert_raw_into};
use oxigeo_core::error::Result;
use oxigeo_core::types::RasterDataType;

fn decode<T: RasterElement>(raw: &[u8], src: RasterDataType, dst: &mut [T]) -> Result<()> {
    convert_raw_into(raw, src, dst)
}

let mut out = [0i32; 2];
decode(&[1u8, 2], RasterDataType::UInt8, &mut out)?;
assert_eq!(out, [1, 2]);
assert_eq!(i32::DATA_TYPE, RasterDataType::Int32);

Required Associated Constants§

Source

const DATA_TYPE: RasterDataType

The RasterDataType this Rust type stores.

Source

const KIND: RasterElementKind

Whether this type is an integer or a float.

Provided Associated Constants§

Source

const SIZE: usize = _

Size of one sample in bytes (always size_of::<Self>()).

Required Associated Types§

Source

type Bytes: AsRef<[u8]> + AsMut<[u8]> + Copy + Default + for<'a> TryFrom<&'a [u8]>

The fixed-size native-endian byte array for this type ([u8; size_of::<Self>()]).

Required Methods§

Source

fn from_ne_bytes(bytes: Self::Bytes) -> Self

Decodes a sample from its native-endian byte array.

Source

fn to_ne_bytes(self) -> Self::Bytes

Encodes a sample into its native-endian byte array.

Source

fn to_raster_f64(self) -> f64

Widens the sample to f64.

Exact for every type except u64/i64 magnitudes beyond 253, which are rounded to the nearest representable f64.

Source

fn to_raster_i128(self) -> i128

Widens the sample to i128.

Exact for every integer type. For floats the value is rounded half away from zero and saturated at the i128 bounds; NaN becomes 0.

Source

fn from_raster_f64(value: f64) -> Self

Narrows an f64 to this type, saturating at the type bounds and rounding halves away from zero.

NaN becomes 0 for integer destinations; float destinations keep NaN/±∞ as-is.

Source

fn from_raster_f64_truncating(value: f64) -> Self

Narrows an f64 to this type, saturating at the type bounds and truncating towards zero (C-style (int)x).

Identical to RasterElement::from_raster_f64 for float destinations.

Source

fn from_raster_i128(value: i128) -> Self

Narrows an i128 to this type, saturating at the type bounds.

Exact for integer destinations. Float destinations round to nearest.

Provided Methods§

Source

fn from_ne_slice(bytes: &[u8]) -> Self

Decodes a sample from the first SIZE bytes of bytes (native endian).

Longer slices are truncated — this is what makes complex sources work, where each stride holds [real, imaginary] and only the real component is read. Shorter slices yield Default::default instead of panicking.

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

Source§

const DATA_TYPE: RasterDataType = RasterDataType::Float32

Source§

const KIND: RasterElementKind = RasterElementKind::Float

Source§

type Bytes = [u8; 4]

Source§

fn from_ne_bytes(bytes: Self::Bytes) -> Self

Source§

fn to_ne_bytes(self) -> Self::Bytes

Source§

fn to_raster_f64(self) -> f64

Source§

fn to_raster_i128(self) -> i128

Source§

fn from_raster_f64(value: f64) -> Self

Source§

fn from_raster_f64_truncating(value: f64) -> Self

Source§

fn from_raster_i128(value: i128) -> Self

Source§

impl RasterElement for f64

Source§

const DATA_TYPE: RasterDataType = RasterDataType::Float64

Source§

const KIND: RasterElementKind = RasterElementKind::Float

Source§

type Bytes = [u8; 8]

Source§

fn from_ne_bytes(bytes: Self::Bytes) -> Self

Source§

fn to_ne_bytes(self) -> Self::Bytes

Source§

fn to_raster_f64(self) -> f64

Source§

fn to_raster_i128(self) -> i128

Source§

fn from_raster_f64(value: f64) -> Self

Source§

fn from_raster_f64_truncating(value: f64) -> Self

Source§

fn from_raster_i128(value: i128) -> Self

Source§

impl RasterElement for i8

Source§

const DATA_TYPE: RasterDataType = RasterDataType::Int8

Source§

const KIND: RasterElementKind = RasterElementKind::Integer

Source§

type Bytes = [u8; 1]

Source§

fn from_ne_bytes(bytes: Self::Bytes) -> Self

Source§

fn to_ne_bytes(self) -> Self::Bytes

Source§

fn to_raster_f64(self) -> f64

Source§

fn to_raster_i128(self) -> i128

Source§

fn from_raster_f64(value: f64) -> Self

Source§

fn from_raster_f64_truncating(value: f64) -> Self

Source§

fn from_raster_i128(value: i128) -> Self

Source§

impl RasterElement for i16

Source§

const DATA_TYPE: RasterDataType = RasterDataType::Int16

Source§

const KIND: RasterElementKind = RasterElementKind::Integer

Source§

type Bytes = [u8; 2]

Source§

fn from_ne_bytes(bytes: Self::Bytes) -> Self

Source§

fn to_ne_bytes(self) -> Self::Bytes

Source§

fn to_raster_f64(self) -> f64

Source§

fn to_raster_i128(self) -> i128

Source§

fn from_raster_f64(value: f64) -> Self

Source§

fn from_raster_f64_truncating(value: f64) -> Self

Source§

fn from_raster_i128(value: i128) -> Self

Source§

impl RasterElement for i32

Source§

const DATA_TYPE: RasterDataType = RasterDataType::Int32

Source§

const KIND: RasterElementKind = RasterElementKind::Integer

Source§

type Bytes = [u8; 4]

Source§

fn from_ne_bytes(bytes: Self::Bytes) -> Self

Source§

fn to_ne_bytes(self) -> Self::Bytes

Source§

fn to_raster_f64(self) -> f64

Source§

fn to_raster_i128(self) -> i128

Source§

fn from_raster_f64(value: f64) -> Self

Source§

fn from_raster_f64_truncating(value: f64) -> Self

Source§

fn from_raster_i128(value: i128) -> Self

Source§

impl RasterElement for i64

Source§

const DATA_TYPE: RasterDataType = RasterDataType::Int64

Source§

const KIND: RasterElementKind = RasterElementKind::Integer

Source§

type Bytes = [u8; 8]

Source§

fn from_ne_bytes(bytes: Self::Bytes) -> Self

Source§

fn to_ne_bytes(self) -> Self::Bytes

Source§

fn to_raster_f64(self) -> f64

Source§

fn to_raster_i128(self) -> i128

Source§

fn from_raster_f64(value: f64) -> Self

Source§

fn from_raster_f64_truncating(value: f64) -> Self

Source§

fn from_raster_i128(value: i128) -> Self

Source§

impl RasterElement for u8

Source§

const DATA_TYPE: RasterDataType = RasterDataType::UInt8

Source§

const KIND: RasterElementKind = RasterElementKind::Integer

Source§

type Bytes = [u8; 1]

Source§

fn from_ne_bytes(bytes: Self::Bytes) -> Self

Source§

fn to_ne_bytes(self) -> Self::Bytes

Source§

fn to_raster_f64(self) -> f64

Source§

fn to_raster_i128(self) -> i128

Source§

fn from_raster_f64(value: f64) -> Self

Source§

fn from_raster_f64_truncating(value: f64) -> Self

Source§

fn from_raster_i128(value: i128) -> Self

Source§

impl RasterElement for u16

Source§

const DATA_TYPE: RasterDataType = RasterDataType::UInt16

Source§

const KIND: RasterElementKind = RasterElementKind::Integer

Source§

type Bytes = [u8; 2]

Source§

fn from_ne_bytes(bytes: Self::Bytes) -> Self

Source§

fn to_ne_bytes(self) -> Self::Bytes

Source§

fn to_raster_f64(self) -> f64

Source§

fn to_raster_i128(self) -> i128

Source§

fn from_raster_f64(value: f64) -> Self

Source§

fn from_raster_f64_truncating(value: f64) -> Self

Source§

fn from_raster_i128(value: i128) -> Self

Source§

impl RasterElement for u32

Source§

const DATA_TYPE: RasterDataType = RasterDataType::UInt32

Source§

const KIND: RasterElementKind = RasterElementKind::Integer

Source§

type Bytes = [u8; 4]

Source§

fn from_ne_bytes(bytes: Self::Bytes) -> Self

Source§

fn to_ne_bytes(self) -> Self::Bytes

Source§

fn to_raster_f64(self) -> f64

Source§

fn to_raster_i128(self) -> i128

Source§

fn from_raster_f64(value: f64) -> Self

Source§

fn from_raster_f64_truncating(value: f64) -> Self

Source§

fn from_raster_i128(value: i128) -> Self

Source§

impl RasterElement for u64

Source§

const DATA_TYPE: RasterDataType = RasterDataType::UInt64

Source§

const KIND: RasterElementKind = RasterElementKind::Integer

Source§

type Bytes = [u8; 8]

Source§

fn from_ne_bytes(bytes: Self::Bytes) -> Self

Source§

fn to_ne_bytes(self) -> Self::Bytes

Source§

fn to_raster_f64(self) -> f64

Source§

fn to_raster_i128(self) -> i128

Source§

fn from_raster_f64(value: f64) -> Self

Source§

fn from_raster_f64_truncating(value: f64) -> Self

Source§

fn from_raster_i128(value: i128) -> Self

Implementors§