31 #include <DGtal/base/Common.h>    32 #include <DGtal/io/readers/VolReader.h>    33 #include <DGtal/io/writers/VolWriter.h>    34 #include <DGtal/helpers/StdDefs.h>    35 #include <DGtal/images/ImageContainerBySTLVector.h>    36 #include <DGtal/images/ConstImageAdapter.h>    37 #include <boost/program_options/options_description.hpp>    38 #include <boost/program_options/parsers.hpp>    39 #include <boost/program_options/variables_map.hpp>    43 using namespace DGtal;
    46 namespace po = boost::program_options;
    93 void missingParam ( std::string param )
    95   trace.error() <<
" Parameter: "<<param<<
" is required..";
    96   trace.info() <<std::endl;
   101 int main(
int argc, 
char**argv)
   105   po::options_description general_opt ( 
"Allowed options are: " );
   106   general_opt.add_options()
   107     ( 
"help,h", 
"display this message." )
   108     ( 
"input,i", po::value<std::string>(), 
"Input vol file." )
   109     ( 
"xMin", po::value<unsigned int>()->default_value(0), 
"x coordinate of lower point." )
   110     ( 
"yMin", po::value<unsigned int >()->default_value(0), 
"y coordinate of lower point." )
   111     ( 
"zMin", po::value<unsigned int >()->default_value(0), 
"z coordinate of lower point." )
   112     ( 
"xMax", po::value<unsigned int >(), 
"x coordinate of upper point." )
   113     ( 
"yMax", po::value<unsigned int>(), 
"y coordinate of upper point." )
   114     ( 
"zMax", po::value<unsigned int>(), 
"z coordinate of upper point." )
   115     ( 
"output,o", po::value<string>()->default_value(
"output.vol"),
"Output filename." );
   118   po::variables_map vm;
   122     po::store ( po::parse_command_line ( argc, argv, general_opt ), vm );
   123   }
catch(
const std::exception& ex){
   125     trace.info()<< 
"Error checking program options: "<< ex.what()<< endl;
   128   if (!parseOK || vm.count ( 
"help" ) || !vm.count(
"input"))
   130       trace.info() << 
" Crops a 3D vol image from domain coordinates."<<std::endl
   131                    << std::endl << 
"Basic usage: "<<std::endl
   132                    << 
"\t volCrop --input <volFileName> --o <volOutputFileName> (both files can be independently in vol, pgm3D, p3d format)"<<std::endl
   133                    << general_opt << 
"\n";
   134       std::cout << 
"Example:\n"   135                 << 
"volCrop --xMin 50 --yMin 50 --zMin 10 --xMax 150 --yMax 150 --zMax 50 -i ${DGtal}/examples/samples/lobster.vol -o croppedLobster.vol \n";
   141   if ( ! ( vm.count ( 
"input" ) ) ) missingParam ( 
"--input" );
   142   if ( ! ( vm.count ( 
"xMax" ) ) ) missingParam ( 
"--xMax" );
   143   if ( ! ( vm.count ( 
"yMax" ) ) ) missingParam ( 
"--yMax" );
   144   if ( ! ( vm.count ( 
"zMax" ) ) ) missingParam ( 
"--zMax" );
   146   std::string filename = vm[
"input"].as<std::string>();
   147   if ( ! ( vm.count ( 
"output" ) ) ) missingParam ( 
"--output" );
   148   std::string outputFileName = vm[
"output"].as<std::string>();
   150   Z3i::Point ptLow( vm[
"xMin"].as<unsigned int>(), vm[
"yMin"].as<unsigned int>(),vm[
"zMin"].as<unsigned int>());
   151   Z3i::Point ptMax( vm[
"xMax"].as<unsigned int>(), vm[
"yMax"].as<unsigned int>(),vm[
"zMax"].as<unsigned int>());
   153   trace.beginBlock(
"Loading file");
   154   typedef ImageContainerBySTLVector<Z3i::Domain, unsigned char>  MyImageC;
   155   MyImageC  imageC = VolReader< MyImageC >::importVol ( filename );
   156   functors::Identity df;  
   158   typedef ConstImageAdapter<MyImageC, Domain, functors::Identity, MyImageC::Value, functors::Identity > ConstImageAdapterForSubImage;
   159   Domain subDomain(ptLow, ptMax);
   160   ConstImageAdapterForSubImage subImage(imageC, subDomain, df, df);
   163   trace.beginBlock(
"Exporting...");
   164   bool res =  VolWriter< ConstImageAdapterForSubImage>::exportVol(outputFileName, subImage);
   166   if (res) 
return 0; 
else return 1;