rearranged everything
This commit is contained in:
parent
3420c1a544
commit
7f10b384f9
10 changed files with 337 additions and 307 deletions
135
src/lily_png.cpp
Normal file
135
src/lily_png.cpp
Normal file
|
@ -0,0 +1,135 @@
|
|||
#include "lily_png.h"
|
||||
|
||||
std::vector<color> palette;
|
||||
bool palette_found = false;
|
||||
|
||||
static void read_raw_data(const std::string &file_path, buffer_unsigned &data, metadata &meta)
|
||||
{
|
||||
std::println("Zlib version is {}", zlibVersion());
|
||||
unsigned char magic[9] = {137, 80, 78, 71, 13, 10, 26, 10};
|
||||
file_reader reader(file_path);
|
||||
char file_magic[9] = {0};
|
||||
reader.read_buffer(file_magic, 8);
|
||||
if (memcmp(magic, file_magic, 8) != 0)
|
||||
{
|
||||
std::println("File is not a png!");
|
||||
return ;
|
||||
}
|
||||
buffer_unsigned raw_dat{};
|
||||
while (true)
|
||||
{
|
||||
std::tuple<unsigned int, buffer> chunk_header;
|
||||
std::get<1>(chunk_header).size = 4;
|
||||
auto ret = reader.read_from_tuple(chunk_header);
|
||||
if (ret.second == READ_FILE_ENDED || ret.second == READ_INCOMPLETE)
|
||||
{
|
||||
std::println("Chunk incomplete!");
|
||||
return ;
|
||||
}
|
||||
unsigned int size = std::get<0>(chunk_header);
|
||||
//std::println("Chunk type {} Size {}", std::get<1>(chunk_header).data, std::get<0>(chunk_header));
|
||||
|
||||
buffer_unsigned raw_data{};
|
||||
raw_data.size = std::get<0>(chunk_header);
|
||||
std::tuple<buffer, unsigned> dat;
|
||||
std::get<0>(dat).size = std::get<0>(chunk_header);
|
||||
ret = reader.read_from_tuple(dat);
|
||||
if (ret.second == READ_FILE_ENDED || ret.second == READ_INCOMPLETE)
|
||||
{
|
||||
std::println("Chunk incomplete!");
|
||||
return ;
|
||||
}
|
||||
unsigned long crc = crc32(0, reinterpret_cast<unsigned char *>(std::get<1>(chunk_header).data), 4);
|
||||
crc = crc32(crc, reinterpret_cast<unsigned char *>(std::get<0>(dat).data), std::get<0>(chunk_header));
|
||||
if (crc != std::get<1>(dat))
|
||||
throw std::runtime_error("Crc check failed");
|
||||
|
||||
|
||||
if (strncmp(std::get<1>(chunk_header).data, "IDAT", 4) == 0)
|
||||
{
|
||||
raw_dat.write(reinterpret_cast<unsigned char *>(std::get<0>(dat).data), std::get<0>(chunk_header));
|
||||
}
|
||||
else if (strncmp(std::get<1>(chunk_header).data, "IHDR", 4) == 0)
|
||||
{
|
||||
meta = parse_metadata(std::get<0>(dat));
|
||||
}
|
||||
else if (strncmp(std::get<1>(chunk_header).data, "PLTE", 4) == 0)
|
||||
{
|
||||
palette_found = true;
|
||||
if (size%3 != 0)
|
||||
{
|
||||
std::println("Palette size is not divisible by 3");
|
||||
return ;
|
||||
}
|
||||
for (int i = 0; i < size; i += 3)
|
||||
{
|
||||
color tmp_color;
|
||||
tmp_color.r = std::get<0>(dat).data[i];
|
||||
tmp_color.g = std::get<0>(dat).data[i + 1];
|
||||
tmp_color.b = std::get<0>(dat).data[i + 2];
|
||||
palette.push_back(tmp_color);
|
||||
}
|
||||
}
|
||||
else if (strncmp(std::get<1>(chunk_header).data, "IEND", 4) == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
data.allocate(get_uncompressed_size(meta));
|
||||
size_t prev_allocated = data.allocated;
|
||||
int r = uncompress(data.data, &prev_allocated, raw_dat.data, raw_dat.allocated);
|
||||
if (r != Z_OK)
|
||||
{
|
||||
std::println("Uncompress failed {}", r);
|
||||
throw std::runtime_error("Uncompress fail");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void apply_palette_scanline(unsigned char *scanline, unsigned char *dest, metadata &meta)
|
||||
{
|
||||
size_t pixel_size = get_pixel_bit_size(meta);
|
||||
size_t pixel_size_bytes = (pixel_size + 7)/8;
|
||||
size_t scanline_size = (meta.width * pixel_size + 7)/8;
|
||||
unsigned long dest_index = 0;
|
||||
for (int i = 0; i < scanline_size; i++)
|
||||
{
|
||||
color tmp_color = palette[scanline[i]];
|
||||
dest[dest_index] = tmp_color.r;
|
||||
dest[dest_index++] = tmp_color.g;
|
||||
dest[dest_index++] = tmp_color.b;
|
||||
}
|
||||
}
|
||||
|
||||
static void apply_palette(buffer_unsigned &data, buffer_unsigned &dest, metadata &meta)
|
||||
{
|
||||
unsigned long index = 0;
|
||||
unsigned long index_dest = 0;
|
||||
size_t pixel_size = get_pixel_bit_size(meta);
|
||||
size_t pixel_size_bytes = (pixel_size + 7)/8;
|
||||
size_t scanline_size = (meta.width * pixel_size + 7)/8;
|
||||
unsigned long scanlines = 0;
|
||||
dest.allocate(get_uncompressed_size(meta) * 3);
|
||||
while (scanlines < meta.height)
|
||||
{
|
||||
unsigned char filter = data.data[index];
|
||||
std::println("Filter {}", filter);
|
||||
apply_palette_scanline(&data.data[index + 1], &dest.data[index_dest], meta);
|
||||
index += scanline_size + 1;
|
||||
index_dest += scanline_size;
|
||||
scanlines++;
|
||||
}
|
||||
}
|
||||
|
||||
void read_png(const std::string &file_path, buffer_unsigned &data)
|
||||
{
|
||||
buffer_unsigned tmp_data{};
|
||||
metadata meta{0};
|
||||
read_raw_data(file_path, tmp_data, meta);
|
||||
if (palette_found == true)
|
||||
{
|
||||
buffer_unsigned dest_palette{};
|
||||
apply_palette(tmp_data, dest_palette, meta);
|
||||
tmp_data = dest_palette;
|
||||
}
|
||||
filter(tmp_data, data, meta);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue