-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTransformsPack - Models.avsi
2976 lines (2324 loc) · 151 KB
/
TransformsPack - Models.avsi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###########################################################
### ##
### ##
### Transforms Pack - Models v2.2.1 (19-02-2024) ##
### ##
### https://forum.doom9.org/showthread.php?t=182881 ##
### ##
### by Dogway (Jose Linares) ##
### ##
### MODELS: ##
### RGB_to_YUV / YUV_to_RGB ##
### RGB_to_YcCbcCrc / YcCbcCrc_to_RGB ##
### RGB_to_YUVp / YUVp_to_RGB ##
### YCC_to_YUVp / YUVp_to_YCC ##
### RGB_to_YIQ / YIQ_to_RGB ##
### YCC_to_YIQ / YIQ_to_YCC ##
### RGB_to_YCoCg / YCoCg_to_RGB ##
### RGB_to_YCoCgR / YCoCgR_to_RGB ##
### RGB_to_YUVr / YUVr_to_RGB ##
### RGB_to_OPP / OPP_to_RGB ##
### XYZ_to_Yxy / Yxy_to_XYZ ##
### XYZ_to_YDzDx / YDzDx_to_XYZ ##
### XYZ_to_Lab / Lab_to_XYZ ##
### XYZ_to_Luv / Luv_to_XYZ ##
### XYZ_to_XYB / XYB_to_XYZ ##
### RGB_to_HSV / HSV_to_RGB ##
### RGB_to_IPT / IPT_to_RGB ##
### RGB_to_ICtCp / ICtCp_to_RGB ##
### RGB_to_Oklab / Oklab_to_RGB ##
### RGB_to_XYZ / XYZ_to_RGB ##
### XYZ_to_LMS / LMS_to_XYZ ##
### ADAPTATION: ##
### GamutCompression ##
### CAT ##
### CMF ##
### SPD_D ##
### CIE1931_Locus ##
### Chromaticity_xy ##
### ##
###########################################################
##########################################
### ##
### MODEL CONVERSION FUNCTIONS ##
### ##
##########################################
# List of Color Spaces:
#
# Color Spaces define the size and shape of our source color gamut container in the unitary XYZ coordinates cube. In other words, a color space conversion doesn't change the size (saturation), quantity (interpolation in HBD) or shape (gamut mapping) of our fruits (gamut), but the basket that holds them.
# Color Spaces are presented normally as a 3x3 matrix of their color primaries represented in the CIE 1931 diagram coordinates. This might be ok but be aware this is an over-simplification as color spaces are actually 3D volumes within the unitary XYZ cube, and part of them may actually fall outside the CIE 1931 color space which represents all the physically plausible colors.
#
# There are several types of Color Spaces defined by their role:
# Characterization spaces are normally used to represent device gamuts for profiling on a unit-to-unit basis.
# Standard spaces define a standard to comply with for delivery, normally they are derived from averaging measurements of real unit-by-unit devices defined by their used phosphors.
# Finally working spaces don't need to be attached to any physical constraints so they can be as big as the CIE 1931 color space (some actually bigger than that), so they are suited for heavy color transformations without much degradation in the process. They can also be geared towards certain effects like getting more color resolution in certain HUEs for exposing better shades in color grading tasks. Ultimately everything is being transformed into your viewing transform color space in real time so use any working space that suits your job.
#
#-470M - (Historical) ITU-R Rec. BT.470 M. Also known as FCC or NTSC (1953) with C illuminant and 170M OETF.
#-470BG - ITU-R Rec. BT.1700 (or legacy ITU-R Rec. BT.601) (625-line = PAL, SECAM).
#-170M - ITU-R Rec. BT.1700 (or legacy ITU-R Rec. BT.601) (525-line = NTSC, SMPTE-C) (originally defined in "CCIR Recommendation 601-1 (1982-1986)").
#-240M - SMPTE 240M.
#-NTSC-J - ARIB TR-B9. Phosphor primaries used in Japan's consumer CRTs along 9300K + 27 MPCD illuminant.
#-sRGB - IEC 61966-2-1. Jointly developed by Microsoft and Hewlett-Packard as a color standard designed primarily for office, home and web users. Comprises Primaries -derived from phosphors of calibrated consumer CRTs of the 90s-, Illuminant and Transfer Characteristics.
# scRGB - IEC 61966-2-2. Extended RGB. The scRGB colour space is an extension of sRGB and it is considered compatible with sRGB, designed to provide high bit precision, large colour gamut and extended dynamic range that is linear with respect to scene radiance.
#-AdobeRGB - IEC 61966-2-5:1999. Also known as opRGB (optional RGB). Designed to encompass most printers' gamuts and to be used as a working space in Photoshop, ideally at 16-bit. It's basically the 470BG primaries with greens from 470M allegedly after a sucessions of mistakes.
#-Adobe Wide Gamut - Adobe second and final wide gamut version of AdobeRGB with same transfer but in D50 illuminant.
#-DCI-P3 - SMPTE RP 432-2. Digital Cinema.
#-Display-P3/P3-D65 - SMPTE RP 432-1. Display-P3 is Apple's take on P3, same DCI-P3 primaries but with D65 illuminant and sRGB TRC. P3-D65 instead uses PQ for HDR and 1886 for SDR.
#-2020 - ITU-R Rec. BT.2020.
#-AP0 - ACES AP0 coefficients used for ACES2065 color space.
#-AP1 - ACES AP1 coefficients used for ACEScg color space.
#-S-Gamut - Sony Gamut
# S-Gamut3 - Sony Gamut 3
# S-Gamut3 (Venice) - Sony Gamut Cinema 3 Venice
#-S-Gamut3.Cine - Sony Gamut Cinema 3. A little over DCI-P3 gamut in size, paired with S-Log3 it reproduces a scanned negative film.
# S-Gamut3.Cine (Venice) - Sony Gamut Cinema 3 Venice
#-Cinema Gamut - Canon Cinema Gamut (D65)
# CinemaDay - Canon Cinema Gamut Daylight (5500K).
# CinemaTungsten - Canon Cinema Gamut Tungsten (3200K).
#-CanonP3 - Canon DCI-P3+. Expanded DCI-P3 gamut from a compressed version of Canon Cinema Gamut
# D-Gamut - DJI D-Gamut.
# D-GamutM - DJI D-GamutM.
# DragonColor - RED DragonColor
# DragonColor2 - RED DragonColor2
# REDColor -
# REDColor2 -
# REDColor3 -
# REDColor4 -
# REDWide - RED Wide Gamut RGB.
# Protune - GoPro Protune Native.
# Bolex - Bolex Wide Gamut RGB.
# DaVinci Wide Gamut -
# Almira709 -
# LC709 -
# LC709A -
# s709 -
# Cine709 - Sony Cine+709.
# ADX10 -
# ADX16 -
# BMD Wide Gamut - BlackMagic Design Wide Gamut
# BMP 6k Film - BlackMagic Pocket 6K Film
# BMD Film - (Legacy)
# BMD Broadcast Video - (Legacy)
# BMD Broadcast Film - (Legacy)
# BMD 4K Film - (Legacy)
# BMD 4K Film V3 - (Legacy)
# BMD 4K Video V3 - (Legacy)
# BMD 4.6K Film - (Legacy)
# BMD 4.6K Film v3 - (Legacy)
# BMD 4.6K Video - (Legacy)
# BMD 4.6K Video v3 - (Legacy)
# Alexa - Arri (Alexa) Wide Gamut.
# Film - Arri Film Matrix. Traditional film primaries with Illuminant C.
#-FS-Gamut - SMPTE Free Scale Gamut for imagery having arbitrary chromaticity that can be conveyed, inherited from Sony's F23, F35, and F65 Wide Gamut cameras. (ST 2048-1).
# V-Gamut - Panasonic V-Gamut (Varicam).
# V709 - Panasonic Varicam V709.
# F-Log - Fujifilm F-Log Gamut.
# Barco Unisee -
#-ProPhoto RGB - RIMM/ROMM RGB by Kodak to encompass all printable and most real world colors. D50 with Apple gamma. ERIMM RGB is geared towards HDR.
#-Wide Gamut RGB - Adobe first wide gamut version with D50, and AdobeRGB gamma (2.19921875). Designed by Adobe to be a wide gamut color space that uses spectral colors as its primaries.
#-AllColorsRGB - Developed by Ellen Stone to encompass all colors, slightly wider than AP0. Defined with linear transfer at D50 and prims: 0.734690265, 0.265309735, 0.17451, 0.005182, 0.0, -0.07585421971554103213.
#-MaxRGB - Developed by Don Hutcheson. "If you can accept 'illegal' colors (outside the CIEYxy limits) then try the outrageously saturated gamut of MaxRGB. Not that you gain anything useful, but at least you can say you have an even bigger Photoshop-style tri-coordinate RGB working space than ProPhoto RGB!"
#-xTremeRGB - Developed by Don Hutcheson. "XtremeRGB pushes Photoshop (tri-coordinate 3x3 matrix) color spaces rules to the absolute limit, producing the world's largest possible Photoshop-legal tricoordinate RGB space. However it does so at extreme risk and is provided here as a curiosity more than a useful tool. Only use XtremeRGB on 16 bit-per-channel (or higher) images."
#-CIE RGB - or CIE 1931 RGB in linear space (sometimes over generic gamma of 2.2) and E illuminant.
#-Apple RGB - RGB color space by Apple derived from Sony Trinitron phosphors, similar in gamut size to sRGB or ColorMatch. Employs a gamma of 1.8.
#-ColorMatch RGB - Radius PressView Monitors with D50 and a power gamma of 1.8.
#-EBU Tech. 3213-E - EBU Tech. 3213-E / JEDEC P22 phosphors. Legacy EBU Tech 3213 (later 470BG) (625-line = PAL, SECAM).
#-Bruce RGB - Developed by Bruce Fraser as a conservative-gamut space for dealing with 8-bit imagery that needs heavy editing. Early and modest gamut that made into one of the SWOP printing standards in the U.S.
#-DonRGB4 - Developed by Don Hutcheson in 1996. Wide gamut working space for photography, prepress, etc. 2.2 pure power gamma.
#-BestRGB - Developed by Don Hutcheson. Improved DonRGB4 for saturated reds and magentas. 2.2 pure power gamma.
#-Beta RGB - Developed by Bruce Lindbloom. An optimized capture, archiving and editing space for high-end digital imaging applications, modeled after famous photographic films like Fuji, Agfa or Kodak. 2.2 pure power gamma.
#-Russell RGB - Developed by Russell Cottrell. An extension of Beta RGB for cyans and blues to encompass some printers' gamuts. Uses D55 illuminant in 2.2 power gamma.
#-ECI-RGB v2 - Developed by the European Color Initiative <www.eci.org>. Employs the L* gamma transfer.
#-Ekta Space PS5 - Developed by Joseph Holmes for high quality storage of image data from scans of transparencies (aka chromes), mostly for positives but works fine also for negative film. 2.2 pure power gamma.
#-Chrome Space 100 - Developed by Joseph Holmes. Refined 2005 version of Ekta Space PS5, conserving primaries and illuminant but with a different gamma, designed to match the tonality of a perceptually linear greyscale for printers.
# DCam Sets - Developed by Joseph Holmes. 5 color spaces designed for digital cameras, they also use Chrome Space 100 tone curve.
# And many more oldies not included here for simplicity: P22 (Measured SMPTE-C), EBU measured, Conrac 7211N19, Hitachi, Barco, Tektronics, SGI Trinitron, Sony Trinitron KV-20M20.
# List of Color Models:
# Color Models -not to be confused with Color Spaces- describe the geometry coordinates of the color gamut container and hence its relation (or correlation) with the other channels. They are typically designed or tailored to certain Color Spaces, but not necessarily bound to them, like RGB or YCbCr which support many different Color Spaces and thus are nicked Color Absolute Models.
# Color Spaces comprises Primaries and an associated Illuminant. It is also common to be associated with a transfer function although its relationship is not as strict (as it depends on viewing conditions).
#
# UCS: Uniform Color Space. Space (model) in which equal distances on the coordinate diagram correspond to equal perceived color differences.
# With *: Color Appearance Models (CAM) (Perceptually Uniform -UCS- to various degrees)
# With +: HDR aware
# With -: Implemented
#
# CMYK - Color model used in print industry.
#-RGB+ - RGB color model for channels Red, Green, and Blue. Normally found in interleaved packing format. AviSynth works best with planar RGB so a conversion is first needed.
#-OPP - by Liang et al. RGB opponent space.
# NOPP - by Liang et al.
# COPP - by Liang et al.
# rg - Photometric invariant (shadows, shading) 2 channel color representation, by Danelljan [R/(R+G+B), G/(R+G+B)]
# I1I2I3 - by Guo and Lyu [0.3333*(R+G+B),0.5*(R-B),0.25*(2G-R-B)]
# C1C2C3 - by Salvador et al.
# LSLM - RGB opponent space based on the signals of the cones: black-white, red-green, and yellow-blue.
# YES - by Saber et al.
# UVW - by Ohta et al.
# SO - Biologically Inspired Color Image Descriptor.
# c1c2c4 - Biologically Inspired Color Image Descriptor.
# o1o2 - For Robust Image Detection in Scenes under Cast Shadows. https://digital.csic.es/bitstream/10261/30099/1/Combining%20color-based.pdf
#-YPbPr - A model for analogue signals. Has no definition for the SMPTE range so saturates full range. Roughly based on human perception of luma and chroma contribution (approximately perceptually uniform). Uses 3 planes for Luma, opposing blue-yellow, and opposing red-green. It's a 45º skewed parallelogram relative to the square RGB box, therefore only 23.6% of its color volume matches RGB and 76.4% is out of RGB gamut.
#-YCbCr - YPbPr scaled to SMPTE legal range ((16,128,128) + (219*Y,224*Pb,224*Pr)). Format used for digital systems.
#-YUV - Decorrelated Opponent color model for analogue system used in PAL territories and NTSC since 1979.
#-YIQ - YUV with UV axes rotated 33º, used for old FCC 1953 NTSC TV analogue systems until about 1979. Quote from avisynth.nl/index.php/Tweak: "In YIQ, orange and teal are along the in-phase (I) axis, while green and magenta are along the quadrature (Q) axis. The eye is far more sensitive to detail along the I axis than along the Q axis, and NTSC allocated more bandwidth for I than for Q."
# YDbDr - PAL-N and SECAM TV analogue format.
#-YUVr - "YUV RCT". Same as YCbCr but fully reversible to RGB.
# sYUV - "Sharp YUV". Same as YCbCr but luma-weighted chroma subsampling, retaining chroma fidelity.
#-YcCbcCrc - YCbCr Constant Luminance (CL). Better decorrelation than YCbCr.
# xvYCC - Expanded Gamut YCbCr. For newer TV systems it is/was a model that allowed the color gamut (typically Rec709) to be stored in the full range while luma in the narrow range.
#-YCoCg - Better luma/chroma decorrelation and compression efficiency than YCbCr.
#-YCoCgR - Same as YCoCg but losslessly reversible to RGB.
# YFbFr - Similar to YCoCg, employs Karhunen-Loève transform (KLT) to design an integer reversible transform.
# Yrg - Truelight Yrg model, described in "Chromaticity Coordinates for Graphic Arts Based on CIE 2006 LMS with Even Spacing of Munsell Colours" by Richard Kirk
#-XYZ - CIE 1931 XYZ tristimulus color model representing all the non-imaginary colors.
# LSY - Reads lambda-SY. Spectral color space. Representation based on brightness, dominant wavelength and saturation attributes. LSY color coordinates are defined from xyY color coordinates.
#-YDzDx+ - HDR version of XYZ. Requires at least 10-bit for no visible quantization.
# YCxCz - Color Space for image fidelity metrics. It's a first step space, you have to "filter" it and then convert to Lab to retrieve error delta.
#-DCI XYZ - XYZ space in DCI illuminant. In effect it's just a container for DCI-P3 in XYZ coordinates for delivery to theaters.
#-Yxy/xyY - Chroma normalized XYZ space which gives full luma/chroma decorrelation. Not apt for HDR, use YDzDx+ instead.
#-HSV - Cylindrical representation of RGB. Not recommended as it doesn't take into account human luminance perception.
#-Duv* - Perceptual based model which measures the closest distance from the Planckian locus on the CIE 1976 (u', 2/3 v') diagram. Some color spaces and Delta metrics are computed in this model which is an update of the CIE 1960 UCS chromaticity diagram. A white point defined in Duv coordinates is called a "color tint".
# HunterLab* - A more perceptually uniform version of CIELab.
#-CIELab* - Derived from CIE XYZ, one of the first approaches to a perceptually uniform color model. CIELch(ab) is its cylindrical (polar coordinates) version. You can derive it's Delta E with the next equation: DE = (DL^2 + Da^2 + Db^2)/2 = ( (Ly-Lx)^2 + (ay-ax)^2 + (by-bx)^2)/2.
# CIEsLab* - CIE simple Lab.
# R-Lab* - Ruderman Lab space, using log luma and different weights for ab.
# L-Lab* -
# SRLab2* - A 2009 modification of CIELab by Jan Behrens in the spirit of RLAB. Uses CIECAM02 chromatic adaptation matrix to fix the blue hue issue but keeps CIELab transfer.
#-CIELuv* - CIE 1976. Derived from CIE 1964 UVW, a competing perceptually uniform color model of CIELab around the same time the latter appeared. CIELuv uses a Judd-type CAT, while CIELab uses "wrong" von-Kries (XYZ Scaling).
# CIELch(uv)*- HCL (polar coordinates or cylindrical) version of CIELuv.
# HSLuv* - A cylindrical HSL type adaptation of CIELuv. It extends CIELuv with a new saturation component that allows you to span all the available chroma as a neat percentage.
# LogLuv*+ - HDR version of CIELuv, that uses log to encode HDR values in a 15-bit container.
# Y''U''V''*+ - HDR version of CIELuv by Charles Poynton. Improves on LogLuv by using a PQ transfer which just needs 12-bits.
# CIEDuDv*+ - or YDuDv. HDR version of CIE Luv. Requires only 8-bit for no visible quantization.
# CIECAM97s* -
#-XYB* - Uniform (UCS) space used in Guetzli's butteraugli metric and JPEG-XL, with a main goal in compression. Better uniformity than CIE Lab but not as good as IPT.
# DIN99o* - Color space based on CIE Lab for which main characteristic is to show comparable accuracy to CIEDE2000 but with a simple Euclidean distance.
# JzAzBz*+ - A 2017 uniform (UCS) space designed for HDR color. Has J (lightness) and two chromaticities. JzCzhz is the HCL (polar coordinates or cylindrical) version of JzAzBz.
#-IPT* - Intensity Protanope Tritanope. A more uniform color space in SDR colors. There's a version called hdrIPT.
# IgPgTg - Based on IPT but optimized using gaussian spectra to derive a hue-linear color space.
# ICaCb+ - Precursor and arguably better color model than ICtCp which based on this.
#-ICtCp+ - BT.2100 (HDR + Rec.2020) version of IPT. HDR version of YcCbcCrc but with Constant Intensity this time.
# ITP*+ - Perceptually uniform color space version of ICtCp, used extensively in error metrics by scaling Ct by 0.5. This transformation is the basis of the Rec.2124 wide gamut color difference metric D.itp (simple scaled Euclidean distance: 720*sqrt(Di^2+D(t*0.5)^2+Dp^2) )
#-IPTPQc2*+ - Improved HDR10 version of ICtCp. Since it's a Dolby's propietary model all that is known is by means of reverse engineering.
# CIECAM02* - Improves on CIECAM97s and can be made uniform with CAM02-UCS. CIECAM02Jch is its cylindrical version.
# OSA* -
# CIECAM16* - Improves on CIECAM02 and can be made uniform with CAM16-UCS, which works "out-of-the-box" for Euclidean distance metrics.
# CAM20u - Improvement over CIECAM16 for predicting the color appearance of unrelated colors.
# ZCAM*+ - A uniform (UCS) space for HDR color.
#-Oklab* - A uniform (UCS) space for SDR color. Simple to compute yet powerful. Takes CAM16 lightness and chroma, and IPT for hue. OkLch is the cylindrical form of Oklab.
# DT UCS22* - The darktable UCS 22 is a perceptually uniform color space designed from psychoperceptual experimental data specifically for the purpose of artistic saturation changes, as performed in the color balance RGB module. It uses a brightness-saturation scheme that compensates for the Helmholtz-Kohlraush effect. https://eng.aurelienpierre.com/2022/02/color-saturation-control-for-the-21th-century
# iCAM06 - An image (not only color) appearance metric model to not only compute perceived difference in lightness, hue or chroma, but also contrast, sharpness or graininess. Involves converting to IPT, low-pass filtering in linear space, convert to non-linearity, then to Jch model and QM if necessary for the metrics.
# Hellwig2022 - Hellwig and Fairchild (2022) CAM
# Others not included: RLAB, Nayatani, LLAB, Kim, Weyrich and Kautz (2009)
# YCbCr to RGB' standalone function (at least 10-bit precision recommended)
function YUV_to_RGB (clip yuv, string "matrix", bool "tv_in", bool "tv_out", string "kernel", float "b", float "c", float "p", string "cplace", bool "UVrecon") {
matrix = Default (matrix, "709")
bi = BitsPerComponent(yuv)
fs = propNumElements (yuv,"_ColorRange") > 0 ? \
propGetInt (yuv,"_ColorRange") == 0 : (matrix=="JPEG" || matrix=="JPG")
kernel = Default (kernel, "CatRom") # Catmull-Rom. Curiously enough the kernel doesn't make a big difference when going to RGB, be it spline36, bicubics, etc.
tv_in = Default (tv_in, !fs)
tv_out = Default (tv_out, false)
cplace = Default (cplace, "")
recon = Default (UVrecon, false)
b = Default (b, 0.0) # Bicubic 'b' coefficient, or 'taps' for sinc kernels
c = Default (c, 0.5) # Catmull-Rom
p = Default (p, 9)
Assert(IsVersionOrGreater(3,7,2), "YUV_to_RGB: Update AviSynth+ version")
Assert(!isRGB(yuv), "YUV_to_RGB: Only YUV format supported")
p_type = format_Fuzzy_Search(yuv, PixelType(yuv), bi)
matrix = color_Fuzzy_Search (matrix)
coef = color_coef(mat=matrix[0])
bc = bicubic_coeffs(kernel)
bc_b = string(bc[0]>-2?bc[0]:b)
bc_c = string(bc[0]>-2?bc[1]:c)
kernel = bc[0]>-2 ? "Bicubic" : LCase(kernel)
p_type4 = p_type[1] == "444"
p_type2 = p_type[1] == "422"
p_type1 = p_type[1] == "411"
p_type0 = p_type[1] == "420"
p_type10 = p_type[1] == "410"
w = width (yuv)
h = height(yuv)
nw = p_type4 ? w : p_type1 || p_type10 ? round(w/4.0) : round(w/2.0)
nh = p_type4 || p_type1 || p_type2 ? h : round(h/2.0)
cplace = chroma_placement(w, h, w, h, matrix[0], matrix[0], "YUV"+p_type[1], "RGB", p_type[1], "444", cplace, "center" )
p_type1 ? Assert(bi == 8, "YUV_to_RGB: Unsupported Pixel Type: HBD YUV411") : nop()
Assert(!p_type10, "YUV_to_RGB: Unsupported Pixel Type: YUV410")
cplaceH = ",src_left="+string(cplace[0])
cplaceV = ",src_top ="+string(cplace[1])
resampler = FindStr(kernel, "lanczos")>0 && kernel!="lanczos4" || FindStr(kernel, "blackman")>0 || FindStr(kernel, "sinc")>0 ? \
kernel + "Resize(" + String(w) + "," + String(h) + cplaceH + cplaceV +",taps= int("+bc_b+"))": \
kernel == "nnedi3" ? "nnedi3resize(" + String(w) + "," + String(h) + ")" : \
kernel == "bicubic"? "BicubicResize(" + String(w) + "," + String(h) + cplaceH + cplaceV +",b="+bc_b+",c="+bc_c+")" : \
kernel == "gauss" ? "GaussResize(" + String(w) + "," + String(h) + cplaceH + cplaceV +",p=p)" : \
kernel + "Resize(" + String(w) + "," + String(h) + cplaceH + cplaceV +")"
Y = ExtractY(yuv)
Uor = ExtractU(yuv)
Vor = ExtractV(yuv)
# feisty2's ChromaReconstructor_faster v3.0 HBD mod
if (recon && !p_type4) {
ref = Y.KNLMeansCL(0, 12, 0, 31.078131515593690, wref=1)
Luma = ref.nnedi3wrap( 2, nns=1, qual=1, etype=1, nsize=0, HBD=false, gpuid=0)
Uu = Uor.nnedi3wrap(p_type1||p_type10?8:4, p_type10?8:4, nns=1, qual=1, etype=1, nsize=0, HBD=false, gpuid=0)
Vu = Vor.nnedi3wrap(p_type1||p_type10?8:4, p_type10?8:4, nns=1, qual=1, etype=1, nsize=0, HBD=false, gpuid=0)
Unew = Uu.KNLMeansCL(0, 12, 0, 9, wref=0, rclip=Luma).BicubicResize(w, h, b=0.0, c=0.5, src_left=cplace[0], src_top=cplace[1])
Vnew = Vu.KNLMeansCL(0, 12, 0, 9, wref=0, rclip=Luma).BicubicResize(w, h, b=0.0, c=0.5, src_left=cplace[0], src_top=cplace[1])
U = ex_makeadddiff(Unew, ex_boxblur(Unew), Uu.BicubicResize(w, h, b=0.0, c=0.5, src_left=cplace[0], src_top=cplace[1]))
V = ex_makeadddiff(Vnew, ex_boxblur(Vnew), Vu.BicubicResize(w, h, b=0.0, c=0.5, src_left=cplace[0], src_top=cplace[1]))
} else {
U = p_type4 ? Uor : Eval("Uor." + resampler)
V = p_type4 ? Vor : Eval("Vor." + resampler)
}
if (matrix[0]=="2020CL" || matrix[0]=="DCI-P3" || matrix[0]=="Display-P3") {
Assert(false, "YUV_to_RGB: Use YcCbcCrc_to_RGB() function")
} else {
scale_y = tv_in != tv_out ? tv_in ? 256/219. : 219/256. : 1.0
scale_uv = tv_in != tv_out ? tv_in ? 256/112. : 224/128. : 2.0
Kr = 1. - coef[0]
Kb = 1. - coef[2]
m0 = scale_y m2 = scale_uv * Kr
m4 = scale_uv * ( -Kb * coef[2] / coef[1]) m5 = scale_uv * ( -Kr * coef[0] / coef[1])
m7 = scale_uv * Kb
range_PC = scale_y == 256/219. ? "ymin - " : ""
range_TV = scale_y == 219/256. ? "ymin + " : ""
UVf = bi < 32 ? "range_half - " : ""
Expr(Y, U, V, ex_dlut("x "+range_PC+" "+string(m0)+" * "+range_TV+" z "+UVf + string(m2)+" * + ", bi, false), \
ex_dlut("x "+range_PC+" "+string(m0)+" * "+range_TV+" y "+UVf + string(m4)+" * + z "+UVf + string(m5)+" * + ", bi, false), \
ex_dlut("x "+range_PC+" "+string(m0)+" * "+range_TV+" y "+UVf + string(m7)+" * + ", bi, false), optSingleMode=true, format=bi>16?"RGBPS":"RGBP"+string(bi))
}
tv_in!=tv_out ? propSet("_ColorRange", tv_out ? 1 : 0) : last
propSet("_Matrix", 0)
propSet("_Model", 1) }
# RGB' to YCbCr standalone function (at least 10-bit precision recommended)
function RGB_to_YUV (clip rgb, string "matrix", bool "tv_in", bool "tv_out", string "Jab", string "kernel", float "b", float "c", float "p", string "cplace") {
bi = BitsPerComponent(rgb)
fs = propNumElements (rgb,"_ColorRange") > 0 ? \
propGetInt (rgb,"_ColorRange") == 0 : true
matrix = Default (matrix, "709")
p_type = Default (Jab, "420") # target YUV chroma subsampling
kernel = Default (kernel, "Didee")
tv_in = Default (tv_in, !fs)
tv_out = Default (tv_out, true)
cplace = Default (cplace, "")
b = Default (b, -0.5)
c = Default (c, 0.25) # Didée's Bicubic
p = Default (p, 9)
Assert(IsVersionOrGreater(3,7,2), "RGB_to_YUV: Update AviSynth+ version")
Assert(isRGB(rgb) && IsPlanar(rgb), "RGB_to_YUV: Only Planar RGB format supported")
p_type = format_Fuzzy_Search (rgb, p_type, bi)
matrix = color_Fuzzy_Search (matrix)
coef = color_coef(mat=matrix[0])
bc = bicubic_coeffs(kernel)
bc_b = string(bc[0]>-2?bc[0]:b)
bc_c = string(bc[0]>-2?bc[1]:c)
kernel = bc[0]>-2 ? "Bicubic" : LCase(kernel)
p_type4 = p_type[1] == "444"
p_type2 = p_type[1] == "422"
p_type1 = p_type[1] == "411"
p_type10 = p_type[1] == "410"
w = width (rgb)
h = height(rgb)
nw = p_type4 ? w : p_type1 || p_type10 ? round(w/4.0) : round(w/2.0)
nh = p_type4 || p_type1 || p_type2 ? h : round(h/2.0)
cplace = chroma_placement(w, h, w, h, matrix[0], matrix[0], "RGB", "YUV"+p_type[1], "444", p_type[1], "center", cplace )
p_type1 ? Assert(bi == 8, "RGB_to_YUV: Unsupported Pixel Type: HBD YUV411") : nop()
Assert(!p_type10, "RGB_to_YUV: Unsupported Pixel Type: YUV410")
Assert(h%2==0, "RGB_to_YUV: Height is not mod2" )
!p_type1 || !p_type4 ? Assert(w%2==0, "RGB_to_YUV: Width is not mod2" ) : nop()
p_type1 ? Assert(w%4==0, "RGB_to_YUV: Width is not mod4" ) : nop()
Rx = ExtractR(rgb)
Gx = ExtractG(rgb)
Bx = ExtractB(rgb)
if (matrix[0]=="2020CL" || matrix[0]=="DCI-P3" || matrix[0]=="Display-P3") {
Assert(false, "RGB_to_YUV: Use RGB_to_YcCbcCrc() function")
} else {
scale_y = tv_in != tv_out ? tv_in ? 256/219. : 219/256. : 1.0
scale_uv = tv_in != tv_out ? tv_in ? 256/112. : 112/256. : 0.5
Kr = 1. - coef[0]
Kb = 1. - coef[2]
m0 = scale_y * coef[0] m1 = scale_y * coef[1] m2 = scale_y * coef[2]
m3 = scale_uv * (-coef[0] / Kb) m4 = scale_uv * (-coef[1] / Kb) m5 = scale_uv
m7 = scale_uv * (-coef[1] / Kr) m8 = scale_uv * (-coef[2] / Kr)
rangeY_TV = scale_y ==219/256. ? "ymin +" : \
scale_y ==256/219. ? "ymin -" : ""
rangeC_TV = bi < 32 ? "range_half +" : ""
Y = ex_dlut("x "+string(m0)+" * y "+string(m1)+" * + z "+string(m2)+" * + "+rangeY_TV+"", bi, false)
Cb = ex_dlut("x "+string(m3)+" * y "+string(m4)+" * + z "+string(m5)+" * + "+rangeC_TV+"", bi, false)
Cr = ex_dlut("x "+string(m5)+" * y "+string(m7)+" * + z "+string(m8)+" * + "+rangeC_TV+"", bi, false)
}
if (p_type4) {
Expr(Rx, Gx, Bx, Y, Cb, Cr, optSingleMode=true, format=bi>16?"YUV444PS":"YUV444P"+string(bi))
} else {
Y = Expr(Rx, Gx, Bx, Y, optSingleMode=true)
Cb = Expr(Rx, Gx, Bx, Cb, optSingleMode=true)
Cr = Expr(Rx, Gx, Bx, Cr, optSingleMode=true)
cplaceH = ",src_left="+string(cplace[0])
cplaceV = ",src_top ="+string(cplace[1])
# Although not optimal, technically YUV420 accepts odd size chroma planes
resampler = FindStr(kernel, "lanczos")>0 && kernel!="lanczos4" || FindStr(kernel, "blackman")>0 || FindStr(kernel, "sinc")>0 ? \
kernel + "Resize(" + String(nw) + "," + String(nh) + cplaceH + cplaceV +",taps= int("+bc_b+"))": \
kernel == "nnedi3" ? "nnedi3resize(" + String(nw) + "," + String(nh) + ")" : \
kernel == "bicubic"? "BicubicResize(" + String(nw) + "," + String(nh) + cplaceH + cplaceV +",b="+bc_b+",c="+bc_c+")" : \
kernel == "gauss" ? "GaussResize(" + String(nw) + "," + String(nh) + cplaceH + cplaceV +",p=p)" : \
kernel + "Resize(" + String(nw) + "," + String(nh) + cplaceH + cplaceV +")"
Cb = p_type4 ? Cb : Eval("Cb." + resampler)
Cr = p_type4 ? Cr : Eval("Cr." + resampler)
CombinePlanes(Y, Cb, Cr, planes="YUV", pixel_type="YUV"+p_type[1]+p_type[2])
}
tv_in!=tv_out ? propSet("_ColorRange", tv_out ? 1 : 0) : last
propSet("_Matrix", matrix[3])
propSet("_Model", 5 ) }
# RGB' to YcCbcCrc 444 function (for Rec.2020CL)
function RGB_to_YcCbcCrc (clip RGB, clip "Gx", clip "Bx", string "matrix", string "EOTFi", float_array "coef", bool "tv_in", bool "tv_out", bool "Array") {
isy = isy(RGB)
bi = BitsPerComponent(RGB)
fs = propNumElements (RGB,"_ColorRange") > 0 ? \
propGetInt (RGB,"_ColorRange") == 0 : true
tr = propNumElements (RGB,"_Transfer") > 0 ? \
propGetInt (RGB,"_Transfer") == 8 : false
lut = bi > 12 || isRunTime(RGB,!isy) ? 0 : 2
mat = Default (matrix, "709")
trc = Default (EOTFi, "1886")
coef = Default (coef, color_coef(mat=mat))
tvi = Default (tv_in, !fs)
tvo = Default (tv_out, false)
arc = Default (Array, isy)
trcl = tr ? "linear" : trc
mat = color_Fuzzy_Search(mat)
s_gam = moncurve_coef(trc) # some values are getting crushed in the Rec709 transfer
bi32 = bi == 32 ? "" : "range_half +"
rangeC_PC = tvi ? "range_half - range_max cmax cmin - / * range_half + " : ""
rangeC_TV = tvo ? "range_half + cmax cmin - range_max / * cmin + " : bi32
clp = !isy ? ExtractClip(RGB) : nop()
R = !isy ? clp[0] : RGB
B = !isy ? clp[2] : Bx
clp = !isy ? CCTF(RGB, trcl, true, tvi, false).ExtractClip() : nop()
Rln = !isy ? clp[0] : CCTF(RGB, trcl, true, tvi, false)
Gln = !isy ? clp[1] : CCTF(Gx, trcl, true, tvi, false)
Bln = !isy ? clp[2] : CCTF(Bx, trcl, true, tvi, false)
ca = 1.0 + s_gam[1]
cb = 1.0 / s_gam[0]
Pb = 1 / (2. * (ca*(1.0 - pow(coef[2], cb))))
Pr = 1 / (2. * (ca*(1.0 - pow(coef[0], cb))))
Nb = 1 / (-2. * (ca*(1.0 - pow(coef[0] + coef[1], cb)) - 1.0))
Nr = 1 / (-2. * (ca*(1.0 - pow(coef[1] + coef[2], cb)) - 1.0))
Y = Expr(Rln, Gln, Bln, "x "+string(coef[0])+" * y "+string(coef[1])+" * + z "+string(coef[2])+" * + ")
Yx = CCTF(Y, trc, false, false, false).propSet("_Matrix", mat[3]).propSet("_Model", 11)
Cbc = Expr(B, Yx, ex_dlut(Format("x "+rangeC_PC+" y - A@ 0 <= A {Nb} * A {Pb} * ? "+rangeC_TV+" "), bi, true), lut=lut)
Crc = Expr(R, Yx, ex_dlut(Format("x "+rangeC_PC+" y - A@ 0 <= A {Nr} * A {Pr} * ? "+rangeC_TV+" "), bi, true), lut=lut)
Yc = !tvo ? Yx : CCTF(Y, trc, false, false, true)
arc ? [Yc, Cbc, Crc] : CombinePlanes(Yc, Cbc, Crc, planes="YUV") }
# WIP. YcCbcCrc 444 to RGB' function (for Rec.2020CL)
function YcCbcCrc_to_RGB (clip YUV, clip "Cb", clip "Cr", string "matrix", string "EOTFi", float_array "coef", string "kernel", bool "tv_in", bool "tv_out", bool "Array") {
isy = isy(YUV)
bi = BitsPerComponent(YUV)
fs = propNumElements (YUV,"_ColorRange") > 0 ? \
propGetInt (YUV,"_ColorRange") == 0 : false
tr = propNumElements (YUV,"_Transfer") > 0 ? \
propGetInt (YUV,"_Transfer") == 8 : false
lut = bi > 12 || isRunTime(YUV,false) ? 0 : 2
mat = Default (matrix, "709")
trc = Default (EOTFi, "1886")
knn = Default (kernel, "CatRom")
coef = Default (coef, color_coef(mat=mat))
tvi = Default (tv_in, !fs)
tvo = Default (tv_out, false)
arc = Default (Array, isy)
trcl = tr ? "linear" : trc
mat = color_Fuzzy_Search(mat)
Y444 = ConvertFormat(YUV,1,1,fmt_out="444",kernel_c=knn,tv_in=tvi,tv_out=tvi)
clp = !isy ? ExtractClip(Y444) : nop()
Y = !isy ? clp[0] : YUV
Cb = !isy ? clp[1] : Cb # Need to scale up
Cr = !isy ? clp[2] : Cr
bi32m = bi == 32 ? "" : "range_half -"
bi32p = bi == 32 ? "" : "range_half +"
rangeC_PC = tvi ? ""+bi32m+" range_max cmax cmin - / *" : bi32m
s_gam = moncurve_coef(trc)
a = 1.0 + s_gam[1]
b = 1.0 / s_gam[0]
Pb = 2. * (a*(1.0 - pow(coef[2], b)))
Pr = 2. * (a*(1.0 - pow(coef[0], b)))
Nb = -2. * (a*(1.0 - pow(coef[0] + coef[1], b)) - 1.0)
Nr = -2. * (a*(1.0 - pow(coef[1] + coef[2], b)) - 1.0)
Y = CCTF(Y, trcl, true, tvi, false)
Bx = Expr(Cb, Y, ex_dlut(Format("x "+rangeC_PC+" A@ 0 <= A {Nb} * y "+bi32m+" B@ + A {Pb} * B + ? "+bi32p), bi, true), lut=lut)
Rx = Expr(Cr, Y, ex_dlut(Format("x "+rangeC_PC+" A@ 0 <= A {Nr} * y "+bi32m+" B@ + A {Pr} * B + ? "+bi32p), bi, true), lut=lut)
Gx = Expr(Y, Bx, Rx, "x "+string(coef[2])+" y * - "+string(coef[0])+" z * - "+string(coef[1])+" / ", optSingleMode=true)
if (arc) {
R = CCTF(Rx, trc, false, false, tvo).propSet("_Matrix", 0).propSet("_Model", 1)
B = CCTF(Bx, trc, false, false, tvo)
G = CCTF(Gx, trc, false, false, tvo)
[R, G, B]
} else {
CombinePlanes(Rx, Gx, Bx, planes="RGB")
CCTF(trc, false, false, tvo)
propSet("_Matrix", 0)
propSet("_Model", 1) } }
##
## Y'UV and Y'IQ analogue color models
##
## The chroma subcarriers were strictly defined by their
## bandwidths so they were nor full nor narrow/legal range
##
## Luma in the other hand could be both, depending on region:
## 0-235 Y'UV PAL
## 0-235 Y'UV NTSC-J
## 16-235 Y'UV NTSC
##
## * Since 1979 the barely used FCC NTSC primaries along its Y'IQ color model
## gave way to the then PAL-only Y'UV color model for all regions, therefore
## using the same bandwidths for both chroma subcarriers.
##
## * A few PAL regions though still used a different color model like Y'DbDr.
##
## For a summary of the explicitly used matrices and explanations check this post:
## https://forum.doom9.org/showthread.php?p=1989126
##
# Bymax 0.885515
# Rymax 0.701088
# R'G'B' full range to Decorrelated Intermediate (Y,B-Y,R-Y)
# Rows should sum to 0, except first one which sums 1
global g_Luma = [0.298912, 0.586603, 0.114485]
global g_RGB_to_YByRy = [g_Luma[0], g_Luma[1], g_Luma[2],\
-g_Luma[0], -g_Luma[1], 1-g_Luma[2],\
1-g_Luma[0], -g_Luma[1], -g_Luma[2]]
# Y'UV to Y'IQ. Y'IQ is the same as Y'UV but with chroma axes rotated 33º
# The derivations of the coefficients are to be computed in radians
# so it matches the known RGB_to_YIQ matrix coefficients we all know (and love).
toRad = 33*(pi/180)
global g_YUV_to_YIQ = [1.0, 0.00000000, 0.00000000,\
0.0, -sin(toRad), cos(toRad),\
0.0, cos(toRad), sin(toRad)]
# R'G'B' to Y'UV (PAL -later also NTSC- analogue color model)
# Chroma range (U: ±0.435812284313725, V: ±0.615857694117647)
# Chroma range centered at unsigned range_size when not in float type, so V channel might clip when using 16-bit int and below
function RGB_to_YUVp (clip RGB, clip "G", clip "B", bool "tv_in", bool "tv_out", bool "Array") {
isy = isy(RGB)
bi = BitsPerComponent(RGB)
fs = propNumElements (RGB,"_ColorRange") > 0 ? \
propGetInt (RGB,"_ColorRange") == 0 : true
b32 = bi == 32
tvi = Default (tv_in, !fs)
tvo = Default (tv_out, true) # When 'True' 16-235 (NTSC-U), when 'False' 0-235 (PAL, NTSC-J)
arc = Default (Array, isy)
RGB = tvi ? SMPTE_legal (RGB,false) : RGB
clp = !isy ? ExtractClip (RGB) : nop()
R = !isy ? clp[0] : tvi ? RGB.SMPTE_legal(false) : RGB
G = !isy ? clp[1] : tvi ? G. SMPTE_legal(false) : G
B = !isy ? clp[2] : tvi ? B.SMPTE_legal(false) : B
UVf = b32 ? "" : "range_half + "
rangeTV = tvo ? " ymax ymin - range_max / * ymin +" : " ymax range_max / *"
sclU = ((0.5*(235-16)+16)/255.) # This yields Luma grey at around 0.49216 or 125.5 in 8-bit
sclV = (240-16) /255. # This yields Chroma range at around 0.87843 or 224 in 8-bit
Yrow = g_Luma
Urow = ArrayOp(sclU,[g_RGB_to_YByRy[3],g_RGB_to_YByRy[4],g_RGB_to_YByRy[5]],"*")
Vrow = ArrayOp(sclV,[g_RGB_to_YByRy[6],g_RGB_to_YByRy[7],g_RGB_to_YByRy[8]],"*")
Y = ex_dlut("x "+string(Yrow[0])+" * y "+string(Yrow[1])+" * + z "+string(Yrow[2])+" * + "+rangeTV+"", bi, false)
U = ex_dlut("x "+string(Urow[0])+" * y "+string(Urow[1])+" * + z "+string(Urow[2])+" * + "+UVf , bi, false)
V = ex_dlut("x "+string(Vrow[0])+" * y "+string(Vrow[1])+" * + z "+string(Vrow[2])+" * + "+UVf , bi, false)
if (arc) {
Y = Expr(R, G, B, Y, optSingleMode=true).propSet("_Matrix", 5).propSet("_Model", 42)
U = Expr(R, G, B, U, optSingleMode=true)
V = Expr(R, G, B, V, optSingleMode=true)
[Y, U, V]
} else {
Expr(R, G, B, Y, U, V, optSingleMode=true, format=b32?"YUV444PS":"YUV444P"+string(bi))
propSet("_ColorRange", tvo ? 1 : 0)
propSet("_Matrix", 5)
propSet("_Model", 42) } }
# Y'UV 444 (PAL -later also NTSC- analogue color model) to R'G'B'
function YUVp_to_RGB (clip YUV, clip "U", clip "V", bool "tv_in", bool "tv_out", bool "Array") {
isy = isy(YUV)
bi = BitsPerComponent(YUV)
fs = propNumElements (YUV,"_ColorRange") > 0 ? \
propGetInt (YUV,"_ColorRange") == 0 : true
b32 = bi == 32
tvi = Default (tv_in, !fs)
tvo = Default (tv_out, false)
arc = Default (Array, isy)
clp = !isy ? ExtractClip(YUV) : nop()
Y = !isy ? clp[0] : YUV
U = !isy ? clp[1] : U
V = !isy ? clp[2] : V
UVf = !b32 ? " range_half - " : ""
rangePC = tvi ? " ymin - range_max ymax ymin - / *" : " range_max ymax / *"
rangeTV = tvo ? " ymax ymin - range_max / * ymin +" : ""
# Derived from inverting RGB_to_YUVp coeffs
YUVtoRGB = [1.0000000000000000, 1.00000000000000000, 1.000000000000000,\
-0.0000000293788260, -0.39655256271362305, 2.031872510910034,\
1.1383928060531616, -0.58008438348770140, 0.000000000000000]
R = ex_dlut("x "+rangePC+" y "+UVf+string(YUVtoRGB[3])+" * + z "+UVf+string(YUVtoRGB[6])+" * + "+rangeTV, bi, false)
G = ex_dlut("x "+rangePC+" y "+UVf+string(YUVtoRGB[4])+" * + z "+UVf+string(YUVtoRGB[7])+" * + "+rangeTV, bi, false)
B = ex_dlut("x "+rangePC+" y "+UVf+string(YUVtoRGB[5])+" * + z "+UVf+string(YUVtoRGB[8])+" * + "+rangeTV, bi, false)
if (arc) {
R = Expr(Y, U, V, R, optSingleMode=true).propSet("_Matrix", 0).propSet("_Model", 1)
G = Expr(Y, U, V, G, optSingleMode=true)
B = Expr(Y, U, V, B, optSingleMode=true)
[R, G, B]
} else {
Expr(Y, U, V, R, G, B, optSingleMode=true, format="RGBP"+(bi>16?"S":string(bi)))
propSet("_ColorRange", tvo ? 1 : 0)
propSet("_Matrix", 0)
propSet("_Model", 1) } }
# Y'CbCr 444 to Y'UV 444 (PAL -later also NTSC- analogue color model)
# Chroma range (U: ±0.435812284313725, V: ±0.615857694117647)
# Chroma range centered at unsigned range_size when not in float type, so V channel might clip when using 16-bit int and below
function YCC_to_YUVp (clip YCC, clip "Cb", clip "Cr", bool "tv_in", bool "tv_out", bool "Array") {
isy = isy(YCC)
bi = BitsPerComponent(YCC)
fs = propNumElements (YCC,"_ColorRange") > 0 ? \
propGetInt (YCC,"_ColorRange") == 0 : true
b32 = bi == 32
lut = b32 || isRunTime(YCC,false) ? 0 : 1
tvi = Default (tv_in, !fs)
tvo = Default (tv_out, true) # When 'True' 16-235 (NTSC-U), when 'False' 0-235 (PAL, NTSC-J)
arc = Default (Array, isy)
if (!isy) {
clp = ExtractClip(YCC)
Yc = clp[0]
Cb = clp[1]
Cr = clp[2] }
UVf = !b32 ? " range_half - " : ""
rangeC_PC = tvi ? " range_max cmax cmin - / * " : ""
rangeC_pos = !b32 ? " range_half +" : ""
sclU = ((0.5*(235-16)+16)/255.) # This yields Luma grey at around 0.49216 or 125.5 in 8-bit
sclV = (240-16) /255. # This yields Chroma range at around 0.87843 or 224 in 8-bit
Yrow = g_Luma
Cb_to_U = (sclU*g_RGB_to_YByRy[5]) / 0.5 # = 0.871624568627450
Cr_to_V = (sclV*g_RGB_to_YByRy[6]) / 0.5 # = 1.231715388235294
Y = ex_dlut(!tvi && tvo ? "x range_max ymax ymin - / * ymin +" : !tvi && !tvo ? "x ymax range_max / *" : \
tvi && !tvo ? "x ymin - ymax range_max / *" : "", bi, true)
U = ex_dlut("x "+UVf+string(Cb_to_U)+rangeC_PC+" * "+rangeC_pos, bi, true)
V = ex_dlut("x "+UVf+string(Cr_to_V)+rangeC_PC+" * "+rangeC_pos, bi, true)
if (arc) {
Y = Expr(Yc, Y, optSingleMode=false, lut=lut).propSet("_Matrix", 5).propSet("_Model", 42)
U = Expr(Cb, U, optSingleMode=false, lut=lut)
V = Expr(Cr, V, optSingleMode=false, lut=lut)
[Y, U, V]
} else {
Expr(YCC, Y, U, V, optSingleMode=false, format=b32?"YUV444PS":"YUV444P"+string(bi))
propSet("_ColorRange", tvo ? 1 : 0)
propSet("_Matrix", 5)
propSet("_Model", 42) } }
# Y'UV (PAL -later also NTSC- analogue color model) to Y'CbCr 444
function YUVp_to_YCC (clip YUV, clip "U", clip "V", bool "tv_in", bool "tv_out", bool "Array") {
isy = isy(YUV)
bi = BitsPerComponent(YUV)
fs = propNumElements (YUV,"_ColorRange") > 0 ? \
propGetInt (YUV,"_ColorRange") == 0 : true
b32 = bi == 32
lut = b32 || isRunTime(YUV,false) ? 0 : 1
tvi = Default (tv_in, !fs)
tvo = Default (tv_out, true)
arc = Default (Array, isy)
if (!isy) {
clp = ExtractClip(YUV)
Y = clp[0]
U = clp[1]
V = clp[2] }
UVf = !b32 ? " range_half - " : ""
rangeC_PC = tvo ? " cmax cmin - range_max / * " : ""
rangeC_pos = !b32 ? " range_half +" : ""
# Derived from inverting YCC_to_YUVp coeffs
YUVtoYCC = [1.0, 0.0000000000000000, 0.0000000000000000,\
0.0, 1.1472828388214111, 0.0000000000000000,\
0.0, 0.0000000000000000, 0.8118758201599121]
Yc = ex_dlut(!tvi && tvo ? "x ymax ymin - ymax / * ymin +" : !tvi && !tvo ? "x range_max ymax / *" : \
tvi && !tvo ? "x ymin - range_max ymax / *" : "", bi, true)
Cb = ex_dlut("x "+UVf+string(YUVtoYCC[4])+rangeC_PC+" * "+rangeC_pos, bi, true)
Cr = ex_dlut("x "+UVf+string(YUVtoYCC[8])+rangeC_PC+" * "+rangeC_pos, bi, true)
if (arc) {
Yc = Expr(Y, Yc, optSingleMode=false, lut=lut).propSet("_Matrix", 5).propSet("_Model", 5)
Cb = Expr(U, Cb, optSingleMode=false, lut=lut)
Cr = Expr(V, Cr, optSingleMode=false, lut=lut)
[Yc, Cb, Cr]
} else {
Expr(YUV, Yc, Cb, Cr, optSingleMode=false, format=b32?"YUV444PS":"YUV444P"+string(bi))
propSet("_ColorRange", tvo ? 1 : 0)
propSet("_Matrix", 5)
propSet("_Model", 5) } }
# R'G'B' to Y'IQ 444 (NTSC 1953 analogue color model)
# Chroma range (I: ±0.59662443399429320, Q: ±0.52277213335037230)
# Chroma range centered at range_size when not in float type, so I/Q channels might clip when using 16-bit int and below
function RGB_to_YIQ (clip RGB, clip "G", clip "B", bool "tv_in", bool "tv_out", bool "Array") {
isy = isy(RGB)
bi = BitsPerComponent(RGB)
fs = propNumElements (RGB,"_ColorRange") > 0 ? \
propGetInt (RGB,"_ColorRange") == 0 : true
b32 = bi == 32
tvi = Default (tv_in, !fs)
tvo = Default (tv_out, true) # When 'True' 16-235 (NTSC-U), when 'False' 0-255 (no standard)
arc = Default (Array, isy)
RGB = tvi ? SMPTE_legal (RGB,false) : RGB
clp = !isy ? ExtractClip (RGB) : nop()
R = !isy ? clp[0] : tvi ? RGB.SMPTE_legal(false) : RGB
G = !isy ? clp[1] : tvi ? G. SMPTE_legal(false) : G
B = !isy ? clp[2] : tvi ? B.SMPTE_legal(false) : B
UVf = b32 ? "" : "range_half + "
rangeTV = tvo ? " ymax ymin - range_max / * ymin +" : " range_max *"
sclU = ((0.5*(235-16)+16)/255.) # This yields Luma grey at around 0.49216 or 125.5 in 8-bit
sclV = (240-16) /255. # This yields Chroma range at around 0.87843 or 224 in 8-bit
Yrow = g_Luma
Urow = ArrayOp(sclU,[g_RGB_to_YByRy[3],g_RGB_to_YByRy[4],g_RGB_to_YByRy[5]],"*")
Vrow = ArrayOp(sclV,[g_RGB_to_YByRy[6],g_RGB_to_YByRy[7],g_RGB_to_YByRy[8]],"*")
YUV = ArrayAppend(ArrayAppend(Yrow, Urow), Vrow)
YIQ = MatrixDot(g_YUV_to_YIQ, YUV) # Rotating Y'UV chroma axes by 33º
Y = ex_dlut("x "+string(YIQ[0])+" * y "+string(YIQ[1])+" * + z "+string(YIQ[2])+" * + "+rangeTV+"", bi, false)
I = ex_dlut("x "+string(YIQ[3])+" * y "+string(YIQ[4])+" * + z "+string(YIQ[5])+" * + "+UVf , bi, false)
Q = ex_dlut("x "+string(YIQ[6])+" * y "+string(YIQ[7])+" * + z "+string(YIQ[8])+" * + "+UVf , bi, false)
if (arc) {
Y = Expr(R, G, B, Y, optSingleMode=true).propSet("_Matrix", 4).propSet("_Model", 3)
I = Expr(R, G, B, I, optSingleMode=true)
Q = Expr(R, G, B, Q, optSingleMode=true)
[Y, I, Q]
} else {
Expr(R, G, B, Y, I, Q, optSingleMode=true, format=b32?"YUV444PS":"YUV444P"+string(bi))
propSet("_ColorRange", tvo ? 1 : 0)
propSet("_Matrix", 4)
propSet("_Model", 3) } }
# Y'IQ 444 (NTSC 1953 analogue color model) to R'G'B'
function YIQ_to_RGB (clip YIQ, clip "I", clip "Q", bool "tv_in", bool "tv_out", bool "Array") {
isy = isy(YIQ)
bi = BitsPerComponent(YIQ)
fs = propNumElements (YIQ,"_ColorRange") > 0 ? \
propGetInt (YIQ,"_ColorRange") == 0 : true
b32 = bi == 32
tvi = Default (tv_in, !fs)
tvo = Default (tv_out, false)
arc = Default (Array, isy)
clp = !isy ? ExtractClip(YIQ) : nop()
Y = !isy ? clp[0] : YIQ
I = !isy ? clp[1] : I
Q = !isy ? clp[2] : Q
UVf = !b32 ? " range_half - " : ""
rangePC = tvi ? " ymin - range_max ymax ymin - / *" : ""
rangeTV = tvo ? " ymax ymin - range_max / * ymin +" : ""
# Derived from inverting RGB_to_YIQ coeffs
YIQtoRGB = [1.0000000000000000, 1.00000000000000000, 1.0000000000000000,\
0.9547365307807922, -0.27052170038223267, -1.1066371202468872,\
0.6200131177902222, -0.64851361513137820, 1.7040715217590332]
R = ex_dlut("x "+rangePC+" y "+UVf+string(YIQtoRGB[3])+" * + z "+UVf+string(YIQtoRGB[6])+" * + "+rangeTV, bi, false)
G = ex_dlut("x "+rangePC+" y "+UVf+string(YIQtoRGB[4])+" * + z "+UVf+string(YIQtoRGB[7])+" * + "+rangeTV, bi, false)
B = ex_dlut("x "+rangePC+" y "+UVf+string(YIQtoRGB[5])+" * + z "+UVf+string(YIQtoRGB[8])+" * + "+rangeTV, bi, false)
if (arc) {
R = Expr(Y, I, Q, R, optSingleMode=true).propSet("_Matrix", 0).propSet("_Model", 1)
G = Expr(Y, I, Q, G, optSingleMode=true)
B = Expr(Y, I, Q, B, optSingleMode=true)
[R, G, B]
} else {
Expr(Y, I, Q, R, G, B, optSingleMode=true, format="RGBP"+(bi>16?"S":string(bi)))
propSet("_ColorRange", tvo ? 1 : 0)
propSet("_Matrix", 0)
propSet("_Model", 1) } }
# Y'CbCr 444 to Y'IQ 444 (NTSC 1953 analogue color model)
# Chroma range (I: ±0.59662443399429320, Q: ±0.52277213335037230)
# Chroma range centered at range_size when not in float type, so I/Q channels might clip when using 16-bit int and below
function YCC_to_YIQ (clip YCC, clip "Cb", clip "Cr", bool "tv_in", bool "tv_out", bool "Array") {
isy = isy(YCC)
bi = BitsPerComponent(YCC)
fs = propNumElements (YCC,"_ColorRange") > 0 ? \
propGetInt (YCC,"_ColorRange") == 0 : true
b32 = bi == 32
iRn = isRunTime(YCC,false)
lt1 = b32 || iRn ? 0 : 1
lt2 = bi > 12 || iRn ? 0 : 2
tvi = Default (tv_in, !fs)
tvo = Default (tv_out, true) # When 'True' 16-235 (NTSC-U), when 'False' 0-255 (no standard)
arc = Default (Array, isy)
clp = !isy ? ExtractClip(YCC) : nop()
Yc = !isy ? clp[0] : YCC
Cb = !isy ? clp[1] : Cb
Cr = !isy ? clp[2] : Cr
UVf = !b32 ? " range_half - " : ""
rangeC_PC = tvi ? " range_max cmax cmin - / * " : ""
rangeC_pos = !b32 ? " range_half +" : ""
# Derivation
# YCbCr->YUV
# Cb->U = 0.435812284313725 / 0.5 = 0.87162456862745
# Cr->V = 0.615857694117647 / 0.5 = 1.231715388235294
#
# YCbCr->YUV->YIQ
# Cb->I = 0.871624568627450 *-sin(33) =-0.474720768297668
# Cb->Q = 0.871624568627450 * cos(33) = 0.731005873796788
# Cr->I = 1.231715388235294 * cos(33) = 1.033003446729065
# Cr->Q = 1.231715388235294 * sin(33) = 0.670840286601698
YCCtoYIQ = [1.0, 0.0000000000000000, 0.0000000000000000,\
0.0, -0.4747207760810852, 0.7310058474540710,\
0.0, 1.0330034494400024, 0.6708403229713440]
Y = ex_dlut(!tvi && tvo ? "x ymax ymin - range_max / * ymin +" : \
tvi && !tvo ? "x ymin - range_max ymax ymin - / *" : "", bi, true)
I = ex_dlut("x "+UVf+string(YCCtoYIQ[4])+rangeC_PC+" * y "+UVf+string(YCCtoYIQ[7])+rangeC_PC+" * + "+rangeC_pos, bi, true)
Q = ex_dlut("x "+UVf+string(YCCtoYIQ[5])+rangeC_PC+" * y "+UVf+string(YCCtoYIQ[8])+rangeC_PC+" * + "+rangeC_pos, bi, true)
if (arc) {
Y = Expr(Yc, Y, optSingleMode=false, lut=lt1).propSet("_Matrix", 4).propSet("_Model", 3)
I = Expr(Cb, Cr, I, optSingleMode=false, lut=lt2)
Q = Expr(Cb, Cr, Q, optSingleMode=false, lut=lt2)
[Y, I, Q]
} else {
I = ReplaceStr(ReplaceStr(I,"y ","z "),"x ","y ")
Q = ReplaceStr(ReplaceStr(Q,"y ","z "),"x ","y ")
Expr(Yc, Cb, Cr, Y, I, Q, optSingleMode=true, format=b32?"YUV444PS":"YUV444P"+string(bi))
propSet("_ColorRange", tvo ? 1 : 0)
propSet("_Matrix", 4)
propSet("_Model", 3) } }
# Y'IQ 444 (NTSC 1953 analogue color model) to Y'CbCr 444
function YIQ_to_YCC (clip YIQ, clip "I", clip "Q", bool "tv_in", bool "tv_out", bool "Array") {
isy = isy(YIQ)
bi = BitsPerComponent(YIQ)
fs = propNumElements (YIQ,"_ColorRange") > 0 ? \
propGetInt (YIQ,"_ColorRange") == 0 : true
b32 = bi == 32
iRn = isRunTime(YIQ,false)
lt1 = b32 || iRn ? 0 : 1
lt2 = bi > 12 || iRn ? 0 : 2
tvi = Default (tv_in, !fs)
tvo = Default (tv_out, true)
arc = Default (Array, isy)
clp = !isy ? ExtractClip(YIQ) : nop()
Y = !isy ? clp[0] : YIQ
I = !isy ? clp[1] : I
Q = !isy ? clp[2] : Q
UVf = !b32 ? " range_half - " : ""
rangeC_PC = tvo ? " cmax cmin - range_max / * " : ""
rangeC_pos = !b32 ? " range_half +" : ""
# Derived from inverting YCC_to_YIQ coeffs
YIQtoYCC = [1.0, 0.0000000000000000, 0.00000000000000000,\
0.0, -0.6248551011085510, 0.68089640140533450,\
0.0, 0.9621924757957458, 0.44217929244041443]
Yc = ex_dlut(!tvi && tvo ? "x ymax ymin - range_max / * ymin +" : \
tvi && !tvo ? "x ymin - range_max ymax ymin - / *" : "", bi, true)
Cb = ex_dlut("x "+UVf+string(YIQtoYCC[4])+rangeC_PC+" * y "+UVf+string(YIQtoYCC[7])+rangeC_PC+" * + "+rangeC_pos, bi, true)
Cr = ex_dlut("x "+UVf+string(YIQtoYCC[5])+rangeC_PC+" * y "+UVf+string(YIQtoYCC[8])+rangeC_PC+" * + "+rangeC_pos, bi, true)
if (arc) {
Yc = Expr(Y, Yc, optSingleMode=false, lut=lt1).propSet("_Matrix", 4).propSet("_Model", 5)
Cb = Expr(I, Q, Cb, optSingleMode=false, lut=lt2)
Cr = Expr(I, Q, Cr, optSingleMode=false, lut=lt2)
[Yc, Cb, Cr]
} else {
Cb = ReplaceStr(ReplaceStr(Cb,"y ","z "),"x ","y ")