Skip to content

Commit

Permalink
PicoGraphics: Implement RGB888 alpha blending.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Nov 21, 2024
1 parent 18d3ed3 commit ad5120a
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions libraries/pico_graphics/pico_graphics_pen_rgb888.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,35 @@ namespace pimoroni {
}
bool PicoGraphics_PenRGB888::render_tile(const Tile *tile) {
for(int y = 0; y < tile->h; y++) {
uint8_t *palpha = &tile->data[(y * tile->stride)];
uint32_t *pdest = &((uint32_t *)frame_buffer)[tile->x + ((tile->y + y) * bounds.w)];
uint8_t *p_alpha = &tile->data[(y * tile->stride)];
uint32_t *p_dest = &((uint32_t *)frame_buffer)[tile->x + ((tile->y + y) * bounds.w)];
for(int x = 0; x < tile->w; x++) {
uint8_t alpha = *palpha;
uint16_t dest = *p_dest;
uint8_t alpha = *p_alpha;

// TODO: Alpha blending
if(alpha == 0) {
if(alpha == 255) {
*p_dest = color;
}else if(alpha == 0) {
} else {
*pdest = color;
// blend tha pixel
uint32_t sr = (color >> 16) & 0xff;
uint32_t sg = (color >> 8) & 0xff;
uint32_t sb = (color >> 0) & 0xff;

uint32_t dr = (dest >> 16) & 0xff;
uint32_t dg = (dest >> 8) & 0xff;
uint32_t db = (dest >> 0) & 0xff;

uint8_t r = ((sr * alpha) + (dr * (255 - alpha))) >> 8;
uint8_t g = ((sg * alpha) + (dg * (255 - alpha))) >> 8;
uint8_t b = ((sb * alpha) + (db * (255 - alpha))) >> 8;

*p_dest = (r << 16) | (g << 8) | b;
}

pdest++;
palpha++;
p_dest++;
p_alpha++;
}
}

Expand Down

0 comments on commit ad5120a

Please sign in to comment.