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

displayio.Bitmap: Make memoryview()able #4429

Merged
merged 2 commits into from
Mar 18, 2021

Conversation

jepler
Copy link
Member

@jepler jepler commented Mar 17, 2021

This makes a displayio.Bitmap usable as a read-only buffer.
It enables this to work:

>>> b[0] = 32
>>> b[1] = 64
>>> b[8] = 64
>>> ulab.frombuffer(b, dtype=ulab.uint16).reshape((8,8))
array([[32, 64, 0, 0, 0, 0, 0, 0],
       [64, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint16)

By having a buffer-like Bitmap we can now get bitmap data INTO ulab efficiently, just as we recently became able to get it OUT efficiently by addng bitmaptools.arrayblit.

The docs explain that this is really only "defined" for 8/16/32-bit bitmaps where a row is a multiple of 4 bytes. For other cases, we pack bits in odd ways, and/or add padding bytes to the end of a row. I could throw an error in these cases instead, or we could fully document the in-memory format of the bitmap. I'm open to thoughts.

@tannewt tannewt requested a review from kmatch98 March 18, 2021 01:12
@kmatch98
Copy link
Collaborator

I didn't test it but it makes sense to me. This is new to me to define the typecode of data stored in a buffer. Need to lock this away so I'll remember it.

Good example for defining a buffer, see:

int common_hal_displayio_bitmap_get_buffer(displayio_bitmap_t *self, mp_buffer_info_t *bufinfo, mp_uint_t flags)

Copy link
Collaborator

@kmatch98 kmatch98 left a comment

Choose a reason for hiding this comment

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

Didn't test, but seems to make sense. I'm not familiar with the usage of buffers or the notation used in the type definition .buffer_p, so I can't comment on the implementation.

Based on the test code demonstrated, it looks like it performs the function requested, to get ulab access into a bitmap.

As for the color depth, main thing is to keep the color index >= 256 colors if you want to use this in ulab. If folks are clamoring to save memory on bitmaps when using this, perhaps the another way could be developed to handle smaller color palettes.

@dhalbert
Copy link
Collaborator

@jepler Merge if this is ready from your point of view.

@jepler jepler merged commit 623ece2 into adafruit:main Mar 18, 2021
@jepler jepler deleted the displayio-bitmap-memoryview branch November 3, 2021 21:09
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.

3 participants