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

% Last Modified by GUIDE v2.5 25-May-2015 21:59:04

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @pseudogui_OpeningFcn, ...
                   'gui_OutputFcn',  @pseudogui_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 pseudogui is made visible.
function pseudogui_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 pseudogui (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = pseudogui_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 edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
val=str2double(get(handles.edit1,'String')); % prah 
if isnumeric(val) & length(val)==1 & ...     
        val >= get(handles.slider1,'Min') & ...
        val <= get(handles.slider1,'Max')
    set(handles.slider1,'Value',val);
else
    handles.numberOfErrors = handles.numberOfErrors + 1;
    set(handles.edit1,'String', ...
        [handles.errorString,num2str(hnaldes.numberOfErrors)]);
    guidata(gcbo,handles);
end

if str2double(get(handles.edit3,'String'))>0 && str2double(get(handles.edit4,'String'))>0
    
    global myFolder
h = waitbar(0,'Chvili strpeni'); %cekaci okno 
if ~isdir(myFolder)           %nacteni snimku
  errorMessage = sprintf('Pozor: Tato složka neexsistuje:\n%s', myFolder);
  uiwait(warndlg(errorMessage));
  return;
end

filePattern = fullfile(myFolder, '*.tif'); %tvorba imageArray
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
  baseFileName = jpegFiles(k).name;
  fullFileName = fullfile(myFolder, baseFileName);
  imageArray(k).image = imread(fullFileName);
      imageArray(k).image=rgb2gray(imageArray(k).image); %na sedoton
    imageArray(k).image=im2double(imageArray(k).image);%na double

end


praha = str2double(get(handles.edit1,'string'));
spektrum1 = str2double(get(handles.edit3,'string'));
spektrum2 = str2double(get(handles.edit4,'string'));

[X Y ]=size(imageArray(1).image);
pseudo=zeros(X,Y); %tvorba nove matice o rozmerech ImageArray
indexy=linspace(spektrum1,spektrum2,length(imageArray)); %vektor od min do max s krokem počtu snímků

 aa=struct2cell(imageArray); %ImageArray na bunku 
for m=1:X
    for n=1:Y
 abbrev = cellfun(@(x) x(m,n), aa, 'UniformOutput', false);
 pom=[abbrev{:,:}]';
 
 pomm=find(pom==max(pom)&pom>praha); %prochazeni a hledani maximalniho pixelu
 if length(pomm)==0
     pseudo(m,n)=indexy(1);
 elseif length(pomm)==1
     pseudo(m,n)=indexy(pomm);
 else
    pseudo(m,n)=indexy(randi(length(pomm))); %pokud dve maxima nahodne vybere
 end
    end
end
close(h)
axes(handles.axes1)
 cmap = jet(256); %pseudo mapa
 cmap(1,:) = [ 0 0 0 ];
    imshow(pseudo, 'Colormap',cmap) 
    zoom on
    caxis([spektrum1, spektrum2])
h = colorbar('southoutside');

h.Label.String = 'Emisní spektrum [nm]';%funguje jen u matlab 2015


end

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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



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


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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



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


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global myFolder  %stejny kod pro spusteni tlacitkem start
h = waitbar(0,'Chvili strpeni');
if ~isdir(myFolder)
  errorMessage = sprintf('Pozor: Tato složka neexsistuje:\n%s', myFolder);
  uiwait(warndlg(errorMessage));
  return;
end

filePattern = fullfile(myFolder, '*.tif');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
  baseFileName = jpegFiles(k).name;
  fullFileName = fullfile(myFolder, baseFileName);
  imageArray(k).image = imread(fullFileName);
      imageArray(k).image=rgb2gray(imageArray(k).image);
    imageArray(k).image=im2double(imageArray(k).image);

end


praha = str2double(get(handles.edit1,'string'));
spektrum1 = str2double(get(handles.edit3,'string'));
spektrum2 = str2double(get(handles.edit4,'string'));

[X Y Z]=size(imageArray(1).image);
pseudo=zeros(X,Y);
indexy=linspace(spektrum1,spektrum2,length(imageArray));

 aa=struct2cell(imageArray);
for m=1:X
    for n=1:Y
 abbrev = cellfun(@(x) x(m,n), aa, 'UniformOutput', false);
 pom=[abbrev{:,:}]';
 
 pomm=find(pom==max(pom)&pom>praha);
 if length(pomm)==0
     pseudo(m,n)=indexy(1);
 elseif length(pomm)==1
     pseudo(m,n)=indexy(pomm);
 else
    pseudo(m,n)=indexy(randi(length(pomm)));
 end
    end
end
close(h)
axes(handles.axes1)
 cmap = jet(256);
 cmap(1,:) = [ 0 0 0 ];
    imshow(pseudo, 'Colormap',cmap) 
    zoom on
    caxis([spektrum1, spektrum2])
h = colorbar('southoutside');
h.Label.String = 'Emisní spektrum [nm]';




% --- 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
set(handles.edit1,'String',num2str(get(handles.slider1,'Value')))

if str2double(get(handles.edit3,'String'))>0 && str2double(get(handles.edit4,'String'))>0
    
global myFolder %stejny kod, zde pro spusteni programu zmenou prahu
h = waitbar(0,'Chvili strpeni');
if ~isdir(myFolder)
  errorMessage = sprintf('Pozor: Tato složka neexsistuje:\n%s', myFolder);
  uiwait(warndlg(errorMessage));
  return;
end

filePattern = fullfile(myFolder, '*.tif');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
  baseFileName = jpegFiles(k).name;
  fullFileName = fullfile(myFolder, baseFileName);
  imageArray(k).image = imread(fullFileName);
      imageArray(k).image=rgb2gray(imageArray(k).image);
    imageArray(k).image=im2double(imageArray(k).image);

end


praha = str2double(get(handles.edit1,'string'));
spektrum1 = str2double(get(handles.edit3,'string'));
spektrum2 = str2double(get(handles.edit4,'string'));

[X Y]=size(imageArray(1).image);
pseudo=zeros(X,Y);
indexy=linspace(spektrum1,spektrum2,length(imageArray));

 aa=struct2cell(imageArray);
for m=1:X
    for n=1:Y
 abbrev = cellfun(@(x) x(m,n), aa, 'UniformOutput', false);
 pom=[abbrev{:,:}]';
 
 pomm=find(pom==max(pom)&pom>praha);
 if length(pomm)==0
     pseudo(m,n)=indexy(1);
 elseif length(pomm)==1
     pseudo(m,n)=indexy(pomm);
 else
    pseudo(m,n)=indexy(randi(length(pomm)));
 end
    end
end
close(h)
axes(handles.axes1)
 cmap = jet(256);
 cmap(1,:) = [ 0 0 0 ];
    imshow(pseudo, 'Colormap',cmap) 
    zoom on
    caxis([spektrum1, spektrum2])
h = colorbar('southoutside');
h.Label.String = 'Emisní spektrum [nm]';


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


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes1); %vynuluje hodnoty
imshow(0)
set(handles.edit1,'String',0)
set(handles.edit3,'String',0)
set(handles.edit4,'String',0)
set(handles.slider1,'Value',0)
