Coverage Report

Created: 2026-05-24 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/context.h
Line
Count
Source
1
/*
2
 * HEIF codec.
3
 * Copyright (c) 2017 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_CONTEXT_H
22
#define LIBHEIF_CONTEXT_H
23
24
#include <map>
25
#include <memory>
26
#include <set>
27
#include <string>
28
#include <vector>
29
#include <utility>
30
31
#include "error.h"
32
33
#include "libheif/heif.h"
34
#include "libheif/heif_experimental.h"
35
#include "libheif/heif_plugin.h"
36
#include "bitstream.h"
37
38
#include "box.h" // only for color_profile, TODO: maybe move the color_profiles to its own header
39
#include "file.h"
40
#include "region.h"
41
42
#include "text.h"
43
44
class HeifFile;
45
46
class HeifPixelImage;
47
48
class StreamWriter;
49
50
class ImageItem;
51
52
class Track;
53
54
struct TrackOptions;
55
56
57
Result<std::shared_ptr<HeifPixelImage>>
58
create_alpha_image_from_image_alpha_channel(const std::shared_ptr<HeifPixelImage>& image,
59
                                            const heif_security_limits* limits);
60
61
62
// This is a higher-level view than HeifFile.
63
// Images are grouped logically into main images and their thumbnails.
64
// The class also handles automatic color-space conversion.
65
class HeifContext : public ErrorBuffer
66
{
67
public:
68
  HeifContext();
69
70
  ~HeifContext();
71
72
  static constexpr int default_max_decoding_threads = 4;
73
74
97.0k
  void set_max_decoding_threads(int max_threads) { m_max_decoding_threads = max_threads; }
75
76
15.0k
  int get_max_decoding_threads() const { return m_max_decoding_threads; }
77
78
  void set_security_limits(const heif_security_limits* limits);
79
80
99.7k
  [[nodiscard]] heif_security_limits* get_security_limits() { return &m_limits; }
81
82
116k
  [[nodiscard]] const heif_security_limits* get_security_limits() const { return &m_limits; }
83
84
  Error read(const std::shared_ptr<StreamReader>& reader);
85
86
  Error read_from_file(const char* input_filename);
87
88
  Error read_from_memory(const void* data, size_t size, bool copy);
89
90
270k
  std::shared_ptr<HeifFile> get_heif_file() const { return m_heif_file; }
91
92
93
  void set_unif(bool flag);
94
95
  bool get_unif() const;
96
97
  IDCreator& get_id_creator();
98
99
  // === image items ===
100
101
  std::vector<std::shared_ptr<ImageItem>> get_top_level_images(bool return_error_images);
102
103
0
  void insert_image_item(heif_item_id id, const std::shared_ptr<ImageItem>& img) {
104
0
    m_all_images.insert(std::make_pair(id, img));
105
0
  }
106
107
  std::shared_ptr<ImageItem> get_image(heif_item_id id, bool return_error_images);
108
109
  std::shared_ptr<const ImageItem> get_image(heif_item_id id, bool return_error_images) const
110
25.1k
  {
111
25.1k
    return const_cast<HeifContext*>(this)->get_image(id, return_error_images);
112
25.1k
  }
113
114
  std::shared_ptr<ImageItem> get_primary_image(bool return_error_image);
115
116
  std::shared_ptr<const ImageItem> get_primary_image(bool return_error_image) const;
117
118
  bool is_image(heif_item_id ID) const;
119
120
  bool has_alpha(heif_item_id ID) const;
121
122
  Result<std::shared_ptr<HeifPixelImage>> decode_image(heif_item_id ID,
123
                                                       heif_colorspace out_colorspace,
124
                                                       heif_chroma out_chroma,
125
                                                       const heif_decoding_options& options,
126
                                                       bool decode_only_tile, uint32_t tx, uint32_t ty,
127
                                                       std::set<heif_item_id> processed_ids) const;
128
129
  Result<std::shared_ptr<HeifPixelImage>> convert_to_output_colorspace(std::shared_ptr<HeifPixelImage> img,
130
                                                                       heif_colorspace out_colorspace,
131
                                                                       heif_chroma out_chroma,
132
                                                                       const heif_decoding_options& options) const;
133
134
  Result<heif_item_id> find_first_coded_image_id(heif_item_id in) const;
135
136
  std::string debug_dump_boxes() const;
137
138
  std::string debug_dump_item_data() const;
139
140
141
  // === writing ===
142
143
  [[nodiscard]] Error write(StreamWriter& writer);
144
145
  void set_write_mini_format(bool enable);
146
147
  // Create all boxes necessary for an empty HEIF file.
148
  // Note that this is no valid HEIF file, since some boxes (e.g. pitm) are generated, but
149
  // contain no valid data yet.
150
  void reset_to_empty_heif();
151
152
  Result<std::shared_ptr<ImageItem>> encode_image(const std::shared_ptr<HeifPixelImage>& image,
153
                                                  heif_encoder* encoder,
154
                                                  const heif_encoding_options& options,
155
                                                  heif_image_input_class input_class);
156
157
  void set_primary_image(const std::shared_ptr<ImageItem>& image);
158
159
0
  bool is_primary_image_set() const { return m_primary_image != nullptr; }
160
161
  Error assign_thumbnail(const std::shared_ptr<ImageItem>& master_image,
162
                         const std::shared_ptr<ImageItem>& thumbnail_image);
163
164
  Result<std::shared_ptr<ImageItem>> encode_thumbnail(const std::shared_ptr<HeifPixelImage>& image,
165
                                                      heif_encoder* encoder,
166
                                                      const heif_encoding_options& options,
167
                                                      int bbox_size);
168
169
  Error add_exif_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size);
170
171
  Error add_XMP_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size, heif_metadata_compression compression);
172
173
  Error add_generic_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size,
174
                             uint32_t item_type, const char* content_type, const char* item_uri_type,
175
                             heif_metadata_compression compression, heif_item_id* out_item_id);
176
177
  heif_property_id add_property(heif_item_id targetItem, std::shared_ptr<Box> property, bool essential);
178
179
  Result<heif_item_id> add_pyramid_group(const std::vector<heif_item_id>& layers);
180
181
  Result<heif_property_id> add_text_property(heif_item_id, const std::string& language);
182
183
184
  // --- region items
185
186
  void add_region_item(std::shared_ptr<RegionItem> region_item)
187
0
  {
188
0
    m_region_items.push_back(std::move(region_item));
189
0
  }
190
191
  Result<std::shared_ptr<RegionItem>> add_region_item(uint32_t reference_width, uint32_t reference_height);
192
193
  std::shared_ptr<RegionItem> get_region_item(heif_item_id id) const
194
0
  {
195
0
    for (auto& item : m_region_items) {
196
0
      if (item->item_id == id)
197
0
        return item;
198
0
    }
199
0
200
0
    return nullptr;
201
0
  }
202
203
  void add_region_referenced_mask_ref(heif_item_id region_item_id, heif_item_id mask_item_id);
204
205
206
  // === sequences ==
207
208
46.0k
  bool has_sequence() const { return !m_tracks.empty(); }
209
210
0
  int get_number_of_tracks() const { return static_cast<int>(m_tracks.size()); }
211
212
  std::vector<uint32_t> get_track_IDs() const;
213
214
  // If 0 is passed as track_id, the main visual track is returned (we assume that there is only one visual track).
215
  Result<std::shared_ptr<Track>> get_track(uint32_t track_id);
216
217
  Result<std::shared_ptr<const Track>> get_track(uint32_t track_id) const;
218
219
  uint32_t get_sequence_timescale() const;
220
221
  uint64_t get_sequence_duration() const;
222
223
  // Returns true if the mvhd box signals an "indefinite" / unknown duration.
224
  // For such files, an editlist in repeat mode means "loop forever".
225
  bool is_sequence_duration_indefinite() const;
226
227
  void set_sequence_timescale(uint32_t timescale);
228
229
  void set_number_of_sequence_repetitions(uint32_t repetitions);
230
231
  Result<std::shared_ptr<class Track_Visual>> add_visual_sequence_track(const TrackOptions*, uint32_t handler_type,
232
                                                                        uint16_t width, uint16_t height);
233
234
  Result<std::shared_ptr<class Track_Metadata>> add_uri_metadata_sequence_track(const TrackOptions*, std::string uri);
235
236
  void add_text_item(std::shared_ptr<TextItem> text_item)
237
0
  {
238
0
    m_text_items.push_back(std::move(text_item));
239
0
  }
240
241
  Result<std::shared_ptr<TextItem>> add_text_item(const char* content_type, const char* text);
242
243
  std::shared_ptr<TextItem> get_text_item(heif_item_id id) const
244
0
  {
245
0
    for (auto& item : m_text_items) {
246
0
      if (item->get_item_id() == id)
247
0
        return item;
248
0
    }
249
0
250
0
    return nullptr;
251
0
  }
252
253
  template<typename T>
254
  Result<std::shared_ptr<T>> find_property(heif_item_id itemId, heif_property_id propertyId)
255
0
  {
256
0
    auto file = this->get_heif_file();
257
258
    // For propertyId == 0, return the first property with this type.
259
0
    if (propertyId == 0) {
260
0
      return find_property<T>(itemId);
261
0
    }
262
263
0
    std::vector<std::shared_ptr<Box>> properties;
264
0
    Error err = file->get_properties(itemId, properties);
265
0
    if (err) {
266
0
      return err;
267
0
    }
268
269
0
    if (propertyId - 1 >= properties.size()) {
270
0
      Error(heif_error_Usage_error, heif_suberror_Invalid_property, "property index out of range");
271
0
    }
272
273
0
    auto box = properties[propertyId - 1];
274
0
    auto box_casted = std::dynamic_pointer_cast<T>(box);
275
0
    if (!box_casted) {
276
0
      return Error(heif_error_Usage_error, heif_suberror_Invalid_property, "wrong property type");
277
0
    }
278
279
0
    return box_casted;
280
0
  }
Unexecuted instantiation: Result<std::__1::shared_ptr<Box_udes> > HeifContext::find_property<Box_udes>(unsigned int, unsigned int)
Unexecuted instantiation: Result<std::__1::shared_ptr<Box_imir> > HeifContext::find_property<Box_imir>(unsigned int, unsigned int)
Unexecuted instantiation: Result<std::__1::shared_ptr<Box_irot> > HeifContext::find_property<Box_irot>(unsigned int, unsigned int)
Unexecuted instantiation: Result<std::__1::shared_ptr<Box_clap> > HeifContext::find_property<Box_clap>(unsigned int, unsigned int)
Unexecuted instantiation: Result<std::__1::shared_ptr<Box_other> > HeifContext::find_property<Box_other>(unsigned int, unsigned int)
281
282
  template<typename T>
283
0
  Result<std::shared_ptr<T>> find_property(heif_item_id itemId) {
284
0
    auto file = this->get_heif_file();
285
0
    auto result = file->get_property_for_item<T>(itemId);
286
0
    if (!result) {
287
0
      return Error(heif_error_Invalid_input,
288
0
                   heif_suberror_No_properties_assigned_to_item,
289
0
                   "property not found on item");
290
0
    }
291
0
    return result;
292
0
  }
Unexecuted instantiation: Result<std::__1::shared_ptr<Box_elng> > HeifContext::find_property<Box_elng>(unsigned int)
Unexecuted instantiation: Result<std::__1::shared_ptr<Box_udes> > HeifContext::find_property<Box_udes>(unsigned int)
Unexecuted instantiation: Result<std::__1::shared_ptr<Box_imir> > HeifContext::find_property<Box_imir>(unsigned int)
Unexecuted instantiation: Result<std::__1::shared_ptr<Box_irot> > HeifContext::find_property<Box_irot>(unsigned int)
Unexecuted instantiation: Result<std::__1::shared_ptr<Box_clap> > HeifContext::find_property<Box_clap>(unsigned int)
Unexecuted instantiation: Result<std::__1::shared_ptr<Box_other> > HeifContext::find_property<Box_other>(unsigned int)
293
294
  template<typename T>
295
  bool has_property(heif_item_id itemId) const
296
  {
297
    auto file = this->get_heif_file();
298
    auto result = file->get_property_for_item<T>(itemId);
299
    return result != nullptr;
300
  }
301
302
private:
303
  std::map<heif_item_id, std::shared_ptr<ImageItem>> m_all_images;
304
305
  // We store this in a vector because we need stable indices for the C API.
306
  // TODO: stable indices are obsolet now...
307
  std::vector<std::shared_ptr<ImageItem>> m_top_level_images;
308
309
  std::shared_ptr<ImageItem> m_primary_image; // shortcut to primary image
310
311
  std::shared_ptr<HeifFile> m_heif_file;
312
313
  int m_max_decoding_threads = default_max_decoding_threads;
314
315
  heif_security_limits m_limits;
316
  TotalMemoryTracker m_memory_tracker;
317
318
  std::vector<std::shared_ptr<RegionItem>> m_region_items;
319
  std::vector<std::shared_ptr<TextItem>> m_text_items;
320
321
  // --- sequences
322
323
  std::map<uint32_t, std::shared_ptr<Track>> m_tracks;
324
  uint32_t m_visual_track_id = 0;
325
  uint32_t m_sequence_repetitions = 1;
326
327
  Error interpret_heif_file();
328
329
  Error interpret_heif_file_images();
330
331
  Error interpret_heif_file_sequences();
332
333
  void remove_top_level_image(const std::shared_ptr<ImageItem>& image);
334
};
335
336
#endif