function [d1, d2, maxVal] = My_NormXcorr_subpixel_align_setWindow_atomicRegion(Img1, Img2, windowDim1ind, windowDim2ind, Res, searchRange1,searchRange2)

if Res >= 0.5
    disp('Resolution should be less than 0.5');
    return;
end

if size(Img1)~=size(Img2)
    disp('two images should be in the same size!');
    return;
end

Dim1 = size(Img1,1);
Dim2 = size(Img1,2);

DivFact = 4;

NumIter = abs(floor(log(Res)/log(DivFact)));
curr_start1 = -0.5;
curr_end1 = 0.5;
curr_start2 = -0.5;
curr_end2 = 0.5;

N1_array = -1*floor(Dim1/2):floor( (Dim1-1)/2);
N2_array = -1*floor(Dim2/2):floor( (Dim2-1)/2);
[KX, KY] = ndgrid(N1_array,N2_array);

img2_FF = fftshift(fft2(ifftshift(Img2)));

img1_windowed = Img1(windowDim1ind, windowDim2ind);
for i=1:NumIter
    
    curr_boundary1arr = curr_start1:(curr_end1-curr_start1)/DivFact:curr_end1;
    curr_boundary2arr = curr_start2:(curr_end2-curr_start2)/DivFact:curr_end2;
    
    curr_ind1arr = (circshift(curr_boundary1arr,[0 -1]) + curr_boundary1arr)/2;
    curr_ind2arr = (circshift(curr_boundary2arr,[0 -1]) + curr_boundary2arr)/2;
    
    curr_ind1arr = curr_ind1arr(1:end-1);
    curr_ind2arr = curr_ind2arr(1:end-1);

    maxXCar = zeros(length(curr_ind1arr),length(curr_ind2arr));
    for j=1:length(curr_ind1arr)
        for k=1:length(curr_ind2arr)
            curr_Pfactor = exp(2*pi*1i*(curr_ind1arr(j)*KX/Dim1 + curr_ind2arr(k)*KY/Dim2));
            curr_Simage = real(fftshift(ifft2(ifftshift(img2_FF.*curr_Pfactor))));
            curr_Xcorr = normxcorr2(img1_windowed,curr_Simage);
            subRegion = zeros(size(curr_Xcorr));
%             subRegion(floor(size(img1_windowed,1)/2)+windowDim1ind(50:end-50),floor(size(img1_windowed,2)/2)+windowDim2ind(50:end-50)) = ...
%                 curr_Xcorr(floor(size(img1_windowed,1)/2)+windowDim1ind(50:end-50),floor(size(img1_windowed,2)/2)+windowDim2ind(50:end-50));
%             maxXCar(j,k) = max(curr_Xcorr(:));
            windowCenter1 = round((length(windowDim1ind)+1)/2);
            windowCenter2 = round((length(windowDim2ind)+1)/2);
            subRegion(round((size(img1_windowed,1)+1)/2)-1+windowDim1ind(windowCenter1-searchRange1:windowCenter1+searchRange1),round((size(img1_windowed,2)+1)/2)-1+windowDim2ind(windowCenter2-searchRange2:windowCenter2+searchRange2)) = ...
                curr_Xcorr(round((size(img1_windowed,1)+1)/2)-1+windowDim1ind(windowCenter1-searchRange1:windowCenter1+searchRange1),round((size(img1_windowed,2)+1)/2)-1+windowDim2ind(windowCenter2-searchRange2:windowCenter2+searchRange2));
            maxXCar(j,k) = max(subRegion(:));
        end
    end
    
    
    
    [~, maxInd] = max(maxXCar(:));
    [maxj, maxk] = ind2sub(size(maxXCar),maxInd);
    
    curr_start1 = curr_boundary1arr(maxj);
    curr_end1 = curr_boundary1arr(maxj+1);
    curr_start2 = curr_boundary2arr(maxk);
    curr_end2 = curr_boundary2arr(maxk+1);
    
end

sd1 = curr_ind1arr(maxj);
sd2 = curr_ind2arr(maxk);
max_Pfactor = exp(2*pi*1i*(curr_ind1arr(maxj)*KX/Dim1 + curr_ind2arr(maxk)*KY/Dim2));

Simage = real(fftshift(ifft2(ifftshift(img2_FF.*max_Pfactor))));
max_Xcorr =  normxcorr2(img1_windowed,Simage);
%% XT:using subregion
subRegion = zeros(size(max_Xcorr));
% subRegion(floor(size(img1_windowed,1)/2)+windowDim1ind(50:end-50),floor(size(img1_windowed,2)/2)+windowDim2ind(50:end-50)) = ...
%     max_Xcorr(floor(size(img1_windowed,1)/2)+windowDim1ind(50:end-50),floor(size(img1_windowed,2)/2)+windowDim2ind(50:end-50));
subRegion(round((size(img1_windowed,1)+1)/2)-1+windowDim1ind(windowCenter1-searchRange1:windowCenter1+searchRange1),round((size(img1_windowed,2)+1)/2)-1+windowDim2ind(windowCenter2-searchRange2:windowCenter2+searchRange2)) = ...
    max_Xcorr(round((size(img1_windowed,1)+1)/2)-1+windowDim1ind(windowCenter1-searchRange1:windowCenter1+searchRange1),round((size(img1_windowed,2)+1)/2)-1+windowDim2ind(windowCenter2-searchRange2:windowCenter2+searchRange2));

%%
[maxVal, maxInd] = max(subRegion(:));

% [maxVal, maxInd] = max(max_Xcorr(:));
[id1, id2] = ind2sub(size(img2_FF) + size(img1_windowed) - 1,maxInd);


offset1 = id1 - size(img1_windowed,1) + 1;
offset2 = id2 - size(img1_windowed,2) + 1;

intShift1 = windowDim1ind(1) - offset1;
intShift2 = windowDim2ind(1) - offset2;


d1 = intShift1-sd1;
d2 = intShift2-sd2;

end