CVB++ 15.0
decl_image.hpp
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cstddef>
6#include <functional>
7#include <list>
8#include <memory>
9#include <mutex>
10#include <new>
11#include <stdexcept>
12#include <system_error>
13#include <utility>
14
15#include "../_cexports/c_img.h"
16#include "../_cexports/c_pixfmtcnv.h"
17
18#include "../global.hpp"
19
20#include "../affine_matrix_2d.hpp"
21#include "../area_2d.hpp"
22#include "../data_type.hpp"
23#include "../pfnc_format.hpp"
24#include "../rect.hpp"
25#include "../string.hpp"
26
27#include "../utilities/system_info.hpp"
28
29namespace Cvb
30{
31
32 CVB_BEGIN_INLINE_NS
33
34 class ImagePlane;
35
36 template <>
37 inline HandleGuard<Image>::HandleGuard(void *handle) noexcept
38 : HandleGuard<Image>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
39 {
40 }
41
43
49 class Image
50 {
52 friend ImagePlane;
53 friend Device;
55
56 public:
57 using GuardType = HandleGuard<Image>;
58
60
65 static std::unique_ptr<Image> Load(const String &fileName);
66
68
75 static std::unique_ptr<Image> Create(Size2D<int> size, int numPlanes = 1,
77 {
78 return Create(size.Width(), size.Height(), numPlanes, dataType);
79 }
80
82
90 static std::unique_ptr<Image> Create(int width, int height, int numPlanes = 1,
92
94
100 template <class RANGE>
101 static typename TypedRange<std::unique_ptr<Image>, ImagePtr, RANGE>::type FromImages(MappingOption mapping,
102 const RANGE &images)
103 {
104 auto rangeAdapter = MakeRangeAdapter<ImagePtr>(images);
105 std::vector<void *> handles;
106 handles.reserve(rangeAdapter.Size());
107 for (const auto &image : images)
108 handles.emplace_back(image->Handle());
109 return FromImageHandles(mapping, handles);
110 }
111
113
119 template <class... IMAGES>
120 static typename VarArgRange<std::unique_ptr<Image>, const Image &, IMAGES...>::type
121 FromImages(MappingOption mapping, const IMAGES &...images)
122 {
123 std::array<void *, sizeof...(IMAGES)> handles = {images.Handle()...};
124 return FromImageHandles(mapping, handles);
125 }
126
128
134 template <class RANGE>
135 static typename TypedRange<std::unique_ptr<Image>, ImagePlane, RANGE>::type FromPlanes(MappingOption mapping,
136 const RANGE &planes);
137
138#ifdef PLANES
139# undef PLANES
140#endif
142
148 template <class... PLANES>
149 static typename VarArgRange<std::unique_ptr<Image>, ImagePlane &&, PLANES...>::type
150 FromPlanes(MappingOption mapping, PLANES &&...planes);
151
153
160 static std::unique_ptr<Image> FromHandle(HandleGuard<Image> &&guard)
161 {
162 if (!guard.Handle())
163 throw std::runtime_error("handle must not be null");
164
165 auto image = std::unique_ptr<Image>(new Image(std::move(guard)));
166 return image;
167 }
168
170
179 template <class T, class... ARGS>
180 static std::unique_ptr<T> FromHandle(HandleGuard<Image> &&guard, ARGS &&...args)
181 {
182 if (!guard.Handle())
183 throw std::runtime_error("handle must not be null");
184
185 static_assert(std::is_base_of<Image, T>::value, "CVB: Type must be derived from Image");
186 auto image = std::unique_ptr<T>(new T(std::move(guard), std::forward<ARGS>(args)...));
187 return image;
188 }
189
191
197 explicit Image(Size2D<int> size, int numPlanes = 1, DataType dataType = DataType::Int8BppUnsigned())
198 : Image(size.Width(), size.Height(), numPlanes, dataType)
199 {
200 }
201
203
210 Image(int width, int height, int numPlanes = 1, DataType dataType = DataType::Int8BppUnsigned())
211 : Image(std::move(*Create(width, height, numPlanes, dataType)))
212 {
213 }
214
216
220 explicit Image(const String &fileName)
221 : Image(std::move(*Load(fileName)))
222 {
223 }
224
225 Image(const Image &other) = delete;
226 Image &operator=(const Image &other) = delete;
227
228 virtual ~Image() = default;
229
231
237 void *Handle() const noexcept
238 {
239 return handle_.Handle();
240 }
241
243
247 int PlanesCount() const noexcept
248 {
249 return static_cast<int>(CExports::ImageDimension(Handle()));
250 }
251
253
257 bool PlaneDataTypesIdentical() const noexcept
258 {
259 if (PlanesCount() == 1)
260 return true;
261
262 auto dataTypePlane0 = DataType::FromNativeDescriptor(static_cast<int>(CExports::ImageDatatype(Handle(), 0)));
263 for (int i = 1; i < PlanesCount(); ++i)
265 static_cast<int>(CExports::ImageDatatype(Handle(), static_cast<CExports::cvbdim_t>(i))))
266 != dataTypePlane0)
267 return false;
268 return true;
269 }
270
272
277 ImagePlane Plane(int plane) const;
278
280
284 std::vector<ImagePlane> Planes() const noexcept;
285
287
292 std::vector<double> GetPixel(Point2D<int> position) const;
293
295
299 int Height() const noexcept
300 {
301 return static_cast<int>(CExports::ImageHeight(Handle()));
302 }
303
305
309 int Width() const noexcept
310 {
311 return static_cast<int>(CExports::ImageWidth(Handle()));
312 }
313
315
320 {
321 return static_cast<Cvb::ColorModel>(CExports::ImageColorModel(Handle()));
322 }
323
325
331 void Save(const String &fileName) const;
332
334
342 void Save(const String &fileName, double quality) const;
343
345
353 {
354 return Map(rect, rect.Size());
355 }
356
358
368 std::unique_ptr<Image> Map(Rect<int> sourceRect, Size2D<int> targetSize) const;
369
371
380 std::unique_ptr<Image> Map(RotationMap rotation) const;
381
383
393
396
402 void Copy(Image &targetImage, Rect<int> sourceRect, Point2D<int> targetPosition) const
403 {
404 if (!CVB_CALL_CAPI(CopyImageRectPlanes(Handle(), targetImage.Handle(), sourceRect.Left(), sourceRect.Top(),
405 sourceRect.Right(), sourceRect.Bottom(), targetPosition.X(),
406 targetPosition.Y())))
407 Utilities::SystemInfo::ThrowLastError();
408 }
409
411
417 void Copy(Image &targetImage) const
418 {
419 Copy(targetImage, Bounds(), Point2D<int>());
420 targetImage.RaisePixelContentChanged();
421 }
422
424
428 Size2D<int> Size() const noexcept
429 {
430 return Size2D<int>(Width(), Height());
431 }
432
434
438 Rect<int> Bounds() const noexcept
439 {
440 return Rect<int>(Point2D<int>(), Size());
441 }
442
445
452
455
460 {
461 pixelContentChangedCarrierContainer_.Call<void(const Image &, Rect<int>)>(*this, rect);
462 }
463
465
471 {
472 auto carrier = Internal::CbCarrier<void(const Image &, Rect<int>)>::Create(handler);
473 return pixelContentChangedCarrierContainer_.Register(carrier);
474 }
475
477
482 {
483 pixelContentChangedCarrierContainer_.Unregister(eventCookie);
484 }
485
487
493 {
496 CExports::ImageToPixel(Handle(), point.X(), point.Y(), x, y);
497 return Point2D<double>(x, y);
498 }
499
501
507 {
508 Area2D retval;
509 CExports::ImageAreaToPixel(Handle(), *reinterpret_cast<CExports::TArea *>(&area),
510 reinterpret_cast<CExports::TArea &>(retval));
511 return retval;
512 }
513
515
521 {
524 CExports::PixelToImage(Handle(), point.X(), point.Y(), x, y);
525 return Point2D<double>(x, y);
526 }
527
529
535 {
536 Area2D retval;
537 CExports::PixelAreaToImage(Handle(), *reinterpret_cast<CExports::TArea *>(&area),
538 reinterpret_cast<CExports::TArea &>(retval));
539 return retval;
540 }
541
543
548 {
549 AffineMatrix2D retval;
550 CVB_CALL_CAPI(GetImageCoordinates(Handle(), reinterpret_cast<CExports::TCoordinateMap &>(retval)));
551 return retval;
552 }
553
555
559 void SetCoordinateSystem(AffineMatrix2D affineMatrix) noexcept
560 {
561 CVB_CALL_CAPI(SetImageCoordinates(Handle(), *reinterpret_cast<CExports::TCoordinateMap *>(&affineMatrix)));
562 }
563
565
572 {
573 return FromPixelFormat(size.Width(), size.Height(), format);
574 }
575
577
584 static std::unique_ptr<Image> FromPixelFormat(int width, int height, Cvb::PfncFormat format)
585 {
586 return Internal::DoResCallObjectOut<Image>([&](void *&image) {
587 return CVB_CALL_CAPI(
588 CVPFCreateImageFromPFNCValue(static_cast<Cvb::CExports::cvbuint32_t>(format), width, height, image));
589 });
590 }
591
592 protected:
593 explicit Image(HandleGuard<Image> &&guard) noexcept
594 : handle_(std::move(guard))
595 {
596 }
597
598 void Detach() noexcept
599 {
600 handle_.Release();
601 }
602
603 void ResetHandle(void *newHandle) noexcept
604 {
605 handle_.Reset(newHandle);
606 }
607
608 private:
609 Image(Image &&other) noexcept
610 : handle_(std::move(other.handle_))
611 , pixelContentChangedCarrierContainer_(std::move(other.pixelContentChangedCarrierContainer_))
612 {
613 }
614
615 Image &operator=(Image &&other) noexcept = delete;
616
617 void IncreaseRefCount() const noexcept
618 {
619 CVB_CALL_CAPI(ShareObject(Handle()));
620 }
621
622 template <class RANGE>
623 static typename TypedRange<std::unique_ptr<Image>, void *, RANGE>::type FromImageHandles(MappingOption mapping,
624 const RANGE &range)
625 {
626 return Internal::DoBoolCallObjectOut<Image>([&](void *&handle) {
627 auto rangeAdapter = MakeRangeAdapter<void *>(range);
628 return CVB_CALL_CAPI(CreateConcatenatedImage(rangeAdapter.Data(), rangeAdapter.Size(),
629 mapping == MappingOption::LinkPixels, handle));
630 });
631 }
632
633 HandleGuard<Image> handle_;
634
635 mutable std::mutex handlerMutex_;
636
637 Internal::CarrierContainer pixelContentChangedCarrierContainer_;
638 };
639
640 CVB_END_INLINE_NS
641
642} // namespace Cvb
Compacted affine matrix describing the Common Vision Blox coordinate system.
Definition affine_matrix_2d.hpp:17
Structure that represents an area of interest in the image.
Definition area_2d.hpp:21
Data type description for an image plane.
Definition data_type.hpp:23
static DataType Int8BppUnsigned() noexcept
Represents 8-bit unsigned integer pixels (bytes).
Definition data_type.hpp:41
static DataType FromNativeDescriptor(int dataTypeDescriptor) noexcept
Construct a data type descriptor from one of the native library's descriptor values.
Definition data_type.hpp:31
Generic CVB physical device.
Definition decl_device.hpp:78
The Common Vision Blox image.
Definition decl_image.hpp:50
static std::unique_ptr< Image > FromPixelFormat(int width, int height, Cvb::PfncFormat format)
Creates an image from a specified pixel format.
Definition decl_image.hpp:584
Point2D< double > ImageToPixelCoordinates(Point2D< double > point) const noexcept
Convert a point from image to pixel coordinates.
Definition decl_image.hpp:492
EventCookie RegisterEventPixelContentChanged(std::function< void(const Image &, Rect< int >)> handler)
Register a listener to the pixel content changed event.
Definition decl_image.hpp:470
void RaisePixelContentChanged() const
Inform clients listening to the pixel content changed event, that the image data has been completely ...
Definition decl_image.hpp:448
Area2D PixelToImageCoordinates(Area2D area) const noexcept
Convert an Area2D from pixel to image coordinates.
Definition decl_image.hpp:534
int Width() const noexcept
Width of the image in pixels.
Definition decl_image.hpp:309
Image(Size2D< int > size, int numPlanes=1, DataType dataType=DataType::Int8BppUnsigned())
Constructs an uninitialized image with the given parameters.
Definition decl_image.hpp:197
void Copy(Image &targetImage) const
Copies the image data from this image to the target image.
Definition decl_image.hpp:417
int Height() const noexcept
Height of the image in pixels.
Definition decl_image.hpp:299
AffineMatrix2D CoordinateSystem() const noexcept
Get the coordinate system of the image.
Definition decl_image.hpp:547
Image(const String &fileName)
Loads an image with the given file name.
Definition decl_image.hpp:220
std::unique_ptr< Image > Map(Rect< int > rect) const
Creates a mapped image of the region of this image.
Definition decl_image.hpp:352
void RaisePixelContentChanged(Rect< int > rect) const
Inform clients listening to the pixel content changed event, that the image data has been updated in ...
Definition decl_image.hpp:459
int PlanesCount() const noexcept
Get the number of planes for this image.
Definition decl_image.hpp:247
Image(int width, int height, int numPlanes=1, DataType dataType=DataType::Int8BppUnsigned())
Constructs an uninitialized image with the given parameters.
Definition decl_image.hpp:210
void Copy(Image &targetImage, Rect< int > sourceRect, Point2D< int > targetPosition) const
Copies the image data from the source rectangle of this image to the target position inside the targe...
Definition decl_image.hpp:402
static TypedRange< std::unique_ptr< Image >, ImagePlane, RANGE >::type FromPlanes(MappingOption mapping, const RANGE &planes)
Create an image that is the result of concatenating a series of input planes.
Definition detail_image.hpp:40
void UnregisterEventPixelContentChanged(EventCookie eventCookie) noexcept
Manually unregister a listener from the pixel content changed event.
Definition decl_image.hpp:481
Rect< int > Bounds() const noexcept
Bounding rectangle of the image in pixels.
Definition decl_image.hpp:438
static std::unique_ptr< Image > FromHandle(HandleGuard< Image > &&guard)
Creates an image from a classic API handle.
Definition decl_image.hpp:160
ImagePlane Plane(int plane) const
Indexed access to the individual plane information.
Definition detail_image.hpp:58
void Save(const String &fileName) const
Write the current content of the image into a file.
Definition detail_image.hpp:84
std::unique_ptr< Image > Clone() const
Creates a new image object, that is a copy of the current instance.
Definition detail_image.hpp:116
Size2D< int > Size() const noexcept
Size of the image in pixels.
Definition decl_image.hpp:428
static TypedRange< std::unique_ptr< Image >, ImagePtr, RANGE >::type FromImages(MappingOption mapping, const RANGE &images)
Create an image that is the result of concatenating a series of input images.
Definition decl_image.hpp:101
void SetCoordinateSystem(AffineMatrix2D affineMatrix) noexcept
Set the coordinate system of the image.
Definition decl_image.hpp:559
Area2D ImageToPixelCoordinates(Area2D area) const noexcept
Convert an Area2D from image to pixel coordinates.
Definition decl_image.hpp:506
std::vector< ImagePlane > Planes() const noexcept
Access all planes for this image.
Definition detail_image.hpp:65
static std::unique_ptr< Image > FromPixelFormat(const Cvb::Size2D< int > &size, Cvb::PfncFormat format)
Creates an image from a specified pixel format.
Definition decl_image.hpp:571
static std::unique_ptr< T > FromHandle(HandleGuard< Image > &&guard, ARGS &&...args)
Creates an image from a classic API handle (typed).
Definition decl_image.hpp:180
Cvb::ColorModel ColorModel() const noexcept
Color model realized by this image.
Definition decl_image.hpp:319
std::vector< double > GetPixel(Point2D< int > position) const
Gets the pixel values for all planes at a give position.
Definition detail_image.hpp:74
static std::unique_ptr< Image > Load(const String &fileName)
Loads an image with the given file name.
Definition detail_image.hpp:25
bool PlaneDataTypesIdentical() const noexcept
Check if all planes have the same data type.
Definition decl_image.hpp:257
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:237
Point2D< double > PixelToImageCoordinates(Point2D< double > point) const noexcept
Convert a point from pixel to image coordinates.
Definition decl_image.hpp:520
static VarArgRange< std::unique_ptr< Image >, constImage &, IMAGES... >::type FromImages(MappingOption mapping, const IMAGES &...images)
Create an image that is the result of concatenating a series of input images.
Definition decl_image.hpp:121
static std::unique_ptr< Image > Create(Size2D< int > size, int numPlanes=1, DataType dataType=DataType::Int8BppUnsigned())
Creates an uninitialized image with the given parameters.
Definition decl_image.hpp:75
Image plane information container.
Definition decl_image_plane.hpp:29
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
T X() const noexcept
Gets the x-component of the point.
Definition point_2d.hpp:84
T Y() const noexcept
Gets the y-component of the point.
Definition point_2d.hpp:104
Rectangle object.
Definition rect.hpp:24
T Bottom() const noexcept
Gets bottom row of the rectangle (still inside the rectangle).
Definition rect.hpp:144
Size2D< T > Size() const noexcept
Gets the size of the rectangle.
Definition rect.hpp:204
T Top() const noexcept
Gets first row of the rectangle.
Definition rect.hpp:104
T Right() const noexcept
Gets rightmost column of the rectangle (still inside the rectangle).
Definition rect.hpp:124
T Left() const noexcept
Gets first column of the rectangle.
Definition rect.hpp:84
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition size_2d.hpp:20
T Height() const noexcept
Gets the vertical component of the size.
Definition size_2d.hpp:77
T Width() const noexcept
Gets the horizontal component of the size.
Definition size_2d.hpp:57
T forward(T... args)
cvbbool_t GetImageCoordinates(IMG Image, TCoordinateMap &CS)
cvbbool_t SetImageCoordinates(IMG Image, TCoordinateMap CS)
cvbbool_t CopyImageRectPlanes(IMG ImageIn, IMG ImageOut, cvbdim_t InLeft, cvbdim_t InTop, cvbdim_t InRight, cvbdim_t InBottom, cvbdim_t OutX, cvbdim_t OutY)
cvbbool_t CreateConcatenatedImage(IMG *ImageArray, size_t ArrayLength, cvbbool_t ShareMemory, IMG &ImageOut)
cvbbool_t ShareObject(OBJ Object)
cvbbool_t ReleaseObject(OBJ &Object)
T move(T... args)
PfncFormat
GenICam Pixel Format Naming Convention (PFNC) format values.
Definition pfnc_format.hpp:34
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
RotationMap
Amount of rotation to apply when mapping an image.
Definition global.hpp:377
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
MappingOption
Mapping options when creating a (potentially) mapped image.
Definition global.hpp:361
@ LinkPixels
Definition global.hpp:372
std::shared_ptr< Image > ImagePtr
Convenience shared pointer for Image.
Definition global.hpp:86
ColorModel
Color model that this image is using.
Definition global.hpp:176
T quiet_NaN(T... args)