% array oprations

clear variables; %all;
close all;

% size of array - given as the half length (radius) of each side
Sx=27;
Sy=54;
% create the array
ArraySize=[2*Sx+1,2*Sy+1];
% run long enough to reach any element during propagation
Time=(27+54)*2; %this is the perimeter, it is enough to distribute an 
% element around the perimeter
% we need the diagonal to ensure that the information can reach the corners
% half the diagonal if we read out the element in the middle
deltat=1;

Array=round(rand(ArraySize)*255);

% create an other array representing the processors
Processors=ones(ArraySize)*255;
for a=1:Sx
	for b=1:Sy
		Processors(2*a,2*b)=0;
	end
end
%show processors
figure
image(Processors);




figure
h = imagesc(Array, [0, 255]); %set(h,'erasemode','xor'); 
colormap gray;
%caxis([0 255])

for t=1:Time
	% this is just a simulation; so sequential processing will happen
    % permutating the order of execution of the processors:
	XOrder=randperm(Sx);
	YOrder=randperm(Sy);
    % iterate through all the cells
	for a=1:Sx
		for b=1:Sy
			XIndex=XOrder(a);
			YIndex=YOrder(b);
			% get neighborhood
			LocalMem = Array((2*XIndex-1):(2*XIndex+1),(2*YIndex-1):(2*YIndex+1));
            % execute the operation on all elements:
			LocalMem = OperationMax(LocalMem,t);
			Array((2*XIndex-1):(2*XIndex+1),(2*YIndex-1):(2*YIndex+1)) = LocalMem;
		end
	end
	%display the result
	title(sprintf('%d(/%d). iteracio',t,Time)) 
	pause(deltat)
	imagesc(Array, [0, 255]);
    %set(h,'cdata',Array);
	%colormap gray;
end
