Go to the documentation of this file.00001
00010 #include <cv.h>
00011 #include <highgui.h>
00012 #include "CameraImageSaver.h"
00013
00014
00015
00016 static const char* spec[] =
00017 {
00018 "implementation_id", "CameraImageSaver",
00019 "type_name", "CameraImageSaver",
00020 "description", "camera image saver",
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 "conf.defalt.basename", "image",
00030
00031 ""
00032 };
00033
00034
00035 CameraImageSaver::CameraImageSaver(RTC::Manager* manager)
00036 : RTC::DataFlowComponentBase(manager),
00037
00038 m_imageIn("image", m_image),
00039
00040 m_count(0),
00041 dummy(0)
00042 {
00043 }
00044
00045 CameraImageSaver::~CameraImageSaver()
00046 {
00047 }
00048
00049
00050
00051 RTC::ReturnCode_t CameraImageSaver::onInitialize()
00052 {
00053
00054
00055
00056 bindParameter("basename", m_basename, "image");
00057
00058
00059
00060
00061
00062
00063 addInPort("image", m_imageIn);
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 CameraImageSaver::onActivated(RTC::UniqueId ec_id)
00104 {
00105 std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00106 return RTC::RTC_OK;
00107 }
00108
00109 RTC::ReturnCode_t CameraImageSaver::onDeactivated(RTC::UniqueId ec_id)
00110 {
00111 std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00112 return RTC::RTC_OK;
00113 }
00114
00115 RTC::ReturnCode_t CameraImageSaver::onExecute(RTC::UniqueId ec_id)
00116 {
00117
00118 if (m_imageIn.isNew()){
00119 m_imageIn.read();
00120
00121 IplImage* cvImage;
00122 switch (m_image.data.image.format){
00123 case Img::CF_RGB:
00124 cvImage = cvCreateImage(cvSize(m_image.data.image.width,
00125 m_image.data.image.height),
00126 IPL_DEPTH_8U, 3);
00127 break;
00128 case Img::CF_GRAY:
00129 cvImage = cvCreateImage(cvSize(m_image.data.image.width,
00130 m_image.data.image.height),
00131 IPL_DEPTH_8U, 1);
00132 break;
00133 default:
00134 std::cerr << "unsupported color format("
00135 << m_image.data.image.format << ")" << std::endl;
00136 return RTC::RTC_ERROR;
00137 }
00138 switch(m_image.data.image.format){
00139 case Img::CF_RGB:
00140 {
00141
00142 char *dst = cvImage->imageData;
00143 for (unsigned int i=0; i<m_image.data.image.raw_data.length(); i+=3){
00144 dst[i ] = m_image.data.image.raw_data[i+2];
00145 dst[i+1] = m_image.data.image.raw_data[i+1];
00146 dst[i+2] = m_image.data.image.raw_data[i ];
00147 }
00148 break;
00149 }
00150 case Img::CF_GRAY:
00151 memcpy(cvImage->imageData,
00152 m_image.data.image.raw_data.get_buffer(),
00153 m_image.data.image.raw_data.length());
00154 break;
00155 default:
00156 break;
00157 }
00158
00159 char fname[256];
00160 sprintf(fname, "%s%04d.png", m_basename.c_str(), m_count++);
00161 cvSaveImage(fname, cvImage);
00162
00163 cvReleaseImage(&cvImage);
00164 }
00165
00166 return RTC::RTC_OK;
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
00202
00203
00204
00205
00206 extern "C"
00207 {
00208
00209 void CameraImageSaverInit(RTC::Manager* manager)
00210 {
00211 RTC::Properties profile(spec);
00212 manager->registerFactory(profile,
00213 RTC::Create<CameraImageSaver>,
00214 RTC::Delete<CameraImageSaver>);
00215 }
00216
00217 };
00218
00219