usability changes and initial convert commit

This commit is contained in:
Luna 2025-07-18 08:04:42 +02:00
parent 66ab20c0f0
commit 919c9126b0
8 changed files with 46 additions and 18 deletions

View file

@ -13,6 +13,8 @@ add_library(lily_png STATIC src/lily_png.cpp
src/utils.cpp src/utils.cpp
src/utils.h src/utils.h
src/filter.cpp src/filter.cpp
src/filter.h) src/filter.h
src/convert.cpp
src/convert.h)
target_link_libraries(lily_png PRIVATE file_read ZLIB::ZLIB) target_link_libraries(lily_png PRIVATE file_read ZLIB::ZLIB)

9
src/convert.cpp Normal file
View file

@ -0,0 +1,9 @@
#include "convert.h"
void lily_png::convert_to_R32G32B32A32(file_reader::buffer<unsigned char> &data, metadata &meta)
{
if (meta.color_type != static_cast<char>(color::rgba))
{
}
}

8
src/convert.h Normal file
View file

@ -0,0 +1,8 @@
#pragma once
#include "../file_reader/src/buffer.h"
#include "metadata.h"
namespace lily_png
{
void convert_to_R32G32B32A32(file_reader::buffer<unsigned char> &data, metadata &meta);
}

View file

@ -1,6 +1,6 @@
#include "lily_png.h" #include "lily_png.h"
std::vector<lily_png::color> palette; std::vector<lily_png::color_rgb> palette;
bool palette_found = false; bool palette_found = false;
static void read_raw_data(const std::string &file_path, file_reader::buffer<unsigned char> &data, lily_png::metadata &meta) static void read_raw_data(const std::string &file_path, file_reader::buffer<unsigned char> &data, lily_png::metadata &meta)
@ -63,7 +63,7 @@ static void read_raw_data(const std::string &file_path, file_reader::buffer<unsi
} }
for (int i = 0; i < size; i += 3) for (int i = 0; i < size; i += 3)
{ {
lily_png::color tmp_color; lily_png::color_rgb tmp_color;
tmp_color.r = std::get<0>(dat).data[i]; tmp_color.r = std::get<0>(dat).data[i];
tmp_color.g = std::get<0>(dat).data[i + 1]; tmp_color.g = std::get<0>(dat).data[i + 1];
tmp_color.b = std::get<0>(dat).data[i + 2]; tmp_color.b = std::get<0>(dat).data[i + 2];
@ -93,7 +93,7 @@ static void apply_palette_scanline(unsigned char *scanline, unsigned char *dest,
unsigned long dest_index = 0; unsigned long dest_index = 0;
for (int i = 0; i < scanline_size; i++) for (int i = 0; i < scanline_size; i++)
{ {
lily_png::color tmp_color = palette[scanline[i]]; lily_png::color_rgb tmp_color = palette[scanline[i]];
dest[dest_index] = tmp_color.r; dest[dest_index] = tmp_color.r;
dest[dest_index++] = tmp_color.g; dest[dest_index++] = tmp_color.g;
dest[dest_index++] = tmp_color.b; dest[dest_index++] = tmp_color.b;
@ -120,7 +120,7 @@ static void apply_palette(file_reader::buffer<unsigned char> &data, file_reader:
} }
} }
void lily_png::read_png(const std::string &file_path, file_reader::buffer<unsigned char> &data) lily_png::metadata lily_png::read_png(const std::string &file_path, file_reader::buffer<unsigned char> &data)
{ {
file_reader::buffer<unsigned char> tmp_data{}; file_reader::buffer<unsigned char> tmp_data{};
metadata meta{0}; metadata meta{0};
@ -132,4 +132,5 @@ void lily_png::read_png(const std::string &file_path, file_reader::buffer<unsign
tmp_data = dest_palette; tmp_data = dest_palette;
} }
filter(tmp_data, data, meta); filter(tmp_data, data, meta);
return meta;
} }

View file

@ -9,7 +9,7 @@
namespace lily_png namespace lily_png
{ {
struct color struct color_rgb
{ {
unsigned char r = 0; unsigned char r = 0;
unsigned char g = 0; unsigned char g = 0;
@ -17,5 +17,5 @@ namespace lily_png
}; };
void read_png(const std::string &file_path, file_reader::buffer<unsigned char> &data); metadata read_png(const std::string &file_path, file_reader::buffer<unsigned char> &data);
} }

View file

@ -3,6 +3,14 @@
namespace lily_png namespace lily_png
{ {
enum class color
{
grayscale = 0,
rgb = 2,
indexed = 3,
grayscale_alpha = 4,
rgba = 6
};
struct metadata struct metadata
{ {
unsigned int width; unsigned int width;

View file

@ -6,7 +6,7 @@ size_t lily_png::get_pixel_bit_size(const metadata &meta)
size_t ret = 0; size_t ret = 0;
switch (meta.color_type) switch (meta.color_type)
{ {
case 0: case static_cast<int>(color::grayscale):
if (meta.bit_depth == 1 || meta.bit_depth == 2 || meta.bit_depth == 4 || meta.bit_depth == 8 || meta.bit_depth == 16) if (meta.bit_depth == 1 || meta.bit_depth == 2 || meta.bit_depth == 4 || meta.bit_depth == 8 || meta.bit_depth == 16)
{ {
ret = meta.bit_depth; ret = meta.bit_depth;
@ -14,7 +14,7 @@ size_t lily_png::get_pixel_bit_size(const metadata &meta)
else else
throw std::runtime_error("Invalid bit depht"); throw std::runtime_error("Invalid bit depht");
break; break;
case 2: case static_cast<int>(color::rgb):
if (meta.bit_depth == 8 || meta.bit_depth == 16) if (meta.bit_depth == 8 || meta.bit_depth == 16)
{ {
ret = meta.bit_depth * 3; ret = meta.bit_depth * 3;
@ -22,7 +22,7 @@ size_t lily_png::get_pixel_bit_size(const metadata &meta)
else else
throw std::runtime_error("Invalid bit depht"); throw std::runtime_error("Invalid bit depht");
break; break;
case 3: case static_cast<int>(color::indexed):
if (meta.bit_depth == 1 || meta.bit_depth == 2 || meta.bit_depth == 4 || meta.bit_depth == 8) if (meta.bit_depth == 1 || meta.bit_depth == 2 || meta.bit_depth == 4 || meta.bit_depth == 8)
{ {
ret = meta.bit_depth; ret = meta.bit_depth;
@ -30,7 +30,7 @@ size_t lily_png::get_pixel_bit_size(const metadata &meta)
else else
throw std::runtime_error("Invalid bit depht"); throw std::runtime_error("Invalid bit depht");
break; break;
case 4: case static_cast<int>(color::grayscale_alpha):
if (meta.bit_depth == 8 || meta.bit_depth == 16) if (meta.bit_depth == 8 || meta.bit_depth == 16)
{ {
ret = meta.bit_depth * 2; ret = meta.bit_depth * 2;
@ -38,7 +38,7 @@ size_t lily_png::get_pixel_bit_size(const metadata &meta)
else else
throw std::runtime_error("Invalid bit depht"); throw std::runtime_error("Invalid bit depht");
break; break;
case 6: case static_cast<int>(color::rgba):
if (meta.bit_depth == 8 || meta.bit_depth == 16) if (meta.bit_depth == 8 || meta.bit_depth == 16)
{ {
ret = meta.bit_depth * 4; ret = meta.bit_depth * 4;
@ -60,12 +60,12 @@ size_t lily_png::get_uncompressed_size(const metadata meta)
return ret; return ret;
} }
int lily_png::paeth_predict(int a, int b, int c) int lily_png::paeth_predict(const int a, const int b, const int c)
{ {
int pred = a+b-c; const int pred = a+b-c;
int pred1 = abs(pred - a); const int pred1 = abs(pred - a);
int pred2 = abs(pred - b); const int pred2 = abs(pred - b);
int pred3 = abs(pred - c); const int pred3 = abs(pred - c);
if (pred1 <= pred2 && pred1 <= pred3) if (pred1 <= pred2 && pred1 <= pred3)
return a; return a;
if (pred2 <= pred3) if (pred2 <= pred3)

View file

@ -5,5 +5,5 @@ namespace lily_png
{ {
size_t get_pixel_bit_size(const metadata &meta); size_t get_pixel_bit_size(const metadata &meta);
size_t get_uncompressed_size(const metadata meta); size_t get_uncompressed_size(const metadata meta);
int paeth_predict(int a, int b, int c); int paeth_predict(const int a, const int b, const int c);
} }