18 #include "vtkObjectFactory.h"
19 #include <vtkColorTransferFunction.h>
20 #include <vtkImageData.h>
21 #include <vtkImageIterator.h>
22 #include <vtkInformation.h>
23 #include <vtkInformationVector.h>
24 #include <vtkLookupTable.h>
25 #include <vtkPiecewiseFunction.h>
27 #include <vtkStreamingDemandDrivenPipeline.h>
37 static const double PI = itk::Math::pi;
42 : m_LookupTable(nullptr), m_OpacityFunction(nullptr), m_MinOpacity(0.0), m_MaxOpacity(255.0)
53 unsigned long mTime = this->vtkObject::GetMTime();
56 if (this->m_LookupTable !=
nullptr)
58 time = this->m_LookupTable->GetMTime();
59 mTime = (time > mTime ? time : mTime);
67 if (m_LookupTable != lookupTable)
69 m_LookupTable = lookupTable;
81 if (m_OpacityFunction != opacityFunction)
83 m_OpacityFunction = opacityFunction;
95 T R = RGB[0], G = RGB[1], B = RGB[2], nR = (R < 0 ? 0 : (R > 255 ? 255 : R)) / 255,
96 nG = (G < 0 ? 0 : (G > 255 ? 255 : G)) / 255, nB = (B < 0 ? 0 : (B > 255 ? 255 : B)) / 255,
97 m = nR < nG ? (nR < nB ? nR : nB) : (nG < nB ? nG : nB),
98 theta = (T)(std::acos(0.5f * ((nR - nG) + (nR - nB)) / std::sqrt(std::pow(nR - nG, 2) + (nR - nB) * (nG - nB))) *
101 T H = 0, S = 0, I = 0;
103 H = (nB <= nG) ? theta : 360 - theta;
118 T H = (T)HSI[0], S = (T)HSI[1], I = (T)HSI[2], a = I * (1 - S), R = 0, G = 0, B = 0;
122 R = (T)(I * (1 + S * std::cos(H *
PI / 180) / std::cos((60 - H) *
PI / 180)));
129 G = (T)(I * (1 + S * std::cos(H *
PI / 180) / std::cos((60 - H) *
PI / 180)));
136 B = (T)(I * (1 + S * std::cos(H *
PI / 180) / std::cos((60 - H) *
PI / 180)));
142 RGB[0] = (T)(R < 0 ? 0 : (R > 255 ? 255 : R));
143 RGB[1] = (T)(G < 0 ? 0 : (G > 255 ? 255 : G));
144 RGB[2] = (T)(B < 0 ? 0 : (B > 255 ? 255 : B));
152 vtkImageData *inData,
153 vtkImageData *outData,
155 double *clippingBounds,
158 vtkImageIterator<T> inputIt(inData, outExt);
159 vtkImageIterator<T> outputIt(outData, outExt);
160 vtkLookupTable *lookupTable;
161 const int maxC = inData->GetNumberOfScalarComponents();
163 double tableRange[2];
165 lookupTable =
dynamic_cast<vtkLookupTable *
>(
self->GetLookupTable());
167 lookupTable->GetTableRange(tableRange);
170 const double scale = (tableRange[1] - tableRange[0] > 0 ? 255.0 / (tableRange[1] - tableRange[0]) : 0.0);
171 const double bias = tableRange[0] * scale;
174 const double scaleOpac =
175 (
self->GetMaxOpacity() -
self->GetMinOpacity() > 0 ? 255.0 / (
self->GetMaxOpacity() -
self->GetMinOpacity()) : 0.0);
176 const double biasOpac =
self->GetMinOpacity() * scaleOpac;
181 while (!outputIt.IsAtEnd())
183 T *inputSI = inputIt.BeginSpan();
184 T *outputSI = outputIt.BeginSpan();
185 T *outputSIEnd = outputIt.EndSpan();
187 if (y >= clippingBounds[2] && y < clippingBounds[3])
191 while (outputSI != outputSIEnd)
193 if (x >= clippingBounds[0] && x < clippingBounds[1])
195 double rgb[3], alpha, hsi[3];
198 rgb[0] =
static_cast<double>(*inputSI);
200 rgb[1] =
static_cast<double>(*inputSI);
202 rgb[2] =
static_cast<double>(*inputSI);
205 RGBtoHSI<double>(rgb, hsi);
206 hsi[2] = hsi[2] * 255.0 * scale - bias;
207 hsi[2] = (hsi[2] > 255.0 ? 255 : (hsi[2] < 0.0 ? 0 : hsi[2]));
209 HSItoRGB<double>(hsi, rgb);
211 *outputSI =
static_cast<T
>(rgb[0]);
213 *outputSI =
static_cast<T
>(rgb[1]);
215 *outputSI =
static_cast<T
>(rgb[2]);
218 unsigned char finalAlpha = 255;
224 alpha =
static_cast<double>(*inputSI);
226 alpha = alpha * scaleOpac - biasOpac;
231 else if (alpha < 0.0)
235 finalAlpha =
static_cast<unsigned char>(alpha);
237 for (
int c = 4; c < maxC; c++)
241 *outputSI =
static_cast<T
>(finalAlpha);
262 while (outputSI != outputSIEnd)
287 vtkImageIterator<T> inputIt(inData, outExt);
288 vtkImageIterator<unsigned char> outputIt(outData, outExt);
290 double tableRange[2];
293 vtkLookupTable *lookupTable =
dynamic_cast<vtkLookupTable *
>(
self->GetLookupTable());
294 lookupTable->GetTableRange(tableRange);
297 const int *realLookupTable =
reinterpret_cast<int *
>(lookupTable->GetTable()->GetPointer(0));
298 int maxIndex = lookupTable->GetNumberOfColors() - 1;
300 const float scale = (tableRange[1] - tableRange[0] > 0 ? (maxIndex + 1) / (tableRange[1] - tableRange[0]) : 0.0);
302 float bias = -tableRange[0] * scale;
307 while (!outputIt.IsAtEnd())
309 unsigned char *outputSI = outputIt.BeginSpan();
310 unsigned char *outputSIEnd = outputIt.EndSpan();
312 T *inputSI = inputIt.BeginSpan();
314 while (outputSI != outputSIEnd)
317 int idx =
static_cast<int>(*inputSI * scale + bias);
321 else if (idx > maxIndex)
324 *
reinterpret_cast<int *
>(outputSI) = realLookupTable[idx];
340 vtkImageData *inData,
341 vtkImageData *outData,
343 double *clippingBounds,
346 vtkImageIterator<T> inputIt(inData, outExt);
347 vtkImageIterator<unsigned char> outputIt(outData, outExt);
348 vtkScalarsToColors *lookupTable =
self->GetLookupTable();
353 while (!outputIt.IsAtEnd())
355 unsigned char *outputSI = outputIt.BeginSpan();
356 unsigned char *outputSIEnd = outputIt.EndSpan();
359 if (y >= clippingBounds[2] && y < clippingBounds[3])
361 T *inputSI = inputIt.BeginSpan();
365 while (outputSI != outputSIEnd)
368 if (x >= clippingBounds[0] && x < clippingBounds[1])
371 double grayValue =
static_cast<double>(*inputSI);
373 *
reinterpret_cast<int *
>(outputSI) = *reinterpret_cast<int *>(lookupTable->MapValue(grayValue));
378 *
reinterpret_cast<int *
>(outputSI) = 0;
389 while (outputSI != outputSIEnd)
391 *
reinterpret_cast<int *
>(outputSI) = 0;
407 vtkImageData *inData,
408 vtkImageData *outData,
410 double *clippingBounds,
413 vtkImageIterator<T> inputIt(inData, outExt);
414 vtkImageIterator<unsigned char> outputIt(outData, outExt);
415 vtkColorTransferFunction *lookupTable =
dynamic_cast<vtkColorTransferFunction *
>(
self->GetLookupTable());
416 vtkPiecewiseFunction *opacityFunction =
self->GetOpacityPiecewiseFunction();
421 while (!outputIt.IsAtEnd())
423 unsigned char *outputSI = outputIt.BeginSpan();
424 unsigned char *outputSIEnd = outputIt.EndSpan();
427 if (y >= clippingBounds[2] && y < clippingBounds[3])
429 T *inputSI = inputIt.BeginSpan();
433 while (outputSI != outputSIEnd)
436 if (x >= clippingBounds[0] && x < clippingBounds[1])
439 double grayValue =
static_cast<double>(*inputSI);
444 lookupTable->GetColor(grayValue, rgba);
447 rgba[3] = opacityFunction->GetValue(grayValue);
449 for (
int i = 0; i < 4; ++i)
451 outputSI[i] =
static_cast<unsigned char>(255.0 * rgba[i] + 0.5);
457 *
reinterpret_cast<int *
>(outputSI) = 0;
468 while (outputSI != outputSIEnd)
470 *
reinterpret_cast<int *
>(outputSI) = 0;
482 vtkInformationVector **inputVector,
483 vtkInformationVector *outputVector)
485 vtkInformation *outInfo = outputVector->GetInformationObject(0);
488 this->CopyInputArrayAttributesToOutput(request, inputVector, outputVector);
490 vtkDataObject::SetPointDataActiveScalarInfo(outInfo, VTK_UNSIGNED_CHAR, 4);
498 if (inData->GetNumberOfScalarComponents() > 2)
500 switch (inData->GetScalarType())
505 vtkErrorMacro(<<
"Execute: Unknown ScalarType");
511 bool dontClip = extent[2] >= m_ClippingBounds[2] && extent[3] <= m_ClippingBounds[3] &&
512 extent[0] >= m_ClippingBounds[0] && extent[1] <= m_ClippingBounds[1];
517 vtkLookupTable *vlt =
dynamic_cast<vtkLookupTable *
>(this->
GetLookupTable());
518 vtkColorTransferFunction *ctf =
dynamic_cast<vtkColorTransferFunction *
>(this->
GetLookupTable());
520 bool linearLookupTable = vlt && vlt->GetScale() == VTK_SCALE_LINEAR;
522 bool useFast = dontClip && linearLookupTable;
526 switch (inData->GetScalarType())
529 this, inData, outData, extent, m_ClippingBounds, static_cast<VTK_TT *>(
nullptr)));
531 vtkErrorMacro(<<
"Execute: Unknown ScalarType");
537 switch (inData->GetScalarType())
542 vtkErrorMacro(<<
"Execute: Unknown ScalarType");
548 switch (inData->GetScalarType())
551 this, inData, outData, extent, m_ClippingBounds, static_cast<VTK_TT *>(
nullptr)));
553 vtkErrorMacro(<<
"Execute: Unknown ScalarType");
567 m_MinOpacity = minOpacity;
577 m_MaxOpacity = maxOpacity;
587 for (
unsigned int i = 0; i < 4; ++i)
588 m_ClippingBounds[i] = bounds[i];
double GetMinOpacity() const
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 *)
void SetLookupTable(vtkScalarsToColors *lookupTable)
Set the lookup table for the RGB level window.
vtkScalarsToColors * GetLookupTable()
Get the lookup table for the RGB level window.
~vtkMitkLevelWindowFilter()
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)
virtual unsigned long int GetMTime() override
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 *)
void SetMaxOpacity(double maxOpacity)
Get/Set the upper window opacity for the alpha level window.
Applies the grayvalue or color/opacity level window to scalar or RGB(A) images.
vtkMitkLevelWindowFilter()
double GetMaxOpacity() const
int RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
void vtkApplyLookupTableOnScalarsCTF(vtkMitkLevelWindowFilter *self, vtkImageData *inData, vtkImageData *outData, int outExt[6], double *clippingBounds, T *)
void SetMinOpacity(double minOpacity)
Get/Set the lower window opacity for the alpha level window.