function picturecomp(fid,figtitle,subtitle,filetype,M,IPS,minsize)
% PICTURECOMP automated picture compilation and labeling
%
% PICTURECOMP(fid,figtitle,filetype,M,IPS,minsize)
%       fid = directory pictures are contained in (required)
%       figtitle = title to be displayed on the side of the compiled figure
%       (string, optional)
%       filetype = what type of files are the pictures ({JPG}|TIF|..., optional)
%       M = Number of columns in the compiled figure (optional)
%       IPS = Inter-picture spacing in inches (optional)
%       minsize = Minimum file size to be included in the
%       compilaiton(optional)
%
%       Created by: Jon Newman (jnewman6 at gatech dot edu)
%       Location: The Georgia Institute of Technology
%       Created on: Nov 30, 2009
%       Last modified: Nov 30, 2009
%
%       Licensed under the GPL: http://www.gnu.org/licenses/gpl.txt

% check number and type of arguments
if( nargin < 7) || isempty(minsize)
    minsize = 10000; % min file size in bytes
end
if( nargin < 6) || isempty(sizeboost)
    IPS = 0.15; % Inter-picture space in cm
end
if( nargin < 5) || isempty(M)
    M = 3; % number of picture columns
end
if( nargin < 4) || isempty(filetype)
    filetype = 'JPG'; % file extension for picture (must be type readable by imread
end
if( nargin < 3) || isempty(subtitle)
    subtitle = 1; % file extension for picture (must be type readable by imread
end
if( nargin < 2) || isempty(figtitle)
    figtitle = datestr(date); % title is the today's date
end
if( nargin < 1)
    error('Function requires a valid directory for image files');
end

s = dir(fid);
filedat = struct2cell(s);

numfiles = size(filedat,2);

% are the files valid
valind = [];
for i = 1:numfiles
    fn = filedat{1,i};
    exlen = length(filetype);
    if (length(fn) > exlen)
        if (filedat{3,i} > minsize && filedat{4,i} == 0 && sum(fn(end-exlen+1:end) == filetype) == exlen)
            valind = [valind i];
        end
    end
end
numgoodfiles = length(valind);

% Clean up file path
if fid(end) ~= '\'
    fid = [fid '\'];
end

% get the sizes of the images that are going into the compilation
imsizes = zeros(numgoodfiles,2);
for j = 1:numgoodfiles
    ind = valind(j);
    fileloc = strcat([fid filedat{1,ind}]);
    [X,map1]=imread(fileloc);
    temp = size(X);
    imsizes(j,:) = temp(1:2);
end

% parameters for calculating image size
cellsize = max(imsizes);
N = ceil(numgoodfiles/M);
h = 3*cellsize(1)/cellsize(2);
w = 3;

% Create compound image
fighand = figure();
set(fighand,'units','inches','visible','off');
set(fighand,'position',[0 0 (M+1)*IPS+M*w+1 (N+1)*IPS+N*h],'PaperPosition',[0 0 (M+1)*IPS+M*w+1 (N+1)*IPS+N*h],'papersize',[(M+1)*IPS+M*w+1 (N+1)*IPS+M*h]);
p = [0 0 (M+1)*IPS+M*w+1 (N+1)*IPS+N*h];

axes('units','inches','position', p,'visible','off');
text('Position',[0.5,((N+1)*IPS+N*h)/2,0],'String',figtitle,...
    'Interpreter','none','color',[0 0 0],'FontSize',30,'HorizontalAlignment','center',...
    'units','inches','Rotation',90);

k = 1;
for i = N:-1:1
    for j = 1:M
        ind = valind(k);
        fn = filedat{1,ind};
        fileloc = strcat([fid fn]);
        p = [j*IPS+(j-1)*w+1 i*IPS+(i-1)*h w h];
        axes('units','inches','position', p);
        [X1,map1]=imread(fileloc,'jpg');
        imshow(X1,map1,'border','tight');
        if subtitle
            text('Position',[w-0.05,h-0.2,0],'String',fn(1:end-exlen-1),...
                'Interpreter','none','color',[0 0 1],'FontSize',18,'HorizontalAlignment','right',...
                'units','inches');
        end
        k = k+1;
        if k > numgoodfiles
            break
        end
    end
end

% save the compiled figure
saveas(fighand,strcat([fid figtitle '_compilation']),'jpg');

end