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.

2630 lines
101KB

  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_RENDERINGHELPERS_H_INCLUDED
  18. #define JUCE_RENDERINGHELPERS_H_INCLUDED
  19. #if JUCE_MSVC
  20. #pragma warning (push)
  21. #pragma warning (disable: 4127) // "expression is constant" warning
  22. #endif
  23. namespace RenderingHelpers
  24. {
  25. //==============================================================================
  26. /** Holds either a simple integer translation, or an affine transform.
  27. */
  28. class TranslationOrTransform
  29. {
  30. public:
  31. TranslationOrTransform (Point<int> origin) noexcept
  32. : offset (origin), isOnlyTranslated (true), isRotated (false)
  33. {
  34. }
  35. TranslationOrTransform (const TranslationOrTransform& other) noexcept
  36. : complexTransform (other.complexTransform), offset (other.offset),
  37. isOnlyTranslated (other.isOnlyTranslated), isRotated (other.isRotated)
  38. {
  39. }
  40. AffineTransform getTransform() const noexcept
  41. {
  42. return isOnlyTranslated ? AffineTransform::translation (offset)
  43. : complexTransform;
  44. }
  45. AffineTransform getTransformWith (const AffineTransform& userTransform) const noexcept
  46. {
  47. return isOnlyTranslated ? userTransform.translated (offset)
  48. : userTransform.followedBy (complexTransform);
  49. }
  50. void setOrigin (Point<int> delta) noexcept
  51. {
  52. if (isOnlyTranslated)
  53. offset += delta;
  54. else
  55. complexTransform = AffineTransform::translation (delta)
  56. .followedBy (complexTransform);
  57. }
  58. void addTransform (const AffineTransform& t) noexcept
  59. {
  60. if (isOnlyTranslated && t.isOnlyTranslation())
  61. {
  62. const int tx = (int) (t.getTranslationX() * 256.0f);
  63. const int ty = (int) (t.getTranslationY() * 256.0f);
  64. if (((tx | ty) & 0xf8) == 0)
  65. {
  66. offset += Point<int> (tx >> 8, ty >> 8);
  67. return;
  68. }
  69. }
  70. complexTransform = getTransformWith (t);
  71. isOnlyTranslated = false;
  72. isRotated = (complexTransform.mat01 != 0 || complexTransform.mat10 != 0);
  73. }
  74. float getPhysicalPixelScaleFactor() const noexcept
  75. {
  76. return isOnlyTranslated ? 1.0f : std::abs (complexTransform.getScaleFactor());
  77. }
  78. void moveOriginInDeviceSpace (Point<int> delta) noexcept
  79. {
  80. if (isOnlyTranslated)
  81. offset += delta;
  82. else
  83. complexTransform = complexTransform.translated (delta);
  84. }
  85. Rectangle<int> translated (const Rectangle<int>& r) const noexcept
  86. {
  87. jassert (isOnlyTranslated);
  88. return r + offset;
  89. }
  90. Rectangle<float> translated (const Rectangle<float>& r) const noexcept
  91. {
  92. jassert (isOnlyTranslated);
  93. return r + offset.toFloat();
  94. }
  95. template <typename RectangleOrPoint>
  96. RectangleOrPoint transformed (const RectangleOrPoint& r) const noexcept
  97. {
  98. jassert (! isOnlyTranslated);
  99. return r.transformedBy (complexTransform);
  100. }
  101. template <typename Type>
  102. Rectangle<Type> deviceSpaceToUserSpace (const Rectangle<Type>& r) const noexcept
  103. {
  104. return isOnlyTranslated ? r - offset
  105. : r.transformedBy (complexTransform.inverted());
  106. }
  107. AffineTransform complexTransform;
  108. Point<int> offset;
  109. bool isOnlyTranslated, isRotated;
  110. };
  111. //==============================================================================
  112. /** Holds a cache of recently-used glyph objects of some type. */
  113. template <class CachedGlyphType, class RenderTargetType>
  114. class GlyphCache : private DeletedAtShutdown
  115. {
  116. public:
  117. GlyphCache()
  118. {
  119. addNewGlyphSlots (120);
  120. }
  121. ~GlyphCache()
  122. {
  123. getSingletonPointer() = nullptr;
  124. }
  125. static GlyphCache& getInstance()
  126. {
  127. GlyphCache*& g = getSingletonPointer();
  128. if (g == nullptr)
  129. g = new GlyphCache();
  130. return *g;
  131. }
  132. //==============================================================================
  133. void drawGlyph (RenderTargetType& target, const Font& font, const int glyphNumber, Point<float> pos)
  134. {
  135. ++accessCounter;
  136. CachedGlyphType* glyph = nullptr;
  137. const ScopedReadLock srl (lock);
  138. for (int i = glyphs.size(); --i >= 0;)
  139. {
  140. CachedGlyphType* const g = glyphs.getUnchecked (i);
  141. if (g->glyph == glyphNumber && g->font == font)
  142. {
  143. glyph = g;
  144. ++hits;
  145. break;
  146. }
  147. }
  148. if (glyph == nullptr)
  149. {
  150. ++misses;
  151. const ScopedWriteLock swl (lock);
  152. if (hits.value + misses.value > glyphs.size() * 16)
  153. {
  154. if (misses.value * 2 > hits.value)
  155. addNewGlyphSlots (32);
  156. hits.set (0);
  157. misses.set (0);
  158. glyph = glyphs.getLast();
  159. }
  160. else
  161. {
  162. glyph = findLeastRecentlyUsedGlyph();
  163. }
  164. jassert (glyph != nullptr);
  165. glyph->generate (font, glyphNumber);
  166. }
  167. glyph->lastAccessCount = accessCounter.value;
  168. glyph->draw (target, pos);
  169. }
  170. private:
  171. friend struct ContainerDeletePolicy<CachedGlyphType>;
  172. OwnedArray<CachedGlyphType> glyphs;
  173. Atomic<int> accessCounter, hits, misses;
  174. ReadWriteLock lock;
  175. void addNewGlyphSlots (int num)
  176. {
  177. while (--num >= 0)
  178. glyphs.add (new CachedGlyphType());
  179. }
  180. CachedGlyphType* findLeastRecentlyUsedGlyph() const noexcept
  181. {
  182. CachedGlyphType* oldest = glyphs.getLast();
  183. int oldestCounter = oldest->lastAccessCount;
  184. for (int i = glyphs.size() - 1; --i >= 0;)
  185. {
  186. CachedGlyphType* const glyph = glyphs.getUnchecked(i);
  187. if (glyph->lastAccessCount <= oldestCounter)
  188. {
  189. oldestCounter = glyph->lastAccessCount;
  190. oldest = glyph;
  191. }
  192. }
  193. return oldest;
  194. }
  195. static GlyphCache*& getSingletonPointer() noexcept
  196. {
  197. static GlyphCache* g = nullptr;
  198. return g;
  199. }
  200. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GlyphCache)
  201. };
  202. //==============================================================================
  203. /** Caches a glyph as an edge-table. */
  204. template <class RendererType>
  205. class CachedGlyphEdgeTable
  206. {
  207. public:
  208. CachedGlyphEdgeTable() : glyph (0), lastAccessCount (0) {}
  209. void draw (RendererType& state, Point<float> pos) const
  210. {
  211. if (snapToIntegerCoordinate)
  212. pos.x = std::floor (pos.x + 0.5f);
  213. if (edgeTable != nullptr)
  214. state.fillEdgeTable (*edgeTable, pos.x, roundToInt (pos.y));
  215. }
  216. void generate (const Font& newFont, const int glyphNumber)
  217. {
  218. font = newFont;
  219. Typeface* const typeface = newFont.getTypeface();
  220. snapToIntegerCoordinate = typeface->isHinted();
  221. glyph = glyphNumber;
  222. const float fontHeight = font.getHeight();
  223. edgeTable = typeface->getEdgeTableForGlyph (glyphNumber,
  224. AffineTransform::scale (fontHeight * font.getHorizontalScale(), fontHeight)
  225. #if JUCE_MAC || JUCE_IOS
  226. .translated (0.0f, -0.5f)
  227. #endif
  228. );
  229. }
  230. Font font;
  231. int glyph, lastAccessCount;
  232. bool snapToIntegerCoordinate;
  233. private:
  234. ScopedPointer <EdgeTable> edgeTable;
  235. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CachedGlyphEdgeTable)
  236. };
  237. //==============================================================================
  238. /** Calculates the alpha values and positions for rendering the edges of a
  239. non-pixel-aligned rectangle.
  240. */
  241. struct FloatRectangleRasterisingInfo
  242. {
  243. FloatRectangleRasterisingInfo (const Rectangle<float>& area)
  244. : left (roundToInt (256.0f * area.getX())),
  245. top (roundToInt (256.0f * area.getY())),
  246. right (roundToInt (256.0f * area.getRight())),
  247. bottom (roundToInt (256.0f * area.getBottom()))
  248. {
  249. if ((top >> 8) == (bottom >> 8))
  250. {
  251. topAlpha = bottom - top;
  252. bottomAlpha = 0;
  253. totalTop = top >> 8;
  254. totalBottom = bottom = top = totalTop + 1;
  255. }
  256. else
  257. {
  258. if ((top & 255) == 0)
  259. {
  260. topAlpha = 0;
  261. top = totalTop = (top >> 8);
  262. }
  263. else
  264. {
  265. topAlpha = 255 - (top & 255);
  266. totalTop = (top >> 8);
  267. top = totalTop + 1;
  268. }
  269. bottomAlpha = bottom & 255;
  270. bottom >>= 8;
  271. totalBottom = bottom + (bottomAlpha != 0 ? 1 : 0);
  272. }
  273. if ((left >> 8) == (right >> 8))
  274. {
  275. leftAlpha = right - left;
  276. rightAlpha = 0;
  277. totalLeft = (left >> 8);
  278. totalRight = right = left = totalLeft + 1;
  279. }
  280. else
  281. {
  282. if ((left & 255) == 0)
  283. {
  284. leftAlpha = 0;
  285. left = totalLeft = (left >> 8);
  286. }
  287. else
  288. {
  289. leftAlpha = 255 - (left & 255);
  290. totalLeft = (left >> 8);
  291. left = totalLeft + 1;
  292. }
  293. rightAlpha = right & 255;
  294. right >>= 8;
  295. totalRight = right + (rightAlpha != 0 ? 1 : 0);
  296. }
  297. }
  298. template <class Callback>
  299. void iterate (Callback& callback) const
  300. {
  301. if (topAlpha != 0) callback (totalLeft, totalTop, totalRight - totalLeft, 1, topAlpha);
  302. if (bottomAlpha != 0) callback (totalLeft, bottom, totalRight - totalLeft, 1, bottomAlpha);
  303. if (leftAlpha != 0) callback (totalLeft, totalTop, 1, totalBottom - totalTop, leftAlpha);
  304. if (rightAlpha != 0) callback (right, totalTop, 1, totalBottom - totalTop, rightAlpha);
  305. callback (left, top, right - left, bottom - top, 255);
  306. }
  307. inline bool isOnePixelWide() const noexcept { return right - left == 1 && leftAlpha + rightAlpha == 0; }
  308. inline int getTopLeftCornerAlpha() const noexcept { return (topAlpha * leftAlpha) >> 8; }
  309. inline int getTopRightCornerAlpha() const noexcept { return (topAlpha * rightAlpha) >> 8; }
  310. inline int getBottomLeftCornerAlpha() const noexcept { return (bottomAlpha * leftAlpha) >> 8; }
  311. inline int getBottomRightCornerAlpha() const noexcept { return (bottomAlpha * rightAlpha) >> 8; }
  312. //==============================================================================
  313. int left, top, right, bottom; // bounds of the solid central area, excluding anti-aliased edges
  314. int totalTop, totalLeft, totalBottom, totalRight; // bounds of the total area, including edges
  315. int topAlpha, leftAlpha, bottomAlpha, rightAlpha; // alpha of each anti-aliased edge
  316. };
  317. //==============================================================================
  318. /** Contains classes for calculating the colour of pixels within various types of gradient. */
  319. namespace GradientPixelIterators
  320. {
  321. /** Iterates the colour of pixels in a linear gradient */
  322. class Linear
  323. {
  324. public:
  325. Linear (const ColourGradient& gradient, const AffineTransform& transform,
  326. const PixelARGB* const colours, const int numColours)
  327. : lookupTable (colours),
  328. numEntries (numColours)
  329. {
  330. jassert (numColours >= 0);
  331. Point<float> p1 (gradient.point1);
  332. Point<float> p2 (gradient.point2);
  333. if (! transform.isIdentity())
  334. {
  335. const Line<float> l (p2, p1);
  336. Point<float> p3 = l.getPointAlongLine (0.0f, 100.0f);
  337. p1.applyTransform (transform);
  338. p2.applyTransform (transform);
  339. p3.applyTransform (transform);
  340. p2 = Line<float> (p2, p3).findNearestPointTo (p1);
  341. }
  342. vertical = std::abs (p1.x - p2.x) < 0.001f;
  343. horizontal = std::abs (p1.y - p2.y) < 0.001f;
  344. if (vertical)
  345. {
  346. scale = roundToInt ((numEntries << (int) numScaleBits) / (double) (p2.y - p1.y));
  347. start = roundToInt (p1.y * scale);
  348. }
  349. else if (horizontal)
  350. {
  351. scale = roundToInt ((numEntries << (int) numScaleBits) / (double) (p2.x - p1.x));
  352. start = roundToInt (p1.x * scale);
  353. }
  354. else
  355. {
  356. grad = (p2.getY() - p1.y) / (double) (p1.x - p2.x);
  357. yTerm = p1.getY() - p1.x / grad;
  358. scale = roundToInt ((numEntries << (int) numScaleBits) / (yTerm * grad - (p2.y * grad - p2.x)));
  359. grad *= scale;
  360. }
  361. }
  362. forcedinline void setY (const int y) noexcept
  363. {
  364. if (vertical)
  365. linePix = lookupTable [jlimit (0, numEntries, (y * scale - start) >> (int) numScaleBits)];
  366. else if (! horizontal)
  367. start = roundToInt ((y - yTerm) * grad);
  368. }
  369. inline PixelARGB getPixel (const int x) const noexcept
  370. {
  371. return vertical ? linePix
  372. : lookupTable [jlimit (0, numEntries, (x * scale - start) >> (int) numScaleBits)];
  373. }
  374. private:
  375. const PixelARGB* const lookupTable;
  376. const int numEntries;
  377. PixelARGB linePix;
  378. int start, scale;
  379. double grad, yTerm;
  380. bool vertical, horizontal;
  381. enum { numScaleBits = 12 };
  382. JUCE_DECLARE_NON_COPYABLE (Linear)
  383. };
  384. //==============================================================================
  385. /** Iterates the colour of pixels in a circular radial gradient */
  386. class Radial
  387. {
  388. public:
  389. Radial (const ColourGradient& gradient, const AffineTransform&,
  390. const PixelARGB* const colours, const int numColours)
  391. : lookupTable (colours),
  392. numEntries (numColours),
  393. gx1 (gradient.point1.x),
  394. gy1 (gradient.point1.y)
  395. {
  396. jassert (numColours >= 0);
  397. const Point<float> diff (gradient.point1 - gradient.point2);
  398. maxDist = diff.x * diff.x + diff.y * diff.y;
  399. invScale = numEntries / std::sqrt (maxDist);
  400. jassert (roundToInt (std::sqrt (maxDist) * invScale) <= numEntries);
  401. }
  402. forcedinline void setY (const int y) noexcept
  403. {
  404. dy = y - gy1;
  405. dy *= dy;
  406. }
  407. inline PixelARGB getPixel (const int px) const noexcept
  408. {
  409. double x = px - gx1;
  410. x *= x;
  411. x += dy;
  412. return lookupTable [x >= maxDist ? numEntries : roundToInt (std::sqrt (x) * invScale)];
  413. }
  414. protected:
  415. const PixelARGB* const lookupTable;
  416. const int numEntries;
  417. const double gx1, gy1;
  418. double maxDist, invScale, dy;
  419. JUCE_DECLARE_NON_COPYABLE (Radial)
  420. };
  421. //==============================================================================
  422. /** Iterates the colour of pixels in a skewed radial gradient */
  423. class TransformedRadial : public Radial
  424. {
  425. public:
  426. TransformedRadial (const ColourGradient& gradient, const AffineTransform& transform,
  427. const PixelARGB* const colours, const int numColours)
  428. : Radial (gradient, transform, colours, numColours),
  429. inverseTransform (transform.inverted())
  430. {
  431. tM10 = inverseTransform.mat10;
  432. tM00 = inverseTransform.mat00;
  433. }
  434. forcedinline void setY (const int y) noexcept
  435. {
  436. lineYM01 = inverseTransform.mat01 * y + inverseTransform.mat02 - gx1;
  437. lineYM11 = inverseTransform.mat11 * y + inverseTransform.mat12 - gy1;
  438. }
  439. inline PixelARGB getPixel (const int px) const noexcept
  440. {
  441. double x = px;
  442. const double y = tM10 * x + lineYM11;
  443. x = tM00 * x + lineYM01;
  444. x *= x;
  445. x += y * y;
  446. if (x >= maxDist)
  447. return lookupTable [numEntries];
  448. return lookupTable [jmin (numEntries, roundToInt (std::sqrt (x) * invScale))];
  449. }
  450. private:
  451. double tM10, tM00, lineYM01, lineYM11;
  452. const AffineTransform inverseTransform;
  453. JUCE_DECLARE_NON_COPYABLE (TransformedRadial)
  454. };
  455. }
  456. #define JUCE_PERFORM_PIXEL_OP_LOOP(op) \
  457. { \
  458. const int destStride = destData.pixelStride; \
  459. do { dest->op; dest = addBytesToPointer (dest, destStride); } while (--width > 0); \
  460. }
  461. //==============================================================================
  462. /** Contains classes for filling edge tables with various fill types. */
  463. namespace EdgeTableFillers
  464. {
  465. /** Fills an edge-table with a solid colour. */
  466. template <class PixelType, bool replaceExisting = false>
  467. class SolidColour
  468. {
  469. public:
  470. SolidColour (const Image::BitmapData& image, const PixelARGB colour)
  471. : destData (image), sourceColour (colour)
  472. {
  473. if (sizeof (PixelType) == 3 && destData.pixelStride == sizeof (PixelType))
  474. {
  475. areRGBComponentsEqual = sourceColour.getRed() == sourceColour.getGreen()
  476. && sourceColour.getGreen() == sourceColour.getBlue();
  477. filler[0].set (sourceColour);
  478. filler[1].set (sourceColour);
  479. filler[2].set (sourceColour);
  480. filler[3].set (sourceColour);
  481. }
  482. }
  483. forcedinline void setEdgeTableYPos (const int y) noexcept
  484. {
  485. linePixels = (PixelType*) destData.getLinePointer (y);
  486. }
  487. forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) const noexcept
  488. {
  489. if (replaceExisting)
  490. getPixel (x)->set (sourceColour);
  491. else
  492. getPixel (x)->blend (sourceColour, (uint32) alphaLevel);
  493. }
  494. forcedinline void handleEdgeTablePixelFull (const int x) const noexcept
  495. {
  496. if (replaceExisting)
  497. getPixel (x)->set (sourceColour);
  498. else
  499. getPixel (x)->blend (sourceColour);
  500. }
  501. forcedinline void handleEdgeTableLine (const int x, const int width, const int alphaLevel) const noexcept
  502. {
  503. PixelARGB p (sourceColour);
  504. p.multiplyAlpha (alphaLevel);
  505. PixelType* dest = getPixel (x);
  506. if (replaceExisting || p.getAlpha() >= 0xff)
  507. replaceLine (dest, p, width);
  508. else
  509. blendLine (dest, p, width);
  510. }
  511. forcedinline void handleEdgeTableLineFull (const int x, const int width) const noexcept
  512. {
  513. PixelType* dest = getPixel (x);
  514. if (replaceExisting || sourceColour.getAlpha() >= 0xff)
  515. replaceLine (dest, sourceColour, width);
  516. else
  517. blendLine (dest, sourceColour, width);
  518. }
  519. private:
  520. const Image::BitmapData& destData;
  521. PixelType* linePixels;
  522. PixelARGB sourceColour;
  523. PixelRGB filler [4];
  524. bool areRGBComponentsEqual;
  525. forcedinline PixelType* getPixel (const int x) const noexcept
  526. {
  527. return addBytesToPointer (linePixels, x * destData.pixelStride);
  528. }
  529. inline void blendLine (PixelType* dest, const PixelARGB colour, int width) const noexcept
  530. {
  531. JUCE_PERFORM_PIXEL_OP_LOOP (blend (colour))
  532. }
  533. forcedinline void replaceLine (PixelRGB* dest, const PixelARGB colour, int width) const noexcept
  534. {
  535. if (destData.pixelStride == sizeof (*dest))
  536. {
  537. if (areRGBComponentsEqual) // if all the component values are the same, we can cheat..
  538. {
  539. memset (dest, colour.getRed(), (size_t) width * 3);
  540. }
  541. else
  542. {
  543. if (width >> 5)
  544. {
  545. const int* const intFiller = reinterpret_cast<const int*> (filler);
  546. while (width > 8 && (((pointer_sized_int) dest) & 7) != 0)
  547. {
  548. dest->set (colour);
  549. ++dest;
  550. --width;
  551. }
  552. while (width > 4)
  553. {
  554. int* d = reinterpret_cast<int*> (dest);
  555. *d++ = intFiller[0];
  556. *d++ = intFiller[1];
  557. *d++ = intFiller[2];
  558. dest = reinterpret_cast<PixelRGB*> (d);
  559. width -= 4;
  560. }
  561. }
  562. while (--width >= 0)
  563. {
  564. dest->set (colour);
  565. ++dest;
  566. }
  567. }
  568. }
  569. else
  570. {
  571. JUCE_PERFORM_PIXEL_OP_LOOP (set (colour))
  572. }
  573. }
  574. forcedinline void replaceLine (PixelAlpha* dest, const PixelARGB colour, int width) const noexcept
  575. {
  576. if (destData.pixelStride == sizeof (*dest))
  577. memset (dest, colour.getAlpha(), (size_t) width);
  578. else
  579. JUCE_PERFORM_PIXEL_OP_LOOP (setAlpha (colour.getAlpha()))
  580. }
  581. forcedinline void replaceLine (PixelARGB* dest, const PixelARGB colour, int width) const noexcept
  582. {
  583. JUCE_PERFORM_PIXEL_OP_LOOP (set (colour))
  584. }
  585. JUCE_DECLARE_NON_COPYABLE (SolidColour)
  586. };
  587. //==============================================================================
  588. /** Fills an edge-table with a gradient. */
  589. template <class PixelType, class GradientType>
  590. class Gradient : public GradientType
  591. {
  592. public:
  593. Gradient (const Image::BitmapData& dest, const ColourGradient& gradient, const AffineTransform& transform,
  594. const PixelARGB* const colours, const int numColours)
  595. : GradientType (gradient, transform, colours, numColours - 1),
  596. destData (dest)
  597. {
  598. }
  599. forcedinline void setEdgeTableYPos (const int y) noexcept
  600. {
  601. linePixels = (PixelType*) destData.getLinePointer (y);
  602. GradientType::setY (y);
  603. }
  604. forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) const noexcept
  605. {
  606. getPixel (x)->blend (GradientType::getPixel (x), (uint32) alphaLevel);
  607. }
  608. forcedinline void handleEdgeTablePixelFull (const int x) const noexcept
  609. {
  610. getPixel (x)->blend (GradientType::getPixel (x));
  611. }
  612. void handleEdgeTableLine (int x, int width, const int alphaLevel) const noexcept
  613. {
  614. PixelType* dest = getPixel (x);
  615. if (alphaLevel < 0xff)
  616. JUCE_PERFORM_PIXEL_OP_LOOP (blend (GradientType::getPixel (x++), (uint32) alphaLevel))
  617. else
  618. JUCE_PERFORM_PIXEL_OP_LOOP (blend (GradientType::getPixel (x++)))
  619. }
  620. void handleEdgeTableLineFull (int x, int width) const noexcept
  621. {
  622. PixelType* dest = getPixel (x);
  623. JUCE_PERFORM_PIXEL_OP_LOOP (blend (GradientType::getPixel (x++)))
  624. }
  625. private:
  626. const Image::BitmapData& destData;
  627. PixelType* linePixels;
  628. forcedinline PixelType* getPixel (const int x) const noexcept
  629. {
  630. return addBytesToPointer (linePixels, x * destData.pixelStride);
  631. }
  632. JUCE_DECLARE_NON_COPYABLE (Gradient)
  633. };
  634. //==============================================================================
  635. /** Fills an edge-table with a non-transformed image. */
  636. template <class DestPixelType, class SrcPixelType, bool repeatPattern>
  637. class ImageFill
  638. {
  639. public:
  640. ImageFill (const Image::BitmapData& dest, const Image::BitmapData& src,
  641. const int alpha, const int x, const int y)
  642. : destData (dest),
  643. srcData (src),
  644. extraAlpha (alpha + 1),
  645. xOffset (repeatPattern ? negativeAwareModulo (x, src.width) - src.width : x),
  646. yOffset (repeatPattern ? negativeAwareModulo (y, src.height) - src.height : y)
  647. {
  648. }
  649. forcedinline void setEdgeTableYPos (int y) noexcept
  650. {
  651. linePixels = (DestPixelType*) destData.getLinePointer (y);
  652. y -= yOffset;
  653. if (repeatPattern)
  654. {
  655. jassert (y >= 0);
  656. y %= srcData.height;
  657. }
  658. sourceLineStart = (SrcPixelType*) srcData.getLinePointer (y);
  659. }
  660. forcedinline void handleEdgeTablePixel (const int x, int alphaLevel) const noexcept
  661. {
  662. alphaLevel = (alphaLevel * extraAlpha) >> 8;
  663. getDestPixel (x)->blend (*getSrcPixel (repeatPattern ? ((x - xOffset) % srcData.width) : (x - xOffset)), (uint32) alphaLevel);
  664. }
  665. forcedinline void handleEdgeTablePixelFull (const int x) const noexcept
  666. {
  667. getDestPixel (x)->blend (*getSrcPixel (repeatPattern ? ((x - xOffset) % srcData.width) : (x - xOffset)), (uint32) extraAlpha);
  668. }
  669. void handleEdgeTableLine (int x, int width, int alphaLevel) const noexcept
  670. {
  671. DestPixelType* dest = getDestPixel (x);
  672. alphaLevel = (alphaLevel * extraAlpha) >> 8;
  673. x -= xOffset;
  674. jassert (repeatPattern || (x >= 0 && x + width <= srcData.width));
  675. if (alphaLevel < 0xfe)
  676. {
  677. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*getSrcPixel (repeatPattern ? (x++ % srcData.width) : x++), (uint32) alphaLevel))
  678. }
  679. else
  680. {
  681. if (repeatPattern)
  682. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*getSrcPixel (x++ % srcData.width)))
  683. else
  684. copyRow (dest, getSrcPixel (x), width);
  685. }
  686. }
  687. void handleEdgeTableLineFull (int x, int width) const noexcept
  688. {
  689. DestPixelType* dest = getDestPixel (x);
  690. x -= xOffset;
  691. jassert (repeatPattern || (x >= 0 && x + width <= srcData.width));
  692. if (extraAlpha < 0xfe)
  693. {
  694. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*getSrcPixel (repeatPattern ? (x++ % srcData.width) : x++), (uint32) extraAlpha))
  695. }
  696. else
  697. {
  698. if (repeatPattern)
  699. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*getSrcPixel (x++ % srcData.width)))
  700. else
  701. copyRow (dest, getSrcPixel (x), width);
  702. }
  703. }
  704. void clipEdgeTableLine (EdgeTable& et, int x, int y, int width)
  705. {
  706. jassert (x - xOffset >= 0 && x + width - xOffset <= srcData.width);
  707. SrcPixelType* s = (SrcPixelType*) srcData.getLinePointer (y - yOffset);
  708. uint8* mask = (uint8*) (s + x - xOffset);
  709. if (sizeof (SrcPixelType) == sizeof (PixelARGB))
  710. mask += PixelARGB::indexA;
  711. et.clipLineToMask (x, y, mask, sizeof (SrcPixelType), width);
  712. }
  713. private:
  714. const Image::BitmapData& destData;
  715. const Image::BitmapData& srcData;
  716. const int extraAlpha, xOffset, yOffset;
  717. DestPixelType* linePixels;
  718. SrcPixelType* sourceLineStart;
  719. forcedinline DestPixelType* getDestPixel (int const x) const noexcept
  720. {
  721. return addBytesToPointer (linePixels, x * destData.pixelStride);
  722. }
  723. forcedinline SrcPixelType const* getSrcPixel (int const x) const noexcept
  724. {
  725. return addBytesToPointer (sourceLineStart, x * srcData.pixelStride);
  726. }
  727. forcedinline void copyRow (DestPixelType* dest, SrcPixelType const* src, int width) const noexcept
  728. {
  729. if (srcData.pixelStride == 3 && destData.pixelStride == 3)
  730. {
  731. memcpy (dest, src, sizeof (PixelRGB) * (size_t) width);
  732. }
  733. else
  734. {
  735. const int destStride = destData.pixelStride;
  736. const int srcStride = srcData.pixelStride;
  737. do
  738. {
  739. dest->blend (*src);
  740. dest = addBytesToPointer (dest, destStride);
  741. src = addBytesToPointer (src, srcStride);
  742. } while (--width > 0);
  743. }
  744. }
  745. JUCE_DECLARE_NON_COPYABLE (ImageFill)
  746. };
  747. //==============================================================================
  748. /** Fills an edge-table with a transformed image. */
  749. template <class DestPixelType, class SrcPixelType, bool repeatPattern>
  750. class TransformedImageFill
  751. {
  752. public:
  753. TransformedImageFill (const Image::BitmapData& dest, const Image::BitmapData& src,
  754. const AffineTransform& transform, const int alpha, const Graphics::ResamplingQuality q)
  755. : interpolator (transform,
  756. q != Graphics::lowResamplingQuality ? 0.5f : 0.0f,
  757. q != Graphics::lowResamplingQuality ? -128 : 0),
  758. destData (dest),
  759. srcData (src),
  760. extraAlpha (alpha + 1),
  761. quality (q),
  762. maxX (src.width - 1),
  763. maxY (src.height - 1),
  764. scratchSize (2048)
  765. {
  766. scratchBuffer.malloc (scratchSize);
  767. }
  768. forcedinline void setEdgeTableYPos (const int newY) noexcept
  769. {
  770. y = newY;
  771. linePixels = (DestPixelType*) destData.getLinePointer (newY);
  772. }
  773. forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) noexcept
  774. {
  775. SrcPixelType p;
  776. generate (&p, x, 1);
  777. getDestPixel (x)->blend (p, (uint32) (alphaLevel * extraAlpha) >> 8);
  778. }
  779. forcedinline void handleEdgeTablePixelFull (const int x) noexcept
  780. {
  781. SrcPixelType p;
  782. generate (&p, x, 1);
  783. getDestPixel (x)->blend (p, (uint32) extraAlpha);
  784. }
  785. void handleEdgeTableLine (const int x, int width, int alphaLevel) noexcept
  786. {
  787. if (width > (int) scratchSize)
  788. {
  789. scratchSize = (size_t) width;
  790. scratchBuffer.malloc (scratchSize);
  791. }
  792. SrcPixelType* span = scratchBuffer;
  793. generate (span, x, width);
  794. DestPixelType* dest = getDestPixel (x);
  795. alphaLevel *= extraAlpha;
  796. alphaLevel >>= 8;
  797. if (alphaLevel < 0xfe)
  798. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*span++, (uint32) alphaLevel))
  799. else
  800. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*span++))
  801. }
  802. forcedinline void handleEdgeTableLineFull (const int x, int width) noexcept
  803. {
  804. handleEdgeTableLine (x, width, 255);
  805. }
  806. void clipEdgeTableLine (EdgeTable& et, int x, int y_, int width)
  807. {
  808. if (width > (int) scratchSize)
  809. {
  810. scratchSize = (size_t) width;
  811. scratchBuffer.malloc (scratchSize);
  812. }
  813. y = y_;
  814. generate (scratchBuffer.getData(), x, width);
  815. et.clipLineToMask (x, y_,
  816. reinterpret_cast<uint8*> (scratchBuffer.getData()) + SrcPixelType::indexA,
  817. sizeof (SrcPixelType), width);
  818. }
  819. private:
  820. forcedinline DestPixelType* getDestPixel (const int x) const noexcept
  821. {
  822. return addBytesToPointer (linePixels, x * destData.pixelStride);
  823. }
  824. //==============================================================================
  825. template <class PixelType>
  826. void generate (PixelType* dest, const int x, int numPixels) noexcept
  827. {
  828. this->interpolator.setStartOfLine ((float) x, (float) y, numPixels);
  829. do
  830. {
  831. int hiResX, hiResY;
  832. this->interpolator.next (hiResX, hiResY);
  833. int loResX = hiResX >> 8;
  834. int loResY = hiResY >> 8;
  835. if (repeatPattern)
  836. {
  837. loResX = negativeAwareModulo (loResX, srcData.width);
  838. loResY = negativeAwareModulo (loResY, srcData.height);
  839. }
  840. if (quality != Graphics::lowResamplingQuality)
  841. {
  842. if (isPositiveAndBelow (loResX, maxX))
  843. {
  844. if (isPositiveAndBelow (loResY, maxY))
  845. {
  846. // In the centre of the image..
  847. render4PixelAverage (dest, this->srcData.getPixelPointer (loResX, loResY),
  848. hiResX & 255, hiResY & 255);
  849. ++dest;
  850. continue;
  851. }
  852. if (! repeatPattern)
  853. {
  854. // At a top or bottom edge..
  855. if (loResY < 0)
  856. render2PixelAverageX (dest, this->srcData.getPixelPointer (loResX, 0), hiResX & 255);
  857. else
  858. render2PixelAverageX (dest, this->srcData.getPixelPointer (loResX, maxY), hiResX & 255);
  859. ++dest;
  860. continue;
  861. }
  862. }
  863. else
  864. {
  865. if (isPositiveAndBelow (loResY, maxY) && ! repeatPattern)
  866. {
  867. // At a left or right hand edge..
  868. if (loResX < 0)
  869. render2PixelAverageY (dest, this->srcData.getPixelPointer (0, loResY), hiResY & 255);
  870. else
  871. render2PixelAverageY (dest, this->srcData.getPixelPointer (maxX, loResY), hiResY & 255);
  872. ++dest;
  873. continue;
  874. }
  875. }
  876. }
  877. if (! repeatPattern)
  878. {
  879. if (loResX < 0) loResX = 0;
  880. if (loResY < 0) loResY = 0;
  881. if (loResX > maxX) loResX = maxX;
  882. if (loResY > maxY) loResY = maxY;
  883. }
  884. dest->set (*(const PixelType*) this->srcData.getPixelPointer (loResX, loResY));
  885. ++dest;
  886. } while (--numPixels > 0);
  887. }
  888. //==============================================================================
  889. void render4PixelAverage (PixelARGB* const dest, const uint8* src, const int subPixelX, const int subPixelY) noexcept
  890. {
  891. uint32 c[4] = { 256 * 128, 256 * 128, 256 * 128, 256 * 128 };
  892. uint32 weight = (uint32) ((256 - subPixelX) * (256 - subPixelY));
  893. c[0] += weight * src[0];
  894. c[1] += weight * src[1];
  895. c[2] += weight * src[2];
  896. c[3] += weight * src[3];
  897. src += this->srcData.pixelStride;
  898. weight = (uint32) (subPixelX * (256 - subPixelY));
  899. c[0] += weight * src[0];
  900. c[1] += weight * src[1];
  901. c[2] += weight * src[2];
  902. c[3] += weight * src[3];
  903. src += this->srcData.lineStride;
  904. weight = (uint32) (subPixelX * subPixelY);
  905. c[0] += weight * src[0];
  906. c[1] += weight * src[1];
  907. c[2] += weight * src[2];
  908. c[3] += weight * src[3];
  909. src -= this->srcData.pixelStride;
  910. weight = (uint32) ((256 - subPixelX) * subPixelY);
  911. c[0] += weight * src[0];
  912. c[1] += weight * src[1];
  913. c[2] += weight * src[2];
  914. c[3] += weight * src[3];
  915. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 16),
  916. (uint8) (c[PixelARGB::indexR] >> 16),
  917. (uint8) (c[PixelARGB::indexG] >> 16),
  918. (uint8) (c[PixelARGB::indexB] >> 16));
  919. }
  920. void render2PixelAverageX (PixelARGB* const dest, const uint8* src, const uint32 subPixelX) noexcept
  921. {
  922. uint32 c[4] = { 128, 128, 128, 128 };
  923. uint32 weight = 256 - subPixelX;
  924. c[0] += weight * src[0];
  925. c[1] += weight * src[1];
  926. c[2] += weight * src[2];
  927. c[3] += weight * src[3];
  928. src += this->srcData.pixelStride;
  929. weight = subPixelX;
  930. c[0] += weight * src[0];
  931. c[1] += weight * src[1];
  932. c[2] += weight * src[2];
  933. c[3] += weight * src[3];
  934. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 8),
  935. (uint8) (c[PixelARGB::indexR] >> 8),
  936. (uint8) (c[PixelARGB::indexG] >> 8),
  937. (uint8) (c[PixelARGB::indexB] >> 8));
  938. }
  939. void render2PixelAverageY (PixelARGB* const dest, const uint8* src, const uint32 subPixelY) noexcept
  940. {
  941. uint32 c[4] = { 128, 128, 128, 128 };
  942. uint32 weight = 256 - subPixelY;
  943. c[0] += weight * src[0];
  944. c[1] += weight * src[1];
  945. c[2] += weight * src[2];
  946. c[3] += weight * src[3];
  947. src += this->srcData.lineStride;
  948. weight = subPixelY;
  949. c[0] += weight * src[0];
  950. c[1] += weight * src[1];
  951. c[2] += weight * src[2];
  952. c[3] += weight * src[3];
  953. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 8),
  954. (uint8) (c[PixelARGB::indexR] >> 8),
  955. (uint8) (c[PixelARGB::indexG] >> 8),
  956. (uint8) (c[PixelARGB::indexB] >> 8));
  957. }
  958. //==============================================================================
  959. void render4PixelAverage (PixelRGB* const dest, const uint8* src, const uint32 subPixelX, const uint32 subPixelY) noexcept
  960. {
  961. uint32 c[3] = { 256 * 128, 256 * 128, 256 * 128 };
  962. uint32 weight = (256 - subPixelX) * (256 - subPixelY);
  963. c[0] += weight * src[0];
  964. c[1] += weight * src[1];
  965. c[2] += weight * src[2];
  966. src += this->srcData.pixelStride;
  967. weight = subPixelX * (256 - subPixelY);
  968. c[0] += weight * src[0];
  969. c[1] += weight * src[1];
  970. c[2] += weight * src[2];
  971. src += this->srcData.lineStride;
  972. weight = subPixelX * subPixelY;
  973. c[0] += weight * src[0];
  974. c[1] += weight * src[1];
  975. c[2] += weight * src[2];
  976. src -= this->srcData.pixelStride;
  977. weight = (256 - subPixelX) * subPixelY;
  978. c[0] += weight * src[0];
  979. c[1] += weight * src[1];
  980. c[2] += weight * src[2];
  981. dest->setARGB ((uint8) 255,
  982. (uint8) (c[PixelRGB::indexR] >> 16),
  983. (uint8) (c[PixelRGB::indexG] >> 16),
  984. (uint8) (c[PixelRGB::indexB] >> 16));
  985. }
  986. void render2PixelAverageX (PixelRGB* const dest, const uint8* src, const uint32 subPixelX) noexcept
  987. {
  988. uint32 c[3] = { 128, 128, 128 };
  989. const uint32 weight = 256 - subPixelX;
  990. c[0] += weight * src[0];
  991. c[1] += weight * src[1];
  992. c[2] += weight * src[2];
  993. src += this->srcData.pixelStride;
  994. c[0] += subPixelX * src[0];
  995. c[1] += subPixelX * src[1];
  996. c[2] += subPixelX * src[2];
  997. dest->setARGB ((uint8) 255,
  998. (uint8) (c[PixelRGB::indexR] >> 8),
  999. (uint8) (c[PixelRGB::indexG] >> 8),
  1000. (uint8) (c[PixelRGB::indexB] >> 8));
  1001. }
  1002. void render2PixelAverageY (PixelRGB* const dest, const uint8* src, const uint32 subPixelY) noexcept
  1003. {
  1004. uint32 c[3] = { 128, 128, 128 };
  1005. const uint32 weight = 256 - subPixelY;
  1006. c[0] += weight * src[0];
  1007. c[1] += weight * src[1];
  1008. c[2] += weight * src[2];
  1009. src += this->srcData.lineStride;
  1010. c[0] += subPixelY * src[0];
  1011. c[1] += subPixelY * src[1];
  1012. c[2] += subPixelY * src[2];
  1013. dest->setARGB ((uint8) 255,
  1014. (uint8) (c[PixelRGB::indexR] >> 8),
  1015. (uint8) (c[PixelRGB::indexG] >> 8),
  1016. (uint8) (c[PixelRGB::indexB] >> 8));
  1017. }
  1018. //==============================================================================
  1019. void render4PixelAverage (PixelAlpha* const dest, const uint8* src, const uint32 subPixelX, const uint32 subPixelY) noexcept
  1020. {
  1021. uint32 c = 256 * 128;
  1022. c += src[0] * ((256 - subPixelX) * (256 - subPixelY));
  1023. src += this->srcData.pixelStride;
  1024. c += src[1] * (subPixelX * (256 - subPixelY));
  1025. src += this->srcData.lineStride;
  1026. c += src[1] * (subPixelX * subPixelY);
  1027. src -= this->srcData.pixelStride;
  1028. c += src[0] * ((256 - subPixelX) * subPixelY);
  1029. *((uint8*) dest) = (uint8) (c >> 16);
  1030. }
  1031. void render2PixelAverageX (PixelAlpha* const dest, const uint8* src, const uint32 subPixelX) noexcept
  1032. {
  1033. uint32 c = 128;
  1034. c += src[0] * (256 - subPixelX);
  1035. src += this->srcData.pixelStride;
  1036. c += src[0] * subPixelX;
  1037. *((uint8*) dest) = (uint8) (c >> 8);
  1038. }
  1039. void render2PixelAverageY (PixelAlpha* const dest, const uint8* src, const uint32 subPixelY) noexcept
  1040. {
  1041. uint32 c = 128;
  1042. c += src[0] * (256 - subPixelY);
  1043. src += this->srcData.lineStride;
  1044. c += src[0] * subPixelY;
  1045. *((uint8*) dest) = (uint8) (c >> 8);
  1046. }
  1047. //==============================================================================
  1048. class TransformedImageSpanInterpolator
  1049. {
  1050. public:
  1051. TransformedImageSpanInterpolator (const AffineTransform& transform,
  1052. const float offsetFloat, const int offsetInt) noexcept
  1053. : inverseTransform (transform.inverted()),
  1054. pixelOffset (offsetFloat), pixelOffsetInt (offsetInt)
  1055. {}
  1056. void setStartOfLine (float sx, float sy, const int numPixels) noexcept
  1057. {
  1058. jassert (numPixels > 0);
  1059. sx += pixelOffset;
  1060. sy += pixelOffset;
  1061. float x1 = sx, y1 = sy;
  1062. sx += numPixels;
  1063. inverseTransform.transformPoints (x1, y1, sx, sy);
  1064. xBresenham.set ((int) (x1 * 256.0f), (int) (sx * 256.0f), numPixels, pixelOffsetInt);
  1065. yBresenham.set ((int) (y1 * 256.0f), (int) (sy * 256.0f), numPixels, pixelOffsetInt);
  1066. }
  1067. void next (int& px, int& py) noexcept
  1068. {
  1069. px = xBresenham.n; xBresenham.stepToNext();
  1070. py = yBresenham.n; yBresenham.stepToNext();
  1071. }
  1072. private:
  1073. class BresenhamInterpolator
  1074. {
  1075. public:
  1076. BresenhamInterpolator() noexcept {}
  1077. void set (const int n1, const int n2, const int steps, const int offsetInt) noexcept
  1078. {
  1079. numSteps = steps;
  1080. step = (n2 - n1) / numSteps;
  1081. remainder = modulo = (n2 - n1) % numSteps;
  1082. n = n1 + offsetInt;
  1083. if (modulo <= 0)
  1084. {
  1085. modulo += numSteps;
  1086. remainder += numSteps;
  1087. --step;
  1088. }
  1089. modulo -= numSteps;
  1090. }
  1091. forcedinline void stepToNext() noexcept
  1092. {
  1093. modulo += remainder;
  1094. n += step;
  1095. if (modulo > 0)
  1096. {
  1097. modulo -= numSteps;
  1098. ++n;
  1099. }
  1100. }
  1101. int n;
  1102. private:
  1103. int numSteps, step, modulo, remainder;
  1104. };
  1105. const AffineTransform inverseTransform;
  1106. BresenhamInterpolator xBresenham, yBresenham;
  1107. const float pixelOffset;
  1108. const int pixelOffsetInt;
  1109. JUCE_DECLARE_NON_COPYABLE (TransformedImageSpanInterpolator)
  1110. };
  1111. //==============================================================================
  1112. TransformedImageSpanInterpolator interpolator;
  1113. const Image::BitmapData& destData;
  1114. const Image::BitmapData& srcData;
  1115. const int extraAlpha;
  1116. const Graphics::ResamplingQuality quality;
  1117. const int maxX, maxY;
  1118. int y;
  1119. DestPixelType* linePixels;
  1120. HeapBlock <SrcPixelType> scratchBuffer;
  1121. size_t scratchSize;
  1122. JUCE_DECLARE_NON_COPYABLE (TransformedImageFill)
  1123. };
  1124. //==============================================================================
  1125. template <class Iterator>
  1126. void renderImageTransformed (Iterator& iter, const Image::BitmapData& destData, const Image::BitmapData& srcData,
  1127. const int alpha, const AffineTransform& transform, Graphics::ResamplingQuality quality, bool tiledFill)
  1128. {
  1129. switch (destData.pixelFormat)
  1130. {
  1131. case Image::ARGB:
  1132. switch (srcData.pixelFormat)
  1133. {
  1134. case Image::ARGB:
  1135. if (tiledFill) { TransformedImageFill <PixelARGB, PixelARGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1136. else { TransformedImageFill <PixelARGB, PixelARGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1137. break;
  1138. case Image::RGB:
  1139. if (tiledFill) { TransformedImageFill <PixelARGB, PixelRGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1140. else { TransformedImageFill <PixelARGB, PixelRGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1141. break;
  1142. default:
  1143. if (tiledFill) { TransformedImageFill <PixelARGB, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1144. else { TransformedImageFill <PixelARGB, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1145. break;
  1146. }
  1147. break;
  1148. case Image::RGB:
  1149. switch (srcData.pixelFormat)
  1150. {
  1151. case Image::ARGB:
  1152. if (tiledFill) { TransformedImageFill <PixelRGB, PixelARGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1153. else { TransformedImageFill <PixelRGB, PixelARGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1154. break;
  1155. case Image::RGB:
  1156. if (tiledFill) { TransformedImageFill <PixelRGB, PixelRGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1157. else { TransformedImageFill <PixelRGB, PixelRGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1158. break;
  1159. default:
  1160. if (tiledFill) { TransformedImageFill <PixelRGB, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1161. else { TransformedImageFill <PixelRGB, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1162. break;
  1163. }
  1164. break;
  1165. default:
  1166. switch (srcData.pixelFormat)
  1167. {
  1168. case Image::ARGB:
  1169. if (tiledFill) { TransformedImageFill <PixelAlpha, PixelARGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1170. else { TransformedImageFill <PixelAlpha, PixelARGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1171. break;
  1172. case Image::RGB:
  1173. if (tiledFill) { TransformedImageFill <PixelAlpha, PixelRGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1174. else { TransformedImageFill <PixelAlpha, PixelRGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1175. break;
  1176. default:
  1177. if (tiledFill) { TransformedImageFill <PixelAlpha, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1178. else { TransformedImageFill <PixelAlpha, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1179. break;
  1180. }
  1181. break;
  1182. }
  1183. }
  1184. template <class Iterator>
  1185. void renderImageUntransformed (Iterator& iter, const Image::BitmapData& destData, const Image::BitmapData& srcData, const int alpha, int x, int y, bool tiledFill)
  1186. {
  1187. switch (destData.pixelFormat)
  1188. {
  1189. case Image::ARGB:
  1190. switch (srcData.pixelFormat)
  1191. {
  1192. case Image::ARGB:
  1193. if (tiledFill) { ImageFill <PixelARGB, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1194. else { ImageFill <PixelARGB, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1195. break;
  1196. case Image::RGB:
  1197. if (tiledFill) { ImageFill <PixelARGB, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1198. else { ImageFill <PixelARGB, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1199. break;
  1200. default:
  1201. if (tiledFill) { ImageFill <PixelARGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1202. else { ImageFill <PixelARGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1203. break;
  1204. }
  1205. break;
  1206. case Image::RGB:
  1207. switch (srcData.pixelFormat)
  1208. {
  1209. case Image::ARGB:
  1210. if (tiledFill) { ImageFill <PixelRGB, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1211. else { ImageFill <PixelRGB, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1212. break;
  1213. case Image::RGB:
  1214. if (tiledFill) { ImageFill <PixelRGB, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1215. else { ImageFill <PixelRGB, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1216. break;
  1217. default:
  1218. if (tiledFill) { ImageFill <PixelRGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1219. else { ImageFill <PixelRGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1220. break;
  1221. }
  1222. break;
  1223. default:
  1224. switch (srcData.pixelFormat)
  1225. {
  1226. case Image::ARGB:
  1227. if (tiledFill) { ImageFill <PixelAlpha, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1228. else { ImageFill <PixelAlpha, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1229. break;
  1230. case Image::RGB:
  1231. if (tiledFill) { ImageFill <PixelAlpha, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1232. else { ImageFill <PixelAlpha, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1233. break;
  1234. default:
  1235. if (tiledFill) { ImageFill <PixelAlpha, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1236. else { ImageFill <PixelAlpha, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1237. break;
  1238. }
  1239. break;
  1240. }
  1241. }
  1242. template <class Iterator, class DestPixelType>
  1243. void renderSolidFill (Iterator& iter, const Image::BitmapData& destData, const PixelARGB fillColour, const bool replaceContents, DestPixelType*)
  1244. {
  1245. if (replaceContents)
  1246. {
  1247. EdgeTableFillers::SolidColour <DestPixelType, true> r (destData, fillColour);
  1248. iter.iterate (r);
  1249. }
  1250. else
  1251. {
  1252. EdgeTableFillers::SolidColour <DestPixelType, false> r (destData, fillColour);
  1253. iter.iterate (r);
  1254. }
  1255. }
  1256. template <class Iterator, class DestPixelType>
  1257. void renderGradient (Iterator& iter, const Image::BitmapData& destData, const ColourGradient& g, const AffineTransform& transform,
  1258. const PixelARGB* const lookupTable, const int numLookupEntries, const bool isIdentity, DestPixelType*)
  1259. {
  1260. if (g.isRadial)
  1261. {
  1262. if (isIdentity)
  1263. {
  1264. EdgeTableFillers::Gradient <DestPixelType, GradientPixelIterators::Radial> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1265. iter.iterate (renderer);
  1266. }
  1267. else
  1268. {
  1269. EdgeTableFillers::Gradient <DestPixelType, GradientPixelIterators::TransformedRadial> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1270. iter.iterate (renderer);
  1271. }
  1272. }
  1273. else
  1274. {
  1275. EdgeTableFillers::Gradient <DestPixelType, GradientPixelIterators::Linear> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1276. iter.iterate (renderer);
  1277. }
  1278. }
  1279. }
  1280. //==============================================================================
  1281. template <class SavedStateType>
  1282. struct ClipRegions
  1283. {
  1284. class Base : public SingleThreadedReferenceCountedObject
  1285. {
  1286. public:
  1287. Base() {}
  1288. virtual ~Base() {}
  1289. typedef ReferenceCountedObjectPtr<Base> Ptr;
  1290. virtual Ptr clone() const = 0;
  1291. virtual Ptr applyClipTo (const Ptr& target) const = 0;
  1292. virtual Ptr clipToRectangle (const Rectangle<int>&) = 0;
  1293. virtual Ptr clipToRectangleList (const RectangleList<int>&) = 0;
  1294. virtual Ptr excludeClipRectangle (const Rectangle<int>&) = 0;
  1295. virtual Ptr clipToPath (const Path&, const AffineTransform&) = 0;
  1296. virtual Ptr clipToEdgeTable (const EdgeTable& et) = 0;
  1297. virtual Ptr clipToImageAlpha (const Image&, const AffineTransform&, const Graphics::ResamplingQuality) = 0;
  1298. virtual void translate (Point<int> delta) = 0;
  1299. virtual bool clipRegionIntersects (const Rectangle<int>&) const = 0;
  1300. virtual Rectangle<int> getClipBounds() const = 0;
  1301. virtual void fillRectWithColour (SavedStateType&, const Rectangle<int>&, const PixelARGB colour, bool replaceContents) const = 0;
  1302. virtual void fillRectWithColour (SavedStateType&, const Rectangle<float>&, const PixelARGB colour) const = 0;
  1303. virtual void fillAllWithColour (SavedStateType&, const PixelARGB colour, bool replaceContents) const = 0;
  1304. virtual void fillAllWithGradient (SavedStateType&, ColourGradient&, const AffineTransform&, bool isIdentity) const = 0;
  1305. virtual void renderImageTransformed (SavedStateType&, const Image&, const int alpha, const AffineTransform&, Graphics::ResamplingQuality, bool tiledFill) const = 0;
  1306. virtual void renderImageUntransformed (SavedStateType&, const Image&, const int alpha, int x, int y, bool tiledFill) const = 0;
  1307. };
  1308. //==============================================================================
  1309. class EdgeTableRegion : public Base
  1310. {
  1311. public:
  1312. EdgeTableRegion (const EdgeTable& e) : edgeTable (e) {}
  1313. EdgeTableRegion (const Rectangle<int>& r) : edgeTable (r) {}
  1314. EdgeTableRegion (const Rectangle<float>& r) : edgeTable (r) {}
  1315. EdgeTableRegion (const RectangleList<int>& r) : edgeTable (r) {}
  1316. EdgeTableRegion (const RectangleList<float>& r) : edgeTable (r) {}
  1317. EdgeTableRegion (const Rectangle<int>& bounds, const Path& p, const AffineTransform& t) : edgeTable (bounds, p, t) {}
  1318. EdgeTableRegion (const EdgeTableRegion& other) : Base(), edgeTable (other.edgeTable) {}
  1319. typedef typename Base::Ptr Ptr;
  1320. Ptr clone() const { return new EdgeTableRegion (*this); }
  1321. Ptr applyClipTo (const Ptr& target) const { return target->clipToEdgeTable (edgeTable); }
  1322. Ptr clipToRectangle (const Rectangle<int>& r)
  1323. {
  1324. edgeTable.clipToRectangle (r);
  1325. return edgeTable.isEmpty() ? nullptr : this;
  1326. }
  1327. Ptr clipToRectangleList (const RectangleList<int>& r)
  1328. {
  1329. RectangleList<int> inverse (edgeTable.getMaximumBounds());
  1330. if (inverse.subtract (r))
  1331. for (const Rectangle<int>* i = inverse.begin(), * const e = inverse.end(); i != e; ++i)
  1332. edgeTable.excludeRectangle (*i);
  1333. return edgeTable.isEmpty() ? nullptr : this;
  1334. }
  1335. Ptr excludeClipRectangle (const Rectangle<int>& r)
  1336. {
  1337. edgeTable.excludeRectangle (r);
  1338. return edgeTable.isEmpty() ? nullptr : this;
  1339. }
  1340. Ptr clipToPath (const Path& p, const AffineTransform& transform)
  1341. {
  1342. EdgeTable et (edgeTable.getMaximumBounds(), p, transform);
  1343. edgeTable.clipToEdgeTable (et);
  1344. return edgeTable.isEmpty() ? nullptr : this;
  1345. }
  1346. Ptr clipToEdgeTable (const EdgeTable& et)
  1347. {
  1348. edgeTable.clipToEdgeTable (et);
  1349. return edgeTable.isEmpty() ? nullptr : this;
  1350. }
  1351. Ptr clipToImageAlpha (const Image& image, const AffineTransform& transform, const Graphics::ResamplingQuality quality)
  1352. {
  1353. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  1354. if (transform.isOnlyTranslation())
  1355. {
  1356. // If our translation doesn't involve any distortion, just use a simple blit..
  1357. const int tx = (int) (transform.getTranslationX() * 256.0f);
  1358. const int ty = (int) (transform.getTranslationY() * 256.0f);
  1359. if (quality == Graphics::lowResamplingQuality || ((tx | ty) & 224) == 0)
  1360. {
  1361. const int imageX = ((tx + 128) >> 8);
  1362. const int imageY = ((ty + 128) >> 8);
  1363. if (image.getFormat() == Image::ARGB)
  1364. straightClipImage (srcData, imageX, imageY, (PixelARGB*) 0);
  1365. else
  1366. straightClipImage (srcData, imageX, imageY, (PixelAlpha*) 0);
  1367. return edgeTable.isEmpty() ? nullptr : this;
  1368. }
  1369. }
  1370. if (transform.isSingularity())
  1371. return Ptr();
  1372. {
  1373. Path p;
  1374. p.addRectangle (0, 0, (float) srcData.width, (float) srcData.height);
  1375. EdgeTable et2 (edgeTable.getMaximumBounds(), p, transform);
  1376. edgeTable.clipToEdgeTable (et2);
  1377. }
  1378. if (! edgeTable.isEmpty())
  1379. {
  1380. if (image.getFormat() == Image::ARGB)
  1381. transformedClipImage (srcData, transform, quality, (PixelARGB*) 0);
  1382. else
  1383. transformedClipImage (srcData, transform, quality, (PixelAlpha*) 0);
  1384. }
  1385. return edgeTable.isEmpty() ? nullptr : this;
  1386. }
  1387. void translate (Point<int> delta)
  1388. {
  1389. edgeTable.translate ((float) delta.x, delta.y);
  1390. }
  1391. bool clipRegionIntersects (const Rectangle<int>& r) const
  1392. {
  1393. return edgeTable.getMaximumBounds().intersects (r);
  1394. }
  1395. Rectangle<int> getClipBounds() const
  1396. {
  1397. return edgeTable.getMaximumBounds();
  1398. }
  1399. void fillRectWithColour (SavedStateType& state, const Rectangle<int>& area, const PixelARGB colour, bool replaceContents) const
  1400. {
  1401. const Rectangle<int> totalClip (edgeTable.getMaximumBounds());
  1402. const Rectangle<int> clipped (totalClip.getIntersection (area));
  1403. if (! clipped.isEmpty())
  1404. {
  1405. EdgeTableRegion et (clipped);
  1406. et.edgeTable.clipToEdgeTable (edgeTable);
  1407. state.fillWithSolidColour (et.edgeTable, colour, replaceContents);
  1408. }
  1409. }
  1410. void fillRectWithColour (SavedStateType& state, const Rectangle<float>& area, const PixelARGB colour) const
  1411. {
  1412. const Rectangle<float> totalClip (edgeTable.getMaximumBounds().toFloat());
  1413. const Rectangle<float> clipped (totalClip.getIntersection (area));
  1414. if (! clipped.isEmpty())
  1415. {
  1416. EdgeTableRegion et (clipped);
  1417. et.edgeTable.clipToEdgeTable (edgeTable);
  1418. state.fillWithSolidColour (et.edgeTable, colour, false);
  1419. }
  1420. }
  1421. void fillAllWithColour (SavedStateType& state, const PixelARGB colour, bool replaceContents) const
  1422. {
  1423. state.fillWithSolidColour (edgeTable, colour, replaceContents);
  1424. }
  1425. void fillAllWithGradient (SavedStateType& state, ColourGradient& gradient, const AffineTransform& transform, bool isIdentity) const
  1426. {
  1427. state.fillWithGradient (edgeTable, gradient, transform, isIdentity);
  1428. }
  1429. void renderImageTransformed (SavedStateType& state, const Image& src, const int alpha, const AffineTransform& transform, Graphics::ResamplingQuality quality, bool tiledFill) const
  1430. {
  1431. state.renderImageTransformed (edgeTable, src, alpha, transform, quality, tiledFill);
  1432. }
  1433. void renderImageUntransformed (SavedStateType& state, const Image& src, const int alpha, int x, int y, bool tiledFill) const
  1434. {
  1435. state.renderImageUntransformed (edgeTable, src, alpha, x, y, tiledFill);
  1436. }
  1437. EdgeTable edgeTable;
  1438. private:
  1439. template <class SrcPixelType>
  1440. void transformedClipImage (const Image::BitmapData& srcData, const AffineTransform& transform, const Graphics::ResamplingQuality quality, const SrcPixelType*)
  1441. {
  1442. EdgeTableFillers::TransformedImageFill <SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, transform, 255, quality);
  1443. for (int y = 0; y < edgeTable.getMaximumBounds().getHeight(); ++y)
  1444. renderer.clipEdgeTableLine (edgeTable, edgeTable.getMaximumBounds().getX(), y + edgeTable.getMaximumBounds().getY(),
  1445. edgeTable.getMaximumBounds().getWidth());
  1446. }
  1447. template <class SrcPixelType>
  1448. void straightClipImage (const Image::BitmapData& srcData, int imageX, int imageY, const SrcPixelType*)
  1449. {
  1450. Rectangle<int> r (imageX, imageY, srcData.width, srcData.height);
  1451. edgeTable.clipToRectangle (r);
  1452. EdgeTableFillers::ImageFill <SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, 255, imageX, imageY);
  1453. for (int y = 0; y < r.getHeight(); ++y)
  1454. renderer.clipEdgeTableLine (edgeTable, r.getX(), y + r.getY(), r.getWidth());
  1455. }
  1456. EdgeTableRegion& operator= (const EdgeTableRegion&);
  1457. };
  1458. //==============================================================================
  1459. class RectangleListRegion : public Base
  1460. {
  1461. public:
  1462. RectangleListRegion (const Rectangle<int>& r) : clip (r) {}
  1463. RectangleListRegion (const RectangleList<int>& r) : clip (r) {}
  1464. RectangleListRegion (const RectangleListRegion& other) : Base(), clip (other.clip) {}
  1465. typedef typename Base::Ptr Ptr;
  1466. Ptr clone() const { return new RectangleListRegion (*this); }
  1467. Ptr applyClipTo (const Ptr& target) const { return target->clipToRectangleList (clip); }
  1468. Ptr clipToRectangle (const Rectangle<int>& r)
  1469. {
  1470. clip.clipTo (r);
  1471. return clip.isEmpty() ? nullptr : this;
  1472. }
  1473. Ptr clipToRectangleList (const RectangleList<int>& r)
  1474. {
  1475. clip.clipTo (r);
  1476. return clip.isEmpty() ? nullptr : this;
  1477. }
  1478. Ptr excludeClipRectangle (const Rectangle<int>& r)
  1479. {
  1480. clip.subtract (r);
  1481. return clip.isEmpty() ? nullptr : this;
  1482. }
  1483. Ptr clipToPath (const Path& p, const AffineTransform& transform) { return toEdgeTable()->clipToPath (p, transform); }
  1484. Ptr clipToEdgeTable (const EdgeTable& et) { return toEdgeTable()->clipToEdgeTable (et); }
  1485. Ptr clipToImageAlpha (const Image& image, const AffineTransform& transform, const Graphics::ResamplingQuality quality)
  1486. {
  1487. return toEdgeTable()->clipToImageAlpha (image, transform, quality);
  1488. }
  1489. void translate (Point<int> delta) { clip.offsetAll (delta); }
  1490. bool clipRegionIntersects (const Rectangle<int>& r) const { return clip.intersects (r); }
  1491. Rectangle<int> getClipBounds() const { return clip.getBounds(); }
  1492. void fillRectWithColour (SavedStateType& state, const Rectangle<int>& area, const PixelARGB colour, bool replaceContents) const
  1493. {
  1494. SubRectangleIterator iter (clip, area);
  1495. state.fillWithSolidColour (iter, colour, replaceContents);
  1496. }
  1497. void fillRectWithColour (SavedStateType& state, const Rectangle<float>& area, const PixelARGB colour) const
  1498. {
  1499. SubRectangleIteratorFloat iter (clip, area);
  1500. state.fillWithSolidColour (iter, colour, false);
  1501. }
  1502. void fillAllWithColour (SavedStateType& state, const PixelARGB colour, bool replaceContents) const
  1503. {
  1504. state.fillWithSolidColour (*this, colour, replaceContents);
  1505. }
  1506. void fillAllWithGradient (SavedStateType& state, ColourGradient& gradient, const AffineTransform& transform, bool isIdentity) const
  1507. {
  1508. state.fillWithGradient (*this, gradient, transform, isIdentity);
  1509. }
  1510. void renderImageTransformed (SavedStateType& state, const Image& src, const int alpha, const AffineTransform& transform, Graphics::ResamplingQuality quality, bool tiledFill) const
  1511. {
  1512. state.renderImageTransformed (*this, src, alpha, transform, quality, tiledFill);
  1513. }
  1514. void renderImageUntransformed (SavedStateType& state, const Image& src, const int alpha, int x, int y, bool tiledFill) const
  1515. {
  1516. state.renderImageUntransformed (*this, src, alpha, x, y, tiledFill);
  1517. }
  1518. RectangleList<int> clip;
  1519. //==============================================================================
  1520. template <class Renderer>
  1521. void iterate (Renderer& r) const noexcept
  1522. {
  1523. for (const Rectangle<int>* i = clip.begin(), * const e = clip.end(); i != e; ++i)
  1524. {
  1525. const int x = i->getX();
  1526. const int w = i->getWidth();
  1527. jassert (w > 0);
  1528. const int bottom = i->getBottom();
  1529. for (int y = i->getY(); y < bottom; ++y)
  1530. {
  1531. r.setEdgeTableYPos (y);
  1532. r.handleEdgeTableLineFull (x, w);
  1533. }
  1534. }
  1535. }
  1536. private:
  1537. //==============================================================================
  1538. class SubRectangleIterator
  1539. {
  1540. public:
  1541. SubRectangleIterator (const RectangleList<int>& clipList, const Rectangle<int>& clipBounds)
  1542. : clip (clipList), area (clipBounds)
  1543. {}
  1544. template <class Renderer>
  1545. void iterate (Renderer& r) const noexcept
  1546. {
  1547. for (const Rectangle<int>* i = clip.begin(), * const e = clip.end(); i != e; ++i)
  1548. {
  1549. const Rectangle<int> rect (i->getIntersection (area));
  1550. if (! rect.isEmpty())
  1551. {
  1552. const int x = rect.getX();
  1553. const int w = rect.getWidth();
  1554. const int bottom = rect.getBottom();
  1555. for (int y = rect.getY(); y < bottom; ++y)
  1556. {
  1557. r.setEdgeTableYPos (y);
  1558. r.handleEdgeTableLineFull (x, w);
  1559. }
  1560. }
  1561. }
  1562. }
  1563. private:
  1564. const RectangleList<int>& clip;
  1565. const Rectangle<int> area;
  1566. JUCE_DECLARE_NON_COPYABLE (SubRectangleIterator)
  1567. };
  1568. //==============================================================================
  1569. class SubRectangleIteratorFloat
  1570. {
  1571. public:
  1572. SubRectangleIteratorFloat (const RectangleList<int>& clipList, const Rectangle<float>& clipBounds) noexcept
  1573. : clip (clipList), area (clipBounds)
  1574. {
  1575. }
  1576. template <class Renderer>
  1577. void iterate (Renderer& r) const noexcept
  1578. {
  1579. const RenderingHelpers::FloatRectangleRasterisingInfo f (area);
  1580. for (const Rectangle<int>* i = clip.begin(), * const e = clip.end(); i != e; ++i)
  1581. {
  1582. const int clipLeft = i->getX();
  1583. const int clipRight = i->getRight();
  1584. const int clipTop = i->getY();
  1585. const int clipBottom = i->getBottom();
  1586. if (f.totalBottom > clipTop && f.totalTop < clipBottom
  1587. && f.totalRight > clipLeft && f.totalLeft < clipRight)
  1588. {
  1589. if (f.isOnePixelWide())
  1590. {
  1591. if (f.topAlpha != 0 && f.totalTop >= clipTop)
  1592. {
  1593. r.setEdgeTableYPos (f.totalTop);
  1594. r.handleEdgeTablePixel (f.left, f.topAlpha);
  1595. }
  1596. const int endY = jmin (f.bottom, clipBottom);
  1597. for (int y = jmax (clipTop, f.top); y < endY; ++y)
  1598. {
  1599. r.setEdgeTableYPos (y);
  1600. r.handleEdgeTablePixelFull (f.left);
  1601. }
  1602. if (f.bottomAlpha != 0 && f.bottom < clipBottom)
  1603. {
  1604. r.setEdgeTableYPos (f.bottom);
  1605. r.handleEdgeTablePixel (f.left, f.bottomAlpha);
  1606. }
  1607. }
  1608. else
  1609. {
  1610. const int clippedLeft = jmax (f.left, clipLeft);
  1611. const int clippedWidth = jmin (f.right, clipRight) - clippedLeft;
  1612. const bool doLeftAlpha = f.leftAlpha != 0 && f.totalLeft >= clipLeft;
  1613. const bool doRightAlpha = f.rightAlpha != 0 && f.right < clipRight;
  1614. if (f.topAlpha != 0 && f.totalTop >= clipTop)
  1615. {
  1616. r.setEdgeTableYPos (f.totalTop);
  1617. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.getTopLeftCornerAlpha());
  1618. if (clippedWidth > 0) r.handleEdgeTableLine (clippedLeft, clippedWidth, f.topAlpha);
  1619. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.getTopRightCornerAlpha());
  1620. }
  1621. const int endY = jmin (f.bottom, clipBottom);
  1622. for (int y = jmax (clipTop, f.top); y < endY; ++y)
  1623. {
  1624. r.setEdgeTableYPos (y);
  1625. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.leftAlpha);
  1626. if (clippedWidth > 0) r.handleEdgeTableLineFull (clippedLeft, clippedWidth);
  1627. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.rightAlpha);
  1628. }
  1629. if (f.bottomAlpha != 0 && f.bottom < clipBottom)
  1630. {
  1631. r.setEdgeTableYPos (f.bottom);
  1632. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.getBottomLeftCornerAlpha());
  1633. if (clippedWidth > 0) r.handleEdgeTableLine (clippedLeft, clippedWidth, f.bottomAlpha);
  1634. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.getBottomRightCornerAlpha());
  1635. }
  1636. }
  1637. }
  1638. }
  1639. }
  1640. private:
  1641. const RectangleList<int>& clip;
  1642. const Rectangle<float>& area;
  1643. JUCE_DECLARE_NON_COPYABLE (SubRectangleIteratorFloat)
  1644. };
  1645. Ptr toEdgeTable() const { return new EdgeTableRegion (clip); }
  1646. RectangleListRegion& operator= (const RectangleListRegion&);
  1647. };
  1648. };
  1649. //==============================================================================
  1650. template <class SavedStateType>
  1651. class SavedStateBase
  1652. {
  1653. public:
  1654. typedef typename ClipRegions<SavedStateType>::Base BaseRegionType;
  1655. typedef typename ClipRegions<SavedStateType>::EdgeTableRegion EdgeTableRegionType;
  1656. typedef typename ClipRegions<SavedStateType>::RectangleListRegion RectangleListRegionType;
  1657. SavedStateBase (const Rectangle<int>& initialClip)
  1658. : clip (new RectangleListRegionType (initialClip)), transform (Point<int>()),
  1659. interpolationQuality (Graphics::mediumResamplingQuality), transparencyLayerAlpha (1.0f)
  1660. {
  1661. }
  1662. SavedStateBase (const RectangleList<int>& clipList, Point<int> origin)
  1663. : clip (new RectangleListRegionType (clipList)), transform (origin),
  1664. interpolationQuality (Graphics::mediumResamplingQuality), transparencyLayerAlpha (1.0f)
  1665. {
  1666. }
  1667. SavedStateBase (const SavedStateBase& other)
  1668. : clip (other.clip), transform (other.transform), fillType (other.fillType),
  1669. interpolationQuality (other.interpolationQuality),
  1670. transparencyLayerAlpha (other.transparencyLayerAlpha)
  1671. {
  1672. }
  1673. SavedStateType& getThis() noexcept { return *static_cast <SavedStateType*> (this); }
  1674. bool clipToRectangle (const Rectangle<int>& r)
  1675. {
  1676. if (clip != nullptr)
  1677. {
  1678. if (transform.isOnlyTranslated)
  1679. {
  1680. cloneClipIfMultiplyReferenced();
  1681. clip = clip->clipToRectangle (transform.translated (r));
  1682. }
  1683. else if (! transform.isRotated)
  1684. {
  1685. cloneClipIfMultiplyReferenced();
  1686. clip = clip->clipToRectangle (transform.transformed (r));
  1687. }
  1688. else
  1689. {
  1690. Path p;
  1691. p.addRectangle (r);
  1692. clipToPath (p, AffineTransform::identity);
  1693. }
  1694. }
  1695. return clip != nullptr;
  1696. }
  1697. bool clipToRectangleList (const RectangleList<int>& r)
  1698. {
  1699. if (clip != nullptr)
  1700. {
  1701. if (transform.isOnlyTranslated)
  1702. {
  1703. cloneClipIfMultiplyReferenced();
  1704. RectangleList<int> offsetList (r);
  1705. offsetList.offsetAll (transform.offset.x, transform.offset.y);
  1706. clip = clip->clipToRectangleList (offsetList);
  1707. }
  1708. else if (! transform.isRotated)
  1709. {
  1710. cloneClipIfMultiplyReferenced();
  1711. RectangleList<int> scaledList;
  1712. for (const Rectangle<int>* i = r.begin(), * const e = r.end(); i != e; ++i)
  1713. scaledList.add (transform.transformed (*i));
  1714. clip = clip->clipToRectangleList (scaledList);
  1715. }
  1716. else
  1717. {
  1718. clipToPath (r.toPath(), AffineTransform::identity);
  1719. }
  1720. }
  1721. return clip != nullptr;
  1722. }
  1723. static Rectangle<int> getLargestIntegerWithin (Rectangle<float> r)
  1724. {
  1725. const int x1 = (int) std::ceil (r.getX());
  1726. const int y1 = (int) std::ceil (r.getY());
  1727. const int x2 = (int) std::floor (r.getRight());
  1728. const int y2 = (int) std::floor (r.getBottom());
  1729. return Rectangle<int> (x1, y1, x2 - x1, y2 - y1);
  1730. }
  1731. bool excludeClipRectangle (const Rectangle<int>& r)
  1732. {
  1733. if (clip != nullptr)
  1734. {
  1735. cloneClipIfMultiplyReferenced();
  1736. if (transform.isOnlyTranslated)
  1737. {
  1738. clip = clip->excludeClipRectangle (getLargestIntegerWithin (transform.translated (r.toFloat())));
  1739. }
  1740. else if (! transform.isRotated)
  1741. {
  1742. clip = clip->excludeClipRectangle (getLargestIntegerWithin (transform.transformed (r.toFloat())));
  1743. }
  1744. else
  1745. {
  1746. Path p;
  1747. p.addRectangle (r.toFloat());
  1748. p.applyTransform (transform.complexTransform);
  1749. p.addRectangle (clip->getClipBounds().toFloat());
  1750. p.setUsingNonZeroWinding (false);
  1751. clip = clip->clipToPath (p, AffineTransform::identity);
  1752. }
  1753. }
  1754. return clip != nullptr;
  1755. }
  1756. void clipToPath (const Path& p, const AffineTransform& t)
  1757. {
  1758. if (clip != nullptr)
  1759. {
  1760. cloneClipIfMultiplyReferenced();
  1761. clip = clip->clipToPath (p, transform.getTransformWith (t));
  1762. }
  1763. }
  1764. void clipToImageAlpha (const Image& sourceImage, const AffineTransform& t)
  1765. {
  1766. if (clip != nullptr)
  1767. {
  1768. if (sourceImage.hasAlphaChannel())
  1769. {
  1770. cloneClipIfMultiplyReferenced();
  1771. clip = clip->clipToImageAlpha (sourceImage, transform.getTransformWith (t), interpolationQuality);
  1772. }
  1773. else
  1774. {
  1775. Path p;
  1776. p.addRectangle (sourceImage.getBounds());
  1777. clipToPath (p, t);
  1778. }
  1779. }
  1780. }
  1781. bool clipRegionIntersects (const Rectangle<int>& r) const
  1782. {
  1783. if (clip != nullptr)
  1784. {
  1785. if (transform.isOnlyTranslated)
  1786. return clip->clipRegionIntersects (transform.translated (r));
  1787. return getClipBounds().intersects (r);
  1788. }
  1789. return false;
  1790. }
  1791. Rectangle<int> getClipBounds() const
  1792. {
  1793. return clip != nullptr ? transform.deviceSpaceToUserSpace (clip->getClipBounds())
  1794. : Rectangle<int>();
  1795. }
  1796. void setFillType (const FillType& newFill)
  1797. {
  1798. fillType = newFill;
  1799. }
  1800. void fillTargetRect (const Rectangle<int>& r, const bool replaceContents)
  1801. {
  1802. if (fillType.isColour())
  1803. {
  1804. clip->fillRectWithColour (getThis(), r, fillType.colour.getPixelARGB(), replaceContents);
  1805. }
  1806. else
  1807. {
  1808. const Rectangle<int> clipped (clip->getClipBounds().getIntersection (r));
  1809. if (! clipped.isEmpty())
  1810. fillShape (new RectangleListRegionType (clipped), false);
  1811. }
  1812. }
  1813. void fillTargetRect (const Rectangle<float>& r)
  1814. {
  1815. if (fillType.isColour())
  1816. {
  1817. clip->fillRectWithColour (getThis(), r, fillType.colour.getPixelARGB());
  1818. }
  1819. else
  1820. {
  1821. const Rectangle<float> clipped (clip->getClipBounds().toFloat().getIntersection (r));
  1822. if (! clipped.isEmpty())
  1823. fillShape (new EdgeTableRegionType (clipped), false);
  1824. }
  1825. }
  1826. template <typename CoordType>
  1827. void fillRectAsPath (const Rectangle<CoordType>& r)
  1828. {
  1829. Path p;
  1830. p.addRectangle (r);
  1831. fillPath (p, AffineTransform::identity);
  1832. }
  1833. void fillRect (const Rectangle<int>& r, const bool replaceContents)
  1834. {
  1835. if (clip != nullptr)
  1836. {
  1837. if (transform.isOnlyTranslated)
  1838. {
  1839. fillTargetRect (transform.translated (r), replaceContents);
  1840. }
  1841. else if (! transform.isRotated)
  1842. {
  1843. fillTargetRect (transform.transformed (r), replaceContents);
  1844. }
  1845. else
  1846. {
  1847. jassert (! replaceContents); // not implemented..
  1848. fillRectAsPath (r);
  1849. }
  1850. }
  1851. }
  1852. void fillRect (const Rectangle<float>& r)
  1853. {
  1854. if (clip != nullptr)
  1855. {
  1856. if (transform.isOnlyTranslated)
  1857. fillTargetRect (transform.translated (r));
  1858. else if (! transform.isRotated)
  1859. fillTargetRect (transform.transformed (r));
  1860. else
  1861. fillRectAsPath (r);
  1862. }
  1863. }
  1864. void fillRectList (const RectangleList<float>& list)
  1865. {
  1866. if (clip != nullptr)
  1867. {
  1868. if (! transform.isRotated)
  1869. {
  1870. RectangleList<float> transformed (list);
  1871. if (transform.isOnlyTranslated)
  1872. transformed.offsetAll (transform.offset.toFloat());
  1873. else
  1874. transformed.transformAll (transform.getTransform());
  1875. fillShape (new EdgeTableRegionType (transformed), false);
  1876. }
  1877. else
  1878. {
  1879. fillPath (list.toPath(), AffineTransform::identity);
  1880. }
  1881. }
  1882. }
  1883. void fillPath (const Path& path, const AffineTransform& t)
  1884. {
  1885. if (clip != nullptr)
  1886. fillShape (new EdgeTableRegionType (clip->getClipBounds(), path, transform.getTransformWith (t)), false);
  1887. }
  1888. void fillEdgeTable (const EdgeTable& edgeTable, const float x, const int y)
  1889. {
  1890. if (clip != nullptr)
  1891. {
  1892. EdgeTableRegionType* edgeTableClip = new EdgeTableRegionType (edgeTable);
  1893. edgeTableClip->edgeTable.translate (x, y);
  1894. fillShape (edgeTableClip, false);
  1895. }
  1896. }
  1897. void drawLine (const Line <float>& line)
  1898. {
  1899. Path p;
  1900. p.addLineSegment (line, 1.0f);
  1901. fillPath (p, AffineTransform::identity);
  1902. }
  1903. void drawImage (const Image& sourceImage, const AffineTransform& trans)
  1904. {
  1905. if (clip != nullptr && ! fillType.colour.isTransparent())
  1906. renderImage (sourceImage, trans, nullptr);
  1907. }
  1908. void renderImage (const Image& sourceImage, const AffineTransform& trans,
  1909. const BaseRegionType* const tiledFillClipRegion)
  1910. {
  1911. const AffineTransform t (transform.getTransformWith (trans));
  1912. const int alpha = fillType.colour.getAlpha();
  1913. if (t.isOnlyTranslation())
  1914. {
  1915. // If our translation doesn't involve any distortion, just use a simple blit..
  1916. int tx = (int) (t.getTranslationX() * 256.0f);
  1917. int ty = (int) (t.getTranslationY() * 256.0f);
  1918. if (interpolationQuality == Graphics::lowResamplingQuality || ((tx | ty) & 224) == 0)
  1919. {
  1920. tx = ((tx + 128) >> 8);
  1921. ty = ((ty + 128) >> 8);
  1922. if (tiledFillClipRegion != nullptr)
  1923. {
  1924. tiledFillClipRegion->renderImageUntransformed (getThis(), sourceImage, alpha, tx, ty, true);
  1925. }
  1926. else
  1927. {
  1928. Rectangle<int> area (tx, ty, sourceImage.getWidth(), sourceImage.getHeight());
  1929. area = area.getIntersection (getThis().getMaximumBounds());
  1930. if (! area.isEmpty())
  1931. if (typename BaseRegionType::Ptr c = clip->applyClipTo (new EdgeTableRegionType (area)))
  1932. c->renderImageUntransformed (getThis(), sourceImage, alpha, tx, ty, false);
  1933. }
  1934. return;
  1935. }
  1936. }
  1937. if (! t.isSingularity())
  1938. {
  1939. if (tiledFillClipRegion != nullptr)
  1940. {
  1941. tiledFillClipRegion->renderImageTransformed (getThis(), sourceImage, alpha, t, interpolationQuality, true);
  1942. }
  1943. else
  1944. {
  1945. Path p;
  1946. p.addRectangle (sourceImage.getBounds());
  1947. typename BaseRegionType::Ptr c (clip->clone());
  1948. c = c->clipToPath (p, t);
  1949. if (c != nullptr)
  1950. c->renderImageTransformed (getThis(), sourceImage, alpha, t, interpolationQuality, false);
  1951. }
  1952. }
  1953. }
  1954. void fillShape (typename BaseRegionType::Ptr shapeToFill, const bool replaceContents)
  1955. {
  1956. jassert (clip != nullptr);
  1957. shapeToFill = clip->applyClipTo (shapeToFill);
  1958. if (shapeToFill != nullptr)
  1959. {
  1960. if (fillType.isGradient())
  1961. {
  1962. jassert (! replaceContents); // that option is just for solid colours
  1963. ColourGradient g2 (*(fillType.gradient));
  1964. g2.multiplyOpacity (fillType.getOpacity());
  1965. AffineTransform t (transform.getTransformWith (fillType.transform).translated (-0.5f, -0.5f));
  1966. const bool isIdentity = t.isOnlyTranslation();
  1967. if (isIdentity)
  1968. {
  1969. // If our translation doesn't involve any distortion, we can speed it up..
  1970. g2.point1.applyTransform (t);
  1971. g2.point2.applyTransform (t);
  1972. t = AffineTransform::identity;
  1973. }
  1974. shapeToFill->fillAllWithGradient (getThis(), g2, t, isIdentity);
  1975. }
  1976. else if (fillType.isTiledImage())
  1977. {
  1978. renderImage (fillType.image, fillType.transform, shapeToFill);
  1979. }
  1980. else
  1981. {
  1982. shapeToFill->fillAllWithColour (getThis(), fillType.colour.getPixelARGB(), replaceContents);
  1983. }
  1984. }
  1985. }
  1986. void cloneClipIfMultiplyReferenced()
  1987. {
  1988. if (clip->getReferenceCount() > 1)
  1989. clip = clip->clone();
  1990. }
  1991. typename BaseRegionType::Ptr clip;
  1992. RenderingHelpers::TranslationOrTransform transform;
  1993. FillType fillType;
  1994. Graphics::ResamplingQuality interpolationQuality;
  1995. float transparencyLayerAlpha;
  1996. };
  1997. //==============================================================================
  1998. class SoftwareRendererSavedState : public SavedStateBase<SoftwareRendererSavedState>
  1999. {
  2000. typedef SavedStateBase<SoftwareRendererSavedState> BaseClass;
  2001. public:
  2002. SoftwareRendererSavedState (const Image& im, const Rectangle<int>& clipBounds)
  2003. : BaseClass (clipBounds), image (im)
  2004. {
  2005. }
  2006. SoftwareRendererSavedState (const Image& im, const RectangleList<int>& clipList, Point<int> origin)
  2007. : BaseClass (clipList, origin), image (im)
  2008. {
  2009. }
  2010. SoftwareRendererSavedState (const SoftwareRendererSavedState& other)
  2011. : BaseClass (other), image (other.image), font (other.font)
  2012. {
  2013. }
  2014. SoftwareRendererSavedState* beginTransparencyLayer (float opacity)
  2015. {
  2016. SoftwareRendererSavedState* s = new SoftwareRendererSavedState (*this);
  2017. if (clip != nullptr)
  2018. {
  2019. const Rectangle<int> layerBounds (clip->getClipBounds());
  2020. s->image = Image (Image::ARGB, layerBounds.getWidth(), layerBounds.getHeight(), true);
  2021. s->transparencyLayerAlpha = opacity;
  2022. s->transform.moveOriginInDeviceSpace (-layerBounds.getPosition());
  2023. s->cloneClipIfMultiplyReferenced();
  2024. s->clip->translate (-layerBounds.getPosition());
  2025. }
  2026. return s;
  2027. }
  2028. void endTransparencyLayer (SoftwareRendererSavedState& finishedLayerState)
  2029. {
  2030. if (clip != nullptr)
  2031. {
  2032. const Rectangle<int> layerBounds (clip->getClipBounds());
  2033. const ScopedPointer<LowLevelGraphicsContext> g (image.createLowLevelContext());
  2034. g->setOpacity (finishedLayerState.transparencyLayerAlpha);
  2035. g->drawImage (finishedLayerState.image, AffineTransform::translation (layerBounds.getPosition()));
  2036. }
  2037. }
  2038. //==============================================================================
  2039. void drawGlyph (int glyphNumber, const AffineTransform& trans)
  2040. {
  2041. if (clip != nullptr)
  2042. {
  2043. if (trans.isOnlyTranslation() && ! transform.isRotated)
  2044. {
  2045. typedef GlyphCache <CachedGlyphEdgeTable <SoftwareRendererSavedState>, SoftwareRendererSavedState> GlyphCacheType;
  2046. GlyphCacheType& cache = GlyphCacheType::getInstance();
  2047. Point<float> pos (trans.getTranslationX(), trans.getTranslationY());
  2048. if (transform.isOnlyTranslated)
  2049. {
  2050. cache.drawGlyph (*this, font, glyphNumber, pos + transform.offset.toFloat());
  2051. }
  2052. else
  2053. {
  2054. pos = transform.transformed (pos);
  2055. Font f (font);
  2056. f.setHeight (font.getHeight() * transform.complexTransform.mat11);
  2057. const float xScale = transform.complexTransform.mat00 / transform.complexTransform.mat11;
  2058. if (std::abs (xScale - 1.0f) > 0.01f)
  2059. f.setHorizontalScale (xScale);
  2060. cache.drawGlyph (*this, f, glyphNumber, pos);
  2061. }
  2062. }
  2063. else
  2064. {
  2065. const float fontHeight = font.getHeight();
  2066. AffineTransform t (transform.getTransformWith (AffineTransform::scale (fontHeight * font.getHorizontalScale(), fontHeight)
  2067. .followedBy (trans)));
  2068. const ScopedPointer<EdgeTable> et (font.getTypeface()->getEdgeTableForGlyph (glyphNumber, t));
  2069. if (et != nullptr)
  2070. fillShape (new EdgeTableRegionType (*et), false);
  2071. }
  2072. }
  2073. }
  2074. Rectangle<int> getMaximumBounds() const { return image.getBounds(); }
  2075. //==============================================================================
  2076. template <typename IteratorType>
  2077. void renderImageTransformed (IteratorType& iter, const Image& src, const int alpha, const AffineTransform& trans, Graphics::ResamplingQuality quality, bool tiledFill) const
  2078. {
  2079. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2080. const Image::BitmapData srcData (src, Image::BitmapData::readOnly);
  2081. EdgeTableFillers::renderImageTransformed (iter, destData, srcData, alpha, trans, quality, tiledFill);
  2082. }
  2083. template <typename IteratorType>
  2084. void renderImageUntransformed (IteratorType& iter, const Image& src, const int alpha, int x, int y, bool tiledFill) const
  2085. {
  2086. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2087. const Image::BitmapData srcData (src, Image::BitmapData::readOnly);
  2088. EdgeTableFillers::renderImageUntransformed (iter, destData, srcData, alpha, x, y, tiledFill);
  2089. }
  2090. template <typename IteratorType>
  2091. void fillWithSolidColour (IteratorType& iter, const PixelARGB colour, bool replaceContents) const
  2092. {
  2093. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2094. switch (destData.pixelFormat)
  2095. {
  2096. case Image::ARGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelARGB*) 0); break;
  2097. case Image::RGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelRGB*) 0); break;
  2098. default: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelAlpha*) 0); break;
  2099. }
  2100. }
  2101. template <typename IteratorType>
  2102. void fillWithGradient (IteratorType& iter, ColourGradient& gradient, const AffineTransform& trans, bool isIdentity) const
  2103. {
  2104. HeapBlock <PixelARGB> lookupTable;
  2105. const int numLookupEntries = gradient.createLookupTable (trans, lookupTable);
  2106. jassert (numLookupEntries > 0);
  2107. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2108. switch (destData.pixelFormat)
  2109. {
  2110. case Image::ARGB: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelARGB*) 0); break;
  2111. case Image::RGB: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelRGB*) 0); break;
  2112. default: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelAlpha*) 0); break;
  2113. }
  2114. }
  2115. //==============================================================================
  2116. Image image;
  2117. Font font;
  2118. private:
  2119. SoftwareRendererSavedState& operator= (const SoftwareRendererSavedState&);
  2120. };
  2121. //==============================================================================
  2122. template <class StateObjectType>
  2123. class SavedStateStack
  2124. {
  2125. public:
  2126. SavedStateStack (StateObjectType* const initialState) noexcept
  2127. : currentState (initialState)
  2128. {}
  2129. SavedStateStack() noexcept {}
  2130. void initialise (StateObjectType* state)
  2131. {
  2132. currentState = state;
  2133. }
  2134. inline StateObjectType* operator->() const noexcept { return currentState; }
  2135. inline StateObjectType& operator*() const noexcept { return *currentState; }
  2136. void save()
  2137. {
  2138. stack.add (new StateObjectType (*currentState));
  2139. }
  2140. void restore()
  2141. {
  2142. if (StateObjectType* const top = stack.getLast())
  2143. {
  2144. currentState = top;
  2145. stack.removeLast (1, false);
  2146. }
  2147. else
  2148. {
  2149. jassertfalse; // trying to pop with an empty stack!
  2150. }
  2151. }
  2152. void beginTransparencyLayer (float opacity)
  2153. {
  2154. save();
  2155. currentState = currentState->beginTransparencyLayer (opacity);
  2156. }
  2157. void endTransparencyLayer()
  2158. {
  2159. const ScopedPointer<StateObjectType> finishedTransparencyLayer (currentState);
  2160. restore();
  2161. currentState->endTransparencyLayer (*finishedTransparencyLayer);
  2162. }
  2163. private:
  2164. ScopedPointer<StateObjectType> currentState;
  2165. OwnedArray<StateObjectType> stack;
  2166. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedStateStack)
  2167. };
  2168. //==============================================================================
  2169. template <class SavedStateType>
  2170. class StackBasedLowLevelGraphicsContext : public LowLevelGraphicsContext
  2171. {
  2172. public:
  2173. bool isVectorDevice() const override { return false; }
  2174. void setOrigin (Point<int> o) override { stack->transform.setOrigin (o); }
  2175. void addTransform (const AffineTransform& t) override { stack->transform.addTransform (t); }
  2176. float getPhysicalPixelScaleFactor() override { return stack->transform.getPhysicalPixelScaleFactor(); }
  2177. Rectangle<int> getClipBounds() const override { return stack->getClipBounds(); }
  2178. bool isClipEmpty() const override { return stack->clip == nullptr; }
  2179. bool clipRegionIntersects (const Rectangle<int>& r) override { return stack->clipRegionIntersects (r); }
  2180. bool clipToRectangle (const Rectangle<int>& r) override { return stack->clipToRectangle (r); }
  2181. bool clipToRectangleList (const RectangleList<int>& r) override { return stack->clipToRectangleList (r); }
  2182. void excludeClipRectangle (const Rectangle<int>& r) override { stack->excludeClipRectangle (r); }
  2183. void clipToPath (const Path& path, const AffineTransform& t) override { stack->clipToPath (path, t); }
  2184. void clipToImageAlpha (const Image& im, const AffineTransform& t) override { stack->clipToImageAlpha (im, t); }
  2185. void saveState() override { stack.save(); }
  2186. void restoreState() override { stack.restore(); }
  2187. void beginTransparencyLayer (float opacity) override { stack.beginTransparencyLayer (opacity); }
  2188. void endTransparencyLayer() override { stack.endTransparencyLayer(); }
  2189. void setFill (const FillType& fillType) override { stack->setFillType (fillType); }
  2190. void setOpacity (float newOpacity) override { stack->fillType.setOpacity (newOpacity); }
  2191. void setInterpolationQuality (Graphics::ResamplingQuality quality) override { stack->interpolationQuality = quality; }
  2192. void fillRect (const Rectangle<int>& r, bool replace) override { stack->fillRect (r, replace); }
  2193. void fillRect (const Rectangle<float>& r) override { stack->fillRect (r); }
  2194. void fillRectList (const RectangleList<float>& list) override { stack->fillRectList (list); }
  2195. void fillPath (const Path& path, const AffineTransform& t) override { stack->fillPath (path, t); }
  2196. void drawImage (const Image& im, const AffineTransform& t) override { stack->drawImage (im, t); }
  2197. void drawGlyph (int glyphNumber, const AffineTransform& t) override { stack->drawGlyph (glyphNumber, t); }
  2198. void drawLine (const Line <float>& line) override { stack->drawLine (line); }
  2199. void setFont (const Font& newFont) override { stack->font = newFont; }
  2200. const Font& getFont() override { return stack->font; }
  2201. protected:
  2202. StackBasedLowLevelGraphicsContext (SavedStateType* initialState) : stack (initialState) {}
  2203. StackBasedLowLevelGraphicsContext() {}
  2204. RenderingHelpers::SavedStateStack<SavedStateType> stack;
  2205. };
  2206. }
  2207. #if JUCE_MSVC
  2208. #pragma warning (pop)
  2209. #endif
  2210. #endif // JUCE_RENDERINGHELPERS_H_INCLUDED