8#include <yarp/os/Network.h> 
   11using namespace yarp::os;
 
   12using namespace yarp::sig;
 
   17    calibToolRight = NULL;
 
   20    time_lastOut=yarp::os::Time::now();
 
   21    dualImage_mode = 
false;
 
   27    calibToolRight = NULL;
 
   32    string str = rf.check(
"name", Value(
"/camCalib"), 
"module name (string)").asString();
 
   33    verboseExecTime = rf.check(
"verboseExecTime");
 
   35    if      (rf.check(
"w_align")) align=ALIGN_WIDTH;
 
   36    else if (rf.check(
"h_align")) align=ALIGN_HEIGHT;
 
   40        requested_fps=rf.find(
"fps").asFloat64();
 
   41        yInfo() << 
"Module will run at " << requested_fps;
 
   45        yInfo() << 
"Module will run at max fps";
 
   50    Bottle botLeftConfig(rf.toString().c_str());
 
   51    Bottle botRightConfig(rf.toString().c_str());
 
   53    string strLeftGroup = 
"CAMERA_CALIBRATION_LEFT";
 
   54    if (botLeftConfig.check(strLeftGroup.c_str()))
 
   56        Bottle &group=botLeftConfig.findGroup(strLeftGroup.c_str(),
string(
"Loading configuration from group " + strLeftGroup).c_str());
 
   57        botLeftConfig.fromString(group.toString());
 
   61        yError() << 
"Group " << strLeftGroup << 
" not found.";
 
   65    string strRightGroup = 
"CAMERA_CALIBRATION_RIGHT";
 
   66    if (botRightConfig.check(strRightGroup.c_str()))
 
   68        Bottle &group=botRightConfig.findGroup(strRightGroup.c_str(),
string(
"Loading configuration from group " + strRightGroup).c_str());
 
   69        botRightConfig.fromString(group.toString());
 
   73        yError() << 
"Group " << strRightGroup << 
" not found.";
 
   77    string calibToolLeftName  = botLeftConfig.check(
"projection", Value(
"pinhole"), 
"Projection/mapping applied to calibrated image [projection|spherical] (string).").asString().c_str();
 
   78    string calibToolRightName = botRightConfig.check(
"projection", Value(
"pinhole"), 
"Projection/mapping applied to calibrated image [projection|spherical] (string).").asString().c_str();
 
   81    if (calibToolLeft!=NULL)
 
   83        bool ok = calibToolLeft->
open(botLeftConfig);
 
   92    if (calibToolRight!=NULL)
 
   94        bool ok = calibToolRight->
open(botRightConfig);
 
   97            delete calibToolRight;
 
   98            calibToolRight = NULL;
 
  105        dualImage_mode = 
true;
 
  106        yInfo() << 
"Dual mode activate!!";
 
  111        leftImage  = 
new yarp::sig::ImageOf<yarp::sig::PixelRgb>;
 
  112        rightImage = 
new yarp::sig::ImageOf<yarp::sig::PixelRgb>;
 
  115        if (yarp::os::Network::exists(getName(
"/dual:i")))
 
  117            yWarning() << 
"port " << getName(
"/dual:i") << 
" already in use";
 
  119        if(! imageInLeft.open(getName(
"/dual:i")) )
 
  121        imageInLeft.setStrict(
false);
 
  125        if (yarp::os::Network::exists(getName(
"/left:i")))
 
  127            yWarning() << 
"port " << getName(
"/left:i") << 
" already in use";
 
  129        if (yarp::os::Network::exists(getName(
"/right:i")))
 
  131            yWarning() << 
"port " << getName(
"/right:i") << 
" already in use";
 
  133        imageInLeft.open(getName(
"/left:i"));
 
  134        imageInRight.open(getName(
"/right:i"));
 
  135        imageInLeft.setStrict(
false);
 
  136        imageInRight.setStrict(
false);
 
  139    if (yarp::os::Network::exists(getName(
"/out")))
 
  141        yWarning() << 
"port " << getName(
"/out") << 
" already in use";
 
  143    if (yarp::os::Network::exists(getName(
"/conf")))
 
  145        yWarning() << 
"port " << getName(
"/conf") << 
" already in use";
 
  147    imageOut.open(getName(
"/out:o"));
 
  148    configPort.open(getName(
"/conf"));
 
  158        imageInRight.close();
 
  161    if (calibToolLeft != NULL)
 
  163        calibToolLeft->
close();
 
  164        delete calibToolLeft;
 
  165        calibToolLeft = NULL;
 
  167    if (calibToolRight != NULL)
 
  169        calibToolRight->
close();
 
  170        delete calibToolRight;
 
  171        calibToolRight = NULL;
 
  178    imageInLeft.interrupt();
 
  180        imageInRight.interrupt();
 
  181    imageOut.interrupt();
 
  182    configPort.interrupt();
 
  192    int dual_rowsize_pixels, dual_height_pixels, single_rowsize_pixels;
 
  193    int dual_rowsize_bytes,  single_rowsize_bytes;
 
  198    unsigned char *left_raw, *right_raw, *dual_raw;
 
  203        yarp::sig::ImageOf<yarp::sig::PixelRgb>* dual = imageInLeft.read();
 
  206            yarp::os::Time::delay(0.001);
 
  210        dual_rowsize_pixels = dual->width();
 
  211        single_rowsize_pixels = dual_rowsize_pixels/2;
 
  212        dual_height_pixels = dual->height();
 
  214        dual_rowsize_bytes = dual->getRowSize();
 
  215        single_rowsize_bytes = dual_rowsize_bytes/2;
 
  217        leftImage->resize(dual_rowsize_pixels/2, dual_height_pixels);
 
  218        rightImage->resize(dual_rowsize_pixels/2, dual_height_pixels);
 
  220        leftImage->setQuantum(dual->getQuantum());
 
  221        rightImage->setQuantum(dual->getQuantum());
 
  223        left_raw  = leftImage->getRawImage();
 
  224        right_raw = rightImage->getRawImage();
 
  225        dual_raw  = dual->getRawImage();
 
  227        for(
int h=0; 
h<dual_height_pixels; 
h++)
 
  229            memcpy(left_raw+(
h*single_rowsize_bytes),  dual_raw+(
h*dual_rowsize_bytes),                         single_rowsize_bytes);
 
  230            memcpy(right_raw+(
h*single_rowsize_bytes), dual_raw+(
h*dual_rowsize_bytes) + single_rowsize_bytes,  single_rowsize_bytes);
 
  235        leftImage  = imageInLeft.read(
false);
 
  236        rightImage = imageInRight.read(
false);
 
  239    yarp::sig::ImageOf<yarp::sig::PixelRgb> &calibratedImgOut=imageOut.prepare();
 
  241    if (calibToolLeft!=NULL && leftImage!=NULL)
 
  243        calibToolLeft->
apply(*leftImage,calibratedImgLeft);
 
  248            if (align == ALIGN_WIDTH)
 
  250                outw = calibratedImgLeft.width()*2;
 
  251                outh = calibratedImgLeft.height();
 
  253            else if (align==ALIGN_HEIGHT)
 
  255                outw = calibratedImgLeft.width();
 
  256                outh = calibratedImgLeft.height()*2;
 
  260                yError() << 
"Invalid alignment";
 
  262            calibratedImgOut.copy(calibratedImgLeft,outw,outh);
 
  266        calibratedImgOut.resize(outw, outh);
 
  267        for (
int r=0; r<calibratedImgLeft.height(); r++)
 
  268        for (
int c=0; c<calibratedImgLeft.width(); c++)
 
  270            unsigned char *pixel = calibratedImgOut.getPixelAddress(c,r);
 
  272            pixel[0] = *(calibratedImgLeft.getPixelAddress(c,r)+0);
 
  273            pixel[1] = *(calibratedImgLeft.getPixelAddress(c,r)+1);
 
  274            pixel[2] = *(calibratedImgLeft.getPixelAddress(c,r)+2);
 
  278    if (calibToolRight!=NULL && rightImage!=NULL)
 
  280        calibToolRight->
apply(*rightImage,calibratedImgRight);
 
  287            if (align == ALIGN_WIDTH)
 
  289                outw = calibratedImgLeft.width()*2;
 
  290                outh = calibratedImgLeft.height();
 
  292            else if (align==ALIGN_HEIGHT)
 
  294                outw = calibratedImgLeft.width();
 
  295                outh = calibratedImgLeft.height()*2;
 
  299                yError() << 
"Invalid alignment";
 
  301            calibratedImgOut.copy(calibratedImgRight,outw,outh);
 
  305        for (
int r=0; r<calibratedImgLeft.height(); r++)
 
  306        for (
int c=0; c<calibratedImgLeft.width(); c++)
 
  310            if      (align == ALIGN_WIDTH)  {cp = c+calibratedImgLeft.width();  rp = r;}
 
  311            else if (align == ALIGN_HEIGHT) {cp = c; rp = r+calibratedImgLeft.height();}
 
  312            unsigned char *pixel = calibratedImgOut.getPixelAddress(cp,rp);
 
  313            pixel[0] = *(calibratedImgRight.getPixelAddress(c,r)+0);
 
  314            pixel[1] = *(calibratedImgRight.getPixelAddress(c,r)+1);
 
  315            pixel[2] = *(calibratedImgRight.getPixelAddress(c,r)+2);
 
  319    if (requested_fps==0)
 
  321        if (lready==
true || rready==
true)
 
  323            imageOut.writeStrict();
 
  324            double diffOut = yarp::os::Time::now() -time_lastOut;
 
  325            time_lastOut = yarp::os::Time::now();
 
  326            if (verboseExecTime) yDebug (
"%f", diffOut);
 
  333        double diffOut = yarp::os::Time::now() - time_lastOut;
 
  334        if (diffOut>(1/requested_fps))
 
  336            imageOut.writeStrict();
 
  337            time_lastOut = yarp::os::Time::now();
 
  338            if (verboseExecTime) yDebug (
"%f", diffOut);
 
  356    if (command.get(0).asString()==
"quit") 
 
  358        reply.addString(
"quitting");
 
  363        yError() << 
"command not known - type help for more info";
 
virtual bool interruptModule()
 
virtual bool respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply)
 
virtual bool configure(yarp::os::ResourceFinder &rf)
Passes config on to CalibTool.
 
virtual bool updateModule()
 
virtual double getPeriod()