Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_EdgeTable.cpp 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. const int juce_edgeTableDefaultEdgesPerLine = 32;
  18. //==============================================================================
  19. EdgeTable::EdgeTable (const Rectangle<int>& area,
  20. const Path& path, const AffineTransform& transform)
  21. : bounds (area),
  22. maxEdgesPerLine (juce_edgeTableDefaultEdgesPerLine),
  23. lineStrideElements (juce_edgeTableDefaultEdgesPerLine * 2 + 1),
  24. needToCheckEmptiness (true)
  25. {
  26. allocate();
  27. int* t = table;
  28. for (int i = bounds.getHeight(); --i >= 0;)
  29. {
  30. *t = 0;
  31. t += lineStrideElements;
  32. }
  33. const int leftLimit = bounds.getX() << 8;
  34. const int topLimit = bounds.getY() << 8;
  35. const int rightLimit = bounds.getRight() << 8;
  36. const int heightLimit = bounds.getHeight() << 8;
  37. PathFlatteningIterator iter (path, transform);
  38. while (iter.next())
  39. {
  40. int y1 = roundToInt (iter.y1 * 256.0f);
  41. int y2 = roundToInt (iter.y2 * 256.0f);
  42. if (y1 != y2)
  43. {
  44. y1 -= topLimit;
  45. y2 -= topLimit;
  46. const int startY = y1;
  47. int direction = -1;
  48. if (y1 > y2)
  49. {
  50. std::swap (y1, y2);
  51. direction = 1;
  52. }
  53. if (y1 < 0)
  54. y1 = 0;
  55. if (y2 > heightLimit)
  56. y2 = heightLimit;
  57. if (y1 < y2)
  58. {
  59. const double startX = 256.0f * iter.x1;
  60. const double multiplier = (iter.x2 - iter.x1) / (iter.y2 - iter.y1);
  61. const int stepSize = jlimit (1, 256, 256 / (1 + (int) std::abs (multiplier)));
  62. do
  63. {
  64. const int step = jmin (stepSize, y2 - y1, 256 - (y1 & 255));
  65. int x = roundToInt (startX + multiplier * ((y1 + (step >> 1)) - startY));
  66. if (x < leftLimit)
  67. x = leftLimit;
  68. else if (x >= rightLimit)
  69. x = rightLimit - 1;
  70. addEdgePoint (x, y1 >> 8, direction * step);
  71. y1 += step;
  72. }
  73. while (y1 < y2);
  74. }
  75. }
  76. }
  77. sanitiseLevels (path.isUsingNonZeroWinding());
  78. }
  79. EdgeTable::EdgeTable (const Rectangle<int>& rectangleToAdd)
  80. : bounds (rectangleToAdd),
  81. maxEdgesPerLine (juce_edgeTableDefaultEdgesPerLine),
  82. lineStrideElements (juce_edgeTableDefaultEdgesPerLine * 2 + 1),
  83. needToCheckEmptiness (true)
  84. {
  85. allocate();
  86. table[0] = 0;
  87. const int x1 = rectangleToAdd.getX() << 8;
  88. const int x2 = rectangleToAdd.getRight() << 8;
  89. int* t = table;
  90. for (int i = rectangleToAdd.getHeight(); --i >= 0;)
  91. {
  92. t[0] = 2;
  93. t[1] = x1;
  94. t[2] = 255;
  95. t[3] = x2;
  96. t[4] = 0;
  97. t += lineStrideElements;
  98. }
  99. }
  100. EdgeTable::EdgeTable (const RectangleList<int>& rectanglesToAdd)
  101. : bounds (rectanglesToAdd.getBounds()),
  102. maxEdgesPerLine (juce_edgeTableDefaultEdgesPerLine),
  103. lineStrideElements (juce_edgeTableDefaultEdgesPerLine * 2 + 1),
  104. needToCheckEmptiness (true)
  105. {
  106. allocate();
  107. clearLineSizes();
  108. for (const Rectangle<int>* r = rectanglesToAdd.begin(), * const e = rectanglesToAdd.end(); r != e; ++r)
  109. {
  110. const int x1 = r->getX() << 8;
  111. const int x2 = r->getRight() << 8;
  112. int y = r->getY() - bounds.getY();
  113. for (int j = r->getHeight(); --j >= 0;)
  114. addEdgePointPair (x1, x2, y++, 255);
  115. }
  116. sanitiseLevels (true);
  117. }
  118. EdgeTable::EdgeTable (const RectangleList<float>& rectanglesToAdd)
  119. : bounds (rectanglesToAdd.getBounds().getSmallestIntegerContainer()),
  120. maxEdgesPerLine (rectanglesToAdd.getNumRectangles() * 2),
  121. lineStrideElements (rectanglesToAdd.getNumRectangles() * 4 + 1),
  122. needToCheckEmptiness (true)
  123. {
  124. bounds.setHeight (bounds.getHeight() + 1);
  125. allocate();
  126. clearLineSizes();
  127. for (const Rectangle<float>* r = rectanglesToAdd.begin(), * const e = rectanglesToAdd.end(); r != e; ++r)
  128. {
  129. const int x1 = roundToInt (r->getX() * 256.0f);
  130. const int x2 = roundToInt (r->getRight() * 256.0f);
  131. const int y1 = roundToInt (r->getY() * 256.0f) - (bounds.getY() << 8);
  132. const int y2 = roundToInt (r->getBottom() * 256.0f) - (bounds.getY() << 8);
  133. if (x2 <= x1 || y2 <= y1)
  134. continue;
  135. int y = y1 >> 8;
  136. const int lastLine = y2 >> 8;
  137. if (y == lastLine)
  138. {
  139. addEdgePointPair (x1, x2, y, y2 - y1);
  140. }
  141. else
  142. {
  143. addEdgePointPair (x1, x2, y++, 255 - (y1 & 255));
  144. while (y < lastLine)
  145. addEdgePointPair (x1, x2, y++, 255);
  146. jassert (y < bounds.getHeight());
  147. addEdgePointPair (x1, x2, y, y2 & 255);
  148. }
  149. }
  150. sanitiseLevels (true);
  151. }
  152. EdgeTable::EdgeTable (const Rectangle<float>& rectangleToAdd)
  153. : bounds (Rectangle<int> ((int) std::floor (rectangleToAdd.getX()),
  154. roundToInt (rectangleToAdd.getY() * 256.0f) >> 8,
  155. 2 + (int) rectangleToAdd.getWidth(),
  156. 2 + (int) rectangleToAdd.getHeight())),
  157. maxEdgesPerLine (juce_edgeTableDefaultEdgesPerLine),
  158. lineStrideElements ((juce_edgeTableDefaultEdgesPerLine << 1) + 1),
  159. needToCheckEmptiness (true)
  160. {
  161. jassert (! rectangleToAdd.isEmpty());
  162. allocate();
  163. table[0] = 0;
  164. const int x1 = roundToInt (rectangleToAdd.getX() * 256.0f);
  165. const int x2 = roundToInt (rectangleToAdd.getRight() * 256.0f);
  166. int y1 = roundToInt (rectangleToAdd.getY() * 256.0f) - (bounds.getY() << 8);
  167. jassert (y1 < 256);
  168. int y2 = roundToInt (rectangleToAdd.getBottom() * 256.0f) - (bounds.getY() << 8);
  169. if (x2 <= x1 || y2 <= y1)
  170. {
  171. bounds.setHeight (0);
  172. return;
  173. }
  174. int lineY = 0;
  175. int* t = table;
  176. if ((y1 >> 8) == (y2 >> 8))
  177. {
  178. t[0] = 2;
  179. t[1] = x1;
  180. t[2] = y2 - y1;
  181. t[3] = x2;
  182. t[4] = 0;
  183. ++lineY;
  184. t += lineStrideElements;
  185. }
  186. else
  187. {
  188. t[0] = 2;
  189. t[1] = x1;
  190. t[2] = 255 - (y1 & 255);
  191. t[3] = x2;
  192. t[4] = 0;
  193. ++lineY;
  194. t += lineStrideElements;
  195. while (lineY < (y2 >> 8))
  196. {
  197. t[0] = 2;
  198. t[1] = x1;
  199. t[2] = 255;
  200. t[3] = x2;
  201. t[4] = 0;
  202. ++lineY;
  203. t += lineStrideElements;
  204. }
  205. jassert (lineY < bounds.getHeight());
  206. t[0] = 2;
  207. t[1] = x1;
  208. t[2] = y2 & 255;
  209. t[3] = x2;
  210. t[4] = 0;
  211. ++lineY;
  212. t += lineStrideElements;
  213. }
  214. while (lineY < bounds.getHeight())
  215. {
  216. t[0] = 0;
  217. t += lineStrideElements;
  218. ++lineY;
  219. }
  220. }
  221. EdgeTable::EdgeTable (const EdgeTable& other)
  222. {
  223. operator= (other);
  224. }
  225. EdgeTable& EdgeTable::operator= (const EdgeTable& other)
  226. {
  227. bounds = other.bounds;
  228. maxEdgesPerLine = other.maxEdgesPerLine;
  229. lineStrideElements = other.lineStrideElements;
  230. needToCheckEmptiness = other.needToCheckEmptiness;
  231. allocate();
  232. copyEdgeTableData (table, lineStrideElements, other.table, lineStrideElements, bounds.getHeight());
  233. return *this;
  234. }
  235. EdgeTable::~EdgeTable()
  236. {
  237. }
  238. //==============================================================================
  239. static size_t getEdgeTableAllocationSize (int lineStride, int height) noexcept
  240. {
  241. // (leave an extra line at the end for use as scratch space)
  242. return (size_t) (lineStride * (2 + jmax (0, height)));
  243. }
  244. void EdgeTable::allocate()
  245. {
  246. table.malloc (getEdgeTableAllocationSize (lineStrideElements, bounds.getHeight()));
  247. }
  248. void EdgeTable::clearLineSizes() noexcept
  249. {
  250. int* t = table;
  251. for (int i = bounds.getHeight(); --i >= 0;)
  252. {
  253. *t = 0;
  254. t += lineStrideElements;
  255. }
  256. }
  257. void EdgeTable::copyEdgeTableData (int* dest, const int destLineStride, const int* src, const int srcLineStride, int numLines) noexcept
  258. {
  259. while (--numLines >= 0)
  260. {
  261. memcpy (dest, src, (size_t) (src[0] * 2 + 1) * sizeof (int));
  262. src += srcLineStride;
  263. dest += destLineStride;
  264. }
  265. }
  266. void EdgeTable::sanitiseLevels (const bool useNonZeroWinding) noexcept
  267. {
  268. // Convert the table from relative windings to absolute levels..
  269. int* lineStart = table;
  270. for (int y = bounds.getHeight(); --y >= 0;)
  271. {
  272. int num = lineStart[0];
  273. if (num > 0)
  274. {
  275. LineItem* items = reinterpret_cast<LineItem*> (lineStart + 1);
  276. // sort the X coords
  277. std::sort (items, items + num);
  278. // merge duplicate X coords
  279. for (int i = 0; i < num - 1; ++i)
  280. {
  281. if (items[i].x == items[i + 1].x)
  282. {
  283. items[i].level += items[i + 1].level;
  284. memmove (items + i + 1, items + i + 2, (size_t) (num - i - 2) * sizeof (LineItem));
  285. --num;
  286. --lineStart[0];
  287. --i;
  288. }
  289. }
  290. int level = 0;
  291. if (useNonZeroWinding)
  292. {
  293. while (--num > 0)
  294. {
  295. level += items->level;
  296. int corrected = std::abs (level);
  297. if (corrected >> 8)
  298. corrected = 255;
  299. items->level = corrected;
  300. ++items;
  301. }
  302. }
  303. else
  304. {
  305. while (--num > 0)
  306. {
  307. level += items->level;
  308. int corrected = std::abs (level);
  309. if (corrected >> 8)
  310. {
  311. corrected &= 511;
  312. if (corrected >> 8)
  313. corrected = 511 - corrected;
  314. }
  315. items->level = corrected;
  316. ++items;
  317. }
  318. }
  319. items->level = 0; // force the last level to 0, just in case something went wrong in creating the table
  320. }
  321. lineStart += lineStrideElements;
  322. }
  323. }
  324. void EdgeTable::remapTableForNumEdges (const int newNumEdgesPerLine)
  325. {
  326. if (newNumEdgesPerLine != maxEdgesPerLine)
  327. {
  328. maxEdgesPerLine = newNumEdgesPerLine;
  329. jassert (bounds.getHeight() > 0);
  330. const int newLineStrideElements = maxEdgesPerLine * 2 + 1;
  331. HeapBlock<int> newTable (getEdgeTableAllocationSize (newLineStrideElements, bounds.getHeight()));
  332. copyEdgeTableData (newTable, newLineStrideElements, table, lineStrideElements, bounds.getHeight());
  333. table.swapWith (newTable);
  334. lineStrideElements = newLineStrideElements;
  335. }
  336. }
  337. void EdgeTable::optimiseTable()
  338. {
  339. int maxLineElements = 0;
  340. for (int i = bounds.getHeight(); --i >= 0;)
  341. maxLineElements = jmax (maxLineElements, table [i * lineStrideElements]);
  342. remapTableForNumEdges (maxLineElements);
  343. }
  344. void EdgeTable::addEdgePoint (const int x, const int y, const int winding)
  345. {
  346. jassert (y >= 0 && y < bounds.getHeight());
  347. int* line = table + lineStrideElements * y;
  348. const int numPoints = line[0];
  349. if (numPoints >= maxEdgesPerLine)
  350. {
  351. remapTableForNumEdges (maxEdgesPerLine + juce_edgeTableDefaultEdgesPerLine);
  352. jassert (numPoints < maxEdgesPerLine);
  353. line = table + lineStrideElements * y;
  354. }
  355. line[0]++;
  356. int n = numPoints << 1;
  357. line [n + 1] = x;
  358. line [n + 2] = winding;
  359. }
  360. void EdgeTable::addEdgePointPair (int x1, int x2, int y, int winding)
  361. {
  362. jassert (y >= 0 && y < bounds.getHeight());
  363. int* line = table + lineStrideElements * y;
  364. const int numPoints = line[0];
  365. if (numPoints + 1 >= maxEdgesPerLine)
  366. {
  367. remapTableForNumEdges (maxEdgesPerLine + juce_edgeTableDefaultEdgesPerLine);
  368. jassert (numPoints < maxEdgesPerLine);
  369. line = table + lineStrideElements * y;
  370. }
  371. line[0] += 2;
  372. int n = numPoints << 1;
  373. line [n + 1] = x1;
  374. line [n + 2] = winding;
  375. line [n + 3] = x2;
  376. line [n + 4] = -winding;
  377. }
  378. void EdgeTable::translate (float dx, const int dy) noexcept
  379. {
  380. bounds.translate ((int) std::floor (dx), dy);
  381. int* lineStart = table;
  382. const int intDx = (int) (dx * 256.0f);
  383. for (int i = bounds.getHeight(); --i >= 0;)
  384. {
  385. int* line = lineStart;
  386. lineStart += lineStrideElements;
  387. int num = *line++;
  388. while (--num >= 0)
  389. {
  390. *line += intDx;
  391. line += 2;
  392. }
  393. }
  394. }
  395. void EdgeTable::intersectWithEdgeTableLine (const int y, const int* const otherLine)
  396. {
  397. jassert (y >= 0 && y < bounds.getHeight());
  398. int* srcLine = table + lineStrideElements * y;
  399. int srcNum1 = *srcLine;
  400. if (srcNum1 == 0)
  401. return;
  402. int srcNum2 = *otherLine;
  403. if (srcNum2 == 0)
  404. {
  405. *srcLine = 0;
  406. return;
  407. }
  408. const int right = bounds.getRight() << 8;
  409. // optimise for the common case where our line lies entirely within a
  410. // single pair of points, as happens when clipping to a simple rect.
  411. if (srcNum2 == 2 && otherLine[2] >= 255)
  412. {
  413. clipEdgeTableLineToRange (srcLine, otherLine[1], jmin (right, otherLine[3]));
  414. return;
  415. }
  416. bool isUsingTempSpace = false;
  417. const int* src1 = srcLine + 1;
  418. int x1 = *src1++;
  419. const int* src2 = otherLine + 1;
  420. int x2 = *src2++;
  421. int destIndex = 0, destTotal = 0;
  422. int level1 = 0, level2 = 0;
  423. int lastX = std::numeric_limits<int>::min(), lastLevel = 0;
  424. while (srcNum1 > 0 && srcNum2 > 0)
  425. {
  426. int nextX;
  427. if (x1 <= x2)
  428. {
  429. if (x1 == x2)
  430. {
  431. level2 = *src2++;
  432. x2 = *src2++;
  433. --srcNum2;
  434. }
  435. nextX = x1;
  436. level1 = *src1++;
  437. x1 = *src1++;
  438. --srcNum1;
  439. }
  440. else
  441. {
  442. nextX = x2;
  443. level2 = *src2++;
  444. x2 = *src2++;
  445. --srcNum2;
  446. }
  447. if (nextX > lastX)
  448. {
  449. if (nextX >= right)
  450. break;
  451. lastX = nextX;
  452. const int nextLevel = (level1 * (level2 + 1)) >> 8;
  453. jassert (isPositiveAndBelow (nextLevel, (int) 256));
  454. if (nextLevel != lastLevel)
  455. {
  456. if (destTotal >= maxEdgesPerLine)
  457. {
  458. srcLine[0] = destTotal;
  459. remapTableForNumEdges (jmax (256, destTotal * 2));
  460. srcLine = table + lineStrideElements * y;
  461. }
  462. ++destTotal;
  463. lastLevel = nextLevel;
  464. if (! isUsingTempSpace)
  465. {
  466. isUsingTempSpace = true;
  467. int* const temp = table + lineStrideElements * bounds.getHeight();
  468. memcpy (temp, src1, (size_t) (srcNum1 * 2 * sizeof (int)));
  469. src1 = temp;
  470. }
  471. srcLine[++destIndex] = nextX;
  472. srcLine[++destIndex] = nextLevel;
  473. }
  474. }
  475. }
  476. if (lastLevel > 0)
  477. {
  478. if (destTotal >= maxEdgesPerLine)
  479. {
  480. srcLine[0] = destTotal;
  481. remapTableForNumEdges (jmax (256, destTotal * 2));
  482. srcLine = table + lineStrideElements * y;
  483. }
  484. ++destTotal;
  485. srcLine[++destIndex] = right;
  486. srcLine[++destIndex] = 0;
  487. }
  488. srcLine[0] = destTotal;
  489. }
  490. void EdgeTable::clipEdgeTableLineToRange (int* dest, const int x1, const int x2) noexcept
  491. {
  492. int* lastItem = dest + (dest[0] * 2 - 1);
  493. if (x2 < lastItem[0])
  494. {
  495. if (x2 <= dest[1])
  496. {
  497. dest[0] = 0;
  498. return;
  499. }
  500. while (x2 < lastItem[-2])
  501. {
  502. --(dest[0]);
  503. lastItem -= 2;
  504. }
  505. lastItem[0] = x2;
  506. lastItem[1] = 0;
  507. }
  508. if (x1 > dest[1])
  509. {
  510. while (lastItem[0] > x1)
  511. lastItem -= 2;
  512. const int itemsRemoved = (int) (lastItem - (dest + 1)) / 2;
  513. if (itemsRemoved > 0)
  514. {
  515. dest[0] -= itemsRemoved;
  516. memmove (dest + 1, lastItem, (size_t) dest[0] * (sizeof (int) * 2));
  517. }
  518. dest[1] = x1;
  519. }
  520. }
  521. //==============================================================================
  522. void EdgeTable::clipToRectangle (const Rectangle<int>& r)
  523. {
  524. const Rectangle<int> clipped (r.getIntersection (bounds));
  525. if (clipped.isEmpty())
  526. {
  527. needToCheckEmptiness = false;
  528. bounds.setHeight (0);
  529. }
  530. else
  531. {
  532. const int top = clipped.getY() - bounds.getY();
  533. const int bottom = clipped.getBottom() - bounds.getY();
  534. if (bottom < bounds.getHeight())
  535. bounds.setHeight (bottom);
  536. for (int i = top; --i >= 0;)
  537. table [lineStrideElements * i] = 0;
  538. if (clipped.getX() > bounds.getX() || clipped.getRight() < bounds.getRight())
  539. {
  540. const int x1 = clipped.getX() << 8;
  541. const int x2 = jmin (bounds.getRight(), clipped.getRight()) << 8;
  542. int* line = table + lineStrideElements * top;
  543. for (int i = bottom - top; --i >= 0;)
  544. {
  545. if (line[0] != 0)
  546. clipEdgeTableLineToRange (line, x1, x2);
  547. line += lineStrideElements;
  548. }
  549. }
  550. needToCheckEmptiness = true;
  551. }
  552. }
  553. void EdgeTable::excludeRectangle (const Rectangle<int>& r)
  554. {
  555. const Rectangle<int> clipped (r.getIntersection (bounds));
  556. if (! clipped.isEmpty())
  557. {
  558. const int top = clipped.getY() - bounds.getY();
  559. const int bottom = clipped.getBottom() - bounds.getY();
  560. const int rectLine[] = { 4, std::numeric_limits<int>::min(), 255,
  561. clipped.getX() << 8, 0,
  562. clipped.getRight() << 8, 255,
  563. std::numeric_limits<int>::max(), 0 };
  564. for (int i = top; i < bottom; ++i)
  565. intersectWithEdgeTableLine (i, rectLine);
  566. needToCheckEmptiness = true;
  567. }
  568. }
  569. void EdgeTable::clipToEdgeTable (const EdgeTable& other)
  570. {
  571. const Rectangle<int> clipped (other.bounds.getIntersection (bounds));
  572. if (clipped.isEmpty())
  573. {
  574. needToCheckEmptiness = false;
  575. bounds.setHeight (0);
  576. }
  577. else
  578. {
  579. const int top = clipped.getY() - bounds.getY();
  580. const int bottom = clipped.getBottom() - bounds.getY();
  581. if (bottom < bounds.getHeight())
  582. bounds.setHeight (bottom);
  583. if (clipped.getRight() < bounds.getRight())
  584. bounds.setRight (clipped.getRight());
  585. for (int i = 0; i < top; ++i)
  586. table [lineStrideElements * i] = 0;
  587. const int* otherLine = other.table + other.lineStrideElements * (clipped.getY() - other.bounds.getY());
  588. for (int i = top; i < bottom; ++i)
  589. {
  590. intersectWithEdgeTableLine (i, otherLine);
  591. otherLine += other.lineStrideElements;
  592. }
  593. needToCheckEmptiness = true;
  594. }
  595. }
  596. void EdgeTable::clipLineToMask (int x, int y, const uint8* mask, int maskStride, int numPixels)
  597. {
  598. y -= bounds.getY();
  599. if (y < 0 || y >= bounds.getHeight())
  600. return;
  601. needToCheckEmptiness = true;
  602. if (numPixels <= 0)
  603. {
  604. table [lineStrideElements * y] = 0;
  605. return;
  606. }
  607. int* tempLine = static_cast<int*> (alloca ((size_t) (numPixels * 2 + 4) * sizeof (int)));
  608. int destIndex = 0, lastLevel = 0;
  609. while (--numPixels >= 0)
  610. {
  611. const int alpha = *mask;
  612. mask += maskStride;
  613. if (alpha != lastLevel)
  614. {
  615. tempLine[++destIndex] = (x << 8);
  616. tempLine[++destIndex] = alpha;
  617. lastLevel = alpha;
  618. }
  619. ++x;
  620. }
  621. if (lastLevel > 0)
  622. {
  623. tempLine[++destIndex] = (x << 8);
  624. tempLine[++destIndex] = 0;
  625. }
  626. tempLine[0] = destIndex >> 1;
  627. intersectWithEdgeTableLine (y, tempLine);
  628. }
  629. bool EdgeTable::isEmpty() noexcept
  630. {
  631. if (needToCheckEmptiness)
  632. {
  633. needToCheckEmptiness = false;
  634. int* t = table;
  635. for (int i = bounds.getHeight(); --i >= 0;)
  636. {
  637. if (t[0] > 1)
  638. return false;
  639. t += lineStrideElements;
  640. }
  641. bounds.setHeight (0);
  642. }
  643. return bounds.getHeight() == 0;
  644. }