5 #include <yarp/os/Os.h>
6 #include <yarp/os/Time.h>
7 #include <yarp/os/LogStream.h>
12 using namespace yarp::os;
13 using namespace yarp::sig;
29 bool shouldConnect =
false;
32 cout<<
"Usage:"<<endl<<
"<imageSplitter> --param1 arg1 --param2 arg2 ..."<<endl;
33 cout<<
"Here the available parameters:"<<endl;
34 cout<<
"align specify if the alignement of the images is 'vertical' or 'horizontal'"<<endl;
35 cout<<
"local prefix for the ports that will be opened by this module, if not specified the default is '/imageSplitter'"<<endl;
36 cout<<
"nameInput name of the module's' input port, if not specified by default is <local> + '/input:i'"<<endl;
37 cout<<
"nameLeft name of the output port for the 'left image', if not specified by default is <local> + '/left:o'"<<endl;
38 cout<<
"nameRight name of the output port for the 'right image', if not specified by default is <local> + '/right:o'"<<endl;
39 cout<<
"remote name of the source port, if specified it connects automatically to the module's input port"<<endl;
45 string align = rf.find(
"align").asString();
46 if(align ==
"vertical")
50 else if(align ==
"horizontal")
56 yError() <<
" Incorrect parameter. Supported values for alignment are 'horizontal' or 'vertical'";
61 string outLeftPortName;
62 string outRightPortName;
67 string rootName = rf.find(
"local").asString();
68 inputPortName = rootName +
"/input:i";
69 outLeftPortName = rootName +
"/left:o";
70 outRightPortName = rootName +
"/right:o";
73 inputPortName =
"/imageSplitter/input:i";
74 outLeftPortName =
"/imageSplitter/left:o";
75 outRightPortName =
"/imageSplitter/right:o";
77 if (rf.check(
"nameInput"))
79 inputPortName = rf.find(
"nameInput").asString();
82 if(rf.check(
"nameLeft"))
84 outLeftPortName = rf.find(
"nameLeft").asString();
87 if(rf.check(
"nameRight"))
89 outRightPortName = rf.find(
"nameRight").asString();
94 ret &= inputPort.open(inputPortName);
95 ret &= outLeftPort.open(outLeftPortName);
96 ret &= outRightPort.open(outRightPortName);
98 string remotePortName;
100 if(rf.check(
"remote"))
102 remotePortName = rf.find(
"remote").asString();
103 shouldConnect =
true;
107 yInfo() <<
"ImageSplitter: waiting for connection to port" << inputPortName;
111 yError() <<
" Cannot open ports";
117 yInfo(
"connecting to the source...\n");
118 if(! yarp::os::Network::connect(remotePortName, inputPortName))
120 yError() <<
"Cannot connect to remote port " << remotePortName;
128 string align = rf.find(
"m").asString();
133 else if(align ==
"pixel2")
137 else if(align ==
"line")
141 else if(align ==
"whole")
144 yError() <<
"Cannot use 'whole' method for input image horizontally aligned";
149 yError() <<
"Methods are pixel, line, whole; got " << align;
154 yInfo() <<
"using method " << method;
171 ImageOf<PixelRgb> *inputImage = inputPort.read();
172 yarp::os::Stamp stamp;
173 inputPort.getEnvelope(stamp);
175 ImageOf<PixelRgb> &outLeftImage = outLeftPort.prepare();
176 ImageOf<PixelRgb> &outRightImage = outRightPort.prepare();
178 inWidth = inputImage->width();
179 inHeight = inputImage->height();
183 outWidth = inWidth/2;
184 outHeight = inHeight;
189 outHeight = inHeight/2;
192 outLeftImage.setQuantum(inputImage->getQuantum());
193 outRightImage.setQuantum(inputImage->getQuantum());
194 outLeftImage.resize(outWidth, outHeight);
195 outRightImage.resize(outWidth, outHeight);
199 unsigned char *pixelLeft, *pixelRight;
200 unsigned char *pixelInputL, *pixelInputR;
201 unsigned char *pixelInput = inputImage->getRawImage();
202 int dualImage_rowSizeByte = inputImage->getRowSize();
203 int singleImage_rowSizeByte = outLeftImage.getRowSize();
204 int singleImage_wholeSizeByte = outWidth * outHeight * outLeftImage.getPixelSize();
206 static int counter = 0;
207 static double start = 0;
208 start = yarp::os::Time::now();
216 for(
int h=0;
h<outHeight;
h++)
218 for(
int w1=0; w1<outWidth; w1++)
221 pixelLeft = outLeftImage.getPixelAddress(w1,
h);
222 pixelLeft[0] = *(inputImage->getPixelAddress(w1,
h)+0);
223 pixelLeft[1] = *(inputImage->getPixelAddress(w1,
h)+1);
224 pixelLeft[2] = *(inputImage->getPixelAddress(w1,
h)+2);
226 pixelRight = outRightImage.getPixelAddress(w1,
h);
227 pixelRight[0] = *(inputImage->getPixelAddress(w2,
h)+0);
228 pixelRight[1] = *(inputImage->getPixelAddress(w2,
h)+1);
229 pixelRight[2] = *(inputImage->getPixelAddress(w2,
h)+2);
235 for(
int h1=0; h1<outHeight; h1++)
237 for(
int w=0; w<outWidth; w++)
240 pixelLeft = outLeftImage.getPixelAddress(w, h1);
241 pixelLeft[0] = *(inputImage->getPixelAddress(w, h1)+0);
242 pixelLeft[1] = *(inputImage->getPixelAddress(w, h1)+1);
243 pixelLeft[2] = *(inputImage->getPixelAddress(w, h1)+2);
245 pixelRight = outRightImage.getPixelAddress(w, h1);
246 pixelRight[0] = *(inputImage->getPixelAddress(w, h2)+0);
247 pixelRight[1] = *(inputImage->getPixelAddress(w, h2)+1);
248 pixelRight[2] = *(inputImage->getPixelAddress(w, h2)+2);
258 pixelLeft = outLeftImage.getRawImage();
259 pixelRight = outRightImage.getRawImage();
261 pixelInputL = pixelInput;
262 pixelInputR = pixelInput+singleImage_rowSizeByte;
263 for(
int h=0,
idx=0, idx2=0;
h<outHeight;
h++)
265 for(
int w=0; w<outWidth; w++)
267 pixelLeft[
idx++] = *(pixelInputL++);
268 pixelLeft[
idx++] = *(pixelInputL++);
269 pixelLeft[
idx++] = *(pixelInputL++);
271 pixelRight[idx2++] = *(pixelInputR++);
272 pixelRight[idx2++] = *(pixelInputR++);
273 pixelRight[idx2++] = *(pixelInputR++);
275 pixelInputL += singleImage_rowSizeByte;
276 pixelInputR += singleImage_rowSizeByte;
289 pixelLeft = outLeftImage.getRawImage();
290 pixelRight = outRightImage.getRawImage();
292 for(
int h=0;
h<inHeight;
h++)
294 memcpy(pixelLeft +
h*singleImage_rowSizeByte, pixelInput, singleImage_rowSizeByte);
295 memcpy(pixelRight +
h*singleImage_rowSizeByte, pixelInput+=singleImage_rowSizeByte, singleImage_rowSizeByte);
296 pixelInput+= dualImage_rowSizeByte/2;
301 pixelLeft = outLeftImage.getRawImage();
302 pixelRight = outRightImage.getRawImage();
303 pixelInputL = pixelInput;
304 pixelInputR = pixelInput+singleImage_wholeSizeByte;
306 for(
int h=0;
h<outHeight;
h++)
308 memcpy(pixelLeft +
h*singleImage_rowSizeByte, pixelInputL, singleImage_rowSizeByte);
309 memcpy(pixelRight +
h*singleImage_rowSizeByte, pixelInputR, singleImage_rowSizeByte);
310 pixelInputL+= singleImage_rowSizeByte;
311 pixelInputR+= singleImage_rowSizeByte;
320 yError() <<
"Cannot use this copy method with horizontally aligned source image.";
324 pixelLeft = outLeftImage.getRawImage();
325 pixelRight = outRightImage.getRawImage();
327 memcpy(pixelLeft, pixelInput, singleImage_wholeSizeByte);
328 memcpy(pixelRight, pixelInput+ singleImage_wholeSizeByte, singleImage_wholeSizeByte);
334 yError() <<
" @line " << __LINE__ <<
"unhandled switch case, we should not be here!";
338 static double end = 0;
339 static double elapsed = 0;
340 end = yarp::os::Time::now();
341 elapsed += (end-start);
344 if((counter % 100) == 0)
346 yInfo() <<
"Elapsed time: " << elapsed;
350 outLeftPort.setEnvelope(stamp);
351 outRightPort.setEnvelope(stamp);
354 outRightPort.write();
bool configure(yarp::os::ResourceFinder &rf)
static uint32_t idx[BOARD_NUM]