function varargout = patch_gui(varargin)

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

set(findobj('Tag','klic1'),'Value', 1);
set(findobj('Tag','edit_hladina'),'String',7);


% 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 patch_gui (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


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

%*******************************************************************
%                         Vlastni kod
%*******************************************************************

function otevri_Callback(hObject, eventdata, handles)

[file,cesta] = uigetfile('*.bmp', 'Nacteni obrazu pro vodoznaceni');        % *.* nacteni vsech souboru *.bmp
  if file == 0
      warning_2;
  else
      nacteny_obraz = (imread(fullfile(cesta,file)));                                  % info 0 - 255 o kazdem bodu obrazu  
      obraz_info = imfinfo(fullfile(cesta,file),'bmp');                                       % kompletni informace o obrazu 

      set(findobj('Tag','text_jmeno'),'String', file);                                 % jmeno .... atd info o souboru
      set(findobj('Tag','text_sirka'),'String', obraz_info.Width);                   
      set(findobj('Tag','text_vyska'),'String', obraz_info.Height);                  
      set(findobj('Tag','text_velikost'),'String', obraz_info.FileSize);             
      set(findobj('Tag','text_vytvoreno'),'String', obraz_info.FileModDate);         
      set(findobj('Tag','text_pocet'),'String', obraz_info.Width*obraz_info.Height);             
      
      set(findobj('Tag','help'),'String', fullfile(cesta,file));                     % zaloha cesty
      
      axes(handles.zobraz_obraz);
      imshow(nacteny_obraz);                                                         % zobrazeni nacteneho obrazku.
      
      set(findobj('Tag','vloz'),'Enable','on');                        % az po nacteni obrazu smime provadet upravy !
      set(findobj('Tag','rozklad'),'Enable','on');
  end

% --------------------------------------------------------------------
function konec_prace_Callback(hObject, eventdata, handles)
close;


% --- 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


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


% --- Executes during object creation, after setting all properties.
function edit_hladina_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_hladina (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 selection change in listbox.
function listbox_Callback(hObject, eventdata, handles)
% hObject    handle to listbox (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 listbox contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox
hladina = get(findobj('Tag','listbox'),'Value');
set(findobj('Tag','edit_hladina'),'String', 8-hladina );


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

% Hint: listbox 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 konec.
function konec_Callback(hObject, eventdata, handles)
% hObject    handle to konec (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 key press over konec with no controls selected.
function konec_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to konec (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in vloz.
function varargout = vloz_Callback(hObject, eventdata, handles)

hladina = str2num(get(findobj('Tag','edit_hladina'),'String'));

if get(findobj('Tag','klic1'),'Value') == 1                         % vyber daneho klice
    klic = 1;
elseif get(findobj('Tag','klic2'),'Value') == 1                     % prvni 3 jsou nahodne vybrane
    klic = 2;
elseif get(findobj('Tag','klic3'),'Value') == 1
    klic = 3;
elseif get(findobj('Tag','klic4'),'Value') == 1                     % 4 je vlozeni nacteneho obrazu
    klic = 4;    
end

save klic                                                           % ulozeni pro pouziti pri dekodovani v checkmarku

%************ Aby uzivatel nemohl dale menit nastaveni
set(findobj('Tag','klic1'),'Enable','off');
set(findobj('Tag','klic2'),'Enable','off');
set(findobj('Tag','klic3'),'Enable','off');
set(findobj('Tag','klic4'),'Enable','off');
set(findobj('Tag','listbox'),'Enable','off');


%               Volani funkce pro vlozeni vodoznaku
%*******************************************************************

[PSNR, OH, WO, HU, S1]=patchwork(get(findobj('Tag','help'),'String'),8-hladina,klic,get(findobj('Tag','check'),'Value'));

  set(findobj('Tag','psnr_vlozeno'),'String', PSNR);

  axes(handles.axes_vystup);
  imshow(uint8(OH.*255));
  title('Obraz z vybrane hladiny','Fontsize',10,'color','red');
   
  axes(handles.axes5);
  imshow(uint8(HU.*255));
  title('Upravena hladina klicem','Fontsize',10,'color','red');
   
  axes(handles.axes6);  
  imshow(uint8(WO));
  title('Vodoznaceny obraz','Fontsize',10,'color','red'); 
  
  set(findobj('Tag','dekoduj'),'Enable','on');                % uvolneni tlacitka
  set(findobj('Tag','vloz'),'Enable','off');                  % zakaz tlacitka
  set(findobj('Tag','utok'),'Enable','on');                   % uvolneni tlacitka
     
  
% --- Executes on button press in klic1.
function klic1_Callback(hObject, eventdata, handles)

set(findobj('Tag','klic2'),'Value', 0);                     % lze zadat jen jeden klic
set(findobj('Tag','klic3'),'Value', 0);
set(findobj('Tag','klic4'),'Value', 0);

% --- Executes on button press in klic2.
function klic2_Callback(hObject, eventdata, handles)

set(findobj('Tag','klic1'),'Value', 0);                     % lze zadat jen jeden klic
set(findobj('Tag','klic3'),'Value', 0);
set(findobj('Tag','klic4'),'Value', 0);

% --- Executes on button press in klic3.
function klic3_Callback(hObject, eventdata, handles)

set(findobj('Tag','klic1'),'Value', 0);
set(findobj('Tag','klic2'),'Value', 0);                     % lze zadat jen jeden klic
set(findobj('Tag','klic4'),'Value', 0);


function psnr_vlozeno_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function psnr_vlozeno_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function vytazeno_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function ncc_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in rozklad.
function rozklad_Callback(hObject, eventdata, handles)
   
bitove_hladiny(get(findobj('Tag','help'),'String'))


% --- Executes on button press in check.
function check_Callback(hObject, eventdata, handles)



function vytazeno_Callback(hObject, eventdata, handles)


function ncc_Callback(hObject, eventdata, handles)



% <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Dekodovani >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
% --- Executes on button press in dekoduj.
function dekoduj_Callback(hObject, eventdata, handles)
 
load puvodni_obraz.mat
load W0.mat 
load S1.mat

hladina = str2num(get(findobj('Tag','edit_hladina'),'String'));

if get(findobj('Tag','klic1'),'Value') == 1                         % vyber daneho klice
    klic = 1;
elseif get(findobj('Tag','klic2'),'Value') == 1
    klic = 2;
elseif get(findobj('Tag','klic3'),'Value') == 1
    klic = 3;
elseif get(findobj('Tag','klic4'),'Value') == 1
    klic = 4;    
end

[hlad_po, hlad_vodoz, hlad_uprav,w, NCC]=Patchwork_dekoder(puvodni_obraz ,WO, 8-hladina,S1, klic,get(findobj('Tag','check'),'Value'));

  axes(handles.axes_vystup);
  imshow(uint8(hlad_po.*255));
  title('Obraz z puvodni hladiny','Fontsize',10,'color','red');
   
  axes(handles.axes5);
  imshow(uint8(hlad_vodoz.*255));
  title('Obraz z vodoznacene hladiny','Fontsize',10,'color','red');
  
  axes(handles.axes6);
  imshow(uint8(hlad_uprav.*255));
  title('Nalezeny obraz po utoku','Fontsize',10,'color','red');
   
  set(findobj('Tag','vytazeno'),'String', w);
  set(findobj('Tag','ncc'),'String', NCC);

%************ Aby uzivatel mohl dale menit nastaveni
set(findobj('Tag','klic1'),'Enable','on');
set(findobj('Tag','klic2'),'Enable','on');
set(findobj('Tag','klic3'),'Enable','on');
set(findobj('Tag','klic4'),'Enable','on');
set(findobj('Tag','listbox'),'Enable','on');
set(findobj('Tag','vloz'),'Enable','on');                      % uvolneni tlacitka

%     Pokud klikneme na 4 vodoznak musime nacist obraz
% *********************************************************
function klic4_Callback(hObject, eventdata, handles)
  set(findobj('Tag','klic1'),'Value', 0);                     % volba pouze jednoho klice do funkce
  set(findobj('Tag','klic2'),'Value', 0);
  set(findobj('Tag','klic3'),'Value', 0);  
  set(findobj('Tag','klic4'),'Value', 1);
  
[file1,cesta1] = uigetfile('*.bmp', 'Nacteni vodoznaku');                    % *.* nacteni souboru *.bmp 
  if file1 == 0
      warning_2;
      set(findobj('Tag','klic4'),'Value', 0);                                % spravnost nastaveni klice
  else
      Vodoznak =(imread(fullfile(cesta1,file1)));                            % nacteni 0 - 255 obraz
      [vy,vx] = size(Vodoznak);                                              % velikost vodoznaku   
      
      if (vy > str2num(get(findobj('Tag','text_sirka'),'String'))) || (vx > str2num(get(findobj('Tag','text_vyska'),'String')))
          warning_1;
          klic4_Callback(hObject, eventdata, handles);
      else
          save Vodoznak                                                          % zaloha nacteneho vodoznaku
          axes(handles.axes_vodoznak);                                           % nalezeni daneho prvku podle handles
          imshow(Vodoznak);                                                      % zobrazeni nacteneho vodoznaku
      end
  end


% --- Executes on button press in utok.
function utok_Callback(hObject, eventdata, handles)

%zavolame si funkci checkmarku a provedeme utoky.

cd('c:\metoda_1\Checkmark\');

runCheckmark;                   % provedem utoky podle copyright

cd('c:\metoda_1\');

set(findobj('Tag','komplet'),'enable', 'on');
set(findobj('Tag','dekoduj'),'enable', 'on');
  

% --- Executes on button press in komplet.
function komplet_Callback(hObject, eventdata, handles)

%zavolame si funkci checkmarku a provedeme kontrolu.

%************ Aby uzivatel mohl dale menit nastaveni
set(findobj('Tag','klic1'),'Enable','on');
set(findobj('Tag','klic2'),'Enable','on');
set(findobj('Tag','klic3'),'Enable','on');
set(findobj('Tag','klic4'),'Enable','on');
set(findobj('Tag','listbox'),'Enable','on');
set(findobj('Tag','vloz'),'Enable','on');                        % uvolneni tlacitka
set(findobj('Tag','parser'),'Enable','on');                      % uvolneni tlacitka

cd('c:\metoda_1\Checkmark\');

testDetector;                   % provedem utoky podle copyright

cd('c:\metoda_1\');

zobraz_vysledky_checkmark


% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject    handle to listbox2 (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 listbox2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox2


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

% Hint: listbox 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

% --------------------------------------------------------------------
function popis_programu_Callback(hObject, eventdata, handles)
% hObject    handle to popis_programu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

    popis_programu      % help



% --- Executes on button press in parser.
function parser_Callback(hObject, eventdata, handles)

cd('c:\metoda_1\Checkmark\');

parseresults;                   % provedem rozparsovani

cd('c:\metoda_1\');
 
