function varargout = RHC_gui(varargin)
% RHC_GUI M-file for RHC_gui.fig
%      RHC_GUI, by itself, creates a new RHC_GUI or raises the existing
%      singleton*.
%
%      H = RHC_GUI returns the handle to a new RHC_GUI or the handle to
%      the existing singleton*.
%
%      RHC_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in RHC_GUI.M with the given input arguments.
%
%      RHC_GUI('Property','Value',...) creates a new RHC_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before RHC_gui_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to RHC_gui_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 RHC_gui

% Last Modified by GUIDE v2.5 09-Mar-2009 15:28:46

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

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes RHC_gui wait for user response (see UIRESUME)
% uiwait(handles.hlavni);


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

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


% --------------------------------------------------------------------
function otevri_Callback(hObject, eventdata, handles)

[file,cesta] = uigetfile('*.jpg', 'Nacteni obrazu pro vodoznaceni');                % *.* nacteni vsech souboru *.bmp
  if file == 0
      warning_2;
      return;
  else
      nacteny_obraz = (imread(fullfile(cesta,file)));                               % info 0 - 255 o kazdem bodu obrazu  
      obraz_info = imfinfo(fullfile(cesta,file),'jpg');                             % kompletni informace o obrazu 
  end  
  if mod(obraz_info.Width,8) == 0 && mod(obraz_info.Height,8) == 0
       set(findobj('Tag','jmeno'),'String', file);                                  % jmeno .... atd info o souboru
       set(findobj('Tag','sirka'),'String', obraz_info.Width);                   
       set(findobj('Tag','vyska'),'String', obraz_info.Height);                  
       set(findobj('Tag','format'),'String', obraz_info.Format);             
       set(findobj('Tag','barevny'),'String', obraz_info.ColorType);  
       set(findobj('Tag','velikost'),'String', obraz_info.FileSize);         
       set(findobj('Tag','vytvoreno'),'String', obraz_info.FileModDate);  
       set(findobj('Tag','komentar'),'String', obraz_info.Comment);         
       
       set(findobj('Tag','panel_obraz'),'UserData', fullfile(cesta,file));            % zaloha cesty
       set(findobj('Tag','panel_obraz'),'Title',[' Originální obraz ']);      
       set(findobj('Tag','popup_blok'),'Value',1);       % nacteni hodnoty pro rozdelovani do bloku

       m = str2num(get(findobj('Tag','vyska'),'String'));           % aby jiz pri nacteni obsahoval edit nejakou hodnotu
       n = str2num(get(findobj('Tag','sirka'),'String'));
       pocet = (m*n)/(2^2);

       set(findobj('Tag','pocet_bloku'),'String',pocet);       % ulozeni hodnoty pro rozdelovani do bloku
       set(findobj('Tag','popup_blok'),'UserData',2);          % B je automaticky 2 
             
       axes(handles.axes_obraz);
       imshow(nacteny_obraz);           % zobrazeni nacteneho obrazku.    
  else
      warning_3;
      return;
  end
  
% --------------------------------------------------------------------
function konec_Callback(hObject, eventdata, handles)
close;
% --------------------------------------------------------------------
function menu_Callback(hObject, eventdata, handles)

% % --- Executes on button press in pushbutton1.
% function pushbutton1_Callback(hObject, eventdata, handles)
% Close;

% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
 set(findobj('Tag','text_pocatek'),'String', num2str(get(findobj('Tag','slider1'),'Value')));         

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

 set(findobj('Tag','slider1'),'Value', 0.1664 );         
 set(findobj('Tag','text_pocatek'),'String', num2str(get(findobj('Tag','slider1'),'Value')));         


function pocet_bloku_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function pocet_bloku_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popup_blok.
function popup_blok_Callback(hObject, eventdata, handles)

x = get(findobj('Tag','popup_blok'),'Value');       % nacteni hodnoty pro rozdelovani do bloku
switch x
    case 1 
        B = 2;
    case 2 
        B = 4;
    case 3 
        B = 8;
    case 4 
        B = 16;
    case 5 
        B = 32;
end

m = str2num(get(findobj('Tag','vyska'),'String'));
n = str2num(get(findobj('Tag','sirka'),'String'));

pocet = (m*n)/(B^2);                                    % kolik bloku bude potreba prehazovat 

set(findobj('Tag','pocet_bloku'),'String',pocet);       % ulozeni hodnoty pro rozdelovani do bloku
set(findobj('Tag','popup_blok'),'UserData',B);          % ulozeni hodnoty pro rozdelovani do bloku

% --- Executes during object creation, after setting all properties.
function popup_blok_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
 set(findobj('Tag','rozkmit'),'String', num2str(get(findobj('Tag','slider2'),'Value')));         


% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

set(findobj('Tag','slider2'),'Value', 4 );
set(findobj('Tag','rozkmit'),'String', num2str(get(findobj('Tag','slider2'),'Value')));         


% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

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

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

function edit_Rth_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit_Rth_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit_alfa_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit_alfa_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checkbox_chaos.
function checkbox_chaos_Callback(hObject, eventdata, handles)

    set(findobj('Tag','checkbox_rand'),'Value',0);      % aby nemohly byt vybrany obe fce zaroven


% --- Executes on button press in checkbox_rand.
function checkbox_rand_Callback(hObject, eventdata, handles)

    set(findobj('Tag','checkbox_chaos'),'Value',0);     % aby nemohly byt vybrany obe fce zaroven


function psnr_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function psnr_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function edit_ncc_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function edit_ncc_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function edit_rozhodnuti_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function edit_rozhodnuti_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in ukonceni.
function ukonceni_Callback(hObject, eventdata, handles)
close;


function jmeno_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function jmeno_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function sirka_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function sirka_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function vyska_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function vyska_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function format_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function format_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function barevny_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function barevny_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function velikost_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function velikost_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function vytvoreno_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function vytvoreno_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function komentar_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function komentar_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
% --------------------------------------------------------------------

function otevri_vodoznak_Callback(hObject, eventdata, handles)

cd('obrazky');
[file1,cesta1] = uigetfile('*.bmp', 'Nacteni vodoznaku');                % *.* nacteni vsech souboru *.bmp
  if file1 == 0
      warning_2;
      return;
  else
      obraz_vodoznak = (imread(fullfile(cesta1,file1)));                 % 0 - 1 vodoznak 
  end
  cd ..
    
  axes(handles.axes_puvodni);
  imshow(obraz_vodoznak);           % zobrazeni vodoznaku 
  set(findobj('Tag','panel_info_obraz'),'UserData', fullfile(cesta1,file1));            % zaloha cesty
  set(findobj('Tag','preskupeni'),'Enable','on');          % povoleni tlacitka po nacteni obrazu 


% --------------------------------------------------------------------
% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function preskupeni_Callback(hObject, eventdata, handles)
clc

if get(findobj('Tag','checkbox_chaos'),'Value') == 1
    x = str2num(get(findobj('Tag','text_pocatek'),'String'));
    R = str2num(get(findobj('Tag','rozkmit'),'String'));
    N = str2num(get(findobj('Tag','pocet_bloku'),'String'));  
    [s]=chaoticka_mapa(x,R,N);
else
    N = str2num(get(findobj('Tag','pocet_bloku'),'String'));
    s = randperm(N);
end

save s                                          % ulozeni chaoticke mapy na disk        
set(findobj('Tag','preskupit'),'Value',s);      % ulozeni chaoticke mapy do gui       

% axes(handles.axes_vodoznaceno);
% plot(s);
% set(findobj('Tag','panel_vodoznak'),'Title',['Zobrazeni chaotickeho rozlozeni']);    

obraz_c = get(findobj('Tag','panel_obraz'),'UserData');            % obnova cesty
obraz = imread(obraz_c);
B = get(findobj('Tag','popup_blok'),'UserData');                 % hodnota pro rozdelovani do bloku
kolikrat_rotovat = get(findobj('Tag','popupmenu2'),'Value');

save B                                                           % kvuli checkmarku ulozime na disk      
save kolikrat_rotovat

[obraz_new, chaos] = chaoticky_obraz(obraz,B,s,kolikrat_rotovat);

set(findobj('Tag','preskupit'),'Value',s);                       % ulozeni chaoticke mapy do gui       
set(findobj('Tag','panel_nastaveni'),'UserData',obraz_new);      % ulozeni obrazu do gui       
set(findobj('Tag','text2'),'UserData',chaos);                    % ulozeni rozhoz pozic na pozdeji

axes(handles.axes_chaos);
imagesc(obraz_new);
set(findobj('Tag','panel_chaos'),'Title',[' Preskladany obraz - ', num2str(kolikrat_rotovat), ' krat ']);      % ulozeni chaoticke mapy do gui       

set(findobj('Tag','vloz_vodoznak'),'Enable','on');          % povoleni tlacitka pro vlozeni vodoznaku


% --------------------------------------------------------------------
function vloz_vodoznak_Callback(hObject, eventdata, handles)
clc;

obraz_new = get(findobj('Tag','panel_nastaveni'),'UserData');      % zisk prehazeneho obrazu do gui       
Qfaktor = str2num(get(findobj('Tag','edit_qfaktor'),'String'));
alfa = str2num(get(findobj('Tag','edit_alfa'),'String'));
Rth = str2num(get(findobj('Tag','edit_Rth'),'String'));
vodoznak_cesta = get(findobj('Tag','panel_info_obraz'),'UserData');            % nacteni vodoznaku

[obraz_IIBPC] = vloz_vodoznak(obraz_new,vodoznak_cesta,Qfaktor,alfa,Rth);

chaos = get(findobj('Tag','text2'),'UserData');                  % nacteni rozhoz pozic na pozdeji
B = get(findobj('Tag','popup_blok'),'UserData');                 % hodnota pro rozdelovani do bloku
kolikrat_rotovat = get(findobj('Tag','popupmenu2'),'Value');

[vysledek]=chaos_obraz_zpet(obraz_IIBPC,chaos,B,kolikrat_rotovat);

set(findobj('Tag','panel_vodoznak'),'UserData',vysledek);         % ulozeni obrazu do gui       

axes(handles.axes_vodoznaceno);
imagesc(vysledek);


obraz_cesta = get(findobj('Tag','panel_obraz'),'UserData');            % obnova cesty
obraz = imread(obraz_cesta);                % nacteni znovu kvuli vypoctu, dalo by se resit i jinak !!             
[m,n]=size(obraz);

% vypocet PSNR
obr_original = double(obraz);
obr_vodoznaceny = double(uint8(vysledek));
obr_velikost = double(m*n);
MSE=(sum(sum((obr_original-obr_vodoznaceny).^2)))/obr_velikost;
PSNR=10*log10(255^2/(MSE))

set(findobj('Tag','psnr'),'String',num2str(PSNR));         % Zobrazeni PSNR      

% ulozeni vodoznaceneho obrazku na zvolene misto
imwrite(uint8(vysledek),'PatchworkWM1.bmp');                        % ulozeni kvuli checkmarku
imwrite(uint8(vysledek),'PatchworkWM1.jpg','Quality',100);

set(findobj('Tag','dek_vodoznak'),'Enable','on');          % povoleni tlacitka pro dekodovani vodoznaku
set(findobj('Tag','utocit'),'Enable','on');          % povoleni tlacitka pro provedeni utoku checkmarkem
set(findobj('Tag','checkmark'),'Enable','on');          % povoleni tlacitka pro vyhodnoceni checkmarkem
set(findobj('Tag','parser'),'Enable','on');          % povoleni tlacitka pro tvorbu html vystupu


% --------------------------------------------------------------------
function dek_vodoznak_Callback(hObject, eventdata, handles)

[file2,cesta2] = uigetfile('*.*', 'Nacteni obrazu po utoku na vodoznak !');                % *.* nacteni vsech souboru *.bmp
  if file2 == 0
      warning_2;
      return;
  else
      po_utoku = (imread(fullfile(cesta2,file2)));                 % obraz po utoku
  end
 
 axes(handles.axes_obraz);
 imshow(po_utoku);   
 set(findobj('Tag','panel_obraz'),'Title',[' Obraz po útoku ']);      
 
 B = get(findobj('Tag','popup_blok'),'UserData');                 % hodnota pro rozdelovani do bloku
 kolikrat_rotovat = get(findobj('Tag','popupmenu2'),'Value');
 
 load s 

 [obraz_new, chaos] = chaoticky_obraz(po_utoku,B,s,kolikrat_rotovat);

 Qfaktor = str2num(get(findobj('Tag','edit_qfaktor'),'String'));
 alfa = str2num(get(findobj('Tag','edit_alfa'),'String'));
 Rth = str2num(get(findobj('Tag','edit_Rth'),'String'));
 vodoznak_cesta = get(findobj('Tag','panel_info_obraz'),'UserData');            % nacteni vodoznaku

 [vytazeny_vodoznak, NCC] = najdi_vodoznak(obraz_new,vodoznak_cesta,Qfaktor,alfa,Rth);
 set(findobj('Tag','edit_ncc'),'String',num2str(NCC));      

 if NCC > 0.7 
    set(findobj('Tag','edit_rozhodnuti'),'String','Vodoznak nalezen');      
 else
    set(findobj('Tag','edit_rozhodnuti'),'String','Vodoznak nenalezen');      
 end
 
 axes(handles.axes_ziskany);
 imshow(vytazeny_vodoznak);
 
 
% --------------------------------------------------------------------
function Untitled_6_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function Untitled_7_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function Untitled_8_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function Untitled_9_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function Untitled_10_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function utocit_Callback(hObject, eventdata, handles)

%zavolame si funkci checkmarku a provedeme utoky.

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

runCheckmark;                   % provedem utoky podle copyright

cd('c:\metoda_2\');

% --------------------------------------------------------------------
function checkmark_Callback(hObject, eventdata, handles)
%zavolame si funkci checkmarku a provedeme kontrolu.

B = get(findobj('Tag','popup_blok'),'UserData');                 % hodnota pro rozdelovani do bloku
kolikrat_rotovat = get(findobj('Tag','popupmenu2'),'Value');
Qfaktor = str2num(get(findobj('Tag','edit_qfaktor'),'String'));
alfa = str2num(get(findobj('Tag','edit_alfa'),'String'));
Rth = str2num(get(findobj('Tag','edit_Rth'),'String'));
vodoznak_cesta = get(findobj('Tag','panel_info_obraz'),'UserData');            % nacteni vodoznaku
 
%cd('c:\Skola\diplomka\matlab_prace\Checkmark\');

testDetector(B,kolikrat_rotovat,Qfaktor,alfa,Rth,vodoznak_cesta)          % provedem utoky podle copyright

cd('c:\metoda_2\');

%*************************************************************
% zapsat vysledky ziskane z checkmarku do souboru

load vysledek_checkmark
fid = fopen('vysledky.txt', 'w');

pocet = size(vysledek_checkmark,2);                 % pocet provedenych testu

for j=1:pocet
vypis{j} = ['Soubor - ' vysledek_checkmark(j).jmeno_vodoznaku  '      NCC - ' num2str(vysledek_checkmark(j).NCC) '   Shoda - ' num2str(vysledek_checkmark(j).result)];
fprintf(fid,'\n Soubor - %s, NCC - %c, Shoda - %d', vysledek_checkmark(j).jmeno_vodoznaku, vysledek_checkmark(j).NCC, vysledek_checkmark(j).result); 
% zapsani do souboru a zaokrouhleni na 3 des. mista
end
fclose(fid);



% --------------------------------------------------------------------
function parser_Callback(hObject, eventdata, handles)

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

parseresults;                   % provedem rozparsovani

cd('c:\metoda_2\');
 

