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 "../URL.hpp"
13#include "../resource_view.hpp"
14#include "../byte_string.hpp"
15#include <span>
16#include <vector>
17#include <cstddef>
18#include <cstdint>
19
20namespace tt {
21
22class png {
23public:
24 [[nodiscard]] png(std::span<std::byte const> bytes);
25
26 [[nodiscard]] png(std::unique_ptr<resource_view> view);
27
28 [[nodiscard]] png(URL const &url) :
29 png(url.loadView()) {}
30
31 [[nodiscard]] size_t width() const noexcept
32 {
33 return _width;
34 }
35
36 [[nodiscard]] size_t height() const noexcept
37 {
38 return _height;
39 }
40
41 void decode_image(pixel_map<sfloat_rgba16> &image) const;
42
43 [[nodiscard]] static pixel_map<sfloat_rgba16> load(URL const &url);
44
45private:
49 matrix3 _color_to_sRGB = geo::identity();
50
53 std::vector<float> _transfer_function;
54
55 int _width = 0;
56 int _height = 0;
57 int _bit_depth = 0;
58 int _color_type = 0;
59 int _compression_method = 0;
60 int _filter_method = 0;
61 int _interlace_method = 0;
62
63 bool _has_alpha;
64 bool _is_palletted;
65 bool _is_color;
66 int _samples_per_pixel = 0;
67 int _bits_per_pixel = 0;
68 int _bytes_per_pixel = 0;
69 int _bytes_per_line = 0;
70 int _stride = 0;
71
75
79
80 void read_header(std::span<std::byte const> bytes, ssize_t &offset);
81 void read_chunks(std::span<std::byte const> bytes, ssize_t &offset);
82 void read_IHDR(std::span<std::byte const> bytes);
83 void read_cHRM(std::span<std::byte const> bytes);
84 void read_gAMA(std::span<std::byte const> bytes);
85 void read_iBIT(std::span<std::byte const> bytes);
86 void read_iCCP(std::span<std::byte const> bytes);
87 void read_sRGB(std::span<std::byte const> bytes);
88 void generate_sRGB_transfer_function() noexcept;
89 void generate_Rec2100_transfer_function() noexcept;
90 void generate_gamma_transfer_function(float gamma) noexcept;
91 [[nodiscard]] bstring decompress_IDATs(ssize_t image_data_size) const;
92 void unfilter_lines(bstring &image_data) const;
93 void unfilter_line(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const;
94 void unfilter_line_sub(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
95 void unfilter_line_up(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
96 void unfilter_line_average(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
97 void unfilter_line_paeth(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
98 void data_to_image(bstring bytes, pixel_map<sfloat_rgba16> &image) const noexcept;
99 void data_to_image_line(std::span<std::byte const> bytes, pixel_row<sfloat_rgba16> &row) const noexcept;
100 u16x4 extract_pixel_from_line(std::span<std::byte const> bytes, int x) const noexcept;
101
102};
103
104}
Definition png.hpp:22
Definition identity.hpp:11
Definition numeric_array.hpp:27
A 2D canvas of pixels.
Definition pixel_map.hpp:100
A row of pixels.
Definition pixel_map.hpp:20
Definition URL.hpp:46
std::unique_ptr< resource_view > loadView() const
Load a resource.