function [ pgms, w, h, pgm_num ] = pgms_pack ( filelist )

%*****************************************************************************80
%
%% PGMS_PACK packs a set of PGM images into a single file.
%
%  Modified:
%
%    29 November 2018
%
%  Modifier:
%
%    John Burkardt
%
%  Author:
%
%    Matthew Dailey, 2000
%
%  Parameters:
%
%    Input, string FILELIST, the name of a file, which lists the PGM files
%    to be packed.
%
%    Output, integer PGMS(w*h,pgm_num), contains the packed information.
%
%    Output, integer W, H, the width and height of a single image.
%
%    Output, integer PGM_NUM, the number of PGM's that were packed.

%
%  Open the list file.
%
  pgm_num = line_count ( filelist );

  fid = fopen ( filelist, 'r' );
  if ( fid < 0 )
    error ( 'pgms_pack - Cannot open list file.' )
  end
%
%  Get the images one at a time.
%
  for j = 1 : pgm_num
%
%  Read the next line of the list file.
%
    pgm_name = fgetl ( fid );

    if ( ~ ischar ( pgm_name ) )
      break;
    end

    fprintf ( '  %d  %s\n', j, pgm_name );

    pgm = pgm_read ( pgm_name );
%
%  Notice the size of the first image.
%  Use it to allocate memory.
%
    if ( j == 1 )
      w = size ( pgm, 2 );
      h = size ( pgm, 1 );
      pgms = zeros ( w * h, pgm_num );
    end
%
%  Transpose the image.
%
    pgm = pgm';
%
%  Insert image into column j.
%
    pgms(1:w*h,j) = reshape ( pgm, w * h, 1 );

  end

  fclose ( fid );

  return
end

