function specimage(x, sizeofimage, maxval, minval)

% specimage(x [, sizeofimage] [, maxval] [, minval]);
%    display colored 3D map (for sound spectrogram)

global Xdisplay;

if nargin < 2
  sizeofimage = [600 400];
end
if nargin == 0
  help specimage
  return;
end
if nargin < 4
  minval = min (min (x));
  if nargin < 3  
    maxval = max (max (x));
  end
end
    
if findstr(computer, 'cygwin32')
  cygwin = 1;
  viewer = 'lview31';
else
  cygwin = 0;
end

## Rescale matrix so that all values are in the range 0 to
## length (colormap) inclusive.

if (maxval == minval)
  x = ones (high, wide);
else
  ## Rescale values to between 1 and length (colormap) inclusive.
  x = fix ((x - minval) / (maxval - minval) * (length ...
      (colormap) - 1)) + 1;
end

ppm_name = tmpnam ();
  
saveimage (ppm_name, x, "ppm");

## Start the viewer.  Try display (ImageMagick)
      
% dispcom = sprintf ("display -negate -rotate 90 -geometry %dx%d! pnm:%s", ...
%    sizeofimage(1), sizeofimage(2), ppm_name);
if cygwin
  ppm2_name = [tmpnam(), '.bmp'];
  convcom = sprintf("convert -flip -geometry %dx%d! pnm:%s %s", ...
      sizeofimage(1), sizeofimage(2), ppm_name, ppm2_name);
  system(convcom);
  dispcom = sprintf("%s %s", viewer, ppm2_name);
elseif ~isempty(Xdisplay)
  keyboard;
  dispcom = sprintf("display -display %s -flip -geometry %dx%d! pnm:%s", ...
      Xdisplay, sizeofimage(1), sizeofimage(2), ppm_name);
else
  dispcom = sprintf("display -flip -geometry %dx%d! pnm:%s", ...
      sizeofimage(1), sizeofimage(2), ppm_name);
end
	

## Need to let the shell clean up the tmp file because we are putting
## the viewer in the background.

if cygwin
  rm = sprintf ("rm -f %s %s", ppm_name, ppm2_name);
  command = sprintf("%s; %s", dispcom, rm);
  system(command, 1, 'async');
else
  rm = sprintf ("rm -f %s", ppm_name);
  command = sprintf ("( %s && %s ) > /dev/null 2>&1 &", ...
      dispcom, rm);
%  system (command);
end

					 
			  
