agg_rasterizer_compound_aa.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. //----------------------------------------------------------------------------
  2. // Anti-Grain Geometry - Version 2.3
  3. // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
  4. //
  5. // C# port by: Lars Brubaker
  6. // larsbrubaker@gmail.com
  7. // Copyright (C) 2007
  8. //
  9. // Permission to copy, use, modify, sell and distribute this software
  10. // is granted provided this copyright notice appears in all copies.
  11. // This software is provided "as is" without express or implied
  12. // warranty, and with no claim as to its suitability for any purpose.
  13. //
  14. //----------------------------------------------------------------------------
  15. //
  16. // The author gratefully acknowledges the support of David Turner,
  17. // Robert Wilhelm, and Werner Lemberg - the authors of the FreeType
  18. // library - in producing this work. See http://www.freetype.org for details.
  19. //
  20. //----------------------------------------------------------------------------
  21. // Contact: mcseem@antigrain.com
  22. // mcseemagg@yahoo.com
  23. // http://www.antigrain.com
  24. //----------------------------------------------------------------------------
  25. //
  26. // Adaptation for 32-bit screen coordinates has been sponsored by
  27. // Liberty Technology Systems, Inc., visit http://lib-sys.com
  28. //
  29. // Liberty Technology Systems, Inc. is the provider of
  30. // PostScript and PDF technology for software developers.
  31. //
  32. //----------------------------------------------------------------------------
  33. using MatterHackers.Agg.VertexSource;
  34. namespace MatterHackers.Agg
  35. {
  36. //===========================================================layer_order_e
  37. public enum LayerOrder
  38. {
  39. Unsorted, //------layer_unsorted
  40. Direct, //------layer_direct
  41. Inverse //------layer_inverse
  42. };
  43. //==================================================rasterizer_compound_aa
  44. //template<class Clip=rasterizer_sl_clip_int>
  45. sealed public class rasterizer_compound_aa : IRasterizer
  46. {
  47. private RasterizerCellsAa rasterizerCellsAa;
  48. private VectorClipper vectorClipper;
  49. private Util.filling_rule_e fillingRule;
  50. private LayerOrder layerOrder;
  51. private VectorPOD<style_info> activeStyles; // Active Styles
  52. private VectorPOD<int> activeStyleTable; // Active Style Table (unique values)
  53. private VectorPOD<byte> activeStyleMask; // Active Style Mask
  54. private VectorPOD<PixelCellAa> m_cells;
  55. private VectorPOD<byte> m_cover_buf;
  56. private VectorPOD<int> m_master_alpha;
  57. private int m_min_style;
  58. private int m_max_style;
  59. private int m_start_x;
  60. private int m_start_y;
  61. private int m_scan_y;
  62. private int m_sl_start;
  63. private int m_sl_len;
  64. private struct style_info
  65. {
  66. internal int start_cell;
  67. internal int num_cells;
  68. internal int last_x;
  69. };
  70. private const int aa_shift = 8;
  71. private const int aa_scale = 1 << aa_shift;
  72. private const int aa_mask = aa_scale - 1;
  73. private const int aa_scale2 = aa_scale * 2;
  74. private const int aa_mask2 = aa_scale2 - 1;
  75. private const int poly_subpixel_shift = (int)Util.poly_subpixel_scale_e.poly_subpixel_shift;
  76. public rasterizer_compound_aa()
  77. {
  78. rasterizerCellsAa = new RasterizerCellsAa();
  79. vectorClipper = new VectorClipper();
  80. fillingRule = Util.filling_rule_e.fill_non_zero;
  81. layerOrder = LayerOrder.Direct;
  82. activeStyles = new VectorPOD<style_info>(); // Active Styles
  83. activeStyleTable = new VectorPOD<int>(); // Active Style Table (unique values)
  84. activeStyleMask = new VectorPOD<byte>(); // Active Style Mask
  85. m_cells = new VectorPOD<PixelCellAa>();
  86. m_cover_buf = new VectorPOD<byte>();
  87. m_master_alpha = new VectorPOD<int>();
  88. m_min_style = (0x7FFFFFFF);
  89. m_max_style = (-0x7FFFFFFF);
  90. m_start_x = (0);
  91. m_start_y = (0);
  92. m_scan_y = (0x7FFFFFFF);
  93. m_sl_start = (0);
  94. m_sl_len = (0);
  95. }
  96. public void gamma(IGammaFunction gamma_function)
  97. {
  98. throw new System.NotImplementedException();
  99. }
  100. public void reset()
  101. {
  102. rasterizerCellsAa.reset();
  103. m_min_style = 0x7FFFFFFF;
  104. m_max_style = -0x7FFFFFFF;
  105. m_scan_y = 0x7FFFFFFF;
  106. m_sl_start = 0;
  107. m_sl_len = 0;
  108. }
  109. private void filling_rule(Util.filling_rule_e filling_rule)
  110. {
  111. fillingRule = filling_rule;
  112. }
  113. private void layer_order(LayerOrder order)
  114. {
  115. layerOrder = order;
  116. }
  117. private void clip_box(double x1, double y1,
  118. double x2, double y2)
  119. {
  120. reset();
  121. vectorClipper.clip_box(vectorClipper.upscale(x1), vectorClipper.upscale(y1),
  122. vectorClipper.upscale(x2), vectorClipper.upscale(y2));
  123. }
  124. private void reset_clipping()
  125. {
  126. reset();
  127. vectorClipper.reset_clipping();
  128. }
  129. public void styles(int left, int right)
  130. {
  131. PixelCellAa cell = new PixelCellAa();
  132. cell.Initial();
  133. cell.left = (int)left;
  134. cell.right = (int)right;
  135. rasterizerCellsAa.style(cell);
  136. if (left >= 0 && left < m_min_style) m_min_style = left;
  137. if (left >= 0 && left > m_max_style) m_max_style = left;
  138. if (right >= 0 && right < m_min_style) m_min_style = right;
  139. if (right >= 0 && right > m_max_style) m_max_style = right;
  140. }
  141. public void move_to(int x, int y)
  142. {
  143. if (rasterizerCellsAa.sorted()) reset();
  144. vectorClipper.move_to(m_start_x = vectorClipper.downscale(x),
  145. m_start_y = vectorClipper.downscale(y));
  146. }
  147. public void line_to(int x, int y)
  148. {
  149. vectorClipper.line_to(rasterizerCellsAa,
  150. vectorClipper.downscale(x),
  151. vectorClipper.downscale(y));
  152. }
  153. public void move_to_d(double x, double y)
  154. {
  155. if (rasterizerCellsAa.sorted()) reset();
  156. vectorClipper.move_to(m_start_x = vectorClipper.upscale(x),
  157. m_start_y = vectorClipper.upscale(y));
  158. }
  159. public void line_to_d(double x, double y)
  160. {
  161. vectorClipper.line_to(rasterizerCellsAa,
  162. vectorClipper.upscale(x),
  163. vectorClipper.upscale(y));
  164. }
  165. private void add_vertex(double x, double y, ShapePath.FlagsAndCommand cmd)
  166. {
  167. if (ShapePath.is_move_to(cmd))
  168. {
  169. move_to_d(x, y);
  170. }
  171. else
  172. if (ShapePath.is_vertex(cmd))
  173. {
  174. line_to_d(x, y);
  175. }
  176. else
  177. if (ShapePath.is_close(cmd))
  178. {
  179. vectorClipper.line_to(rasterizerCellsAa, m_start_x, m_start_y);
  180. }
  181. }
  182. private void edge(int x1, int y1, int x2, int y2)
  183. {
  184. if (rasterizerCellsAa.sorted()) reset();
  185. vectorClipper.move_to(vectorClipper.downscale(x1), vectorClipper.downscale(y1));
  186. vectorClipper.line_to(rasterizerCellsAa,
  187. vectorClipper.downscale(x2),
  188. vectorClipper.downscale(y2));
  189. }
  190. private void edge_d(double x1, double y1,
  191. double x2, double y2)
  192. {
  193. if (rasterizerCellsAa.sorted()) reset();
  194. vectorClipper.move_to(vectorClipper.upscale(x1), vectorClipper.upscale(y1));
  195. vectorClipper.line_to(rasterizerCellsAa,
  196. vectorClipper.upscale(x2),
  197. vectorClipper.upscale(y2));
  198. }
  199. private void sort()
  200. {
  201. rasterizerCellsAa.sort_cells();
  202. }
  203. public bool rewind_scanlines()
  204. {
  205. rasterizerCellsAa.sort_cells();
  206. if (rasterizerCellsAa.total_cells() == 0)
  207. {
  208. return false;
  209. }
  210. if (m_max_style < m_min_style)
  211. {
  212. return false;
  213. }
  214. m_scan_y = rasterizerCellsAa.min_y();
  215. activeStyles.Allocate((int)(m_max_style - m_min_style + 2), 128);
  216. allocate_master_alpha();
  217. return true;
  218. }
  219. // Returns the number of styles
  220. public int sweep_styles()
  221. {
  222. for (; ; )
  223. {
  224. if (m_scan_y > rasterizerCellsAa.max_y()) return 0;
  225. int num_cells = (int)rasterizerCellsAa.scanline_num_cells(m_scan_y);
  226. PixelCellAa[] cells;
  227. int cellOffset = 0;
  228. int curCellOffset;
  229. rasterizerCellsAa.scanline_cells(m_scan_y, out cells, out cellOffset);
  230. int num_styles = (int)(m_max_style - m_min_style + 2);
  231. int style_id;
  232. int styleOffset = 0;
  233. m_cells.Allocate((int)num_cells * 2, 256); // Each cell can have two styles
  234. activeStyleTable.Capacity(num_styles, 64);
  235. activeStyleMask.Allocate((num_styles + 7) >> 3, 8);
  236. activeStyleMask.zero();
  237. if (num_cells > 0)
  238. {
  239. // Pre-add zero (for no-fill style, that is, -1).
  240. // We need that to ensure that the "-1 style" would go first.
  241. activeStyleMask.Array[0] |= 1;
  242. activeStyleTable.add(0);
  243. activeStyles.Array[styleOffset].start_cell = 0;
  244. activeStyles.Array[styleOffset].num_cells = 0;
  245. activeStyles.Array[styleOffset].last_x = -0x7FFFFFFF;
  246. m_sl_start = cells[0].x;
  247. m_sl_len = (int)(cells[num_cells - 1].x - m_sl_start + 1);
  248. while (num_cells-- != 0)
  249. {
  250. curCellOffset = (int)cellOffset++;
  251. add_style(cells[curCellOffset].left);
  252. add_style(cells[curCellOffset].right);
  253. }
  254. // Convert the Y-histogram into the array of starting indexes
  255. int i;
  256. int start_cell = 0;
  257. style_info[] stylesArray = activeStyles.Array;
  258. for (i = 0; i < activeStyleTable.Count; i++)
  259. {
  260. int IndexToModify = (int)activeStyleTable[i];
  261. int v = stylesArray[IndexToModify].start_cell;
  262. stylesArray[IndexToModify].start_cell = start_cell;
  263. start_cell += v;
  264. }
  265. num_cells = (int)rasterizerCellsAa.scanline_num_cells(m_scan_y);
  266. rasterizerCellsAa.scanline_cells(m_scan_y, out cells, out cellOffset);
  267. while (num_cells-- > 0)
  268. {
  269. curCellOffset = (int)cellOffset++;
  270. style_id = (int)((cells[curCellOffset].left < 0) ? 0 :
  271. cells[curCellOffset].left - m_min_style + 1);
  272. styleOffset = (int)style_id;
  273. if (cells[curCellOffset].x == stylesArray[styleOffset].last_x)
  274. {
  275. cellOffset = stylesArray[styleOffset].start_cell + stylesArray[styleOffset].num_cells - 1;
  276. unchecked
  277. {
  278. cells[cellOffset].area += cells[curCellOffset].area;
  279. cells[cellOffset].cover += cells[curCellOffset].cover;
  280. }
  281. }
  282. else
  283. {
  284. cellOffset = stylesArray[styleOffset].start_cell + stylesArray[styleOffset].num_cells;
  285. cells[cellOffset].x = cells[curCellOffset].x;
  286. cells[cellOffset].area = cells[curCellOffset].area;
  287. cells[cellOffset].cover = cells[curCellOffset].cover;
  288. stylesArray[styleOffset].last_x = cells[curCellOffset].x;
  289. stylesArray[styleOffset].num_cells++;
  290. }
  291. style_id = (int)((cells[curCellOffset].right < 0) ? 0 :
  292. cells[curCellOffset].right - m_min_style + 1);
  293. styleOffset = (int)style_id;
  294. if (cells[curCellOffset].x == stylesArray[styleOffset].last_x)
  295. {
  296. cellOffset = stylesArray[styleOffset].start_cell + stylesArray[styleOffset].num_cells - 1;
  297. unchecked
  298. {
  299. cells[cellOffset].area -= cells[curCellOffset].area;
  300. cells[cellOffset].cover -= cells[curCellOffset].cover;
  301. }
  302. }
  303. else
  304. {
  305. cellOffset = stylesArray[styleOffset].start_cell + stylesArray[styleOffset].num_cells;
  306. cells[cellOffset].x = cells[curCellOffset].x;
  307. cells[cellOffset].area = -cells[curCellOffset].area;
  308. cells[cellOffset].cover = -cells[curCellOffset].cover;
  309. stylesArray[styleOffset].last_x = cells[curCellOffset].x;
  310. stylesArray[styleOffset].num_cells++;
  311. }
  312. }
  313. }
  314. if (activeStyleTable.Count > 1) break;
  315. ++m_scan_y;
  316. }
  317. ++m_scan_y;
  318. if (layerOrder != LayerOrder.Unsorted)
  319. {
  320. VectorPodRangeAdaptor ra = new VectorPodRangeAdaptor(activeStyleTable, 1, activeStyleTable.Count - 1);
  321. if (layerOrder == LayerOrder.Direct)
  322. {
  323. QuickSortRangeAdaptorUint m_QSorter = new QuickSortRangeAdaptorUint();
  324. m_QSorter.Sort(ra);
  325. //quick_sort(ra, uint_greater);
  326. }
  327. else
  328. {
  329. throw new System.NotImplementedException();
  330. //QuickSort_range_adaptor_uint m_QSorter = new QuickSort_range_adaptor_uint();
  331. //m_QSorter.Sort(ra);
  332. //quick_sort(ra, uint_less);
  333. }
  334. }
  335. return activeStyleTable.Count - 1;
  336. }
  337. // Returns style ID depending of the existing style index
  338. public int style(int style_idx)
  339. {
  340. return activeStyleTable[style_idx + 1] + (int)m_min_style - 1;
  341. }
  342. private bool navigate_scanline(int y)
  343. {
  344. rasterizerCellsAa.sort_cells();
  345. if (rasterizerCellsAa.total_cells() == 0)
  346. {
  347. return false;
  348. }
  349. if (m_max_style < m_min_style)
  350. {
  351. return false;
  352. }
  353. if (y < rasterizerCellsAa.min_y() || y > rasterizerCellsAa.max_y())
  354. {
  355. return false;
  356. }
  357. m_scan_y = y;
  358. activeStyles.Allocate((int)(m_max_style - m_min_style + 2), 128);
  359. allocate_master_alpha();
  360. return true;
  361. }
  362. private bool hit_test(int tx, int ty)
  363. {
  364. if (!navigate_scanline(ty))
  365. {
  366. return false;
  367. }
  368. int num_styles = sweep_styles();
  369. if (num_styles <= 0)
  370. {
  371. return false;
  372. }
  373. scanline_hit_test sl = new scanline_hit_test(tx);
  374. sweep_scanline(sl, -1);
  375. return sl.hit();
  376. }
  377. private byte[] allocate_cover_buffer(int len)
  378. {
  379. m_cover_buf.Allocate(len, 256);
  380. return m_cover_buf.Array;
  381. }
  382. private void master_alpha(int style, double alpha)
  383. {
  384. if (style >= 0)
  385. {
  386. while ((int)m_master_alpha.Count <= style)
  387. {
  388. m_master_alpha.add(aa_mask);
  389. }
  390. m_master_alpha.Array[style] = Util.uround(alpha * aa_mask);
  391. }
  392. }
  393. public void add_path(IVertexSource vs)
  394. {
  395. add_path(vs, 0);
  396. }
  397. public void add_path(IVertexSource vs, int path_id)
  398. {
  399. double x;
  400. double y;
  401. ShapePath.FlagsAndCommand cmd;
  402. vs.rewind(path_id);
  403. if (rasterizerCellsAa.sorted()) reset();
  404. while (!ShapePath.is_stop(cmd = vs.vertex(out x, out y)))
  405. {
  406. add_vertex(x, y, cmd);
  407. }
  408. }
  409. public int min_x()
  410. {
  411. return rasterizerCellsAa.min_x();
  412. }
  413. public int min_y()
  414. {
  415. return rasterizerCellsAa.min_y();
  416. }
  417. public int max_x()
  418. {
  419. return rasterizerCellsAa.max_x();
  420. }
  421. public int max_y()
  422. {
  423. return rasterizerCellsAa.max_y();
  424. }
  425. public int min_style()
  426. {
  427. return m_min_style;
  428. }
  429. public int max_style()
  430. {
  431. return m_max_style;
  432. }
  433. public int scanline_start()
  434. {
  435. return m_sl_start;
  436. }
  437. public int scanline_length()
  438. {
  439. return m_sl_len;
  440. }
  441. public int calculate_alpha(int area, int master_alpha)
  442. {
  443. int cover = area >> (poly_subpixel_shift * 2 + 1 - aa_shift);
  444. if (cover < 0) cover = -cover;
  445. if (fillingRule == Util.filling_rule_e.fill_even_odd)
  446. {
  447. cover &= aa_mask2;
  448. if (cover > aa_scale)
  449. {
  450. cover = aa_scale2 - cover;
  451. }
  452. }
  453. if (cover > aa_mask) cover = aa_mask;
  454. return (int)((cover * master_alpha + aa_mask) >> aa_shift);
  455. }
  456. public bool sweep_scanline(IScanlineCache sl)
  457. {
  458. throw new System.NotImplementedException();
  459. }
  460. // Sweeps one scanline with one style index. The style ID can be
  461. // determined by calling style().
  462. //template<class Scanline>
  463. public bool sweep_scanline(IScanlineCache sl, int style_idx)
  464. {
  465. int scan_y = m_scan_y - 1;
  466. if (scan_y > rasterizerCellsAa.max_y()) return false;
  467. sl.ResetSpans();
  468. int master_alpha = aa_mask;
  469. if (style_idx < 0)
  470. {
  471. style_idx = 0;
  472. }
  473. else
  474. {
  475. style_idx++;
  476. master_alpha = m_master_alpha[(int)(activeStyleTable[(int)style_idx] + m_min_style - 1)];
  477. }
  478. style_info st = activeStyles[activeStyleTable[style_idx]];
  479. int num_cells = (int)st.num_cells;
  480. int CellOffset = st.start_cell;
  481. PixelCellAa cell = m_cells[CellOffset];
  482. int cover = 0;
  483. while (num_cells-- != 0)
  484. {
  485. int alpha;
  486. int x = cell.x;
  487. int area = cell.area;
  488. cover += cell.cover;
  489. cell = m_cells[++CellOffset];
  490. if (area != 0)
  491. {
  492. alpha = calculate_alpha((cover << (poly_subpixel_shift + 1)) - area,
  493. master_alpha);
  494. sl.add_cell(x, alpha);
  495. x++;
  496. }
  497. if (num_cells != 0 && cell.x > x)
  498. {
  499. alpha = calculate_alpha(cover << (poly_subpixel_shift + 1),
  500. master_alpha);
  501. if (alpha != 0)
  502. {
  503. sl.add_span(x, cell.x - x, alpha);
  504. }
  505. }
  506. }
  507. if (sl.num_spans() == 0) return false;
  508. sl.finalize(scan_y);
  509. return true;
  510. }
  511. private void add_style(int style_id)
  512. {
  513. if (style_id < 0) style_id = 0;
  514. else style_id -= m_min_style - 1;
  515. int nbyte = (int)((int)style_id >> 3);
  516. int mask = (int)(1 << (style_id & 7));
  517. style_info[] stylesArray = activeStyles.Array;
  518. if ((activeStyleMask[nbyte] & mask) == 0)
  519. {
  520. activeStyleTable.add((int)style_id);
  521. activeStyleMask.Array[nbyte] |= (byte)mask;
  522. stylesArray[style_id].start_cell = 0;
  523. stylesArray[style_id].num_cells = 0;
  524. stylesArray[style_id].last_x = -0x7FFFFFFF;
  525. }
  526. ++stylesArray[style_id].start_cell;
  527. }
  528. private void allocate_master_alpha()
  529. {
  530. while ((int)m_master_alpha.Count <= m_max_style)
  531. {
  532. m_master_alpha.add(aa_mask);
  533. }
  534. }
  535. };
  536. }