function [N,s]=dalinfo(fn)
% dalinfo   Extract size and original head and tail information
% usage:: [Nimg info]=dalinfo(filename).
%
% Extract size and original head and tail information from a bin file
% produced by the Dalsa camera. info is a structure.

% revision history:
% 04/19/96 Mark D. Shattuck <mds> dalinfo.m  
% 12/22/97 Added force type
% 01/28/98 Removed different dalsa types put into one file created imginfo
%          wrapper to parse types; added xillex type
% 09/12/00 added structure info.

s.Nx=260;
s.Ny=260;
s.NI=4;  % number of interlace channels
s.rihead=4;     %raw image head
s.ritail=44;
s.ihead=4;
s.head=0;
s.fdesc=dir(fn);
s.idsize=2;
s.MINPIXVAL=0;
s.MAXPIXVAL=255;
s.type='uint8';        % pixel depth
N=-1;

% parse filename
[s.path,s.fn,s.ext,s.versn]=fileparts(fn);
if(isempty(s.path))
  s.path=pwd;
end
s.fullname=fullfile(s.path,[s.fn s.ext s.versn]);
s.id=s.fn(end-3:end);
s.base=s.fn(1:end-19);
s.date=s.fn(end-17:end-12);
s.time=s.fn(end-10:end-5);
s.rfn=sprintf('%s_%s_%s_%s',s.base,s.date,s.time,s.id);
if(~strcmp(s.fn,s.rfn))
  warning('imginfo:NoParse','Problem parsing filename.\nReconstructed filename=%s ~= original filename=%s.',s.rfn,s.fn);
end

if (isempty(s.fdesc))
  disp('File does not exist!');
  return
end
  
magic=false;
if(readraw(fn,1,'uint16',0,0,s.fdesc.bytes-s.idsize)==hex2dec('fee3'))
  magic=true;  
  s.imgsize=s.Nx*s.Ny+s.rihead+s.ritail;
else
  s.imgsize=s.Nx*s.Ny;
  s.idsize=0;
end
  
N=(s.fdesc.bytes-s.idsize)/s.imgsize;
s.N=N;
  
if (N~=fix(N))
  disp('File length not multiple of imgsize');
  return
end
  
if ((nargout>1) && magic)
    s.iheadtail=uint8(reshape(readraw(fn,(s.rihead+s.ritail)*s.N,'uchar',0,0,s.Nx*s.Nx*s.N),s.rihead+s.ritail,N));
end
