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.

2550 lines
97KB

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