CVB++ 15.0
detail_image.hpp
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <memory>
6#include <new>
7#include <stdexcept>
8#include <system_error>
9#include <utility>
10
11#include "../_cexports/c_img.h"
12
13#include "../global.hpp"
14#include "../data_type.hpp"
15
16#include "../_decl/decl_image.hpp"
17#include "../_decl/decl_image_plane.hpp"
18#include "../_decl/decl_device_factory.hpp"
19
20namespace Cvb
21{
22
23 CVB_BEGIN_INLINE_NS
24
26 {
27 return Internal::DoBoolCallObjectOut<Image>(
28 [&](void *&handle) { return CVB_CALL_CAPI(LoadImageFileTyped(fileName.c_str(), handle)); });
29 }
30
31 inline std::unique_ptr<Image> Image::Create(int width, int height, int numPlanes, DataType dataType)
32 {
33 return Internal::DoBoolCallObjectOut<Image>([&](void *&handle) {
34 return CVB_CALL_CAPI(CreateGenericImageDT(
35 numPlanes, width, height, static_cast<CExports::cvbdatatype_t>(dataType.NativeDescriptor()), handle));
36 });
37 }
38
39 template <class RANGE>
40 inline typename TypedRange<std::unique_ptr<Image>, ImagePlane, RANGE>::type Image::FromPlanes(MappingOption mapping,
41 const RANGE &planes)
42 {
43 auto rangeAdapter = MakeRangeAdapter<ImagePlane>(planes);
45 images.reserve(rangeAdapter.Size());
46 for (const auto &plane : planes)
47 images.emplace_back(plane.Map());
48 return FromImages(mapping, images);
49 }
50
51 template <class... PLANES>
52 inline typename VarArgRange<std::unique_ptr<Image>, ImagePlane &&, PLANES...>::type
53 Image::FromPlanes(MappingOption mapping, PLANES &&...planes)
54 {
55 return FromImages(mapping, *planes.Map()...);
56 }
57
58 inline ImagePlane Image::Plane(int plane) const
59 {
60 if ((plane < 0) || (plane >= PlanesCount()))
61 throw std::out_of_range("plane index out of range");
62 return ImagePlane(plane, *this);
63 }
64
65 inline std::vector<ImagePlane> Image::Planes() const noexcept
66 {
67 auto planesCount = PlanesCount();
69 for (int i = 0; i < planesCount; ++i)
70 planes.push_back(Plane(i));
71 return planes;
72 }
73
75 {
76 std::vector<double> pixelValues;
77 auto planes = Planes();
78 for (auto &plane : planes)
79 pixelValues.push_back(plane.GetPixel(position));
80
81 return pixelValues;
82 }
83
84 inline void Image::Save(const String &fileName) const
85 {
86 std::unique_lock<std::mutex> lock(DeviceFactory::LockAnchor());
87 if (!CVB_CALL_CAPI(WriteImageFileTyped(Handle(), fileName.c_str())))
88 Utilities::SystemInfo::ThrowLastError();
89 }
90
91 inline void Image::Save(const String &fileName, double quality) const
92 {
93 std::unique_lock<std::mutex> lock(DeviceFactory::LockAnchor());
94 if (!CVB_CALL_CAPI(WriteLossyImageFileTyped(Handle(), quality, fileName.c_str())))
95 Utilities::SystemInfo::ThrowLastError();
96 }
97
98 inline std::unique_ptr<Image> Image::Map(Rect<int> sourceRect, Size2D<int> targetSize) const
99 {
100 return Internal::DoBoolCallObjectOut<Image>([&](void *&img) {
101 return CVB_CALL_CAPI(CreateImageMap(
102 Handle(), static_cast<CExports::cvbdim_t>(sourceRect.Left()),
103 static_cast<CExports::cvbdim_t>(sourceRect.Top()), static_cast<CExports::cvbdim_t>(sourceRect.Right()),
104 static_cast<CExports::cvbdim_t>(sourceRect.Bottom()), static_cast<CExports::cvbdim_t>(targetSize.Width()),
105 static_cast<CExports::cvbdim_t>(targetSize.Height()), img));
106 });
107 }
108
110 {
111 return Internal::DoBoolCallObjectOut<Image>([&](void *&img) {
112 return CVB_CALL_CAPI(CreateRotatedImageMap(Handle(), static_cast<CExports::TVPATRotation>(rotation), img));
113 });
114 }
115
117 {
118 return Internal::DoBoolCallObjectOut<Image>(
119 [&](void *&img) { return CVB_CALL_CAPI(CreateDuplicateImageEx(Handle(), img)); });
120 }
121
122 CVB_END_INLINE_NS
123
124} // namespace Cvb
Data type description for an image plane.
Definition data_type.hpp:23
int NativeDescriptor() const noexcept
Native data type descriptor.
Definition data_type.hpp:312
std::unique_ptr< Image > Map(Rect< int > rect) const
Creates a mapped image of the region of this image.
Definition decl_image.hpp:352
int PlanesCount() const noexcept
Get the number of planes for this image.
Definition decl_image.hpp:247
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
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
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
std::vector< ImagePlane > Planes() const noexcept
Access all planes for this image.
Definition detail_image.hpp:65
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
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:237
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
Rectangle object.
Definition rect.hpp:24
T Bottom() const noexcept
Gets bottom row of the rectangle (still inside the rectangle).
Definition rect.hpp:144
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
cvbbool_t CreateGenericImageDT(cvbval_t NumPlanes, cvbval_t Width, cvbval_t Height, cvbdatatype_t Dataype, IMG &ImageOut)
cvbbool_t CreateImageMap(IMG ImageIn, cvbval_t InLeft, cvbval_t InTop, cvbval_t InRight, cvbval_t InBottom, cvbval_t OutWidth, cvbval_t OutHeight, IMG &ImageOut)
cvbbool_t CreateRotatedImageMap(IMG ImageIn, TVPATRotation Rotation, IMG &ImageOut)
cvbbool_t CreateDuplicateImageEx(IMG ImageIn, IMG &ImageOut)
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