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.

219 lines
8.7KB

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