Coverage Report

Created: 2026-06-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/image-items/image_item.h
Line
Count
Source
1
/*
2
 * HEIF image base 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_H
22
#define LIBHEIF_IMAGEITEM_H
23
24
#include "api/libheif/heif.h"
25
#include "error.h"
26
#include "nclx.h"
27
#include <string>
28
#include <vector>
29
#include <memory>
30
#include <mutex>
31
#include <utility>
32
#include <set>
33
34
#include "image/pixelimage.h"
35
#include "api/libheif/heif_plugin.h"
36
#include "codecs/encoder.h"
37
38
39
class HeifContext;
40
41
class HeifPixelImage;
42
43
44
class ImageMetadata
45
{
46
public:
47
  heif_item_id item_id;
48
  std::string item_type;  // e.g. "Exif"
49
  std::string content_type;
50
  std::string item_uri_type;
51
  std::vector<uint8_t> m_data;
52
};
53
54
55
class ImageItem : public ImageDescription,
56
                  public ErrorBuffer
57
{
58
public:
59
  ImageItem(HeifContext* ctx);
60
61
  ImageItem(HeifContext* ctx, heif_item_id id);
62
63
  static std::shared_ptr<ImageItem> alloc_for_infe_box(HeifContext*, const std::shared_ptr<Box_infe>&);
64
65
  static std::shared_ptr<ImageItem> alloc_for_compression_format(HeifContext*, heif_compression_format);
66
67
  static heif_compression_format compression_format_from_fourcc_infe_type(uint32_t type);
68
69
  static uint32_t compression_format_to_fourcc_infe_type(heif_compression_format);
70
71
0
  virtual uint32_t get_infe_type() const { return 0; }
72
73
0
  virtual const char* get_auxC_alpha_channel_type() const { return "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha"; }
74
75
0
  virtual bool is_ispe_essential() const { return false; }
76
77
  bool is_property_essential(const std::shared_ptr<Box>& property) const;
78
79
391k
  virtual Error get_item_error() const { return m_item_error; }
80
81
  // Mark this item as undecodable. The file still loads and other items remain
82
  // usable, but decoding this item (or listing it as a non-error image) will
83
  // surface this error.
84
0
  void set_item_error(const Error& err) { m_item_error = err; }
85
86
0
  virtual heif_compression_format get_compression_format() const { return heif_compression_undefined; }
87
88
0
  virtual Result<std::vector<uint8_t>> read_bitstream_configuration_data() const { return std::vector<uint8_t>{}; }
89
90
  void clear()
91
95.3k
  {
92
95.3k
    m_thumbnails.clear();
93
95.3k
    m_alpha_channel.reset();
94
95.3k
    m_depth_channel.reset();
95
95.3k
    m_aux_images.clear();
96
95.3k
  }
97
98
84.4k
  HeifContext* get_context() { return m_heif_context; }
99
100
170k
  const HeifContext* get_context() const { return m_heif_context; }
101
102
  std::shared_ptr<class HeifFile> get_file() const;
103
104
100k
  void set_properties(std::vector<std::shared_ptr<Box>> properties) {
105
100k
    m_properties = std::move(properties);
106
    // Codec-config-independent populate (e.g. unci, which reads cmpd/uncC
107
    // directly off the just-set property boxes). Visual codecs leave
108
    // m_components empty here; their populate runs after initialize_decoder()
109
    // because they need codec config (colorspace, bit depth) which only the
110
    // decoder can read.
111
100k
    populate_component_descriptions();
112
100k
  }
113
114
  // Populate the inherited ImageDescription::m_components from the just-set
115
  // property boxes / codec config. The unci subclass overrides this and
116
  // reads cmpd/uncC directly. The base implementation handles visual codecs
117
  // (HEVC/AVC/AVIF/JPEG/JPEG2000/VVC) by querying get_coded_image_colorspace()
118
  // + get_luma_bits_per_pixel() + get_chroma_bits_per_pixel() and emitting
119
  // Y/Cb/Cr or R/G/B / monochrome descriptions in canonical order.
120
  // Idempotent: skips work if m_components is already populated.
121
  // Alpha-from-aux (separate alpha item linked via auxl) is *not* emitted
122
  // here; the alpha plane is attached at decode time via
123
  // transfer_channel_from_image_as, which mints its own component on the
124
  // destination.
125
  virtual void populate_component_descriptions();
126
127
  // Populate this item's m_components by cloning descriptions from a child
128
  // item (e.g. tili's tile item, grid/iden/iovl's first child) and rescaling
129
  // per-component dims from the child's logical size to this item's full
130
  // image (ispe) size. Returns true on success. Returns false (and leaves
131
  // m_components untouched) if the child has no descriptions or sizes are
132
  // unusable, so the caller can fall back to the base populate.
133
  bool populate_descriptions_from_child(const class ImageItem& child,
134
                                        uint32_t child_w, uint32_t child_h);
135
136
  template<class BoxType>
137
  std::shared_ptr<BoxType> get_property() const
138
731k
  {
139
2.12M
    for (auto& property : m_properties) {
140
2.12M
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
308k
        return box;
142
308k
      }
143
2.12M
    }
144
145
422k
    return nullptr;
146
731k
  }
std::__1::shared_ptr<Box_clli> ImageItem::get_property<Box_clli>() const
Line
Count
Source
138
21.9k
  {
139
81.5k
    for (auto& property : m_properties) {
140
81.5k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
1
        return box;
142
1
      }
143
81.5k
    }
144
145
21.9k
    return nullptr;
146
21.9k
  }
std::__1::shared_ptr<Box_mdcv> ImageItem::get_property<Box_mdcv>() const
Line
Count
Source
138
21.9k
  {
139
81.5k
    for (auto& property : m_properties) {
140
81.5k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
0
        return box;
142
0
      }
143
81.5k
    }
144
145
21.9k
    return nullptr;
146
21.9k
  }
std::__1::shared_ptr<Box_amve> ImageItem::get_property<Box_amve>() const
Line
Count
Source
138
21.9k
  {
139
81.5k
    for (auto& property : m_properties) {
140
81.5k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
0
        return box;
142
0
      }
143
81.5k
    }
144
145
21.9k
    return nullptr;
146
21.9k
  }
std::__1::shared_ptr<Box_ndwt> ImageItem::get_property<Box_ndwt>() const
Line
Count
Source
138
21.9k
  {
139
81.5k
    for (auto& property : m_properties) {
140
81.5k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
0
        return box;
142
0
      }
143
81.5k
    }
144
145
21.9k
    return nullptr;
146
21.9k
  }
Unexecuted instantiation: std::__1::shared_ptr<Box_taic> ImageItem::get_property<Box_taic>() const
std::__1::shared_ptr<Box_itai> ImageItem::get_property<Box_itai>() const
Line
Count
Source
138
21.9k
  {
139
81.5k
    for (auto& property : m_properties) {
140
81.5k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
0
        return box;
142
0
      }
143
81.5k
    }
144
145
21.9k
    return nullptr;
146
21.9k
  }
std::__1::shared_ptr<Box_cmpd> ImageItem::get_property<Box_cmpd>() const
Line
Count
Source
138
2.86k
  {
139
5.26k
    for (auto& property : m_properties) {
140
5.26k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
2.80k
        return box;
142
2.80k
      }
143
5.26k
    }
144
145
60
    return nullptr;
146
2.86k
  }
std::__1::shared_ptr<Box_uncC> ImageItem::get_property<Box_uncC>() const
Line
Count
Source
138
2.92k
  {
139
7.90k
    for (auto& property : m_properties) {
140
7.90k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
2.81k
        return box;
142
2.81k
      }
143
7.90k
    }
144
145
108
    return nullptr;
146
2.92k
  }
std::__1::shared_ptr<Box_pasp> ImageItem::get_property<Box_pasp>() const
Line
Count
Source
138
21.9k
  {
139
80.6k
    for (auto& property : m_properties) {
140
80.6k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
261
        return box;
142
261
      }
143
80.6k
    }
144
145
21.6k
    return nullptr;
146
21.9k
  }
Unexecuted instantiation: std::__1::shared_ptr<Box_gimi_component_content_ids> ImageItem::get_property<Box_gimi_component_content_ids>() const
std::__1::shared_ptr<Box_ispe> ImageItem::get_property<Box_ispe>() const
Line
Count
Source
138
298k
  {
139
624k
    for (auto& property : m_properties) {
140
624k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
223k
        return box;
142
223k
      }
143
624k
    }
144
145
74.3k
    return nullptr;
146
298k
  }
std::__1::shared_ptr<Box_gimi_content_id> ImageItem::get_property<Box_gimi_content_id>() const
Line
Count
Source
138
103k
  {
139
389k
    for (auto& property : m_properties) {
140
389k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
0
        return box;
142
0
      }
143
389k
    }
144
145
103k
    return nullptr;
146
103k
  }
std::__1::shared_ptr<Box_prfr> ImageItem::get_property<Box_prfr>() const
Line
Count
Source
138
103k
  {
139
389k
    for (auto& property : m_properties) {
140
389k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
0
        return box;
142
0
      }
143
389k
    }
144
145
103k
    return nullptr;
146
103k
  }
Unexecuted instantiation: std::__1::shared_ptr<Box_pixi> ImageItem::get_property<Box_pixi>() const
Unexecuted instantiation: std::__1::shared_ptr<Box_tilC> ImageItem::get_property<Box_tilC>() const
std::__1::shared_ptr<Box_cpat> ImageItem::get_property<Box_cpat>() const
Line
Count
Source
138
1.86k
  {
139
7.99k
    for (auto& property : m_properties) {
140
7.99k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
0
        return box;
142
0
      }
143
7.99k
    }
144
145
1.86k
    return nullptr;
146
1.86k
  }
std::__1::shared_ptr<Box_cmpC> ImageItem::get_property<Box_cmpC>() const
Line
Count
Source
138
921
  {
139
3.57k
    for (auto& property : m_properties) {
140
3.57k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
650
        return box;
142
650
      }
143
3.57k
    }
144
145
271
    return nullptr;
146
921
  }
std::__1::shared_ptr<Box_icef> ImageItem::get_property<Box_icef>() const
Line
Count
Source
138
921
  {
139
4.00k
    for (auto& property : m_properties) {
140
4.00k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
0
        return box;
142
0
      }
143
4.00k
    }
144
145
921
    return nullptr;
146
921
  }
std::__1::shared_ptr<Box_cloc> ImageItem::get_property<Box_cloc>() const
Line
Count
Source
138
921
  {
139
4.00k
    for (auto& property : m_properties) {
140
4.00k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
0
        return box;
142
0
      }
143
4.00k
    }
144
145
921
    return nullptr;
146
921
  }
std::__1::shared_ptr<Box_auxC> ImageItem::get_property<Box_auxC>() const
Line
Count
Source
138
1.06k
  {
139
3.78k
    for (auto& property : m_properties) {
140
3.78k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
981
        return box;
142
981
      }
143
3.78k
    }
144
145
79
    return nullptr;
146
1.06k
  }
std::__1::shared_ptr<Box_hvcC> ImageItem::get_property<Box_hvcC>() const
Line
Count
Source
138
33.9k
  {
139
59.1k
    for (auto& property : m_properties) {
140
59.1k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
31.8k
        return box;
142
31.8k
      }
143
59.1k
    }
144
145
2.12k
    return nullptr;
146
33.9k
  }
std::__1::shared_ptr<Box_av1C> ImageItem::get_property<Box_av1C>() const
Line
Count
Source
138
44.8k
  {
139
128k
    for (auto& property : m_properties) {
140
128k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
42.9k
        return box;
142
42.9k
      }
143
128k
    }
144
145
1.87k
    return nullptr;
146
44.8k
  }
std::__1::shared_ptr<Box_avcC> ImageItem::get_property<Box_avcC>() const
Line
Count
Source
138
846
  {
139
967
    for (auto& property : m_properties) {
140
967
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
788
        return box;
142
788
      }
143
967
    }
144
145
58
    return nullptr;
146
846
  }
std::__1::shared_ptr<Box_j2kH> ImageItem::get_property<Box_j2kH>() const
Line
Count
Source
138
1.14k
  {
139
1.29k
    for (auto& property : m_properties) {
140
1.29k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
1.05k
        return box;
142
1.05k
      }
143
1.29k
    }
144
145
90
    return nullptr;
146
1.14k
  }
std::__1::shared_ptr<Box_vvcC> ImageItem::get_property<Box_vvcC>() const
Line
Count
Source
138
1.01k
  {
139
1.12k
    for (auto& property : m_properties) {
140
1.12k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
952
        return box;
142
952
      }
143
1.12k
    }
144
145
65
    return nullptr;
146
1.01k
  }
std::__1::shared_ptr<Box_jpgC> ImageItem::get_property<Box_jpgC>() const
Line
Count
Source
138
1.79k
  {
139
6.47k
    for (auto& property : m_properties) {
140
6.47k
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
0
        return box;
142
0
      }
143
6.47k
    }
144
145
1.79k
    return nullptr;
146
1.79k
  }
std::__1::shared_ptr<Box_mskC> ImageItem::get_property<Box_mskC>() const
Line
Count
Source
138
40
  {
139
284
    for (auto& property : m_properties) {
140
284
      if (auto box = std::dynamic_pointer_cast<BoxType>(property)) {
141
12
        return box;
142
12
      }
143
284
    }
144
145
28
    return nullptr;
146
40
  }
147
148
  template<class BoxType>
149
  std::vector<std::shared_ptr<const BoxType>> get_all_properties() const
150
2.76k
  {
151
2.76k
    std::vector<std::shared_ptr<const BoxType>> result;
152
12.0k
    for (auto& property : m_properties) {
153
12.0k
      if (auto box = std::dynamic_pointer_cast<const BoxType>(property)) {
154
0
        result.push_back(box);
155
0
      }
156
12.0k
    }
157
2.76k
    return result;
158
2.76k
  }
std::__1::vector<std::__1::shared_ptr<Box_splz const>, std::__1::allocator<std::__1::shared_ptr<Box_splz const> > > ImageItem::get_all_properties<Box_splz>() const
Line
Count
Source
150
921
  {
151
921
    std::vector<std::shared_ptr<const BoxType>> result;
152
4.00k
    for (auto& property : m_properties) {
153
4.00k
      if (auto box = std::dynamic_pointer_cast<const BoxType>(property)) {
154
0
        result.push_back(box);
155
0
      }
156
4.00k
    }
157
921
    return result;
158
921
  }
std::__1::vector<std::__1::shared_ptr<Box_sbpm const>, std::__1::allocator<std::__1::shared_ptr<Box_sbpm const> > > ImageItem::get_all_properties<Box_sbpm>() const
Line
Count
Source
150
921
  {
151
921
    std::vector<std::shared_ptr<const BoxType>> result;
152
4.00k
    for (auto& property : m_properties) {
153
4.00k
      if (auto box = std::dynamic_pointer_cast<const BoxType>(property)) {
154
0
        result.push_back(box);
155
0
      }
156
4.00k
    }
157
921
    return result;
158
921
  }
std::__1::vector<std::__1::shared_ptr<Box_snuc const>, std::__1::allocator<std::__1::shared_ptr<Box_snuc const> > > ImageItem::get_all_properties<Box_snuc>() const
Line
Count
Source
150
921
  {
151
921
    std::vector<std::shared_ptr<const BoxType>> result;
152
4.00k
    for (auto& property : m_properties) {
153
4.00k
      if (auto box = std::dynamic_pointer_cast<const BoxType>(property)) {
154
0
        result.push_back(box);
155
0
      }
156
4.00k
    }
157
921
    return result;
158
921
  }
159
160
  heif_property_id add_property(std::shared_ptr<Box> property, bool essential);
161
162
  heif_property_id add_property_without_deduplication(std::shared_ptr<Box> property, bool essential);
163
164
  void set_resolution(uint32_t w, uint32_t h)
165
66.4k
  {
166
66.4k
    m_width = w;
167
66.4k
    m_height = h;
168
66.4k
  }
169
170
527k
  heif_item_id get_id() const { return m_id; }
171
172
0
  void set_id(heif_item_id id) { m_id = id; }
173
174
58.8k
  void set_primary(bool flag = true) { m_is_primary = flag; }
175
176
0
  bool is_primary() const { return m_is_primary; }
177
178
  // 32bit limitation from `ispe`
179
123k
  uint32_t get_width() const { return m_width; }
180
181
123k
  uint32_t get_height() const { return m_height; }
182
183
  bool has_ispe_resolution() const;
184
185
  uint32_t get_ispe_width() const;
186
187
  uint32_t get_ispe_height() const;
188
189
  // Default behavior: forward call to Decoder
190
  [[nodiscard]] virtual int get_luma_bits_per_pixel() const;
191
192
  // Default behavior: forward call to Decoder
193
  [[nodiscard]] virtual int get_chroma_bits_per_pixel() const;
194
195
  void set_size(uint32_t w, uint32_t h)
196
0
  {
197
0
    m_width = w;
198
0
    m_height = h;
199
0
  }
200
201
  virtual void get_tile_size(uint32_t& w, uint32_t& h) const;
202
203
  virtual Error get_coded_image_colorspace(heif_colorspace* out_colorspace, heif_chroma* out_chroma) const;
204
205
  Error postprocess_coded_image_colorspace(heif_colorspace* inout_colorspace, heif_chroma* inout_chroma) const;
206
207
0
  virtual Error process_before_write() { return {}; }
208
209
  // -- thumbnails
210
211
  void set_is_thumbnail()
212
2.39k
  {
213
2.39k
    m_is_thumbnail = true;
214
2.39k
  }
215
216
2.37k
  void add_thumbnail(const std::shared_ptr<ImageItem>& img) { m_thumbnails.push_back(img); }
217
218
2.37k
  bool is_thumbnail() const { return m_is_thumbnail; }
219
220
0
  const std::vector<std::shared_ptr<ImageItem>>& get_thumbnails() const { return m_thumbnails; }
221
222
223
  // --- alpha channel
224
225
  void set_is_alpha_channel()
226
426
  {
227
426
    m_is_alpha_channel = true;
228
426
  }
229
230
  // Attach an alpha aux ImageItem and append a corresponding Alpha
231
  // ComponentDescription to this item's m_components. Idempotent.
232
  void set_alpha_channel(std::shared_ptr<ImageItem> img);
233
234
0
  bool is_alpha_channel() const { return m_is_alpha_channel; }
235
236
85.2k
  const std::shared_ptr<ImageItem>& get_alpha_channel() const { return m_alpha_channel; }
237
238
70
  void set_is_premultiplied_alpha(bool flag) { m_premultiplied_alpha = flag; }
239
240
48
  bool is_premultiplied_alpha() const { return m_premultiplied_alpha; }
241
242
  // Whether the image has an alpha channel coded in the main image (not as an auxiliary image)
243
56.1k
  virtual bool has_coded_alpha_channel() const { return false; }
244
245
  // --- depth channel
246
247
  void set_is_depth_channel()
248
95
  {
249
95
    m_is_depth_channel = true;
250
95
  }
251
252
57
  void set_depth_channel(std::shared_ptr<ImageItem> img) { m_depth_channel = std::move(img); }
253
254
0
  bool is_depth_channel() const { return m_is_depth_channel; }
255
256
0
  const std::shared_ptr<ImageItem>& get_depth_channel() const { return m_depth_channel; }
257
258
259
  void set_depth_representation_info(heif_depth_representation_info& info)
260
0
  {
261
0
    m_has_depth_representation_info = true;
262
0
    m_depth_representation_info = info;
263
0
  }
264
265
  bool has_depth_representation_info() const
266
0
  {
267
0
    return m_has_depth_representation_info;
268
0
  }
269
270
  const heif_depth_representation_info& get_depth_representation_info() const
271
0
  {
272
0
    return m_depth_representation_info;
273
0
  }
274
275
276
  // --- generic aux image
277
278
  void set_is_aux_image(const std::string& aux_type)
279
887
  {
280
887
    m_is_aux_image = true;
281
887
    m_aux_image_type = aux_type;
282
887
  }
283
284
753
  void add_aux_image(std::shared_ptr<ImageItem> img) { m_aux_images.push_back(std::move(img)); }
285
286
0
  bool is_aux_image() const { return m_is_aux_image; }
287
288
0
  const std::string& get_aux_type() const { return m_aux_image_type; }
289
290
  std::vector<std::shared_ptr<ImageItem>> get_aux_images(int aux_image_filter = 0) const
291
0
  {
292
0
    if (aux_image_filter == 0) {
293
0
      return m_aux_images;
294
0
    }
295
0
    else {
296
0
      std::vector<std::shared_ptr<ImageItem>> auxImgs;
297
0
      for (const auto& aux : m_aux_images) {
298
0
        if ((aux_image_filter & LIBHEIF_AUX_IMAGE_FILTER_OMIT_ALPHA) && aux->is_alpha_channel()) {
299
0
          continue;
300
0
        }
301
0
302
0
        if ((aux_image_filter & LIBHEIF_AUX_IMAGE_FILTER_OMIT_DEPTH) &&
303
0
            aux->is_depth_channel()) {
304
0
          continue;
305
0
        }
306
0
307
0
        auxImgs.push_back(aux);
308
0
      }
309
0
310
0
      return auxImgs;
311
0
    }
312
0
  }
313
314
315
  // --- metadata
316
317
  void add_metadata(std::shared_ptr<ImageMetadata> metadata)
318
6.57k
  {
319
6.57k
    m_metadata.push_back(std::move(metadata));
320
6.57k
  }
321
322
81.2k
  const std::vector<std::shared_ptr<ImageMetadata>>& get_metadata() const { return m_metadata; }
323
324
325
326
  // --- ImageDescription
327
328
  void set_clli(const heif_content_light_level& clli) override;
329
330
  void set_mdcv(const heif_mastering_display_colour_volume& mdcv) override;
331
332
  void set_amve(const heif_ambient_viewing_environment& amve) override;
333
334
  void set_nominal_diffuse_white_luminance(uint32_t luminance) override;
335
336
  void set_pixel_ratio(uint32_t h, uint32_t v) override;
337
338
  void set_color_profile_nclx(const nclx_profile& profile) override;
339
340
  void set_color_profile_icc(const std::shared_ptr<const color_profile_raw>& profile) override;
341
342
  void set_omaf_image_projection(heif_omaf_image_projection image_projection) override;
343
344
  // --- miaf
345
346
  // TODO: we should have a function that checks all MIAF constraints and sets the compatibility flag.
347
0
  void mark_not_miaf_compatible() { m_miaf_compatible = false; }
348
349
0
  bool is_miaf_compatible() const { return m_miaf_compatible; }
350
351
  // return 0 if we don't know the brand
352
0
  virtual heif_brand2 get_compatible_brand() const { return 0; }
353
354
  // === decoding ===
355
356
7.56k
  virtual Error initialize_decoder() { return Error::Ok; }
357
358
9.70k
  virtual void set_decoder_input_data() { }
359
360
  virtual Result<std::shared_ptr<HeifPixelImage>> decode_image(const heif_decoding_options& options,
361
                                                               bool decode_tile_only, uint32_t tile_x0,
362
                                                               uint32_t tile_y0,
363
                                                               std::set<heif_item_id> processed_ids) const;
364
365
  virtual Result<std::shared_ptr<HeifPixelImage>> decode_compressed_image(const heif_decoding_options& options,
366
                                                                          bool decode_tile_only, uint32_t tile_x0,
367
                                                                          uint32_t tile_y0,
368
                                                                          std::set<heif_item_id> processed_ids) const;
369
370
  // Validate the just-decoded pixel image against the size signaled for this item.
371
  // Called by decode_image() right after decode_compressed_image(), BEFORE transforms,
372
  // so the reference is the pre-transform coded size (ispe), or the signaled tile size
373
  // for a tile decode -- NOT get_width()/get_height() (which are post-transform).
374
  // Default impl handles plain coded codecs (HEVC/AVC/VVC/AVIF/JPEG). Subclasses
375
  // override where the size source or component layout differs.
376
  virtual Error check_decoded_image_size(const HeifPixelImage& img,
377
                                         bool decode_tile_only,
378
                                         uint32_t tile_x0, uint32_t tile_y0) const;
379
380
  Result<std::vector<std::shared_ptr<Box>>> get_properties() const;
381
382
  bool has_essential_property_other_than(const std::set<uint32_t>&) const;
383
384
  // === encoding ===
385
386
  Result<Encoder::CodedImageData> encode_to_bitstream_and_boxes(const std::shared_ptr<HeifPixelImage>& image,
387
                                                                heif_encoder* encoder,
388
                                                                const heif_encoding_options& options,
389
                                                                heif_image_input_class input_class);
390
391
  // Entry point for encoding pixel images.
392
  Error encode_to_item(HeifContext* ctx,
393
                       const std::shared_ptr<HeifPixelImage>& image,
394
                       heif_encoder* encoder,
395
                       const heif_encoding_options& options,
396
                       heif_image_input_class input_class);
397
398
18
  void set_intrinsic_matrix(const Box_cmin::RelativeIntrinsicMatrix& cmin) {
399
18
    m_has_intrinsic_matrix = true;
400
18
    m_intrinsic_matrix = cmin.to_absolute(get_ispe_width(), get_ispe_height());
401
18
  }
402
403
1.81k
  bool has_intrinsic_matrix() const { return m_has_intrinsic_matrix; }
404
405
821
  Box_cmin::AbsoluteIntrinsicMatrix& get_intrinsic_matrix() { return m_intrinsic_matrix; }
406
407
0
  const Box_cmin::AbsoluteIntrinsicMatrix& get_intrinsic_matrix() const { return m_intrinsic_matrix; }
408
409
410
4
  void set_extrinsic_matrix(const Box_cmex::ExtrinsicMatrix& cmex) {
411
4
    m_has_extrinsic_matrix = true;
412
4
    m_extrinsic_matrix = cmex;
413
4
  }
414
415
0
  bool has_extrinsic_matrix() const { return m_has_extrinsic_matrix; }
416
417
0
  Box_cmex::ExtrinsicMatrix& get_extrinsic_matrix() { return m_extrinsic_matrix; }
418
419
0
  const Box_cmex::ExtrinsicMatrix& get_extrinsic_matrix() const { return m_extrinsic_matrix; }
420
421
422
127
  void add_region_item_id(heif_item_id id) { m_region_item_ids.push_back(id); }
423
424
0
  const std::vector<heif_item_id>& get_region_item_ids() const { return m_region_item_ids; }
425
426
427
23.6k
  void add_decoding_warning(Error err) const { m_decoding_warnings.emplace_back(std::move(err)); }
428
429
19.9k
  const std::vector<Error>& get_decoding_warnings() const { return m_decoding_warnings; }
430
431
  virtual heif_image_tiling get_heif_image_tiling() const;
432
433
  Error process_image_transformations_on_tiling(heif_image_tiling&) const;
434
435
  Error transform_requested_tile_position_to_original_tile_position(uint32_t& tile_x, uint32_t& tile_y) const;
436
437
  virtual Result<std::shared_ptr<class Decoder>> get_decoder() const
438
20.3k
  {
439
20.3k
    return Error{
440
20.3k
      heif_error_Unsupported_feature,
441
20.3k
      heif_suberror_No_matching_decoder_installed,
442
20.3k
      "No decoder for this image format"
443
20.3k
    };
444
20.3k
  }
445
446
0
  virtual std::shared_ptr<class Encoder> get_encoder() const { return nullptr; }
447
448
0
  void add_text_item_id(heif_item_id id) {
449
0
    m_text_item_ids.push_back(id);
450
0
  }
451
452
0
  const std::vector<heif_item_id>& get_text_item_ids() const { return m_text_item_ids; }
453
454
private:
455
  HeifContext* m_heif_context;
456
  std::vector<std::shared_ptr<Box>> m_properties;
457
458
  Error m_item_error;  // if set, this item cannot be decoded (e.g. unsupported required reference types)
459
460
  heif_item_id m_id = 0;
461
  uint32_t m_width = 0, m_height = 0;  // after all transformations have been applied
462
  bool m_is_primary = false;
463
464
  bool m_is_thumbnail = false;
465
466
  std::vector<std::shared_ptr<ImageItem>> m_thumbnails;
467
468
  bool m_is_alpha_channel = false;
469
  bool m_premultiplied_alpha = false;
470
  std::shared_ptr<ImageItem> m_alpha_channel;
471
472
  bool m_is_depth_channel = false;
473
  std::shared_ptr<ImageItem> m_depth_channel;
474
475
  bool m_has_depth_representation_info = false;
476
  heif_depth_representation_info m_depth_representation_info;
477
478
  bool m_is_aux_image = false;
479
  std::string m_aux_image_type;
480
  std::vector<std::shared_ptr<ImageItem>> m_aux_images;
481
482
  std::vector<std::shared_ptr<ImageMetadata>> m_metadata;
483
484
  bool m_miaf_compatible = true;
485
486
  std::vector<heif_item_id> m_region_item_ids;
487
488
  bool m_has_intrinsic_matrix = false;
489
  Box_cmin::AbsoluteIntrinsicMatrix m_intrinsic_matrix{};
490
491
  bool m_has_extrinsic_matrix = false;
492
  Box_cmex::ExtrinsicMatrix m_extrinsic_matrix{};
493
494
  mutable std::vector<Error> m_decoding_warnings;
495
496
  mutable std::mutex m_decode_mutex;
497
498
  std::vector<heif_item_id> m_text_item_ids;
499
500
  void generate_property_boxes_for_ImageDescription();
501
502
protected:
503
  // Result<std::vector<uint8_t>> read_bitstream_configuration_data_override(heif_item_id itemId, heif_compression_format format) const;
504
505
  virtual Result<Encoder::CodedImageData> encode(const std::shared_ptr<HeifPixelImage>& image,
506
                                                 heif_encoder* encoder,
507
                                                 const heif_encoding_options& options,
508
                                                 heif_image_input_class input_class);
509
510
  // --- encoding utility functions
511
512
  static std::vector<std::shared_ptr<Box_colr> >
513
  add_color_profile(const std::shared_ptr<HeifPixelImage>& image,
514
                    const heif_encoding_options& options,
515
                    heif_image_input_class input_class,
516
                    const heif_color_profile_nclx* target_heif_nclx);
517
};
518
519
520
class ImageItem_Error : public ImageItem
521
{
522
public:
523
  // dummy ImageItem class that is a placeholder for unsupported item types
524
525
  ImageItem_Error(uint32_t item_type, heif_item_id id, Error err)
526
12.7k
    : ImageItem(nullptr, id), m_item_type(item_type), m_item_error(err) {}
527
528
  uint32_t get_infe_type() const override
529
4
  {
530
4
    return m_item_type;
531
4
  }
532
533
35.2k
  Error get_item_error() const override { return m_item_error; }
534
535
  Result<std::shared_ptr<HeifPixelImage>> decode_image(const heif_decoding_options& options,
536
                                                       bool decode_tile_only, uint32_t tile_x0,
537
                                                       uint32_t tile_y0,
538
                                                       std::set<heif_item_id> processed_ids) const override
539
0
  {
540
0
    return m_item_error;
541
0
  }
542
543
  Result<std::shared_ptr<HeifPixelImage>> decode_compressed_image(const heif_decoding_options& options,
544
                                                                  bool decode_tile_only, uint32_t tile_x0,
545
                                                                  uint32_t tile_y0, std::set<heif_item_id> processed_ids) const override
546
0
  {
547
0
    return m_item_error;
548
0
  }
549
550
141
  [[nodiscard]] int get_luma_bits_per_pixel() const override { return -1; }
551
552
0
  [[nodiscard]] int get_chroma_bits_per_pixel() const override { return -1; }
553
554
private:
555
  uint32_t m_item_type;
556
  Error m_item_error;
557
};
558
559
#endif //LIBHEIF_IMAGEITEM_H