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.

2449 lines
93KB

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