diff --git a/CMakeLists.txt b/CMakeLists.txt index 94751c8..31a472d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,8 @@ project(lily_png) set(CMAKE_CXX_STANDARD 23) add_subdirectory(file_reader) +find_package(ZLIB) add_library(lily_png STATIC lily_png.cpp) +target_link_libraries(lily_png file_read ZLIB::ZLIB) -target_link_libraries(lily_png file_read) diff --git a/file_reader b/file_reader index 35c9579..238f9a9 160000 --- a/file_reader +++ b/file_reader @@ -1 +1 @@ -Subproject commit 35c95790edb62100e8f05d27f2cbe52ee11109df +Subproject commit 238f9a9b975ab97195b0982a08c8939e4eee2251 diff --git a/lily_png.cpp b/lily_png.cpp index 5745cff..526b1b2 100644 --- a/lily_png.cpp +++ b/lily_png.cpp @@ -1,7 +1,5 @@ #include "lily_png.h" -#include - static metadata parse_metadata(buffer &data) { std::tuple meta; @@ -23,7 +21,7 @@ static metadata parse_metadata(buffer &data) return m; } -buffer read_png(const std::string &file_path) +buffer_unsigned read_png(const std::string &file_path) { unsigned char magic[9] = {137, 80, 78, 71, 13, 10, 26, 10}; file_reader reader(file_path); @@ -36,7 +34,8 @@ buffer read_png(const std::string &file_path) if (memcmp(magic, std::get<0>(head_tup).data, 8) != 0) throw std::runtime_error("File is not a png"); metadata meta{0}; - buffer image_data{0}; + buffer_unsigned image_data_concat{0}; + buffer_unsigned image_data{0}; while (true) { buffer chunk_type{0}; @@ -50,21 +49,49 @@ buffer read_png(const std::string &file_path) std::println("Header stats size {} type {}", std::get<0>(chunk_head), std::get<1>(chunk_head).data); buffer data_body{0}; data_body.size = std::get<0>(chunk_head); - auto data = std::make_tuple(data_body, chunk_type); + auto data = std::make_tuple(data_body, (unsigned int)0); auto ree = reader.read_from_tuple(data); + unsigned long crc = crc32(0, reinterpret_cast(std::get<1>(chunk_head).data), std::get<1>(chunk_head).size); + crc = crc32(crc, reinterpret_cast(std::get<0>(data).data), std::get<0>(data).size); + if (crc != std::get<1>(data)) + throw std::runtime_error("Crc check failed"); std::println("Size received {}", ree.first); if (strcmp("IHDR", std::get<1>(chunk_head).data) == 0) meta = parse_metadata(std::get<0>(data)); else if (strcmp("IDAT", std::get<1>(chunk_head).data) == 0) { buffer &temp_buf = std::get<0>(data); - image_data.write(temp_buf.data, temp_buf.size); + image_data_concat.write(reinterpret_cast(temp_buf.data), temp_buf.size); } else if (strcmp("IEND",std::get<1>(chunk_head).data) == 0) break; } - std::println("Image data size {}", image_data.size); - std::println("Image data allocated {}", image_data.allocated); - std::println("Allocations {}", image_data.allocations); + std::println("Image data size {}", image_data_concat.size); + std::println("Image data allocated {}", image_data_concat.allocated); + std::println("Allocations {}", image_data_concat.allocations); + image_data.allocate(image_data_concat.allocated); + uncompress(image_data.data, &image_data.allocated, image_data_concat.data, image_data_concat.size); + std::println("Filter {}", (int)meta.filter); + + switch (meta.filter) + { + case 0: + break; + case 1: + std::println("sub filter now implemented"); + break; + case 2: + std::println("up filter now implemented"); + break; + case 3: + std::println("average filter now implemented"); + break; + case 4: + std::println("paeth filter now implemented"); + break; + default: + std::println("Filter not recognised"); + break; + } return image_data; } diff --git a/lily_png.h b/lily_png.h index f1ff778..2a4ce6f 100644 --- a/lily_png.h +++ b/lily_png.h @@ -2,6 +2,7 @@ #include "file_reader/src/file_read.h" #include +#include struct metadata { @@ -14,4 +15,4 @@ struct metadata char interface; }; -buffer read_png(const std::string &file_path); \ No newline at end of file +buffer_unsigned read_png(const std::string &file_path); \ No newline at end of file