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.

2689 lines
102KB

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