function [Crsum, Cgsum, Cbsum] = comparasum(kbeg,kend,skip);

if nargout ~=3
  error("comparasum: must have 3 output arguments (Crsum, Cgsum, Cbsum)")
end%if nargout

if nargin < 2
  error("comparasum: must have 2 input arguments kbeg and kend")
end%if nargin

if nargin == 2
  skip=1
end%if

Crsum=zeros(256,256);
Cgsum=zeros(256,256);
Cbsum=zeros(256,256);
%for k=158:162-skip
for k=kbeg:kend-skip
  eval(sprintf("system(""comparagram r%03d.jpg r%03d.jpg"")",k,k+skip));
  %eval(sprintf("system(""mv comparagram.u32.rgb.raw C%03d_%03d.u32.rgb.raw"")",k,k+1);
  fd = fopen("comparagram.u32.rgb.raw","r");
  Cr=fread(fd,[256,256],"uint").';  % transpose
  Cb=fread(fd,[256,256],"uint").';
  Cg=fread(fd,[256,256],"uint").';
  Crsum=Crsum+Cr;
  Cgsum=Cgsum+Cg;
  Cbsum=Cbsum+Cb;
  %tvs(sqrt(Cr),sqrt(Cb),sqrt(Cg));
  tvs(sqrt(Crsum),sqrt(Cbsum),sqrt(Cgsum));
end%for k

