/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 | 614 | 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); |
42 | | |
43 | | Error encode(std::vector<uint8_t>& result) const; |
44 | | |
45 | 0 | int get_number_of_regions() { return (int) mRegions.size(); } |
46 | | |
47 | 0 | 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 | 9.26k | 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) = 0; |
71 | | |
72 | 0 | virtual bool encode_needs_32bit() const { return false; } |
73 | | |
74 | 0 | virtual void encode(StreamWriter&, int field_size_bytes) const {} |
75 | | |
76 | | protected: |
77 | | uint32_t parse_unsigned(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset); |
78 | | |
79 | | int32_t parse_signed(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset); |
80 | | }; |
81 | | |
82 | | class RegionGeometry_Point : public RegionGeometry |
83 | | { |
84 | | public: |
85 | | Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset) override; |
86 | | |
87 | | bool encode_needs_32bit() const override; |
88 | | |
89 | | void encode(StreamWriter&, int field_size_bytes) const override; |
90 | | |
91 | 0 | heif_region_type getRegionType() override { return heif_region_type_point; } |
92 | | |
93 | | int32_t x, y; |
94 | | }; |
95 | | |
96 | | class RegionGeometry_Rectangle : public RegionGeometry |
97 | | { |
98 | | public: |
99 | | Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset) override; |
100 | | |
101 | | bool encode_needs_32bit() const override; |
102 | | |
103 | | void encode(StreamWriter&, int field_size_bytes) const override; |
104 | | |
105 | 0 | heif_region_type getRegionType() override { return heif_region_type_rectangle; } |
106 | | |
107 | | int32_t x, y; |
108 | | uint32_t width, height; |
109 | | }; |
110 | | |
111 | | class RegionGeometry_Ellipse : public RegionGeometry |
112 | | { |
113 | | public: |
114 | | Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset) override; |
115 | | |
116 | | bool encode_needs_32bit() const override; |
117 | | |
118 | | void encode(StreamWriter&, int field_size_bytes) const override; |
119 | | |
120 | 0 | heif_region_type getRegionType() override { return heif_region_type_ellipse; } |
121 | | |
122 | | int32_t x, y; |
123 | | uint32_t radius_x, radius_y; |
124 | | }; |
125 | | |
126 | | class RegionGeometry_Polygon : public RegionGeometry |
127 | | { |
128 | | public: |
129 | | Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int* dataOffset) override; |
130 | | |
131 | | bool encode_needs_32bit() const override; |
132 | | |
133 | | void encode(StreamWriter&, int field_size_bytes) const override; |
134 | | |
135 | | heif_region_type getRegionType() override |
136 | 0 | { |
137 | 0 | return closed ? heif_region_type_polygon : heif_region_type_polyline; |
138 | 0 | } |
139 | | |
140 | | struct Point |
141 | | { |
142 | | int32_t x, y; |
143 | | }; |
144 | | |
145 | | bool closed = true; |
146 | | std::vector<Point> points; |
147 | | }; |
148 | | |
149 | | class RegionGeometry_ReferencedMask : public RegionGeometry |
150 | | { |
151 | | public: |
152 | | Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int *dataOffset) override; |
153 | | |
154 | | void encode(StreamWriter&, int field_size_bytes) const override; |
155 | | |
156 | 0 | heif_region_type getRegionType() override { return heif_region_type_referenced_mask; } |
157 | | |
158 | | int32_t x,y; |
159 | | uint32_t width, height; |
160 | | heif_item_id referenced_item; |
161 | | }; |
162 | | |
163 | | class RegionGeometry_InlineMask : public RegionGeometry |
164 | | { |
165 | | public: |
166 | | Error parse(const std::vector<uint8_t>& data, int field_size, unsigned int *dataOffset) override; |
167 | | |
168 | | void encode(StreamWriter&, int field_size_bytes) const override; |
169 | | |
170 | | int32_t x,y; |
171 | | uint32_t width, height; |
172 | | std::vector<uint8_t> mask_data; |
173 | | |
174 | 0 | heif_region_type getRegionType() override { return heif_region_type_inline_mask; } |
175 | | }; |
176 | | |
177 | | class HeifFile; |
178 | | |
179 | | class RegionCoordinateTransform |
180 | | { |
181 | | public: |
182 | | static RegionCoordinateTransform create(std::shared_ptr<HeifFile> file, |
183 | | heif_item_id item_id, |
184 | | int reference_width, int reference_height); |
185 | | |
186 | | struct Point |
187 | | { |
188 | | double x, y; |
189 | | }; |
190 | | |
191 | | struct Extent |
192 | | { |
193 | | double x, y; |
194 | | }; |
195 | | |
196 | | Point transform_point(Point); |
197 | | |
198 | | Extent transform_extent(Extent); |
199 | | |
200 | | private: |
201 | | double a = 1.0, b = 0.0, c = 0.0, d = 1.0, tx = 0.0, ty = 0.0; |
202 | | }; |
203 | | |
204 | | #endif //LIBHEIF_REGION_H |