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.

2496 lines
96KB

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