function varargout = SimpleColorAdjust(varargin)
%SimpleColorAdjust is a simple GUI example of color adjust.
% If we know the white pixel, the color adjustment is easily performed.
% Please load an image, and pick (click) the white pixel.
% Then color adjustment result will appear in the output image.
%
%Version: 20120613

% Edit the above text to modify the response to help SimpleColorAdjust

% Last Modified by GUIDE v2.5 13-Jun-2012 08:29:07

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple Color Adjust:                                     %
%                                                          %
% Copyright (C) 2012 Masayuki Tanaka. All rights reserved. %
%                    mtanaka@ctrl.titech.ac.jp             %
%                                                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

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

% Update handles structure
guidata(hObject, handles);

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

axes(handles.axesSrc);
imshow(zeros(3,4,'uint8'));
axes(handles.axesDst);
imshow(zeros(3,4,'uint8'));

global src dst;
src = [];
dst = [];

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

global src;
global dst;
[filename, dirpath, filterindex] = uigetfile( '*.*', 'Load Image');
src = double(imread(strcat(dirpath,filename)));
dst = src;

axes(handles.axesSrc);
imshow(uint8(src));

axes(handles.axesDst);
imshow(uint8(dst));


% --- Executes on button press in pushbuttonSave.
function pushbuttonSave_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonSave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, dirpath, filterindex] = uiputfile( '*.*', 'Save Image');
global dst;
imwrite(uint8(dst),strcat(dirpath,filename));

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global src dst;
s = size(src);
if( s(1) > 0 )
    pos = get( handles.axesSrc, 'CurrentPoint');
    row = round(pos(1,2));
    col = round(pos(1,1));
    if( row >= 1 && row <= s(1) && col >=1 && col <= s(2) )
        dst = wb(src, row, col);
        axes(handles.axesDst);
        imshow(uint8(dst));
    end
end

function dst = wb( src, row, col )
r = src(row, col, 1 );
g = src(row, col, 2 );
b = src(row, col, 3 );

dst = src;
dst(:,:,1) = dst(:,:,1) * (g+1E-12)/(r+1E-12);
dst(:,:,3) = dst(:,:,3) * (g+1E-12)/(b+1E-12);