function [rec, obj1, obj2]=extrapolate_ksvd(Im , extrap , direction, patch)
% Run synthetic inpainting+denoising tests, 
% using local patch-based processing with the DCT overcomplete dictionary
% missingPixelsRatio is the part of the pixels that are removed 
%                       (0.2 means 20% of the pixels are removed)
% noiseSig is the standard deviation of the noise

% Read image
trueIm = double(Im);
sizeIm = size(Im);

% Create mask for extrapolation
thick = extrap;
Mask = ones(sizeIm);

% Create noisy image
% noisyIm = trueIm + noiseSig * randn(size(trueIm));
% noisyIm(noisyIm < 0) = 0; noisyIm(noisyIm > 255) = 0;
noisyIm = trueIm;
% 
switch direction
    case 1 
        for i=1:thick
            Mask(:,sizeIm(2)-i)=0;
        end
    case 2 
        for i=1:thick
             Mask(sizeIm(1)-i,:)=0;
        end
    case 3 
        for i=1:thick
             Mask(:,+i)=0;
        end
    case 4 
        for i=1:thick
             Mask(i,:)=0;
        end
end

Mask = logical(Mask);
maskedIm = trueIm .* Mask;

Mask = logical(Mask);

maskedNoisyIm = noisyIm .* Mask;
noiseSig=0;

% Inpaint the image
[ResIm , ResIm_OMPonly] = ksvd_inpaint(trueIm, maskedNoisyIm , Mask , patch);
rec=ResIm;
P = Compute_Error_Stats(ResIm , trueIm); 
% title(sprintf('Result, replacing only missing pixels , %02.2f dB' , P));

obj1=P;
obj2=ssim_index(ResIm , trueIm);
close all;
% Show results
% figure; imshow(trueIm , [0 255]); title('True Im');
% 
% figure; imshow(maskedNoisyIm , [0 255]); 
% P = Compute_Error_Stats(maskedNoisyIm , trueIm); 
% title(sprintf('Masked & noisy Im , %02.2f dB' , P));
% 
% figure; imshow(ResIm_OMPonly , [0 255]); 
% P = Compute_Error_Stats(ResIm_OMPonly , trueIm); 
% title(sprintf('Result , %02.2f dB' , P));
% 
% figure; imshow(ResIm , [0 255]); 
% P = Compute_Error_Stats(ResIm , trueIm); 
% title(sprintf('Result, replacing only missing pixels , %02.2f dB' , P));

% Finished
return;
