%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Script to collect data files to be used for training and testing actual classes
%
% Use this in conjunction with collect_tests and collect_classes
%
% Author: Marc'Aurelio Ranzato
% Date: Dec 2003

%Modified by doliver@mbari.org on December 5, 2004
%Modified by dcline@mbari.org on April 19, 2005 - created from
%collectclasses - factored out functions common to 
%both collectclasses and collect_tests

function [classfilemetadata,classdata] = collect(dbroot, dirct_or_filelist, classname )

%resolution and data files
resolfiles={};
datafiles={};

% check if directory exists to save output to, if not create it
if(isdir(dbroot) == 0)
    mkdir(dbroot);   
end

ii=0;
data = [];
classfilemetadata =[];

%*added if is a dir, then extract the jpeg or ppm images,
%*modified store the file names for all jpeg or ppm images       
fprintf(1,'Getting file names from directory...\n');
if(~iscell(dirct_or_filelist) & isdir(dirct_or_filelist))
    s1 = dir([dirct_or_filelist '/*.ppm']);
    s2 = dir([dirct_or_filelist '/*.jpg']);    
        
    %*added - concatenate together
    s = [s1 s2];
    sz = length(s);
else
    % otherwise assume is a list of liles names, so copy to an array
    s = dirct_or_filelist;
    sz = size(s,2);
end
        
fprintf(1,'Computing invariants for %s files...',classname);
    
%*added comment now loop on this struct array of images
while ( ii < sz ) 
    
    %*added  - get filename from struct array
    if(~iscell(dirct_or_filelist) & isdir(dirct_or_filelist))
        filename = [dirct_or_filelist '/' s(ii+1).name];                                  
    else
        filename = s{1,ii+1};
    end
        
    % COMPUTE INVARIANTS
    [data, imsize] = compute_invariant(filename);               
        
    % store invariant data 
    classdata(ii+1,:) = data;    
        
    %modified store name and size of current file
    if(~iscell(dirct_or_filelist) & isdir(dirct_or_filelist))
        classfilemetadata{ii+1,1} = s(ii+1).name;
    else
        classfilemetadata{ii+1,1} = s{1,ii+1};
    end
    
    classfilemetadata{ii+1,2} = imsize;
    ii = ii + 1;                
end
    
fprintf(1, '\n');

if(sz > 1)
    %modified - store the resolution and data from linear 3d application
    str = [dbroot '/' classname '_data_collection_avljNL3_cl_pcsnew' '.mat'];
    fprintf(1, 'Saving %s\n', str);
    save(str,'classdata');           
    
    %modified - store name of class files from the directory
    str = [dbroot '/' classname '_image_file_metadata' '.mat'];
    fprintf(1, 'Saving %s\n', str);
    save(str,'classfilemetadata');
end

end
