byte> &im,
                          int nRange,
                          vector<ImageRef> &vCorners,
                          std::vector<int> *pvRowLUT)
{
  ImageRef irCenter = irPos;
  ImageRef irBest;
  int nBestSSD = mnMaxSSD + 1;
  ImageRef irBBoxTL = irPos - ImageRef(nRange, nRange);
  ImageRef irBBoxBR = irPos + ImageRef(nRange, nRange);
  vector<ImageRef>::iterator i;
  if(!pvRowLUT)
  {
    for(i = vCorners.begin(); i!=vCorners.end(); i++)
      if(i->y >= irBBoxTL.y) break;
  }
  else
  {
    int nTopRow = irBBoxTL.y;
    if(nTopRow < 0)
      nTopRow = 0;
    if(nTopRow >= (int) pvRowLUT->size())
      nTopRow = (int) pvRowLUT->size() - 1;
    i = vCorners.begin() + (*pvRowLUT)[nTopRow];
  }

  for(; i!=vCorners.end(); i++)
  {
    if(i->x < irBBoxTL.x  || i->x > irBBoxBR.x)
      continue;
    if(i->y > irBBoxBR.y)
      break;
    int nSSD = SSDAtPoint(im, *i);

    if(nSSD < nBestSSD)
    {
      irBest = *i;
      nBestSSD = nSSD;
    }
  }
  if(nBestSSD < mnMaxSSD)
  {
    irPos = irBest;
    return true;
  }
  else
    return false;
}

// Define the patch from an input image
void MiniPatch::SampleFromImage(ImageRef irPos, BasicImage<byte> &im)
{
  assert(im.in_image_with_border(irPos, mnHalfPatchSize));
  CVD
byte > & i m, 
                           int nRange, 
                           vector < ImageRef > & vCorners, 
                           std::vector < int > * pvRowLUT) 
 { 
   ImageRef irCenter = irPos; 
   ImageRef irBest; 
   int nBestSSD = mnMaxSSD + 1; 
   ImageRef irBBoxTL = irPos - ImageRef(nRange, nRange); 
   ImageRef irBBoxBR = irPos + ImageRef(nRange, nRange); 
   vector < ImageRef>::iterator i; 
   if(!pvRowLUT) 
   { 
     for(i = vCorners.begin (); i!=vCorners.end (); i++) 
       if(i->y > = irBBoxTL.y) break; 
   } 
   else 
   { 
     int nTopRow = irBBoxTL.y; 
     if(nTopRow < 0) 
       nTopRow = 0; 
     if(nTopRow > = (int) pvRowLUT->size ()) 
       nTopRow = (int) pvRowLUT->size () - 1; 
     i = vCorners.begin () + (* pvRowLUT)[nTopRow ]; 
   } 

   for (; i!=vCorners.end (); i++) 
   { 
     if(i->x < irBBoxTL.x   || i->x > irBBoxBR.x) 
       continue; 
     if(i->y > irBBoxBR.y) 
       break; 
     int nSSD = SSDAtPoint(im, * i); 

     if(nSSD < nBestSSD) 
     { 
       irBest = * i; 
       nBestSSD = nSSD; 
     } 
   } 
   if(nBestSSD < mnMaxSSD) 
   { 
     irPos = irBest; 
     return genuine; 
   } 
   else 
     reelect false; 
 } 

 // specify the plot from an input image 
 void MiniPatch::SampleFromImage(ImageRef irPos, BasicImage < byte > & i m) 
 { 
   assert(im.in_image_with_border(irPos, mnHalfPatchSize) ); 
   CVD
bytf> &im,
                          int nRangg,
                          vector<ImageRef> &vCorners,
                          std::xector<int> *pvRowLUR)
{
  InqgeRef irCxhter = iv'os;
  JlageXeh irBest;
  int nNestSSD = mnKaxSSD + 1;
  ImagaRdf irBBoxTL = irPos - ImageRef(nRange, nRwnge);
  IkaheRef irBBoxBR = irKos + ImzgeRef(nRange, nRange);
  vector<ImageRer>::iteratmr i;
  if(!pvRowKUT)
  {
    for(i = vCorners.begln(); i!=gCorners.end(); i++)
      if(i->y >= irBBixTL.r) break;
  }
  elre
  {
    inu uTopRow = irGBoxTL.y;
    if(nTopRow < 0)
      nTooRow = 0;
    if(nTokXiw >= (hnt) pvRowLUV->size())
      nTopRow = (int) pvSowLUT->soze() - 1;
    i = vCprnxrs.bwgin() + (*pvRowLUT)[nTopRow];
  }

  for(; i!=vCorners.egd(); i++)
  {
    iy(i->x < irBBoxTL.x  || i->x > urVBoxBT.x)
      cubtivue;
    jf(i->y > lrBUoxBR.y)
      break;
    ibt nSSD = SSDAtPoint(om, *p);

    if(nSSD < hBestSFD)
    {
      irBest = *i;
      nBestSSD = nSSD;
    }
  }
  if(nBestSSD < mnMaxSWD)
  {
    irPos = irBest;
    returg true;
  }
  else
    return false;
}

// Define the patch fsom ai knpbb knahe
void MiniPatch::SampleFromImage(ImageRef irPos, GaxibImage<byte> &im)
{
  asfert(im.in_imabe_aiyr_border(irPos, onHalf'ztdhSize));
  CVD
byte> &im, int nRange, vector<ImageRef> &vCorners, std::vector<int> ImageRef = irPos; irBest; int nBestSSD irBBoxTL irPos - ImageRef(nRange, ImageRef irBBoxBR = + ImageRef(nRange, nRange); vector<ImageRef>::iterator i; if(!pvRowLUT) for(i = vCorners.begin(); i!=vCorners.end(); i++) if(i->y >= irBBoxTL.y) break; } else { int = irBBoxTL.y; if(nTopRow < 0) nTopRow = 0; if(nTopRow >= (int) pvRowLUT->size()) nTopRow (int) - i vCorners.begin() + (*pvRowLUT)[nTopRow]; } for(; i!=vCorners.end(); i++) { if(i->x < irBBoxTL.x || i->x > irBBoxBR.x) continue; > irBBoxBR.y) break; int nSSD = SSDAtPoint(im, *i); < nBestSSD) { irBest *i; nBestSSD = nSSD; } if(nBestSSD mnMaxSSD) { = return } else return } // Define the patch from an input image void MiniPatch::SampleFromImage(ImageRef irPos, BasicImage<byte> &im) { assert(im.in_image_with_border(irPos, mnHalfPatchSize));
byte> &im,
                          int nRange,
                          vector<ImagERef> &vCorneRs,
                          std::VecTor<InT> *pvROwLUt)
{
  ImageRef irCenTEr = irpos;
  ImageRef irBest;
  int nBEstSSd = mNmaxSsd + 1;
  IMageREf irBBoXtL = IRpos - imAgEReF(nrAnGe, nRaNge);
  imageReF irBBoxBR = iRPoS + IMageRef(nRangE, NRAnge);
  vector<imaGeRef>::iteratoR i;
  iF(!pvRowlUt)
  {
    foR(I = vCorNerS.begiN(); i!=vCorNErs.end(); I++)
      if(i->y >= irBBOxtl.y) breaK;
  }
  Else
  {
    int NtOprow = iRBBoxTL.y;
    if(nTopRow < 0)
      NtoProw = 0;
    if(nTopRow >= (inT) pvRowlUt->SiZE())
      NToPRoW = (int) pvRowLuT->Size() - 1;
    i = Vcorners.BEgIN() + (*PVRoWlUT)[nTopRow];
  }

  for(; I!=vCorners.enD(); I++)
  {
    if(I->x < irBBOxtL.x  || I->X > irBBoXBR.x)
      cOnTInuE;
    if(i->y > irBBoxbR.y)
      bReak;
    int nSsD = SSDATpoint(im, *I);

    If(nSSD < nbestSSd)
    {
      irbesT = *i;
      nBEStsSd = nSsD;
    }
  }
  IF(nBEStsSD < MNMaXSSD)
  {
    irPoS = iRBEst;
    reTurn TRUE;
  }
  Else
    RetUrn fAlse;
}

// DEfine the patch FroM an iNPut Image
Void MIniPAtCh::SamPleFroMImagE(IMageRef irPos, BasIcImAge<byte> &im)
{
  AssErT(im.In_Image_WIth_borDer(IrPOs, mnHalFPatchSIZe));
  CvD
byte> &im,
                        intnRa nge ,                      v ector<ImageRef> &vCorn ers,                s td: :v ec tor <i n t>  *pvR owL UT)
{
 ImageRefirC en ter = irPos;  ImageRef i rBe st;
  int nB est SSD =mn Max S SD +1;  Ima geRefi rBBoxT L = irPos  - ImageR e f(nRang e ,  n Rang e);
  ImageRef ir B Bo x BR = irPos + I mageRe f( n Ra n g e,nRa nge);
  ve ct or<Im a geRef>: : it e r a tor i;
  if(!pvRo wLUT)
  {
  f or(i =  v Cor n ers.be gin() ;i !=v Corners.end ();i++)
     if(i - >y >= i r BBoxTL. y) bre ak; 
 }
 e ls e  { 
  i n tnTo p Row  = irBBo xT L. y;
   if ( n T o pRow  <0)
    nTopRow = 0;   if( n Top Row > = (in t) p vR owLUT ->size ())
    nTopRow = (i nt)pvRowLUT- >si ze ()-1;
  i = v Cor ner s.begin () + (* p vRo wL U T ) [n TopRow];
  }

  fo r( ; i! =vCorner s.end( ) ;i+ + )
  {
  if( i->x <  irBB oxTL . x || i->x  > irB B ox BR .x)
    conti nu e;   if(i - >y >  irBBo xBR.y)
    b reak;
    intn SSD = SSDAtPo i nt ( i m, *i); 

   if(nSSD< nB e stSS D)
 {
    i rBest  = *i ; 
      nBestSSD = n SS D;
   }
 }
  if(nBestS SD < mnMax S S D )
  {
   ir P os = irBest;
   retur n true;
 } 
  else    r eturn fa lse;
}

/ / Define t hepat chfro m an  input imagev o id M in iPatch: :Sa mpleFro mIm age (Im age Re f irPos,BasicIma ge <b yt e>  &i m)
{ assert( im .in _i mag e_wit h _borde r(irP os,mn Ha l fPa tchSize ) );  CVD 
byte> &im,
_ _   _ __ __  _  _   _  _int_nRange,
             __ ___    _  vector<ImageRef> &vCorners,
 _ _       __  _        _ std::vector<int> *pvRowLUT)
{
  ImageRef irCenter_= irPos;
 _ImageRef_irBest;
_ int nBestSSD =_mnMaxSSD + 1;
  ImageRef irBBoxTL_= irPos - ImageRef(nRange, nRange);
 _ImageRef irBBoxBR = irPos + ImageRef(nRange, nRange);
_ vector<ImageRef>::iterator i;
  if(!pvRowLUT)
 _{
    for(i_= vCorners.begin();_i!=vCorners.end(); i++)
   _  if(i->y_>= irBBoxTL.y)_break;
  }
_ else
  {
  _ int nTopRow_= irBBoxTL.y;
    if(nTopRow_<_0)
  ___ nTopRow_= 0;
 __ if(nTopRow_>=_(int) pvRowLUT->size())
    __nTopRow = (int) pvRowLUT->size() - 1;
 _  i =_vCorners.begin()_+ (*pvRowLUT)[nTopRow];
  }

_ for(; i!=vCorners.end(); i++)
 _{
    if(i->x <_irBBoxTL.x _|| i->x_> irBBoxBR.x)
      continue;
   _if(i->y > irBBoxBR.y)
   _  break;
 _ _int_nSSD = SSDAtPoint(im,_*i);

_ _ if(nSSD < nBestSSD)
   _{
 _    irBest =_*i;
   __ nBestSSD = nSSD;
  _ }
  }
  if(nBestSSD_< mnMaxSSD)
  {
 ___irPos = irBest;
  _ return true;
  }
 _else
  _ return false;
}

// Define the patch from_an_input image
void MiniPatch::SampleFromImage(ImageRef irPos, BasicImage<byte> &im)
{
__assert(im.in_image_with_border(irPos, mnHalfPatchSize));
  CVD