Skip to content

Commit

Permalink
Auto merge of #191 - PossiblyAShrub:convert-rgb24-rgba32, r=jdm
Browse files Browse the repository at this point in the history
Fix panic when converting from Rgb24 to Rgba32

Created new struct `BlitRgb24ToRgba32` implementing `Blit` to facilitate the conversion.

Fixes #190.
  • Loading branch information
bors-servo authored Mar 27, 2022
2 parents 59213db + 07181e1 commit c359470
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ impl Canvas {
}
(Format::Rgb24, Format::Rgba32) => self
.blit_from_with::<BlitRgba32ToRgb24>(dst_rect, src_bytes, src_stride, src_format),
(Format::Rgba32, Format::Rgb24)
| (Format::Rgba32, Format::A8)
| (Format::A8, Format::Rgba32) => unimplemented!(),
(Format::Rgba32, Format::Rgb24) => self
.blit_from_with::<BlitRgb24ToRgba32>(dst_rect, src_bytes, src_stride, src_format),
(Format::Rgba32, Format::A8) | (Format::A8, Format::Rgba32) => unimplemented!(),
}
}

Expand Down Expand Up @@ -307,3 +307,16 @@ impl Blit for BlitRgba32ToRgb24 {
}
}
}

struct BlitRgb24ToRgba32;

impl Blit for BlitRgb24ToRgba32 {
fn blit(dest: &mut [u8], src: &[u8]) {
for (dest, src) in dest.chunks_mut(4).zip(src.chunks(3)) {
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = 255;
}
}
}

0 comments on commit c359470

Please sign in to comment.