Fraxinus  16.5.0-fx-rc3
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxUnsignedDerivedImage.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 #include "cxUnsignedDerivedImage.h"
35 
36 #include <vtkUnsignedCharArray.h>
37 #include <vtkImageData.h>
38 #include <vtkPointData.h>
39 #include <vtkDoubleArray.h>
40 
41 #include "cxImage.h"
42 
43 #include <vtkImageResample.h>
44 #include <vtkImageClip.h>
45 #include "vtkImageShiftScale.h"
46 
47 #include "cxImage.h"
48 #include "cxUtilHelpers.h"
49 #include "cxImageTF3D.h"
50 #include "cxImageLUT2D.h"
52 
53 
55 #include "cxLogger.h"
56 
57 typedef vtkSmartPointer<vtkDoubleArray> vtkDoubleArrayPtr;
58 
59 namespace cx
60 {
61 
63 {
64  boost::shared_ptr<UnsignedDerivedImage> retval;
65  retval.reset(new UnsignedDerivedImage(base));
66  return retval;
67 }
68 
69 UnsignedDerivedImage::UnsignedDerivedImage(ImagePtr base) : Image(base->getUid()+"_u", vtkImageDataPtr(), base->getName())
70 {
71  this->mBase = base;
72 
73  // redirected signals:
74  connect(base.get(), SIGNAL(transformChanged()), this, SIGNAL(transformChanged()));
75  connect(base.get(), SIGNAL(propertiesChanged()), this, SIGNAL(propertiesChanged()));
76  connect(base.get(), SIGNAL(clipPlanesChanged()), this, SIGNAL(clipPlanesChanged()));
77  connect(base.get(), SIGNAL(cropBoxChanged()), this, SIGNAL(cropBoxChanged()));
78 
79  // override signals:
80  connect(base.get(), SIGNAL(transferFunctionsChanged()), this, SLOT(unsignedTransferFunctionsChangedSlot()));
81  connect(base.get(), SIGNAL(vtkImageDataChanged()), this, SLOT(unsignedImageChangedSlot()));
82 
83  connect(this, SIGNAL(transferFunctionsChanged()), this, SLOT(testSlot()));
84  this->unsignedImageChangedSlot();
85  this->unsignedTransferFunctionsChangedSlot();
86 }
87 
88 void UnsignedDerivedImage::testSlot()
89 {
90 }
91 
92 void UnsignedDerivedImage::unsignedTransferFunctionsChangedSlot()
93 {
94  ImagePtr base = mBase.lock();
95  if (!base)
96  return;
97 
98 
99  // this is a slow operation, triggered every time the mBase is changed. Take care.
100 
101  int shift = this->findShift();
102 
103  ImageTF3DPtr TF3D = base->getTransferFunctions3D()->createCopy();
104  ImageLUT2DPtr LUT2D = base->getLookupTable2D()->createCopy();
105  TF3D->shift(shift);
106  LUT2D->shift(shift);
107  this->setLookupTable2D(LUT2D);
108  this->setTransferFunctions3D(TF3D);
109 }
110 
111 void UnsignedDerivedImage::unsignedImageChangedSlot()
112 {
113  this->setVtkImageData(this->convertImage());
114 }
115 
116 int UnsignedDerivedImage::findShift()
117 {
118  ImagePtr base = mBase.lock();
119  if (!base)
120  return 0;
121  vtkImageDataPtr input = base->getBaseVtkImageData();
122 
123  if (input->GetScalarTypeMin() >= 0)
124  return 0;
125 
126  // start by shifting up to zero
127  int shift = -input->GetScalarRange()[0];
128  // if CT: always shift by 1024 (houndsfield units definition)
129  if (base->getModality().contains("CT", Qt::CaseInsensitive))
130  shift = 1024;
131  return shift;
132 }
133 
134 vtkImageDataPtr UnsignedDerivedImage::convertImage()
135 {
136  vtkImageDataPtr retval;
137 
138  ImagePtr base = mBase.lock();
139  if (!base)
140  return retval;
141 
142  int shift = this->findShift();
143  vtkImageDataPtr input = base->getBaseVtkImageData();
144 
145  vtkImageShiftScalePtr cast = vtkImageShiftScalePtr::New();
146  cast->SetInputData(input);
147  cast->ClampOverflowOn();
148 
149  cast->SetShift(shift);
150 
151  // total intensity range of voxels:
152  double range = input->GetScalarRange()[1] - input->GetScalarRange()[0];
153 
154  // to to fit within smallest type
155  if (range <= VTK_UNSIGNED_SHORT_MAX-VTK_UNSIGNED_SHORT_MIN)
156  cast->SetOutputScalarType(VTK_UNSIGNED_SHORT);
157  else if (range <= VTK_UNSIGNED_INT_MAX-VTK_UNSIGNED_INT_MIN)
158  cast->SetOutputScalarType(VTK_UNSIGNED_INT);
159 // else if (range <= VTK_UNSIGNED_LONG_MAX-VTK_UNSIGNED_LONG_MIN) // not supported by vtk - it seems (crash in rendering)
160 // cast->SetOutputScalarType(VTK_UNSIGNED_LONG);
161  else
162  cast->SetOutputScalarType(VTK_UNSIGNED_INT);
163 
164  cast->Update();
165 // if (verbose)
166  report(QString("Converting image %1 from %2 to %3").arg(this->getName()).arg(input->GetScalarTypeAsString()).arg(cast->GetOutput()->GetScalarTypeAsString()));
167  retval = cast->GetOutput();
168  return retval;
169 }
170 
172 {
174 }
175 
176 }
virtual void setTransferFunctions3D(ImageTF3DPtr transferFuntion)
Definition: cxImage.cpp:332
vtkSmartPointer< class vtkImageShiftScale > vtkImageShiftScalePtr
virtual void setVtkImageData(const vtkImageDataPtr &data, bool resetTransferFunctions=true)
Definition: cxImage.cpp:289
void propertiesChanged()
emitted when one of the metadata properties (uid, name etc) changes
#define CALL_IN_WEAK_PTR(weak_base, func, defarg)
virtual void setLookupTable2D(ImageLUT2DPtr imageLookupTable2D)
Definition: cxImage.cpp:351
void transformChanged()
emitted when transform is changed
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
static ImagePtr create(ImagePtr base)
void clipPlanesChanged()
boost::shared_ptr< class ImageLUT2D > ImageLUT2DPtr
A volumetric data set.
Definition: cxImage.h:66
vtkSmartPointer< vtkDoubleArray > vtkDoubleArrayPtr
void transferFunctionsChanged()
emitted when image transfer functions in 2D or 3D are changed.
Identification of a Coordinate system.
Class that holds an unsigned version of a base Image.
virtual QString getName() const
void report(QString msg)
Definition: cxLogger.cpp:90
void cropBoxChanged()
void vtkImageDataChanged()
emitted when the vktimagedata are invalidated and must be retrieved anew.
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
boost::shared_ptr< class ImageTF3D > ImageTF3DPtr
virtual CoordinateSystem getCoordinateSystem()