00001
00010 #include "CameraImageViewer.h"
00011
00012
00013
00014 static const char* cameraimageviewercomponent_spec[] =
00015 {
00016 "implementation_id", "CameraImageViewer",
00017 "type_name", "CameraImageViewer",
00018 "description", "camera image viewer component",
00019 "version", HRPSYS_PACKAGE_VERSION,
00020 "vendor", "AIST",
00021 "category", "example",
00022 "activity_type", "DataFlowComponent",
00023 "max_instance", "10",
00024 "language", "C++",
00025 "lang_type", "compile",
00026
00027 "conf.default.depthBits", "11",
00028
00029 ""
00030 };
00031
00032
00033 CameraImageViewer::CameraImageViewer(RTC::Manager* manager)
00034 : RTC::DataFlowComponentBase(manager),
00035
00036 m_imageIn("imageIn", m_image),
00037 m_imageOldIn("imageOldIn", m_imageOld),
00038
00039 m_cvImage(NULL),
00040 dummy(0)
00041 {
00042 }
00043
00044 CameraImageViewer::~CameraImageViewer()
00045 {
00046 }
00047
00048
00049
00050 RTC::ReturnCode_t CameraImageViewer::onInitialize()
00051 {
00052 std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00053
00054
00055 bindParameter("depthBits", m_depthBits, "11");
00056
00057
00058
00059
00060
00061
00062 addInPort("imageIn", m_imageIn);
00063 addInPort("imageOldIn", m_imageOldIn);
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 RTC::Properties& prop = getProperties();
00076
00077 return RTC::RTC_OK;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 RTC::ReturnCode_t CameraImageViewer::onActivated(RTC::UniqueId ec_id)
00104 {
00105 std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00106 cvNamedWindow("Image",CV_WINDOW_AUTOSIZE);
00107 return RTC::RTC_OK;
00108 }
00109
00110 RTC::ReturnCode_t CameraImageViewer::onDeactivated(RTC::UniqueId ec_id)
00111 {
00112 std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00113 if (m_cvImage) {
00114 cvReleaseImage(&m_cvImage);
00115 m_cvImage = NULL;
00116 }
00117 cvDestroyWindow("Image");
00118 return RTC::RTC_OK;
00119 }
00120
00121 RTC::ReturnCode_t CameraImageViewer::onExecute(RTC::UniqueId ec_id)
00122 {
00123
00124
00125 if (m_imageIn.isNew()){
00126 do {
00127 m_imageIn.read();
00128 }while(m_imageIn.isNew());
00129 if (m_cvImage && (m_image.data.image.width != m_cvImage->width
00130 || m_image.data.image.height != m_cvImage->height)){
00131 cvReleaseImage(&m_cvImage);
00132 m_cvImage = NULL;
00133 }
00134 if (!m_cvImage){
00135 switch (m_image.data.image.format){
00136 case Img::CF_RGB:
00137 m_cvImage = cvCreateImage(cvSize(m_image.data.image.width,
00138 m_image.data.image.height),
00139 IPL_DEPTH_8U, 3);
00140 break;
00141 case Img::CF_GRAY:
00142 case Img::CF_DEPTH:
00143 m_cvImage = cvCreateImage(cvSize(m_image.data.image.width,
00144 m_image.data.image.height),
00145 IPL_DEPTH_8U, 1);
00146 break;
00147 default:
00148 std::cerr << "unsupported color format("
00149 << m_image.data.image.format << ")" << std::endl;
00150 return RTC::RTC_ERROR;
00151 }
00152 }
00153 switch(m_image.data.image.format){
00154 case Img::CF_RGB:
00155 {
00156
00157 char *dst = m_cvImage->imageData;
00158 for (unsigned int i=0; i<m_image.data.image.raw_data.length(); i+=3){
00159 dst[i ] = m_image.data.image.raw_data[i+2];
00160 dst[i+1] = m_image.data.image.raw_data[i+1];
00161 dst[i+2] = m_image.data.image.raw_data[i ];
00162 }
00163 break;
00164 }
00165 case Img::CF_GRAY:
00166 memcpy(m_cvImage->imageData,
00167 m_image.data.image.raw_data.get_buffer(),
00168 m_image.data.image.raw_data.length());
00169 break;
00170 case Img::CF_DEPTH:
00171 {
00172
00173 char *dst = m_cvImage->imageData;
00174 Img::ImageData &id = m_image.data.image;
00175 unsigned short *src = (unsigned short *)id.raw_data.get_buffer();
00176 int shift = m_depthBits - 8;
00177 for (unsigned int i=0; i<id.width*id.height; i++){
00178 dst[i] = 0xff - src[i]>>shift;
00179 }
00180 }
00181 break;
00182 default:
00183 break;
00184 }
00185 }
00186
00187 if (m_imageOldIn.isNew()){
00188 do {
00189 m_imageOldIn.read();
00190 }while(m_imageOldIn.isNew());
00191 if (m_cvImage && (m_imageOld.width != m_cvImage->width
00192 || m_imageOld.height != m_cvImage->height)){
00193 cvReleaseImage(&m_cvImage);
00194 m_cvImage = NULL;
00195 }
00196 int bytes = m_imageOld.bpp/8;
00197 if (!bytes){
00198 bytes = m_imageOld.pixels.length()/(m_imageOld.width*m_imageOld.height);
00199 }
00200 if (!m_cvImage){
00201 m_cvImage = cvCreateImage(cvSize(m_imageOld.width,
00202 m_imageOld.height),
00203 IPL_DEPTH_8U, bytes);
00204 }
00205 switch(bytes){
00206 case 1:
00207 memcpy(m_cvImage->imageData,
00208 m_imageOld.pixels.get_buffer(),
00209 m_imageOld.pixels.length());
00210 break;
00211 case 3:
00212
00213 char *dst = m_cvImage->imageData;
00214 for (unsigned int i=0; i<m_imageOld.pixels.length(); i+=3){
00215 dst[i ] = m_imageOld.pixels[i+2];
00216 dst[i+1] = m_imageOld.pixels[i+1];
00217 dst[i+2] = m_imageOld.pixels[i ];
00218 }
00219 break;
00220 }
00221 }
00222 if (m_cvImage){
00223 cvShowImage("Image",m_cvImage);
00224 cvWaitKey(10);
00225 }
00226
00227 return RTC::RTC_OK;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 extern "C"
00268 {
00269
00270 void CameraImageViewerInit(RTC::Manager* manager)
00271 {
00272 RTC::Properties profile(cameraimageviewercomponent_spec);
00273 manager->registerFactory(profile,
00274 RTC::Create<CameraImageViewer>,
00275 RTC::Delete<CameraImageViewer>);
00276 }
00277
00278 };
00279
00280