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.

2651 lines
101KB

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