otbWrapperInputImageParameter.hxx 7.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
20

21 22
#ifndef otbWrapperInputImageParameter_hxx
#define otbWrapperInputImageParameter_hxx
23 24 25

#include "otbWrapperInputImageParameter.h"

26
#include "otbClampImageFilter.h"
27 28 29 30 31 32 33

namespace otb
{
namespace Wrapper
{


34 35
#define INPUT_IMAGE_PARAMETER_GET_IMAGE_EXCEPTION 0

36
template <class TImageType>
OTB Bot's avatar
OTB Bot committed
37
TImageType*
38 39 40 41 42 43 44 45
InputImageParameter::GetImage()
{
  // Used m_PreviousFileName because if not, when the user call twice GetImage,
  // it without changing the filename, it returns 2 different
  // image pointers
  // Only one image type can be used

  // 2 cases : the user set a filename vs. the user set an image
46
  if( m_UseFilename )
47
    {
48 49
    if( m_PreviousFileName!=m_FileName &&
	!m_FileName.empty() )
50 51 52 53
      {
      //////////////////////// Filename case:
      // A new valid filename has been given : a reader is created
      typedef otb::ImageFileReader<TImageType> ReaderType;
54

55
      typename ReaderType::Pointer reader = ReaderType::New();
56 57

      reader->SetFileName( m_FileName );
58

59
      reader->UpdateOutputInformation();
60 61 62 63

      m_Image = reader->GetOutput();
      m_Reader = reader;

64 65
      m_PreviousFileName = m_FileName;

66 67 68 69 70
      // Pay attention, don't return m_Image because it is a ImageBase...
      return reader->GetOutput();
      }
    else
      {
71 72 73 74
#if INPUT_IMAGE_PARAMETER_GET_IMAGE_EXCEPTION
      return dynamic_cast< TImageType* >( m_Image.GetPointer() );

#else // INPUT_IMAGE_PARAMETER_GET_IMAGE_EXCEPTION
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      // In this case, the reader and the image should already be there
      if (m_Image.IsNull())
        {
        itkExceptionMacro("No input image or filename detected...");
        }
      else
        {
        // Check if the image type asked here is the same as the one used for the reader
        if (dynamic_cast<TImageType*> (m_Image.GetPointer()))
          {
          return dynamic_cast<TImageType*> (m_Image.GetPointer());
          }
        else
          {
          itkExceptionMacro("Cannot ask a different image type");
          }
        }
92
#endif // INPUT_IMAGE_PARAMETER_GET_IMAGE_EXCEPTION
93 94
      }
    }
95

96 97 98 99 100
  else
    {
    //////////////////////// Image case:
    if (m_Image.IsNull())
      {
101
#if INPUT_IMAGE_PARAMETER_GET_IMAGE_EXCEPTION
102
      itkExceptionMacro("No input image or filename detected...");
103 104 105
#else
      return nullptr;
#endif
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
      }
    else
      {
      if (dynamic_cast<UInt8ImageType*> (m_Image.GetPointer()))
        {
        return CastImage<UInt8ImageType, TImageType> ();
        }
      else if (dynamic_cast<Int16ImageType*> (m_Image.GetPointer()))
        {
        return CastImage<Int16ImageType, TImageType> ();
        }
      else if (dynamic_cast<UInt16ImageType*> (m_Image.GetPointer()))
        {
        return CastImage<UInt16ImageType, TImageType> ();
        }
      else if (dynamic_cast<Int32ImageType*> (m_Image.GetPointer()))
        {
        return CastImage<Int32ImageType, TImageType> ();
        }
      else if (dynamic_cast<UInt32ImageType*> (m_Image.GetPointer()))
        {
        return CastImage<UInt32ImageType, TImageType> ();
        }
      else if (dynamic_cast<FloatImageType*> (m_Image.GetPointer()))
        {
        return CastImage<FloatImageType, TImageType> ();
        }
      else if (dynamic_cast<DoubleImageType*> (m_Image.GetPointer()))
        {
        return CastImage<DoubleImageType, TImageType> ();
        }
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
      else if (dynamic_cast<ComplexInt16ImageType*>(m_Image.GetPointer()))
        {
        return CastImage<ComplexInt16ImageType, TImageType>();
        }
      else if (dynamic_cast<ComplexInt32ImageType*>(m_Image.GetPointer()))
        {
        return CastImage<ComplexInt32ImageType, TImageType>();
        }
      else if (dynamic_cast<ComplexFloatImageType*>(m_Image.GetPointer()))
        {
        return CastImage<ComplexFloatImageType, TImageType>();
        }
      else if (dynamic_cast<ComplexDoubleImageType*>(m_Image.GetPointer()))
        {
        return CastImage<ComplexDoubleImageType, TImageType>();
        }
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      else if (dynamic_cast<UInt8VectorImageType*> (m_Image.GetPointer()))
        {
        return CastImage<UInt8VectorImageType, TImageType> ();
        }
      else if (dynamic_cast<Int16VectorImageType*> (m_Image.GetPointer()))
        {
        return CastImage<Int16VectorImageType, TImageType> ();
        }
      else if (dynamic_cast<UInt16VectorImageType*> (m_Image.GetPointer()))
        {
        return CastImage<UInt16VectorImageType, TImageType> ();
        }
      else if (dynamic_cast<Int32VectorImageType*> (m_Image.GetPointer()))
        {
        return CastImage<Int32VectorImageType, TImageType> ();
        }
      else if (dynamic_cast<UInt32VectorImageType*> (m_Image.GetPointer()))
        {
        return CastImage<UInt32VectorImageType, TImageType> ();
        }
      else if (dynamic_cast<FloatVectorImageType*> (m_Image.GetPointer()))
        {
        return CastImage<FloatVectorImageType, TImageType> ();
        }
      else if (dynamic_cast<DoubleVectorImageType*> (m_Image.GetPointer()))
        {
        return CastImage<DoubleVectorImageType, TImageType> ();
        }
      else if (dynamic_cast<UInt8RGBAImageType*> (m_Image.GetPointer()))
        {
        return CastImage<UInt8RGBAImageType, TImageType> ();
        }
      else if (dynamic_cast<UInt8RGBImageType*> (m_Image.GetPointer()))
        {
        return CastImage<UInt8RGBImageType, TImageType> ();
        }
189
      else if (dynamic_cast<ComplexInt16VectorImageType*>(m_Image.GetPointer()))
190
        {
191
        return CastImage<ComplexInt16VectorImageType, TImageType>();
192
        }
193
      else if (dynamic_cast<ComplexInt32VectorImageType*>(m_Image.GetPointer()))
194
        {
195
        return CastImage<ComplexInt32VectorImageType, TImageType>();
196
        }
197
      else if (dynamic_cast<ComplexFloatVectorImageType*>(m_Image.GetPointer()))
198
        {
199
        return CastImage<ComplexFloatVectorImageType, TImageType>();
200
        }
201
      else if (dynamic_cast<ComplexDoubleVectorImageType*>(m_Image.GetPointer()))
202
        {
203
        return CastImage<ComplexDoubleVectorImageType, TImageType>();
204
        }
205 206
      else
        {
207
#if INPUT_IMAGE_PARAMETER_GET_IMAGE_EXCEPTION
208
        itkExceptionMacro("Unknown image type");
209 210 211
#else
	return nullptr;
#endif
212 213 214 215 216
        }
      }
    }
}

217 218
/** declare a specialization for ImageBaseType */
template <>
219
OTBApplicationEngine_EXPORT
220
ImageBaseType*
221 222
InputImageParameter::GetImage<ImageBaseType>();

223 224 225 226 227

template <class TInputImage, class TOutputImage>
TOutputImage*
InputImageParameter::CastImage()
{
228 229 230 231 232 233 234
  if ( dynamic_cast<TOutputImage*> (m_Image.GetPointer()) )
    {
    return dynamic_cast<TOutputImage*> (m_Image.GetPointer());
    }
  else
    {
    TInputImage* realInputImage = dynamic_cast<TInputImage*>(m_Image.GetPointer());
235

236
    typedef ClampImageFilter<TInputImage, TOutputImage> CasterType;
237
    typename CasterType::Pointer caster = CasterType::New();
238

239 240
    caster->SetInput(realInputImage);
    caster->UpdateOutputInformation();
241

242 243
    m_Image = caster->GetOutput();
    m_Caster = caster;
244

245
    return caster->GetOutput();
246 247
    } 
  // itkExceptionMacro("Cast from "<<typeid(TInputImage).name()<<" to "<<typeid(TOutputImage).name()<<" not authorized.");
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
}

template <class TInputImage>
void
InputImageParameter::SetImage(TInputImage* image)
{
  m_UseFilename = false;
  m_Image = image;
}


} // End namespace Wrapper
} // End namespace otb

#endif