function gray = gray_salt_and_pepper ( gray, level )

%*****************************************************************************80
%
%% gray_salt_and_pepper() adds salt-and-pepper noise to a grayscale image.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    17 March 2017
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    uint8 GRAY(:,:), the image.
%
%    real LEVEL, the level of noise to add, between 0.0 (none)
%    and 1.0 (all).
%
%  Output:
%
%    uint8 GRAY(:,:), the image with added noise.  A fraction of
%    about LEVEL of the pixels have been reset to a random value.
%
  [ m, n ] = size ( gray );

  r = rand ( m, n );

  i = find ( r <= level / 2.0 );
  gray ( i ) = 0;

  i = find ( 1.0 - level / 2.0 <= r );
  gray ( i ) = 255;

  return
end
