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.

2660 lines
102KB

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