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.

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