3 * Copyright (C) 1997-2015 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
26 #include "arraylist.h"
27 #include "bccmodels.h"
31 #include "edlsession.h"
33 #include "keyframes.h"
34 #include "overlayframe.h"
35 #include "pluginserver.h"
36 #include "preferences.h"
38 #include "sketcherwindow.h"
39 #include "transportque.inc"
43 void SketcherPoint::init(int id, int arc, coord x, coord y)
45 this->id = id; this->arc = arc;
46 this->x = x; this->y = y;
48 SketcherPoint::SketcherPoint(int id)
50 init(id, ARC_LINE, 0, 0);
52 SketcherPoint::SketcherPoint(int id, int arc, coord x, coord y)
56 SketcherPoint::~SketcherPoint()
59 SketcherPoint::SketcherPoint(SketcherPoint &pt)
63 int SketcherPoint::equivalent(SketcherPoint &that)
65 return this->id == that.id &&
66 this->arc == that.arc &&
67 EQUIV(this->x, that.x) &&
68 EQUIV(this->y, that.y) ? 1 : 0;
70 void SketcherPoint::copy_from(SketcherPoint &that)
72 this->id = that.id; this->arc = that.arc;
73 this->x = that.x; this->y = that.y;
75 void SketcherPoint::save_data(FileXML &output)
78 sprintf(point,"/POINT_%d",id);
79 output.tag.set_title(point+1);
80 output.tag.set_property("TYPE", arc);
81 output.tag.set_property("X", x);
82 output.tag.set_property("Y", y);
84 output.tag.set_title(point+0);
86 output.append_newline();
88 void SketcherPoint::read_data(FileXML &input)
90 id = atoi(input.tag.get_title() + 6);
91 arc = input.tag.get_property("TYPE", ARC_OFF);
92 x = input.tag.get_property("X", (coord)0);
93 y = input.tag.get_property("Y", (coord)0);
94 bclamp(arc, 0, ARC_SZ-1);
97 void SketcherCurve::init(int id, int pen, int width, int color)
104 SketcherCurve::SketcherCurve(int id)
106 init(id, 1, PEN_SQUARE, CV_COLOR);
108 SketcherCurve::SketcherCurve(int id, int pen, int width, int color)
110 init(id, pen, width, color);
112 SketcherCurve::~SketcherCurve()
115 SketcherCurve::SketcherCurve(SketcherCurve &cv)
119 int SketcherCurve::equivalent(SketcherCurve &that)
121 if( this->id != that.id ) return 0;
122 if( this->pen != that.pen ) return 0;
123 if( this->width != that.width ) return 0;
124 if( this->color != that.color ) return 0;
125 int n = this->points.size();
126 if( n != that.points.size() ) return 0;
127 for( int i=0; i<n; ++i ) {
128 if( !points[i]->equivalent(*that.points[i]) ) return 0;
132 void SketcherCurve::copy_from(SketcherCurve &that)
135 this->pen = that.pen;
136 this->width = that.width;
137 this->color = that.color;
138 int m = points.size(), n = that.points.size();
139 while( m > n ) points.remove_object_number(--m);
140 while( m < n ) { points.append(new SketcherPoint()); ++m; }
141 for( int i=0; i<n; ++i ) points[i]->copy_from(*that.points[i]);
143 void SketcherCurve::save_data(FileXML &output)
145 char curve[BCSTRLEN];
146 sprintf(curve,"/CURVE_%d",id);
147 output.tag.set_title(curve+1);
148 output.tag.set_property("PEN", pen);
149 output.tag.set_property("RADIUS", width);
150 output.tag.set_property("COLOR", color);
152 output.append_newline();
153 for( int i=0,n=points.size(); i<n; ++i )
154 points[i]->save_data(output);
155 output.tag.set_title(curve+0);
157 output.append_newline();
159 void SketcherCurve::read_data(FileXML &input)
161 id = atoi(input.tag.get_title() + 6);
162 pen = input.tag.get_property("PEN", PEN_OFF);
163 width = input.tag.get_property("RADIUS", 1.);
164 color = input.tag.get_property("COLOR", CV_COLOR);
165 bclamp(pen, 0, PEN_SZ-1);
168 int SketcherConfig::new_curve(int pen, int width, int color)
170 int k = curves.size(), id = 1;
171 for( int i=k; --i>=0; ) {
172 int n = curves[i]->id;
173 if( n >= id ) id = n + 1;
175 SketcherCurve *cv = new SketcherCurve(id, pen, width, color);
180 int Sketcher::new_curve(int pen, int width, int color)
182 cv_selected = config.new_curve(pen, width, color);
186 int Sketcher::new_curve()
188 return new_curve(PEN_XLANT, 1, CV_COLOR);
191 int Sketcher::new_point(SketcherCurve *cv, int arc, coord x, coord y, int idx)
194 for( int i=cv->points.size(); --i>=0; ) {
195 int n = cv->points[i]->id;
196 if( n >= id ) id = n + 1;
198 SketcherPoint *pt = new SketcherPoint(id, arc, x, y);
199 int n = cv->points.size();
200 if( idx < 0 || idx > n ) idx = n;
201 cv->points.insert(pt, idx);
205 int Sketcher::new_point(int idx, int arc)
207 int ci = cv_selected;
208 if( ci < 0 || ci >= config.curves.size() )
210 SketcherCurve *cv = config.curves[ci];
211 EDLSession *session = get_edl()->session;
212 coord x = !session ? 0.f : session->output_w / 2.f;
213 coord y = !session ? 0.f : session->output_h / 2.f;
214 return new_point(cv, arc, x, y, idx);
217 double SketcherCurve::nearest_point(int &pi, coord x, coord y)
220 double dist = DBL_MAX;
221 for( int i=0; i<points.size(); ++i ) {
222 SketcherPoint *p = points[i];
223 double d = DISTANCE(x,y, p->x,p->y);
224 if( d < dist ) { dist = d; pi = i; }
226 return pi >= 0 ? dist : -1.;
229 double SketcherConfig::nearest_point(int &ci, int &pi, coord x, coord y)
231 double dist = DBL_MAX;
233 for( int i=0; i<curves.size(); ++i ) {
234 SketcherCurve *crv = curves[i];
235 SketcherPoints &points = crv->points;
236 for( int k=0; k<points.size(); ++k ) {
237 SketcherPoint *p = points[k];
238 double d = DISTANCE(x,y, p->x,p->y);
239 if( d < dist ) { dist = d; ci = i; pi = k; }
242 return pi >= 0 ? dist : -1.;
246 REGISTER_PLUGIN(Sketcher)
248 SketcherConfig::SketcherConfig()
252 SketcherConfig::~SketcherConfig()
256 int SketcherConfig::equivalent(SketcherConfig &that)
258 if( this->aliasing != that.aliasing ) return 0;
259 if( this->curves.size() != that.curves.size() ) return 0;
260 for( int i=0, n=curves.size(); i<n; ++i ) {
261 if( !curves[i]->equivalent(*that.curves[i]) ) return 0;
266 void SketcherConfig::copy_from(SketcherConfig &that)
268 this->aliasing = that.aliasing;
269 int m = curves.size(), n = that.curves.size();
270 while( m > n ) curves.remove_object_number(--m);
271 while( m < n ) { curves.append(new SketcherCurve()); ++m; }
272 for( int i=0; i<n; ++i ) curves[i]->copy_from(*that.curves[i]);
275 void SketcherConfig::interpolate(SketcherConfig &prev, SketcherConfig &next,
276 long prev_frame, long next_frame, long current_frame)
278 this->aliasing = prev.aliasing;
280 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
281 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
283 curves.remove_all_objects();
284 int prev_cv_sz = prev.curves.size();
285 int next_cv_sz = next.curves.size();
286 for( int i=0; i<prev_cv_sz; ++i ) {
287 SketcherCurve *pcv = prev.curves[i], *ncv = 0;
288 SketcherCurve *cv = curves.append(new SketcherCurve());
289 int k = next_cv_sz; // associated by id in next
290 while( --k >= 0 && pcv->id != (ncv=next.curves[k])->id );
294 cv->width = pcv->width == ncv->width ? pcv->width :
295 pcv->width*prev_scale + ncv->width*next_scale + 0.5;
296 int pr = (pcv->color>>16)&0xff, nr = (ncv->color>>16)&0xff;
297 int pg = (pcv->color>> 8)&0xff, ng = (ncv->color>> 8)&0xff;
298 int pb = (pcv->color>> 0)&0xff, nb = (ncv->color>> 0)&0xff;
299 int pa = (~pcv->color>>24)&0xff, na = (~ncv->color>>24)&0xff;
300 int r = pr == nr ? pr : pr*prev_scale + nr*next_scale + 0.5;
301 int g = pg == ng ? pg : pg*prev_scale + ng*next_scale + 0.5;
302 int b = pb == nb ? pb : pb*prev_scale + nb*next_scale + 0.5;
303 int a = pa == na ? pa : pa*prev_scale + na*next_scale + 0.5;
304 bclamp(r,0,255); bclamp(g,0,255); bclamp(b,0,255); bclamp(a,0,255);
305 cv->color = (~a<<24) | (r<<16) | (g<<8) | (b<<0);
306 int prev_pt_sz = pcv->points.size(), next_pt_sz = ncv->points.size();
307 for( int j=0; j<prev_pt_sz; ++j ) {
308 SketcherPoint &pt = *pcv->points[j], *nt = 0;
309 k = next_pt_sz; // associated by id in next
310 while( --k >= 0 && pt.id != (nt=ncv->points[k])->id );
311 coord x = pt.x, y = pt.y;
314 x = x * prev_scale + nt->x * next_scale;
316 y = y * prev_scale + nt->y * next_scale;
318 cv->points.append(new SketcherPoint(pt.id, pt.arc, x, y));
326 void SketcherConfig::limits()
331 Sketcher::Sketcher(PluginServer *server)
332 : PluginVClient(server)
343 Sketcher::~Sketcher()
347 delete overlay_frame;
350 const char* Sketcher::plugin_title() { return N_("Sketcher"); }
351 int Sketcher::is_realtime() { return 1; }
352 int Sketcher::is_synthesis() { return 1; }
354 NEW_WINDOW_MACRO(Sketcher, SketcherWindow);
355 LOAD_CONFIGURATION_MACRO(Sketcher, SketcherConfig)
357 void Sketcher::render_gui(void *data)
359 Sketcher *sketcher = (Sketcher *)data;
360 sketcher->drag = drag;
361 sketcher->cv_selected = cv_selected;
362 sketcher->pt_selected = pt_selected;
365 int Sketcher::is_dragging()
370 send_render_gui(this);
375 void SketcherConfig::save_data(KeyFrame *keyframe)
378 // cause data to be stored directly in text
379 output.set_shared_output(keyframe->xbuf);
381 output.tag.set_title("SKETCHER");
382 output.tag.set_property("ALIASING", aliasing);
384 output.append_newline();
385 for( int i=0,n=curves.size(); i<n; ++i ) {
386 curves[i]->save_data(output);
388 output.tag.set_title("/SKETCHER");
390 output.append_newline();
391 output.terminate_string();
394 void Sketcher::save_data(KeyFrame *keyframe)
396 config.save_data(keyframe);
399 void SketcherConfig::read_data(KeyFrame *keyframe)
402 input.set_shared_input(keyframe->xbuf);
403 curves.remove_all_objects();
405 SketcherCurve *cv = 0;
407 while( !(result=input.read_tag()) ) {
408 if( input.tag.title_is("SKETCHER") ) {
409 aliasing = input.tag.get_property("ALIASING", aliasing);
411 else if( !strncmp(input.tag.get_title(),"CURVE_",6) ) {
412 cv = new SketcherCurve();
413 cv->read_data(input);
416 else if( !strncmp(input.tag.get_title(),"/CURVE_",7) )
418 else if( !strncmp(input.tag.get_title(),"POINT_",6) ) {
420 SketcherPoint *pt = new SketcherPoint();
421 pt->read_data(input);
422 cv->points.append(pt);
425 printf("SketcherConfig::read_data: no curve for point\n");
432 void Sketcher::read_data(KeyFrame *keyframe)
434 config.read_data(keyframe);
435 if( !config.curves.size() )
439 void SketcherPoint::update_parameter(SketcherPoint *prev, SketcherPoint *src)
441 if( prev->arc != src->arc ) arc = src->arc;
442 if( prev->x != src->x ) x = src->x;
443 if( prev->y != src->y ) y = src->y;
446 void SketcherCurve::update_parameter(SketcherCurve *prev, SketcherCurve *src)
448 if( prev->pen != src->pen ) pen = src->pen;
449 if( prev->width != src->width ) width = src->width;
450 if( prev->color != src->color ) color = src->color;
451 int prev_points = prev->points.size();
452 int src_points = src->points.size();
453 int dst_points = this->points.size();
454 int npoints = bmin(prev_points, bmin(src_points, dst_points));
455 for( int i=0; i<npoints; ++i ) {
456 SketcherPoint *prev_point = prev->points[i];
457 SketcherPoint *src_point = src->points[i];
458 SketcherPoint *dst_point = this->points[i];
459 dst_point->update_parameter(prev_point, src_point);
463 void Sketcher::span_keyframes(KeyFrame *src, int64_t start, int64_t end)
465 SketcherConfig src_config;
466 src_config.read_data(src);
467 KeyFrames *keyframes = (KeyFrames *)src->autos;
468 KeyFrame *prev = keyframes->get_prev_keyframe(start, PLAY_FORWARD);
469 SketcherConfig prev_config;
470 prev_config.read_data(prev);
471 // Always update the first one
472 update_parameter(prev_config, src_config, prev);
473 KeyFrame *curr = (KeyFrame*)prev->next;
474 while( curr && curr->position < end ) {
475 update_parameter(prev_config, src_config, curr);
476 curr = (KeyFrame*)curr->next;
480 void Sketcher::update_parameter(SketcherConfig &prev_config, SketcherConfig &src_config,
483 SketcherConfig dst_config;
484 dst_config.read_data(keyframe);
485 if( prev_config.aliasing != src_config.aliasing )
486 dst_config.aliasing = src_config.aliasing;
487 int src_curves = src_config.curves.size();
488 int dst_curves = dst_config.curves.size();
489 int prev_curves = prev_config.curves.size();
490 int ncurves = bmin(prev_curves, bmin(src_curves, dst_curves));
491 for( int i=0; i<ncurves; ++i ) {
492 SketcherCurve *prev_curve = prev_config.curves[i];
493 SketcherCurve *src_curve = src_config.curves[i];
494 SketcherCurve *dst_curve = dst_config.curves[i];
495 dst_curve->update_parameter(prev_curve, src_curve);
497 dst_config.save_data(keyframe);
500 void Sketcher::update_gui()
502 if( !thread ) return;
503 thread->window->lock_window("Sketcher::update_gui");
504 if( load_configuration() ) {
505 SketcherWindow *window = (SketcherWindow*)thread->window;
506 window->update_gui();
509 thread->window->unlock_window();
512 void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color, int d)
514 int r = d/2+1, x = pt->x, y = pt->y;
515 vfrm->set_pixel_color(color);
516 vfrm->draw_smooth(x-r,y+0, x-r, y-r, x+0,y-r);
517 vfrm->draw_smooth(x+0,y-r, x+r, y-r, x+r,y+0);
518 vfrm->draw_smooth(x+r,y+0, x+r, y+r, x+0,y+r);
519 vfrm->draw_smooth(x+0,y+r, x-r, y+r, x-r,y+0);
520 vfrm->draw_x(pt->x, pt->y, d);
522 void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color)
524 draw_point(vfrm, pt, color, bmax(w,h)/200 + 2);
528 int SketcherVPen::draw_mask(int x, int y)
530 int w = vfrm->get_w(), h = vfrm->get_h();
531 if( x < 0 || x >= w ) return 1;
532 if( y < 0 || y >= h ) return 1;
533 msk[w * y + x] = 0xff;
537 int SketcherVPen::draw_pixel(float x, float y, float frac, int axis)
540 return VFrame::draw_pixel(x, y, frac, axis);
543 int SketcherPenSquare::draw_pixel(float x, float y, float a)
545 vfrm->set_draw_alpha(a);
546 vfrm->draw_line(x-n, y, x+n, y);
547 for( int i=-n; i<n; ++i )
548 vfrm->draw_line(x-n, y+i, x+n, y+i);
551 int SketcherPenPlus::draw_pixel(float x, float y, float a)
553 vfrm->set_draw_alpha(a);
555 vfrm->draw_line(x-n, y, x+n, y);
556 vfrm->draw_line(x, y-n, x, y+n);
559 vfrm->draw_pixel(x, y);
562 int SketcherPenSlant::draw_pixel(float x, float y, float a)
564 vfrm->set_draw_alpha(a);
565 vfrm->draw_line(x-n, y+n, x+n, y-n);
566 vfrm->draw_line(x-n+1, y+n, x+n+1, y-n);
567 vfrm->draw_line(x-n, y+n+1, x+n, y-n+1);
570 int SketcherPenXlant::draw_pixel(float x, float y, float a)
572 vfrm->set_draw_alpha(a);
573 vfrm->draw_line(x-n, y+n, x+n, y-n);
574 vfrm->draw_line(x-n+1, y+n, x+n+1, y-n);
575 vfrm->draw_line(x-n, y+n+1, x+n, y-n+1);
576 vfrm->draw_line(x-n, y-n, x+n, y+n);
577 vfrm->draw_line(x-n+1, y-n, x+n+1, y+n);
578 vfrm->draw_line(x-n, y-n+1, x+n, y-n+1);
583 SketcherVPen *SketcherCurve::new_vpen(VFrame *out)
586 case PEN_SQUARE: return new SketcherPenSquare(out, width);
587 case PEN_PLUS: return new SketcherPenPlus(out, width);
588 case PEN_SLANT: return new SketcherPenSlant(out, width);
589 case PEN_XLANT: return new SketcherPenXlant(out, width);
594 static int intersects_at(float &x, float &y,
595 float ax,float ay, float bx, float by, float cx,float cy, // line slope ab thru c
596 float dx,float dy, float ex, float ey, float fx,float fy, // line slope de thru f
599 float badx = bx - ax, bady = by - ay;
600 float eddx = ex - dx, eddy = ey - dy;
601 float d = badx*eddy - bady*eddx;
603 if( fabsf(d) < 1 ) { ret = 1; d = signbit(d) ? -1 : 1; }
604 x = (badx*cy*eddx - badx*eddx*fy + badx*eddy*fx - bady*cx*eddx) / d;
605 y = (badx*cy*eddy - bady*cx*eddy - bady*eddx*fy + bady*eddy*fx) / d;
606 if( mx > 0 ) { bclamp(x, -mx,mx); bclamp(y, -mx,mx); }
610 static void smooth_axy(float &ax, float &ay,
611 float bx, float by, float cx, float cy, float dx, float dy)
613 //middle of bd reflected around ctr
614 // point ctr = (b+d)/2, dv=c-ctr, a=ctr-dv;
615 float xc = (bx+dx)*.5f, yc = (by+dy)*.5f;
616 float xd = cx - xc, yd = cy - yc;
617 ax = xc - xd; ay = yc - yd;
619 static void smooth_dxy(float &dx, float &dy,
620 float ax, float ay, float bx, float by, float cx, float cy)
622 //middle of ac reflected around ctr
623 // point ctr = (a+c)/2, dv=c-ctr, d=ctr-dv;
624 float xc = (ax+cx)*.5f, yc = (ay+cy)*.5f;
625 float xd = bx - xc, yd = by - yc;
626 dx = xc - xd; dy = yc - yd;
629 static int convex(float ax,float ay, float bx,float by,
630 float cx,float cy, float dx,float dy)
632 float abdx = bx-ax, abdy = by-ay;
633 float acdx = cx-ax, acdy = cy-ay;
634 float bcdx = cx-bx, bcdy = cy-by;
635 float bddx = dx-bx, bddy = dy-by;
636 float abc = abdx*acdy - abdy*acdx;
637 float bcd = bcdx*bddy - bcdy*bddx;
639 return !v ? 0 : v>0 ? 1 : -1;
644 class segment { public: int y, lt, rt; };
645 ArrayList<segment> stack;
647 void push(int y, int lt, int rt) {
648 segment &seg = stack.append();
649 seg.y = y; seg.lt = lt; seg.rt = rt;
651 void pop(int &y, int <, int &rt) {
652 segment &seg = stack.last();
653 y = seg.y; lt = seg.lt; rt = seg.rt;
660 SketcherPoints &points;
662 SketcherPoint *next();
663 bool exists() { return stack.size() > 0; }
664 void draw_pixel(int x, int y) { img->draw_pixel(x, y); }
665 void start_at(int x, int y);
667 FillRegion(SketcherPoints &pts, SketcherVPen *vpen);
671 FillRegion::FillRegion(SketcherPoints &pts, SketcherVPen *vpen)
674 this->img = vpen->vfrm;
675 this->msk = vpen->msk;
676 this->w = img->get_w();
677 this->h = img->get_h();
680 FillRegion::~FillRegion()
684 void FillRegion::start_at(int x, int y)
691 void FillRegion::run()
693 img->set_draw_alpha(1);
694 while( stack.size() > 0 ) {
698 for( int x=ilt; x<=irt; ++x,++ofs ) {
700 if( msk[ofs] ) continue;
704 for( int i=lt; --i>=0; ) {
706 if( msk[--lofs] ) break;
707 msk[lofs] = 0xff; lt = i;
710 for( int i=rt; ++i< w; ) {
712 if( msk[++rofs] ) break;
713 msk[rofs] = 0xff; rt = i;
715 if( y+1 < h ) push(y+1, lt, rt);
716 if( y-1 >= 0 ) push(y-1, lt, rt);
721 SketcherPoint *FillRegion::next()
723 while( nxt < points.size() ) {
724 SketcherPoint *pt = points[nxt++];
725 if( pt->arc == ARC_OFF ) continue;
726 if( pt->arc != ARC_FILL ) return pt;
727 start_at(pt->x, pt->y);
732 void SketcherCurve::draw(VFrame *img, int alias)
734 if( !points.size() ) return;
735 img->set_pixel_color(color, (~color>>24)&0xff);
736 const float fmx = 16383;
737 SketcherVPen *vpen = new_vpen(img);
738 vpen->set_draw_flags(alias);
739 FillRegion fill(points, vpen);
740 SketcherPoint *pnt0 = fill.next();
741 SketcherPoint *pnt1 = pnt0 ? fill.next() : 0;
742 SketcherPoint *pnt2 = pnt1 ? fill.next() : 0;
743 if( pnt0 && pnt1 && pnt2 ) {
744 SketcherPoint *pt0 = pnt0, *pt1 = pnt1, *pt2 = pnt2;
745 float ax,ay, bx,by, cx,cy, dx,dy, sx,sy;
746 bx = pt0->x; by = pt0->y;
747 cx = pt1->x; cy = pt1->y;
748 dx = pt2->x; dy = pt2->y;
749 smooth_axy(ax,ay, bx,by, cx,cy, dx,dy);
751 dx = pt2->x; dy = pt2->y;
754 if( convex(ax,ay, bx,by, cx,cy, dx,dy) >= 0 ) {
755 // s = ac thru b x bd thru c
756 intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
757 vpen->draw_smooth(bx,by, sx,sy, cx,cy);
761 vpen->draw_line(bx, by, cx, cy);
764 ax = bx; ay = by; pt0 = pt1;
765 bx = cx; by = cy; pt1 = pt2;
766 cx = dx; cy = dy; pt2 = fill.next();
770 vpen->draw_line(bx, by, cx, cy);
771 if( fill.exists() ) {
772 dx = pnt0->x; dy = pnt0->y;
773 vpen->draw_line(cx,cy, dx,dy);
777 if( fill.exists() ) {
778 dx = pnt0->x; dy = pnt0->y;
779 intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
780 vpen->draw_smooth(bx,by, sx,sy, cx,cy);
784 dx = pnt1->x; dy = pnt1->y;
787 smooth_dxy(dx,dy, ax,ay, bx,by, cx,cy);
788 intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
789 vpen->draw_smooth(bx,by, sx,sy, cx,cy);
794 else if( pnt0 && pnt1 ) {
795 vpen->draw_line(pnt0->x, pnt0->y, pnt1->x, pnt1->y);
798 vpen->draw_pixel(pnt0->x, pnt0->y, 1);
803 int Sketcher::process_realtime(VFrame *input, VFrame *output)
805 this->input = input; this->output = output;
806 w = output->get_w(); h = output->get_h();
808 load_configuration();
810 int out_color_model = output->get_color_model();
811 int color_model = out_color_model;
812 switch( color_model ) { // add alpha if needed
813 case BC_RGB888: color_model = BC_RGBA8888; break;
814 case BC_YUV888: color_model = BC_YUVA8888; break;
815 case BC_RGB161616: color_model = BC_RGBA16161616; break;
816 case BC_YUV161616: color_model = BC_YUVA16161616; break;
817 case BC_RGB_FLOAT: color_model = BC_RGBA_FLOAT; break;
818 case BC_RGB_FLOATP: color_model = BC_RGBA_FLOATP; break;
820 if( color_model == out_color_model ) {
821 delete out; out = output;
822 if( output != input )
823 output->transfer_from(input);
826 VFrame::get_temp(out, w, h, color_model);
827 out->transfer_from(input);
829 VFrame::get_temp(img, w, h, color_model);
831 if( !overlay_frame ) {
832 int cpus = server->preferences->project_smp;
833 int max = (w*h)/0x80000 + 2;
834 if( cpus > max ) cpus = max;
835 overlay_frame = new OverlayFrame(cpus);
838 for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
839 SketcherCurve *cv = config.curves[ci];
840 if( cv->pen == PEN_OFF ) continue;
841 int m = cv->points.size();
844 int alias = config.aliasing < 0 ? ALIAS_OFF :
845 config.aliasing > 0 ? ALIAS_DBL : ALIAS_NRM;
846 cv->draw(img, alias);
847 overlay_frame->overlay(out, img, 0,0,w,h, 0,0,w,h,
848 1.f, TRANSFER_SRC_OVER, NEAREST_NEIGHBOR);
851 if( is_dragging() ) {
852 for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
853 SketcherCurve *cv = config.curves[ci];
854 for( int pi=0,m=cv->points.size(); pi<m; ++pi ) {
855 int color = pi==pt_selected && ci==cv_selected ?
857 draw_point(out, cv->points[pi], color);
863 output->transfer_from(out);
870 void SketcherPoints::dump()
872 for( int i=0; i<size(); ++i ) {
873 SketcherPoint *pt = get(i);
874 printf(" Pt %d, id=%d, arc=%s, x=%0.1f, y=%0.1f\n",
875 i, pt->id, pt_type[pt->arc], pt->x, pt->y);
878 void SketcherCurves::dump()
880 for( int i=0; i<size(); ++i ) {
881 SketcherCurve *cv = get(i);
882 printf("Curve %d, id=%d, pen=%s, r=%d, color=%02x%02x%02x%02x, %d points\n",
883 i, cv->id, cv_pen[cv->pen], cv->width, (~cv->color>>24)&0xff,
884 (cv->color>>16)&0xff, (cv->color>>8)&0xff, (cv->color>>0)&0xff,
889 void SketcherConfig::dump()
891 printf("Config %d curves\n", curves.size());