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