CVB++ 15.0
Loading...
Searching...
No Matches
data_type.hpp
1#pragma once
2
3#include <limits>
4#include <type_traits>
5#include <utility>
6
7#include <cmath>
8
9#include "_cexports/c_img.h"
10
11#include "global.hpp"
12#include "exception.hpp"
13
14namespace Cvb
15{
16
17 CVB_BEGIN_INLINE_NS
18
20
22 class DataType final
23 {
24 public:
26
31 static DataType FromNativeDescriptor(int dataTypeDescriptor) noexcept
32 {
33 return DataType(dataTypeDescriptor);
34 }
35
37
41 static DataType Int8BppUnsigned() noexcept
42 {
43 return DataType(8);
44 }
45
47
51 static DataType Int8BppSigned() noexcept
52 {
53 return DataType(8 | CExports::DT_Signed);
54 }
55
57
61 static DataType Int10BppUnsigned() noexcept
62 {
63 return DataType(10);
64 }
65
67
71 static DataType Int12BppUnsigned() noexcept
72 {
73 return DataType(12);
74 }
75
77
81 static DataType Int16BppUnsigned() noexcept
82 {
83 return DataType(16);
84 }
85
87
91 static DataType Int16BppSigned() noexcept
92 {
93 return DataType(16 | CExports::DT_Signed);
94 }
95
97
101 static DataType Int32BppUnsigned() noexcept
102 {
103 return DataType(32);
104 }
105
107
111 static DataType Int32BppSigned() noexcept
112 {
113 return DataType(32 | CExports::DT_Signed);
114 }
115
117
121 static DataType Int64BppUnsigned() noexcept
122 {
123 return DataType(64);
124 }
125
127
131 static DataType Int64BppSigned() noexcept
132 {
133 return DataType(64 | CExports::DT_Signed);
134 }
135
137
141 static DataType Float32Bpp() noexcept
142 {
143 return DataType(32 | CExports::DT_Float | CExports::DT_Signed);
144 }
145
147
151 static DataType Float64Bpp() noexcept
152 {
153 return DataType(64 | CExports::DT_Float | CExports::DT_Signed);
154 }
155
157
172 template <typename T>
173 static DataType FromNativeType() noexcept
174 {
176 "CVB: Unsupported image plane data type!");
177 static_assert((1 << 8) == CExports::DT_Signed, "CVB: DT_Signed does not match!");
178 static_assert((1 << 9) == CExports::DT_Float, "CVB: DT_Float does not match!");
179
180 CExports::cvbdatatype_t nativeDescriptor = sizeof(T) * 8 // bit size
182 << 8 // set signed bit if signed int or float
183 | std::is_floating_point<T>::value << 9; // set float bit if float
184
185 return DataType(nativeDescriptor);
186 }
187
189
199 template <template <class> class T, class ACTION, class... CTORARGS>
200 auto CallWithInstanceOf(ACTION action, CTORARGS... args) const
201 -> decltype(action(T<int>{std::forward<CTORARGS>(args)...}))
202 {
203 if (this->IsUnsignedInteger())
204 {
205 switch (this->BytesPerPixel())
206 {
207 case 1:
208 {
209 T<std::uint8_t> t{std::forward<CTORARGS>(args)...};
210 return action(t);
211 }
212 case 2:
213 {
214 T<std::uint16_t> t{std::forward<CTORARGS>(args)...};
215 return action(t);
216 }
217 case 4:
218 {
219 T<std::uint32_t> t{std::forward<CTORARGS>(args)...};
220 return action(t);
221 }
222 case 8:
223 {
224 T<std::uint64_t> t{std::forward<CTORARGS>(args)...};
225 return action(t);
226 }
227 default:
228 {
229 break;
230 }
231 }
232 }
233 else if (this->IsSignedInteger())
234 {
235 switch (this->BytesPerPixel())
236 {
237 case 1:
238 {
239 T<std::int8_t> t{std::forward<CTORARGS>(args)...};
240 return action(t);
241 }
242 case 2:
243 {
244 T<std::int16_t> t{std::forward<CTORARGS>(args)...};
245 return action(t);
246 }
247 case 4:
248 {
249 T<std::int32_t> t{std::forward<CTORARGS>(args)...};
250 return action(t);
251 }
252 case 8:
253 {
254 T<std::int64_t> t{std::forward<CTORARGS>(args)...};
255 return action(t);
256 }
257 default:
258 {
259 break;
260 }
261 }
262 }
263 else if (this->IsFloat())
264 {
265 switch (this->BytesPerPixel())
266 {
267 case 4:
268 {
269 T<float> t{std::forward<CTORARGS>(args)...};
270 return action(t);
271 }
272 case 8:
273 {
274 T<double> t{std::forward<CTORARGS>(args)...};
275 return action(t);
276 }
277 default:
278 {
279 break;
280 }
281 }
282 }
283
284 throw std::runtime_error("Could not match native data type");
285 }
286
288
296 template <class T>
297 bool Matches() const noexcept
298 {
300 "CVB: T must be integer or floating point");
301
302 // floats use specialization below
303 return this->BytesPerPixel() == sizeof(T) && this->IsFloat() == std::is_floating_point<T>::value
305 }
306
308
312 int NativeDescriptor() const noexcept
313 {
314 return dataTypeDescriptor_;
315 }
316
318
322 int BitsPerPixel() const noexcept
323 {
324 return dataTypeDescriptor_ & 0xFF;
325 }
326
328
332 int BytesPerPixel() const noexcept
333 {
334 // remember: 0x7 is equivalent to % 8
335 return BitsPerPixel() / 8 + (((BitsPerPixel() & 0x7) != 0) ? 1 : 0);
336 }
337
339
343 bool IsFloat() const noexcept
344 {
345 // as defined in iCVCImg.h...
346 return (dataTypeDescriptor_ & CExports::DT_Float) != 0;
347 }
348
350
354 bool IsSignedInteger() const noexcept
355 {
356 return (dataTypeDescriptor_ & CExports::DT_Signed) != 0 && !IsFloat();
357 }
358
360
364 bool IsUnsignedInteger() const noexcept
365 {
366 return (dataTypeDescriptor_ & 0xB00) == 0;
367 }
368
370
374 bool IsSigned() const noexcept
375 {
376 return (dataTypeDescriptor_ & CExports::DT_Signed) != 0;
377 }
378
380
384 bool HasOverlayBit() const noexcept
385 {
386 return (dataTypeDescriptor_ & CExports::DT_Overlay) != 0;
387 }
388
390
400 bool IsComplexPacked() const noexcept
401 {
402 return (dataTypeDescriptor_ & CExports::DT_ComplexPacked) != 0;
403 }
404
406
410 double MinVal() const noexcept
411 {
412 if (IsFloat())
413 {
414#ifdef min
415# undef min
416#endif
417 if (BytesPerPixel() == 4)
418 return static_cast<double>(std::numeric_limits<float>::lowest());
419 else if (BytesPerPixel() == 8)
421 else
423 }
424 else
425 {
426 CExports::cvbval_t min = 0;
427 CExports::cvbval_t max = 0;
428 CVB_CALL_CAPI(GetDatatypeMinMaxVal(static_cast<CExports::cvbdatatype_t>(dataTypeDescriptor_), min, max));
429 return static_cast<double>(min);
430 }
431 }
432
434
438 double MaxVal() const noexcept
439 {
440#ifdef max
441# undef max
442#endif
443 if (IsFloat())
444 {
445 if (BytesPerPixel() == 4)
446 return static_cast<double>(std::numeric_limits<float>::max());
447 else if (BytesPerPixel() == 8)
449 else
451 }
452 else
453 {
454 CExports::cvbval_t min = 0;
455 CExports::cvbval_t max = 0;
456 CVB_CALL_CAPI(GetDatatypeMinMaxVal(static_cast<CExports::cvbdatatype_t>(dataTypeDescriptor_), min, max));
457 return static_cast<double>(max);
458 }
459 }
460
462
466 PixelDataType NumericType() const noexcept
467 {
468
469 if ((IsComplexPacked()) && (IsFloat()))
471 else if (IsFloat())
473 else if (IsSignedInteger())
474 return PixelDataType::Int;
475 else if (IsUnsignedInteger())
476 return PixelDataType::UInt;
477 else
479 }
480
482
489 double UndefinedVal() const noexcept
490 {
491
493 return round((MaxVal() - MinVal()) / 2.0);
494 else
495 return 0.0;
496 }
497
499
504 bool operator==(const DataType &dataType) const noexcept
505 {
506 if (IsFloat() && dataType.IsFloat())
507 return BitsPerPixel() == dataType.BitsPerPixel(); // skip signed bit if float or double
508 return dataTypeDescriptor_ == dataType.dataTypeDescriptor_;
509 }
510
512
517 bool operator!=(const DataType &dataType) const noexcept
518 {
519 return !(*this == dataType);
520 }
521
522 DataType() noexcept = default;
523
524 private:
525 explicit DataType(CExports::cvbdatatype_t dataTypeDescriptor) noexcept
526 : dataTypeDescriptor_(dataTypeDescriptor)
527 {
528 }
529
530 int dataTypeDescriptor_ = 0;
531 };
532
533 CVB_END_INLINE_NS
534
535} // namespace Cvb
Data type description for an image plane.
Definition data_type.hpp:23
int BytesPerPixel() const noexcept
Number of bytes occupied by each pixel.
Definition data_type.hpp:332
static DataType Int64BppSigned() noexcept
Represents 64-bit signed integer pixels.
Definition data_type.hpp:131
static DataType Int12BppUnsigned() noexcept
Represents 12-bit unsigned integer pixels.
Definition data_type.hpp:71
double MinVal() const noexcept
Gets the minimum pixel value that fits this data type.
Definition data_type.hpp:410
static DataType Int16BppUnsigned() noexcept
Represents 16-bit unsigned integer pixels.
Definition data_type.hpp:81
bool HasOverlayBit() const noexcept
Gets whether bit 0 of the pixels of the plane are being used as an overlay indicator bit.
Definition data_type.hpp:384
int BitsPerPixel() const noexcept
Number of actually valid bits per pixel.
Definition data_type.hpp:322
bool IsSigned() const noexcept
Gets whether the pixels of the plane have signed values.
Definition data_type.hpp:374
int NativeDescriptor() const noexcept
Native data type descriptor.
Definition data_type.hpp:312
bool IsSignedInteger() const noexcept
Gets whether the pixels of the plane have signed integer values.
Definition data_type.hpp:354
static DataType Int8BppUnsigned() noexcept
Represents 8-bit unsigned integer pixels (bytes).
Definition data_type.hpp:41
bool operator!=(const DataType &dataType) const noexcept
Compares to an other data type.
Definition data_type.hpp:517
bool Matches() const noexcept
Gets whether the type T matches this data type.
Definition data_type.hpp:297
bool IsUnsignedInteger() const noexcept
Gets whether the pixels of the plane have unsigned integer values.
Definition data_type.hpp:364
static DataType Int32BppUnsigned() noexcept
Represents 32-bit unsigned integer pixels.
Definition data_type.hpp:101
PixelDataType NumericType() const noexcept
Returns the basic numeric type of the pixels.
Definition data_type.hpp:466
static DataType Int32BppSigned() noexcept
Represents 32-bit signed integer pixels.
Definition data_type.hpp:111
bool operator==(const DataType &dataType) const noexcept
Compares to an other data type.
Definition data_type.hpp:504
static DataType FromNativeType() noexcept
Construct a data type descriptor from one of the native data type value equivalents.
Definition data_type.hpp:173
static DataType Int16BppSigned() noexcept
Represents 16-bit signed integer pixels.
Definition data_type.hpp:91
bool IsFloat() const noexcept
Gets whether the pixels of the plane have floating point values.
Definition data_type.hpp:343
auto CallWithInstanceOf(ACTION action, CTORARGS... args) const -> decltype(action(T< int >{std::forward< CTORARGS >(args)...}))
Calls action with T instance of native data type this value represents.
Definition data_type.hpp:200
static DataType Int8BppSigned() noexcept
Represents 8-bit signed integer pixels.
Definition data_type.hpp:51
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
static DataType Float32Bpp() noexcept
Represents single precision (32-bit) floating point pixels.
Definition data_type.hpp:141
static DataType Int10BppUnsigned() noexcept
Represents 10-bit unsigned integer pixels.
Definition data_type.hpp:61
static DataType Int64BppUnsigned() noexcept
Represents 64-bit unsigned integer pixels.
Definition data_type.hpp:121
static DataType Float64Bpp() noexcept
Represents double precision (64-bit) floating point pixels.
Definition data_type.hpp:151
double MaxVal() const noexcept
Gets the maximum pixel value that fits this data type.
Definition data_type.hpp:438
double UndefinedVal() const noexcept
Get the undefined value for this data type.
Definition data_type.hpp:489
bool IsComplexPacked() const noexcept
Indicates whether or not the plane contains a complex packed image format.
Definition data_type.hpp:400
T forward(T... args)
cvbbool_t GetDatatypeMinMaxVal(cvbdatatype_t Datatype, cvbval_t &MinVal, cvbval_t &MaxVal)
long cvbdatatype_t
T lowest(T... args)
T max(T... args)
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
PixelDataType
Defines the numeric data type of one pixel.
Definition global.hpp:149
@ UInt
Definition global.hpp:159
@ Int
Definition global.hpp:163
@ Float
Definition global.hpp:167
@ ComplexPackedFloat
Definition global.hpp:171
@ Undefined
Definition global.hpp:155
T quiet_NaN(T... args)