Coverage Report

Created: 2026-08-31 07:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libultrahdr/third_party/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
40
#include "region.h"
41
42
class HeifFile;
43
44
class HeifPixelImage;
45
46
class StreamWriter;
47
48
class ImageItem;
49
50
class ImageMetadata;
51
52
// This is a higher-level view than HeifFile.
53
// Images are grouped logically into main images and their thumbnails.
54
// The class also handles automatic color-space conversion.
55
class HeifContext : public ErrorBuffer
56
{
57
public:
58
  HeifContext();
59
60
  ~HeifContext();
61
62
0
  void set_max_decoding_threads(int max_threads) { m_max_decoding_threads = max_threads; }
63
64
0
  int get_max_decoding_threads() const { return m_max_decoding_threads; }
65
66
  void set_security_limits(const heif_security_limits* limits);
67
68
0
  [[nodiscard]] heif_security_limits* get_security_limits() { return &m_limits; }
69
70
0
  [[nodiscard]] const heif_security_limits* get_security_limits() const { return &m_limits; }
71
72
  Error read(const std::shared_ptr<StreamReader>& reader);
73
74
  Error read_from_file(const char* input_filename);
75
76
  Error read_from_memory(const void* data, size_t size, bool copy);
77
78
0
  std::shared_ptr<HeifFile> get_heif_file() const { return m_heif_file; }
79
80
81
  // === image items ===
82
83
  std::vector<std::shared_ptr<ImageItem>> get_top_level_images(bool return_error_images);
84
85
0
  void insert_image_item(heif_item_id id, const std::shared_ptr<ImageItem>& img) {
86
0
    m_all_images.insert(std::make_pair(id, img));
87
0
  }
88
89
  std::shared_ptr<ImageItem> get_image(heif_item_id id, bool return_error_images);
90
91
  std::shared_ptr<const ImageItem> get_image(heif_item_id id, bool return_error_images) const
92
0
  {
93
0
    return const_cast<HeifContext*>(this)->get_image(id, return_error_images);
94
0
  }
95
96
  std::shared_ptr<ImageItem> get_primary_image(bool return_error_image);
97
98
  bool is_image(heif_item_id ID) const;
99
100
  bool has_alpha(heif_item_id ID) const;
101
102
  Result<std::shared_ptr<HeifPixelImage>> decode_image(heif_item_id ID,
103
                                                       heif_colorspace out_colorspace,
104
                                                       heif_chroma out_chroma,
105
                                                       const struct heif_decoding_options& options,
106
                                                       bool decode_only_tile, uint32_t tx, uint32_t ty) const;
107
108
  Error get_id_of_non_virtual_child_image(heif_item_id in, heif_item_id& out) const;
109
110
  std::string debug_dump_boxes() const;
111
112
113
  // === writing ===
114
115
  void write(StreamWriter& writer);
116
117
  // Create all boxes necessary for an empty HEIF file.
118
  // Note that this is no valid HEIF file, since some boxes (e.g. pitm) are generated, but
119
  // contain no valid data yet.
120
  void reset_to_empty_heif();
121
122
  Result<std::shared_ptr<ImageItem>> encode_image(const std::shared_ptr<HeifPixelImage>& image,
123
                                                  struct heif_encoder* encoder,
124
                                                  const struct heif_encoding_options& options,
125
                                                  enum heif_image_input_class input_class);
126
127
  void set_primary_image(const std::shared_ptr<ImageItem>& image);
128
129
0
  bool is_primary_image_set() const { return m_primary_image != nullptr; }
130
131
  Error assign_thumbnail(const std::shared_ptr<ImageItem>& master_image,
132
                         const std::shared_ptr<ImageItem>& thumbnail_image);
133
134
  Result<std::shared_ptr<ImageItem>> encode_thumbnail(const std::shared_ptr<HeifPixelImage>& image,
135
                                                      struct heif_encoder* encoder,
136
                                                      const struct heif_encoding_options& options,
137
                                                      int bbox_size);
138
139
  Error add_exif_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size);
140
141
  Error add_XMP_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size, heif_metadata_compression compression);
142
143
  Error add_generic_metadata(const std::shared_ptr<ImageItem>& master_image, const void* data, int size,
144
                             uint32_t item_type, const char* content_type, const char* item_uri_type,
145
                             heif_metadata_compression compression, heif_item_id* out_item_id);
146
147
  heif_property_id add_property(heif_item_id targetItem, std::shared_ptr<Box> property, bool essential);
148
149
  Result<heif_item_id> add_pyramid_group(const std::vector<heif_item_id>& layers);
150
151
#if WITH_EXPERIMENTAL_GAIN_MAP
152
  Error add_tmap_item(const std::vector<uint8_t>& metadata, heif_item_id& item_id);
153
154
  Error link_gain_map(const std::shared_ptr<ImageItem>& primary_image,
155
                      const std::shared_ptr<ImageItem>& gain_map_image, const heif_item_id tmap_id);
156
#endif
157
158
159
  // --- region items
160
161
  void add_region_item(std::shared_ptr<RegionItem> region_item)
162
0
  {
163
0
    m_region_items.push_back(std::move(region_item));
164
0
  }
165
166
  std::shared_ptr<RegionItem> add_region_item(uint32_t reference_width, uint32_t reference_height);
167
168
  std::shared_ptr<RegionItem> get_region_item(heif_item_id id) const
169
0
  {
170
0
    for (auto& item : m_region_items) {
171
0
      if (item->item_id == id)
172
0
        return item;
173
0
    }
174
0
175
0
    return nullptr;
176
0
  }
177
178
  void add_region_referenced_mask_ref(heif_item_id region_item_id, heif_item_id mask_item_id);
179
180
private:
181
  std::map<heif_item_id, std::shared_ptr<ImageItem>> m_all_images;
182
183
  // We store this in a vector because we need stable indices for the C API.
184
  // TODO: stable indices are obsolet now...
185
  std::vector<std::shared_ptr<ImageItem>> m_top_level_images;
186
187
  std::shared_ptr<ImageItem> m_primary_image; // shortcut to primary image
188
189
  std::shared_ptr<HeifFile> m_heif_file;
190
191
  int m_max_decoding_threads = 4;
192
193
  heif_security_limits m_limits;
194
195
  std::vector<std::shared_ptr<RegionItem>> m_region_items;
196
197
  Error interpret_heif_file();
198
199
  void remove_top_level_image(const std::shared_ptr<ImageItem>& image);
200
};
201
202
#endif