-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmconv.lua
131 lines (99 loc) · 3.03 KB
/
cmconv.lua
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
include("utils.lua")
script_name = "BT.601 -> BT.709 Color Fixer"
script_description = "Change colors from BT.601 to BT.709."
script_name_2 = "BT.601 -> BT.709 Global Color Fixer"
script_description_2 = "Globally change colors from BT.601 to BT.709."
script_author = "Daiz"
script_version = "1.0.2"
local Rec601 = {
Kr = 0.299,
Kg = 0.587,
Kb = 0.114
}
local Rec709 = {
Kr = 0.2126,
Kg = 0.7152,
Kb = 0.0722
}
function RGBtoYUV(r, g, b, matrix)
local Kr = matrix.Kr
local Kg = matrix.Kg
local Kb = matrix.Kb
local y = (Kr*219/255)*r + (Kg*219/255)*g + (Kb*219/255)*b
local v = 112/255*r - Kg*112/255*g/(1-Kr) - Kb*112/255*b/(1-Kr)
local u = - Kr*112/255*r/(1-Kb) - Kg*112/255*g/(1-Kb) + 112/255*b
return y+16, u+128, v+128
end
function YUVtoRGB(y, u, v, matrix)
local Kr = matrix.Kr
local Kg = matrix.Kg
local Kb = matrix.Kb
local r = (255/219)*y + (255/112)*v*(1-Kr) - (255*16/219 + 255*128/112*(1-Kr))
local g = (255/219)*y - (255/112)*u*(1-Kb)*Kb/Kg - (255/112)*v*(1-Kr)*Kr/Kg - (255*16/219 - 255/112*128*(1-Kb)*Kb/Kg - 255/112*128*(1-Kr)*Kr/Kg)
local b = (255/219)*y + (255/112)*u*(1-Kb) - (255*16/219 + 255*128/112*(1-Kb))
r = clamp(math.floor(r + 0.5), 0, 255)
g = clamp(math.floor(g + 0.5), 0, 255)
b = clamp(math.floor(b + 0.5), 0, 255)
return r, g, b
end
function cm_conv(r, g, b, m1, m2)
local y, u, v = RGBtoYUV(r, g, b, m1)
local r, g, b = YUVtoRGB(y, u, v, m2)
return r, g, b
end
function replace(colorstring)
local r, g, b, a = extract_color(colorstring)
nr, ng, nb = cm_conv(r, g, b, Rec601, Rec709)
return ass_color(nr, ng, nb)
end
function correct(subs, sel, active)
for _, i in pairs(sel) do
local l = subs[i]
local t = l.text
l.text = t:gsub("&H%x%x%x%x%x%x&", replace)
subs[i] = l
end
aegisub.set_undo_point("BT.601->BT.709 Color Fix")
end
function correct_global(subs, sel, active)
for i = 1, #subs do
local l = subs[i]
if l.class == "info" then
if l.key == "YCbCr Matrix" then
if l.value == "TV.601" then
l.value = "TV.709"
end
end
end
if l.class == "style" then
local r, g, b, a = extract_color(l.color1)
local nr, ng, nb = cm_conv(r, g, b, Rec601, Rec709)
l.color1 = ass_style_color(nr, ng, nb, a)
r, g, b, a = extract_color(l.color2)
nr, ng, nb = cm_conv(r, g, b, Rec601, Rec709)
l.color2 = ass_style_color(nr, ng, nb, a)
r, g, b, a = extract_color(l.color3)
nr, ng, nb = cm_conv(r, g, b, Rec601, Rec709)
l.color3 = ass_style_color(nr, ng, nb, a)
r, g, b, a = extract_color(l.color4)
nr, ng, nb = cm_conv(r, g, b, Rec601, Rec709)
l.color4 = ass_style_color(nr, ng, nb, a)
end
if l.class == "dialogue" then
local t = l.text
l.text = t:gsub("&H%x%x%x%x%x%x&", replace)
end
subs[i] = l
end
aegisub.set_undo_point("BT.601->BT.709 Global Color Fix")
end
aegisub.register_macro(
script_name,
script_description,
correct
)
aegisub.register_macro(
script_name_2,
script_description_2,
correct_global
)