function Corrected_Image = jas (af, METHOD)
%% vyber metody
METHOD = 'averaging'; %default method

Y = double(af);

%% normalizace
% The original image is normalised for the range from 1 to 256;
if max(max(Y)) <= 1
   Y = 255*Y * 1;
end   
[m,n] = size(Y);
max_Y = 256;
middle_Y = 128; 
N = 51;
% N = 3
%% filtrace
if METHOD(1:6) == 'median' 
   Z = medfilt2(Y,[N N]);
else     
   h = fspecial('average',N);
   Z = filter2(h,Y);
end


%Retina
null_pts = find(Z == 0);
Z(null_pts) = ones(size(null_pts));
max_Z = max(max(Z));
coeff = max_Z ./ Z; 
R = (Y - Z) .* coeff + middle_Y;
%% vysl
i = find(R > 256);
j = find(R < 0);
R(i) = max_Y * ones(size(i));
%R(j) = max_Y * zeros(size(j));
R(j) = ones(size(j));
Corrected_Image = R;
end