]> Creatis software - cpPlugins.git/blob - appli/examples/example_RGBImageToHSVChannels.cxx
7266800302c47aa44ebd84b1b82b587d26c3f1e3
[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   cpPlugins::Interface::ProcessObject* writer;
22   writer =
23     dynamic_cast< TProcessObject* >(
24       plugins.CreateObject( "cpPlugins::Plugins::ImageWriter" )
25       );
26   if( writer == NULL )
27   {
28     std::cerr << "No suitable writer found in plugins." << std::endl;
29     return;
30
31   } // fi
32   // Configure reader
33   TParameters writer_params = writer->GetDefaultParameters( );
34   writer_params[ "FileName" ].second = fname;
35   writer->SetParameters( writer_params );
36
37   writer->SetInput( 0, image );
38   std::string msg = writer->Update( );
39   if( msg != "" )
40     std::cerr << "ERROR: " << msg << std::endl;
41   delete writer;
42 }
43
44 // -------------------------------------------------------------------------
45 int main( int argc, char* argv[] )
46 {
47   if( argc < 8 )
48   {
49     std::cerr
50       << "Usage: " << argv[ 0 ]
51       << " plugins_file"
52       << " input_image"
53       << " output_hue_image output_saturation_image output_value_image"
54       << " dimensions pixel_type" << std::endl;
55     return( 1 );
56
57   } // fi
58   std::string plugins_file = argv[ 1 ];
59   std::string input_image_file = argv[ 2 ];
60   std::string output_hue_image_file = argv[ 3 ];
61   std::string output_saturation_image_file = argv[ 4 ];
62   std::string output_value_image_file = argv[ 5 ];
63   std::string dimensions = argv[ 6 ];
64   std::string pixel_type = argv[ 7 ];
65
66   // Create interface
67
68   TInterface plugins;
69   plugins.Load( plugins_file );
70
71   // Create objects
72   cpPlugins::Interface::ProcessObject* reader;
73   cpPlugins::Interface::ProcessObject* filter;
74
75   reader =
76     dynamic_cast< TProcessObject* >(
77       plugins.CreateObject( "cpPlugins::Plugins::ImageReader" )
78       );
79   if( reader == NULL )
80   {
81     std::cerr << "No suitable reader found in plugins." << std::endl;
82     return( 1 );
83
84   } // fi
85   filter =
86     dynamic_cast< TProcessObject* >(
87       plugins.CreateObject( "cpPlugins::Plugins::RGBImageToHSVChannelsFilter" )
88       );
89   if( filter == NULL )
90   {
91     std::cerr << "No suitable filter found in plugins." << std::endl;
92     return( 1 );
93
94   } // fi
95
96   // Configure reader
97   TParameters reader_params = reader->GetDefaultParameters( );
98   reader_params[ "FileName" ].second = input_image_file;
99   reader_params[ "PixelType" ].second = pixel_type;
100   reader_params[ "ImageDimension" ].second = dimensions;
101   reader_params[ "IsColorImage" ].second = "1";
102   reader->SetParameters( reader_params );
103
104   // Connect pipeline
105   filter->SetInput( 0, reader->GetOutput( 0 ) );
106
107   // Save outputs
108   SaveImage( plugins, output_hue_image_file, filter->GetOutput( 0 ) );
109   SaveImage( plugins, output_saturation_image_file, filter->GetOutput( 1 ) );
110   SaveImage( plugins, output_value_image_file, filter->GetOutput( 2 ) );
111
112   // Free memory
113   delete filter;
114   delete reader;
115
116   return( 0 );
117 }
118
119 // eof - $RCSfile$