IDS peak genericC++  v1.4.3
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 
22 namespace peak
23 {
24 
36 template <>
37 inline peak::ipl::Image BufferTo<peak::ipl::Image>(const std::shared_ptr<peak::core::Buffer>& buffer)
38 {
39  if (buffer == nullptr)
40  {
41  throw core::InvalidArgumentException("The given buffer is a nullptr!");
42  }
43 
44  if (buffer->HasImage())
45  {
46  assert(buffer->PixelFormatNamespace() != core::PixelFormatNamespace::IIDC
47  && buffer->PixelFormatNamespace() != core::PixelFormatNamespace::Custom);
48 
49  size_t usableHeight = buffer->Height();
50  size_t cutOff = 0;
51 
52  if (buffer->HasChunks())
53  {
54  auto chunks = buffer->Chunks();
55  for (const auto& chunk : chunks)
56  {
57  if (0x73020405 == chunk->ID())
58  {
59  uint16_t* chunkBase = static_cast<uint16_t*>(chunk->BasePtr());
60 
61  usableHeight = static_cast<size_t>(chunkBase[1]);
62  size_t usableOffsetY = static_cast<size_t>(chunkBase[3]);
63 
64  // Pitch per line
65  size_t pitch = buffer->Size() / buffer->Height();
66  cutOff = pitch * usableOffsetY;
67 
68  break;
69  }
70  }
71  }
72 
73  return peak::ipl::Image(static_cast<peak::ipl::PixelFormatName>(buffer->PixelFormat()),
74  static_cast<uint8_t*>(buffer->BasePtr()) + buffer->ImageOffset() + cutOff, buffer->Size() - buffer->ImageOffset() - cutOff,
75  buffer->Width(), usableHeight);
76  }
77  else if (buffer->PayloadType() == core::BufferPayloadType::Chunk)
78  {
79  auto remoteNodeMap = buffer->ParentDataStream()->ParentDevice()->RemoteDevice()->NodeMaps()[0];
80  remoteNodeMap->UpdateChunkNodes(buffer);
81 
82  auto chunkWidth = remoteNodeMap->FindNode<core::nodes::IntegerNode>("ChunkWidth");
83  if (chunkWidth->AccessStatus() == core::nodes::NodeAccessStatus::NotAvailable)
84  {
85  throw core::InvalidCastException("Buffer has no ChunkWidth.");
86  }
87  auto width = chunkWidth->Value();
88 
89  auto chunkHeight = remoteNodeMap->FindNode<core::nodes::IntegerNode>("ChunkHeight");
90  if (chunkHeight->AccessStatus() == core::nodes::NodeAccessStatus::NotAvailable)
91  {
92  throw core::InvalidCastException("Buffer has no ChunkHeight.");
93  }
94  auto height = chunkHeight->Value();
95 
96  auto chunkPixelFormat = remoteNodeMap->FindNode<core::nodes::EnumerationNode>("ChunkPixelFormat");
97  if (chunkPixelFormat->AccessStatus() == core::nodes::NodeAccessStatus::NotAvailable)
98  {
99  throw core::InvalidCastException("Buffer has no ChunkPixelFormat.");
100  }
101  auto pixelFormatValue = chunkPixelFormat->CurrentEntry()->NumericValue();
102  auto pixelFormat = static_cast<peak::ipl::PixelFormatName>(pixelFormatValue);
103 
104  // assume first chunks is image data
105  auto imageData = buffer->Chunks()[0];
106 
107  auto expectedDataSize = peak::ipl::PixelFormat(pixelFormat).CalculateStorageSizeOfPixels(static_cast<uint64_t>(width * height));
108  if (imageData->Size() < expectedDataSize)
109  {
110  std::stringstream msg;
111  msg << "The buffer's first chunk's size (" << imageData->Size()
112  << ") is smaller than the expected data size (" << expectedDataSize << ").";
113  throw core::InvalidCastException(msg.str());
114  }
115 
116  return peak::ipl::Image(pixelFormat, static_cast<uint8_t*>(imageData->BasePtr()), imageData->Size(),
117  static_cast<size_t>(width), static_cast<size_t>(height));
118  }
119  else
120  {
121  throw core::InvalidCastException("Buffer has no image data and no chunks.");
122  }
123 }
124 
125 } /* namespace peak */
Represents a GenAPI integer node.
Definition: peak_integer_node.hpp:35
The exception thrown for pass an invalid argument to a function.
Definition: peak_exception.hpp:73
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition: peak_dynamic_loader.h:32
The exception thrown for trying to apply an invalid cast.
Definition: peak_exception.hpp:81
Represents a GenAPI enumeration node.
Definition: peak_enumeration_node.hpp:36