function repI = replicateBounds(I, deltax, deltay, mirror)
% extend the image by replicating its edge rows and columns
% if mirror = 'mirror' then use a mirror extension

if(nargin == 3)
   
   [rows, cols] = size(I);
   onesX = ones([1 deltax]);
   onesY = ones([deltay 1]);
   firstRow = I(1, :);
   lastRow = I(rows, :);
   firstCol = I(:, 1);
   lastCol = I(:, cols);
   ulPix = I(1,1);
   urPix = I(1,cols);
   llPix = I(rows,1);
   lrPix = I(rows,cols);
   
   onesY*onesX;
   onesY*firstRow;
   
   repI = [ ...
         ulPix*onesY*onesX , onesY*firstRow , urPix*onesY*onesX; ...
         firstCol*onesX,     I              , lastCol*onesX; ...
         llPix*onesY*onesX,  onesY*lastRow,   lrPix*onesY*onesX ];
else
   [rows, cols] = size(I);
   if(or(deltax > cols, deltay > rows))
      error('cant replicate bounds with mirror technique : filter is too big');
   end
   
   UL = flipud(fliplr(I(1+1:deltay+1, 1+1:deltax+1)));
   U = flipud(I(1+1:deltay+1, :));
   UR = flipud(fliplr(I(1+1:deltay+1, (cols-deltax-1):cols-1)));
   L = fliplr(I(:, 1+1:deltax+1));
   R = fliplr(I(:, (cols-deltax-1):cols-1));
   BL = flipud(fliplr(I((rows-deltay-1):rows-1, 1+1:deltax+1)));
   B = flipud(I((rows-deltay-1):rows-1, :));
   BR = flipud(fliplr(I((rows-deltay-1):rows-1, (cols-deltax-1):cols-1)));
   
   repI = [ ...
         UL   U    UR ;...
         L    I    R  ;...
         BL   B    BR  ];
      
      
   end
   
      
   
   
   


