IDS peak IPL  v1.6.0
peak_ipl_image.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 
16 
17 #include <cstddef>
18 #include <cstdint>
19 #include <utility>
20 
21 namespace
22 {
23 class ImageBackendAccessor;
24 }
25 
29 namespace peak
30 {
31 namespace ipl
32 {
33 
37 class Image
38 {
39 public:
47  Image();
60  Image(const peak::ipl::PixelFormat& pixelFormat, size_t width, size_t height);
79  Image(
80  const peak::ipl::PixelFormat& pixelFormat, uint8_t* buffer, size_t bufferSize, size_t width, size_t height);
81  Image(const peak::ipl::PixelFormat& pixelFormat, size_t width, size_t height, uint64_t timestamp_ns);
82  Image(
83  const peak::ipl::PixelFormat& pixelFormat, uint8_t* buffer, size_t bufferSize, size_t width, size_t height, uint64_t timestamp_ns);
84  ~Image();
85  Image(const Image& other) = delete;
86  Image& operator=(const Image& other) = delete;
87  Image(Image&& other) noexcept;
88  Image& operator=(Image&& other) noexcept;
89 
99  size_t Width() const;
100 
110  size_t Height() const;
111 
125  uint8_t* PixelPointer(size_t xPos, size_t yPos) const;
126 
136  size_t ByteCount() const;
137 
148 
158  uint8_t* Data() const;
159 
174  Image ConvertTo(const peak::ipl::PixelFormat& outputPixelFormat,
176 
195  Image ConvertTo(const peak::ipl::PixelFormat& outputPixelFormat, uint8_t* outputImageBuffer,
196  size_t outputImageBufferSize,
198 
208  Image Clone() const;
209 
219  bool Empty() const;
220 
229  uint64_t Timestamp() const;
230 
234  bool operator==(const Image& img) const;
235 
239  bool operator!=(const Image& img) const;
240 
241 private:
242  friend ImageBackendAccessor;
243  explicit Image(PEAK_IPL_IMAGE_HANDLE imageHandle) noexcept;
244  PEAK_IPL_IMAGE_HANDLE m_backendHandle{};
245 };
246 
247 } /* namespace ipl */
248 } /* namespace peak */
249 
250 namespace
251 {
252 
253 // helper class to access the C-backend of the Image, which are hidden from the public interface
254 class ImageBackendAccessor
255 {
256 public:
257  static PEAK_IPL_IMAGE_HANDLE BackendHandle(const peak::ipl::Image& image)
258  {
259  return image.m_backendHandle;
260  }
261  static peak::ipl::Image CreateImage(PEAK_IPL_IMAGE_HANDLE imageHandle)
262  {
263  return peak::ipl::Image(imageHandle);
264  }
265 };
266 
267 } /* namespace */
268 
270 
271 namespace peak
272 {
273 namespace ipl
274 {
275 
276 inline Image::Image()
277 {
280  });
281 }
282 
283 inline Image::Image(const peak::ipl::PixelFormat& pixelFormat, size_t width, size_t height)
284 {
287  static_cast<PEAK_IPL_PIXEL_FORMAT>(pixelFormat.PixelFormatName()), width, height, &m_backendHandle);
288  });
289 }
290 
292  const peak::ipl::PixelFormat& pixelFormat, uint8_t* buffer, size_t bufferSize, size_t width, size_t height)
293 {
296  static_cast<PEAK_IPL_PIXEL_FORMAT>(pixelFormat.PixelFormatName()), buffer, bufferSize, width, height,
297  &m_backendHandle);
298  });
299 }
300 
301 inline Image::Image(const peak::ipl::PixelFormat& pixelFormat, size_t width, size_t height, uint64_t timestamp_ns) :
302  Image::Image(pixelFormat, width, height)
303 {
305  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_SetTimestamp(m_backendHandle, timestamp_ns);
306  });
307 }
308 
310  const peak::ipl::PixelFormat& pixelFormat, uint8_t* buffer, size_t bufferSize, size_t width, size_t height, uint64_t timestamp_ns) :
311  Image::Image(pixelFormat, buffer, bufferSize, width, height)
312 {
314  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_SetTimestamp(m_backendHandle, timestamp_ns);
315  });
316 }
317 
318 inline Image::Image(PEAK_IPL_IMAGE_HANDLE imageHandle) noexcept
319  : m_backendHandle(imageHandle)
320 {}
321 
323 {
324  (void)PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Destruct(m_backendHandle);
325 }
326 
327 inline Image::Image(Image&& other) noexcept
328 {
329  *this = std::move(other);
330 }
331 
332 inline Image& Image::operator=(Image&& other) noexcept
333 {
334  if (this != &other)
335  {
336  (void)PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Destruct(m_backendHandle);
337  m_backendHandle = other.m_backendHandle;
338  other.m_backendHandle = nullptr;
339  }
340 
341  return *this;
342 }
343 
344 inline size_t Image::Width() const
345 {
346  size_t width = 0;
347 
348  ExecuteAndMapReturnCodes([&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetWidth(m_backendHandle, &width); });
349 
350  return width;
351 }
352 
353 inline size_t Image::Height() const
354 {
355  size_t height = 0;
356 
357  ExecuteAndMapReturnCodes([&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetHeight(m_backendHandle, &height); });
358 
359  return height;
360 }
361 
362 inline uint8_t* Image::PixelPointer(size_t xPos, size_t yPos) const
363 {
364  uint8_t* pixelPosition = nullptr;
365 
367  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetPixelPointer(m_backendHandle, xPos, yPos, &pixelPosition);
368  });
369 
370  return pixelPosition;
371 }
372 
373 inline size_t Image::ByteCount() const
374 {
375  size_t byteCount = 0;
376 
378  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetByteCount(m_backendHandle, &byteCount); });
379 
380  return byteCount;
381 }
382 
384 {
386 
389  m_backendHandle, reinterpret_cast<PEAK_IPL_PIXEL_FORMAT*>(&pixelFormatName));
390  });
391 
392  return peak::ipl::PixelFormat{ pixelFormatName };
393 }
394 
395 inline uint8_t* Image::Data() const
396 {
397  uint8_t* data = nullptr;
398 
399  ExecuteAndMapReturnCodes([&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetData(m_backendHandle, &data); });
400 
401  return data;
402 }
403 
405  const peak::ipl::PixelFormat& outputPixelFormat, peak::ipl::ConversionMode conversionMode) const
406 {
407  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
409  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_ConvertTo(m_backendHandle,
410  static_cast<PEAK_IPL_PIXEL_FORMAT>(outputPixelFormat.PixelFormatName()),
411  static_cast<PEAK_IPL_CONVERSION_MODE>(conversionMode), &outputImageHandle);
412  });
413 
414  return ImageBackendAccessor::CreateImage(outputImageHandle);
415 }
416 
417 inline Image Image::ConvertTo(const peak::ipl::PixelFormat& outputPixelFormat, uint8_t* outputImageBuffer,
418  size_t outputImageBufferSize, peak::ipl::ConversionMode conversionMode) const
419 {
420  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
423  static_cast<PEAK_IPL_PIXEL_FORMAT>(outputPixelFormat.PixelFormatName()), outputImageBuffer,
424  outputImageBufferSize, static_cast<PEAK_IPL_CONVERSION_MODE>(conversionMode), &outputImageHandle);
425  });
426 
427  return ImageBackendAccessor::CreateImage(outputImageHandle);
428 }
429 
430 inline Image Image::Clone() const
431 {
432  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
434  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Clone(m_backendHandle, &outputImageHandle); });
435 
436  return ImageBackendAccessor::CreateImage(outputImageHandle);
437 }
438 
439 inline bool Image::Empty() const
440 {
441  return (m_backendHandle == nullptr) || (PixelFormat().PixelFormatName() == PixelFormatName::Invalid);
442 }
443 
444 inline uint64_t Image::Timestamp() const
445 {
446  uint64_t timestamp_ns;
448  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetTimestamp(m_backendHandle, &timestamp_ns);
449  });
450 
451  return timestamp_ns;
452 }
453 
454 inline bool Image::operator==(const peak::ipl::Image& img) const
455 {
456  if (img.ByteCount() != this->ByteCount())
457  {
458  return false;
459  }
460  if (img.Width() != this->Width())
461  {
462  return false;
463  }
464  if (img.Height() != this->Height())
465  {
466  return false;
467  }
468  if (img.PixelFormat().PixelFormatName() != this->PixelFormat().PixelFormatName())
469  {
470  return false;
471  }
472  if (img.Timestamp() != this->Timestamp())
473  {
474  return false;
475  }
476 
477  return memcmp(img.Data(), this->Data(), img.ByteCount()) == 0;
478 }
479 
480 inline bool Image::operator!=(const peak::ipl::Image& img) const
481 {
482  return !(*this == img);
483 }
484 
485 } /* namespace ipl */
486 } /* namespace peak */
PEAK_IPL_C_API PEAK_IPL_Image_GetData(PEAK_IPL_IMAGE_HANDLE imageHandle, uint8_t **data)
Queries the data pointer of the given image.
PEAK_IPL_C_API PEAK_IPL_Image_GetPixelFormat(PEAK_IPL_IMAGE_HANDLE imageHandle, PEAK_IPL_PIXEL_FORMAT *pixelFormat)
Queries the pixel format of the given image.
PEAK_IPL_C_API PEAK_IPL_Image_ConvertToBuffer(PEAK_IPL_IMAGE_HANDLE imageHandle, PEAK_IPL_PIXEL_FORMAT outputPixelFormat, uint8_t *outputImageBuffer, size_t outputImageBufferSize, PEAK_IPL_CONVERSION_MODE conversionMode, PEAK_IPL_IMAGE_HANDLE *outputImageHandle)
Stores the data of the current image converted to the given pixel format into a destination buffer an...
#define PEAK_IPL_C_ABI_PREFIX
Definition: peak_ipl_dll_defines.h:16
Image()
Creates an empty image, i.e. with size 0x0 and invalid pixel format.
Definition: peak_ipl_image.hpp:276
Image ConvertTo(const peak::ipl::PixelFormat &outputPixelFormat, peak::ipl::ConversionMode conversionMode=peak::ipl::ConversionMode::Fast) const
Returns a new created image containing the data of the current image converted to the given pixel for...
Definition: peak_ipl_image.hpp:404
PixelFormatName
Enum holding the possible pixel format names.
Definition: peak_ipl_simple_types.hpp:38
size_t ByteCount() const
Returns the size of the given image in number of bytes.
Definition: peak_ipl_image.hpp:373
PEAK_IPL_C_API PEAK_IPL_Image_Clone(PEAK_IPL_IMAGE_HANDLE imageHandle, PEAK_IPL_IMAGE_HANDLE *outputImageHandle)
Returns a new created image containing the data of the current image as deep copy.
The "peak::ipl" namespace contains the whole image processing library.
Definition: peak_ipl_binning.hpp:16
bool operator!=(const Image &img) const
Checks whether the image is data wise different.
Definition: peak_ipl_image.hpp:480
Definition: peak_ipl_backend.h:59
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
Definition: peak_ipl_exception.hpp:182
Stores the pixel format, width and height of an image and the pointer to the image buffer...
Definition: peak_ipl_image.hpp:37
PEAK_IPL_C_API PEAK_IPL_Image_GetTimestamp(PEAK_IPL_IMAGE_HANDLE imageHandle, uint64_t *timestamp_ns)
Get the timestamp for an image in nanoseconds.
PEAK_IPL_C_API PEAK_IPL_Image_GetHeight(PEAK_IPL_IMAGE_HANDLE imageHandle, size_t *height)
Queries the height of the given image.
ConversionMode
Enum holding the possible conversion modes.
Definition: peak_ipl_simple_types.hpp:28
size_t Height() const
Returns the height.
Definition: peak_ipl_image.hpp:353
PEAK_IPL_C_API PEAK_IPL_Image_GetPixelPointer(PEAK_IPL_IMAGE_HANDLE imageHandle, size_t xPos, size_t yPos, uint8_t **pixelPointer)
Queries the pointer to the given pixel position.
struct PEAK_IPL_IMAGE * PEAK_IPL_IMAGE_HANDLE
Definition: peak_ipl_backend.h:307
PEAK_IPL_C_API PEAK_IPL_Image_ConvertTo(PEAK_IPL_IMAGE_HANDLE imageHandle, PEAK_IPL_PIXEL_FORMAT outputPixelFormat, PEAK_IPL_CONVERSION_MODE conversionMode, PEAK_IPL_IMAGE_HANDLE *outputImageHandle)
Creates a new image containing the data of the current image converted to the given pixel format...
~Image()
Definition: peak_ipl_image.hpp:322
Image Clone() const
Returns a new created image containing the data of the current image as deep copy.
Definition: peak_ipl_image.hpp:430
peak::ipl::PixelFormatName PixelFormatName() const
Returns the name of the pixel format as enum value.
Definition: peak_ipl_pixel_format.hpp:67
uint64_t Timestamp() const
The timestamp in nanoseconds.
Definition: peak_ipl_image.hpp:444
PEAK_IPL_C_API PEAK_IPL_Image_ConstructFromBuffer(PEAK_IPL_PIXEL_FORMAT pixelFormat, uint8_t *buffer, uint64_t bufferSize, size_t width, size_t height, PEAK_IPL_IMAGE_HANDLE *imageHandle)
Creates an image from a given buffer.
PEAK_IPL_C_API PEAK_IPL_Image_GetWidth(PEAK_IPL_IMAGE_HANDLE imageHandle, size_t *width)
Queries the width of the given image.
uint8_t * Data() const
Returns the pointer to the first pixel position in dependency on the size of the pixel format...
Definition: peak_ipl_image.hpp:395
bool operator==(const Image &img) const
Checks whether the image is data wise the same.
Definition: peak_ipl_image.hpp:454
PEAK_IPL_C_API PEAK_IPL_Image_GetByteCount(PEAK_IPL_IMAGE_HANDLE imageHandle, size_t *byteCount)
Queries the size of the given image in number of bytes.
size_t Width() const
Returns the width.
Definition: peak_ipl_image.hpp:344
uint8_t * PixelPointer(size_t xPos, size_t yPos) const
Returns the pixel pointer to the given pixel position in dependency of the size of the pixel format...
Definition: peak_ipl_image.hpp:362
PEAK_IPL_C_API PEAK_IPL_Image_SetTimestamp(PEAK_IPL_IMAGE_HANDLE imageHandle, uint64_t timestamp_ns)
Set the timestamp for an image in nanoseconds.
bool Empty() const
Checks whether the image is empty.
Definition: peak_ipl_image.hpp:439
Represents a pixel format and its specific properties.
Definition: peak_ipl_pixel_format.hpp:31
PEAK_IPL_C_API PEAK_IPL_Image_Construct(PEAK_IPL_PIXEL_FORMAT pixelFormat, size_t width, size_t height, PEAK_IPL_IMAGE_HANDLE *imageHandle)
Creates an image.
peak::ipl::PixelFormat PixelFormat() const
Returns the pixel format.
Definition: peak_ipl_image.hpp:383
PEAK_IPL_C_API PEAK_IPL_Image_Destruct(PEAK_IPL_IMAGE_HANDLE imageHandle)
Destroys the image associated with the given handle.
Image & operator=(const Image &other)=delete