function [caS2Centroid,SourceLocations] = GrayscaleS2Centroids(yxSizes, AvailableIndicies,savename);
%function [caS2Centroid,SourceLocations] = GrayscaleS2Centroids(yxSizes, AvailableIndicies,savename);
%
%intent:  Mimic the "NaturalS2Centroids" code, but instead, crop out grayscale patches.  These will be
%used in a C2 patchbased mockup, where orientation filters are not used.
%
%
%pseudocode:  for each of the yxsizes, choose a random AvailableIndex and a random location
%  within the image associated with that index.  Extract a template (centroid) and record

HelpTextInfo = GetHelpTextInfo;
nS2 = size(yxSizes,1);
seedrand;
load CAfilelist_FilterSS;%-->CAfilelist

tic
for i =1:nS2
   fprintf('%d of %d.  %d seconds elapsed\r',i,nS2,round(toc));
   p = randperm(length(AvailableIndicies));
   SSIdx = AvailableIndicies(p(1));
   SourceLocations(i).SSIdx = SSIdx;
   OriginalImage = imread(CAfilelist{SSIdx}.imagename);
   OriginalImage = im2double(imresize(rgb2gray(OriginalImage),[240 320],'bilinear'));
   s = yxSizes(i,:);
   SourceLocations(i).size = s;
   AvailPos = [(size(OriginalImage,1) - s(1) + 1),(size(OriginalImage,2) - s(2) + 1)];
   yxTopLeft = GetRandTopLeft(AvailPos);
   SourceLocations(i).yxTopLeft = yxTopLeft;
   caS2Centroid{i} = CropC1(OriginalImage, SourceLocations(i));
end
fprintf('\n');
if(nargin > 2);
  save(savename, 'caS2Centroid','SourceLocations','HelpTextInfo');
end

function yxTopLeft = GetRandTopLeft(AvailPos);
seedrand, r = rand(1,2);
yxTopLeft = floor(r .* AvailPos) + 1;

function S2c = CropC1(C1Img, sSource);
a = (1:sSource.size(1)) + sSource.yxTopLeft(1) - 1;
b = (1:sSource.size(2)) + sSource.yxTopLeft(2) - 1;
S2c = C1Img(a,b,:);
