IDS peak ICV v1.1.0
Loading...
Searching...
No Matches
peak_icv_image.ipp
Go to the documentation of this file.
1
11#pragma once
12
16#include <algorithm>
17#include <cstring>
18#include <utility>
19
20namespace peak
21{
22namespace icv
23{
24inline peak_icv_image_handle Image::CreateFromFile(const std::string& path)
25{
26 peak_icv_image_handle handle{};
28 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_CreateFromFile(&handle, path.c_str());
29 });
30 return handle;
31}
32
33inline peak_icv_image_handle Image::CreateFromFile(const std::string& path, peak::common::PixelFormat name)
34{
35 peak_icv_image_handle handle{};
37 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_CreateFromFileWithPixelFormat(
38 &handle, path.c_str(), static_cast<peak_common_pixel_format>(name));
39 });
40 return handle;
41}
42
43inline peak_icv_image_info Image::CreateFromHandle(peak_icv_image_handle handle)
44{
45 peak_icv_image_info imageInfo{};
47 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_GetInfo(handle, &imageInfo, sizeof(imageInfo));
48 });
49
50 return imageInfo;
51}
52
53inline peak_icv_image_handle Image::CreateFromSize(peak::common::PixelFormat pixelFormat, const peak::common::Size& size)
54{
55 peak_icv_image_handle handle{};
57 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_Create(
58 &handle, static_cast<peak_common_pixel_format>(pixelFormat), { size.GetWidth(), size.GetHeight() });
59 });
60 return handle;
61}
62
63inline peak_icv_image_handle Image::CreateFromBuffer(
64 peak::common::PixelFormat pixelFormat, const peak::common::Size& size, const uint8_t* buffer, size_t bufferSize)
65{
66 peak_icv_image_handle handle{};
68 peak_icv_image_info info{ static_cast<peak_common_pixel_format>(pixelFormat), { size.GetWidth(), size.GetHeight() }, bufferSize,
69 const_cast<uint8_t*>(buffer) };
70 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_CreateFromImageInfo(&handle, info, sizeof(info));
71 });
72 return handle;
73}
74
75inline peak::common::Metadata Image::AddCaptureInformationDataToMetadata(
76 peak_icv_capture_information captureInformationC, peak::common::Metadata metadata)
77{
78 metadata.SetValueByKey<peak::common::MetadataKey::DeviceTimestamp>(captureInformationC.relative_timestamp);
79 metadata.SetValueByKey<peak::common::MetadataKey::BinningHorizontal>(captureInformationC.binning_factor.x);
80 metadata.SetValueByKey<peak::common::MetadataKey::BinningVertical>(captureInformationC.binning_factor.y);
82 peak::common::RectangleU{ captureInformationC.region_of_interest.x, captureInformationC.region_of_interest.y,
83 captureInformationC.region_of_interest.width, captureInformationC.region_of_interest.height });
84 return metadata;
85}
86
87inline Image::Image(peak_icv_image_handle handle, peak_icv_image_info info)
88 : m_imageHandle{ handle }
89 , m_size{ info.size.width, info.size.height }
90 , m_buffer{ info.buffer }
91 , m_pixelFormatInfo{ static_cast<peak::common::PixelFormat>(info.pixelFormat) }
92{}
93
94#ifdef WITH_peak_ipl
95inline Image::Image(const peak::ipl::Image& image)
96 : Image(CreateFromBuffer(static_cast<peak::common::PixelFormat>(image.PixelFormat().PixelFormatName()),
97 peak::common::Size{ static_cast<uint32_t>(image.Width()), static_cast<uint32_t>(image.Height()) }, image.Data(),
98 image.ByteCount()))
99{
100 peak_icv_capture_information captureInformationC{ { 1, 1 }, image.Timestamp(), {}, {} };
101 detail::ExecuteAndMapReturnCodes([&] {
102 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_SetCaptureInformation(m_imageHandle, captureInformationC, sizeof(captureInformationC));
103 });
104
105 m_metaData = std::make_unique<peak::common::Metadata>(
106 AddCaptureInformationDataToMetadata(captureInformationC, peak::common::Metadata{}));
107 m_image = image;
108}
109#endif
110
111inline Image::Image(const peak::common::IImageView& imageView)
112 : Image(CreateFromBuffer(imageView.GetPixelFormat(), imageView.GetSize(), imageView.GetData(), imageView.GetSizeInBytes()))
113{
114 const auto metadata = imageView.GetMetadata();
115 SetMetadata(*metadata);
116}
117
118inline Image::Image(const std::string& path)
119 : Image{ CreateFromFile(path) }
120{}
121
122inline Image::Image(const std::string& path, peak::common::PixelFormat name)
123 : Image{ CreateFromFile(path, name) }
124{}
125
126inline Image::Image(peak::common::PixelFormat pixelFormat, uint8_t* buffer, size_t bufferSize, const peak::common::Size& imageSize)
127 : Image{ CreateFromBuffer(pixelFormat, imageSize, buffer, bufferSize),
128 { static_cast<peak_common_pixel_format>(pixelFormat), { imageSize.GetWidth(), imageSize.GetHeight() }, bufferSize, buffer } }
129{}
130
131inline Image::Image(peak::common::PixelFormat pixelFormat, const peak::common::Size& imageSize)
132 : Image{ CreateFromSize(pixelFormat, imageSize) }
133{}
134
135inline Image::Image(peak_icv_image_handle imageHandle)
136 : Image{ imageHandle, CreateFromHandle(imageHandle) }
137{}
138
139inline Image::Image(const Image& other)
140 : Image{ other.m_imageHandle, CreateFromHandle(other.m_imageHandle) }
141{
143 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_IncreaseUseCount(other.m_imageHandle);
144 });
145#ifdef WITH_peak_ipl
146 m_image = other.m_image;
147#endif
148}
149
150inline Image::Image(Image&& other) noexcept
151 : Image{ other.m_imageHandle, CreateFromHandle(other.m_imageHandle) }
152{
153#ifdef WITH_peak_ipl
154 m_image = std::move(other.m_image);
155#endif
156 other.m_imageHandle = nullptr;
157}
158
159inline Image& Image::operator=(const Image& other)
160{
161 if (this->m_imageHandle != other.m_imageHandle)
162 {
163 if (m_imageHandle)
164 {
166 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_Destroy(m_imageHandle);
167 });
168 }
169
171 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_IncreaseUseCount(other.m_imageHandle);
172 });
173 m_imageHandle = other.m_imageHandle;
174 m_size = other.m_size;
175 m_pixelFormatInfo = other.m_pixelFormatInfo;
176 m_buffer = other.m_buffer;
177#ifdef WITH_peak_ipl
178 m_image = other.m_image;
179#endif
180 }
181
182 return *this;
183}
184
185inline Image& Image::operator=(Image&& other) noexcept
186{
187 if (this->m_imageHandle != other.m_imageHandle)
188 {
189 if (m_imageHandle)
190 {
192 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_Destroy(m_imageHandle);
193 });
194 }
195
196 m_imageHandle = std::exchange(other.m_imageHandle, nullptr);
197 m_size = other.m_size;
198 m_pixelFormatInfo = other.m_pixelFormatInfo;
199 m_buffer = std::exchange(other.m_buffer, nullptr);
200#ifdef WITH_peak_ipl
201 m_image = std::move(other.m_image);
202#endif
203 }
204
205 return *this;
206}
207
209{
210 if (m_imageHandle != nullptr)
211 {
212 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_Image_Destroy(m_imageHandle);
213 }
214}
215
216inline bool Image::operator==(const Image& rhs) const
217{
218 if (rhs.GetSizeInBytes() != this->GetSizeInBytes())
219 {
220 return false;
221 }
222 if (rhs.GetSize() != this->GetSize())
223 {
224 return false;
225 }
226 if (rhs.GetPixelFormat() != this->GetPixelFormat())
227 {
228 return false;
229 }
230 if (rhs.GetRegion() != this->GetRegion())
231 {
232 return false;
233 }
234 bool isSame{};
236 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_Compare(
237 m_imageHandle, peak::common::detail::BackendAccessor<Image>::BackendHandle(rhs), &isSame);
238 });
239 return isSame;
240}
241
242inline Image Image::Copy() const
243{
244 Image tmpImg(m_pixelFormatInfo.GetPixelFormat(), m_size);
245#if _MSC_VER
246 memcpy_s(tmpImg.GetData(), tmpImg.GetSizeInBytes(), GetData(), GetSizeInBytes());
247#else
248 std::memcpy(tmpImg.GetData(), GetData(), GetSizeInBytes());
249#endif
250 return tmpImg;
251}
252
254{
255 return m_size;
256}
257
258inline uint8_t* Image::GetData() const
259{
260 return m_buffer;
261}
262
263inline size_t Image::GetSizeInBytes() const
264{
265 return m_pixelFormatInfo.GetSizeInBytes(m_size);
266}
267
268inline size_t Image::GetBytesPerLine() const
269{
270 return GetSizeInBytes() / m_size.GetHeight();
271}
272
273inline detail::handle_of_t<Image> Image::GetHandle() const
274{
275 return m_imageHandle;
276}
277
279{
280 return m_pixelFormatInfo.GetPixelFormat();
281}
282
283#ifdef WITH_peak_ipl
284inline peak::ipl::Image Image::GetIPLImage() const
285{
286 if (!m_image.Empty())
287 {
288 return m_image;
289 }
290
291 PEAK_IPL_IMAGE_HANDLE imageHandle{};
292
294 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_GetIPLImage(m_imageHandle, &imageHandle);
295 });
296
297 return ImageBackendAccessor::CreateImage(imageHandle);
298}
299#endif
300
302{
303 Region region;
305 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_GetRegion(
306 m_imageHandle, peak::common::detail::BackendAccessor<Region>::BackendHandleAddress(region));
307 });
308 return region;
309}
310
311inline void Image::SetRegion(const Region& region) const
312{
313 const peak_icv_region_handle regionHandle = peak::common::detail::BackendAccessor<Region>::BackendHandle(region);
314
316 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_SetRegion(m_imageHandle, regionHandle);
317 });
318}
319
320inline void Image::ResetRegion() const
321{
323 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_ResetRegion(m_imageHandle);
324 });
325}
326
328{
329 Image outputImage(pixelFormat, GetSize());
330 const auto imageHandle = peak::common::detail::BackendAccessor<Image>::BackendHandle(outputImage);
332 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_ConvertPixelFormat(
333 m_imageHandle, static_cast<peak_common_pixel_format>(pixelFormat), imageHandle);
334 });
335 return outputImage;
336}
337
338inline Image Image::ConvertPixelFormatWithFactor(peak::common::PixelFormat pixelFormat, const double factor) const
339{
340 Image outputImage(pixelFormat, GetSize());
341 const auto imageHandle = peak::common::detail::BackendAccessor<Image>::BackendHandle(outputImage);
343 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_ConvertPixelFormatWithFactor(
344 m_imageHandle, static_cast<peak_common_pixel_format>(pixelFormat), factor, imageHandle);
345 });
346 return outputImage;
347}
348
349inline void Image::SetMetadata(const peak::common::Metadata& metadata)
350{
351 auto captureInformationC = peak::icv::detail::CreateCCaptureInformationFromMetadata(metadata, m_size);
352
354 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_SetCaptureInformation(m_imageHandle, captureInformationC, sizeof(captureInformationC));
355 });
356
357 m_metaData = std::make_unique<peak::common::Metadata>(AddCaptureInformationDataToMetadata(captureInformationC, metadata));
358}
359
361{
362 peak_icv_capture_information captureInformationC;
364 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_GetCaptureInformation(
365 m_imageHandle, &captureInformationC, sizeof(captureInformationC));
366 });
367
368 if (!m_metaData)
369 {
370 m_metaData = std::make_unique<peak::common::Metadata>(
371 AddCaptureInformationDataToMetadata(captureInformationC, peak::common::Metadata{}));
372 }
373 else
374 {
375 m_metaData = std::make_unique<peak::common::Metadata>(AddCaptureInformationDataToMetadata(captureInformationC, *m_metaData));
376 }
377
378
379 return *m_metaData;
380}
381
382inline Image Image::Subtract(const peak::icv::Image& subtrahend) const
383{
384 Image difference{ GetPixelFormat(), GetSize() };
385
387 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_Subtract(GetHandle(), subtrahend.GetHandle(), difference.GetHandle());
388 });
389
390
391 return difference;
392}
393
394inline Image Image::operator-(const peak::icv::Image& subtrahend) const
395{
396 return Subtract(subtrahend);
397}
398
400{
401 Image croppedImage{ GetPixelFormat(), { rect.GetWidth(), rect.GetHeight() } };
402
404 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_Crop(GetHandle(), croppedImage.GetHandle(),
405 peak::common::detail::BackendAccessor<peak::common::Rectangle>::CreateCType(rect));
406 });
407 return croppedImage;
408}
409
410inline Image Image::Scale(const peak::common::Size& targetImageSize, const Interpolation& interpolation) const
411{
412 Image scaledImage{ GetPixelFormat(), targetImageSize };
414 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_Scale(
415 GetHandle(), scaledImage.GetHandle(), static_cast<peak_icv_interpolation>(interpolation));
416 });
417 return scaledImage;
418}
419
420#ifdef WITH_peak_ipl
421inline Image::operator peak::ipl::Image() const
422{
423 return GetIPLImage();
424}
425#endif
426} /* namespace icv */
427} /* namespace peak */
Interface for viewing image pixel data from arbitrary sources.
virtual std::shared_ptr< peak::common::Metadata > GetMetadata()=0
Returns the metadata object which can be used to retrieve or set metadata belonging to the image.
Type-safe container for image acquisition metadata.
void SetValueByKey(T &&value)
Stores a value using a strongly typed metadata key.
size_t GetSizeInBytes(const Size &size) const
Calculates the memory size in bytes for a given image size.
peak::common::PixelFormat GetPixelFormat() const
Returns the underlying pixel format enumeration value.
A generic rectangle defined by a point and a size.
constexpr SizeType GetWidth() const
Returns the width of the rectangle.
constexpr SizeType GetHeight() const
Returns the height of the rectangle.
Represents a two-dimensional size with non-negative width and height.
constexpr T GetWidth() const
Returns the width.
constexpr T GetHeight() const
Returns the height.
For detailed information about the use of ids_peak_icv Image refer to the Image Guide.
peak::common::Size GetSize() const
Provides the size of the image in pixels.
void ResetRegion() const
Resets the region on the image to the complete image.
Image operator-(const Image &subtrahend) const
Subtracts the specified subtrahend image pixelwise from the current image and returns the resulting i...
Image & operator=(const Image &other)
uint8_t * GetData() const
Provides a pointer to the image data buffer.
Image Subtract(const Image &subtrahend) const
Subtracts the specified subtrahend image pixelwise from the current image and returns the resulting i...
Region GetRegion() const
Provides the region of the image.
Image(const peak::common::IImageView &imageView)
Constructs an Image from an IImageView Instance.
peak::common::PixelFormat GetPixelFormat() const
Provides the pixel format of the image.
Image Crop(const peak::common::Rectangle &rect) const
Crops the image to the given rectangle.
peak::common::Metadata GetMetadata() const
Returns the Metadata of an image which contains information about the image capture information.
peak::icv::Image ConvertPixelFormat(peak::common::PixelFormat pixelFormat) const
Converts the image to a different pixel format.
void SetMetadata(const peak::common::Metadata &metadata)
Updates the Metadata of an image which is used to store and manage capture information.
Image Copy() const
Creates a deep copy of the current Image.
peak::icv::Image ConvertPixelFormatWithFactor(peak::common::PixelFormat pixelFormat, const double factor) const
Converts an image into another pixel format with a factor multiplied to every pixel.
Image Scale(const peak::common::Size &targetImageSize, const Interpolation &interpolation=Interpolation::NearestNeighbor) const
Returns an image scaled to the given image size.
size_t GetSizeInBytes() const
Provides the size of the image data in bytes.
bool operator==(const Image &rhs) const
size_t GetBytesPerLine() const
Computes the number of bytes required to store one line of pixel data (no padding).
void SetRegion(const Region &region) const
Updates the region on the image.
Represents a region defined by a set of points in the image plane.
PixelFormat
Enum listing all supported pixel formats and their internal IDs.
Interpolation
Enumeration of interpolation methods used in image transformations.
@ Roi
Region of interest applied to the image.
@ BinningHorizontal
Horizontal pixel binning factor.
@ DeviceTimestamp
Device-provided image acquisition timestamp in nanoseconds.
@ BinningVertical
Vertical pixel binning factor.
peak_icv_capture_information CreateCCaptureInformationFromMetadata(const peak::common::Metadata &metadata)
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
peak::common::detail::c_type_of_t< Type > * handle_of_t
peak_icv_image * peak_icv_image_handle