agg_scanline_bin.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 scanline_bin - binary scanline.
  17. //
  18. //----------------------------------------------------------------------------
  19. //
  20. // Adaptation for 32-bit screen coordinates (scanline32_bin) has been sponsored by
  21. // Liberty Technology Systems, Inc., visit http://lib-sys.com
  22. //
  23. // Liberty Technology Systems, Inc. is the provider of
  24. // PostScript and PDF technology for software developers.
  25. //
  26. //----------------------------------------------------------------------------
  27. #ifndef AGG_SCANLINE_BIN_INCLUDED
  28. #define AGG_SCANLINE_BIN_INCLUDED
  29. #include "agg_array.h"
  30. namespace agg
  31. {
  32. //=============================================================scanline_bin
  33. //
  34. // This is binary scaline container which supports the interface
  35. // used in the rasterizer::render(). See description of agg_scanline_u8
  36. // for details.
  37. //
  38. //------------------------------------------------------------------------
  39. class scanline_bin
  40. {
  41. public:
  42. typedef int32 coord_type;
  43. struct span
  44. {
  45. int16 x;
  46. int16 len;
  47. };
  48. typedef const span* const_iterator;
  49. //--------------------------------------------------------------------
  50. scanline_bin() :
  51. m_last_x(0x7FFFFFF0),
  52. m_spans(),
  53. m_cur_span(0)
  54. {
  55. }
  56. //--------------------------------------------------------------------
  57. void reset(int min_x, int max_x)
  58. {
  59. unsigned max_len = max_x - min_x + 3;
  60. if(max_len > m_spans.size())
  61. {
  62. m_spans.resize(max_len);
  63. }
  64. m_last_x = 0x7FFFFFF0;
  65. m_cur_span = &m_spans[0];
  66. }
  67. //--------------------------------------------------------------------
  68. void add_cell(int x, unsigned)
  69. {
  70. if(x == m_last_x+1)
  71. {
  72. m_cur_span->len++;
  73. }
  74. else
  75. {
  76. ++m_cur_span;
  77. m_cur_span->x = (int16)x;
  78. m_cur_span->len = 1;
  79. }
  80. m_last_x = x;
  81. }
  82. //--------------------------------------------------------------------
  83. void add_span(int x, unsigned len, unsigned)
  84. {
  85. if(x == m_last_x+1)
  86. {
  87. m_cur_span->len = (int16)(m_cur_span->len + len);
  88. }
  89. else
  90. {
  91. ++m_cur_span;
  92. m_cur_span->x = (int16)x;
  93. m_cur_span->len = (int16)len;
  94. }
  95. m_last_x = x + len - 1;
  96. }
  97. //--------------------------------------------------------------------
  98. void add_cells(int x, unsigned len, const void*)
  99. {
  100. add_span(x, len, 0);
  101. }
  102. //--------------------------------------------------------------------
  103. void finalize(int y)
  104. {
  105. m_y = y;
  106. }
  107. //--------------------------------------------------------------------
  108. void reset_spans()
  109. {
  110. m_last_x = 0x7FFFFFF0;
  111. m_cur_span = &m_spans[0];
  112. }
  113. //--------------------------------------------------------------------
  114. int y() const { return m_y; }
  115. unsigned num_spans() const { return unsigned(m_cur_span - &m_spans[0]); }
  116. const_iterator begin() const { return &m_spans[1]; }
  117. private:
  118. scanline_bin(const scanline_bin&);
  119. const scanline_bin operator = (const scanline_bin&);
  120. int m_last_x;
  121. int m_y;
  122. pod_array<span> m_spans;
  123. span* m_cur_span;
  124. };
  125. //===========================================================scanline32_bin
  126. class scanline32_bin
  127. {
  128. public:
  129. typedef int32 coord_type;
  130. //--------------------------------------------------------------------
  131. struct span
  132. {
  133. span() {}
  134. span(coord_type x_, coord_type len_) : x(x_), len(len_) {}
  135. coord_type x;
  136. coord_type len;
  137. };
  138. typedef pod_bvector<span, 4> span_array_type;
  139. //--------------------------------------------------------------------
  140. class const_iterator
  141. {
  142. public:
  143. const_iterator(const span_array_type& spans) :
  144. m_spans(spans),
  145. m_span_idx(0)
  146. {}
  147. const span& operator*() const { return m_spans[m_span_idx]; }
  148. const span* operator->() const { return &m_spans[m_span_idx]; }
  149. void operator ++ () { ++m_span_idx; }
  150. private:
  151. const span_array_type& m_spans;
  152. unsigned m_span_idx;
  153. };
  154. //--------------------------------------------------------------------
  155. scanline32_bin() : m_max_len(0), m_last_x(0x7FFFFFF0) {}
  156. //--------------------------------------------------------------------
  157. void reset(int min_x, int max_x)
  158. {
  159. m_last_x = 0x7FFFFFF0;
  160. m_spans.remove_all();
  161. }
  162. //--------------------------------------------------------------------
  163. void add_cell(int x, unsigned)
  164. {
  165. if(x == m_last_x+1)
  166. {
  167. m_spans.last().len++;
  168. }
  169. else
  170. {
  171. m_spans.add(span(coord_type(x), 1));
  172. }
  173. m_last_x = x;
  174. }
  175. //--------------------------------------------------------------------
  176. void add_span(int x, unsigned len, unsigned)
  177. {
  178. if(x == m_last_x+1)
  179. {
  180. m_spans.last().len += coord_type(len);
  181. }
  182. else
  183. {
  184. m_spans.add(span(coord_type(x), coord_type(len)));
  185. }
  186. m_last_x = x + len - 1;
  187. }
  188. //--------------------------------------------------------------------
  189. void add_cells(int x, unsigned len, const void*)
  190. {
  191. add_span(x, len, 0);
  192. }
  193. //--------------------------------------------------------------------
  194. void finalize(int y)
  195. {
  196. m_y = y;
  197. }
  198. //--------------------------------------------------------------------
  199. void reset_spans()
  200. {
  201. m_last_x = 0x7FFFFFF0;
  202. m_spans.remove_all();
  203. }
  204. //--------------------------------------------------------------------
  205. int y() const { return m_y; }
  206. unsigned num_spans() const { return m_spans.size(); }
  207. const_iterator begin() const { return const_iterator(m_spans); }
  208. private:
  209. scanline32_bin(const scanline32_bin&);
  210. const scanline32_bin operator = (const scanline32_bin&);
  211. unsigned m_max_len;
  212. int m_last_x;
  213. int m_y;
  214. span_array_type m_spans;
  215. };
  216. }
  217. #endif