IDS peak genericC++ v1.9.0
Loading...
Searching...
No Matches
peak_buffer_converter_ipl.hpp
Go to the documentation of this file.
1
11#pragma once
12
13
14#include <peak_ipl/peak_ipl.hpp>
18
19#include <cassert>
20
21
22namespace peak
23{
24
39template <>
40inline peak::ipl::Image BufferTo<peak::ipl::Image>(const std::shared_ptr<peak::core::Buffer>& buffer)
41{
42 if (buffer == nullptr)
43 {
44 throw core::InvalidArgumentException("The given buffer is a nullptr!");
45 }
46
47 if (buffer->HasImage())
48 {
49 assert(buffer->PixelFormatNamespace() != core::PixelFormatNamespace::IIDC
50 && buffer->PixelFormatNamespace() != core::PixelFormatNamespace::Custom);
51
52 size_t usableHeight = buffer->Height();
53 const size_t usableWidth = buffer->Width();
54 size_t usableBytes = buffer->Size() - buffer->ImageOffset();
55 uint8_t* firstPixelPtr = static_cast<uint8_t*>(buffer->BasePtr()) + buffer->ImageOffset();
56
57 if (buffer->HasChunks())
58 {
59 auto chunks = buffer->Chunks();
60
61 if (chunks.empty())
62 {
63 throw core::InternalErrorException("Buffer has no chunks.");
64 }
65
66 const auto imageSizeWithoutChunks = chunks[0]->Size();
67
68 for (const auto& chunk : chunks)
69 {
70 if (0x73020405 == chunk->ID())
71 {
72 uint16_t* chunkBase = static_cast<uint16_t*>(chunk->BasePtr());
73
74 usableHeight = static_cast<size_t>(chunkBase[1]);
75 const auto usableOffsetY = static_cast<size_t>(chunkBase[3]);
76
77 // Pitch per line
78 const auto pitch = imageSizeWithoutChunks / buffer->Height();
79 const auto cutOffTop = pitch * usableOffsetY;
80 const auto cutOff = (buffer->Height() - usableHeight) * pitch;
81
82 firstPixelPtr += cutOffTop;
83 usableBytes = imageSizeWithoutChunks - cutOff;
84
85 break;
86 }
87 }
88 }
89
90 return peak::ipl::Image(static_cast<peak::ipl::PixelFormatName>(buffer->PixelFormat()),
91 firstPixelPtr, usableBytes, usableWidth, usableHeight, buffer->Timestamp_ns());
92 }
93 else if (buffer->PayloadType() == core::BufferPayloadType::Chunk)
94 {
95 auto remoteNodeMap = buffer->ParentDataStream()->ParentDevice()->RemoteDevice()->NodeMaps()[0];
96 remoteNodeMap->UpdateChunkNodes(buffer);
97
98 auto chunkWidth = remoteNodeMap->FindNode<core::nodes::IntegerNode>("ChunkWidth");
99 if (chunkWidth->AccessStatus() == core::nodes::NodeAccessStatus::NotAvailable)
100 {
101 throw core::InvalidCastException("Buffer has no ChunkWidth.");
102 }
103 auto width = chunkWidth->Value();
104
105 auto chunkHeight = remoteNodeMap->FindNode<core::nodes::IntegerNode>("ChunkHeight");
106 if (chunkHeight->AccessStatus() == core::nodes::NodeAccessStatus::NotAvailable)
107 {
108 throw core::InvalidCastException("Buffer has no ChunkHeight.");
109 }
110 auto height = chunkHeight->Value();
111
112 auto chunkPixelFormat = remoteNodeMap->FindNode<core::nodes::EnumerationNode>("ChunkPixelFormat");
113 if (chunkPixelFormat->AccessStatus() == core::nodes::NodeAccessStatus::NotAvailable)
114 {
115 throw core::InvalidCastException("Buffer has no ChunkPixelFormat.");
116 }
117 auto pixelFormatValue = chunkPixelFormat->CurrentEntry()->NumericValue();
118 auto pixelFormat = static_cast<peak::ipl::PixelFormatName>(pixelFormatValue);
119
120 // assume first chunks is image data
121 auto imageData = buffer->Chunks()[0];
122
123 auto expectedDataSize = peak::ipl::PixelFormat(pixelFormat).CalculateStorageSizeOfPixels(static_cast<uint64_t>(width * height));
124 if (imageData->Size() < expectedDataSize)
125 {
126 std::stringstream msg;
127 msg << "The buffer's first chunk's size (" << imageData->Size()
128 << ") is smaller than the expected data size (" << expectedDataSize << ").";
129 throw core::InvalidCastException(msg.str());
130 }
131
132 return peak::ipl::Image(pixelFormat, static_cast<uint8_t*>(imageData->BasePtr()), imageData->Size(),
133 static_cast<size_t>(width), static_cast<size_t>(height), buffer->Timestamp_ns());
134 }
135 else
136 {
137 throw core::InvalidCastException("Buffer has no image data and no chunks.");
138 }
139}
140
141} /* namespace peak */
The exception thrown for internal errors.
Definition peak_exception.hpp:58
The exception thrown for passing an invalid argument to a function.
Definition peak_exception.hpp:74
The exception thrown for trying to apply an invalid cast.
Definition peak_exception.hpp:82
Represents a GenAPI enumeration node.
Definition peak_enumeration_node.hpp:37
Represents a GenAPI integer node.
Definition peak_integer_node.hpp:36
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_dynamic_loader.h:33
peak::ipl::Image BufferTo< peak::ipl::Image >(const std::shared_ptr< peak::core::Buffer > &buffer)
Converts a core::Buffer into a peak::ipl::Image.
Definition peak_buffer_converter_ipl.hpp:40