IDS peak genericC++ v1.12.0
Loading...
Searching...
No Matches
peak_image_view_impl.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
13#include "peak_image_view.hpp"
14
17
24
25
26#include <memory>
27
28namespace peak
29{
30namespace core
31{
32
33inline ImageView::ImageView(const peak::core::ImageView::ExtractedPayload& payload)
34 : m_metadata{ std::make_shared<peak::common::Metadata>() }
35 , m_payload{ payload }
36{}
37
38inline ImageView::ImageView(const std::shared_ptr<peak::core::Buffer>& buffer)
39 : ImageView(ExtractPayload(buffer))
40{
41 m_metadata->SetValueByKey<peak::common::MetadataKey::DeviceFrameID>(buffer->FrameID());
42
43 // NOTE: We need to try the timestamp, as it may not be supported by the TL
44 try
45 {
46 m_metadata->SetValueByKey<peak::common::MetadataKey::DeviceTimestamp>(buffer->Timestamp_ns());
47 }
49 {}
51 {}
52}
53
54inline ImageView::ImageView(const std::shared_ptr<core::BufferPart>& part)
55 : ImageView(ExtractPayloadFromBufferPart(part))
56{
57 const auto buffer = part->ParentBuffer();
58 m_metadata->SetValueByKey<peak::common::MetadataKey::DeviceFrameID>(buffer->FrameID());
59
60 // NOTE: We need to try the timestamp, as it may not be supported by the TL
61 try
62 {
63 m_metadata->SetValueByKey<peak::common::MetadataKey::DeviceTimestamp>(buffer->Timestamp_ns());
64 }
66 {}
68 {}
69}
70
71inline ImageView::ExtractedPayload ImageView::ExtractPayload(
72 const std::shared_ptr<peak::core::Buffer>& buffer)
73{
74 if (buffer == nullptr)
75 {
76 throw core::InvalidArgumentException("The given buffer is a nullptr!");
77 }
78
79 if (buffer->HasImage())
80 {
81 return ExtractPayloadTypeImage(buffer);
82 }
83 else if (buffer->PayloadType() == core::BufferPayloadType::Chunk)
84 {
85 return ExtractPayloadTypeChunk(buffer);
86 }
87 else
88 {
89 throw core::InvalidCastException("Buffer has no image data and no chunks.");
90 }
91}
92
93inline ImageView::ExtractedPayload ImageView::ExtractPayloadTypeImage(
94 const std::shared_ptr<peak::core::Buffer>& buffer)
95{
96 assert(buffer->PixelFormatNamespace() != core::PixelFormatNamespace::IIDC
97 && buffer->PixelFormatNamespace() != core::PixelFormatNamespace::Custom);
98
99 size_t usableHeight = buffer->Height();
100 const size_t usableWidth = buffer->Width();
101 size_t usableBytes = buffer->Size() - buffer->ImageOffset();
102 uint8_t* firstPixelPtr = static_cast<uint8_t*>(buffer->BasePtr()) + buffer->ImageOffset();
103
104 if (buffer->HasChunks())
105 {
106 auto chunks = buffer->Chunks();
107
108 if (chunks.empty())
109 {
110 throw core::InternalErrorException("Buffer has no chunks.");
111 }
112
113 const auto imageSizeWithoutChunks = chunks[0]->Size();
114
115 for (const auto& chunk : chunks)
116 {
117 if (0x73020405 == chunk->ID())
118 {
119 auto* chunkBase = static_cast<uint16_t*>(chunk->BasePtr());
120
121 usableHeight = static_cast<size_t>(chunkBase[1]);
122 const auto usableOffsetY = static_cast<size_t>(chunkBase[3]);
123
124 // Pitch per line
125 const auto pitch = imageSizeWithoutChunks / buffer->Height();
126 const auto cutOffTop = pitch * usableOffsetY;
127 const auto cutOff = (buffer->Height() - usableHeight) * pitch;
128
129 firstPixelPtr += cutOffTop;
130 usableBytes = imageSizeWithoutChunks - cutOff;
131
132 break;
133 }
134 }
135 }
136
137 return { { static_cast<uint32_t>(usableWidth), static_cast<uint32_t>(usableHeight) },
138 static_cast<peak::common::PixelFormat>(buffer->PixelFormat()), firstPixelPtr, usableBytes };
139}
140
141inline ImageView::ExtractedPayload ImageView::ExtractPayloadTypeChunk(
142 const std::shared_ptr<peak::core::Buffer>& buffer)
143{
144 auto remoteNodeMap = buffer->ParentDataStream()->ParentDevice()->RemoteDevice()->NodeMaps()[0];
145 remoteNodeMap->UpdateChunkNodes(buffer);
146
147 auto chunkWidth = remoteNodeMap->FindNode<core::nodes::IntegerNode>("ChunkWidth");
148 if (chunkWidth->AccessStatus() == core::nodes::NodeAccessStatus::NotAvailable)
149 {
150 throw core::InvalidCastException("Buffer has no ChunkWidth.");
151 }
152 auto width = chunkWidth->Value();
153
154 auto chunkHeight = remoteNodeMap->FindNode<core::nodes::IntegerNode>("ChunkHeight");
155 if (chunkHeight->AccessStatus() == core::nodes::NodeAccessStatus::NotAvailable)
156 {
157 throw core::InvalidCastException("Buffer has no ChunkHeight.");
158 }
159 auto height = chunkHeight->Value();
160
161 peak::common::Size size(static_cast<uint32_t>(width), static_cast<uint32_t>(height));
162
163 auto chunkPixelFormat = remoteNodeMap->FindNode<core::nodes::EnumerationNode>("ChunkPixelFormat");
164 if (chunkPixelFormat->AccessStatus() == core::nodes::NodeAccessStatus::NotAvailable)
165 {
166 throw core::InvalidCastException("Buffer has no ChunkPixelFormat.");
167 }
168 auto pixelFormatValue = chunkPixelFormat->CurrentEntry()->NumericValue();
169 auto pixelFormat = static_cast<peak::common::PixelFormat>(pixelFormatValue);
170
171 // assume first chunks is image data
172 auto imageData = buffer->Chunks()[0];
173 auto rawData = static_cast<uint8_t*>(imageData->BasePtr());
174 auto sizeInBytes = imageData->Size();
175
176 const auto pixelFormatInfo = peak::common::PixelFormatInfo(pixelFormat);
177 const auto expectedDataSize = pixelFormatInfo.GetSizeInBytes(size);
178 if (sizeInBytes < expectedDataSize)
179 {
180 std::stringstream msg;
181 msg << "The buffer's first chunk's size (" << imageData->Size() << ") is smaller than the expected data size ("
182 << expectedDataSize << ").";
183 throw core::InvalidCastException(msg.str());
184 }
185
186 return { size, pixelFormat, rawData, sizeInBytes };
187}
188
189inline ImageView::ExtractedPayload ImageView::ExtractPayloadFromBufferPart(
190 const std::shared_ptr<core::BufferPart>& part)
191{
192 if (part == nullptr)
193 {
194 throw core::InvalidArgumentException("The given part is a nullptr!");
195 }
196
197 const auto partType = part->Type();
198
200 || partType == core::BufferPartType::Image3D)
201 {
202 assert(part->FormatNamespace() != static_cast<uint64_t>(core::PixelFormatNamespace::IIDC)
203 && part->FormatNamespace() != static_cast<uint64_t>(core::PixelFormatNamespace::Custom));
204
205 const size_t height = part->Height();
206 const size_t width = part->Width();
207 return { { static_cast<uint32_t>(width), static_cast<uint32_t>(height) },
208 static_cast<peak::common::PixelFormat>(part->Format()), static_cast<uint8_t*>(part->BasePtr()),
209 part->Size() };
210 }
211 else
212 {
213 throw core::InvalidCastException("BufferPart has no image data.");
214 }
215}
216
217inline const uint8_t* ImageView::GetData() const
218{
219 return m_payload.m_rawData;
220}
221
222inline uint8_t* ImageView::GetData()
223{
224 return m_payload.m_rawData;
225}
226
227inline std::size_t ImageView::GetSizeInBytes() const
228{
229 return m_payload.m_sizeInBytes;
230}
231
233{
234 return m_payload.m_pixelFormat;
235}
236
238{
239 return peak::common::PixelFormatInfo(m_payload.m_pixelFormat);
240}
241
243{
244 return m_payload.m_size;
245}
246
247inline uint32_t ImageView::GetWidth() const
248{
249 return m_payload.m_size.GetWidth();
250}
251
252inline uint32_t ImageView::GetHeight() const
253{
254 return m_payload.m_size.GetHeight();
255}
256
257inline size_t ImageView::GetBitsPerPixel() const
258{
259 return peak::common::PixelFormatInfo(m_payload.m_pixelFormat).GetAllocatedBitsPerPixel();
260}
261
262inline size_t ImageView::GetBytesPerLine() const
263{
264 const auto pixelformatInfo = peak::common::PixelFormatInfo(m_payload.m_pixelFormat);
265
266 return (pixelformatInfo.GetSizeInBytes(m_payload.m_size) / m_payload.m_size.GetHeight());
267}
268
269inline std::shared_ptr<peak::common::Metadata> ImageView::GetMetadata()
270{
271 return m_metadata;
272}
273
274inline std::shared_ptr<const peak::common::Metadata> ImageView::GetMetadata() const
275{
276 return m_metadata;
277}
278
279} // namespace core
280} // namespace peak
Provides information and utility functions for pixel formats.
Definition peak_common_pixel_format.hpp:141
PEAK_COMMON_NO_DISCARD size_t GetAllocatedBitsPerPixel() const
Returns the number of allocated bits per pixel.
Definition peak_common_pixel_format.hpp:2566
PEAK_COMMON_NO_DISCARD constexpr T GetWidth() const
Returns the width.
Definition peak_common_size.hpp:119
PEAK_COMMON_NO_DISCARD uint32_t GetHeight() const override
Height of the image in pixels.
Definition peak_image_view_impl.hpp:252
PEAK_COMMON_NO_DISCARD std::size_t GetSizeInBytes() const override
Size of the image in bytes.
Definition peak_image_view_impl.hpp:227
PEAK_COMMON_NO_DISCARD size_t GetBytesPerLine() const override
The amount of bytes per line.
Definition peak_image_view_impl.hpp:262
PEAK_COMMON_NO_DISCARD peak::common::PixelFormatInfo GetPixelFormatInfo() const override
Information object for the PixelFormat of the contained image.
Definition peak_image_view_impl.hpp:237
PEAK_COMMON_NO_DISCARD size_t GetBitsPerPixel() const override
The amount of bits in one pixel.
Definition peak_image_view_impl.hpp:257
PEAK_COMMON_NO_DISCARD uint32_t GetWidth() const override
Width of the image in pixels.
Definition peak_image_view_impl.hpp:247
PEAK_COMMON_NO_DISCARD const uint8_t * GetData() const override
Get pointer to the first byte of the image (read-only).
Definition peak_image_view_impl.hpp:217
PEAK_COMMON_NO_DISCARD peak::common::PixelFormat GetPixelFormat() const override
PixelFormat of the contained image.
Definition peak_image_view_impl.hpp:232
PEAK_COMMON_NO_DISCARD peak::common::Size GetSize() const override
Get the dimensions of the image. I.e. Width, Height, etc.
Definition peak_image_view_impl.hpp:242
ImageView(const std::shared_ptr< peak::core::Buffer > &buffer)
Definition peak_image_view_impl.hpp:38
PEAK_COMMON_NO_DISCARD std::shared_ptr< peak::common::Metadata > GetMetadata() override
The metadata object belonging to the image.
Definition peak_image_view_impl.hpp:269
The exception thrown for passing an invalid argument to a function.
Definition peak_exception.hpp:109
The exception thrown for signaling that a feature is not available in the device.
Definition peak_exception.hpp:167
The exception thrown for signaling a feature is not implemented.
Definition peak_exception.hpp:183
detail::SizeT< uint32_t > Size
Size with unsigned 32-bit integer dimensions.
Definition peak_common_size.hpp:273
PixelFormat
Enum listing all supported pixel formats and their internal IDs.
Definition peak_common_pixel_format.hpp:45
@ DeviceTimestamp
Device-provided image acquisition timestamp in nanoseconds.
Definition peak_common_metadata_key.hpp:44
@ DeviceFrameID
A sequentially incremented identifier for the frame.
Definition peak_common_metadata_key.hpp:57
@ NotAvailable
Definition peak_node.hpp:47
The "core" namespace contains the direct GenAPI and GenTL wrapper.
Definition peak_buffer.hpp:34
@ Image3D
Definition peak_buffer_part.hpp:41
@ ConfidenceMap
Definition peak_buffer_part.hpp:45
@ Image2D
Definition peak_buffer_part.hpp:37
@ Chunk
Definition peak_buffer.hpp:47
@ Custom
Definition peak_buffer.hpp:70
@ IIDC
Definition peak_buffer.hpp:66
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_dynamic_loader.h:33