Created
September 15, 2018 20:09
-
-
Save leiradel/f8e317e0019ade4c34016a4337cbbcca to your computer and use it in GitHub Desktop.
An image encoder for Gamebuino Classic bitmaps
This file contains 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> | |
#include <stdint.h> | |
/*---------------------------------------------------------------------------*/ | |
/* stb_image config and inclusion */ | |
#define STBI_ASSERT( x ) | |
#define STB_IMAGE_IMPLEMENTATION | |
#include "stb_image.h" | |
/*---------------------------------------------------------------------------*/ | |
int main(int argc, const char* argv[]) { | |
if (argc != 3) { | |
fprintf(stderr, "USAGE: bmpenc inputfile arrayname\n"); | |
return 1; | |
} | |
int width, height; | |
uint32_t* image = (uint32_t*)stbi_load(argv[1], &width, &height, NULL, STBI_rgb_alpha); | |
printf("static const byte %sBlack[] PROGMEM = {\n", argv[2]); | |
printf(" /* width */ %d,\n", (width + 7) & ~7); | |
printf(" /* height */ %d,\n", height); | |
for (int y = 0; y < height; y++) { | |
printf(" /* row %2d */", y); | |
uint32_t* row = image + y * width; | |
for (int x = 0; x < width; x += 8) { | |
int byte = 0; | |
for (int i = 0; i < 8; i++) { | |
uint32_t color = (x + i) < width ? row[x + i] : 0; | |
uint8_t a = color >> 24; | |
uint8_t b = (color >> 16) & 255; | |
uint8_t g = (color >> 8) & 255; | |
uint8_t r = color & 255; | |
if (a == 255) { | |
byte |= 128 >> i; | |
} | |
} | |
printf(" 0x%02x,", byte); | |
} | |
printf("\n"); | |
} | |
printf("};\n\n"); | |
printf("static const byte %sWhite[] PROGMEM = {\n", argv[2]); | |
printf(" /* width */ %d,\n", (width + 7) & ~7); | |
printf(" /* height */ %d,\n", height); | |
for (int y = 0; y < height; y++) { | |
printf(" /* row %2d */", y); | |
uint32_t* row = image + y * width; | |
for (int x = 0; x < width; x += 8) { | |
int byte = 0; | |
for (int i = 0; i < 8; i++) { | |
uint32_t color = (x + i) < width ? row[x + i] : 0; | |
uint8_t a = color >> 24; | |
uint8_t b = (color >> 16) & 255; | |
uint8_t g = (color >> 8) & 255; | |
uint8_t r = color & 255; | |
if (a == 255 && r == 255 && g == 255 && b == 255) { | |
byte |= 128 >> i; | |
} | |
} | |
printf(" 0x%02x,", byte); | |
} | |
printf("\n"); | |
} | |
printf("};\n\n"); | |
printf("#if 0\n"); | |
printf(" gb.display.setColor(BLACK);\n"); | |
printf(" gb.display.drawBitmap(x, y, %sBlack);\n", argv[2]); | |
printf(" gb.display.setColor(WHITE);\n"); | |
printf(" gb.display.drawBitmap(x, y, %sWhite);\n", argv[2]); | |
printf("#endif\n"); | |
free((void*)image); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment