function [info,msg] = imfinfo(filename, format)
%IMFINFO Information about graphics file.
%   INFO = IMFINFO(FILENAME,FMT) returns a structure whose
%   fields contain information about an image in a graphics
%   file.  FILENAME is a string that specifies the name of the
%   graphics file, and FMT is a string that specifies the format
%   of the file.  The file must be in the current directory or in
%   a directory on the MATLAB path.  If IMFINFO cannot find a
%   file named FILENAME, it looks for a file named FILENAME.FMT.
%   
%   The possible values for FMT include:
%   
%      'bmp'           Windows Bitmap (BMP)
%      'hdf'           Hierarchical Data Format (HDF)
%      'jpg' or 'jpeg' Joint Photographric Experts Group
%      'pcx'           Windows Paintbrush (PCX)
%      'png'           Portable Network Graphics (PNG)
%      'tif' or 'tiff' Tagged Image File Format (TIFF)
%      'xwd'           X Window Dump (XWD)
%
%   If FILENAME is a TIFF or HDF file containing more than one
%   image, INFO is a structure array with one element for each
%   image in the file.  For example, INFO(3) would contain
%   information about the third image in the file.
%
%   INFO = IMFINFO(FILENAME) attempts to infer the format of the
%   file from its content.
%
%   The set of fields in INFO depends on the individual file and
%   its format.  However, the first nine fields are always the
%   same.  These common fields are:
%
%   Filename       A string containing the name of the file; if
%                  the file is not in the current directory, the
%                  string contains the full pathname of the file
%
%   FileModDate    A string containing the modification date of
%                  the file
%
%   FileSize       An integer indicating the size of the file in
%                  bytes
%
%   Format         A string containing the file format, as
%                  specified by FMT; for JPEG and TIFF files, the
%                  three-letter variant is returned
%
%   FormatVersion  A string or number specifying the file format
%                  version 
%
%   Width          An integer indicating the width of the image
%                  in pixels
%
%   Height         An integer indicating the height of the image
%                  in pixels
%
%   BitDepth       An integer indicating the number of bits per
%                  pixel 
%
%   ColorType      A string indicating the type of image; either
%                  'truecolor' for a truecolor (RGB) image,
%                  'grayscale' for a grayscale intensity image,
%                  or 'indexed', for an indexed image 
%
%   
%   See also IMREAD, IMWRITE.

%   Steven L. Eddins, June 1996
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.12 $  $Date: 1998/05/06 17:46:18 $

error(nargchk(1, 2, nargin));

info = [];
msg = '';

if (nargin < 2)
    % With 1 input argument, we must be able to open the file
    % exactly as given.  Try it.
    fid = fopen(filename, 'r');
    if (fid == -1)
        msg = sprintf('Unable to open file "%s" for reading', filename);
        if (nargout < 2)
            error(msg);
        else
            return;
        end
    else
        filename = fopen(fid);  % Get the full pathname if not in pwd
        fclose(fid);
    end
    
    format = imftype(filename);
    if (isempty(format))
        msg = 'Unable to determine file format';
        if (nargout < 2)
           error(msg);
        else
           return;
        end
    end
else
    % Allow for some variance in specifying file format
    format = lower(format);
    if (strcmp(format,'tiff'))
        format = 'tif';
    elseif (strcmp(format,'jpeg'))
        format = 'jpg';
    end
    
    % See if we can open the file.  If we can't, and since
    % the user explicitly specified the format, see if we
    % can find the file using an extension.
    fid = fopen(filename,'r');
    if (fid == -1)
        switch format
        case 'hdf'
            fid = fopen([filename '.hdf'], 'r');
            
        case 'bmp'
            fid = fopen([filename '.bmp'], 'r');
            
        case 'pcx'
            fid = fopen([filename '.pcx'], 'r');
            
        case 'xwd'
            fid = fopen([filename '.xwd'], 'r');
            
        case 'png'
            fid = fopen([filename '.png'], 'r');
            
        case 'jpg'
            fid = fopen([filename '.jpg'], 'r');
            if (fid == -1)
                fid = fopen([filename '.jpeg'], 'r');
            end
            
        case 'tif'
            fid = fopen([filename '.tif'], 'r');
            if (fid == -1)
                fid = fopen([filename '.tiff'], 'r');
            end
            
        otherwise
	    if (nargout > 1)
	      msg = sprintf('Unrecognized or unsupported format "%s"', format);
	      return;
	    else	    
              error(sprintf('Unrecognized or unsupported format "%s"', format));
            end
        end
        
        if (fid == -1)
	    if (nargout > 1)
	      msg = sprintf('Cannot open "%s" for reading', filename);
	      return;
            else
               error(sprintf('Cannot open "%s" for reading', filename));
            end
        end
    end
    
    filename = fopen(fid);  % gets the full pathname if not in pwd
    fclose(fid);

end

msg = '';

switch format
    case 'hdf'
        [info,msg] = imhdfinfo(filename);
        
    case 'bmp'
        [info,msg] = imbmpinfo(filename);
        
    case 'pcx'
        [info,msg] = impcxinfo(filename);
        
    case 'xwd'
        [info,msg] = imxwdinfo(filename);
        
    case 'jpg'
        [info,msg] = imjpginfo(filename);
        
    case 'tif'
        [info,msg] = imtifinfo(filename);
        
    case 'png'
        [info,msg] = impnginfo(filename);
        
    otherwise
	if (nargout > 1)
          msg = sprintf('Unrecognized or unsupported format "%s"', format);
	  return;
        else
          error(sprintf('Unrecognized or unsupported format "%s"', format));
        end
        
end
            
if ((nargout < 2) & (isempty(info)))
    error(msg);
end
