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.

217 lines
8.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #pragma once
  18. //==============================================================================
  19. /**
  20. A table of horizontal scan-line segments - used for rasterising Paths.
  21. @see Path, Graphics
  22. */
  23. class JUCE_API EdgeTable
  24. {
  25. public:
  26. //==============================================================================
  27. /** Creates an edge table containing a path.
  28. A table is created with a fixed vertical range, and only sections of the path
  29. which lie within this range will be added to the table.
  30. @param clipLimits only the region of the path that lies within this area will be added
  31. @param pathToAdd the path to add to the table
  32. @param transform a transform to apply to the path being added
  33. */
  34. EdgeTable (const Rectangle<int>& clipLimits,
  35. const Path& pathToAdd,
  36. const AffineTransform& transform);
  37. /** Creates an edge table containing a rectangle. */
  38. explicit EdgeTable (const Rectangle<int>& rectangleToAdd);
  39. /** Creates an edge table containing a rectangle list. */
  40. explicit EdgeTable (const RectangleList<int>& rectanglesToAdd);
  41. /** Creates an edge table containing a rectangle list. */
  42. explicit EdgeTable (const RectangleList<float>& rectanglesToAdd);
  43. /** Creates an edge table containing a rectangle. */
  44. explicit EdgeTable (const Rectangle<float>& rectangleToAdd);
  45. /** Creates a copy of another edge table. */
  46. EdgeTable (const EdgeTable&);
  47. /** Copies from another edge table. */
  48. EdgeTable& operator= (const EdgeTable&);
  49. /** Destructor. */
  50. ~EdgeTable();
  51. //==============================================================================
  52. void clipToRectangle (const Rectangle<int>& r);
  53. void excludeRectangle (const Rectangle<int>& r);
  54. void clipToEdgeTable (const EdgeTable&);
  55. void clipLineToMask (int x, int y, const uint8* mask, int maskStride, int numPixels);
  56. bool isEmpty() noexcept;
  57. const Rectangle<int>& getMaximumBounds() const noexcept { return bounds; }
  58. void translate (float dx, int dy) noexcept;
  59. /** Scales all the alpha-levels in the table by the given multiplier. */
  60. void multiplyLevels (float factor);
  61. /** Reduces the amount of space the table has allocated.
  62. This will shrink the table down to use as little memory as possible - useful for
  63. read-only tables that get stored and re-used for rendering.
  64. */
  65. void optimiseTable();
  66. //==============================================================================
  67. /** Iterates the lines in the table, for rendering.
  68. This function will iterate each line in the table, and call a user-defined class
  69. to render each pixel or continuous line of pixels that the table contains.
  70. @param iterationCallback this templated class must contain the following methods:
  71. @code
  72. inline void setEdgeTableYPos (int y);
  73. inline void handleEdgeTablePixel (int x, int alphaLevel) const;
  74. inline void handleEdgeTablePixelFull (int x) const;
  75. inline void handleEdgeTableLine (int x, int width, int alphaLevel) const;
  76. inline void handleEdgeTableLineFull (int x, int width) const;
  77. @endcode
  78. (these don't necessarily have to be 'const', but it might help it go faster)
  79. */
  80. template <class EdgeTableIterationCallback>
  81. void iterate (EdgeTableIterationCallback& iterationCallback) const noexcept
  82. {
  83. const int* lineStart = table;
  84. for (int y = 0; y < bounds.getHeight(); ++y)
  85. {
  86. const int* line = lineStart;
  87. lineStart += lineStrideElements;
  88. int numPoints = line[0];
  89. if (--numPoints > 0)
  90. {
  91. int x = *++line;
  92. jassert ((x >> 8) >= bounds.getX() && (x >> 8) < bounds.getRight());
  93. int levelAccumulator = 0;
  94. iterationCallback.setEdgeTableYPos (bounds.getY() + y);
  95. while (--numPoints >= 0)
  96. {
  97. const int level = *++line;
  98. jassert (isPositiveAndBelow (level, (int) 256));
  99. const int endX = *++line;
  100. jassert (endX >= x);
  101. const int endOfRun = (endX >> 8);
  102. if (endOfRun == (x >> 8))
  103. {
  104. // small segment within the same pixel, so just save it for the next
  105. // time round..
  106. levelAccumulator += (endX - x) * level;
  107. }
  108. else
  109. {
  110. // plot the fist pixel of this segment, including any accumulated
  111. // levels from smaller segments that haven't been drawn yet
  112. levelAccumulator += (0x100 - (x & 0xff)) * level;
  113. levelAccumulator >>= 8;
  114. x >>= 8;
  115. if (levelAccumulator > 0)
  116. {
  117. if (levelAccumulator >= 255)
  118. iterationCallback.handleEdgeTablePixelFull (x);
  119. else
  120. iterationCallback.handleEdgeTablePixel (x, levelAccumulator);
  121. }
  122. // if there's a run of similar pixels, do it all in one go..
  123. if (level > 0)
  124. {
  125. jassert (endOfRun <= bounds.getRight());
  126. const int numPix = endOfRun - ++x;
  127. if (numPix > 0)
  128. iterationCallback.handleEdgeTableLine (x, numPix, level);
  129. }
  130. // save the bit at the end to be drawn next time round the loop.
  131. levelAccumulator = (endX & 0xff) * level;
  132. }
  133. x = endX;
  134. }
  135. levelAccumulator >>= 8;
  136. if (levelAccumulator > 0)
  137. {
  138. x >>= 8;
  139. jassert (x >= bounds.getX() && x < bounds.getRight());
  140. if (levelAccumulator >= 255)
  141. iterationCallback.handleEdgeTablePixelFull (x);
  142. else
  143. iterationCallback.handleEdgeTablePixel (x, levelAccumulator);
  144. }
  145. }
  146. }
  147. }
  148. private:
  149. //==============================================================================
  150. // table line format: number of points; point0 x, point0 levelDelta, point1 x, point1 levelDelta, etc
  151. struct LineItem
  152. {
  153. int x, level;
  154. bool operator< (const LineItem& other) const noexcept { return x < other.x; }
  155. };
  156. HeapBlock<int> table;
  157. Rectangle<int> bounds;
  158. int maxEdgesPerLine, lineStrideElements;
  159. bool needToCheckEmptiness;
  160. void allocate();
  161. void clearLineSizes() noexcept;
  162. void addEdgePoint (int x, int y, int winding);
  163. void addEdgePointPair (int x1, int x2, int y, int winding);
  164. void remapTableForNumEdges (int newNumEdgesPerLine);
  165. void intersectWithEdgeTableLine (int y, const int* otherLine);
  166. void clipEdgeTableLineToRange (int* line, int x1, int x2) noexcept;
  167. void sanitiseLevels (bool useNonZeroWinding) noexcept;
  168. static void copyEdgeTableData (int* dest, int destLineStride, const int* src, int srcLineStride, int numLines) noexcept;
  169. JUCE_LEAK_DETECTOR (EdgeTable)
  170. };