% IMWRITESC - Writes an image to file, rescaling if necessary.
%
% Usage:   imwritesc(im,name)
%         
% Floating point image values are rescaled to the range 0-1 so that no
% overflow occurs when writing 8-bit intensity values.  The image format to
% use is determined by MATLAB from the file ending.
% If the image type is of uint8 no rescaling is performed.

% Peter Kovesi
% School of Computer Science & Software Engineering
% The University of Western Australia
% pk @ csse uwa edu au
% http://www.csse.uwa.edu.au/~pk
%
% October 1999 - original version
% March 2004   - modified to allow colour images of class 'double'

function imwritesc(im,name)

    if strcmp(class(im), 'double')
	im = im - min(im(:));       % Offset so that min value is 0.
	im = im./max(im(:));        % Rescale so that max is 1.
    end
    imwrite(im,name);

