% scales and displays a greyscale image; returns nothing
%
% displays NaN as red

function like_PVwave(A)

oldmap = colormap;
[M,N]=size(map);
if N~=3
  disp('tvs: colormap is not 3 wide;')
  disp('     i am exiting so you can do something about it')
  return
end%if

if 1-(map == gray(M))   % different effect than map~=gray(M)
  disp1='tvs: colormap not gray so i am setting it to gray; ';
  disp2=sprintf('using same colormap length=%g',M);
  disp([disp1 disp2])
  colormap(gray(M));
end%if

if M>=255
  disp1=sprintf('tvs: colormap length=%g: longer than 254; ',M);
  disp([disp1 'setting to gray(254) and reserving 255 for NaN'])
  M=254;
  colormap(gray(M));
end%if

map = [map;[1 0 0]];  % red for NaN... a red flag for those nasty numbers
disp(sprintf('colormap was %gby%g and is now %gby%g\n',M,N,M+1,N));
colormap(map)
% now length of map is M+1

% scale non-NaN parts of A on 0..M   (so they span the orignal colormap length)
Amin = min2nan(A)
Amax = max2nan(A)
Asc = M*(A-Amin)/(Amax-Amin);
disp('finished computing Asc')
B = chnan(Asc,M+1);
disp('computed B')
image(B)

%axis('equal')
axis('image')   % equal axes with cropping as well.

