From 98db12ea711dbc7fb3862cb62b7d9438c5749fac Mon Sep 17 00:00:00 2001 From: Cyrille Valladeau <cyrille.valladeau@c-s.fr> Date: Mon, 19 Sep 2011 18:48:52 +0200 Subject: [PATCH] ENh: add output pixel type + correct a small bug : exception if a key doesn't have value --- .../otbWrapperCommandLineLauncher.cxx | 144 +++++++----------- .../otbWrapperCommandLineParser.cxx | 43 +++--- 2 files changed, 78 insertions(+), 109 deletions(-) diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx index 6a4f58ed42..9c3769743e 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineLauncher.cxx @@ -224,7 +224,7 @@ CommandLineLauncher::LoadPath() { std::vector<std::string> pathList; // If users has set path... - if( m_Parser->GetPaths( pathList, m_Expression) == CommandLineParser::OK ) + if( m_Parser->GetPaths( pathList, m_Expression ) == CommandLineParser::OK ) { // Contain paths into a string, separating each path with ":" m_Path = std::string(""); @@ -233,7 +233,7 @@ CommandLineLauncher::LoadPath() m_Path.append(pathList[i]); m_Path.append(":"); } - + std::string specificEnv("ITK_AUTOLOAD_PATH="); specificEnv.append(m_Path); // do NOT use putenv() directly, since the string memory must be managed carefully @@ -371,91 +371,56 @@ CommandLineLauncher::LoadParameters() } } - /* - // Mandatory case - ParameterGroup::Pointer paramGr = m_Application->GetParameterList(); - const unsigned int nbOfParam = paramGr->GetNumberOfParameters(); - - for( unsigned int i=0; i<nbOfParam; i++ ) - { - std::vector<std::string> values; - Parameter::Pointer param = paramGr->GetParameterByIndex(i); - const std::string paramKey(param->GetKey()); - ParameterType type = m_Application->GetParameterType( paramKey ); - - const bool paramExists( m_Parser->IsAttributExists( std::string("--").append(paramKey), m_Expression ) ); - // Check if mandatory parameter are present and have value - if( param->GetMandatory() == true ) - { - if( !paramExists ) - { - return MISSINGMANDATORYPARAMETER; - } - values = m_Parser->GetAttribut( std::string("--").append(paramKey), m_Expression); - if( values.size() == 0 ) - { - return MISSINGPARAMETERVALUE; - } - } - // Check if non mandatory parameter have values - else + // case of output pixel type... + if ( m_Parser->IsAttributExists( std::string("--outPix"), m_Expression ) ) + { + std::vector<std::string> values = m_Parser->GetAttribut( "--outPix", m_Expression ); + if( values.size() == 0 ) + return MISSINGPARAMETERVALUE; + else if( values.size() > 1 ) + return INVALIDNUMBEROFVALUE; + + ImagePixelType outPixType = ImagePixelType_float; + if( values[0] == "int8" ) + outPixType = ImagePixelType_int8; + else if( values[0] == "uint8" ) + outPixType = ImagePixelType_uint8; + else if( values[0] == "int16" ) + outPixType = ImagePixelType_int16; + else if( values[0] == "uint16" ) + outPixType = ImagePixelType_uint16; + else if( values[0] == "int32" ) + outPixType = ImagePixelType_int32; + else if( values[0] == "uint32" ) + outPixType = ImagePixelType_uint32; + else if( values[0] == "float" ) + outPixType = ImagePixelType_float; + else if( values[0] == "double" ) + outPixType = ImagePixelType_double; + else { - if( paramExists ) - { - values = m_Parser->GetAttribut( std::string("--").append(paramKey), m_Expression); - if( values.size() == 0 ) - { - return MISSINGPARAMETERVALUE; - } - } + return WRONGPARAMETERVALUE; } - // If the param is optionnal and hasn't been set : don't do anything - if( paramExists ) + // Loop over each output image parameter + std::vector<std::string> paramList = m_Application->GetParametersKeys(true); + std::vector<std::string>::const_iterator it = paramList.begin(); + for (; it != paramList.end(); ++it) { - // List values parameter case - if( type == ParameterType_InputImageList ) - { - dynamic_cast<InputImageListParameter *>(param.GetPointer())->SetListFromFileName( values ); - } - else if( type == ParameterType_StringList ) - { - dynamic_cast<StringListParameter *>(param.GetPointer())->SetValue( values ); - } - else if( values.size() != 1) + if (m_Application->GetParameterType(*it) == ParameterType_OutputImage) { - return INVALIDNUMBEROFVALUE; - } - - // Single value parameter - if( type == ParameterType_Float || type == ParameterType_Int || type == ParameterType_Radius - || type == ParameterType_Directory || type == ParameterType_String || type == ParameterType_Filename || type == ParameterType_InputComplexImage - || type == ParameterType_InputImage || type == ParameterType_InputVectorData || type == ParameterType_OutputImage || type == ParameterType_OutputVectorData ) - { - m_Application->SetParameterString( paramKey, values[0] ); - } - else if( type == ParameterType_Empty ) - { - if( values[0] == "1" || values[0] == "true") - { - dynamic_cast<EmptyParameter *>(param.GetPointer())->SetActive(true); - } - else if( values[0] == "0" || values[0] == "false") - { - dynamic_cast<EmptyParameter *>(param.GetPointer())->SetActive(false); - } - else - { - return WRONGPARAMETERVALUE; - } + Parameter* param = m_Application->GetParameterByKey(*it); + OutputImageParameter* outputParam = dynamic_cast<OutputImageParameter*>(param); + outputParam->SetPixelType( outPixType ); } } } -*/ + return OKPARAM; } + void CommandLineLauncher::LinkWatchers() { @@ -543,6 +508,19 @@ CommandLineLauncher::DisplayHelp() std::cerr<< "\t Status: none"<<m_Path<<std::endl; else std::cerr<< "\t Status: USER VALUE: "<<m_Parser->GetAttribut( "--progress", m_Expression )[0]<<std::endl; + + //// Output pixel type + std::cerr<<"--outPix (Output pixel type)"<<std::endl; + std::cerr<<"\t Description: Defines the output images pixel type."<<std::endl; + std::cerr<<"\t Type: String (int8, uint8, int16, uint16, int32, uint32, float or double)"<<std::endl; + std::cerr<<"\t Default value: float"<< std::endl; + if( !m_Parser->IsAttributExists( "--progress", m_Expression ) ) + std::cerr<<"\t Status: DEFAULT VALUE"<<std::endl; + else if( m_Parser->GetAttribut( "--outPix", m_Expression ).size() == 0 ) + std::cerr<< "\t Status: none"<<m_Path<<std::endl; + else + std::cerr<< "\t Status: USER VALUE: "<<m_Parser->GetAttribut( "--outPix", m_Expression )[0]<<std::endl; + for( unsigned int i=0; i<nbOfParam; i++ ) { Parameter::Pointer param = m_Application->GetParameterByKey( appKeyList[i] ); @@ -726,7 +704,7 @@ CommandLineLauncher::CheckKeyValidity() appKeyList.push_back( std::string(m_Parser->GetModuleNameKey()).substr(2, std::string(m_Parser->GetModuleNameKey()).size()) ); appKeyList.push_back( "help" ); appKeyList.push_back( "progress" ); - + appKeyList.push_back( "outPix" ); // Check if each key in the expression exists in the application for( unsigned int i=0; i<expKeyList.size(); i++ ) { @@ -747,20 +725,6 @@ CommandLineLauncher::CheckKeyValidity() } } - /* - if(res == false) - { - for( unsigned int i=0; i<expKeyList.size(); i++ ) - { - std::cout<< expKeyList[i]<<std::endl; - } - for( unsigned int j=0; j<appKeyList.size(); j++ ) - { - std::cout<< appKeyList[j]<<std::endl; - } - } - */ - return res; } diff --git a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx index 84d71daed8..101365ba13 100644 --- a/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx +++ b/Code/Wrappers/CommandLine/otbWrapperCommandLineParser.cxx @@ -164,42 +164,47 @@ CommandLineParser::GetAttribut( const std::string & key, const std::string & exp itkExceptionMacro("No key \""<<key<<"\" found in \""<<exp<<"\"."); } - std::vector<std::string> res; + std::vector<std::string> res; std::string expFromKey = std::string(exp).substr(found+key.size(), std::string(exp).size()); - + if( expFromKey.size() == 0 ) { return res; } - + std::string tempModKey = expFromKey; // remove other key in the string if there's any if( expFromKey.find("--") != std::string::npos) { tempModKey = expFromKey.substr( 0, expFromKey.find("--")-1); } - std::vector<itksys::String> spaceSplitted = itksys::SystemTools::SplitString(tempModKey.substr(1, tempModKey.size()).c_str(), ' ', false); - // Remove " " string element - for(unsigned int i=0; i<spaceSplitted.size(); i++) + + // Only if the key has values assciated + if( tempModKey.size() > 0 ) { - if( spaceSplitted[i] == " ") + std::vector<itksys::String> spaceSplitted = itksys::SystemTools::SplitString(tempModKey.substr(1, tempModKey.size()).c_str(), ' ', false); + + // Remove " " string element + for(unsigned int i=0; i<spaceSplitted.size(); i++) { - spaceSplitted.erase(spaceSplitted.begin()+i); - i--; + if( spaceSplitted[i] == " ") + { + spaceSplitted.erase(spaceSplitted.begin()+i); + i--; + } } - } - - // Remove space at the begining of the string and cast into std::vector<std::string> - for(unsigned int i=0; i<spaceSplitted.size(); i++) - { - while( spaceSplitted[i].size()>0 && spaceSplitted[i][0] == ' ' ) + + // Remove space at the begining of the string and cast into std::vector<std::string> + for(unsigned int i=0; i<spaceSplitted.size(); i++) { - spaceSplitted[i] = spaceSplitted[i].substr(1, spaceSplitted[i].size()); + while( spaceSplitted[i].size()>0 && spaceSplitted[i][0] == ' ' ) + { + spaceSplitted[i] = spaceSplitted[i].substr(1, spaceSplitted[i].size()); + } + res.push_back(spaceSplitted[i]); } - res.push_back(spaceSplitted[i]); } - - return res; + return res; } std::string -- GitLab