comparison Core/Images/PixelTraits.h @ 2489:e91bab2d8c75

Bresenham's line algorithm
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 13 Mar 2018 21:14:21 +0100
parents be1dbb1dcdd6
children 5aa787a03e7d
comparison
equal deleted inserted replaced
2488:345725b9350c 2489:e91bab2d8c75
32 32
33 33
34 #pragma once 34 #pragma once
35 35
36 #include "../Enumerations.h" 36 #include "../Enumerations.h"
37 37 #include "../OrthancException.h"
38
39 #include <limits>
38 40
39 namespace Orthanc 41 namespace Orthanc
40 { 42 {
43 template <PixelFormat format,
44 typename _PixelType>
45 struct IntegerPixelTraits
46 {
47 typedef _PixelType PixelType;
48
49 ORTHANC_FORCE_INLINE
50 static PixelFormat GetPixelFormat()
51 {
52 return format;
53 }
54
55 ORTHANC_FORCE_INLINE
56 static PixelType IntegerToPixel(int64_t value)
57 {
58 if (value < static_cast<int64_t>(std::numeric_limits<PixelType>::min()) ||
59 value > static_cast<int64_t>(std::numeric_limits<PixelType>::max()))
60 {
61 throw OrthancException(ErrorCode_ParameterOutOfRange);
62 }
63 else
64 {
65 return static_cast<PixelType>(value);
66 }
67 }
68
69 ORTHANC_FORCE_INLINE
70 static void SetZero(PixelType& target)
71 {
72 target = 0;
73 }
74
75 ORTHANC_FORCE_INLINE
76 static void SetMinValue(PixelType& target)
77 {
78 target = std::numeric_limits<PixelType>::min();
79 }
80
81 ORTHANC_FORCE_INLINE
82 static void SetMaxValue(PixelType& target)
83 {
84 target = std::numeric_limits<PixelType>::max();
85 }
86
87 ORTHANC_FORCE_INLINE
88 static void Copy(PixelType& target,
89 const PixelType& source)
90 {
91 target = source;
92 }
93
94 ORTHANC_FORCE_INLINE
95 static float PixelToFloat(const PixelType& source)
96 {
97 return static_cast<float>(source);
98 }
99
100 ORTHANC_FORCE_INLINE
101 static void FloatToPixel(PixelType& target,
102 float value)
103 {
104 if (value < static_cast<float>(std::numeric_limits<PixelType>::min()))
105 {
106 target = std::numeric_limits<PixelType>::min();
107 }
108 else if (value > static_cast<float>(std::numeric_limits<PixelType>::max()))
109 {
110 target = std::numeric_limits<PixelType>::max();
111 }
112 else
113 {
114 target = static_cast<PixelType>(value);
115 }
116 }
117
118 ORTHANC_FORCE_INLINE
119 static bool IsEqual(const PixelType& a,
120 const PixelType& b)
121 {
122 return a == b;
123 }
124 };
125
126
41 template <PixelFormat Format> 127 template <PixelFormat Format>
42 struct PixelTraits; 128 struct PixelTraits;
43 129
44 130
45 template <> 131 template <>
46 struct PixelTraits<PixelFormat_Grayscale8> 132 struct PixelTraits<PixelFormat_Grayscale8> :
47 { 133 public IntegerPixelTraits<PixelFormat_Grayscale8, uint8_t>
48 typedef uint8_t PixelType; 134 {
49 135 };
50 ORTHANC_FORCE_INLINE 136
51 static PixelFormat GetPixelFormat() 137
52 { 138 template <>
53 return PixelFormat_Grayscale8; 139 struct PixelTraits<PixelFormat_Grayscale16> :
54 } 140 public IntegerPixelTraits<PixelFormat_Grayscale16, uint16_t>
55 141 {
56 ORTHANC_FORCE_INLINE 142 };
57 static void SetZero(PixelType& target) 143
58 { 144
59 target = 0; 145 template <>
60 } 146 struct PixelTraits<PixelFormat_SignedGrayscale16> :
61 147 public IntegerPixelTraits<PixelFormat_SignedGrayscale16, int16_t>
62 ORTHANC_FORCE_INLINE 148 {
63 static void Copy(PixelType& target,
64 const PixelType& source)
65 {
66 target = source;
67 }
68
69 ORTHANC_FORCE_INLINE
70 static float PixelToFloat(const PixelType& source)
71 {
72 return static_cast<float>(source);
73 }
74
75 ORTHANC_FORCE_INLINE
76 static void FloatToPixel(PixelType& target,
77 float value)
78 {
79 target = static_cast<uint8_t>(value);
80 }
81
82 ORTHANC_FORCE_INLINE
83 static bool IsEqual(const PixelType& a,
84 const PixelType& b)
85 {
86 return a == b;
87 }
88 };
89
90
91 template <>
92 struct PixelTraits<PixelFormat_Grayscale16>
93 {
94 typedef uint16_t PixelType;
95
96 ORTHANC_FORCE_INLINE
97 static PixelFormat GetPixelFormat()
98 {
99 return PixelFormat_Grayscale16;
100 }
101
102 ORTHANC_FORCE_INLINE
103 static void SetZero(PixelType& target)
104 {
105 target = 0;
106 }
107
108 ORTHANC_FORCE_INLINE
109 static void Copy(PixelType& target,
110 const PixelType& source)
111 {
112 target = source;
113 }
114
115 ORTHANC_FORCE_INLINE
116 static float PixelToFloat(const PixelType& source)
117 {
118 return static_cast<float>(source);
119 }
120
121 ORTHANC_FORCE_INLINE
122 static void FloatToPixel(PixelType& target,
123 float value)
124 {
125 target = static_cast<uint16_t>(value);
126 }
127
128 ORTHANC_FORCE_INLINE
129 static bool IsEqual(const PixelType& a,
130 const PixelType& b)
131 {
132 return a == b;
133 }
134 };
135
136
137 template <>
138 struct PixelTraits<PixelFormat_SignedGrayscale16>
139 {
140 typedef int16_t PixelType;
141
142 ORTHANC_FORCE_INLINE
143 static PixelFormat GetPixelFormat()
144 {
145 return PixelFormat_SignedGrayscale16;
146 }
147
148 ORTHANC_FORCE_INLINE
149 static void SetZero(PixelType& target)
150 {
151 target = 0;
152 }
153
154 ORTHANC_FORCE_INLINE
155 static void Copy(PixelType& target,
156 const PixelType& source)
157 {
158 target = source;
159 }
160
161 ORTHANC_FORCE_INLINE
162 static float PixelToFloat(const PixelType& source)
163 {
164 return static_cast<float>(source);
165 }
166
167 ORTHANC_FORCE_INLINE
168 static void FloatToPixel(PixelType& target,
169 float value)
170 {
171 target = static_cast<int16_t>(value);
172 }
173
174 ORTHANC_FORCE_INLINE
175 static bool IsEqual(const PixelType& a,
176 const PixelType& b)
177 {
178 return a == b;
179 }
180 }; 149 };
181 150
182 151
183 template <> 152 template <>
184 struct PixelTraits<PixelFormat_RGB24> 153 struct PixelTraits<PixelFormat_RGB24>