Orfeo Toolbox  4.0
otbWrapperComplexInputImageParameter.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
19 #include "itksys/SystemTools.hxx"
20 #include "otbImageFileReader.h"
21 #include "itkCastImageFilter.h"
23 #include "otbWrapperTypes.h"
24 
25 
26 namespace otb
27 {
28 namespace Wrapper
29 {
30 
32 {
33  this->SetName("Complex Input Image");
34  this->SetKey("cin");
35  m_FileName="";
37  m_UseFilename = true;
38  this->ClearValue();
39 }
40 
42 {
43 }
44 
45 bool
46 ComplexInputImageParameter::SetFromFileName(const std::string& filename)
47 {
48  // First clear previous file choosen
49  this->ClearValue();
50 
51  // No file existence is done here :
52  // - Done in the reader
53  // - allow appending additional information to the filename
54  // myfile.tif:2 for example, or myfile.tif:nocarto
55  if (!filename.empty())
56  {
58  reader->SetFileName(filename);
59 
60  try
61  {
62  reader->UpdateOutputInformation();
63  }
64  catch(itk::ExceptionObject & /*err*/)
65  {
66  return false;
67  }
68 
69  // the specified filename is valid => store the value
70  m_FileName = filename;
71  m_UseFilename = true;
72  SetActive(true);
73  return true;
74  }
75  return false;
76 }
77 
78 
81 {
82  return this->GetImage<ComplexFloatVectorImageType>();
83 }
84 
85 
86 #define otbGetImageMacro(image) \
87  image##Type * \
88  ComplexInputImageParameter::Get##image () \
89  { \
90  return this->GetImage< image##Type > (); \
91  }
92 
93 otbGetImageMacro(ComplexFloatImage);
94 otbGetImageMacro(ComplexDoubleImage);
95 
96 otbGetImageMacro(ComplexFloatVectorImage);
97 otbGetImageMacro(ComplexDoubleVectorImage);
98 
99 template <class TOutputImage>
100 TOutputImage *
102 {
103  // Used m_PreviousFileName because if not, when the user call twice GetImage,
104  // it without changing the filename, it returns 2 different
105  // image pointers
106  // Only one image type can be used
107 
108  // 2 cases : the user set a filename vs. the user set an image
109  if (m_UseFilename)
110  {
111  if( m_PreviousFileName!=m_FileName && !m_FileName.empty() )
112  {
114  // A new valid filename has been given : a reader is created
116  typedef otb::ImageFileReader<TOutputImage> ReaderType;
117  typename ReaderType::Pointer reader = ReaderType::New();
118  reader->SetFileName(m_FileName);
119  try
120  {
121  reader->UpdateOutputInformation();
122  }
123  catch(itk::ExceptionObject &)
124  {
125  this->ClearValue();
126  }
127 
128  m_Image = reader->GetOutput();
129  m_Reader = reader;
130 
131  // Pay attention, don't return m_Image because it is a ImageBase...
132  return reader->GetOutput();
133  }
134  else
135  {
136  // In this case, the reader and the image should already be there
137  if (m_Image.IsNull())
138  {
139  itkExceptionMacro("No input image or filename detected...");
140  }
141  else
142  {
143  // Check if the image type asked here is the same as the one used for the reader
144  if (dynamic_cast<TOutputImage*> (m_Image.GetPointer()))
145  {
146  return dynamic_cast<TOutputImage*> (m_Image.GetPointer());
147  }
148  else
149  {
150  itkExceptionMacro("Cannot ask a different image type");
151  }
152  }
153  }
154  }
155  else
156  {
158  if( m_Image.IsNull() )
159  {
160  itkExceptionMacro("No input image or filename detected...");
161  }
162  else
163  {
164  if (dynamic_cast<ComplexFloatVectorImageType*>(m_Image.GetPointer()))
165  {
166  return CastImage<ComplexFloatVectorImageType, TOutputImage>();
167  }
168  else if (dynamic_cast<ComplexDoubleVectorImageType*>(m_Image.GetPointer()))
169  {
170  return CastImage<ComplexDoubleVectorImageType, TOutputImage>();
171  }
172  else if (dynamic_cast<ComplexFloatImageType*>(m_Image.GetPointer()))
173  {
174  return CastImage<ComplexFloatImageType, TOutputImage>();
175  }
176  else if (dynamic_cast<ComplexDoubleImageType*>(m_Image.GetPointer()))
177  {
178  return CastImage<ComplexDoubleImageType, TOutputImage>();
179  }
180  else
181  {
182  itkExceptionMacro("Unknown image type");
183  }
184  }
185  }
186 }
187 
188 
189 template <class TComplexInputImage, class TOutputImage>
190 TOutputImage*
192 {
193  TComplexInputImage* realComplexInputImage = dynamic_cast<TComplexInputImage*>(m_Image.GetPointer());
194 
196  typename CasterType::Pointer caster = CasterType::New();
197 
198  caster->SetInput(realComplexInputImage);
199  caster->UpdateOutputInformation();
200 
201  m_Image = caster->GetOutput();
202  m_Caster = caster;
203 
204  return caster->GetOutput();
205 }
206 
207 
208 template <class TComplexInputImage, class TOutputImage>
209 TOutputImage*
211 {
212  TComplexInputImage* realComplexInputImage = dynamic_cast<TComplexInputImage*>(m_Image.GetPointer());
213 
215  typename CasterType::Pointer caster = CasterType::New();
216 
217  caster->SetInput(realComplexInputImage);
218  caster->UpdateOutputInformation();
219 
220  m_Image = caster->GetOutput();
221  m_Caster = caster;
222 
223  return caster->GetOutput();
224 }
225 
226 
227 #define otbCastImageMacro(ComplexInputImageType, OutputImageType, theMethod) \
228  template<> OutputImageType * \
229  ComplexInputImageParameter::CastImage<ComplexInputImageType , OutputImageType>() \
230  { \
231  return this->theMethod<ComplexInputImageType , OutputImageType>(); \
232  }
233 
234 #define otbGenericCastImageMacro(ComplexInputImageType, theMethod, prefix) \
235  otbCastImageMacro(ComplexInputImageType, ComplexFloat##prefix##ImageType, theMethod) \
236  otbCastImageMacro(ComplexInputImageType, ComplexDouble##prefix##ImageType, theMethod)
237 
238 
239 /*********************************************************************
240 ********************** Image -> Image
241 **********************************************************************/
242 
245 
246 
247 /*********************************************************************
248 ********************** VectorImage -> VectorImage
249 **********************************************************************/
252 
253  void
254 ComplexInputImageParameter::SetImage(ComplexFloatVectorImageType* image)
255 {
256  m_UseFilename = false;
257  this->SetImage<ComplexFloatVectorImageType>( image );
258 }
259 
260 
261 template <class TComplexInputImage>
262 void
263 ComplexInputImageParameter::SetImage(TComplexInputImage* image)
264 {
265  m_Image = image;
266 }
267 
268 
269 bool
271 {
272  if( m_FileName.empty() && m_Image.IsNull() )
273  return false;
274  else
275  return true;
276 }
277 
278 void
280 {
281  m_Image = NULL;
282  m_Reader = NULL;
283  m_Caster = NULL;
284  m_FileName = "";
286  m_UseFilename = true;
287 }
288 
289 
290 }
291 }
292 

Generated at Sat Mar 8 2014 16:28:48 for Orfeo Toolbox with doxygen 1.8.3.1