CVB++ 15.0
detail_plane_access.hpp
1#pragma once
2
3#include "../global.hpp"
4
5#include "../_decl/decl_vpat.hpp"
6#include "../_decl/decl_linear_access.hpp"
7#include "../_decl/decl_array_access.hpp"
8
9#include "../_decl/decl_image_plane.hpp"
10
11#include "../plane.hpp"
12
13namespace Cvb
14{
15
16 CVB_BEGIN_INLINE_NS
17
18 template <class PLANE_T, std::enable_if_t<PlaneTraits<PLANE_T>::HasVpat, int>>
19 LinearAccessData LinearAccessData::FromPlaneImpl(const PLANE_T &plane) noexcept
20 {
21 using PlaneTrait = PlaneTraits<PLANE_T>;
22 return LinearAccessData::FromVpat(PlaneTrait::GetVpat(plane),
23 {PlaneTrait::GetWidth(plane), PlaneTrait::GetHeight(plane)},
24 PlaneTrait::GetDataType(plane));
25 }
26
27 template <class PLANE_T, std::enable_if_t<!PlaneTraits<PLANE_T>::HasVpat, int>>
28 LinearAccessData LinearAccessData::FromPlaneImpl(const PLANE_T &plane) noexcept
29 {
30 using PlaneTrait = PlaneTraits<PLANE_T>;
31 const std::intptr_t xInc = PlaneTrait::GetXInc(plane);
32 const int width = PlaneTrait::GetWidth(plane);
33 if (PlaneTrait::GetRank(plane) == 1)
34 return LinearAccessData{PlaneTrait::GetBasePtr(plane), xInc, xInc * width, width};
35 return LinearAccessData{PlaneTrait::GetBasePtr(plane), xInc, PlaneTrait::GetYInc(plane), width};
36 }
37
38 // declaration see linear_access.hpp
39 template <class PLANE_T>
40 inline LinearAccessData LinearAccessData::FromPlane(const PLANE_T &plane) noexcept
41 {
42 // hiding the impl, as this would be a perfect case to use C++17 if constexpr(PlaneTraits<PLANE_T>::HasVpat)
43 // the change can then be done without any API change.
44 return FromPlaneImpl(plane);
45 }
46
48 Cvb::DataType dataType) noexcept
49 {
50 auto vpat = access.VpatPtr();
51 if (!vpat || size.Width() <= 0 || size.Height() <= 0)
52 return {};
53 const std::intptr_t bytesPerPixel = dataType.BytesPerPixel(); // returns int
54 if (bytesPerPixel == 0)
55 return {};
56
57 auto lastXEntry = vpat[0].OffsetX;
58 const auto xInc = size.Width() == 1 ? bytesPerPixel : vpat[1].OffsetX - lastXEntry;
59 auto lastYEntry = vpat[0].OffsetY;
60 const auto yInc = size.Height() == 1 ? xInc * size.Width() : vpat[1].OffsetY - lastYEntry;
61 const auto basePtr = access.BasePtr() + vpat->OffsetY + vpat->OffsetX;
62
63 const auto max = std::max<int>(size.Width(), size.Height());
64 for (int i = 1; i < max; ++i)
65 {
66 auto current = vpat[i];
67 if (i < size.Width())
68 {
69 if ((current.OffsetX - lastXEntry) != xInc)
70 return {};
71 lastXEntry = current.OffsetX;
72 }
73 if (i < size.Height())
74 {
75 if ((current.OffsetY - lastYEntry) != yInc)
76 return {};
77 lastYEntry = current.OffsetY;
78 }
79 }
80
81 return LinearAccessData{basePtr, xInc, yInc, size.Width()};
82 }
83
84 // declaration see array_access.hpp
85 template <class PLANE_T>
86 inline ArrayAccess ArrayAccess::FromPlane(const PLANE_T &plane) noexcept
87 {
88 using PlaneTrait = PlaneTraits<PLANE_T>;
89 return ArrayAccess::FromLinearAccess(LinearAccessData::FromPlane(plane), {PlaneTrait::GetWidth(plane), 0});
90 // size.Height is never used, so 0 is fine here..
91 }
92
93 inline ArrayAccess ArrayAccess::FromLinearAccess(const LinearAccessData &access, Size2D<int> size) noexcept
94 {
95 if (access)
96 {
97 if (access.XInc() > 0 && access.XInc() * size.Width() == access.YInc())
98 return ArrayAccess{access.BasePtr(), access.XInc(), size.Width()};
99 }
100 return {};
101 }
102
103 inline ArrayAccess ArrayAccess::FromVpat(const Vpat &access, Size2D<int> size, DataType dataType) noexcept
104 {
105 return FromLinearAccess(LinearAccessData::FromVpat(access, size, dataType), size);
106 }
107
108 inline LinearAccessData ArrayAccess::NewMoved(const Rect<int> newAoi) const noexcept
109 {
110 return LinearAccessData{basePtr_ + newAoi.Top() * inc_ * width_ + newAoi.Left() * inc_, inc_, inc_ * width_,
111 newAoi.Width()};
112 }
113
114 CVB_END_INLINE_NS
115
116} // namespace Cvb
static ArrayAccess FromVpat(const Vpat &access, Size2D< int > size, DataType dataType) noexcept
Tries to get a valid ArrayAccess trait from the given VPAT access.
Definition detail_plane_access.hpp:103
static ArrayAccess FromLinearAccess(const LinearAccessData &access, Size2D< int > size) noexcept
Tries to get a valid ArrayAccess trait from the given access.
Definition detail_plane_access.hpp:93
LinearAccessData NewMoved(const Rect< int > newAoi) const noexcept
Creates a new, moved linear access object.
Definition detail_plane_access.hpp:108
static ArrayAccess FromPlane(const PLANE_T &plane) noexcept
Tries to get a valid ArrayAccess trait from the given plane.
Definition detail_plane_access.hpp:86
Data type description for an image plane.
Definition data_type.hpp:23
Linear access properties.
Definition decl_linear_access.hpp:25
LinearAccessData() noexcept
Creates a default linear access data set.
static LinearAccessData FromPlane(const PLANE_T &plane) noexcept
Tries to get a valid LinearAccessData from the given plane.
Definition detail_plane_access.hpp:40
static LinearAccessData FromVpat(const Vpat &access, Cvb::Size2D< int > size, Cvb::DataType dataType) noexcept
Tries to get a valid LinearAccessData trait from the given vpat.
Definition detail_plane_access.hpp:47
Rectangle object.
Definition rect.hpp:24
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition size_2d.hpp:20
Virtual Pixel Access Table.
Definition decl_vpat.hpp:24
T max(T... args)
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
Definition global.hpp:1080