sketcher rework, plugindialog selection fixes, new ffmpeg plugin.opts/plugin info...
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / crikey / crikey.C
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2015 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<stdio.h>
22 #include<stdint.h>
23 #include<math.h>
24 #include<string.h>
25
26 #include "arraylist.h"
27 #include "bccmodels.h"
28 #include "bccolors.h"
29 #include "clip.h"
30 #include "edlsession.h"
31 #include "filexml.h"
32 #include "crikey.h"
33 #include "crikeywindow.h"
34 #include "language.h"
35 #include "vframe.h"
36
37 // chroma interpolated key, crikey
38
39 REGISTER_PLUGIN(CriKey)
40
41 #if 0
42 void crikey_pgm(const char *fn,VFrame *vfrm)
43 {
44         FILE *fp = fopen(fn,"w");
45         int w = vfrm->get_w(), h = vfrm->get_h();
46         fprintf(fp,"P5\n%d %d\n255\n",w,h);
47         fwrite(vfrm->get_data(),w,h,fp);
48         fclose(fp);
49 }
50 #endif
51
52 CriKeyPoint::CriKeyPoint(int tag, int e, float x, float y, float t)
53 {
54         this->tag = tag;  this->e = e;
55         this->x = x;      this->y = y;
56         this->t = t;
57 }
58 CriKeyPoint::~CriKeyPoint()
59 {
60 }
61
62 CriKeyConfig::CriKeyConfig()
63 {
64         threshold = 0.5f;
65         draw_mode = DRAW_ALPHA;
66         drag = 0;
67         selected = 0;
68 }
69 CriKeyConfig::~CriKeyConfig()
70 {
71 }
72
73 int CriKeyConfig::equivalent(CriKeyConfig &that)
74 {
75         if( !EQUIV(this->threshold, that.threshold) ) return 0;
76         if( this->draw_mode != that.draw_mode ) return 0;
77         if( this->drag != that.drag ) return 0;
78         if( this->points.size() != that.points.size() ) return 0;
79         for( int i=0, n=points.size(); i<n; ++i ) {
80                 CriKeyPoint *ap = this->points[i], *bp = that.points[i];
81                 if( ap->tag != bp->tag ) return 0;
82                 if( ap->e != bp->e ) return 0;
83                 if( !EQUIV(ap->x, bp->x) ) return 0;
84                 if( !EQUIV(ap->y, bp->y) ) return 0;
85                 if( !EQUIV(ap->t, bp->t) ) return 0;
86         }
87         return 1;
88 }
89
90 void CriKeyConfig::copy_from(CriKeyConfig &that)
91 {
92         this->threshold = that.threshold;
93         this->draw_mode = that.draw_mode;
94         this->drag = that.drag;
95         this->selected = that.selected;
96
97         points.remove_all_objects();
98         for( int i=0,n=that.points.size(); i<n; ++i ) {
99                 CriKeyPoint *pt = that.points[i];
100                 add_point(pt->tag, pt->e, pt->x, pt->y, pt->t);
101         }
102 }
103
104 void CriKeyConfig::interpolate(CriKeyConfig &prev, CriKeyConfig &next,
105                 long prev_frame, long next_frame, long current_frame)
106 {
107         this->threshold = prev.threshold;
108         this->draw_mode = prev.draw_mode;
109         this->drag = prev.drag;
110         this->selected = prev.selected;
111
112         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
113         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
114
115         points.remove_all_objects();
116         int prev_sz = prev.points.size(), next_sz = next.points.size();
117         for( int i=0; i<prev_sz; ++i ) {
118                 CriKeyPoint *pt = prev.points[i], *nt = 0;
119                 float x = pt->x, y = pt->y, t = pt->t;
120                 int k = next_sz;  // associated by tag id in next
121                 while( --k >= 0 && pt->tag != (nt=next.points[k])->tag );
122                 if( k >= 0 ) {
123                         x = x * prev_scale + nt->x * next_scale;
124                         y = y * prev_scale + nt->y * next_scale;
125                         t = t * prev_scale + nt->t * next_scale;
126                 }
127                 add_point(pt->tag, pt->e, x, y, t);
128         }
129 }
130
131 void CriKeyConfig::limits()
132 {
133         bclamp(threshold, 0.0f, 1.0f);
134         bclamp(draw_mode, 0, DRAW_MODES-1);
135 }
136
137 int CriKeyConfig::add_point(int tag, int e, float x, float y, float t)
138 {
139         int k = points.size();
140         if( tag < 0 ) {
141                 tag = 0;
142                 for( int i=k; --i>=0; ) {
143                         int n = points[i]->tag;
144                         if( n >= tag ) tag = n + 1;
145                 }
146         }
147         points.append(new CriKeyPoint(tag, e, x, y, t));
148         return k;
149 }
150
151 void CriKeyConfig::del_point(int i)
152 {
153         points.remove_object_number(i);
154 }
155
156
157 class FillRegion
158 {
159         class segment { public: int y, lt, rt; };
160         ArrayList<segment> stack;
161
162         void push(int y, int lt, int rt) {
163                 segment &seg = stack.append();
164                 seg.y = y;  seg.lt = lt;  seg.rt = rt;
165         }
166         void pop(int &y, int &lt, int &rt) {
167                 segment &seg = stack.last();
168                 y = seg.y;  lt = seg.lt;  rt = seg.rt;
169                 stack.remove();
170         }
171  
172         int w, h;
173         float *edg;
174         uint8_t *msk;
175         bool edge_pixel(int i) { return edg[i] > 0; }
176
177 public:
178         void fill(int x, int y);
179         void run();
180
181         FillRegion(VFrame *edg, VFrame *msk);
182 };
183
184 FillRegion::FillRegion(VFrame *edg, VFrame *msk)
185 {
186         this->w = msk->get_w();
187         this->h = msk->get_h();
188         this->msk = (uint8_t*) msk->get_data();
189         this->edg = (float*) edg->get_data();
190 }
191
192 void FillRegion::fill(int x, int y)
193 {
194         push(y, x, x);
195 }
196
197 void FillRegion::run()
198 {
199         while( stack.size() > 0 ) {
200                 int y, ilt, irt;
201                 pop(y, ilt, irt);
202                 int ofs = y*w + ilt;
203                 for( int x=ilt; x<=irt; ++x,++ofs ) {
204                         if( !msk[ofs] ) continue;
205                         msk[ofs] = 0;
206                         if( edge_pixel(ofs) ) continue;
207                         int lt = x, rt = x;
208                         int lofs = ofs;
209                         for( int i=lt; --i>=0; ) {
210                                 if( !msk[--lofs] ) break;
211                                 msk[lofs] = 0;  lt = i;
212                                 if( edge_pixel(lofs) ) break;
213                         }
214                         int rofs = ofs;
215                         for( int i=rt; ++i< w; rt=i,msk[rofs]=0 ) {
216                                 if( !msk[++rofs] ) break;
217                                 msk[rofs] = 0;  rt = i;
218                                 if( edge_pixel(rofs) ) break;
219                         }
220                         if( y+1 <  h ) push(y+1, lt, rt);
221                         if( y-1 >= 0 ) push(y-1, lt, rt);
222                 }
223         }
224 }
225
226
227 CriKey::CriKey(PluginServer *server)
228  : PluginVClient(server)
229 {
230         engine = 0;
231         msk = 0;
232         edg = 0;
233 }
234
235 CriKey::~CriKey()
236 {
237         delete engine;
238         delete msk;
239         delete edg;
240 }
241
242 int CriKey::set_target(float *color, int x, int y)
243 {
244         if( x < 0 || x >= w ) return 1;
245         if( y < 0 || y >= h ) return 1;
246         uint8_t **src_rows = src->get_rows();
247         if( is_float ) {
248                 float *fp = (float*)(src_rows[y] + x*bpp);
249                 for( int i=0; i<comp; ++i,++fp ) color[i] = *fp;
250         }
251         else {
252                 uint8_t *sp = src_rows[y] + x*bpp;
253                 for( int i=0; i<comp; ++i,++sp ) color[i] = *sp;
254         }
255         return 0;
256 }
257
258 const char* CriKey::plugin_title() { return N_("CriKey"); }
259 int CriKey::is_realtime() { return 1; }
260
261 NEW_WINDOW_MACRO(CriKey, CriKeyWindow);
262 LOAD_CONFIGURATION_MACRO(CriKey, CriKeyConfig)
263
264 int CriKey::new_point()
265 {
266         EDLSession *session = get_edlsession();
267         float x = !session ? 0.f : session->output_w / 2.f;
268         float y = !session ? 0.f : session->output_h / 2.f;
269         return config.add_point(-1, 0, x, y, 0.5f);
270 }
271
272 void CriKey::save_data(KeyFrame *keyframe)
273 {
274         FileXML output;
275
276 // cause data to be stored directly in text
277         output.set_shared_output(keyframe->xbuf);
278
279         output.tag.set_title("CRIKEY");
280         output.tag.set_property("THRESHOLD", config.threshold);
281         output.tag.set_property("DRAW_MODE", config.draw_mode);
282         output.tag.set_property("DRAG", config.drag);
283         output.tag.set_property("SELECTED", config.selected);
284         output.append_tag();
285         output.append_newline();
286         output.tag.set_title("/CRIKEY");
287         output.append_tag();
288         output.append_newline();
289         for( int i=0, n = config.points.size(); i<n; ++i ) {
290                 CriKeyPoint *pt = config.points[i];
291                 char point[BCSTRLEN];
292                 sprintf(point,"/POINT_%d",pt->tag);
293                 output.tag.set_title(point+1);
294                 output.tag.set_property("E", pt->e);
295                 output.tag.set_property("X", pt->x);
296                 output.tag.set_property("Y", pt->y);
297                 output.tag.set_property("T", pt->t);
298                 output.append_tag();
299                 output.tag.set_title(point+0);
300                 output.append_tag();
301                 output.append_newline();
302         }
303         output.terminate_string();
304 }
305
306 void CriKey::read_data(KeyFrame *keyframe)
307 {
308         FileXML input;
309         input.set_shared_input(keyframe->xbuf);
310         config.points.remove_all_objects();
311         int result = 0;
312
313         while( !(result=input.read_tag()) ) {
314                 if( input.tag.title_is("CRIKEY") ) {
315                         config.threshold = input.tag.get_property("THRESHOLD", config.threshold);
316                         config.draw_mode = input.tag.get_property("DRAW_MODE", config.draw_mode);
317                         config.drag = input.tag.get_property("DRAG", config.drag);
318                         config.selected = input.tag.get_property("SELECTED", 0);
319                         config.limits();
320                 }
321                 else if( !strncmp(input.tag.get_title(),"POINT_",6) ) {
322                         int tag = atoi(input.tag.get_title() + 6);
323                         int e = input.tag.get_property("E", 0);
324                         float x = input.tag.get_property("X", 0.f);
325                         float y = input.tag.get_property("Y", 0.f);
326                         float t = input.tag.get_property("T", .5f);
327                         config.add_point(tag, e, x, y, t);
328                 }
329         }
330
331         if( !config.points.size() ) new_point();
332 }
333
334 void CriKey::update_gui()
335 {
336         if( !thread ) return;
337         thread->window->lock_window("CriKey::update_gui");
338         CriKeyWindow *window = (CriKeyWindow*)thread->window;
339         if( load_configuration() ) {
340                 window->update_gui();
341                 window->flush();
342         }
343         thread->window->unlock_window();
344 }
345
346 void CriKey::draw_alpha(VFrame *msk)
347 {
348         uint8_t **src_rows = src->get_rows();
349         uint8_t **msk_rows = msk->get_rows();
350         switch( color_model ) {
351         case BC_RGB_FLOAT:
352                 for( int y=0; y<h; ++y ) {
353                         uint8_t *sp = src_rows[y], *mp = msk_rows[y];
354                         for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
355                                 if( *mp ) continue;
356                                 float *px = (float *)sp;
357                                 px[0] = px[1] = px[2] = 0;
358                         }
359                 }
360                 break;
361         case BC_RGBA_FLOAT:
362                 for( int y=0; y<h; ++y ) {
363                         uint8_t *sp = src_rows[y], *mp = msk_rows[y];
364                         for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
365                                 if( *mp ) continue;
366                                 float *px = (float *)sp;
367                                 px[3] = 0;
368                         }
369                 }
370                 break;
371         case BC_RGB888:
372                 for( int y=0; y<h; ++y ) {
373                         uint8_t *sp = src_rows[y], *mp = msk_rows[y];
374                         for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
375                                 if( *mp ) continue;
376                                 uint8_t *px = sp;
377                                 px[0] = px[1] = px[2] = 0;
378                         }
379                 }
380                 break;
381         case BC_YUV888:
382                 for( int y=0; y<h; ++y ) {
383                         uint8_t *sp = src_rows[y], *mp = msk_rows[y];
384                         for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
385                                 if( *mp ) continue;
386                                 uint8_t *px = sp;
387                                 px[0] = 0;
388                                 px[1] = px[2] = 0x80;
389                         }
390                 }
391                 break;
392         case BC_RGBA8888:
393         case BC_YUVA8888:
394                 for( int y=0; y<h; ++y ) {
395                         uint8_t *sp = src_rows[y], *mp = msk_rows[y];
396                         for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
397                                 if( *mp ) continue;
398                                 uint8_t *px = sp;
399                                 px[3] = 0;
400                         }
401                 }
402                 break;
403         }
404 }
405
406 void CriKey::draw_edge(VFrame *edg)
407 {
408         uint8_t **src_rows = src->get_rows();
409         float **edg_rows = (float**) edg->get_rows(), gain = 10;
410         switch( color_model ) {
411         case BC_RGB_FLOAT:
412                 for( int y=0; y<h; ++y ) {
413                         uint8_t *sp = src_rows[y];
414                         float *ap = edg_rows[y];
415                         for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
416                                 float *px = (float *)sp, v = *ap * gain;
417                                 px[0] = px[1] = px[2] = v<1 ? v : 1;
418                         }
419                 }
420                 break;
421         case BC_RGBA_FLOAT:
422                 for( int y=0; y<h; ++y ) {
423                         uint8_t *sp = src_rows[y];
424                         float *ap = edg_rows[y];
425                         for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
426                                 float *px = (float *)sp, v = *ap * gain;
427                                 px[0] = px[1] = px[2] = v<1 ? v : 1;
428                                 px[3] = 1.0f;
429                         }
430                 }
431                 break;
432         case BC_RGB888:
433                 for( int y=0; y<h; ++y ) {
434                         uint8_t *sp = src_rows[y];
435                         float *ap = edg_rows[y];
436                         for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
437                                 uint8_t *px = sp;  float v = *ap * gain;
438                                 px[0] = px[1] = px[2] = v<1 ? v*256 : 255;
439                         }
440                 }
441                 break;
442         case BC_RGBA8888:
443                 for( int y=0; y<h; ++y ) {
444                         uint8_t *sp = src_rows[y];
445                         float *ap = edg_rows[y];
446                         for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
447                                 uint8_t *px = sp;  float v = *ap * gain;
448                                 px[0] = px[1] = px[2] = v<1 ? v*256 : 255;
449                                 px[3] = 0xff;
450                         }
451                 }
452                 break;
453         case BC_YUV888:
454                 for( int y=0; y<h; ++y ) {
455                         uint8_t *sp = src_rows[y];
456                         float *ap = edg_rows[y];
457                         for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
458                                 uint8_t *px = sp;  float v = *ap * gain;
459                                 px[0] = *ap<1 ? v*256 : 255;
460                                 px[1] = px[2] = 0x80;
461                         }
462                 }
463                 break;
464         case BC_YUVA8888:
465                 for( int y=0; y<h; ++y ) {
466                         uint8_t *sp = src_rows[y];
467                         float *ap = edg_rows[y];
468                         for( int x=0; x<w; ++x,++ap,sp+=bpp ) {
469                                 uint8_t *px = sp;  float v = *ap * gain;
470                                 px[0] = *ap<1 ? v*256 : 255;
471                                 px[1] = px[2] = 0x80;
472                                 px[3] = 0xff;
473                         }
474                 }
475                 break;
476         }
477 }
478
479 void CriKey::draw_mask(VFrame *msk)
480 {
481         uint8_t **src_rows = src->get_rows();
482         uint8_t **msk_rows = msk->get_rows();
483         float scale = 1 / 255.0f;
484         switch( color_model ) {
485         case BC_RGB_FLOAT:
486                 for( int y=0; y<h; ++y ) {
487                         uint8_t *sp = src_rows[y], *mp = msk_rows[y];
488                         for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
489                                 float a = *mp * scale;
490                                 float *px = (float *)sp;
491                                 px[0] = px[1] = px[2] = a;
492                         }
493                 }
494                 break;
495         case BC_RGBA_FLOAT:
496                 for( int y=0; y<h; ++y ) {
497                         uint8_t *sp = src_rows[y], *mp = msk_rows[y];
498                         for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
499                                 float a = *mp * scale;
500                                 float *px = (float *)sp;
501                                 px[0] = px[1] = px[2] = a;
502                                 px[3] = 1.0f;
503                         }
504                 }
505                 break;
506         case BC_RGB888:
507                 for( int y=0; y<h; ++y ) {
508                         uint8_t *sp = src_rows[y], *mp = msk_rows[y];
509                         for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
510                                 float a = *mp * scale;
511                                 uint8_t *px = sp;
512                                 px[0] = px[1] = px[2] = a * 255;
513                         }
514                 }
515                 break;
516         case BC_RGBA8888:
517                 for( int y=0; y<h; ++y ) {
518                         uint8_t *sp = src_rows[y], *mp = msk_rows[y];
519                         for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
520                                 float a = *mp * scale;
521                                 uint8_t *px = sp;
522                                 px[0] = px[1] = px[2] = a * 255;
523                                 px[3] = 0xff;
524                         }
525                 }
526                 break;
527         case BC_YUV888:
528                 for( int y=0; y<h; ++y ) {
529                         uint8_t *sp = src_rows[y], *mp = msk_rows[y];
530                         for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
531                                 float a = *mp * scale;
532                                 uint8_t *px = sp;
533                                 px[0] = a * 255;
534                                 px[1] = px[2] = 0x80;
535                         }
536                 }
537                 break;
538         case BC_YUVA8888:
539                 for( int y=0; y<h; ++y ) {
540                         uint8_t *sp = src_rows[y], *mp = msk_rows[y];
541                         for( int x=0; x<w; ++x,++mp,sp+=bpp ) {
542                                 float a = *mp * scale;
543                                 uint8_t *px = sp;
544                                 px[0] = a * 255;
545                                 px[1] = px[2] = 0x80;
546                                 px[3] = 0xff;
547                         }
548                 }
549                 break;
550         }
551 }
552
553
554 void CriKey::draw_point(VFrame *src, CriKeyPoint *pt)
555 {
556         int d = bmax(w,h) / 200 + 2;
557         int r = d/2+1, x = pt->x, y = pt->y;
558         src->draw_smooth(x-r,y+0, x-r, y-r, x+0,y-r);
559         src->draw_smooth(x+0,y-r, x+r, y-r, x+r,y+0);
560         src->draw_smooth(x+r,y+0, x+r, y+r, x+0,y+r);
561         src->draw_smooth(x+0,y+r, x-r, y+r, x-r,y+0);
562         if( pt->e ) {
563                 src->set_pixel_color(RED);
564                 src->draw_x(pt->x, pt->y, d);
565         }
566         else {
567                 src->set_pixel_color(BLUE);
568                 src->draw_t(pt->x, pt->y, d);
569         }
570 }
571
572
573 static void get_vframe(VFrame *&vfrm, int w, int h, int color_model)
574 {
575         if( vfrm && ( vfrm->get_w() != w || vfrm->get_h() != h ) ) {
576                 delete vfrm;  vfrm = 0;
577         }
578         if( !vfrm ) vfrm = new VFrame(w, h, color_model, 0);
579 }
580
581 static void fill_edge(VFrame *vfrm, int w, int h)
582 {
583         int w1 = w-1, h1 = h-1;
584         float *dp = (float*) vfrm->get_data();
585         if( w1 > 0 ) for( int y=0; y<h1; ++y,dp+=w ) dp[w1] = dp[w1-1];
586         if( h1 > 0 ) for( int x=0; x<w; ++x ) dp[x] = dp[x-w];
587 }
588
589 int CriKey::process_buffer(VFrame *frame, int64_t start_position, double frame_rate)
590 {
591         load_configuration();
592         src = frame;
593         w = src->get_w(), h = src->get_h();
594         color_model = src->get_color_model();
595         bpp = BC_CModels::calculate_pixelsize(color_model);
596         is_float = BC_CModels::is_float(color_model);
597         is_yuv = BC_CModels::is_yuv(color_model);
598         comp = BC_CModels::components(color_model);
599         if( comp > 3 ) comp = 3;
600
601         read_frame(src, 0, start_position, frame_rate, 0);
602         get_vframe(edg, w, h, BC_A_FLOAT);
603
604         if( !engine )
605                 engine = new CriKeyEngine(this,
606                         PluginClient::get_project_smp() + 1,
607                         PluginClient::get_project_smp() + 1);
608
609         get_vframe(msk, w, h, BC_A8);
610         memset(msk->get_data(), 0xff, msk->get_data_size());
611
612         for( int i=0, n=config.points.size(); i<n; ++i ) {
613                 CriKeyPoint *pt = config.points[i];
614                 if( !pt->e ) continue;
615                 if( set_target(engine->color, pt->x, pt->y) ) continue;
616                 engine->threshold = pt->t;
617                 edg->clear_frame();
618                 engine->process_packages();
619                 fill_edge(edg, w, h);
620                 FillRegion fill_region(edg, msk);
621                 fill_region.fill(pt->x, pt->y);
622                 fill_region.run();
623         }
624
625 //crikey_pgm("/tmp/msk.pgm",msk);
626         switch( config.draw_mode ) {
627         case DRAW_ALPHA: draw_alpha(msk);  break;
628         case DRAW_EDGE:  draw_edge(edg);   break;
629         case DRAW_MASK:  draw_mask(msk);   break;
630         }
631
632         if( config.drag ) {
633                 for( int i=0, n=config.points.size(); i<n; ++i ) {
634                         CriKeyPoint *pt = config.points[i];
635                         src->set_pixel_color(config.selected == i ? GREEN : WHITE);
636                         draw_point(src, pt);
637                 }
638         }
639         return 0;
640 }
641
642
643 void CriKeyEngine::init_packages()
644 {
645         int y = 0, h1 = plugin->h-1;
646         for(int i = 0; i < get_total_packages(); ) {
647                 CriKeyPackage *pkg = (CriKeyPackage*)get_package(i++);
648                 pkg->y1 = y;
649                 y = h1 * i / LoadServer::get_total_packages();
650                 pkg->y2 = y;
651         }
652 }
653
654 LoadPackage* CriKeyEngine::new_package()
655 {
656         return new CriKeyPackage();
657 }
658
659 LoadClient* CriKeyEngine::new_client()
660 {
661         return new CriKeyUnit(this);
662 }
663
664 #define EDGE_MACRO(type, components, is_yuv) \
665 { \
666         uint8_t **src_rows = src->get_rows(); \
667         int comps = MIN(components, 3); \
668         for( int y=y1; y<y2; ++y ) { \
669                 uint8_t *row0 = src_rows[y], *row1 = src_rows[y+1]; \
670                 float *edgp = edg_rows[y]; \
671                 for( int x=x1; x<x2; ++edgp,++x,row0+=bpp,row1+=bpp ) { \
672                         type *r0 = (type*)row0, *r1 = (type*)row1; \
673                         float a00 = 0, a01 = 0, a10 = 0, a11 = 0; \
674                         for( int c=0; c<comps; ++c,++r0,++r1 ) { \
675                                 float t = target_color[c]; \
676                                 a00 += fabs(t - r0[0]); \
677                                 a01 += fabs(t - r0[components]); \
678                                 a10 += fabs(t - r1[0]); \
679                                 a11 += fabs(t - r1[components]); \
680                         } \
681                         float mx = scale * bmax(bmax(a00, a01), bmax(a10, a11)); \
682                         if( mx < threshold ) continue; \
683                         float mn = scale * bmin(bmin(a00, a01), bmin(a10, a11)); \
684                         if( mn >= threshold ) continue; \
685                         *edgp += (mx - mn); \
686                 } \
687         } \
688 } break
689
690
691 void CriKeyUnit::process_package(LoadPackage *package)
692 {
693         int color_model = server->plugin->color_model;
694         int bpp = server->plugin->bpp;
695         VFrame *src = server->plugin->src;
696         VFrame *edg = server->plugin->edg;
697         float **edg_rows = (float**)edg->get_rows();
698         float *target_color = server->color;
699         float threshold = 2.f * server->threshold*server->threshold;
700         float scale = 1./BC_CModels::calculate_max(color_model);
701         CriKeyPackage *pkg = (CriKeyPackage*)package;
702         int x1 = 0, x2 = server->plugin->w-1;
703         int y1 = pkg->y1, y2 = pkg->y2;
704
705         switch( color_model ) {
706         case BC_RGB_FLOAT:  EDGE_MACRO(float, 3, 0);
707         case BC_RGBA_FLOAT: EDGE_MACRO(float, 4, 0);
708         case BC_RGB888:     EDGE_MACRO(unsigned char, 3, 0);
709         case BC_YUV888:     EDGE_MACRO(unsigned char, 3, 1);
710         case BC_RGBA8888:   EDGE_MACRO(unsigned char, 4, 0);
711         case BC_YUVA8888:   EDGE_MACRO(unsigned char, 4, 1);
712         }
713 }
714