The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

742 lines
20KB

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