]> Creatis software - cpPlugins.git/blob - appli/examples/example_ReadWriteImage.cxx
e07761c07a0a8d93b21d2ddf6be5a913bfae2037
[cpPlugins.git] / appli / examples / example_ReadWriteImage.cxx
1 #include <cstdlib>
2 #include <iostream>
3 #include <string>
4
5 #include <cpPlugins/Interface/Interface.h>
6 #include <cpPlugins/Interface/ProcessObject.h>
7
8 int main( int argc, char* argv[] )
9 {
10   if( argc < 7 )
11   {
12     std::cerr
13       << "Usage: " << argv[ 0 ]
14       << " plugins_file"
15       << " input_image output_image"
16       << " dimensions pixel_type is_color" << std::endl;
17     return( 1 );
18
19   } // fi
20   std::string plugins_file = argv[ 1 ];
21   std::string input_image_file = argv[ 2 ];
22   std::string output_image_file = argv[ 3 ];
23   std::string dimensions = argv[ 4 ];
24   std::string pixel_type = argv[ 5 ];
25   bool is_color = ( std::atoi( argv[ 6 ] ) == 1 );
26
27   // Create interface
28   typedef cpPlugins::Interface::Interface TInterface;
29   typedef TInterface::TClasses            TClasses;
30
31   TInterface plugins;
32   plugins.Load( plugins_file );
33
34   // Create objects
35   typedef cpPlugins::Interface::ProcessObject TProcessObject;
36   typedef TProcessObject::TParameters         TParameters;
37   cpPlugins::Interface::ProcessObject* reader;
38   cpPlugins::Interface::ProcessObject* writer;
39
40   reader =
41     dynamic_cast< TProcessObject* >(
42       plugins.CreateObject( "cpPlugins::Plugins::ImageReader" )
43       );
44   if( reader == NULL )
45   {
46     std::cerr << "No suitable reader found in plugins." << std::endl;
47     return( 1 );
48
49   } // fi
50   writer =
51     dynamic_cast< TProcessObject* >(
52       plugins.CreateObject( "cpPlugins::Plugins::ImageWriter" )
53       );
54   if( writer == NULL )
55   {
56     delete reader;
57     std::cerr << "No suitable writer found in plugins." << std::endl;
58     return( 1 );
59
60   } // fi
61
62   // Configure reader
63   TParameters reader_params = reader->GetDefaultParameters( );
64   reader_params[ "FileName" ].second = input_image_file;
65   reader_params[ "PixelType" ].second = pixel_type;
66   reader_params[ "ImageDimension" ].second = dimensions;
67   reader_params[ "IsColorImage" ].second = ( is_color )? "1": "0";
68   reader->SetParameters( reader_params );
69
70   // Configure reader
71   TParameters writer_params = writer->GetDefaultParameters( );
72   writer_params[ "FileName" ].second = output_image_file;
73   writer->SetParameters( writer_params );
74
75   // Connect pipeline
76   writer->SetInput( 0, reader->GetOutput( 0 ) );
77
78   std::string msg = writer->Update( );
79
80   if( msg != "" )
81     std::cerr << "ERROR: " << msg << std::endl;
82
83   // Free memory
84   delete writer;
85   delete reader;
86
87   return( 0 );
88 }
89
90 // eof - $RCSfile$