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

2687 lines
103KB

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