function two_scale_avgface(input_set)

% Two scale matching model with average face
% Decomposing with boxfilter


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


%input_set = 'inputs';

switch input_set 
  case 'inputs'
    inputs = {...
                  %'a00000_yewang',...
                  'a3988-_MG_0823',...
                  'a4576-DSC_0217',...
        'a2816-IMG_3974',...
        'a4854-09-05-19-at-20h58m50s-_MG_9314'...
        };
  case 'raw_model'
    inputs = { ...
              '79073_fashion_8423_122_737lo',...
              '79073_fashion_8423_122_737lo_noblemish',...
              '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;

    use_log = false;


    load(sprintf('../AlignFace/sum_face/00_%s.mat', style));
    example = im_mean_color;

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

    if use_log
      load(sprintf('../AlignFace/sum_face/00_%s_log.mat', style));
      example = im_mean_color_log;

      load(sprintf('../AlignFace/sum_face/03_%s_log.mat', style));
      texture_map_s = avg_var_I_log;
    end


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

    flags.patch_size =7;
    flags.epsilon=7;
    flags.gamma=10;

    if use_log
      flags.epsilon = 10;
      flags.gamma =50;
    end

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

    input_s = imresize(input, scale);
    example_s = imresize(example, scale);
    [h w num_channels]=size(input_s);
    if use_log 
      input_s = log(input_s+0.01);
    end


    input_contrast = local_var(input_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


    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);

    if use_log
      output = exp(output);
      b      = exp(b);
      %output = exp(5*delta_x(:,:,i) + example_s(:,:,1));
    end

    output(1:p_width, 1:p_width)=1;

    %% Write Outputs
    check_residual = false;

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

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

    if check_residual
      imwrite(b, ...
      fullfile('results',   [input_name '_b_' style '.jpg']),...
      'Quality', 100);


      imwrite( ViewRB(b - example_s(:,:,1)), ...
      fullfile('results',   [input_name '_b_res_' style '.jpg']),...
      'Quality', 100);


      imwrite( ViewRB(E), ...
      fullfile('results',   [input_name '_linearity_' style '.jpg']),...
      'Quality', 100);

      imwrite( a,jet, ...
      fullfile('results',   [input_name '_a_' style '.jpg']),...
      'Quality', 100);

      imwrite( texture_gain, jet, ...
      fullfile('results',   [input_name '_contrast_gain_' style '.jpg']),...
      'Quality', 100);


      imwrite( a - texture_gain, jet, ...
      fullfile('results',   [input_name '_a_res_' style '.jpg']),...
      'Quality', 100);
    end

    
    output_tag = '_output_';
    if use_log
      output_tag = '_output_log_';
    end

    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.15); 


  

