Struct tetra::graphics::ImageData

source ·
pub struct ImageData { /* private fields */ }
Expand description

Raw image data.

The data can be stored in a variety of formats, as represented by the TextureFormat enum.

Supported File Formats

Images can be decoded from various common file formats via the new and from_encoded constructors. Individual decoders can be enabled or disabled via Cargo feature flags.

FormatCargo featureEnabled by default?
PNGtexture_pngYes
JPEGtexture_jpegYes
GIFtexture_gifYes
BMPtexture_bmpYes
TIFFtexture_tiffNo
TGAtexture_tgaNo
WebPtexture_webpNo
ICOtexture_icoNo
PNMtexture_pnmNo
DDS/DXTtexture_ddsNo

Implementations§

source§

impl ImageData

source

pub fn new<P>(path: P) -> Result<ImageData>where P: AsRef<Path>,

Loads image data from the given file.

The format will be determined based on the file extension.

Errors
source

pub fn from_data<D>( width: i32, height: i32, format: TextureFormat, data: D ) -> Result<ImageData>where D: Into<Vec<u8>>,

Creates an ImageData from raw pixel data.

This function takes Into<Vec<u8>>. If you pass a Vec<u8>, that Vec will be reused for the created ImageData without reallocating. Otherwise, the data will be copied.

This function requires you to provide enough data to fill the image’s bounds. If you provide too little data, an error will be returned. If you provide too much data, it will be truncated.

Errors
source

pub fn from_encoded(data: &[u8]) -> Result<ImageData>

Decodes image data that is encoded in one of Tetra’s supported file formats (except for TGA).

This is useful in combination with include_bytes, as it allows you to include your image data directly in the binary.

The format will be determined based on the ‘magic bytes’ at the beginning of the data. Note that TGA files do not have recognizable magic bytes, so this function will not recognize them.

Errors
source

pub fn width(&self) -> i32

Returns the width of the image.

source

pub fn height(&self) -> i32

Returns the height of the image.

source

pub fn size(&self) -> (i32, i32)

Returns the size of the image.

source

pub fn format(&self) -> TextureFormat

Returns the format of the data contained within the image.

source

pub fn as_bytes(&self) -> &[u8]

Returns the image’s data, as a slice of raw bytes.

source

pub fn into_bytes(self) -> Vec<u8>

Returns the image’s underlying buffer.

source

pub fn region(&self, region: Rectangle<i32>) -> ImageData

Creates a new ImageData from a region.

This will copy the data into a new buffer - as such, calling this function can be expensive!

Panics

This function will panic if you try to read outside of the image’s bounds.

source

pub fn to_texture(&self, ctx: &mut Context) -> Result<Texture>

Creates a new Texture from the stored data.

Errors
source

pub fn get_pixel_color(&self, position: Vec2<i32>) -> Color

Gets the color of the pixel at the specified location.

If the image’s TextureFormat does not contain one of the three color channels, the returned color will have that channel set to zero. Similarly, if the format does not have an alpha channel, the returned color will have an alpha value of one.

Panics

Panics if the location is outside the bounds of the image.

source

pub fn set_pixel_color(&mut self, position: Vec2<i32>, color: Color)

Sets the color of the pixel at the specified location.

Any channels of the color that are not supported by the image’s TextureFormat will be ignored. For example, if the image has TextureFormat::R8, only the red channel of the color will be stored.

Panics

Panics if the location is outside the bounds of the image.

source

pub fn transform<F>(&mut self, func: F)where F: FnMut(Vec2<i32>, Color) -> Color,

Transforms the image data by applying a function to each pixel.

If the image’s TextureFormat does not contain one of the three color channels, the colors provided to the closure will have that channel set to zero. Similarly, if the format does not have an alpha channel, the returned color will have an alpha value of one. The unsupported channels will be ignored when writing back to the image buffer.

source

pub fn premultiply(&mut self)

Multiplies the RGB components of each pixel by the alpha component.

This can be useful when working with premultiplied alpha blending.

If the image’s data format does not have an alpha component, this function will have no effect.

Trait Implementations§

source§

impl Clone for ImageData

source§

fn clone(&self) -> ImageData

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for ImageData

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

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

const: unstable · 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.

§

impl<F, T> IntoSample<T> for Fwhere T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> ToSample<U> for Twhere U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,