Skip to main content
antonius
Associate III
April 11, 2014
Question

Decoding bmp ?

  • April 11, 2014
  • 90 replies
  • 37501 views
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
    This topic has been closed for replies.

    90 replies

    antonius
    antoniusAuthor
    Associate III
    April 21, 2014
    Posted on April 21, 2014 at 07:28

    I have test6.bmp which is 2x2 pixel 24 bits:

    0690X00000602kbQAA.jpg

    Is offset 0x36 which value = 0xC0 equal to coordinate 0,0 (red color) ?

    Red color is defined from 0x36 until 0x38 ? ==> 0xC0,0x40,0x40, then (0,1)

    green color 0x00,0xFF,0xFF, then (1,0),blue color = 0x82,0x04,0x40,then (1,1) yellow color =

    0x20, 0xE0,0x40

    0690X00000602tqQAA.jpg

    and then following :

    0690X00000602tvQAA.jpg

    antonius
    antoniusAuthor
    Associate III
    April 22, 2014
    Posted on April 22, 2014 at 02:17

    Am I on the right calculation and way of thinking ?

    thanks

    antonius
    antoniusAuthor
    Associate III
    April 22, 2014
    Posted on April 22, 2014 at 02:30

    Does it always begin from offset 0x3E on the file no matter the resolution of the picture ?

    It's the same for 1bit,8bits,16bits and 24bits picture ? I can see from every pictures, it's started from 0x36, reference :

    Start of pixel array (bitmap data)

    36h 3

    00 00 FF

    0 0 255 Red, Pixel (0,1)

    http://en.wikipedia.org/wiki/BMP_file_format#Bitmap_file_header

    Thanks
    antonius
    antoniusAuthor
    Associate III
    April 27, 2014
    Posted on April 27, 2014 at 06:36

    I tried this code :

    
    //262K picture
    
    uint16_t destx;
    
    uint16_t desty;
    
    
    delay_ms(1500);
    
    LCD_Clear(Black); 
    
    res = f_open(&fsrc, filename, FA_READ);
    
    
    f_read(&fsrc, hdr_256, 
    sizeof
    (hdr_256),&bytesread); 
    // Read header
    
    off = *((uint32_t *)&hdr_256[0x0A]); 
    // offset to rasters
    
    
    sourcewidth = *((uint32_t *)&hdr_256[0x12]); 
    // X pixels
    
    sourceheight = *((uint32_t *)&hdr_256[0x16]); 
    // Y pixels
    
    
    
    f_lseek(&fsrc, off); 
    // Seek to the data
    
    destx = 0;
    
    desty = 0;
    
    for
    (y = 0, ny = 0; y < sourceheight; ny += 3, ny++)
    
    
    {
    
    if
    (ny == 5)
    
    {
    
    y++;
    
    ny = 0;
    
    }
    
    for
    (x = 0,nx = 0; x < sourcewidth; x += 3,nx++)
    
    {
    
    uint32_t rgb;
    
    if
    (nx == 5)
    
    {
    
    x++;
    
    nx = 0;
    
    }
    
    f_read(&fsrc, &rgb, 3, &bytesread); 
    // Read pixel B G R
    
    // RRRRRRRRGGGGGGGGBBBBBBBB 8:8:8
    
    // RRRRRGGGGGGBBBBB 5:6:5
    
    rgb = ((rgb & 0xF80000) >> 8) | ((rgb & 0x00FC00) >> 5) | ((rgb & 0x0000F8) >> 3);
    
    LCD_SetPoint(desty, destx, rgb);
    
    destx++;
    
    if
    (destx == 320)
    
    {
    
    destx = 0;
    
    desty++;
    
    }
    
    }
    
    }
    
    f_close(&fsrc);
    
    delay_ms(2000);

    for displaying :

    http://iphotobucket.com/albums/p231/picture_77/test32_zpsa24eb6e1.png

    http://iphotobucket.com/albums/p231/picture_77/test32_zpsa24eb6e1.png

    but it's displaying :

    http://iphotobucket.com/albums/p231/picture_77/IMG_20140427_095513_zpsd8864b8e.jpg

    Does anyone have a clue ? thanks
    Tesla DeLorean
    Guru
    April 27, 2014
    Posted on April 27, 2014 at 18:40

    Yes, the logic there seems rather broken, and the PNG won't be handled by BMP code

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    antonius
    antoniusAuthor
    Associate III
    April 27, 2014
    Posted on April 28, 2014 at 01:26

    I'm not using *.png on my SD card, that's for sure, photo bucket convert it...

    I tried this function

    
    void LCD_BMP_reading_shrink_test(char filename[120])
    
    {
    
    char temp_buffer[100],line[512];
    
    int count,z,bmpData,bmpAddr;
    
    int x = 0;
    
    int i,y = 0;
    
    int xx, yy,ny,nx;
    
    uint8_t hdr[0x28];
    
    UINT bytesread;
    
    uint8_t hdr_256[0x36];
    
    static uint16_t w[256];
    
    uint32_t off,sourcewidth,sourceheight,pixeloff;
    
    
    //262K picture
    
    uint16_t destx;
    
    uint16_t desty;
    
    
    delay_ms(1500);
    
    LCD_Clear(Black); 
    
    res = f_open(&fsrc, filename, FA_READ);
    
    
    f_read(&fsrc, hdr_256, sizeof(hdr_256),&bytesread); // Read header
    
    off = *((uint32_t *)&hdr_256[0x0A]); // offset to rasters
    
    
    sourcewidth = *((uint32_t *)&hdr_256[0x12]); // X pixels
    
    sourceheight = *((uint32_t *)&hdr_256[0x16]); // Y pixels
    
    
    pixeloff = off + (y * 1024 * 3) + (x * 3); // Assuming three bytes per pixel
    
    //f_lseek(&fsrc, pixeloff); // Seek to the desired pixel 
    
    f_lseek(&fsrc, off); // Seek to the data
    
    
    destx = 0;
    
    desty = 0;
    
    //for(y = 0, ny = 0; y < 
    sourceheight
    ; ny += 3, ny++)
    
    for(
    y
    = 
    0
    , 
    ny
    = 
    0
    ; y < sourceheight; y += 3, ny++)
    
    
    {
    
    if (ny == 5)
    
    {
    
    y++;
    
    ny
    = 
    0
    ;
    
    }
    
    
    for(
    x
    = 
    0
    ,
    nx
    = 
    0
    ; x < sourcewidth; x += 3,nx++)
    
    {
    
    uint32_t rgb;
    
    if (nx == 5)
    
    {
    
    x++;
    
    nx
    = 
    0
    ;
    
    }
    
    f_read(&fsrc, &rgb, 3, &bytesread); // Read pixel B G R
    
    // RRRRRRRRGGGGGGGGBBBBBBBB 8:8:8
    
    // RRRRRGGGGGGBBBBB 5:6:5
    
    rgb = ((rgb & 0xF80000) >> 8) | ((rgb & 0x00FC00) >> 5) | ((rgb & 0x0000F8) >> 3);
    
    LCD_SetPoint(desty, destx, rgb);
    
    
    
    
    destx++;
    
    if(destx == 320)
    
    {
    
    destx = 0;
    
    desty++;
    
    }
    
    }
    
    }
    
    f_close(&fsrc);
    
    delay_ms(2000);
    
    
    }

    I want to convert 1024*768 :

    http://iphotobucket.com/albums/p231/picture_77/test32_zpsa24eb6e1.png

    The result I got from that function :

    http://iphotobucket.com/albums/p231/picture_77/IMG_20140427_095513_zpsd8864b8e.jpg

    Do you have idea where I miss here ? thanks
    antonius
    antoniusAuthor
    Associate III
    April 28, 2014
    Posted on April 28, 2014 at 13:06

    I was trying to display

    0690X00000602dMQAQ.jpg but I got like this on LCD : 0690X00000602uPQAQ.jpg The code I used :

    
    int
    count,z,bmpData,bmpAddr;
    
    int
    x = 0;
    
    int
    i,y = 0;
    
    int
    xx, yy,ny,nx;
    
    uint8_t hdr[0x28];
    
    UINT
    bytesread;
    
    uint8_t hdr_256[0x36];
    
    static
    uint16_t w[256];
    
    uint32_t off,sourcewidth,sourceheight,pixeloff;
    
    
    //262K picture
    
    uint16_t destx;
    
    uint16_t desty;
    
    
    delay_ms(1500);
    
    LCD_Clear(Black); 
    
    res = f_open(&fsrc, filename, FA_READ);
    
    
    f_read(&fsrc, hdr_256, 
    sizeof
    (hdr_256),&bytesread); 
    // Read header
    
    off = *((uint32_t *)&hdr_256[0x0A]); 
    // offset to rasters
    
    
    sourcewidth = *((uint32_t *)&hdr_256[0x12]); 
    // X pixels
    
    sourceheight = *((uint32_t *)&hdr_256[0x16]); 
    // Y pixels
    
    
    
    
    destx = 0;
    
    desty = 0;
    
    //for(y = 0, ny = 0; y < sourceheight; ny += 3, ny++)
    
    for
    (y = 0, ny = 0; y < sourceheight; y += 3, ny++)
    
    
    {
    
    if
    (ny == 5)
    
    {
    
    y++;
    
    ny = 0; 
    
    }
    
    
    for
    (x = 0,nx = 0; x < sourcewidth; x += 3,nx++)
    
    {
    
    uint32_t rgb;
    
    if
    (nx == 5)
    
    {
    
    x++;
    
    nx = 0;
    
    }
    
    pixeloff = off + (y * 1024 * 3) + (x * 3); 
    // Assuming three bytes per pixel
    
    f_lseek(&fsrc, pixeloff); 
    // Seek to the desired pixel 
    
    //f_lseek(&fsrc, off); // Seek to the data
    
    f_read(&fsrc, &rgb, 3, &bytesread); 
    // Read pixel B G R
    
    // RRRRRRRRGGGGGGGGBBBBBBBB 8:8:8
    
    // RRRRRGGGGGGBBBBB 5:6:5
    
    rgb = ((rgb & 0xF80000) >> 8) | ((rgb & 0x00FC00) >> 5) | ((rgb & 0x0000F8) >> 3);
    
    LCD_SetPoint(desty, destx, rgb);
    
    //sprintf(temp_buffer,''%.2d'',rgb);printf(temp_buffer);printf(''
    '');
    
    
    
    destx++;
    
    if
    (destx == 320)
    
    {
    
    destx = 0;
    
    desty++;
    
    }
    
    }
    
    }
    
    f_close(&fsrc);
    
    delay_ms(2000);

    Any clues ? thanks
    Tesla DeLorean
    Guru
    April 28, 2014
    Posted on April 28, 2014 at 14:27

    int count,z,bmpData,bmpAddr;
    int x = 0;
    int i,y = 0;
    int xx, yy;
    uint8_t hdr[0x28];
    UINT bytesread;
    uint8_t hdr_256[0x36];
    static uint16_t w[256];
    uint32_t off,sourcewidth,sourceheight,pixeloff;
    //262K picture
    delay_ms(1500);
    LCD_Clear(Black);
    res = f_open(&fsrc, filename, FA_READ);
    f_read(&fsrc, hdr_256, sizeof(hdr_256),&bytesread); // Read header
    off = *((uint32_t *)&hdr_256[0x0A]); // offset to rasters
    sourcewidth = *((uint32_t *)&hdr_256[0x12]); // X pixels
    sourceheight = *((uint32_t *)&hdr_256[0x16]); // Y pixels
    for(y = 0; y < 
    sourceheight
    ; y += 3)
    {
    for(
    x
    = 
    0
    ; x < sourcewidth; x += 3)
    {
    pixeloff
    = off + (((y * sourcewidth) + x) * 3); // Assuming three bytes per pixel
    f_lseek(&fsrc, pixeloff); // Seek to the desired pixel
    f_read(&fsrc, &rgb, 3, &bytesread); // Read pixel B G R
    // RRRRRRRRGGGGGGGGBBBBBBBB 8:8:8
    // RRRRRGGGGGGBBBBB 5:6:5
    rgb = ((rgb & 0xF80000) >> 8) | ((rgb & 0x00FC00) >> 5) | ((rgb & 0x0000F8) >> 3);
    LCD_SetPoint(y/3, x/3, rgb);
    }
    }
    f_close(&fsrc);
    delay_ms(2000);

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    antonius
    antoniusAuthor
    Associate III
    April 28, 2014
    Posted on April 28, 2014 at 14:38

    It does the task but why can't I see the white color ? suppose to be blue and white,

    but on LCD blue and dot dot white not complete white ? Have a look...

    0690X00000602dNQAQ.jpg

    Thanks

    Tesla DeLorean
    Guru
    April 28, 2014
    Posted on April 28, 2014 at 15:31

    I'd look at the BMP, but the JPG/PNG provided so far are not usable by me, attach the real BMP to your post as an attachment.

    Also please don't use line-numbering when posting code, it just makes it more difficult to paste.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..