Chaste  Build::
ImageReader.cpp
1 /*
2 
3  Copyright (c) 2005-2015, University of Oxford.
4  All rights reserved.
5 
6  University of Oxford means the Chancellor, Masters and Scholars of the
7  University of Oxford, having an administrative office at Wellington
8  Square, Oxford OX1 2JD, UK.
9 
10  This file is part of Chaste.
11 
12  Redistribution and use in source and binary forms, with or without
13  modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34  */
35 
36 #include "Exception.hpp"
37 #include <boost/filesystem.hpp>
38 #define _BACKWARD_BACKWARD_WARNING_H 1 //Cut out the vtk deprecated warning
39 #include <vtkTIFFReader.h>
40 #include <vtkPNGReader.h>
41 #include <vtkJPEGReader.h>
42 #include <vtkBMPReader.h>
43 #include <vtkImageResize.h>
44 #include <vtkImageLuminance.h>
45 #include "ImageReader.hpp"
46 
48  : mpVtkImage(),
49  mFilepath(),
50  mResizeX(1.0),
51  mResizeY(1.0),
52  mResizeZ(1.0)
53 {
54 
55 }
56 
57 boost::shared_ptr<ImageReader> ImageReader::Create()
58 {
59  MAKE_PTR(ImageReader, pSelf);
60  return pSelf;
61 }
62 
64 {
65 
66 }
67 
68 
69 vtkSmartPointer<vtkImageData> ImageReader::GetImage()
70 {
71  if(mpVtkImage)
72  {
73  return mpVtkImage;
74  }
75  else
76  {
77  EXCEPTION("No image data has been set.");
78  }
79 }
80 
81 void ImageReader::SetFilename(const std::string& filename)
82 {
83  mFilepath = filename;
84 }
85 
86 void ImageReader::SetImageResizeFactors(double factorX, double factorY, double factorZ)
87 {
88  mResizeX = factorX;
89  mResizeY = factorY;
90  mResizeZ = factorZ;
91 }
92 
94 {
95  // Get the file extension
96  if(mFilepath == "")
97  {
98  EXCEPTION("Input file not specified for image reader");
99  }
100  std::string file_extension = boost::filesystem::extension(mFilepath);
101 
102  if(file_extension == ".tif" or file_extension == ".tiff" or file_extension == ".TIF" or file_extension == ".TIFF")
103  {
104  vtkSmartPointer<vtkTIFFReader> p_reader = vtkSmartPointer<vtkTIFFReader>::New();
105  p_reader->SetFileName(mFilepath.c_str());
106  if(mResizeX==1.0 and mResizeY==1.0 and mResizeZ==1.0)
107  {
108  p_reader->Update();
109  mpVtkImage = p_reader->GetOutput();
110  }
111  else
112  {
113  vtkSmartPointer<vtkImageResize> p_resize = vtkSmartPointer<vtkImageResize>::New();
114  p_resize->SetInputConnection(p_reader->GetOutputPort());
115  p_resize->SetMagnificationFactors(mResizeX, mResizeY, mResizeZ);
116  p_resize->SetResizeMethodToMagnificationFactors();
117  p_resize->Update();
118  mpVtkImage = p_resize->GetOutput();
119  }
120  }
121  else if(file_extension == ".png" or file_extension == ".PNG")
122  {
123  vtkSmartPointer<vtkPNGReader> p_reader = vtkSmartPointer<vtkPNGReader>::New();
124  p_reader->SetFileName(mFilepath.c_str());
125  if(mResizeX==1.0 and mResizeY==1.0 and mResizeZ==1.0)
126  {
127  p_reader->Update();
128 
129  // RGB to scalar
130  vtkSmartPointer<vtkImageLuminance> p_lum = vtkSmartPointer<vtkImageLuminance>::New();
131  p_lum->SetInputConnection(p_reader->GetOutputPort());
132  p_lum->Update();
133  mpVtkImage = p_lum->GetOutput();
134  }
135  else
136  {
137  vtkSmartPointer<vtkImageResize> p_resize = vtkSmartPointer<vtkImageResize>::New();
138  p_resize->SetInputConnection(p_reader->GetOutputPort());
139  p_resize->SetMagnificationFactors(mResizeX, mResizeY, mResizeZ);
140  p_resize->SetResizeMethodToMagnificationFactors();
141  p_resize->Update();
142 
143  // RGB to scalar
144  vtkSmartPointer<vtkImageLuminance> p_lum = vtkSmartPointer<vtkImageLuminance>::New();
145  p_lum->SetInputConnection(p_resize->GetOutputPort());
146  p_lum->Update();
147  mpVtkImage = p_lum->GetOutput();
148  }
149  }
150  else if(file_extension == ".jpg" or file_extension == ".JPG")
151  {
152  vtkSmartPointer<vtkJPEGReader> p_reader = vtkSmartPointer<vtkJPEGReader>::New();
153  p_reader->SetFileName(mFilepath.c_str());
154  if(mResizeX==1.0 and mResizeY==1.0 and mResizeZ==1.0)
155  {
156  p_reader->Update();
157  mpVtkImage = p_reader->GetOutput();
158  }
159  else
160  {
161  vtkSmartPointer<vtkImageResize> p_resize = vtkSmartPointer<vtkImageResize>::New();
162  p_resize->SetInputConnection(p_reader->GetOutputPort());
163  p_resize->SetMagnificationFactors(mResizeX, mResizeY, mResizeZ);
164  p_resize->SetResizeMethodToMagnificationFactors();
165  p_resize->Update();
166  mpVtkImage = p_resize->GetOutput();
167  }
168  }
169  else if(file_extension == ".bmp" or file_extension == ".BMP")
170  {
171  vtkSmartPointer<vtkBMPReader> p_reader = vtkSmartPointer<vtkBMPReader>::New();
172  p_reader->SetFileName(mFilepath.c_str());
173  if(mResizeX==1.0 and mResizeY==1.0 and mResizeZ==1.0)
174  {
175  p_reader->Update();
176  mpVtkImage = p_reader->GetOutput();
177  }
178  else
179  {
180  vtkSmartPointer<vtkImageResize> p_resize = vtkSmartPointer<vtkImageResize>::New();
181  p_resize->SetInputConnection(p_reader->GetOutputPort());
182  p_resize->SetMagnificationFactors(mResizeX, mResizeY, mResizeZ);
183  p_resize->SetResizeMethodToMagnificationFactors();
184  p_resize->Update();
185  mpVtkImage = p_resize->GetOutput();
186  }
187  }
188  else
189  {
190  EXCEPTION("Input file extension not recognized");
191  }
192 
193  if(!mpVtkImage)
194  {
195  EXCEPTION("Image reading failed.");
196  }
197 }
vtkSmartPointer< vtkImageData > mpVtkImage
Definition: ImageReader.hpp:56
double mResizeX
Definition: ImageReader.hpp:66
double mResizeY
Definition: ImageReader.hpp:71
void SetFilename(const std::string &rFilename)
Definition: ImageReader.cpp:81
void SetImageResizeFactors(double factorX, double factorY=1.0, double factorZ=1.0)
Definition: ImageReader.cpp:86
static boost::shared_ptr< ImageReader > Create()
Definition: ImageReader.cpp:57
std::string mFilepath
Definition: ImageReader.hpp:61
vtkSmartPointer< vtkImageData > GetImage()
Definition: ImageReader.cpp:69
double mResizeZ
Definition: ImageReader.hpp:76