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;
00135 unsigned char *dst = m_image.data.image.raw_data.get_buffer();
00136 for (int i=0; i<image->height; i++){
00137 for (int j=0; j<image->width; j++){
00138 src = image->imageData + image->widthStep*i + j*3;
00139 dst[2] = src[0];
00140 dst[1] = src[1];
00141 dst[0] = src[2];
00142 dst += 3;
00143 }
00144 }
00145 break;
00146 }
00147 case 1:
00148 m_image.data.image.format = Img::CF_GRAY;
00149 memcpy(m_image.data.image.raw_data.get_buffer(),
00150 image->imageData,
00151 m_image.data.image.raw_data.length());
00152 break;
00153 default:
00154 break;
00155 }
00156
00157 cvReleaseImage (&image);
00158
00159 m_imageOut.write();
00160
00161 return RTC::RTC_OK;
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
00197
00198
00199
00200
00201 extern "C"
00202 {
00203
00204 void CameraImageLoaderInit(RTC::Manager* manager)
00205 {
00206 RTC::Properties profile(spec);
00207 manager->registerFactory(profile,
00208 RTC::Create<CameraImageLoader>,
00209 RTC::Delete<CameraImageLoader>);
00210 }
00211
00212 };
00213
00214