function [rec,jj]=Border_Image2(D , Im, param)

jj=1;
thick = 6;
Mask = ones(256,256);

for i=1:thick
   Mask(:,257-i)=0;
end

Mask = logical(Mask);

trueIm = double(Im);
maskedIm = trueIm .* Mask;

% imPatches = im2col(maskedIm , param.patchSize , 'sliding');
% maskPatches = im2col(Mask , param.patchSize , 'sliding');

X=maskedIm;
Masks=Mask;

% Masks=maskPatches;
% X=imPatches;
% res=zeros(size(X));

dim = size(D , 1);
nAtoms = size(D , 2);
% nSignals = size(X , 2);

% if ~isfield(param , 'IgnoreUnmasked'), param.IgnoreUnmasked = 0; end;

testErrorGoal = false; errorGoal = inf; 
if isfield(param , 'errorGoal'), 
	testErrorGoal = true; 
  	errorGoal = param.errorGoal * dim * (param.noiseSig.^2); 
end;

testMaxAtoms = false; maxAtoms = nAtoms;
if isfield(param , 'maxAtoms'), testMaxAtoms = true; maxAtoms = param.maxAtoms; end;
if (~testErrorGoal) && (~testMaxAtoms), error('At least one stopping criterion is needed!'); end;

% 
% allCoeffsList  = zeros(1 , nSignals * min(maxAtoms , ceil(0.2*nAtoms)));
% allIndsList    = zeros(size(allCoeffsList));
% allSignalsList = zeros(size(allCoeffsList));
% totNcoeffs = 0;
% 
% if param.IgnoreUnmasked
% 	sumUnmasked = sum(Masks , 1);
% 	patchesToIgnore = sumUnmasked == dim;
% else
% 	patchesToIgnore = [];
% end

    res = zeros(256*thick-8,64);
    vt = find(Masks);
    
% Testing and processing signals
    while length(vt)~=256*256-8
    
    
        
	% process one signal
	
	% Find zeros in Mask and insert 1 to global Mask
	[vzr vzc]= find(Masks==0);
    vz=find(Masks==0);

    
	
    % Find the first patch to process
    patch=X(vzr(1:8),vzc(1)+(-7:0));
    vm=Masks(vzr(1:8),vzc(1)+(-7:0));
    Masks(vzr(1),vzc(1))=1;
    
	% Get current signal
	x = patch(vm);
	
	% Take the needed dictionary rows
	currD = D(vm , :);
	atomsNormFacts = sqrt(sum(currD.^2 , 1));
	currD = currD ./ repmat(atomsNormFacts , [size(currD , 1) 1]);
	
	% Update error goal
	currErrorGoal = errorGoal * length(vm) / dim;
	
	% Initialize coefficient and indices vectors
	residual = x;
	coeffsList = zeros(dim , 1);
	indsList = zeros(1 , dim);
	card = 0;
	
	% Make sure the initial signal in itself has enough energy, 
	% otherwise the zero signal is returned
% 	if ((testErrorGoal) && (sum(residual.^2) < currErrorGoal))
% 		continue; % simply move on to the next signal
% 	end
	
	% Repeat as long as stopping condition isn't filled
	while 1
		
		% Update cardinality
		card = card + 1;
		
		% Compute projections
		proj = currD' * residual;
		
		% Find the index of the largest absolute inner product
		maxProjInd = find(max(abs(proj))==abs(proj),1); 
				
		% Add this new atom to the list
		indsList(card) = maxProjInd;
		
		% If this is the first atom, keep its projection
		if (card == 1)
			coeffsList(card) = proj(maxProjInd);
		else
			% If not the first atom, do least-squares (LS) over all atoms in the representation
			coeffsList(1 : card) = currD(: , indsList(1 : card)) \ x;
		end
		
		% Compute new residual
		residual = x - currD(: , indsList(1 : card)) * coeffsList(1 : card);
		
		% Check if we can stop running
		if testErrorGoal % Error Treshold
			residNorm = sum(residual.^2);
			if residNorm < currErrorGoal, break; end;
		end
		
		if testMaxAtoms % Cardinality Threshold
			if (card >= maxAtoms), break; end;
		end
		
	end
	
	% We need to re-modify the coefficients, 
	% to undo the effects of the dictionary normalization
	if (card > 0)
		coeffsList(1 : card) = coeffsList(1 : card) ./ atomsNormFacts(indsList(1 : card))';
	end
		
	% Assign results
% 	allCoeffsList( totNcoeffs + (1 : card)) = coeffsList(1 : card);
% 	allIndsList(   totNcoeffs + (1 : card)) = indsList(1 : card);
% 	allSignalsList(totNcoeffs + (1 : card)) = cSignal;
% 	totNcoeffs = totNcoeffs + card;

    rec=zeros(64,1);

    % Computing the new patch
    for ii=1:card    

        rec=rec+coeffsList(ii).*D(:,ii);
    end
    X(vz:vz+7)=rec(57:64);
    
    % Overwrite the existing sliding coefficients
%     X(:,cSignal)=rec;
    
%     nrow=floor(cSignal/249);
%     
%     for ii=1:64
%        maskedIm(cSignal-7*nrow+vecc(i))=rec(i);
%        Mask(cSignal-7*nrow+vecc(i))=1;
%     end
    
%     Masks = im2col(Mask , param.patchSize , 'sliding');
%     X = im2col(maskedIm , param.patchSize , 'sliding');
    

    res(jj,:)=rec;
    vt = find(Masks);
    jj=jj+1;
    end
    
    close all;
    figure; imshow(X , [0 255]); title('Reconstructed Image');
    
end

% ResIm = Average_Overlapping_Patches(res, [256 256] , param.patchSize );



% Construct the sparse matrix
% resA = sparse(...
% 	allIndsList(1:totNcoeffs) , allSignalsList(1:totNcoeffs) , allCoeffsList(1:totNcoeffs) , ...
% 	nAtoms , nSignals);

% Create the output signals
% resX = D * resA;
% resX(: , patchesToIgnore) = X(: , patchesToIgnore);






