Coverage Report

Created: 2026-01-20 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/region.h
Line
Count
Source
1
/*
2
 * HEIF codec.
3
 * Copyright (c) 2023 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_REGION_H
22
#define LIBHEIF_REGION_H
23
24
#include <cstdint>
25
#include <vector>
26
#include <memory>
27
#include "pixelimage.h"
28
#include "libheif/heif_regions.h"
29
30
31
class RegionGeometry;
32
33
class RegionItem
34
{
35
public:
36
1.13k
  RegionItem() = default;
37
38
  RegionItem(heif_item_id itemId, uint32_t ref_width, uint32_t ref_height)
39
0
      : item_id(itemId), reference_width(ref_width), reference_height(ref_height) {}
40
41
  Error parse(const std::vector<uint8_t>& data, const heif_security_limits*);
42
43
  Error encode(std::vector<uint8_t>& result) const;
44
45
1.53k
  int get_number_of_regions() { return (int) mRegions.size(); }
46
47
1.53k
  std::vector<std::shared_ptr<RegionGeometry>> get_regions() { return mRegions; }
48
49
  void add_region(const std::shared_ptr<RegionGeometry>& region)
50
0
  {
51
0
    mRegions.push_back(region);
52
0
  }
53
54
  heif_item_id item_id = 0;
55
  uint32_t reference_width = 0;
56
  uint32_t reference_height = 0;
57
58
private:
59
  std::vector<std::shared_ptr<RegionGeometry>> mRegions;
60
};
61
62
63
class RegionGeometry
64
{
65
public:
66
13.3k
  virtual ~RegionGeometry() = default;
67
68
  virtual heif_region_type getRegionType() = 0;
69
70
  virtual Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset,
71
                      const heif_security_limits* limits) = 0;
72
73
0
  virtual bool encode_needs_32bit() const { return false; }
74
75
0
  virtual void encode(StreamWriter&, int field_size_bytes) const {}
76
77
protected:
78
  uint32_t parse_unsigned(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset);
79
80
  int32_t parse_signed(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset);
81
};
82
83
class RegionGeometry_Point : public RegionGeometry
84
{
85
public:
86
  Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset,
87
              const heif_security_limits* limits) override;
88
89
  bool encode_needs_32bit() const override;
90
91
  void encode(StreamWriter&, int field_size_bytes) const override;
92
93
1.02k
  heif_region_type getRegionType() override { return heif_region_type_point; }
94
95
  int32_t x, y;
96
};
97
98
class RegionGeometry_Rectangle : public RegionGeometry
99
{
100
public:
101
  Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset,
102
              const heif_security_limits* limits) override;
103
104
  bool encode_needs_32bit() const override;
105
106
  void encode(StreamWriter&, int field_size_bytes) const override;
107
108
165
  heif_region_type getRegionType() override { return heif_region_type_rectangle; }
109
110
  int32_t x, y;
111
  uint32_t width, height;
112
};
113
114
class RegionGeometry_Ellipse : public RegionGeometry
115
{
116
public:
117
  Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset,
118
              const heif_security_limits* limits) override;
119
120
  bool encode_needs_32bit() const override;
121
122
  void encode(StreamWriter&, int field_size_bytes) const override;
123
124
200
  heif_region_type getRegionType() override { return heif_region_type_ellipse; }
125
126
  int32_t x, y;
127
  uint32_t radius_x, radius_y;
128
};
129
130
class RegionGeometry_Polygon : public RegionGeometry
131
{
132
public:
133
  Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset,
134
              const heif_security_limits* limits) override;
135
136
  bool encode_needs_32bit() const override;
137
138
  void encode(StreamWriter&, int field_size_bytes) const override;
139
140
  heif_region_type getRegionType() override
141
17
  {
142
17
    return closed ? heif_region_type_polygon : heif_region_type_polyline;
143
17
  }
144
145
  struct Point
146
  {
147
    int32_t x, y;
148
  };
149
150
  bool closed = true;
151
  std::vector<Point> points;
152
  MemoryHandle m_memory_handle;
153
};
154
155
class RegionGeometry_ReferencedMask : public RegionGeometry
156
{
157
public:
158
  Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset,
159
              const heif_security_limits* limits) override;
160
161
  void encode(StreamWriter&, int field_size_bytes) const override;
162
163
63
  heif_region_type getRegionType() override { return heif_region_type_referenced_mask; }
164
165
  int32_t x,y;
166
  uint32_t width, height;
167
  heif_item_id referenced_item;
168
};
169
170
class RegionGeometry_InlineMask : public RegionGeometry
171
{
172
public:
173
  Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset,
174
              const heif_security_limits* limits) override;
175
176
  void encode(StreamWriter&, int field_size_bytes) const override;
177
178
  int32_t x,y;
179
  uint32_t width, height;
180
  std::vector<uint8_t> mask_data;
181
  MemoryHandle m_memory_handle;
182
183
0
  heif_region_type getRegionType() override { return heif_region_type_inline_mask; }
184
};
185
186
class HeifFile;
187
188
class RegionCoordinateTransform
189
{
190
public:
191
  static RegionCoordinateTransform create(std::shared_ptr<HeifFile> file,
192
                                          heif_item_id item_id,
193
                                          int reference_width, int reference_height);
194
195
  struct Point
196
  {
197
    double x, y;
198
  };
199
200
  struct Extent
201
  {
202
    double x, y;
203
  };
204
205
  Point transform_point(Point);
206
207
  Extent transform_extent(Extent);
208
209
private:
210
  double a = 1.0, b = 0.0, c = 0.0, d = 1.0, tx = 0.0, ty = 0.0;
211
};
212
213
#endif //LIBHEIF_REGION_H