function [outPut,TheMovie]=immovieFolder(inFolder,varargin)
% IMMOVIEFOLDER records a movie from a group of image files in a
% specified folder and plays it. Image files must have the same size.
%
% [A,B]=immovieFolder(inFolder) the default, reads files with the extension
% (*.jpg) from the folder with name (inFolder), without any image resize,
% and plays the movie with 30 frames per second (fps).
% The output A is an M-by-N-by-P-by-K array, where K is the number of
% images in the specified folder. The image size is assumed to be [M,N,P].
% B is movie in a form of multiframe indexed image (a structure).
%
% immovieFolder(inFolder,s) reads files with the extension (*.jpg) from
% the folder with name (inFolder), with resizing parameter = s (s < 1). Use
% this when you get (Out of memory) error, and try s values 0.5 or less.
%
% immovieFolder(inFolder,s,'fileType') reads files with the extension
% (fileType) from the folder with name (inFolder), and using a resizing
% parameter = s.
%
% immovieFolder(inFolder,s,'fileType',fps) reads files with the extension
% (fileType) from the folder with name (inFolder),using a resizing
% parameter = s, and plays the movie with a rate fps per a second.
%
% This function calls folderFiles.m function.
% ------------------------------------------------------------------------
%
% NOTE:
% -----
% To simply read and view image files, use simpler and less memory
% consuming codes, as below:
%
% Example
% -------
% % Reading images from a folder and viewing them all.
% % This function calls folderFiles.m function.
% dirPath = 'D:\MyFolder\'; % The path of the folder
% fileNameExt = '*.jpg'; % The file extension
% [outFiles] = folderFiles(dirPath,fileNameExt);
% [a,b] = size(outFiles);
% addpath (dirPath);
% for i = 1:a
% fileIs = outFiles(i,:);
% I = imread(fileIs);
% imshow(I);
% pause(.01); % Simple fps. Use x (single) if you want fps = 1/x
% end
% rmpath(dirPath);
%
%
% See also MOVIE, IMPLAY, IMMOVIE, IMRESIZE, FOLDERFILES
% immovieFolder.m Function
% by Ahmed A. Selman, 23 March 2013
addpath (inFolder);
a=nargin;
if a > 4
error('myApp:argChk',...
'Too many input arguments. Maximum input arguments is 4.');
end
N=varargin;
if a==1
y='*jpg';
s1=1;
fps=30;
elseif a==2
s1=N{1};
y='*jpg';
fps=30;
elseif a == 3
s1=N{1};
y=N{2};
fps=30;
else
s1=N{1};
y=N{2};
fps=N{3};
end
if( ~ischar(inFolder) || ~ischar(y) );
error('MATLAB:catdirs:ArgNotString',...
'First and third arguments must be strings.');
end
C=folderFiles(inFolder,y);
[a0]=size(C);
a1=a0(1);
d3=zeros(ceil(512*s1),ceil(512*s1),3,a1);
for j=1:a1
p(j,:)=[inFolder,C(j,:)];
d1=imread(p(j,:));
d2=(imresize(d1,s1));
d3(:,:,:,j)=(d2);
end
outPut=uint8(d3);
TheMovie=immovie(outPut);
implay(TheMovie,fps);
% No need to remove path added since it is called in the folderFiles.m
% function, i.e., the very same folder name here is used and deleted above.
end