function imc= AumCont(im,r) 
% - This function performs a local contrast enhancement by scanning across the entire image.
% - The variable "r" determines the window radius used to generate the local contrast enhancement.
%
% INPUT PARAMETERS:
% im  - Input image (2D matrix).
% r   - Half-window radius (integer).
%
% OUTPUT PARAMETERS:
% imc - Contrast-normalized image within the range [0, 1].

    h=size(im);
    imc=zeros(h(1),h(2));
    for i=1:h(1)
        %Traverse rows, clamp vertical window to image bounds
        if i-r<=0
            minY=1;
            maxY=i+r;
        elseif i+r>=h(1)
            minY=i-r;
            maxY=h(1);
        else
            minY=i-r;
            maxY=i+r;
        end
        for j=1:h(2)
            %Traverse columns, clamp horizontal window to image bounds
            if j-r<=0
                minX=1;
                maxX=j+r;
            elseif j+r>=h(2)
                minX=j-r;
                maxX=h(2);
            else
                minX=j-r;
                maxX=j+r;
            end
            iref=im(minY:maxY,minX:maxX);
            %Local max and min within the neighborhood
            mx=max(iref,[],'all');
            mn=min(iref,[],'all');
            %Avoid division by zero or negative scale; return zero for flat regions
            if mx<=0
                imc(i,j)=0;
            elseif mx==mn
                imc(i,j)=0;
            else
                imc(i,j)=(im(i,j)-mn)/(mx-mn);
            end
        end
    end
end