]> Creatis software - cpPlugins.git/blob - appli/examples/example_RGBImageToHSVChannels.cxx
Garbage collector added
[cpPlugins.git] / appli / examples / example_RGBImageToHSVChannels.cxx
1 #include <cstdlib>
2 #include <iostream>
3 #include <string>
4
5 #include <cpPlugins/Interface/Interface.h>
6 #include <cpPlugins/Interface/DataObject.h>
7 #include <cpPlugins/Interface/ProcessObject.h>
8
9 // -------------------------------------------------------------------------
10 typedef cpPlugins::Interface::Interface     TInterface;
11 typedef cpPlugins::Interface::DataObject    TDataObject;
12 typedef TInterface::TClasses                TClasses;
13 typedef cpPlugins::Interface::ProcessObject TProcessObject;
14 typedef TProcessObject::TParameters         TParameters;
15
16 // -------------------------------------------------------------------------
17 void SaveImage(
18   TInterface& plugins, const std::string& fname, TDataObject* image
19   )
20 {
21   TProcessObject::Pointer writer;
22   writer = plugins.CreateProcessObject( "cpPlugins::Plugins::ImageWriter" );
23   if( writer.IsNull( ) )
24   {
25     std::cerr << "No suitable writer found in plugins." << std::endl;
26     return;
27
28   } // fi
29   // Configure reader
30   TParameters writer_params = writer->GetDefaultParameters( );
31   writer_params[ "FileName" ].second = fname;
32   writer->SetParameters( writer_params );
33
34   writer->SetInput( 0, image );
35   std::string msg = writer->Update( );
36   if( msg != "" )
37     std::cerr << "ERROR: " << msg << std::endl;
38 }
39
40 // -------------------------------------------------------------------------
41 int main( int argc, char* argv[] )
42 {
43   if( argc < 8 )
44   {
45     std::cerr
46       << "Usage: " << argv[ 0 ]
47       << " plugins_file"
48       << " input_image"
49       << " output_hue_image output_saturation_image output_value_image"
50       << " dimensions pixel_type" << std::endl;
51     return( 1 );
52
53   } // fi
54   std::string plugins_file = argv[ 1 ];
55   std::string input_image_file = argv[ 2 ];
56   std::string output_hue_image_file = argv[ 3 ];
57   std::string output_saturation_image_file = argv[ 4 ];
58   std::string output_value_image_file = argv[ 5 ];
59   std::string dimensions = argv[ 6 ];
60   std::string pixel_type = argv[ 7 ];
61
62   // Create interface
63
64   TInterface plugins;
65   plugins.Load( plugins_file );
66
67   // Create objects
68   TProcessObject::Pointer reader;
69   TProcessObject::Pointer filter;
70
71   reader = plugins.CreateProcessObject( "cpPlugins::Plugins::ImageReader" );
72   if( reader.IsNull( ) )
73   {
74     std::cerr << "No suitable reader found in plugins." << std::endl;
75     return( 1 );
76
77   } // fi
78   filter = plugins.CreateProcessObject( "cpPlugins::Plugins::RGBImageToHSVChannelsFilter" );
79   if( filter.IsNull( ) )
80   {
81     std::cerr << "No suitable filter found in plugins." << std::endl;
82     return( 1 );
83
84   } // fi
85
86   // Configure reader
87   TParameters reader_params = reader->GetDefaultParameters( );
88   reader_params[ "FileName" ].second = input_image_file;
89   reader_params[ "PixelType" ].second = pixel_type;
90   reader_params[ "ImageDimension" ].second = dimensions;
91   reader_params[ "IsColorImage" ].second = "1";
92   reader->SetParameters( reader_params );
93
94   // Connect pipeline
95   filter->SetInput( 0, reader->GetOutput( 0 ) );
96
97   // Save outputs
98   SaveImage( plugins, output_hue_image_file, filter->GetOutput( 0 ) );
99   SaveImage( plugins, output_saturation_image_file, filter->GetOutput( 1 ) );
100   SaveImage( plugins, output_value_image_file, filter->GetOutput( 2 ) );
101
102   return( 0 );
103 }
104
105 // eof - $RCSfile$