function varargout = slider02(varargin)
% SLIDER02 M-file for slider02.fig
%      SLIDER02, by itself, creates a new SLIDER02 or raises the existing
%      singleton*.
%
%      H = SLIDER02 returns the handle to a new SLIDER02 or the handle to
%      the existing singleton*.
%
%      SLIDER02('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SLIDER02.M with the given input arguments.
%
%      SLIDER02('Property','Value',...) creates a new SLIDER02 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before slider02_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to slider02_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help slider02

% Last Modified by GUIDE v2.5 15-Jun-2009 13:51:58

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @slider02_OpeningFcn, ...
                   'gui_OutputFcn',  @slider02_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(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 slider02 is made visible.
function slider02_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 slider02 (see VARARGIN)

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

handles.vid = videoinput('winvideo',1,'YUY2_320x240');



% Update handles structure
guidata(hObject, handles);


% global vid
% axes(handles.bild2)
% mov=aviread('test2.avi');                      %Film abspielen
% movie(mov);

% vid = videoinput('winvideo',1,'YUY2_320x240');  %Preview abspielen
% set(vid, 'returnedColorSpace', 'rgb')

% vidRes = get(vid, 'VideoResolution');
% nBands = get(vid, 'NumberOfBands');
% axes(handles.bild2);
% hImage = image(zeros(vidRes(2), vidRes(1), nBands) );
% preview(vid, hImage);


% UIWAIT makes slider02 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = slider02_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;


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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

%obtains the slider value from the slider component
sliderValue = get(handles.slider1,'Value');
sliderValue = floor(sliderValue); 
%puts the slider value into the edit text component
set(handles.slider_editText,'String', num2str(sliderValue));  %fuer Sliderausgabe
 
% Update handles structure
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (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.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



function slider_editText_Callback(hObject, eventdata, handles)
% hObject    handle to slider_editText (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,'String') returns contents of slider_editText as text
%        str2double(get(hObject,'String')) returns contents of slider_editText as a double



%get the string for the editText component
sliderValue = get(handles.slider_editText,'String');
 
%convert from string to number if possible, otherwise returns empty
sliderValue = floor(str2num(sliderValue));                                                          %rundet Eingabe auf ein ganzzahliges Ergebnis ab
set(handles.slider_editText,'String',num2str(sliderValue));                                         %stellt sicher dass nur ganzzahlige Frames eingegeben werden
 
%if user inputs something is not a number, or if the input is less than 0
%or greater than 100, then the slider value defaults to 0
if (isempty(sliderValue) || sliderValue < 0 || sliderValue > 100 || sliderValue == NaN)
    set(handles.slider1,'Value',0);
    set(handles.slider_editText,'String','0');
else
    set(handles.slider1,'Value',sliderValue);
end


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

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


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


axes(handles.bild2)
handles.mov=aviread('test2.avi');                      %Film abspielen
handles.movinfo=aviinfo('test2.avi');
stoppreview(handles.vid);  %damit es nicht flackert... Grund fuer Flackern bei Gleichzeitig abspielen unbekannt

%%%  frameStart und frameEnd String

frameEnd = handles.movinfo.NumFrames;
set(handles.frameEnd,'String',frameEnd);
set(handles.slider1,'Max',frameEnd);
set(handles.frameStart,'String',0);

%%% fps herausfinden

fps = handles.movinfo.FramesPerSecond;
set(handles.fps,'String',fps);

%%%%%%%%%%% Film abspielen mit synchronen Slider
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% mov mit passenden fps 1x abspielen

movie(handles.mov,1,fps);
% currentFrame = Getframe();
% set(handles.slider_editText,'String',currentFrame);

%movie(mov);

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

% global vid;
% vid = videoinput('winvideo',1,'YUY2_320x240');  %Preview abspielen
set(handles.vid, 'returnedColorSpace', 'rgb')
vidRes = get(handles.vid, 'VideoResolution');
nBands = get(handles.vid, 'NumberOfBands');
axes(handles.bild1)
hImage = image(zeros(vidRes(2), vidRes(1), nBands) );
preview(handles.vid, hImage);


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

%global vid;
stoppreview(handles.vid);


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

closepreview(handles.vid);
cla;






%%%%Frame Readout
% function y = GetFrame(handles)
% 
%     y=handles.mov.cdata(:,:,:,handles.currentframe)
    
