/src/libheif/libheif/image-items/grid.h
Line | Count | Source |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2024 Dirk Farin <dirk.farin@gmail.com> |
4 | | * |
5 | | * This file is part of libheif. |
6 | | * |
7 | | * libheif is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libheif is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #ifndef LIBHEIF_IMAGEITEM_GRID_H |
22 | | #define LIBHEIF_IMAGEITEM_GRID_H |
23 | | |
24 | | #include "image_item.h" |
25 | | #include <vector> |
26 | | #include <string> |
27 | | #include <memory> |
28 | | #include <set> |
29 | | |
30 | | |
31 | | class ImageGrid |
32 | | { |
33 | | public: |
34 | | Error parse(const std::vector<uint8_t>& data); |
35 | | |
36 | | std::vector<uint8_t> write() const; |
37 | | |
38 | | std::string dump() const; |
39 | | |
40 | 0 | uint32_t get_width() const { return m_output_width; } |
41 | | |
42 | 0 | uint32_t get_height() const { return m_output_height; } |
43 | | |
44 | | uint16_t get_rows() const |
45 | 0 | { |
46 | 0 | return m_rows; |
47 | 0 | } |
48 | | |
49 | | uint16_t get_columns() const |
50 | 0 | { |
51 | 0 | return m_columns; |
52 | 0 | } |
53 | | |
54 | | void set_num_tiles(uint16_t columns, uint16_t rows) |
55 | 0 | { |
56 | 0 | m_rows = rows; |
57 | 0 | m_columns = columns; |
58 | 0 | } |
59 | | |
60 | | void set_output_size(uint32_t width, uint32_t height) |
61 | 0 | { |
62 | 0 | m_output_width = width; |
63 | 0 | m_output_height = height; |
64 | 0 | } |
65 | | |
66 | | private: |
67 | | uint16_t m_rows = 0; |
68 | | uint16_t m_columns = 0; |
69 | | uint32_t m_output_width = 0; |
70 | | uint32_t m_output_height = 0; |
71 | | }; |
72 | | |
73 | | |
74 | | |
75 | | |
76 | | |
77 | | class ImageItem_Grid : public ImageItem |
78 | | { |
79 | | public: |
80 | | ImageItem_Grid(HeifContext* ctx, heif_item_id id); |
81 | | |
82 | | ImageItem_Grid(HeifContext* ctx); |
83 | | |
84 | | ~ImageItem_Grid() override; |
85 | | |
86 | 0 | uint32_t get_infe_type() const override { return fourcc("grid"); } |
87 | | |
88 | | static Result<std::shared_ptr<ImageItem_Grid>> add_new_grid_item(HeifContext* ctx, |
89 | | uint32_t output_width, |
90 | | uint32_t output_height, |
91 | | uint16_t tile_rows, |
92 | | uint16_t tile_columns, |
93 | | const heif_encoding_options* encoding_options); |
94 | | |
95 | | Error add_image_tile(uint32_t tile_x, uint32_t tile_y, |
96 | | const std::shared_ptr<HeifPixelImage>& image, |
97 | | heif_encoder* encoder); |
98 | | |
99 | | static Result<std::shared_ptr<ImageItem_Grid>> add_and_encode_full_grid(HeifContext* ctx, |
100 | | const std::vector<std::shared_ptr<HeifPixelImage>>& tiles, |
101 | | uint16_t rows, |
102 | | uint16_t columns, |
103 | | heif_encoder* encoder, |
104 | | const heif_encoding_options& options); |
105 | | |
106 | | |
107 | | // TODO: nclx depends on contained format |
108 | | // const heif_color_profile_nclx* get_forced_output_nclx() const override { return nullptr; } |
109 | | |
110 | | // heif_compression_format get_compression_format() const override { return heif_compression_HEVC; } |
111 | | |
112 | | Error initialize_decoder() override; |
113 | | |
114 | | // Delegates to the first grid tile's already-populated descriptions and |
115 | | // rescales per-component dims to the grid's full ispe size, so the handle |
116 | | // exposes the correct datatype/bit-depth even for unci float tiles |
117 | | // (which the base populate would mis-tag as unsigned_integer). |
118 | | void populate_component_descriptions() override; |
119 | | |
120 | | int get_luma_bits_per_pixel() const override; |
121 | | |
122 | | int get_chroma_bits_per_pixel() const override; |
123 | | |
124 | | void set_tile_encoding_options(const heif_encoding_options* options); |
125 | | |
126 | | Result<Encoder::CodedImageData> encode(const std::shared_ptr<HeifPixelImage>& image, |
127 | | heif_encoder* encoder, |
128 | | const heif_encoding_options& options, |
129 | | heif_image_input_class input_class) override |
130 | 0 | { |
131 | 0 | return Error{heif_error_Unsupported_feature, |
132 | 0 | heif_suberror_Unspecified, "Cannot encode image to 'grid'"}; |
133 | 0 | } |
134 | | |
135 | | Result<std::shared_ptr<HeifPixelImage>> decode_compressed_image(const heif_decoding_options& options, |
136 | | bool decode_tile_only, uint32_t tile_x0, uint32_t tile_y0, |
137 | | std::set<heif_item_id> processed_ids) const override; |
138 | | |
139 | | heif_brand2 get_compatible_brand() const override; |
140 | | |
141 | | protected: |
142 | | Result<std::shared_ptr<Decoder>> get_decoder() const override; |
143 | | |
144 | | public: |
145 | | |
146 | | // --- grid specific |
147 | | |
148 | 0 | const ImageGrid& get_grid_spec() const { return m_grid_spec; } |
149 | | |
150 | 0 | void set_grid_spec(const ImageGrid& grid) { m_grid_spec = grid; m_grid_tile_ids.resize(size_t{grid.get_rows()} * grid.get_columns()); } |
151 | | |
152 | 0 | const std::vector<heif_item_id>& get_grid_tiles() const { return m_grid_tile_ids; } |
153 | | |
154 | | void set_grid_tile_id(uint32_t tile_x, uint32_t tile_y, heif_item_id); |
155 | | |
156 | | heif_image_tiling get_heif_image_tiling() const override; |
157 | | |
158 | | void get_tile_size(uint32_t& w, uint32_t& h) const override; |
159 | | |
160 | | private: |
161 | | ImageGrid m_grid_spec; |
162 | | std::vector<heif_item_id> m_grid_tile_ids; |
163 | | |
164 | | heif_orientation m_grid_orientation = heif_orientation_normal; |
165 | | heif_encoding_options* m_tile_encoding_options = nullptr; |
166 | | |
167 | | Error read_grid_spec(); |
168 | | |
169 | | Result<std::shared_ptr<HeifPixelImage>> decode_full_grid_image(const heif_decoding_options& options, std::set<heif_item_id> processed_ids) const; |
170 | | |
171 | | Result<std::shared_ptr<HeifPixelImage>> decode_grid_tile(const heif_decoding_options& options, uint32_t tx, uint32_t ty, std::set<heif_item_id> processed_ids) const; |
172 | | |
173 | | Error decode_and_paste_tile_image(heif_item_id tileID, uint32_t x0, uint32_t y0, |
174 | | std::shared_ptr<HeifPixelImage>& inout_image, |
175 | | const heif_decoding_options& options, int& progress_counter, |
176 | | std::shared_ptr<std::vector<Error> > warnings, |
177 | | std::set<heif_item_id> processed_ids) const; |
178 | | }; |
179 | | |
180 | | |
181 | | #endif //LIBHEIF_GRID_H |