function softCompressLightness

%Please note that this step will take about 40 mins for the musicians image,
%large image may take a longer time. 
%softCompressLightness compresses source lightness into destination gamut
%lightness with lambda=2/3. 

    close all
    % 8 vertices of Destination gamut, we only use white and black lightness value
    load('data/np8_ycxcz_measured.mat')
    load('data/imgYyCxCzUnique.mat')

    [num,~] = size(imgYyCxCzUnique);

    %Find Lightness value from image YyCxCz
    imgLightness=imgYyCxCzUnique(:,1);

    %Get white and black YyCxCz value from image and Indigo
    imgWhite=imgYyCxCzUnique(num,:);
    imgCMY=imgYyCxCzUnique(1,:);
    indigoWhite=np8_ycxcz(1,4:6);
    indigoCMY=np8_ycxcz(8,4:6);

    %Find slope of Indigo lightness curve
    k_slope1=(indigoWhite(1)-indigoCMY(1))/(imgWhite(1)-imgCMY(1));
    b1=k_slope1*(-imgCMY(1))+indigoCMY(1);
    syms x
    solx = solve(k_slope1*x+b1==x, x);

    %lambda value;
    lambda=2/3;

    %Compression
    for i=1:num
        if imgLightness(i) > solx
            if imgLightness(i) > indigoWhite(1) %if lightness is larger than max destination gamut lightness
                imgYyCxCzUnique(i,1) =  (1-lambda)*((indigoWhite(1)-solx)/(imgWhite(1)-solx))*(imgLightness(i)-solx) + lambda*(indigoWhite(1)-solx)+solx;
            else
                imgYyCxCzUnique(i,1) =  (1-lambda)*((indigoWhite(1)-solx)/(imgWhite(1)-solx))*(imgLightness(i)-solx) + lambda*(imgLightness(i)-solx)+solx;
            end
        else
            if imgLightness(i) < indigoCMY(1) %if lightness is smaller than min destination gamut lightness
                imgYyCxCzUnique(i,1) =  (1-lambda)*((indigoCMY(1)-solx)/(imgCMY(1)-solx))*(imgLightness(i)-solx) + lambda*(indigoCMY(1)-solx)+solx;
            else
                imgYyCxCzUnique(i,1) =  (1-lambda)*((indigoCMY(1)-solx)/(imgCMY(1)-solx))*(imgLightness(i)-solx) + lambda*(imgLightness(i)-solx)+solx;
            end
        end
        %progress output
        if mod(i,5000) == 0
            fprintf('%d pixels of lightness compressed, %d pixels to be compressed. \n',i,num-i);
        end
    end
    fprintf('Soft compress lightness done.\n')

    save data/imgYyCxCzUniqueCmpL.mat imgYyCxCzUnique
end