function pgm_project_display ( filelist, n, pgm_filename, png_filename )

%*****************************************************************************80
%
%% pgm_project_display() displays projections of a PGM image against SVD vectors.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    25 December 2018
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string FILELIST, the name of a file, which lists the PGM files
%    to be packed.
%
%    integer N, the number of SVD vectors to extract.
%
%    string PGM_FILENAME, the name of a file containing a binary
%    PGM image to be projected onto the SVD components.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'pgm_project_display():\n' );
  fprintf ( 1, '  Packing files from the list "%s"\n', filelist );
  fprintf ( 1, '  Extracting average and %d SVD vectors\n', n );
  fprintf ( 1, '  Projecting PGM data from file "%s"\n', pgm_filename );
%
%  1: Create average and SVD vectors.
%
  [ pgms, w, h, pgm_num ] = pgms_pack ( filelist );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Packed array contains %d PGM files\n', pgm_num );
  fprintf ( 1, '  Each PGM has width %d and height %d\n', w, h );
%
%  Compute N principal component vectors.
%
  [ pc_vec, pc_val, av_vec ] = svd_vectors ( pgms, n );
%
%  2: Choose a face from the original list, and reconstruct it by projection.
%
  pgm = pgm_read ( pgm_filename );
%
%  Display the face.
%
  figure ( 1 );
  clf ( );
  subplot ( 3, 4, 1 );
  image ( pgm );
  colormap ( gray(256) );
  daspect ( [1,1,1] );
% title ( 'Original', 'Fontsize', 16 );
  set ( gca, 'Xtick', [], 'Ytick', [] );
%
%  Create a version of the face.
%
  pgm_vec = reshape ( pgm', w * h, 1 );
%
%  Compute projections.
%
  c = zeros ( n, 1 );
  for j = 1 : n
    c(j) = pc_vec(:,j)' * ( pgm_vec - av_vec );
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Projections of image onto vectors:\n' );
  fprintf ( 1, '\n' );
  for j = 1 : n
    fprintf ( 1, '  %d  %g\n', j, c(j) );
  end
%
%  Create projected faces.
%
  proj_vec = av_vec;
%
%  Display projected face and report norms of difference.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Vectors  |error|\n' );
  fprintf ( 1, '\n' );

  for j = 0 : n

    subplot (3, 4, j + 2 );
    if ( 1 <= j )
      proj_vec = proj_vec + pc_vec(:,j) * c(j);
    end
    normd = norm ( proj_vec - pgm_vec );
    fprintf ( 1, '  %d  %g\n', j, normd );
    I = reshape ( proj_vec, w, h );
    I = I';

    imagesc ( I );
    colormap ( gray(256) );
    daspect ( [1,1,1] );
    label = sprintf ( '%d vectors\n', j );
%   title ( label, 'Fontsize', 16 );
    set ( gca, 'Xtick', [], 'Ytick', [] );
    
  end

% sgtitle ( 'Face | Average | Approx 1 : 10', 'Fontsize', 16 );
  print ( '-dpng', png_filename );

  return
end
