maorduino/tools/gimp_img_conv/gimp_img_conv.c

44 lines
1.3 KiB
C

#define SIZE 120
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdbool.h>
#include "glib.h"
#include "base_image.c"
int color_u8_to_u6(int v) {
const double conversion_ratio = 63.0 / 255.0;
return (int)(v * conversion_ratio);
}
int color_u8_to_u5(int v) {
const double conversion_ratio = 31.0 / 255.0;
return (int)(v * conversion_ratio);
}
void main() {
printf("static const unsigned short gimp_image[%d] = {", gimp_image.width * gimp_image.height);
bool include_comma = false;
for (int x = 0; x < gimp_image.width; x++) {
for (int y = 0; y < gimp_image.height; y++) {
int idx = 3 * (y * gimp_image.width + x);
// for some reason
// red = 5 bytes
// green = 6 bytes
// blue = 5 bytes
int r = (int)gimp_image.pixel_data[idx] & 0xFF;
int g = (int)gimp_image.pixel_data[idx + 1] & 0xFF;
int b = (int)gimp_image.pixel_data[idx + 2] & 0xFF;
uint16_t color = (color_u8_to_u5(r) << 11) | (color_u8_to_u6(g) << 5) | color_u8_to_u5(b);
if (!include_comma) {
include_comma = true;
} else {
printf(", ");
}
printf("0x%X", color);
}
}
printf("};\n");
}