fb888/tools/mkCharmap.cpp
2020-11-16 14:08:41 +01:00

65 lines
2.1 KiB
C++

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <png++/png.hpp>
int main(int argc, char *argv[])
{
if (argc < 3)
{
std::cerr << "USAGE: " << argv[0] << " INPUT.PNG OUTPUT" << std::endl;
exit(EXIT_FAILURE);
}
png::image< png::rgba_pixel > input(argv[1]);
std::ofstream bitmap(argv[2]);
uint32_t width, height, x, y;
width = input.get_width();
height = input.get_height();
x = y = 0;
size_t i = 0;
std::stringstream result;
result << std::string("static const unsigned int ") << std::string(argv[2]) << std::string("_width = ") << std::to_string(width) << std::string(";\n");
result << std::string("static const unsigned int ") << std::string(argv[2]) << std::string("_height = ") << std::to_string(height) << std::string(";\n");
result << std::string("static const unsigned char ");
result << std::string(argv[2]);
result << std::string("_rgba[");
result << std::to_string(width*height*4);
result << std::string("] =\n{\n\t");
result << std::setfill('0') << std::setw(2) << std::hex;
while (i < width * height)
{
x = (uint32_t) i % width;
y = (uint32_t)(i / width);
result << std::string("0x") << (int)input[y][x].red;
result << std::string(", ");
result << std::string("0x") << (int)input[y][x].green;
result << std::string(", ");
result << std::string("0x") << (int)input[y][x].blue;
result << std::string(", ");
result << std::string("0x") << (int)input[y][x].alpha;
i++;
if (i == width*height)
result << std::string(",");
else if (i % 5 == 0)
result << std::string(",\n\t");
else
result << std::string(", ");
}
result << std::string("\n};");
bitmap << result.rdbuf();
bitmap.close();
return EXIT_SUCCESS;
}