diff --git a/file_reader b/file_reader index ec3d796..5cdd4f8 160000 --- a/file_reader +++ b/file_reader @@ -1 +1 @@ -Subproject commit ec3d79677455c47c31f3244a763d53933da84eb8 +Subproject commit 5cdd4f883523520c768a5a387d723a504b4922f8 diff --git a/src/filter.cpp b/src/filter.cpp index 4a4089e..5824256 100644 --- a/src/filter.cpp +++ b/src/filter.cpp @@ -1,6 +1,6 @@ #include "filter.h" -void filter_scanline(unsigned char *scanline, unsigned char *previous_scanline, unsigned char *dest, metadata &meta, unsigned char filter_type) +void lily_png::filter_scanline(unsigned char *scanline, unsigned char *previous_scanline, unsigned char *dest, metadata &meta, unsigned char filter_type) { size_t pixel_size = get_pixel_bit_size(meta); size_t pixel_size_bytes = (pixel_size + 7)/8; @@ -43,7 +43,7 @@ void filter_scanline(unsigned char *scanline, unsigned char *previous_scanline, } } -void filter(buffer &data, buffer &dest ,metadata &meta) +void lily_png::filter(file_reader::buffer &data, file_reader::buffer &dest ,metadata &meta) { unsigned long index = 0; unsigned long index_dest = 0; diff --git a/src/filter.h b/src/filter.h index 3b66ac4..3bbd496 100644 --- a/src/filter.h +++ b/src/filter.h @@ -1,6 +1,10 @@ #pragma once #include "utils.h" +#include -void filter_scanline(unsigned char *scanline, unsigned char *previous_scanline, unsigned char *dest, metadata &meta, unsigned char filter_type); -void filter(buffer &data, buffer &dest ,metadata &meta); +namespace lily_png +{ + void filter_scanline(unsigned char *scanline, unsigned char *previous_scanline, unsigned char *dest, metadata &meta, unsigned char filter_type); + void filter(file_reader::buffer &data, file_reader::buffer &dest ,metadata &meta); +} diff --git a/src/lily_png.cpp b/src/lily_png.cpp index 6d59601..73665a3 100644 --- a/src/lily_png.cpp +++ b/src/lily_png.cpp @@ -1,13 +1,13 @@ #include "lily_png.h" -std::vector palette; +std::vector palette; bool palette_found = false; -static void read_raw_data(const std::string &file_path, buffer &data, metadata &meta) +static void read_raw_data(const std::string &file_path, file_reader::buffer &data, lily_png::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); + file_reader::file_reader reader(file_path); char file_magic[9] = {0}; reader.read_buffer(file_magic, 8); if (memcmp(magic, file_magic, 8) != 0) @@ -15,13 +15,13 @@ static void read_raw_data(const std::string &file_path, buffer &d std::println("File is not a png!"); return ; } - buffer raw_dat{}; + file_reader::buffer raw_dat{}; while (true) { - std::tuple> chunk_header; + std::tuple> 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) + if (ret.second == file_reader::READ_FILE_ENDED || ret.second == file_reader::READ_INCOMPLETE) { std::println("Chunk incomplete!"); return ; @@ -29,12 +29,12 @@ static void read_raw_data(const std::string &file_path, buffer &d 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 raw_data{}; + file_reader::buffer raw_data{}; raw_data.size = std::get<0>(chunk_header); - std::tuple, unsigned> dat; + std::tuple, 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) + if (ret.second == file_reader::READ_FILE_ENDED || ret.second == file_reader::READ_INCOMPLETE) { std::println("Chunk incomplete!"); return ; @@ -51,7 +51,7 @@ static void read_raw_data(const std::string &file_path, buffer &d } else if (strncmp(std::get<1>(chunk_header).data, "IHDR", 4) == 0) { - meta = parse_metadata(std::get<0>(dat)); + meta = lily_png::parse_metadata(std::get<0>(dat)); } else if (strncmp(std::get<1>(chunk_header).data, "PLTE", 4) == 0) { @@ -63,7 +63,7 @@ static void read_raw_data(const std::string &file_path, buffer &d } for (int i = 0; i < size; i += 3) { - color tmp_color; + lily_png::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]; @@ -85,7 +85,7 @@ static void read_raw_data(const std::string &file_path, buffer &d } -static void apply_palette_scanline(unsigned char *scanline, unsigned char *dest, metadata &meta) +static void apply_palette_scanline(unsigned char *scanline, unsigned char *dest, lily_png::metadata &meta) { size_t pixel_size = get_pixel_bit_size(meta); size_t pixel_size_bytes = (pixel_size + 7)/8; @@ -93,18 +93,18 @@ static void apply_palette_scanline(unsigned char *scanline, unsigned char *dest, unsigned long dest_index = 0; for (int i = 0; i < scanline_size; i++) { - color tmp_color = palette[scanline[i]]; + lily_png::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 &data, buffer &dest, metadata &meta) +static void apply_palette(file_reader::buffer &data, file_reader::buffer &dest, lily_png::metadata &meta) { unsigned long index = 0; unsigned long index_dest = 0; - size_t pixel_size = get_pixel_bit_size(meta); + size_t pixel_size = lily_png::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; @@ -120,14 +120,14 @@ static void apply_palette(buffer &data, buffer &de } } -void read_png(const std::string &file_path, buffer &data) +void lily_png::read_png(const std::string &file_path, file_reader::buffer &data) { - buffer tmp_data{}; + file_reader::buffer tmp_data{}; metadata meta{0}; read_raw_data(file_path, tmp_data, meta); if (palette_found == true) { - buffer dest_palette{}; + file_reader::buffer dest_palette{}; apply_palette(tmp_data, dest_palette, meta); tmp_data = dest_palette; } diff --git a/src/lily_png.h b/src/lily_png.h index ea316f9..3023fe9 100644 --- a/src/lily_png.h +++ b/src/lily_png.h @@ -7,11 +7,15 @@ #include "filter.h" #include -struct color +namespace lily_png { - unsigned char r = 0; - unsigned char g = 0; - unsigned char b = 0; -}; + struct color + { + unsigned char r = 0; + unsigned char g = 0; + unsigned char b = 0; + }; -void read_png(const std::string &file_path, buffer &data); \ No newline at end of file + + void read_png(const std::string &file_path, file_reader::buffer &data); +} \ No newline at end of file diff --git a/src/metadata.cpp b/src/metadata.cpp index ca4007c..af06fc8 100644 --- a/src/metadata.cpp +++ b/src/metadata.cpp @@ -1,11 +1,10 @@ #include "metadata.h" - -metadata parse_metadata(buffer &data) +lily_png::metadata lily_png::parse_metadata(file_reader::buffer &data) { std::tuple meta; constexpr std::size_t size = std::tuple_size_v; - parsing_buffer par_buf(data); + file_reader::parsing_buffer par_buf(data); par_buf.point = par_buf.buf.data; par_buf.consumed_size = 0; read_comp(size, par_buf, meta); diff --git a/src/metadata.h b/src/metadata.h index 01f77df..7170d3b 100644 --- a/src/metadata.h +++ b/src/metadata.h @@ -1,15 +1,18 @@ #pragma once #include "../file_reader/src/file_read.h" -struct metadata +namespace lily_png { - unsigned int width; - unsigned int height; - char bit_depth; - char color_type; - char compression; - char filter; - char interface; -}; + struct metadata + { + unsigned int width; + unsigned int height; + char bit_depth; + char color_type; + char compression; + char filter; + char interface; + }; -metadata parse_metadata(buffer &data); + metadata parse_metadata(file_reader::buffer &data); +} diff --git a/src/utils.cpp b/src/utils.cpp index 95fe886..99c2665 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,7 +1,7 @@ #include "utils.h" -size_t get_pixel_bit_size(const metadata &meta) +size_t lily_png::get_pixel_bit_size(const metadata &meta) { size_t ret = 0; switch (meta.color_type) @@ -52,7 +52,7 @@ size_t get_pixel_bit_size(const metadata &meta) return ret; } -size_t get_uncompressed_size(const metadata meta) +size_t lily_png::get_uncompressed_size(const metadata meta) { size_t ret = (meta.width * get_pixel_bit_size(meta) + 7)/8; //ceil() but for bytes ret = (ret + 1) * meta.height; @@ -60,7 +60,7 @@ size_t get_uncompressed_size(const metadata meta) return ret; } -int paeth_predict(int a, int b, int c) +int lily_png::paeth_predict(int a, int b, int c) { int pred = a+b-c; int pred1 = abs(pred - a); diff --git a/src/utils.h b/src/utils.h index 3048b68..dcb3af4 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,6 +1,9 @@ #pragma once #include "metadata.h" -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); +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); +}