[SOLVED] How to convert monochrome bmp to C array for lcd128x64 ?

Hi,

I found a code that would do the job, tested it, provided me with the array I want; but, when I display it on the lcd, it’s displayed split in two parts, one is good and the other one is flipped upside down + missing the necessary padding I guess.

Here’s the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct BitMap{
    short     Type;
    long      Size;
    short     Reserve1;
    short     Reserve2;
    long      OffBits;
    long      biSize;
    long      biWidth;
    long      biHeight;
    short     biPlanes;
    short    biBitCount;
    long      biCompression;
    long      biSizeImage;
    long      biXPelsPerMeter;
    long      biYPelsPerMeter;
    long      biClrUsed;
    long      biClrImportant;
} Header;
int main( void )
{
    int Horizontal= 0 ,Vertical = 0 , p = 0;
    unsigned char pixelValue =0 ;

    FILE *BMPFile = fopen ("atmel.bmp", "r");
    if (BMPFile == NULL) return -1;

    fread(&Header.Type,             sizeof(Header.Type), 1, BMPFile);
    fread(&Header.Size,             sizeof(Header.Size), 1, BMPFile);
//    fread(&Header.Reserve1,         sizeof(Header.Reserve1) , 1, BMPFile);
//    fread(&Header.Reserve2,         sizeof(Header.Reserve2) , 1, BMPFile);
    fread(&Header.OffBits,          sizeof(Header.OffBits), 1, BMPFile);
    fread(&Header.biSize,           sizeof(Header.biSize), 1, BMPFile);
    fread(&Header.biWidth,          sizeof(Header.biWidth), 1, BMPFile);
    fread(&Header.biHeight,         sizeof(Header.biHeight) , 1, BMPFile);

    fread(&Header.biPlanes,         sizeof(Header.biClrUsed),  1, BMPFile);
    fread(&Header.biBitCount,       sizeof(Header.biBitCount), 1, BMPFile);
//    fread(&Header.biCompression,    sizeof(Header.biCompression), 1, BMPFile);
//    fread(&Header.biSizeImage,      sizeof(Header.biSizeImage), 1, BMPFile);
//    fread(&Header.biXPelsPerMeter,  sizeof(Header.biXPelsPerMeter), 1, BMPFile);
//    fread(&Header.biYPelsPerMeter,  sizeof(Header.biYPelsPerMeter), 1, BMPFile);
//
//    fread(&Header.biClrUsed,        sizeof(Header.biClrUsed), 1, BMPFile);
//    fread(&Header.biClrImportant,   sizeof(Header.biClrImportant),  1, BMPFile);

    printf("\nType:%hd   and Type in  %x\n", Header.Type,Header.Type);
    printf("Size:%ld\n", Header.Size);
//    printf("Reserve1:%hd\n", Header.Reserve1);
//    printf("Reserve2:%hd\n", Header.Reserve2);
    printf("OffBits:%ld\n", Header.OffBits);
    printf("biSize:%ld\n", Header.biSize);
    printf("Width:%ld\n", Header.biWidth);
    printf("Height:%ld\n", Header.biHeight);
    printf("biPlanes:%hd\n", Header.biPlanes);
    printf("biBitCount:%hd\n", Header.biBitCount);
//    printf("biCompression:%ld\n", Header.biCompression);
//    printf("biSizeImage:%ld\n", Header.biSizeImage);
//    printf("biXPelsPerMeter:%ld\n", Header.biXPelsPerMeter);
//    printf("biYPelsPerMeter:%ld\n", Header.biYPelsPerMeter);
//    printf("biClrUsed:%ld\n", Header.biClrUsed);
//    printf("biClrImportant:%ld\n\n", Header.biClrImportant);

    Header.biWidth = 16;    // changed this instead of the one
                            // from the struct read for actual 128 bit width
    Header.biHeight = 64;   // this is already ok as the lcd is 64 height

    for(Vertical = 0 ;    Vertical <  Header.biHeight ;  Vertical ++){
        for(Horizontal = 0 ; Horizontal <  Header.biWidth ;  Horizontal++){
            fread(&pixelValue, sizeof(pixelValue), 1, BMPFile);
            printf("0x%.2x, ",pixelValue);

            p++;
            if(Horizontal%16 == 15){printf("\n");}
        }
        // Padding in Missing Here

    }
    fclose(BMPFile);
    return 0;
}

This is my output:

This is the original bmp photo:
but there’s a strange thing happened now, when I uploaded it to imgBB, it was converted to this distorted shape:

Where the actual shape is this one:
I had to save it as png format

Now, how to modify the code to get the right conversion ?

OK, I solved the code that convert the bmp to C array.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <windows.h>

BITMAPFILEHEADER    Header1;
BITMAPINFOHEADER    Header2;


int main( void ){
    uint8_t arr1[1024];
    int   myPalette;

    FILE *BMP1 = fopen ("../bmp.bmp", "rb");

    fread(&Header1, sizeof(Header1), 1, BMP1);

    printf("Type:%hd  and Type in  %x\n", Header1.bfType,Header1.bfType);
    printf("bfSize %d\n", Header1.bfSize);
    printf("bfOffBits %d\n", Header1.bfOffBits);

    fread(&Header2, sizeof(Header2), 1, BMP1);
    fread(&myPalette, 4, 2, BMP1);

    printf("biHeight %d \n",            Header2.biHeight);
    printf("biWidth %d \n",             Header2.biWidth);
    printf("biBitCount %d \n",          Header2.biBitCount);
    printf("biClrImportant %d \n",      Header2.biClrImportant);
    printf("biClrUsed %d \n",           Header2.biClrUsed);
    printf("biCompression %d \n",       Header2.biCompression);
    printf("biPlanes %d \n",            Header2.biPlanes);
    printf("biSize %d \n",              Header2.biSize);
    printf("biSizeImage %d \n",         Header2.biSizeImage);
    printf("biXPelsPerMeter %d \n",     Header2.biXPelsPerMeter);
    printf("biYPelsPerMeter %d \n",     Header2.biYPelsPerMeter);

    printf("Size of Header1 + Header2: %ld\n", sizeof(Header1)+sizeof(Header2));

//////////////////////////////////////////////////////////////////////////////////

int x, y, i;
uint8_t pixel;

    for(i=0; i < 1024; i++){
        fread(&pixel,sizeof(pixel),1,BMP1);
        arr1[i] = pixel;
    }

     for(y=63; y>=0; y--){
        for(x=0; x<16; x++){
            printf("0x%.2x, ",arr1[(y*16)+x]);
            if(x%16 == 15){printf("\n");}
        }
    }

    return 0;
}

This topic was automatically closed after 120 days. New replies are no longer allowed.