function imageShifted = shiftImage( img, dispX, dispY )
%Shift the image by dispX, dispY pixels, clamping the edges

dispX = round(dispX);
%since my coordinate system has positive Y going down
dispY = round(-dispY);

s = size(img);
%Left/Right
img = img(:,mod((1:s(2))+dispX,s(2))+1,:);
if(dispX > 0)
   img(:, end-abs(dispX):end, :) = ...
       repmat(img(:,end-abs(dispX)-1, :), 1, abs(dispX)+1); 
elseif(dispX < 0)
    img(:, 1:abs(dispX), :) = ...
       repmat(img(:,abs(dispX)+1, :), 1, abs(dispX)); 
end

%Up/Down
img = img(mod((1:s(1))+dispY,s(1))+1,:,:);
if(dispY > 0)
   img( end-abs(dispY):end,:, :) = ...
       repmat(img(end-abs(dispY)-1, :, :), abs(dispY)+1, 1); 
elseif(dispY < 0)
    img( 1:abs(dispY), :, :) = ...
       repmat(img(abs(dispY)+1,:, :), abs(dispY), 1); 
end

imageShifted = img;

end

