sensor_main/tools/img2c/source/main.cpp
2025-06-12 10:04:45 +02:00

37 lines
848 B
C++

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include <cstdio>
#include <iostream>
#include <vector>
/**
* Usage:
* img2c <image> > out.c
*/
int main(int argc, char* argv[]) {
int w, h, c;
unsigned char* buf = stbi_load(argv[1], &w, &h, &c, 3);
std::vector<uint16_t> rgb565;
for (int i = 0; i < w * h * 3; i += 3) {
unsigned char r = buf[i];
unsigned char g = buf[i + 1];
unsigned char b = buf[i + 2];
uint16_t color = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
rgb565.push_back(color);
}
std::cout << "const uint16_t image[" << w * h << "] = {\n";
for (size_t i = 0; i < rgb565.size(); ++i) {
printf("0x%04X", rgb565[i]);
if (i != rgb565.size() - 1) std::cout << ", ";
if ((i + 1) % 8 == 0) std::cout << "\n";
}
std::cout << "\n};\n";
stbi_image_free(buf);
return 0;
}