function A = haar_step_2d ( A )

%*****************************************************************************80
%
%% HAAR_STEP_2D carries out one step of the Haar transform on a 2D array.
%
%  Discussion:
%
%    Given an M by N array, this function applies one step of the Haar
%    transform, computing the sums and differences of columns, and then
%    sums and differences of rows, returning an array of the same size
%    as the input array.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    15 March 2011
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real A(M,N), the array to be processed.
%
%    Output, real A(M,N), the processed array.
%
%
%  Apply Haar to columns, and concatenate rows using semicolon.
%
  m = size ( A, 1 );

  if ( mod ( m, 2 ) == 0 )
    m1 = m;
  else
    m1 = m - 1;
  end

  S = ( A(1:2:m1,:) + A(2:2:m1,:) ) / sqrt ( 2.0 );
  D = ( A(1:2:m1,:) - A(2:2:m1,:) ) / sqrt ( 2.0 );

  if ( mod ( m, 2 ) == 0 )
    A = [ S; ...
          D ];
  else
    A = [ S; ...
          A(m,:); ...
          D ];
  end
%
%  Apply Haar to rows, concatenate columns using comma.
%
  n = size ( A, 2 );

  if ( mod ( n, 2 ) == 0 )
    n1 = n;
  else
    n1 = n - 1;
  end

  S = ( A(:,1:2:n1) + A(:,2:2:n1) ) / sqrt ( 2.0 );
  D = ( A(:,1:2:n1) - A(:,2:2:n1) ) / sqrt ( 2.0 );

  if ( mod ( n, 2 ) == 0 )
    A = [ S, D ];
  else
    A = [ S, A(:,n), D ];
  end

  return
end
