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: i32The width of the image.
height: i32The height of the image.
format: GLuintThe OpenGL format of the image.
pixel_type: GLuintThe OpenGL type of the pixels of the image.
null_data: boolWhether 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
impl Image
Sourcepub fn with_png(bytes: &[u8]) -> Result<Image, PngLoadingError>
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")?)?;Sourcepub fn with_color(
width: i32,
height: i32,
color: &[u8],
) -> Result<Image, ImageCreationError>
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.Sourcepub fn with_null_texture(width: i32, height: i32, format: GLuint) -> Image
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.
Sourcepub fn format(&mut self, format: GLuint) -> &mut Self
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.