function R=gray2rgb(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. 
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);
   
% Converting to ycbcr color space
    nspace1=rgb2ycbcr(ims);
    nspace2= rgb2ycbcr(imt);
%     nspace1=rgb2lab(ims);
%     nspace2= rgb2lab(imt);

    ms=double(nspace1(:,:,1));
    mt=double(nspace2(:,:,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);
    
    [sx,sy,sz]=size(dx1);
for i=1:sx
    for j=1:sy
        if i>1 && i< sx && j>1 && j<sy 
          avgs(i,j)=1/2*dx1(i,j)+1/16*(dx1(i-1,j-1)+dx1(i-1,j)+dx1(i-1,j+1)+dx1(i,j-1)+dx1(i,j+1)+dx1(i+1,j-1)+dx1(i+1,j)+dx1(i+1,j+1)); 
        end
        if i==1 && j>1 && j<sy
           avgs(i,j)=1/2*dx1(i,j)+1/10*(dx1(i,j-1)+dx1(i,j+1)+dx1(i+1,j-1)+dx1(i+1,j)+dx1(i+1,j+1));  
        end
        if i==sx && j>1 && j<sy
           avgs(i,j)=1/2*dx1(i,j)+1/10*(dx1(i-1,j-1)+dx1(i-1,j)+dx1(i-1,j+1)+dx1(i,j-1)+dx1(i,j+1));
        end
        if j==1 && i>1 && i<sx
            avgs(i,j)=1/2*dx1(i,j)+1/10*(dx1(i-1,j)+dx1(i-1,j+1)+dx1(i,j+1)+dx1(i+1,j)+dx1(i+1,j+1)); 
        end
        if j==sy && i>1 && i<sx
            avgs(i,j)=1/2*dx1(i,j)+1/10*(dx1(i-1,j-1)+dx1(i-1,j)+dx1(i,j-1)+dx1(i+1,j-1)+dx1(i+1,j)); 
        end
        
        avgs(1,1)=1/2*dx1(1,1)+1/6*(dx1(1,2)+dx1(2,1)+dx1(2,2)); 
        avgs(1,sy)=1/2*dx1(1,sy)+1/6*(dx1(1,sy-1)+dx1(2,sy-1)+dx1(2,sy));
        avgs(sx,1)=1/2*dx1(sx,1)+1/6*(dx1(sx-1,1)+dx1(sx-1,2)+dx1(sx,2));
        avgs(sx,sy)=1/2*dx1(sx,sy)+1/6*(dx1(sx,sy-1)+dx1(sx-1,sy-1)+dx1(sx-1,sy));
                   
    end
end

for i=1:mx
    for j=1:my
        if i>1 && i< mx && j>1 && j<my 
          avgt(i,j)=1/2*dx2(i,j)+1/16*(dx2(i-1,j-1)+dx2(i-1,j)+dx2(i-1,j+1)+dx2(i,j-1)+dx2(i,j+1)+dx2(i+1,j-1)+dx2(i+1,j)+dx2(i+1,j+1)); 
        end
        if i==1 && j>1 && j<my
           avgt(i,j)=1/2*dx2(i,j)+1/10*(dx2(i,j-1)+dx2(i,j+1)+dx2(i+1,j-1)+dx2(i+1,j)+dx2(i+1,j+1));  
        end
        if i==mx && j>1 && j<my
           avgt(i,j)=1/2*dx2(i,j)+1/10*(dx2(i-1,j-1)+dx2(i-1,j)+dx2(i-1,j+1)+dx2(i,j-1)+dx2(i,j+1));
        end
        if j==1 && i>1 && i<mx
            avgt(i,j)=1/2*dx2(i,j)+1/10*(dx2(i-1,j)+dx2(i-1,j+1)+dx2(i,j+1)+dx2(i+1,j)+dx2(i+1,j+1)); 
        end
        if j==my && i>1 && i<mx
            avgt(i,j)=1/2*dx2(i,j)+1/10*(dx2(i-1,j-1)+dx2(i-1,j)+dx2(i,j-1)+dx2(i+1,j-1)+dx2(i+1,j)); 
        end
        
        avgt(1,1)=1/2*dx2(1,1)+1/6*(dx2(1,2)+dx2(2,1)+dx2(2,2)); 
        avgt(1,my)=1/2*dx2(1,my)+1/6*(dx2(1,my-1)+dx2(2,my-1)+dx2(2,my));
        avgt(mx,1)=1/2*dx2(mx,1)+1/6*(dx2(mx-1,1)+dx2(mx-1,2)+dx2(mx,2));
        avgt(mx,my)=1/2*dx2(mx,my)+1/6*(dx2(mx,my-1)+dx2(mx-1,my-1)+dx2(mx-1,my));
                   
    end
end

%Luminance Comparison
    disp('Please wait..................');
    for i=1:mx
        for j=1:my
             iy=avgt(i,j);
             tmp=abs(avgs-iy);
             ck=min(min(tmp));
             [r,c] = find(tmp==ck);
             ck=isempty(r);
             if (ck~=1)            
                 nimage(i,j,2)=nspace1(r(1),c(1),2);
                 nimage(i,j,3)=nspace1(r(1),c(1),3);
                 nimage(i,j,1)=nspace2(i,j,1);           
             end
%          if i>1 && i<mx && j>1 && j<my
%              
%          else
%          end
             
         end
     end
    rslt=ycbcr2rgb(nimage);
%     rslt=lab2rgb(nimage);
%     save ('gray2rgb.mat');
toc
    figure,imshow(uint8(imt));
    figure,imshow(uint8(rslt));
    imwrite(uint8(rslt),'ori.bmp','bmp');
%     savefile = ['ori.jpg'];
%     saveas(gcf,savefile);
    R=uint8(rslt);
    
end