3 * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "stylizeobj.h"
24 #include "stylizeobjwindow.h"
28 REGISTER_PLUGIN(StylizeObj)
31 StylizeObjConfig::StylizeObjConfig()
33 mode = MODE_EDGE_SMOOTH;
39 int StylizeObjConfig::equivalent(StylizeObjConfig &that)
44 void StylizeObjConfig::copy_from(StylizeObjConfig &that)
48 void StylizeObjConfig::interpolate( StylizeObjConfig &prev, StylizeObjConfig &next,
49 long prev_frame, long next_frame, long current_frame)
54 void StylizeObjConfig::limits()
59 StylizeObj::StylizeObj(PluginServer *server)
60 : PluginVClient(server)
64 StylizeObj::~StylizeObj()
68 const char* StylizeObj::plugin_title() { return N_("StylizeObj"); }
69 int StylizeObj::is_realtime() { return 1; }
71 NEW_WINDOW_MACRO(StylizeObj, StylizeObjWindow);
72 LOAD_CONFIGURATION_MACRO(StylizeObj, StylizeObjConfig)
74 void StylizeObj::save_data(KeyFrame *keyframe)
78 // cause data to be stored directly in text
79 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
80 output.tag.set_title("STYLIZEOBJ");
81 output.tag.set_property("MODE", config.mode);
82 output.tag.set_property("SMOOTHING", config.smoothing);
83 output.tag.set_property("EDGE_STRENGTH", config.edge_strength);
84 output.tag.set_property("SHADE_FACTOR", config.shade_factor);
86 output.append_newline();
87 output.tag.set_title("/STYLIZEOBJ");
89 output.append_newline();
90 output.terminate_string();
93 void StylizeObj::read_data(KeyFrame *keyframe)
96 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
99 while( !(result = input.read_tag()) ) {
100 if( input.tag.title_is("STYLIZEOBJ") ) {
101 config.mode = input.tag.get_property("MODE", config.mode);
102 config.smoothing = input.tag.get_property("SMOOTHING", config.smoothing);
103 config.edge_strength = input.tag.get_property("EDGE_STRENGTH", config.edge_strength);
104 config.shade_factor = input.tag.get_property("SHADE_FACTOR", config.shade_factor);
107 else if( input.tag.title_is("/STYLIZEOBJ") )
112 void StylizeObj::update_gui()
114 if( !thread ) return;
115 if( !load_configuration() ) return;
116 thread->window->lock_window("StylizeObj::update_gui");
117 StylizeObjWindow *window = (StylizeObjWindow*)thread->window;
118 window->unlock_window();
121 void StylizeObj::to_mat(Mat &mat, int mcols, int mrows,
122 VFrame *inp, int ix,int iy, int mcolor_model)
124 int mcomp = BC_CModels::components(mcolor_model);
125 int mbpp = BC_CModels::calculate_pixelsize(mcolor_model);
126 int psz = mbpp / mcomp;
127 int mdepth = psz < 2 ? CV_8U : psz < 4 ? CV_16U : CV_32F;
128 if( mat.dims != 2 || mat.depth() != mdepth || mat.channels() != mcomp ||
129 mat.cols != mcols || mat.rows != mrows ) {
133 int type = CV_MAKETYPE(mdepth, mcomp);
134 mat.create(mrows, mcols, type);
136 uint8_t *mat_rows[mrows];
137 for( int y=0; y<mrows; ++y ) mat_rows[y] = mat.ptr(y);
138 uint8_t **inp_rows = inp->get_rows();
139 int ibpl = inp->get_bytes_per_line(), mbpl = mcols * mbpp;
140 int icolor_model = inp->get_color_model();
141 BC_CModels::transfer(mat_rows, mcolor_model, 0,0, mcols,mrows, mbpl,
142 inp_rows, icolor_model, ix,iy, mcols,mrows, ibpl, 0);
143 // VFrame vfrm(mat_rows[0], -1, mcols,mrows, mcolor_model, mat_rows[1]-mat_rows[0]);
144 // static int vfrm_no = 0; char vfn[64]; sprintf(vfn,"/tmp/idat/%06d.png", vfrm_no++);
145 // vfrm.write_png(vfn);
148 void StylizeObj::from_mat(VFrame *out, int ox, int oy, int ow, int oh, Mat &mat, int mcolor_model)
150 int mbpp = BC_CModels::calculate_pixelsize(mcolor_model);
151 int mrows = mat.rows, mcols = mat.cols;
152 uint8_t *mat_rows[mrows];
153 for( int y=0; y<mrows; ++y ) mat_rows[y] = mat.ptr(y);
154 uint8_t **out_rows = out->get_rows();
155 int obpl = out->get_bytes_per_line(), mbpl = mcols * mbpp;
156 int ocolor_model = out->get_color_model();
157 BC_CModels::transfer(out_rows, ocolor_model, ox,oy, ow,oh, obpl,
158 mat_rows, mcolor_model, 0,0, mcols,mrows, mbpl, 0);
159 // static int vfrm_no = 0; char vfn[64]; sprintf(vfn,"/tmp/odat/%06d.png", vfrm_no++);
160 // out->write_png(vfn);
164 int StylizeObj::process_buffer(VFrame *frame, int64_t start_position, double frame_rate)
167 //int need_reconfigure =
168 load_configuration();
169 input = get_input(0);
170 output = get_output(0);
171 width = input->get_w();
172 height = input->get_h();
173 color_model = input->get_color_model();
174 VFrame *iframe = input;
175 read_frame(iframe, 0, start_position, frame_rate, 0);
176 int cv_color_model = BC_RGB888;
177 if( color_model != cv_color_model ) {
178 iframe = new_temp(width,height, cv_color_model);
179 iframe->transfer_from(input);
183 to_mat(next_img, width,height, iframe, 0,0, cv_color_model);
184 output->clear_frame();
186 switch( config.mode ) {
187 case MODE_EDGE_SMOOTH:
188 edgePreservingFilter(next_img,img,1); // normalized conv filter
190 case MODE_EDGE_RECURSIVE:
191 edgePreservingFilter(next_img,img,2); // recursive filter
193 case MODE_DETAIL_ENHANCE:
194 detailEnhance(next_img,img);
196 case MODE_PENCIL_SKETCH: {
197 float s = config.smoothing / 100; s = s * s * 200;
198 float r = config.edge_strength / 100; r = r * r;
199 float f = config.shade_factor / 100; f = f * 0.1;
200 pencilSketch(next_img,img, img1, s, r, f);
201 cv_color_model = BC_GREY8;
203 case MODE_COLOR_SKETCH: {
204 float s = config.smoothing / 100; s = s * s * 200;
205 float r = config.edge_strength / 100; r = r * r;
206 float f = config.shade_factor / 100; f = f * 0.1;
207 pencilSketch(next_img,img1, img, s, r, f);
209 case MODE_STYLIZATION:
210 stylization(next_img,img);
213 from_mat(output, 0,0,width,height, img, cv_color_model);