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.

2549 lines
96KB

  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 x, int y) noexcept
  33. : xOffset (x), yOffset (y), 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 colours, const int numColours)
  335. : lookupTable (colours),
  336. numEntries (numColours)
  337. {
  338. jassert (numColours >= 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 colours, const int numColours)
  399. : lookupTable (colours),
  400. numEntries (numColours),
  401. gx1 (gradient.point1.x),
  402. gy1 (gradient.point1.y)
  403. {
  404. jassert (numColours >= 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 colours, const int numColours)
  436. : Radial (gradient, transform, colours, numColours),
  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& image, const PixelARGB& colour)
  475. : data (image), sourceColour (colour)
  476. {
  477. if (sizeof (PixelType) == 3 && data.pixelStride == sizeof (PixelType))
  478. {
  479. areRGBComponentsEqual = sourceColour.getRed() == sourceColour.getGreen()
  480. && sourceColour.getGreen() == sourceColour.getBlue();
  481. filler[0].set (sourceColour);
  482. filler[1].set (sourceColour);
  483. filler[2].set (sourceColour);
  484. filler[3].set (sourceColour);
  485. }
  486. }
  487. forcedinline void setEdgeTableYPos (const int y) noexcept
  488. {
  489. linePixels = (PixelType*) data.getLinePointer (y);
  490. }
  491. forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) const noexcept
  492. {
  493. if (replaceExisting)
  494. getPixel (x)->set (sourceColour);
  495. else
  496. getPixel (x)->blend (sourceColour, (uint32) alphaLevel);
  497. }
  498. forcedinline void handleEdgeTablePixelFull (const int x) const noexcept
  499. {
  500. if (replaceExisting)
  501. getPixel (x)->set (sourceColour);
  502. else
  503. getPixel (x)->blend (sourceColour);
  504. }
  505. forcedinline void handleEdgeTableLine (const int x, const int width, const int alphaLevel) const noexcept
  506. {
  507. PixelARGB p (sourceColour);
  508. p.multiplyAlpha (alphaLevel);
  509. PixelType* dest = getPixel (x);
  510. if (replaceExisting || p.getAlpha() >= 0xff)
  511. replaceLine (dest, p, width);
  512. else
  513. blendLine (dest, p, width);
  514. }
  515. forcedinline void handleEdgeTableLineFull (const int x, const int width) const noexcept
  516. {
  517. PixelType* dest = getPixel (x);
  518. if (replaceExisting || sourceColour.getAlpha() >= 0xff)
  519. replaceLine (dest, sourceColour, width);
  520. else
  521. blendLine (dest, sourceColour, width);
  522. }
  523. private:
  524. const Image::BitmapData& data;
  525. PixelType* linePixels;
  526. PixelARGB sourceColour;
  527. PixelRGB filler [4];
  528. bool areRGBComponentsEqual;
  529. forcedinline PixelType* getPixel (const int x) const noexcept
  530. {
  531. return addBytesToPointer (linePixels, x * data.pixelStride);
  532. }
  533. forcedinline void incDestPixelPointer (PixelType*& p) const noexcept
  534. {
  535. p = addBytesToPointer (p, data.pixelStride);
  536. }
  537. inline void blendLine (PixelType* dest, const PixelARGB& colour, int width) const noexcept
  538. {
  539. do
  540. {
  541. dest->blend (colour);
  542. incDestPixelPointer (dest);
  543. }
  544. while (--width > 0);
  545. }
  546. forcedinline void replaceLine (PixelRGB* dest, const PixelARGB& colour, int width) const noexcept
  547. {
  548. if (data.pixelStride == sizeof (*dest))
  549. {
  550. if (areRGBComponentsEqual) // if all the component values are the same, we can cheat..
  551. {
  552. memset (dest, colour.getRed(), (size_t) width * 3);
  553. }
  554. else
  555. {
  556. if (width >> 5)
  557. {
  558. const int* const intFiller = reinterpret_cast<const int*> (filler);
  559. while (width > 8 && (((pointer_sized_int) dest) & 7) != 0)
  560. {
  561. dest->set (colour);
  562. ++dest;
  563. --width;
  564. }
  565. while (width > 4)
  566. {
  567. int* d = reinterpret_cast<int*> (dest);
  568. *d++ = intFiller[0];
  569. *d++ = intFiller[1];
  570. *d++ = intFiller[2];
  571. dest = reinterpret_cast<PixelRGB*> (d);
  572. width -= 4;
  573. }
  574. }
  575. while (--width >= 0)
  576. {
  577. dest->set (colour);
  578. ++dest;
  579. }
  580. }
  581. }
  582. else
  583. {
  584. do
  585. {
  586. dest->set (colour);
  587. incDestPixelPointer (dest);
  588. }
  589. while (--width > 0);
  590. }
  591. }
  592. forcedinline void replaceLine (PixelAlpha* dest, const PixelARGB& colour, int width) const noexcept
  593. {
  594. if (data.pixelStride == sizeof (*dest))
  595. {
  596. memset (dest, colour.getAlpha(), (size_t) width);
  597. }
  598. else
  599. {
  600. do
  601. {
  602. dest->setAlpha (colour.getAlpha());
  603. incDestPixelPointer (dest);
  604. }
  605. while (--width > 0);
  606. }
  607. }
  608. forcedinline void replaceLine (PixelARGB* dest, const PixelARGB& colour, int width) const noexcept
  609. {
  610. do
  611. {
  612. dest->set (colour);
  613. incDestPixelPointer (dest);
  614. } while (--width > 0);
  615. }
  616. JUCE_DECLARE_NON_COPYABLE (SolidColour);
  617. };
  618. //==============================================================================
  619. /** Fills an edge-table with a gradient. */
  620. template <class PixelType, class GradientType>
  621. class Gradient : public GradientType
  622. {
  623. public:
  624. Gradient (const Image::BitmapData& dest, const ColourGradient& gradient, const AffineTransform& transform,
  625. const PixelARGB* const colours, const int numColours)
  626. : GradientType (gradient, transform, colours, numColours - 1),
  627. destData (dest)
  628. {
  629. }
  630. forcedinline void setEdgeTableYPos (const int y) noexcept
  631. {
  632. linePixels = (PixelType*) destData.getLinePointer (y);
  633. GradientType::setY (y);
  634. }
  635. forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) const noexcept
  636. {
  637. getPixel (x)->blend (GradientType::getPixel (x), (uint32) alphaLevel);
  638. }
  639. forcedinline void handleEdgeTablePixelFull (const int x) const noexcept
  640. {
  641. getPixel (x)->blend (GradientType::getPixel (x));
  642. }
  643. void handleEdgeTableLine (int x, int width, const int alphaLevel) const noexcept
  644. {
  645. PixelType* dest = getPixel (x);
  646. if (alphaLevel < 0xff)
  647. {
  648. do
  649. {
  650. dest->blend (GradientType::getPixel (x++), (uint32) alphaLevel);
  651. incDestPixelPointer (dest);
  652. } while (--width > 0);
  653. }
  654. else
  655. {
  656. do
  657. {
  658. dest->blend (GradientType::getPixel (x++));
  659. incDestPixelPointer (dest);
  660. }
  661. while (--width > 0);
  662. }
  663. }
  664. void handleEdgeTableLineFull (int x, int width) const noexcept
  665. {
  666. PixelType* dest = getPixel (x);
  667. do
  668. {
  669. dest->blend (GradientType::getPixel (x++));
  670. incDestPixelPointer (dest);
  671. }
  672. while (--width > 0);
  673. }
  674. private:
  675. const Image::BitmapData& destData;
  676. PixelType* linePixels;
  677. forcedinline PixelType* getPixel (const int x) const noexcept
  678. {
  679. return addBytesToPointer (linePixels, x * destData.pixelStride);
  680. }
  681. forcedinline void incDestPixelPointer (PixelType*& p) const noexcept
  682. {
  683. p = addBytesToPointer (p, destData.pixelStride);
  684. }
  685. JUCE_DECLARE_NON_COPYABLE (Gradient);
  686. };
  687. //==============================================================================
  688. /** Fills an edge-table with a non-transformed image. */
  689. template <class DestPixelType, class SrcPixelType, bool repeatPattern>
  690. class ImageFill
  691. {
  692. public:
  693. ImageFill (const Image::BitmapData& dest, const Image::BitmapData& src,
  694. const int alpha, const int x, const int y)
  695. : destData (dest),
  696. srcData (src),
  697. extraAlpha (alpha + 1),
  698. xOffset (repeatPattern ? negativeAwareModulo (x, src.width) - src.width : x),
  699. yOffset (repeatPattern ? negativeAwareModulo (y, src.height) - src.height : y)
  700. {
  701. }
  702. forcedinline void setEdgeTableYPos (int y) noexcept
  703. {
  704. linePixels = (DestPixelType*) destData.getLinePointer (y);
  705. y -= yOffset;
  706. if (repeatPattern)
  707. {
  708. jassert (y >= 0);
  709. y %= srcData.height;
  710. }
  711. sourceLineStart = (SrcPixelType*) srcData.getLinePointer (y);
  712. }
  713. forcedinline void handleEdgeTablePixel (const int x, int alphaLevel) const noexcept
  714. {
  715. alphaLevel = (alphaLevel * extraAlpha) >> 8;
  716. getDestPixel (x)->blend (*getSrcPixel (repeatPattern ? ((x - xOffset) % srcData.width) : (x - xOffset)), (uint32) alphaLevel);
  717. }
  718. forcedinline void handleEdgeTablePixelFull (const int x) const noexcept
  719. {
  720. getDestPixel (x)->blend (*getSrcPixel (repeatPattern ? ((x - xOffset) % srcData.width) : (x - xOffset)), (uint32) extraAlpha);
  721. }
  722. void handleEdgeTableLine (int x, int width, int alphaLevel) const noexcept
  723. {
  724. DestPixelType* dest = getDestPixel (x);
  725. alphaLevel = (alphaLevel * extraAlpha) >> 8;
  726. x -= xOffset;
  727. jassert (repeatPattern || (x >= 0 && x + width <= srcData.width));
  728. if (alphaLevel < 0xfe)
  729. {
  730. do
  731. {
  732. dest->blend (*getSrcPixel (repeatPattern ? (x++ % srcData.width) : x++), (uint32) alphaLevel);
  733. incDestPixelPointer (dest);
  734. } while (--width > 0);
  735. }
  736. else
  737. {
  738. if (repeatPattern)
  739. {
  740. do
  741. {
  742. dest->blend (*getSrcPixel (x++ % srcData.width));
  743. incDestPixelPointer (dest);
  744. } while (--width > 0);
  745. }
  746. else
  747. {
  748. copyRow (dest, getSrcPixel (x), width);
  749. }
  750. }
  751. }
  752. void handleEdgeTableLineFull (int x, int width) const noexcept
  753. {
  754. DestPixelType* dest = getDestPixel (x);
  755. x -= xOffset;
  756. jassert (repeatPattern || (x >= 0 && x + width <= srcData.width));
  757. if (extraAlpha < 0xfe)
  758. {
  759. do
  760. {
  761. dest->blend (*getSrcPixel (repeatPattern ? (x++ % srcData.width) : x++), (uint32) extraAlpha);
  762. incDestPixelPointer (dest);
  763. } while (--width > 0);
  764. }
  765. else
  766. {
  767. if (repeatPattern)
  768. {
  769. do
  770. {
  771. dest->blend (*getSrcPixel (x++ % srcData.width));
  772. incDestPixelPointer (dest);
  773. } while (--width > 0);
  774. }
  775. else
  776. {
  777. copyRow (dest, getSrcPixel (x), width);
  778. }
  779. }
  780. }
  781. void clipEdgeTableLine (EdgeTable& et, int x, int y, int width)
  782. {
  783. jassert (x - xOffset >= 0 && x + width - xOffset <= srcData.width);
  784. SrcPixelType* s = (SrcPixelType*) srcData.getLinePointer (y - yOffset);
  785. uint8* mask = (uint8*) (s + x - xOffset);
  786. if (sizeof (SrcPixelType) == sizeof (PixelARGB))
  787. mask += PixelARGB::indexA;
  788. et.clipLineToMask (x, y, mask, sizeof (SrcPixelType), width);
  789. }
  790. private:
  791. const Image::BitmapData& destData;
  792. const Image::BitmapData& srcData;
  793. const int extraAlpha, xOffset, yOffset;
  794. DestPixelType* linePixels;
  795. SrcPixelType* sourceLineStart;
  796. forcedinline DestPixelType* getDestPixel (int const x) const noexcept
  797. {
  798. return addBytesToPointer (linePixels, x * destData.pixelStride);
  799. }
  800. forcedinline SrcPixelType const* getSrcPixel (int const x) const noexcept
  801. {
  802. return addBytesToPointer (sourceLineStart, x * srcData.pixelStride);
  803. }
  804. forcedinline void incDestPixelPointer (DestPixelType*& p) const noexcept
  805. {
  806. p = addBytesToPointer (p, destData.pixelStride);
  807. }
  808. forcedinline void copyRow (DestPixelType* dest, SrcPixelType const* src, int width) const noexcept
  809. {
  810. if (srcData.pixelStride == 3 && destData.pixelStride == 3)
  811. {
  812. memcpy (dest, src, sizeof (PixelRGB) * (size_t) width);
  813. }
  814. else
  815. {
  816. do
  817. {
  818. dest->blend (*src);
  819. incDestPixelPointer (dest);
  820. src = addBytesToPointer (src, srcData.pixelStride);
  821. } while (--width > 0);
  822. }
  823. }
  824. JUCE_DECLARE_NON_COPYABLE (ImageFill);
  825. };
  826. //==============================================================================
  827. /** Fills an edge-table with a transformed image. */
  828. template <class DestPixelType, class SrcPixelType, bool repeatPattern>
  829. class TransformedImageFill
  830. {
  831. public:
  832. TransformedImageFill (const Image::BitmapData& dest, const Image::BitmapData& src,
  833. const AffineTransform& transform, const int alpha, const bool higherQuality)
  834. : interpolator (transform,
  835. higherQuality ? 0.5f : 0.0f,
  836. higherQuality ? -128 : 0),
  837. destData (dest),
  838. srcData (src),
  839. extraAlpha (alpha + 1),
  840. betterQuality (higherQuality),
  841. maxX (src.width - 1),
  842. maxY (src.height - 1),
  843. scratchSize (2048)
  844. {
  845. scratchBuffer.malloc (scratchSize);
  846. }
  847. forcedinline void setEdgeTableYPos (const int newY) noexcept
  848. {
  849. y = newY;
  850. linePixels = (DestPixelType*) destData.getLinePointer (newY);
  851. }
  852. forcedinline void handleEdgeTablePixel (const int x, const int alphaLevel) noexcept
  853. {
  854. SrcPixelType p;
  855. generate (&p, x, 1);
  856. getDestPixel (x)->blend (p, (uint32) (alphaLevel * extraAlpha) >> 8);
  857. }
  858. forcedinline void handleEdgeTablePixelFull (const int x) noexcept
  859. {
  860. SrcPixelType p;
  861. generate (&p, x, 1);
  862. getDestPixel (x)->blend (p, (uint32) extraAlpha);
  863. }
  864. void handleEdgeTableLine (const int x, int width, int alphaLevel) noexcept
  865. {
  866. if (width > (int) scratchSize)
  867. {
  868. scratchSize = (size_t) width;
  869. scratchBuffer.malloc (scratchSize);
  870. }
  871. SrcPixelType* span = scratchBuffer;
  872. generate (span, x, width);
  873. DestPixelType* dest = getDestPixel (x);
  874. alphaLevel *= extraAlpha;
  875. alphaLevel >>= 8;
  876. if (alphaLevel < 0xfe)
  877. {
  878. do
  879. {
  880. dest->blend (*span++, (uint32) alphaLevel);
  881. incDestPixelPointer (dest);
  882. } while (--width > 0);
  883. }
  884. else
  885. {
  886. do
  887. {
  888. dest->blend (*span++);
  889. incDestPixelPointer (dest);
  890. } while (--width > 0);
  891. }
  892. }
  893. forcedinline void handleEdgeTableLineFull (const int x, int width) noexcept
  894. {
  895. handleEdgeTableLine (x, width, 255);
  896. }
  897. void clipEdgeTableLine (EdgeTable& et, int x, int y_, int width)
  898. {
  899. if (width > (int) scratchSize)
  900. {
  901. scratchSize = (size_t) width;
  902. scratchBuffer.malloc (scratchSize);
  903. }
  904. y = y_;
  905. generate (scratchBuffer.getData(), x, width);
  906. et.clipLineToMask (x, y_,
  907. reinterpret_cast<uint8*> (scratchBuffer.getData()) + SrcPixelType::indexA,
  908. sizeof (SrcPixelType), width);
  909. }
  910. private:
  911. forcedinline DestPixelType* getDestPixel (const int x) const noexcept
  912. {
  913. return addBytesToPointer (linePixels, x * destData.pixelStride);
  914. }
  915. forcedinline void incDestPixelPointer (DestPixelType*& p) const noexcept
  916. {
  917. p = addBytesToPointer (p, destData.pixelStride);
  918. }
  919. //==============================================================================
  920. template <class PixelType>
  921. void generate (PixelType* dest, const int x, int numPixels) noexcept
  922. {
  923. this->interpolator.setStartOfLine ((float) x, (float) y, numPixels);
  924. do
  925. {
  926. int hiResX, hiResY;
  927. this->interpolator.next (hiResX, hiResY);
  928. int loResX = hiResX >> 8;
  929. int loResY = hiResY >> 8;
  930. if (repeatPattern)
  931. {
  932. loResX = negativeAwareModulo (loResX, srcData.width);
  933. loResY = negativeAwareModulo (loResY, srcData.height);
  934. }
  935. if (betterQuality)
  936. {
  937. if (isPositiveAndBelow (loResX, maxX))
  938. {
  939. if (isPositiveAndBelow (loResY, maxY))
  940. {
  941. // In the centre of the image..
  942. render4PixelAverage (dest, this->srcData.getPixelPointer (loResX, loResY),
  943. hiResX & 255, hiResY & 255);
  944. ++dest;
  945. continue;
  946. }
  947. else if (! repeatPattern)
  948. {
  949. // At a top or bottom edge..
  950. if (loResY < 0)
  951. render2PixelAverageX (dest, this->srcData.getPixelPointer (loResX, 0), hiResX & 255);
  952. else
  953. render2PixelAverageX (dest, this->srcData.getPixelPointer (loResX, maxY), hiResX & 255);
  954. ++dest;
  955. continue;
  956. }
  957. }
  958. else
  959. {
  960. if (isPositiveAndBelow (loResY, maxY) && ! repeatPattern)
  961. {
  962. // At a left or right hand edge..
  963. if (loResX < 0)
  964. render2PixelAverageY (dest, this->srcData.getPixelPointer (0, loResY), hiResY & 255);
  965. else
  966. render2PixelAverageY (dest, this->srcData.getPixelPointer (maxX, loResY), hiResY & 255);
  967. ++dest;
  968. continue;
  969. }
  970. }
  971. }
  972. if (! repeatPattern)
  973. {
  974. if (loResX < 0) loResX = 0;
  975. if (loResY < 0) loResY = 0;
  976. if (loResX > maxX) loResX = maxX;
  977. if (loResY > maxY) loResY = maxY;
  978. }
  979. dest->set (*(const PixelType*) this->srcData.getPixelPointer (loResX, loResY));
  980. ++dest;
  981. } while (--numPixels > 0);
  982. }
  983. //==============================================================================
  984. void render4PixelAverage (PixelARGB* const dest, const uint8* src, const int subPixelX, const int subPixelY) noexcept
  985. {
  986. uint32 c[4] = { 256 * 128, 256 * 128, 256 * 128, 256 * 128 };
  987. uint32 weight = (uint32) ((256 - subPixelX) * (256 - subPixelY));
  988. c[0] += weight * src[0];
  989. c[1] += weight * src[1];
  990. c[2] += weight * src[2];
  991. c[3] += weight * src[3];
  992. src += this->srcData.pixelStride;
  993. weight = (uint32) (subPixelX * (256 - subPixelY));
  994. c[0] += weight * src[0];
  995. c[1] += weight * src[1];
  996. c[2] += weight * src[2];
  997. c[3] += weight * src[3];
  998. src += this->srcData.lineStride;
  999. weight = (uint32) (subPixelX * subPixelY);
  1000. c[0] += weight * src[0];
  1001. c[1] += weight * src[1];
  1002. c[2] += weight * src[2];
  1003. c[3] += weight * src[3];
  1004. src -= this->srcData.pixelStride;
  1005. weight = (uint32) ((256 - subPixelX) * subPixelY);
  1006. c[0] += weight * src[0];
  1007. c[1] += weight * src[1];
  1008. c[2] += weight * src[2];
  1009. c[3] += weight * src[3];
  1010. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 16),
  1011. (uint8) (c[PixelARGB::indexR] >> 16),
  1012. (uint8) (c[PixelARGB::indexG] >> 16),
  1013. (uint8) (c[PixelARGB::indexB] >> 16));
  1014. }
  1015. void render2PixelAverageX (PixelARGB* const dest, const uint8* src, const uint32 subPixelX) noexcept
  1016. {
  1017. uint32 c[4] = { 128, 128, 128, 128 };
  1018. uint32 weight = 256 - subPixelX;
  1019. c[0] += weight * src[0];
  1020. c[1] += weight * src[1];
  1021. c[2] += weight * src[2];
  1022. c[3] += weight * src[3];
  1023. src += this->srcData.pixelStride;
  1024. weight = subPixelX;
  1025. c[0] += weight * src[0];
  1026. c[1] += weight * src[1];
  1027. c[2] += weight * src[2];
  1028. c[3] += weight * src[3];
  1029. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 8),
  1030. (uint8) (c[PixelARGB::indexR] >> 8),
  1031. (uint8) (c[PixelARGB::indexG] >> 8),
  1032. (uint8) (c[PixelARGB::indexB] >> 8));
  1033. }
  1034. void render2PixelAverageY (PixelARGB* const dest, const uint8* src, const uint32 subPixelY) noexcept
  1035. {
  1036. uint32 c[4] = { 128, 128, 128, 128 };
  1037. uint32 weight = 256 - subPixelY;
  1038. c[0] += weight * src[0];
  1039. c[1] += weight * src[1];
  1040. c[2] += weight * src[2];
  1041. c[3] += weight * src[3];
  1042. src += this->srcData.lineStride;
  1043. weight = subPixelY;
  1044. c[0] += weight * src[0];
  1045. c[1] += weight * src[1];
  1046. c[2] += weight * src[2];
  1047. c[3] += weight * src[3];
  1048. dest->setARGB ((uint8) (c[PixelARGB::indexA] >> 8),
  1049. (uint8) (c[PixelARGB::indexR] >> 8),
  1050. (uint8) (c[PixelARGB::indexG] >> 8),
  1051. (uint8) (c[PixelARGB::indexB] >> 8));
  1052. }
  1053. //==============================================================================
  1054. void render4PixelAverage (PixelRGB* const dest, const uint8* src, const uint32 subPixelX, const uint32 subPixelY) noexcept
  1055. {
  1056. uint32 c[3] = { 256 * 128, 256 * 128, 256 * 128 };
  1057. uint32 weight = (256 - subPixelX) * (256 - subPixelY);
  1058. c[0] += weight * src[0];
  1059. c[1] += weight * src[1];
  1060. c[2] += weight * src[2];
  1061. src += this->srcData.pixelStride;
  1062. weight = 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.lineStride;
  1067. weight = subPixelX * subPixelY;
  1068. c[0] += weight * src[0];
  1069. c[1] += weight * src[1];
  1070. c[2] += weight * src[2];
  1071. src -= this->srcData.pixelStride;
  1072. weight = (256 - subPixelX) * subPixelY;
  1073. c[0] += weight * src[0];
  1074. c[1] += weight * src[1];
  1075. c[2] += weight * src[2];
  1076. dest->setARGB ((uint8) 255,
  1077. (uint8) (c[PixelRGB::indexR] >> 16),
  1078. (uint8) (c[PixelRGB::indexG] >> 16),
  1079. (uint8) (c[PixelRGB::indexB] >> 16));
  1080. }
  1081. void render2PixelAverageX (PixelRGB* const dest, const uint8* src, const uint32 subPixelX) noexcept
  1082. {
  1083. uint32 c[3] = { 128, 128, 128 };
  1084. const uint32 weight = 256 - subPixelX;
  1085. c[0] += weight * src[0];
  1086. c[1] += weight * src[1];
  1087. c[2] += weight * src[2];
  1088. src += this->srcData.pixelStride;
  1089. c[0] += subPixelX * src[0];
  1090. c[1] += subPixelX * src[1];
  1091. c[2] += subPixelX * src[2];
  1092. dest->setARGB ((uint8) 255,
  1093. (uint8) (c[PixelRGB::indexR] >> 8),
  1094. (uint8) (c[PixelRGB::indexG] >> 8),
  1095. (uint8) (c[PixelRGB::indexB] >> 8));
  1096. }
  1097. void render2PixelAverageY (PixelRGB* const dest, const uint8* src, const uint32 subPixelY) noexcept
  1098. {
  1099. uint32 c[3] = { 128, 128, 128 };
  1100. const uint32 weight = 256 - subPixelY;
  1101. c[0] += weight * src[0];
  1102. c[1] += weight * src[1];
  1103. c[2] += weight * src[2];
  1104. src += this->srcData.lineStride;
  1105. c[0] += subPixelY * src[0];
  1106. c[1] += subPixelY * src[1];
  1107. c[2] += subPixelY * src[2];
  1108. dest->setARGB ((uint8) 255,
  1109. (uint8) (c[PixelRGB::indexR] >> 8),
  1110. (uint8) (c[PixelRGB::indexG] >> 8),
  1111. (uint8) (c[PixelRGB::indexB] >> 8));
  1112. }
  1113. //==============================================================================
  1114. void render4PixelAverage (PixelAlpha* const dest, const uint8* src, const uint32 subPixelX, const uint32 subPixelY) noexcept
  1115. {
  1116. uint32 c = 256 * 128;
  1117. c += src[0] * ((256 - subPixelX) * (256 - subPixelY));
  1118. src += this->srcData.pixelStride;
  1119. c += src[1] * (subPixelX * (256 - subPixelY));
  1120. src += this->srcData.lineStride;
  1121. c += src[1] * (subPixelX * subPixelY);
  1122. src -= this->srcData.pixelStride;
  1123. c += src[0] * ((256 - subPixelX) * subPixelY);
  1124. *((uint8*) dest) = (uint8) (c >> 16);
  1125. }
  1126. void render2PixelAverageX (PixelAlpha* const dest, const uint8* src, const uint32 subPixelX) noexcept
  1127. {
  1128. uint32 c = 128;
  1129. c += src[0] * (256 - subPixelX);
  1130. src += this->srcData.pixelStride;
  1131. c += src[0] * subPixelX;
  1132. *((uint8*) dest) = (uint8) (c >> 8);
  1133. }
  1134. void render2PixelAverageY (PixelAlpha* const dest, const uint8* src, const uint32 subPixelY) noexcept
  1135. {
  1136. uint32 c = 128;
  1137. c += src[0] * (256 - subPixelY);
  1138. src += this->srcData.lineStride;
  1139. c += src[0] * subPixelY;
  1140. *((uint8*) dest) = (uint8) (c >> 8);
  1141. }
  1142. //==============================================================================
  1143. class TransformedImageSpanInterpolator
  1144. {
  1145. public:
  1146. TransformedImageSpanInterpolator (const AffineTransform& transform,
  1147. const float offsetFloat, const int offsetInt) noexcept
  1148. : inverseTransform (transform.inverted()),
  1149. pixelOffset (offsetFloat), pixelOffsetInt (offsetInt)
  1150. {}
  1151. void setStartOfLine (float sx, float sy, const int numPixels) noexcept
  1152. {
  1153. jassert (numPixels > 0);
  1154. sx += pixelOffset;
  1155. sy += pixelOffset;
  1156. float x1 = sx, y1 = sy;
  1157. sx += numPixels;
  1158. inverseTransform.transformPoints (x1, y1, sx, sy);
  1159. xBresenham.set ((int) (x1 * 256.0f), (int) (sx * 256.0f), numPixels, pixelOffsetInt);
  1160. yBresenham.set ((int) (y1 * 256.0f), (int) (sy * 256.0f), numPixels, pixelOffsetInt);
  1161. }
  1162. void next (int& px, int& py) noexcept
  1163. {
  1164. px = xBresenham.n; xBresenham.stepToNext();
  1165. py = yBresenham.n; yBresenham.stepToNext();
  1166. }
  1167. private:
  1168. class BresenhamInterpolator
  1169. {
  1170. public:
  1171. BresenhamInterpolator() noexcept {}
  1172. void set (const int n1, const int n2, const int numSteps_, const int offsetInt) noexcept
  1173. {
  1174. numSteps = numSteps_;
  1175. step = (n2 - n1) / numSteps;
  1176. remainder = modulo = (n2 - n1) % numSteps;
  1177. n = n1 + offsetInt;
  1178. if (modulo <= 0)
  1179. {
  1180. modulo += numSteps;
  1181. remainder += numSteps;
  1182. --step;
  1183. }
  1184. modulo -= numSteps;
  1185. }
  1186. forcedinline void stepToNext() noexcept
  1187. {
  1188. modulo += remainder;
  1189. n += step;
  1190. if (modulo > 0)
  1191. {
  1192. modulo -= numSteps;
  1193. ++n;
  1194. }
  1195. }
  1196. int n;
  1197. private:
  1198. int numSteps, step, modulo, remainder;
  1199. };
  1200. const AffineTransform inverseTransform;
  1201. BresenhamInterpolator xBresenham, yBresenham;
  1202. const float pixelOffset;
  1203. const int pixelOffsetInt;
  1204. JUCE_DECLARE_NON_COPYABLE (TransformedImageSpanInterpolator);
  1205. };
  1206. //==============================================================================
  1207. TransformedImageSpanInterpolator interpolator;
  1208. const Image::BitmapData& destData;
  1209. const Image::BitmapData& srcData;
  1210. const int extraAlpha;
  1211. const bool betterQuality;
  1212. const int maxX, maxY;
  1213. int y;
  1214. DestPixelType* linePixels;
  1215. HeapBlock <SrcPixelType> scratchBuffer;
  1216. size_t scratchSize;
  1217. JUCE_DECLARE_NON_COPYABLE (TransformedImageFill);
  1218. };
  1219. //==============================================================================
  1220. template <class Iterator>
  1221. void renderImageTransformed (Iterator& iter, const Image::BitmapData& destData, const Image::BitmapData& srcData,
  1222. const int alpha, const AffineTransform& transform, bool betterQuality, bool tiledFill)
  1223. {
  1224. switch (destData.pixelFormat)
  1225. {
  1226. case Image::ARGB:
  1227. switch (srcData.pixelFormat)
  1228. {
  1229. case Image::ARGB:
  1230. if (tiledFill) { TransformedImageFill <PixelARGB, PixelARGB, true> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1231. else { TransformedImageFill <PixelARGB, PixelARGB, false> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1232. break;
  1233. case Image::RGB:
  1234. if (tiledFill) { TransformedImageFill <PixelARGB, PixelRGB, true> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1235. else { TransformedImageFill <PixelARGB, PixelRGB, false> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1236. break;
  1237. default:
  1238. if (tiledFill) { TransformedImageFill <PixelARGB, PixelAlpha, true> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1239. else { TransformedImageFill <PixelARGB, PixelAlpha, false> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1240. break;
  1241. }
  1242. break;
  1243. case Image::RGB:
  1244. switch (srcData.pixelFormat)
  1245. {
  1246. case Image::ARGB:
  1247. if (tiledFill) { TransformedImageFill <PixelRGB, PixelARGB, true> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1248. else { TransformedImageFill <PixelRGB, PixelARGB, false> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1249. break;
  1250. case Image::RGB:
  1251. if (tiledFill) { TransformedImageFill <PixelRGB, PixelRGB, true> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1252. else { TransformedImageFill <PixelRGB, PixelRGB, false> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1253. break;
  1254. default:
  1255. if (tiledFill) { TransformedImageFill <PixelRGB, PixelAlpha, true> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1256. else { TransformedImageFill <PixelRGB, PixelAlpha, false> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1257. break;
  1258. }
  1259. break;
  1260. default:
  1261. switch (srcData.pixelFormat)
  1262. {
  1263. case Image::ARGB:
  1264. if (tiledFill) { TransformedImageFill <PixelAlpha, PixelARGB, true> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1265. else { TransformedImageFill <PixelAlpha, PixelARGB, false> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1266. break;
  1267. case Image::RGB:
  1268. if (tiledFill) { TransformedImageFill <PixelAlpha, PixelRGB, true> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1269. else { TransformedImageFill <PixelAlpha, PixelRGB, false> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1270. break;
  1271. default:
  1272. if (tiledFill) { TransformedImageFill <PixelAlpha, PixelAlpha, true> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1273. else { TransformedImageFill <PixelAlpha, PixelAlpha, false> r (destData, srcData, transform, alpha, betterQuality); iter.iterate (r); }
  1274. break;
  1275. }
  1276. break;
  1277. }
  1278. }
  1279. template <class Iterator>
  1280. void renderImageUntransformed (Iterator& iter, const Image::BitmapData& destData, const Image::BitmapData& srcData, const int alpha, int x, int y, bool tiledFill)
  1281. {
  1282. switch (destData.pixelFormat)
  1283. {
  1284. case Image::ARGB:
  1285. switch (srcData.pixelFormat)
  1286. {
  1287. case Image::ARGB:
  1288. if (tiledFill) { ImageFill <PixelARGB, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1289. else { ImageFill <PixelARGB, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1290. break;
  1291. case Image::RGB:
  1292. if (tiledFill) { ImageFill <PixelARGB, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1293. else { ImageFill <PixelARGB, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1294. break;
  1295. default:
  1296. if (tiledFill) { ImageFill <PixelARGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1297. else { ImageFill <PixelARGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1298. break;
  1299. }
  1300. break;
  1301. case Image::RGB:
  1302. switch (srcData.pixelFormat)
  1303. {
  1304. case Image::ARGB:
  1305. if (tiledFill) { ImageFill <PixelRGB, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1306. else { ImageFill <PixelRGB, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1307. break;
  1308. case Image::RGB:
  1309. if (tiledFill) { ImageFill <PixelRGB, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1310. else { ImageFill <PixelRGB, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1311. break;
  1312. default:
  1313. if (tiledFill) { ImageFill <PixelRGB, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1314. else { ImageFill <PixelRGB, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1315. break;
  1316. }
  1317. break;
  1318. default:
  1319. switch (srcData.pixelFormat)
  1320. {
  1321. case Image::ARGB:
  1322. if (tiledFill) { ImageFill <PixelAlpha, PixelARGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1323. else { ImageFill <PixelAlpha, PixelARGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1324. break;
  1325. case Image::RGB:
  1326. if (tiledFill) { ImageFill <PixelAlpha, PixelRGB, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1327. else { ImageFill <PixelAlpha, PixelRGB, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1328. break;
  1329. default:
  1330. if (tiledFill) { ImageFill <PixelAlpha, PixelAlpha, true> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1331. else { ImageFill <PixelAlpha, PixelAlpha, false> r (destData, srcData, alpha, x, y); iter.iterate (r); }
  1332. break;
  1333. }
  1334. break;
  1335. }
  1336. }
  1337. template <class Iterator, class DestPixelType>
  1338. void renderSolidFill (Iterator& iter, const Image::BitmapData& destData, const PixelARGB& fillColour, const bool replaceContents, DestPixelType*)
  1339. {
  1340. if (replaceContents)
  1341. {
  1342. EdgeTableFillers::SolidColour <DestPixelType, true> r (destData, fillColour);
  1343. iter.iterate (r);
  1344. }
  1345. else
  1346. {
  1347. EdgeTableFillers::SolidColour <DestPixelType, false> r (destData, fillColour);
  1348. iter.iterate (r);
  1349. }
  1350. }
  1351. template <class Iterator, class DestPixelType>
  1352. void renderGradient (Iterator& iter, const Image::BitmapData& destData, const ColourGradient& g, const AffineTransform& transform,
  1353. const PixelARGB* const lookupTable, const int numLookupEntries, const bool isIdentity, DestPixelType*)
  1354. {
  1355. if (g.isRadial)
  1356. {
  1357. if (isIdentity)
  1358. {
  1359. EdgeTableFillers::Gradient <DestPixelType, GradientPixelIterators::Radial> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1360. iter.iterate (renderer);
  1361. }
  1362. else
  1363. {
  1364. EdgeTableFillers::Gradient <DestPixelType, GradientPixelIterators::TransformedRadial> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1365. iter.iterate (renderer);
  1366. }
  1367. }
  1368. else
  1369. {
  1370. EdgeTableFillers::Gradient <DestPixelType, GradientPixelIterators::Linear> renderer (destData, g, transform, lookupTable, numLookupEntries);
  1371. iter.iterate (renderer);
  1372. }
  1373. }
  1374. }
  1375. //==============================================================================
  1376. namespace ClipRegions
  1377. {
  1378. class Base : public SingleThreadedReferenceCountedObject
  1379. {
  1380. public:
  1381. Base() {}
  1382. virtual ~Base() {}
  1383. typedef ReferenceCountedObjectPtr<Base> Ptr;
  1384. virtual Ptr clone() const = 0;
  1385. virtual Ptr applyClipTo (const Ptr& target) const = 0;
  1386. virtual Ptr clipToRectangle (const Rectangle<int>&) = 0;
  1387. virtual Ptr clipToRectangleList (const RectangleList&) = 0;
  1388. virtual Ptr excludeClipRectangle (const Rectangle<int>&) = 0;
  1389. virtual Ptr clipToPath (const Path&, const AffineTransform&) = 0;
  1390. virtual Ptr clipToEdgeTable (const EdgeTable& et) = 0;
  1391. virtual Ptr clipToImageAlpha (const Image&, const AffineTransform&, const bool betterQuality) = 0;
  1392. virtual void translate (const Point<int>& delta) = 0;
  1393. virtual bool clipRegionIntersects (const Rectangle<int>&) const = 0;
  1394. virtual Rectangle<int> getClipBounds() const = 0;
  1395. virtual void fillRectWithColour (Image::BitmapData& destData, const Rectangle<int>&, const PixelARGB& colour, bool replaceContents) const = 0;
  1396. virtual void fillRectWithColour (Image::BitmapData& destData, const Rectangle<float>&, const PixelARGB& colour) const = 0;
  1397. virtual void fillAllWithColour (Image::BitmapData& destData, const PixelARGB& colour, bool replaceContents) const = 0;
  1398. virtual void fillAllWithGradient (Image::BitmapData& destData, ColourGradient&, const AffineTransform&, bool isIdentity) const = 0;
  1399. virtual void renderImageTransformed (const Image::BitmapData& destData, const Image::BitmapData& srcData, const int alpha, const AffineTransform&, bool betterQuality, bool tiledFill) const = 0;
  1400. virtual void renderImageUntransformed (const Image::BitmapData& destData, const Image::BitmapData& srcData, const int alpha, int x, int y, bool tiledFill) const = 0;
  1401. };
  1402. //==============================================================================
  1403. class EdgeTableRegion : public Base
  1404. {
  1405. public:
  1406. EdgeTableRegion (const EdgeTable& e) : edgeTable (e) {}
  1407. EdgeTableRegion (const Rectangle<int>& r) : edgeTable (r) {}
  1408. EdgeTableRegion (const Rectangle<float>& r) : edgeTable (r) {}
  1409. EdgeTableRegion (const RectangleList& r) : edgeTable (r) {}
  1410. EdgeTableRegion (const Rectangle<int>& bounds, const Path& p, const AffineTransform& t) : edgeTable (bounds, p, t) {}
  1411. EdgeTableRegion (const EdgeTableRegion& other) : edgeTable (other.edgeTable) {}
  1412. Ptr clone() const { return new EdgeTableRegion (*this); }
  1413. Ptr applyClipTo (const Ptr& target) const { return target->clipToEdgeTable (edgeTable); }
  1414. Ptr clipToRectangle (const Rectangle<int>& r)
  1415. {
  1416. edgeTable.clipToRectangle (r);
  1417. return edgeTable.isEmpty() ? nullptr : this;
  1418. }
  1419. Ptr clipToRectangleList (const RectangleList& r)
  1420. {
  1421. RectangleList inverse (edgeTable.getMaximumBounds());
  1422. if (inverse.subtract (r))
  1423. for (RectangleList::Iterator iter (inverse); iter.next();)
  1424. edgeTable.excludeRectangle (*iter.getRectangle());
  1425. return edgeTable.isEmpty() ? nullptr : this;
  1426. }
  1427. Ptr excludeClipRectangle (const Rectangle<int>& r)
  1428. {
  1429. edgeTable.excludeRectangle (r);
  1430. return edgeTable.isEmpty() ? nullptr : this;
  1431. }
  1432. Ptr clipToPath (const Path& p, const AffineTransform& transform)
  1433. {
  1434. EdgeTable et (edgeTable.getMaximumBounds(), p, transform);
  1435. edgeTable.clipToEdgeTable (et);
  1436. return edgeTable.isEmpty() ? nullptr : this;
  1437. }
  1438. Ptr clipToEdgeTable (const EdgeTable& et)
  1439. {
  1440. edgeTable.clipToEdgeTable (et);
  1441. return edgeTable.isEmpty() ? nullptr : this;
  1442. }
  1443. Ptr clipToImageAlpha (const Image& image, const AffineTransform& transform, const bool betterQuality)
  1444. {
  1445. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  1446. if (transform.isOnlyTranslation())
  1447. {
  1448. // If our translation doesn't involve any distortion, just use a simple blit..
  1449. const int tx = (int) (transform.getTranslationX() * 256.0f);
  1450. const int ty = (int) (transform.getTranslationY() * 256.0f);
  1451. if ((! betterQuality) || ((tx | ty) & 224) == 0)
  1452. {
  1453. const int imageX = ((tx + 128) >> 8);
  1454. const int imageY = ((ty + 128) >> 8);
  1455. if (image.getFormat() == Image::ARGB)
  1456. straightClipImage (srcData, imageX, imageY, (PixelARGB*) 0);
  1457. else
  1458. straightClipImage (srcData, imageX, imageY, (PixelAlpha*) 0);
  1459. return edgeTable.isEmpty() ? nullptr : this;
  1460. }
  1461. }
  1462. if (transform.isSingularity())
  1463. return Ptr();
  1464. {
  1465. Path p;
  1466. p.addRectangle (0, 0, (float) srcData.width, (float) srcData.height);
  1467. EdgeTable et2 (edgeTable.getMaximumBounds(), p, transform);
  1468. edgeTable.clipToEdgeTable (et2);
  1469. }
  1470. if (! edgeTable.isEmpty())
  1471. {
  1472. if (image.getFormat() == Image::ARGB)
  1473. transformedClipImage (srcData, transform, betterQuality, (PixelARGB*) 0);
  1474. else
  1475. transformedClipImage (srcData, transform, betterQuality, (PixelAlpha*) 0);
  1476. }
  1477. return edgeTable.isEmpty() ? nullptr : this;
  1478. }
  1479. void translate (const Point<int>& delta)
  1480. {
  1481. edgeTable.translate ((float) delta.x, delta.y);
  1482. }
  1483. bool clipRegionIntersects (const Rectangle<int>& r) const
  1484. {
  1485. return edgeTable.getMaximumBounds().intersects (r);
  1486. }
  1487. Rectangle<int> getClipBounds() const
  1488. {
  1489. return edgeTable.getMaximumBounds();
  1490. }
  1491. void fillRectWithColour (Image::BitmapData& destData, const Rectangle<int>& area, const PixelARGB& colour, bool replaceContents) const
  1492. {
  1493. const Rectangle<int> totalClip (edgeTable.getMaximumBounds());
  1494. const Rectangle<int> clipped (totalClip.getIntersection (area));
  1495. if (! clipped.isEmpty())
  1496. {
  1497. EdgeTableRegion et (clipped);
  1498. et.edgeTable.clipToEdgeTable (edgeTable);
  1499. et.fillAllWithColour (destData, colour, replaceContents);
  1500. }
  1501. }
  1502. void fillRectWithColour (Image::BitmapData& destData, const Rectangle<float>& area, const PixelARGB& colour) const
  1503. {
  1504. const Rectangle<float> totalClip (edgeTable.getMaximumBounds().toFloat());
  1505. const Rectangle<float> clipped (totalClip.getIntersection (area));
  1506. if (! clipped.isEmpty())
  1507. {
  1508. EdgeTableRegion et (clipped);
  1509. et.edgeTable.clipToEdgeTable (edgeTable);
  1510. et.fillAllWithColour (destData, colour, false);
  1511. }
  1512. }
  1513. void fillAllWithColour (Image::BitmapData& destData, const PixelARGB& colour, bool replaceContents) const
  1514. {
  1515. switch (destData.pixelFormat)
  1516. {
  1517. case Image::ARGB: EdgeTableFillers::renderSolidFill (edgeTable, destData, colour, replaceContents, (PixelARGB*) 0); break;
  1518. case Image::RGB: EdgeTableFillers::renderSolidFill (edgeTable, destData, colour, replaceContents, (PixelRGB*) 0); break;
  1519. default: EdgeTableFillers::renderSolidFill (edgeTable, destData, colour, replaceContents, (PixelAlpha*) 0); break;
  1520. }
  1521. }
  1522. void fillAllWithGradient (Image::BitmapData& destData, ColourGradient& gradient, const AffineTransform& transform, bool isIdentity) const
  1523. {
  1524. HeapBlock <PixelARGB> lookupTable;
  1525. const int numLookupEntries = gradient.createLookupTable (transform, lookupTable);
  1526. jassert (numLookupEntries > 0);
  1527. switch (destData.pixelFormat)
  1528. {
  1529. case Image::ARGB: EdgeTableFillers::renderGradient (edgeTable, destData, gradient, transform, lookupTable, numLookupEntries, isIdentity, (PixelARGB*) 0); break;
  1530. case Image::RGB: EdgeTableFillers::renderGradient (edgeTable, destData, gradient, transform, lookupTable, numLookupEntries, isIdentity, (PixelRGB*) 0); break;
  1531. default: EdgeTableFillers::renderGradient (edgeTable, destData, gradient, transform, lookupTable, numLookupEntries, isIdentity, (PixelAlpha*) 0); break;
  1532. }
  1533. }
  1534. void renderImageTransformed (const Image::BitmapData& destData, const Image::BitmapData& srcData, const int alpha, const AffineTransform& transform, bool betterQuality, bool tiledFill) const
  1535. {
  1536. EdgeTableFillers::renderImageTransformed (edgeTable, destData, srcData, alpha, transform, betterQuality, tiledFill);
  1537. }
  1538. void renderImageUntransformed (const Image::BitmapData& destData, const Image::BitmapData& srcData, const int alpha, int x, int y, bool tiledFill) const
  1539. {
  1540. EdgeTableFillers::renderImageUntransformed (edgeTable, destData, srcData, alpha, x, y, tiledFill);
  1541. }
  1542. EdgeTable edgeTable;
  1543. private:
  1544. //==============================================================================
  1545. template <class SrcPixelType>
  1546. void transformedClipImage (const Image::BitmapData& srcData, const AffineTransform& transform, const bool betterQuality, const SrcPixelType*)
  1547. {
  1548. EdgeTableFillers::TransformedImageFill <SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, transform, 255, betterQuality);
  1549. for (int y = 0; y < edgeTable.getMaximumBounds().getHeight(); ++y)
  1550. renderer.clipEdgeTableLine (edgeTable, edgeTable.getMaximumBounds().getX(), y + edgeTable.getMaximumBounds().getY(),
  1551. edgeTable.getMaximumBounds().getWidth());
  1552. }
  1553. template <class SrcPixelType>
  1554. void straightClipImage (const Image::BitmapData& srcData, int imageX, int imageY, const SrcPixelType*)
  1555. {
  1556. Rectangle<int> r (imageX, imageY, srcData.width, srcData.height);
  1557. edgeTable.clipToRectangle (r);
  1558. EdgeTableFillers::ImageFill <SrcPixelType, SrcPixelType, false> renderer (srcData, srcData, 255, imageX, imageY);
  1559. for (int y = 0; y < r.getHeight(); ++y)
  1560. renderer.clipEdgeTableLine (edgeTable, r.getX(), y + r.getY(), r.getWidth());
  1561. }
  1562. EdgeTableRegion& operator= (const EdgeTableRegion&);
  1563. };
  1564. //==============================================================================
  1565. class RectangleListRegion : public Base
  1566. {
  1567. public:
  1568. RectangleListRegion (const Rectangle<int>& r) : clip (r) {}
  1569. RectangleListRegion (const RectangleList& r) : clip (r) {}
  1570. RectangleListRegion (const RectangleListRegion& other) : clip (other.clip) {}
  1571. Ptr clone() const { return new RectangleListRegion (*this); }
  1572. Ptr applyClipTo (const Ptr& target) const { return target->clipToRectangleList (clip); }
  1573. Ptr clipToRectangle (const Rectangle<int>& r)
  1574. {
  1575. clip.clipTo (r);
  1576. return clip.isEmpty() ? nullptr : this;
  1577. }
  1578. Ptr clipToRectangleList (const RectangleList& r)
  1579. {
  1580. clip.clipTo (r);
  1581. return clip.isEmpty() ? nullptr : this;
  1582. }
  1583. Ptr excludeClipRectangle (const Rectangle<int>& r)
  1584. {
  1585. clip.subtract (r);
  1586. return clip.isEmpty() ? nullptr : this;
  1587. }
  1588. Ptr clipToPath (const Path& p, const AffineTransform& transform) { return toEdgeTable()->clipToPath (p, transform); }
  1589. Ptr clipToEdgeTable (const EdgeTable& et) { return toEdgeTable()->clipToEdgeTable (et); }
  1590. Ptr clipToImageAlpha (const Image& image, const AffineTransform& transform, const bool betterQuality)
  1591. {
  1592. return toEdgeTable()->clipToImageAlpha (image, transform, betterQuality);
  1593. }
  1594. void translate (const Point<int>& delta)
  1595. {
  1596. clip.offsetAll (delta.x, delta.y);
  1597. }
  1598. bool clipRegionIntersects (const Rectangle<int>& r) const { return clip.intersects (r); }
  1599. Rectangle<int> getClipBounds() const { return clip.getBounds(); }
  1600. void fillRectWithColour (Image::BitmapData& destData, const Rectangle<int>& area, const PixelARGB& colour, bool replaceContents) const
  1601. {
  1602. SubRectangleIterator iter (clip, area);
  1603. switch (destData.pixelFormat)
  1604. {
  1605. case Image::ARGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelARGB*) 0); break;
  1606. case Image::RGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelRGB*) 0); break;
  1607. default: EdgeTableFillers::renderSolidFill (iter, destData, colour, replaceContents, (PixelAlpha*) 0); break;
  1608. }
  1609. }
  1610. void fillRectWithColour (Image::BitmapData& destData, const Rectangle<float>& area, const PixelARGB& colour) const
  1611. {
  1612. SubRectangleIteratorFloat iter (clip, area);
  1613. switch (destData.pixelFormat)
  1614. {
  1615. case Image::ARGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, false, (PixelARGB*) 0); break;
  1616. case Image::RGB: EdgeTableFillers::renderSolidFill (iter, destData, colour, false, (PixelRGB*) 0); break;
  1617. default: EdgeTableFillers::renderSolidFill (iter, destData, colour, false, (PixelAlpha*) 0); break;
  1618. }
  1619. }
  1620. void fillAllWithColour (Image::BitmapData& destData, const PixelARGB& colour, bool replaceContents) const
  1621. {
  1622. switch (destData.pixelFormat)
  1623. {
  1624. case Image::ARGB: EdgeTableFillers::renderSolidFill (*this, destData, colour, replaceContents, (PixelARGB*) 0); break;
  1625. case Image::RGB: EdgeTableFillers::renderSolidFill (*this, destData, colour, replaceContents, (PixelRGB*) 0); break;
  1626. default: EdgeTableFillers::renderSolidFill (*this, destData, colour, replaceContents, (PixelAlpha*) 0); break;
  1627. }
  1628. }
  1629. void fillAllWithGradient (Image::BitmapData& destData, ColourGradient& gradient, const AffineTransform& transform, bool isIdentity) const
  1630. {
  1631. HeapBlock <PixelARGB> lookupTable;
  1632. const int numLookupEntries = gradient.createLookupTable (transform, lookupTable);
  1633. jassert (numLookupEntries > 0);
  1634. switch (destData.pixelFormat)
  1635. {
  1636. case Image::ARGB: EdgeTableFillers::renderGradient (*this, destData, gradient, transform, lookupTable, numLookupEntries, isIdentity, (PixelARGB*) 0); break;
  1637. case Image::RGB: EdgeTableFillers::renderGradient (*this, destData, gradient, transform, lookupTable, numLookupEntries, isIdentity, (PixelRGB*) 0); break;
  1638. default: EdgeTableFillers::renderGradient (*this, destData, gradient, transform, lookupTable, numLookupEntries, isIdentity, (PixelAlpha*) 0); break;
  1639. }
  1640. }
  1641. void renderImageTransformed (const Image::BitmapData& destData, const Image::BitmapData& srcData, const int alpha, const AffineTransform& transform, bool betterQuality, bool tiledFill) const
  1642. {
  1643. EdgeTableFillers::renderImageTransformed (*this, destData, srcData, alpha, transform, betterQuality, tiledFill);
  1644. }
  1645. void renderImageUntransformed (const Image::BitmapData& destData, const Image::BitmapData& srcData, const int alpha, int x, int y, bool tiledFill) const
  1646. {
  1647. EdgeTableFillers::renderImageUntransformed (*this, destData, srcData, alpha, x, y, tiledFill);
  1648. }
  1649. RectangleList clip;
  1650. //==============================================================================
  1651. template <class Renderer>
  1652. void iterate (Renderer& r) const noexcept
  1653. {
  1654. RectangleList::Iterator iter (clip);
  1655. while (iter.next())
  1656. {
  1657. const Rectangle<int> rect (*iter.getRectangle());
  1658. const int x = rect.getX();
  1659. const int w = rect.getWidth();
  1660. jassert (w > 0);
  1661. const int bottom = rect.getBottom();
  1662. for (int y = rect.getY(); y < bottom; ++y)
  1663. {
  1664. r.setEdgeTableYPos (y);
  1665. r.handleEdgeTableLineFull (x, w);
  1666. }
  1667. }
  1668. }
  1669. private:
  1670. //==============================================================================
  1671. class SubRectangleIterator
  1672. {
  1673. public:
  1674. SubRectangleIterator (const RectangleList& clipList, const Rectangle<int>& clipBounds)
  1675. : clip (clipList), area (clipBounds)
  1676. {}
  1677. template <class Renderer>
  1678. void iterate (Renderer& r) const noexcept
  1679. {
  1680. RectangleList::Iterator iter (clip);
  1681. while (iter.next())
  1682. {
  1683. const Rectangle<int> rect (iter.getRectangle()->getIntersection (area));
  1684. if (! rect.isEmpty())
  1685. {
  1686. const int x = rect.getX();
  1687. const int w = rect.getWidth();
  1688. const int bottom = rect.getBottom();
  1689. for (int y = rect.getY(); y < bottom; ++y)
  1690. {
  1691. r.setEdgeTableYPos (y);
  1692. r.handleEdgeTableLineFull (x, w);
  1693. }
  1694. }
  1695. }
  1696. }
  1697. private:
  1698. const RectangleList& clip;
  1699. const Rectangle<int> area;
  1700. JUCE_DECLARE_NON_COPYABLE (SubRectangleIterator);
  1701. };
  1702. //==============================================================================
  1703. class SubRectangleIteratorFloat
  1704. {
  1705. public:
  1706. SubRectangleIteratorFloat (const RectangleList& clipList, const Rectangle<float>& clipBounds) noexcept
  1707. : clip (clipList), area (clipBounds)
  1708. {
  1709. }
  1710. template <class Renderer>
  1711. void iterate (Renderer& r) const noexcept
  1712. {
  1713. const RenderingHelpers::FloatRectangleRasterisingInfo f (area);
  1714. RectangleList::Iterator iter (clip);
  1715. while (iter.next())
  1716. {
  1717. const int clipLeft = iter.getRectangle()->getX();
  1718. const int clipRight = iter.getRectangle()->getRight();
  1719. const int clipTop = iter.getRectangle()->getY();
  1720. const int clipBottom = iter.getRectangle()->getBottom();
  1721. if (f.totalBottom > clipTop && f.totalTop < clipBottom && f.totalRight > clipLeft && f.totalLeft < clipRight)
  1722. {
  1723. if (f.isOnePixelWide())
  1724. {
  1725. if (f.topAlpha != 0 && f.totalTop >= clipTop)
  1726. {
  1727. r.setEdgeTableYPos (f.totalTop);
  1728. r.handleEdgeTablePixel (f.left, f.topAlpha);
  1729. }
  1730. const int endY = jmin (f.bottom, clipBottom);
  1731. for (int y = jmax (clipTop, f.top); y < endY; ++y)
  1732. {
  1733. r.setEdgeTableYPos (y);
  1734. r.handleEdgeTablePixelFull (f.left);
  1735. }
  1736. if (f.bottomAlpha != 0 && f.bottom < clipBottom)
  1737. {
  1738. r.setEdgeTableYPos (f.bottom);
  1739. r.handleEdgeTablePixel (f.left, f.bottomAlpha);
  1740. }
  1741. }
  1742. else
  1743. {
  1744. const int clippedLeft = jmax (f.left, clipLeft);
  1745. const int clippedWidth = jmin (f.right, clipRight) - clippedLeft;
  1746. const bool doLeftAlpha = f.leftAlpha != 0 && f.totalLeft >= clipLeft;
  1747. const bool doRightAlpha = f.rightAlpha != 0 && f.right < clipRight;
  1748. if (f.topAlpha != 0 && f.totalTop >= clipTop)
  1749. {
  1750. r.setEdgeTableYPos (f.totalTop);
  1751. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.getTopLeftCornerAlpha());
  1752. if (clippedWidth > 0) r.handleEdgeTableLine (clippedLeft, clippedWidth, f.topAlpha);
  1753. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.getTopRightCornerAlpha());
  1754. }
  1755. const int endY = jmin (f.bottom, clipBottom);
  1756. for (int y = jmax (clipTop, f.top); y < endY; ++y)
  1757. {
  1758. r.setEdgeTableYPos (y);
  1759. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.leftAlpha);
  1760. if (clippedWidth > 0) r.handleEdgeTableLineFull (clippedLeft, clippedWidth);
  1761. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.rightAlpha);
  1762. }
  1763. if (f.bottomAlpha != 0 && f.bottom < clipBottom)
  1764. {
  1765. r.setEdgeTableYPos (f.bottom);
  1766. if (doLeftAlpha) r.handleEdgeTablePixel (f.totalLeft, f.getBottomLeftCornerAlpha());
  1767. if (clippedWidth > 0) r.handleEdgeTableLine (clippedLeft, clippedWidth, f.bottomAlpha);
  1768. if (doRightAlpha) r.handleEdgeTablePixel (f.right, f.getBottomRightCornerAlpha());
  1769. }
  1770. }
  1771. }
  1772. }
  1773. }
  1774. private:
  1775. const RectangleList& clip;
  1776. const Rectangle<float>& area;
  1777. JUCE_DECLARE_NON_COPYABLE (SubRectangleIteratorFloat);
  1778. };
  1779. inline Ptr toEdgeTable() const { return new EdgeTableRegion (clip); }
  1780. RectangleListRegion& operator= (const RectangleListRegion&);
  1781. };
  1782. }
  1783. //==============================================================================
  1784. class SoftwareRendererSavedState
  1785. {
  1786. public:
  1787. SoftwareRendererSavedState (const Image& im, const Rectangle<int>& clipBounds)
  1788. : image (im), clip (new ClipRegions::RectangleListRegion (clipBounds)),
  1789. transform (0, 0),
  1790. interpolationQuality (Graphics::mediumResamplingQuality),
  1791. transparencyLayerAlpha (1.0f)
  1792. {
  1793. }
  1794. SoftwareRendererSavedState (const Image& im, const RectangleList& clipList, const int x, const int y)
  1795. : image (im), clip (new ClipRegions::RectangleListRegion (clipList)),
  1796. transform (x, y),
  1797. interpolationQuality (Graphics::mediumResamplingQuality),
  1798. transparencyLayerAlpha (1.0f)
  1799. {
  1800. }
  1801. SoftwareRendererSavedState (const SoftwareRendererSavedState& other)
  1802. : image (other.image), clip (other.clip), transform (other.transform),
  1803. font (other.font), fillType (other.fillType),
  1804. interpolationQuality (other.interpolationQuality),
  1805. transparencyLayerAlpha (other.transparencyLayerAlpha)
  1806. {
  1807. }
  1808. bool clipToRectangle (const Rectangle<int>& r)
  1809. {
  1810. if (clip != nullptr)
  1811. {
  1812. if (transform.isOnlyTranslated)
  1813. {
  1814. cloneClipIfMultiplyReferenced();
  1815. clip = clip->clipToRectangle (transform.translated (r));
  1816. }
  1817. else
  1818. {
  1819. Path p;
  1820. p.addRectangle (r);
  1821. clipToPath (p, AffineTransform::identity);
  1822. }
  1823. }
  1824. return clip != nullptr;
  1825. }
  1826. bool clipToRectangleList (const RectangleList& r)
  1827. {
  1828. if (clip != nullptr)
  1829. {
  1830. if (transform.isOnlyTranslated)
  1831. {
  1832. cloneClipIfMultiplyReferenced();
  1833. RectangleList offsetList (r);
  1834. offsetList.offsetAll (transform.xOffset, transform.yOffset);
  1835. clip = clip->clipToRectangleList (offsetList);
  1836. }
  1837. else
  1838. {
  1839. clipToPath (r.toPath(), AffineTransform::identity);
  1840. }
  1841. }
  1842. return clip != nullptr;
  1843. }
  1844. bool excludeClipRectangle (const Rectangle<int>& r)
  1845. {
  1846. if (clip != nullptr)
  1847. {
  1848. cloneClipIfMultiplyReferenced();
  1849. if (transform.isOnlyTranslated)
  1850. {
  1851. clip = clip->excludeClipRectangle (transform.translated (r));
  1852. }
  1853. else
  1854. {
  1855. Path p;
  1856. p.addRectangle (r.toFloat());
  1857. p.applyTransform (transform.complexTransform);
  1858. p.addRectangle (clip->getClipBounds().toFloat());
  1859. p.setUsingNonZeroWinding (false);
  1860. clip = clip->clipToPath (p, AffineTransform::identity);
  1861. }
  1862. }
  1863. return clip != nullptr;
  1864. }
  1865. void clipToPath (const Path& p, const AffineTransform& t)
  1866. {
  1867. if (clip != nullptr)
  1868. {
  1869. cloneClipIfMultiplyReferenced();
  1870. clip = clip->clipToPath (p, transform.getTransformWith (t));
  1871. }
  1872. }
  1873. void clipToImageAlpha (const Image& sourceImage, const AffineTransform& t)
  1874. {
  1875. if (clip != nullptr)
  1876. {
  1877. if (sourceImage.hasAlphaChannel())
  1878. {
  1879. cloneClipIfMultiplyReferenced();
  1880. clip = clip->clipToImageAlpha (sourceImage, transform.getTransformWith (t),
  1881. interpolationQuality != Graphics::lowResamplingQuality);
  1882. }
  1883. else
  1884. {
  1885. Path p;
  1886. p.addRectangle (sourceImage.getBounds());
  1887. clipToPath (p, t);
  1888. }
  1889. }
  1890. }
  1891. bool clipRegionIntersects (const Rectangle<int>& r) const
  1892. {
  1893. if (clip != nullptr)
  1894. {
  1895. if (transform.isOnlyTranslated)
  1896. return clip->clipRegionIntersects (transform.translated (r));
  1897. else
  1898. return getClipBounds().intersects (r);
  1899. }
  1900. return false;
  1901. }
  1902. Rectangle<int> getClipBounds() const
  1903. {
  1904. return clip != nullptr ? transform.deviceSpaceToUserSpace (clip->getClipBounds())
  1905. : Rectangle<int>();
  1906. }
  1907. SoftwareRendererSavedState* beginTransparencyLayer (float opacity)
  1908. {
  1909. SoftwareRendererSavedState* s = new SoftwareRendererSavedState (*this);
  1910. if (clip != nullptr)
  1911. {
  1912. const Rectangle<int> layerBounds (clip->getClipBounds());
  1913. s->image = Image (Image::ARGB, layerBounds.getWidth(), layerBounds.getHeight(), true);
  1914. s->transparencyLayerAlpha = opacity;
  1915. s->transform.moveOriginInDeviceSpace (-layerBounds.getX(), -layerBounds.getY());
  1916. s->cloneClipIfMultiplyReferenced();
  1917. s->clip->translate (-layerBounds.getPosition());
  1918. }
  1919. return s;
  1920. }
  1921. void endTransparencyLayer (SoftwareRendererSavedState& finishedLayerState)
  1922. {
  1923. if (clip != nullptr)
  1924. {
  1925. const Rectangle<int> layerBounds (clip->getClipBounds());
  1926. const ScopedPointer<LowLevelGraphicsContext> g (image.createLowLevelContext());
  1927. g->setOpacity (finishedLayerState.transparencyLayerAlpha);
  1928. g->drawImage (finishedLayerState.image, AffineTransform::translation ((float) layerBounds.getX(),
  1929. (float) layerBounds.getY()));
  1930. }
  1931. }
  1932. //==============================================================================
  1933. void fillRect (const Rectangle<int>& r, const bool replaceContents)
  1934. {
  1935. if (clip != nullptr)
  1936. {
  1937. if (transform.isOnlyTranslated)
  1938. {
  1939. if (fillType.isColour())
  1940. {
  1941. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  1942. clip->fillRectWithColour (destData, transform.translated (r), fillType.colour.getPixelARGB(), replaceContents);
  1943. }
  1944. else
  1945. {
  1946. const Rectangle<int> totalClip (clip->getClipBounds());
  1947. const Rectangle<int> clipped (totalClip.getIntersection (transform.translated (r)));
  1948. if (! clipped.isEmpty())
  1949. fillShape (new ClipRegions::RectangleListRegion (clipped), false);
  1950. }
  1951. }
  1952. else
  1953. {
  1954. Path p;
  1955. p.addRectangle (r);
  1956. fillPath (p, AffineTransform::identity);
  1957. }
  1958. }
  1959. }
  1960. void fillRect (const Rectangle<float>& r)
  1961. {
  1962. if (clip != nullptr)
  1963. {
  1964. if (transform.isOnlyTranslated)
  1965. {
  1966. if (fillType.isColour())
  1967. {
  1968. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  1969. clip->fillRectWithColour (destData, transform.translated (r), fillType.colour.getPixelARGB());
  1970. }
  1971. else
  1972. {
  1973. const Rectangle<float> totalClip (clip->getClipBounds().toFloat());
  1974. const Rectangle<float> clipped (totalClip.getIntersection (transform.translated (r)));
  1975. if (! clipped.isEmpty())
  1976. fillShape (new ClipRegions::EdgeTableRegion (clipped), false);
  1977. }
  1978. }
  1979. else
  1980. {
  1981. Path p;
  1982. p.addRectangle (r);
  1983. fillPath (p, AffineTransform::identity);
  1984. }
  1985. }
  1986. }
  1987. void fillPath (const Path& path, const AffineTransform& t)
  1988. {
  1989. if (clip != nullptr)
  1990. fillShape (new ClipRegions::EdgeTableRegion (clip->getClipBounds(), path, transform.getTransformWith (t)), false);
  1991. }
  1992. void fillEdgeTable (const EdgeTable& edgeTable, const float x, const int y)
  1993. {
  1994. jassert (transform.isOnlyTranslated);
  1995. if (clip != nullptr)
  1996. {
  1997. ClipRegions::EdgeTableRegion* edgeTableClip = new ClipRegions::EdgeTableRegion (edgeTable);
  1998. edgeTableClip->edgeTable.translate (x + transform.xOffset,
  1999. y + transform.yOffset);
  2000. fillShape (edgeTableClip, false);
  2001. }
  2002. }
  2003. void drawGlyph (const Font& f, int glyphNumber, const AffineTransform& t)
  2004. {
  2005. if (clip != nullptr)
  2006. {
  2007. const ScopedPointer<EdgeTable> et (f.getTypeface()->getEdgeTableForGlyph (glyphNumber, transform.getTransformWith (t)));
  2008. if (et != nullptr)
  2009. fillShape (new ClipRegions::EdgeTableRegion (*et), false);
  2010. }
  2011. }
  2012. void fillShape (ClipRegions::Base::Ptr shapeToFill, const bool replaceContents)
  2013. {
  2014. jassert (clip != nullptr);
  2015. shapeToFill = clip->applyClipTo (shapeToFill);
  2016. if (shapeToFill != nullptr)
  2017. {
  2018. Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2019. if (fillType.isGradient())
  2020. {
  2021. jassert (! replaceContents); // that option is just for solid colours
  2022. ColourGradient g2 (*(fillType.gradient));
  2023. g2.multiplyOpacity (fillType.getOpacity());
  2024. AffineTransform t (transform.getTransformWith (fillType.transform).translated (-0.5f, -0.5f));
  2025. const bool isIdentity = t.isOnlyTranslation();
  2026. if (isIdentity)
  2027. {
  2028. // If our translation doesn't involve any distortion, we can speed it up..
  2029. g2.point1.applyTransform (t);
  2030. g2.point2.applyTransform (t);
  2031. t = AffineTransform::identity;
  2032. }
  2033. shapeToFill->fillAllWithGradient (destData, g2, t, isIdentity);
  2034. }
  2035. else if (fillType.isTiledImage())
  2036. {
  2037. renderImage (fillType.image, fillType.transform, shapeToFill);
  2038. }
  2039. else
  2040. {
  2041. shapeToFill->fillAllWithColour (destData, fillType.colour.getPixelARGB(), replaceContents);
  2042. }
  2043. }
  2044. }
  2045. //==============================================================================
  2046. void renderImage (const Image& sourceImage, const AffineTransform& trans,
  2047. const ClipRegions::Base* const tiledFillClipRegion)
  2048. {
  2049. const AffineTransform t (transform.getTransformWith (trans));
  2050. const Image::BitmapData destData (image, Image::BitmapData::readWrite);
  2051. const Image::BitmapData srcData (sourceImage, Image::BitmapData::readOnly);
  2052. const int alpha = fillType.colour.getAlpha();
  2053. const bool betterQuality = (interpolationQuality != Graphics::lowResamplingQuality);
  2054. if (t.isOnlyTranslation())
  2055. {
  2056. // If our translation doesn't involve any distortion, just use a simple blit..
  2057. int tx = (int) (t.getTranslationX() * 256.0f);
  2058. int ty = (int) (t.getTranslationY() * 256.0f);
  2059. if ((! betterQuality) || ((tx | ty) & 224) == 0)
  2060. {
  2061. tx = ((tx + 128) >> 8);
  2062. ty = ((ty + 128) >> 8);
  2063. if (tiledFillClipRegion != nullptr)
  2064. {
  2065. tiledFillClipRegion->renderImageUntransformed (destData, srcData, alpha, tx, ty, true);
  2066. }
  2067. else
  2068. {
  2069. Rectangle<int> area (tx, ty, sourceImage.getWidth(), sourceImage.getHeight());
  2070. area = area.getIntersection (image.getBounds());
  2071. if (! area.isEmpty())
  2072. {
  2073. ClipRegions::Base::Ptr c (clip->applyClipTo (new ClipRegions::EdgeTableRegion (area)));
  2074. if (c != nullptr)
  2075. c->renderImageUntransformed (destData, srcData, alpha, tx, ty, false);
  2076. }
  2077. }
  2078. return;
  2079. }
  2080. }
  2081. if (! t.isSingularity())
  2082. {
  2083. if (tiledFillClipRegion != nullptr)
  2084. {
  2085. tiledFillClipRegion->renderImageTransformed (destData, srcData, alpha, t, betterQuality, true);
  2086. }
  2087. else
  2088. {
  2089. Path p;
  2090. p.addRectangle (sourceImage.getBounds());
  2091. ClipRegions::Base::Ptr c (clip->clone());
  2092. c = c->clipToPath (p, t);
  2093. if (c != nullptr)
  2094. c->renderImageTransformed (destData, srcData, alpha, t, betterQuality, false);
  2095. }
  2096. }
  2097. }
  2098. //==============================================================================
  2099. Image image;
  2100. ClipRegions::Base::Ptr clip;
  2101. RenderingHelpers::TranslationOrTransform transform;
  2102. Font font;
  2103. FillType fillType;
  2104. Graphics::ResamplingQuality interpolationQuality;
  2105. private:
  2106. float transparencyLayerAlpha;
  2107. void cloneClipIfMultiplyReferenced()
  2108. {
  2109. if (clip->getReferenceCount() > 1)
  2110. clip = clip->clone();
  2111. }
  2112. SoftwareRendererSavedState& operator= (const SoftwareRendererSavedState&);
  2113. };
  2114. //==============================================================================
  2115. template <class StateObjectType>
  2116. class SavedStateStack
  2117. {
  2118. public:
  2119. SavedStateStack (StateObjectType* const initialState) noexcept
  2120. : currentState (initialState)
  2121. {}
  2122. inline StateObjectType* operator->() const noexcept { return currentState; }
  2123. inline StateObjectType& operator*() const noexcept { return *currentState; }
  2124. void save()
  2125. {
  2126. stack.add (new StateObjectType (*currentState));
  2127. }
  2128. void restore()
  2129. {
  2130. StateObjectType* const top = stack.getLast();
  2131. if (top != nullptr)
  2132. {
  2133. currentState = top;
  2134. stack.removeLast (1, false);
  2135. }
  2136. else
  2137. {
  2138. jassertfalse; // trying to pop with an empty stack!
  2139. }
  2140. }
  2141. void beginTransparencyLayer (float opacity)
  2142. {
  2143. save();
  2144. currentState = currentState->beginTransparencyLayer (opacity);
  2145. }
  2146. void endTransparencyLayer()
  2147. {
  2148. const ScopedPointer<StateObjectType> finishedTransparencyLayer (currentState);
  2149. restore();
  2150. currentState->endTransparencyLayer (*finishedTransparencyLayer);
  2151. }
  2152. private:
  2153. ScopedPointer<StateObjectType> currentState;
  2154. OwnedArray<StateObjectType> stack;
  2155. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedStateStack);
  2156. };
  2157. }
  2158. #if JUCE_MSVC
  2159. #pragma warning (pop)
  2160. #endif
  2161. #endif // __JUCE_RENDERINGHELPERS_JUCEHEADER__