IDS peak IPL  v1.10.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 
129  ~Image();
130 
142  Image(const Image& other);
143 
155  Image& operator=(const Image& other);
156 
165  Image(Image&& other) noexcept;
166 
175  Image& operator=(Image&& other) noexcept;
176 
186  PEAK_IPL_NO_DISCARD size_t Width() const;
187 
197  PEAK_IPL_NO_DISCARD size_t Height() const;
198 
209 
226  PEAK_IPL_NO_DISCARD uint8_t* PixelPointer(size_t xPos, size_t yPos) const;
227 
228 
234  template<typename T, typename = std::enable_if_t<std::is_arithmetic<T>::value>>
235  PEAK_IPL_NO_DISCARD T* PixelPointer(size_t xPos, size_t yPos) const
236  {
237  return reinterpret_cast<T*>(PixelPointer(xPos, yPos));
238  }
239 
249  PEAK_IPL_NO_DISCARD size_t ByteCount() const;
250 
261 
274  PEAK_IPL_NO_DISCARD uint8_t* Data() const;
275 
281  template<typename T, typename = std::enable_if_t<std::is_arithmetic<T>::value>>
282  PEAK_IPL_NO_DISCARD T* Data() const
283  {
284  return reinterpret_cast<T*>(Data());
285  }
286 
303 
322  Image ConvertTo(const peak::ipl::PixelFormat& outputPixelFormat, uint8_t* outputImageBuffer,
323  size_t outputImageBufferSize,
325 
339  template <typename Integer1, typename Integer2,
340  typename std::enable_if_t<std::is_integral<Integer1>::value, int> = 0,
341  typename std::enable_if_t<std::is_integral<Integer2>::value, int> = 0>
342  PEAK_IPL_NO_DISCARD inline Image Scale(Integer1 width, Integer2 height, peak::ipl::InterpolationAlgorithm algorithm=InterpolationAlgorithm::linear) const
343  {
344  return Scale(peak::ipl::Size2D{static_cast<std::size_t>(width), static_cast<std::size_t>(height)}, algorithm);
345  }
346 
360 
375  PEAK_IPL_NO_DISCARD Image Scale(const peak::ipl::ScaleFactor& factor, peak::ipl::InterpolationAlgorithm algorithm=InterpolationAlgorithm::linear) const;
376 
392  template<typename FloatingPoint1, typename FloatingPoint2,
393  typename std::enable_if_t<std::is_floating_point<FloatingPoint1>::value, int> = 0,
394  typename std::enable_if_t<std::is_floating_point<FloatingPoint2>::value, int> = 0>
395  PEAK_IPL_NO_DISCARD inline Image Scale(FloatingPoint1 scaleFactorX, FloatingPoint2 scaleFactorY, peak::ipl::InterpolationAlgorithm algorithm=InterpolationAlgorithm::linear) const
396  {
397  return Scale(peak::ipl::ScaleFactor{static_cast<double>(scaleFactorX), static_cast<double>(scaleFactorY)}, algorithm);
398  }
399 
412  void ScaleInto(const peak::ipl::Size2D& size, uint8_t* buffer, std::size_t bufferSize, peak::ipl::InterpolationAlgorithm algorithm=InterpolationAlgorithm::linear) const;
413 
426  PEAK_IPL_NO_DISCARD Image Crop(const peak::ipl::Point2D& pos, const peak::ipl::Size2D& size) const;
427 
439  PEAK_IPL_NO_DISCARD Image Crop(const peak::ipl::Size2D& size) const;
440 
450  PEAK_IPL_NO_DISCARD Image Clone() const;
451 
461  PEAK_IPL_NO_DISCARD bool Empty() const;
462 
471  PEAK_IPL_NO_DISCARD uint64_t Timestamp() const;
472 
477  PEAK_IPL_NO_DISCARD bool operator==(const Image& img) const;
478 
483  PEAK_IPL_NO_DISCARD bool operator!=(const Image& img) const;
484 
485 private:
486  friend ImageBackendAccessor;
487 
488  explicit Image(PEAK_IPL_IMAGE_HANDLE imageHandle, bool acquireReference=false);
489 
490  PEAK_IPL_IMAGE_HANDLE m_backendHandle{};
491 };
492 
493 } /* namespace ipl */
494 } /* namespace peak */
495 
496 namespace
497 {
498 
500 class ImageBackendAccessor
501 {
502 public:
510  PEAK_IPL_NO_DISCARD static PEAK_IPL_IMAGE_HANDLE BackendHandle(const peak::ipl::Image& image)
511  {
512  return image.m_backendHandle;
513  }
514 
524  PEAK_IPL_NO_DISCARD static peak::ipl::Image CreateImage(PEAK_IPL_IMAGE_HANDLE imageHandle)
525  {
526  return peak::ipl::Image(imageHandle, false);
527  }
528 
538  PEAK_IPL_NO_DISCARD static peak::ipl::Image CreateImageReference(PEAK_IPL_IMAGE_HANDLE imageHandle)
539  {
540  return peak::ipl::Image(imageHandle, true);
541  }
542 };
543 
544 } /* namespace */
545 
546 namespace peak
547 {
548 namespace ipl
549 {
550 inline Image::Image()
551 {
554  });
555 }
556 
557 inline Image::Image(const peak::ipl::PixelFormat& pixelFormat, size_t width, size_t height)
558 {
561  static_cast<PEAK_IPL_PIXEL_FORMAT>(pixelFormat.PixelFormatName()), width, height, &m_backendHandle);
562  });
563 }
564 
566  const peak::ipl::PixelFormat& pixelFormat, uint8_t* buffer, size_t bufferSize, size_t width, size_t height)
567 {
570  static_cast<PEAK_IPL_PIXEL_FORMAT>(pixelFormat.PixelFormatName()), buffer, bufferSize, width, height,
571  &m_backendHandle);
572  });
573 }
574 
575 inline Image::Image(const peak::ipl::PixelFormat& pixelFormat, size_t width, size_t height, uint64_t timestamp_ns) :
576  Image::Image(pixelFormat, width, height)
577 {
579  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_SetTimestamp(m_backendHandle, timestamp_ns);
580  });
581 }
582 
584  const peak::ipl::PixelFormat& pixelFormat, uint8_t* buffer, size_t bufferSize, size_t width, size_t height, uint64_t timestamp_ns) :
585  Image::Image(pixelFormat, buffer, bufferSize, width, height)
586 {
588  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_SetTimestamp(m_backendHandle, timestamp_ns);
589  });
590 }
591 
592 inline Image::Image(PEAK_IPL_IMAGE_HANDLE imageHandle, bool acquireReference)
593  : m_backendHandle(imageHandle)
594 {
595  if (acquireReference && m_backendHandle)
596  {
598  }
599 }
600 
602 {
603  if (m_backendHandle)
604  {
605  (void)PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Destruct(m_backendHandle);
606  }
607 }
608 
609 inline Image::Image(const Image& other)
610 {
611  if (other.m_backendHandle)
612  {
613  ExecuteAndMapReturnCodes([&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Acquire(other.m_backendHandle); });
614  }
615 
616  m_backendHandle = other.m_backendHandle;
617 }
618 
619 inline Image& Image::operator=(const Image& other)
620 {
621  if (this != &other)
622  {
623  if (m_backendHandle)
624  {
625  (void)PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Destruct(m_backendHandle);
626  }
627 
628  if (other.m_backendHandle)
629  {
630  ExecuteAndMapReturnCodes([&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Acquire(other.m_backendHandle); });
631  }
632  }
633 
634  m_backendHandle = other.m_backendHandle;
635 
636  return *this;
637 }
638 
639 inline Image::Image(Image&& other) noexcept
640 {
641  *this = std::move(other);
642 }
643 
644 inline Image& Image::operator=(Image&& other) noexcept
645 {
646  if (this != &other)
647  {
648  if (m_backendHandle != nullptr)
649  {
650  (void)PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Destruct(m_backendHandle);
651  }
652  m_backendHandle = other.m_backendHandle;
653  other.m_backendHandle = nullptr;
654  }
655 
656  return *this;
657 }
658 
659 inline size_t Image::Width() const
660 {
661  size_t width = 0;
662 
663  ExecuteAndMapReturnCodes([&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetWidth(m_backendHandle, &width); });
664 
665  return width;
666 }
667 
668 inline size_t Image::Height() const
669 {
670  size_t height = 0;
671 
672  ExecuteAndMapReturnCodes([&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetHeight(m_backendHandle, &height); });
673 
674  return height;
675 }
676 
677 inline peak::ipl::Size2D Image::Size() const
678 {
679  return {Width(), Height()};
680 }
681 
682 inline uint8_t* Image::PixelPointer(size_t xPos, size_t yPos) const
683 {
684  uint8_t* pixelPosition = nullptr;
685 
687  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetPixelPointer(m_backendHandle, xPos, yPos, &pixelPosition);
688  });
689 
690  return pixelPosition;
691 }
692 
693 inline size_t Image::ByteCount() const
694 {
695  size_t byteCount = 0;
696 
698  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetByteCount(m_backendHandle, &byteCount); });
699 
700  return byteCount;
701 }
702 
703 inline peak::ipl::PixelFormat Image::PixelFormat() const
704 {
706 
709  m_backendHandle, reinterpret_cast<PEAK_IPL_PIXEL_FORMAT*>(&pixelFormatName));
710  });
711 
712  return peak::ipl::PixelFormat{ pixelFormatName };
713 }
714 
715 inline uint8_t* Image::Data() const
716 {
717  uint8_t* data = nullptr;
718 
719  ExecuteAndMapReturnCodes([&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetData(m_backendHandle, &data); });
720 
721  return data;
722 }
723 
725  const peak::ipl::PixelFormat& outputPixelFormat, peak::ipl::ConversionMode conversionMode) const
726 {
727  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
729  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_ConvertTo(m_backendHandle,
730  static_cast<PEAK_IPL_PIXEL_FORMAT>(outputPixelFormat.PixelFormatName()),
731  static_cast<PEAK_IPL_CONVERSION_MODE>(conversionMode), &outputImageHandle);
732  });
733 
734  return ImageBackendAccessor::CreateImage(outputImageHandle);
735 }
736 
737 inline Image Image::ConvertTo(const peak::ipl::PixelFormat& outputPixelFormat, uint8_t* outputImageBuffer,
738  size_t outputImageBufferSize, peak::ipl::ConversionMode conversionMode) const
739 {
740  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
743  static_cast<PEAK_IPL_PIXEL_FORMAT>(outputPixelFormat.PixelFormatName()), outputImageBuffer,
744  outputImageBufferSize, static_cast<PEAK_IPL_CONVERSION_MODE>(conversionMode), &outputImageHandle);
745  });
746 
747  return ImageBackendAccessor::CreateImage(outputImageHandle);
748 }
749 
750 inline Image Image::Scale(const Size2D& size, peak::ipl::InterpolationAlgorithm algorithm) const
751 {
752  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
753  const PEAK_IPL_SIZE_2D size_in{size.width, size.height};
754 
756  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Scale(m_backendHandle, &size_in, static_cast<PEAK_IPL_INTERPOLATION_ALGORITHM>(algorithm), &outputImageHandle);
757  });
758 
759  return ImageBackendAccessor::CreateImage(outputImageHandle);
760 }
761 
762 inline Image Image::Scale(const ScaleFactor& factor, peak::ipl::InterpolationAlgorithm algorithm) const
763 {
764  return Scale(Size2D{static_cast<size_t>(static_cast<double>(Width()) * factor.x), static_cast<size_t>(static_cast<double>(Height()) * factor.y)}, algorithm);
765 }
766 
767 inline void Image::ScaleInto(const Size2D& size, uint8_t* buffer, std::size_t bufferSize, peak::ipl::InterpolationAlgorithm algorithm) const
768 {
769  const PEAK_IPL_SIZE_2D size_in{size.width, size.height};
770 
772  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_ScaleInto(m_backendHandle, &size_in, buffer, bufferSize, static_cast<PEAK_IPL_INTERPOLATION_ALGORITHM>(algorithm));
773  });
774 }
775 
776 
777 inline Image Image::Crop(const Point2D& pos, const Size2D& size) const
778 {
779  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
780  const PEAK_IPL_POINT_2D pos_in{pos.x, pos.y};
781  const PEAK_IPL_SIZE_2D size_in{size.width, size.height};
782 
784  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Crop(m_backendHandle, &pos_in, &size_in, &outputImageHandle);
785  });
786 
787  return ImageBackendAccessor::CreateImage(outputImageHandle);
788 }
789 
790 inline Image Image::Crop(const Size2D& size) const
791 {
792  return Crop({0,0}, size);
793 }
794 
795 inline Image Image::Clone() const
796 {
797  PEAK_IPL_IMAGE_HANDLE outputImageHandle = nullptr;
799  [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_Clone(m_backendHandle, &outputImageHandle); });
800 
801  return ImageBackendAccessor::CreateImage(outputImageHandle);
802 }
803 
804 inline bool Image::Empty() const
805 {
806  return (m_backendHandle == nullptr) || (PixelFormat().PixelFormatName() == PixelFormatName::Invalid);
807 }
808 
809 inline uint64_t Image::Timestamp() const
810 {
811  uint64_t timestamp_ns;
813  return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_Image_GetTimestamp(m_backendHandle, &timestamp_ns);
814  });
815 
816  return timestamp_ns;
817 }
818 
819 inline bool Image::operator==(const peak::ipl::Image& img) const
820 {
821  if (img.ByteCount() != this->ByteCount())
822  {
823  return false;
824  }
825  if (img.Width() != this->Width())
826  {
827  return false;
828  }
829  if (img.Height() != this->Height())
830  {
831  return false;
832  }
833  if (img.PixelFormat().PixelFormatName() != this->PixelFormat().PixelFormatName())
834  {
835  return false;
836  }
837  if (img.Timestamp() != this->Timestamp())
838  {
839  return false;
840  }
841 
842  return memcmp(img.Data(), this->Data(), img.ByteCount()) == 0;
843 }
844 
845 inline bool Image::operator!=(const peak::ipl::Image& img) const
846 {
847  return !(*this == img);
848 }
849 
850 } /* namespace ipl */
851 } /* namespace peak */
#define PEAK_IPL_NO_DISCARD
Definition: peak_ipl_backend.h:26
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...
PEAK_IPL_C_API PEAK_IPL_Image_Scale(PEAK_IPL_IMAGE_HANDLE imageHandle, const struct PEAK_IPL_SIZE_2D *size, PEAK_IPL_INTERPOLATION_ALGORITHM algorithm, PEAK_IPL_IMAGE_HANDLE *outputImageHandle)
Creates a new image scaled to size using algorithm.
#define PEAK_IPL_C_ABI_PREFIX
Definition: peak_ipl_dll_defines.h:16
Image()
Creates an empty image. In other words, size is 0x0 and an invalid pixel format.
Definition: peak_ipl_image.hpp:550
size_t height
Height of size.
Definition: peak_ipl_simple_types.hpp:210
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
size_t x
Definition: peak_ipl_backend.h:267
PixelFormatName
Enum holding the possible pixel format names.
Definition: peak_ipl_simple_types.hpp:38
Definition: peak_ipl_backend.h:75
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
Definition: peak_ipl_exception.hpp:182
2D size of an image.
Definition: peak_ipl_simple_types.hpp:207
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.
bool operator!=(const Sharpness::SharpnessROI &lhs, const Sharpness::SharpnessROI &rhs)
Definition: peak_ipl_sharpness.hpp:425
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_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:361
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()
Destructor for the Image instance.
Definition: peak_ipl_image.hpp:601
Image & operator=(const Image &other)
Copy assigment for Image instance.
Definition: peak_ipl_image.hpp:619
ConversionMode
Enum holding the possible conversion modes.
Definition: peak_ipl_simple_types.hpp:28
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_Acquire(PEAK_IPL_IMAGE_HANDLE imageHandle)
Increase the reference count of an image object.
PEAK_IPL_C_API PEAK_IPL_Image_GetWidth(PEAK_IPL_IMAGE_HANDLE imageHandle, size_t *width)
Queries the width of the given image.
InterpolationAlgorithm
Enum holding the possible interpolation algorithm.
Definition: peak_ipl_simple_types.hpp:193
bool operator==(const Sharpness::SharpnessROI &lhs, const Sharpness::SharpnessROI &rhs)
Definition: peak_ipl_sharpness.hpp:419
PEAK_IPL_C_API PEAK_IPL_Image_ScaleInto(PEAK_IPL_IMAGE_HANDLE imageHandle, const struct 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.
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.
Image ConvertTo(const peak::ipl::PixelFormat &outputPixelFormat, uint8_t *outputImageBuffer, size_t outputImageBufferSize, peak::ipl::ConversionMode conversionMode=peak::ipl::ConversionMode::Fast) const
Saves the data of the current image converted to the given pixel format into a destination buffer and...
Definition: peak_ipl_image.hpp:724
size_t width
Width of size.
Definition: peak_ipl_simple_types.hpp:209
Definition: peak_ipl_backend.h:265
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:271
PEAK_IPL_C_API PEAK_IPL_Image_Crop(PEAK_IPL_IMAGE_HANDLE imageHandle, const struct PEAK_IPL_POINT_2D *pos, const struct PEAK_IPL_SIZE_2D *size, PEAK_IPL_IMAGE_HANDLE *outputImageHandle)
Creates a new image cropped to size starting at position.
Fast conversion mode.
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_C_API PEAK_IPL_Image_Destruct(PEAK_IPL_IMAGE_HANDLE imageHandle)
Destroys the image associated with the given handle.