otbWrapperInputImageListParameter.cxx 6.96 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
23
24
25
26
27
28
29

#include "otbWrapperInputImageListParameter.h"
#include "itksys/SystemTools.hxx"

namespace otb
{
namespace Wrapper
{

InputImageListParameter::InputImageListParameter()
30
31
  : m_InputImageParameterVector(),
    m_ImageList(FloatVectorImageListType::New())
32
33
34
35
36
37
38
39
40
{
  this->SetName("Input Image List");
  this->SetKey("inList");
}

InputImageListParameter::~InputImageListParameter()
{
}

41
bool
42
43
InputImageListParameter::SetListFromFileName(const std::vector<std::string> & filenames)
{
44
  // First clear previous file chosen
45
46
  this->ClearValue();

47
48
  for(unsigned int i=0; i<filenames.size(); i++)
    {
49
    const std::string filename = filenames[i];
Victor Poughon's avatar
Victor Poughon committed
50

51
    // File existence checked by the reader
52
    if (!filename.empty())
53
      {
54
55
56
57
58
      // Try to build a new ParameterInputImage
      InputImageParameter::Pointer tmpInputImageParameter = InputImageParameter::New();
      tmpInputImageParameter->SetFromFileName(filename);

      m_InputImageParameterVector.push_back(tmpInputImageParameter);
59
      }
60
    }
Sébastien Dinot's avatar
Sébastien Dinot committed
61

62
  this->Modified();
Victor Poughon's avatar
Victor Poughon committed
63
  SetActive(true);
64
  return true;
65
66
67
68
69
70
}


void
InputImageListParameter::AddNullElement()
{
71
  m_InputImageParameterVector.push_back(ITK_NULLPTR);
Sébastien Dinot's avatar
Sébastien Dinot committed
72
73
  SetActive(false);
  this->Modified();
74
75
}

76
bool
77
78
InputImageListParameter::AddFromFileName(const std::string & filename)
{
79
  // File existence checked by the reader
80
  if (!filename.empty())
81
    {
82
83
    InputImageParameter::Pointer tmpInputImageParameter = InputImageParameter::New();
    tmpInputImageParameter->SetFromFileName(filename);
Victor Poughon's avatar
Victor Poughon committed
84

85
    m_InputImageParameterVector.push_back(tmpInputImageParameter);
Victor Poughon's avatar
Victor Poughon committed
86

87
    this->Modified();
Victor Poughon's avatar
Victor Poughon committed
88
    SetActive(true);
89
    return true;
90
    }
Sébastien Dinot's avatar
Sébastien Dinot committed
91

92
  return false;
93
94
}

95
bool
96
97
InputImageListParameter::SetNthFileName( const unsigned int id, const std::string & filename )
{
98
  if( m_InputImageParameterVector.size()<id )
99
    {
100
    itkExceptionMacro(<< "No image "<<id<<". Only "<<m_InputImageParameterVector.size()<<" images available.");
101
    }
102

103
  // File existence checked by the reader
104
  if (!filename.empty())
105
    {
106
107
108
109
    InputImageParameter::Pointer tmpInputImageParameter = InputImageParameter::New();
    tmpInputImageParameter->SetFromFileName(filename);

    m_InputImageParameterVector[id] = tmpInputImageParameter;
Victor Poughon's avatar
Victor Poughon committed
110

111
    this->Modified();
112
    SetActive(true);
113
    return true;
114
    }
115
116

  return false;
117
118
119
120
121
122
}


std::vector<std::string>
InputImageListParameter::GetFileNameList() const
{
123
  std::vector<std::string> filenames;
Sébastien Dinot's avatar
Sébastien Dinot committed
124

125
126
127
  for(InputImageParameterVectorType::const_iterator it = m_InputImageParameterVector.begin();
      it!=m_InputImageParameterVector.end();++it)
    {
128
129
130
131
    if(it->IsNull())
      {
      itkExceptionMacro(<< "Empty image in InputImageListParameter.");
      }
132
    filenames.push_back( (*it)->GetFileName() );
133
    }
Victor Poughon's avatar
Victor Poughon committed
134

135
  return filenames;
136
137
138
139
140
141
}


std::string
InputImageListParameter::GetNthFileName( unsigned int i ) const
{
142
    if(m_InputImageParameterVector.size()<i)
143
      {
144
      itkExceptionMacro(<< "No image "<<i<<". Only "<<m_InputImageParameterVector.size()<<" images available.");
145
      }
146
147
148
149
    if(m_InputImageParameterVector[i].IsNull())
      {
      itkExceptionMacro(<< "Requested image "<<i<<" has no filename");
      }
150
    return m_InputImageParameterVector[i]->GetFileName();
151
152
153
154
155
}

FloatVectorImageListType*
InputImageListParameter::GetImageList() const
{
156
157
158
  m_ImageList->Clear();
  for (unsigned int i=0 ; i < this->Size() ; ++i)
    {
159
160
161
162
    if(m_InputImageParameterVector[i].IsNull())
      {
      itkExceptionMacro(<< "Image "<<i<<" is empty.");
      }
163
164
    m_ImageList->PushBack(m_InputImageParameterVector[i]->GetFloatVectorImage());
    }
165
166
167
168
169
170
  return m_ImageList;
}

FloatVectorImageType*
InputImageListParameter::GetNthImage(unsigned int i) const
{
171
  if(this->Size()<=i)
172
    {
173
    itkExceptionMacro(<< "No image "<<i<<". Only "<<this->Size()<<" images available.");
174
    }
175
176
177
178
  if(m_InputImageParameterVector[i].IsNull())
    {
    itkExceptionMacro(<< "Image "<<i<<" is empty.");
    }
179
  return m_InputImageParameterVector[i]->GetFloatVectorImage();
180
181
182
183
184
}

void
InputImageListParameter::SetImageList(FloatVectorImageListType* imList)
{
185
  // Check input availability
186
187
188
189
  for(unsigned int i = 0; i < imList->Size(); i++)
  {
    imList->GetNthElement(i)->UpdateOutputInformation();
  }
Sébastien Dinot's avatar
Sébastien Dinot committed
190

191
192
193
194
  // Clear previous values
  this->ClearValue();

  for(unsigned int i = 0; i<imList->Size(); i++)
195
    {
196
197
    // Try to build a new ParameterInputImage
    InputImageParameter::Pointer tmpInputImageParameter = InputImageParameter::New();
198

199
    tmpInputImageParameter->SetImage(imList->GetNthElement(i));
200

201
202
    m_InputImageParameterVector.push_back(tmpInputImageParameter);
    m_ImageList->PushBack(tmpInputImageParameter->GetFloatVectorImage());
203
    }
204

205
  SetActive(true);
206
207
208
  this->Modified();
}

209
210
void InputImageListParameter::SetNthImage(unsigned int i, ImageBaseType * img)
{
211
  if(this->Size()<i)
212
  {
213
    itkExceptionMacro(<< "No image "<<i<<". Only "<<this->Size()<<" images available.");
214
215
216
217
218
  }

  // Check input availability
  img->UpdateOutputInformation();

219
220
  // Try to build a new ParameterInputImage
  InputImageParameter::Pointer tmpInputImageParameter = InputImageParameter::New();
221

222
223
224
225
226
227
  tmpInputImageParameter->SetImage(img);

  m_InputImageParameterVector[i] = tmpInputImageParameter;
}


228
void
229
InputImageListParameter::AddImage(ImageBaseType* image)
230
{
231
  // Check input availability
232
  image->UpdateOutputInformation();
Sébastien Dinot's avatar
Sébastien Dinot committed
233

234
  InputImageParameter::Pointer tmpInputImageParameter = InputImageParameter::New();
235

236
  tmpInputImageParameter->SetImage(image);
237

238
  m_InputImageParameterVector.push_back(tmpInputImageParameter);
239
240

  this->Modified();
241
242
243
244
245
}

bool
InputImageListParameter::HasValue() const
{
246
  if(this->Size() == 0)
247
248
249
    {
    return false;
    }
250
251
252

  bool res(true);
  unsigned int i(0);
253
  while(i < this->Size() && res == true)
254
    {
255
256
257
    res = (m_InputImageParameterVector[i] ?
           m_InputImageParameterVector[i]->HasValue() :
           false);
258
259
260
261
    i++;
    }

  return res;
262
263
264
265
266
267
}


void
InputImageListParameter::Erase( unsigned int id )
{
268
  if(this->Size()<id)
269
    {
270
    itkExceptionMacro(<< "No image "<<id<<". Only "<<this->Size()<<" images available.");
271
    }
Sébastien Dinot's avatar
Sébastien Dinot committed
272

273
  m_InputImageParameterVector.erase(m_InputImageParameterVector.begin()+id);
274
275

  this->Modified();
276
277
}

278
279
280
281
282
283
284
unsigned int
InputImageListParameter::Size() const
{
  return m_InputImageParameterVector.size();
}


285
void
286
InputImageListParameter::ClearValue()
287
288
{
  m_ImageList->Clear();
289
  m_InputImageParameterVector.clear();
290
291
292

  SetActive(false);
  this->Modified();
293
294
295
296
297
298
}


}
}