Fraxinus  18.10
An IGT application
cxImageLUT2D.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 
13 /*
14  * sscImageLookupTable2D.cpp
15  *
16  * Created on: Jan 9, 2009
17  * Author: christiana
18  */
19 
20 #include "cxImageLUT2D.h"
21 
22 #include <QDomDocument>
23 #include <vtkLookupTable.h>
24 #include <vtkImageData.h>
25 #include <vtkColorTransferFunction.h>
26 #include <vtkPiecewiseFunction.h>
27 
28 #include "cxVector3D.h"
29 
30 
31 namespace cx
32 {
33 
35 {
36 }
37 
38 //void ImageLUT2D::setInitialTFFromImage(vtkImageDataPtr base)
39 //{
40 // double smin = base->GetScalarRange()[0];
41 // double smax = base->GetScalarRange()[1];
42 // double srange = smax - smin;
43 
44 // // this sets the initial opacity tf to full
45 // mOpacityMap.clear();
46 // this->addAlphaPoint(smin - 1, 0);
47 // this->addAlphaPoint(smin, 255);
48 
49 // // this also sets the initial lut to grayscale
50 // mColorMap.clear();
51 // this->addColorPoint(smin, Qt::black);
52 // this->addColorPoint(smax, Qt::white);
53 
69 
70 // this->internalsHaveChanged();
71 //}
72 
74 {
75  ImageLUT2DPtr retval(new ImageLUT2D());
76  retval->deepCopy(this);
77  return retval;
78 }
79 
81 {
82  double smin = image->GetScalarRange()[0];
83  double smax = image->GetScalarRange()[1];
84  double srange = smax - smin;
85  this->setWindow(srange);
86  this->setLevel(smin + srange / 2.0);
87 }
88 
90 {
91  if (!mOutputLUT)
92  {
93  mOutputLUT = vtkLookupTablePtr::New();
94  this->refreshOutputLUT();
95  }
96  return mOutputLUT;
97 }
98 
100 // * This is because the 2D renderer only handles llr+alpha.
101 // */
102 //void ImageLUT2D::buildOpacityMapFromLLRAlpha()
103 //{
104 // // REMOVED CA 2014-02-07 - TODO
105 // mOpacityMap.clear();
108 // this->addAlphaPoint(this->getLLR() - 1, 0);
109 // this->addAlphaPoint(this->getLLR(), this->getAlpha() * 255);
111 //}
112 
114 {
115  this->refreshOutputLUT();
117 }
118 
119 std::pair<int,int> ImageLUT2D::getMapsRange()
120 {
121  if (!mColorMap.empty() && !mOpacityMap.empty())
122  {
123  int imin = std::min(mColorMap.begin()->first, mOpacityMap.begin()->first);
124  int imax = std::max(mColorMap.rbegin()->first, mOpacityMap.rbegin()->first);
125  return std::make_pair(imin,imax);
126  }
127  else if (!mColorMap.empty())
128  {
129  int imin = mColorMap.begin()->first;
130  int imax = mColorMap.rbegin()->first;
131  return std::make_pair(imin,imax);
132  }
133  else if (!mOpacityMap.empty())
134  {
135  int imin = mOpacityMap.begin()->first;
136  int imax = mOpacityMap.rbegin()->first;
137  return std::make_pair(imin,imax);
138  }
139  else
140  {
141  return std::make_pair(0,0);
142  }
143 }
144 
145 void ImageLUT2D::refreshOutputLUT()
146 {
147  if (!mOutputLUT)
148  return;
149 
150  std::pair<int,int> range = this->getMapsRange();
151  int imin = range.first;
152  int imax = range.second;
153  if (imin==imax)
154  imax = imin+1;
155  int icount = imax - imin + 1;
156 
157  vtkLookupTablePtr lut = mOutputLUT;
158  lut->Build();
159  lut->SetNumberOfTableValues(icount);
160  lut->SetTableRange(imin, imax);
161 
162  vtkColorTransferFunctionPtr colorFunc = this->generateColorTF();
163  vtkPiecewiseFunctionPtr opacityFunc = this->generateOpacityTF();
164 
165  for (int i = 0; i < icount; ++i)
166  {
167  double* rgb = colorFunc->GetColor(i + imin);
168  double alpha = opacityFunc->GetValue(i + imin);
169  lut->SetTableValue(i, rgb[0], rgb[1], rgb[2], alpha);
170  }
171 
172  lut->Modified();
173  lut->GetTable()->Modified();
174 
175  // HACK WARNING!!!!!
176  // Setting vtkLookupTable::SetNumberOfTableValues > 256 causes
177  // crash in vtkImageMapToColors. Seen when upgrading vtk6.1->6.2
178  // This line hacks away the problem....
179  // http://vtk.1045678.n5.nabble.com/Crash-in-vtkImageMapToColors-when-upgrading-6-1-gt-6-2-td5730804.html
180  // Update: added fix in VTK head -> will be fixed in 6.3
181  unsigned char input = 0;
182  lut->MapScalarsThroughTable2(&input, &input, VTK_UNSIGNED_CHAR, 1, 1, 1);
183  // HACK END
184 }
185 
186 //---------------------------------------------------------
187 } // end namespace
188 //---------------------------------------------------------
189 
190 
void transferFunctionsChanged()
IntIntMap mOpacityMap
ImageLUT2DPtr createCopy()
void setFullRangeWinLevel(vtkImageDataPtr image)
Set winlevel spanning the entire range.
vtkColorTransferFunctionPtr generateColorTF() const
vtkSmartPointer< class vtkPiecewiseFunction > vtkPiecewiseFunctionPtr
vtkSmartPointer< class vtkColorTransferFunction > vtkColorTransferFunctionPtr
virtual void internalsHaveChanged()
boost::shared_ptr< class ImageLUT2D > ImageLUT2DPtr
void setWindow(double val)
range [1..scalarMax-scalarMin]
vtkPiecewiseFunctionPtr generateOpacityTF() const
vtkSmartPointer< class vtkLookupTable > vtkLookupTablePtr
void setLevel(double val)
range [scalarMin..scalarMax]
ColorMap mColorMap
vtkLookupTablePtr getOutputLookupTable()
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
Namespace for all CustusX production code.