function gamutMappingStep0

%gamutMappingStep0 shifts and rotates source and destination gamut to align 
%with Yy axis and computes lightness, hue, and chroma of source and 
%destination gamut. Also it allows you to display 2D hue sectors view 
%before any compression.

    close all
    
    load('data/IndigoGamut.mat')%Indigo/Destination Gamut
    load('data/imgYyCxCzUnique.mat')% Source Gamut without any compression

    [num,l] = size(imgYyCxCzUnique);
    [num1,l1] = size(IndigoGamut);

    img_w=imgYyCxCzUnique(num,:);
    img_cmy=imgYyCxCzUnique(1,:);
    np_w=IndigoGamut(1,:);
    np_cmy=IndigoGamut(num1,:);

    %shift and rotate Indigo/destination gamut to origin
    dest_gamut_shifted=IndigoGamut-np_cmy;%shift
    R1 = rotM((np_w-np_cmy),[100,0,0]);
    dest_gamut=R1*(dest_gamut_shifted)';
    dest_gamut=dest_gamut';
    dest_gamut(1,2:3)=round(dest_gamut(1,2:3));

    %Shift and Rotate Image/source gamut to origin
    ycxcz_unique_shifted=imgYyCxCzUnique-img_cmy;%shift
    R = rotM(img_w-img_cmy,[100,0,0]/100);
    src_gamut = R*(ycxcz_unique_shifted)';
    src_gamut=src_gamut';
    src_gamut(num,:)=dest_gamut(1,:);

    %Destination gamut lightness center
    center=0.5*(dest_gamut(1,1)-dest_gamut(num1,1));
    

    %Image Hue and Lightness
    hue_src=zeros(num,1); %hue
    ch_src=zeros(num,1);%chroma
    ch_src_ref=zeros(num,1);% another copy of chroma
    l_src=zeros(num,1);% lightness
    l_src_ref=zeros(num,1);
    for i = 1:num
        hue = abs((atan(src_gamut(i,3)/src_gamut(i,2))/pi)*180);%calculate hue
        if src_gamut(i,2)==0 && src_gamut(i,3)==0
            hue_src(i) = 0;
        elseif src_gamut(i,2)>=0 && src_gamut(i,3)>=0%first
            hue_src(i) = hue;
        elseif src_gamut(i,2)<=0 && src_gamut(i,3)>=0%second
            hue_src(i) = 180 - hue;
        elseif src_gamut(i,2)<=0 && src_gamut(i,3)<=0%third
            hue_src(i) = -180 + hue;
        elseif src_gamut(i,2)>=0 && src_gamut(i,3)<=0%fourth
            hue_src(i) = -hue;
        end
        if hue_src(i)==-180
            hue_src(i)=180;
        end
        ch_src(i)=sqrt((src_gamut(i,3)^2)+(src_gamut(i,2)^2));%calculate chroma
        l_src(i)=src_gamut(i,1);
    end
    ch_src_ref=ch_src;
    l_src_ref=l_src;

    %%%%%%%%%%%%%%%%%%Indigo gamut Hue and Lightness ****************
    hue_dest=zeros(num1,1);%hue
    ch_dest=zeros(num1,1);% chroma
    l_dest=zeros(num1,1);%lightness
    for i = 1:num1
        hue = (atan(abs(dest_gamut(i,3))/abs(dest_gamut(i,2)))/pi)*180; %calculate hue
        if dest_gamut(i,2)==0 && dest_gamut(i,3)==0
            hue_dest(i) = 0;
        elseif dest_gamut(i,2)>=0 && dest_gamut(i,3)>=0%first
            hue_dest(i) = hue;
        elseif dest_gamut(i,2)<=0 && dest_gamut(i,3)>=0%second
            hue_dest(i) = 180 - hue;
        elseif dest_gamut(i,2)<=0 && dest_gamut(i,3)<=0%third
            hue_dest(i) = -180 + hue;
        elseif dest_gamut(i,2)>=0 && dest_gamut(i,3)<=0%fourth
            hue_dest(i) = -hue;
        end
        if hue_dest(i)==-180
            hue_dest(i)=180;
        end
        ch_dest(i)=sqrt((dest_gamut(i,3))^2+(dest_gamut(i,2)^2));%calculate chroma
        l_dest(i)=dest_gamut(i,1);
    end

    index=1;
    %find out source and destination pixels in each hue sector
    for j=-180:5:175

        i1=find(hue_dest>j);%find hue angle larger than j in destination gamut
        i2=find(hue_dest(i1)<=j+5);%find hue angle equal or smaller than j+5 in destination gamue
        dest_list=i1(i2);%dest is list of Indigo/destination gamut point inside this sector
        ll=length(dest_list);
        dest_list(ll+1:ll+2)=[1 num1];%add white and black from destination gamut to this list

        i1=find(hue_src>j);%find hue angle larger than j in source gamut
        i2=find(hue_src(i1)<=j+5);%find hue angle equal or smaller than j+5 in source gamut
        src_list=i1(i2);%src is list of Image/source gamut pixels inside this sector

        %for Destination gamut
        ch_dest_list=ch_dest(dest_list);%Chroma values in this sector
        ch_dest_max=max(ch_dest(dest_list));%max chroma value in this sector

        l_dest_list=l_dest(dest_list);%lightness values in this sector
        l_dest_max=max(l_dest(dest_list));%max chroma value in this sector
        l_dest_min=min(l_dest(dest_list));%min chroma value in this sector

        %use MATLAB build-in function 'convhull' to form a gamut convex hull from
        %the chroma and lightness for this sector
        dest_list_index = convhull(ch_dest(dest_list),l_dest(dest_list));
        if length(src_list) > 5
            src_list_index = convhull(ch_src(src_list),l_src(src_list));
        else 
            src_list_index = [1:length(src_list)];
        end

        x=[0,0,ch_dest_max,ch_dest_max,0];
        y=[l_dest_max,l_dest_min,l_dest_min,l_dest_max,l_dest_max];


        %for Image/Source gamut
        [nrow,~]=size(src_list);
        ch_src_list=ch_src(src_list);%Chroma values in this sector
        ch_src_max=max(ch_src(src_list));% max chroma value in this sector

        l_src_list=l_src(src_list);%all lightness values in this sector
        l_src_max=max(l_src(src_list));%max lightness value in this sector


        %Comment out plotting to speed process,
        %this plot is showing Lightness v.s. Chroma before compression

        figure
        plot(x,y)
        hold on
        %%%Indigo
        plot(ch_dest(dest_list),l_dest(dest_list),'.','MarkerSize',10)
        hold on
        plot(ch_dest(dest_list(dest_list_index)),l_dest(dest_list(dest_list_index)),'o-','MarkerSize',10)
        hold on
        %%%Image
        plot(ch_src(src_list),l_src(src_list),'.','MarkerSize',10)
        hold on
        plot(ch_src(src_list(src_list_index)),l_src(src_list(src_list_index)),'o-','MarkerSize',10)

        xlim([0 180])
        ylim([0 220])
        grid on
        legend('Bounding Cylinder','pixels of Destination Gamut in this hue sector','Convex hull of pixels of Destination Gamut','pixels of Source Gamut in this hue sector','Convex hull of pixels of Source Gamut')
        xlabel('Chroma');ylabel('Lightness')
        name = sprintf('Step 0 Before any compression Hue slice view (%d,%d]',j,j+5);
        title(name)
        name = sprintf('Step 0 Before any compression Hue slice view (%d,%d].png',j,j+5);
        saveas(gca,[pwd '/output/hueSlicePlots/',name]);
        close all


        index=index+1;

    end


end


