function gamutMappingStep2

%gamutMappingStep2 uses soft compression with lambda=2/3 to compress any 
%source pixels that are outside of bounding cylinder into bounding 
%cylinder. Bounding cylinder is a rectangle in 2D generated by max 
%destination chroma value and max destination lightness value in any 
%particular hue sector. Also it allows you to display 2D hue sectors view 
%after compression into bounding cylinder.

    close all
    load('data/keyInfo.mat')

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


    index=1;
    %Gamut Mapping
    for j=-180:5:175


        %Code below is the same from step 1


        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        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



        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



        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %Code above is the same from step 1

        %Code below is step 2

        %Compress Chroma into Bounding Cylinder
        lambda = 2/3;
        if ch_src_max > ch_dest_max
            for i=1:nrow
                if ch_src_list(i) > ch_dest_max
                    ch_src_list(i) =  (1-lambda)*(ch_dest_max/ch_src_max)*ch_src_list(i) + lambda*ch_dest_max;
                else
                    ch_src_list(i) =  (1-lambda)*(ch_dest_max/ch_src_max)*ch_src_list(i) + lambda*ch_src_list(i);
                end

            end
        end

        %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

        %Comment out plotting to speed process,
        %this plot is showing Lightness v.s. Chroma after compression into
        %bounding cylinder

        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_list,l_src_list,'.','MarkerSize',10)
        hold on
        plot(ch_src_list(src_list_index),l_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')
        input = sprintf('Step 2 Compression into Bounding Cylinder slice view (%d,%d]',j,j+5);
        title(input)
        name = sprintf('Step 2 Compression into Bounding Cylinder slice view (%d,%d].png',j,j+5);
        saveas(gca,[pwd '/output/hueSlicePlots/',name]);
        close all


        index=index+1;

    end

end


