Audio plugin host https://kx.studio/carla
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.

juce_RenderingHelpers.h 102KB

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