Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2691 lines
102KB

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