%==================================================================
%
%	MoviecopyFile.m
%	hs
%
%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.                                                                      
%
%	
%	Function definition

	function [InFileMat] = OrderfMRI2


%********************************************************
%************* Read  Line Numbers for File Info  ********************
XX=1; % Anzahl der Bilder
SaveInd=0;
PixNo=[];
% BMPNo=load(deblank('HD:future:Masking:RauschMatlab:batchNum.txt'));% Tray Info-File
% BMPNo;

hFig=figure('Units','Pixel','Position',[400 300 640 480]);
hAxes=axes('Units','Pixel','Position',[0 0 640 480]);
Lim=100;
for i=1:XX
	i=i
% 	row=BMPNo(i);
% 	FileArrayBMP(i)=row;
% 	num=num2str(row);
	ImFilePath=['Paul:0035'];	%Strcat('HD:future:Masking:100:0',num,'.JPG')
	ImFilePath
	OrigImMat=imread(ImFilePath,'jpg');
	OrigRandImMat=OrigImMat;
	[pHor,qHor,rHor]=size(OrigImMat)
	BlackMaskHor=squeeze(sum(sum(OrigImMat,3),1));
%  	PlotTmp=BlackMaskHor;
	Tmp=find(BlackMaskHor>Lim);
	MinHor=min(Tmp)
	MaxHor=max(Tmp)
	ImMat=OrigImMat(:,MinHor:MaxHor,:);
	[pVert,qVert,rVert]=size(ImMat)
	BlackMaskVert=squeeze(sum(sum(ImMat,3),2));
	Tmp=find(BlackMaskVert>Lim);
	MinVert=min(Tmp);
	MaxVert=max(Tmp);
	ImMat=ImMat(MinVert:MaxVert,:,:);
	[m,n,o]=size(ImMat)
	RandImMat=[];
	RandImMat=ImMat;

	ColRandMat=zeros(m,n);
	RowRandMat=zeros(m,n);
	RandMat=zeros(3,m*n);
		Fak=.99;

	if Fak < 1	
		for mInd=1:m
			RandMat(1,(mInd-1)*n+1:mInd*n)=randperm(n);
		end
		for nInd=1:n
			RandMat(2,(nInd-1)*m+1:nInd*m)=randperm(m);
		end
		RandMat(3,:)=randperm(m*n);
		NRandPix=Fak*m*n;
		k=0;
		for i=1:m
			for j=1:n
				k=k+1;
				if RandMat(3,k)<=NRandPix
					RandImMat(i,j,:)=ImMat(RandMat(2,k),RandMat(1,k),:);
				end
			end
		end
		OrigRandImMat(MinVert:MaxVert,MinHor:MaxHor,:)=RandImMat;
	else
		OrigRandImMat=OrigImMat;
	end
% 	BlackMaskHor=squeeze(sum(sum(OrigRandImMat,3),1));
% figure
% plot(PlotTmp,'r')
% hold on
% plot(BlackMaskHor)
% pause
% 	Tmp=find(BlackMaskHor>100);
% 	MinHor=min(Tmp)
% 	MaxHor=max(Tmp)
% 	BlackMaskVert=squeeze(sum(sum(OrigRandImMat,3),2));
% 	Tmp=find(BlackMaskVert>100);
% 	Minvert=min(Tmp)
% 	MaxVert=max(Tmp)
	image(OrigRandImMat); 
	axis('off');
	hold off
	
	SaveInd=SaveInd+1;
	if SaveInd<10
		IndStr=['000' int2str(SaveInd)];
	elseif SaveInd>9 & SaveInd <100
		IndStr=['00' int2str(SaveInd)];
	elseif SaveInd>99 & SaveInd <1000
		IndStr=['0' int2str(SaveInd)];
	elseif SaveInd>999 & SaveInd <10000
		IndStr=[int2str(SaveInd)];
	end
	Print2FilePath=['Paul:',int2str(SaveInd),'.',int2Str(Fak.*10),'.jpg'];
	imwrite(OrigRandImMat,Print2FilePath,'jpg')
	pause(.1)
end
return;
