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

Display a bitmap with the DrawTile function... #2531

Open
FredM67 opened this issue Nov 8, 2024 · 7 comments
Open

Display a bitmap with the DrawTile function... #2531

FredM67 opened this issue Nov 8, 2024 · 7 comments
Milestone

Comments

@FredM67
Copy link

FredM67 commented Nov 8, 2024

I've a bitmap of 72x64, which I can display without problem in "buffer mode".
Since this method needs too much ram, I wanted to draw this bitmap with the drawTile function.
I've tried to cut the bitmap in any possible way, I just get "garbage" on the display.
Here's the code:

const unsigned char logo_bmp[] PROGMEM = {/*DATA*/};
constexpr uint8_t TILE_WIDTH{8};
constexpr uint8_t TILE_HEIGHT{8};
void drawTile(U8X8_SSD1306_128X64_NONAME_HW_I2C &u8x8, const uint8_t *bitmap, uint8_t tileX, uint8_t tileY)
{
  uint8_t tile[TILE_HEIGHT];
  for (uint8_t i = 0; i < TILE_HEIGHT; i++)
  {
   tile[i] = pgm_read_byte(bitmap + (tileY * TILE_HEIGHT + i) * (72 / 8) + tileX);
  }
  u8x8.drawTile(tileX, tileY, 1, tile);
}

and then in setup:

  // Loop through the bitmap and draw each tile
  for (uint8_t y = 0; y < (64 / TILE_HEIGHT); y++)
  {
    for (uint8_t x = 0; x < (72 / TILE_WIDTH); x++)
    {
      drawTile(u8x8, logo_bmp, x, y);
    }
  }

Could someone tell me what I'm doing wrong ?
Or is it simply impossible to do that ?!?
Or is the drawTile method broken ?

Thx
Fred

@olikraus
Copy link
Owner

olikraus commented Nov 8, 2024

I think you need a bitlevel converion of the 8x8 bitmatrix.
A tile (usually) is a bit matrix which looks like this: https://github.com/olikraus/u8g2/wiki/u8x8reference#drawtile

Whereas a BMP has a mirrored bit structure:
byte 0: bit 7, bit 6, bit 5..., bit 0
byte 1: bit 7, bit 6, ...

ok I even dont know if the the BMP starts with MSB or LSB... needs to be figured out...

@FredM67
Copy link
Author

FredM67 commented Nov 8, 2024

Well, I've read all your doc. I used the tool https://javl.github.io/[image2cpp](https://javl.github.io/image2cpp/)/ and tried a lot of combination, including "Swap bits in byte" with the remarks "Useful when working with the u8g2 library."... But somehow, I couldn't find a working solution :-(

@olikraus
Copy link
Owner

I think that online tool is not able to swap the bytes...

olikraus added a commit that referenced this issue Nov 10, 2024
@olikraus
Copy link
Owner

olikraus commented Nov 10, 2024

I have created an example:

uint8_t *get_tile_from_xbm(uint8_t tx, uint8_t ty, uint8_t xbm_byte_width, const unsigned char *xbm)
{
static uint8_t d[8];
uint8_t mask = 1;
uint8_t i;
const uint8_t *p = xbm;
p += tx;
p += xbm_byte_width*ty*8;
for( i = 0; i < 8; i++ )
d[i] = 0;
for( i = 0; i < 8; i++ )
{
if ( *p & 1 ) d[0] |= mask;
if ( *p & 2 ) d[1] |= mask;
if ( *p & 4 ) d[2] |= mask;
if ( *p & 8 ) d[3] |= mask;
if ( *p & 16 ) d[4] |= mask;
if ( *p & 32 ) d[5] |= mask;
if ( *p & 64 ) d[6] |= mask;
if ( *p & 128 ) d[7] |= mask;
mask <<= 1;
p += xbm_byte_width;
}
return d;
}
void u8x8_draw_xbm(uint8_t tx, uint8_t ty, uint8_t xbm_width, uint8_t xbm_height, const unsigned char *xbm)
{
uint8_t x, y;
uint8_t *tile;
for( y = 0; y < xbm_height/8; y++ )
{
for( x = 0; x < xbm_width/8; x++ )
{
tile = get_tile_from_xbm(x, y, xbm_width/8, xbm);
u8x8_DrawTile(&u8x8, tx+x, ty+y, 1, tile);
// u8x8.drawTile(tx+x, ty+y, 1, tile);
}
}
}

You probably want to use line 64 instead of line 63.
Moreover this procedure works for XBM, so best is to take your image and save the image as .xbm with "gimp" tool.
Limitation:

  • not speed optimized
  • image width and height must be multiple of 8

@olikraus olikraus added this to the 2.36 milestone Nov 10, 2024
@FredM67
Copy link
Author

FredM67 commented Nov 10, 2024

Thx a lot, I'll try it out tomorrow.

olikraus added a commit that referenced this issue Nov 11, 2024
@olikraus
Copy link
Owner

I have created a complete Arduino example, which also puts the image into PROGMEM area: https://github.com/olikraus/u8g2/blob/b88a767cdfff3fc5c5029ba8b5b2b1470ef60400/sys/arduino/u8x8/DrawXBM/DrawXBM.ino

@FredM67
Copy link
Author

FredM67 commented Nov 11, 2024

A huge thank you for your help, the new example...
I works like a charm ;-)

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

No branches or pull requests

2 participants