 function z = LinInterp2Dc(xc,yc,a,b,c,d,fA)
%  z = LinInterp2D(xc,yc,a,b,c,d,fA)
% Linear interpolation on a grid of f(x,y) evaluations
% xc, yc, a, b, c, and d are scalars that satisfy a<=xc<=b and c<=yc<=d.
% fA is an nXm matrix with the property that
%
% A(i,j) = f(a+(i-1)(b-a)/(n-1), c+(j-1)(d-c)/(m-1), i=1:n, j=1:m
%
% z is a linearly interpolated value of f(xc,yc).
%z=zeros(3,1);
 
[n,m,lc] = size(fA(:,:,:));
%xc = a+(i-1+dx)*hx  0<=dx<=1
hx = (b-a)/(n-1);
i  = max([1 ceil((xc-a)/hx)]);
dx = (xc - (a+(i-1)*hx))/hx;
%yc = c+(j-1+dy)*hy  0<=dy<=1
hy = (d-c)/(m-1);
j  = max([1 ceil((yc-c)/hy)]);
dy = (yc - (c+(j-1)*hy))/hy;
for k=1:lc
   z(k) = uint8((1-dy)*((1-dx)*double(fA(i,j,k))+dx*double(fA(i+1,j,k)))+dy*((1-dx)*double(fA(i,j+1,k))+dx*double(fA(i+1,j+1,k))));
end
%uint8 data is an unsigned integer in range [0,255]; must convert to double
% in order to perform arithmetical operations, then convert back.
% each color component is represented thus; must interpolate each color's
% intensity separately.
% help IMREAD, COLORMAP, UINT8 give necessary info; image 'hub' is a JPG
% file.
