1 /*=========================================================================
3 Program: Visualization Toolkit
4 Module: $RCSfile: vtkImageMapToWindowLevelColors2.cxx,v $
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
14 =========================================================================*/
15 #include "vtkImageMapToWindowLevelColors2.h"
17 #include "vtkDataArray.h"
18 #include "vtkImageData.h"
19 #include "vtkInformation.h"
20 #include "vtkInformationVector.h"
21 #include "vtkObjectFactory.h"
22 #include "vtkScalarsToColors.h"
23 #include "vtkPointData.h"
25 vtkCxxRevisionMacro(vtkImageMapToWindowLevelColors2, "$Revision: 1.2 $")
26 vtkStandardNewMacro(vtkImageMapToWindowLevelColors2)
28 // Constructor sets default values
29 vtkImageMapToWindowLevelColors2::vtkImageMapToWindowLevelColors2()
35 vtkImageMapToWindowLevelColors2::~vtkImageMapToWindowLevelColors2()
39 //----------------------------------------------------------------------------
40 // This method checks to see if we can simply reference the input data
41 int vtkImageMapToWindowLevelColors2::RequestData(
42 vtkInformation *request,
43 vtkInformationVector **inputVector,
44 vtkInformationVector *outputVector)
46 vtkInformation *outInfo = outputVector->GetInformationObject(0);
47 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
49 vtkImageData *outData = vtkImageData::SafeDownCast(
50 outInfo->Get(vtkDataObject::DATA_OBJECT()));
51 vtkImageData *inData = vtkImageData::SafeDownCast(
52 inInfo->Get(vtkDataObject::DATA_OBJECT()));
54 // If LookupTable is null and window / level produces no change,
55 // then just pass the data
56 if (this->LookupTable == NULL &&
57 (inData->GetScalarType() == VTK_UNSIGNED_CHAR &&
58 this->Window == 255 && this->Level == 127.5))
60 vtkDebugMacro("ExecuteData: LookupTable not set, "\
61 "Window / Level at default, "\
62 "passing input to output.");
64 outData->SetExtent(inData->GetExtent());
65 outData->GetPointData()->PassData(inData->GetPointData());
66 this->DataWasPassed = 1;
69 // normal behaviour - skip up a level since we don't want to
70 // call the superclasses ExecuteData - it would pass the data if there
71 // is no lookup table even if there is a window / level - wrong
74 if (this->DataWasPassed)
76 outData->GetPointData()->SetScalars(NULL);
77 this->DataWasPassed = 0;
80 return this->vtkThreadedImageAlgorithm::RequestData(request, inputVector,
87 //----------------------------------------------------------------------------
88 int vtkImageMapToWindowLevelColors2::RequestInformation (
89 vtkInformation *vtkNotUsed(request),
90 vtkInformationVector **inputVector,
91 vtkInformationVector *outputVector)
93 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
94 vtkInformation *outInfo = outputVector->GetInformationObject(0);
96 vtkInformation *inScalarInfo =
97 vtkDataObject::GetActiveFieldInformation(inInfo,
98 vtkDataObject::FIELD_ASSOCIATION_POINTS, vtkDataSetAttributes::SCALARS);
101 vtkErrorMacro("Missing scalar field on input information!");
105 // If LookupTable is null and window / level produces no change,
106 // then the data will be passed
107 if ( this->LookupTable == NULL &&
108 (inScalarInfo->Get(vtkDataObject::FIELD_ARRAY_TYPE()) ==
110 this->Window == 255 && this->Level == 127.5) )
112 if (inScalarInfo->Get(vtkDataObject::FIELD_ARRAY_TYPE()) !=
115 vtkErrorMacro("ExecuteInformation: No LookupTable was set and input data is not VTK_UNSIGNED_CHAR!");
119 // no lookup table, pass the input if it was UNSIGNED_CHAR
120 vtkDataObject::SetPointDataActiveScalarInfo
121 (outInfo, VTK_UNSIGNED_CHAR,
122 inScalarInfo->Get(vtkDataObject::FIELD_NUMBER_OF_COMPONENTS()));
125 else // the lookup table was set or window / level produces a change
127 int numComponents = 4;
128 switch (this->OutputFormat)
136 case VTK_LUMINANCE_ALPHA:
143 vtkErrorMacro("ExecuteInformation: Unrecognized color format.");
146 vtkDataObject::SetPointDataActiveScalarInfo(outInfo, VTK_UNSIGNED_CHAR, numComponents);
153 * This templated routine calculates effective lower and upper limits
154 * for a window of values of type T, lower and upper.
157 void vtkImageMapToWindowLevelClamps ( vtkImageData *data, double w,
158 double l, T& lower, T& upper,
159 unsigned char &lower_val,
160 unsigned char &upper_val)
162 double f_lower, f_upper, f_lower_val, f_upper_val;
163 double adjustedLower, adjustedUpper;
166 data->GetPointData()->GetScalars()->GetDataTypeRange( range );
168 f_lower = l - fabs(w) / 2.0;
169 f_upper = f_lower + fabs(w);
171 // Set the correct lower value
172 if ( f_lower <= range[1])
174 if (f_lower >= range[0])
177 adjustedLower = f_lower;
181 lower = (T) range[0];
182 adjustedLower = range[0];
187 lower = (T) range[1];
188 adjustedLower = range[1];
191 // Set the correct upper value
192 if ( f_upper >= range[0])
194 if (f_upper <= range[1])
197 adjustedUpper = f_upper;
201 upper = (T) range[1];
202 adjustedUpper = range[1];
207 upper = (T) range [0];
208 adjustedUpper = range [0];
211 // now compute the lower and upper values
214 f_lower_val = 255.0*(adjustedLower - f_lower)/w;
215 f_upper_val = 255.0*(adjustedUpper - f_lower)/w;
219 f_lower_val = 255.0 + 255.0*(adjustedLower - f_lower)/w;
220 f_upper_val = 255.0 + 255.0*(adjustedUpper - f_lower)/w;
223 if (f_upper_val > 255)
227 else if (f_upper_val < 0)
233 upper_val = (unsigned char)(f_upper_val);
236 if (f_lower_val > 255)
240 else if (f_lower_val < 0)
246 lower_val = (unsigned char)(f_lower_val);
250 //----------------------------------------------------------------------------
251 // Small helper to do the clamp:
252 template <typename T>
253 void vtkClampHelper1(T* iptr, unsigned char *optr,
255 unsigned char lower_val, unsigned char upper_val,
256 double shift, double scale)
258 unsigned short ushort_val;
261 ushort_val = lower_val;
263 else if (*iptr >= upper)
265 ushort_val = upper_val;
269 ushort_val = (unsigned char) ((*iptr + shift)*scale);
271 *optr = (unsigned char)((*optr * ushort_val) >> 8);
274 //----------------------------------------------------------------------------
275 template <typename T>
276 void vtkClampHelper2(T* iptr, unsigned char *optr,
278 unsigned char lower_val, unsigned char upper_val,
279 double shift, double scale)
281 unsigned char result_val;
284 result_val = lower_val;
286 else if (*iptr >= upper)
288 result_val = upper_val;
292 result_val = (unsigned char) ((*iptr + shift)*scale);
297 //----------------------------------------------------------------------------
298 // This non-templated function executes the filter for any type of data.
300 void vtkImageMapToWindowLevelColors2Execute(
301 vtkImageMapToWindowLevelColors2 *self,
302 vtkImageData *inData, T *inPtr,
303 vtkImageData *outData,
304 unsigned char *outPtr,
305 int outExt[6], int id)
307 int idxX, idxY, idxZ;
308 int extX, extY, extZ;
309 vtkIdType inIncX, inIncY, inIncZ;
310 vtkIdType outIncX, outIncY, outIncZ;
311 unsigned long count = 0;
312 unsigned long target;
313 int dataType = inData->GetScalarType();
314 int numberOfComponents,numberOfOutputComponents,outputFormat;
316 vtkScalarsToColors *lookupTable = self->GetLookupTable();
317 unsigned char *outPtr1;
321 double shift = self->GetWindow() / 2.0 - self->GetLevel();
322 double scale = 255.0 / self->GetWindow();
325 unsigned char lower_val, upper_val;
326 vtkImageMapToWindowLevelClamps( inData, self->GetWindow(),
328 lower, upper, lower_val, upper_val );
330 // find the region to loop over
331 extX = outExt[1] - outExt[0] + 1;
332 extY = outExt[3] - outExt[2] + 1;
333 extZ = outExt[5] - outExt[4] + 1;
335 target = (unsigned long)(extZ*extY/50.0);
338 // Get increments to march through data
339 inData->GetContinuousIncrements(outExt, inIncX, inIncY, inIncZ);
341 outData->GetContinuousIncrements(outExt, outIncX, outIncY, outIncZ);
342 numberOfComponents = inData->GetNumberOfScalarComponents();
343 numberOfOutputComponents = outData->GetNumberOfScalarComponents();
344 outputFormat = self->GetOutputFormat();
346 rowLength = extX*numberOfComponents;
348 // Loop through output pixels
351 for (idxZ = 0; idxZ < extZ; idxZ++)
353 for (idxY = 0; !self->AbortExecute && idxY < extY; idxY++)
359 self->UpdateProgress(count/(50.0*target));
369 lookupTable->MapScalarsThroughTable2(inPtr1,(unsigned char *)outPtr1,
370 dataType,extX,numberOfComponents,
373 for (idxX = 0; idxX < extX; idxX++)
375 vtkClampHelper1<T>(iptr,optr,lower,upper,lower_val,upper_val,shift,scale);
376 switch (outputFormat)
379 vtkClampHelper1<T>(iptr+1,optr+1,lower,upper,lower_val,upper_val,shift,scale);
380 vtkClampHelper1<T>(iptr+2,optr+2,lower,upper,lower_val,upper_val,shift,scale);
384 vtkClampHelper1<T>(iptr+1,optr+1,lower,upper,lower_val,upper_val,shift,scale);
385 vtkClampHelper1<T>(iptr+2,optr+2,lower,upper,lower_val,upper_val,shift,scale);
387 case VTK_LUMINANCE_ALPHA:
391 iptr += numberOfComponents;
392 optr += numberOfOutputComponents;
397 for (idxX = 0; idxX < extX; idxX++)
399 vtkClampHelper2<T>(iptr,optr,lower,upper,lower_val,upper_val,shift,scale);
400 switch (outputFormat)
403 vtkClampHelper2<T>(iptr+1,optr+1,lower,upper,lower_val,upper_val,shift,scale);
404 vtkClampHelper2<T>(iptr+2,optr+2,lower,upper,lower_val,upper_val,shift,scale);
408 vtkClampHelper2<T>(iptr+1,optr+1,lower,upper,lower_val,upper_val,shift,scale);
409 vtkClampHelper2<T>(iptr+2,optr+2,lower,upper,lower_val,upper_val,shift,scale);
411 case VTK_LUMINANCE_ALPHA:
415 iptr += numberOfComponents;
416 optr += numberOfOutputComponents;
419 outPtr1 += outIncY + extX*numberOfOutputComponents;
420 inPtr1 += inIncY + rowLength;
427 //----------------------------------------------------------------------------
428 // This method is passed a input and output data, and executes the filter
429 // algorithm to fill the output from the input.
431 void vtkImageMapToWindowLevelColors2::ThreadedRequestData(
432 vtkInformation *vtkNotUsed(request),
433 vtkInformationVector **vtkNotUsed(inputVector),
434 vtkInformationVector *vtkNotUsed(outputVector),
435 vtkImageData ***inData,
436 vtkImageData **outData,
437 int outExt[6], int id)
439 void *inPtr = inData[0][0]->GetScalarPointerForExtent(outExt);
440 void *outPtr = outData[0]->GetScalarPointerForExtent(outExt);
442 switch (inData[0][0]->GetScalarType())
445 vtkImageMapToWindowLevelColors2Execute( this,
449 (unsigned char *)(outPtr),
453 vtkErrorMacro(<< "Execute: Unknown ScalarType");
458 //----------------------------------------------------------------------------
459 void vtkImageMapToWindowLevelColors2::PrintSelf(ostream& os, vtkIndent indent)
461 this->Superclass::PrintSelf(os,indent);
463 os << indent << "Window: " << this->Window << endl;
464 os << indent << "Level: " << this->Level << endl;