Coverage Report

Created: 2026-01-20 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/image-items/iden.cc
Line
Count
Source
1
/*
2
 * HEIF 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
#include "iden.h"
22
#include "context.h"
23
#include "file.h"
24
25
26
ImageItem_iden::ImageItem_iden(HeifContext* ctx)
27
0
    : ImageItem(ctx)
28
0
{
29
0
}
30
31
32
ImageItem_iden::ImageItem_iden(HeifContext* ctx, heif_item_id id)
33
145
    : ImageItem(ctx, id)
34
145
{
35
145
}
36
37
38
Result<std::shared_ptr<HeifPixelImage>> ImageItem_iden::decode_compressed_image(const heif_decoding_options& options,
39
                                                                                bool decode_tile_only, uint32_t tile_x0, uint32_t tile_y0,
40
                                                                                std::set<heif_item_id> processed_ids) const
41
0
{
42
0
  if (processed_ids.contains(get_id())) {
43
0
    return Error{heif_error_Invalid_input,
44
0
                 heif_suberror_Unspecified,
45
0
                 "'iref' has cyclic references"};
46
0
  }
47
48
0
  processed_ids.insert(get_id());
49
50
51
0
  std::shared_ptr<HeifPixelImage> img;
52
53
  // find the ID of the image this image is derived from
54
55
0
  auto iref_box = get_file()->get_iref_box();
56
57
0
  if (!iref_box) {
58
0
    return Error(heif_error_Invalid_input,
59
0
                 heif_suberror_No_iref_box,
60
0
                 "No iref box available, but needed for iden image");
61
0
  }
62
63
0
  std::vector<heif_item_id> image_references = iref_box->get_references(get_id(), fourcc("dimg"));
64
65
0
  if (image_references.size() > 1) {
66
0
    return Error(heif_error_Invalid_input,
67
0
                 heif_suberror_Unspecified,
68
0
                 "'iden' image with more than one reference image");
69
0
  }
70
71
0
  if (image_references.empty()) {
72
0
    return Error(heif_error_Invalid_input,
73
0
                 heif_suberror_Unspecified,
74
0
                 "'iden' image without 'dimg' reference");
75
0
  }
76
77
78
0
  heif_item_id reference_image_id = image_references[0];
79
80
0
  if (reference_image_id == get_id()) {
81
0
    return Error(heif_error_Invalid_input,
82
0
                 heif_suberror_Unspecified,
83
0
                 "'iden' image referring to itself");
84
0
  }
85
86
0
  std::shared_ptr<const ImageItem> imgitem = get_context()->get_image(reference_image_id, true);
87
0
  if (!imgitem) {
88
0
    return Error(heif_error_Invalid_input,
89
0
                 heif_suberror_Unspecified,
90
0
                 "'iden' image references unavailable image");
91
0
  }
92
0
  if (auto error = imgitem->get_item_error()) {
93
0
    return error;
94
0
  }
95
96
0
  return imgitem->decode_image(options, decode_tile_only, tile_x0, tile_y0, processed_ids);
97
0
}
98
99
100
Error ImageItem_iden::get_coded_image_colorspace(heif_colorspace* out_colorspace, heif_chroma* out_chroma) const
101
112
{
102
112
  heif_item_id child;
103
112
  Error err = get_context()->get_id_of_non_virtual_child_image(get_id(), child);
104
112
  if (err) {
105
110
    return err;
106
110
  }
107
108
2
  auto image = get_context()->get_image(child, true);
109
2
  if (!image) {
110
0
    return Error{heif_error_Invalid_input,
111
0
                 heif_suberror_Nonexisting_item_referenced};
112
0
  }
113
114
2
  return image->get_coded_image_colorspace(out_colorspace, out_chroma);
115
2
}
116
117
118
int ImageItem_iden::get_luma_bits_per_pixel() const
119
0
{
120
0
  heif_item_id child;
121
0
  Error err = get_context()->get_id_of_non_virtual_child_image(get_id(), child);
122
0
  if (err) {
123
0
    return -1;
124
0
  }
125
126
0
  auto image = get_context()->get_image(child, true);
127
0
  if (!image) {
128
0
    return -1;
129
0
  }
130
131
0
  return image->get_luma_bits_per_pixel();
132
0
}
133
134
135
int ImageItem_iden::get_chroma_bits_per_pixel() const
136
0
{
137
0
  heif_item_id child;
138
0
  Error err = get_context()->get_id_of_non_virtual_child_image(get_id(), child);
139
0
  if (err) {
140
0
    return -1;
141
0
  }
142
143
0
  auto image = get_context()->get_image(child, true);
144
0
  if (!image) {
145
0
    return -1;
146
0
  }
147
148
0
  return image->get_chroma_bits_per_pixel();
149
0
}
150
151
heif_brand2 ImageItem_iden::get_compatible_brand() const
152
0
{
153
0
  assert(false);
154
0
  return 0; // TODO (we never write 'iden' images)
155
0
}