function [im, Err] = ReadImage(filename, format)

% Im = ReadImage(filename)
% Im = ReadImage(filename, format)
% [Im, Err] = ReadImage(...)
% format = 'jpg', 'jpeg', 'tif', 'tiff', 'ppm'

% Liam Pedersen, August 1998, NASA A.R.C.
% version 1.0

% Liam Pedersen, September 1998, CMU
% version 2.0 - reads PPM files.
  
% Liam Pedersen, September 1999, CMU
% version 2.1 - LINUX port.

Err = 0;

% get path
i = find(filename == '/');
if (isempty(i))
   i = 0;
   spath = './';			% default case
else
   i = max(i);
   spath = filename(1:i);
end

% get extension
j = find(filename == '.');
if (isempty(j))
   ext = 'mat';			% default case
   j = length(filename)+1;
else
   ext = filename(j+1:length(filename));
end

% get root file name
fn = filename(i+1:j-1);

% check input arguments
if (nargin == 1)
   format = ext;
end

Err = 0;
fail = 'Err = 1';
if (strcmp(format,'ppm'))
   com = '[r,g,b]=ppmread(filename); im(:,:,1)=uint8(255*r); im(:,:,2)=uint8(255*g); im(:,:,3)=uint8(255*b);';
else
   com = 'im = imread(filename,format);';
end
eval(com,fail);

if (Err == 1)
   im = -1;
end
