function S = setUpGUI(image3D)
% S = SETUPGUI(IMAGE3D) sets up the graphical user interface for random
% walker based segmentation. 
% Input: IMAGE3D = 3D image
% Output:      S = structure containing handles of different elements of
%                  the GUI
%
% 03/15/2010 - Nayanjyoti Pathak

% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
% USA.


SCR = get(0,'Screensize');  % Get screensize.
[r, c, maxSlice] = size(image3D);
Dim  = getDimensions(r, c, SCR);

% Figure handle
S.fh = figure('numbertitle','off',...
              'units','pixels',...
              'position',Dim.fh,...
              'name','RW Segmentation tool',...
              'resize','on');
% Axes displayig input image and results probability map              
S.ax(1) = axes('Parent',S.fh, ...
               'units','pixels', ...
               'box','off', ...
               'visible','off',...
               'position',Dim.ax{1});
          imagesc(image3D(:,:,round(maxSlice/2)));colormap(gray); axis off
S.ax(2) = axes('Parent',S.fh, ...
               'units','pixels', ...
               'box','off', ...
               'visible','off',...
               'position',Dim.ax{2});
          probabilities = rand(size(image3D));
          imagesc(probabilities(:,:,round(maxSlice/2)));colormap(gray); axis off
% Panels for toggle buttons and parmeters interface        
S.ph(1) = uipanel('parent', S.fh,...
               'Title','',...
               'FontSize',12,...
               'BackgroundColor','white',...
               'units','pixels',...
               'position',Dim.ph{1});
S.ph(2) = uipanel('parent', S.fh,...
               'Title','Parameters',...
               'FontSize',12,...
               'BackgroundColor','white',...
               'units','pixels',...
               'position',Dim.ph{2});
% Slider           
S.sl = uicontrol('style','slide',...
                 'unit','pix',...
                 'position',Dim.sl,...
                 'SliderStep',[1/(maxSlice-1) 10/(maxSlice-1)],...
                 'min',1,'max',maxSlice,'val',uint16(round(maxSlice/2)));
% Toggle buttons             
S.tg(1) = uicontrol('style','toggle',... % Visualize Image
                    'units','pixels',...
                    'position',Dim.tg{1},...
                    'string','Visualize Image',...
                    'val',1);
S.tg(2) = uicontrol('style','toggle',...
                    'units','pixels',... % Undo
                    'position',Dim.tg{2},...
                    'string','Undo',...
                    'value',0,...
                    'enable','on');
S.tg(3) = uicontrol('style','toggle',... % ABOUT
                    'units','pixels',...
                    'position',Dim.tg{3},...
                    'string','ABOUT',...
                    'value',0,...
                    'enable','on');
S.tg(4) = uicontrol('style','toggle',... % Select Slice
                    'units','pixels',...
                    'position',Dim.tg{4},...
                    'string','Select Slice',...
                    'value',0,...
                    'enable','on');    
S.tg(5) = uicontrol('style','toggle',... % Done?
                    'units','pixels',...
                    'position',Dim.tg{5},...
                    'string','Done?',...
                    'value',0,...
                    'enable','on');
S.tg(6) = uicontrol('style','toggle',... % Done?
                    'units','pixels',...
                    'position',Dim.tg{6},...
                    'string','Load Results',...
                    'value',0,...
                    'enable','on');
% Get help text to display on the GUI
txt = HelpRW;               
S.tx = uicontrol('style','list',...
                 'units','pixels',...
                 'BackgroundColor','w',...
                 'position',Dim.tx,...
                 'HorizontalAlign','left',...
                 'visible','off',...
                 'string',{' ',txt{:},' ',' ','Copyright:',...
                 'Nayanjyoti Pathak 2010'},...
                 'fontsize',16,'fontweight','normal');

% Set properties for the slider
S.tx1(1) = uicontrol('style','text',...
                    'unit','pix',...
                    'position',Dim.tx1{1},...
                    'fontsize',12,...
                    'string','1');   % Displays the min.          
S.tx1(2) = uicontrol('style','text',...
                    'unit','pix',...
                    'position',Dim.tx1{2},...
                    'fontsize',16,...
                    'string',num2str(round(maxSlice/2)));  % Displays the value.
S.tx1(3) = uicontrol('style','text',...
                    'unit','pix',...
                    'position',Dim.tx1{3},...
                    'fontsize',12,...
                    'string',num2str(maxSlice));    % Displays the max.  

% Create interface to supply buffer amounts
S.tx2(1)     = uicontrol('style', 'text',...
                    'unit','pix',...
                    'position',Dim.tx2{1},...
                    'fontsize',12,...
                    'visible','on',...
                    'string', 'xy-buffer');
S.tx2(2)     = uicontrol('style', 'text', ...
                    'unit','pix',...
                    'position',Dim.tx2{2},...
                    'fontsize',12,...
                    'string', 'z-buffer');                             
S.ed2(1)     = uicontrol('style', 'edit',...
                    'unit','pix',...
                    'position',Dim.ed2{1},...
                    'fontsize',12,...
                    'visible','on',...
                    'BackgroundColor','white',...
                    'string', '1');
S.ed2(2)     = uicontrol('style', 'edit', ...
                    'unit','pix',...
                    'position',Dim.ed2{2},...
                    'fontsize',12,...
                    'BackgroundColor','white',...
                    'string', '3');              
                
 %-----------------------------------------
function Dim  = getDimensions(r, c, SCR)
% Set the positions and Dimensions for each uicontrol

marginWidth     = 22;
separatorWidth  = 20;
toggleButtonWidth = 100;
toggleButtonHeight = 40;
numTB = 5;

if (r<256)
    r = r*1.5;
end

if (c<256)
    c = c*1.5;
end

Dim.fh    = [SCR(3)/2-200 ,SCR(4)/2-200, c*3+300, r*1.5+250];
Dim.ax{1} = [175 110 c*1.5 r*1.5];
Dim.ax{2} = [175+c*1.5+separatorWidth 110 c*1.5 r*1.5];
Dim.ph{1} = [20    r*1.5-166 104 toggleButtonHeight*numTB+(numTB-1)*2];
Dim.ph{2} = [200   10        592 66];
Dim.sl    = [246   12        350 25];
Dim.tg{1} = [marginWidth    r*1.5     toggleButtonWidth toggleButtonHeight];
Dim.tg{2} = [marginWidth    r*1.5-41  toggleButtonWidth toggleButtonHeight];
Dim.tg{4} = [marginWidth    r*1.5-82  toggleButtonWidth toggleButtonHeight];
Dim.tg{5} = [marginWidth    r*1.5-123 toggleButtonWidth toggleButtonHeight];
Dim.tg{6} = [marginWidth    r*1.5-164 toggleButtonWidth toggleButtonHeight];
Dim.tg{3} = [marginWidth    5         toggleButtonWidth toggleButtonHeight];

Dim.tx     = [150 100 c*3 r*1.5];
Dim.tx1{1} = [202 12 40 25];
Dim.tx1{2} = [401 39 40 25];
Dim.tx1{3} = [598 12 40 25];
Dim.tx2{1} = [675   39  80 25];
Dim.tx2{2} = [675   12  80 25];
Dim.ed2{1} = [765   39  25 25];
Dim.ed2{2} = [765   12  25 25];


                