Initial Comit
This commit is contained in:
6
tools/img2c/CMakeLists.txt
Normal file
6
tools/img2c/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
project(img2c LANGUAGES CXX)
|
||||
|
||||
add_executable(img2c source/main.cpp)
|
||||
target_include_directories(img2c PRIVATE include)
|
7988
tools/img2c/include/stb_image.h
Normal file
7988
tools/img2c/include/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
37
tools/img2c/source/main.cpp
Normal file
37
tools/img2c/source/main.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#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;
|
||||
}
|
Reference in New Issue
Block a user