X-Git-Url: https://git.cinelerra-gg.org/git/?p=goodguy%2Fcinelerra.git;a=blobdiff_plain;f=cinelerra-5.1%2Fplugins%2Fsketcher%2Fsketcher.C;h=906980467729a76a626162aaa6d5fb840cd72c29;hp=e3b61b192e15f85f030c4b69d80660c95a011ba0;hb=b64142b983e53a7f0bfe8422efc3bb4f4ebb897c;hpb=b46dd3711f7aaa94d2df9f27cd2ce16f7487693a diff --git a/cinelerra-5.1/plugins/sketcher/sketcher.C b/cinelerra-5.1/plugins/sketcher/sketcher.C index e3b61b19..90698046 100644 --- a/cinelerra-5.1/plugins/sketcher/sketcher.C +++ b/cinelerra-5.1/plugins/sketcher/sketcher.C @@ -29,12 +29,15 @@ #include "clip.h" #include "edlsession.h" #include "filexml.h" +#include "overlayframe.h" +#include "pluginserver.h" +#include "preferences.h" #include "sketcher.h" #include "sketcherwindow.h" #include "language.h" #include "vframe.h" -void SketcherPoint::init(int id, int pty, int x, int y) +void SketcherPoint::init(int id, int pty, coord x, coord y) { this->id = id; this->pty = pty; this->x = x; this->y = y; @@ -43,7 +46,7 @@ SketcherPoint::SketcherPoint(int id) { init(id, PTY_LINE, 0, 0); } -SketcherPoint::SketcherPoint(int id, int pty, int x, int y) +SketcherPoint::SketcherPoint(int id, int pty, coord x, coord y) { init(id, pty, x, y); } @@ -58,8 +61,8 @@ int SketcherPoint::equivalent(SketcherPoint &that) { return this->id == that.id && this->pty == that.pty && - this->x == that.x && - this->y == that.y ? 1 : 0; + EQUIV(this->x, that.x) && + EQUIV(this->y, that.y) ? 1 : 0; } void SketcherPoint::copy_from(SketcherPoint &that) { @@ -83,15 +86,15 @@ void SketcherPoint::read_data(FileXML &input) { id = atoi(input.tag.get_title() + 6); pty = input.tag.get_property("TYPE", PTY_OFF); - x = input.tag.get_property("X", 0.f); - y = input.tag.get_property("Y", 0.f); + x = input.tag.get_property("X", (coord)0); + y = input.tag.get_property("Y", (coord)0); bclamp(pty, 0, PTY_SZ-1); } -void SketcherCurve::init(int id, int pen, int radius, int color) +void SketcherCurve::init(int id, int pen, int width, int color) { this->id = id; - this->radius = radius; + this->width = width; this->pen = pen; this->color = color; } @@ -99,9 +102,9 @@ SketcherCurve::SketcherCurve(int id) { init(id, 1, PTY_LINE, CV_COLOR); } -SketcherCurve::SketcherCurve(int id, int pen, int radius, int color) +SketcherCurve::SketcherCurve(int id, int pen, int width, int color) { - init(id, pen, radius, color); + init(id, pen, width, color); } SketcherCurve::~SketcherCurve() { @@ -114,7 +117,7 @@ int SketcherCurve::equivalent(SketcherCurve &that) { if( this->id != that.id ) return 0; if( this->pen != that.pen ) return 0; - if( this->radius != that.radius ) return 0; + if( this->width != that.width ) return 0; if( this->color != that.color ) return 0; int n = this->points.size(); if( n != that.points.size() ) return 0; @@ -127,7 +130,7 @@ void SketcherCurve::copy_from(SketcherCurve &that) { this->id = that.id; this->pen = that.pen; - this->radius = that.radius; + this->width = that.width; this->color = that.color; int m = points.size(), n = that.points.size(); while( m > n ) points.remove_object_number(--m); @@ -141,7 +144,7 @@ void SketcherCurve::save_data(FileXML &output) sprintf(curve,"/CURVE_%d",id); output.tag.set_title(curve+1); output.tag.set_property("PEN", pen); - output.tag.set_property("RADIUS", radius); + output.tag.set_property("RADIUS", width); output.tag.set_property("COLOR", color); output.append_tag(); output.append_newline(); @@ -155,12 +158,12 @@ void SketcherCurve::read_data(FileXML &input) { id = atoi(input.tag.get_title() + 6); pen = input.tag.get_property("PEN", PTY_OFF); - radius = input.tag.get_property("RADIUS", 1.); + width = input.tag.get_property("RADIUS", 1.); color = input.tag.get_property("COLOR", CV_COLOR); bclamp(pen, 0, PEN_SZ-1); } -int Sketcher::new_curve(int pen, int radius, int color) +int Sketcher::new_curve(int pen, int width, int color) { SketcherCurves &curves = config.curves; int k = curves.size(), id = 1; @@ -168,7 +171,7 @@ int Sketcher::new_curve(int pen, int radius, int color) int n = config.curves[i]->id; if( n >= id ) id = n + 1; } - SketcherCurve *cv = new SketcherCurve(id, pen, radius, color); + SketcherCurve *cv = new SketcherCurve(id, pen, width, color); curves.append(cv); config.cv_selected = k; return k; @@ -179,7 +182,7 @@ int Sketcher::new_curve() return new_curve(PEN_XLANT, 1, CV_COLOR); } -int Sketcher::new_point(SketcherCurve *cv, int pty, int x, int y, int idx) +int Sketcher::new_point(SketcherCurve *cv, int pty, coord x, coord y, int idx) { int id = 1; for( int i=cv->points.size(); --i>=0; ) { @@ -200,11 +203,40 @@ int Sketcher::new_point(int idx) return -1; SketcherCurve *cv = config.curves[ci]; EDLSession *session = get_edlsession(); - int x = !session ? 0.f : session->output_w / 2.f; - int y = !session ? 0.f : session->output_h / 2.f; + coord x = !session ? 0.f : session->output_w / 2.f; + coord y = !session ? 0.f : session->output_h / 2.f; return new_point(cv, PTY_LINE, x, y, idx); } +double SketcherCurve::nearest_point(int &pi, coord x, coord y) +{ + pi = -1; + double dist = DBL_MAX; + for( int i=0; ix,p->y); + if( d < dist ) { dist = d; pi = i; } + } + return pi >= 0 ? dist : -1.; +} + +double SketcherConfig::nearest_point(int &ci, int &pi, coord x, coord y) +{ + double dist = DBL_MAX; + ci = -1; pi = -1; + for( int i=0; ipoints; + for( int k=0; kx,p->y); + if( d < dist ) { dist = d; ci = i; pi = k; } + } + } + return pi >= 0 ? dist : -1.; +} + + REGISTER_PLUGIN(Sketcher) SketcherConfig::SketcherConfig() @@ -261,17 +293,29 @@ void SketcherConfig::interpolate(SketcherConfig &prev, SketcherConfig &next, if( k >= 0 ) { cv->id = pcv->id; cv->pen = pcv->pen; - cv->radius = pcv->radius; - cv->color = pcv->color; + cv->width = pcv->width == ncv->width ? pcv->width : + pcv->width*prev_scale + ncv->width*next_scale + 0.5; + int pr = (pcv->color>>16)&0xff, nr = (ncv->color>>16)&0xff; + int pg = (pcv->color>> 8)&0xff, ng = (ncv->color>> 8)&0xff; + int pb = (pcv->color>> 0)&0xff, nb = (ncv->color>> 0)&0xff; + int pa = (~pcv->color>>24)&0xff, na = (~ncv->color>>24)&0xff; + int r = pr == nr ? pr : pr*prev_scale + nr*next_scale + 0.5; + int g = pg == ng ? pg : pg*prev_scale + ng*next_scale + 0.5; + int b = pb == nb ? pb : pb*prev_scale + nb*next_scale + 0.5; + int a = pa == na ? pa : pa*prev_scale + na*next_scale + 0.5; + bclamp(r,0,255); bclamp(g,0,255); bclamp(b,0,255); bclamp(a,0,255); + cv->color = (~a<<24) | (r<<16) | (g<<8) | (b<<0); int prev_pt_sz = pcv->points.size(), next_pt_sz = ncv->points.size(); for( int j=0; jpoints[j], *nt = 0; k = next_pt_sz; // associated by id in next while( --k >= 0 && pt.id != (nt=ncv->points[k])->id ); - int x = pt.x, y = pt.y; + coord x = pt.x, y = pt.y; if( k >= 0 ) { - x = x * prev_scale + nt->x * next_scale; - y = y * prev_scale + nt->y * next_scale; + if( x != nt->x ) + x = x * prev_scale + nt->x * next_scale; + if( y != nt->y ) + y = y * prev_scale + nt->y * next_scale; } cv->points.append(new SketcherPoint(pt.id, pt.pty, x, y)); } @@ -289,10 +333,16 @@ void SketcherConfig::limits() Sketcher::Sketcher(PluginServer *server) : PluginVClient(server) { + img = 0; + out = 0; + overlay_frame = 0; } Sketcher::~Sketcher() { + delete img; + delete out; + delete overlay_frame; } const char* Sketcher::plugin_title() { return N_("Sketcher"); } @@ -387,12 +437,20 @@ void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color) } +int SketcherVPen::draw_pixel(int x, int y) +{ + if( x >= 0 && x < vfrm->get_w() && + y >= 0 && y < vfrm->get_h() ) + msk[vfrm->get_w()*y + x] = 0xff; + return 0; +} + int SketcherPenSquare::draw_pixel(int x, int y) { vfrm->draw_line(x-n, y, x+n, y); for( int i=-n; idraw_line(x-n, y+i, x+n, y+i); - return 0; + return SketcherVPen::draw_pixel(x, y); } int SketcherPenPlus::draw_pixel(int x, int y) { @@ -402,14 +460,14 @@ int SketcherPenPlus::draw_pixel(int x, int y) } else vfrm->draw_pixel(x, y); - return 0; + return SketcherVPen::draw_pixel(x, y); } int SketcherPenSlant::draw_pixel(int x, int y) { vfrm->draw_line(x-n, y+n, x+n, y-n); vfrm->draw_line(x-n+1, y+n, x+n+1, y-n); vfrm->draw_line(x-n, y+n+1, x+n, y-n+1); - return 0; + return SketcherVPen::draw_pixel(x, y); } int SketcherPenXlant::draw_pixel(int x, int y) { @@ -419,17 +477,17 @@ int SketcherPenXlant::draw_pixel(int x, int y) vfrm->draw_line(x-n, y-n, x+n, y+n); vfrm->draw_line(x-n+1, y-n, x+n+1, y+n); vfrm->draw_line(x-n, y-n+1, x+n, y-n+1); - return 0; + return SketcherVPen::draw_pixel(x, y); } -VFrame *SketcherCurve::new_vpen(VFrame *out) +SketcherVPen *SketcherCurve::new_vpen(VFrame *out) { switch( pen ) { - case PEN_SQUARE: return new SketcherPenSquare(out, radius); - case PEN_PLUS: return new SketcherPenPlus(out, radius); - case PEN_SLANT: return new SketcherPenSlant(out, radius); - case PEN_XLANT: return new SketcherPenXlant(out, radius); + case PEN_SQUARE: return new SketcherPenSquare(out, width); + case PEN_PLUS: return new SketcherPenPlus(out, width); + case PEN_SLANT: return new SketcherPenSlant(out, width); + case PEN_XLANT: return new SketcherPenXlant(out, width); } return 0; } @@ -454,7 +512,7 @@ static void smooth_axy(float &ax, float &ay, float bx, float by, float cx, float cy, float dx, float dy) { //middle of bd reflected around ctr -// point ctr = b+d/2, dv=c-ctr, a=ctr-dv; +// point ctr = (b+d)/2, dv=c-ctr, a=ctr-dv; float xc = (bx+dx)*.5f, yc = (by+dy)*.5f; float xd = cx - xc, yd = cy - yc; ax = xc - xd; ay = yc - yd; @@ -463,7 +521,7 @@ static void smooth_dxy(float &dx, float &dy, float ax, float ay, float bx, float by, float cx, float cy) { //middle of ac reflected around ctr -// point ctr = a+c/2, dv=c-ctr, d=ctr-dv; +// point ctr = (a+c)/2, dv=c-ctr, d=ctr-dv; float xc = (ax+cx)*.5f, yc = (ay+cy)*.5f; float xd = bx - xc, yd = by - yc; dx = xc - xd; dy = yc - yd; @@ -484,27 +542,115 @@ static int convex(float ax,float ay, float bx,float by, } #endif -void SketcherCurve::draw(VFrame *out) + +class FillRegion +{ + class segment { public: int y, lt, rt; }; + ArrayList stack; + + void push(int y, int lt, int rt) { + segment &seg = stack.append(); + seg.y = y; seg.lt = lt; seg.rt = rt; + } + void pop(int &y, int <, int &rt) { + segment &seg = stack.last(); + y = seg.y; lt = seg.lt; rt = seg.rt; + stack.remove(); + } + + VFrame *img; + uint8_t *msk; + int w, h, nxt; + SketcherPoints &points; +public: + SketcherPoint *next(); + bool exists() { return stack.size() > 0; } + void start_at(int x, int y); + void run(); + FillRegion(SketcherPoints &pts, SketcherVPen *vpen); + ~FillRegion(); +}; + +FillRegion::FillRegion(SketcherPoints &pts, SketcherVPen *vpen) + : points(pts) +{ + this->img = vpen->vfrm; + this->msk = vpen->msk; + this->w = img->get_w(); + this->h = img->get_h(); + nxt = 0; +} +FillRegion::~FillRegion() +{ +} + +void FillRegion::start_at(int x, int y) +{ + bclamp(x, 0, w-1); + bclamp(y, 0, h-1); + push(y, x, x); +} + +void FillRegion::run() +{ + while( stack.size() > 0 ) { + int y, ilt, irt; + pop(y, ilt, irt); + int ofs = y*w + ilt; + for( int x=ilt; x<=irt; ++x,++ofs ) { + if( msk[ofs] ) continue; + msk[ofs] = 0xff; + img->draw_pixel(x, y); + int lt = x, rt = x; + int lofs = ofs; + for( int i=lt; --i>=0; ) { + if( msk[--lofs] ) break; + img->draw_pixel(i, y); + msk[lofs] = 0xff; lt = i; + } + int rofs = ofs; + for( int i=rt; ++i< w; ) { + if( msk[++rofs] ) break; + img->draw_pixel(i, y); + msk[rofs] = 0xff; rt = i; + } + if( y+1 < h ) push(y+1, lt, rt); + if( y-1 >= 0 ) push(y-1, lt, rt); + } + } +} + +SketcherPoint *FillRegion::next() { + while( nxt < points.size() ) { + SketcherPoint *pt = points[nxt]; + if( pt->pty != PTY_FILL ) break; + start_at(pt->x, pt->y); + ++nxt; + } + return nxt < points.size() ? points[nxt++] : 0; +} + + +void SketcherCurve::draw(VFrame *img) +{ + if( !points.size() ) return; const float fmx = 16383; - VFrame *vpen = new_vpen(out); - out->set_pixel_color(color); - int n = points.size(); - if( !n ) return; - if( n > 2 ) { - int n2 = n - 2; - SketcherPoint *pt0 = points[0]; - SketcherPoint *pt1 = points[1]; - SketcherPoint *pt2 = points[2]; + SketcherVPen *vpen = new_vpen(img); + FillRegion fill(points, vpen); + SketcherPoint *pnt0 = fill.next(); + SketcherPoint *pnt1 = pnt0 ? fill.next() : 0; + SketcherPoint *pnt2 = pnt1 ? fill.next() : 0; + if( pnt0 && pnt1 && pnt2 ) { + SketcherPoint *pt0 = pnt0, *pt1 = pnt1, *pt2 = pnt2; float ax,ay, bx,by, cx,cy, dx,dy, sx,sy; bx = pt0->x; by = pt0->y; cx = pt1->x; cy = pt1->y; dx = pt2->x; dy = pt2->y; smooth_axy(ax,ay, bx,by, cx,cy, dx,dy); - for( int pi=0; pipty; - dx = points[pi+2]->x; dy = points[pi+2]->y; - switch( pty ) { + while( pt2 ) { + dx = pt2->x; dy = pt2->y; + switch( pt0->pty ) { case PTY_LINE: vpen->draw_line(bx, by, cx, cy); break; @@ -514,28 +660,41 @@ void SketcherCurve::draw(VFrame *out) vpen->draw_smooth(bx,by, sx,sy, cx,cy); break; } } - ax = bx; ay = by; - bx = cx; by = cy; - cx = dx; cy = dy; + ax = bx; ay = by; pt0 = pt1; + bx = cx; by = cy; pt1 = pt2; + cx = dx; cy = dy; pt2 = fill.next(); } - switch( points[n2]->pty ) { + switch( pt1->pty ) { case PTY_LINE: vpen->draw_line(bx, by, cx, cy); + if( fill.exists() ) { + dx = pnt0->x; dy = pnt0->y; + vpen->draw_line(cx,cy, dx,dy); + } break; case PTY_CURVE: { - smooth_dxy(dx,dy, ax,ay, bx,by, cx,cy); + if( fill.exists() ) { + dx = pnt0->x; dy = pnt0->y; + intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx); + vpen->draw_smooth(bx,by, sx,sy, cx,cy); + ax = bx; ay = by; + bx = cx; by = cy; + cx = dx; cy = dy; + dx = pnt1->x; dy = pnt1->y; + } + else + smooth_dxy(dx,dy, ax,ay, bx,by, cx,cy); intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx); vpen->draw_smooth(bx,by, sx,sy, cx,cy); break; } } + fill.run(); } - else if( n == 2 ) { - SketcherPoint *pt0 = points[0], *pt1 = points[1]; - vpen->draw_line(pt0->x, pt0->y, pt1->x, pt1->y); + else if( pnt0 && pnt1 ) { + vpen->draw_line(pnt0->x, pnt0->y, pnt1->x, pnt1->y); } - else if( n > 0 ) { - SketcherPoint *pt0 = points[0]; - vpen->draw_pixel(pt0->x, pt0->y); + else if( pnt0 ) { + vpen->draw_pixel(pnt0->x, pnt0->y); } delete vpen; } @@ -544,47 +703,90 @@ int Sketcher::process_realtime(VFrame *input, VFrame *output) { this->input = input; this->output = output; w = output->get_w(); h = output->get_h(); - if( input != output ) output->transfer_from(input); load_configuration(); + int out_color_model = output->get_color_model(); + int color_model = out_color_model; + switch( color_model ) { // add alpha if needed + case BC_RGB888: color_model = BC_RGBA8888; break; + case BC_YUV888: color_model = BC_YUVA8888; break; + case BC_RGB161616: color_model = BC_RGBA16161616; break; + case BC_YUV161616: color_model = BC_YUVA16161616; break; + case BC_RGB_FLOAT: color_model = BC_RGBA_FLOAT; break; + case BC_RGB_FLOATP: color_model = BC_RGBA_FLOATP; break; + } + if( color_model == out_color_model ) { + delete out; out = output; + if( output != input ) + output->transfer_from(input); + } + else { + VFrame::get_temp(out, w, h, color_model); + out->transfer_from(input); + } + VFrame::get_temp(img, w, h, color_model); + + if( !overlay_frame ) { + int cpus = server->preferences->project_smp; + int max = (w*h)/0x80000 + 2; + if( cpus > max ) cpus = max; + overlay_frame = new OverlayFrame(cpus); + } + for( int ci=0, n=config.curves.size(); cipoints.size(); if( !m || cv->pen == PTY_OFF ) continue; - cv->draw(output); + img->clear_frame(); + img->set_pixel_color(cv->color, (~cv->color>>24)&0xff); + cv->draw(img); + overlay_frame->overlay(out, img, 0,0,w,h, 0,0,w,h, + 1.f, TRANSFER_NORMAL, NEAREST_NEIGHBOR); } if( config.drag ) { for( int ci=0, n=config.curves.size(); cipoints.size(); picolor ; - draw_point(output, cv->points[pi], color); + draw_point(out, cv->points[pi], color); } } } + if( output != out ) + output->transfer_from(out); + else + out = 0; + return 0; } +void SketcherPoints::dump() +{ + for( int i=0; iid, pt_type[pt->pty], pt->x, pt->y); + } +} void SketcherCurves::dump() { for( int i=0; iid, cv_pen[cv->pen], cv->radius, - (cv->color>>16)&0xff, (cv->color>>8)&0xff, (cv->color>>0)&0xff); + printf("Curve %d, id=%d, pen=%s, r=%d, color=%02x%02x%02x%02x, %d points\n", + i, cv->id, cv_pen[cv->pen], cv->width, (~cv->color>>24)&0xff, + (cv->color>>16)&0xff, (cv->color>>8)&0xff, (cv->color>>0)&0xff, + cv->points.size()); cv->points.dump(); } } -void SketcherPoints::dump() +void SketcherConfig::dump() { - for( int i=0; iid, pt_type[pt->pty], pt->x, pt->y); - } + printf("Config drag=%d, cv_selected=%d, pt_selected=%d %d curves\n", + drag, cv_selected, pt_selected, curves.size()); + curves.dump(); }