34 : m_metadata{ std::make_shared<peak::common::Metadata>() }
35 , m_payload{ payload }
55 :
ImageView(ExtractPayloadFromBufferPart(part))
57 const auto buffer = part->ParentBuffer();
71inline ImageView::ExtractedPayload ImageView::ExtractPayload(
72 const std::shared_ptr<peak::core::Buffer>& buffer)
74 if (buffer ==
nullptr)
79 if (buffer->HasImage())
81 return ExtractPayloadTypeImage(buffer);
85 return ExtractPayloadTypeChunk(buffer);
89 throw core::InvalidCastException(
"Buffer has no image data and no chunks.");
93inline ImageView::ExtractedPayload ImageView::ExtractPayloadTypeImage(
94 const std::shared_ptr<peak::core::Buffer>& buffer)
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();
104 if (buffer->HasChunks())
106 auto chunks = buffer->Chunks();
110 throw core::InternalErrorException(
"Buffer has no chunks.");
113 const auto imageSizeWithoutChunks = chunks[0]->Size();
115 for (
const auto& chunk : chunks)
117 if (0x73020405 == chunk->ID())
119 auto* chunkBase =
static_cast<uint16_t*
>(chunk->BasePtr());
121 usableHeight =
static_cast<size_t>(chunkBase[1]);
122 const auto usableOffsetY =
static_cast<size_t>(chunkBase[3]);
125 const auto pitch = imageSizeWithoutChunks / buffer->Height();
126 const auto cutOffTop = pitch * usableOffsetY;
127 const auto cutOff = (buffer->Height() - usableHeight) * pitch;
129 firstPixelPtr += cutOffTop;
130 usableBytes = imageSizeWithoutChunks - cutOff;
137 return { {
static_cast<uint32_t
>(usableWidth),
static_cast<uint32_t
>(usableHeight) },
141inline ImageView::ExtractedPayload ImageView::ExtractPayloadTypeChunk(
142 const std::shared_ptr<peak::core::Buffer>& buffer)
144 auto remoteNodeMap = buffer->ParentDataStream()->ParentDevice()->RemoteDevice()->NodeMaps()[0];
145 remoteNodeMap->UpdateChunkNodes(buffer);
147 auto chunkWidth = remoteNodeMap->FindNode<core::nodes::IntegerNode>(
"ChunkWidth");
150 throw core::InvalidCastException(
"Buffer has no ChunkWidth.");
152 auto width = chunkWidth->Value();
154 auto chunkHeight = remoteNodeMap->FindNode<core::nodes::IntegerNode>(
"ChunkHeight");
157 throw core::InvalidCastException(
"Buffer has no ChunkHeight.");
159 auto height = chunkHeight->Value();
163 auto chunkPixelFormat = remoteNodeMap->FindNode<core::nodes::EnumerationNode>(
"ChunkPixelFormat");
166 throw core::InvalidCastException(
"Buffer has no ChunkPixelFormat.");
168 auto pixelFormatValue = chunkPixelFormat->CurrentEntry()->NumericValue();
172 auto imageData = buffer->Chunks()[0];
173 auto rawData =
static_cast<uint8_t*
>(imageData->BasePtr());
174 auto sizeInBytes = imageData->Size();
176 const auto pixelFormatInfo = peak::common::PixelFormatInfo(pixelFormat);
177 const auto expectedDataSize = pixelFormatInfo.GetSizeInBytes(size);
178 if (sizeInBytes < expectedDataSize)
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());
186 return { size, pixelFormat, rawData, sizeInBytes };
189inline ImageView::ExtractedPayload ImageView::ExtractPayloadFromBufferPart(
190 const std::shared_ptr<core::BufferPart>& part)
194 throw core::InvalidArgumentException(
"The given part is a nullptr!");
197 const auto partType = part->Type();
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) },
213 throw core::InvalidCastException(
"BufferPart has no image data.");
219 return m_payload.m_rawData;
224 return m_payload.m_rawData;
229 return m_payload.m_sizeInBytes;
234 return m_payload.m_pixelFormat;
244 return m_payload.m_size;
254 return m_payload.m_size.GetHeight();
266 return (pixelformatInfo.GetSizeInBytes(m_payload.m_size) / m_payload.m_size.GetHeight());
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