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.

218 lines
8.8KB

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