]> Creatis software - cpPlugins.git/blob - appli/examples/example_ReadWriteImage.cxx
0098973bd520c79049c6a60112d1327664adce30
[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 #include <cpPlugins/Interface/Image.h>
9
10 int main( int argc, char* argv[] )
11 {
12   if( argc < 4 )
13   {
14     std::cerr
15       << "Usage: " << argv[ 0 ]
16       << " plugins_file"
17       << " input_image output_image" << std::endl;
18     return( 1 );
19
20   } // fi
21
22   // Create interface
23   typedef cpPlugins::Interface::Interface TInterface;
24   typedef TInterface::TClasses            TClasses;
25   TInterface plugins;
26   if( !plugins.Load( argv[ 1 ] ) )
27   {
28     std::cerr << "Failed to load plugins." << std::endl;
29     return( 1 );
30
31   } // fi
32
33   // Create objects
34   typedef cpPlugins::Interface::ProcessObject TProcessObject;
35   typedef cpPlugins::Interface::Parameters    TParameters;
36   cpPlugins::Interface::ProcessObject::Pointer reader, writer;
37   reader = plugins.CreateProcessObject( "cpPlugins::ImageReader" );
38   if( reader.IsNull( ) )
39   {
40     std::cerr << "No suitable reader found in plugins." << std::endl;
41     return( 1 );
42
43   } // fi
44   writer = plugins.CreateProcessObject( "cpPlugins::ImageWriter" );
45   if( writer.IsNull( ) )
46   {
47     std::cerr << "No suitable writer found in plugins." << std::endl;
48     return( 1 );
49
50   } // fi
51
52   // Configure reader
53   TParameters reader_params = reader->GetDefaultParameters( );
54   for( int i = 2; i < argc - 1; ++i )
55     reader_params.AddValueToStringList( "FileNames", argv[ i ] );
56   reader->SetParameters( reader_params );
57
58   // Configure writer
59   TParameters writer_params = writer->GetDefaultParameters( );
60   writer_params.SetValueAsString( "FileName", argv[ argc - 1 ] );
61   writer->SetParameters( writer_params );
62
63   // Connect pipeline
64   writer->SetInput( 0, reader->GetOutput( 0 ) );
65
66   // Execute pipeline
67   std::string err = writer->Update( );
68   if( err != "" )
69   {
70     std::cerr << "ERROR: " << err << std::endl;
71     return( 1 );
72
73   } // fi
74
75   dynamic_cast< cpPlugins::Interface::Image* >( reader->GetOutput( 0 ) )->GetVTKImageData( )->Print( std::cout );
76
77   return( 0 );
78 }
79
80 // eof - $RCSfile$