function single_face(input_set)

% Run two scale matching model on a single given face

addpath('../../../timeLapseCode/TimeLapseVideo/helpers/local_linear_transfer/');
addpath('../../../timeLapseCode/TimeLapseVideo/helpers/');
addpath('../AlignFace/');


%input_set = 'inputs';

switch input_set 
  case 'inputs'
    inputs = {...
                  'a3988-_MG_0823',... % Steve
                  'a4576-DSC_0217',... % Sam
        'a2816-IMG_3974',...           % Lior
        'a4854-09-05-19-at-20h58m50s-_MG_9314'... %Vlad
        };

    by_examples = {...
      '2609309235_780eae77b5_z',...
      '2388557829_2700f5a874_z',...
      '2376006511_f285de8b73_z',...
      '3616364040_68971b6d90_z',...
      };
  case 'raw_model'
    inputs = { ...
              '79073_fashion_8423_122_737lo',...
              'oldGuy_v001_Zblur8Bits',...
              '78146_fashion_426_122_1142lo',...
              'model',...
              '78164_fashion_141_122_614lo',...
                };
  case 'friends'
    inputs = { ...
      'yair_8', ...
      'yichang_26',...
      'ama_0', ...
      '56', ...
      'hui_0',...
    };
  otherwise
end

%styles = {'martin'};
styles = { 'kelco'};


for i = 1 : length(inputs)
  for j = 1 : length(styles)


    input_name = inputs{i};
    style      = styles{j};

    %% Loading inputs, example mean and texture.
    input_c = double(imread(sprintf('../aligned_data/%s_%s/%s.jpg', ...
                                        input_set, style, input_name)))/255;

    

    %load(sprintf('../AlignFace/sum_face/00_%s.mat', style));
    %example = im_mean_color;
    examples = dir('../aligned_data/kelco/*.jpg');
    %example_name = examples(randi(length(examples))).name;
    example_name = [by_examples{i} '.jpg'];
    example = double(imread(sprintf('../aligned_data/kelco/%s', example_name)))/255;

    %load(sprintf('../AlignFace/sum_face/03_%s.mat', style));
    %texture_map_s = avg_var_I;


 
    if strcmp(style, 'kelco')
      input = rgb2gray(input_c);
      example = rgb2gray( example);
    else
      input = input_c;
    end

    flags.patch_size =9;
    flags.epsilon=7;
    flags.gamma=2;

    flags.model = 'linear';
    scale=0.5;
    %p_width=round(scale*50/2)*2-1;
    p_width = 35;

    input_s = imresize(input, scale);
    example_s = imresize(example, scale);
    [h w num_channels]=size(input_s);


    input_contrast = local_var(input_s, (p_width-1)/2); 
    texture_map_s  = local_var(example_s, (p_width-1)/2);
    texture_gain = texture_map_s./input_contrast;
    texture_gain = 1.0*texture_gain.^0.5;
    %texture_gain = texture_gain*0 + 1 ; 


    %% Low freq. comp. of input
    input_a=input_s*0;
    for i = 1 : num_channels
      input_a(:,:,i) = myfilter(input_s(:,:,i), (p_width-1)/2);
    end

   % example_s = imfilter(example_s, fspecial('gaussian', 30, 10)); 

    for i = 1 : num_channels
      delta_x  = input_s(:,:,i) - input_a(:,:,i);

      delta_x= cat(3, delta_x, delta_x*0 + 1);
      output_vec = LocalTransfer(delta_x,  example_s(:,:,i), texture_gain, flags);
      output(:,:,i) = reshape(output_vec, [h w 1]);
    end

    [a b E] = LocalRegres(delta_x(:,:,1), output, texture_gain, ...
              example_s(:,:,1), flags.patch_size, flags.gamma, flags.epsilon);

    %figure; imagesc(a);
    %figure; imagesc(delta_x(:,:,1));
    %figure; imshow(exp(b));
    %figure; imagesc(E);


    %% Write Outputs
    imwrite(input_c, ...
    fullfile('results',   [input_name '_input_' style '.jpg']),...
    'Quality', 100);

    imwrite(example_s, ...
    fullfile('results',   [input_name '_example_' style '.jpg']),...
    'Quality', 100);


    output_tag = '_output_singlef_';

    output(1:p_width, 1:p_width)=1;
    imwrite(output, ...
    fullfile('results',   [input_name output_tag style '.jpg']),...
    'Quality', 100);

  end
end

% helpers:
function var_I=local_var(I, r_support)

[h w nc]=size(I);
if nc==3
  I = rgb2gray(I);
end
[hei, wid] = size(I);
mean_I  = myfilter(I, r_support);
delta_I = I - mean_I ; 
var_I   = boxfilter_n(delta_I.^2, r_support);


function output = myfilter(I, r_support) 
  %output = boxfilter_n(I, r_support);
  output = bilateralFilter(I, I, 0, 1, r_support, 0.20); 


  
% helpers

