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.

2765 lines
104KB

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