%
% function [img,map]=imload(imgFile,rawWidth,rawHeight);
%          loads an image to a table
%          known image formats are: RAW, PNM, TIF, JPG, BMP, HDF, PCX, XWD
%

%
% Revision 1, Copyright (c) 31-Oct-2002 George P. Pavlidis (gpavlid@ceti.gr, gpavlid@ee.duth.gr)
%
function [img,map]=imload(varargin);

	if nargin==0,error('IMLOAD: no image file specified!');end;

	img=[];map=[];
	formats = { 'RAW', 'PPM', 'PGM', 'PBM', 'JPG', 'TIF', 'GIF', ...
					'BMP', 'PNG', 'HDF', 'PCX', 'XWD', 'ICO', 'CUR'};
	imgFile=varargin{1};

	%% check	file parts
	[PTH,NM,EXT] = fileparts( imgFile);
	if isempty( EXT) | strcmp( EXT, '.') | length( EXT )>4,		%% the user gave no file extension
		for i = 1: length( formats),
			if exist( [imgFile,'.',lower(formats{i})], 'file'),
				imgFile = [imgFile,'.',lower(formats{i})];
				break;
			end;
			if exist( [imgFile,'.',formats{i}], 'file'),
				imgFile = [imgFile,'.',formats{i}];
				break;
			end;
		end;
	end;
	[PTH,NM,EXT] = fileparts( imgFile);

	%% RAW file format needs extra parameters
	if strcmp(upper(EXT),'.RAW'),
		if (nargin<3),
			error('IMLOAD: Width and Height parameters needed for raw images!');
		else
			rawWidth=varargin{2};
			rawHeight=varargin{3};
		end;
	end;
		
	switch upper( EXT ),
	case '.RAW',
		img=rawread(imgFile,rawWidth,rawHeight);
	case '.PPM',
		[img,map]=ppmread(imgFile);
	case '.PGM',
		img=pgmread(imgFile);
	case '.PBM',
		img=pbmread(imgFile);
	otherwise,
		img=double(imread(imgFile));
	end;
