function im2c = iris_transfer(im1c, im2c)
% Transfer the high light from 

if ~exist('im1c')
  addpath('../../../../timeLapseCode/TimeLapseVideo/helpers/iris');
  addpath('../../../../timeLapseCode/TimeLapseVideo/helpers');
  addpath('../../../../timeLapseCode/TimeLapseVideo/helpers/matting/');
  im1c = imread(sprintf('../../aligned_data/martin/15.jpg'));
  keyboard;
  im1c = double(im1c(500:650, 250:450, :))/255;
end


if ~exist('im2c')
  %fname='a4576-DSC_0217_mscale_auto_warped_martin'; % sam
  fname='a2816-IMG_3974_mscale_auto_warped_martin'; % lior
  im2c = imread(sprintf('../results/%s.jpg', fname));
  im2c = double(im2c(500:650, 250:450, :))/255;
end


rmin=20;
rmax=200;
delta_r = 5;

% Iris mask
[ci1, cp1, o1]= thresh(rgb2gray(im1c), rmin, rmax);
[ci2, cp2, o2]= thresh(rgb2gray(im2c), rmin, rmax);
r = ci2(3)-delta_r;
spatch = im1c(ci1(1)-r:ci1(1)+r, ci1(2)-r:ci1(2)+r, :); 
irisM = repmat(circleMask(r), [1 1 3]);

% Skin mask
skinM = repmat(skin(255*spatch), [1 1 3]);
iris  = (1-skinM).*spatch.*irisM;

% Reflection matting
[refl_c iris_c ]= estimate_iris_color(iris)

% Baysian matting with one iteration 
% [alpha_map fg bg ]= estimate_alpha(iris, iris_c, refl_c);

% Laplacian matting
[alpha_map fg bg] = laplacianMatting(iris, iris_c, refl_c, [0 0 0]);

alpha_map = repmat(alpha_map, [1 1 3]);


% Alpha matting
im2co=im2c;
eye2 =im2c(ci2(1)-r : ci2(1)+r, ci2(2)-r : ci2(2)+r,:) ;
im2c(ci2(1)-r : ci2(1)+r, ci2(2)-r : ci2(2)+r,:) = ...
                  (1-alpha_map).*eye2 +  alpha_map.*fg; 

%figure;imshow([spatch alpha_map fg bg]);
%figure;imshow([o1 o2]);
%figure; imshow([1-skinM irisM alpha_map]);
%figure;imshow([im1c im2co im2c ]);
%imwrite_q([im1c im2co im2c ; ], 'hahaha.jpg');

function [alpha fg bg] = laplacianMatting(I, ic, rc, bc)

  threc = 0.1;
  [h w nc]=size(I);
  ii = sum(abs(I - cmap(ic, h,w)),3)<threc; % iris 
  rr = sum(abs(I - cmap(rc, h,w)),3)<threc; % reflection
  bb = sum(abs(I - cmap(bc, h,w)),3)<threc; % background

  consts_map = ii+rr+bb; 
  consts_vals = rr;
  %figure;imshow([ii rr bb consts_map]);

  %keyboard;
  levels_num=1;
  active_levels_num=1;
  thr_alpha=[];
  epsilon=[];
  win_size=[];

 alpha=solveAlphaC2F(I,consts_map,consts_vals,levels_num, ...
                     active_levels_num,thr_alpha,epsilon,win_size);
  [fg,bg]=solveFB(I,alpha);



function out = cmap(c_in, h,w)
  out = zeros(h,w,3);
  out(:,:,1) = c_in(1);
  out(:,:,2) = c_in(2);
  out(:,:,3) = c_in(3);
  

function [alpha fg bg] = estimate_alpha(iris, ic, rc)
addpath('../../../../timeLapseCode/TimeLapseVideo/helpers/matting/');
  [h w nc]=size(iris);
  ic_v   = repmat(ic, [h*w 1]);
  rc_v   = repmat(rc, [h*w 1]);
  iris_v = im2Vec(iris);

  alpha_v = dot(iris_v-ic_v,rc_v-ic_v, 2)./dot(rc_v-ic_v,rc_v-ic_v,2);
  alpha   = reshape(alpha_v, h, w);
  alpha(alpha>1)=1;
  alpha(alpha<0)=0;
  figure;imshow(alpha);

  % TODO background and foreground
  [fg,bg]=solveFB(iris,alpha);



function [refl_c iris_c] = estimate_iris_color(iris)
  % Assume there are only 3 components: 
  % Black(pupil and non iris region), white (reflecton) 
  % and iris color.

  [~, clusters]= kmeans(im2Vec(iris), 3, 'emptyaction', 'singleton');
  [~, idx_s]   = sort(sum(clusters,2), 'descend');
  refl_c       = clusters(idx_s(1),:);
  iris_c       = clusters(idx_s(2),:);


function out = im2Vec(im) 
  [h w nc]=size(im);
  out     = reshape(im, [h*w nc]);

function mask = circleMask(r)
  mask = zeros(2*r+1);
  [xx yy]=meshgrid(-r:r, -r:r);
  mask(xx.*xx + yy.*yy <=r^2)=1;

% Legacy code ===========================

function p = lighten(p1, p2)  
  [h w nc]=size(p1);
  pmap = rgb2gray(p1)<rgb2gray(p2);
  pmap = repmat(pmap, [1 1 nc]);

  p=p1;
  p(pmap)=p2(pmap);


function l = rgb2light(input)
  lab=RGB2Lab(input);
  l = lab(:,:,1)/100;
  

function output = medfilt2c(input, kernel)
  output = input;
  for i = 1 : size(input,3)
    output(:,:,i) = medfilt2(input(:,:,i), kernel);
  end


