-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rudimentary support for BMPs with alpha masks #43
Open
samcday
wants to merge
2
commits into
embedded-graphics:main
Choose a base branch
from
samcday:alpha-hack
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,6 +209,7 @@ pub use raw_iter::{RawPixel, RawPixels}; | |
pub struct Bmp<'a, C> { | ||
raw_bmp: RawBmp<'a>, | ||
color_type: PhantomData<C>, | ||
alpha_bg: Rgb888, | ||
} | ||
|
||
impl<'a, C> Bmp<'a, C> | ||
|
@@ -225,9 +226,18 @@ where | |
Ok(Self { | ||
raw_bmp, | ||
color_type: PhantomData, | ||
alpha_bg: Rgb888::BLACK, | ||
}) | ||
} | ||
|
||
/// If this image contains transparent pixels (pixels with an alpha channel), then blend these | ||
/// pixels with the provided color. Note that this will only be used when drawing to a target. | ||
/// It will not be applied when querying pixels from the image. | ||
pub fn with_alpha_bg<BG: Into<Rgb888>>(mut self, alpha_bg: BG) -> Self { | ||
self.alpha_bg = alpha_bg.into(); | ||
self | ||
} | ||
|
||
/// Returns an iterator over the pixels in this image. | ||
/// | ||
/// The iterator always starts at the top left corner of the image, regardless of the row order | ||
|
@@ -362,6 +372,37 @@ where | |
RawColors::<RawU32>::new(&self.raw_bmp) | ||
.map(|raw| Rgb888::from(RawU24::new(raw.into_inner())).into()), | ||
), | ||
ColorType::Argb8888 => { | ||
target.fill_contiguous( | ||
&area, | ||
RawColors::<RawU32>::new(&self.raw_bmp).map(|raw| { | ||
// integer blending approach from https://stackoverflow.com/a/12016968 | ||
let v = raw.into_inner(); | ||
let mut alpha = v & 0xFF; | ||
let inv_alpha = 256 - alpha; | ||
alpha += 1; | ||
if alpha == 0 { | ||
// pixel is completely transparent, use bg color | ||
self.alpha_bg | ||
} else if alpha == 255 { | ||
// pixel is completely opaque, just use its color | ||
Rgb888::from(RawU24::new(v >> 8)) | ||
Comment on lines
+383
to
+389
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't have time to thoroughly check the blending code, but the checking for |
||
} else { | ||
// pixel has transparency, blend with BG color | ||
let col = Rgb888::from(RawU24::new(v >> 8)); | ||
Rgb888::new( | ||
((alpha * col.r() as u32 + inv_alpha * self.alpha_bg.r() as u32) | ||
>> 8) as u8, | ||
((alpha * col.g() as u32 + inv_alpha * self.alpha_bg.g() as u32) | ||
>> 8) as u8, | ||
((alpha * col.b() as u32 + inv_alpha * self.alpha_bg.b() as u32) | ||
>> 8) as u8, | ||
) | ||
} | ||
.into() | ||
}), | ||
) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -421,6 +462,10 @@ where | |
.raw_bmp | ||
.pixel(p) | ||
.map(|raw| Rgb888::from(RawU24::from_u32(raw)).into()), | ||
ColorType::Argb8888 => self | ||
.raw_bmp | ||
.pixel(p) | ||
.map(|raw| Rgb888::from(RawU24::from_u32(raw >> 8)).into()), | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be a regular setter (
set_alpha_bg<...>(&mut self, alpha_bg: BG)
) instead of this builder style method. Using a mutable reference instead of having to transfer ownership makes it more flexible.An even better solution would be to add a wrapper type around the
Bmp
type, that sets the background color, but this would be a lot more work:This would allow the
Bmp
object to be drawn with any background color without needing a mutable reference or even ownership of the object. You could then easily implement something like:This would still kind of work with a mutable reference to the icon, but wouldn't work with the original transfer of ownership in the
with_alpha_bg
method.