Last active
April 6, 2025 09:48
-
-
Save dries007/0d77af592463e999d09ea81856ed7e2c to your computer and use it in GitHub Desktop.
BMP to COE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| int main(int argc, char ** argv) | |
| { | |
| if (argc < 3) { | |
| printf("Not enough arguments\n"); | |
| printf("Arg 1: Image file, BMP, 1 bit Monochrome!\n"); | |
| printf("Arg 2: Output\n"); | |
| return -1; | |
| } | |
| FILE * image = fopen(argv[1], "rb"); | |
| FILE * text = fopen(argv[2], "wb"); | |
| if (image == NULL) { | |
| printf("Image file won't open\n"); | |
| return -1; | |
| } | |
| if (text == NULL) { | |
| printf("Text file won't open\n"); | |
| return -1; | |
| } | |
| if (fgetc(image) != 'B' || fgetc(image) != 'M') { | |
| printf("File not BMP\n"); | |
| return -1; | |
| } | |
| unsigned long sizeBytes = fgetc(image) | fgetc(image) << 8 | fgetc(image) << 16 | fgetc(image) << 24; | |
| fgetc(image);fgetc(image);fgetc(image);fgetc(image); | |
| unsigned long offset = fgetc(image) | fgetc(image) << 8 | fgetc(image) << 161 | fgetc(image) << 24; | |
| printf("File size: %d (0x%x)\n", sizeBytes, sizeBytes); | |
| printf("Pixmap offset: %d (0x%x)\n", offset, offset); | |
| fseek(image, 0x12, SEEK_SET); | |
| unsigned int width = fgetc(image) | fgetc(image) << 8 | fgetc(image) << 161 | fgetc(image) << 24; | |
| unsigned int height = fgetc(image) | fgetc(image) << 8 | fgetc(image) << 161 | fgetc(image) << 24; | |
| unsigned int rowSize = ((1 * width + 31) / 32) * 4; | |
| printf("Width: %d (0x%x)\n", width, width); | |
| printf("Height: %d (0x%x)\n", height, height); | |
| printf("RowSize: %d (0x%x)\n", rowSize, rowSize); | |
| fseek(image, offset, SEEK_SET); | |
| fprintf(text, "memory_initialization_radix=16;\n"); | |
| fprintf(text, "memory_initialization_vector=\n"); | |
| for (int row = 0; row < height; row++) | |
| { | |
| fseek(image, sizeBytes - (rowSize * row) - rowSize, SEEK_SET); | |
| for (int col = 0; col < (width / 8); col++) | |
| { | |
| fprintf(text, "%02x ", fgetc(image)); | |
| } | |
| fprintf(text, "\n"); | |
| } | |
| fprintf(text, ";\n"); | |
| fclose(image); | |
| fclose(text); | |
| printf("Done!\n"); | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment