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 equalize input 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::ipReal64& fOutImgMin,
ipsdk::ipReal64& fOutImgMax);
int
main(int argc, char* argv[])
{
// add console appender for 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:
// IPSDK library is initialized but there were warnings;
// notify the user by displaying a message
% initRes.getMsg());
break;
case ipsdk::core::eLibInitStatus::eLIS_Failed:
// IPSDK library initialization; notify the user and exit
% initRes.getMsg());
return -1;
break;
default:
break;
}
// boost objects, used to store input and output images files paths
boost::filesystem::path inImgFilePath, outImgFilePath;
// variable storing expected range of output image
ipsdk::ipReal64 fOutImgMin, fOutImgMax;
// read program options from command line, and, if appropriate,
// initialize input and output images files paths
if(!readCmdArguments(argc, argv, inImgFilePath, outImgFilePath,
fOutImgMin, fOutImgMax))
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
// the equalization
ImagePtr pOutImg;
try {
attr::RangePtr pInOutputRange(boost::make_shared<attr::Range>());
pInOutputRange->setValue<attr::Range::Min>(fOutImgMin);
pInOutputRange->setValue<attr::Range::Max>(fOutImgMax);
// equalize input image
pOutImg = ipsdk::imaproc::itrans::equalize2dImg(pInImg, pInOutputRange);
// 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::ipReal64& fOutImgMin,
ipsdk::ipReal64& fOutImgMax)
{
// 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)")
("outImgMin", value<ipReal64>(),
"expected minimal value of output image")
("outImgMax", value<ipReal64>(),
"expected maximal value of output image")
;
// 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 input image file path with a default file path
inImgFilePath = getIPSDKDirectory(eInternalDirectory::eID_Images) / "Unequalized_Hawkes_Bay_NZ.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 / "equalization.tif";
}
// initialize fOutImgMin with default value 0
fOutImgMin = .0;
if(vm.count("outImgMin"))
fOutImgMin = vm["outImgMin"].as<ipReal64>();
// initialize fOutImgMin with default value 255
fOutImgMax = 255.0;
if(vm.count("outImgMax"))
fOutImgMax = vm["outImgMax"].as<ipReal64>();
// check that input image file path exists. If not...
if(!boost::filesystem::exists(inImgFilePath)) {
// ... notify the user, and abort
% inImgFilePath);
return false;
}
return true;
}