
function cutout = getSample(fullsample, i, j, width)
%% getSample(fullsample,i,j,width)
%%  gets a square subsample of fullsample of size (widthxwidth)
%%  starting at (i,j) as the upper-left corner and ending at 
%%  (min(rows,i+w-1),min(columns,j+w-1) .  pads with 0s along
%%  edges. accepts negative ijs, and ijs beyond borders

cutout = zeros(width);
fullsize = size(fullsample);

%% ts_ == target start
ts_i = 1; ts_j = 1;
if (i < 1)
  ts_i = 2-i;
end
if (j < 1)
  ts_j = 2-j;
end

tcopyi = min([fullsize(1)-max(1,i) + 1,width,width+i-1]);
tcopyj = min([fullsize(2)-max(1,j) + 1,width,width+j-1]);

ss_i = max(1,i);
ss_j = max(1,j);

cutout(ts_i:(ts_i+tcopyi-1), ts_j:(ts_j+tcopyj-1)) = ...
    fullsample( ss_i:ss_i+tcopyi-1, ss_j:ss_j+tcopyj-1 );


