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

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