function img = BuildClosureTestImage(options)
% built off of Build continuity test image, choses a center and a size
% and draws a circle. 
if(nargin < 1)
  options = [];
end

d.fPurity = .5;
d.size = 81;
d.padsize = 0;
d.bRifle = 1;

options=ResolveMissingOptions(options,d);
sz = (options.size - 2*options.padsize);
if(options.bRifle), sz = floor(sz/2); end
hs = floor(sz / 2);
% pick a point on the continuous line
centerDist = hs * 2;
while(centerDist > (hs/2)) 
   % The center of the circle must be within hs/2 of the center of the image
   lp_yx = floor(rand(1,2) * sz);
   centerDist = norm(hs - lp_yx);
end
% pick radius: the radius will be 1/16 -> 1/2 the length of the image
radius = 0;
while ((radius < hs/4) || (radius > hs))
   radius = rand * hs;
end
% fill arc (top and bottom half)
% r^2 = (i_x - cx)^2 + (i_y - c_y)^2
cx = lp_yx(2);
cy = lp_yx(1);
contimg = zeros(sz);
for i_x = 1:sz
    updown = radius^2 - (i_x - cx)^2;
    if(updown < 0)
      continue;
    else
       % keyboard
       i_y1 = round(cy + sqrt(updown));
       i_y2 = round(cy - sqrt(updown));
       if ((i_y1 >= 1) && (i_y1 <= sz))
         contimg(i_y1,i_x) = 1;
       end
       if ((i_y2 >= 1) && (i_y2 <= sz))
         contimg(i_y2,i_x) = 1;
       end
  end  
end
for i_y = 1:sz
    leftright = radius^2 - (i_y - cy)^2;
    if(leftright < 0)
      continue;
    else
       % keyboard
       i_x1 = round(cx + sqrt(leftright));
       i_x2 = round(cx - sqrt(leftright));
       if ((i_x1 >= 1) && (i_x1 <= sz))
         contimg(i_y,i_x1) = 1;
       end
       if ((i_x2 >= 1) && (i_x2 <= sz))
         contimg(i_y,i_x2) = 1;
       end
  end  
end
img = contimg;
img = img(:);
% add noize
nPip = sum(img);
n2move = floor(nPip * (1-options.fPurity));
f = find(img);
p = randperm(length(f));
f = f(p);
for i = 1:n2move
  f(i) = ceil(rand * sz * sz);
end
img = zeros(sz);
img(f) = 1;
if(options.bRifle)
  img = rifle(img);
  if(mod(options.size,2))
     img = padimage(img,[0 0 1 1],0);
  end
end  
img = padimage(img,options.padsize,0);
  
  

function i2 = rifle(i1)
i2 = zeros(2*size(i1));
i2(2:2:end,2:2:end) = i1;

