MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlScaleShiftData.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_SCALE_SHIFT_DATA_H
14#define ML_SCALE_SHIFT_DATA_H
15
17
18// ML-includes
19#include "mlInitSystemML.h"
20#include <mlTypeDefTraits.h>
21#include <mlDataTypes.h>
22
23#include <iostream>
24#include <utility>
25#include <string>
26
28
29//-------------------------------------------------------------------------
52//-------------------------------------------------------------------------
53template <typename DT> class TScaleShiftData
54{
55
56public:
65 NoReordering = 0,
66 ReorderColorPlanesToInterleaved = 1
67 };
68
69 //------------------------------------------------------
72 //------------------------------------------------------
73
76 : _scale(static_cast<DT>(1))
77 , _shift(static_cast<DT>(0))
78 , _reorderMode(NoReordering)
79 {}
80
83 : _scale(ssd._scale)
84 , _shift(ssd._shift)
85 , _reorderMode(ssd._reorderMode)
86 {}
87
90 inline TScaleShiftData(DT scale, DT shift)
91 : _scale(scale)
92 , _shift(shift)
93 , _reorderMode(NoReordering)
94 {}
95
107 : _reorderMode(NoReordering)
108 {
109 setFromMinMaxToMinMax(fromMin, fromMax, toMin, toMax);
110 }
112
113
114 //------------------------------------------------------
117 //------------------------------------------------------
118
121 {
122 _scale = ssd._scale;
123 _shift = ssd._shift;
124 _reorderMode = ssd._reorderMode;
125
126 return *this;
127 }
129
130
131 //------------------------------------------------------
134 //------------------------------------------------------
135
137 inline bool operator==(const TScaleShiftData<DT>& ssd) const
138 {
139 return MLValuesAreEqualWOM(_scale, ssd._scale) && MLValuesAreEqualWOM(_shift, ssd._shift) &&
140 (_reorderMode == ssd._reorderMode);
141 }
142
144 inline bool operator!=(const TScaleShiftData<DT>& ssd) const
145 {
146 return !(*this == ssd);
147 }
149
150
151 //------------------------------------------------------
154 //------------------------------------------------------
155
158 inline void setScaleOffset(DT scale, DT shift)
159 {
160 _scale = scale;
161 _shift = shift;
162 }
163
174 inline void setFromMinMaxToMinMax(DT fromMin, DT fromMax, DT toMin, DT toMax)
175 {
176 if (toMin > toMax) {
177 std::swap(toMin, toMax);
178 }
179 if (fromMin > fromMax) {
180 std::swap(fromMin, fromMax);
181 }
182 DT srcWidth = fromMax - fromMin;
183 if (srcWidth > 0) {
184 _scale = (toMax - toMin) / srcWidth;
185 _shift = -fromMin * _scale + toMin;
186 }
187 else {
188 // Our source interval has (nearly) identical left and right interval borders.
189 // Since this case often occurs if images have identical minimum and maximum
190 // values, we handle it so that the source interval is assumed as [fromMin, fromMin+1].
191 // This way, the only value from that interval is projected to the minimum value of the
192 // target interval.
193 _scale = static_cast<DT>(1);
194 _shift = toMin - fromMin;
195 }
196 }
197
235 {
237 bool toTypeIsInt = MLIsIntType(toType) != 0;
238 if (toMin > toMax) {
239 std::swap(toMin, toMax);
240 }
241 if (fromMin > fromMax) {
242 std::swap(fromMin, fromMax);
243 }
244 DT srcWidth = fromMax - fromMin;
245 DT destWidth = toMax - toMin;
246 if (srcWidth > 0) {
247
248 if (toTypeIsInt) {
249 if (!fromTypeIsInt) {
250 // Float -> Int:
251 // adding 0.99 to the integer range, since that is almost the same
252 // as adding 1.0 and avoids having to clamp the result to the integer max value.
253 destWidth += 0.99;
254 }
255 else if (srcWidth > destWidth) {
256 // Int -> Int and srcWidth > destWidth
257 // max+1 is be used for both ranges, which is the same as shifting for
258 // power-of-two differences in range
259 srcWidth += 1;
260 destWidth += 1;
261 }
262 else {
263 // Int -> Int and srcWidth <= destWidth
264 // leave as is, so that fromMax is mapped to toMax
265 }
266 }
267 else {
268 // Int -> Float:
269 // use max integer value, and not max+1, so that the max integer value will get the
270 // max float value (like in OpenGL, where the max integer is mapped to 1.0)
271
272 // Float -> Float:
273 // keep max as given
274 }
275 _scale = destWidth / srcWidth;
276 _shift = -fromMin * _scale + toMin;
277 }
278 else {
279 // Our source interval has (nearly) identical left and right interval border.
280 // Since this case often occurs if images have identical minimum and maximum
281 // values, we handle it so that the source interval is assumed as [fromMin, fromMin+1].
282 // This way, the only value from that interval is projected to the minimum value of the
283 // target interval.
284 _scale = static_cast<DT>(1);
285 _shift = toMin - fromMin;
286 }
287 }
288
290 inline void unset()
291 {
292 _scale = static_cast<DT>(1);
293 _shift = static_cast<DT>(0);
294 _reorderMode = NoReordering;
295 }
297
298
299 //------------------------------------------------------
302 //------------------------------------------------------
303
305 inline DT getScale() const
306 {
307 return _scale;
308 }
309
311 inline DT getShift() const
312 {
313 return _shift;
314 }
316
319 {
320 return _reorderMode;
321 }
322
324 inline void setReorderMode(ReorderMode mode)
325 {
326 _reorderMode = mode;
327 }
328
329private:
333 DT _scale;
334
336 DT _shift;
338
340 ReorderMode _reorderMode;
341};
342
345
346
348
349//-----------------------------------------------------------------------------------
350// Stream output for std::ostream
351//-----------------------------------------------------------------------------------
352namespace std
353{
354
356 inline ostream& operator<<(ostream& s, const ML_NAMESPACE::ScaleShiftData& ssd)
357 {
358 return s << "Scale=" << ssd.getScale() << ", Shift=" << ssd.getShift();
359 }
360
361} // namespace std
362
363
364#endif // of __mlScaleShiftData_H
The class defines a first order linear transformation.
TScaleShiftData()
Default constructor, i.e., builds identity transformation.
void setReorderMode(ReorderMode mode)
Sets the reorder mode.
TScaleShiftData(DT fromMin, DT fromMax, DT toMin, DT toMax)
Values from interval [fromMin, fromMax] are transformed linearly into [toMin, toMax].
TScaleShiftData & operator=(const TScaleShiftData< DT > &ssd)
Assignment operator from an existing object to get an identical copy.
bool operator!=(const TScaleShiftData< DT > &ssd) const
Returns true if any members differ.
DT getScale() const
Returns the scale constant of the transformation y=scale*x+offset.
TScaleShiftData(const TScaleShiftData< DT > &ssd)
Copy constructor.
void setFromMinMaxToMinMax(DT fromMin, DT fromMax, DT toMin, DT toMax)
Values from interval [fromMin, fromMax] are transformed linearly into [toMin, toMax].
void setScaleOffset(DT scale, DT shift)
Sets scale and shift: values are multiplied with scale and then shift is added.
void setFromMinMaxToMinMax(MLDataType fromType, DT fromMin, DT fromMax, MLDataType toType, DT toMin, DT toMax)
Values from interval [fromMin, fromMax] are transformed linearly into [toMin, toMax].
ReorderMode getReorderMode() const
Returns the reorder mode.
bool operator==(const TScaleShiftData< DT > &ssd) const
Returns true if all members are equal.
void unset()
Sets the default, i.e., identity transformation.
ReorderMode
Mode that indicates whether copySubImage should reorder the color from planes (from x,...
TScaleShiftData(DT scale, DT shift)
Constructor: sets scale and shift to define a transformation so that a scaled value v' is v' = v*scal...
DT getShift() const
Returns the addition constant of the transformation y=scale*x+shift.
MLint32 MLDataType
MLDataType.
Definition mlTypeDefs.h:592
MLEXPORT MLint32 MLIsIntType(MLDataType dataType)
Returns true (=1) if data type dataType is an integer data type, otherwise false (=0).
bool MLValuesAreEqualWOM(MLint8 a, MLint8 b)
Returns true if values a and b are equal; otherwise, it returns false.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
STL namespace.
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator '<<' for stream output of Field objects.