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.

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