Go to the documentation of this file.00001
00010 #include <cv.h>
00011 #include <highgui.h>
00012 #include "CameraImageLoader.h"
00013
00014
00015
00016 static const char* spec[] =
00017 {
00018 "implementation_id", "CameraImageLoader",
00019 "type_name", "CameraImageLoader",
00020 "description", "camera image loader",
00021 "version", HRPSYS_PACKAGE_VERSION,
00022 "vendor", "AIST",
00023 "category", "example",
00024 "activity_type", "DataFlowComponent",
00025 "max_instance", "10",
00026 "language", "C++",
00027 "lang_type", "compile",
00028
00029
00030 ""
00031 };
00032
00033
00034 CameraImageLoader::CameraImageLoader(RTC::Manager* manager)
00035 : RTC::DataFlowComponentBase(manager),
00036
00037 m_imageOut("image", m_image),
00038
00039 dummy(0)
00040 {
00041 }
00042
00043 CameraImageLoader::~CameraImageLoader()
00044 {
00045 }
00046
00047
00048
00049 RTC::ReturnCode_t CameraImageLoader::onInitialize()
00050 {
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 addOutPort("image", m_imageOut);
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 RTC::Properties& prop = getProperties();
00073
00074 return RTC::RTC_OK;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 RTC::ReturnCode_t CameraImageLoader::onActivated(RTC::UniqueId ec_id)
00101 {
00102 std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00103 return RTC::RTC_OK;
00104 }
00105
00106 RTC::ReturnCode_t CameraImageLoader::onDeactivated(RTC::UniqueId ec_id)
00107 {
00108 std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00109 return RTC::RTC_OK;
00110 }
00111
00112 RTC::ReturnCode_t CameraImageLoader::onExecute(RTC::UniqueId ec_id)
00113 {
00114
00115 std::cerr << "image filename: " << std::flush;
00116 std::string filename;
00117 std::cin >> filename;
00118
00119 IplImage *image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
00120 if (!image) {
00121 std::cerr << m_profile.instance_name << ": failed to load("
00122 << filename << ")" << std::endl;
00123 return RTC::RTC_OK;
00124 }
00125
00126 m_image.data.image.width = image->width;
00127 m_image.data.image.height = image->height;
00128 m_image.data.image.raw_data.length(image->imageSize);
00129 switch(image->nChannels){
00130 case 3:
00131 m_image.data.image.format = Img::CF_RGB;
00132 {
00133
00134 char *src = image->imageData;
00135 for (unsigned int i=0; i<m_image.data.image.raw_data.length(); i+=3){
00136 m_image.data.image.raw_data[i+2] = src[i ];
00137 m_image.data.image.raw_data[i+1] = src[i+1];
00138 m_image.data.image.raw_data[i ] = src[i+2];
00139 }
00140 break;
00141 }
00142 case 1:
00143 m_image.data.image.format = Img::CF_GRAY;
00144 memcpy(m_image.data.image.raw_data.get_buffer(),
00145 image->imageData,
00146 m_image.data.image.raw_data.length());
00147 break;
00148 default:
00149 break;
00150 }
00151
00152 cvReleaseImage (&image);
00153
00154 m_imageOut.write();
00155
00156 return RTC::RTC_OK;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 extern "C"
00197 {
00198
00199 void CameraImageLoaderInit(RTC::Manager* manager)
00200 {
00201 RTC::Properties profile(spec);
00202 manager->registerFactory(profile,
00203 RTC::Create<CameraImageLoader>,
00204 RTC::Delete<CameraImageLoader>);
00205 }
00206
00207 };
00208
00209