function bwhist = bwest(blksz, image, percnt1, percnt2)
% bwhist = bwest(blksz, image, percnt1, percnt2)
[m n] = size(image);
brem = rem(m,blksz); bwhist = zeros(n,1);
bnum = fix(m/blksz);
for b = 1:bnum   % do full size blocks
  rmin = (b-1)*blksz + 1; rmax = rmin + blksz - 1;
  glmax = max(max(image(rmin:rmax,:)));
  thresh = glmax*percnt1;
  for r = rmin:rmax
    for c = 1:n
      val = image(r,c);
      if val > thresh    % examine above threshold pixels
        c1 = c-1; high = 1;
        while c1>1 & high==1    % look left till boundary or low pixels
          cmin = max(1,c1-1);
          cmax = min(n,c1+1);
          if max([image(r,cmin),image(r,c1),image(r,cmax)]) < val*percnt2
            high = 0;
          else
            c1 = c1-1;
          end
        end
        cleft = c1;
        c1 = c+1; high = 1;
        while c1<n & high==1   % look right
          cmin = c1-1;
          cmax = min(n,c1+1);
          if max([image(r,cmin),image(r,c1),image(r,cmax)]) < val*percnt2
            high = 0;
          else
            c1 = c1+1;
          end
        end
        cright = c1;
        cdif = cright-cleft;
        if cdif>0
          bwhist(cdif) = bwhist(cdif)+1;
        end
      end
    end
  end
end
