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
7#include "../utility.hpp"
8#include "../pixel_map.hpp"
9#include "../rapid/sfloat_rgba16.hpp"
10#include "../geometry/identity.hpp"
11#include "../URL.hpp"
12#include "../resource_view.hpp"
13#include "../byte_string.hpp"
14#include "../strings.hpp"
15#include <span>
16#include <vector>
17#include <cstddef>
18#include <cstdint>
19#include <numeric>
20
21namespace hi::inline v1 {
22
23class png {
24public:
25 [[nodiscard]] png(std::span<std::byte const> bytes);
26
27 [[nodiscard]] png(std::unique_ptr<resource_view> view);
28
29 [[nodiscard]] png(URL const &url) : png(url.loadView()) {}
30
31 [[nodiscard]] std::size_t width() const noexcept
32 {
33 return _width;
34 }
35
36 [[nodiscard]] std::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, std::size_t &offset);
81 void read_chunks(std::span<std::byte const> bytes, std::size_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(std::size_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} // namespace hi::inline v1
Utilities used by the HikoGUI library itself.
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:23
A 2D canvas of pixels.
Definition pixel_map.hpp:111
A row of pixels.
Definition pixel_map.hpp:29
Definition numeric_array.hpp:59