initial commit
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / stylizeobj / stylizeobj.C
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
4  * 
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.
9  * 
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.
14  * 
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
18  * 
19  */
20
21 #include "clip.h"
22 #include "filexml.h"
23 #include "stylizeobj.h"
24 #include "stylizeobjwindow.h"
25 #include "language.h"
26 #include "mutex.h"
27
28 REGISTER_PLUGIN(StylizeObj)
29
30
31 StylizeObjConfig::StylizeObjConfig()
32 {
33         mode = MODE_EDGE_SMOOTH;
34         smoothing = 10;
35         edge_strength = 25;
36         shade_factor = 40;
37 }
38
39 int StylizeObjConfig::equivalent(StylizeObjConfig &that)
40 {
41         return 1;
42 }
43
44 void StylizeObjConfig::copy_from(StylizeObjConfig &that)
45 {
46 }
47
48 void StylizeObjConfig::interpolate( StylizeObjConfig &prev, StylizeObjConfig &next, 
49         long prev_frame, long next_frame, long current_frame)
50 {
51         copy_from(next);
52 }
53
54 void StylizeObjConfig::limits()
55 {
56 }
57
58
59 StylizeObj::StylizeObj(PluginServer *server)
60  : PluginVClient(server)
61 {
62 }
63
64 StylizeObj::~StylizeObj()
65 {
66 }
67
68 const char* StylizeObj::plugin_title() { return N_("StylizeObj"); }
69 int StylizeObj::is_realtime() { return 1; }
70
71 NEW_WINDOW_MACRO(StylizeObj, StylizeObjWindow);
72 LOAD_CONFIGURATION_MACRO(StylizeObj, StylizeObjConfig)
73
74 void StylizeObj::save_data(KeyFrame *keyframe)
75 {
76         FileXML output;
77
78 // cause data to be stored directly in text
79         output.set_shared_output(keyframe->xbuf);
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);
85         output.append_tag();
86         output.append_newline();
87         output.tag.set_title("/STYLIZEOBJ");
88         output.append_tag();
89         output.append_newline();
90         output.terminate_string();
91 }
92
93 void StylizeObj::read_data(KeyFrame *keyframe)
94 {
95         FileXML input;
96         input.set_shared_input(keyframe->xbuf);
97
98         int result = 0;
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);
105                         config.limits();
106                 }
107                 else if( input.tag.title_is("/STYLIZEOBJ") )
108                         result = 1;
109         }
110 }
111
112 void StylizeObj::update_gui()
113 {
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();
119 }
120
121 void StylizeObj::to_mat(Mat &mat, int mcols, int mrows,
122         VFrame *inp, int ix,int iy, int mcolor_model)
123 {
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 ) {
130                 mat.release();
131         }
132         if( mat.empty() ) {
133                 int type = CV_MAKETYPE(mdepth, mcomp);
134                 mat.create(mrows, mcols, type);
135         }
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);
146 }
147
148 void StylizeObj::from_mat(VFrame *out, int ox, int oy, int ow, int oh, Mat &mat, int mcolor_model)
149 {
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);
161 }
162
163
164 int StylizeObj::process_buffer(VFrame *frame, int64_t start_position, double frame_rate)
165 {
166
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);
180         }
181
182 // load next image
183         to_mat(next_img, width,height, iframe, 0,0, cv_color_model);
184         output->clear_frame();
185         Mat img, img1;
186         switch( config.mode ) {
187         case MODE_EDGE_SMOOTH:
188                 edgePreservingFilter(next_img,img,1); // normalized conv filter
189                 break;
190         case MODE_EDGE_RECURSIVE:
191                 edgePreservingFilter(next_img,img,2); // recursive filter
192                 break;
193         case MODE_DETAIL_ENHANCE:
194                 detailEnhance(next_img,img);
195                 break;
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;
202                 break; }
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);
208                 break; }
209         case MODE_STYLIZATION:
210                 stylization(next_img,img);
211                 break;
212         }
213         from_mat(output, 0,0,width,height, img, cv_color_model);
214         return 0;
215 }
216