I found a code on a forum to convert a bmp monochrome to C array, I want to use the array for microcontroller graphic LCD 128x64. The output array is 1024 bytes. Color depth is 1 as it's black and white only.
At first I got two problems:
1. Output is flipped upside down
2. The image get misplaced on the LCD
This is the code:
#include <stdio.h> #include <stdint.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; short arr[1024]; int main(void){ short x= 0 ,y = 0 , p = 0; short pixelValue = 0 ; int myPalette[2]; FILE *BMPFile = fopen ("bmp.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); fread(&myPalette[0], 4, 2, BMPFile); printf("\nType:%hd and Type in %x\n", Header.Type,Header.Type); printf("Size:%ld\n", Header.Size); 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; Header.biHeight = 64; //////////////////////////////////////////////////////////////////// // #1 for(y = 0; y < Header.biHeight; y++){ for(x = 0; x < Header.biWidth; x++){ fread(&pixelValue, sizeof(unsigned char), 1, BMPFile); arr[(y*16)+x] = pixelValue; printf("0x%.2x, ",pixelValue); // printf("%d ",(y*16)+x); // to print the index counting -- I did it for testing if(x%16 == 15){printf("\n");} // this is because the lcd width is divided on 16 bytes = 128 bits } } fclose(BMPFile); //////////////////////////////////////////////////////////////////// // #2 // in this part I tried to reverse printing the array content but it works differently that the one that stores the bytes according to x, y indexing //for(x=1023; x>=0; x--){ // printf("0x%.2x, ",arr[x]); // if(x%16 == 0){printf("\n");} //} return 0; }
So, now what I do to test the array instead of uploading it to the microcontroller. I found a website that emulate exactly the resolution and the same shape on my LCD.
image2cpp
First it gives two options, upload and image or paste the array. So I copy the output of the code from output console window and paste it, then choose read as horizontal.
////////////////////////////////////////////////////////////////////////////////////////////////////
Now what I did:
Actually I only solved one problem, which is the misplacing of the picture by commenting this line:
fread(&Header.Reserve1, sizeof(Header.Reserve1) , 1, BMPFile);
It's already commented in the code I posted, but I couldn't flip the image vertically.
Here's an example array output to paste in the website:
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x01, 0xf8, 0x1f, 0xf8, 0x1f, 0xf0, 0x3c, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x78, 0x0f, 0xf8, 0x1f, 0xf0, 0x1c, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x38, 0x0f, 0xf0, 0x1f, 0xf0, 0x1c, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x38, 0x0f, 0xf0, 0x0f, 0xf0, 0x1c, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x38, 0x0f, 0xe0, 0x0f, 0xf0, 0x1c, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x18, 0x0f, 0xe0, 0x07, 0xf0, 0x1c, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x18, 0x0f, 0xe0, 0x07, 0xf0, 0x1c, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x18, 0x0f, 0xc0, 0x07, 0xf0, 0x1c, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xf0, 0x18, 0x0f, 0xc0, 0x03, 0xf0, 0x1c, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xf0, 0x18, 0x0f, 0xc0, 0x03, 0xf0, 0x1c, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xf0, 0x18, 0x0f, 0x80, 0x01, 0xf0, 0x1c, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xf0, 0x38, 0x0f, 0x80, 0x01, 0xf0, 0x1c, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x38, 0x1f, 0x01, 0x81, 0xf0, 0x1c, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x38, 0x1f, 0x01, 0x80, 0xf0, 0x1c, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x78, 0x1f, 0x01, 0x80, 0xf0, 0x1c, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0xf8, 0x1e, 0x03, 0x80, 0xf0, 0x1c, 0x07, 0xff, 0x80, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x78, 0x1e, 0x03, 0xc0, 0x70, 0x1c, 0x07, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x78, 0x1e, 0x03, 0xc0, 0x70, 0x1c, 0x07, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xc0, 0x38, 0x0c, 0x07, 0xe0, 0x70, 0x1c, 0x07, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xe0, 0x38, 0x0c, 0x07, 0xe0, 0x30, 0x1c, 0x07, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xe0, 0x38, 0x0c, 0x07, 0xe0, 0x30, 0x1c, 0x07, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xe0, 0x38, 0x18, 0x0f, 0xe0, 0x10, 0x1c, 0x07, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xe0, 0x38, 0x00, 0x0f, 0xf0, 0x10, 0x1c, 0x07, 0xff, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x38, 0x00, 0x0f, 0xf0, 0x00, 0x1c, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x38, 0x00, 0x1f, 0xf8, 0x00, 0x1c, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x38, 0x00, 0x1f, 0xf8, 0x00, 0x1c, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x78, 0x00, 0x3f, 0xf8, 0x00, 0x1c, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x78, 0x00, 0x3f, 0xfc, 0x00, 0x1c, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x3f, 0xfc, 0x00, 0x1c, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x7f, 0xfc, 0x00, 0x1c, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
This is the actual image example:

///////////////////////////////////////////////////////////////////////////////////////////////////
1. I tried to comment/uncomment the fread lines, but I don't know what I'm doing exactly, the one of the misplacing was just by coincidence.
2. Also I want to know why commenting/uncommenting the fread lines, actually affect pixelValue ?
3. I want to learn how to control this code and know how to adjust it to what I want.
Thanks in advance,