1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #include "vvImageMapToWLColors.h"
19 #include "clitkCommon.h"
20 #include "vtkDataArray.h"
21 #include "vtkImageData.h"
22 #include "vtkInformation.h"
23 #include "vtkInformationVector.h"
24 #include "vtkObjectFactory.h"
25 #include "vtkScalarsToColors.h"
26 #include "vtkPointData.h"
27 #include <vtkObjectFactory.h>
29 vtkStandardNewMacro(vvImageMapToWLColors);
31 vvImageMapToWLColors::vvImageMapToWLColors() :
36 void vtkImageMapToWindowLevelClamps ( vtkImageData *data, double w,
37 double l, T& lower, T& upper,
38 unsigned char &lower_val,
39 unsigned char &upper_val)
41 double f_lower, f_upper, f_lower_val, f_upper_val;
42 double adjustedLower, adjustedUpper;
45 data->GetPointData()->GetScalars()->GetDataTypeRange( range );
47 f_lower = l - fabs(w) / 2.0;
48 f_upper = f_lower + fabs(w);
50 // Set the correct lower value
51 if ( f_lower <= range[1])
53 if (f_lower >= range[0])
55 lower = static_cast<T>(f_lower);
56 adjustedLower = f_lower;
60 lower = static_cast<T>(range[0]);
61 adjustedLower = range[0];
66 lower = static_cast<T>(range[1]);
67 adjustedLower = range[1];
70 // Set the correct upper value
71 if ( f_upper >= range[0])
73 if (f_upper <= range[1])
75 upper = static_cast<T>(f_upper);
76 adjustedUpper = f_upper;
80 upper = static_cast<T>(range[1]);
81 adjustedUpper = range[1];
86 upper = static_cast<T>(range[0]);
87 adjustedUpper = range [0];
90 // now compute the lower and upper values
93 f_lower_val = 255.0*(adjustedLower - f_lower)/w;
94 f_upper_val = 255.0*(adjustedUpper - f_lower)/w;
98 f_lower_val = 255.0 + 255.0*(adjustedLower - f_lower)/w;
99 f_upper_val = 255.0 + 255.0*(adjustedUpper - f_lower)/w;
102 if (f_upper_val > 255)
106 else if (f_upper_val < 0)
112 upper_val = static_cast<unsigned char>(f_upper_val);
115 if (f_lower_val > 255)
119 else if (f_lower_val < 0)
125 lower_val = static_cast<unsigned char>(f_lower_val);
130 void vvImageMapToWindowLevelColorsExecute(
131 vtkImageMapToWindowLevelColors *self,
132 vtkImageData *inData, T *inPtr,
133 vtkImageData *outData,
134 unsigned char *outPtr,
135 int outExt[6], int id, bool wl_mode)
137 int idxX, idxY, idxZ;
138 int extX, extY, extZ;
139 vtkIdType inIncX, inIncY, inIncZ;
140 vtkIdType outIncX, outIncY, outIncZ;
141 unsigned long count = 0;
142 unsigned long target;
143 int dataType = inData->GetScalarType();
144 int numberOfComponents,numberOfOutputComponents,outputFormat;
146 vtkScalarsToColors *lookupTable = self->GetLookupTable();
147 unsigned char *outPtr1;
151 double shift = self->GetWindow() / 2.0 - self->GetLevel();
152 double scale = 255.0 / self->GetWindow();
155 unsigned char lower_val, upper_val, result_val;
156 vtkImageMapToWindowLevelClamps( inData, self->GetWindow(),
158 lower, upper, lower_val, upper_val );
160 // find the region to loop over
161 extX = outExt[1] - outExt[0] + 1;
162 extY = outExt[3] - outExt[2] + 1;
163 extZ = outExt[5] - outExt[4] + 1;
165 target = static_cast<unsigned long>(extZ*extY/50.0);
168 // Get increments to march through data
169 inData->GetContinuousIncrements(outExt, inIncX, inIncY, inIncZ);
171 outData->GetContinuousIncrements(outExt, outIncX, outIncY, outIncZ);
172 numberOfComponents = inData->GetNumberOfScalarComponents();
173 numberOfOutputComponents = outData->GetNumberOfScalarComponents();
174 outputFormat = self->GetOutputFormat();
176 rowLength = extX*numberOfComponents;
178 // Loop through output pixels
181 for (idxZ = 0; idxZ < extZ; idxZ++)
183 for (idxY = 0; !self->AbortExecute && idxY < extY; idxY++)
189 self->UpdateProgress(count/(50.0*target));
199 lookupTable->MapScalarsThroughTable2(
201 static_cast<unsigned char *>(outPtr1),
202 dataType,extX,numberOfComponents,
206 unsigned short ushort_val;
207 for (idxX = 0; idxX < extX; idxX++)
211 ushort_val = lower_val;
213 else if (*iptr >= upper)
215 ushort_val = upper_val;
219 ushort_val = static_cast<unsigned char>((*iptr + shift)*scale);
221 *optr = static_cast<unsigned char>((*optr * ushort_val) >> 8);
222 switch (outputFormat)
225 *(optr+1) = static_cast<unsigned char>(
226 (*(optr+1) * ushort_val) >> 8);
227 *(optr+2) = static_cast<unsigned char>(
228 (*(optr+2) * ushort_val) >> 8);
232 *(optr+1) = static_cast<unsigned char>(
233 (*(optr+1) * ushort_val) >> 8);
234 *(optr+2) = static_cast<unsigned char>(
235 (*(optr+2) * ushort_val) >> 8);
237 case VTK_LUMINANCE_ALPHA:
241 iptr += numberOfComponents;
242 optr += numberOfOutputComponents;
248 for (idxX = 0; idxX < extX; idxX++)
252 result_val = lower_val;
254 else if (*iptr >= upper)
256 result_val = upper_val;
260 result_val = static_cast<unsigned char>((*iptr + shift)*scale);
263 switch (outputFormat)
266 *(optr+1) = result_val;
267 *(optr+2) = result_val;
271 *(optr+1) = result_val;
272 *(optr+2) = result_val;
274 case VTK_LUMINANCE_ALPHA:
278 iptr += numberOfComponents;
279 optr += numberOfOutputComponents;
282 outPtr1 += outIncY + extX*numberOfOutputComponents;
283 inPtr1 += inIncY + rowLength;
291 void vvImageMapToWLColors::ThreadedRequestData(
292 vtkInformation *vtkNotUsed(request),
293 vtkInformationVector **vtkNotUsed(inputVector),
294 vtkInformationVector *vtkNotUsed(outputVector),
295 vtkImageData ***inData,
296 vtkImageData **outData,
297 int outExt[6], int id)
299 void *inPtr = inData[0][0]->GetScalarPointerForExtent(outExt);
300 void *outPtr = outData[0]->GetScalarPointerForExtent(outExt);
302 switch (inData[0][0]->GetScalarType())
305 vvImageMapToWindowLevelColorsExecute(this,
307 static_cast<VTK_TT *>(inPtr),
309 static_cast<unsigned char *>(outPtr),
313 vtkErrorMacro(<< "Execute: Unknown ScalarType");