GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GoMegaImageStructure.h
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  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  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
34 
35 #ifndef GoMegaImageStructure_H
36 #define GoMegaImageStructure_H
37 
38 // Required for dynamic libs on Windows (QGoIOExport)
39 #include "QGoIOConfigure.h"
40 
41 // includes from external libs
42 // VTK
43 #include "vtkSmartPointer.h"
44 #include "vtkLookupTable.h"
45 #include "vtkImageData.h"
46 #include "vtkPiecewiseFunction.h"
47 #include "vtkImageAccumulate.h"
48 
49 // convert VTK to ITK
50 #include "itkImage.h"
51 #include "itkVTKImageImport.h"
52 #include "vtkImageExport.h"
53 #include "vtkitkAdaptor.h"
54 
60 struct QGOIO_EXPORT GoMegaImageStructure
61 {
62  unsigned int Index;
63  vtkSmartPointer<vtkLookupTable> LUT;
64  vtkSmartPointer<vtkImageData> Image;
65  std::vector< double > Color;
66  bool Visibility;
67  std::string Name;
68  std::map<unsigned int, unsigned int> Alpha;
69  vtkSmartPointer<vtkPiecewiseFunction> OpacityTF;
70  vtkSmartPointer<vtkImageAccumulate> Histogram;
71  int Gamma;
72  int Min;
73  int Max;
74 
76  GoMegaImageStructure(unsigned int iIndex,
77  vtkSmartPointer<vtkLookupTable> iLUT,
78  vtkSmartPointer<vtkImageData> iImage,
79  std::vector< double > iColor,
80  bool iVisibility,
81  std::string iName):
82  Index(iIndex), LUT(iLUT), Color(iColor), Visibility(iVisibility),
83  Name(iName)
84  {
85  Image = iImage;
86 
87  // alpha, modified by user on clicks
88  Alpha[0] = 0;
89  Alpha[255] = Color[3];
90 
91  // gamma
92  Gamma = 100;
93  // min
94  Min = 0;
95  // max
96  Max = LUT->GetRange()[1];
97 
98  OpacityTF = vtkSmartPointer<vtkPiecewiseFunction>::New();
99  OpacityTF->AddPoint(0, 0);
100  OpacityTF->AddPoint(LUT->GetRange()[1], Color[3]/255);
101 
102  //compute histogram
103  double* range;
104  range = Image->GetScalarRange();
105  Histogram = vtkSmartPointer<vtkImageAccumulate>::New();
106  Histogram->SetInput( Image );
107  Histogram->SetComponentExtent(
108  0,
109  static_cast<int>(range[1])-static_cast<int>(range[0])-1,0,0,0,0 );
110  Histogram->SetComponentOrigin( range[0],0,0 );
111  Histogram->SetComponentSpacing( 1,0,0 );
112  Histogram->SetIgnoreZero( false );
113  Histogram->Update();
114  }
115 
116  // functions to modify the structure through the boost::multiindexcontainer
117  void setGamma(int iGamma)
118  {
119  Gamma = iGamma;
120  }
121 
122  void setMin(int iMin)
123  {
124  Min = iMin;
125  }
126 
127  void setMax(int iMax)
128  {
129  Max = iMax;
130  }
131 
132  void setLUTParameters(int iGamma, int iMin, int iMax)
133  {
134  Gamma = iGamma;
135  Min = iMin;
136  Max = iMax;
137  }
138 
139  void setLUT(vtkSmartPointer<vtkLookupTable> iLUT)
140  {
141  LUT = iLUT;
142  }
143 
144  void setVisibility(bool iVisibility)
145  {
146  Visibility = iVisibility;
147  }
148 
149  void setColor(std::vector<double> iColor)
150  {
151  Color = iColor;
152  }
153 
154  void setName(std::string iName)
155  {
156  Name = iName;
157  }
158 
159  void setImage(vtkImageData* iImage)
160  {
161  Image = iImage;
162 
163  // compute new histogram
164  double* range;
165  range = Image->GetScalarRange();
166 
167  Histogram->RemoveAllInputs();
168  Histogram->SetInput( Image );
169  Histogram->SetComponentExtent(
170  0,
171  static_cast<int>(range[1])-static_cast<int>(range[0])-1,0,0,0,0 );
172  Histogram->SetComponentOrigin( range[0],0,0 );
173  Histogram->SetComponentSpacing( 1,0,0 );
174  Histogram->SetIgnoreZero( false );
175  Histogram->Update();
176  }
177 
178  void set_PointsAlpha(std::map< unsigned int, unsigned int> iAlpha)
179  {
180  Alpha = iAlpha;
181  }
182 
183  /*
184  * \brief Convert a vtkImage to a itkImage. If we call after "ExtractROI",
185  * the dimension should be 3 all the time.
186  * (Even if we extract a2D region from a 3d image)
187  * \tparam PixelType type of pixel (unsigned char, etc.)
188  * \tparam VImageDimension dimension of the image (2 or 3)
189  * \param[in] iInput Pointer to a vtkImageData
190  * \return Pointer to an itkImage
191  */
192  template< class PixelType, const unsigned int VImageDimension >
193  typename itk::Image< PixelType, VImageDimension >::Pointer
194  Convert2ITK() const
195  {
196  //Export VTK image to ITK
197  vtkSmartPointer<vtkImageExport> exporter =
198  vtkSmartPointer<vtkImageExport>::New();
199  exporter->SetInput(Image);
200  exporter->Update();
201 
202  // ImageType
203  typedef itk::Image< PixelType, VImageDimension > ImageType;
204  // Import VTK Image to ITK
205  typedef itk::VTKImageImport< ImageType > ImageImportType;
206  typedef typename ImageImportType::Pointer ImageImportPointer;
207  ImageImportPointer importer = ImageImportType::New();
208 
209  ConnectPipelines< vtkImageExport, ImageImportPointer >(
210  exporter,
211  importer);
212 
213  typename ImageType::Pointer itkImage = importer->GetOutput();
214  itkImage->DisconnectPipeline();
215 
216  return itkImage;
217  }
218 
219  friend std::ostream& operator<<(std::ostream& os,const GoMegaImageStructure& e)
220  {
221  os<< "index: "<<e.Index<<std::endl;
222  return os;
223  }
224 };
225 
226 #endif // GoMegaImageStructure_H
void setImage(vtkImageData *iImage)
std::vector< double > Color
vtkSmartPointer< vtkLookupTable > LUT
Convenience structure to store visible image.
vtkSmartPointer< vtkImageAccumulate > Histogram
void setName(std::string iName)
void setLUTParameters(int iGamma, int iMin, int iMax)
vtkSmartPointer< vtkImageData > Image
friend std::ostream & operator<<(std::ostream &os, const GoMegaImageStructure &e)
void set_PointsAlpha(std::map< unsigned int, unsigned int > iAlpha)
void setVisibility(bool iVisibility)
itk::Image< PixelType, VImageDimension >::Pointer Convert2ITK() const
void setColor(std::vector< double > iColor)
vtkSmartPointer< vtkPiecewiseFunction > OpacityTF
GoMegaImageStructure(unsigned int iIndex, vtkSmartPointer< vtkLookupTable > iLUT, vtkSmartPointer< vtkImageData > iImage, std::vector< double > iColor, bool iVisibility, std::string iName)
void setLUT(vtkSmartPointer< vtkLookupTable > iLUT)
std::map< unsigned int, unsigned int > Alpha