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.

211 lines
8.5KB

  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. #include "../geometry/juce_AffineTransform.h"
  20. #include "../geometry/juce_Rectangle.h"
  21. #include "../geometry/juce_RectangleList.h"
  22. class Path;
  23. class Image;
  24. //==============================================================================
  25. /**
  26. A table of horizontal scan-line segments - used for rasterising Paths.
  27. @see Path, Graphics
  28. */
  29. class JUCE_API EdgeTable
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an edge table containing a path.
  34. A table is created with a fixed vertical range, and only sections of the path
  35. which lie within this range will be added to the table.
  36. @param clipLimits only the region of the path that lies within this area will be added
  37. @param pathToAdd the path to add to the table
  38. @param transform a transform to apply to the path being added
  39. */
  40. EdgeTable (const Rectangle<int>& clipLimits,
  41. const Path& pathToAdd,
  42. const AffineTransform& transform);
  43. /** Creates an edge table containing a rectangle. */
  44. explicit EdgeTable (const Rectangle<int>& rectangleToAdd);
  45. /** Creates an edge table containing a rectangle list. */
  46. explicit EdgeTable (const RectangleList<int>& rectanglesToAdd);
  47. /** Creates an edge table containing a rectangle. */
  48. explicit EdgeTable (const Rectangle<float>& rectangleToAdd);
  49. /** Creates a copy of another edge table. */
  50. EdgeTable (const EdgeTable& other);
  51. /** Copies from another edge table. */
  52. EdgeTable& operator= (const EdgeTable& other);
  53. /** Destructor. */
  54. ~EdgeTable();
  55. //==============================================================================
  56. void clipToRectangle (const Rectangle<int>& r);
  57. void excludeRectangle (const Rectangle<int>& r);
  58. void clipToEdgeTable (const EdgeTable& other);
  59. void clipLineToMask (int x, int y, const uint8* mask, int maskStride, int numPixels);
  60. bool isEmpty() noexcept;
  61. const Rectangle<int>& getMaximumBounds() const noexcept { return bounds; }
  62. void translate (float dx, int dy) noexcept;
  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. HeapBlock<int> table;
  154. Rectangle<int> bounds;
  155. int maxEdgesPerLine, lineStrideElements;
  156. bool needToCheckEmptinesss;
  157. void addEdgePoint (int x, int y, int winding);
  158. void remapTableForNumEdges (int newNumEdgesPerLine);
  159. void intersectWithEdgeTableLine (int y, const int* otherLine);
  160. void clipEdgeTableLineToRange (int* line, int x1, int x2) noexcept;
  161. void sanitiseLevels (bool useNonZeroWinding) noexcept;
  162. static void copyEdgeTableData (int* dest, int destLineStride, const int* src, int srcLineStride, int numLines) noexcept;
  163. JUCE_LEAK_DETECTOR (EdgeTable)
  164. };
  165. #endif // JUCE_EDGETABLE_H_INCLUDED