function c1params = PROCEDURE_INRIAToC1_stanedit(Root,fn, newfn, variablename, options);
%function c1params = PROCEDURE_INRIAToC1(Root,fn, newfn, variablename, options);
%
%read the grayscale data and convert it to C1

HelpTextInfo = 'PROCEDURE_INRIAToC1_stanedit';
d.C1Ver002 = 0;
d.C1Ver003 = 0;
d.C1Ver004 = 0;
d.C1Ver006 = 0;
d.C1Ver007 = 0;
d.C1Ver008 = 0;
d.C1Ver009 = 0;
d.C1Ver010 = 0;
d.RegularC1 = 0;
d.Triggs = 0;
if(nargin < 5),options = [];,end
options = ResolveMissingOptions(options,d);
[y, X] = GetNewDataX(fn, variablename, Root);
[C1X,c1params, options] = GrayX2C1X(X,[128 64],options);
savename = fullfile(Root,newfn);
save(savename,'C1X','y','HelpTextInfo','c1params','options');




function [yfull, NewDataX, options] = GetNewDataX(fn, variablename, Root)
NewDataX = [];
yfull = [];
D = load(fullfile(Root,fn));%-->(variablename),y;
NewDataX = getfield(D,variablename);
yfull = D.y; 




function [C1X,c1params,options] = GrayX2C1X(X,originalres,options);
[options,filters,fSiz,c1SpaceSS,c1ScaleSS,c1OL,numSimpleFilters] = GetOptions(options);
for i = 1:size(X,2)
  fprintf('Converting Grayscale To C1 %.4d of %.4d\r',i,size(X,2));
  stim = reshape(X(:,i),originalres);
  [c1,s1,v1] = C1_stanedit(stim, filters, fSiz, c1SpaceSS, c1ScaleSS, c1OL,options);
  if (i == 1)
    C1X = zeros(length(v1),size(X,2));
  end
  C1X(:,i) = v1;
end
fprintf('\n done. \n');
c1params = [];
% c1params.rot = rot;
% c1params.div = div;
% c1params.RF_siz = RF_siz;
c1params.c1ScaleSS = c1ScaleSS;
c1params.c1SpaceSS = c1SpaceSS;
c1params.c1OL = c1OL;
c1params.filters = filters;
c1params.fSiz = fSiz;
c1params.numSimpleFilters = numSimpleFilters;




function [options,filters,fSiz,c1SpaceSS,c1ScaleSS,c1OL,numSimpleFilters] = GetOptions(options)
if(options.C1Ver002)
  options.includelaplacianfilter = 0;
  options.includegrayscalefilter = 0;
  options.bEstimateOrientations = 0;
  options.nOriEstimates = 9;
  options.INCLUDEBORDERS = 1;  
  options.bUseBlockwiseNormalization = 1;
  options.BlockwiseNormParams_yxCellsPerBlock = [2,2];
  options.BlockwiseNormParams_yxBlockSpacing =  [1,1];
  options.BlockwiseNormParams_Norm =  'sqrtL1';
  c1ScaleSS = [1:2:3];
  c1SpaceSS = [24];
  c1OL = 3; 
  fSiz = repmat(3,[1,12]);
  filters(:,1) = [1;1;1;0;0;0;-1;-1;-1];
  filters(:,2) = [-1;0;1;-1;0;1;-1;0;1];
  filters(:,3) = [0;1;1;-1;0;1;-1;-1;0];
  filters(:,4) = [1;1;0;1;0;-1;0;-1;-1];
  filters(:,5) = [-1;-1;-1;-1;8;-1;-1;-1;-1];
  filters(:,6) = [0;0;0;0;1;0;0;0;0];
  filters(:,7) = [1;1;1;0;0;0;-1;-1;-1];
  filters(:,8) = [-1;0;1;-1;0;1;-1;0;1];
  filters(:,9) = [0;1;1;-1;0;1;-1;-1;0];
  filters(:,10) = [1;1;0;1;0;-1;0;-1;-1];
  filters(:,11) = [-1;-1;-1;-1;8;-1;-1;-1;-1];
  filters(:,12) = [0;0;0;0;1;0;0;0;0];
  numSimpleFilters = 6;
  options.NORMALIZEMASK = [1 1 1 1 1 0];
end
if(options.C1Ver003)
  options.includelaplacianfilter = 0;
  options.includegrayscalefilter = 0;
  options.bEstimateOrientations = 1;
  options.nOriEstimates = 9;
  options.INCLUDEBORDERS = 1;  
  options.bUseBlockwiseNormalization = 1;
  options.BlockwiseNormParams_yxCellsPerBlock = [2,2];
  options.BlockwiseNormParams_yxBlockSpacing =  [1,1];
  options.BlockwiseNormParams_Norm =  'sqrtL1';
  options.NORMALIZEMASK = [0 0];
  c1ScaleSS = [1:2:3];
  c1SpaceSS = [24];
  c1OL = 3; 
  fSiz = repmat(3,[1,4]);
  filters(:,1) = [0;1;0;0;0;0;0;-1;0];
  filters(:,2) = [0;0;0;-1;0;1;0;0;0];
  filters(:,3) = [0;1;0;0;0;0;0;-1;0];
  filters(:,4) = [0;0;0;-1;0;1;0;0;0];
  numSimpleFilters = 2;
end
if(options.C1Ver004)
  options.includelaplacianfilter = 0;
  options.includegrayscalefilter = 0;
  options.bEstimateOrientations = 1;
  options.nOriEstimates = 9;
  options.INCLUDEBORDERS = 1;  
  options.bUseBlockwiseNormalization = 1;
  options.BlockwiseNormParams_yxCellsPerBlock = [2,2];
  options.BlockwiseNormParams_yxBlockSpacing =  [1,1];
  options.BlockwiseNormParams_Norm =  'sqrtL1';
  options.NORMALIZEMASK = [0 0];
  options.DecimationScheme = 'kernel_sum_filter';
  c1ScaleSS = [1:2:3];
  c1SpaceSS = [24];
  c1OL = 3; 
  fSiz = repmat(3,[1,4]);
  filters(:,1) = [0;1;0;0;0;0;0;-1;0];
  filters(:,2) = [0;0;0;-1;0;1;0;0;0];
  filters(:,3) = [0;1;0;0;0;0;0;-1;0];
  filters(:,4) = [0;0;0;-1;0;1;0;0;0];
  numSimpleFilters = 2;
end
if(options.Triggs)
  options.includelaplacianfilter = 0;
  options.includegrayscalefilter = 0;
  options.bEstimateOrientations = 1;
  options.nOriEstimates = 9;
  options.INCLUDEBORDERS = 1;  
  options.bUseBlockwiseNormalization = 1;
  options.BlockwiseNormParams_yxCellsPerBlock = [2,2];
  options.BlockwiseNormParams_yxBlockSpacing =  [1,1];
  options.BlockwiseNormParams_Norm =  'sqrtL1';
  options.NORMALIZEMASK = [0 0];
  options.DecimationScheme = 'kernel_sum_filter';
  c1ScaleSS = [1:2:3];
  c1SpaceSS = [8];
  c1OL = 1; 
  fSiz = repmat(3,[1,4]);
  filters(:,1) = [0;1;0;0;0;0;0;-1;0];
  filters(:,2) = [0;0;0;-1;0;1;0;0;0];
  filters(:,3) = [0;1;0;0;0;0;0;-1;0];
  filters(:,4) = [0;0;0;-1;0;1;0;0;0];
  numSimpleFilters = 2;
end
if(options.C1Ver006)
  options.includelaplacianfilter = 0;
  options.includegrayscalefilter = 0;
  options.bEstimateOrientations = 0;
  options.nOriEstimates = 9;
  options.INCLUDEBORDERS = 1;  
  options.bUseBlockwiseNormalization = 0;
  options.BlockwiseNormParams_yxCellsPerBlock = [2,2];
  options.BlockwiseNormParams_yxBlockSpacing =  [1,1];
  options.BlockwiseNormParams_Norm =  'sqrtL1';
  options.DecimationScheme = 'kernel_sum_filter';
  c1ScaleSS = [1:2:3];
  c1SpaceSS = [24];
  c1OL = 3; 
  fSiz = repmat(3,[1,12]);
  filters(:,1) = [1;1;1;0;0;0;-1;-1;-1];
  filters(:,2) = [-1;0;1;-1;0;1;-1;0;1];
  filters(:,3) = [0;1;1;-1;0;1;-1;-1;0];
  filters(:,4) = [1;1;0;1;0;-1;0;-1;-1];
  filters(:,5) = [-1;-1;-1;-1;8;-1;-1;-1;-1];
  filters(:,6) = [0;0;0;0;1;0;0;0;0];
  filters(:,7) = [1;1;1;0;0;0;-1;-1;-1];
  filters(:,8) = [-1;0;1;-1;0;1;-1;0;1];
  filters(:,9) = [0;1;1;-1;0;1;-1;-1;0];
  filters(:,10) = [1;1;0;1;0;-1;0;-1;-1];
  filters(:,11) = [-1;-1;-1;-1;8;-1;-1;-1;-1];
  filters(:,12) = [0;0;0;0;1;0;0;0;0];
  numSimpleFilters = 6;
  options.NORMALIZEMASK = [1 1 1 1 1 0];
end
if(options.C1Ver007)
  options.includelaplacianfilter = 0;
  options.includegrayscalefilter = 0;
  options.bEstimateOrientations = 0;
  options.nOriEstimates = 9;
  options.INCLUDEBORDERS = 1;  
  options.bUseBlockwiseNormalization = 0;
  options.BlockwiseNormParams_yxCellsPerBlock = [2,2];
  options.BlockwiseNormParams_yxBlockSpacing =  [1,1];
  options.BlockwiseNormParams_Norm =  'sqrtL1';
  options.DecimationScheme = 'sumfilter';
  c1ScaleSS = [1:2:3];
  c1SpaceSS = [24];
  c1OL = 3; 
  fSiz = repmat(3,[1,12]);
  filters(:,1) = [1;1;1;0;0;0;-1;-1;-1];
  filters(:,2) = [-1;0;1;-1;0;1;-1;0;1];
  filters(:,3) = [0;1;1;-1;0;1;-1;-1;0];
  filters(:,4) = [1;1;0;1;0;-1;0;-1;-1];
  filters(:,5) = [-1;-1;-1;-1;8;-1;-1;-1;-1];
  filters(:,6) = [0;0;0;0;1;0;0;0;0];
  filters(:,7) = [1;1;1;0;0;0;-1;-1;-1];
  filters(:,8) = [-1;0;1;-1;0;1;-1;0;1];
  filters(:,9) = [0;1;1;-1;0;1;-1;-1;0];
  filters(:,10) = [1;1;0;1;0;-1;0;-1;-1];
  filters(:,11) = [-1;-1;-1;-1;8;-1;-1;-1;-1];
  filters(:,12) = [0;0;0;0;1;0;0;0;0];
  numSimpleFilters = 6;
  options.NORMALIZEMASK = [1 1 1 1 1 0];
end
if(options.C1Ver008)
  options.includelaplacianfilter = 0;
  options.includegrayscalefilter = 0;
  options.bEstimateOrientations = 0;
  options.nOriEstimates = 9;
  options.INCLUDEBORDERS = 1;  
  options.bUseBlockwiseNormalization = 1;
  options.BlockwiseNormParams_yxCellsPerBlock = [2,2];
  options.BlockwiseNormParams_yxBlockSpacing =  [1,1];
  options.BlockwiseNormParams_Norm =  'sqrtL1';
  options.NORMALIZEMASK = [0 0];
  options.DecimationScheme = 'kernel_sum_filter';
  c1ScaleSS = [1:2:3];
  c1SpaceSS = [24];
  c1OL = 3; 
  fSiz = repmat(3,[1,4]);
  filters(:,1) = [0;1;0;0;0;0;0;-1;0];
  filters(:,2) = [0;0;0;-1;0;1;0;0;0];
  filters(:,3) = [0;1;0;0;0;0;0;-1;0];
  filters(:,4) = [0;0;0;-1;0;1;0;0;0];
  numSimpleFilters = 2;
end
if(options.C1Ver009)
  options.includelaplacianfilter = 0;
  options.includegrayscalefilter = 0;
  options.bEstimateOrientations = 1;
  options.nOriEstimates = 9;
  options.INCLUDEBORDERS = 1;  
  options.bUseBlockwiseNormalization = 0;
  options.BlockwiseNormParams_yxCellsPerBlock = [2,2];
  options.BlockwiseNormParams_yxBlockSpacing =  [1,1];
  options.BlockwiseNormParams_Norm =  'sqrtL1';
  options.NORMALIZEMASK = [0 0];
  options.DecimationScheme = 'kernel_sum_filter';
  c1ScaleSS = [1:2:3];
  c1SpaceSS = [24];
  c1OL = 3; 
  fSiz = repmat(3,[1,4]);
  filters(:,1) = [0;1;0;0;0;0;0;-1;0];
  filters(:,2) = [0;0;0;-1;0;1;0;0;0];
  filters(:,3) = [0;1;0;0;0;0;0;-1;0];
  filters(:,4) = [0;0;0;-1;0;1;0;0;0];
  numSimpleFilters = 2;
end
if(options.C1Ver010)
  options.includelaplacianfilter = 0;
  options.includegrayscalefilter = 0;
  options.bEstimateOrientations = 1;
  options.nOriEstimates = 9;
  options.INCLUDEBORDERS = 1;  
  options.bUseBlockwiseNormalization = 0;
  options.BlockwiseNormParams_yxCellsPerBlock = [2,2];
  options.BlockwiseNormParams_yxBlockSpacing =  [1,1];
  options.BlockwiseNormParams_Norm =  'sqrtL1';
  options.NORMALIZEMASK = [0 0];
  options.DecimationScheme = 'maxfilter';
  c1ScaleSS = [1:2:3];
  c1SpaceSS = [24];
  c1OL = 3; 
  fSiz = repmat(3,[1,4]);
  filters(:,1) = [0;1;0;0;0;0;0;-1;0];
  filters(:,2) = [0;0;0;-1;0;1;0;0;0];
  filters(:,3) = [0;1;0;0;0;0;0;-1;0];
  filters(:,4) = [0;0;0;-1;0;1;0;0;0];
  numSimpleFilters = 2;
end
if(options.RegularC1)
  rot = [90 -45 0 45];
  RF_siz = [3,3];
  div = [4,4];
  [fSiz,filters,c1OL,numSimpleFilters] = init_gabor_alpha(rot, RF_siz, div,options);
end


