IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
// main.cpp:
// ---------
//
//
// --- IPSDK includes
// ------------------
// used to initialize IPSDK environment
#include <IPSDKCore/Config/LibraryInitializer.h>
// used to compute the variance image
// used to manage exceptions possibly thrown by algoritms functions
#include <IPSDKBaseProcessing/Logger/IPSDKBaseProcessingException.h>
// used to catch exceptions potentially thrown by functions loadTiffImageFile and saveTiffImageFile
#include <IPSDKImageFile/Logger/IPSDKImageFileException.h>
// used to read/write an image from/to a TIFF file:
// used to retrieve usual folders (IPSDK temporary folder, root development folder, etc.)
// used to display log messages
// --- third-party boost includes
// ------------------------------
// boost/filesystem/*: contains functions and classes providing facilities to
// manipulate files and directories, and associated paths
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>
// boost/program_options/*: contains functions and classes used to manage and
// interpret arguments of command line
#include <boost/program_options/cmdline.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
// --- third-party log4cplus include
// ---------------------------------
// used to add console as output support of logs
#include <log4cplus/consoleappender.h>
// --- STL include
// ---------------
// for std::cout
#include <iostream>
using namespace boost::filesystem;
using namespace boost::program_options;
using namespace ipsdk;
using namespace ipsdk::image;
using namespace ipsdk::imaproc;
using namespace ipsdk::sample;
bool
readCmdArguments(int argc, char* argv[],
boost::filesystem::path& inImgFilePath,
boost::filesystem::path& outImgFilePath,
ipsdk::ipUInt32& inHalfKnlSizeX,
ipsdk::ipUInt32& inHalfKnlSizeY);
int
main(int argc, char* argv[])
{
// add console appender for application logs
log4cplus::SharedAppenderPtr pConsole(new log4cplus::ConsoleAppender);
log4cplus::Logger::getRoot().addAppender(pConsole);
log4cplus::Logger::getRoot().setLogLevel(log4cplus::INFO_LOG_LEVEL);
// initialize IPSDK environment (first call to be done before calling any
// function or using any entity of IPSDK environment)
switch(initRes.getResult().value()) {
case ipsdk::core::eLibInitStatus::eLIS_Warn:
break;
case ipsdk::core::eLibInitStatus::eLIS_Failed:
return -1;
break;
default:
break;
}
// boost objects, used to store input and output images files paths
boost::filesystem::path inImgFilePath, outImgFilePath;
// variables storing half dimensions of kernel, used to compute the variance
ipsdk::ipUInt32 inHalfKnlSizeX, inHalfKnlSizeY;
// read program options from command line, and, if appropriate,
// initialize input and output images files paths
if(!readCmdArguments(argc, argv, inImgFilePath, outImgFilePath,
inHalfKnlSizeX, inHalfKnlSizeY))
return -1;
// display a log message in "INFO" level, to notify the user of the current
// step
% inImgFilePath.string());
// declare the variable that will contain the input image, loaded from
// TIFF file
try {
// read input image from specified path
pInImg = ipsdk::image::file::loadTiffImageFile(inImgFilePath);
// loadTiffImageFile function threw an exception; display error log
// message
% inImgFilePath.string() % e.getMsg());
// clear IPSDK environment features; should be called before exiting
// program
// quit the application with an exit code indicating an error
return -1;
}
// display a log message in "INFO" level, to notify the user of the current
// step
// declare the variable that will contain the output image, resulting from
// variance computation
ImagePtr pOutImg;
try {
// compute image of variance
inHalfKnlSizeX, inHalfKnlSizeY);
// one of the 2 previous function calls
% e.getMsg());
// clear IPSDK environment features; should be called before exiting
// program
// quit the application with an exit code indicating an error
return -1;
}
% outImgFilePath.string());
try {
// save the resulting image in specified path
ipsdk::image::file::saveTiffImageFile(outImgFilePath, pOutImg);
% outImgFilePath % e.getMsg());
// clear IPSDK environment features; should be called before exiting
// program
// quit the application with an exit code indicating an error
return -1;
}
// display a log message in "INFO" level, to notify the user that the job
// is successfully done
// clearing IPSDK environment features; should be called before exiting
// program
return 0;
}
bool
readCmdArguments(int argc, char* argv[],
boost::filesystem::path& inImgFilePath,
boost::filesystem::path& outImgFilePath,
ipsdk::ipUInt32& inHalfKnlSizeX,
ipsdk::ipUInt32& inHalfKnlSizeY)
{
// list of allowed program options
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("inputImgFilePath", value<path>(),
"input image file path (optional; only TIFF format is accepted)")
("outputImgFilePath", value<path>(),
"output image file path (optional; only TIFF format is accepted)")
("inHalfKnlSizeX", value<ipUInt32>(),
"half size of kernel, along x axis (optional; 1 by default")
("inHalfKnlSizeY", value<ipUInt32>(),
"half size of kernel, along y axis (optional; 1 by default")
;
// vm stores options found in command line
boost::program_options::variables_map vm;
try {
boost::program_options::store(parse_command_line(argc, argv, desc), vm);
} catch(const std::exception& e) {
% e.what());
return false;
}
boost::program_options::notify(vm);
// if the user typed "<app_name>.exe --help"...
if (vm.count("help")) {
// ... the application displays help message
std::cout << desc << "\n";
return false;
}
// initialize half size of kernel along x axis with default value (1)
inHalfKnlSizeX = 1;
// if the user specified his own half size value...
if(vm.count("inHalfKnlSizeX"))
inHalfKnlSizeX = vm["inHalfKnlSizeX"].as<ipUInt32>();
// initialize half size of kernel along y axis with default value (1)
inHalfKnlSizeY = 1;
// if the user specified his own half size value...
if(vm.count("inHalfKnlSizeY"))
inHalfKnlSizeY = vm["inHalfKnlSizeY"].as<ipUInt32>();
// initialize input image file path with a default file path
inImgFilePath = getIPSDKDirectory(eInternalDirectory::eID_Images) / "Lena_510x509_UInt8.tif";
// if the user specified his own input image file path as argument...
if(vm.count("inputImgFilePath"))
// ... replace the default one by his own
inImgFilePath = vm["inputImgFilePath"].as<path>();
// if the user specified his own input image file path as argument...
if(vm.count("outputImgFilePath"))
// ... replace the default one by his own
outImgFilePath = vm["outputImgFilePath"].as<path>();
else {
// ... otherwise, output image will be saved in temporary user directory
const path outputDir =
getIPSDKDefaultDirectory(eDefaultExternalDirectory::eDED_Tmp) /
"Sample";
// if output directory does not exist, we try to create it
if(!boost::filesystem::exists(outputDir)) {
try {
boost::filesystem::create_directories(outputDir);
} catch(const std::exception& e) {
% outputDir % e.what());
}
}
// initialize output image file path with a default file path
outImgFilePath = outputDir / "variance.tif";
}
// check that input image file path exists. If not...
if(!boost::filesystem::exists(inImgFilePath)) {
// ... notify the user, and abort
% inImgFilePath);
return false;
}
return true;
}