Skip to content
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

Add pixel getter #34

Merged
merged 3 commits into from
Sep 29, 2022
Merged

Add pixel getter #34

merged 3 commits into from
Sep 29, 2022

Conversation

rfuest
Copy link
Member

@rfuest rfuest commented Sep 29, 2022

This PR adds pixel getters to Bmp and RawBmp. It doesn't yet implement the new ImagePixelGetter trait (embedded-graphics/embedded-graphics#612) to stay compatible with e-g 0.7.

match self.header.bpp {
Bpp::Bits1 => RawDataSlice::<RawU1, LittleEndian>::new(row)
.into_iter()
.nth(p.x as usize)
Copy link

@mbuesch mbuesch Sep 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using nth() seems to be rather expensive for random pixel lookup.
In Bmp format it should be possible to lookup pixels in the raw data block in O(1) time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a specialized implementation of nth which is O(1).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RawDataSlice and the iterators are included in embedded-graphics: https://github.com/embedded-graphics/embedded-graphics/blob/master/src/iterator/raw.rs

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Does that also count for the nth in line 100?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Chunks does also implements nth: https://doc.rust-lang.org/src/core/slice/iter.rs.html#1501

There might be some performance gain by not using Chunks, but that could be explored later. My focus for this PR was getting a working implementation not necessarily the fastest.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice. Looks good to me then.

Copy link
Member

@jamwaffles jamwaffles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beat me to it! Looks good to me, although perhaps adding a (not doc) comment to explain the performance in nth().

@rfuest rfuest merged commit 58c8e3f into embedded-graphics:master Sep 29, 2022
@rfuest rfuest deleted the pixel-getter branch September 29, 2022 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants