add RgbaBitmap::from_bytes with some limited pixel format support
This commit is contained in:
parent
1815a2236c
commit
ad1dbee5fd
|
@ -24,6 +24,9 @@ pub enum BitmapError {
|
|||
#[error("Unknown bitmap file type: {0}")]
|
||||
UnknownFileType(String),
|
||||
|
||||
#[error("Bitmap I/O error")]
|
||||
IOError(#[from] std::io::Error),
|
||||
|
||||
#[error("Bitmap IFF file error")]
|
||||
IffError(#[from] iff::IffError),
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
use byteorder::ReadBytesExt;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::graphics::bitmap::{Bitmap, BitmapError};
|
||||
use crate::graphics::color::to_rgb32;
|
||||
use crate::graphics::color::{to_argb32, to_rgb32};
|
||||
use crate::graphics::palette::Palette;
|
||||
|
||||
pub mod blit;
|
||||
|
@ -9,6 +10,12 @@ pub mod primitives;
|
|||
|
||||
pub type RgbaBitmap = Bitmap<u32>;
|
||||
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
pub enum RgbaPixelFormat {
|
||||
ARGB,
|
||||
RGBA,
|
||||
}
|
||||
|
||||
impl RgbaBitmap {
|
||||
/// Creates a new Bitmap with the specified dimensions.
|
||||
///
|
||||
|
@ -22,6 +29,34 @@ impl RgbaBitmap {
|
|||
Self::internal_new(width, height, to_rgb32(0, 0, 0))
|
||||
}
|
||||
|
||||
pub fn from_bytes<T: ReadBytesExt>(
|
||||
width: u32,
|
||||
height: u32,
|
||||
format: RgbaPixelFormat,
|
||||
reader: &mut T,
|
||||
) -> Result<Self, BitmapError> {
|
||||
let mut bitmap = Self::internal_new(width, height, 0)?;
|
||||
for pixel in bitmap.pixels_mut().iter_mut() {
|
||||
*pixel = match format {
|
||||
RgbaPixelFormat::RGBA => {
|
||||
let r = reader.read_u8()?;
|
||||
let g = reader.read_u8()?;
|
||||
let b = reader.read_u8()?;
|
||||
let a = reader.read_u8()?;
|
||||
to_argb32(a, r, g, b)
|
||||
}
|
||||
RgbaPixelFormat::ARGB => {
|
||||
let a = reader.read_u8()?;
|
||||
let r = reader.read_u8()?;
|
||||
let g = reader.read_u8()?;
|
||||
let b = reader.read_u8()?;
|
||||
to_argb32(a, r, g, b)
|
||||
}
|
||||
};
|
||||
}
|
||||
Ok(bitmap)
|
||||
}
|
||||
|
||||
pub fn load_file(path: &Path) -> Result<(Self, Option<Palette>), BitmapError> {
|
||||
if let Some(extension) = path.extension() {
|
||||
let extension = extension.to_ascii_lowercase();
|
||||
|
|
Loading…
Reference in a new issue