How to calculate RP-177 3×3 matrices in Matlab. Part 2. TRA. Rec709/P3 to Rec2020.

(work in progress)
Matlab code made by Tinna Lif Gunnarsdottir.

Part 1 is here

SMPTE RP-177:1993  Rec709/P3 to Rec2020 NPM/TRA

(note: The color red needs some adjusting in P3 to Rec2020 TRAs if trying to do a 3d lut in nuke with degamma/gamma)

COLOR REC 2020 NPM & NPM INV

        R = [0.708 ; 0.292 ; 0.000];
        G = [0.170 ; 0.797 ; 0.0330];
        B = [0.131 ; 0.046 ; 0.8230];

P = [R G B];

White Point D65

        X = 0.3127;
        Y = 0.3290;
        Z = 0.3583;
        W = [X/Y ; 1 ; Z/Y];


CC = P \ W;

C = [CC(1,1) 0 0 ; 0 CC(2,1) 0 ; 0 0 CC(3,1)];
format long 
NPM = P * C 
NPM_inv = inv(NPM)

NPM =

0.636958048301291   0.144616903586208   0.168880975164172
0.262700212011267   0.677998071518871   0.059301716469862
0                   0.028072693049088   1.060985057710791

NPM_inv =

 1.716651187971268  -0.355670783776393  -0.253366281373660
-0.666684351832489   1.616481236634939   0.015768545813911
 0.017639857445311  -0.042770613257809   0.942103121235474



From P3 D65 to Rec-2020

% COLOR DCI P3 - D65
R_P3 = [0.6800   ; 0.3200 ; 0.0000];
G_P3 = [0.2650  ; 0.6900 ; 0.0450];
B_P3 = [0.1500  ; 0.0600 ; 0.7900];
P_P3 = [R_P3 G_P3 B_P3];
X_P3 = 0.3127;
Y_P3 = 0.3290;
Z_P3 = 0.3583;
W_P3 = [X_P3/Y_P3 ; 1 ; Z_P3/Y_P3];
CC_P3 = P_P3 \ W_P3;
C_P3 = [ CC_P3(1,1) 0 0 ;  0  CC_P3(2,1) 0 ;  0    0   CC_P3(3,1)];
NPM_P3 = P_P3 * C_P3
% TRA
TRA_P3 =  NPM \ NPM_P3

NPM_P3 =

0.486570948648216   0.265667693169093   0.198217285234362
0.228974564069749   0.691738521836506   0.079286914093745
                0   0.045113381858903   1.043944368900976

TRA_P3 =

 0.753833034361722   0.198597369052617   0.047569596585662
 0.045743848965358   0.941777219811693   0.012478931222948
-0.001210340354518   0.017601717301090   0.983608623053428

From REC 709 D65 to REC-2020

        R_7 = [0.640 ;  0.330 ; 0.030];
        G_7 = [0.300 ;  0.600 ; 0.100];
        B_7 = [0.150 ;  0.060 ; 0.790];
        P_7 = [R_7 G_7 B_7];
        % White Point  D65

X_7 = 0.3127;
Y_7 = 0.3290;
Z_7 = 0.3583;
W_7 = [X_7/Y_7 ; 1 ; Z_7/Y_7];
CC_7 = P_7 \ W_7;             % P inv

 

C_7 = [ CC_7(1,1) 0 0 ;  0  CC_7(2,1) 0 ;  0  0 CC_7(3,1)];
NPM_7 = P_7 * C_7
TRA_7 =  NPM \ NPM_7 % Rec709 source, NPM/2020 dest, and inv

NPM_7 =

0.412390799265959   0.357584339383878   0.180480788401834
0.212639005871510   0.715168678767756   0.072192315360734
0.019330818715592   0.119194779794626   0.950532152249661

TRA_7 =

0.627403895934699   0.329283038377884   0.043313065687417
0.069097289358232   0.919540395075459   0.011362315566309
0.016391438875150   0.088013307877226   0.895595253247624

From P3 DCI WHITE to Rec-2020

X_P3_W = 0.314;
Y_P3_W = 0.351;
Z_P3_W = 0.335;
W_P3_W = [X_P3_W/Y_P3_W ; 1 ; Z_P3_W/Y_P3_W];
CC_P3_W = P_P3 \ W_P3_W;
C_P3_W = [ CC_P3_W(1,1) 0 0 ;  0  CC_P3_W(2,1) 0 ;  0    0
CC_P3_W(3,1)];
NPM_P3_W = P_P3 * C_P3_W
TRA_P3_W =  NPM \ NPM_P3_W

NPM_P3_W =

0.445169815564552   0.277134409206778   0.172282669815565
0.209491677912731   0.721595254161044   0.068913067926226
                0   0.047060560053981   0.907355394361973

TRA_P3_W =

 0.689691223459987   0.207169204075508   0.041345622770170
 0.041851616632057   0.982426091420886   0.010846196309229
-0.001107355451221   0.018361440441151   0.854913936657422

How to apply X’Y’Z’ 3D LUTS in Photoshop to an image so it will match the white point of a DCP

Sometimes you may want to add something to a DCP trailer or feature film.
Example: Trailers often need new graphics in a new language.

In Photoshop and Resolve you can apply 3D LUTS to an image to match the white point of the original DCP.

If the DCP has a warm white you could use a D60 3D LUT and if it has a cooler white you can use a D65 3D LUT.

If you render a video file in Final Cut X or Premiere with graphics that has a X’Y’Z’ 3D LUT applied, remember to choose no color transform in Easydcp and similar DCP mastering software.

If you apply a rec709 to DCI X´Y´Z´ 3D LUT to a video or image in Resolve that is used on a DCP, change Project settings – Color Management – Timeline Colorspace  to DCI X´Y´Z´ .

You can apply 3DL 3D LUTs in Easydcp Creator.

You can apply Cube 3D LUTs in Resolve and Photoshop.

How to apply a 3D LUT in Photoshop
Layer – New adjustment Layer – Color lookup

 

Click on load 3d lut and choose a 3D LUT. You can now use a cube 3D LUT.

 

Some 3D LUTS

Here are some P3 and REC709 3D LUTS in 3DL and CUBE (float) format using the Nuke 3D LUT method from this post and the matlab NPMs from this post.

P3 D61 2.6 gamma to X’Y’Z’ 7z

White in the 12 bit 3dl file is 3885 3960 3997 It is the correct D61 value in SMPTE EG-432-1-10

 

P3 D60 2.6 gamma to X’Y’Z’ 7z

White in the 12 bit 3dl file is 3886 3960 3972. It is the correct D60 value in SMPTE EG-432-1-10

 

P3D65 2.6 gamma to X’Y’Z’ 7z

White in the 12 bit 3dl file is 3883 3960 4092
It is the correct D65 value in SMPTE EG-432-1-10

 

P3 DCI WHITE 2.6 gamma to X’Y’Z’  7z

White in the 12 bit 3dl file is 3794 3960 3890 (greenish)
It is the correct DCI WHITE value in SMPTE EG-432-1-10

 

REC709 D65 2.2 2.4 2.6 gamma to X’Y’Z’ 7z

White in the 12 bit 3dl files is 3883 3960 4092
It is the correct D65 value in SMPTE EG-432-1-10

 

REC 709 D60 2.2 2.4 2.6 gamma to X’Y’Z’ 7z

White in the 12 bit 3dl files is 3886 3960 3972. It is the correct D60 value in SMPTE EG-432-1-10

 

How to calculate RP-177 3×3 matrices in Matlab

How to calculate RP-177 3×3 matrices in Matlab.

Update: For part 2 with the RP-177 TRA matlab code see this post

The Matlab code is made by Tinna Lif Gunnarsdottir.

To make DCPs in DCI-P3 D6O, D61, D65 or Rec 709 D60, D61, D65 you can use SMPTE RP-177:1993 3×3 NPMs to transfer from RGB to XYZ.

NPM = normalized primary matrix

For more info on the math see SMPTE EG 432-1-10 and How to make 3D LUTs in Nuke (R´B´G´to X´Y´Z´)

update:

Here are some 3D LUTS made with these 3×3 matrices

 

Example: DCI-P3 D6O

Here is the matlab code to calculate the correct RP-177 RGB-XYZ matrices to go from DCI-P3 D6O to XYZ.

MATLAB CODE
#DCI-P3 Primaries

R = [0.68 ; 0.320 ; 0];
G = [0.265 ; 0.690 ; 0.045];
B = [0.150 ; 0.060 ; 0.79];

P = [R G B];

#DCI- P3 D60 Whitepoint

X = 0.3217;
Y = 0.3378;
Z = 0.3405;

W = [X/Y ; 1 ; Z/Y];

CC = P \ W;

C = [ CC(1,1) 0 0 ; 0 CC(2,1) 0 ; 0 0 CC(3,1)];

NPM = P * C

NPM =

0.504739646629896 0.264744009223770   0.182855006076470 0.237524539590539 0.689333457978873   0.073142002430588
0                                               0.044956529868187   0.963036365336076

END MATLAB CODE

Pasted into a Nuke color matrix node:
0.50473965   0.26474401     0.18285501
0.23752454    0.68933346    0.073142
0                            0.04495653   0.96303637

The middle row sum to 1
0.23752454+0.68933346+0.073142=1

Other 3X3 NPM matrices

The Rec709 and DCI-P3 D65 3×3 matrices is also used in this post

DCI-P3 D65

WHITE POINT
X = 0.3127;
Y = 0.3290;
Z = 0.3583;

NPM from matlab:
0.486570948648216 0.265667693169093 0.198217285234362
0.228974564069749 0.691738521836506 0.0792869140937450
0                                               0.045113381858903 1.043944368900976

Pasted into a Nuke color matrix node:
0.48657095  0.26566769  0.19821729
0.22897457   0.69173852    0.07928691
0                           0.04511338    1.04394437

The middle row sums to 1
0.22897457+0.69173852+0.07928691 = 1


DCI-P3 D61

WHITE POINT
X = 0.3198;
Y = 0.3360;
Z = 0.3442;

NPM from matlab:
0.500852913533834 0.264968867481203  0.185963933270677
0.235695488721804 0.689918937969925  0.0743855733082713
0                                              0.044994713345865  0.979410048558897

Pasted into a Nuke color matrix node:
0.50085291 0.26496887 0.18596393
0.23569549 0.68991894 0.07438557
0                          0.04499471 0.97941005

Middle row sums to 1
0.23569549+0.68991894+0.07438557=1

DCI-P3 DCI-WHITE

WHITE POINT
X = 0.314;
Y = 0.351;
Z = 0.335;

NPM from matlab
0.445169815564552 0.277134409206778   0.172282669815565
0.209491677912731   0.721595254161044    0.068913067926226
0                                              0.047060560053981 0.907355394361973

Pasted into a Nuke color matrix node:
0.44516982 0.27713441     0.17228267
0.20949168 0.72159525    0.06891307
0                         0.04706056   0.90735539

Middle row sums to 1
0.20949168+0.72159525+0.06891307=1

REC 709
REC 709 D65, D60, D61 fits inside DCI-P3 and can be used in DCPs.

REC709 Primaries
R = [0.640 ; 0.330 ; 0.030];
G = [0.300 ; 0.600 ; 0.100];
B = [0.150 ; 0.060 ; 0.790];

REC 709 D65

D65 WHITE POINT
X = 0.3127;
Y = 0.3290;
Z = 0.3583;

NPM from matlab:
0.412390799265959 0.357584339383878 0.180480788401834
0.212639005871510   0.715168678767756   0.072192315360734
0.019330818715592   0.119194779794626   0.950532152249661

Pasted into a Nuke color matrix node:
0.4123908  0.3575843   0.18048079
0.21263901 0.71516867 0.07219232
0.01933082 0.1191948   0.95053215

The middle row should sum to 1 according to RP-177 so I changed 0.71516868 to 0.71516867.


REC 709 D60

D60 WHITE POINT
X = 0.3217;
Y = 0.3378;
Z = 0.3405;

NPM from matlab:
0.431575918832227 0.355727231008106 0.165035512089804
0.222531333147867 0.711454462016212   0.066014204835922
0.020230121195261 0.118575743669369  0.869187030339634

Pasted into a Nuke color matrix node:
0.43157592 0.35572723 0.16503551
0.22253133 0.71145447  0.0660142
0.02023012 0.11857574 0.86918703

Changed 0.71145446 to 0.71145447 so the middle row sums to 1
0.22253133+0.71145447+0.0660142= 1

REC 709 D61

D61 WHITE POINT

X = 0.3198;
Y = 0.3360;
Z = 0.3442;

NPM from matlab:
0.427462229871868 0.356162746222987 0.168160738190859
0.220410212277682  0.712325492445975 0.067264295276343
0.020037292025244 0.118720915407662 0.885646554471856

Pasted into a Nuke color matrix node:
0.42746223 0.35616275 0.16816074
0.22041021  0.71232549 0.0672643
0.02003729 0.11872092 0.88564655
Middle row sums to 1
0.22041021+0.71232549+0.0672643=1