function [c2_global] = C2_GlobalMaxInBox(SSIdxs, BBoxs, C2Root)
%function [c2_global] = C2_GlobalMaxInBox(SSIdxs, BBoxs, C2Root)
%
%input:  the root of the c2 data (in JPGWrite3DMatrix format);
%        a list of object boxs = {ssidx, bbox}
%
%output: the maximum c2 response within each box.

nBoxs = size(BBoxs,1);
[n_c2Features,SSResolution] = GetNC2Features(C2Root);
c2_global = zeros(n_c2Features, nBoxs);
t = cputime;
n=0;
previdx = -1;
for i = 1:nBoxs
   n = n+1;
   e = cputime;
   fprintf('%d of %d.  %d mins elapsed:  ',n,nBoxs,round((e-t)/60));
   if(SSIdxs(i) ~= previdx)
      A = JPEGRead3DMatrix(fullfile(C2Root, sprintf('%.5d',SSIdxs(i))),'C2_Data');
      previdx = SSIdxs(i);
   end
   bbox = BBoxs(i,:);
   bbox = round(CoordinateRespace(bbox,[960 1280], SSResolution,'bbox'));
   z = imcrop_pad(A,bbox,'symmetric');
   c2_global(:,i) = permute(max(max(z,[],1),[],2),[3,1,2]);
   e = cputime;
   cpe = ((e-t) / n)/60;
   fprintf('Cost Per Each = %f min:  ', cpe);
   fprintf('Time Remaining = %f min:  \r', cpe * (nBoxs - n));
end
fprintf('\n');

function [n_c2Features,SSResolution] = GetNC2Features(C2Root);
A = JPEGRead3DMatrix(fullfile(C2Root, sprintf('%.5d',1)),'C2_Data');
s = size(A);
n_c2Features = s(3);
SSResolution = s([1,2]);
