Coverage Report

Created: 2026-09-01 06:54

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