CVB++ 15.0
Loading...
Searching...
No Matches
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
44 class Image
45 {
47 friend ImagePlane;
48 friend Device;
50
51 public:
52 using GuardType = HandleGuard<Image>;
53
55
60 static std::unique_ptr<Image> Load(const String &fileName);
61
63
70 static std::unique_ptr<Image> Create(Size2D<int> size, int numPlanes = 1,
72 {
73 return Create(size.Width(), size.Height(), numPlanes, dataType);
74 }
75
77
85 static std::unique_ptr<Image> Create(int width, int height, int numPlanes = 1,
87
89
95 template <class RANGE>
96 static typename TypedRange<std::unique_ptr<Image>, ImagePtr, RANGE>::type FromImages(MappingOption mapping,
97 const RANGE &images)
98 {
99 auto rangeAdapter = MakeRangeAdapter<ImagePtr>(images);
100 std::vector<void *> handles;
101 handles.reserve(rangeAdapter.Size());
102 for (const auto &image : images)
103 handles.emplace_back(image->Handle());
104 return FromImageHandles(mapping, handles);
105 }
106
108
114 template <class... IMAGES>
115 static typename VarArgRange<std::unique_ptr<Image>, const Image &, IMAGES...>::type
116 FromImages(MappingOption mapping, const IMAGES &...images)
117 {
118 std::array<void *, sizeof...(IMAGES)> handles = {images.Handle()...};
119 return FromImageHandles(mapping, handles);
120 }
121
123
129 template <class RANGE>
130 static typename TypedRange<std::unique_ptr<Image>, ImagePlane, RANGE>::type FromPlanes(MappingOption mapping,
131 const RANGE &planes);
132
133#ifdef PLANES
134# undef PLANES
135#endif
137
143 template <class... PLANES>
144 static typename VarArgRange<std::unique_ptr<Image>, ImagePlane &&, PLANES...>::type
145 FromPlanes(MappingOption mapping, PLANES &&...planes);
146
148
155 static std::unique_ptr<Image> FromHandle(HandleGuard<Image> &&guard)
156 {
157 if (!guard.Handle())
158 throw std::runtime_error("handle must not be null");
159
160 auto image = std::unique_ptr<Image>(new Image(std::move(guard)));
161 return image;
162 }
163
165
174 template <class T, class... ARGS>
175 static std::unique_ptr<T> FromHandle(HandleGuard<Image> &&guard, ARGS &&...args)
176 {
177 if (!guard.Handle())
178 throw std::runtime_error("handle must not be null");
179
180 static_assert(std::is_base_of<Image, T>::value, "CVB: Type must be derived from Image");
181 auto image = std::unique_ptr<T>(new T(std::move(guard), std::forward<ARGS>(args)...));
182 return image;
183 }
184
186
192 explicit Image(Size2D<int> size, int numPlanes = 1, DataType dataType = DataType::Int8BppUnsigned())
193 : Image(size.Width(), size.Height(), numPlanes, dataType)
194 {
195 }
196
198
205 Image(int width, int height, int numPlanes = 1, DataType dataType = DataType::Int8BppUnsigned())
206 : Image(std::move(*Create(width, height, numPlanes, dataType)))
207 {
208 }
209
211
215 explicit Image(const String &fileName)
216 : Image(std::move(*Load(fileName)))
217 {
218 }
219
220 Image(const Image &other) = delete;
221 Image &operator=(const Image &other) = delete;
222
223 virtual ~Image() = default;
224
226
232 void *Handle() const noexcept
233 {
234 return handle_.Handle();
235 }
236
238
242 int PlanesCount() const noexcept
243 {
244 return static_cast<int>(CExports::ImageDimension(Handle()));
245 }
246
248
252 bool PlaneDataTypesIdentical() const noexcept
253 {
254 if (PlanesCount() == 1)
255 return true;
256
257 auto dataTypePlane0 = DataType::FromNativeDescriptor(static_cast<int>(CExports::ImageDatatype(Handle(), 0)));
258 for (int i = 1; i < PlanesCount(); ++i)
260 static_cast<int>(CExports::ImageDatatype(Handle(), static_cast<CExports::cvbdim_t>(i))))
261 != dataTypePlane0)
262 return false;
263 return true;
264 }
265
267
272 ImagePlane Plane(int plane) const;
273
275
279 std::vector<ImagePlane> Planes() const noexcept;
280
282
287 std::vector<double> GetPixel(Point2D<int> position) const;
288
290
294 int Height() const noexcept
295 {
296 return static_cast<int>(CExports::ImageHeight(Handle()));
297 }
298
300
304 int Width() const noexcept
305 {
306 return static_cast<int>(CExports::ImageWidth(Handle()));
307 }
308
310
315 {
316 return static_cast<Cvb::ColorModel>(CExports::ImageColorModel(Handle()));
317 }
318
320
326 void Save(const String &fileName) const;
327
329
337 void Save(const String &fileName, double quality) const;
338
340
348 {
349 return Map(rect, rect.Size());
350 }
351
353
363 std::unique_ptr<Image> Map(Rect<int> sourceRect, Size2D<int> targetSize) const;
364
366
375 std::unique_ptr<Image> Map(RotationMap rotation) const;
376
378
388
391
397 void Copy(Image &targetImage, Rect<int> sourceRect, Point2D<int> targetPosition) const
398 {
399 if (!CVB_CALL_CAPI(CopyImageRectPlanes(Handle(), targetImage.Handle(), sourceRect.Left(), sourceRect.Top(),
400 sourceRect.Right(), sourceRect.Bottom(), targetPosition.X(),
401 targetPosition.Y())))
402 Utilities::SystemInfo::ThrowLastError();
403 }
404
406
412 void Copy(Image &targetImage) const
413 {
414 Copy(targetImage, Bounds(), Point2D<int>());
415 targetImage.RaisePixelContentChanged();
416 }
417
419
423 Size2D<int> Size() const noexcept
424 {
425 return Size2D<int>(Width(), Height());
426 }
427
429
433 Rect<int> Bounds() const noexcept
434 {
435 return Rect<int>(Point2D<int>(), Size());
436 }
437
440
447
450
455 {
456 pixelContentChangedCarrierContainer_.Call<void(const Image &, Rect<int>)>(*this, rect);
457 }
458
460
466 {
467 auto carrier = Internal::CbCarrier<void(const Image &, Rect<int>)>::Create(handler);
468 return pixelContentChangedCarrierContainer_.Register(carrier);
469 }
470
472
477 {
478 pixelContentChangedCarrierContainer_.Unregister(eventCookie);
479 }
480
482
488 {
491 CExports::ImageToPixel(Handle(), point.X(), point.Y(), x, y);
492 return Point2D<double>(x, y);
493 }
494
496
502 {
503 Area2D retval;
504 CExports::ImageAreaToPixel(Handle(), *reinterpret_cast<CExports::TArea *>(&area),
505 reinterpret_cast<CExports::TArea &>(retval));
506 return retval;
507 }
508
510
516 {
519 CExports::PixelToImage(Handle(), point.X(), point.Y(), x, y);
520 return Point2D<double>(x, y);
521 }
522
524
530 {
531 Area2D retval;
532 CExports::PixelAreaToImage(Handle(), *reinterpret_cast<CExports::TArea *>(&area),
533 reinterpret_cast<CExports::TArea &>(retval));
534 return retval;
535 }
536
538
543 {
544 AffineMatrix2D retval;
545 CVB_CALL_CAPI(GetImageCoordinates(Handle(), reinterpret_cast<CExports::TCoordinateMap &>(retval)));
546 return retval;
547 }
548
550
554 void SetCoordinateSystem(AffineMatrix2D affineMatrix) noexcept
555 {
556 CVB_CALL_CAPI(SetImageCoordinates(Handle(), *reinterpret_cast<CExports::TCoordinateMap *>(&affineMatrix)));
557 }
558
560
567 {
568 return FromPixelFormat(size.Width(), size.Height(), format);
569 }
570
572
579 static std::unique_ptr<Image> FromPixelFormat(int width, int height, Cvb::PfncFormat format)
580 {
581 return Internal::DoResCallObjectOut<Image>([&](void *&image) {
582 return CVB_CALL_CAPI(
583 CVPFCreateImageFromPFNCValue(static_cast<Cvb::CExports::cvbuint32_t>(format), width, height, image));
584 });
585 }
586
587 protected:
588 explicit Image(HandleGuard<Image> &&guard) noexcept
589 : handle_(std::move(guard))
590 {
591 }
592
593 void Detach() noexcept
594 {
595 handle_.Release();
596 }
597
598 void ResetHandle(void *newHandle) noexcept
599 {
600 handle_.Reset(newHandle);
601 }
602
603 private:
604 Image(Image &&other) noexcept
605 : handle_(std::move(other.handle_))
606 , pixelContentChangedCarrierContainer_(std::move(other.pixelContentChangedCarrierContainer_))
607 {
608 }
609
610 Image &operator=(Image &&other) noexcept = delete;
611
612 void IncreaseRefCount() const noexcept
613 {
614 CVB_CALL_CAPI(ShareObject(Handle()));
615 }
616
617 template <class RANGE>
618 static typename TypedRange<std::unique_ptr<Image>, void *, RANGE>::type FromImageHandles(MappingOption mapping,
619 const RANGE &range)
620 {
621 return Internal::DoBoolCallObjectOut<Image>([&](void *&handle) {
622 auto rangeAdapter = MakeRangeAdapter<void *>(range);
623 return CVB_CALL_CAPI(CreateConcatenatedImage(rangeAdapter.Data(), rangeAdapter.Size(),
624 mapping == MappingOption::LinkPixels, handle));
625 });
626 }
627
628 HandleGuard<Image> handle_;
629
630 mutable std::mutex handlerMutex_;
631
632 Internal::CarrierContainer pixelContentChangedCarrierContainer_;
633 };
634
635 CVB_END_INLINE_NS
636
637} // 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:39
The Common Vision Blox image.
Definition decl_image.hpp:45
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:579
Point2D< double > ImageToPixelCoordinates(Point2D< double > point) const noexcept
Convert a point from image to pixel coordinates.
Definition decl_image.hpp:487
EventCookie RegisterEventPixelContentChanged(std::function< void(const Image &, Rect< int >)> handler)
Register a listener to the pixel content changed event.
Definition decl_image.hpp:465
void RaisePixelContentChanged() const
Inform clients listening to the pixel content changed event, that the image data has been completely ...
Definition decl_image.hpp:443
Area2D PixelToImageCoordinates(Area2D area) const noexcept
Convert an Area2D from pixel to image coordinates.
Definition decl_image.hpp:529
int Width() const noexcept
Width of the image in pixels.
Definition decl_image.hpp:304
Image(Size2D< int > size, int numPlanes=1, DataType dataType=DataType::Int8BppUnsigned())
Constructs an uninitialized image with the given parameters.
Definition decl_image.hpp:192
void Copy(Image &targetImage) const
Copies the image data from this image to the target image.
Definition decl_image.hpp:412
int Height() const noexcept
Height of the image in pixels.
Definition decl_image.hpp:294
AffineMatrix2D CoordinateSystem() const noexcept
Get the coordinate system of the image.
Definition decl_image.hpp:542
Image(const String &fileName)
Loads an image with the given file name.
Definition decl_image.hpp:215
std::unique_ptr< Image > Map(Rect< int > rect) const
Creates a mapped image of the region of this image.
Definition decl_image.hpp:347
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:454
int PlanesCount() const noexcept
Get the number of planes for this image.
Definition decl_image.hpp:242
Image(int width, int height, int numPlanes=1, DataType dataType=DataType::Int8BppUnsigned())
Constructs an uninitialized image with the given parameters.
Definition decl_image.hpp:205
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:397
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:476
Rect< int > Bounds() const noexcept
Bounding rectangle of the image in pixels.
Definition decl_image.hpp:433
static std::unique_ptr< Image > FromHandle(HandleGuard< Image > &&guard)
Creates an image from a classic API handle.
Definition decl_image.hpp:155
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:423
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:96
void SetCoordinateSystem(AffineMatrix2D affineMatrix) noexcept
Set the coordinate system of the image.
Definition decl_image.hpp:554
Area2D ImageToPixelCoordinates(Area2D area) const noexcept
Convert an Area2D from image to pixel coordinates.
Definition decl_image.hpp:501
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:566
static std::unique_ptr< T > FromHandle(HandleGuard< Image > &&guard, ARGS &&...args)
Creates an image from a classic API handle (typed).
Definition decl_image.hpp:175
Cvb::ColorModel ColorModel() const noexcept
Color model realized by this image.
Definition decl_image.hpp:314
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:252
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:232
Point2D< double > PixelToImageCoordinates(Point2D< double > point) const noexcept
Convert a point from pixel to image coordinates.
Definition decl_image.hpp:515
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:116
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:70
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:21
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)