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.

2748 lines
103KB

  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::sqrt (std::abs (complexTransform.getDeterminant()));
  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. case Image::SingleChannel:
  1190. case Image::UnknownFormat:
  1191. default:
  1192. if (tiledFill) { TransformedImageFill<PixelARGB, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1193. else { TransformedImageFill<PixelARGB, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1194. break;
  1195. }
  1196. break;
  1197. case Image::RGB:
  1198. {
  1199. switch (srcData.pixelFormat)
  1200. {
  1201. case Image::ARGB:
  1202. if (tiledFill) { TransformedImageFill<PixelRGB, PixelARGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1203. else { TransformedImageFill<PixelRGB, PixelARGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1204. break;
  1205. case Image::RGB:
  1206. if (tiledFill) { TransformedImageFill<PixelRGB, PixelRGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1207. else { TransformedImageFill<PixelRGB, PixelRGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1208. break;
  1209. case Image::SingleChannel:
  1210. case Image::UnknownFormat:
  1211. default:
  1212. if (tiledFill) { TransformedImageFill<PixelRGB, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1213. else { TransformedImageFill<PixelRGB, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1214. break;
  1215. }
  1216. break;
  1217. }
  1218. case Image::SingleChannel:
  1219. case Image::UnknownFormat:
  1220. default:
  1221. switch (srcData.pixelFormat)
  1222. {
  1223. case Image::ARGB:
  1224. if (tiledFill) { TransformedImageFill<PixelAlpha, PixelARGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1225. else { TransformedImageFill<PixelAlpha, PixelARGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1226. break;
  1227. case Image::RGB:
  1228. if (tiledFill) { TransformedImageFill<PixelAlpha, PixelRGB, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1229. else { TransformedImageFill<PixelAlpha, PixelRGB, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1230. break;
  1231. case Image::SingleChannel:
  1232. case Image::UnknownFormat:
  1233. default:
  1234. if (tiledFill) { TransformedImageFill<PixelAlpha, PixelAlpha, true> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1235. else { TransformedImageFill<PixelAlpha, PixelAlpha, false> r (destData, srcData, transform, alpha, quality); iter.iterate (r); }
  1236. break;
  1237. }
  1238. break;
  1239. }
  1240. }
  1241. template <class Iterator>
  1242. void renderImageUntransformed (Iterator& iter, const Image::BitmapData& destData, const Image::BitmapData& srcData, int alpha, int x, int y, bool tiledFill)
  1243. {
  1244. switch (destData.pixelFormat)
  1245. {
  1246. case Image::ARGB:
  1247. switch (srcData.pixelFormat)
  1248. {
  1249. case Image::ARGB:
  1250. if (tiledFill) { ImageFill<PixelARGB, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1251. else { ImageFill<PixelARGB, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1252. break;
  1253. case Image::RGB:
  1254. if (tiledFill) { ImageFill<PixelARGB, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1255. else { ImageFill<PixelARGB, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1256. break;
  1257. case Image::SingleChannel:
  1258. case Image::UnknownFormat:
  1259. default:
  1260. if (tiledFill) { ImageFill<PixelARGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1261. else { ImageFill<PixelARGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1262. break;
  1263. }
  1264. break;
  1265. case Image::RGB:
  1266. switch (srcData.pixelFormat)
  1267. {
  1268. case Image::ARGB:
  1269. if (tiledFill) { ImageFill<PixelRGB, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1270. else { ImageFill<PixelRGB, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1271. break;
  1272. case Image::RGB:
  1273. if (tiledFill) { ImageFill<PixelRGB, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1274. else { ImageFill<PixelRGB, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1275. break;
  1276. case Image::SingleChannel:
  1277. case Image::UnknownFormat:
  1278. default:
  1279. if (tiledFill) { ImageFill<PixelRGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1280. else { ImageFill<PixelRGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1281. break;
  1282. }
  1283. break;
  1284. case Image::SingleChannel:
  1285. case Image::UnknownFormat:
  1286. default:
  1287. switch (srcData.pixelFormat)
  1288. {
  1289. case Image::ARGB:
  1290. if (tiledFill) { ImageFill<PixelAlpha, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1291. else { ImageFill<PixelAlpha, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1292. break;
  1293. case Image::RGB:
  1294. if (tiledFill) { ImageFill<PixelAlpha, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1295. else { ImageFill<PixelAlpha, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1296. break;
  1297. case Image::SingleChannel:
  1298. case Image::UnknownFormat:
  1299. default:
  1300. if (tiledFill) { ImageFill<PixelAlpha, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1301. else { ImageFill<PixelAlpha, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1302. break;
  1303. }
  1304. break;
  1305. }
  1306. }
  1307. template <class Iterator, class DestPixelType>
  1308. void renderSolidFill (Iterator& iter, const Image::BitmapData& destData, PixelARGB fillColour, bool replaceContents, DestPixelType*)
  1309. {
  1310. if (replaceContents)
  1311. {
  1312. EdgeTableFillers::SolidColour<DestPixelType, true> r (destData, fillColour);
  1313. iter.iterate (r);
  1314. }
  1315. else
  1316. {
  1317. EdgeTableFillers::SolidColour<DestPixelType, false> r (destData, fillColour);
  1318. iter.iterate (r);
  1319. }
  1320. }
  1321. template <class Iterator, class DestPixelType>
  1322. void renderGradient (Iterator& iter, const Image::BitmapData& destData, const ColourGradient& g, const AffineTransform& transform,
  1323. const PixelARGB* lookupTable, int numLookupEntries, bool isIdentity, DestPixelType*)
  1324. {
  1325. if (g.isRadial)
  1326. {
  1327. if (isIdentity)
  1328. {
  1329. EdgeTableFillers::Gradient<DestPixelType, GradientPixelIterators::Radial> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1330. iter.iterate (renderer);
  1331. }
  1332. else
  1333. {
  1334. EdgeTableFillers::Gradient<DestPixelType, GradientPixelIterators::TransformedRadial> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1335. iter.iterate (renderer);
  1336. }
  1337. }
  1338. else
  1339. {
  1340. EdgeTableFillers::Gradient<DestPixelType, GradientPixelIterators::Linear> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1341. iter.iterate (renderer);
  1342. }
  1343. }
  1344. }
  1345. //==============================================================================
  1346. template <class SavedStateType>
  1347. struct ClipRegions
  1348. {
  1349. struct Base : public SingleThreadedReferenceCountedObject
  1350. {
  1351. Base() = default;
  1352. ~Base() override = default;
  1353. using Ptr = ReferenceCountedObjectPtr<Base>;
  1354. virtual Ptr clone() const = 0;
  1355. virtual Ptr applyClipTo (const Ptr& target) const = 0;
  1356. virtual Ptr clipToRectangle (Rectangle<int>) = 0;
  1357. virtual Ptr clipToRectangleList (const RectangleList<int>&) = 0;
  1358. virtual Ptr excludeClipRectangle (Rectangle<int>) = 0;
  1359. virtual Ptr clipToPath (const Path&, const AffineTransform&) = 0;
  1360. virtual Ptr clipToEdgeTable (const EdgeTable&) = 0;
  1361. virtual Ptr clipToImageAlpha (const Image&, const AffineTransform&, Graphics::ResamplingQuality) = 0;
  1362. virtual void translate (Point<int> delta) = 0;
  1363. virtual bool clipRegionIntersects (Rectangle<int>) const = 0;
  1364. virtual Rectangle<int> getClipBounds() const = 0;
  1365. virtual void fillRectWithColour (SavedStateType&, Rectangle<int>, PixelARGB colour, bool replaceContents) const = 0;
  1366. virtual void fillRectWithColour (SavedStateType&, Rectangle<float>, PixelARGB colour) const = 0;
  1367. virtual void fillAllWithColour (SavedStateType&, PixelARGB colour, bool replaceContents) const = 0;
  1368. virtual void fillAllWithGradient (SavedStateType&, ColourGradient&, const AffineTransform&, bool isIdentity) const = 0;
  1369. virtual void renderImageTransformed (SavedStateType&, const Image&, int alpha, const AffineTransform&, Graphics::ResamplingQuality, bool tiledFill) const = 0;
  1370. virtual void renderImageUntransformed (SavedStateType&, const Image&, int alpha, int x, int y, bool tiledFill) const = 0;
  1371. };
  1372. //==============================================================================
  1373. struct EdgeTableRegion : public Base
  1374. {
  1375. EdgeTableRegion (const EdgeTable& e) : edgeTable (e) {}
  1376. EdgeTableRegion (Rectangle<int> r) : edgeTable (r) {}
  1377. EdgeTableRegion (Rectangle<float> r) : edgeTable (r) {}
  1378. EdgeTableRegion (const RectangleList<int>& r) : edgeTable (r) {}
  1379. EdgeTableRegion (const RectangleList<float>& r) : edgeTable (r) {}
  1380. EdgeTableRegion (Rectangle<int> bounds, const Path& p, const AffineTransform& t) : edgeTable (bounds, p, t) {}
  1381. EdgeTableRegion (const EdgeTableRegion& other) : Base(), edgeTable (other.edgeTable) {}
  1382. EdgeTableRegion& operator= (const EdgeTableRegion&) = delete;
  1383. using Ptr = typename Base::Ptr;
  1384. Ptr clone() const override { return *new EdgeTableRegion (*this); }
  1385. Ptr applyClipTo (const Ptr& target) const override { return target->clipToEdgeTable (edgeTable); }
  1386. Ptr clipToRectangle (Rectangle<int> r) override
  1387. {
  1388. edgeTable.clipToRectangle (r);
  1389. return edgeTable.isEmpty() ? Ptr() : Ptr (*this);
  1390. }
  1391. Ptr clipToRectangleList (const RectangleList<int>& r) override
  1392. {
  1393. RectangleList<int> inverse (edgeTable.getMaximumBounds());
  1394. if (inverse.subtract (r))
  1395. for (auto& i : inverse)
  1396. edgeTable.excludeRectangle (i);
  1397. return edgeTable.isEmpty() ? Ptr() : Ptr (*this);
  1398. }
  1399. Ptr excludeClipRectangle (Rectangle<int> r) override
  1400. {
  1401. edgeTable.excludeRectangle (r);
  1402. return edgeTable.isEmpty() ? Ptr() : Ptr (*this);
  1403. }
  1404. Ptr clipToPath (const Path& p, const AffineTransform& transform) override
  1405. {
  1406. EdgeTable et (edgeTable.getMaximumBounds(), p, transform);
  1407. edgeTable.clipToEdgeTable (et);
  1408. return edgeTable.isEmpty() ? Ptr() : Ptr (*this);
  1409. }
  1410. Ptr clipToEdgeTable (const EdgeTable& et) override
  1411. {
  1412. edgeTable.clipToEdgeTable (et);
  1413. return edgeTable.isEmpty() ? Ptr() : Ptr (*this);
  1414. }
  1415. Ptr clipToImageAlpha (const Image& image, const AffineTransform& transform, Graphics::ResamplingQuality quality) override
  1416. {
  1417. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  1418. if (transform.isOnlyTranslation())
  1419. {
  1420. // If our translation doesn't involve any distortion, just use a simple blit..
  1421. auto tx = (int) (transform.getTranslationX() * 256.0f);
  1422. auto ty = (int) (transform.getTranslationY() * 256.0f);
  1423. if (quality == Graphics::lowResamplingQuality || ((tx | ty) & 224) == 0)
  1424. {
  1425. auto imageX = ((tx + 128) >> 8);
  1426. auto imageY = ((ty + 128) >> 8);
  1427. if (image.getFormat() == Image::ARGB)
  1428. straightClipImage (srcData, imageX, imageY, (PixelARGB*) nullptr);
  1429. else
  1430. straightClipImage (srcData, imageX, imageY, (PixelAlpha*) nullptr);
  1431. return edgeTable.isEmpty() ? Ptr() : Ptr (*this);
  1432. }
  1433. }
  1434. if (transform.isSingularity())
  1435. return Ptr();
  1436. {
  1437. Path p;
  1438. p.addRectangle (0, 0, (float) srcData.width, (float) srcData.height);
  1439. EdgeTable et2 (edgeTable.getMaximumBounds(), p, transform);
  1440. edgeTable.clipToEdgeTable (et2);
  1441. }
  1442. if (! edgeTable.isEmpty())
  1443. {
  1444. if (image.getFormat() == Image::ARGB)
  1445. transformedClipImage (srcData, transform, quality, (PixelARGB*) nullptr);
  1446. else
  1447. transformedClipImage (srcData, transform, quality, (PixelAlpha*) nullptr);
  1448. }
  1449. return edgeTable.isEmpty() ? Ptr() : Ptr (*this);
  1450. }
  1451. void translate (Point<int> delta) override
  1452. {
  1453. edgeTable.translate ((float) delta.x, delta.y);
  1454. }
  1455. bool clipRegionIntersects (Rectangle<int> r) const override
  1456. {
  1457. return edgeTable.getMaximumBounds().intersects (r);
  1458. }
  1459. Rectangle<int> getClipBounds() const override
  1460. {
  1461. return edgeTable.getMaximumBounds();
  1462. }
  1463. void fillRectWithColour (SavedStateType& state, Rectangle<int> area, PixelARGB colour, bool replaceContents) const override
  1464. {
  1465. auto totalClip = edgeTable.getMaximumBounds();
  1466. auto clipped = totalClip.getIntersection (area);
  1467. if (! clipped.isEmpty())
  1468. {
  1469. EdgeTableRegion et (clipped);
  1470. et.edgeTable.clipToEdgeTable (edgeTable);
  1471. state.fillWithSolidColour (et.edgeTable, colour, replaceContents);
  1472. }
  1473. }
  1474. void fillRectWithColour (SavedStateType& state, Rectangle<float> area, PixelARGB colour) const override
  1475. {
  1476. auto totalClip = edgeTable.getMaximumBounds().toFloat();
  1477. auto clipped = totalClip.getIntersection (area);
  1478. if (! clipped.isEmpty())
  1479. {
  1480. EdgeTableRegion et (clipped);
  1481. et.edgeTable.clipToEdgeTable (edgeTable);
  1482. state.fillWithSolidColour (et.edgeTable, colour, false);
  1483. }
  1484. }
  1485. void fillAllWithColour (SavedStateType& state, PixelARGB colour, bool replaceContents) const override
  1486. {
  1487. state.fillWithSolidColour (edgeTable, colour, replaceContents);
  1488. }
  1489. void fillAllWithGradient (SavedStateType& state, ColourGradient& gradient, const AffineTransform& transform, bool isIdentity) const override
  1490. {
  1491. state.fillWithGradient (edgeTable, gradient, transform, isIdentity);
  1492. }
  1493. void renderImageTransformed (SavedStateType& state, const Image& src, int alpha, const AffineTransform& transform, Graphics::ResamplingQuality quality, bool tiledFill) const override
  1494. {
  1495. state.renderImageTransformed (edgeTable, src, alpha, transform, quality, tiledFill);
  1496. }
  1497. void renderImageUntransformed (SavedStateType& state, const Image& src, int alpha, int x, int y, bool tiledFill) const override
  1498. {
  1499. state.renderImageUntransformed (edgeTable, src, alpha, x, y, tiledFill);
  1500. }
  1501. EdgeTable edgeTable;
  1502. private:
  1503. template <class SrcPixelType>
  1504. void transformedClipImage (const Image::BitmapData& srcData, const AffineTransform& transform, Graphics::ResamplingQuality quality, const SrcPixelType*)
  1505. {
  1506. EdgeTableFillers::TransformedImageFill<SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, transform, 255, quality);
  1507. for (int y = 0; y < edgeTable.getMaximumBounds().getHeight(); ++y)
  1508. renderer.clipEdgeTableLine (edgeTable, edgeTable.getMaximumBounds().getX(), y + edgeTable.getMaximumBounds().getY(),
  1509. edgeTable.getMaximumBounds().getWidth());
  1510. }
  1511. template <class SrcPixelType>
  1512. void straightClipImage (const Image::BitmapData& srcData, int imageX, int imageY, const SrcPixelType*)
  1513. {
  1514. Rectangle<int> r (imageX, imageY, srcData.width, srcData.height);
  1515. edgeTable.clipToRectangle (r);
  1516. EdgeTableFillers::ImageFill<SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, 255, imageX, imageY);
  1517. for (int y = 0; y < r.getHeight(); ++y)
  1518. renderer.clipEdgeTableLine (edgeTable, r.getX(), y + r.getY(), r.getWidth());
  1519. }
  1520. };
  1521. //==============================================================================
  1522. class RectangleListRegion : public Base
  1523. {
  1524. public:
  1525. RectangleListRegion (Rectangle<int> r) : clip (r) {}
  1526. RectangleListRegion (const RectangleList<int>& r) : clip (r) {}
  1527. RectangleListRegion (const RectangleListRegion& other) : Base(), clip (other.clip) {}
  1528. using Ptr = typename Base::Ptr;
  1529. Ptr clone() const override { return *new RectangleListRegion (*this); }
  1530. Ptr applyClipTo (const Ptr& target) const override { return target->clipToRectangleList (clip); }
  1531. Ptr clipToRectangle (Rectangle<int> r) override
  1532. {
  1533. clip.clipTo (r);
  1534. return clip.isEmpty() ? Ptr() : Ptr (*this);
  1535. }
  1536. Ptr clipToRectangleList (const RectangleList<int>& r) override
  1537. {
  1538. clip.clipTo (r);
  1539. return clip.isEmpty() ? Ptr() : Ptr (*this);
  1540. }
  1541. Ptr excludeClipRectangle (Rectangle<int> r) override
  1542. {
  1543. clip.subtract (r);
  1544. return clip.isEmpty() ? Ptr() : Ptr (*this);
  1545. }
  1546. Ptr clipToPath (const Path& p, const AffineTransform& transform) override { return toEdgeTable()->clipToPath (p, transform); }
  1547. Ptr clipToEdgeTable (const EdgeTable& et) override { return toEdgeTable()->clipToEdgeTable (et); }
  1548. Ptr clipToImageAlpha (const Image& image, const AffineTransform& transform, Graphics::ResamplingQuality quality) override
  1549. {
  1550. return toEdgeTable()->clipToImageAlpha (image, transform, quality);
  1551. }
  1552. void translate (Point<int> delta) override { clip.offsetAll (delta); }
  1553. bool clipRegionIntersects (Rectangle<int> r) const override { return clip.intersects (r); }
  1554. Rectangle<int> getClipBounds() const override { return clip.getBounds(); }
  1555. void fillRectWithColour (SavedStateType& state, Rectangle<int> area, PixelARGB colour, bool replaceContents) const override
  1556. {
  1557. SubRectangleIterator iter (clip, area);
  1558. state.fillWithSolidColour (iter, colour, replaceContents);
  1559. }
  1560. void fillRectWithColour (SavedStateType& state, Rectangle<float> area, PixelARGB colour) const override
  1561. {
  1562. SubRectangleIteratorFloat iter (clip, area);
  1563. state.fillWithSolidColour (iter, colour, false);
  1564. }
  1565. void fillAllWithColour (SavedStateType& state, PixelARGB colour, bool replaceContents) const override
  1566. {
  1567. state.fillWithSolidColour (*this, colour, replaceContents);
  1568. }
  1569. void fillAllWithGradient (SavedStateType& state, ColourGradient& gradient, const AffineTransform& transform, bool isIdentity) const override
  1570. {
  1571. state.fillWithGradient (*this, gradient, transform, isIdentity);
  1572. }
  1573. void renderImageTransformed (SavedStateType& state, const Image& src, int alpha, const AffineTransform& transform, Graphics::ResamplingQuality quality, bool tiledFill) const override
  1574. {
  1575. state.renderImageTransformed (*this, src, alpha, transform, quality, tiledFill);
  1576. }
  1577. void renderImageUntransformed (SavedStateType& state, const Image& src, int alpha, int x, int y, bool tiledFill) const override
  1578. {
  1579. state.renderImageUntransformed (*this, src, alpha, x, y, tiledFill);
  1580. }
  1581. RectangleList<int> clip;
  1582. //==============================================================================
  1583. template <class Renderer>
  1584. void iterate (Renderer& r) const noexcept
  1585. {
  1586. for (auto& i : clip)
  1587. {
  1588. auto x = i.getX();
  1589. auto w = i.getWidth();
  1590. jassert (w > 0);
  1591. auto bottom = i.getBottom();
  1592. for (int y = i.getY(); y < bottom; ++y)
  1593. {
  1594. r.setEdgeTableYPos (y);
  1595. r.handleEdgeTableLineFull (x, w);
  1596. }
  1597. }
  1598. }
  1599. private:
  1600. //==============================================================================
  1601. class SubRectangleIterator
  1602. {
  1603. public:
  1604. SubRectangleIterator (const RectangleList<int>& clipList, Rectangle<int> clipBounds)
  1605. : clip (clipList), area (clipBounds)
  1606. {}
  1607. template <class Renderer>
  1608. void iterate (Renderer& r) const noexcept
  1609. {
  1610. for (auto& i : clip)
  1611. {
  1612. auto rect = i.getIntersection (area);
  1613. if (! rect.isEmpty())
  1614. r.handleEdgeTableRectangleFull (rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
  1615. }
  1616. }
  1617. private:
  1618. const RectangleList<int>& clip;
  1619. const Rectangle<int> area;
  1620. JUCE_DECLARE_NON_COPYABLE (SubRectangleIterator)
  1621. };
  1622. //==============================================================================
  1623. class SubRectangleIteratorFloat
  1624. {
  1625. public:
  1626. SubRectangleIteratorFloat (const RectangleList<int>& clipList, Rectangle<float> clipBounds) noexcept
  1627. : clip (clipList), area (clipBounds)
  1628. {
  1629. }
  1630. template <class Renderer>
  1631. void iterate (Renderer& r) const noexcept
  1632. {
  1633. const RenderingHelpers::FloatRectangleRasterisingInfo f (area);
  1634. for (auto& i : clip)
  1635. {
  1636. auto clipLeft = i.getX();
  1637. auto clipRight = i.getRight();
  1638. auto clipTop = i.getY();
  1639. auto clipBottom = i.getBottom();
  1640. if (f.totalBottom > clipTop && f.totalTop < clipBottom
  1641. && f.totalRight > clipLeft && f.totalLeft < clipRight)
  1642. {
  1643. if (f.isOnePixelWide())
  1644. {
  1645. if (f.topAlpha != 0 && f.totalTop >= clipTop)
  1646. {
  1647. r.setEdgeTableYPos (f.totalTop);
  1648. r.handleEdgeTablePixel (f.left, f.topAlpha);
  1649. }
  1650. auto y1 = jmax (clipTop, f.top);
  1651. auto y2 = jmin (f.bottom, clipBottom);
  1652. auto h = y2 - y1;
  1653. if (h > 0)
  1654. r.handleEdgeTableRectangleFull (f.left, y1, 1, h);
  1655. if (f.bottomAlpha != 0 && f.bottom < clipBottom)
  1656. {
  1657. r.setEdgeTableYPos (f.bottom);
  1658. r.handleEdgeTablePixel (f.left, f.bottomAlpha);
  1659. }
  1660. }
  1661. else
  1662. {
  1663. auto clippedLeft = jmax (f.left, clipLeft);
  1664. auto clippedWidth = jmin (f.right, clipRight) - clippedLeft;
  1665. bool doLeftAlpha = f.leftAlpha != 0 && f.totalLeft >= clipLeft;
  1666. bool doRightAlpha = f.rightAlpha != 0 && f.right < clipRight;
  1667. if (f.topAlpha != 0 && f.totalTop >= clipTop)
  1668. {
  1669. r.setEdgeTableYPos (f.totalTop);
  1670. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.getTopLeftCornerAlpha());
  1671. if (clippedWidth > 0) r.handleEdgeTableLine (clippedLeft, clippedWidth, f.topAlpha);
  1672. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.getTopRightCornerAlpha());
  1673. }
  1674. auto y1 = jmax (clipTop, f.top);
  1675. auto y2 = jmin (f.bottom, clipBottom);
  1676. auto h = y2 - y1;
  1677. if (h > 0)
  1678. {
  1679. if (h == 1)
  1680. {
  1681. r.setEdgeTableYPos (y1);
  1682. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.leftAlpha);
  1683. if (clippedWidth > 0) r.handleEdgeTableLineFull (clippedLeft, clippedWidth);
  1684. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.rightAlpha);
  1685. }
  1686. else
  1687. {
  1688. if (doLeftAlpha) r.handleEdgeTableRectangle (f.totalLeft, y1, 1, h, f.leftAlpha);
  1689. if (clippedWidth > 0) r.handleEdgeTableRectangleFull (clippedLeft, y1, clippedWidth, h);
  1690. if (doRightAlpha) r.handleEdgeTableRectangle (f.right, y1, 1, h, f.rightAlpha);
  1691. }
  1692. }
  1693. if (f.bottomAlpha != 0 && f.bottom < clipBottom)
  1694. {
  1695. r.setEdgeTableYPos (f.bottom);
  1696. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.getBottomLeftCornerAlpha());
  1697. if (clippedWidth > 0) r.handleEdgeTableLine (clippedLeft, clippedWidth, f.bottomAlpha);
  1698. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.getBottomRightCornerAlpha());
  1699. }
  1700. }
  1701. }
  1702. }
  1703. }
  1704. private:
  1705. const RectangleList<int>& clip;
  1706. Rectangle<float> area;
  1707. JUCE_DECLARE_NON_COPYABLE (SubRectangleIteratorFloat)
  1708. };
  1709. Ptr toEdgeTable() const { return *new EdgeTableRegion (clip); }
  1710. RectangleListRegion& operator= (const RectangleListRegion&) = delete;
  1711. };
  1712. };
  1713. //==============================================================================
  1714. template <class SavedStateType>
  1715. class SavedStateBase
  1716. {
  1717. public:
  1718. using BaseRegionType = typename ClipRegions<SavedStateType>::Base;
  1719. using EdgeTableRegionType = typename ClipRegions<SavedStateType>::EdgeTableRegion;
  1720. using RectangleListRegionType = typename ClipRegions<SavedStateType>::RectangleListRegion;
  1721. SavedStateBase (Rectangle<int> initialClip)
  1722. : clip (new RectangleListRegionType (initialClip)),
  1723. interpolationQuality (Graphics::mediumResamplingQuality), transparencyLayerAlpha (1.0f)
  1724. {
  1725. }
  1726. SavedStateBase (const RectangleList<int>& clipList, Point<int> origin)
  1727. : clip (new RectangleListRegionType (clipList)), transform (origin),
  1728. interpolationQuality (Graphics::mediumResamplingQuality), transparencyLayerAlpha (1.0f)
  1729. {
  1730. }
  1731. SavedStateBase (const SavedStateBase& other)
  1732. : clip (other.clip), transform (other.transform), fillType (other.fillType),
  1733. interpolationQuality (other.interpolationQuality),
  1734. transparencyLayerAlpha (other.transparencyLayerAlpha)
  1735. {
  1736. }
  1737. SavedStateType& getThis() noexcept { return *static_cast<SavedStateType*> (this); }
  1738. bool clipToRectangle (Rectangle<int> r)
  1739. {
  1740. if (clip != nullptr)
  1741. {
  1742. if (transform.isOnlyTranslated)
  1743. {
  1744. cloneClipIfMultiplyReferenced();
  1745. clip = clip->clipToRectangle (transform.translated (r));
  1746. }
  1747. else if (! transform.isRotated)
  1748. {
  1749. cloneClipIfMultiplyReferenced();
  1750. clip = clip->clipToRectangle (transform.transformed (r));
  1751. }
  1752. else
  1753. {
  1754. Path p;
  1755. p.addRectangle (r);
  1756. clipToPath (p, {});
  1757. }
  1758. }
  1759. return clip != nullptr;
  1760. }
  1761. bool clipToRectangleList (const RectangleList<int>& r)
  1762. {
  1763. if (clip != nullptr)
  1764. {
  1765. if (transform.isOnlyTranslated)
  1766. {
  1767. cloneClipIfMultiplyReferenced();
  1768. if (transform.isIdentity())
  1769. {
  1770. clip = clip->clipToRectangleList (r);
  1771. }
  1772. else
  1773. {
  1774. RectangleList<int> offsetList (r);
  1775. offsetList.offsetAll (transform.offset);
  1776. clip = clip->clipToRectangleList (offsetList);
  1777. }
  1778. }
  1779. else if (! transform.isRotated)
  1780. {
  1781. cloneClipIfMultiplyReferenced();
  1782. RectangleList<int> scaledList;
  1783. for (auto& i : r)
  1784. scaledList.add (transform.transformed (i));
  1785. clip = clip->clipToRectangleList (scaledList);
  1786. }
  1787. else
  1788. {
  1789. clipToPath (r.toPath(), {});
  1790. }
  1791. }
  1792. return clip != nullptr;
  1793. }
  1794. static Rectangle<int> getLargestIntegerWithin (Rectangle<float> r)
  1795. {
  1796. auto x1 = (int) std::ceil (r.getX());
  1797. auto y1 = (int) std::ceil (r.getY());
  1798. auto x2 = (int) std::floor (r.getRight());
  1799. auto y2 = (int) std::floor (r.getBottom());
  1800. return { x1, y1, x2 - x1, y2 - y1 };
  1801. }
  1802. bool excludeClipRectangle (Rectangle<int> r)
  1803. {
  1804. if (clip != nullptr)
  1805. {
  1806. cloneClipIfMultiplyReferenced();
  1807. if (transform.isOnlyTranslated)
  1808. {
  1809. clip = clip->excludeClipRectangle (getLargestIntegerWithin (transform.translated (r.toFloat())));
  1810. }
  1811. else if (! transform.isRotated)
  1812. {
  1813. clip = clip->excludeClipRectangle (getLargestIntegerWithin (transform.transformed (r.toFloat())));
  1814. }
  1815. else
  1816. {
  1817. Path p;
  1818. p.addRectangle (r.toFloat());
  1819. p.applyTransform (transform.complexTransform);
  1820. p.addRectangle (clip->getClipBounds().toFloat());
  1821. p.setUsingNonZeroWinding (false);
  1822. clip = clip->clipToPath (p, {});
  1823. }
  1824. }
  1825. return clip != nullptr;
  1826. }
  1827. void clipToPath (const Path& p, const AffineTransform& t)
  1828. {
  1829. if (clip != nullptr)
  1830. {
  1831. cloneClipIfMultiplyReferenced();
  1832. clip = clip->clipToPath (p, transform.getTransformWith (t));
  1833. }
  1834. }
  1835. void clipToImageAlpha (const Image& sourceImage, const AffineTransform& t)
  1836. {
  1837. if (clip != nullptr)
  1838. {
  1839. if (sourceImage.hasAlphaChannel())
  1840. {
  1841. cloneClipIfMultiplyReferenced();
  1842. clip = clip->clipToImageAlpha (sourceImage, transform.getTransformWith (t), interpolationQuality);
  1843. }
  1844. else
  1845. {
  1846. Path p;
  1847. p.addRectangle (sourceImage.getBounds());
  1848. clipToPath (p, t);
  1849. }
  1850. }
  1851. }
  1852. bool clipRegionIntersects (Rectangle<int> r) const
  1853. {
  1854. if (clip != nullptr)
  1855. {
  1856. if (transform.isOnlyTranslated)
  1857. return clip->clipRegionIntersects (transform.translated (r));
  1858. return getClipBounds().intersects (r);
  1859. }
  1860. return false;
  1861. }
  1862. Rectangle<int> getClipBounds() const
  1863. {
  1864. return clip != nullptr ? transform.deviceSpaceToUserSpace (clip->getClipBounds())
  1865. : Rectangle<int>();
  1866. }
  1867. void setFillType (const FillType& newFill)
  1868. {
  1869. fillType = newFill;
  1870. }
  1871. void fillTargetRect (Rectangle<int> r, bool replaceContents)
  1872. {
  1873. if (fillType.isColour())
  1874. {
  1875. clip->fillRectWithColour (getThis(), r, fillType.colour.getPixelARGB(), replaceContents);
  1876. }
  1877. else
  1878. {
  1879. auto clipped = clip->getClipBounds().getIntersection (r);
  1880. if (! clipped.isEmpty())
  1881. fillShape (*new RectangleListRegionType (clipped), false);
  1882. }
  1883. }
  1884. void fillTargetRect (Rectangle<float> r)
  1885. {
  1886. if (fillType.isColour())
  1887. {
  1888. clip->fillRectWithColour (getThis(), r, fillType.colour.getPixelARGB());
  1889. }
  1890. else
  1891. {
  1892. auto clipped = clip->getClipBounds().toFloat().getIntersection (r);
  1893. if (! clipped.isEmpty())
  1894. fillShape (*new EdgeTableRegionType (clipped), false);
  1895. }
  1896. }
  1897. template <typename CoordType>
  1898. void fillRectAsPath (Rectangle<CoordType> r)
  1899. {
  1900. Path p;
  1901. p.addRectangle (r);
  1902. fillPath (p, {});
  1903. }
  1904. void fillRect (Rectangle<int> r, bool replaceContents)
  1905. {
  1906. if (clip != nullptr)
  1907. {
  1908. if (transform.isOnlyTranslated)
  1909. {
  1910. fillTargetRect (transform.translated (r), replaceContents);
  1911. }
  1912. else if (! transform.isRotated)
  1913. {
  1914. fillTargetRect (transform.transformed (r), replaceContents);
  1915. }
  1916. else
  1917. {
  1918. jassert (! replaceContents); // not implemented..
  1919. fillRectAsPath (r);
  1920. }
  1921. }
  1922. }
  1923. void fillRect (Rectangle<float> r)
  1924. {
  1925. if (clip != nullptr)
  1926. {
  1927. if (transform.isOnlyTranslated)
  1928. fillTargetRect (transform.translated (r));
  1929. else if (! transform.isRotated)
  1930. fillTargetRect (transform.transformed (r));
  1931. else
  1932. fillRectAsPath (r);
  1933. }
  1934. }
  1935. void fillRectList (const RectangleList<float>& list)
  1936. {
  1937. if (clip != nullptr)
  1938. {
  1939. if (list.getNumRectangles() == 1)
  1940. return fillRect (*list.begin());
  1941. if (transform.isIdentity())
  1942. {
  1943. fillShape (*new EdgeTableRegionType (list), false);
  1944. }
  1945. else if (! transform.isRotated)
  1946. {
  1947. RectangleList<float> transformed (list);
  1948. if (transform.isOnlyTranslated)
  1949. transformed.offsetAll (transform.offset.toFloat());
  1950. else
  1951. transformed.transformAll (transform.getTransform());
  1952. fillShape (*new EdgeTableRegionType (transformed), false);
  1953. }
  1954. else
  1955. {
  1956. fillPath (list.toPath(), {});
  1957. }
  1958. }
  1959. }
  1960. void fillPath (const Path& path, const AffineTransform& t)
  1961. {
  1962. if (clip != nullptr)
  1963. {
  1964. auto trans = transform.getTransformWith (t);
  1965. auto clipRect = clip->getClipBounds();
  1966. if (path.getBoundsTransformed (trans).getSmallestIntegerContainer().intersects (clipRect))
  1967. fillShape (*new EdgeTableRegionType (clipRect, path, trans), false);
  1968. }
  1969. }
  1970. void fillEdgeTable (const EdgeTable& edgeTable, float x, int y)
  1971. {
  1972. if (clip != nullptr)
  1973. {
  1974. auto* edgeTableClip = new EdgeTableRegionType (edgeTable);
  1975. edgeTableClip->edgeTable.translate (x, y);
  1976. if (fillType.isColour())
  1977. {
  1978. auto brightness = fillType.colour.getBrightness() - 0.5f;
  1979. if (brightness > 0.0f)
  1980. edgeTableClip->edgeTable.multiplyLevels (1.0f + 1.6f * brightness);
  1981. }
  1982. fillShape (*edgeTableClip, false);
  1983. }
  1984. }
  1985. void drawLine (Line<float> line)
  1986. {
  1987. Path p;
  1988. p.addLineSegment (line, 1.0f);
  1989. fillPath (p, {});
  1990. }
  1991. void drawImage (const Image& sourceImage, const AffineTransform& trans)
  1992. {
  1993. if (clip != nullptr && ! fillType.colour.isTransparent())
  1994. renderImage (sourceImage, trans, {});
  1995. }
  1996. static bool isOnlyTranslationAllowingError (const AffineTransform& t, float tolerance) noexcept
  1997. {
  1998. return std::abs (t.mat01) < tolerance
  1999. && std::abs (t.mat10) < tolerance
  2000. && std::abs (t.mat00 - 1.0f) < tolerance
  2001. && std::abs (t.mat11 - 1.0f) < tolerance;
  2002. }
  2003. void renderImage (const Image& sourceImage, const AffineTransform& trans, const BaseRegionType* tiledFillClipRegion)
  2004. {
  2005. auto t = transform.getTransformWith (trans);
  2006. auto alpha = fillType.colour.getAlpha();
  2007. if (isOnlyTranslationAllowingError (t, 0.002f))
  2008. {
  2009. // If our translation doesn't involve any distortion, just use a simple blit..
  2010. auto tx = (int) (t.getTranslationX() * 256.0f);
  2011. auto ty = (int) (t.getTranslationY() * 256.0f);
  2012. if (interpolationQuality == Graphics::lowResamplingQuality || ((tx | ty) & 224) == 0)
  2013. {
  2014. tx = ((tx + 128) >> 8);
  2015. ty = ((ty + 128) >> 8);
  2016. if (tiledFillClipRegion != nullptr)
  2017. {
  2018. tiledFillClipRegion->renderImageUntransformed (getThis(), sourceImage, alpha, tx, ty, true);
  2019. }
  2020. else
  2021. {
  2022. Rectangle<int> area (tx, ty, sourceImage.getWidth(), sourceImage.getHeight());
  2023. area = area.getIntersection (getThis().getMaximumBounds());
  2024. if (! area.isEmpty())
  2025. if (auto c = clip->applyClipTo (*new EdgeTableRegionType (area)))
  2026. c->renderImageUntransformed (getThis(), sourceImage, alpha, tx, ty, false);
  2027. }
  2028. return;
  2029. }
  2030. }
  2031. if (! t.isSingularity())
  2032. {
  2033. if (tiledFillClipRegion != nullptr)
  2034. {
  2035. tiledFillClipRegion->renderImageTransformed (getThis(), sourceImage, alpha,
  2036. t, interpolationQuality, true);
  2037. }
  2038. else
  2039. {
  2040. Path p;
  2041. p.addRectangle (sourceImage.getBounds());
  2042. if (auto c = clip->clone()->clipToPath (p, t))
  2043. c->renderImageTransformed (getThis(), sourceImage, alpha,
  2044. t, interpolationQuality, false);
  2045. }
  2046. }
  2047. }
  2048. void fillShape (typename BaseRegionType::Ptr shapeToFill, bool replaceContents)
  2049. {
  2050. jassert (clip != nullptr);
  2051. shapeToFill = clip->applyClipTo (shapeToFill);
  2052. if (shapeToFill != nullptr)
  2053. {
  2054. if (fillType.isGradient())
  2055. {
  2056. jassert (! replaceContents); // that option is just for solid colours
  2057. auto g2 = *(fillType.gradient);
  2058. g2.multiplyOpacity (fillType.getOpacity());
  2059. auto t = transform.getTransformWith (fillType.transform).translated (-0.5f, -0.5f);
  2060. bool isIdentity = t.isOnlyTranslation();
  2061. if (isIdentity)
  2062. {
  2063. // If our translation doesn't involve any distortion, we can speed it up..
  2064. g2.point1.applyTransform (t);
  2065. g2.point2.applyTransform (t);
  2066. t = {};
  2067. }
  2068. shapeToFill->fillAllWithGradient (getThis(), g2, t, isIdentity);
  2069. }
  2070. else if (fillType.isTiledImage())
  2071. {
  2072. renderImage (fillType.image, fillType.transform, shapeToFill.get());
  2073. }
  2074. else
  2075. {
  2076. shapeToFill->fillAllWithColour (getThis(), fillType.colour.getPixelARGB(), replaceContents);
  2077. }
  2078. }
  2079. }
  2080. void cloneClipIfMultiplyReferenced()
  2081. {
  2082. if (clip->getReferenceCount() > 1)
  2083. clip = clip->clone();
  2084. }
  2085. typename BaseRegionType::Ptr clip;
  2086. RenderingHelpers::TranslationOrTransform transform;
  2087. FillType fillType;
  2088. Graphics::ResamplingQuality interpolationQuality;
  2089. float transparencyLayerAlpha;
  2090. };
  2091. //==============================================================================
  2092. class SoftwareRendererSavedState : public SavedStateBase<SoftwareRendererSavedState>
  2093. {
  2094. using BaseClass = SavedStateBase<SoftwareRendererSavedState>;
  2095. public:
  2096. SoftwareRendererSavedState (const Image& im, Rectangle<int> clipBounds)
  2097. : BaseClass (clipBounds), image (im)
  2098. {
  2099. }
  2100. SoftwareRendererSavedState (const Image& im, const RectangleList<int>& clipList, Point<int> origin)
  2101. : BaseClass (clipList, origin), image (im)
  2102. {
  2103. }
  2104. SoftwareRendererSavedState (const SoftwareRendererSavedState& other) = default;
  2105. SoftwareRendererSavedState* beginTransparencyLayer (float opacity)
  2106. {
  2107. auto* s = new SoftwareRendererSavedState (*this);
  2108. if (clip != nullptr)
  2109. {
  2110. auto layerBounds = clip->getClipBounds();
  2111. s->image = Image (Image::ARGB, layerBounds.getWidth(), layerBounds.getHeight(), true);
  2112. s->transparencyLayerAlpha = opacity;
  2113. s->transform.moveOriginInDeviceSpace (-layerBounds.getPosition());
  2114. s->cloneClipIfMultiplyReferenced();
  2115. s->clip->translate (-layerBounds.getPosition());
  2116. }
  2117. return s;
  2118. }
  2119. void endTransparencyLayer (SoftwareRendererSavedState& finishedLayerState)
  2120. {
  2121. if (clip != nullptr)
  2122. {
  2123. auto layerBounds = clip->getClipBounds();
  2124. auto g = image.createLowLevelContext();
  2125. g->setOpacity (finishedLayerState.transparencyLayerAlpha);
  2126. g->drawImage (finishedLayerState.image, AffineTransform::translation (layerBounds.getPosition()));
  2127. }
  2128. }
  2129. using GlyphCacheType = GlyphCache<CachedGlyphEdgeTable<SoftwareRendererSavedState>, SoftwareRendererSavedState>;
  2130. static void clearGlyphCache()
  2131. {
  2132. GlyphCacheType::getInstance().reset();
  2133. }
  2134. //==============================================================================
  2135. void drawGlyph (int glyphNumber, const AffineTransform& trans)
  2136. {
  2137. if (clip != nullptr)
  2138. {
  2139. if (trans.isOnlyTranslation() && ! transform.isRotated)
  2140. {
  2141. auto& cache = GlyphCacheType::getInstance();
  2142. Point<float> pos (trans.getTranslationX(), trans.getTranslationY());
  2143. if (transform.isOnlyTranslated)
  2144. {
  2145. cache.drawGlyph (*this, font, glyphNumber, pos + transform.offset.toFloat());
  2146. }
  2147. else
  2148. {
  2149. pos = transform.transformed (pos);
  2150. Font f (font);
  2151. f.setHeight (font.getHeight() * transform.complexTransform.mat11);
  2152. auto xScale = transform.complexTransform.mat00 / transform.complexTransform.mat11;
  2153. if (std::abs (xScale - 1.0f) > 0.01f)
  2154. f.setHorizontalScale (xScale);
  2155. cache.drawGlyph (*this, f, glyphNumber, pos);
  2156. }
  2157. }
  2158. else
  2159. {
  2160. auto fontHeight = font.getHeight();
  2161. auto t = transform.getTransformWith (AffineTransform::scale (fontHeight * font.getHorizontalScale(), fontHeight)
  2162. .followedBy (trans));
  2163. std::unique_ptr<EdgeTable> et (font.getTypeface()->getEdgeTableForGlyph (glyphNumber, t, fontHeight));
  2164. if (et != nullptr)
  2165. fillShape (*new EdgeTableRegionType (*et), false);
  2166. }
  2167. }
  2168. }
  2169. Rectangle<int> getMaximumBounds() const { return image.getBounds(); }
  2170. //==============================================================================
  2171. template <typename IteratorType>
  2172. void renderImageTransformed (IteratorType& iter, const Image& src, int alpha, const AffineTransform& trans, Graphics::ResamplingQuality quality, bool tiledFill) const
  2173. {
  2174. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2175. const Image::BitmapData srcData (src, Image::BitmapData::readOnly);
  2176. EdgeTableFillers::renderImageTransformed (iter, destData, srcData, alpha, trans, quality, tiledFill);
  2177. }
  2178. template <typename IteratorType>
  2179. void renderImageUntransformed (IteratorType& iter, const Image& src, int alpha, int x, int y, bool tiledFill) const
  2180. {
  2181. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2182. const Image::BitmapData srcData (src, Image::BitmapData::readOnly);
  2183. EdgeTableFillers::renderImageUntransformed (iter, destData, srcData, alpha, x, y, tiledFill);
  2184. }
  2185. template <typename IteratorType>
  2186. void fillWithSolidColour (IteratorType& iter, PixelARGB colour, bool replaceContents) const
  2187. {
  2188. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2189. switch (destData.pixelFormat)
  2190. {
  2191. case Image::ARGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelARGB*) nullptr); break;
  2192. case Image::RGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelRGB*) nullptr); break;
  2193. case Image::SingleChannel:
  2194. case Image::UnknownFormat:
  2195. default: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelAlpha*) nullptr); break;
  2196. }
  2197. }
  2198. template <typename IteratorType>
  2199. void fillWithGradient (IteratorType& iter, ColourGradient& gradient, const AffineTransform& trans, bool isIdentity) const
  2200. {
  2201. HeapBlock<PixelARGB> lookupTable;
  2202. auto numLookupEntries = gradient.createLookupTable (trans, lookupTable);
  2203. jassert (numLookupEntries > 0);
  2204. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2205. switch (destData.pixelFormat)
  2206. {
  2207. case Image::ARGB: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelARGB*) nullptr); break;
  2208. case Image::RGB: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelRGB*) nullptr); break;
  2209. case Image::SingleChannel:
  2210. case Image::UnknownFormat:
  2211. default: EdgeTableFillers::renderGradient (iter, destData, gradient, trans, lookupTable, numLookupEntries, isIdentity, (PixelAlpha*) nullptr); break;
  2212. }
  2213. }
  2214. //==============================================================================
  2215. Image image;
  2216. Font font;
  2217. private:
  2218. SoftwareRendererSavedState& operator= (const SoftwareRendererSavedState&) = delete;
  2219. };
  2220. //==============================================================================
  2221. template <class StateObjectType>
  2222. class SavedStateStack
  2223. {
  2224. public:
  2225. SavedStateStack (StateObjectType* initialState) noexcept
  2226. : currentState (initialState)
  2227. {}
  2228. SavedStateStack() = default;
  2229. void initialise (StateObjectType* state)
  2230. {
  2231. currentState.reset (state);
  2232. }
  2233. inline StateObjectType* operator->() const noexcept { return currentState.get(); }
  2234. inline StateObjectType& operator*() const noexcept { return *currentState; }
  2235. void save()
  2236. {
  2237. stack.add (new StateObjectType (*currentState));
  2238. }
  2239. void restore()
  2240. {
  2241. if (auto* top = stack.getLast())
  2242. {
  2243. currentState.reset (top);
  2244. stack.removeLast (1, false);
  2245. }
  2246. else
  2247. {
  2248. jassertfalse; // trying to pop with an empty stack!
  2249. }
  2250. }
  2251. void beginTransparencyLayer (float opacity)
  2252. {
  2253. save();
  2254. currentState.reset (currentState->beginTransparencyLayer (opacity));
  2255. }
  2256. void endTransparencyLayer()
  2257. {
  2258. std::unique_ptr<StateObjectType> finishedTransparencyLayer (currentState.release());
  2259. restore();
  2260. currentState->endTransparencyLayer (*finishedTransparencyLayer);
  2261. }
  2262. private:
  2263. std::unique_ptr<StateObjectType> currentState;
  2264. OwnedArray<StateObjectType> stack;
  2265. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedStateStack)
  2266. };
  2267. //==============================================================================
  2268. template <class SavedStateType>
  2269. class StackBasedLowLevelGraphicsContext : public LowLevelGraphicsContext
  2270. {
  2271. public:
  2272. bool isVectorDevice() const override { return false; }
  2273. void setOrigin (Point<int> o) override { stack->transform.setOrigin (o); }
  2274. void addTransform (const AffineTransform& t) override { stack->transform.addTransform (t); }
  2275. float getPhysicalPixelScaleFactor() override { return stack->transform.getPhysicalPixelScaleFactor(); }
  2276. Rectangle<int> getClipBounds() const override { return stack->getClipBounds(); }
  2277. bool isClipEmpty() const override { return stack->clip == nullptr; }
  2278. bool clipRegionIntersects (const Rectangle<int>& r) override { return stack->clipRegionIntersects (r); }
  2279. bool clipToRectangle (const Rectangle<int>& r) override { return stack->clipToRectangle (r); }
  2280. bool clipToRectangleList (const RectangleList<int>& r) override { return stack->clipToRectangleList (r); }
  2281. void excludeClipRectangle (const Rectangle<int>& r) override { stack->excludeClipRectangle (r); }
  2282. void clipToPath (const Path& path, const AffineTransform& t) override { stack->clipToPath (path, t); }
  2283. void clipToImageAlpha (const Image& im, const AffineTransform& t) override { stack->clipToImageAlpha (im, t); }
  2284. void saveState() override { stack.save(); }
  2285. void restoreState() override { stack.restore(); }
  2286. void beginTransparencyLayer (float opacity) override { stack.beginTransparencyLayer (opacity); }
  2287. void endTransparencyLayer() override { stack.endTransparencyLayer(); }
  2288. void setFill (const FillType& fillType) override { stack->setFillType (fillType); }
  2289. void setOpacity (float newOpacity) override { stack->fillType.setOpacity (newOpacity); }
  2290. void setInterpolationQuality (Graphics::ResamplingQuality quality) override { stack->interpolationQuality = quality; }
  2291. void fillRect (const Rectangle<int>& r, bool replace) override { stack->fillRect (r, replace); }
  2292. void fillRect (const Rectangle<float>& r) override { stack->fillRect (r); }
  2293. void fillRectList (const RectangleList<float>& list) override { stack->fillRectList (list); }
  2294. void fillPath (const Path& path, const AffineTransform& t) override { stack->fillPath (path, t); }
  2295. void drawImage (const Image& im, const AffineTransform& t) override { stack->drawImage (im, t); }
  2296. void drawGlyph (int glyphNumber, const AffineTransform& t) override { stack->drawGlyph (glyphNumber, t); }
  2297. void drawLine (const Line<float>& line) override { stack->drawLine (line); }
  2298. void setFont (const Font& newFont) override { stack->font = newFont; }
  2299. const Font& getFont() override { return stack->font; }
  2300. protected:
  2301. StackBasedLowLevelGraphicsContext (SavedStateType* initialState) : stack (initialState) {}
  2302. StackBasedLowLevelGraphicsContext() = default;
  2303. RenderingHelpers::SavedStateStack<SavedStateType> stack;
  2304. };
  2305. }
  2306. #if JUCE_MSVC
  2307. #pragma warning (pop)
  2308. #endif
  2309. } // namespace juce