14 #include "vtkObjectFactory.h" 15 #include <vtkColorTransferFunction.h> 16 #include <vtkImageData.h> 17 #include <vtkImageIterator.h> 18 #include <vtkInformation.h> 19 #include <vtkInformationVector.h> 20 #include <vtkLookupTable.h> 21 #include <vtkPiecewiseFunction.h> 23 #include <vtkStreamingDemandDrivenPipeline.h> 33 static const double PI = itk::Math::pi;
38 : m_LookupTable(nullptr), m_OpacityFunction(nullptr), m_MinOpacity(0.0), m_MaxOpacity(255.0)
49 vtkMTimeType mTime = this->vtkObject::GetMTime();
52 if (this->m_LookupTable !=
nullptr)
54 time = this->m_LookupTable->GetMTime();
55 mTime = (time > mTime ? time : mTime);
63 if (m_LookupTable != lookupTable)
65 m_LookupTable = lookupTable;
77 if (m_OpacityFunction != opacityFunction)
79 m_OpacityFunction = opacityFunction;
91 T R = RGB[0], G = RGB[1], B = RGB[2], nR = (R < 0 ? 0 : (R > 255 ? 255 : R)) / 255,
92 nG = (G < 0 ? 0 : (G > 255 ? 255 : G)) / 255, nB = (B < 0 ? 0 : (B > 255 ? 255 : B)) / 255,
93 m = nR < nG ? (nR < nB ? nR : nB) : (nG < nB ? nG : nB),
94 theta = (T)(std::acos(0.5f * ((nR - nG) + (nR - nB)) / std::sqrt(std::pow(nR - nG, 2) + (nR - nB) * (nG - nB))) *
97 T H = 0, S = 0, I = 0;
99 H = (nB <= nG) ? theta : 360 - theta;
114 T H = (T)HSI[0], S = (T)HSI[1], I = (T)HSI[2], a = I * (1 - S), R = 0, G = 0, B = 0;
118 R = (T)(I * (1 + S * std::cos(H *
PI / 180) / std::cos((60 - H) *
PI / 180)));
125 G = (T)(I * (1 + S * std::cos(H *
PI / 180) / std::cos((60 - H) *
PI / 180)));
132 B = (T)(I * (1 + S * std::cos(H *
PI / 180) / std::cos((60 - H) *
PI / 180)));
138 RGB[0] = (T)(R < 0 ? 0 : (R > 255 ? 255 : R));
139 RGB[1] = (T)(G < 0 ? 0 : (G > 255 ? 255 : G));
140 RGB[2] = (T)(B < 0 ? 0 : (B > 255 ? 255 : B));
148 vtkImageData *inData,
149 vtkImageData *outData,
151 double *clippingBounds,
154 vtkImageIterator<T> inputIt(inData, outExt);
155 vtkImageIterator<T> outputIt(outData, outExt);
156 vtkLookupTable *lookupTable;
157 const int maxC = inData->GetNumberOfScalarComponents();
159 double tableRange[2];
161 lookupTable =
dynamic_cast<vtkLookupTable *
>(
self->GetLookupTable());
163 lookupTable->GetTableRange(tableRange);
166 const double scale = (tableRange[1] - tableRange[0] > 0 ? 255.0 / (tableRange[1] - tableRange[0]) : 0.0);
167 const double bias = tableRange[0] * scale;
170 const double scaleOpac =
171 (
self->GetMaxOpacity() -
self->GetMinOpacity() > 0 ? 255.0 / (
self->GetMaxOpacity() -
self->GetMinOpacity()) : 0.0);
172 const double biasOpac =
self->GetMinOpacity() * scaleOpac;
177 while (!outputIt.IsAtEnd())
179 T *inputSI = inputIt.BeginSpan();
180 T *outputSI = outputIt.BeginSpan();
181 T *outputSIEnd = outputIt.EndSpan();
183 if (y >= clippingBounds[2] && y < clippingBounds[3])
187 while (outputSI != outputSIEnd)
189 if (x >= clippingBounds[0] && x < clippingBounds[1])
191 double rgb[3], alpha, hsi[3];
194 rgb[0] =
static_cast<double>(*inputSI);
196 rgb[1] =
static_cast<double>(*inputSI);
198 rgb[2] =
static_cast<double>(*inputSI);
201 RGBtoHSI<double>(rgb, hsi);
202 hsi[2] = hsi[2] * 255.0 * scale - bias;
203 hsi[2] = (hsi[2] > 255.0 ? 255 : (hsi[2] < 0.0 ? 0 : hsi[2]));
205 HSItoRGB<double>(hsi, rgb);
207 *outputSI =
static_cast<T
>(rgb[0]);
209 *outputSI =
static_cast<T
>(rgb[1]);
211 *outputSI =
static_cast<T
>(rgb[2]);
214 unsigned char finalAlpha = 255;
220 alpha =
static_cast<double>(*inputSI);
222 alpha = alpha * scaleOpac - biasOpac;
227 else if (alpha < 0.0)
231 finalAlpha =
static_cast<unsigned char>(alpha);
233 for (
int c = 4; c < maxC; c++)
237 *outputSI =
static_cast<T
>(finalAlpha);
258 while (outputSI != outputSIEnd)
283 vtkImageIterator<T> inputIt(inData, outExt);
284 vtkImageIterator<unsigned char> outputIt(outData, outExt);
286 double tableRange[2];
289 auto *lookupTable =
dynamic_cast<vtkLookupTable *
>(
self->GetLookupTable());
290 lookupTable->GetTableRange(tableRange);
293 const int *realLookupTable =
reinterpret_cast<int *
>(lookupTable->GetTable()->GetPointer(0));
294 int maxIndex = lookupTable->GetNumberOfColors() - 1;
296 const float scale = (tableRange[1] - tableRange[0] > 0 ? (maxIndex + 1) / (tableRange[1] - tableRange[0]) : 0.0);
298 float bias = -tableRange[0] * scale;
303 while (!outputIt.IsAtEnd())
305 unsigned char *outputSI = outputIt.BeginSpan();
306 unsigned char *outputSIEnd = outputIt.EndSpan();
308 T *inputSI = inputIt.BeginSpan();
310 while (outputSI != outputSIEnd)
313 auto idx =
static_cast<int>(*inputSI * scale + bias);
317 else if (idx > maxIndex)
320 *
reinterpret_cast<int *
>(outputSI) = realLookupTable[idx];
336 vtkImageData *inData,
337 vtkImageData *outData,
339 double *clippingBounds,
342 vtkImageIterator<T> inputIt(inData, outExt);
343 vtkImageIterator<unsigned char> outputIt(outData, outExt);
344 vtkScalarsToColors *lookupTable =
self->GetLookupTable();
349 while (!outputIt.IsAtEnd())
351 unsigned char *outputSI = outputIt.BeginSpan();
352 unsigned char *outputSIEnd = outputIt.EndSpan();
355 if (y >= clippingBounds[2] && y < clippingBounds[3])
357 T *inputSI = inputIt.BeginSpan();
361 while (outputSI != outputSIEnd)
364 if (x >= clippingBounds[0] && x < clippingBounds[1])
367 auto grayValue =
static_cast<double>(*inputSI);
369 *
reinterpret_cast<int *
>(outputSI) = *reinterpret_cast<int *>(lookupTable->MapValue(grayValue));
374 *
reinterpret_cast<int *
>(outputSI) = 0;
385 while (outputSI != outputSIEnd)
387 *
reinterpret_cast<int *
>(outputSI) = 0;
403 vtkImageData *inData,
404 vtkImageData *outData,
406 double *clippingBounds,
409 vtkImageIterator<T> inputIt(inData, outExt);
410 vtkImageIterator<unsigned char> outputIt(outData, outExt);
411 auto *lookupTable =
dynamic_cast<vtkColorTransferFunction *
>(
self->GetLookupTable());
412 vtkPiecewiseFunction *opacityFunction =
self->GetOpacityPiecewiseFunction();
417 while (!outputIt.IsAtEnd())
419 unsigned char *outputSI = outputIt.BeginSpan();
420 unsigned char *outputSIEnd = outputIt.EndSpan();
423 if (y >= clippingBounds[2] && y < clippingBounds[3])
425 T *inputSI = inputIt.BeginSpan();
429 while (outputSI != outputSIEnd)
432 if (x >= clippingBounds[0] && x < clippingBounds[1])
435 auto grayValue =
static_cast<double>(*inputSI);
440 lookupTable->GetColor(grayValue, rgba);
443 rgba[3] = opacityFunction->GetValue(grayValue);
445 for (
int i = 0; i < 4; ++i)
447 outputSI[i] =
static_cast<unsigned char>(255.0 * rgba[i] + 0.5);
453 *
reinterpret_cast<int *
>(outputSI) = 0;
464 while (outputSI != outputSIEnd)
466 *
reinterpret_cast<int *
>(outputSI) = 0;
478 vtkInformationVector **inputVector,
479 vtkInformationVector *outputVector)
481 vtkInformation *outInfo = outputVector->GetInformationObject(0);
484 this->CopyInputArrayAttributesToOutput(request, inputVector, outputVector);
486 vtkDataObject::SetPointDataActiveScalarInfo(outInfo, VTK_UNSIGNED_CHAR, 4);
494 if (inData->GetNumberOfScalarComponents() > 2)
496 switch (inData->GetScalarType())
501 vtkErrorMacro(<<
"Execute: Unknown ScalarType");
507 bool dontClip = extent[2] >= m_ClippingBounds[2] && extent[3] <= m_ClippingBounds[3] &&
508 extent[0] >= m_ClippingBounds[0] && extent[1] <= m_ClippingBounds[1];
513 auto *vlt =
dynamic_cast<vtkLookupTable *
>(this->
GetLookupTable());
514 auto *ctf =
dynamic_cast<vtkColorTransferFunction *
>(this->
GetLookupTable());
516 bool linearLookupTable = vlt && vlt->GetScale() == VTK_SCALE_LINEAR;
518 bool useFast = dontClip && linearLookupTable;
522 switch (inData->GetScalarType())
525 this, inData, outData, extent, m_ClippingBounds, static_cast<VTK_TT *>(
nullptr)));
527 vtkErrorMacro(<<
"Execute: Unknown ScalarType");
533 switch (inData->GetScalarType())
538 vtkErrorMacro(<<
"Execute: Unknown ScalarType");
544 switch (inData->GetScalarType())
547 this, inData, outData, extent, m_ClippingBounds, static_cast<VTK_TT *>(
nullptr)));
549 vtkErrorMacro(<<
"Execute: Unknown ScalarType");
563 m_MinOpacity = minOpacity;
573 m_MaxOpacity = maxOpacity;
583 for (
unsigned int i = 0; i < 4; ++i)
584 m_ClippingBounds[i] = bounds[i];
void SetOpacityPiecewiseFunction(vtkPiecewiseFunction *opacityFunction)
Set the piecewise function used to map scalar to alpha component value (only used when the lookupTabl...
void vtkApplyLookupTableOnScalarsFast(vtkMitkLevelWindowFilter *self, vtkImageData *inData, vtkImageData *outData, int outExt[6], T *)
vtkMTimeType GetMTime() override
void SetLookupTable(vtkScalarsToColors *lookupTable)
Set the lookup table for the RGB level window.
vtkScalarsToColors * GetLookupTable()
Get the lookup table for the RGB level window.
void HSItoRGB(T *HSI, T *RGB)
void vtkApplyLookupTableOnScalars(vtkMitkLevelWindowFilter *self, vtkImageData *inData, vtkImageData *outData, int outExt[6], double *clippingBounds, T *)
vtkStandardNewMacro(vtkMitkLevelWindowFilter)
void SetClippingBounds(double *)
Set clipping bounds for the opaque part of the resliced 2d image.
void RGBtoHSI(T *RGB, T *HSI)
void ThreadedExecute(vtkImageData *inData, vtkImageData *outData, int extent[6], int id) override
Method for threaded execution of the filter.
void vtkApplyLookupTableOnRGBA(vtkMitkLevelWindowFilter *self, vtkImageData *inData, vtkImageData *outData, int outExt[6], double *clippingBounds, T *)
double GetMinOpacity() const
void SetMaxOpacity(double maxOpacity)
Get/Set the upper window opacity for the alpha level window.
double GetMaxOpacity() const
Applies the grayvalue or color/opacity level window to scalar or RGB(A) images.
vtkMitkLevelWindowFilter()
int RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
void vtkApplyLookupTableOnScalarsCTF(vtkMitkLevelWindowFilter *self, vtkImageData *inData, vtkImageData *outData, int outExt[6], double *clippingBounds, T *)
~vtkMitkLevelWindowFilter() override
void SetMinOpacity(double minOpacity)
Get/Set the lower window opacity for the alpha level window.