function explore_p()

% Exploring the parameters space for two scale model
% Decompose the scale with box filter
% Exploring decomposition patch size and regularization patch size

input_set = 'raw_model';


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


%input_set = 'inputs';

switch input_set 
  case 'inputs'
    inputs = {...
                  'a3829-07-06-02-at-05h48m48-s_MG_1315',...
                  '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',...
              %'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'};

p_widths=[3 7 11 25 51 71 101 151 201]; 
k_widths=[3 7 11 ];

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;

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


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


    scale=0.5;
    for n = 1 : length(p_widths)
      p_width = p_widths(n); % patch width for contrast 
      fprintf('Computing local variance for p_width %d \n', p_width);
      [texture_map_s ~] = LocalVar(style, scale, p_width); 
      fprintf('Done. Render the portrait... \n');

      for m = 1 : length(k_widths)

        k_width = k_widths(m); % patch width for regularization 

        flags.epsilon=7;
        flags.gamma=10;
        flags.model = 'linear';
        flags.patch_size = k_width;


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

        N_support = boxfilter(zeros(h, w)+1, (p_width-1)/2); 

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

        %% Low freq. comp. of input
        input_a=input_s*0;
        for i = 1 : num_channels
          input_a(:,:,i) = boxfilter(input_s(:,:,i), (p_width-1)/2)./...
                      boxfilter(zeros(h,w)+1, (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);
        
        output_tag = '_output_';

        imwrite(output, ...
        fullfile('results',   [input_name output_tag ...
        '_pwidth_' num2str(n) ...
        '_kwidth_' num2str(m) '_'...
        style '.jpg']),...
        'Quality', 100);
      end
    end

  end
end


%% helpers

function var_I=local_var(I, r_support, N_support)

[h w nc]=size(I);
if nc==3
  I = rgb2gray(I);
end
[hei, wid] = size(I);
mean_I = boxfilter(I, r_support) ./ N_support;
mean_II = boxfilter(I.*I, r_support) ./ N_support;
var_I = mean_II - mean_I .* mean_I;

