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

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