CVB++ 15.0
decl_plane.hpp
1#pragma once
2
3#include <memory>
4
5#include "../data_type.hpp"
6#include "../global.hpp"
7#include "../iarray.hpp"
8
9namespace Cvb
10{
11
12 CVB_BEGIN_INLINE_NS
13
14 template <>
15 inline HandleGuard<Plane>::HandleGuard(void *handle) noexcept
16 : HandleGuard<Plane>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
17 {
18 }
19
21
24 class Plane : public IArray
25 {
26 protected:
27 struct PrivateTag
28 {
29 };
30
31 public:
32 using GuardType = HandleGuard<Plane>;
33
34 Plane(HandleGuard<Plane> &&guard, PrivateTag) noexcept
35 : handle_(std::move(guard))
36 , shared_(0)
37 {
38 }
39
40 Plane(const Plane &other) = delete;
41 Plane &operator=(const Plane &other) = delete;
42 Plane(Plane &&other) = delete;
43 Plane &operator=(Plane &&other) = delete;
44 virtual ~Plane() = default;
45
47
55 explicit Plane(const ImagePlane &imagePlane, PlaneRole role = PlaneRole::Undefined);
56
58
68 {
69 return std::make_shared<Plane>(imagePlane, role);
70 }
71
73
76 static PlanePtr FromHandle(HandleGuard<Plane> &&guard)
77 {
78 if (!guard.Handle())
79 throw std::runtime_error("handle must not be null");
80
81 return std::make_shared<Plane>(std::move(guard), PrivateTag{});
82 }
83
84 template <class T>
85 static PlanePtr FromHandle(HandleGuard<Plane> &&guard)
86 {
87 static_assert(std::is_base_of<Plane, T>::value, "CVB: Type must be derived from Plane");
88 return FromHandle(std::move(guard));
89 }
90
92
98 void *Handle() const noexcept override
99 {
100 return handle_.Handle();
101 }
102
104
111 int Rank() const noexcept override
112 {
113 CExports::cvbdim_t rank = 0;
114 CVB_CALL_CAPI(CVCPlaneGetRank(Handle(), rank));
115 return static_cast<int>(rank);
116 }
117
119
123 PlaneRole Role() const noexcept
124 {
125 CExports::CVCPlaneRole role = CExports::CVCPR_Undefined;
126 CVB_CALL_CAPI(CVCPlaneGetRole(Handle(), role));
127 return static_cast<PlaneRole>(role);
128 }
129
131
136 class DataType DataType() const noexcept override
137 {
138 CExports::cvbdatatype_t dataType = 0;
139 CVB_CALL_CAPI(CVCPlaneGetDataType(Handle(), dataType));
140 return Cvb::DataType::FromNativeDescriptor(static_cast<int>(dataType));
141 }
142
144
149 std::intptr_t Increment(int dimension) const
150 {
151 return NativeCall<std::intptr_t>([&](std::intptr_t &value) {
152 return CVB_CALL_CAPI(CVCPlaneGetIncrement(Handle(), static_cast<CExports::cvbdim_t>(dimension), value));
153 });
154 }
155
157
162 int Length(int dimension) const
163 {
164 return static_cast<int>(NativeCall<CExports::cvbint64_t>([&](CExports::cvbint64_t &value) {
165 return CVB_CALL_CAPI(CVCPlaneGetLength(Handle(), static_cast<CExports::cvbdim_t>(dimension), value));
166 }));
167 }
168
170
175 {
176 return reinterpret_cast<std::uint8_t *>(
177 NativeCall<void *>([&](void *&value) { return CVB_CALL_CAPI(CVCPlaneGetBasePtr(Handle(), value)); }));
178 }
179
180 private:
181 template <class T>
182 T NativeCall(std::function<CExports::cvbres_t(T &value)> fn) const
183 {
184 T value;
185 auto result = fn(value);
186 if (result < 0)
187 Utilities::SystemInfo::ThrowLastError(result);
188 return value;
189 }
190
191 HandleGuard<Plane> handle_;
192 ReleaseObjectGuard shared_;
193 };
194
195 template <>
196 struct PlaneTraits<Plane>
197 {
198 using PlaneT = Plane;
199 using TypeList = DispatchableTypeList<std::uint8_t, std::int8_t, std::uint16_t, std::int16_t, std::uint32_t,
200 std::int32_t, std::uint64_t, std::int64_t, float, double>;
201
202 static constexpr bool HasVpat = false;
203
204 static int GetWidth(const Plane &plane)
205 {
206 return plane.Length(0);
207 }
208
209 // Throws if GetRank < 2.
210 static int GetHeight(const Plane &plane)
211 {
212 return plane.Length(1);
213 }
214
215 static DataType GetDataType(const Plane &plane)
216 {
217 return plane.DataType();
218 }
219
220 static int GetRank(const Plane &plane)
221 {
222 return plane.Rank();
223 }
224
225 static std::uint8_t *GetBasePtr(const Plane &plane)
226 {
227 return plane.BasePtr();
228 }
229
230 static std::ptrdiff_t GetXInc(const Plane &plane)
231 {
232 return plane.Increment(0);
233 }
234
235 // Throws if GetRank < 2.
236 static std::ptrdiff_t GetYInc(const Plane &plane)
237 {
238 return plane.Increment(1);
239 }
240 };
241
242 CVB_END_INLINE_NS
243
244} // namespace Cvb
static DataType FromNativeDescriptor(int dataTypeDescriptor) noexcept
Construct a data type descriptor from one of the native library's descriptor values.
Definition data_type.hpp:31
Image plane information container.
Definition decl_image_plane.hpp:29
Plane information container.
Definition decl_plane.hpp:25
int Length(int dimension) const
Gets the number of elements in the given dimension.
Definition decl_plane.hpp:162
static PlanePtr FromHandle(HandleGuard< Plane > &&guard)
Creates a plane from a classic API handle.
Definition decl_plane.hpp:76
static PlanePtr FromImagePlane(const ImagePlane &imagePlane, PlaneRole role=PlaneRole::Undefined)
Creates a plane from an image plane.
Definition decl_plane.hpp:67
int Rank() const noexcept override
Gets the number of dimensions of this plane.
Definition decl_plane.hpp:111
PlaneRole Role() const noexcept
Get the meaning of this plane.
Definition decl_plane.hpp:123
void * Handle() const noexcept override
Classic API image handle.
Definition decl_plane.hpp:98
std::intptr_t Increment(int dimension) const
Gets the offset in bytes to move to the next element in the given dimension.
Definition decl_plane.hpp:149
class DataType DataType() const noexcept override
Gets the data type of the plane.
Definition decl_plane.hpp:136
std::uint8_t * BasePtr() const
Get the pointer to the first element of this plane.
Definition decl_plane.hpp:174
cvbbool_t ReleaseObject(OBJ &Object)
T make_shared(T... args)
T move(T... args)
@ DataType
Definition spectral.hpp:148
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
PlaneRole
A plane role describes the components of the plane. They can coarsely be separated in two sets.
Definition global.hpp:411
@ Undefined
Unknown/undefined value.
Definition global.hpp:413
std::shared_ptr< Plane > PlanePtr
Convenience shared pointer for Plane.
Definition global.hpp:78
Definition global.hpp:1080