function [o, others] = maroi_image(params)
% maroi_image - class constructor
% inputs [defaults]
%  params  - filename ending in .img, defining ROI
%            or spm vol struct (see spm_vol)

myclass = 'maroi_image';
defstruct = struct('vol', [],'func', '');

if nargin < 1
  params = [];
end
if isa(params, myclass)
  o = params;
  return
end

% check for filename;
if ischar(params) 
  params.vol = spm_vol(params);
end
% check for vol struct
if isfield(params, 'fname') 
  params.vol = params;
end

% fill with defaults
pparams = maroi('fillmerge', defstruct, params);

% check and process vol, if passed
if ~isempty(pparams.vol) 
  % check there is a valid vol
  if ~isfield(pparams.vol, 'fname')
    error('No valid image information passed to maroi_image constructor');
  end

  % load image into matrix
  img = spm_read_vols(pparams.vol);
  
  % apply function if passed
  if ~isempty(pparams.func)
    sz = size(img);
    img = eval(pparams.func);
    % check that the image has not changed size
    sz2 = size(img);
    if length(sz) ~= length(sz2) | any(sz ~= sz2)
      error(['Bad function: ' pparams.func ...
	    ' - the image has changed size']);
    end
  end
  
  % prepare for maroi_matrix creation
  pparams.dat = img;
  pparams.mat = pparams.vol.mat;
  
  % fill source information if empty
  if ~isfield(pparams, 'source') | isempty(pparams.source)
    pparams.source = maroi('filename',pparams.vol.fname);
  end
end

% umbrella object, parse out fields for (this object and children)
[uo, pparams] = maroi_matrix(pparams);

% reparse parameters into those for this object, children
[pparams, others] = maroi('split', pparams, defstruct);

o = class(pparams, myclass, uo);
return