IDS peak ICV v1.0.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.SetValueByName<decltype(captureInformationC.binning_factor.x)>(
80 "BinningHorizontal", std::move(captureInformationC.binning_factor.x));
81 metadata.SetValueByName<decltype(captureInformationC.binning_factor.y)>(
82 "BinningVertical", std::move(captureInformationC.binning_factor.y));
83 return metadata;
84}
85
86inline Image::Image(peak_icv_image_handle handle, peak_icv_image_info info)
87 : m_imageHandle{ handle }
88 , m_size{ info.size.width, info.size.height }
89 , m_buffer{ info.buffer }
90 , m_pixelFormatInfo{ static_cast<peak::common::PixelFormat>(info.pixelFormat) }
91{}
92
93#if WITH_peak_ipl
94inline Image::Image(const peak::ipl::Image& image)
95 : Image(CreateFromBuffer(static_cast<peak::common::PixelFormat>(image.PixelFormat().PixelFormatName()),
96 peak::common::Size{ static_cast<uint32_t>(image.Width()), static_cast<uint32_t>(image.Height()) }, image.Data(),
97 image.ByteCount()))
98{
99 peak_icv_capture_information captureInformationC{ { 1, 1 }, image.Timestamp(), {} };
100 detail::ExecuteAndMapReturnCodes([&] {
101 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_SetCaptureInformation(m_imageHandle, captureInformationC, sizeof(captureInformationC));
102 });
103
104 m_metaData = std::make_unique<peak::common::Metadata>(
105 AddCaptureInformationDataToMetadata(captureInformationC, peak::common::Metadata{}));
106 m_image = image;
107}
108#endif
109
110inline Image::Image(const peak::common::IImageView& imageView)
111 : Image(CreateFromBuffer(imageView.GetPixelFormat(), imageView.GetSize(), imageView.GetData(), imageView.GetSizeInBytes()))
112{
113 const auto metadata = imageView.GetMetadata();
114 SetMetadata(*metadata);
115}
116
117inline Image::Image(const std::string& path)
118 : Image{ CreateFromFile(path) }
119{}
120
121inline Image::Image(const std::string& path, peak::common::PixelFormat name)
122 : Image{ CreateFromFile(path, name) }
123{}
124
125inline Image::Image(peak::common::PixelFormat pixelFormat, uint8_t* buffer, size_t bufferSize, const peak::common::Size& imageSize)
126 : Image{ CreateFromBuffer(pixelFormat, imageSize, buffer, bufferSize),
127 { static_cast<peak_common_pixel_format>(pixelFormat), { imageSize.GetWidth(), imageSize.GetHeight() }, bufferSize, buffer } }
128{}
129
130inline Image::Image(peak::common::PixelFormat pixelFormat, const peak::common::Size& imageSize)
131 : Image{ CreateFromSize(pixelFormat, imageSize) }
132{}
133
134inline Image::Image(peak_icv_image_handle imageHandle)
135 : Image{ imageHandle, CreateFromHandle(imageHandle) }
136{}
137
138inline Image::Image(const Image& other)
139 : Image{ other.m_imageHandle, CreateFromHandle(other.m_imageHandle) }
140{
142 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_IncreaseUseCount(other.m_imageHandle);
143 });
144#if WITH_peak_ipl
145 m_image = other.m_image;
146#endif
147}
148
149inline Image::Image(Image&& other) noexcept
150 : Image{ other.m_imageHandle, CreateFromHandle(other.m_imageHandle) }
151{
152
153#if 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#if 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#if 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 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#if 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{
305 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_GetRegion(
306 m_imageHandle, peak::common::detail::BackendAccessor<experimental::Region>::BackendHandleAddress(region));
307 });
308 return region;
309}
310
311inline void Image::SetRegion(const experimental::Region& region) const
312{
313 const peak_icv_region_handle regionHandle = peak::common::detail::BackendAccessor<experimental::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 void Image::SetMetadata(const peak::common::Metadata& metadata)
339{
340 auto captureInformationC = peak::icv::detail::CreateCCaptureInformationFromMetadata(metadata);
341
343 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_SetCaptureInformation(m_imageHandle, captureInformationC, sizeof(captureInformationC));
344 });
345
346 m_metaData = std::make_unique<peak::common::Metadata>(AddCaptureInformationDataToMetadata(captureInformationC, metadata));
347}
348
350{
351
352 peak_icv_capture_information captureInformationC;
354 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_GetCaptureInformation(
355 m_imageHandle, &captureInformationC, sizeof(captureInformationC));
356 });
357
358 if (!m_metaData)
359 {
360 m_metaData = std::make_unique<peak::common::Metadata>(
361 AddCaptureInformationDataToMetadata(captureInformationC, peak::common::Metadata{}));
362 }
363 else
364 {
365 m_metaData = std::make_unique<peak::common::Metadata>(AddCaptureInformationDataToMetadata(captureInformationC, *m_metaData));
366 }
367
368
369 return *m_metaData;
370}
371
372inline Image Image::Subtract(const peak::icv::Image& subtrahend) const
373{
374 Image difference{ GetPixelFormat(), GetSize() };
375
377 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_Subtract(GetHandle(), subtrahend.GetHandle(), difference.GetHandle());
378 });
379
380
381 return difference;
382}
383
384inline Image Image::operator-(const peak::icv::Image& subtrahend) const
385{
386 return Subtract(subtrahend);
387}
388
390{
391 Image croppedImage{ GetPixelFormat(), { rect.GetWidth(), rect.GetHeight() } };
392
394 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_Crop(GetHandle(), croppedImage.GetHandle(),
395 peak::common::detail::BackendAccessor<peak::common::Rectangle>::CreateCType(rect));
396 });
397 return croppedImage;
398}
399
400inline Image Image::Scale(const peak::common::Size& targetImageSize, const Interpolation& interpolation) const
401{
402 Image scaledImage{ GetPixelFormat(), targetImageSize };
404 return PEAK_ICV_C_ABI_PREFIX peak_icv_Image_Scale(
405 GetHandle(), scaledImage.GetHandle(), static_cast<peak_icv_interpolation>(interpolation));
406 });
407 return scaledImage;
408}
409
410#if WITH_peak_ipl
411inline Image::operator peak::ipl::Image() const
412{
413 return GetIPLImage();
414}
415#endif
416} /* namespace icv */
417} /* 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 SetValueByName(const std::string &key, T &&value)
Stores a value using a string-based key.
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.
void SetRegion(const experimental::Region &region) const
Updates the region on the image.
peak::common::Size GetSize() const
Provides the size of the image.
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)
experimental::Region GetRegion() const
Provides the region of the image.
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...
Image(const peak::common::IImageView &imageView)
Constructs an Image from an IImageView Instance. This can be a buffer or a possibility to convert oth...
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 CaptureInformation of an image which contains information about the image capture setting...
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 CaptureInformation of an image which is used to store and manage capture settings.
Image Copy() const
Creates a deep copy of the current Image.
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).
A region consists of a set of points in the image plane.
detail::SizeT< uint32_t > Size
Size with unsigned 32-bit integer dimensions.
PixelFormat
Enum listing all supported pixel formats and their internal IDs.
Interpolation
Interpolation method for image transformations.
@ DeviceTimestamp
Device-provided image acquisition timestamp in nanoseconds.
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