u8g2 icon indicating copy to clipboard operation
u8g2 copied to clipboard

(Proposal) Optimization for draw XBMP

Open v-ivanyshyn opened this issue 7 months ago • 3 comments

Current implementation of void u8g2_DrawHXBMP(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t len, const uint8_t *b) renders XBM image pixel-by-pixel using u8g2_DrawHVLine() function. It may be optimized to render horizontal line not of 1px width, but until current bit from the image remains the same. Here is the code patch:

replace:

    if( current_bit ) {
      u8g2->draw_color = color;
      u8g2_DrawHVLine(u8g2, x, y, 1, 0);
    } else if( u8g2->bitmap_transparency == 0 ) {
      u8g2->draw_color = ncolor;
      u8g2_DrawHVLine(u8g2, x, y, 1, 0);
    }
   
    x++;
    mask <<= 1;
    if ( mask == 0 )
    {
      mask = 1;
      b++;
    }
    len--;

with:

    int run_length = 0;
    // Determine the run length of consecutive bits with the same color
    while (len > 0 && ((u8x8_pgm_read(b) & mask) == current_bit))
    {
        run_length++;
        x++;
        mask <<= 1;
        if (mask == 0)
        {
            mask = 1;
            b++;
        }
        len--;
    }
    if (current_bit)
    {
        u8g2->draw_color = color;
        u8g2_DrawHVLine(u8g2, x - run_length, y, run_length, 0);
    }
    else if (u8g2->bitmap_transparency == 0)
    {
        u8g2->draw_color = ncolor;
        u8g2_DrawHVLine(u8g2, x - run_length, y, run_length, 0);
    }

I've tested it on STM32F103 with 2 images: image 101x61px performance using current approach: 23730 us performance using proposed approach: 12537 us

image

94x60px performance using current approach: 21430 us performance using proposed approach: 16454 us

Looks like pretty solid improvement in performance. All credits for the solution to ChatGPT

v-ivanyshyn avatar Jul 13 '24 02:07 v-ivanyshyn