HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
png.hpp
1// Copyright Take Vos 2020-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5
6#pragma once
7
8#include "../required.hpp"
9#include "../pixel_map.hpp"
10#include "../color/sfloat_rgba16.hpp"
11#include "../geometry/identity.hpp"
12#include "../numeric_array.hpp"
13#include "../URL.hpp"
14#include "../resource_view.hpp"
15#include "../byte_string.hpp"
16#include <span>
17#include <vector>
18#include <cstddef>
19#include <cstdint>
20
21namespace tt {
22
23class png {
24public:
25 png(std::span<std::byte const> bytes);
26
28
29 png(URL const &url) :
30 png(url.loadView()) {}
31
32 i32x4 extent() const noexcept {
33 return i32x4{width, height};
34 }
35
36 void decode_image(pixel_map<sfloat_rgba16> &image) const;
37
38 static pixel_map<sfloat_rgba16> load(URL const &url);
39
40private:
44 matrix3 color_to_sRGB = geo::identity();
45
48 std::vector<float> transfer_function;
49
50 int width = 0;
51 int height = 0;
52 int bit_depth = 0;
53 int color_type = 0;
54 int compression_method = 0;
55 int filter_method = 0;
56 int interlace_method = 0;
57
58 bool has_alpha;
59 bool is_palletted;
60 bool is_color;
61 int samples_per_pixel = 0;
62 int bits_per_pixel = 0;
63 int bytes_per_pixel = 0;
64 int bytes_per_line = 0;
65 int stride = 0;
66
70
74
75 void read_header(std::span<std::byte const> bytes, ssize_t &offset);
76 void read_chunks(std::span<std::byte const> bytes, ssize_t &offset);
77 void read_IHDR(std::span<std::byte const> bytes);
78 void read_cHRM(std::span<std::byte const> bytes);
79 void read_gAMA(std::span<std::byte const> bytes);
80 void read_iBIT(std::span<std::byte const> bytes);
81 void read_iCCP(std::span<std::byte const> bytes);
82 void read_sRGB(std::span<std::byte const> bytes);
83 void generate_sRGB_transfer_function() noexcept;
84 void generate_Rec2100_transfer_function() noexcept;
85 void generate_gamma_transfer_function(float gamma) noexcept;
86 bstring decompress_IDATs(ssize_t image_data_size) const;
87 void unfilter_lines(bstring &image_data) const;
88 void unfilter_line(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const;
89 void unfilter_line_sub(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
90 void unfilter_line_up(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
91 void unfilter_line_average(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
92 void unfilter_line_paeth(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
93 void data_to_image(bstring bytes, pixel_map<sfloat_rgba16> &image) const noexcept;
94 void data_to_image_line(std::span<std::byte const> bytes, pixel_row<sfloat_rgba16> &row) const noexcept;
95 i32x4 extract_pixel_from_line(std::span<std::byte const> bytes, int x) const noexcept;
96
97};
98
99}
Definition png.hpp:23
Definition identity.hpp:11
A 2D canvas of pixels.
Definition pixel_map.hpp:99
A 4D vector.
Definition ivec.hpp:38
A row of pixels.
Definition pixel_map.hpp:19
Definition URL.hpp:46
std::unique_ptr< resource_view > loadView() const
Load a resource.