digiKam
heif_context.h
Go to the documentation of this file.
1 /*
2  * HEIF codec.
3  * Copyright (c) 2017 struktur AG, Dirk Farin <farin@struktur.de>
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_HEIF_CONTEXT_H
22 #define LIBHEIF_HEIF_CONTEXT_H
23 
24 #include <map>
25 #include <memory>
26 #include <set>
27 #include <string>
28 #include <vector>
29 
30 #include "error.h"
31 
32 #include "heif.h"
33 #include "heif_plugin.h"
34 #include "bitstream.h"
35 
36 #include "box.h" // only for color_profile, TODO: maybe move the color_profiles to its own header
37 
38 namespace heif {
39 class HeifContext;
40 }
41 
42 
43 namespace heif {
44 
45  class HeifFile;
46  class HeifPixelImage;
47  class StreamWriter;
48 
49 
51  {
52  public:
54  std::string item_type; // e.g. "Exif"
55  std::string content_type;
56  std::vector<uint8_t> m_data;
57  };
58 
59 
60  // This is a higher-level view than HeifFile.
61  // Images are grouped logically into main images and their thumbnails.
62  // The class also handles automatic color-space conversion.
63  class HeifContext : public ErrorBuffer {
64  public:
67 
68  void set_max_decoding_threads(int max_threads) { m_max_decoding_threads = max_threads; }
69 
70  void set_maximum_image_size_limit(int maximum_size) {
71  m_maximum_image_width_limit = maximum_size;
72  m_maximum_image_height_limit = maximum_size;
73  }
74 
75  Error read(std::shared_ptr<StreamReader> reader);
76  Error read_from_file(const char* input_filename);
77  Error read_from_memory(const void* data, size_t size, bool copy);
78 
79  class Image : public ErrorBuffer {
80  public:
82  ~Image();
83 
84  void set_resolution(int w,int h) { m_width=w; m_height=h; }
85  void set_ispe_resolution(int w,int h) { m_ispe_width=w; m_ispe_height=h; }
86 
87  void set_primary(bool flag=true) { m_is_primary=flag; }
88 
89  heif_item_id get_id() const { return m_id; }
90 
91  //void set_id(heif_item_id id) { m_id=id; } (already set in constructor)
92 
93  int get_width() const { return m_width; }
94  int get_height() const { return m_height; }
95 
96  int get_ispe_width() const { return m_ispe_width; }
97  int get_ispe_height() const { return m_ispe_height; }
98 
101 
102  bool is_primary() const { return m_is_primary; }
103 
104  Error decode_image(std::shared_ptr<HeifPixelImage>& img,
107  const struct heif_decoding_options* options = nullptr) const;
108 
109 
110  // -- thumbnails
111 
112  void set_is_thumbnail_of(heif_item_id id) { m_is_thumbnail=true; m_thumbnail_ref_id=id; }
113  void add_thumbnail(std::shared_ptr<Image> img) { m_thumbnails.push_back(img); }
114 
115  bool is_thumbnail() const { return m_is_thumbnail; }
116  std::vector<std::shared_ptr<Image>> get_thumbnails() const { return m_thumbnails; }
117 
118 
119  // --- alpha channel
120 
121  void set_is_alpha_channel_of(heif_item_id id) { m_is_alpha_channel=true; m_alpha_channel_ref_id=id; }
122  void set_alpha_channel(std::shared_ptr<Image> img) { m_alpha_channel=img; }
123 
124  bool is_alpha_channel() const { return m_is_alpha_channel; }
125  std::shared_ptr<Image> get_alpha_channel() const { return m_alpha_channel; }
126 
127 
128  // --- depth channel
129 
130  void set_is_depth_channel_of(heif_item_id id) { m_is_depth_channel=true; m_depth_channel_ref_id=id; }
131  void set_depth_channel(std::shared_ptr<Image> img) { m_depth_channel=img; }
132 
133  bool is_depth_channel() const { return m_is_depth_channel; }
134  std::shared_ptr<Image> get_depth_channel() const { return m_depth_channel; }
135 
136 
138  m_has_depth_representation_info = true;
139  m_depth_representation_info = info;
140  }
141 
143  return m_has_depth_representation_info;
144  }
145 
147  return m_depth_representation_info;
148  }
149 
150 
151  // --- metadata
152 
153  void add_metadata(std::shared_ptr<ImageMetadata> metadata) {
154  m_metadata.push_back(metadata);
155  }
156 
157  std::vector<std::shared_ptr<ImageMetadata>> get_metadata() const { return m_metadata; }
158 
159 
160  // === writing ===
161 
162  void set_preencoded_hevc_image(const std::vector<uint8_t>& data);
163 
164  Error encode_image_as_hevc(std::shared_ptr<HeifPixelImage> image,
165  struct heif_encoder* encoder,
166  const struct heif_encoding_options* options,
167  enum heif_image_input_class input_class);
168 
169  std::shared_ptr<const color_profile> get_color_profile() const { return m_color_profile; }
170 
171  void set_color_profile(std::shared_ptr<const color_profile> profile) { m_color_profile = profile; };
172 
173  private:
174  HeifContext* m_heif_context;
175 
176  heif_item_id m_id = 0;
177  uint32_t m_width=0, m_height=0;
178  uint32_t m_ispe_width=0, m_ispe_height=0; // original image resolution
179  bool m_is_primary = false;
180 
181  bool m_is_thumbnail = false;
182  heif_item_id m_thumbnail_ref_id = 0;
183 
184  std::vector<std::shared_ptr<Image>> m_thumbnails;
185 
186  bool m_is_alpha_channel = false;
187  heif_item_id m_alpha_channel_ref_id = 0;
188  std::shared_ptr<Image> m_alpha_channel;
189 
190  bool m_is_depth_channel = false;
191  heif_item_id m_depth_channel_ref_id = 0;
192  std::shared_ptr<Image> m_depth_channel;
193 
194  bool m_has_depth_representation_info = false;
195  struct heif_depth_representation_info m_depth_representation_info;
196 
197  std::vector<std::shared_ptr<ImageMetadata>> m_metadata;
198 
199  std::shared_ptr<const color_profile> m_color_profile;
200  };
201 
202  std::vector<std::shared_ptr<Image>> get_top_level_images() { return m_top_level_images; }
203 
204  std::shared_ptr<Image> get_primary_image() { return m_primary_image; }
205 
206  void register_decoder(const heif_decoder_plugin* decoder_plugin);
207 
208  bool is_image(heif_item_id ID) const;
209 
210  Error decode_image(heif_item_id ID, std::shared_ptr<HeifPixelImage>& img,
211  const struct heif_decoding_options* options = nullptr) const;
212 
213  std::string debug_dump_boxes() const;
214 
215 
216  // === writing ===
217 
218  // Create all boxes necessary for an empty HEIF file.
219  // Note that this is no valid HEIF file, since some boxes (e.g. pitm) are generated, but
220  // contain no valid data yet.
222 
223  Error encode_image(std::shared_ptr<HeifPixelImage> image,
224  struct heif_encoder* encoder,
225  const struct heif_encoding_options* options,
226  enum heif_image_input_class input_class,
227  std::shared_ptr<Image>& out_image);
228 
229  void set_primary_image(std::shared_ptr<Image> image);
230 
232 
233  bool is_primary_image_set() const { return !!m_primary_image; }
234 
235  Error assign_thumbnail(std::shared_ptr<Image> master_image,
236  std::shared_ptr<Image> thumbnail_image);
237 
238  Error encode_thumbnail(std::shared_ptr<HeifPixelImage> image,
239  struct heif_encoder* encoder,
240  const struct heif_encoding_options* options,
241  int bbox_size,
242  std::shared_ptr<Image>& out_image_handle);
243 
244  Error add_exif_metadata(std::shared_ptr<Image> master_image, const void* data, int size);
245 
246  Error add_XMP_metadata(std::shared_ptr<Image> master_image, const void* data, int size);
247 
248  Error add_generic_metadata(std::shared_ptr<Image> master_image, const void* data, int size,
249  const char* item_type, const char* content_type);
250 
251  void write(StreamWriter& writer);
252 
253  private:
254  const struct heif_decoder_plugin* get_decoder(enum heif_compression_format type) const;
255 
256  std::set<const struct heif_decoder_plugin*> m_decoder_plugins;
257 
258  std::map<heif_item_id, std::shared_ptr<Image>> m_all_images;
259 
260  // We store this in a vector because we need stable indices for the C API.
261  // TODO: stable indices are obsolet now...
262  std::vector<std::shared_ptr<Image>> m_top_level_images;
263 
264  std::shared_ptr<Image> m_primary_image; // shortcut to primary image
265 
266  std::shared_ptr<HeifFile> m_heif_file;
267 
268  int m_max_decoding_threads = 4;
269 
270  uint32_t m_maximum_image_width_limit;
271  uint32_t m_maximum_image_height_limit;
272 
273  Error interpret_heif_file();
274 
275  void remove_top_level_image(std::shared_ptr<Image> image);
276 
277  Error decode_full_grid_image(heif_item_id ID,
278  std::shared_ptr<HeifPixelImage>& img,
279  const std::vector<uint8_t>& grid_data) const;
280 
281  Error decode_and_paste_tile_image(heif_item_id tileID,
282  std::shared_ptr<HeifPixelImage> out_image,
283  int x0,int y0) const;
284 
285  Error decode_derived_image(heif_item_id ID,
286  std::shared_ptr<HeifPixelImage>& img) const;
287 
288  Error decode_overlay_image(heif_item_id ID,
289  std::shared_ptr<HeifPixelImage>& img,
290  const std::vector<uint8_t>& overlay_data) const;
291 
292  Error get_id_of_non_virtual_child_image(heif_item_id in, heif_item_id& out) const;
293  };
294 }
295 
296 #endif
Definition: error.h:50
Definition: error.h:75
Definition: heif_context.h:79
void set_primary(bool flag=true)
Definition: heif_context.h:87
int get_height() const
Definition: heif_context.h:94
bool is_alpha_channel() const
Definition: heif_context.h:124
void set_is_alpha_channel_of(heif_item_id id)
Definition: heif_context.h:121
int get_width() const
Definition: heif_context.h:93
int get_ispe_width() const
Definition: heif_context.h:96
heif_item_id get_id() const
Definition: heif_context.h:89
std::shared_ptr< const color_profile > get_color_profile() const
Definition: heif_context.h:169
bool is_depth_channel() const
Definition: heif_context.h:133
bool has_depth_representation_info() const
Definition: heif_context.h:142
void set_preencoded_hevc_image(const std::vector< uint8_t > &data)
bool is_primary() const
Definition: heif_context.h:102
void set_depth_channel(std::shared_ptr< Image > img)
Definition: heif_context.h:131
void add_metadata(std::shared_ptr< ImageMetadata > metadata)
Definition: heif_context.h:153
int get_chroma_bits_per_pixel() const
Image(HeifContext *file, heif_item_id id)
std::shared_ptr< Image > get_depth_channel() const
Definition: heif_context.h:134
bool is_thumbnail() const
Definition: heif_context.h:115
Error decode_image(std::shared_ptr< HeifPixelImage > &img, heif_colorspace colorspace=heif_colorspace_undefined, heif_chroma chroma=heif_chroma_undefined, const struct heif_decoding_options *options=nullptr) const
void set_is_depth_channel_of(heif_item_id id)
Definition: heif_context.h:130
std::vector< std::shared_ptr< ImageMetadata > > get_metadata() const
Definition: heif_context.h:157
std::vector< std::shared_ptr< Image > > get_thumbnails() const
Definition: heif_context.h:116
int get_luma_bits_per_pixel() const
void add_thumbnail(std::shared_ptr< Image > img)
Definition: heif_context.h:113
int get_ispe_height() const
Definition: heif_context.h:97
std::shared_ptr< Image > get_alpha_channel() const
Definition: heif_context.h:125
void set_is_thumbnail_of(heif_item_id id)
Definition: heif_context.h:112
void set_ispe_resolution(int w, int h)
Definition: heif_context.h:85
void set_resolution(int w, int h)
Definition: heif_context.h:84
void set_alpha_channel(std::shared_ptr< Image > img)
Definition: heif_context.h:122
void set_depth_representation_info(struct heif_depth_representation_info &info)
Definition: heif_context.h:137
void set_color_profile(std::shared_ptr< const color_profile > profile)
Definition: heif_context.h:171
Error encode_image_as_hevc(std::shared_ptr< HeifPixelImage > image, struct heif_encoder *encoder, const struct heif_encoding_options *options, enum heif_image_input_class input_class)
const struct heif_depth_representation_info & get_depth_representation_info() const
Definition: heif_context.h:146
Definition: heif_context.h:63
bool is_image(heif_item_id ID) const
std::string debug_dump_boxes() const
void write(StreamWriter &writer)
void set_primary_image(std::shared_ptr< Image > image)
Error set_primary_item(heif_item_id id)
bool is_primary_image_set() const
Definition: heif_context.h:233
Error add_XMP_metadata(std::shared_ptr< Image > master_image, const void *data, int size)
Error add_exif_metadata(std::shared_ptr< Image > master_image, const void *data, int size)
std::shared_ptr< Image > get_primary_image()
Definition: heif_context.h:204
Error read(std::shared_ptr< StreamReader > reader)
Error decode_image(heif_item_id ID, std::shared_ptr< HeifPixelImage > &img, const struct heif_decoding_options *options=nullptr) const
Error add_generic_metadata(std::shared_ptr< Image > master_image, const void *data, int size, const char *item_type, const char *content_type)
Error encode_image(std::shared_ptr< HeifPixelImage > image, struct heif_encoder *encoder, const struct heif_encoding_options *options, enum heif_image_input_class input_class, std::shared_ptr< Image > &out_image)
Error read_from_memory(const void *data, size_t size, bool copy)
Error read_from_file(const char *input_filename)
void set_maximum_image_size_limit(int maximum_size)
Definition: heif_context.h:70
void register_decoder(const heif_decoder_plugin *decoder_plugin)
Error assign_thumbnail(std::shared_ptr< Image > master_image, std::shared_ptr< Image > thumbnail_image)
void set_max_decoding_threads(int max_threads)
Definition: heif_context.h:68
Error encode_thumbnail(std::shared_ptr< HeifPixelImage > image, struct heif_encoder *encoder, const struct heif_encoding_options *options, int bbox_size, std::shared_ptr< Image > &out_image_handle)
std::vector< std::shared_ptr< Image > > get_top_level_images()
Definition: heif_context.h:202
void reset_to_empty_heif()
Definition: heif_context.h:51
heif_item_id item_id
Definition: heif_context.h:53
std::vector< uint8_t > m_data
Definition: heif_context.h:56
std::string item_type
Definition: heif_context.h:54
std::string content_type
Definition: heif_context.h:55
Definition: bitstream.h:256
heif_colorspace
Definition: heif.h:741
@ heif_colorspace_undefined
Definition: heif.h:742
heif_chroma
Definition: heif.h:722
@ heif_chroma_undefined
Definition: heif.h:723
heif_compression_format
Definition: heif.h:715
uint32_t heif_item_id
Definition: heif.h:276
heif_image_input_class
Definition: heif_plugin.h:111
Definition: bitstream.h:41
const struct heif_decoder_plugin * get_decoder(enum heif_compression_format type)
Definition: heif_plugin.h:51
Definition: heif.h:767
Definition: heif.h:520
Definition: heif_api_structs.h:51
Definition: heif.h:1121