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.

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