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.

212 lines
8.7KB

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