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.

2770 lines
104KB

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