function varargout = perturb(varargin)
% PERTURB M-file for perturb.fig
%      PERTURB, by itself, creates a new PERTURB or raises the existing
%      singleton*.
%
%      H = PERTURB returns the handle to a new PERTURB or the handle to
%      the existing singleton*.
%
%      PERTURB('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named PERTURB in PERTURB.M with the given input arguments.
%
%      PERTURB('Property','Value',...) creates a new PERTURB or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before perturb_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to perturb_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% Code construction assisted by GUIDE
%
%    GENERAL
%
%      Omitted.
%
%    INPUT/S
%
%      -
%          
%           
%    OUTPUT/S
%
%      -
%
%    PENDING WORK
%
%      -
%
%    KNOWN BUG/S
%
%      -
%
%    COMMENT/S
%
%      -
%
%    RELATED FUNCTION/S
%
%      
%
%    ABOUT
%
%      -Created:     February 12th, 2004
%      -Last update: 
%      -Revision:    0.0.9
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

% Edit the above text to modify the response to help_button register

% Last Modified by GUIDE v2.5 11-Mar-2004 15:26:19

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @perturb_OpeningFcn, ...
    'gui_OutputFcn',  @perturb_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before register is made visible.
function perturb_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to register (see VARARGIN)

% Choose default command line output for register
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

if strcmp(get(hObject,'Visible'),'off')
    initialize_gui(hObject, handles);
end

% UIWAIT makes register pause for user response (see UIRESUME)
% uiwait(handles.Advanced_window);

% ==================================================================== %  

% --- Outputs from this function are returned to the command line.
function varargout = perturb_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% ==================================================================== %  

function initialize_gui(fig_handle, handles)

set(handles.offset, 'String', getappdata(0, 'perturbation_offset'));
set(handles.number_of_perturbations_value, 'String', getappdata(0, 'number_of_perturbations'));

set(handles.offset_slider, 'Value', str2num(get(handles.offset, 'String')));
set(handles.number_of_perturbations_slider, 'Value', str2num(get(handles.number_of_perturbations_value, 'String')));


set(handles.perturbation_method, 'String', getappdata(0, 'perturbation_method'));




background = imread('background.jpg');

axes(handles.background);
axis off;
imshow(background,[0,255]);

% ==================================================================== %  

% --- Executes on button press in ok_botton.
function ok_botton_Callback(hObject, eventdata, handles)
% hObject    handle to ok_botton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


setappdata(0, 'perturbation_method', get(handles.perturbation_method, 'String'));
setappdata(0, 'perturbation_offset', get(handles.offset, 'String'));
setappdata(0, 'number_of_perturbations', get(handles.number_of_perturbations_value, 'String'));


close;

% ==================================================================== %  

% --- Executes on button press in cancel_button.
function cancel_button_Callback(hObject, eventdata, handles)
% hObject    handle to cancel_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close;

% ==================================================================== %  

% --- Executes on button press in apply_button.
function apply_button_Callback(hObject, eventdata, handles)
% hObject    handle to apply_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
setappdata(0, 'perturbation_method', get(handles.perturbation_method, 'String'));
setappdata(0, 'perturbation_offset', get(handles.offset, 'String'));
setappdata(0, 'number_of_perturbations', get(handles.number_of_perturbations_value, 'String'));

% ==================================================================== %  

% --- Executes during object creation, after setting all properties.
function perturbation_method_menu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to perturbation_method_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% ==================================================================== %  

% --- Executes on selection change in perturbation_method_menu.
function perturbation_method_menu_Callback(hObject, eventdata, handles)
% hObject    handle to perturbation_method_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns perturbation_method_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from perturbation_method_menu
contents = get(hObject,'String');

if (strcmp(contents{get(hObject,'Value')},'CPS Warps')),
       set(handles.perturbation_method, 'String', 'CPS warp');  
elseif (strcmp(contents{get(hObject,'Value')},'Random Noise')),
       set(handles.perturbation_method, 'String', 'random noise');     
else
    error('Wrong values passed from perturbation method menu. Internal error.');
end

% ==================================================================== %  

% --- Executes during object creation, after setting all properties.
function offset_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to offset_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% ==================================================================== %  

% --- Executes during object creation, after setting all properties.
function number_of_perturbations_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to offset_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% ==================================================================== %  

% --- Executes on slider movement.
function offset_slider_Callback(hObject, eventdata, handles)
% hObject    handle to offset_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

set(handles.offset, 'String', num2str(ceil(get(handles.offset_slider, 'Value'))));

% ==================================================================== %  

% --- Executes on slider movement.
function number_of_perturbations_slider_Callback(hObject, eventdata, handles)
% hObject    handle to offset_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

set(handles.number_of_perturbations_value, 'String', num2str(ceil(get(handles.number_of_perturbations_slider, 'Value'))));

% ==================================================================== % 

% --- Executes on button press in disable_perturbation.
function disable_perturbation_Callback(hObject, eventdata, handles)
% hObject    handle to disable_perturbation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(handles.offset, 'String', '0');
set(handles.offset_slider, 'Value', 0);

