OTB  6.7.0
Orfeo Toolbox
otbWrapperInputImageParameter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbWrapperInputImageParameter_hxx
22 #define otbWrapperInputImageParameter_hxx
23 
25 
26 #include "otbWrapperCastImage.h"
27 
28 namespace otb
29 {
30 namespace Wrapper
31 {
32 
33 
34 #define CLAMP_IMAGE_IF( Out, In, image_base ) \
35  { \
36  In * in_image = dynamic_cast< In * >( image_base ); \
37  \
38  if( in_image ) \
39  return Cast< Out, In >( in_image ); \
40  }
41 
42 #define CLAMP_IMAGE_BASE( T, image_base ) \
43  { \
44  CLAMP_IMAGE_IF( T, UInt8VectorImageType, image_base ); \
45  CLAMP_IMAGE_IF( T, Int16VectorImageType, image_base ); \
46  CLAMP_IMAGE_IF( T, UInt16VectorImageType, image_base ); \
47  CLAMP_IMAGE_IF( T, Int32VectorImageType, image_base ); \
48  CLAMP_IMAGE_IF( T, UInt32VectorImageType, image_base ); \
49  \
50  CLAMP_IMAGE_IF( T, FloatVectorImageType, image_base ); \
51  CLAMP_IMAGE_IF( T, DoubleVectorImageType, image_base ); \
52  \
53  CLAMP_IMAGE_IF( T, ComplexInt16VectorImageType, image_base ); \
54  CLAMP_IMAGE_IF( T, ComplexInt32VectorImageType, image_base ); \
55  \
56  CLAMP_IMAGE_IF( T, ComplexFloatVectorImageType, image_base ); \
57  CLAMP_IMAGE_IF( T, ComplexDoubleVectorImageType, image_base ); \
58  \
59  CLAMP_IMAGE_IF( T, UInt8RGBImageType, image_base ); \
60  CLAMP_IMAGE_IF( T, UInt8RGBAImageType, image_base ); \
61  \
62  CLAMP_IMAGE_IF( T, UInt8ImageType, image_base ); \
63  CLAMP_IMAGE_IF( T, Int16ImageType, image_base ); \
64  CLAMP_IMAGE_IF( T, UInt16ImageType, image_base ); \
65  CLAMP_IMAGE_IF( T, Int32ImageType, image_base ); \
66  CLAMP_IMAGE_IF( T, UInt32ImageType, image_base ); \
67  \
68  CLAMP_IMAGE_IF( T, FloatImageType, image_base ); \
69  CLAMP_IMAGE_IF( T, DoubleImageType, image_base ); \
70  \
71  CLAMP_IMAGE_IF( T, ComplexInt16ImageType, image_base ); \
72  CLAMP_IMAGE_IF( T, ComplexInt32ImageType, image_base ); \
73  \
74  CLAMP_IMAGE_IF( T, ComplexFloatImageType, image_base ); \
75  CLAMP_IMAGE_IF( T, ComplexDoubleImageType, image_base ); \
76  \
77  return nullptr; \
78  }
79 
80 
81 template< typename TOutputImage,
82  typename TInputImage >
83 TOutputImage *
85 ::Cast( TInputImage * image )
86 {
88 
89  if( clamp.ocif )
90  clamp.ocif->UpdateOutputInformation();
91 
92  m_InputCaster = clamp.icif;
93  m_OutputCaster = clamp.ocif;
94 
95  return clamp.out;
96 }
97 
98 
99 template <class TImageType>
100 TImageType*
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  {
112  !m_FileName.empty() )
113  {
115  // A new valid filename has been given : a reader is created
116  typedef otb::ImageFileReader<TImageType> ReaderType;
117 
118  typename ReaderType::Pointer reader = ReaderType::New();
119 
120  reader->SetFileName( m_FileName );
121 
122  reader->UpdateOutputInformation();
123 
124  m_Image = reader->GetOutput();
125  m_Reader = reader;
126 
128 
129  // Pay attention, don't return m_Image because it is a ImageBase...
130  return reader->GetOutput();
131  }
132  else
133  {
134  // In this case, the reader and the image should already be there
135  if (m_Image.IsNull())
136  {
137  itkExceptionMacro("No input image or filename detected...");
138  }
139  else
140  {
141  // Check if the image type asked here is the same as the one used for the reader
142  if (dynamic_cast<TImageType*> (m_Image.GetPointer()))
143  {
144  return dynamic_cast<TImageType*> (m_Image.GetPointer());
145  }
146  else
147  {
148  itkExceptionMacro(
149  "GetImage() was already called with a different type, "
150  "probably due to two calls to GetParameter<Type>Image with different types in application code.");
151  }
152  }
153  }
154  }
155 
156  else
157  {
159  if (m_Image.IsNull())
160  {
161  return nullptr;
162  }
163  // Check if the image type asked here is the same as m_image
164  else if ( dynamic_cast < TImageType* > ( m_Image.GetPointer() ) )
165  {
166  return dynamic_cast < TImageType* > ( m_Image.GetPointer() );
167  }
168  // check if we already done this cast
169  else if ( dynamic_cast <
171  ( m_OutputCaster.GetPointer() ) )
172  {
173  return dynamic_cast <
175  ( m_OutputCaster.GetPointer() )->GetOutput();
176  }
177  else
178  {
180  {
181  itkExceptionMacro(
182  "GetImage() was already called with a different type, "
183  "probably due to two calls to GetParameter<Type>Image with different types in application code.");
184  }
185  CLAMP_IMAGE_BASE( TImageType, m_Image.GetPointer() );
186  }
187  }
188 }
189 
191 template <>
192 OTBApplicationEngine_EXPORT
194 InputImageParameter::GetImage<ImageBaseType>();
195 
196 
197 } // End namespace Wrapper
198 } // End namespace otb
199 
200 #endif
#define CLAMP_IMAGE_BASE(T, image_base)
Helper class (private) which casts and clamps input-image type into output-image type.
Reads image data.
InputClampImageFilter::Pointer icif
ObjectType * GetPointer() const
Clamp image values to be below, over, or between threhold values.
OutputClampImageFilter::Pointer ocif
bool IsNotNull() const
bool IsNull() const
ImageBaseType const * GetImage() const