The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2726 lines
102KB

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