function m_slice(varargin)

% based on spm_transverse
% SPM,VOL, and hReg are optional and do not have to be specified
%
% function spm_transverse(SPM,VOL,hReg)
%
% Rendering of regional effects [SPM{T/F}] on transverse sections
% FORMAT spm_slices(SPM,VOL,hReg)
%
% SPM    - structure containing SPM, distribution & filtering details
%        - required fields are:
% .Z     - minimum of n Statistics {filtered on u and k}
% .STAT  - distribution {Z, T, X or F}     
% .u     - height threshold
% .XYZ   - location of voxels {voxel coords}
%
% VOL    - structure containing details of volume analysed
%        - required fields are:
% .iM    - mm  -> voxels matrix
% .VOX   - voxel dimensions {mm}
% .DIM   - image dimensions {voxels}
%
% hReg   - handle of MIP XYZ registry object (see spm_XYZreg for details)
%
% spm_transverse automatically updates its co-ordinates from the
% registry, but clicking on the slices has no effect on the registry.
% i.e., the updating is one way only.
%
% ====================================
% kch: add an optional T maximum value
% varargin{1} = d (default = 7)
% ====================================
%
% See also: spm_getSPM
%_______________________________________________________________________
%
% spm_transverse is called by the SPM results section and uses
% variables in SPM and VOL to create three transverse sections though a
% background image.  Regional foci from the selected SPM{T/F} are
% rendered on this image.
%
% Although the SPM{.} adopts the neurological convention (left = left)
% the rendered images follow the same convention as the original data.
%_______________________________________________________________________
% @(#)spm_transverse.m	2.15 Karl Friston - modified by John Ashburner 99/10/14
% modified by Kalina Christoff; 2000/2/27

% declare SPM, VOL, & hReg global, so that they can be used by 
% init(SPM,VOL,hReg) below
%-----------------------------
global SPM;
global VOL;
global hReg;

% if max T is not specified, set default d=maxT=7
%------------------------------------------------
if nargin == 0 & isstruct(SPM),
        d=7; 
        fprintf('\nThe maxT is set to a default of T=7. To use a different');
        fprintf(' value, type m_slices(diff_value), e.g. m_slices(8)\n');
        init(SPM,VOL,hReg);
end;


% if max T is specified, assign it to d=maxT=varargin{1}
%------------------------------------------------------
if nargin == 1 & isstruct(SPM),
        init(SPM,VOL,hReg);
        d=varargin{1};
end;

if ~isstruct(SPM) & strcmp(lower(SPM),'setcoords'),
	reposition(VOL);
end;
if ~isstruct(SPM) & strcmp(lower(SPM),'clear'),
	clear_global;
end;
return;
%_______________________________________________________________________
%_______________________________________________________________________
function init(SPM,VOL,hReg)

%-Get figure handles
%-----------------------------------------------------------------------
Fgraph = spm_figure('GetWin','Graphics');

%-Get the image on which to render
%-----------------------------------------------------------------------
spms   = spm_get(1,'.img','select an image for rendering');
spm('Pointer','Watch');

%-Delete previous axis and their pagination controls (if any)
%-----------------------------------------------------------------------
spm_results_ui('Clear',Fgraph);

% kch - delete all the glass brains and design matrix picture to make
% space for the slices
%---------------------------------------------------------------------
spm_figure('Clear',Fgraph)

global transv
transv = struct('blob',[],'V',spm_vol(spms),'h',[],'hReg',hReg,'fig',Fgraph);
transv.blob = struct('xyz', round(SPM.XYZ), 't',SPM.Z, 'dim',VOL.DIM(1:3),...
	             'iM',VOL.iM,...
		     'vox', sqrt(sum(VOL.M(1:3,1:3).^2)), 'u', SPM.u);


%-Get current location and convert to pixel co-ordinates
%-----------------------------------------------------------------------
xyzmm  = spm_XYZreg('GetCoords',transv.hReg);
xyz    = round(transv.blob.iM(1:3,:)*[xyzmm; 1]);


% extract data from SPM [at one plane separation]
% and get background slices
%-----------------------------------------------------------------------------
dim    = ceil(transv.blob.dim(1:3)'.*transv.blob.vox); % the bounding box in mm
A      = transv.blob.iM*transv.V.mat;
hld    = 0;

zoomM  = inv(spm_matrix([0 0 -1  0 0 0  transv.blob.vox([1 2]) 1]));
zoomM1 =     spm_matrix([0 0  0  0 0 0  transv.blob.vox([1 2]) 1]);


%-Determine paramters
%----------------------
slice_gap  = transv.blob.vox(3);
num_slices = min ([ 22 ; ( ((52 + xyzmm(3)) / slice_gap) +1 ) ]);

maxD=0;

for i = 1 : num_slices

   Q       = find(abs(transv.blob.xyz(3,:) - (xyz(3) -(i-1) ) ) < 0.5);

   T_tmp= full(sparse(transv.blob.xyz(1,Q),transv.blob.xyz(2,Q), ...
                      transv.blob.t(Q),transv.blob.dim(1),transv.blob.dim(2)));
   T_tmp   = spm_slice_vol(T_tmp,zoomM,dim([1 2]),hld);
   T(:,:,i)= T_tmp;
   D            = zoomM1*[1 0 0 0;0 1 0 0;0 0 1 -(xyz(3)-(i-1)); 0 0 0 1]*A; 
   D_tmp        = spm_slice_vol(transv.V,inv(D),dim([1 2]),1);
   Dslice(:,:,i)= D_tmp;
   maxD         = max([maxD ; D_tmp(:)]);

end


for i= 1:num_slices
  Dslice(:,:,i)=Dslice(:,:,i)/maxD;
end

%-Configure {128 level} colormap
%-----------------------------------------------------------------------
cmap   = get(Fgraph,'Colormap');
if size(cmap,1) ~= 128
	figure(Fgraph)
	spm_figure('Colormap','gray-hot')
	cmap   = get(Fgraph,'Colormap');
end

D      = length(cmap)/2;


for i = 1:num_slices
   T_tmp          = T(:,:,i);
   D_tmp          = Dslice(:,:,i);

              Q        = T_tmp(:) > transv.blob.u; 
              T_tmp    = T_tmp(Q)/d;  
              D_tmp(Q) = 1 + T_tmp;
              T_tmp    = D*D_tmp; 

   Tslice(:,:,i) = T_tmp;
   Dslice(:,:,i) = D_tmp;
end

set(Fgraph,'Units','pixels')
siz    = get(Fgraph,'Position');
siz    = siz(3:4);


%-Render activation foci on background images
%-----------------------------------------------------------------------
	P = xyz.*transv.blob.vox';

% Matrix of positions, ([left, bottom, width, height] x 24)
%----------------------------------------------------------

RECT(1,:) = [425 320 215 110 5 425 320 215 110 5   110 5   425 320 215 110 5   425 320 215 110 5   425 320 215 110 5  ];
RECT(2,:) = [0   0   0   0   0 140 140 140 140 140 280 280 420 420 420 420 420 560 560 560 560 560 680 680 680 680 680];

RECT(3,:) = 100;
RECT(4,:) = 120;

for i = 1:num_slices        % index for slices   
   
        j=2*i-1;             % index for transv.h

	transv.h(j) = axes('Units','pixels','Parent',Fgraph, ... 
                           'Position',RECT(:,i)');
	transv.h(j+1) = image(rot90(Tslice(:,:,i)));
	axis image; axis off;
	title(sprintf('z = %0.0fmm',(xyzmm(3) - (i-1)*slice_gap)));

end

clear j

	% colorbar
	%-----------------------------------------------------------------------
	q      = [ 505 280 20 120];
	transv.h(num_slices*2) = axes('Units','pixels','Parent',Fgraph,'Position',q,'Visible','off');
	transv.h(num_slices*2+1) = image([0 d/32],[0 d],[1:D]' + D);
	str    = [SPM.STAT ' value'];
	axis xy; title(str,'FontSize',9);
	set(gca,'XTickLabel',[]);

        % annotation
        %-----------------------------------------------------------------------
        q      = [ 240 280 20 120];
        transv.h(num_slices*2+2) = axes('Units','pixels','Parent',Fgraph,'Position',q,'Visible','off');

        AnBox  = get(transv.h(num_slices*2+2),'Position'); 
        set(transv.h(num_slices*2+2),'YLim',[0,AnBox(4)])

        transv.h(num_slices*2+3) = text(0,95,SPM.title,'Parent', ...
                                     transv.h(num_slices*2+2),'FontWeight','Bold','FontSize',16);

        transv.h(num_slices*2+4) = text(0,65, ['...' SPM.swd(length(SPM.swd)-30 : length(SPM.swd))], ...
                            'Parent',transv.h(num_slices*2+2),'FontWeight','Bold','FontSize',12);

        transv.h(num_slices*2+5) = text(0,45, ...
                sprintf('Height threshold %c = %0.2f',SPM.STAT,SPM.u),'Parent', transv.h(num_slices*2+2), ...
                         'FontSize',12);

        transv.h(num_slices*2+6) = text (0,25, ...
                sprintf('Extent threshold k = %0.0f voxels',SPM.k),'Parent', transv.h(num_slices*2+2), ...
                         'FontSize',12);



%spm_XYZreg('Add2Reg',transv.hReg,transv.h(1), 'spm_transverse');

%for h=transv.h,
%	set(h,'DeleteFcn','spm_transverse(''clear'');');
%end;

%-Reset pointer
%-----------------------------------------------------------------------
%spm('Pointer','Arrow')
%return;
%_______________________________________________________________________



%-Reset pointer
%-----------------------------------------------------------------------
%spm('Pointer','Arrow')
%return;
%_______________________________________________________________________

%_______________________________________________________________________
%function clear_global
%global transv
%if isstruct(transv),
%	for h=transv.h,
%		if ishandle(h), set(h,'DeleteFcn',';'); end;
%	end;
%	for h=transv.h,
%		if ishandle(h), delete(h); end;
%	end;
%	transv = [];
%	clear global transv;
%end;
%return;













