IDS peak IPL  v1.7.0
peak_ipl_image.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <utility>
21 #include <type_traits>
22 
23 namespace
24 {
25 class ImageBackendAccessor;
26 }
27 
31 namespace peak
32 {
33 namespace ipl
34 {
35 
39 class Image
40 {
41 public:
49  Image();
50 
63  Image(const peak::ipl::PixelFormat& pixelFormat, size_t width, size_t height);
64 
83  Image(
84  const peak::ipl::PixelFormat& pixelFormat, uint8_t* buffer, size_t bufferSize, size_t width, size_t height);
85 
99  Image(const peak::ipl::PixelFormat& pixelFormat, size_t width, size_t height, uint64_t timestamp_ns);
100 
121  Image(const peak::ipl::PixelFormat& pixelFormat, uint8_t* buffer, size_t bufferSize, size_t width, size_t height, uint64_t timestamp_ns);
122 
123  ~Image();
124 
125  Image(const Image& other) = delete;
126  Image& operator=(const Image& other) = delete;
127 
128  Image(Image&& other) noexcept;
129  Image& operator=(Image&& other) noexcept;
130 
140  size_t Width() const;
141 
151  size_t Height() const;
152 
162  peak::ipl::Size2D Size() const;
163 
177  uint8_t* PixelPointer(size_t xPos, size_t yPos) const;
178 
188  size_t ByteCount() const;
189 
200 
210  uint8_t* Data() const;
211 
226  Image ConvertTo(const peak::ipl::PixelFormat& outputPixelFormat,
228 
247  Image ConvertTo(const peak::ipl::PixelFormat& outputPixelFormat, uint8_t* outputImageBuffer,
248  size_t outputImageBufferSize,
250 
264  template <typename Integer1, typename Integer2,
265  typename std::enable_if_t<std::is_integral<Integer1>::value, int> = 0,
266  typename std::enable_if_t<std::is_integral<Integer2>::value, int> = 0>
267  inline Image Scale(Integer1 width, Integer2 height, peak::ipl::InterpolationAlgorithm algorithm=InterpolationAlgorithm::linear) const
268  {
269  return Scale(peak::ipl::Size2D{static_cast<std::size_t>(width), static_cast<std::size_t>(height)}, algorithm);
270  }
271 
285 
301 
317  template<typename FloatingPoint1, typename FloatingPoint2,
318  typename std::enable_if_t<std::is_floating_point<FloatingPoint1>::value, int> = 0,
319  typename std::enable_if_t<std::is_floating_point<FloatingPoint2>::value, int> = 0>
320  inline Image Scale(FloatingPoint1 scaleFactorX, FloatingPoint2 scaleFactorY, peak::ipl::InterpolationAlgorithm algorithm=InterpolationAlgorithm::linear) const
321  {
322  return Scale(peak::ipl::ScaleFactor{static_cast<double>(scaleFactorX), static_cast<double>(scaleFactorY)}, algorithm);
323  }
324 
339  void ScaleInto(const peak::ipl::Size2D& size, uint8_t* buffer, std::size_t bufferSize, peak::ipl::InterpolationAlgorithm algorithm=InterpolationAlgorithm::linear) const;
340 
353  Image Crop(const peak::ipl::Point2D& pos, const peak::ipl::Size2D& size) const;
354 
366  Image Crop(const peak::ipl::Size2D& size) const;
367 
377  Image Clone() const;
378 
388  bool Empty() const;
389 
398  uint64_t Timestamp() const;
399 
403  bool operator==(const Image& img) const;
404 
408  bool operator!=(const Image& img) const;
409 
410 private:
411  friend ImageBackendAccessor;
412  explicit Image(PEAK_IPL_IMAGE_HANDLE imageHandle) noexcept;
413  PEAK_IPL_IMAGE_HANDLE m_backendHandle{};
414 };
415 
416 } /* namespace ipl */
417 } /* namespace peak */
418 
419 namespace
420 {
421 
422 // helper class to access the C-backend of the Image, which are hidden from the public interface
423 class ImageBackendAccessor
424 {
425 public:
426  static PEAK_IPL_IMAGE_HANDLE BackendHandle(const peak::ipl::Image& image)
427  {
428  return image.m_backendHandle;
429  }
430  static peak::ipl::Image CreateImage(PEAK_IPL_IMAGE_HANDLE imageHandle)
431  {
432  return peak::ipl::Image(imageHandle);
433  }
434 };
435 
436 } /* namespace */
437 
438 namespace peak
439 {
440 namespace ipl
441 {
442 inline Image::Image()
443 {
446  });
447 }
448 
449 inline Image::Image(const peak::ipl::PixelFormat& pixelFormat, size_t width, size_t height)
450 {
453  static_cast<PEAK_IPL_PIXEL_FORMAT>(pixelFormat.PixelFormatName()), width, height, &m_backendHandle);
454  });
455 }
456 
458  const peak::ipl::PixelFormat& pixelFormat, uint8_t* buffer, size_t bufferSize, size_t width, size_t height)
459 {
462  static_cast<PEAK_IPL_PIXEL_FORMAT>(pixelFormat.PixelFormatName()), buffer, bufferSize, width, height,
463  &m_backendHandle);
464  });
465 }
466 
467 inline Image::Image(const peak::ipl::PixelFormat& pixelFormat, size_t width, size_t height, uint64_t timestamp_ns) :
468  Image::Image(pixelFormat, width, height)
469 {
471  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_SetTimestamp(m_backendHandle, timestamp_ns);
472  });
473 }
474 
476  const peak::ipl::PixelFormat& pixelFormat, uint8_t* buffer, size_t bufferSize, size_t width, size_t height, uint64_t timestamp_ns) :
477  Image::Image(pixelFormat, buffer, bufferSize, width, height)
478 {
480  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_SetTimestamp(m_backendHandle, timestamp_ns);
481  });
482 }
483 
484 inline Image::Image(PEAK_IPL_IMAGE_HANDLE imageHandle) noexcept
485  : m_backendHandle(imageHandle)
486 {}
487 
489 {
490  if (m_backendHandle)
491  {
492  (void)PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Destruct(m_backendHandle);
493  }
494 }
495 
496 inline Image::Image(Image&& other) noexcept
497 {
498  *this = std::move(other);
499 }
500 
501 inline Image& Image::operator=(Image&& other) noexcept
502 {
503  if (this != &other)
504  {
505  (void)PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Destruct(m_backendHandle);
506  m_backendHandle = other.m_backendHandle;
507  other.m_backendHandle = nullptr;
508  }
509 
510  return *this;
511 }
512 
513 inline size_t Image::Width() const
514 {
515  size_t width = 0;
516 
517  ExecuteAndMapReturnCodes([&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetWidth(m_backendHandle, &width); });
518 
519  return width;
520 }
521 
522 inline size_t Image::Height() const
523 {
524  size_t height = 0;
525 
526  ExecuteAndMapReturnCodes([&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetHeight(m_backendHandle, &height); });
527 
528  return height;
529 }
530 
532 {
533  return {Width(), Height()};
534 }
535 
536 inline uint8_t* Image::PixelPointer(size_t xPos, size_t yPos) const
537 {
538  uint8_t* pixelPosition = nullptr;
539 
541  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetPixelPointer(m_backendHandle, xPos, yPos, &pixelPosition);
542  });
543 
544  return pixelPosition;
545 }
546 
547 inline size_t Image::ByteCount() const
548 {
549  size_t byteCount = 0;
550 
552  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetByteCount(m_backendHandle, &byteCount); });
553 
554  return byteCount;
555 }
556 
558 {
560 
563  m_backendHandle, reinterpret_cast<PEAK_IPL_PIXEL_FORMAT*>(&pixelFormatName));
564  });
565 
566  return peak::ipl::PixelFormat{ pixelFormatName };
567 }
568 
569 inline uint8_t* Image::Data() const
570 {
571  uint8_t* data = nullptr;
572 
573  ExecuteAndMapReturnCodes([&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetData(m_backendHandle, &data); });
574 
575  return data;
576 }
577 
579  const peak::ipl::PixelFormat& outputPixelFormat, peak::ipl::ConversionMode conversionMode) const
580 {
581  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
583  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_ConvertTo(m_backendHandle,
584  static_cast<PEAK_IPL_PIXEL_FORMAT>(outputPixelFormat.PixelFormatName()),
585  static_cast<PEAK_IPL_CONVERSION_MODE>(conversionMode), &outputImageHandle);
586  });
587 
588  return ImageBackendAccessor::CreateImage(outputImageHandle);
589 }
590 
591 inline Image Image::ConvertTo(const peak::ipl::PixelFormat& outputPixelFormat, uint8_t* outputImageBuffer,
592  size_t outputImageBufferSize, peak::ipl::ConversionMode conversionMode) const
593 {
594  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
597  static_cast<PEAK_IPL_PIXEL_FORMAT>(outputPixelFormat.PixelFormatName()), outputImageBuffer,
598  outputImageBufferSize, static_cast<PEAK_IPL_CONVERSION_MODE>(conversionMode), &outputImageHandle);
599  });
600 
601  return ImageBackendAccessor::CreateImage(outputImageHandle);
602 }
603 
604 inline Image Image::Scale(const Size2D& size, peak::ipl::InterpolationAlgorithm algorithm) const
605 {
606  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
607  const PEAK_IPL_SIZE_2D size_in{size.width, size.height};
608 
610  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Scale(m_backendHandle, &size_in, static_cast<PEAK_IPL_INTERPOLATION_ALGORITHM>(algorithm), &outputImageHandle);
611  });
612 
613  return ImageBackendAccessor::CreateImage(outputImageHandle);
614 }
615 
616 inline Image Image::Scale(const ScaleFactor& factor, peak::ipl::InterpolationAlgorithm algorithm) const
617 {
618  return Scale(Size2D{static_cast<size_t>(static_cast<double>(Width()) * factor.x), static_cast<size_t>(static_cast<double>(Height()) * factor.y)}, algorithm);
619 }
620 
621 inline void Image::ScaleInto(const Size2D& size, uint8_t* buffer, std::size_t bufferSize, peak::ipl::InterpolationAlgorithm algorithm) const
622 {
623  const PEAK_IPL_SIZE_2D size_in{size.width, size.height};
624 
626  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_ScaleInto(m_backendHandle, &size_in, buffer, bufferSize, static_cast<PEAK_IPL_INTERPOLATION_ALGORITHM>(algorithm));
627  });
628 }
629 
630 
631 inline Image Image::Crop(const Point2D& pos, const Size2D& size) const
632 {
633  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
634  const PEAK_IPL_POINT_2D pos_in{pos.x, pos.y};
635  const PEAK_IPL_SIZE_2D size_in{size.width, size.height};
636 
638  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Crop(m_backendHandle, &pos_in, &size_in, &outputImageHandle);
639  });
640 
641  return ImageBackendAccessor::CreateImage(outputImageHandle);
642 }
643 
644 inline Image Image::Crop(const Size2D& size) const
645 {
646  return Crop({0,0}, size);
647 }
648 
649 inline Image Image::Clone() const
650 {
651  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
653  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Clone(m_backendHandle, &outputImageHandle); });
654 
655  return ImageBackendAccessor::CreateImage(outputImageHandle);
656 }
657 
658 inline bool Image::Empty() const
659 {
660  return (m_backendHandle == nullptr) || (PixelFormat().PixelFormatName() == PixelFormatName::Invalid);
661 }
662 
663 inline uint64_t Image::Timestamp() const
664 {
665  uint64_t timestamp_ns;
667  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetTimestamp(m_backendHandle, &timestamp_ns);
668  });
669 
670  return timestamp_ns;
671 }
672 
673 inline bool Image::operator==(const peak::ipl::Image& img) const
674 {
675  if (img.ByteCount() != this->ByteCount())
676  {
677  return false;
678  }
679  if (img.Width() != this->Width())
680  {
681  return false;
682  }
683  if (img.Height() != this->Height())
684  {
685  return false;
686  }
687  if (img.PixelFormat().PixelFormatName() != this->PixelFormat().PixelFormatName())
688  {
689  return false;
690  }
691  if (img.Timestamp() != this->Timestamp())
692  {
693  return false;
694  }
695 
696  return memcmp(img.Data(), this->Data(), img.ByteCount()) == 0;
697 }
698 
699 inline bool Image::operator!=(const peak::ipl::Image& img) const
700 {
701  return !(*this == img);
702 }
703 
704 } /* namespace ipl */
705 } /* 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.
void ScaleInto(const peak::ipl::Size2D &size, uint8_t *buffer, std::size_t bufferSize, peak::ipl::InterpolationAlgorithm algorithm=InterpolationAlgorithm::linear) const
Scale the image to the supplied width and height using the supplied algorithm and save it into the su...
Definition: peak_ipl_image.hpp:621
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:442
size_t height
Definition: peak_ipl_simple_types.hpp:180
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:578
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:547
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:22
bool operator!=(const Image &img) const
Checks whether the image is data wise different.
Definition: peak_ipl_image.hpp:699
Definition: peak_ipl_backend.h:59
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
Definition: peak_ipl_exception.hpp:182
size_t x
Definition: peak_ipl_simple_types.hpp:197
2D size of an image.
Definition: peak_ipl_simple_types.hpp:177
Stores the pixel format, width and height of an image and the pointer to the image buffer...
Definition: peak_ipl_image.hpp:39
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.
double x
Definition: peak_ipl_simple_types.hpp:216
PEAK_IPL_C_API PEAK_IPL_Image_GetHeight(PEAK_IPL_IMAGE_HANDLE imageHandle, size_t *height)
Queries the height of the given image.
PEAK_IPL_C_API PEAK_IPL_Image_Scale(PEAK_IPL_IMAGE_HANDLE imageHandle, const PEAK_IPL_SIZE_2D *size, PEAK_IPL_INTERPOLATION_ALGORITHM algorithm, PEAK_IPL_IMAGE_HANDLE *outputImageHandle)
Creates a new image scaled to size using algorithm.
ConversionMode
Enum holding the possible conversion modes.
Definition: peak_ipl_simple_types.hpp:28
2D position in an image.
Definition: peak_ipl_simple_types.hpp:195
size_t Height() const
Returns the height.
Definition: peak_ipl_image.hpp:522
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.
PEAK_IPL_C_API PEAK_IPL_Image_ScaleInto(PEAK_IPL_IMAGE_HANDLE imageHandle, const PEAK_IPL_SIZE_2D *size, uint8_t *buffer, size_t bufferSize, PEAK_IPL_INTERPOLATION_ALGORITHM algorithm)
Creates a new image scaled to size using algorithm into the supplied buffer.
struct PEAK_IPL_IMAGE * PEAK_IPL_IMAGE_HANDLE
Definition: peak_ipl_backend.h:326
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:488
Image Clone() const
Returns a new created image containing the data of the current image as deep copy.
Definition: peak_ipl_image.hpp:649
peak::ipl::PixelFormatName PixelFormatName() const
Returns the name of the pixel format as enum value.
Definition: peak_ipl_pixel_format.hpp:67
double y
Definition: peak_ipl_simple_types.hpp:217
Scale Factors of an image.
Definition: peak_ipl_simple_types.hpp:214
uint64_t Timestamp() const
The timestamp in nanoseconds.
Definition: peak_ipl_image.hpp:663
peak::ipl::Size2D Size() const
Returns the Size.
Definition: peak_ipl_image.hpp:531
Image Scale(FloatingPoint1 scaleFactorX, FloatingPoint2 scaleFactorY, peak::ipl::InterpolationAlgorithm algorithm=InterpolationAlgorithm::linear) const
Scale the image to the supplied scale factors scaleFactorX and scaleFactorY using the supplied algori...
Definition: peak_ipl_image.hpp:320
Image Scale(Integer1 width, Integer2 height, peak::ipl::InterpolationAlgorithm algorithm=InterpolationAlgorithm::linear) const
Scale the image to the supplied width and height using the supplied algorithm.
Definition: peak_ipl_image.hpp:267
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:569
InterpolationAlgorithm
Enum holding the possible interpolation algorithm.
Definition: peak_ipl_simple_types.hpp:163
bool operator==(const Image &img) const
Checks whether the image is data wise the same.
Definition: peak_ipl_image.hpp:673
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:513
Image Crop(const peak::ipl::Point2D &pos, const peak::ipl::Size2D &size) const
Crop the image to the size starting at pos.
Definition: peak_ipl_image.hpp:631
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:536
size_t width
Definition: peak_ipl_simple_types.hpp:179
Definition: peak_ipl_backend.h:230
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.
Definition: peak_ipl_backend.h:236
bool Empty() const
Checks whether the image is empty.
Definition: peak_ipl_image.hpp:658
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:557
PEAK_IPL_C_API PEAK_IPL_Image_Crop(PEAK_IPL_IMAGE_HANDLE imageHandle, const PEAK_IPL_POINT_2D *pos, const PEAK_IPL_SIZE_2D *size, PEAK_IPL_IMAGE_HANDLE *outputImageHandle)
Creates a new image cropped to size starting at position.
PEAK_IPL_C_API PEAK_IPL_Image_Destruct(PEAK_IPL_IMAGE_HANDLE imageHandle)
Destroys the image associated with the given handle.
size_t y
Definition: peak_ipl_simple_types.hpp:198
Image & operator=(const Image &other)=delete