4 * Copyright (C) 2008-2021 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "posterize.h"
32 REGISTER_PLUGIN(PosterizeMain)
38 PosterizeConfig::PosterizeConfig()
43 int PosterizeConfig::equivalent(PosterizeConfig &that)
45 return steps == that.steps;
48 void PosterizeConfig::copy_from(PosterizeConfig &that)
53 void PosterizeConfig::boundaries()
55 CLAMP(steps, MIN_STEPS, MAX_STEPS);
59 void PosterizeConfig::interpolate(PosterizeConfig &prev,
60 PosterizeConfig &next,
63 int64_t current_frame)
65 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
66 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
67 this->steps = (int)(prev.steps * prev_scale + next.steps * next_scale);
71 PosterizeMain::PosterizeMain(PluginServer *server)
72 : PluginVClient(server)
77 PosterizeMain::~PosterizeMain()
82 const char* PosterizeMain::plugin_title() { return N_("Posterize"); }
83 int PosterizeMain::is_realtime() { return 1; }
85 NEW_WINDOW_MACRO(PosterizeMain, PosterizeWindow)
86 LOAD_CONFIGURATION_MACRO(PosterizeMain, PosterizeConfig)
89 void PosterizeMain::update_gui()
93 if(load_configuration())
95 thread->window->lock_window();
96 ((PosterizeWindow*)thread->window)->update(1, 1);
97 thread->window->unlock_window();
104 void PosterizeMain::save_data(KeyFrame *keyframe)
108 // cause data to be stored directly in text
109 output.set_shared_output(keyframe->xbuf);
110 output.tag.set_title("POSTERIZE");
111 output.tag.set_property("STEPS", config.steps);
113 output.tag.set_title("/POSTERIZE");
115 output.append_newline();
116 output.terminate_string();
119 void PosterizeMain::read_data(KeyFrame *keyframe)
123 input.set_shared_input(keyframe->xbuf);
129 result = input.read_tag();
133 if(input.tag.title_is("POSTERIZE"))
135 config.steps = input.tag.get_property("STEPS", config.steps);
144 #define PROCESS(type, components, yuv) \
146 for(int i = 0; i < h; i++) \
148 type *in_row = (type*)frame->get_rows()[i]; \
149 type *out_row = (type*)frame->get_rows()[i]; \
151 for(int j = 0; j < w; j++) \
153 if(sizeof(type) == 4) \
155 out_row[j * components + 0] = (int)(in_row[j * components + 0] / division) * division; \
156 out_row[j * components + 1] = (int)(in_row[j * components + 1] / division) * division; \
157 out_row[j * components + 2] = (int)(in_row[j * components + 2] / division) * division; \
161 out_row[j * components + 0] = table_r[(int)in_row[j * components + 0]]; \
162 out_row[j * components + 1] = table_g[(int)in_row[j * components + 1]]; \
163 out_row[j * components + 2] = table_b[(int)in_row[j * components + 2]]; \
170 int PosterizeMain::process_buffer(VFrame *frame,
171 int64_t start_position,
174 load_configuration();
182 int w = frame->get_w();
183 int h = frame->get_h();
188 float division = (float)255 / config.steps;
189 for(int i = 0; i < 256; i++)
192 table_r[i] = (int)(i / division) * division;
193 //printf("PosterizeMain::process_buffer %d i=%d %d\n", __LINE__, i, table_r[i]);
194 // if(BC_CModels::is_yuv(frame->get_color_model()))
196 // table_g[i] = (int)(i / division) * division;
197 // table_b[i] = (int)(i / division) * division;
201 table_g[i] = table_r[i];
202 table_b[i] = table_r[i];
208 switch(frame->get_color_model())
211 PROCESS(unsigned char, 3, 1);
214 PROCESS(unsigned char, 4, 1);
217 PROCESS(unsigned char, 3, 0);
220 PROCESS(unsigned char, 4, 0);
223 division = (float)1 / config.steps;
224 PROCESS(float, 3, 0);
227 division = (float)1 / config.steps;
228 PROCESS(float, 4, 0);
237 PosterizeText::PosterizeText(PosterizeMain *plugin,
238 PosterizeWindow *gui,
243 : BC_TextBox(x, y, w, 1, (int64_t)*output, 1, MEDIUMFONT)
245 this->plugin = plugin;
247 this->output = output;
250 int PosterizeText::handle_event()
252 *output = atoi(get_text());
254 plugin->send_configure_change();
261 PosterizeSlider::PosterizeSlider(PosterizeMain *plugin,
262 PosterizeWindow *gui,
269 : BC_ISlider(x, y, 0, w, w, min, max, *output)
271 this->plugin = plugin;
273 this->output = output;
275 int PosterizeSlider::handle_event()
277 *output = get_value();
279 plugin->send_configure_change();
287 PosterizeWindow::PosterizeWindow(PosterizeMain *plugin)
288 : PluginClientWindow(plugin,
295 this->plugin = plugin;
298 PosterizeWindow::~PosterizeWindow()
302 void PosterizeWindow::create_objects()
304 int text_w = xS(100);
305 int margin = client->get_theme()->widget_border;
306 int x = margin, y = margin;
308 add_tool(title = new BC_Title(x, y, _("Steps per channel:")));
309 y += title->get_h() + margin;
310 add_tool(slider = new PosterizeSlider(plugin,
314 get_w() - text_w - margin - margin - margin,
315 &plugin->config.steps,
318 x += slider->get_w() + margin;
319 add_tool(text = new PosterizeText(plugin,
324 &plugin->config.steps));
330 void PosterizeWindow::update(int do_slider, int do_text)
332 if(do_text) text->update((int64_t)plugin->config.steps);
333 if(do_slider) slider->update(plugin->config.steps);