function ImgHndl=gimage(x,P1,P2,P3,P4)
%GIMAGE	Display a grayscale image
%	ImgHndl=gimage(x) displays a grayscale image with 64 graylevels.
%	NaN's are automatically replaced by the minimum number in the image
%
%	ImgHndl=gimage(x,'Levels',L) displays a grayscale image with L 
%	graylevels.
%
%	ImgHndl=gimage(x,'Colorbar','Right') displays a colorbar on the
%	right side of the image (default)
%
%	ImgHndl=gimage(x,'Colorbar','Bottom') displays a colorbar below the
%	image 
%
%	ImgHndl=gimage(x,'Colorbar','Off') do not display a colorbar

L=64;
ColorBarPos='off';

for i=2:2:nargin,
  Command=eval(sprintf('P%d',i-1));
  Parameter=eval(sprintf('P%d',i));
  if strcmp(lower(Command),'levels'),
    L=Parameter;
  elseif strcmp(lower(Command),'colorbar'),
    if strcmp(lower(Parameter),'right'),
      ColorBarPos='vert';
    elseif strcmp(lower(Parameter),'bottom'),
      ColorBarPos='horiz';
    elseif strcmp(lower(Parameter),'off'),
      ColorBarPos='off';
    elseif strcmp(lower(Parameter),'on'),
      ColorBarPos='vert';
    end
  end
end

x=NaN2Val(x,min2(x));
ImgHndl=imagesc(x);
axis image;
if ~strcmp(ColorBarPos,'off'),
  colorbar(ColorBarPos);
end

if L~=64,
  map=1:L;
  map=[map;map;map];
  colormap(map);
else
  colormap(gray);
end

