cancel
Showing results for 
Search instead for 
Did you mean: 

Decoding bmp ?

antonius
Senior
Posted on April 11, 2014 at 11:21

Guys,

Does anyone of you have experience on decoding bmp ?

I want to display bmp file to my LCD...

Any clues ?

thanks

#i-have-a-clue
90 REPLIES 90
antonius
Senior
Posted on April 17, 2014 at 06:51

if I used

1.
for
(y=0; y<320; y++)
2.
{
3.
for
(x=0; x<240; x+=8) 
// advance 8 pixels (one byte of bits)

I got : 0690X00000602tMQAQ.jpg Do you have a clue ? thanks
Posted on April 17, 2014 at 17:25

Got the x/y sizes switched

// a more general solution would read dimensions from header
// and handle errors, etc.
// For the specific BMP, 1-bit per pixel, 320x240
{
int i, x, y;
res = f_open(&fsrc, ''test.bmp'', FA_READ);
f_lseek(&fsrc, 0x3E); // Seek to the data
for(y=0; y<240; y++)
{
for(x=0; x<320; x+=8) // advance 8 pixels (one byte of bits)
{
uint8_t b;
UINT bytesread;
f_read(&fsrc, &b, 1, &bytesread); // Read byte
for(i=0; i<8; i++) // Process bits in byte, MSB first
{
if (b & 0x80) // Pixel On?
LCD_SetPoint(x + i, y, 0x0F0F); // Colour
else
LCD_SetPoint(x + i, y, 0x0000); // Black
b <<= 1;
}
} // x
} // y
f_close(&fsrc);
}

// For the specific BMP, 1-bit per pixel
{
int i, x, y, xx, yy;
uint8_t hdr[0x28];
UINT bytesread;
res = f_open(&fsrc, ''test.bmp'', FA_READ);
f_read(&fsrc, hdr, sizeof(hdr), &bytesread); // Read header
xx = *((uint32_t *)&hdr[0x12]); // X pixels (want multiple of 8)
yy = *((uint32_t *)&hdr[0x16]); // Y pixels 
f_lseek(&fsrc, 0x3E); // Seek to the data
for(y=0; y<yy; y++)
{
for(x=0; x<xx; x+=8) // advance 8 pixels (one byte of bits)
{
uint8_t b;
f_read(&fsrc, &b, 1, &bytesread); // Read byte
for(i=0; i<8; i++) // Process bits in byte, MSB first
{
if (b & 0x80) // Pixel On?
LCD_SetPoint(x + i, y, 0x0F0F); // Colour
else
LCD_SetPoint(x + i, y, 0x0000); // Black
b <<= 1;
}
} // x
} // y
f_close(&fsrc);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
antonius
Senior
Posted on April 18, 2014 at 03:59

Thanks a lot, it works for the star, but why isn't it finished to the end of the screen ?0690X00000602tRQAQ.jpg

antonius
Senior
Posted on April 18, 2014 at 04:00

I haven't tried the second code, is it possible if I have 640x480 and resize directly to 320x240 without a code ( automatically resize )? thanks

antonius
Senior
Posted on April 18, 2014 at 04:10

I tested with different picture, it seems that the x/y is reversed ?

originally :

0690X00000602Q5QAI.bmp

on lcd :

0690X00000602tWQAQ.jpg

antonius
Senior
Posted on April 18, 2014 at 04:20

fixed :


for
(i=0; i<8; i++) 
// Process bits in byte, MSB first

{

if
(b & 0x80) 
// Pixel On?


LCD_SetPoint(y, x+i, 0xF0F0); 
// Colour

else


LCD_SetPoint(y, x+i, 0x0000); 
// Black


b <<= 1;

}

If I want to play with color, what should I change ? for example 256 color of BMP ?
antonius
Senior
Posted on April 18, 2014 at 04:27

http://youtu.be/OOEIqjNn3jw

Posted on April 18, 2014 at 05:03

For 256 colour, you'd need to process a byte at a time, index into the palette table to extract the 24-bit RGB data, and then you'd have to translate that into the colour space of your display.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
antonius
Senior
Posted on April 18, 2014 at 05:58

so byte after 0x3E or after 0x36 since it's 256 color (offset 0x0A), then process a byte at a time, index into the palette table to extract the 24-bit RGB data ....

Am I right ? thanks

antonius
Senior
Posted on April 18, 2014 at 06:00

Pixel format

In a bitmap image file on a disk or a bitmap image in memory, the pixels can be defined by a varying number of bits.

  • The 1-bit per pixel (1bpp) format supports 2 distinct colors, (for example: black and white). The pixel values are stored in each bit, with the first (left-most) pixel in the most-significant bit of the first byte.

    http://en.wikipedia.org/wiki/BMP_file_format#cite_note-DIBhelp-5

    Each bit is an index into a table of 2 colors. An unset bit will refer to the first color table entry, and a set bit will refer to the last (second) color table entry.
  • The 2-bit per pixel (2bpp) format supports 4 distinct colors and stores 4 pixels per 1 byte, the left-most pixel being in the two most significant bits (

    http://en.wikipedia.org/wiki/Windows_CE

    only:

    http://en.wikipedia.org/wiki/BMP_file_format#cite_note-PSformats-16

    ). Each pixel value is a 2-bit index into a table of up to 4 colors.
  • The 4-bit per pixel (4bpp) format supports 16 distinct colors and stores 2 pixels per 1 byte, the left-most pixel being in the more significant

    http://en.wikipedia.org/wiki/Nibble

    .

    http://en.wikipedia.org/wiki/BMP_file_format#cite_note-DIBhelp-5

    Each pixel value is a 4-bit index into a table of up to 16 colors.
  • The 8-bit per pixel (8bpp) format supports 256 distinct colors and stores 1 pixel per 1 byte. Each byte is an index into a table of up to 256 colors.
  • The 16-bit per pixel (16bpp) format supports 65536 distinct colors and stores 1 pixel per 2 byte WORD. Each WORD can define the alpha, red, green and blue

    http://en.wikipedia.org/wiki/Sample_%28graphics%29

    of the pixel.
  • The 24-bit pixel (24bpp) format supports 16,777,216 distinct colors and stores 1 pixel value per 3 bytes. Each pixel value defines the red, green and blue samples of the pixel (8.8.8.0.0 in RGBAX notation). Specifically in the order (blue, green and red, 8-bits per each sample).

    http://en.wikipedia.org/wiki/BMP_file_format#cite_note-DIBhelp-5

  • The 32-bit per pixel (32bpp) format supports 4,294,967,296 distinct colors and stores 1 pixel per 4 byte DWORD. Each DWORD can define the Alpha, Red, Green and Blue samples of the pixel.