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.

2495 lines
95KB

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