function R=gray2rgb_DDWT2_LL(img1,img2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This Program converts a gray image ro RGB image based on the colors of the destination image. The better the destination image match with the source   %
%gray image, the better the coloring will be. The program takes some time  as the searching time is high. You can decrease the searching time by taking %
%only samples from the used color image but quality may decrease. U can use jittered sampling for improving running speed.                              %
%                                                                                                                                                       %
%  You can use also use the attahed test images, Use the following combinations for better result nature1.jpg(as img1) and nature2.jpg(as img2)  or     % 
%  test1.jpg(as img1) and test2.jpg (as img2)
%                                                                                                                                                       %
%                                                                                                                                                       %
% Usage                                                                                                                                                 %
% gray2rgb('nature1.jpg','nature2.jpg');  
% 
%                                                                                                                                                       %
%               Authors : Jeny Rajan                                                                                                                    %
%                         Chandrashekar P.S                                                                                                             %                                     
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                    

% img1 - Source Image  (gray image)                                                                                                        
% img2 - Selected color image for coloring the gray image. 

% [Faf, Fsf] = Lift53;
% Faf{1}(:,1) = Faf{1}(:,1)/sqrt(2); 
% Faf{1}(:,2) = Faf{1}(:,2)*sqrt(2);
% Faf{2}(:,1) = Faf{2}(:,1)/sqrt(2);
% Faf{2}(:,2) = Faf{2}(:,2)*sqrt(2);
% 
% 
% Fsf{1}(:,1) = Fsf{1}(:,1)*sqrt(2); 
% Fsf{1}(:,2) = Fsf{1}(:,2)/sqrt(2);
% Fsf{2}(:,1) = Fsf{2}(:,1)*sqrt(2);
% Fsf{2}(:,2) = Fsf{2}(:,2)/sqrt(2);
% % 
% af = Faf;
% sf = Fsf;

[Faf, Fsf] = FSfarras;
% Faf{1}(:,1) = Faf{1}(:,1)/sqrt(2); 
% Faf{1}(:,2) = Faf{1}(:,2)*sqrt(2);
% Faf{2}(:,1) = Faf{2}(:,1)/sqrt(2);
% Faf{2}(:,2) = Faf{2}(:,2)*sqrt(2);
% 
% 
% Fsf{1}(:,1) = Fsf{1}(:,1)*sqrt(2); 
% Fsf{1}(:,2) = Fsf{1}(:,2)/sqrt(2);
% Fsf{2}(:,1) = Fsf{2}(:,1)*sqrt(2);
% Fsf{2}(:,2) = Fsf{2}(:,2)/sqrt(2);

[af sf]= dualfilt1;
% af{1}(:,1) = af{1}(:,1)/sqrt(2); 
% af{1}(:,2) = af{1}(:,2)*sqrt(2);
% af{2}(:,1) = af{2}(:,1)/sqrt(2);
% af{2}(:,2) = af{2}(:,2)*sqrt(2);
% 
% 
% sf{1}(:,1) = sf{1}(:,1)*sqrt(2); 
% sf{1}(:,2) = sf{1}(:,2)/sqrt(2);
% sf{2}(:,1) = sf{2}(:,1)*sqrt(2);
% sf{2}(:,2) = sf{2}(:,2)/sqrt(2);

J = 1;
T1 = 128;
T2 = 1;
Step = -1;
T = T1:Step:T2;
k=1.6;

tic
clc;
warning off;
imt=imread(img1);
ims=imread(img2);
[sx sy sz]=size(imt);
[tx ty tz]=size(ims);
if sz~=1
    imt=rgb2gray(imt);
end
if tz~=3
    disp ('img2 must be a color image (not indexed)');
else
    imt(:,:,2)=imt(:,:,1);
    imt(:,:,3)=imt(:,:,1);
    
    for i=1:1:3
      coeffs{i} = dualtree2D(double(ims(:,:,i)), J, Faf, af);
      coefft{i} = dualtree2D(double(imt(:,:,i)), J, Faf, af);
      [cfs{1}{i},cfs{2}{i}] = ddwtOne2Two(coeffs{i}, J);
      [cft{1}{i},cft{2}{i}] = ddwtOne2Two(coefft{i}, J);
      cims1(:,:,i) = dwtcell2arr(cfs{1}{i}, J);
      cims2(:,:,i) = dwtcell2arr(cfs{2}{i}, J);
      cimt1(:,:,i) = dwtcell2arr(cft{1}{i}, J);
      cimt2(:,:,i) = dwtcell2arr(cft{2}{i}, J);
    end
    
% Converting to ycbcr color space
    nspace11=rgb2ycbcr(uint8(cims1));
    nspace12=rgb2ycbcr(uint8(cims2));
    nspace21=rgb2ycbcr(uint8(cimt1));
    nspace22=rgb2ycbcr(uint8(cimt2));
    
   for i=1:1:3
    cfs{1}{i}=dwtarr2cell(nspace11(:,:,i),J);
    cfs{2}{i}=dwtarr2cell(nspace12(:,:,i),J);
    cft{1}{i}=dwtarr2cell(nspace21(:,:,i),J);
    cft{2}{i}=dwtarr2cell(nspace22(:,:,i),J);
   end
    
%     nspace1=rgb2ycbcr(ims);
%     nspace2=rgb2ycbcr(imt);
% nspace1=ims;
% nspace2=imt;
    
%     figure;
%     imshow(uint8(nspace1));
    
%     for i=1:1:3
%       coeffs{i} = Lifting2D(double(nspace1(:,:,i)), J, lsi53);
%       coefft{i} = Lifting2D(double(nspace2(:,:,i)), J, lsi53);
%     end
%     coeff53 = dwtcell2arr(coefft{1}, J);
    
%     figure;
%     imshow(uint8(coeff53));

for tr=1:1:2
    
%     LL J+1
    ms=double(cfs{tr}{1}{J+1});
    mt=double(cft{tr}{1}{J+1});
    m1=max(max(ms));
    m2=min(min(ms));
    m3=max(max(mt));
    m4=min(min(mt));
    d1=m1-m2;
    d2=m3-m4;
% Normalization
    dx1=ms;
    dx2=mt;
    dx1=(dx1*255)/(255-d1);
    dx2=(dx2*255)/(255-d2);
    [mx,my,mz]=size(dx2);
%Luminance Comparison
    disp('Please wait..................');
    for i=1:mx
        for j=1:my
             iy=dx2(i,j);
             tmp=abs(dx1-iy);
             ck=min(min(tmp));
             [r,c] = find(tmp==ck);
             ck=isempty(r);
             if (ck~=1)            
                 coeff{tr}{2}{J+1}(i,j)=cfs{tr}{2}{J+1}(r(1),c(1));
                 coeff{tr}{3}{J+1}(i,j)=cfs{tr}{3}{J+1}(r(1),c(1));
                 coeff{tr}{1}{J+1}(i,j)=cft{tr}{1}{J+1}(i,j); 
                 for k=1:1:J
                    for b=1:1:3
                 coeff{tr}{2}{k}{b}(i,j)=cfs{tr}{2}{k}{b}(r(1),c(1));
                 coeff{tr}{3}{k}{b}(i,j)=cfs{tr}{3}{k}{b}(r(1),c(1));
                 coeff{tr}{1}{k}{b}(i,j)=cft{tr}{1}{k}{b}(i,j); 
                    end
                end
             end
         end
    end
    
end    
%     for i=1:1:3
%       nimage(:,:,i) = iLifting2D(coeff{i}, J, lsi53);
%     end
%    
%     unimage=uint8(nimage);
% %     figure;
% %     imshow(uint8(nimage));
%     
%     rslt=ycbcr2rgb(unimage);
% %     rslt=unimage;
%     RR=uint8(rslt);
   for i=1:1:3
   cim1(:,:,i) = dwtcell2arr(coeff{1}{i}, J);
   cim2(:,:,i) = dwtcell2arr(coeff{2}{i}, J);
   end
   nimage1=double(ycbcr2rgb(uint8(cim1)));
   nimage2=double(ycbcr2rgb(uint8(cim2)));
   for i=1:1:3
   coecell{1}{i}=dwtarr2cell(nimage1(:,:,i),J);
   coecell{2}{i}=dwtarr2cell(nimage2(:,:,i),J);
   imy{i} = iddwtOne2Two(coecell{1}{i},coecell{2}{i},J);
   rslt(:,:,i) = idualtree2D(imy{i}, J, Fsf, sf);
   end
   
%    for i=1:1:3
%       rslt(:,:,i) = idualtree2D(imy{i}, J, Fsf, sf);
%    end
   
%     save ('gray2rgb_DWT.mat');
    figure,imshow(uint8(imt));
    figure,imshow(uint8(rslt));
%     figure,imshow(rslt);
   
    toc
end