Skip to main content

Image

Struct Image 

Source
pub struct Image {
    pub pixels: Vec<u8>,
    pub width: i32,
    pub height: i32,
    pub format: GLuint,
    pub pixel_type: GLuint,
    pub null_data: bool,
}
Expand description

Contains the raw pixel color data of an image.

Fields§

§pixels: Vec<u8>

The pixels of the image.

§width: i32

The width of the image.

§height: i32

The height of the image.

§format: GLuint

The OpenGL format of the image.

§pixel_type: GLuint

The OpenGL type of the pixels of the image.

§null_data: bool

Whether the image represents a null pointer for glTexImage2D. If true, the memory for the texture of width x height will be allocated on the GPU, but will probably be garbage.

Implementations§

Source§

impl Image

Source

pub fn with_png(bytes: &[u8]) -> Result<Image, PngLoadingError>

Parses a PNG image and makes an Image out of it.

This function assumes that the image is in SRGB space, so the image format defaults to SRGB or SRGB_ALPHA if the image contains the RGB or RGBA components.

§Color type note

If your image has a ColorType of Grayscale, Indexed or GrayscaleAlpha, it will not display as you would imagine with the default shaders. These images will use GL_RED, GL_RED, and GL_RG as their format when uploading the texture, so you need to take that into account in your shaders (eg. when using GrayscaleAlpha, you’d use the color.g value as your alpha, and color.r as your grayscale value).

§Errors

A PngError will be returned if the data couldn’t be read for some reason by the png crate (most probably, bytes doesn’t describe a valid PNG image). An UnsupportedBitDepth error will be returned if the PNG bit depth isn’t 8 or 16 bits per channel.

§Example
let sprite = fae::Image::with_png(&std::fs::read("sprite.png")?)?;
Source

pub fn with_color( width: i32, height: i32, color: &[u8], ) -> Result<Image, ImageCreationError>

Creates a solid color image.

The color can be 1-4 items long, and will be interpreted in the following order: red, green, blue, alpha.

The color is interpreted as SRGB when 3 or 4 color components are provided, to be consistent with loading images from the disk, which are assumed to be in SRGB space by default.

Based on the length of the color slice, the format of the resulting image will be gl::RED, gl::RG, gl::SRGB, or gl::SRGB_ALPHA. This can be changed with format().

§Example
use fae::Image;
let image = Image::with_color(128, 128, &[0xB4, 0x6E, 0xC8, 0xFF]);
// image now represents a 128px by 128px image that consists of fully opaque violet pixels.
Source

pub fn with_null_texture(width: i32, height: i32, format: GLuint) -> Image

Creates an image with a specified width, height and a format, and signals to OpenGL that the texture will be filled in later. The memory for the texture will be allocated on the GPU, but no pixel data needs to be sent from the CPU to the GPU during initialization.

See also: Spritesheet::upload_texture_region.

Source

pub fn format(&mut self, format: GLuint) -> &mut Self

Sets the format of the pixels.

Generally: gl::RED for grayscale pixels, gl::RGB for linear non-transparent pixels, and gl::RGBA for linear and transparent pixels.

§Example
use fae::{gl, Image};
let image = Image::with_color(128, 128, &[0x88])?.format(gl::RED);
// image now represents a 128px by 128px image that consists of half-red pixels taking up only one byte per pixel.

Trait Implementations§

Source§

impl Clone for Image

Source§

fn clone(&self) -> Image

Returns a duplicate 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 Image

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Image

§

impl RefUnwindSafe for Image

§

impl Send for Image

§

impl Sync for Image

§

impl Unpin for Image

§

impl UnsafeUnpin for Image

§

impl UnwindSafe for Image

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

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
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.