function [M] = AllRelativePatchL2Dist(I,patchsize,range);
 nOri = size(I,3);
 rangerange = 2*range + 1;
 M = zeros(size(I,1),size(I,2),rangerange,rangerange);
 M = M + inf;
 s = size(I);
 D = zeros(size(I,1), size(I,2), 2);
 % D = zeros(size(I,1), size(I,2), 4);
 n =0;
 for i = 1:rangerange,
   transi = i - range - 1;
   % if(transi == 0), continue;, end; % (null translations are too unstable)
   % fprintf('correlation %.3d of about %.3d \r',n,floor((rangerange^2) / 2));
   for j = floor(rangerange/2):rangerange,
   % for j = 1:rangerange,
     n = n+1;
     transj = j - range - 1;
     if((transj == 0) && (transi == 0)) , continue;, end; % (null translations are too unstable)
     SHIFTI = TranslateIm(I,[transi,transj]);
     % for iOri = 1:4
     %   D(:,:,iOri) = conv2(ones(patchsize,1),ones(1,patchsize),(I(:,:,iOri)-SHIFTI(:,:,iOri)).^2,'same');
     %   % D(:,:,iOri) = sumfilter((I(:,:,iOri)-SHIFTI(:,:,iOri)).^2,floor(patchsize/2));
     %   % D(:,:,iOri) = I(:,:,iOri)+SHIFTI(:,:,iOri);
     % end
     for iOri = 1:nOri
       D(:,:,iOri) = conv2(ones(patchsize,1),ones(1,patchsize),(I(:,:,iOri)-SHIFTI(:,:,iOri)).^2,'same');
       % D(:,:,iOri) = sumfilter((I(:,:,iOri)-SHIFTI(:,:,iOri)).^2,floor(patchsize/2));
       % D(:,:,iOri) = I(:,:,iOri)+SHIFTI(:,:,iOri);
     end
     M(:,:,i,j) = sum(D,3);
   end
 end
 for i = 1:rangerange,
   transi = i - range - 1;
   % if(transi == 0), continue;, end; % (null translations are too unstable)
   for j = 1:(floor(rangerange/2)-1),
     transj = j - range - 1;
     % if(transj == 0), continue;, end; % (null translations are too unstable)
     M(:,:,i,j) = TranslateIm(M(:,:,rangerange-i+1,rangerange-j+1),[transi, transj],inf);
   end
 end
 M = M.^-1;
 