-
Notifications
You must be signed in to change notification settings - Fork 4
/
filter-halftone-dithering.lua
279 lines (223 loc) · 11.6 KB
/
filter-halftone-dithering.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
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
obs = obslua
-- Returns the description displayed in the Scripts window
function script_description()
return [[<center><h2>Halftone Filter with Dithering</h2></center>
<p>This Lua script adds a video filter named <it>Halftone Dithering</it>. The filter can be added
to a video source to reduce the number of colors of the input picture. It reproduces
the style of a magnified printed picture.</p>]]
end
-- Called on script startup
function script_load(settings)
obs.obs_register_source(source_info)
end
-- Definition of the global variable containing the source_info structure
source_info = {}
source_info.id = 'filter-halftone-dithering' -- Unique string identifier of the source type
source_info.type = obs.OBS_SOURCE_TYPE_FILTER -- INPUT or FILTER or TRANSITION
source_info.output_flags = obs.OBS_SOURCE_VIDEO -- Combination of VIDEO/AUDIO/ASYNC/etc
-- Returns the name displayed in the list of filters
source_info.get_name = function()
print("In source_info.get_name")
return "Halftone Dithering"
end
-- Creates the implementation data for the source
source_info.create = function(settings, source)
print("In source_info.create")
-- Initializes the custom data table
local data = {}
data.source = source -- Keeps a reference to this filter as a source object
data.width = 1 -- Dummy value during initialization phase
data.height = 1 -- Dummy value during initialization phase
-- Compiles the effect
obs.obs_enter_graphics()
local effect_file_path = script_path() .. 'filter-halftone-dithering.effect.hlsl'
data.effect = obs.gs_effect_create_from_file(effect_file_path, nil)
-- data.effect = obs.gs_effect_create(EFFECT, "halftone_effect_code", nil)
obs.obs_leave_graphics()
-- Calls the destroy function if the effect was not compiled properly
if data.effect == nil then
obs.blog(obs.LOG_ERROR, "Effect compilation failed for " .. effect_file_path)
source_info.destroy(data)
return nil
end
-- Retrieves the shader uniform variables
data.params = {}
data.params.width = obs.gs_effect_get_param_by_name(data.effect, "width")
data.params.height = obs.gs_effect_get_param_by_name(data.effect, "height")
data.params.gamma = obs.gs_effect_get_param_by_name(data.effect, "gamma")
data.params.gamma_shift = obs.gs_effect_get_param_by_name(data.effect, "gamma_shift")
data.params.scale = obs.gs_effect_get_param_by_name(data.effect, "scale")
data.params.amplitude = obs.gs_effect_get_param_by_name(data.effect, "amplitude")
data.params.number_of_color_levels = obs.gs_effect_get_param_by_name(data.effect, "number_of_color_levels")
data.params.offset = obs.gs_effect_get_param_by_name(data.effect, "offset")
data.params.pattern_texture = obs.gs_effect_get_param_by_name(data.effect, "pattern_texture")
data.params.pattern_size = obs.gs_effect_get_param_by_name(data.effect, "pattern_size")
data.params.pattern_gamma = obs.gs_effect_get_param_by_name(data.effect, "pattern_gamma")
data.params.palette_texture = obs.gs_effect_get_param_by_name(data.effect, "palette_texture")
data.params.palette_size = obs.gs_effect_get_param_by_name(data.effect, "palette_size")
data.params.palette_gamma = obs.gs_effect_get_param_by_name(data.effect, "palette_gamma")
-- Calls update to initialize the rest of the properties-managed settings
source_info.update(data, settings)
return data
end
-- Destroys and release resources linked to the custom data
source_info.destroy = function(data)
if data.effect ~= nil then
obs.obs_enter_graphics()
obs.gs_effect_destroy(data.effect)
data.effect = nil
obs.obs_leave_graphics()
end
end
-- Returns the width of the source
source_info.get_width = function(data)
return data.width
end
-- Returns the height of the source
source_info.get_height = function(data)
return data.height
end
function set_texture_effect_parameters(image, param_texture, param_size, nanoseconds)
local size = obs.vec2()
if image then
obs.gs_image_file_tick(image, nanoseconds)
obs.gs_image_file_update_texture(image)
obs.gs_effect_set_texture(param_texture, image.texture)
obs.vec2_set(size, image.cx, image.cy)
else
obs.vec2_set(size, -1, -1)
end
obs.gs_effect_set_vec2(param_size, size)
end
-- Called when rendering the source with the graphics subsystem
source_info.video_render = function(data)
local parent = obs.obs_filter_get_target(data.source)
data.width = obs.obs_source_get_base_width(parent)
data.height = obs.obs_source_get_base_height(parent)
obs.obs_source_process_filter_begin(data.source, obs.GS_RGBA, obs.OBS_NO_DIRECT_RENDERING)
-- Effect parameters initialization goes here
obs.gs_effect_set_int(data.params.width, data.width)
obs.gs_effect_set_int(data.params.height, data.height)
obs.gs_effect_set_float(data.params.gamma, data.gamma)
obs.gs_effect_set_float(data.params.gamma_shift, data.gamma_shift)
obs.gs_effect_set_float(data.params.scale, data.scale)
obs.gs_effect_set_float(data.params.amplitude, data.amplitude)
obs.gs_effect_set_int(data.params.number_of_color_levels, data.number_of_color_levels)
obs.gs_effect_set_float(data.params.offset, data.offset)
-- Pattern texture
set_texture_effect_parameters(data.pattern, data.params.pattern_texture,
data.params.pattern_size, data.nanoseconds)
obs.gs_effect_set_float(data.params.pattern_gamma, data.pattern_gamma)
-- Palette texture
set_texture_effect_parameters(data.palette, data.params.palette_texture,
data.params.palette_size, data.nanoseconds)
obs.gs_effect_set_float(data.params.palette_gamma, data.palette_gamma)
obs.obs_source_process_filter_end(data.source, data.effect, data.width, data.height)
end
source_info.video_tick = function(data, seconds)
data.nanoseconds = seconds*1e9
end
-- Sets the default settings for this source
source_info.get_defaults = function(settings)
print("In source_info.get_defaults")
obs.obs_data_set_default_double(settings, "gamma", 1.0)
obs.obs_data_set_default_double(settings, "gamma_shift", 0.0)
obs.obs_data_set_default_double(settings, "scale", 1.0)
obs.obs_data_set_default_double(settings, "amplitude", 0.2)
obs.obs_data_set_default_int(settings, "number_of_color_levels", 4)
obs.obs_data_set_default_double(settings, "offset", 0.0)
obs.obs_data_set_default_string(settings, "pattern_path", "")
obs.obs_data_set_default_double(settings, "pattern_gamma", 1.0)
obs.obs_data_set_default_string(settings, "palette_path", "")
obs.obs_data_set_default_double(settings, "palette_gamma", 1.0)
end
-- Properties "modified callback" to set visible flags of the displayed properties
function set_properties_visibility(props, property, settings)
local pattern = string.len(obslua.obs_data_get_string(settings, "pattern_path")) > 0
local palette = string.len(obslua.obs_data_get_string(settings, "palette_path")) > 0
obs.obs_property_set_visible(obs.obs_properties_get(props, "pattern_reset"), pattern)
obs.obs_property_set_visible(obs.obs_properties_get(props, "pattern_gamma"), pattern)
obs.obs_property_set_visible(obs.obs_properties_get(props, "number_of_color_levels"), not palette)
obs.obs_property_set_visible(obs.obs_properties_get(props, "palette_reset"), palette)
obs.obs_property_set_visible(obs.obs_properties_get(props, "palette_gamma"), palette)
return true
end
-- Gets the property information of this source
source_info.get_properties = function(data)
print("In source_info.get_properties")
local props = obs.obs_properties_create()
local gprops = obs.obs_properties_create()
obs.obs_properties_add_group(props, "input", "Input Source", obs.OBS_GROUP_NORMAL, gprops)
obs.obs_properties_add_float_slider(gprops, "gamma", "Gamma encoding exponent", 1.0, 2.2, 0.2)
obs.obs_properties_add_float_slider(gprops, "gamma_shift", "Gamma shift", -2.0, 2.0, 0.01)
gprops = obs.obs_properties_create()
obs.obs_properties_add_group(props, "pattern", "Dithering Pattern", obs.OBS_GROUP_NORMAL, gprops)
obs.obs_properties_add_float_slider(gprops, "scale", "Pattern scale", 0.01, 10.0, 0.01)
obs.obs_properties_add_float_slider(gprops, "amplitude", "Dithering amplitude", -2.0, 2.0, 0.01)
obs.obs_properties_add_float_slider(gprops, "offset", "Dithering luminosity shift", -2.0, 2.0, 0.01)
local p = obs.obs_properties_add_path(gprops, "pattern_path", "Pattern texture", obs.OBS_PATH_FILE,
"Picture (*.png *.bmp *.jpg *.gif)", nil)
obs.obs_property_set_modified_callback(p, set_properties_visibility)
obs.obs_properties_add_float_slider(gprops, "pattern_gamma", "Pattern gamma exponent", 1.0, 2.2, 0.2)
obs.obs_properties_add_button(gprops, "pattern_reset", "Reset pattern texture", function(properties, property)
obs.obs_data_set_string(data.settings, "pattern_path", ""); data.pattern = nil;
set_properties_visibility(properties, property, data.settings); return true; end)
gprops = obs.obs_properties_create()
obs.obs_properties_add_group(props, "palette", "Color palette", obs.OBS_GROUP_NORMAL, gprops)
obs.obs_properties_add_int_slider(gprops, "number_of_color_levels", "Number of color levels", 2, 10, 1)
p = obs.obs_properties_add_path(gprops, "palette_path", "Palette texture", obs.OBS_PATH_FILE,
"Picture (*.png *.bmp *.jpg *.gif)", nil)
obs.obs_property_set_modified_callback(p, set_properties_visibility)
obs.obs_properties_add_float_slider(gprops, "palette_gamma", "Palette gamma exponent", 1.0, 2.2, 0.2)
obs.obs_properties_add_button(gprops, "palette_reset", "Reset palette texture", function(properties, property)
obs.obs_data_set_string(data.settings, "palette_path", ""); data.palette = nil;
set_properties_visibility(properties, property, data.settings); return true; end)
return props
end
-- Returns new texture and free current texture if loaded
function load_texture(path, current_texture)
obs.obs_enter_graphics()
-- Free any existing image
if current_texture then
obs.gs_image_file_free(current_texture)
end
-- Loads and inits image for texture
local new_texture = nil
if string.len(path) > 0 then
new_texture = obs.gs_image_file()
obs.gs_image_file_init(new_texture, path)
if new_texture.loaded then
obs.gs_image_file_init_texture(new_texture)
else
obs.blog(obs.LOG_ERROR, "Cannot load image " .. path)
obs.gs_image_file_free(current_texture)
new_texture = nil
end
end
obs.obs_leave_graphics()
return new_texture
end
-- Updates the internal data for this source upon settings change
source_info.update = function(data, settings)
print("In source_info.update")
data.gamma = obs.obs_data_get_double(settings, "gamma")
data.gamma_shift = obs.obs_data_get_double(settings, "gamma_shift")
data.scale = obs.obs_data_get_double(settings, "scale")
data.amplitude = obs.obs_data_get_double(settings, "amplitude")
data.number_of_color_levels = obs.obs_data_get_int(settings, "number_of_color_levels")
-- Keeps a reference on the settings
data.settings = settings
data.offset = obs.obs_data_get_double(settings, "offset")
local pattern_path = obs.obs_data_get_string(settings, "pattern_path")
if data.loaded_pattern_path ~= pattern_path then
data.pattern = load_texture(pattern_path, data.pattern)
data.loaded_pattern_path = pattern_path
end
data.pattern_gamma = obs.obs_data_get_double(settings, "pattern_gamma")
local palette_path = obs.obs_data_get_string(settings, "palette_path")
if data.loaded_palette_path ~= palette_path then
data.palette = load_texture(palette_path, data.palette)
data.loaded_palette_path = palette_path
end
data.palette_gamma = obs.obs_data_get_double(settings, "palette_gamma")
end