function writetif(data, map, filename, varargin)
%WRITETIF Write a TIF file to disk.
%   WRITETIF(X,MAP,FILENAME) writes the indexed image X,MAP
%   to the file specified by the string FILENAME.
%
%   WRITETIF(GRAY,[],FILENAME) writes the grayscale image GRAY
%   to the file.
%
%   WRITETIF(RGB,[],FILENAME) writes the truecolor image
%   represented by the M-by-N-by-3 array RGB.
%
%   WRITETIF(..., 'compression', COMP) uses the compression
%   type indicated by the string COMP.  COMP can be 'packbits',
%   'ccitt', 'fax3', 'fax4', or 'none'.  'ccitt', 'fax3', and 'fax4'
%   are allowed for logical inputs only.  'packbits' is the default.
%
%   WRITETIF(..., 'description', DES) writes the string contained
%   in DES to the TIFF file as an ImageDescription tag.
%
%   WRITETIF(..., 'resolution', XYRes) uses the scalar in XYRes
%   for the XResolution and YResolution tags.

%   Steven L. Eddins, August 1996
%   Copyright 1984-2000 The MathWorks, Inc.
%   $Revision: 1.13 $  $Date: 2000/06/01 04:17:01 $

if (ndims(data) > 3)
    error(sprintf('%d-D data not supported for TIFF files', ndims(data)));
end

% 1 component or 3 components?
ncomp = size(data,3);
if ((ncomp ~= 1) & (ncomp ~= 3))
    error(sprintf('Data with %d components not supported for TIFF files', ...
            ncomp));
end

description = '';
compression = 'packbits';
resolution = 72;
writemode = 1;

if (islogical(data) & (ndims(data) == 2) & isempty(map))
    if (~isa(data,'uint8'))
        data = uint8(data);
    end
    compression = 'ccitt';
end

% Process param/value pairs
paramStrings = ['compression'
                'description'
                'resolution '
                'writemode  '];
    
for k = 1:2:length(varargin)
    param = lower(varargin{k});
    if (~isstr(param))
        error('Parameter name must be a string');
    end
    idx = strmatch(param, paramStrings);
    if (isempty(idx))
        error(sprintf('Unrecognized parameter name "%s"', param));
    elseif (length(idx) > 1)
        error(sprintf('Ambiguous parameter name "%s"', param));
    end
    
    param = deblank(paramStrings(idx,:));
    
    switch param
    case 'compression'
        compression = varargin{k+1};
        if (~isstr(compression))
            error('COMPRESSION must be a string');
        end
        
    case 'description'
        description = varargin{k+1};
        if (~isstr(description))
            error('DESCRIPTION must be a string');
        end
        
    case 'resolution'
        resolution = varargin{k+1};
        
    case 'writemode'
        switch lower(varargin{k+1})
            case 'overwrite'
                writemode = 1;
                
            case 'append'
                writemode = 0;
                
            otherwise
                error('WRITEMODE must be ''overwrite'' or ''append''.');
        end
    end
end

if ~(isa(data, 'uint8') | isa(data, 'uint16'))
    if (~isempty(map))
        data = uint8(double(data)-1);
    else
        data = min(1, max(0, data));    
        data = uint8(round(255*double(data)));
    end
end

if (~isempty(map) & ~isa(map,'uint16'))
    map = uint16(round(65535*double(map)));
end

wtifc(data, map, filename, compression, description, resolution, writemode);

