FrameBuffer blit palette

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

FrameBuffer blit palette

Post by rdagger » Fri Aug 05, 2022 4:27 am

I’m trying to blit a MONO_VLSB framebuffer onto a GS4_HMSB framebuffer. The image shows up as very dim because instead of converting the white pixels from 1 to 15, they are remaining at 1 which is a very dark gray on the grayscale. I tried setting up a palette framebuffer to handle the conversion, but I can’t figure out the correct syntax.

I've tried several permutations of the following code with no luck:

Code: Select all

buf = bytearray(b'\x00\x01')
fb_palette = FrameBuffer(buf, 2, 1, GS4_HMSB)
fb_target.blit(fb_source, 0, 0, -1, fb_palette)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: FrameBuffer blit palette

Post by pythoncoder » Fri Aug 05, 2022 10:56 am

Here is how I use it to blit monochrome glyphs onto color framebufs. The boolpalette class has two pixels in foreground and background colors:

Code: Select all

class BoolPalette(framebuf.FrameBuffer):

    def __init__(self, mode):
        buf = bytearray(4)  # OK for <= 16 bit color
        super().__init__(buf, 2, 1, mode)
    
    def fg(self, color):  # Set foreground color
        self.pixel(1, 0, color)

    def bg(self, color):
        self.pixel(0, 0, color)
It is used as follows:

Code: Select all

        palette = self.device.palette
        palette.bg(self.fgcolor if invert else self.bgcolor)
        palette.fg(self.bgcolor if invert else self.fgcolor)
        self.device.blit(fbc, s.text_col, s.text_row, -1, palette)
Sources https://github.com/peterhinch/micropyth ... palette.py and https://github.com/peterhinch/micropyth ... /writer.py
Peter Hinch
Index to my micropython libraries.

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: FrameBuffer blit palette

Post by rdagger » Fri Aug 05, 2022 3:38 pm

@pythoncoder So helpful, thanks! I understand now. Since GS4 stores two 4-bit grayscale colors per byte, the palette buffer only needs 1 byte for mono to GS4 conversion (1 nibble for background and 1 nibble for foreground). Here's my working code:

Code: Select all

bg, fg = 0, 15
palette = FrameBuffer(bytearray(1), 2, 1, GS4_HMSB)
palette.pixel(1, 0, fg)
palette.pixel(0, 0, bg)
target.blit(source, 0, 0, -1, palette)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: FrameBuffer blit palette

Post by pythoncoder » Mon Aug 08, 2022 8:07 am

I'm glad you've sorted it. I guess the official docs are rather terse (my fault).
Peter Hinch
Index to my micropython libraries.

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: FrameBuffer blit palette

Post by rdagger » Mon Aug 08, 2022 3:56 pm

pythoncoder wrote:
Mon Aug 08, 2022 8:07 am
I'm glad you've sorted it. I guess the official docs are rather terse (my fault).
Code examples are always more helpful to me rather than explanations. As soon as I saw your code it became clear.

Post Reply