agg_renderer_mclip.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //----------------------------------------------------------------------------
  2. // Anti-Grain Geometry - Version 2.4
  3. // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
  4. //
  5. // Permission to copy, use, modify, sell and distribute this software
  6. // is granted provided this copyright notice appears in all copies.
  7. // This software is provided "as is" without express or implied
  8. // warranty, and with no claim as to its suitability for any purpose.
  9. //
  10. //----------------------------------------------------------------------------
  11. // Contact: mcseem@antigrain.com
  12. // mcseemagg@yahoo.com
  13. // http://www.antigrain.com
  14. //----------------------------------------------------------------------------
  15. //
  16. // class renderer_mclip
  17. //
  18. //----------------------------------------------------------------------------
  19. #ifndef AGG_RENDERER_MCLIP_INCLUDED
  20. #define AGG_RENDERER_MCLIP_INCLUDED
  21. #include "agg_basics.h"
  22. #include "agg_array.h"
  23. #include "agg_renderer_base.h"
  24. namespace agg
  25. {
  26. //----------------------------------------------------------renderer_mclip
  27. template<class PixelFormat> class renderer_mclip
  28. {
  29. public:
  30. typedef PixelFormat pixfmt_type;
  31. typedef typename pixfmt_type::color_type color_type;
  32. typedef typename pixfmt_type::row_data row_data;
  33. typedef renderer_base<pixfmt_type> base_ren_type;
  34. //--------------------------------------------------------------------
  35. explicit renderer_mclip(pixfmt_type& pixf) :
  36. m_ren(pixf),
  37. m_curr_cb(0),
  38. m_bounds(m_ren.xmin(), m_ren.ymin(), m_ren.xmax(), m_ren.ymax())
  39. {}
  40. void attach(pixfmt_type& pixf)
  41. {
  42. m_ren.attach(pixf);
  43. reset_clipping(true);
  44. }
  45. //--------------------------------------------------------------------
  46. const pixfmt_type& ren() const { return m_ren.ren(); }
  47. pixfmt_type& ren() { return m_ren.ren(); }
  48. //--------------------------------------------------------------------
  49. unsigned width() const { return m_ren.width(); }
  50. unsigned height() const { return m_ren.height(); }
  51. //--------------------------------------------------------------------
  52. const rect_i& clip_box() const { return m_ren.clip_box(); }
  53. int xmin() const { return m_ren.xmin(); }
  54. int ymin() const { return m_ren.ymin(); }
  55. int xmax() const { return m_ren.xmax(); }
  56. int ymax() const { return m_ren.ymax(); }
  57. //--------------------------------------------------------------------
  58. const rect_i& bounding_clip_box() const { return m_bounds; }
  59. int bounding_xmin() const { return m_bounds.x1; }
  60. int bounding_ymin() const { return m_bounds.y1; }
  61. int bounding_xmax() const { return m_bounds.x2; }
  62. int bounding_ymax() const { return m_bounds.y2; }
  63. //--------------------------------------------------------------------
  64. void first_clip_box()
  65. {
  66. m_curr_cb = 0;
  67. if(m_clip.size())
  68. {
  69. const rect_i& cb = m_clip[0];
  70. m_ren.clip_box_naked(cb.x1, cb.y1, cb.x2, cb.y2);
  71. }
  72. }
  73. //--------------------------------------------------------------------
  74. bool next_clip_box()
  75. {
  76. if(++m_curr_cb < m_clip.size())
  77. {
  78. const rect_i& cb = m_clip[m_curr_cb];
  79. m_ren.clip_box_naked(cb.x1, cb.y1, cb.x2, cb.y2);
  80. return true;
  81. }
  82. return false;
  83. }
  84. //--------------------------------------------------------------------
  85. void reset_clipping(bool visibility)
  86. {
  87. m_ren.reset_clipping(visibility);
  88. m_clip.remove_all();
  89. m_curr_cb = 0;
  90. m_bounds = m_ren.clip_box();
  91. }
  92. //--------------------------------------------------------------------
  93. void add_clip_box(int x1, int y1, int x2, int y2)
  94. {
  95. rect_i cb(x1, y1, x2, y2);
  96. cb.normalize();
  97. if(cb.clip(rect_i(0, 0, width() - 1, height() - 1)))
  98. {
  99. m_clip.add(cb);
  100. if(cb.x1 < m_bounds.x1) m_bounds.x1 = cb.x1;
  101. if(cb.y1 < m_bounds.y1) m_bounds.y1 = cb.y1;
  102. if(cb.x2 > m_bounds.x2) m_bounds.x2 = cb.x2;
  103. if(cb.y2 > m_bounds.y2) m_bounds.y2 = cb.y2;
  104. }
  105. }
  106. //--------------------------------------------------------------------
  107. void clear(const color_type& c)
  108. {
  109. m_ren.clear(c);
  110. }
  111. //--------------------------------------------------------------------
  112. void copy_pixel(int x, int y, const color_type& c)
  113. {
  114. first_clip_box();
  115. do
  116. {
  117. if(m_ren.inbox(x, y))
  118. {
  119. m_ren.ren().copy_pixel(x, y, c);
  120. break;
  121. }
  122. }
  123. while(next_clip_box());
  124. }
  125. //--------------------------------------------------------------------
  126. void blend_pixel(int x, int y, const color_type& c, cover_type cover)
  127. {
  128. first_clip_box();
  129. do
  130. {
  131. if(m_ren.inbox(x, y))
  132. {
  133. m_ren.ren().blend_pixel(x, y, c, cover);
  134. break;
  135. }
  136. }
  137. while(next_clip_box());
  138. }
  139. //--------------------------------------------------------------------
  140. color_type pixel(int x, int y) const
  141. {
  142. first_clip_box();
  143. do
  144. {
  145. if(m_ren.inbox(x, y))
  146. {
  147. return m_ren.ren().pixel(x, y);
  148. }
  149. }
  150. while(next_clip_box());
  151. return color_type::no_color();
  152. }
  153. //--------------------------------------------------------------------
  154. void copy_hline(int x1, int y, int x2, const color_type& c)
  155. {
  156. first_clip_box();
  157. do
  158. {
  159. m_ren.copy_hline(x1, y, x2, c);
  160. }
  161. while(next_clip_box());
  162. }
  163. //--------------------------------------------------------------------
  164. void copy_vline(int x, int y1, int y2, const color_type& c)
  165. {
  166. first_clip_box();
  167. do
  168. {
  169. m_ren.copy_vline(x, y1, y2, c);
  170. }
  171. while(next_clip_box());
  172. }
  173. //--------------------------------------------------------------------
  174. void blend_hline(int x1, int y, int x2,
  175. const color_type& c, cover_type cover)
  176. {
  177. first_clip_box();
  178. do
  179. {
  180. m_ren.blend_hline(x1, y, x2, c, cover);
  181. }
  182. while(next_clip_box());
  183. }
  184. //--------------------------------------------------------------------
  185. void blend_vline(int x, int y1, int y2,
  186. const color_type& c, cover_type cover)
  187. {
  188. first_clip_box();
  189. do
  190. {
  191. m_ren.blend_vline(x, y1, y2, c, cover);
  192. }
  193. while(next_clip_box());
  194. }
  195. //--------------------------------------------------------------------
  196. void copy_bar(int x1, int y1, int x2, int y2, const color_type& c)
  197. {
  198. first_clip_box();
  199. do
  200. {
  201. m_ren.copy_bar(x1, y1, x2, y2, c);
  202. }
  203. while(next_clip_box());
  204. }
  205. //--------------------------------------------------------------------
  206. void blend_bar(int x1, int y1, int x2, int y2,
  207. const color_type& c, cover_type cover)
  208. {
  209. first_clip_box();
  210. do
  211. {
  212. m_ren.blend_bar(x1, y1, x2, y2, c, cover);
  213. }
  214. while(next_clip_box());
  215. }
  216. //--------------------------------------------------------------------
  217. void blend_solid_hspan(int x, int y, int len,
  218. const color_type& c, const cover_type* covers)
  219. {
  220. first_clip_box();
  221. do
  222. {
  223. m_ren.blend_solid_hspan(x, y, len, c, covers);
  224. }
  225. while(next_clip_box());
  226. }
  227. //--------------------------------------------------------------------
  228. void blend_solid_vspan(int x, int y, int len,
  229. const color_type& c, const cover_type* covers)
  230. {
  231. first_clip_box();
  232. do
  233. {
  234. m_ren.blend_solid_vspan(x, y, len, c, covers);
  235. }
  236. while(next_clip_box());
  237. }
  238. //--------------------------------------------------------------------
  239. void copy_color_hspan(int x, int y, int len, const color_type* colors)
  240. {
  241. first_clip_box();
  242. do
  243. {
  244. m_ren.copy_color_hspan(x, y, len, colors);
  245. }
  246. while(next_clip_box());
  247. }
  248. //--------------------------------------------------------------------
  249. void blend_color_hspan(int x, int y, int len,
  250. const color_type* colors,
  251. const cover_type* covers,
  252. cover_type cover = cover_full)
  253. {
  254. first_clip_box();
  255. do
  256. {
  257. m_ren.blend_color_hspan(x, y, len, colors, covers, cover);
  258. }
  259. while(next_clip_box());
  260. }
  261. //--------------------------------------------------------------------
  262. void blend_color_vspan(int x, int y, int len,
  263. const color_type* colors,
  264. const cover_type* covers,
  265. cover_type cover = cover_full)
  266. {
  267. first_clip_box();
  268. do
  269. {
  270. m_ren.blend_color_vspan(x, y, len, colors, covers, cover);
  271. }
  272. while(next_clip_box());
  273. }
  274. //--------------------------------------------------------------------
  275. void copy_from(const rendering_buffer& from,
  276. const rect_i* rc=0,
  277. int x_to=0,
  278. int y_to=0)
  279. {
  280. first_clip_box();
  281. do
  282. {
  283. m_ren.copy_from(from, rc, x_to, y_to);
  284. }
  285. while(next_clip_box());
  286. }
  287. //--------------------------------------------------------------------
  288. template<class SrcPixelFormatRenderer>
  289. void blend_from(const SrcPixelFormatRenderer& src,
  290. const rect_i* rect_src_ptr = 0,
  291. int dx = 0,
  292. int dy = 0,
  293. cover_type cover = cover_full)
  294. {
  295. first_clip_box();
  296. do
  297. {
  298. m_ren.blend_from(src, rect_src_ptr, dx, dy, cover);
  299. }
  300. while(next_clip_box());
  301. }
  302. private:
  303. renderer_mclip(const renderer_mclip<PixelFormat>&);
  304. const renderer_mclip<PixelFormat>&
  305. operator = (const renderer_mclip<PixelFormat>&);
  306. base_ren_type m_ren;
  307. pod_bvector<rect_i, 4> m_clip;
  308. unsigned m_curr_cb;
  309. rect_i m_bounds;
  310. };
  311. }
  312. #endif