% light wavelength
lambda=670e-9; % 670 nm - red light

% set up mask (source image)

% two vertical slits  mask
img_source=imread('mask_WM.png'); %reading mask from a file
% by default image is read as mask for Red, Green, and Blue colors
% but we need only brightness part
img_source=im2bw(img_source);
[Ny_s,Nx_s]=size(img_source); 
xpos_s=linspace( -1.4e-2, 1.4e-2, Nx_s);
ypos_s=linspace( -0.6e-2, 0.6e-2, Ny_s);

% set up target image
Nx_t=693;  % resolution at the target can be decreased by factor of 2
Ny_t=300;
img_t=zeros(Ny_t,Nx_t); 
xpos_t=linspace( -1.5e-2, 1.5e-2, Nx_t);
ypos_t=linspace( -0.7e-2, 0.7e-2, Ny_t);
z_t=1.1e0;


% generating image of the mask accounting the diffraction
tic; % start timer
% lets fill image part by part since it is boring to stare to non updated screen
for iy=1:Ny_t
img_t(iy,:)=diffracted_image_at_target(img_source, xpos_s, ypos_s, img_t(iy,:), xpos_t, ypos_t(iy), z_t, lambda);
show_diffraction(img_source, xpos_s, ypos_s, img_t, xpos_t, ypos_t);
iy/Ny_t % this shows percentage done
drawnow;
end
% above 'for' loop can be replaced with a single one liner but there is no intermidiate
% update of target image
% img_t=diffracted_image_at_target(img_source, xpos_s, ypos_s, img_t, xpos_t, ypos_t, z_t, lambda);
toc % show evaluation time


% plot source and target images
show_diffraction(img_source, xpos_s, ypos_s, img_t, xpos_t, ypos_t);

% before we save we need to renormalizes the image
img_n=img_t/max(img_t(:));
imwrite(img_n, 'image_wm_diffracted.png');

% calculation with above numbers too 23000 seconds (6 hours 23 minutes)
% of my 6 cores AMD Phenom II X6 1100T processor

