maorduino/tools/gimp_img_conv/gimp_img_conv.c

44 lines
1.3 KiB
C
Raw Permalink Normal View History

2024-04-03 07:55:51 +01:00
#define SIZE 120
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
2024-04-03 08:27:42 +01:00
#include <stdbool.h>
2024-04-03 07:55:51 +01:00
#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() {
2024-04-03 08:27:42 +01:00
printf("static const unsigned short gimp_image[%d] = {", gimp_image.width * gimp_image.height);
bool include_comma = false;
2024-04-03 07:55:51 +01:00
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);
2024-04-03 08:27:42 +01:00
if (!include_comma) {
include_comma = true;
} else {
printf(", ");
}
printf("0x%X", color);
2024-04-03 07:55:51 +01:00
}
}
2024-04-03 08:27:42 +01:00
printf("};\n");
2024-04-03 07:55:51 +01:00
}