function ConvertToC1(oldfn,newfn)
%function ConvertToC1(oldfn,newfn)
%
% reads the data as 81x81 columns in one matrix 
% returns a new matrix in C1 format

options.C1VerRegularC1 = 1;
options.OriginalSize = [81 81];
load(oldfn,'X');%-->X
saMat = SSDB_C1Variants(X,options);
X = saMat.C1X;
save(newfn,'X');












function saMat = SSDB_C1Variants(X,options);
%implements all the c1 varients we did for INRIA, including triggs, in a format conducive
%to the SSDB examples


HelpTextInfo = 'PROCEDURE_INRIAToC1_stanedit';
d.C1Ver001 = 0;
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.C1VerStanFavorite = 0;
d.C1VerTriggs = 0;
d.C1VerStanFavorite2 = 0;
d.C1VerRegularC1 = 0;
d.C1VerRegularC1OnlyFirstBand = 0;

d.RegularC1 = 0;
d.Triggs = 0;
d.DoAreaOfSegmentedImage = 0;
d.OriginalSize = [128 128];

if(nargin < 2),options = [];,end
options = ResolveMissingOptions(options,d);
[saMat.C1X] = GrayX2C1X(X,[options.OriginalSize],options);


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);
  if(options.DoAreaOfSegmentedImage)
    stim = SegmentationImToAreaIm(stim);
    stim = (stim - min(stim(:))) / (max(stim(:)) - min(stim(:)));
    stim = histeq(stim);
  end
  [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.C1VerRegularC1)
  rot = [90 -45 0 45];
  c1ScaleSS = [1:2:18];
  RF_siz    = [7:2:39];
  c1SpaceSS = [8:2:22];
  div = [4:-.05:3.2];
  [fSiz,filters,c1OL,numSimpleFilters] = init_gabor(rot, RF_siz, div);
end
if(options.C1VerRegularC1OnlyFirstBand)
  rot = [90 -45 0 45];
  c1ScaleSS = [1:2:3];
  RF_siz    = [7:2:9];
  c1SpaceSS = [8:2:10];
  div = [4:-.05:3.95];
  [fSiz,filters,c1OL,numSimpleFilters] = init_gabor(rot, RF_siz, div);
end

