% AHE
%


function [ out ] = AHE(I, n, windowsize)

if ( nargin < 3 )
    windowsize = size(I,1) * 0.1;
end

r = floor((windowsize-1)/2);
minI = min(min(I));
maxI = max(max(I));
step = (maxI - minI) / (n - 1);

H = zeros(n,1);

[height width] = size(I);
out = zeros(height, width);
area = 0;

for j=1:height
    lowj = max(1, j - r);
    highj = min(height, j + r);
    
    for i=(-r+1):(width+r+1)
        % line to remove
        subi = i - r - 1;
        %line to add
        addi = i + r;
        
        % Remove pixels on the left.
        if ( subi >= 1 )
            for jj = lowj:highj
                idx = floor(I(jj, subi) / step) + 1;
                H(idx) = H(idx) - 1;
            end
            area = area - (highj - lowj + 1);
        end
        
        % Add pixels on the right.
        if ( addi <= width )
         
            for jj = lowj:highj
                idx = floor(I(jj, addi) / step) + 1;
                H(idx) = H(idx) + 1;
            end
            area = area + (highj - lowj + 1);
        end
        
        if ( i >= 1 && i <= width )
           
            
            idx = floor(I(j, i) / step) + 1;
            val = 0;
          
            for k=1:(idx-1)
                val = val + H(k);
            end
            
            val = (val / area) * (n - 1);
            
            out(j, i) = val;
        end
    end
    
end

out = out / n;

end

