function varargout = EDgui(varargin)
% EDGUI Application M-file for EDgui.fig
%    FIG = EDGUI launch EDgui GUI.
%    EDGUI('callback_name', ...) invoke the named callback.

% Last Modified by GUIDE v2.0 30-Oct-2002 12:40:00

if nargin == 0  % LAUNCH GUI

	fig = openfig(mfilename,'reuse');
    set(fig, 'Colormap', gray(256));

	% Generate a structure of handles to pass to callbacks, and store it. 
	handles = guihandles(fig);
	guidata(fig, handles);
    set(get(handles.ImageAxes, 'title'), 'string', 'Original Image');
    set(get(handles.EdgeAxes, 'title'), 'string', 'Edge Map');
    %================================
    % Original Image 
        handles.Image = image('Interruptible' , 'off',...
        'BusyAction', 'queue', ...
        'CData', [], ...
        'CDataMapping', 'scaled', ...
        'Parent',handles.ImageAxes,...
        'Xdata', [1 256],...
        'Ydata', [1 256],...
        'EraseMode', 'none');
%     EDgui('pushbutton2_Callback',gcbo,[],guidata(gcbo))
%     EDgui('pushbutton1_Callback',gcbo,[],guidata(gcbo))

    %================================
    % Edge Map Image
        handles.Edge = image('Interruptible' , 'off',...
        'BusyAction', 'queue', ...
        'CData', [], ...
        'CDataMapping', 'scaled', ...
        'Parent',handles.EdgeAxes,...
        'Xdata', [1 256],...
        'Ydata', [1 256],...
        'EraseMode', 'none');
    
    %====================================
    % The LoadNewImage menu : ip-> Image Popup
    
        handles.ImgPop=uicontrol('Interruptible' , 'off',...
        'BusyAction', 'queue', ...
        'Parent', fig, ...
        'Style','popupmenu', ...
        'Units','pixels', ...
        'Position',[92 68 149 21], ...
        'Enable','on', ...
        'String','Blood|Aluminum|Rice|Saturn', ...
        'Tag','ImagesPop',...
        'Callback','edgui(''LoadNewImage'')');
    
    %====================================
    % The Method menu : mp-> Method Popup
        handles.Method = 'Sobel';
        handles.MethodPop=uicontrol('Interruptible' , 'off',...
        'BusyAction', 'queue', ...
        'Parent', fig, ...
        'Style','popupmenu', ...
        'Units','pixels', ...
        'Position',[91 27 149 21], ...
        'Enable','on', ...
        'String','Sobel|Prewitt|Roberts|Kirsch|Robinson|Laplacian', ...
        'Tag','MethodPop',...
        'Callback','edgui(''SelectMethod'')');
    
    %====================================
    % The Directionality Popup menu : dp-> Direction Popup
        handles.Directionality = 'both';
        handles.sprDirPop=uicontrol('Interruptible' , 'off',...
        'BusyAction', 'queue', ...
        'Parent', fig, ...
        'Style','popupmenu', ...
        'Units','pixels', ...
        'Position',[291 27 149 21], ...
        'Enable','on', ...
        'String','Both|Horizontal|Vertical', ...
        'Tag','DirectionPop',...
        'Callback','edgui(''UpdateDirectionality'')');
    
    %====================================
    % The Directionality Popup menu : dp-> Roberts Option Popup
        handles.Roption = 'Absolute';
        handles.robopPop=uicontrol('Interruptible' , 'off',...
        'BusyAction', 'queue', ...
        'Parent', fig, ...
        'Style','popupmenu', ...
        'Units','pixels', ...
        'Position',[292 68 149 21], ...
        'Enable','on', ...
        'String','Absolute|Square', ...
        'Tag','RobertsPop',...
        'Callback','edgui(''UpdateRoberts'')');
    
    %====================================
    % The APPLY button

        handles.Apply=uicontrol('Interruptible' , 'off',...
        'BusyAction', 'queue', ...
        'Parent', fig, ...
        'Style','pushbutton', ...
        'Units','pixels', ...
        'Position',[469 71 90 25], ...
        'Enable','off', ...
        'String','Apply', ...
        'Callback','edgui(''ComputeEdgeMap'')');
    
    set(fig, 'Userdata', handles, 'Visible', 'on');
    drawnow
    LoadNewImage(fig);
    drawnow
    set(fig, 'HandleVisibility', 'Callback');
    set([handles.Apply handles.Close] , 'Enable', 'on');

	if nargout > 0
		varargout{1} = fig;
	end

elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK

	try
		[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
	catch
		disp(lasterr);
	end

end


%| ABOUT CALLBACKS:
%| GUIDE automatically appends subfunction prototypes to this file, and 
%| sets objects' callback properties to call them through the FEVAL 
%| switchyard above. This comment describes that mechanism.
%|
%| Each callback subfunction declaration has the following form:
%| <SUBFUNCTION_NAME>(H, EVENTDATA, HANDLES, VARARGIN)
%|
%| The subfunction name is composed using the object's Tag and the 
%| callback type separated by '_', e.g. 'slider2_Callback',
%| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'.
%|
%| H is the callback object's handle (obtained using GCBO).
%|
%| EVENTDATA is empty, but reserved for future use.
%|
%| HANDLES is a structure containing handles of components in GUI using
%| tags as fieldnames, e.g. handles.figure1, handles.slider2. This
%| structure is created at GUI startup using GUIHANDLES and stored in
%| the figure's application data using GUIDATA. A copy of the structure
%| is passed to each callback.  You can store additional information in
%| this structure at GUI startup, and you can change the structure
%| during callbacks.  Call guidata(h, handles) after changing your
%| copy to replace the stored original so that subsequent callbacks see
%| the updates. Type "help guihandles" and "help guidata" for more
%| information.
%|
%| VARARGIN contains any extra arguments you have passed to the
%| callback. Specify the extra arguments by editing the callback
%| property in the inspector. By default, GUIDE sets the property to:
%| <MFILENAME>('<SUBFUNCTION_NAME>', gcbo, [], guidata(gcbo))
%| Add any extra arguments after the last argument, before the final
%| closing parenthesis.



% --------------------------------------------------------------------
function varargout = popupmenu1_Callback(h, eventdata, handles, varargin)
% Stub for Callback of the uicontrol handles.popupmenu1.
disp('popupmenu1 Callback not implemented yet.')


% --------------------------------------------------------------------
function varargout = popupmenu3_Callback(h, eventdata, handles, varargin)
% Stub for Callback of the uicontrol handles.popupmenu3.
disp('popupmenu3 Callback not implemented yet.')


% --------------------------------------------------------------------
function varargout = popupmenu4_Callback(h, eventdata, handles, varargin)
% Stub for Callback of the uicontrol handles.popupmenu4.
disp('popupmenu4 Callback not implemented yet.')


% --------------------------------------------------------------------
function varargout = popupmenu5_Callback(h, eventdata, handles, varargin)
% Stub for Callback of the uicontrol handles.popupmenu5.
disp('popupmenu5 Callback not implemented yet.')


% --------------------------------------------------------------------
function varargout = pushbutton1_Callback(h, eventdata, handles, varargin)
% Stub for Callback of the uicontrol handles.pushbutton1.
disp('pushbutton1 Callback not implemented yet.')


% --------------------------------------------------------------------
function varargout = pushbutton2_Callback(h, eventdata, handles, varargin)
% Stub for Callback of the uicontrol handles.pushbutton2.
disp('pushbutton2 Callback not implemented yet.')


%%%
%%%  Sub-Function - LoadNewImage
%%%

function LoadNewImage(DemoFig)

if nargin<1
   callb = 1;    % We're in a callback
   DemoFig = gcbf;
else 
   callb = 0;    % We're in the initialization
end

set(DemoFig,'Pointer','watch');
% hdl = guihandles(DemoFig);
hdl=get(DemoFig,'Userdata');
v = get(hdl.ImgPop,{'value','String'});
name = deblank(v{2}(v{1},:));

switch name
case 'Aluminum',
   img = imread('alumgrns.tif');
case 'Blood',                 
   img = imread('blood1.tif');
case 'Rice',
   img = imread('rice.tif');
case 'Saturn',
   img = imread('saturn.tif'); 
otherwise 
   error('EDGEDETECTION: Unknown Image Option!');
end

hdl.img = img;

set(hdl.Image, 'Cdata', img);
set(get(hdl.ImageAxes,'title'),'string',['Original ' name ' Image']);
set(DemoFig,'Pointer','arrow');
if callb
   set(hdl.Apply, 'Enable', 'on');
end
drawnow
ComputeEdgeMap(DemoFig);
return;


%%%
%%%  Sub-Function - ComputeEdgeMap
%%%

function ComputeEdgeMap(DemoFig)

if nargin<1
   callb = 1;    % We're in a callback
   DemoFig = gcbf;
else 
   callb = 0;    % We're in the initialization
end

set(DemoFig,'Pointer','watch');
hdl=get(DemoFig,'Userdata');

v = get(hdl.ImgPop,{'value','String'});
name = deblank(v{2}(v{1},:));

switch name
case 'Aluminum',
   img = imread('alumgrns.tif');
case 'Blood',                 
   img = imread('blood1.tif');
case 'Rice',
   img = imread('rice.tif');
case 'Saturn',
   img = imread('saturn.tif'); 
otherwise 
   error('EDGEDETECTION: Unknown Image Option!');
end

% img = hdl.img;
% img = getimage(hdl.Image);

switch hdl.Method
case {'Sobel','Prewitt'}
      edgemap = edgedetector(img, hdl.Method, hdl.Directionality);
case {'Kirsch','Robinson'}
      edgemap = edgedetector(img, hdl.Method);
case 'Laplacian'
      edgemap = edgedetector(img, hdl.Method);
case 'Roberts'
      edgemap = edgedetector(img, hdl.Roption);
otherwise
   error('EDGEDEMO: Invalid edge detection method.');
end

set(hdl.Edge, 'CData', edgemap);
set(hdl.Apply, 'Enable', 'off');
set(DemoFig,'Pointer','arrow');
drawnow

%%%
%%%  Sub-Function - SelectMethod
%%%

function SelectMethod

DemoFig = gcbf;

hdl = get(DemoFig, 'userdata');
v = get(hdl.MethodPop,{'value','String'});
hdl.Method = deblank(v{2}(v{1},:));
switch hdl.Method
case {'Sobel','Prewitt'}
   set(hdl.sprDirPop, 'Visible', 'on');
   set(hdl.robopPop, 'Visible', 'off');
   set(hdl.sprDirPop, 'Enable', 'on');
case {'Laplacian','Kirsch','Robinson'}
   set(hdl.sprDirPop, 'Visible', 'off');
   set(hdl.robopPop, 'Visible', 'off');
case 'Roberts'
   set(hdl.sprDirPop, 'Visible', 'off');
   set(hdl.robopPop, 'Visible', 'on');
   set(hdl.robopPop, 'Enable', 'on');
otherwise
   error('EDGEDEMO: invalid method specifier.');
end
   
set(hdl.Apply, 'Enable', 'on');
set(DemoFig, 'userdata', hdl);

%%%
%%%  Sub-function - UpdateDirectionality
%%%

function UpdateDirectionality()

DemoFig = gcbf;
hdl = get(DemoFig, 'UserData');
v = get(hdl.sprDirPop,{'value','String'});
dir = deblank(v{2}(v{1},:));
set(hdl.sprDirPop, 'userdata', dir);
hdl.Directionality = dir;
set(hdl.Apply, 'Enable', 'on');
set(DemoFig, 'UserData', hdl);
return

%%%
%%%  Sub-function - UpdateRoberts
%%%

function UpdateRoberts()

DemoFig = gcbf;
hdl = get(DemoFig, 'UserData');
v = get(hdl.robopPop,{'value','String'});
opt = deblank(v{2}(v{1},:));
set(hdl.robopPop, 'userdata', dir);
hdl.Roption =opt;
set(hdl.Apply, 'Enable', 'on');
set(DemoFig, 'UserData', hdl);
return
