function outVal = zoomImage(val, dim)

% Zoom input data to required dimension size

in_dim = size(val);
factor = fix(dim ./ in_dim);
offset = mod(dim, in_dim);
start = factor .* in_dim + 1;

outVal = zeros(dim);
for m = 1:in_dim(1)
for n = 1:in_dim(2)
  outVal(factor(1)*(m-1)+1:factor(1)*m, factor(2)*(n-1)+1:factor(2)*n) = val(m,n);
end
end

if start(1) < dim(1)
  for n = 1:in_dim(2)
    outVal(start(1):dim(1), factor(2)*(n-1)+1:factor(2)*n) = val(end,n);
  end
end

if start(2) < dim(2)
  for m = 1:in_dim(1)
    outVal(factor(1)*(m-1)+1:factor(1)*m, start(2):dim(2)) = val(m,end);
  end
end

if start(1) < dim(1) && start(2) < dim(2)
  outVal(start(1):end, start(2):end) = val(end, end);
end
