%       function A = iplab_read(file)
%               Reads an IPlab file and returns the image in A.
%               A is an array with cols columns and rows rows.
%               rows, cols are optional. Default rows=1024, cols = 1280.
%
%   from Sumant Pattanaik
%   Revised by Stephen H. Westin 18 November 1997
%     to read the header to determine image size and data type.
%   Revised by Stephen H. Westin 06 March 1998
%     for tidier status message.
%
function A = iplab_read(file)

ifl = fopen(file,'r');
% Version number
v = fread(ifl,4,'char');
%version = sprintf ( '%c%c%c%c', v );
% file format (should be zero)
format = fread(ifl,1,'int8');
% Data type (only some are supported)
typeflag = fread(ifl,1,'int8');
if typeflag == 0
  datatype = 'uchar';
elseif typeflag == 1
  datatype = 'int16';
elseif typeflag == 2
  datatype = 'int32';
elseif typeflag == 3
  datatype = 'float';
elseif typeflag == 6
  datatype = 'ushort';
else
  printf ( 'Error: unsupported data type %d\n', typeflag );
end
% Width of image
cols = fread(ifl,1,'int32');
% Height of image
rows = fread(ifl,1,'int32');
% Skip rest of header
tmp = fread(ifl,2106,'char');

fprintf ( 1, 'Reading image file "%s": %d rows by %d columns\n', file, rows, cols );

% Read image data

A=zeros(rows,cols);
for i=1:rows
        B = fread(ifl,cols,datatype);
        A(i,:) = B';
end;
fclose(ifl);
