123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731 |
- //----------------------------------------------------------------------------
- // Anti-Grain Geometry - Version 2.4
- // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
- //
- // Permission to copy, use, modify, sell and distribute this software
- // is granted provided this copyright notice appears in all copies.
- // This software is provided "as is" without express or implied
- // warranty, and with no claim as to its suitability for any purpose.
- //
- //----------------------------------------------------------------------------
- // Contact: mcseem@antigrain.com
- // mcseemagg@yahoo.com
- // http://www.antigrain.com
- //----------------------------------------------------------------------------
- //
- // class renderer_base
- //
- //----------------------------------------------------------------------------
- #ifndef AGG_RENDERER_BASE_INCLUDED
- #define AGG_RENDERER_BASE_INCLUDED
- #include "agg_basics.h"
- #include "agg_rendering_buffer.h"
- namespace agg
- {
- //-----------------------------------------------------------renderer_base
- template<class PixelFormat> class renderer_base
- {
- public:
- typedef PixelFormat pixfmt_type;
- typedef typename pixfmt_type::color_type color_type;
- typedef typename pixfmt_type::row_data row_data;
- //--------------------------------------------------------------------
- renderer_base() : m_ren(0), m_clip_box(1, 1, 0, 0) {}
- explicit renderer_base(pixfmt_type& ren) :
- m_ren(&ren),
- m_clip_box(0, 0, ren.width() - 1, ren.height() - 1)
- {}
- void attach(pixfmt_type& ren)
- {
- m_ren = &ren;
- m_clip_box = rect_i(0, 0, ren.width() - 1, ren.height() - 1);
- }
- //--------------------------------------------------------------------
- const pixfmt_type& ren() const { return *m_ren; }
- pixfmt_type& ren() { return *m_ren; }
-
- //--------------------------------------------------------------------
- unsigned width() const { return m_ren->width(); }
- unsigned height() const { return m_ren->height(); }
- //--------------------------------------------------------------------
- bool clip_box(int x1, int y1, int x2, int y2)
- {
- rect_i cb(x1, y1, x2, y2);
- cb.normalize();
- if(cb.clip(rect_i(0, 0, width() - 1, height() - 1)))
- {
- m_clip_box = cb;
- return true;
- }
- m_clip_box.x1 = 1;
- m_clip_box.y1 = 1;
- m_clip_box.x2 = 0;
- m_clip_box.y2 = 0;
- return false;
- }
- //--------------------------------------------------------------------
- void reset_clipping(bool visibility)
- {
- if(visibility)
- {
- m_clip_box.x1 = 0;
- m_clip_box.y1 = 0;
- m_clip_box.x2 = width() - 1;
- m_clip_box.y2 = height() - 1;
- }
- else
- {
- m_clip_box.x1 = 1;
- m_clip_box.y1 = 1;
- m_clip_box.x2 = 0;
- m_clip_box.y2 = 0;
- }
- }
- //--------------------------------------------------------------------
- void clip_box_naked(int x1, int y1, int x2, int y2)
- {
- m_clip_box.x1 = x1;
- m_clip_box.y1 = y1;
- m_clip_box.x2 = x2;
- m_clip_box.y2 = y2;
- }
- //--------------------------------------------------------------------
- bool inbox(int x, int y) const
- {
- return x >= m_clip_box.x1 && y >= m_clip_box.y1 &&
- x <= m_clip_box.x2 && y <= m_clip_box.y2;
- }
- //--------------------------------------------------------------------
- const rect_i& clip_box() const { return m_clip_box; }
- int xmin() const { return m_clip_box.x1; }
- int ymin() const { return m_clip_box.y1; }
- int xmax() const { return m_clip_box.x2; }
- int ymax() const { return m_clip_box.y2; }
- //--------------------------------------------------------------------
- const rect_i& bounding_clip_box() const { return m_clip_box; }
- int bounding_xmin() const { return m_clip_box.x1; }
- int bounding_ymin() const { return m_clip_box.y1; }
- int bounding_xmax() const { return m_clip_box.x2; }
- int bounding_ymax() const { return m_clip_box.y2; }
- //--------------------------------------------------------------------
- void clear(const color_type& c)
- {
- unsigned y;
- if(width())
- {
- for(y = 0; y < height(); y++)
- {
- m_ren->copy_hline(0, y, width(), c);
- }
- }
- }
-
- //--------------------------------------------------------------------
- void fill(const color_type& c)
- {
- unsigned y;
- if(width())
- {
- for(y = 0; y < height(); y++)
- {
- m_ren->blend_hline(0, y, width(), c, cover_mask);
- }
- }
- }
-
- //--------------------------------------------------------------------
- void copy_pixel(int x, int y, const color_type& c)
- {
- if(inbox(x, y))
- {
- m_ren->copy_pixel(x, y, c);
- }
- }
- //--------------------------------------------------------------------
- void blend_pixel(int x, int y, const color_type& c, cover_type cover)
- {
- if(inbox(x, y))
- {
- m_ren->blend_pixel(x, y, c, cover);
- }
- }
- //--------------------------------------------------------------------
- color_type pixel(int x, int y) const
- {
- return inbox(x, y) ?
- m_ren->pixel(x, y) :
- color_type::no_color();
- }
- //--------------------------------------------------------------------
- void copy_hline(int x1, int y, int x2, const color_type& c)
- {
- if(x1 > x2) { int t = x2; x2 = x1; x1 = t; }
- if(y > ymax()) return;
- if(y < ymin()) return;
- if(x1 > xmax()) return;
- if(x2 < xmin()) return;
- if(x1 < xmin()) x1 = xmin();
- if(x2 > xmax()) x2 = xmax();
- m_ren->copy_hline(x1, y, x2 - x1 + 1, c);
- }
- //--------------------------------------------------------------------
- void copy_vline(int x, int y1, int y2, const color_type& c)
- {
- if(y1 > y2) { int t = y2; y2 = y1; y1 = t; }
- if(x > xmax()) return;
- if(x < xmin()) return;
- if(y1 > ymax()) return;
- if(y2 < ymin()) return;
- if(y1 < ymin()) y1 = ymin();
- if(y2 > ymax()) y2 = ymax();
- m_ren->copy_vline(x, y1, y2 - y1 + 1, c);
- }
- //--------------------------------------------------------------------
- void blend_hline(int x1, int y, int x2,
- const color_type& c, cover_type cover)
- {
- if(x1 > x2) { int t = x2; x2 = x1; x1 = t; }
- if(y > ymax()) return;
- if(y < ymin()) return;
- if(x1 > xmax()) return;
- if(x2 < xmin()) return;
- if(x1 < xmin()) x1 = xmin();
- if(x2 > xmax()) x2 = xmax();
- m_ren->blend_hline(x1, y, x2 - x1 + 1, c, cover);
- }
- //--------------------------------------------------------------------
- void blend_vline(int x, int y1, int y2,
- const color_type& c, cover_type cover)
- {
- if(y1 > y2) { int t = y2; y2 = y1; y1 = t; }
- if(x > xmax()) return;
- if(x < xmin()) return;
- if(y1 > ymax()) return;
- if(y2 < ymin()) return;
- if(y1 < ymin()) y1 = ymin();
- if(y2 > ymax()) y2 = ymax();
- m_ren->blend_vline(x, y1, y2 - y1 + 1, c, cover);
- }
- //--------------------------------------------------------------------
- void copy_bar(int x1, int y1, int x2, int y2, const color_type& c)
- {
- rect_i rc(x1, y1, x2, y2);
- rc.normalize();
- if(rc.clip(clip_box()))
- {
- int y;
- for(y = rc.y1; y <= rc.y2; y++)
- {
- m_ren->copy_hline(rc.x1, y, unsigned(rc.x2 - rc.x1 + 1), c);
- }
- }
- }
- //--------------------------------------------------------------------
- void blend_bar(int x1, int y1, int x2, int y2,
- const color_type& c, cover_type cover)
- {
- rect_i rc(x1, y1, x2, y2);
- rc.normalize();
- if(rc.clip(clip_box()))
- {
- int y;
- for(y = rc.y1; y <= rc.y2; y++)
- {
- m_ren->blend_hline(rc.x1,
- y,
- unsigned(rc.x2 - rc.x1 + 1),
- c,
- cover);
- }
- }
- }
- //--------------------------------------------------------------------
- void blend_solid_hspan(int x, int y, int len,
- const color_type& c,
- const cover_type* covers)
- {
- if(y > ymax()) return;
- if(y < ymin()) return;
- if(x < xmin())
- {
- len -= xmin() - x;
- if(len <= 0) return;
- covers += xmin() - x;
- x = xmin();
- }
- if(x + len > xmax())
- {
- len = xmax() - x + 1;
- if(len <= 0) return;
- }
- m_ren->blend_solid_hspan(x, y, len, c, covers);
- }
- //--------------------------------------------------------------------
- void blend_solid_vspan(int x, int y, int len,
- const color_type& c,
- const cover_type* covers)
- {
- if(x > xmax()) return;
- if(x < xmin()) return;
- if(y < ymin())
- {
- len -= ymin() - y;
- if(len <= 0) return;
- covers += ymin() - y;
- y = ymin();
- }
- if(y + len > ymax())
- {
- len = ymax() - y + 1;
- if(len <= 0) return;
- }
- m_ren->blend_solid_vspan(x, y, len, c, covers);
- }
- //--------------------------------------------------------------------
- void copy_color_hspan(int x, int y, int len, const color_type* colors)
- {
- if(y > ymax()) return;
- if(y < ymin()) return;
- if(x < xmin())
- {
- int d = xmin() - x;
- len -= d;
- if(len <= 0) return;
- colors += d;
- x = xmin();
- }
- if(x + len > xmax())
- {
- len = xmax() - x + 1;
- if(len <= 0) return;
- }
- m_ren->copy_color_hspan(x, y, len, colors);
- }
- //--------------------------------------------------------------------
- void copy_color_vspan(int x, int y, int len, const color_type* colors)
- {
- if(x > xmax()) return;
- if(x < xmin()) return;
- if(y < ymin())
- {
- int d = ymin() - y;
- len -= d;
- if(len <= 0) return;
- colors += d;
- y = ymin();
- }
- if(y + len > ymax())
- {
- len = ymax() - y + 1;
- if(len <= 0) return;
- }
- m_ren->copy_color_vspan(x, y, len, colors);
- }
- //--------------------------------------------------------------------
- void blend_color_hspan(int x, int y, int len,
- const color_type* colors,
- const cover_type* covers,
- cover_type cover = agg::cover_full)
- {
- if(y > ymax()) return;
- if(y < ymin()) return;
- if(x < xmin())
- {
- int d = xmin() - x;
- len -= d;
- if(len <= 0) return;
- if(covers) covers += d;
- colors += d;
- x = xmin();
- }
- if(x + len > xmax())
- {
- len = xmax() - x + 1;
- if(len <= 0) return;
- }
- m_ren->blend_color_hspan(x, y, len, colors, covers, cover);
- }
- //--------------------------------------------------------------------
- void blend_color_vspan(int x, int y, int len,
- const color_type* colors,
- const cover_type* covers,
- cover_type cover = agg::cover_full)
- {
- if(x > xmax()) return;
- if(x < xmin()) return;
- if(y < ymin())
- {
- int d = ymin() - y;
- len -= d;
- if(len <= 0) return;
- if(covers) covers += d;
- colors += d;
- y = ymin();
- }
- if(y + len > ymax())
- {
- len = ymax() - y + 1;
- if(len <= 0) return;
- }
- m_ren->blend_color_vspan(x, y, len, colors, covers, cover);
- }
- //--------------------------------------------------------------------
- rect_i clip_rect_area(rect_i& dst, rect_i& src, int wsrc, int hsrc) const
- {
- rect_i rc(0,0,0,0);
- rect_i cb = clip_box();
- ++cb.x2;
- ++cb.y2;
- if(src.x1 < 0)
- {
- dst.x1 -= src.x1;
- src.x1 = 0;
- }
- if(src.y1 < 0)
- {
- dst.y1 -= src.y1;
- src.y1 = 0;
- }
- if(src.x2 > wsrc) src.x2 = wsrc;
- if(src.y2 > hsrc) src.y2 = hsrc;
- if(dst.x1 < cb.x1)
- {
- src.x1 += cb.x1 - dst.x1;
- dst.x1 = cb.x1;
- }
- if(dst.y1 < cb.y1)
- {
- src.y1 += cb.y1 - dst.y1;
- dst.y1 = cb.y1;
- }
- if(dst.x2 > cb.x2) dst.x2 = cb.x2;
- if(dst.y2 > cb.y2) dst.y2 = cb.y2;
- rc.x2 = dst.x2 - dst.x1;
- rc.y2 = dst.y2 - dst.y1;
- if(rc.x2 > src.x2 - src.x1) rc.x2 = src.x2 - src.x1;
- if(rc.y2 > src.y2 - src.y1) rc.y2 = src.y2 - src.y1;
- return rc;
- }
- //--------------------------------------------------------------------
- template<class RenBuf>
- void copy_from(const RenBuf& src,
- const rect_i* rect_src_ptr = 0,
- int dx = 0,
- int dy = 0)
- {
- rect_i rsrc(0, 0, src.width(), src.height());
- if(rect_src_ptr)
- {
- rsrc.x1 = rect_src_ptr->x1;
- rsrc.y1 = rect_src_ptr->y1;
- rsrc.x2 = rect_src_ptr->x2 + 1;
- rsrc.y2 = rect_src_ptr->y2 + 1;
- }
- // Version with xdst, ydst (absolute positioning)
- //rect_i rdst(xdst, ydst, xdst + rsrc.x2 - rsrc.x1, ydst + rsrc.y2 - rsrc.y1);
- // Version with dx, dy (relative positioning)
- rect_i rdst(rsrc.x1 + dx, rsrc.y1 + dy, rsrc.x2 + dx, rsrc.y2 + dy);
- rect_i rc = clip_rect_area(rdst, rsrc, src.width(), src.height());
- if(rc.x2 > 0)
- {
- int incy = 1;
- if(rdst.y1 > rsrc.y1)
- {
- rsrc.y1 += rc.y2 - 1;
- rdst.y1 += rc.y2 - 1;
- incy = -1;
- }
- while(rc.y2 > 0)
- {
- m_ren->copy_from(src,
- rdst.x1, rdst.y1,
- rsrc.x1, rsrc.y1,
- rc.x2);
- rdst.y1 += incy;
- rsrc.y1 += incy;
- --rc.y2;
- }
- }
- }
- //--------------------------------------------------------------------
- template<class SrcPixelFormatRenderer>
- void blend_from(const SrcPixelFormatRenderer& src,
- const rect_i* rect_src_ptr = 0,
- int dx = 0,
- int dy = 0,
- cover_type cover = agg::cover_full)
- {
- rect_i rsrc(0, 0, src.width(), src.height());
- if(rect_src_ptr)
- {
- rsrc.x1 = rect_src_ptr->x1;
- rsrc.y1 = rect_src_ptr->y1;
- rsrc.x2 = rect_src_ptr->x2 + 1;
- rsrc.y2 = rect_src_ptr->y2 + 1;
- }
- // Version with xdst, ydst (absolute positioning)
- //rect_i rdst(xdst, ydst, xdst + rsrc.x2 - rsrc.x1, ydst + rsrc.y2 - rsrc.y1);
- // Version with dx, dy (relative positioning)
- rect_i rdst(rsrc.x1 + dx, rsrc.y1 + dy, rsrc.x2 + dx, rsrc.y2 + dy);
- rect_i rc = clip_rect_area(rdst, rsrc, src.width(), src.height());
- if(rc.x2 > 0)
- {
- int incy = 1;
- if(rdst.y1 > rsrc.y1)
- {
- rsrc.y1 += rc.y2 - 1;
- rdst.y1 += rc.y2 - 1;
- incy = -1;
- }
- while(rc.y2 > 0)
- {
- typename SrcPixelFormatRenderer::row_data rw = src.row(rsrc.y1);
- if(rw.ptr)
- {
- int x1src = rsrc.x1;
- int x1dst = rdst.x1;
- int len = rc.x2;
- if(rw.x1 > x1src)
- {
- x1dst += rw.x1 - x1src;
- len -= rw.x1 - x1src;
- x1src = rw.x1;
- }
- if(len > 0)
- {
- if(x1src + len-1 > rw.x2)
- {
- len -= x1src + len - rw.x2 - 1;
- }
- if(len > 0)
- {
- m_ren->blend_from(src,
- x1dst, rdst.y1,
- x1src, rsrc.y1,
- len,
- cover);
- }
- }
- }
- rdst.y1 += incy;
- rsrc.y1 += incy;
- --rc.y2;
- }
- }
- }
- //--------------------------------------------------------------------
- template<class SrcPixelFormatRenderer>
- void blend_from_color(const SrcPixelFormatRenderer& src,
- const color_type& color,
- const rect_i* rect_src_ptr = 0,
- int dx = 0,
- int dy = 0,
- cover_type cover = agg::cover_full)
- {
- rect_i rsrc(0, 0, src.width(), src.height());
- if(rect_src_ptr)
- {
- rsrc.x1 = rect_src_ptr->x1;
- rsrc.y1 = rect_src_ptr->y1;
- rsrc.x2 = rect_src_ptr->x2 + 1;
- rsrc.y2 = rect_src_ptr->y2 + 1;
- }
- // Version with xdst, ydst (absolute positioning)
- //rect_i rdst(xdst, ydst, xdst + rsrc.x2 - rsrc.x1, ydst + rsrc.y2 - rsrc.y1);
- // Version with dx, dy (relative positioning)
- rect_i rdst(rsrc.x1 + dx, rsrc.y1 + dy, rsrc.x2 + dx, rsrc.y2 + dy);
- rect_i rc = clip_rect_area(rdst, rsrc, src.width(), src.height());
- if(rc.x2 > 0)
- {
- int incy = 1;
- if(rdst.y1 > rsrc.y1)
- {
- rsrc.y1 += rc.y2 - 1;
- rdst.y1 += rc.y2 - 1;
- incy = -1;
- }
- while(rc.y2 > 0)
- {
- typename SrcPixelFormatRenderer::row_data rw = src.row(rsrc.y1);
- if(rw.ptr)
- {
- int x1src = rsrc.x1;
- int x1dst = rdst.x1;
- int len = rc.x2;
- if(rw.x1 > x1src)
- {
- x1dst += rw.x1 - x1src;
- len -= rw.x1 - x1src;
- x1src = rw.x1;
- }
- if(len > 0)
- {
- if(x1src + len-1 > rw.x2)
- {
- len -= x1src + len - rw.x2 - 1;
- }
- if(len > 0)
- {
- m_ren->blend_from_color(src,
- color,
- x1dst, rdst.y1,
- x1src, rsrc.y1,
- len,
- cover);
- }
- }
- }
- rdst.y1 += incy;
- rsrc.y1 += incy;
- --rc.y2;
- }
- }
- }
- //--------------------------------------------------------------------
- template<class SrcPixelFormatRenderer>
- void blend_from_lut(const SrcPixelFormatRenderer& src,
- const color_type* color_lut,
- const rect_i* rect_src_ptr = 0,
- int dx = 0,
- int dy = 0,
- cover_type cover = agg::cover_full)
- {
- rect_i rsrc(0, 0, src.width(), src.height());
- if(rect_src_ptr)
- {
- rsrc.x1 = rect_src_ptr->x1;
- rsrc.y1 = rect_src_ptr->y1;
- rsrc.x2 = rect_src_ptr->x2 + 1;
- rsrc.y2 = rect_src_ptr->y2 + 1;
- }
- // Version with xdst, ydst (absolute positioning)
- //rect_i rdst(xdst, ydst, xdst + rsrc.x2 - rsrc.x1, ydst + rsrc.y2 - rsrc.y1);
- // Version with dx, dy (relative positioning)
- rect_i rdst(rsrc.x1 + dx, rsrc.y1 + dy, rsrc.x2 + dx, rsrc.y2 + dy);
- rect_i rc = clip_rect_area(rdst, rsrc, src.width(), src.height());
- if(rc.x2 > 0)
- {
- int incy = 1;
- if(rdst.y1 > rsrc.y1)
- {
- rsrc.y1 += rc.y2 - 1;
- rdst.y1 += rc.y2 - 1;
- incy = -1;
- }
- while(rc.y2 > 0)
- {
- typename SrcPixelFormatRenderer::row_data rw = src.row(rsrc.y1);
- if(rw.ptr)
- {
- int x1src = rsrc.x1;
- int x1dst = rdst.x1;
- int len = rc.x2;
- if(rw.x1 > x1src)
- {
- x1dst += rw.x1 - x1src;
- len -= rw.x1 - x1src;
- x1src = rw.x1;
- }
- if(len > 0)
- {
- if(x1src + len-1 > rw.x2)
- {
- len -= x1src + len - rw.x2 - 1;
- }
- if(len > 0)
- {
- m_ren->blend_from_lut(src,
- color_lut,
- x1dst, rdst.y1,
- x1src, rsrc.y1,
- len,
- cover);
- }
- }
- }
- rdst.y1 += incy;
- rsrc.y1 += incy;
- --rc.y2;
- }
- }
- }
- private:
- pixfmt_type* m_ren;
- rect_i m_clip_box;
- };
- }
- #endif
|