From 919c9126b0bccebcdd42617761664e66912337b7 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 18 Jul 2025 08:04:42 +0200 Subject: [PATCH] usability changes and initial convert commit --- CMakeLists.txt | 4 +++- src/convert.cpp | 9 +++++++++ src/convert.h | 8 ++++++++ src/lily_png.cpp | 9 +++++---- src/lily_png.h | 4 ++-- src/metadata.h | 8 ++++++++ src/utils.cpp | 20 ++++++++++---------- src/utils.h | 2 +- 8 files changed, 46 insertions(+), 18 deletions(-) create mode 100644 src/convert.cpp create mode 100644 src/convert.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 152d927..6658ae1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,8 @@ add_library(lily_png STATIC src/lily_png.cpp src/utils.cpp src/utils.h 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) diff --git a/src/convert.cpp b/src/convert.cpp new file mode 100644 index 0000000..40207d8 --- /dev/null +++ b/src/convert.cpp @@ -0,0 +1,9 @@ +#include "convert.h" + +void lily_png::convert_to_R32G32B32A32(file_reader::buffer &data, metadata &meta) +{ + if (meta.color_type != static_cast(color::rgba)) + { + + } +} diff --git a/src/convert.h b/src/convert.h new file mode 100644 index 0000000..95503a8 --- /dev/null +++ b/src/convert.h @@ -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 &data, metadata &meta); +} diff --git a/src/lily_png.cpp b/src/lily_png.cpp index 73665a3..f09bbf4 100644 --- a/src/lily_png.cpp +++ b/src/lily_png.cpp @@ -1,6 +1,6 @@ #include "lily_png.h" -std::vector palette; +std::vector palette; bool palette_found = false; static void read_raw_data(const std::string &file_path, file_reader::buffer &data, lily_png::metadata &meta) @@ -63,7 +63,7 @@ static void read_raw_data(const std::string &file_path, file_reader::buffer(dat).data[i]; tmp_color.g = std::get<0>(dat).data[i + 1]; 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; 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.g; dest[dest_index++] = tmp_color.b; @@ -120,7 +120,7 @@ static void apply_palette(file_reader::buffer &data, file_reader: } } -void lily_png::read_png(const std::string &file_path, file_reader::buffer &data) +lily_png::metadata lily_png::read_png(const std::string &file_path, file_reader::buffer &data) { file_reader::buffer tmp_data{}; metadata meta{0}; @@ -132,4 +132,5 @@ void lily_png::read_png(const std::string &file_path, file_reader::buffer &data); + metadata read_png(const std::string &file_path, file_reader::buffer &data); } \ No newline at end of file diff --git a/src/metadata.h b/src/metadata.h index 7170d3b..42c2e01 100644 --- a/src/metadata.h +++ b/src/metadata.h @@ -3,6 +3,14 @@ namespace lily_png { + enum class color + { + grayscale = 0, + rgb = 2, + indexed = 3, + grayscale_alpha = 4, + rgba = 6 + }; struct metadata { unsigned int width; diff --git a/src/utils.cpp b/src/utils.cpp index 99c2665..1ca060a 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -6,7 +6,7 @@ size_t lily_png::get_pixel_bit_size(const metadata &meta) size_t ret = 0; switch (meta.color_type) { - case 0: + case static_cast(color::grayscale): 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; @@ -14,7 +14,7 @@ size_t lily_png::get_pixel_bit_size(const metadata &meta) else throw std::runtime_error("Invalid bit depht"); break; - case 2: + case static_cast(color::rgb): if (meta.bit_depth == 8 || meta.bit_depth == 16) { ret = meta.bit_depth * 3; @@ -22,7 +22,7 @@ size_t lily_png::get_pixel_bit_size(const metadata &meta) else throw std::runtime_error("Invalid bit depht"); break; - case 3: + case static_cast(color::indexed): if (meta.bit_depth == 1 || meta.bit_depth == 2 || meta.bit_depth == 4 || meta.bit_depth == 8) { ret = meta.bit_depth; @@ -30,7 +30,7 @@ size_t lily_png::get_pixel_bit_size(const metadata &meta) else throw std::runtime_error("Invalid bit depht"); break; - case 4: + case static_cast(color::grayscale_alpha): if (meta.bit_depth == 8 || meta.bit_depth == 16) { ret = meta.bit_depth * 2; @@ -38,7 +38,7 @@ size_t lily_png::get_pixel_bit_size(const metadata &meta) else throw std::runtime_error("Invalid bit depht"); break; - case 6: + case static_cast(color::rgba): if (meta.bit_depth == 8 || meta.bit_depth == 16) { ret = meta.bit_depth * 4; @@ -60,12 +60,12 @@ size_t lily_png::get_uncompressed_size(const metadata meta) 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; - int pred1 = abs(pred - a); - int pred2 = abs(pred - b); - int pred3 = abs(pred - c); + const int pred = a+b-c; + const int pred1 = abs(pred - a); + const int pred2 = abs(pred - b); + const int pred3 = abs(pred - c); if (pred1 <= pred2 && pred1 <= pred3) return a; if (pred2 <= pred3) diff --git a/src/utils.h b/src/utils.h index dcb3af4..60c0b68 100644 --- a/src/utils.h +++ b/src/utils.h @@ -5,5 +5,5 @@ namespace lily_png { size_t get_pixel_bit_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); }