MeVisLab Toolbox Reference
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>
55{
56
57public:
66 NoReordering = 0,
67 ReorderColorPlanesToInterleaved = 1
68 };
69
70 //------------------------------------------------------
73 //------------------------------------------------------
74
77 : _scale(static_cast<DT>(1)),
78 _shift(static_cast<DT>(0)),
79 _reorderMode(NoReordering)
80 {
81 }
82
85 : _scale(ssd._scale),
86 _shift(ssd._shift),
87 _reorderMode(ssd._reorderMode)
88 {
89 }
90
93 inline TScaleShiftData(DT scale, DT shift) : _scale(scale), _shift(shift) , _reorderMode(NoReordering)
94 {
95 }
96
108 :_reorderMode(NoReordering)
109 {
110 setFromMinMaxToMinMax(fromMin, fromMax, toMin, toMax);
111 }
113
114
115 //------------------------------------------------------
118 //------------------------------------------------------
119
122 {
123 _scale = ssd._scale;
124 _shift = ssd._shift;
125 _reorderMode = ssd._reorderMode;
126
127 return *this;
128 }
130
131
132 //------------------------------------------------------
135 //------------------------------------------------------
136
138 inline bool operator==(const TScaleShiftData<DT> &ssd) const
139 {
140 return MLValuesAreEqualWOM(_scale, ssd._scale) &&
141 MLValuesAreEqualWOM(_shift, ssd._shift) && (_reorderMode == ssd._reorderMode);
142 }
143
145 inline bool operator!=(const TScaleShiftData<DT> &ssd) const
146 {
147 return !(*this == ssd);
148 }
150
151
152 //------------------------------------------------------
155 //------------------------------------------------------
156
159 inline void setScaleOffset(DT scale, DT shift)
160 {
161 _scale=scale;
162 _shift=shift;
163 }
164
175 inline void setFromMinMaxToMinMax(DT fromMin, DT fromMax, DT toMin, DT toMax)
176 {
177 if (toMin > toMax) { std::swap(toMin, toMax); }
178 if (fromMin > fromMax) { std::swap(fromMin, fromMax); }
180 if (srcWidth > 0){
181 _scale = (toMax - toMin) / srcWidth;
182 _shift = -fromMin * _scale + toMin;
183 }
184 else{
185 // Our source interval has (nearly) identical left and right interval border.
186 // Since this case often occurs if images have identical minimum and maximum
187 // values we handle it so that the source interval is assumed as [fromMin, fromMin+1].
188 // So the only value from that interval is projected to the minimum value of the
189 // target interval.
190 _scale = static_cast<DT>(1);
191 _shift = toMin - fromMin;
192 }
193 }
194
233 {
235 bool toTypeIsInt = MLIsIntType(toType)!=0;
236 if (toMin > toMax) { std::swap(toMin, toMax); }
237 if (fromMin > fromMax) { std::swap(fromMin, fromMax); }
239 DT destWidth = toMax-toMin;
240 if (srcWidth > 0){
241
242 if (toTypeIsInt) {
243 if (!fromTypeIsInt) {
244 // Float -> Int:
245 // adding 0.99 to the integer range, since that is almost the case
246 // like adding 1. and avoids having to clamp the result to the int max value.
247 destWidth += 0.99;
248 } else if (srcWidth > destWidth) {
249 // Int -> Int and srcWidth > destWidth
250 // max+1 is be used for both ranges (which is the same as shifting for
251 // power-of-two differences in range)
252 srcWidth += 1;
253 destWidth += 1;
254 }
255 else {
256 // Int -> Int and srcWidth <= destWidth
257 // leave as is, so that fromMax is mapped to toMax
258 }
259 } else {
260 // Int -> Float:
261 // use max int value, and not max+1, so that the max integer value will get the
262 // max float value (like in OpenGL, where the max integer is mapped to 1.)
263
264 // Float -> Float:
265 // keep max as given
266 }
267 _scale = destWidth / srcWidth;
268 _shift = -fromMin * _scale + toMin;
269 }
270 else{
271 // Our source interval has (nearly) identical left and right interval border.
272 // Since this case often occurs if images have identical minimum and maximum
273 // values we handle it so that the source interval is assumed as [fromMin, fromMin+1].
274 // So the only value from that interval is projected to the minimum value of the
275 // target interval.
276 _scale = static_cast<DT>(1);
277 _shift = toMin - fromMin;
278 }
279 }
280
282 inline void unset()
283 {
284 _scale = static_cast<DT>(1);
285 _shift = static_cast<DT>(0);
286 _reorderMode = NoReordering;
287 }
289
290
291 //------------------------------------------------------
294 //------------------------------------------------------
295
297 inline DT getScale() const { return _scale; }
298
300 inline DT getShift() const { return _shift; }
302
304 inline ReorderMode getReorderMode() const { return _reorderMode; }
305
307 inline void setReorderMode(ReorderMode mode) { _reorderMode = mode; }
308
309private:
310
314 DT _scale;
315
317 DT _shift;
319
321 ReorderMode _reorderMode;
322
323};
324
327
328
330
331//-----------------------------------------------------------------------------------
332// Stream output for std::ostream
333//-----------------------------------------------------------------------------------
334namespace std {
335
337 inline ostream& operator<<(ostream& s, const ML_NAMESPACE::ScaleShiftData &ssd)
338 {
339 return s << "Scale=" << ssd.getScale() << ", Shift=" << ssd.getShift();
340 }
341
342}
343
344
345#endif //of __mlScaleShiftData_H
346
347
348
Class to define a first order linear transformation.
TScaleShiftData()
Default constructor, i.e., builds identity transformation.
void setReorderMode(ReorderMode mode)
Set 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 scale constant from 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
Get the reorder mode.
bool operator==(const TScaleShiftData< DT > &ssd) const
Returns true if all members are equal.
void unset()
Sets default, i.e., identity transformation.
ReorderMode
Mode that indicates if copySubImage should reorder the color from planes (from x,y,...
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 addition constant shift from transformation y=scale*x+shift.
MLint32 MLDataType
MLDataType.
Definition mlTypeDefs.h:596
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 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.