function varargout = Aufgabe2(varargin)
% Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @Aufgabe2_OpeningFcn, ...
                       'gui_OutputFcn',  @Aufgabe2_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 Aufgabe2 is made visible.
function Aufgabe2_OpeningFcn(hObject, eventdata, handles, varargin)

    handles.A1 = imread( 'katze.bmp' );
    handles.A2 = handles.A1;
    
    handles.R = 1;
    handles.G = 1;
    handles.B = 1;
    
    set( handles.slider1, 'Value', handles.R );
    set( handles.slider2, 'Value', handles.G );
    set( handles.slider3, 'Value', handles.B ); 
    
    set( gcf, 'CurrentAxes', handles.axes1 );
    image( handles.A1 );
    axis image;
    
    set( gcf, 'CurrentAxes', handles.axes2 );
    image( handles.A2 );
    axis image;
    
    % Choose default command line output for Aufgabe2
    handles.output = hObject;

    % Update handles structure
    guidata(hObject, handles);


% --- Outputs from this function are returned to the command line.
function varargout = Aufgabe2_OutputFcn(hObject, eventdata, handles) 
    % Get default command line output from handles structure
    varargout{1} = handles.output;


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
    setSlider( 1, hObject, handles );
   

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
    if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor',[.9 .9 .9]);
    end


% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
    setSlider( 2, hObject, handles );

% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
    if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor',[.9 .9 .9]);
    end


% --- Executes on slider movement.
function slider3_Callback(hObject, eventdata, handles)
    setSlider( 3, hObject, handles );

% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
    if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor',[.9 .9 .9]);
    end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
    % in double wandeln, da bei der Addition Werte > 255 auftreten können 
    D = double( handles.A1 );
    
    GrauWert(:,:) = uint8( ( D(:,:,1) + D(:,:,2) + D(:,:,3) ) / 3.0 );
    Grau( :,:,1 ) = GrauWert(:,:);
    Grau( :,:,2 ) = GrauWert(:,:);
    Grau( :,:,3 ) = GrauWert(:,:);
         
    set( gcf, 'CurrentAxes', handles.axes2 );
    image( Grau );
    axis image; 
    
    % den Grauwert nicht merken !!!
   

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
  
    set( gcf, 'CurrentAxes', handles.axes1 ); 
    
    [x1,y1] = ginput(1);
    [x2,y2] = ginput(1);
    
    if( x1 < x2 )
        xmin = int32(x1);
        xmax = int32(x2);
    else
        xmin = int32(x2);
        xmax = int32(x1);
    end
    
    if( y1 < y2 )
        ymin = int32(y1);
        ymax = int32(y2);
    else
        ymin = int32(y2);
        ymax = int32(y1);
    end
    
    A2 = handles.A1( ymin:ymax, xmin:xmax, : );
     
    set( gcf, 'CurrentAxes', handles.axes2 ); 
    image( A2 );
    axis image; 
   


function setSlider( nr, hObject, handles )
    val = get(hObject,'Value');
    handles.A2(:,:,nr) = uint8( handles.A1(:,:,nr) * val ); 
    guidata(hObject, handles);
    
    set( gcf, 'CurrentAxes', handles.axes2 );
    image( handles.A2 );
    axis image;

