Commit eb4e2c74 authored by Julien Michel's avatar Julien Michel
Browse files

ENH: Modify internal implementation of InputImageListParameter to act as a...

ENH: Modify internal implementation of InputImageListParameter to act as a vector of InputImageParameter

External API remains unchanged, but we can now benefit from all the services of InputImageParameter, including boiler-plate template/macro code to cast ImageBase to any type of image.

Also, this avoids a lot of code duplication and could be used in the future to also avoid duplication in QT widget part.
parent bb19bf75
......@@ -18,10 +18,8 @@
#ifndef otbWrapperInputImageListParameter_h
#define otbWrapperInputImageListParameter_h
#include "otbImageFileReader.h"
#include "otbWrapperParameter.h"
#include "otbObjectList.h"
#include "otbWrapperInputImageParameter.h"
namespace otb
{
......@@ -42,8 +40,7 @@ public:
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef otb::ImageFileReader<FloatVectorImageType> ImageFileReaderType;
typedef otb::ObjectList<ImageFileReaderType> ImageFileReaderListType;
typedef std::vector<InputImageParameter::Pointer> InputImageParameterVectorType;
/** Defining ::New() static method */
itkNewMacro(Self);
......@@ -100,13 +97,14 @@ protected:
~InputImageListParameter() ITK_OVERRIDE;
FloatVectorImageListType::Pointer m_ImageList;
ImageFileReaderListType::Pointer m_ReaderList;
private:
InputImageListParameter(const Parameter &); //purposely not implemented
void operator =(const Parameter&); //purposely not implemented
InputImageParameterVectorType m_InputImageParameterVector;
FloatVectorImageListType::Pointer m_ImageList;
}; // End class InputImage Parameter
} // End namespace Wrapper
......
......@@ -24,11 +24,11 @@ namespace Wrapper
{
InputImageListParameter::InputImageListParameter()
: m_InputImageParameterVector(),
m_ImageList(FloatVectorImageListType::New())
{
this->SetName("Input Image List");
this->SetKey("inList");
m_ImageList = FloatVectorImageListType::New();
m_ReaderList = ImageFileReaderListType::New();
}
InputImageListParameter::~InputImageListParameter()
......@@ -50,22 +50,21 @@ InputImageListParameter::SetListFromFileName(const std::vector<std::string> & fi
// File existance checked by the reader
if (!filename.empty())
{
ImageFileReaderType::Pointer reader = ImageFileReaderType::New();
try
{
reader->SetFileName(filename);
reader->UpdateOutputInformation();
}
catch(itk::ExceptionObject & /*err*/)
// Try to build a new ParameterInputImage
InputImageParameter::Pointer tmpInputImageParameter = InputImageParameter::New();
tmpInputImageParameter->SetFromFileName(filename);
if(!tmpInputImageParameter->HasValue())
{
// If loading failed
this->ClearValue();
isOk = false;
break;
}
// everything went fine, store the object references
m_ReaderList->PushBack(reader);
m_ImageList->PushBack(reader->GetOutput());
m_InputImageParameterVector.push_back(tmpInputImageParameter);
m_ImageList->PushBack(tmpInputImageParameter->GetFloatVectorImage());
}
}
......@@ -83,7 +82,7 @@ InputImageListParameter::SetListFromFileName(const std::vector<std::string> & fi
void
InputImageListParameter::AddNullElement()
{
m_ReaderList->PushBack(ITK_NULLPTR);
m_InputImageParameterVector.push_back(ITK_NULLPTR);
m_ImageList->PushBack(ITK_NULLPTR);
SetActive(false);
this->Modified();
......@@ -97,21 +96,20 @@ InputImageListParameter::AddFromFileName(const std::string & filename)
// File existance checked by the reader
if (!filename.empty())
{
ImageFileReaderType::Pointer reader = ImageFileReaderType::New();
reader->SetFileName(filename);
try
{
reader->UpdateOutputInformation();
}
catch(itk::ExceptionObject & /*err*/)
// Try to build a new ParameterInputImage
InputImageParameter::Pointer tmpInputImageParameter = InputImageParameter::New();
tmpInputImageParameter->SetFromFileName(filename);
if(!tmpInputImageParameter->HasValue())
{
// If loading failed
this->ClearValue();
return false;
}
// everything went fine, store the object references
m_ReaderList->PushBack(reader);
m_ImageList->PushBack(reader->GetOutput());
m_InputImageParameterVector.push_back(tmpInputImageParameter);
m_ImageList->PushBack(tmpInputImageParameter->GetFloatVectorImage());
SetActive(true);
this->Modified();
return true;
......@@ -123,9 +121,9 @@ InputImageListParameter::AddFromFileName(const std::string & filename)
bool
InputImageListParameter::SetNthFileName( const unsigned int id, const std::string & filename )
{
if( m_ReaderList->Size()<id )
if( m_InputImageParameterVector.size()<id )
{
itkExceptionMacro(<< "No image "<<id<<". Only "<<m_ReaderList->Size()<<" images available.");
itkExceptionMacro(<< "No image "<<id<<". Only "<<m_InputImageParameterVector.size()<<" images available.");
}
// TODO : when the logger will be available, redirect the exception
......@@ -133,21 +131,19 @@ InputImageListParameter::SetNthFileName( const unsigned int id, const std::strin
// File existance checked by the reader
if (!filename.empty())
{
ImageFileReaderType::Pointer reader = ImageFileReaderType::New();
reader->SetFileName(filename);
try
{
reader->UpdateOutputInformation();
}
catch(itk::ExceptionObject &)
InputImageParameter::Pointer tmpInputImageParameter = InputImageParameter::New();
tmpInputImageParameter->SetFromFileName(filename);
if(tmpInputImageParameter->HasValue())
{
this->ClearValue();
return false;
}
m_ReaderList->SetNthElement(id, reader);
m_ImageList->SetNthElement(id, reader->GetOutput());
m_InputImageParameterVector[id] = tmpInputImageParameter;
m_ImageList->SetNthElement(id,tmpInputImageParameter->GetFloatVectorImage());
this->Modified();
SetActive(true);
return true;
......@@ -160,36 +156,27 @@ InputImageListParameter::SetNthFileName( const unsigned int id, const std::strin
std::vector<std::string>
InputImageListParameter::GetFileNameList() const
{
if (m_ReaderList)
{
std::vector<std::string> filenames;
for(unsigned int i=0; i<m_ReaderList->Size(); i++)
{
if( m_ReaderList->GetNthElement(i) )
filenames.push_back( m_ReaderList->GetNthElement(i)->GetFileName() );
}
std::vector<std::string> filenames;
return filenames;
for(InputImageParameterVectorType::const_iterator it = m_InputImageParameterVector.begin();
it!=m_InputImageParameterVector.end();++it)
{
filenames.push_back( (*it)->GetFileName() );
}
itkExceptionMacro(<< "No filename value");
return filenames;
}
std::string
InputImageListParameter::GetNthFileName( unsigned int i ) const
{
if (m_ReaderList)
{
if(m_ReaderList->Size()<i)
if(m_InputImageParameterVector.size()<i)
{
itkExceptionMacro(<< "No image "<<i<<". Only "<<m_ReaderList->Size()<<" images available.");
itkExceptionMacro(<< "No image "<<i<<". Only "<<m_InputImageParameterVector.size()<<" images available.");
}
return m_ReaderList->GetNthElement(i)->GetFileName();
}
itkExceptionMacro(<< "No filename value");
return m_InputImageParameterVector[i]->GetFileName();
}
FloatVectorImageListType*
......@@ -226,13 +213,20 @@ InputImageListParameter::SetImageList(FloatVectorImageListType* imList)
return;
}
m_ImageList = imList;
m_ReaderList = ImageFileReaderListType::Pointer();
for(unsigned int i=0; i<m_ImageList->Size(); i++)
// Clear previous values
this->ClearValue();
for(unsigned int i = 0; i<imList->Size(); i++)
{
m_ReaderList->PushBack( ImageFileReaderType::Pointer() );
// Try to build a new ParameterInputImage
InputImageParameter::Pointer tmpInputImageParameter = InputImageParameter::New();
tmpInputImageParameter->SetImage(imList->GetNthElement(i));
m_InputImageParameterVector.push_back(tmpInputImageParameter);
m_ImageList->PushBack(tmpInputImageParameter->GetFloatVectorImage());
}
SetActive(true);
this->Modified();
}
......@@ -252,8 +246,12 @@ InputImageListParameter::AddImage(FloatVectorImageType* image)
return;
}
m_ImageList->PushBack( image );
m_ReaderList->PushBack( ImageFileReaderType::Pointer() );
InputImageParameter::Pointer tmpInputImageParameter = InputImageParameter::New();
tmpInputImageParameter->SetImage(image);
m_InputImageParameterVector.push_back(tmpInputImageParameter);
m_ImageList->PushBack(tmpInputImageParameter->GetFloatVectorImage());
this->Modified();
}
......@@ -287,7 +285,7 @@ InputImageListParameter::Erase( unsigned int id )
}
m_ImageList->Erase( id );
m_ReaderList->Erase( id );
m_InputImageParameterVector.erase(m_InputImageParameterVector.begin()+id);
this->Modified();
}
......@@ -296,7 +294,7 @@ void
InputImageListParameter::ClearValue()
{
m_ImageList->Clear();
m_ReaderList->Clear();
m_InputImageParameterVector.clear();
SetActive(false);
this->Modified();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment