function varargout = interface(varargin)
% INTERFACE MATLAB code for interface.fig
% HOWTO
% Run interface.fig
% Click 'Start' to initialize the program
% Adjust the weights
% Click next, and select a folder to sabe the compositions and weights
% Repeat: Adjust weights and click next.
warning off;
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @interface_OpeningFcn, ...
                   'gui_OutputFcn',  @interface_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 interface is made visible.
function interface_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 interface (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

initialize_gui(hObject, handles, false);

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


% --- Outputs from this function are returned to the command line.
function varargout = interface_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 button press in calculate.
function calculate_Callback(hObject, eventdata, handles)
% hObject    handle to calculate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

clc
%addpath(genpath('./conv_pyr'));
addpath(genpath('./utils'));
addpath(genpath('./images'));
all_names={'church','comparison_color_me_right','eye','eye_on_me','fish','girl','house','lionsavana','mixed_background','plane','portrait','ship','trolls'};
current_composition = 1;

folder = ['images/' all_names{current_composition}];
src = im2double(imread(strcat(folder,'/source.jpg')));
trg = im2double(imread(strcat(folder,'/target.jpg')));
alpha = im2double(imread(strcat(folder,'/alpha.jpg')));
alpha = alpha(:,:,1);
if exist(strcat(folder,'/mask.jpg'))
    mask = im2double(imread(strcat(folder,'/mask.jpg')));
    mask = logical(mask(:,:,1));
else
    mask = imdilate(logical(alpha),strel('disk',1));    
end
[res_poisson,membrane] = ConvolutionPyramid(src, trg, mask);

w = [0.8 0.8 0.8];
res_w = ColorCorrection_dominant_color(src, trg, mask, alpha, membrane,w);
handles.figure_handle = figure;
imshow(res_w);
handles.src = src;
handles.trg = trg;
handles.mask = mask;
handles.alpha = alpha;
handles.membrane = membrane;
handles.w = w;
handles.res_w = res_w;
handles.all_names = all_names;
handles.current_composition = current_composition;
guidata(hObject, handles);


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

%initialize_gui(gcbf, handles, true);
if(handles.current_composition==1)
    folder_name = uigetdir;
else
    folder_name = handles.folder_name;
end
str = strcat(folder_name,'/result_',handles.all_names{handles.current_composition});
if(str)
   imwrite(handles.res_w,strcat(str,'.png'));
end

fileID = fopen(strcat(str,'.txt'),'w');
fprintf(fileID,'%3f %3f %3f\n',handles.w);
fclose(fileID);


handles.current_composition = handles.current_composition+1;

folder = ['images/' handles.all_names{handles.current_composition}];
src = im2double(imread(strcat(folder,'/source.jpg')));
trg = im2double(imread(strcat(folder,'/target.jpg')));
alpha = im2double(imread(strcat(folder,'/alpha.jpg')));
alpha = alpha(:,:,1);
if exist(strcat(folder,'/mask.jpg'))
    mask = im2double(imread(strcat(folder,'/mask.jpg')));
    mask = logical(mask(:,:,1));
else
    mask = imdilate(logical(alpha),strel('disk',1));    
end
[res_poisson,membrane] = ConvolutionPyramid(src, trg, mask);

w = handles.w;
res_w = ColorCorrection_dominant_color(src, trg, mask, alpha, membrane,w);
figure(handles.figure_handle);
imshow(res_w);
handles.src = src;
handles.trg = trg;
handles.mask = mask;
handles.alpha = alpha;
handles.membrane = membrane;
handles.w = w;
handles.res_w = res_w;
handles.folder_name = folder_name;
guidata(hObject, handles);



% --- Executes when selected object changed in unitgroup.
function unitgroup_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in unitgroup 
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function initialize_gui(fig_handle, handles, isreset)
% If the metricdata field is present and the reset flag is false, it means
% we are we are just re-initializing a GUI by calling it from the cmd line
% while it is up. So, bail out as we dont want to reset the data.
if isfield(handles, 'metricdata') && ~isreset
    return;
end

% Update handles structure
guidata(handles.figure1, handles);


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


%imshow(trg);
w = handles.w;
w(1) = get(hObject,'Value');
handles.res_w = ColorCorrection_dominant_color(handles.src, handles.trg, handles.mask, handles.alpha, handles.membrane,w);
figure(handles.figure_handle);
imshow(handles.res_w);
handles.w = w;
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


% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (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
w = handles.w;
w(2) = get(hObject,'Value');
handles.res_w = ColorCorrection_dominant_color(handles.src, handles.trg, handles.mask, handles.alpha, handles.membrane,w);
figure(handles.figure_handle);
imshow(handles.res_w);
handles.w = w;
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (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 slider movement.
function slider3_Callback(hObject, eventdata, handles)
% hObject    handle to slider3 (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
w = handles.w;
w(3) = get(hObject,'Value');
handles.res_w = ColorCorrection_dominant_color(handles.src, handles.trg, handles.mask, handles.alpha, handles.membrane,w);
figure(handles.figure_handle);
imshow(handles.res_w);
handles.w = w;
guidata(hObject, handles);


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