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.

2679 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. if (srcData.pixelStride == 3 && destData.pixelStride == 3)
  753. {
  754. memcpy (dest, src, sizeof (PixelRGB) * (size_t) width);
  755. }
  756. else
  757. {
  758. const int destStride = destData.pixelStride;
  759. const int srcStride = srcData.pixelStride;
  760. do
  761. {
  762. dest->blend (*src);
  763. dest = addBytesToPointer (dest, destStride);
  764. src = addBytesToPointer (src, srcStride);
  765. } while (--width > 0);
  766. }
  767. }
  768. JUCE_DECLARE_NON_COPYABLE (ImageFill)
  769. };
  770. //==============================================================================
  771. /** Fills an edge-table with a transformed image. */
  772. template <class DestPixelType, class SrcPixelType, bool repeatPattern>
  773. class TransformedImageFill
  774. {
  775. public:
  776. TransformedImageFill (const Image::BitmapData& dest, const Image::BitmapData& src,
  777. const AffineTransform& transform, const int alpha, const Graphics::ResamplingQuality q)
  778. : interpolator (transform,
  779. q != Graphics::lowResamplingQuality ? 0.5f : 0.0f,
  780. q != Graphics::lowResamplingQuality ? -128 : 0),
  781. destData (dest),
  782. srcData (src),
  783. extraAlpha (alpha + 1),
  784. quality (q),
  785. maxX (src.width - 1),
  786. maxY (src.height - 1),
  787. scratchSize (2048)
  788. {
  789. scratchBuffer.malloc (scratchSize);
  790. }
  791. forcedinline void setEdgeTableYPos (const int newY) noexcept
  792. {
  793. y = newY;
  794. linePixels = (DestPixelType*) destData.getLinePointer (newY);
  795. }
  796. forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) noexcept
  797. {
  798. SrcPixelType p;
  799. generate (&p, x, 1);
  800. getDestPixel (x)->blend (p, (uint32) (alphaLevel * extraAlpha) >> 8);
  801. }
  802. forcedinline void handleEdgeTablePixelFull (const int x) noexcept
  803. {
  804. SrcPixelType p;
  805. generate (&p, x, 1);
  806. getDestPixel (x)->blend (p, (uint32) extraAlpha);
  807. }
  808. void handleEdgeTableLine (const int x, int width, int alphaLevel) noexcept
  809. {
  810. if (width > (int) scratchSize)
  811. {
  812. scratchSize = (size_t) width;
  813. scratchBuffer.malloc (scratchSize);
  814. }
  815. SrcPixelType* span = scratchBuffer;
  816. generate (span, x, width);
  817. DestPixelType* dest = getDestPixel (x);
  818. alphaLevel *= extraAlpha;
  819. alphaLevel >>= 8;
  820. if (alphaLevel < 0xfe)
  821. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*span++, (uint32) alphaLevel))
  822. else
  823. JUCE_PERFORM_PIXEL_OP_LOOP (blend (*span++))
  824. }
  825. forcedinline void handleEdgeTableLineFull (const int x, int width) noexcept
  826. {
  827. handleEdgeTableLine (x, width, 255);
  828. }
  829. void clipEdgeTableLine (EdgeTable& et, int x, int y_, int width)
  830. {
  831. if (width > (int) scratchSize)
  832. {
  833. scratchSize = (size_t) width;
  834. scratchBuffer.malloc (scratchSize);
  835. }
  836. y = y_;
  837. generate (scratchBuffer.getData(), x, width);
  838. et.clipLineToMask (x, y_,
  839. reinterpret_cast<uint8*> (scratchBuffer.getData()) + SrcPixelType::indexA,
  840. sizeof (SrcPixelType), width);
  841. }
  842. private:
  843. forcedinline DestPixelType* getDestPixel (const int x) const noexcept
  844. {
  845. return addBytesToPointer (linePixels, x * destData.pixelStride);
  846. }
  847. //==============================================================================
  848. template <class PixelType>
  849. void generate (PixelType* dest, const int x, int numPixels) noexcept
  850. {
  851. this->interpolator.setStartOfLine ((float) x, (float) y, numPixels);
  852. do
  853. {
  854. int hiResX, hiResY;
  855. this->interpolator.next (hiResX, hiResY);
  856. int loResX = hiResX >> 8;
  857. int loResY = hiResY >> 8;
  858. if (repeatPattern)
  859. {
  860. loResX = negativeAwareModulo (loResX, srcData.width);
  861. loResY = negativeAwareModulo (loResY, srcData.height);
  862. }
  863. if (quality != Graphics::lowResamplingQuality)
  864. {
  865. if (isPositiveAndBelow (loResX, maxX))
  866. {
  867. if (isPositiveAndBelow (loResY, maxY))
  868. {
  869. // In the centre of the image..
  870. render4PixelAverage (dest, this->srcData.getPixelPointer (loResX, loResY),
  871. hiResX & 255, hiResY & 255);
  872. ++dest;
  873. continue;
  874. }
  875. if (! repeatPattern)
  876. {
  877. // At a top or bottom edge..
  878. if (loResY < 0)
  879. render2PixelAverageX (dest, this->srcData.getPixelPointer (loResX, 0), hiResX & 255);
  880. else
  881. render2PixelAverageX (dest, this->srcData.getPixelPointer (loResX, maxY), hiResX & 255);
  882. ++dest;
  883. continue;
  884. }
  885. }
  886. else
  887. {
  888. if (isPositiveAndBelow (loResY, maxY) && ! repeatPattern)
  889. {
  890. // At a left or right hand edge..
  891. if (loResX < 0)
  892. render2PixelAverageY (dest, this->srcData.getPixelPointer (0, loResY), hiResY & 255);
  893. else
  894. render2PixelAverageY (dest, this->srcData.getPixelPointer (maxX, loResY), hiResY & 255);
  895. ++dest;
  896. continue;
  897. }
  898. }
  899. }
  900. if (! repeatPattern)
  901. {
  902. if (loResX < 0) loResX = 0;
  903. if (loResY < 0) loResY = 0;
  904. if (loResX > maxX) loResX = maxX;
  905. if (loResY > maxY) loResY = maxY;
  906. }
  907. dest->set (*(const PixelType*) this->srcData.getPixelPointer (loResX, loResY));
  908. ++dest;
  909. } while (--numPixels > 0);
  910. }
  911. //==============================================================================
  912. void render4PixelAverage (PixelARGB* const dest, const uint8* src, const int subPixelX, const int subPixelY) noexcept
  913. {
  914. uint32 c[4] = { 256 * 128, 256 * 128, 256 * 128, 256 * 128 };
  915. uint32 weight = (uint32) ((256 - subPixelX) * (256 - subPixelY));
  916. c[0] += weight * src[0];
  917. c[1] += weight * src[1];
  918. c[2] += weight * src[2];
  919. c[3] += weight * src[3];
  920. src += this->srcData.pixelStride;
  921. weight = (uint32) (subPixelX * (256 - subPixelY));
  922. c[0] += weight * src[0];
  923. c[1] += weight * src[1];
  924. c[2] += weight * src[2];
  925. c[3] += weight * src[3];
  926. src += this->srcData.lineStride;
  927. weight = (uint32) (subPixelX * subPixelY);
  928. c[0] += weight * src[0];
  929. c[1] += weight * src[1];
  930. c[2] += weight * src[2];
  931. c[3] += weight * src[3];
  932. src -= this->srcData.pixelStride;
  933. weight = (uint32) ((256 - subPixelX) * subPixelY);
  934. c[0] += weight * src[0];
  935. c[1] += weight * src[1];
  936. c[2] += weight * src[2];
  937. c[3] += weight * src[3];
  938. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 16),
  939. (uint8) (c[PixelARGB::indexR] >> 16),
  940. (uint8) (c[PixelARGB::indexG] >> 16),
  941. (uint8) (c[PixelARGB::indexB] >> 16));
  942. }
  943. void render2PixelAverageX (PixelARGB* const dest, const uint8* src, const uint32 subPixelX) noexcept
  944. {
  945. uint32 c[4] = { 128, 128, 128, 128 };
  946. uint32 weight = 256 - subPixelX;
  947. c[0] += weight * src[0];
  948. c[1] += weight * src[1];
  949. c[2] += weight * src[2];
  950. c[3] += weight * src[3];
  951. src += this->srcData.pixelStride;
  952. weight = subPixelX;
  953. c[0] += weight * src[0];
  954. c[1] += weight * src[1];
  955. c[2] += weight * src[2];
  956. c[3] += weight * src[3];
  957. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 8),
  958. (uint8) (c[PixelARGB::indexR] >> 8),
  959. (uint8) (c[PixelARGB::indexG] >> 8),
  960. (uint8) (c[PixelARGB::indexB] >> 8));
  961. }
  962. void render2PixelAverageY (PixelARGB* const dest, const uint8* src, const uint32 subPixelY) noexcept
  963. {
  964. uint32 c[4] = { 128, 128, 128, 128 };
  965. uint32 weight = 256 - subPixelY;
  966. c[0] += weight * src[0];
  967. c[1] += weight * src[1];
  968. c[2] += weight * src[2];
  969. c[3] += weight * src[3];
  970. src += this->srcData.lineStride;
  971. weight = subPixelY;
  972. c[0] += weight * src[0];
  973. c[1] += weight * src[1];
  974. c[2] += weight * src[2];
  975. c[3] += weight * src[3];
  976. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 8),
  977. (uint8) (c[PixelARGB::indexR] >> 8),
  978. (uint8) (c[PixelARGB::indexG] >> 8),
  979. (uint8) (c[PixelARGB::indexB] >> 8));
  980. }
  981. //==============================================================================
  982. void render4PixelAverage (PixelRGB* const dest, const uint8* src, const uint32 subPixelX, const uint32 subPixelY) noexcept
  983. {
  984. uint32 c[3] = { 256 * 128, 256 * 128, 256 * 128 };
  985. uint32 weight = (256 - subPixelX) * (256 - subPixelY);
  986. c[0] += weight * src[0];
  987. c[1] += weight * src[1];
  988. c[2] += weight * src[2];
  989. src += this->srcData.pixelStride;
  990. weight = subPixelX * (256 - subPixelY);
  991. c[0] += weight * src[0];
  992. c[1] += weight * src[1];
  993. c[2] += weight * src[2];
  994. src += this->srcData.lineStride;
  995. weight = subPixelX * subPixelY;
  996. c[0] += weight * src[0];
  997. c[1] += weight * src[1];
  998. c[2] += weight * src[2];
  999. src -= this->srcData.pixelStride;
  1000. weight = (256 - subPixelX) * subPixelY;
  1001. c[0] += weight * src[0];
  1002. c[1] += weight * src[1];
  1003. c[2] += weight * src[2];
  1004. dest->setARGB ((uint8) 255,
  1005. (uint8) (c[PixelRGB::indexR] >> 16),
  1006. (uint8) (c[PixelRGB::indexG] >> 16),
  1007. (uint8) (c[PixelRGB::indexB] >> 16));
  1008. }
  1009. void render2PixelAverageX (PixelRGB* const dest, const uint8* src, const uint32 subPixelX) noexcept
  1010. {
  1011. uint32 c[3] = { 128, 128, 128 };
  1012. const uint32 weight = 256 - subPixelX;
  1013. c[0] += weight * src[0];
  1014. c[1] += weight * src[1];
  1015. c[2] += weight * src[2];
  1016. src += this->srcData.pixelStride;
  1017. c[0] += subPixelX * src[0];
  1018. c[1] += subPixelX * src[1];
  1019. c[2] += subPixelX * src[2];
  1020. dest->setARGB ((uint8) 255,
  1021. (uint8) (c[PixelRGB::indexR] >> 8),
  1022. (uint8) (c[PixelRGB::indexG] >> 8),
  1023. (uint8) (c[PixelRGB::indexB] >> 8));
  1024. }
  1025. void render2PixelAverageY (PixelRGB* const dest, const uint8* src, const uint32 subPixelY) noexcept
  1026. {
  1027. uint32 c[3] = { 128, 128, 128 };
  1028. const uint32 weight = 256 - subPixelY;
  1029. c[0] += weight * src[0];
  1030. c[1] += weight * src[1];
  1031. c[2] += weight * src[2];
  1032. src += this->srcData.lineStride;
  1033. c[0] += subPixelY * src[0];
  1034. c[1] += subPixelY * src[1];
  1035. c[2] += subPixelY * src[2];
  1036. dest->setARGB ((uint8) 255,
  1037. (uint8) (c[PixelRGB::indexR] >> 8),
  1038. (uint8) (c[PixelRGB::indexG] >> 8),
  1039. (uint8) (c[PixelRGB::indexB] >> 8));
  1040. }
  1041. //==============================================================================
  1042. void render4PixelAverage (PixelAlpha* const dest, const uint8* src, const uint32 subPixelX, const uint32 subPixelY) noexcept
  1043. {
  1044. uint32 c = 256 * 128;
  1045. c += src[0] * ((256 - subPixelX) * (256 - subPixelY));
  1046. src += this->srcData.pixelStride;
  1047. c += src[1] * (subPixelX * (256 - subPixelY));
  1048. src += this->srcData.lineStride;
  1049. c += src[1] * (subPixelX * subPixelY);
  1050. src -= this->srcData.pixelStride;
  1051. c += src[0] * ((256 - subPixelX) * subPixelY);
  1052. *((uint8*) dest) = (uint8) (c >> 16);
  1053. }
  1054. void render2PixelAverageX (PixelAlpha* const dest, const uint8* src, const uint32 subPixelX) noexcept
  1055. {
  1056. uint32 c = 128;
  1057. c += src[0] * (256 - subPixelX);
  1058. src += this->srcData.pixelStride;
  1059. c += src[0] * subPixelX;
  1060. *((uint8*) dest) = (uint8) (c >> 8);
  1061. }
  1062. void render2PixelAverageY (PixelAlpha* const dest, const uint8* src, const uint32 subPixelY) noexcept
  1063. {
  1064. uint32 c = 128;
  1065. c += src[0] * (256 - subPixelY);
  1066. src += this->srcData.lineStride;
  1067. c += src[0] * subPixelY;
  1068. *((uint8*) dest) = (uint8) (c >> 8);
  1069. }
  1070. //==============================================================================
  1071. class TransformedImageSpanInterpolator
  1072. {
  1073. public:
  1074. TransformedImageSpanInterpolator (const AffineTransform& transform,
  1075. const float offsetFloat, const int offsetInt) noexcept
  1076. : inverseTransform (transform.inverted()),
  1077. pixelOffset (offsetFloat), pixelOffsetInt (offsetInt)
  1078. {}
  1079. void setStartOfLine (float sx, float sy, const int numPixels) noexcept
  1080. {
  1081. jassert (numPixels > 0);
  1082. sx += pixelOffset;
  1083. sy += pixelOffset;
  1084. float x1 = sx, y1 = sy;
  1085. sx += numPixels;
  1086. inverseTransform.transformPoints (x1, y1, sx, sy);
  1087. xBresenham.set ((int) (x1 * 256.0f), (int) (sx * 256.0f), numPixels, pixelOffsetInt);
  1088. yBresenham.set ((int) (y1 * 256.0f), (int) (sy * 256.0f), numPixels, pixelOffsetInt);
  1089. }
  1090. void next (int& px, int& py) noexcept
  1091. {
  1092. px = xBresenham.n; xBresenham.stepToNext();
  1093. py = yBresenham.n; yBresenham.stepToNext();
  1094. }
  1095. private:
  1096. class BresenhamInterpolator
  1097. {
  1098. public:
  1099. BresenhamInterpolator() noexcept {}
  1100. void set (const int n1, const int n2, const int steps, const int offsetInt) noexcept
  1101. {
  1102. numSteps = steps;
  1103. step = (n2 - n1) / numSteps;
  1104. remainder = modulo = (n2 - n1) % numSteps;
  1105. n = n1 + offsetInt;
  1106. if (modulo <= 0)
  1107. {
  1108. modulo += numSteps;
  1109. remainder += numSteps;
  1110. --step;
  1111. }
  1112. modulo -= numSteps;
  1113. }
  1114. forcedinline void stepToNext() noexcept
  1115. {
  1116. modulo += remainder;
  1117. n += step;
  1118. if (modulo > 0)
  1119. {
  1120. modulo -= numSteps;
  1121. ++n;
  1122. }
  1123. }
  1124. int n;
  1125. private:
  1126. int numSteps, step, modulo, remainder;
  1127. };
  1128. const AffineTransform inverseTransform;
  1129. BresenhamInterpolator xBresenham, yBresenham;
  1130. const float pixelOffset;
  1131. const int pixelOffsetInt;
  1132. JUCE_DECLARE_NON_COPYABLE (TransformedImageSpanInterpolator)
  1133. };
  1134. //==============================================================================
  1135. TransformedImageSpanInterpolator interpolator;
  1136. const Image::BitmapData& destData;
  1137. const Image::BitmapData& srcData;
  1138. const int extraAlpha;
  1139. const Graphics::ResamplingQuality quality;
  1140. const int maxX, maxY;
  1141. int y;
  1142. DestPixelType* linePixels;
  1143. HeapBlock<SrcPixelType> scratchBuffer;
  1144. size_t scratchSize;
  1145. JUCE_DECLARE_NON_COPYABLE (TransformedImageFill)
  1146. };
  1147. //==============================================================================
  1148. template <class Iterator>
  1149. void renderImageTransformed (Iterator& iter, const Image::BitmapData& destData, const Image::BitmapData& srcData,
  1150. const int alpha, const AffineTransform& transform, Graphics::ResamplingQuality quality, bool tiledFill)
  1151. {
  1152. switch (destData.pixelFormat)
  1153. {
  1154. case Image::ARGB:
  1155. switch (srcData.pixelFormat)
  1156. {
  1157. case Image::ARGB:
  1158. if (tiledFill) { TransformedImageFill<PixelARGB, PixelARGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1159. else { TransformedImageFill<PixelARGB, PixelARGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1160. break;
  1161. case Image::RGB:
  1162. if (tiledFill) { TransformedImageFill<PixelARGB, PixelRGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1163. else { TransformedImageFill<PixelARGB, PixelRGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1164. break;
  1165. default:
  1166. if (tiledFill) { TransformedImageFill<PixelARGB, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1167. else { TransformedImageFill<PixelARGB, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1168. break;
  1169. }
  1170. break;
  1171. case Image::RGB:
  1172. switch (srcData.pixelFormat)
  1173. {
  1174. case Image::ARGB:
  1175. if (tiledFill) { TransformedImageFill<PixelRGB, PixelARGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1176. else { TransformedImageFill<PixelRGB, PixelARGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1177. break;
  1178. case Image::RGB:
  1179. if (tiledFill) { TransformedImageFill<PixelRGB, PixelRGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1180. else { TransformedImageFill<PixelRGB, PixelRGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1181. break;
  1182. default:
  1183. if (tiledFill) { TransformedImageFill<PixelRGB, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1184. else { TransformedImageFill<PixelRGB, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1185. break;
  1186. }
  1187. break;
  1188. default:
  1189. switch (srcData.pixelFormat)
  1190. {
  1191. case Image::ARGB:
  1192. if (tiledFill) { TransformedImageFill<PixelAlpha, PixelARGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1193. else { TransformedImageFill<PixelAlpha, PixelARGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1194. break;
  1195. case Image::RGB:
  1196. if (tiledFill) { TransformedImageFill<PixelAlpha, PixelRGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1197. else { TransformedImageFill<PixelAlpha, PixelRGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1198. break;
  1199. default:
  1200. if (tiledFill) { TransformedImageFill<PixelAlpha, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1201. else { TransformedImageFill<PixelAlpha, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1202. break;
  1203. }
  1204. break;
  1205. }
  1206. }
  1207. template <class Iterator>
  1208. void renderImageUntransformed (Iterator& iter, const Image::BitmapData& destData, const Image::BitmapData& srcData, const int alpha, int x, int y, bool tiledFill)
  1209. {
  1210. switch (destData.pixelFormat)
  1211. {
  1212. case Image::ARGB:
  1213. switch (srcData.pixelFormat)
  1214. {
  1215. case Image::ARGB:
  1216. if (tiledFill) { ImageFill<PixelARGB, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1217. else { ImageFill<PixelARGB, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1218. break;
  1219. case Image::RGB:
  1220. if (tiledFill) { ImageFill<PixelARGB, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1221. else { ImageFill<PixelARGB, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1222. break;
  1223. default:
  1224. if (tiledFill) { ImageFill<PixelARGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1225. else { ImageFill<PixelARGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1226. break;
  1227. }
  1228. break;
  1229. case Image::RGB:
  1230. switch (srcData.pixelFormat)
  1231. {
  1232. case Image::ARGB:
  1233. if (tiledFill) { ImageFill<PixelRGB, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1234. else { ImageFill<PixelRGB, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1235. break;
  1236. case Image::RGB:
  1237. if (tiledFill) { ImageFill<PixelRGB, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1238. else { ImageFill<PixelRGB, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1239. break;
  1240. default:
  1241. if (tiledFill) { ImageFill<PixelRGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1242. else { ImageFill<PixelRGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1243. break;
  1244. }
  1245. break;
  1246. default:
  1247. switch (srcData.pixelFormat)
  1248. {
  1249. case Image::ARGB:
  1250. if (tiledFill) { ImageFill<PixelAlpha, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1251. else { ImageFill<PixelAlpha, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1252. break;
  1253. case Image::RGB:
  1254. if (tiledFill) { ImageFill<PixelAlpha, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1255. else { ImageFill<PixelAlpha, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1256. break;
  1257. default:
  1258. if (tiledFill) { ImageFill<PixelAlpha, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1259. else { ImageFill<PixelAlpha, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1260. break;
  1261. }
  1262. break;
  1263. }
  1264. }
  1265. template <class Iterator, class DestPixelType>
  1266. void renderSolidFill (Iterator& iter, const Image::BitmapData& destData, const PixelARGB fillColour, const bool replaceContents, DestPixelType*)
  1267. {
  1268. if (replaceContents)
  1269. {
  1270. EdgeTableFillers::SolidColour<DestPixelType, true> r (destData, fillColour);
  1271. iter.iterate (r);
  1272. }
  1273. else
  1274. {
  1275. EdgeTableFillers::SolidColour<DestPixelType, false> r (destData, fillColour);
  1276. iter.iterate (r);
  1277. }
  1278. }
  1279. template <class Iterator, class DestPixelType>
  1280. void renderGradient (Iterator& iter, const Image::BitmapData& destData, const ColourGradient& g, const AffineTransform& transform,
  1281. const PixelARGB* const lookupTable, const int numLookupEntries, const bool isIdentity, DestPixelType*)
  1282. {
  1283. if (g.isRadial)
  1284. {
  1285. if (isIdentity)
  1286. {
  1287. EdgeTableFillers::Gradient<DestPixelType, GradientPixelIterators::Radial> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1288. iter.iterate (renderer);
  1289. }
  1290. else
  1291. {
  1292. EdgeTableFillers::Gradient<DestPixelType, GradientPixelIterators::TransformedRadial> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1293. iter.iterate (renderer);
  1294. }
  1295. }
  1296. else
  1297. {
  1298. EdgeTableFillers::Gradient<DestPixelType, GradientPixelIterators::Linear> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1299. iter.iterate (renderer);
  1300. }
  1301. }
  1302. }
  1303. //==============================================================================
  1304. template <class SavedStateType>
  1305. struct ClipRegions
  1306. {
  1307. class Base : public SingleThreadedReferenceCountedObject
  1308. {
  1309. public:
  1310. Base() {}
  1311. virtual ~Base() {}
  1312. typedef ReferenceCountedObjectPtr<Base> Ptr;
  1313. virtual Ptr clone() const = 0;
  1314. virtual Ptr applyClipTo (const Ptr& target) const = 0;
  1315. virtual Ptr clipToRectangle (const Rectangle<int>&) = 0;
  1316. virtual Ptr clipToRectangleList (const RectangleList<int>&) = 0;
  1317. virtual Ptr excludeClipRectangle (const Rectangle<int>&) = 0;
  1318. virtual Ptr clipToPath (const Path&, const AffineTransform&) = 0;
  1319. virtual Ptr clipToEdgeTable (const EdgeTable& et) = 0;
  1320. virtual Ptr clipToImageAlpha (const Image&, const AffineTransform&, const Graphics::ResamplingQuality) = 0;
  1321. virtual void translate (Point<int> delta) = 0;
  1322. virtual bool clipRegionIntersects (const Rectangle<int>&) const = 0;
  1323. virtual Rectangle<int> getClipBounds() const = 0;
  1324. virtual void fillRectWithColour (SavedStateType&, const Rectangle<int>&, const PixelARGB colour, bool replaceContents) const = 0;
  1325. virtual void fillRectWithColour (SavedStateType&, const Rectangle<float>&, const PixelARGB colour) const = 0;
  1326. virtual void fillAllWithColour (SavedStateType&, const PixelARGB colour, bool replaceContents) const = 0;
  1327. virtual void fillAllWithGradient (SavedStateType&, ColourGradient&, const AffineTransform&, bool isIdentity) const = 0;
  1328. virtual void renderImageTransformed (SavedStateType&, const Image&, const int alpha, const AffineTransform&, Graphics::ResamplingQuality, bool tiledFill) const = 0;
  1329. virtual void renderImageUntransformed (SavedStateType&, const Image&, const int alpha, int x, int y, bool tiledFill) const = 0;
  1330. };
  1331. //==============================================================================
  1332. class EdgeTableRegion : public Base
  1333. {
  1334. public:
  1335. EdgeTableRegion (const EdgeTable& e) : edgeTable (e) {}
  1336. EdgeTableRegion (const Rectangle<int>& r) : edgeTable (r) {}
  1337. EdgeTableRegion (const Rectangle<float>& r) : edgeTable (r) {}
  1338. EdgeTableRegion (const RectangleList<int>& r) : edgeTable (r) {}
  1339. EdgeTableRegion (const RectangleList<float>& r) : edgeTable (r) {}
  1340. EdgeTableRegion (const Rectangle<int>& bounds, const Path& p, const AffineTransform& t) : edgeTable (bounds, p, t) {}
  1341. EdgeTableRegion (const EdgeTableRegion& other) : Base(), edgeTable (other.edgeTable) {}
  1342. typedef typename Base::Ptr Ptr;
  1343. Ptr clone() const { return new EdgeTableRegion (*this); }
  1344. Ptr applyClipTo (const Ptr& target) const { return target->clipToEdgeTable (edgeTable); }
  1345. Ptr clipToRectangle (const Rectangle<int>& r)
  1346. {
  1347. edgeTable.clipToRectangle (r);
  1348. return edgeTable.isEmpty() ? nullptr : this;
  1349. }
  1350. Ptr clipToRectangleList (const RectangleList<int>& r)
  1351. {
  1352. RectangleList<int> inverse (edgeTable.getMaximumBounds());
  1353. if (inverse.subtract (r))
  1354. for (const Rectangle<int>* i = inverse.begin(), * const e = inverse.end(); i != e; ++i)
  1355. edgeTable.excludeRectangle (*i);
  1356. return edgeTable.isEmpty() ? nullptr : this;
  1357. }
  1358. Ptr excludeClipRectangle (const Rectangle<int>& r)
  1359. {
  1360. edgeTable.excludeRectangle (r);
  1361. return edgeTable.isEmpty() ? nullptr : this;
  1362. }
  1363. Ptr clipToPath (const Path& p, const AffineTransform& transform)
  1364. {
  1365. EdgeTable et (edgeTable.getMaximumBounds(), p, transform);
  1366. edgeTable.clipToEdgeTable (et);
  1367. return edgeTable.isEmpty() ? nullptr : this;
  1368. }
  1369. Ptr clipToEdgeTable (const EdgeTable& et)
  1370. {
  1371. edgeTable.clipToEdgeTable (et);
  1372. return edgeTable.isEmpty() ? nullptr : this;
  1373. }
  1374. Ptr clipToImageAlpha (const Image& image, const AffineTransform& transform, const Graphics::ResamplingQuality quality)
  1375. {
  1376. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  1377. if (transform.isOnlyTranslation())
  1378. {
  1379. // If our translation doesn't involve any distortion, just use a simple blit..
  1380. const int tx = (int) (transform.getTranslationX() * 256.0f);
  1381. const int ty = (int) (transform.getTranslationY() * 256.0f);
  1382. if (quality == Graphics::lowResamplingQuality || ((tx | ty) & 224) == 0)
  1383. {
  1384. const int imageX = ((tx + 128) >> 8);
  1385. const int imageY = ((ty + 128) >> 8);
  1386. if (image.getFormat() == Image::ARGB)
  1387. straightClipImage (srcData, imageX, imageY, (PixelARGB*) 0);
  1388. else
  1389. straightClipImage (srcData, imageX, imageY, (PixelAlpha*) 0);
  1390. return edgeTable.isEmpty() ? nullptr : this;
  1391. }
  1392. }
  1393. if (transform.isSingularity())
  1394. return Ptr();
  1395. {
  1396. Path p;
  1397. p.addRectangle (0, 0, (float) srcData.width, (float) srcData.height);
  1398. EdgeTable et2 (edgeTable.getMaximumBounds(), p, transform);
  1399. edgeTable.clipToEdgeTable (et2);
  1400. }
  1401. if (! edgeTable.isEmpty())
  1402. {
  1403. if (image.getFormat() == Image::ARGB)
  1404. transformedClipImage (srcData, transform, quality, (PixelARGB*) 0);
  1405. else
  1406. transformedClipImage (srcData, transform, quality, (PixelAlpha*) 0);
  1407. }
  1408. return edgeTable.isEmpty() ? nullptr : this;
  1409. }
  1410. void translate (Point<int> delta)
  1411. {
  1412. edgeTable.translate ((float) delta.x, delta.y);
  1413. }
  1414. bool clipRegionIntersects (const Rectangle<int>& r) const
  1415. {
  1416. return edgeTable.getMaximumBounds().intersects (r);
  1417. }
  1418. Rectangle<int> getClipBounds() const
  1419. {
  1420. return edgeTable.getMaximumBounds();
  1421. }
  1422. void fillRectWithColour (SavedStateType& state, const Rectangle<int>& area, const PixelARGB colour, bool replaceContents) const
  1423. {
  1424. const Rectangle<int> totalClip (edgeTable.getMaximumBounds());
  1425. const Rectangle<int> clipped (totalClip.getIntersection (area));
  1426. if (! clipped.isEmpty())
  1427. {
  1428. EdgeTableRegion et (clipped);
  1429. et.edgeTable.clipToEdgeTable (edgeTable);
  1430. state.fillWithSolidColour (et.edgeTable, colour, replaceContents);
  1431. }
  1432. }
  1433. void fillRectWithColour (SavedStateType& state, const Rectangle<float>& area, const PixelARGB colour) const
  1434. {
  1435. const Rectangle<float> totalClip (edgeTable.getMaximumBounds().toFloat());
  1436. const Rectangle<float> clipped (totalClip.getIntersection (area));
  1437. if (! clipped.isEmpty())
  1438. {
  1439. EdgeTableRegion et (clipped);
  1440. et.edgeTable.clipToEdgeTable (edgeTable);
  1441. state.fillWithSolidColour (et.edgeTable, colour, false);
  1442. }
  1443. }
  1444. void fillAllWithColour (SavedStateType& state, const PixelARGB colour, bool replaceContents) const
  1445. {
  1446. state.fillWithSolidColour (edgeTable, colour, replaceContents);
  1447. }
  1448. void fillAllWithGradient (SavedStateType& state, ColourGradient& gradient, const AffineTransform& transform, bool isIdentity) const
  1449. {
  1450. state.fillWithGradient (edgeTable, gradient, transform, isIdentity);
  1451. }
  1452. void renderImageTransformed (SavedStateType& state, const Image& src, const int alpha, const AffineTransform& transform, Graphics::ResamplingQuality quality, bool tiledFill) const
  1453. {
  1454. state.renderImageTransformed (edgeTable, src, alpha, transform, quality, tiledFill);
  1455. }
  1456. void renderImageUntransformed (SavedStateType& state, const Image& src, const int alpha, int x, int y, bool tiledFill) const
  1457. {
  1458. state.renderImageUntransformed (edgeTable, src, alpha, x, y, tiledFill);
  1459. }
  1460. EdgeTable edgeTable;
  1461. private:
  1462. template <class SrcPixelType>
  1463. void transformedClipImage (const Image::BitmapData& srcData, const AffineTransform& transform, const Graphics::ResamplingQuality quality, const SrcPixelType*)
  1464. {
  1465. EdgeTableFillers::TransformedImageFill<SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, transform, 255, quality);
  1466. for (int y = 0; y < edgeTable.getMaximumBounds().getHeight(); ++y)
  1467. renderer.clipEdgeTableLine (edgeTable, edgeTable.getMaximumBounds().getX(), y + edgeTable.getMaximumBounds().getY(),
  1468. edgeTable.getMaximumBounds().getWidth());
  1469. }
  1470. template <class SrcPixelType>
  1471. void straightClipImage (const Image::BitmapData& srcData, int imageX, int imageY, const SrcPixelType*)
  1472. {
  1473. Rectangle<int> r (imageX, imageY, srcData.width, srcData.height);
  1474. edgeTable.clipToRectangle (r);
  1475. EdgeTableFillers::ImageFill<SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, 255, imageX, imageY);
  1476. for (int y = 0; y < r.getHeight(); ++y)
  1477. renderer.clipEdgeTableLine (edgeTable, r.getX(), y + r.getY(), r.getWidth());
  1478. }
  1479. EdgeTableRegion& operator= (const EdgeTableRegion&);
  1480. };
  1481. //==============================================================================
  1482. class RectangleListRegion : public Base
  1483. {
  1484. public:
  1485. RectangleListRegion (const Rectangle<int>& r) : clip (r) {}
  1486. RectangleListRegion (const RectangleList<int>& r) : clip (r) {}
  1487. RectangleListRegion (const RectangleListRegion& other) : Base(), clip (other.clip) {}
  1488. typedef typename Base::Ptr Ptr;
  1489. Ptr clone() const { return new RectangleListRegion (*this); }
  1490. Ptr applyClipTo (const Ptr& target) const { return target->clipToRectangleList (clip); }
  1491. Ptr clipToRectangle (const Rectangle<int>& r)
  1492. {
  1493. clip.clipTo (r);
  1494. return clip.isEmpty() ? nullptr : this;
  1495. }
  1496. Ptr clipToRectangleList (const RectangleList<int>& r)
  1497. {
  1498. clip.clipTo (r);
  1499. return clip.isEmpty() ? nullptr : this;
  1500. }
  1501. Ptr excludeClipRectangle (const Rectangle<int>& r)
  1502. {
  1503. clip.subtract (r);
  1504. return clip.isEmpty() ? nullptr : this;
  1505. }
  1506. Ptr clipToPath (const Path& p, const AffineTransform& transform) { return toEdgeTable()->clipToPath (p, transform); }
  1507. Ptr clipToEdgeTable (const EdgeTable& et) { return toEdgeTable()->clipToEdgeTable (et); }
  1508. Ptr clipToImageAlpha (const Image& image, const AffineTransform& transform, const Graphics::ResamplingQuality quality)
  1509. {
  1510. return toEdgeTable()->clipToImageAlpha (image, transform, quality);
  1511. }
  1512. void translate (Point<int> delta) { clip.offsetAll (delta); }
  1513. bool clipRegionIntersects (const Rectangle<int>& r) const { return clip.intersects (r); }
  1514. Rectangle<int> getClipBounds() const { return clip.getBounds(); }
  1515. void fillRectWithColour (SavedStateType& state, const Rectangle<int>& area, const PixelARGB colour, bool replaceContents) const
  1516. {
  1517. SubRectangleIterator iter (clip, area);
  1518. state.fillWithSolidColour (iter, colour, replaceContents);
  1519. }
  1520. void fillRectWithColour (SavedStateType& state, const Rectangle<float>& area, const PixelARGB colour) const
  1521. {
  1522. SubRectangleIteratorFloat iter (clip, area);
  1523. state.fillWithSolidColour (iter, colour, false);
  1524. }
  1525. void fillAllWithColour (SavedStateType& state, const PixelARGB colour, bool replaceContents) const
  1526. {
  1527. state.fillWithSolidColour (*this, colour, replaceContents);
  1528. }
  1529. void fillAllWithGradient (SavedStateType& state, ColourGradient& gradient, const AffineTransform& transform, bool isIdentity) const
  1530. {
  1531. state.fillWithGradient (*this, gradient, transform, isIdentity);
  1532. }
  1533. void renderImageTransformed (SavedStateType& state, const Image& src, const int alpha, const AffineTransform& transform, Graphics::ResamplingQuality quality, bool tiledFill) const
  1534. {
  1535. state.renderImageTransformed (*this, src, alpha, transform, quality, tiledFill);
  1536. }
  1537. void renderImageUntransformed (SavedStateType& state, const Image& src, const int alpha, int x, int y, bool tiledFill) const
  1538. {
  1539. state.renderImageUntransformed (*this, src, alpha, x, y, tiledFill);
  1540. }
  1541. RectangleList<int> clip;
  1542. //==============================================================================
  1543. template <class Renderer>
  1544. void iterate (Renderer& r) const noexcept
  1545. {
  1546. for (const Rectangle<int>* i = clip.begin(), * const e = clip.end(); i != e; ++i)
  1547. {
  1548. const int x = i->getX();
  1549. const int w = i->getWidth();
  1550. jassert (w > 0);
  1551. const int bottom = i->getBottom();
  1552. for (int y = i->getY(); y < bottom; ++y)
  1553. {
  1554. r.setEdgeTableYPos (y);
  1555. r.handleEdgeTableLineFull (x, w);
  1556. }
  1557. }
  1558. }
  1559. private:
  1560. //==============================================================================
  1561. class SubRectangleIterator
  1562. {
  1563. public:
  1564. SubRectangleIterator (const RectangleList<int>& clipList, const Rectangle<int>& clipBounds)
  1565. : clip (clipList), area (clipBounds)
  1566. {}
  1567. template <class Renderer>
  1568. void iterate (Renderer& r) const noexcept
  1569. {
  1570. for (const Rectangle<int>* i = clip.begin(), * const e = clip.end(); i != e; ++i)
  1571. {
  1572. const Rectangle<int> rect (i->getIntersection (area));
  1573. if (! rect.isEmpty())
  1574. {
  1575. const int x = rect.getX();
  1576. const int w = rect.getWidth();
  1577. const int bottom = rect.getBottom();
  1578. for (int y = rect.getY(); y < bottom; ++y)
  1579. {
  1580. r.setEdgeTableYPos (y);
  1581. r.handleEdgeTableLineFull (x, w);
  1582. }
  1583. }
  1584. }
  1585. }
  1586. private:
  1587. const RectangleList<int>& clip;
  1588. const Rectangle<int> area;
  1589. JUCE_DECLARE_NON_COPYABLE (SubRectangleIterator)
  1590. };
  1591. //==============================================================================
  1592. class SubRectangleIteratorFloat
  1593. {
  1594. public:
  1595. SubRectangleIteratorFloat (const RectangleList<int>& clipList, const Rectangle<float>& clipBounds) noexcept
  1596. : clip (clipList), area (clipBounds)
  1597. {
  1598. }
  1599. template <class Renderer>
  1600. void iterate (Renderer& r) const noexcept
  1601. {
  1602. const RenderingHelpers::FloatRectangleRasterisingInfo f (area);
  1603. for (const Rectangle<int>* i = clip.begin(), * const e = clip.end(); i != e; ++i)
  1604. {
  1605. const int clipLeft = i->getX();
  1606. const int clipRight = i->getRight();
  1607. const int clipTop = i->getY();
  1608. const int clipBottom = i->getBottom();
  1609. if (f.totalBottom > clipTop && f.totalTop < clipBottom
  1610. && f.totalRight > clipLeft && f.totalLeft < clipRight)
  1611. {
  1612. if (f.isOnePixelWide())
  1613. {
  1614. if (f.topAlpha != 0 && f.totalTop >= clipTop)
  1615. {
  1616. r.setEdgeTableYPos (f.totalTop);
  1617. r.handleEdgeTablePixel (f.left, f.topAlpha);
  1618. }
  1619. const int endY = jmin (f.bottom, clipBottom);
  1620. for (int y = jmax (clipTop, f.top); y < endY; ++y)
  1621. {
  1622. r.setEdgeTableYPos (y);
  1623. r.handleEdgeTablePixelFull (f.left);
  1624. }
  1625. if (f.bottomAlpha != 0 && f.bottom < clipBottom)
  1626. {
  1627. r.setEdgeTableYPos (f.bottom);
  1628. r.handleEdgeTablePixel (f.left, f.bottomAlpha);
  1629. }
  1630. }
  1631. else
  1632. {
  1633. const int clippedLeft = jmax (f.left, clipLeft);
  1634. const int clippedWidth = jmin (f.right, clipRight) - clippedLeft;
  1635. const bool doLeftAlpha = f.leftAlpha != 0 && f.totalLeft >= clipLeft;
  1636. const bool doRightAlpha = f.rightAlpha != 0 && f.right < clipRight;
  1637. if (f.topAlpha != 0 && f.totalTop >= clipTop)
  1638. {
  1639. r.setEdgeTableYPos (f.totalTop);
  1640. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.getTopLeftCornerAlpha());
  1641. if (clippedWidth > 0) r.handleEdgeTableLine (clippedLeft, clippedWidth, f.topAlpha);
  1642. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.getTopRightCornerAlpha());
  1643. }
  1644. const int endY = jmin (f.bottom, clipBottom);
  1645. for (int y = jmax (clipTop, f.top); y < endY; ++y)
  1646. {
  1647. r.setEdgeTableYPos (y);
  1648. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.leftAlpha);
  1649. if (clippedWidth > 0) r.handleEdgeTableLineFull (clippedLeft, clippedWidth);
  1650. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.rightAlpha);
  1651. }
  1652. if (f.bottomAlpha != 0 && f.bottom < clipBottom)
  1653. {
  1654. r.setEdgeTableYPos (f.bottom);
  1655. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.getBottomLeftCornerAlpha());
  1656. if (clippedWidth > 0) r.handleEdgeTableLine (clippedLeft, clippedWidth, f.bottomAlpha);
  1657. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.getBottomRightCornerAlpha());
  1658. }
  1659. }
  1660. }
  1661. }
  1662. }
  1663. private:
  1664. const RectangleList<int>& clip;
  1665. const Rectangle<float>& area;
  1666. JUCE_DECLARE_NON_COPYABLE (SubRectangleIteratorFloat)
  1667. };
  1668. Ptr toEdgeTable() const { return new EdgeTableRegion (clip); }
  1669. RectangleListRegion& operator= (const RectangleListRegion&);
  1670. };
  1671. };
  1672. //==============================================================================
  1673. template <class SavedStateType>
  1674. class SavedStateBase
  1675. {
  1676. public:
  1677. typedef typename ClipRegions<SavedStateType>::Base BaseRegionType;
  1678. typedef typename ClipRegions<SavedStateType>::EdgeTableRegion EdgeTableRegionType;
  1679. typedef typename ClipRegions<SavedStateType>::RectangleListRegion RectangleListRegionType;
  1680. SavedStateBase (const Rectangle<int>& initialClip)
  1681. : clip (new RectangleListRegionType (initialClip)), transform (Point<int>()),
  1682. interpolationQuality (Graphics::mediumResamplingQuality), transparencyLayerAlpha (1.0f)
  1683. {
  1684. }
  1685. SavedStateBase (const RectangleList<int>& clipList, Point<int> origin)
  1686. : clip (new RectangleListRegionType (clipList)), transform (origin),
  1687. interpolationQuality (Graphics::mediumResamplingQuality), transparencyLayerAlpha (1.0f)
  1688. {
  1689. }
  1690. SavedStateBase (const SavedStateBase& other)
  1691. : clip (other.clip), transform (other.transform), fillType (other.fillType),
  1692. interpolationQuality (other.interpolationQuality),
  1693. transparencyLayerAlpha (other.transparencyLayerAlpha)
  1694. {
  1695. }
  1696. SavedStateType& getThis() noexcept { return *static_cast<SavedStateType*> (this); }
  1697. bool clipToRectangle (const Rectangle<int>& r)
  1698. {
  1699. if (clip != nullptr)
  1700. {
  1701. if (transform.isOnlyTranslated)
  1702. {
  1703. cloneClipIfMultiplyReferenced();
  1704. clip = clip->clipToRectangle (transform.translated (r));
  1705. }
  1706. else if (! transform.isRotated)
  1707. {
  1708. cloneClipIfMultiplyReferenced();
  1709. clip = clip->clipToRectangle (transform.transformed (r));
  1710. }
  1711. else
  1712. {
  1713. Path p;
  1714. p.addRectangle (r);
  1715. clipToPath (p, AffineTransform::identity);
  1716. }
  1717. }
  1718. return clip != nullptr;
  1719. }
  1720. bool clipToRectangleList (const RectangleList<int>& r)
  1721. {
  1722. if (clip != nullptr)
  1723. {
  1724. if (transform.isOnlyTranslated)
  1725. {
  1726. cloneClipIfMultiplyReferenced();
  1727. RectangleList<int> offsetList (r);
  1728. offsetList.offsetAll (transform.offset.x, transform.offset.y);
  1729. clip = clip->clipToRectangleList (offsetList);
  1730. }
  1731. else if (! transform.isRotated)
  1732. {
  1733. cloneClipIfMultiplyReferenced();
  1734. RectangleList<int> scaledList;
  1735. for (const Rectangle<int>* i = r.begin(), * const e = r.end(); i != e; ++i)
  1736. scaledList.add (transform.transformed (*i));
  1737. clip = clip->clipToRectangleList (scaledList);
  1738. }
  1739. else
  1740. {
  1741. clipToPath (r.toPath(), AffineTransform::identity);
  1742. }
  1743. }
  1744. return clip != nullptr;
  1745. }
  1746. static Rectangle<int> getLargestIntegerWithin (Rectangle<float> r)
  1747. {
  1748. const int x1 = (int) std::ceil (r.getX());
  1749. const int y1 = (int) std::ceil (r.getY());
  1750. const int x2 = (int) std::floor (r.getRight());
  1751. const int y2 = (int) std::floor (r.getBottom());
  1752. return Rectangle<int> (x1, y1, x2 - x1, y2 - y1);
  1753. }
  1754. bool excludeClipRectangle (const Rectangle<int>& r)
  1755. {
  1756. if (clip != nullptr)
  1757. {
  1758. cloneClipIfMultiplyReferenced();
  1759. if (transform.isOnlyTranslated)
  1760. {
  1761. clip = clip->excludeClipRectangle (getLargestIntegerWithin (transform.translated (r.toFloat())));
  1762. }
  1763. else if (! transform.isRotated)
  1764. {
  1765. clip = clip->excludeClipRectangle (getLargestIntegerWithin (transform.transformed (r.toFloat())));
  1766. }
  1767. else
  1768. {
  1769. Path p;
  1770. p.addRectangle (r.toFloat());
  1771. p.applyTransform (transform.complexTransform);
  1772. p.addRectangle (clip->getClipBounds().toFloat());
  1773. p.setUsingNonZeroWinding (false);
  1774. clip = clip->clipToPath (p, AffineTransform::identity);
  1775. }
  1776. }
  1777. return clip != nullptr;
  1778. }
  1779. void clipToPath (const Path& p, const AffineTransform& t)
  1780. {
  1781. if (clip != nullptr)
  1782. {
  1783. cloneClipIfMultiplyReferenced();
  1784. clip = clip->clipToPath (p, transform.getTransformWith (t));
  1785. }
  1786. }
  1787. void clipToImageAlpha (const Image& sourceImage, const AffineTransform& t)
  1788. {
  1789. if (clip != nullptr)
  1790. {
  1791. if (sourceImage.hasAlphaChannel())
  1792. {
  1793. cloneClipIfMultiplyReferenced();
  1794. clip = clip->clipToImageAlpha (sourceImage, transform.getTransformWith (t), interpolationQuality);
  1795. }
  1796. else
  1797. {
  1798. Path p;
  1799. p.addRectangle (sourceImage.getBounds());
  1800. clipToPath (p, t);
  1801. }
  1802. }
  1803. }
  1804. bool clipRegionIntersects (const Rectangle<int>& r) const
  1805. {
  1806. if (clip != nullptr)
  1807. {
  1808. if (transform.isOnlyTranslated)
  1809. return clip->clipRegionIntersects (transform.translated (r));
  1810. return getClipBounds().intersects (r);
  1811. }
  1812. return false;
  1813. }
  1814. Rectangle<int> getClipBounds() const
  1815. {
  1816. return clip != nullptr ? transform.deviceSpaceToUserSpace (clip->getClipBounds())
  1817. : Rectangle<int>();
  1818. }
  1819. void setFillType (const FillType& newFill)
  1820. {
  1821. fillType = newFill;
  1822. }
  1823. void fillTargetRect (const Rectangle<int>& r, const bool replaceContents)
  1824. {
  1825. if (fillType.isColour())
  1826. {
  1827. clip->fillRectWithColour (getThis(), r, fillType.colour.getPixelARGB(), replaceContents);
  1828. }
  1829. else
  1830. {
  1831. const Rectangle<int> clipped (clip->getClipBounds().getIntersection (r));
  1832. if (! clipped.isEmpty())
  1833. fillShape (new RectangleListRegionType (clipped), false);
  1834. }
  1835. }
  1836. void fillTargetRect (const Rectangle<float>& r)
  1837. {
  1838. if (fillType.isColour())
  1839. {
  1840. clip->fillRectWithColour (getThis(), r, fillType.colour.getPixelARGB());
  1841. }
  1842. else
  1843. {
  1844. const Rectangle<float> clipped (clip->getClipBounds().toFloat().getIntersection (r));
  1845. if (! clipped.isEmpty())
  1846. fillShape (new EdgeTableRegionType (clipped), false);
  1847. }
  1848. }
  1849. template <typename CoordType>
  1850. void fillRectAsPath (const Rectangle<CoordType>& r)
  1851. {
  1852. Path p;
  1853. p.addRectangle (r);
  1854. fillPath (p, AffineTransform::identity);
  1855. }
  1856. void fillRect (const Rectangle<int>& r, const bool replaceContents)
  1857. {
  1858. if (clip != nullptr)
  1859. {
  1860. if (transform.isOnlyTranslated)
  1861. {
  1862. fillTargetRect (transform.translated (r), replaceContents);
  1863. }
  1864. else if (! transform.isRotated)
  1865. {
  1866. fillTargetRect (transform.transformed (r), replaceContents);
  1867. }
  1868. else
  1869. {
  1870. jassert (! replaceContents); // not implemented..
  1871. fillRectAsPath (r);
  1872. }
  1873. }
  1874. }
  1875. void fillRect (const Rectangle<float>& r)
  1876. {
  1877. if (clip != nullptr)
  1878. {
  1879. if (transform.isOnlyTranslated)
  1880. fillTargetRect (transform.translated (r));
  1881. else if (! transform.isRotated)
  1882. fillTargetRect (transform.transformed (r));
  1883. else
  1884. fillRectAsPath (r);
  1885. }
  1886. }
  1887. void fillRectList (const RectangleList<float>& list)
  1888. {
  1889. if (clip != nullptr)
  1890. {
  1891. if (! transform.isRotated)
  1892. {
  1893. RectangleList<float> transformed (list);
  1894. if (transform.isOnlyTranslated)
  1895. transformed.offsetAll (transform.offset.toFloat());
  1896. else
  1897. transformed.transformAll (transform.getTransform());
  1898. fillShape (new EdgeTableRegionType (transformed), false);
  1899. }
  1900. else
  1901. {
  1902. fillPath (list.toPath(), AffineTransform::identity);
  1903. }
  1904. }
  1905. }
  1906. void fillPath (const Path& path, const AffineTransform& t)
  1907. {
  1908. if (clip != nullptr)
  1909. fillShape (new EdgeTableRegionType (clip->getClipBounds(), path, transform.getTransformWith (t)), false);
  1910. }
  1911. void fillEdgeTable (const EdgeTable& edgeTable, const float x, const int y)
  1912. {
  1913. if (clip != nullptr)
  1914. {
  1915. EdgeTableRegionType* edgeTableClip = new EdgeTableRegionType (edgeTable);
  1916. edgeTableClip->edgeTable.translate (x, y);
  1917. if (fillType.isColour())
  1918. {
  1919. float brightness = fillType.colour.getBrightness() - 0.5f;
  1920. if (brightness > 0.0f)
  1921. edgeTableClip->edgeTable.multiplyLevels (1.0f + 1.6f * brightness);
  1922. }
  1923. fillShape (edgeTableClip, false);
  1924. }
  1925. }
  1926. void drawLine (const Line<float>& line)
  1927. {
  1928. Path p;
  1929. p.addLineSegment (line, 1.0f);
  1930. fillPath (p, AffineTransform::identity);
  1931. }
  1932. void drawImage (const Image& sourceImage, const AffineTransform& trans)
  1933. {
  1934. if (clip != nullptr && ! fillType.colour.isTransparent())
  1935. renderImage (sourceImage, trans, nullptr);
  1936. }
  1937. static bool isOnlyTranslationAllowingError (const AffineTransform& t)
  1938. {
  1939. return (std::abs (t.mat01) < 0.002)
  1940. && (std::abs (t.mat10) < 0.002)
  1941. && (std::abs (t.mat00 - 1.0f) < 0.002)
  1942. && (std::abs (t.mat11 - 1.0f) < 0.002);
  1943. }
  1944. void renderImage (const Image& sourceImage, const AffineTransform& trans,
  1945. const BaseRegionType* const tiledFillClipRegion)
  1946. {
  1947. const AffineTransform t (transform.getTransformWith (trans));
  1948. const int alpha = fillType.colour.getAlpha();
  1949. if (isOnlyTranslationAllowingError (t))
  1950. {
  1951. // If our translation doesn't involve any distortion, just use a simple blit..
  1952. int tx = (int) (t.getTranslationX() * 256.0f);
  1953. int ty = (int) (t.getTranslationY() * 256.0f);
  1954. if (interpolationQuality == Graphics::lowResamplingQuality || ((tx | ty) & 224) == 0)
  1955. {
  1956. tx = ((tx + 128) >> 8);
  1957. ty = ((ty + 128) >> 8);
  1958. if (tiledFillClipRegion != nullptr)
  1959. {
  1960. tiledFillClipRegion->renderImageUntransformed (getThis(), sourceImage, alpha, tx, ty, true);
  1961. }
  1962. else
  1963. {
  1964. Rectangle<int> area (tx, ty, sourceImage.getWidth(), sourceImage.getHeight());
  1965. area = area.getIntersection (getThis().getMaximumBounds());
  1966. if (! area.isEmpty())
  1967. if (typename BaseRegionType::Ptr c = clip->applyClipTo (new EdgeTableRegionType (area)))
  1968. c->renderImageUntransformed (getThis(), sourceImage, alpha, tx, ty, false);
  1969. }
  1970. return;
  1971. }
  1972. }
  1973. if (! t.isSingularity())
  1974. {
  1975. if (tiledFillClipRegion != nullptr)
  1976. {
  1977. tiledFillClipRegion->renderImageTransformed (getThis(), sourceImage, alpha, t, interpolationQuality, true);
  1978. }
  1979. else
  1980. {
  1981. Path p;
  1982. p.addRectangle (sourceImage.getBounds());
  1983. typename BaseRegionType::Ptr c (clip->clone());
  1984. c = c->clipToPath (p, t);
  1985. if (c != nullptr)
  1986. c->renderImageTransformed (getThis(), sourceImage, alpha, t, interpolationQuality, false);
  1987. }
  1988. }
  1989. }
  1990. void fillShape (typename BaseRegionType::Ptr shapeToFill, const bool replaceContents)
  1991. {
  1992. jassert (clip != nullptr);
  1993. shapeToFill = clip->applyClipTo (shapeToFill);
  1994. if (shapeToFill != nullptr)
  1995. {
  1996. if (fillType.isGradient())
  1997. {
  1998. jassert (! replaceContents); // that option is just for solid colours
  1999. ColourGradient g2 (*(fillType.gradient));
  2000. g2.multiplyOpacity (fillType.getOpacity());
  2001. AffineTransform t (transform.getTransformWith (fillType.transform).translated (-0.5f, -0.5f));
  2002. const bool isIdentity = t.isOnlyTranslation();
  2003. if (isIdentity)
  2004. {
  2005. // If our translation doesn't involve any distortion, we can speed it up..
  2006. g2.point1.applyTransform (t);
  2007. g2.point2.applyTransform (t);
  2008. t = AffineTransform::identity;
  2009. }
  2010. shapeToFill->fillAllWithGradient (getThis(), g2, t, isIdentity);
  2011. }
  2012. else if (fillType.isTiledImage())
  2013. {
  2014. renderImage (fillType.image, fillType.transform, shapeToFill);
  2015. }
  2016. else
  2017. {
  2018. shapeToFill->fillAllWithColour (getThis(), fillType.colour.getPixelARGB(), replaceContents);
  2019. }
  2020. }
  2021. }
  2022. void cloneClipIfMultiplyReferenced()
  2023. {
  2024. if (clip->getReferenceCount() > 1)
  2025. clip = clip->clone();
  2026. }
  2027. typename BaseRegionType::Ptr clip;
  2028. RenderingHelpers::TranslationOrTransform transform;
  2029. FillType fillType;
  2030. Graphics::ResamplingQuality interpolationQuality;
  2031. float transparencyLayerAlpha;
  2032. };
  2033. //==============================================================================
  2034. class SoftwareRendererSavedState : public SavedStateBase<SoftwareRendererSavedState>
  2035. {
  2036. typedef SavedStateBase<SoftwareRendererSavedState> BaseClass;
  2037. public:
  2038. SoftwareRendererSavedState (const Image& im, const Rectangle<int>& clipBounds)
  2039. : BaseClass (clipBounds), image (im)
  2040. {
  2041. }
  2042. SoftwareRendererSavedState (const Image& im, const RectangleList<int>& clipList, Point<int> origin)
  2043. : BaseClass (clipList, origin), image (im)
  2044. {
  2045. }
  2046. SoftwareRendererSavedState (const SoftwareRendererSavedState& other)
  2047. : BaseClass (other), image (other.image), font (other.font)
  2048. {
  2049. }
  2050. SoftwareRendererSavedState* beginTransparencyLayer (float opacity)
  2051. {
  2052. SoftwareRendererSavedState* s = new SoftwareRendererSavedState (*this);
  2053. if (clip != nullptr)
  2054. {
  2055. const Rectangle<int> layerBounds (clip->getClipBounds());
  2056. s->image = Image (Image::ARGB, layerBounds.getWidth(), layerBounds.getHeight(), true);
  2057. s->transparencyLayerAlpha = opacity;
  2058. s->transform.moveOriginInDeviceSpace (-layerBounds.getPosition());
  2059. s->cloneClipIfMultiplyReferenced();
  2060. s->clip->translate (-layerBounds.getPosition());
  2061. }
  2062. return s;
  2063. }
  2064. void endTransparencyLayer (SoftwareRendererSavedState& finishedLayerState)
  2065. {
  2066. if (clip != nullptr)
  2067. {
  2068. const Rectangle<int> layerBounds (clip->getClipBounds());
  2069. const ScopedPointer<LowLevelGraphicsContext> g (image.createLowLevelContext());
  2070. g->setOpacity (finishedLayerState.transparencyLayerAlpha);
  2071. g->drawImage (finishedLayerState.image, AffineTransform::translation (layerBounds.getPosition()));
  2072. }
  2073. }
  2074. typedef GlyphCache<CachedGlyphEdgeTable<SoftwareRendererSavedState>, SoftwareRendererSavedState> GlyphCacheType;
  2075. static void clearGlyphCache()
  2076. {
  2077. GlyphCacheType::getInstance().reset();
  2078. }
  2079. //==============================================================================
  2080. void drawGlyph (int glyphNumber, const AffineTransform& trans)
  2081. {
  2082. if (clip != nullptr)
  2083. {
  2084. if (trans.isOnlyTranslation() && ! transform.isRotated)
  2085. {
  2086. GlyphCacheType& cache = GlyphCacheType::getInstance();
  2087. Point<float> pos (trans.getTranslationX(), trans.getTranslationY());
  2088. if (transform.isOnlyTranslated)
  2089. {
  2090. cache.drawGlyph (*this, font, glyphNumber, pos + transform.offset.toFloat());
  2091. }
  2092. else
  2093. {
  2094. pos = transform.transformed (pos);
  2095. Font f (font);
  2096. f.setHeight (font.getHeight() * transform.complexTransform.mat11);
  2097. const float xScale = transform.complexTransform.mat00 / transform.complexTransform.mat11;
  2098. if (std::abs (xScale - 1.0f) > 0.01f)
  2099. f.setHorizontalScale (xScale);
  2100. cache.drawGlyph (*this, f, glyphNumber, pos);
  2101. }
  2102. }
  2103. else
  2104. {
  2105. const float fontHeight = font.getHeight();
  2106. AffineTransform t (transform.getTransformWith (AffineTransform::scale (fontHeight * font.getHorizontalScale(), fontHeight)
  2107. .followedBy (trans)));
  2108. const ScopedPointer<EdgeTable> et (font.getTypeface()->getEdgeTableForGlyph (glyphNumber, t, fontHeight));
  2109. if (et != nullptr)
  2110. fillShape (new EdgeTableRegionType (*et), false);
  2111. }
  2112. }
  2113. }
  2114. Rectangle<int> getMaximumBounds() const { return image.getBounds(); }
  2115. //==============================================================================
  2116. template <typename IteratorType>
  2117. void renderImageTransformed (IteratorType& iter, const Image& src, const int alpha, const AffineTransform& trans, Graphics::ResamplingQuality quality, bool tiledFill) const
  2118. {
  2119. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2120. const Image::BitmapData srcData (src, Image::BitmapData::readOnly);
  2121. EdgeTableFillers::renderImageTransformed (iter, destData, srcData, alpha, trans, quality, tiledFill);
  2122. }
  2123. template <typename IteratorType>
  2124. void renderImageUntransformed (IteratorType& iter, const Image& src, const int alpha, int x, int y, bool tiledFill) const
  2125. {
  2126. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2127. const Image::BitmapData srcData (src, Image::BitmapData::readOnly);
  2128. EdgeTableFillers::renderImageUntransformed (iter, destData, srcData, alpha, x, y, tiledFill);
  2129. }
  2130. template <typename IteratorType>
  2131. void fillWithSolidColour (IteratorType& iter, const PixelARGB colour, bool replaceContents) const
  2132. {
  2133. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2134. switch (destData.pixelFormat)
  2135. {
  2136. case Image::ARGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelARGB*) 0); break;
  2137. case Image::RGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelRGB*) 0); break;
  2138. default: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelAlpha*) 0); break;
  2139. }
  2140. }
  2141. template <typename IteratorType>
  2142. void fillWithGradient (IteratorType& iter, ColourGradient& gradient, const AffineTransform& trans, bool isIdentity) const
  2143. {
  2144. HeapBlock<PixelARGB> lookupTable;
  2145. const int numLookupEntries = gradient.createLookupTable (trans, lookupTable);
  2146. jassert (numLookupEntries > 0);
  2147. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2148. switch (destData.pixelFormat)
  2149. {
  2150. case Image::ARGB: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelARGB*) 0); break;
  2151. case Image::RGB: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelRGB*) 0); break;
  2152. default: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelAlpha*) 0); break;
  2153. }
  2154. }
  2155. //==============================================================================
  2156. Image image;
  2157. Font font;
  2158. private:
  2159. SoftwareRendererSavedState& operator= (const SoftwareRendererSavedState&);
  2160. };
  2161. //==============================================================================
  2162. template <class StateObjectType>
  2163. class SavedStateStack
  2164. {
  2165. public:
  2166. SavedStateStack (StateObjectType* const initialState) noexcept
  2167. : currentState (initialState)
  2168. {}
  2169. SavedStateStack() noexcept {}
  2170. void initialise (StateObjectType* state)
  2171. {
  2172. currentState = state;
  2173. }
  2174. inline StateObjectType* operator->() const noexcept { return currentState; }
  2175. inline StateObjectType& operator*() const noexcept { return *currentState; }
  2176. void save()
  2177. {
  2178. stack.add (new StateObjectType (*currentState));
  2179. }
  2180. void restore()
  2181. {
  2182. if (StateObjectType* const top = stack.getLast())
  2183. {
  2184. currentState = top;
  2185. stack.removeLast (1, false);
  2186. }
  2187. else
  2188. {
  2189. jassertfalse; // trying to pop with an empty stack!
  2190. }
  2191. }
  2192. void beginTransparencyLayer (float opacity)
  2193. {
  2194. save();
  2195. currentState = currentState->beginTransparencyLayer (opacity);
  2196. }
  2197. void endTransparencyLayer()
  2198. {
  2199. const ScopedPointer<StateObjectType> finishedTransparencyLayer (currentState);
  2200. restore();
  2201. currentState->endTransparencyLayer (*finishedTransparencyLayer);
  2202. }
  2203. private:
  2204. ScopedPointer<StateObjectType> currentState;
  2205. OwnedArray<StateObjectType> stack;
  2206. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedStateStack)
  2207. };
  2208. //==============================================================================
  2209. template <class SavedStateType>
  2210. class StackBasedLowLevelGraphicsContext : public LowLevelGraphicsContext
  2211. {
  2212. public:
  2213. bool isVectorDevice() const override { return false; }
  2214. void setOrigin (Point<int> o) override { stack->transform.setOrigin (o); }
  2215. void addTransform (const AffineTransform& t) override { stack->transform.addTransform (t); }
  2216. float getPhysicalPixelScaleFactor() override { return stack->transform.getPhysicalPixelScaleFactor(); }
  2217. Rectangle<int> getClipBounds() const override { return stack->getClipBounds(); }
  2218. bool isClipEmpty() const override { return stack->clip == nullptr; }
  2219. bool clipRegionIntersects (const Rectangle<int>& r) override { return stack->clipRegionIntersects (r); }
  2220. bool clipToRectangle (const Rectangle<int>& r) override { return stack->clipToRectangle (r); }
  2221. bool clipToRectangleList (const RectangleList<int>& r) override { return stack->clipToRectangleList (r); }
  2222. void excludeClipRectangle (const Rectangle<int>& r) override { stack->excludeClipRectangle (r); }
  2223. void clipToPath (const Path& path, const AffineTransform& t) override { stack->clipToPath (path, t); }
  2224. void clipToImageAlpha (const Image& im, const AffineTransform& t) override { stack->clipToImageAlpha (im, t); }
  2225. void saveState() override { stack.save(); }
  2226. void restoreState() override { stack.restore(); }
  2227. void beginTransparencyLayer (float opacity) override { stack.beginTransparencyLayer (opacity); }
  2228. void endTransparencyLayer() override { stack.endTransparencyLayer(); }
  2229. void setFill (const FillType& fillType) override { stack->setFillType (fillType); }
  2230. void setOpacity (float newOpacity) override { stack->fillType.setOpacity (newOpacity); }
  2231. void setInterpolationQuality (Graphics::ResamplingQuality quality) override { stack->interpolationQuality = quality; }
  2232. void fillRect (const Rectangle<int>& r, bool replace) override { stack->fillRect (r, replace); }
  2233. void fillRect (const Rectangle<float>& r) override { stack->fillRect (r); }
  2234. void fillRectList (const RectangleList<float>& list) override { stack->fillRectList (list); }
  2235. void fillPath (const Path& path, const AffineTransform& t) override { stack->fillPath (path, t); }
  2236. void drawImage (const Image& im, const AffineTransform& t) override { stack->drawImage (im, t); }
  2237. void drawGlyph (int glyphNumber, const AffineTransform& t) override { stack->drawGlyph (glyphNumber, t); }
  2238. void drawLine (const Line<float>& line) override { stack->drawLine (line); }
  2239. void setFont (const Font& newFont) override { stack->font = newFont; }
  2240. const Font& getFont() override { return stack->font; }
  2241. protected:
  2242. StackBasedLowLevelGraphicsContext (SavedStateType* initialState) : stack (initialState) {}
  2243. StackBasedLowLevelGraphicsContext() {}
  2244. RenderingHelpers::SavedStateStack<SavedStateType> stack;
  2245. };
  2246. }
  2247. #if JUCE_MSVC
  2248. #pragma warning (pop)
  2249. #endif
  2250. #endif // JUCE_RENDERINGHELPERS_H_INCLUDED