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

51 lines
1.4 KiB
C++

#include <iostream>
#include <fstream>
#include <png++/png.hpp>
#define CACHESIZE 4096u //must be lower than 2^13
#define __CACHESIZE (CACHESIZE*4u) //Do not edit
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::rgb_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;
bitmap << width << " " << height << " ";
char* cache = new char[__CACHESIZE];
while (i < width*height*4)
{
x = ((uint32_t) i/4 ) % width;
y = (uint32_t)(i/4/width);
cache[(i+1)%__CACHESIZE] = input[y][x].red;
cache[(i+0)%__CACHESIZE] = input[y][x].green;
cache[(i+3)%__CACHESIZE] = input[y][x].blue;
cache[(i+2)%__CACHESIZE] = 255;
i+=4;
if (i % __CACHESIZE == 0) //Write
bitmap.write(cache, __CACHESIZE);
}
if (i % __CACHESIZE != 0) //Write what has not been written
bitmap.write(cache, width*height*4 % __CACHESIZE);
delete[] cache;
bitmap.close();
return EXIT_SUCCESS;
}