The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2608 lines
100KB

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