forked from Dogway/Avisynth-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformsPack - Models.avsi
2149 lines (1638 loc) · 105 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 v1.0 RC59 (22-10-2022) ##
### ##
### https://forum.doom9.org/showthread.php?t=182825 ##
### 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_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_Oklab / Oklab_to_RGB ##
### RGB_to_XYZ / XYZ_to_RGB ##
### RGB_to_ICtCp / ICtCp_to_RGB ##
### XYZ_to_LMS / LMS_to_XYZ ##
### CAT ##
### CMF ##
### SPD_D ##
### 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.
#-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.
#-470BG - ITU-R Rec. BT.1700 (or legacy ITU-R Rec. BT.601) (625-line = PAL, SECAM).
#-NTSC-J - ARIB TR-B9. Phosphor primaries used in Japan's consumer CRTs along 9300K + 27 MPCD illuminant.
#-sRGB - IEC 61966-2. 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.
#-2020 - ITU-R Rec. BT.2020.
#-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 -
#-DCI-P3 - SMPTE RP 432-2. Digital Cinema.
#-Display-P3 - SMPTE RP 432-1.
#-AP0 - ACES AP0 coefficients used for ACES2065 color space.
#-AP1 - ACES AP1 coefficients used for ACEScg color space.
# S-Gamut -
# S-Gamut3 -
# S-Gamut3 (Venice) -
# S-Gamut3.Cine - Sony Gamut Cinema. A little over DCI-P3 gamut in size, paired with S-Log3 it reproduces a scanned negative film.
# S-Gamut3.Cine (Venice) -
# CinemaDay - Canon Cinema Gamut Daylight (5500K).
# CinemaTungsten - Canon Cinema Gamut Tungsten (3200K).
# CanonP3 - Canon DCI-P3+.
# 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 - 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 3213 - EBU Tech. 3213-E / JEDEC P22 phosphors.
#-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
#-YCbCr - Color format 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.
# YIQ - YCbCr with (YCbCr) UV axes rotated 33º, used for NTSC TV analogue system. 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 YUV but fully reversible to RGB.
# sYUV - "Sharp YUV". Same as YUV 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 was/is 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.
#-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 in the spirit of RLAB. Uses CIECAM02 chromatic adaptation matrix to fix the blue hue issue.
#-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.
# DIN99o* -
# 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 propietary model not much information is known.
# 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.
# 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.
# 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.
# YCbCr to RGB' standalone function (recommended at least 10-bit precision)
function YUV_to_RGB (clip yuv, string "matrix", bool "tv_range_in", bool "tv_range_out", string "kernel", float "b", float "c", float "p", string "cplace", bool "UVrecon") {
matrix = Default (matrix, "709")
kernel = Default (kernel, "Bicubic")
tv_in = Default (tv_range_in, !(matrix=="JPEG" || matrix=="JPG"))
tv_out = Default (tv_range_out, false)
cplace = Default (cplace, "")
recon = Default (UVrecon, false)
b = Default (b, 0.0)
c = Default (c, 0.75) # Precise Bicubic
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")
bi = BitsPerComponent(yuv)
lut = bi > 12 || isRunTime(yuv,false) ? 0 : 2
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[1]>=0.?bc[0]:b)
bc_c = string(bc[1]>=0.?bc[1]:c)
kernel = bc[1]>=0. ? "Bicubic" : 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 (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 = 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) {
cores = SI_PhysicalCores()
threads = SI_LogicalCores()
ref = Y.KNLMeansCL(0, 16, 0, pow(1.464968620512209618455732713658, 6.4), "auto", wref=1)
Luma = ref.ConvertBits(8,dither=-1,fulls=!tv_in).nnedi3_rpow2(rfactor=2, nns=1, qual=1, etype=1, nsize=0, threads=cores, prefetch=(threads+cores)/2,range=fs?1:2, cshift="spline16resize").ConvertBits(bi,fulls=!tv_in)
Uu = Uor.ConvertBits(8,dither=-1,fulls=!tv_in).nnedi3_rpow2(rfactor=2, nns=1, qual=1, etype=1, nsize=0, threads=cores, prefetch=(threads+cores)/2, fwidth=w*2, fheight=h*2, ep0=6, cshift="blackmanresize", mpeg2=false,range=fs?1:2).ConvertBits(bi,fulls=!tv_in)
Vu = Vor.ConvertBits(8,dither=-1,fulls=!tv_in).nnedi3_rpow2(rfactor=2, nns=1, qual=1, etype=1, nsize=0, threads=cores, prefetch=(threads+cores)/2, fwidth=w*2, fheight=h*2, ep0=6, cshift="blackmanresize", mpeg2=false,range=fs?1:2).ConvertBits(bi,fulls=!tv_in)
Unew = Uu.KNLMeansCL(0, 16, 0, 6.4, "auto", wref=0, rclip=Luma).BicubicResize(w, h, b=0.0, c=0.5)
Vnew = Vu.KNLMeansCL(0, 16, 0, 6.4, "auto", wref=0, rclip=Luma).BicubicResize(w, h, b=0.0, c=0.5)
U = ex_LFR(Unew, Uu.BicubicResize(w, h, b=0.0, c=0.5), LFR=w/1.1)
V = ex_LFR(Vnew, Vu.BicubicResize(w, h, b=0.0, c=0.5), LFR=w/1.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") {
RGB = YcCbcCrc_to_RGB( Y, U, V, matrix[0], coef, tv_in, tv_out)
R = RGB[0] G = RGB[1] B = RGB[2]
} else {
scale_y = tv_in ? (256 / 219.) : 1.0
scale_uv = tv_in ? (256 / 112.) : 2.0
Kr = 1. - coef[0]
Kb = 1. - coef[2]
m0 = scale_y m1 = 0.0 m2 = scale_uv * Kr
m3 = scale_y m4 = scale_uv * ( -Kb * coef[2] / coef[1]) m5 = scale_uv * ( -Kr * coef[0] / coef[1])
m6 = scale_y m7 = scale_uv * Kb m8 = 0.0
rangeY = tv_in ? "ymin - " : ""
range_TV = tv_out ? "ymax ymin - range_max / * ymin +" : ""
UVf = bi < 32 ? "range_half - " : ""
R = Expr(Y, V, ex_dlut( "x "+rangeY+" "+string(m0)+" * y "+UVf + string(m2)+" * + "+range_TV+"", bi, false), optSingleMode=false, lut=lut)
G = Expr(Y, U, V, ex_dlut( "x "+rangeY+" "+string(m3)+" * y "+UVf + string(m4)+" * + z "+UVf + string(m5)+" * + "+range_TV+"", bi, false), optSingleMode=true , lut= 0)
B = Expr(Y, U, ex_dlut( "x "+rangeY+" "+string(m6)+" * y "+UVf + string(m7)+" * + "+range_TV+"", bi, false), optSingleMode=false, lut=lut)
}
CombinePlanes(R, G, B, planes="RGB")
tv_in!=tv_out ? propSet("_ColorRange", tv_out ? 1 : 0) : last }
# RGB' to YCbCr standalone function (recommended at least 10-bit precision)
function RGB_to_YUV (clip rgb, string "matrix", bool "tv_range_in", bool "tv_range_out", string "pixel_type", string "kernel", float "b", float "c", float "p", string "cplace") {
matrix = Default (matrix, "sRGB")
p_type = Default (pixel_type, "420") # target YUV pixel_type
kernel = Default (kernel, "Bicubic")
tv_in = Default (tv_range_in, false)
tv_out = Default (tv_range_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")
bi = BitsPerComponent(rgb)
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[1]>=0.?bc[0]:b)
bc_c = string(bc[1]>=0.?bc[1]:c)
kernel = bc[1]>=0. ? "Bicubic" : 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") {
YCbCr = RGB_to_YcCbcCrc( Rx, Gx, Bx, matrix[0], coef, tv_in, tv_out)
Y = YCbCr[0] Cb = YCbCr[1] Cr = YCbCr[2]
} else {
scale_y = !tv_in && tv_out ? ( 219 / 256.) : 1.0
scale_uv = !tv_in && tv_out ? ( 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
m6 = scale_uv m7 = scale_uv * (-coef[1] / Kr) m8 = scale_uv * (-coef[2] / Kr)
UVf = bi < 32 ? "" : "range_half -"
rangeY_TV = tv_out ? tv_in ? "" : "ymin +" : tv_in ? "ymin - range_max ymax ymin - / *" : ""
rangeC_TV = !tv_out && tv_in ? "range_half - range_max cmax cmin - / * range_half + "+UVf+"" : UVf
Y = Expr(Rx, Gx, Bx, ex_dlut(" x "+string(m0)+" * y "+string(m1)+" * + z "+string(m2)+" * + "+rangeY_TV+"", bi, false), optSingleMode=true)
Cb = Expr(Rx, Gx, Bx, ex_dlut("range_half x "+string(m3)+" * + y "+string(m4)+" * + z "+string(m5)+" * + "+rangeC_TV+"", bi, false), optSingleMode=true)
Cr = Expr(Rx, Gx, Bx, ex_dlut("range_half x "+string(m6)+" * + y "+string(m7)+" * + z "+string(m8)+" * + "+rangeC_TV+"", bi, false), 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 = 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 }
# RGB' to YcCbcCrc function (for Rec.2020CL)
function RGB_to_YcCbcCrc (clip RGB, clip "Gx", clip "Bx", string "matrix", float_array "coef", bool "tv_in", bool "tv_out") {
isy = isy(RGB)
bi = BitsPerComponent(RGB)
lut = bi > 12 || isRunTime(RGB,!isy) ? 0 : 2
mat = Default (matrix, "709")
coef = Default (coef, color_coef(mat=mat))
tvi = Default (tv_in, false)
tvo = Default (tv_out, false)
mat = color_Fuzzy_Search(mat)
s_gam = moncurve_coef(mat[2]) # 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, mat[2], true, tvi, false).ExtractClip() : nop()
Rln = !isy ? clp[0] : CCTF(RGB, mat[2], true, tvi, false)
Gln = !isy ? clp[1] : CCTF(Gx, mat[2], true, tvi, false)
Bln = !isy ? clp[2] : CCTF(Bx, mat[2], 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, mat[2], false, false, false)
Cbc = Expr(B, Yx, ex_dlut(Format("x "+rangeC_PC+" y - A@ 0 <= A {Nb} * A {Pb} * ? "+rangeC_TV+" "), bi, !tvi), lut=lut)
Crc = Expr(R, Yx, ex_dlut(Format("x "+rangeC_PC+" y - A@ 0 <= A {Nr} * A {Pr} * ? "+rangeC_TV+" "), bi, !tvi), lut=lut)
Yc = !tvo ? Yx : CCTF(Y, mat[2], false, false, true)
isy ? [Yc, Cbc, Crc] : CombinePlanes(Yc, Cbc, Crc, planes="YUV")
!isy && mat[0] == "2020CL" ? propSet("_Matrix", 10) : last }
# WIP. YcCbcCrc to RGB' function (for Rec.2020CL)
function YcCbcCrc_to_RGB (clip YUV, clip "Cb", clip "Cr", string "matrix", float_array "coef", bool "tv_in", bool "tv_out") {
isy = isy(YUV)
bi = BitsPerComponent(YUV)
lut = bi > 12 || isRunTime(YUV,false) ? 0 : 2
mat = Default (matrix, "709")
coef = Default (coef, color_coef(mat=mat))
tvi = Default (tv_in, false)
tvo = Default (tv_out, false)
mat = color_Fuzzy_Search(mat)
clp = !isy ? ExtractClip(YUV) : nop()
Y = !isy ? clp[0] : YUV
Cb = !isy ? clp[1] : Cb
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(mat[2])
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, mat[2], 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, !tvi), lut=lut)
Rx = Expr(Cr, Y, ex_dlut(Format("x "+rangeC_PC+" A@ 0 <= A {Nr} * y "+bi32m+" B@ + A {Pr} * B + ? "+bi32p), bi, !tvi), lut=lut)
Gx = Expr(Y, Bx, Rx, "x "+string(coef[2])+" y * - "+string(coef[0])+" z * - "+string(coef[1])+" / ", optSingleMode=true)
if (isy) {
B = CCTF(Bx, mat[2], false, false, tvo)
R = CCTF(Rx, mat[2], false, false, tvo)
G = CCTF(Gx, mat[2], false, false, tvo)
[R, G, B]
} else {
CombinePlanes(Rx, Gx, Bx, planes="RGB")
CCTF(mat[2], false, false, tvo)
} }
# YCoCg, YCoCgR and YUVr compress better than plain YUV. YCoCg variants a tiny bit over YUVr, and YUVr and YCoCgR being losslessly reversible (RCT) with RGB
# These formats are ColorSpace and Chroma Subsampling independent, as well as signal range (full or legal)
# RGB' to YCoCg function
function RGB_to_YCoCg (clip RGB, clip "G", clip "B", bool "tv_in", bool "tv_out") {
isy = isy(RGB)
bi = BitsPerComponent(RGB)
lut = bi > 12 || isRunTime(RGB,!isy) ? 0 : 2
tvi = Default (tv_in, false)
tvo = Default (tv_out, false)
clp = !isy ? ExtractClip(RGB) : nop()
R = !isy ? clp[0] : RGB
G = !isy ? clp[1] : G
B = !isy ? clp[2] : B
b32 = bi == 32 ? "" : "range_half +"
if (isy) {
Y = Expr(R, G, B, ex_dlut("x z + 0.25 * y 0.5 * + ", bi, !tvi), optSingleMode=true, lut= 0)
Cg = Expr(R, G, B, ex_dlut("y 0.5 * x z + 0.25 * - "+b32, bi, !tvi), optSingleMode=true, lut= 0)
Co = Expr(R, B, ex_dlut("x y - 0.5 * "+b32, bi, !tvi), optSingleMode=false, lut=lut)
[Y, Cg, Co]
} else {
fmt = format_Fuzzy_Search(RGB, "YUV", bi)
Expr(R, G, B, ex_dlut("x z + 0.25 * y 0.5 * + ", bi, !tvi), \
ex_dlut("y 0.5 * x z + 0.25 * - "+b32, bi, !tvi), \
ex_dlut("x z - 0.5 * "+b32, bi, !tvi), format="YUV444"+fmt[2], optSingleMode=true) } }
# YCoCg to RGB' function
function YCoCg_to_RGB (clip YCC, clip "Cg", clip "Co", bool "tv_in", bool "tv_out") {
isy = isy(YCC)
bi = BitsPerComponent(YCC)
lut = bi > 12 || isRunTime(YCC,false) ? 0 : 2
tvi = Default (tv_in, false)
tvo = Default (tv_out, false)
clp = !isy ? ExtractClip(YCC) : nop()
Y = !isy ? clp[0] : YCC
Cg = !isy ? clp[1] : Cg
Co = !isy ? clp[2] : Co
b32 = bi == 32 ? "" : "range_half -"
if (isy) {
R = Expr(Y, Cg, Co, ex_dlut("x y "+b32+" - z "+b32+" + ", bi, !tvi), optSingleMode=true, lut= 0)
G = Expr(Y, Cg, ex_dlut("x y "+b32+" +", bi, !tvi), optSingleMode=false, lut=lut)
B = Expr(Y, Cg, Co, ex_dlut("x y "+b32+" - z "+b32+" - ", bi, !tvi), optSingleMode=true, lut= 0)
[R, G, B]
} else {
fmt = format_Fuzzy_Search(YCC, "RGB", bi)
Expr(Y, Cg, Co, ex_dlut("x y "+b32+" - z "+b32+" + ", bi, !tvi), \
ex_dlut("x y "+b32+" +", bi, !tvi), \
ex_dlut("x y "+b32+" - z "+b32+" - ", bi, !tvi), format="RGB"+fmt[2], optSingleMode=true) } }
# RGB' to YCoCg RCT function
function RGB_to_YCoCgR (clip RGB, clip "G", clip "B", bool "tv_in", bool "tv_out") {
isy = isy(RGB)
bi = BitsPerComponent(RGB)
lut = bi > 12 || isRunTime(RGB,!isy) ? 0 : 2
tvi = Default (tv_in, false)
tvo = Default (tv_out, false)
clp = !isy ? ExtractClip(RGB) : nop()
R = !isy ? clp[0] : RGB
G = !isy ? clp[1] : G
B = !isy ? clp[2] : B
b32i = bi == 32 ? "" : "range_half +"
b32f = bi == 32 ? "" : "range_half -"
Co = Expr(R, B, ex_dlut("x 0.5 * y 0.5 * - "+b32i, bi, !tvi), optSingleMode=false, lut=lut)
Cg = Expr(Co, G, B, ex_dlut("y z x "+b32f+" 0.5 * + - 0.5 * "+b32i, bi, !tvi), optSingleMode=false, lut= 0)
Y = Expr(Co, Cg, B, ex_dlut(" z x "+b32f+" 0.5 * + y "+b32f+" +", bi, !tvi), optSingleMode=false, lut= 0)
isy ? [Y, Cg, Co] : CombinePlanes(Y, Cg, Co, planes="YUV") }
# YCoCg RCT to RGB' function
function YCoCgR_to_RGB (clip YCC, clip "Cg", clip "Co", bool "tv_in", bool "tv_out") {
isy = isy(YCC)
bi = BitsPerComponent(YCC)
lut = bi > 12 || isRunTime(YCC,false) ? 0 : 2
tvi = Default (tv_in, false)
tvo = Default (tv_out, false)
clp = !isy ? ExtractClip(YCC) : nop()
Y = !isy ? clp[0] : YCC
Cg = !isy ? clp[1] : Cg
Co = !isy ? clp[2] : Co
b32 = bi == 32 ? "" : "range_half -"
if (isy) {
G = Expr(Y, Cg, ex_dlut("y "+b32+" Y@ 2 * x Y - +", bi, !tvi), optSingleMode=false, lut=lut)
B = Expr(Y, Cg, Co, ex_dlut("x y "+b32+" - z "+b32+" 0.5 * -", bi, !tvi), optSingleMode=true, lut= 0)
R = Expr(Co, B, ex_dlut("y x "+b32+" 2 * +", bi, !tvi), optSingleMode=false, lut=lut)
[R, G, B]
} else {
fmt = format_Fuzzy_Search(YCC, "RGB", bi)
Expr(Y, Cg, Co, ex_dlut("z "+b32+" dup 0.5 * x y "+b32+" - swap - swap 2 * +", bi, !tvi), \
ex_dlut("y "+b32+" Y@ 2 * x Y - +", bi, !tvi), \
ex_dlut("x y "+b32+" - z "+b32+" 0.5 * -", bi, !tvi), format="RGB"+fmt[2], optSingleMode=true) } }
# RGB' to YCbCr RCT function (Reversible Color Transform, from JPEG 2000, similar to YCoCg)
function RGB_to_YUVr (clip RGB, clip "G", clip "B", bool "tv_in", bool "tv_out") {
isy = isy(RGB)
bi = BitsPerComponent(RGB)
lut = bi > 12 || isRunTime(RGB,!isy) ? 0 : 2
tvi = Default (tv_in, false)
tvo = Default (tv_out, false)
clp = !isy ? ExtractClip(RGB) : nop()
R = !isy ? clp[0] : RGB
G = !isy ? clp[1] : G
B = !isy ? clp[2] : B
b32 = bi == 32 ? "" : "range_half +"
if (isy) {
Y = Expr(R, G, B, ex_dlut("x z + 0.25 * y 0.5 * + ", bi, !tvi), optSingleMode=true, lut= 0)
Cb = Expr( G, B, ex_dlut("y x - "+b32, bi, !tvi), optSingleMode=false, lut=lut)
Cr = Expr(R, G, ex_dlut("x y - "+b32, bi, !tvi), optSingleMode=false, lut=lut)
[Y, Cb, Cr]
} else {
fmt = format_Fuzzy_Search(RGB, "YUV", bi)
Expr(R, G, B, ex_dlut("x z + 0.25 * y 0.5 * + ", bi, !tvi), \
ex_dlut("z y - "+b32, bi, !tvi), \
ex_dlut("x y - "+b32, bi, !tvi), format="YUV444"+fmt[2], optSingleMode=true) } }
# YCbCr RCT to RGB' function (Reversible Color Transform, from JPEG 2000, similar to YCoCg)
function YUVr_to_RGB (clip YUV, clip "Cb", clip "Cr", bool "tv_in", bool "tv_out") {
isy = isy(YUV)
bi = BitsPerComponent(YUV)
lut = bi > 12 || isRunTime(YUV,false) ? 0 : 2
tvi = Default (tv_in, false)
tvo = Default (tv_out, false)
clp = !isy ? ExtractClip(YUV) : nop()
Y = !isy ? clp[0] : YUV
Cb = !isy ? clp[1] : Cb
Cr = !isy ? clp[2] : Cr
b32 = bi == 32 ? "" : "range_half -"
b32h = bi == 32 ? "" : "range_half 0.5 * +"
G = Expr(Y, Cb, Cr, ex_dlut("x y z + 0.25 * - "+b32h, bi, !tvi), optSingleMode=true, lut= 0)
if (isy) {
R = Expr(G, Cr, ex_dlut("x y + "+b32, bi, !tvi), optSingleMode=false, lut=lut)
B = Expr(G, Cb, ex_dlut("x y + "+b32, bi, !tvi), optSingleMode=false, lut=lut)
[R, G, B]
} else {
fmt = format_Fuzzy_Search(YUV, "RGB", bi)
Expr(G, Cb, Cr, ex_dlut("x z + "+b32, bi, !tvi),"", \
ex_dlut("x y + "+b32, bi, !tvi), format="RGB"+fmt[2]) } }
# RGB' to LSLM'
#
# L [0.209(R-0.5), 0.715(G-0.5), 0.076(B-0.5)],\
# S [0.209(R-0.5), 0.715(G-0.5),-0.924(B-0.5)],\
# LM [3.148(R-0.5), -2.799(G-0.5),-0.349(B-0.5)]]
#
# RGB' to OPP'
# This looks like a gamma encoded transformation.
#
# [1/sqrt(2), -1/sqrt(2), 0],\
# [1/sqrt(6), 1/sqrt(6), -2/sqrt(6)],\ * RGB'
# [1/sqrt(3), 1/sqrt(3), 1/sqrt(3)]]
#
# For the linear version use the matrix functions.
# The matrix there yields from a joint matrix between XYZ to a modified LMS matrix and a LMS to OPP matrix
#
# # XYZ to LMS. Looks like a version of Stockman & Sharpe (2000) LMS (and not the usual Hunt-Pointer-Estevez normalized to D65)
# [[0.2430, 0.8560, -0.0440],\
# [-0.3910, 1.1650, 0.0870],\
# [0.0100, -0.0080, 0.5630]]
# # LMS to OPP
# [[1.0, 0.0, 0.0],\
# [-0.59, 0.80, -0.12],\
# [-0.34, -0.11, 0.93]]
# https://github.com/HomeOfVapourSynthEvolution/VapourSynth-BM3D/blob/7b3d8dd32e4aa4d962cf15d75d191b465682ed42/include/Specification.h#L176
function RGB_to_OPP (clip RGB, clip "G", clip "B", bool "fulls") {
isy = isy(RGB)
bi = BitsPerComponent(RGB)
lut = bi > 12 || isRunTime(RGB,!isy) ? 0 : 2
fs = Default (fulls, true)
clp = !isy ? ExtractClip(RGB) : nop()
R = !isy ? clp[0] : RGB
G = !isy ? clp[1] : G
B = !isy ? clp[2] : B
b32 = bi == 32 ? "" : "range_half +"
Os = ex_dlut(Format("x y z + + 0.333333333 *") , bi, fs)
P1s = ex_dlut(Format("x "+(isy?"y":"z")+" - 0.5 * "+b32), bi, fs)
P2s = ex_dlut(Format("x z + 0.25 * y 0.5 * - " +b32), bi, fs)
if (isy) {
O = Expr(R, G, B, Os, optSingleMode=false, lut= 0)
P1 = Expr(R, B, P1s, optSingleMode=false, lut=lut)
P2 = Expr(R, G, B, P2s, optSingleMode=false, lut= 0)
[O, P1, P2]
} else { Expr(R, G, B, Os, P1s, P2s, format="YUV444"+format_Fuzzy_Search(RGB,"YUV",bi)[2], optSingleMode=true) } }
# https://github.com/HomeOfVapourSynthEvolution/VapourSynth-BM3D/blob/7b3d8dd32e4aa4d962cf15d75d191b465682ed42/include/Specification.h#L176
function OPP_to_RGB (clip OPP, clip "P1", clip "P2", bool "fulls") {
isy = isy(OPP)
bi = BitsPerComponent(OPP)
lut = bi > 12 || isRunTime(OPP,false) ? 0 : 2
fs = Default (fulls, true)
clp = !isy ? ExtractClip(OPP) : nop()
O = !isy ? clp[0] : OPP
P1 = !isy ? clp[1] : P1
P2 = !isy ? clp[2] : P2
b32 = bi == 32 ? "" : "range_half -"
Rs = ex_dlut(Format("x y "+b32+" + z "+b32+" 0.666666666 * +") , bi, fs)
Gs = ex_dlut(Format("x "+(isy?"y":"z")+" "+b32+" 1.333333333 * -"), bi, fs)
Bs = ex_dlut(Format("x z "+b32+" 0.666666666 * + y "+b32+" -") , bi, fs)
if (isy) {
R = Expr(O, P1, P2, Rs, optSingleMode=false, lut= 0)
G = Expr(O, P2, Gs, optSingleMode=false, lut=lut)
B = Expr(O, P1, P2, Bs, optSingleMode=false, lut= 0)
[R, G, B]
} else { Expr(O, P1, P2, Rs, Gs, Bs, format="RGB"+format_Fuzzy_Search(OPP,"RGB",bi)[2], optSingleMode=true) } }
# XYZ to YDzDx (applied on XYZ in SMPTE ST 2084 gamma space)
function XYZ_to_YDzDx (clip XYZ, clip "Y", clip "Z", bool "tv_in", bool "tv_out") {
isy = isy(XYZ)
bi = BitsPerComponent(XYZ)
lut = bi > 12 || isRunTime(XYZ,!isy) ? 0 : 2
tvi = Default (tv_in, false)
tvo = Default (tv_out, false)
clp = !isy ? ExtractClip(XYZ) : nop()
X = !isy ? clp[0] : XYZ
Y = !isy ? clp[1] : Y
Z = !isy ? clp[2] : Z
b32 = bi == 32 ? "" : "range_half +"
cf1 = 2763/2800.
cf2 = 2741/2763.
if (isy) {
Dz = Expr( Y, Z, ex_dlut(Format("{cf1} y "+b32+" * x - "), bi, !tvi), optSingleMode=false, lut=lut)
Dx = Expr(X, Y, ex_dlut(Format("x "+b32+" {cf2} y * - "), bi, !tvi), optSingleMode=false, lut=lut)
[Y, Dz, Dx]
} else {
Expr(Y, X, Z, "", ex_dlut(Format("{cf1} z "+b32+" * x - "), bi, !tvi), \
ex_dlut(Format("y "+b32+" {cf2} x * - "), bi, !tvi), format=PixelType(XYZ)) } }
# YDzDx to XYZ
function YDzDx_to_XYZ (clip YDzDx, clip "Dz", clip "Dx", bool "tv_in", bool "tv_out") {
isy = isy(YDzDx)
bi = BitsPerComponent(YDzDx)
lut = bi > 12 || isRunTime(YDzDx,!isy) ? 0 : 2
tvi = Default (tv_in, false)
tvo = Default (tv_out, false)
clp = !isy ? ExtractClip(YDzDx) : nop()
Y = !isy ? clp[0] : YDzDx
Dz = !isy ? clp[1] : Dz
Dx = !isy ? clp[2] : Dx
b32 = bi == 32 ? "" : "range_half -"
cf1 = 2800/2763.
cf2 = 2763/2741.
if (isy) {
X = Expr(Dx, Y, ex_dlut(Format("x "+b32+" y {cf2} * + "), bi, !tvi), optSingleMode=false, lut=lut)
Z = Expr(Dz, Y, ex_dlut(Format("x "+b32+" y {cf1} * + "), bi, !tvi), optSingleMode=false, lut=lut)
[X, Y, Z]
} else {
Expr(Dx, Y, Dz, ex_dlut(Format("x "+b32+" y {cf1} * + "), bi, !tvi), "", \
ex_dlut(Format("z "+b32+" y {cf2} * + "), bi, !tvi), format=PixelType(YDzDx)) } }
# XYZ to CIE Lab -D65 based- (process in linear space) (float bitdepth required as some values overflow)
# CIELAB is perceptually uniform but only at low frequencies as it is based on 2 degrees of uniform color circles.
function XYZ_to_Lab (clip XYZ, clip "Y", clip "Z", bool "Jch", bool "fulls") {
isy = isy(XYZ)
bi = BitsPerComponent(XYZ)
iRT = isRunTime(XYZ,!isy)
lut1 = bi == 32 || iRT ? 0 : 1
lut2 = bi > 12 || iRT ? 0 : 2
cy = Default (Jch, false)
fs = Default (fulls, true)
clp = !isy ? ExtractClip(XYZ) : nop()
X = !isy ? clp[0] : XYZ
Y = !isy ? clp[1] : Y
Z = !isy ? clp[2] : Z
ep = pow(6. / 29, 3) # 0.00885645167903563081717167575546 (or 216 / 24389.)
coef = pow(6. / 29, 2) * 3 # 0.12841854934601664684898929845422 (or 108 / 841)
coefr = pow(6. / 29, -2) * (1/3.) # 7.78703703703703703703703703703703 (or 841 / 108) reciprocal of above
rW = [1.052134065421009,1,0.918343935177561] # Reference White (Tristimulus Values): Normalization to Y=100 of reciprocal of illuminant -D65- (and divided by 100)
L = Expr(Y, Format("x {ep} > x 0.333333333 ^ x {coefr} * 4 29 / + ? 1.16 * 0.16 -"), optSingleMode=false, lut=lut1)
a = Expr(X, Y, ex_dlut(Format("x "+string(rW[0])+" * X@ {ep} > X 0.333333333 ^ X {coefr} * 4 29 / + ?
y {ep} > y 0.333333333 ^ y {coefr} * 4 29 / + ? - 5 * "), bi, fs), optSingleMode=true, lut=lut2)
b = Expr( Y, Z, ex_dlut(Format("x {ep} > x 0.333333333 ^ x {coefr} * 4 29 / + ?
y "+string(rW[2])+" * Z@ {ep} > Z 0.333333333 ^ Z {coefr} * 4 29 / + ? - 2 * "), bi, fs), optSingleMode=true, lut=lut2)
isy ? [L, a, b] : CombinePlanes(L, a, b, planes="RGB") }
# CIE Lab to XYZ -D65 based- (float bitdepth required as some values overflow)
function Lab_to_XYZ (clip Lab, clip "a", clip "b", bool "Jch", bool "fulls") {
isy = isy(Lab)
bi = BitsPerComponent(Lab)
iRT = isRunTime(Lab,!isy)
lut1 = bi == 32 || iRT ? 0 : 1
lut2 = bi > 12 || iRT ? 0 : 2
cy = Default (Jch, false)
fs = Default (fulls, true)
clp = !isy ? ExtractClip(Lab) : nop()
L = !isy ? clp[0] : Lab
a = !isy ? clp[1] : a
b = !isy ? clp[2] : b
ep = 6 / 29. # 0.20689655172413793103448275862069
ep2 = pow(6 / 29., 3) # 0.00885645167903563081717167575546 (or 216 / 24389.)
coef = pow(6 / 29., 2) * 3 # 0.12841854934601664684898929845422 (or 108 / 841)
kappa = 24389 / 27. # 903.296296296296296296296296296296
rW = [0.950449218275099,1,1.088916648430471] # Reference White (Tristimulus Values): Normalization to Y=100 of illuminant (and divided by 100)
# # De facto formula, for reference
# Y = Expr( L, Format("x 100 * 16 + 116 / Y@ {ep} > Y 3 ^ Y 16 116 / - {coef} * ? "))
# X = Expr( L, a, Format("x 100 * 16 + 116 / y 100 * 500 / + X@ {ep} > X 3 ^ X 16 116 / - {coef} * ? "+string(rW[0])+" *"))
# Z = Expr( L, b, Format("x 100 * 16 + 116 / y 100 * 200 / - Z@ {ep} > Z 3 ^ Z 16 116 / - {coef} * ? "+string(rW[2])+" *"))
Y = Expr( L, Format("x 0.16 + 0.8620689656 * Y@ {ep} > Y 3 ^ Y 4 29 / - {coef} * ? "), optSingleMode=false, lut=lut1)
X = Expr( L, a, Format("x 0.16 + 0.8620689656 * y 0.2 * + X@ {ep} > X 3 ^ X 4 29 / - {coef} * ? "+string(rW[0])+" *"), optSingleMode=false, lut=lut2)
Z = Expr( L, b, Format("x 0.16 + 0.8620689656 * y 0.5 * - Z@ {ep} > Z 3 ^ Z 4 29 / - {coef} * ? "+string(rW[2])+" *"), optSingleMode=false, lut=lut2)
isy ? [X, Y, Z] : CombinePlanes(X, Y, Z, planes="RGB") }
# Only works in 32-bit float (for the time being)
function XYZ_to_Yxy (clip XYZ, clip "Y", clip "Z", bool "fulls") {
isy = isy(XYZ)
bi = BitsPerComponent(XYZ)
fs = Default (fulls, true)
clp = !isy ? ExtractClip(XYZ) : nop()
X = !isy ? clp[0] : XYZ
Y = !isy ? clp[1] : Y
Z = !isy ? clp[2] : Z
if (isy) {
xl = Expr(X, Y, Z, "x 0 <= 0.3805 x dup y z + + / ?", optSingleMode=false)
yl = Expr(X, Y, Z, "y 0 <= 0.3769 y dup x z + + / ?", optSingleMode=false)
[Y, xl, yl]
} else {
Expr(X, Y, Z, "y", "x 0 <= 0.3805 x dup y z + + / ?",\
"y 0 <= 0.3769 y dup x z + + / ?", optSingleMode=true, format=PixelType(XYZ)) } }
# Only works in 32-bit float
function Yxy_to_XYZ (clip Yxy, clip "x", clip "y", bool "fulls") {
isy = isy(Yxy)
bi = BitsPerComponent(Yxy)
fs = Default (fulls, true)
clp = !isy ? ExtractClip(Yxy) : nop()
Yb = !isy ? clp[0] : Yxy
xl = !isy ? clp[1] : x
yl = !isy ? clp[2] : y
if (isy) {
X = Expr(Yb, xl, yl, "y z / x *", optSingleMode=false)
Z = Expr(Yb, xl, yl, "y z / x * Y@ y / Y - x -", optSingleMode=false)
[X, Yb, Z]
} else {
Expr(Yb, xl, yl, "y z / x * ", \
" x ", \
"y z / x * Y@ y / Y - x -", optSingleMode=true, format=PixelType(Yxy)) } }
# WIP. XYZ to Hunter Lab
function XYZ_to_HLab (clip XYZ, clip "Y", clip "Z", bool "Jch", bool "fulls") {
isy = isy(XYZ)
bi = BitsPerComponent(XYZ)
iRT = isRunTime(XYZ,!isy)
lut1 = bi == 32 || iRT ? 0 : 1
lut2 = bi > 12 || iRT ? 0 : 2
cy = Default (Jch, false)
fs = Default (fulls, true)
clp = !isy ? ExtractClip(XYZ) : nop()
X = !isy ? clp[0] : XYZ
Y = !isy ? clp[1] : Y
Z = !isy ? clp[2] : Z
rW = [95.0449218275099, 100., 108.8916648430471] # Reference White: Normalization to Y=100 of illuminant
L = Expr(Y, Format("x sqrt 10 *"), optSingleMode=false, lut=lut1)
a = Expr(X, Y, ex_dlut(Format("x 1.02 * y - 17.5 * y sqrt /"), bi, fs), optSingleMode=true, lut=lut2)
b = Expr( Y, Z, ex_dlut(Format("y 0.847 * x - 7 * y sqrt /"), bi, fs), optSingleMode=true, lut=lut2)
isy ? [L, a, b] : CombinePlanes(L, a, b, planes="RGB") }
# XYZ to CIE Luv/Duv -D65 based- (process in linear space) (32-bit float recommended)
function XYZ_to_Luv (clip XYZ, clip "Y", clip "Z", bool "Jch", bool "Duv", bool "tv_in", bool "tv_out") {
isy = isy(XYZ)
bi = BitsPerComponent(XYZ)
lut = bi == 32 || isRunTime(XYZ,!isy) ? 0 : 1
cy = Default (Jch, false)
uv = Default (Duv, false)
tvi = Default (tv_in, false)
tvo = Default (tv_out, false)
clp = !isy ? ExtractClip(XYZ) : nop()
X = !isy ? clp[0] : XYZ
Y = !isy ? clp[1] : Y
Z = !isy ? clp[2] : Z
b32 = bi == 32 ? "" : "range_half +"
ep = 216 / 24389.
kappa = 24389 / 27.