Skip to content

Commit

Permalink
graphics/image: Implement 8bpp bmp image support
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwuelker committed Feb 17, 2024
1 parent 8fcc646 commit bec50d4
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/graphics/image/src/bmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl InfoHeader {
// 1 bit per pixel
align_up::<8>(self.width as usize) / 8
},
ImageType::Palette8Bit(_) => self.width as usize,
ImageType::Rgb16 => {
// 16 bits per pixel
2 * self.width as usize
Expand Down Expand Up @@ -331,6 +332,37 @@ pub fn decode(bytes: &[u8]) -> Result<DynamicTexture, Error> {
)
.into()
},
ImageType::Palette8Bit(run_length_encoded) => {
if run_length_encoded == RunLengthEncoded::Yes {
todo!("implement run length encoding");
}

let mut texture_data =
Vec::with_capacity(info_header.width as usize * info_header.height as usize * 3);

info_header.for_each_scanline(image_data, |scanline| {
for palette_index in scanline.iter().take(info_header.width as usize) {
let pixel = palette
.get(*palette_index as usize)
.ok_or(Error::PaletteTooSmall)?;
texture_data.extend(pixel.channels());
}

Ok(())
})?;

let mut t = Texture::<Rgb<u8>, Vec<u8>>::from_data(
texture_data,
info_header.width as usize,
info_header.height as usize,
);

for i in 0..info_header.width.min(info_header.height) as usize {
t.set_pixel(i, i, Rgb::<u8>::from_channels(&[255, 255, 255]));
}

t.into()
},
ImageType::Rgb16 => {
todo!("implement .bmp rgb16 format")
},
Expand Down

0 comments on commit bec50d4

Please sign in to comment.