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.

2756 lines
103KB

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