now it has crc checks and uncompression is good now
This commit is contained in:
parent
2fbdd1e43d
commit
a1f2a6d1af
4 changed files with 41 additions and 12 deletions
|
@ -4,7 +4,8 @@ project(lily_png)
|
||||||
set(CMAKE_CXX_STANDARD 23)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
add_subdirectory(file_reader)
|
add_subdirectory(file_reader)
|
||||||
|
find_package(ZLIB)
|
||||||
|
|
||||||
add_library(lily_png STATIC lily_png.cpp)
|
add_library(lily_png STATIC lily_png.cpp)
|
||||||
|
target_link_libraries(lily_png file_read ZLIB::ZLIB)
|
||||||
|
|
||||||
target_link_libraries(lily_png file_read)
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 35c95790edb62100e8f05d27f2cbe52ee11109df
|
Subproject commit 238f9a9b975ab97195b0982a08c8939e4eee2251
|
45
lily_png.cpp
45
lily_png.cpp
|
@ -1,7 +1,5 @@
|
||||||
#include "lily_png.h"
|
#include "lily_png.h"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
static metadata parse_metadata(buffer &data)
|
static metadata parse_metadata(buffer &data)
|
||||||
{
|
{
|
||||||
std::tuple<unsigned int, unsigned int, char, char, char, char, char> meta;
|
std::tuple<unsigned int, unsigned int, char, char, char, char, char> meta;
|
||||||
|
@ -23,7 +21,7 @@ static metadata parse_metadata(buffer &data)
|
||||||
return m;
|
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};
|
unsigned char magic[9] = {137, 80, 78, 71, 13, 10, 26, 10};
|
||||||
file_reader reader(file_path);
|
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)
|
if (memcmp(magic, std::get<0>(head_tup).data, 8) != 0)
|
||||||
throw std::runtime_error("File is not a png");
|
throw std::runtime_error("File is not a png");
|
||||||
metadata meta{0};
|
metadata meta{0};
|
||||||
buffer image_data{0};
|
buffer_unsigned image_data_concat{0};
|
||||||
|
buffer_unsigned image_data{0};
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
buffer chunk_type{0};
|
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);
|
std::println("Header stats size {} type {}", std::get<0>(chunk_head), std::get<1>(chunk_head).data);
|
||||||
buffer data_body{0};
|
buffer data_body{0};
|
||||||
data_body.size = std::get<0>(chunk_head);
|
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);
|
auto ree = reader.read_from_tuple(data);
|
||||||
|
unsigned long crc = crc32(0, reinterpret_cast<unsigned char *>(std::get<1>(chunk_head).data), std::get<1>(chunk_head).size);
|
||||||
|
crc = crc32(crc, reinterpret_cast<unsigned char *>(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);
|
std::println("Size received {}", ree.first);
|
||||||
if (strcmp("IHDR", std::get<1>(chunk_head).data) == 0)
|
if (strcmp("IHDR", std::get<1>(chunk_head).data) == 0)
|
||||||
meta = parse_metadata(std::get<0>(data));
|
meta = parse_metadata(std::get<0>(data));
|
||||||
else if (strcmp("IDAT", std::get<1>(chunk_head).data) == 0)
|
else if (strcmp("IDAT", std::get<1>(chunk_head).data) == 0)
|
||||||
{
|
{
|
||||||
buffer &temp_buf = std::get<0>(data);
|
buffer &temp_buf = std::get<0>(data);
|
||||||
image_data.write(temp_buf.data, temp_buf.size);
|
image_data_concat.write(reinterpret_cast<unsigned char *>(temp_buf.data), temp_buf.size);
|
||||||
}
|
}
|
||||||
else if (strcmp("IEND",std::get<1>(chunk_head).data) == 0)
|
else if (strcmp("IEND",std::get<1>(chunk_head).data) == 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
std::println("Image data size {}", image_data.size);
|
std::println("Image data size {}", image_data_concat.size);
|
||||||
std::println("Image data allocated {}", image_data.allocated);
|
std::println("Image data allocated {}", image_data_concat.allocated);
|
||||||
std::println("Allocations {}", image_data.allocations);
|
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;
|
return image_data;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "file_reader/src/file_read.h"
|
#include "file_reader/src/file_read.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <zlib.h>
|
||||||
|
|
||||||
struct metadata
|
struct metadata
|
||||||
{
|
{
|
||||||
|
@ -14,4 +15,4 @@ struct metadata
|
||||||
char interface;
|
char interface;
|
||||||
};
|
};
|
||||||
|
|
||||||
buffer read_png(const std::string &file_path);
|
buffer_unsigned read_png(const std::string &file_path);
|
Loading…
Add table
Add a link
Reference in a new issue