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#pragma once
6
8#include "../utility.hpp"
9#include "../pixel_map.hpp"
10#include "../rapid/sfloat_rgba16.hpp"
12#include "../byte_string.hpp"
13#include "../strings.hpp"
14#include <span>
15#include <vector>
16#include <cstddef>
17#include <cstdint>
18#include <numeric>
19#include <filesystem>
20#include <memory>
21
22namespace hi::inline v1 {
23
24class png {
25public:
26 [[nodiscard]] png(file_view view);
27
28 [[nodiscard]] png(std::filesystem::path const& path) : png(file_view{path}) {}
29
30 [[nodiscard]] std::size_t width() const noexcept
31 {
32 return _width;
33 }
34
35 [[nodiscard]] std::size_t height() const noexcept
36 {
37 return _height;
38 }
39
40 void decode_image(pixel_map<sfloat_rgba16>& image) const;
41
42 [[nodiscard]] static pixel_map<sfloat_rgba16> load(std::filesystem::path const& path);
43
44private:
48 matrix3 _color_to_sRGB = geo::identity();
49
52 std::vector<float> _transfer_function;
53
54 int _width = 0;
55 int _height = 0;
56 int _bit_depth = 0;
57 int _color_type = 0;
58 int _compression_method = 0;
59 int _filter_method = 0;
60 int _interlace_method = 0;
61
62 bool _has_alpha;
63 bool _is_palletted;
64 bool _is_color;
65 int _samples_per_pixel = 0;
66 int _bits_per_pixel = 0;
67 int _bytes_per_pixel = 0;
68 int _bytes_per_line = 0;
69 int _stride = 0;
70
74
77 file_view _view;
78
79 void read_header(std::span<std::byte const> bytes, std::size_t& offset);
80 void read_chunks(std::span<std::byte const> bytes, std::size_t& offset);
81 void read_IHDR(std::span<std::byte const> bytes);
82 void read_cHRM(std::span<std::byte const> bytes);
83 void read_gAMA(std::span<std::byte const> bytes);
84 void read_iBIT(std::span<std::byte const> bytes);
85 void read_iCCP(std::span<std::byte const> bytes);
86 void read_sRGB(std::span<std::byte const> bytes);
87 void generate_sRGB_transfer_function() noexcept;
88 void generate_Rec2100_transfer_function() noexcept;
89 void generate_gamma_transfer_function(float gamma) noexcept;
90 [[nodiscard]] bstring decompress_IDATs(std::size_t image_data_size) const;
91 void unfilter_lines(bstring& image_data) const;
92 void unfilter_line(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const;
93 void unfilter_line_sub(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
94 void unfilter_line_up(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
95 void unfilter_line_average(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
96 void unfilter_line_paeth(std::span<uint8_t> line, std::span<uint8_t const> prev_line) const noexcept;
97 void data_to_image(bstring bytes, pixel_map<sfloat_rgba16>& image) const noexcept;
98 void data_to_image_line(std::span<std::byte const> bytes, pixel_row<sfloat_rgba16>& row) const noexcept;
99 u16x4 extract_pixel_from_line(std::span<std::byte const> bytes, int x) const noexcept;
100};
101
102} // namespace hi::inline v1
Defines the file_view class.
Utilities used by the HikoGUI library itself.
Defines identity type.
DOXYGEN BUG.
Definition algorithm.hpp:15
hi_force_inline T load(void const *src) noexcept
Load an integer from unaligned memory in native byte-order.
Definition endian.hpp:201
Definition png.hpp:24
A 2D canvas of pixels.
Definition pixel_map.hpp:111
A row of pixels.
Definition pixel_map.hpp:29
Definition numeric_array.hpp:59