openshot-audio  0.1.7
juce_EdgeTable.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission is granted to use this software under the terms of either:
8  a) the GPL v2 (or any later version)
9  b) the Affero GPL v3
10 
11  Details of these licenses can be found at: www.gnu.org/licenses
12 
13  JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15  A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17  ------------------------------------------------------------------------------
18 
19  To release a closed-source product which uses JUCE, commercial licenses are
20  available: visit www.juce.com for more information.
21 
22  ==============================================================================
23 */
24 
25 #ifndef JUCE_EDGETABLE_H_INCLUDED
26 #define JUCE_EDGETABLE_H_INCLUDED
27 
28 
29 //==============================================================================
36 {
37 public:
38  //==============================================================================
48  EdgeTable (const Rectangle<int>& clipLimits,
49  const Path& pathToAdd,
50  const AffineTransform& transform);
51 
53  explicit EdgeTable (const Rectangle<int>& rectangleToAdd);
54 
56  explicit EdgeTable (const RectangleList<int>& rectanglesToAdd);
57 
59  explicit EdgeTable (const RectangleList<float>& rectanglesToAdd);
60 
62  explicit EdgeTable (const Rectangle<float>& rectangleToAdd);
63 
65  EdgeTable (const EdgeTable&);
66 
68  EdgeTable& operator= (const EdgeTable&);
69 
71  ~EdgeTable();
72 
73  //==============================================================================
74  void clipToRectangle (const Rectangle<int>& r);
75  void excludeRectangle (const Rectangle<int>& r);
76  void clipToEdgeTable (const EdgeTable&);
77  void clipLineToMask (int x, int y, const uint8* mask, int maskStride, int numPixels);
78  bool isEmpty() noexcept;
79  const Rectangle<int>& getMaximumBounds() const noexcept { return bounds; }
80  void translate (float dx, int dy) noexcept;
81 
83  void multiplyLevels (float factor);
84 
90  void optimiseTable();
91 
92 
93  //==============================================================================
109  template <class EdgeTableIterationCallback>
110  void iterate (EdgeTableIterationCallback& iterationCallback) const noexcept
111  {
112  const int* lineStart = table;
113 
114  for (int y = 0; y < bounds.getHeight(); ++y)
115  {
116  const int* line = lineStart;
117  lineStart += lineStrideElements;
118  int numPoints = line[0];
119 
120  if (--numPoints > 0)
121  {
122  int x = *++line;
123  jassert ((x >> 8) >= bounds.getX() && (x >> 8) < bounds.getRight());
124  int levelAccumulator = 0;
125 
126  iterationCallback.setEdgeTableYPos (bounds.getY() + y);
127 
128  while (--numPoints >= 0)
129  {
130  const int level = *++line;
131  jassert (isPositiveAndBelow (level, (int) 256));
132  const int endX = *++line;
133  jassert (endX >= x);
134  const int endOfRun = (endX >> 8);
135 
136  if (endOfRun == (x >> 8))
137  {
138  // small segment within the same pixel, so just save it for the next
139  // time round..
140  levelAccumulator += (endX - x) * level;
141  }
142  else
143  {
144  // plot the fist pixel of this segment, including any accumulated
145  // levels from smaller segments that haven't been drawn yet
146  levelAccumulator += (0x100 - (x & 0xff)) * level;
147  levelAccumulator >>= 8;
148  x >>= 8;
149 
150  if (levelAccumulator > 0)
151  {
152  if (levelAccumulator >= 255)
153  iterationCallback.handleEdgeTablePixelFull (x);
154  else
155  iterationCallback.handleEdgeTablePixel (x, levelAccumulator);
156  }
157 
158  // if there's a run of similar pixels, do it all in one go..
159  if (level > 0)
160  {
161  jassert (endOfRun <= bounds.getRight());
162  const int numPix = endOfRun - ++x;
163 
164  if (numPix > 0)
165  iterationCallback.handleEdgeTableLine (x, numPix, level);
166  }
167 
168  // save the bit at the end to be drawn next time round the loop.
169  levelAccumulator = (endX & 0xff) * level;
170  }
171 
172  x = endX;
173  }
174 
175  levelAccumulator >>= 8;
176 
177  if (levelAccumulator > 0)
178  {
179  x >>= 8;
180  jassert (x >= bounds.getX() && x < bounds.getRight());
181 
182  if (levelAccumulator >= 255)
183  iterationCallback.handleEdgeTablePixelFull (x);
184  else
185  iterationCallback.handleEdgeTablePixel (x, levelAccumulator);
186  }
187  }
188  }
189  }
190 
191 private:
192  //==============================================================================
193  // table line format: number of points; point0 x, point0 levelDelta, point1 x, point1 levelDelta, etc
194  struct LineItem
195  {
196  int x, level;
197 
198  bool operator< (const LineItem& other) const noexcept { return x < other.x; }
199  };
200 
201  HeapBlock<int> table;
202  Rectangle<int> bounds;
203  int maxEdgesPerLine, lineStrideElements;
204  bool needToCheckEmptiness;
205 
206  void allocate();
207  void clearLineSizes() noexcept;
208  void addEdgePoint (int x, int y, int winding);
209  void addEdgePointPair (int x1, int x2, int y, int winding);
210  void remapTableForNumEdges (int newNumEdgesPerLine);
211  void intersectWithEdgeTableLine (int y, const int* otherLine);
212  void clipEdgeTableLineToRange (int* line, int x1, int x2) noexcept;
213  void sanitiseLevels (bool useNonZeroWinding) noexcept;
214  static void copyEdgeTableData (int* dest, int destLineStride, const int* src, int srcLineStride, int numLines) noexcept;
215 
217 };
218 
219 
220 #endif // JUCE_EDGETABLE_H_INCLUDED
Definition: juce_EdgeTable.h:35
#define noexcept
Definition: juce_CompilerSupport.h:141
bool isPositiveAndBelow(Type valueToTest, Type upperLimit) noexcept
Definition: juce_core.h:238
Definition: juce_RectangleList.h:40
void iterate(EdgeTableIterationCallback &iterationCallback) const noexcept
Definition: juce_EdgeTable.h:110
#define JUCE_API
Definition: juce_StandardHeader.h:139
#define const
Definition: juce_Rectangle.h:36
Definition: juce_Path.h:62
JUCE_API bool JUCE_CALLTYPE operator<(const String &s1, const String &s2) noexcept
Definition: juce_core.cpp:590
#define jassert(a)
Definition: juce_PlatformDefs.h:146
Definition: juce_HeapBlock.h:90
Definition: juce_AffineTransform.h:40
JUCE_API String translate(const String &text)
Definition: juce_core.cpp:198
#define JUCE_LEAK_DETECTOR(OwnerClass)
Definition: juce_LeakedObjectDetector.h:141
unsigned char uint8
Definition: juce_MathsFunctions.h:43
const Rectangle< int > & getMaximumBounds() const noexcept
Definition: juce_EdgeTable.h:79
LONG x
Definition: juce_win32_Windowing.cpp:78