-
Notifications
You must be signed in to change notification settings - Fork 0
/
numerictype.cpp
133 lines (117 loc) · 5.88 KB
/
numerictype.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "numerictype.h"
static const int numericTypeLUTSize = 18;
static const NumericType numericTypeLUT[numericTypeLUTSize] =
{
{ DataType::UnsignedChar, GL_RED, GL_UNSIGNED_BYTE, CV_8UC1, CV_8U, DT_UINT8 },
{ DataType::Char, GL_RED, GL_BYTE, CV_8SC1, CV_8S, DT_INT8 },
{ DataType::UnsignedChar, GL_RG, GL_UNSIGNED_BYTE, CV_8UC2, CV_8U, DT_UINT8 },
{ DataType::Char, GL_RG, GL_BYTE, CV_8SC2, CV_8S, DT_INT8 },
{ DataType::UnsignedShort, GL_RED, GL_UNSIGNED_SHORT, CV_16UC1, CV_16U, DT_UINT16 },
{ DataType::Short, GL_RED, GL_SHORT, CV_16SC1, CV_16S, DT_INT16 },
{ DataType::UnsignedShort, GL_RG, GL_UNSIGNED_SHORT, CV_16UC2, CV_16U, DT_UINT16 },
{ DataType::Short, GL_RG, GL_SHORT, CV_16SC2, CV_16S, DT_INT16 },
{ DataType::UnsignedInt, GL_RED, GL_UNSIGNED_INT, CV_32SC1, CV_32S, DT_UINT8 }, // OpenCV does not have data type for unsigned int: use signed int
{ DataType::Int, GL_RED, GL_INT, CV_32SC1, CV_32S, DT_INT8 },
{ DataType::UnsignedInt, GL_RG, GL_UNSIGNED_INT, CV_32SC2, CV_32S, DT_UINT32 }, // OpenCV does not have data type for unsigned int: use signed int
{ DataType::Int, GL_RG, GL_INT, CV_32SC2, CV_32S, DT_INT32 },
{ DataType::Float, GL_RED, GL_FLOAT, CV_32FC1, CV_32F, DT_FLOAT32 },
{ DataType::Float, GL_RG, GL_FLOAT, CV_32FC2, CV_32F, DT_FLOAT32 },
{ DataType::UnsignedChar, GL_RGB, GL_UNSIGNED_BYTE, CV_8UC3, CV_8U, DT_RGB24 },
{ DataType::Char, GL_RGBA, GL_UNSIGNED_BYTE, CV_8UC4, CV_8U, DT_RGBA32 },
{ DataType::Double, GL_RED, GL_DOUBLE, CV_64FC1, CV_64F, DT_FLOAT64 },
{ DataType::Double, GL_RG, GL_DOUBLE, CV_64FC2, CV_64F, DT_FLOAT64 }
};
/* Okay, so there are times where you do not know the type of a data structure and want to get the maximum value for it.
* For example, you may load data from a NIFTI file or even an image with OpenCV. There are quite a few options that it
* can be. However, let's say you just want to initialize it or replace part of it with the maximum value of that data
* type. This would essentially make it white in a certain location. This cannot be done without a huge switch statement
* for the various data types. Furthermore, it doesn't help you out because the cv::Scalar option just converts it to a
* double anyways. Therefore, the min/max values are stored as doubles until C++ offers better support for this (if it
* ever does; it may be a limitation of the compiler). No trunucation should occur so it will be alright.
* MSVC still complains about narrowing conversion from char -> double, even though this is not the case? I just disable the
* warning and reenable after done.
*/
#ifdef _MSC_VER
#pragma warning(disable:4838)
#endif // _MSC_VER
static const double minMaxLUT[][2] =
{
{std::numeric_limits<bool>::min(), std::numeric_limits<bool>::max()},
{std::numeric_limits<unsigned char>::min(), std::numeric_limits<unsigned char>::max()},
{std::numeric_limits<char>::min(), std::numeric_limits<char>::max()},
{std::numeric_limits<unsigned short>::min(), std::numeric_limits<unsigned short>::max()},
{std::numeric_limits<short>::min(), std::numeric_limits<short>::max()},
{std::numeric_limits<unsigned int>::min(), std::numeric_limits<unsigned int>::max()},
{std::numeric_limits<int>::min(), std::numeric_limits<int>::max()},
{std::numeric_limits<unsigned long>::min(), std::numeric_limits<unsigned long>::max()},
{std::numeric_limits<long>::min(), std::numeric_limits<long>::max()},
{std::numeric_limits<float>::min(), std::numeric_limits<float>::max()},
{std::numeric_limits<double>::min(), std::numeric_limits<double>::max()}
};
#ifdef _MSC_VER
#pragma warning(default:4838)
#endif // _MSC_VER
NumericType::NumericType(DataType type_, GLenum openGLFormat_, GLenum openGLType_, int openCVType_, int openCVTypeNoChannel_, int NIFTIType_) :
type(type_)
#ifndef NUMERIC_TYPE_NO_OPENGL
, openGLFormat(openGLFormat_), openGLType(openGLType_)
#endif // NUMERIC_TYPE_NO_OPENGL
#ifndef NUMERIC_TYPE_NO_OPENCV
, openCVType(openCVType_), openCVTypeNoChannel(openCVTypeNoChannel_)
#endif // NUMERIC_TYPE_NO_OPENCV
#ifndef NUMERIC_TYPE_NO_NIFTI
, NIFTIType(NIFTIType_)
#endif // NUMERIC_TYPE_NO_NIFTI
{
}
double NumericType::getMin() const
{
return minMaxLUT[(int)type][0];
}
double NumericType::getMax() const
{
return minMaxLUT[(int)type][1];
}
#ifndef NUMERIC_TYPE_NO_OPENGL
const NumericType *NumericType::OpenGL(GLenum format, GLenum type)
{
for (int i = 0; i < numericTypeLUTSize; ++i)
{
if (numericTypeLUT[i].openGLFormat == format && numericTypeLUT[i].openGLType == type)
return &numericTypeLUT[i];
}
return NULL;
}
#endif // NUMERIC_TYPE_NO_OPENGL
#ifndef NUMERIC_TYPE_NO_OPENCV
const NumericType *NumericType::OpenCV(int type)
{
for (int i = 0; i < numericTypeLUTSize; ++i)
{
if (numericTypeLUT[i].openCVType == type)
return &numericTypeLUT[i];
}
return NULL;
}
const NumericType *NumericType::OpenCV(int type, int numChannels)
{
int searchType = CV_MAKETYPE(type, numChannels);
for (int i = 0; i < numericTypeLUTSize; ++i)
{
if (numericTypeLUT[i].openCVType == searchType)
return &numericTypeLUT[i];
}
return NULL;
}
#endif // NUMERIC_TYPE_NO_OPENCV
#ifndef NUMERIC_TYPE_NO_NIFTI
const NumericType *NumericType::NIFTI(int type)
{
for (int i = 0; i < numericTypeLUTSize; ++i)
{
if (numericTypeLUT[i].NIFTIType == type)
return &numericTypeLUT[i];
}
return NULL;
}
#endif // NUMERIC_TYPE_NO_NIFTI