CVB++ 15.0
detail_image_plane.hpp
1#pragma once
2
3#include <algorithm>
4#include <memory>
5#include <new>
6#include <system_error>
7#include <utility>
8
9#include "../_cexports/c_img.h"
10
11#include "../global.hpp"
12#include "../data_type.hpp"
13
14#include "../_decl/decl_image_plane.hpp"
15#include "../_decl/decl_vpat.hpp"
16#include "../_decl/decl_linear_access.hpp"
17#include "../_decl/decl_image.hpp"
18#include "../plane.hpp"
19#include "../_decl/decl_wrapped_image.hpp"
20
21namespace Cvb
22{
23
24 CVB_BEGIN_INLINE_NS
25
26 inline ImagePlane::ImagePlane(const class Plane &plane)
27 : plane_(0)
28 {
29 if (plane.Rank() != 2)
30 throw std::runtime_error("only two dimensional planes can be converted to image planes");
31
32 planeImage_ = WrappedImage::FromArray(plane);
33 }
34
35 inline void *ImagePlane::Handle() const noexcept
36 {
37 return Parent().Handle();
38 }
39
40 inline bool ImagePlane::TryContiguousAccess(std::uint8_t *&basePtr) const noexcept
41 {
42 LinearAccessData linearAccess;
43 if (!TryLinearAccess(linearAccess))
44 return false;
45 auto dataType = DataType();
46 if (linearAccess.XInc() != dataType.BytesPerPixel())
47 return false;
48 auto size = Parent().Size();
49 if (linearAccess.YInc()
50 != static_cast<std::intptr_t>(size.Width()) * static_cast<std::intptr_t>(dataType.BytesPerPixel()))
51 return false;
52 basePtr = linearAccess.BasePtr();
53 return true;
54 }
55
57 {
58 std::uint8_t *basePtr = nullptr;
59 if (!TryContiguousAccess(basePtr))
60 throw std::runtime_error("failed to get contiguous access");
61 return basePtr;
62 }
63
64 inline bool ImagePlane::TryLinearAccess(LinearAccessData &linearAccessData) const noexcept
65 {
66 void *basePtr = nullptr;
67 std::intptr_t xInc = 0;
68 std::intptr_t yInc = 0;
69 if (!CExports::GetLinearAccess(Handle(), static_cast<CExports::cvbdim_t>(plane_), &basePtr, &xInc, &yInc))
70 {
71 linearAccessData = LinearAccessData();
72 return false;
73 }
74
75 linearAccessData = LinearAccessData(static_cast<std::uint8_t *>(basePtr), xInc, yInc, Parent().Width());
76 return true;
77 }
78
80 {
82 if (!TryLinearAccess(acc))
83 throw std::runtime_error("failed to get linear access");
84 return acc;
85 }
86
87 inline const Image &ImagePlane::Parent() const noexcept
88 {
89 if (planeImage_)
90 return *planeImage_;
91
92 return *parentImage_;
93 }
94
95 inline class Vpat ImagePlane::Vpat() const
96 {
97 return Cvb::Vpat(Parent(), plane_);
98 }
99
101 {
102 return Internal::DoBoolCallObjectOut<Image>([&](void *&img) {
103 return CVB_CALL_CAPI(CreateImageSubList(Handle(), static_cast<CExports::cvbdim_t>(plane_), 1, true, img));
104 });
105 }
106
107 inline double ImagePlane::GetPixel(Point2D<int> position) const
108 {
109 if (position.X() < 0 || position.Y() < 0 || position.X() >= Parent().Width() || position.Y() >= Parent().Height())
110 throw std::invalid_argument("invalid image position");
111
113
114 auto vpat = Vpat();
115 auto dataType = DataType();
116
117 if (dataType.IsFloat())
118 {
119 switch (dataType.BytesPerPixel())
120 {
121 case sizeof(float):
122 val = static_cast<double>(vpat.Value<float>(position));
123 break;
124
125 case sizeof(double):
126 val = vpat.Value<double>(position);
127 break;
128
129 default:
130 break;
131 }
132 }
133 else if (dataType.IsSignedInteger())
134 {
135 switch (dataType.BytesPerPixel())
136 {
137 case sizeof(std::int8_t):
138 val = static_cast<double>(vpat.Value<std::int8_t>(position));
139 break;
140
141 case sizeof(std::int16_t):
142 val = static_cast<double>(vpat.Value<std::int16_t>(position));
143 break;
144
145 case sizeof(std::int32_t):
146 val = static_cast<double>(vpat.Value<std::int32_t>(position));
147 break;
148
149 case sizeof(std::int64_t):
150 val = static_cast<double>(vpat.Value<std::int64_t>(position));
151 break;
152
153 default:
154 break;
155 }
156 }
157 else
158 {
159 switch (dataType.BytesPerPixel())
160 {
161 case sizeof(std::uint8_t):
162 val = static_cast<double>(vpat.Value<std::uint8_t>(position));
163 break;
164
165 case sizeof(std::uint16_t):
166 val = static_cast<double>(vpat.Value<std::uint16_t>(position));
167 break;
168
169 case sizeof(std::uint32_t):
170 val = static_cast<double>(vpat.Value<std::uint32_t>(position));
171 break;
172
173 case sizeof(std::uint64_t):
174 val = static_cast<double>(vpat.Value<std::uint64_t>(position));
175 break;
176
177 default:
178 break;
179 }
180 }
181
182 return val;
183 }
184
185 template <class Type>
186 inline ContiguousIterator<Type> ImagePlane::BeginContiguous()
187 {
188 return ContiguousIterator<Type>(reinterpret_cast<Type *>(ContiguousAccess()), 0);
189 }
190
191 template <class Type>
192 inline ContiguousIterator<Type> ImagePlane::EndContiguous()
193 {
194 auto size = Parent().Size();
195 return ContiguousIterator<Type>(reinterpret_cast<Type *>(ContiguousAccess()), size.Width() * size.Height());
196 }
197
198 template <class Type>
203
204 template <class Type>
209
210 template <class Type>
211 inline ContiguousConstIterator<Type> ImagePlane::CBeginContiguous() const
212 {
213 return ContiguousConstIterator<Type>(reinterpret_cast<Type *>(ContiguousAccess()), 0);
214 }
215
216 template <class Type>
217 inline ContiguousConstIterator<Type> ImagePlane::CEndContiguous() const
218 {
219 auto size = Parent().Size();
220 return ContiguousConstIterator<Type>(reinterpret_cast<Type *>(ContiguousAccess()), size.Width() * size.Height());
221 }
222
223 template <class Type>
228
229 template <class Type>
234
235 template <class Type>
236 inline LinearIterator<Type> ImagePlane::BeginLinear()
237 {
238 return LinearIterator<Type>(LinearAccess(), Parent().Size());
239 }
240
241 template <class Type>
242 inline LinearIterator<Type> ImagePlane::EndLinear()
243 {
244 auto size = Parent().Size();
245 return LinearIterator<Type>(LinearAccess(), Parent().Size(), Point2D<int>(0, size.Height()));
246 }
247
248 template <class Type>
253
254 template <class Type>
259
260 template <class Type>
261 inline LinearConstIterator<Type> ImagePlane::CBeginLinear() const
262 {
263 return LinearConstIterator<Type>(LinearAccess(), Parent().Size());
264 }
265
266 template <class Type>
267 inline LinearConstIterator<Type> ImagePlane::CEndLinear() const
268 {
269 auto size = Parent().Size();
270 return LinearConstIterator<Type>(LinearAccess(), Parent().Size(), Point2D<int>(0, size.Height()));
271 }
272
273 template <class Type>
278
279 template <class Type>
284
285 template <class Type>
286 inline VpatIterator<Type> ImagePlane::BeginVpat()
287 {
288 return VpatIterator<Type>(Vpat(), Parent().Size());
289 }
290
291 template <class Type>
292 inline VpatIterator<Type> ImagePlane::EndVpat()
293 {
294 auto size = Parent().Size();
295 return VpatIterator<Type>(Vpat(), Parent().Size(), Point2D<int>(0, size.Height()));
296 }
297
298 template <class Type>
303
304 template <class Type>
310
311 template <class Type>
312 inline VpatConstIterator<Type> ImagePlane::CBeginVpat() const
313 {
314 return VpatConstIterator<Type>(Vpat(), Parent().Size());
315 }
316
317 template <class Type>
318 inline VpatConstIterator<Type> ImagePlane::CEndVpat() const
319 {
320 auto size = Parent().Size();
321 return VpatConstIterator<Type>(Vpat(), Parent().Size(), Point2D<int>(0, size.Height()));
322 }
323
324 template <class Type>
329
330 template <class Type>
336
337 inline DataType ImagePlane::DataType() const noexcept
338 {
340 static_cast<int>(CExports::ImageDatatype(Handle(), static_cast<CExports::cvbdim_t>(plane_))));
341 }
342
343 inline int PlaneTraits<ImagePlane>::GetWidth(const ImagePlane &plane)
344 {
345 return plane.Parent().Width();
346 }
347
348 inline int PlaneTraits<ImagePlane>::GetHeight(const ImagePlane &plane)
349 {
350 return plane.Parent().Height();
351 }
352
353 CVB_END_INLINE_NS
354
355} // 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
The Common Vision Blox image.
Definition decl_image.hpp:50
int Width() const noexcept
Width of the image in pixels.
Definition decl_image.hpp:309
Size2D< int > Size() const noexcept
Size of the image in pixels.
Definition decl_image.hpp:428
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:237
Image plane information container.
Definition decl_image_plane.hpp:29
class DataType DataType() const noexcept override
Data type descriptor for this array.
Definition detail_image_plane.hpp:337
std::uint8_t * ContiguousAccess() const
Attempt a contiguous access on the plane's pixels.
Definition detail_image_plane.hpp:56
VpatIterator< Type > EndVpat()
Get iterator access to the plane.
Definition detail_image_plane.hpp:292
ContiguousConstIterator< Type > CBeginContiguous() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:211
std::reverse_iterator< LinearIterator< Type > > RBeginLinear()
Get iterator access to the plane.
Definition detail_image_plane.hpp:249
std::reverse_iterator< ContiguousIterator< Type > > REndContiguous()
Get iterator access to the plane.
Definition detail_image_plane.hpp:205
VpatConstIterator< Type > CEndVpat() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:318
ContiguousIterator< Type > BeginContiguous()
Get iterator access to the plane.
Definition detail_image_plane.hpp:186
ContiguousIterator< Type > EndContiguous()
Get iterator access to the plane.
Definition detail_image_plane.hpp:192
LinearAccessData LinearAccess() const
Attempt a linear access on the plane's pixels.
Definition detail_image_plane.hpp:79
bool TryLinearAccess(LinearAccessData &linearAccessData) const noexcept
Attempt a linear access on the plane's pixels.
Definition detail_image_plane.hpp:64
std::reverse_iterator< VpatConstIterator< Type > > CRBeginVpat() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:325
VpatConstIterator< Type > CBeginVpat() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:312
std::reverse_iterator< LinearConstIterator< Type > > CREndLinear() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:280
LinearConstIterator< Type > CEndLinear() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:267
VpatIterator< Type > BeginVpat()
Get iterator access to the plane.
Definition detail_image_plane.hpp:286
std::reverse_iterator< LinearConstIterator< Type > > CRBeginLinear() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:274
class Vpat Vpat() const
Accesses the virtual pixel access table.
Definition detail_image_plane.hpp:95
std::reverse_iterator< VpatConstIterator< Type > > CREndVpat() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:331
std::reverse_iterator< ContiguousConstIterator< Type > > CRBeginContiguous() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:224
LinearIterator< Type > EndLinear()
Get iterator access to the plane.
Definition detail_image_plane.hpp:242
std::unique_ptr< Image > Map() const
Create a map from a single image plane that shares its memory with the original plane.
Definition detail_image_plane.hpp:100
void * Handle() const noexcept override
Classic API image handle.
Definition detail_image_plane.hpp:35
bool TryContiguousAccess(std::uint8_t *&basePtr) const noexcept
Attempt a contiguous access on the plane's pixels.
Definition detail_image_plane.hpp:40
std::reverse_iterator< VpatIterator< Type > > REndVpat()
Get iterator access to the plane.
Definition detail_image_plane.hpp:305
ImagePlane(const class Plane &plane)
Implicitly convert a plane to an image plane.
Definition detail_image_plane.hpp:26
std::reverse_iterator< ContiguousIterator< Type > > RBeginContiguous()
Get iterator access to the plane.
Definition detail_image_plane.hpp:199
double GetPixel(Point2D< int > position) const
Gets the pixel value at the given position.
Definition detail_image_plane.hpp:107
ContiguousConstIterator< Type > CEndContiguous() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:217
std::reverse_iterator< VpatIterator< Type > > RBeginVpat()
Get iterator access to the plane.
Definition detail_image_plane.hpp:299
int Plane() const noexcept
Plane index in the image, to which this plane refers to.
Definition decl_image_plane.hpp:147
const Image & Parent() const noexcept
Image to which this plane descriptor refers to.
Definition detail_image_plane.hpp:87
LinearIterator< Type > BeginLinear()
Get iterator access to the plane.
Definition detail_image_plane.hpp:236
LinearConstIterator< Type > CBeginLinear() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:261
std::reverse_iterator< LinearIterator< Type > > REndLinear()
Get iterator access to the plane.
Definition detail_image_plane.hpp:255
std::reverse_iterator< ContiguousConstIterator< Type > > CREndContiguous() const
Get iterator access to the plane.
Definition detail_image_plane.hpp:230
Linear access properties.
Definition decl_linear_access.hpp:25
std::intptr_t XInc() const noexcept
X-increment for linear access.
Definition decl_linear_access.hpp:112
std::intptr_t YInc() const noexcept
Y-increment for linear access.
Definition decl_linear_access.hpp:122
std::uint8_t * BasePtr() const noexcept
Linear access base pointer.
Definition decl_linear_access.hpp:102
int Rank() const noexcept override
Gets the number of dimensions of this plane.
Definition decl_plane.hpp:111
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
T X() const noexcept
Gets the x-component of the point.
Definition point_2d.hpp:84
T Y() const noexcept
Gets the y-component of the point.
Definition point_2d.hpp:104
Virtual Pixel Access Table.
Definition decl_vpat.hpp:24
cvbbool_t CreateImageSubList(IMG ImageIn, cvbval_t PlaneIndex, cvbval_t NumPlanes, cvbbool_t MapImage, IMG &ImageOut)
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
T quiet_NaN(T... args)
Definition global.hpp:1080