CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 
34 /*
35  * sscImageLookupTable2D.cpp
36  *
37  * Created on: Jan 9, 2009
38  * Author: christiana
39  */
40 
41 #include "cxImageLUT2D.h"
42 
43 #include <QDomDocument>
44 #include <vtkLookupTable.h>
45 #include <vtkImageData.h>
46 #include <vtkColorTransferFunction.h>
47 #include <vtkPiecewiseFunction.h>
48 
49 #include "cxVector3D.h"
50 
51 
52 namespace cx
53 {
54 
56 {
57 }
58 
59 //void ImageLUT2D::setInitialTFFromImage(vtkImageDataPtr base)
60 //{
61 // double smin = base->GetScalarRange()[0];
62 // double smax = base->GetScalarRange()[1];
63 // double srange = smax - smin;
64 
65 // // this sets the initial opacity tf to full
66 // mOpacityMap.clear();
67 // this->addAlphaPoint(smin - 1, 0);
68 // this->addAlphaPoint(smin, 255);
69 
70 // // this also sets the initial lut to grayscale
71 // mColorMap.clear();
72 // this->addColorPoint(smin, Qt::black);
73 // this->addColorPoint(smax, Qt::white);
74 
90 
91 // this->internalsHaveChanged();
92 //}
93 
95 {
96  ImageLUT2DPtr retval(new ImageLUT2D());
97  retval->deepCopy(this);
98  return retval;
99 }
100 
102 {
103  double smin = image->GetScalarRange()[0];
104  double smax = image->GetScalarRange()[1];
105  double srange = smax - smin;
106  this->setWindow(srange);
107  this->setLevel(smin + srange / 2.0);
108 }
109 
111 {
112  if (!mOutputLUT)
113  {
114  mOutputLUT = vtkLookupTablePtr::New();
115  this->refreshOutputLUT();
116  }
117  return mOutputLUT;
118 }
119 
121 // * This is because the 2D renderer only handles llr+alpha.
122 // */
123 //void ImageLUT2D::buildOpacityMapFromLLRAlpha()
124 //{
125 // // REMOVED CA 2014-02-07 - TODO
126 // mOpacityMap.clear();
129 // this->addAlphaPoint(this->getLLR() - 1, 0);
130 // this->addAlphaPoint(this->getLLR(), this->getAlpha() * 255);
132 //}
133 
135 {
136  this->refreshOutputLUT();
138 }
139 
140 std::pair<int,int> ImageLUT2D::getMapsRange()
141 {
142  if (!mColorMap.empty() && !mOpacityMap.empty())
143  {
144  int imin = std::min(mColorMap.begin()->first, mOpacityMap.begin()->first);
145  int imax = std::max(mColorMap.rbegin()->first, mOpacityMap.rbegin()->first);
146  return std::make_pair(imin,imax);
147  }
148  else if (!mColorMap.empty())
149  {
150  int imin = mColorMap.begin()->first;
151  int imax = mColorMap.rbegin()->first;
152  return std::make_pair(imin,imax);
153  }
154  else if (!mOpacityMap.empty())
155  {
156  int imin = mOpacityMap.begin()->first;
157  int imax = mOpacityMap.rbegin()->first;
158  return std::make_pair(imin,imax);
159  }
160  else
161  {
162  return std::make_pair(0,0);
163  }
164 }
165 
166 void ImageLUT2D::refreshOutputLUT()
167 {
168  if (!mOutputLUT)
169  return;
170 
171  std::pair<int,int> range = this->getMapsRange();
172  int imin = range.first;
173  int imax = range.second;
174  if (imin==imax)
175  imax = imin+1;
176  int icount = imax - imin + 1;
177 
178  vtkLookupTablePtr lut = mOutputLUT;
179  lut->Build();
180  lut->SetNumberOfTableValues(icount);
181  lut->SetTableRange(imin, imax);
182 
183  vtkColorTransferFunctionPtr colorFunc = this->generateColorTF();
184  vtkPiecewiseFunctionPtr opacityFunc = this->generateOpacityTF();
185 
186  for (int i = 0; i < icount; ++i)
187  {
188  double* rgb = colorFunc->GetColor(i + imin);
189  double alpha = opacityFunc->GetValue(i + imin);
190  lut->SetTableValue(i, rgb[0], rgb[1], rgb[2], alpha);
191  }
192 
193  lut->Modified();
194 
195  // HACK WARNING!!!!!
196  // Setting vtkLookupTable::SetNumberOfTableValues > 256 causes
197  // crash in vtkImageMapToColors. Seen when upgrading vtk6.1->6.2
198  // This line hacks away the problem....
199  // http://vtk.1045678.n5.nabble.com/Crash-in-vtkImageMapToColors-when-upgrading-6-1-gt-6-2-td5730804.html
200  // Update: added fix in VTK head -> will be fixed in 6.3
201  unsigned char input = 0;
202  lut->MapScalarsThroughTable2(&input, &input, VTK_UNSIGNED_CHAR, 1, 1, 1);
203  // HACK END
204 }
205 
206 //---------------------------------------------------------
207 } // end namespace
208 //---------------------------------------------------------
209 
210 
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