%% Get space-time images

foldMain = 'C:\Users\JBums\Desktop\MaiTai_InstallVideos\';
vidName ='IMG_7062.MOV';%dance.mp4'; % filename of video
startTime = 1; % time of first frame to be uploaded from video
numFrames = 200; % number of frames to process
maxXpix = 600; % number of pixels in x dimension, image will be resized
foldSave = 'C:\Users\JBums\Desktop\'; % folder for saving space-time videos
saveXtime = 0; % Save X-time video (1-yes, 0-no')
saveYtime = 0; % Save Y-time video (1-yes, 0-no')
saveXY = 0; % Save XY video (1-yes, 0-no')
[Sx,Sy,V]=genSpaceTimeVid(foldMain,vidName,startTime,numFrames,maxXpix,foldSave,saveXtime,saveYtime,saveXY);

%%% Playback outputs
[nPy,nPx,col,N] = size(V);

%% Y-time video playback %%%
figure;
for d =1:nPx
    
    imagesc(Sy(:,:,:,d)); axis image;
    ylabel('Y SPACE','FontSize',16);
    xlabel('TIME','FontSize',16);
    pause(0.01);
    clf
    
end

%% X-time video playback %%%
figure;
for d =1:nPy
    
    imagesc(Sx(:,:,:,d)); axis image off;
    ylabel('X SPACE','FontSize',16);
    xlabel('TIME','FontSize',16);
    pause(0.01);
    pause(0.01);
    clf
    
end

%% X-Y video playback %%%
figure;
for d =1:N
    
    imagesc(V(:,:,:,d)); axis image off;
    ylabel('X SPACE','FontSize',16);
    xlabel('TIME','FontSize',16);
    pause(0.01);
    pause(0.01);
    clf
    
end

%% create mega frame

figure;
nMx = nPx+N;
if nPy+nPy>nPx
    nMy = nPy+nPy;
else
    nMy = nPx;
end

% M = uint8(zeros(nMy,nMx,3,N));

leftover = round((nPx-N)/2);
leftover2 = round((nMy-nPx)/2);

if leftover2 <0
    leftover2=0;
end

clear M

for p =1:nPy

M((1:nPx)+leftover2,(1:N),:,p)=Sx(:,:,:,p);
M(1:nPy,(1:N)+N+leftover,:,p) = Sy(:,:,:,p);
M((1:nPy)+nPy,(1:nPx)+N,:,p) = V(:,:,:,p);

end

c=nPy+1;
for p =nPy:-1:1

M((1:nPx)+leftover2,(1:N),:,c)=Sx(:,:,:,p);
M(1:nPy,(1:N)+N+leftover,:,c) = Sy(:,:,:,p);
M((1:nPy)+nPy,(1:nPx)+N,:,c) = V(:,:,:,p);
c=c+1;
end



v = VideoWriter([foldSave,vidName,'_M']);
v.FrameRate = 35;
open(v);
writeVideo(v,M);
close(v);

%% MegaFrame video playback %%%
% figure;
% for d =1:nPy
%     
%     imagesc(M(:,:,:,d)); axis image off;
%     ylabel('X SPACE','FontSize',16);
%     xlabel('TIME','FontSize',16);
%     pause(0.01);
%     pause(0.01);
%     clf
%     
% end



% 
% figure;
% nMx = nPx+N;
% nMy = nPy+N;
% 
% p=100;
% 
% M = uint8(zeros(nMy,nMx,3,1));
% 
% leftover = round((nPx-N)/2);
% leftover2 = round((nMy-nPx)/2);
% 
% if leftover2 <0
%     leftover2=0;
% end
% 
% 
% for p =1:N
% 
% M((1:nPx)+leftover2,(1:N),:,1)=Sx(:,:,:,p);
% M(1:nPy,(1:N)+N+leftover,:,1) = Sy(:,:,:,p);
% M((1:nPy)+N,(1:nPx)+N,:,1) = V(:,:,:,p);
% 
% imagesc(M); axis image off;
% 
% pause(0.5);
% %clf
% 
% end
% 
% c = imrotate(Sx(:,:,:,1),90);
% c = c(26:end-25,:,:);
% 
% 
% close all;
% temp =uint8(zeros(450,800,3));
% for n =1:N
%     
%     c = imrotate(Sx(:,:,:,n),90);
%     c = c(26:end-25,:,:);
%     
%     temp(:,1:n,:) = c(:,1:n,:);
%     temp(:,n:end,:) = V(:,n:end,:,n);
%     
% 
%     imagesc(temp);
%     pause(0.1);
%     clf
% 
% end
