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.

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