+
+class FillRegion
+{
+ class segment { public: int y, lt, rt; };
+ ArrayList<segment> 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)