OTB  9.0.0
Orfeo Toolbox
otbWrapperInputImageParameter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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, typename TInputImage>
82 TOutputImage* InputImageParameter::Cast(TInputImage* image)
83 {
85 
86  if (clamp.ocif)
87  clamp.ocif->UpdateOutputInformation();
88 
89  m_OutputCaster = clamp.ocif;
90  m_OutputCasted = clamp.out;
91 
92  return clamp.out;
93 }
94 
95 
96 template <class TImageType>
98 {
99  // Used m_PreviousFileName because if not, when the user call twice GetImage,
100  // it without changing the filename, it returns 2 different
101  // image pointers
102  // Only one image type can be used
103 
104  // 2 cases : the user set a filename vs. the user set an image
105  if (m_UseFilename)
106  {
107  if (m_PreviousFileName != m_FileName && !m_FileName.empty())
108  {
110  // A new valid filename has been given : a reader is created
111  typedef otb::ImageFileReader<TImageType> ReaderType;
112 
113  typename ReaderType::Pointer reader = ReaderType::New();
114 
115  reader->SetFileName(m_FileName);
116 
117  reader->UpdateOutputInformation();
118 
119  m_Image = reader->GetOutput();
120  m_Reader = reader;
121 
123 
124  // Pay attention, don't return m_Image because it is a ImageBase...
125  return reader->GetOutput();
126  }
127  else
128  {
129  // In this case, the reader and the image should already be there
130  if (m_Image.IsNull())
131  {
132  itkExceptionMacro("No input image or filename detected...");
133  }
134  else
135  {
136  // Check if the image type asked here is the same as the one used for the reader
137  if (dynamic_cast<TImageType*>(m_Image.GetPointer()))
138  {
139  return dynamic_cast<TImageType*>(m_Image.GetPointer());
140  }
141  else
142  {
143  itkExceptionMacro(
144  "GetImage() was already called with a different type, "
145  "probably due to two calls to GetParameter<Type>Image with different types in application code.");
146  }
147  }
148  }
149  }
150 
151  else
152  {
154  if (m_Image.IsNull())
155  {
156  return nullptr;
157  }
158  // Check if the image type asked here is the same as m_image
159  TImageType *im = dynamic_cast<TImageType*>(m_Image.GetPointer());
160  if (im)
161  {
162  return im;
163  }
164  // check if we already done this cast
165  if (m_OutputCasted)
166  {
167  im = dynamic_cast<TImageType*>(m_OutputCasted.GetPointer());
168  if (im)
169  {
170  return im;
171  }
172  }
173 
174  if (m_OutputCaster.IsNotNull())
175  {
176  itkExceptionMacro(
177  "GetImage() was already called with a different type, "
178  "probably due to two calls to GetParameter<Type>Image with different types in application code. Expected: "
179  );
180  }
181  CLAMP_IMAGE_BASE(TImageType, m_Image.GetPointer());
182  }
183 }
184 
186 template <>
187 OTBApplicationEngine_EXPORT ImageBaseType* InputImageParameter::GetImage<ImageBaseType>();
188 
189 
190 } // End namespace Wrapper
191 } // End namespace otb
192 
193 #endif
otb::Wrapper::InputImageParameter::m_UseFilename
bool m_UseFilename
Definition: otbWrapperInputImageParameter.h:169
otb::Wrapper::InputImageParameter::GetImage
ImageBaseType const * GetImage() const
otb::Wrapper::InputImageParameter::m_PreviousFileName
std::string m_PreviousFileName
Definition: otbWrapperInputImageParameter.h:166
otb::Wrapper::InputImageParameter::m_OutputCasted
itk::DataObject::Pointer m_OutputCasted
Definition: otbWrapperInputImageParameter.h:158
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Wrapper::InputImageParameter::m_FileName
std::string m_FileName
Definition: otbWrapperInputImageParameter.h:152
otb::Wrapper::InputImageParameter::Cast
TOutputImage * Cast(TInputImage *)
Definition: otbWrapperInputImageParameter.hxx:82
otb::Wrapper::details::CastImage
Helper class (private) which casts and clamps input-image type into output-image type.
Definition: otbWrapperCastImage.h:46
otb::Wrapper::InputImageParameter::m_Reader
itk::ProcessObject::Pointer m_Reader
Definition: otbWrapperInputImageParameter.h:153
otbWrapperInputImageParameter.h
otbWrapperCastImage.h
otb::Wrapper::ImageBaseType
itk::ImageBase< 2 > ImageBaseType
Definition: otbWrapperTypes.h:180
otb::Wrapper::InputImageParameter::m_Image
ImageBaseType::Pointer m_Image
Definition: otbWrapperInputImageParameter.h:155
otb::clamp
constexpr T const & clamp(T const &v, T const &lo, T const &hi) noexcept
Definition: otbAlgoClamp.h:33
otb::Wrapper::InputImageParameter::m_OutputCaster
itk::ProcessObject::Pointer m_OutputCaster
Definition: otbWrapperInputImageParameter.h:157
otb::ImageFileReader
Reads image data.
Definition: otbImageFileReader.h:68
CLAMP_IMAGE_BASE
#define CLAMP_IMAGE_BASE(T, image_base)
Definition: otbWrapperInputImageParameter.hxx:42