GR32_Polygons.pas 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  1. unit GR32_Polygons;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Vectorial Polygon Rasterizer for Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Mattias Andersson <[email protected]>
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2008-2012
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. {$I GR32.inc}
  35. uses
  36. Types, GR32, GR32_Containers, GR32_VPR, GR32_Transforms, GR32_Resamplers;
  37. type
  38. { Polygon join style }
  39. TJoinStyle = (jsMiter, jsBevel, jsRound);
  40. { Polygon end style }
  41. TEndStyle = (esButt, esSquare, esRound);
  42. { Polygon fill mode }
  43. TPolyFillMode = (pfAlternate, pfWinding, pfEvenOdd = 0, pfNonZero);
  44. { TCustomPolygonRenderer }
  45. TCustomPolygonRenderer = class(TThreadPersistent)
  46. public
  47. constructor Create; override;
  48. procedure PolyPolygonFS(const Points: TArrayOfArrayOfFloatPoint;
  49. const ClipRect: TFloatRect; Transformation: TTransformation); overload; virtual;
  50. procedure PolyPolygonFS(const Points: TArrayOfArrayOfFloatPoint;
  51. const ClipRect: TFloatRect); overload; virtual;
  52. procedure PolygonFS(const Points: TArrayOfFloatPoint;
  53. const ClipRect: TFloatRect; Transformation: TTransformation); overload; virtual;
  54. procedure PolygonFS(const Points: TArrayOfFloatPoint;
  55. const ClipRect: TFloatRect); overload; virtual;
  56. // procedure PolyPolygonXS(const Points: TArrayOfArrayOfFixedPoint; const ClipRect: TFixedRect; Transformation: TTransformation); virtual; overload;
  57. // procedure PolyPolygonXS(const Points: TArrayOfArrayOfFixedPoint; const ClipRect: TFixedRect); virtual; overload;
  58. end;
  59. TCustomPolygonRendererClass = class of TCustomPolygonRenderer;
  60. TCustomPolygonFiller = class;
  61. { TPolygonRenderer32 }
  62. TPolygonRenderer32 = class(TCustomPolygonRenderer)
  63. private
  64. FBitmap: TBitmap32;
  65. FFillMode: TPolyFillMode;
  66. FColor: TColor32;
  67. FFiller: TCustomPolygonFiller;
  68. procedure SetColor(const Value: TColor32);
  69. procedure SetFillMode(const Value: TPolyFillMode);
  70. procedure SetFiller(const Value: TCustomPolygonFiller);
  71. protected
  72. procedure SetBitmap(const Value: TBitmap32); virtual;
  73. public
  74. constructor Create(Bitmap: TBitmap32; Fillmode: TPolyFillMode = pfWinding); reintroduce; overload;
  75. procedure PolygonFS(const Points: TArrayOfFloatPoint); overload; virtual;
  76. procedure PolyPolygonFS(const Points: TArrayOfArrayOfFloatPoint); overload; virtual;
  77. property Bitmap: TBitmap32 read FBitmap write SetBitmap;
  78. property FillMode: TPolyFillMode read FFillMode write SetFillMode;
  79. property Color: TColor32 read FColor write SetColor;
  80. property Filler: TCustomPolygonFiller read FFiller write SetFiller;
  81. end;
  82. TPolygonRenderer32Class = class of TPolygonRenderer32;
  83. { TPolygonRenderer32VPR }
  84. { Polygon renderer based on VPR. Computes exact coverages for optimal anti-aliasing. }
  85. TFillProc = procedure(Coverage: PSingleArray; AlphaValues: PColor32Array; Count: Integer; Color: TColor32);
  86. TPolygonRenderer32VPR = class(TPolygonRenderer32)
  87. private
  88. FFillProc: TFillProc;
  89. procedure UpdateFillProcs;
  90. protected
  91. procedure RenderSpan(const Span: TValueSpan; DstY: Integer); virtual;
  92. procedure FillSpan(const Span: TValueSpan; DstY: Integer); virtual;
  93. function GetRenderSpan: TRenderSpanEvent; virtual;
  94. public
  95. procedure PolyPolygonFS(const Points: TArrayOfArrayOfFloatPoint;
  96. const ClipRect: TFloatRect); override;
  97. end;
  98. { TPolygonRenderer32LCD }
  99. TPolygonRenderer32LCD = class(TPolygonRenderer32VPR)
  100. protected
  101. procedure RenderSpan(const Span: TValueSpan; DstY: Integer); override;
  102. public
  103. procedure PolyPolygonFS(const Points: TArrayOfArrayOfFloatPoint;
  104. const ClipRect: TFloatRect); override;
  105. end;
  106. { TPolygonRenderer32LCD2 }
  107. TPolygonRenderer32LCD2 = class(TPolygonRenderer32LCD)
  108. public
  109. procedure RenderSpan(const Span: TValueSpan; DstY: Integer); override;
  110. end;
  111. { TCustomPolygonFiller }
  112. TFillLineEvent = procedure(Dst: PColor32; DstX, DstY, Length: Integer; AlphaValues: PColor32) of object;
  113. TCustomPolygonFiller = class
  114. private
  115. protected
  116. function GetFillLine: TFillLineEvent; virtual; abstract;
  117. public
  118. procedure BeginRendering; virtual;
  119. procedure EndRendering; virtual;
  120. property FillLine: TFillLineEvent read GetFillLine;
  121. end;
  122. { TCallbackPolygonFiller }
  123. TCallbackPolygonFiller = class(TCustomPolygonFiller)
  124. private
  125. FFillLineEvent: TFillLineEvent;
  126. protected
  127. function GetFillLine: TFillLineEvent; override;
  128. public
  129. property FillLineEvent: TFillLineEvent read FFillLineEvent write FFillLineEvent;
  130. end;
  131. { TInvertPolygonFiller }
  132. TInvertPolygonFiller = class(TCustomPolygonFiller)
  133. protected
  134. function GetFillLine: TFillLineEvent; override;
  135. procedure FillLineBlend(Dst: PColor32; DstX, DstY, Length: Integer; AlphaValues: PColor32);
  136. end;
  137. { TBitmapPolygonFiller }
  138. TBitmapPolygonFiller = class(TCustomPolygonFiller)
  139. private
  140. FPattern: TCustomBitmap32;
  141. FOffsetY: Integer;
  142. FOffsetX: Integer;
  143. protected
  144. function GetFillLine: TFillLineEvent; override;
  145. procedure FillLineOpaque(Dst: PColor32; DstX, DstY, Length: Integer; AlphaValues: PColor32);
  146. procedure FillLineBlend(Dst: PColor32; DstX, DstY, Length: Integer; AlphaValues: PColor32);
  147. procedure FillLineBlendMasterAlpha(Dst: PColor32; DstX, DstY, Length: Integer; AlphaValues: PColor32);
  148. procedure FillLineCustomCombine(Dst: PColor32; DstX, DstY, Length: Integer; AlphaValues: PColor32);
  149. public
  150. property Pattern: TCustomBitmap32 read FPattern write FPattern;
  151. property OffsetX: Integer read FOffsetX write FOffsetX;
  152. property OffsetY: Integer read FOffsetY write FOffsetY;
  153. end;
  154. { TSamplerFiller }
  155. TSamplerFiller = class(TCustomPolygonFiller)
  156. private
  157. FSampler: TCustomSampler;
  158. FGetSample: TGetSampleInt;
  159. procedure SetSampler(const Value: TCustomSampler);
  160. protected
  161. procedure SamplerChanged; virtual;
  162. function GetFillLine: TFillLineEvent; override;
  163. procedure SampleLineOpaque(Dst: PColor32; DstX, DstY, Length: Integer; AlphaValues: PColor32);
  164. public
  165. constructor Create(Sampler: TCustomSampler = nil); reintroduce; virtual;
  166. procedure BeginRendering; override;
  167. procedure EndRendering; override;
  168. property Sampler: TCustomSampler read FSampler write SetSampler;
  169. end;
  170. procedure PolyPolygonFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  171. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  172. Transformation: TTransformation = nil); overload;
  173. procedure PolygonFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  174. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  175. Transformation: TTransformation = nil); overload;
  176. procedure PolyPolygonFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  177. Filler: TCustomPolygonFiller; FillMode: TPolyFillMode = pfAlternate;
  178. Transformation: TTransformation = nil); overload;
  179. procedure PolygonFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  180. Filler: TCustomPolygonFiller; FillMode: TPolyFillMode = pfAlternate;
  181. Transformation: TTransformation = nil); overload;
  182. procedure PolyPolygonFS_LCD(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  183. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  184. Transformation: TTransformation = nil); overload;
  185. procedure PolygonFS_LCD(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  186. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  187. Transformation: TTransformation = nil); overload;
  188. procedure PolyPolygonFS_LCD2(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  189. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  190. Transformation: TTransformation = nil); overload;
  191. procedure PolygonFS_LCD2(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  192. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  193. Transformation: TTransformation = nil); overload;
  194. procedure PolyPolygonFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  195. ClipRect: TRect; Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  196. Transformation: TTransformation = nil); overload;
  197. procedure PolygonFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  198. ClipRect: TRect; Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  199. Transformation: TTransformation = nil); overload;
  200. procedure PolyPolygonFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  201. ClipRect: TRect; Filler: TCustomPolygonFiller; FillMode: TPolyFillMode = pfAlternate;
  202. Transformation: TTransformation = nil); overload;
  203. procedure PolygonFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  204. ClipRect: TRect; Filler: TCustomPolygonFiller; FillMode: TPolyFillMode = pfAlternate;
  205. Transformation: TTransformation = nil); overload;
  206. procedure PolyPolygonFS_LCD(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  207. ClipRect: TRect; Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  208. Transformation: TTransformation = nil); overload;
  209. procedure PolygonFS_LCD(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  210. ClipRect: TRect; Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  211. Transformation: TTransformation = nil); overload;
  212. procedure PolyPolygonFS_LCD2(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  213. ClipRect: TRect; Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  214. Transformation: TTransformation = nil); overload;
  215. procedure PolygonFS_LCD2(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  216. ClipRect: TRect; Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  217. Transformation: TTransformation = nil); overload;
  218. procedure PolyPolylineFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  219. Color: TColor32; Closed: Boolean = False; StrokeWidth: TFloat = 1.0;
  220. JoinStyle: TJoinStyle = jsMiter; EndStyle: TEndStyle = esButt;
  221. MiterLimit: TFloat = 4.0; Transformation: TTransformation = nil); overload;
  222. procedure PolyPolylineFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  223. Filler: TCustomPolygonFiller; Closed: Boolean = False; StrokeWidth: TFloat = 1.0;
  224. JoinStyle: TJoinStyle = jsMiter; EndStyle: TEndStyle = esButt;
  225. MiterLimit: TFloat = 4.0; Transformation: TTransformation = nil); overload;
  226. procedure PolylineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  227. Color: TColor32; Closed: Boolean = False; StrokeWidth: TFloat = 1.0;
  228. JoinStyle: TJoinStyle = jsMiter; EndStyle: TEndStyle = esButt;
  229. MiterLimit: TFloat = 4.0; Transformation: TTransformation = nil); overload;
  230. procedure PolylineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  231. Filler: TCustomPolygonFiller; Closed: Boolean = False; StrokeWidth: TFloat = 1.0;
  232. JoinStyle: TJoinStyle = jsMiter; EndStyle: TEndStyle = esButt;
  233. MiterLimit: TFloat = 4.0; Transformation: TTransformation = nil); overload;
  234. //Filled only Dashes ...
  235. procedure DashLineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  236. const Dashes: TArrayOfFloat; Color: TColor32;
  237. Closed: Boolean = False; Width: TFloat = 1.0); overload;
  238. procedure DashLineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  239. const Dashes: TArrayOfFloat; FillColor, StrokeColor: TColor32;
  240. Closed: Boolean; Width: TFloat; StrokeWidth: TFloat = 2.0); overload;
  241. //Filled and stroked Dashes ...
  242. procedure DashLineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  243. const Dashes: TArrayOfFloat; Filler: TCustomPolygonFiller;
  244. Closed: Boolean = False; Width: TFloat = 1.0); overload;
  245. procedure DashLineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  246. const Dashes: TArrayOfFloat; Filler: TCustomPolygonFiller; StrokeColor: TColor32;
  247. Closed: Boolean; Width: TFloat; StrokeWidth: TFloat = 2.0); overload;
  248. procedure PolyPolygonXS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  249. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  250. Transformation: TTransformation = nil); overload;
  251. procedure PolygonXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  252. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  253. Transformation: TTransformation = nil); overload;
  254. procedure PolyPolygonXS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  255. Filler: TCustomPolygonFiller; FillMode: TPolyFillMode = pfAlternate;
  256. Transformation: TTransformation = nil); overload;
  257. procedure PolygonXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  258. Filler: TCustomPolygonFiller; FillMode: TPolyFillMode = pfAlternate;
  259. Transformation: TTransformation = nil); overload;
  260. procedure PolyPolygonXS_LCD(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  261. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  262. Transformation: TTransformation = nil); overload;
  263. procedure PolygonXS_LCD(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  264. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  265. Transformation: TTransformation = nil);
  266. procedure PolyPolygonXS_LCD2(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  267. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  268. Transformation: TTransformation = nil); overload;
  269. procedure PolygonXS_LCD2(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  270. Color: TColor32; FillMode: TPolyFillMode = pfAlternate;
  271. Transformation: TTransformation = nil);
  272. procedure PolyPolylineXS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  273. Color: TColor32; Closed: Boolean = False; StrokeWidth: TFixed = $10000;
  274. JoinStyle: TJoinStyle = jsMiter; EndStyle: TEndStyle = esButt;
  275. MiterLimit: TFixed = $40000; Transformation: TTransformation = nil); overload;
  276. procedure PolyPolylineXS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  277. Filler: TCustomPolygonFiller; Closed: Boolean = False; StrokeWidth: TFixed = $10000;
  278. JoinStyle: TJoinStyle = jsMiter; EndStyle: TEndStyle = esButt;
  279. MiterLimit: TFixed = $40000; Transformation: TTransformation = nil); overload;
  280. procedure PolylineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  281. Color: TColor32; Closed: Boolean = False; StrokeWidth: TFixed = $10000;
  282. JoinStyle: TJoinStyle = jsMiter; EndStyle: TEndStyle = esButt;
  283. MiterLimit: TFixed = $40000; Transformation: TTransformation = nil); overload;
  284. procedure PolylineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  285. Filler: TCustomPolygonFiller; Closed: Boolean = False; StrokeWidth: TFixed = $10000;
  286. JoinStyle: TJoinStyle = jsMiter; EndStyle: TEndStyle = esButt;
  287. MiterLimit: TFixed = $40000; Transformation: TTransformation = nil); overload;
  288. //Filled only Dashes ...
  289. procedure DashLineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  290. const Dashes: TArrayOfFixed; Color: TColor32;
  291. Closed: Boolean = False; Width: TFixed = $10000); overload;
  292. procedure DashLineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  293. const Dashes: TArrayOfFixed; FillColor, StrokeColor: TColor32;
  294. Closed: Boolean; Width: TFixed; StrokeWidth: TFixed = $20000); overload;
  295. //Filled and stroked Dashes ...
  296. procedure DashLineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  297. const Dashes: TArrayOfFixed; Filler: TCustomPolygonFiller;
  298. Closed: Boolean = False; Width: TFixed = $10000); overload;
  299. procedure DashLineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  300. const Dashes: TArrayOfFixed; Filler: TCustomPolygonFiller; StrokeColor: TColor32;
  301. Closed: Boolean; Width: TFixed; StrokeWidth: TFixed = $20000); overload;
  302. // fill entire bitmap with a given polygon filler
  303. procedure FillBitmap(Bitmap: TBitmap32; Filler: TCustomPolygonFiller);
  304. { Registration routines }
  305. procedure RegisterPolygonRenderer(PolygonRendererClass: TCustomPolygonRendererClass);
  306. var
  307. PolygonRendererList: TClassList;
  308. DefaultPolygonRendererClass: TPolygonRenderer32Class = TPolygonRenderer32VPR;
  309. implementation
  310. uses
  311. Math, SysUtils, GR32_Math, GR32_LowLevel, GR32_Blend,
  312. GR32_VectorUtils;
  313. resourcestring
  314. RCStrNoSamplerSpecified = 'No sampler specified!';
  315. type
  316. TBitmap32Access = class(TBitmap32);
  317. procedure RegisterPolygonRenderer(PolygonRendererClass: TCustomPolygonRendererClass);
  318. begin
  319. if not Assigned(PolygonRendererList) then PolygonRendererList := TClassList.Create;
  320. PolygonRendererList.Add(PolygonRendererClass);
  321. end;
  322. // routines for color filling:
  323. procedure MakeAlphaNonZeroUP(Coverage: PSingleArray; AlphaValues: PColor32Array;
  324. Count: Integer; Color: TColor32);
  325. var
  326. I: Integer;
  327. M, V: Cardinal;
  328. Last: TFloat;
  329. C: TColor32Entry absolute Color;
  330. begin
  331. M := C.A * $101;
  332. Last := Infinity;
  333. for I := 0 to Count - 1 do
  334. begin
  335. if PInteger(@Last)^ <> PInteger(@Coverage[I])^ then
  336. begin
  337. Last := Coverage[I];
  338. V := Abs(Round(Last * $10000));
  339. if V > $10000 then V := $10000;
  340. V := V * M shr 24;
  341. {$IFDEF USEGR32GAMMA}
  342. V := GAMMA_TABLE[V];
  343. {$ENDIF}
  344. C.A := V;
  345. end;
  346. AlphaValues[I] := Color;
  347. end;
  348. end;
  349. (*
  350. procedure MakeAlphaNonZeroUP(Coverage: PSingleArray; AlphaValues: PColor32Array;
  351. Count: Integer; Color: TColor32);
  352. var
  353. I: Integer;
  354. M, V, C: Cardinal;
  355. begin
  356. M := Color shr 24 * $101;
  357. C := Color and $00ffffff;
  358. for I := 0 to Count - 1 do
  359. begin
  360. V := Abs(Round(Coverage[I] * $10000));
  361. if V > $10000 then V := $10000;
  362. {$IFDEF USEGR32GAMMA}
  363. V := GAMMA_TABLE[V * M shr 24];
  364. AlphaValues[I] := (V shl 24) or C;
  365. {$ELSE}
  366. AlphaValues[I] := (V * M and $ff000000) or C;
  367. {$ENDIF}
  368. end;
  369. end;
  370. *)
  371. procedure MakeAlphaEvenOddUP(Coverage: PSingleArray; AlphaValues: PColor32Array;
  372. Count: Integer; Color: TColor32);
  373. var
  374. I: Integer;
  375. M, V: Cardinal;
  376. Last: TFloat;
  377. C: TColor32Entry absolute Color;
  378. begin
  379. M := C.A * $101;
  380. Last := Infinity;
  381. for I := 0 to Count - 1 do
  382. begin
  383. if PInteger(@Last)^ <> PInteger(@Coverage[I])^ then
  384. begin
  385. Last := Coverage[I];
  386. V := Abs(Round(Coverage[I] * $10000));
  387. V := V and $01ffff;
  388. if V >= $10000 then
  389. V := V xor $1ffff;
  390. V := V * M shr 24;
  391. {$IFDEF USEGR32GAMMA}
  392. V := GAMMA_TABLE[V];
  393. {$ENDIF}
  394. C.A := V;
  395. end;
  396. AlphaValues[I] := Color;
  397. end;
  398. end;
  399. procedure MakeAlphaNonZeroP(Value: Single; AlphaValues: PColor32Array;
  400. Count: Integer; Color: TColor32);
  401. var
  402. M, V: Cardinal;
  403. C: TColor32Entry absolute Color;
  404. begin
  405. M := C.A * $101;
  406. V := Abs(Round(Value * $10000));
  407. if V > $10000 then V := $10000;
  408. V := V * M shr 24;
  409. {$IFDEF USEGR32GAMMA}
  410. V := GAMMA_TABLE[V];
  411. {$ENDIF}
  412. C.A := V;
  413. FillLongWord(AlphaValues[0], Count, Color);
  414. end;
  415. procedure MakeAlphaEvenOddP(Value: Single; AlphaValues: PColor32Array;
  416. Count: Integer; Color: TColor32);
  417. var
  418. M, V: Cardinal;
  419. C: TColor32Entry absolute Color;
  420. begin
  421. M := C.A * $101;
  422. V := Abs(Round(Value * $10000));
  423. V := V and $01ffff;
  424. if V > $10000 then V := V xor $1ffff;
  425. V := V * M shr 24;
  426. {$IFDEF USEGR32GAMMA}
  427. V := GAMMA_TABLE[V];
  428. {$ENDIF}
  429. C.A := V;
  430. FillLongWord(AlphaValues[0], Count, Color);
  431. end;
  432. // polygon filler routines (extract alpha only):
  433. procedure MakeAlphaNonZeroUPF(Coverage: PSingleArray; AlphaValues: PColor32Array;
  434. Count: Integer; Color: TColor32);
  435. var
  436. I: Integer;
  437. V: Integer;
  438. begin
  439. for I := 0 to Count - 1 do
  440. begin
  441. V := Clamp(Round(Abs(Coverage[I]) * 256));
  442. {$IFDEF USEGR32GAMMA}
  443. V := GAMMA_TABLE[V];
  444. {$ENDIF}
  445. AlphaValues[I] := V;
  446. end;
  447. end;
  448. procedure MakeAlphaEvenOddUPF(Coverage: PSingleArray; AlphaValues: PColor32Array;
  449. Count: Integer; Color: TColor32);
  450. var
  451. I: Integer;
  452. V: Integer;
  453. begin
  454. for I := 0 to Count - 1 do
  455. begin
  456. V := Round(Abs(Coverage[I]) * 256);
  457. V := V and $000001ff;
  458. if V >= $100 then V := V xor $1ff;
  459. {$IFDEF USEGR32GAMMA}
  460. V := GAMMA_TABLE[V];
  461. {$ENDIF}
  462. AlphaValues[I] := V;
  463. end;
  464. end;
  465. procedure MakeAlphaNonZeroPF(Value: Single; AlphaValues: PColor32Array;
  466. Count: Integer; Color: TColor32);
  467. var
  468. V: Integer;
  469. begin
  470. V := Clamp(Round(Abs(Value) * 256));
  471. {$IFDEF USEGR32GAMMA}
  472. V := GAMMA_TABLE[V];
  473. {$ENDIF}
  474. FillLongWord(AlphaValues[0], Count, V);
  475. end;
  476. procedure MakeAlphaEvenOddPF(Value: Single; AlphaValues: PColor32Array;
  477. Count: Integer; Color: TColor32);
  478. var
  479. V: Integer;
  480. begin
  481. V := Round(Abs(Value) * 256);
  482. V := V and $000001ff;
  483. if V >= $100 then V := V xor $1ff;
  484. {$IFDEF USEGR32GAMMA}
  485. V := GAMMA_TABLE[V];
  486. {$ENDIF}
  487. FillLongWord(AlphaValues[0], Count, V);
  488. end;
  489. procedure PolyPolygonFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  490. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  491. var
  492. Renderer: TPolygonRenderer32VPR;
  493. begin
  494. Renderer := TPolygonRenderer32VPR.Create;
  495. try
  496. Renderer.Bitmap := Bitmap;
  497. Renderer.Color := Color;
  498. Renderer.FillMode := FillMode;
  499. Renderer.PolyPolygonFS(Points, FloatRect(Bitmap.ClipRect), Transformation);
  500. finally
  501. Renderer.Free;
  502. end;
  503. end;
  504. procedure PolygonFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  505. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  506. var
  507. Renderer: TPolygonRenderer32VPR;
  508. begin
  509. Renderer := TPolygonRenderer32VPR.Create;
  510. try
  511. Renderer.Bitmap := Bitmap;
  512. Renderer.Color := Color;
  513. Renderer.FillMode := FillMode;
  514. Renderer.PolygonFS(Points, FloatRect(Bitmap.ClipRect), Transformation);
  515. finally
  516. Renderer.Free;
  517. end;
  518. end;
  519. procedure PolyPolygonFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  520. Filler: TCustomPolygonFiller; FillMode: TPolyFillMode; Transformation: TTransformation);
  521. var
  522. Renderer: TPolygonRenderer32VPR;
  523. begin
  524. if not Assigned(Filler) then Exit;
  525. Renderer := TPolygonRenderer32VPR.Create;
  526. try
  527. Renderer.Bitmap := Bitmap;
  528. Renderer.Filler := Filler;
  529. Renderer.FillMode := FillMode;
  530. Renderer.PolyPolygonFS(Points, FloatRect(Bitmap.ClipRect), Transformation);
  531. finally
  532. Renderer.Free;
  533. end;
  534. end;
  535. procedure PolygonFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  536. Filler: TCustomPolygonFiller; FillMode: TPolyFillMode; Transformation: TTransformation);
  537. var
  538. Renderer: TPolygonRenderer32VPR;
  539. begin
  540. if not Assigned(Filler) then Exit;
  541. Renderer := TPolygonRenderer32VPR.Create;
  542. try
  543. Renderer.Bitmap := Bitmap;
  544. Renderer.Filler := Filler;
  545. Renderer.FillMode := FillMode;
  546. Renderer.PolygonFS(Points, FloatRect(Bitmap.ClipRect), Transformation);
  547. finally
  548. Renderer.Free;
  549. end;
  550. end;
  551. procedure PolygonFS_LCD(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  552. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  553. var
  554. Renderer: TPolygonRenderer32LCD;
  555. begin
  556. Renderer := TPolygonRenderer32LCD.Create;
  557. try
  558. Renderer.Bitmap := Bitmap;
  559. Renderer.FillMode := FillMode;
  560. Renderer.Color := Color;
  561. Renderer.PolygonFS(Points, FloatRect(Bitmap.ClipRect), Transformation);
  562. finally
  563. Renderer.Free;
  564. end;
  565. end;
  566. procedure PolyPolygonFS_LCD(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  567. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  568. var
  569. Renderer: TPolygonRenderer32LCD;
  570. begin
  571. Renderer := TPolygonRenderer32LCD.Create;
  572. try
  573. Renderer.Bitmap := Bitmap;
  574. Renderer.FillMode := FillMode;
  575. Renderer.Color := Color;
  576. Renderer.PolyPolygonFS(Points, FloatRect(Bitmap.ClipRect), Transformation);
  577. finally
  578. Renderer.Free;
  579. end;
  580. end;
  581. procedure PolygonFS_LCD2(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  582. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  583. var
  584. Renderer: TPolygonRenderer32LCD2;
  585. begin
  586. Renderer := TPolygonRenderer32LCD2.Create;
  587. try
  588. Renderer.Bitmap := Bitmap;
  589. Renderer.FillMode := FillMode;
  590. Renderer.Color := Color;
  591. Renderer.PolygonFS(Points, FloatRect(Bitmap.ClipRect), Transformation);
  592. finally
  593. Renderer.Free;
  594. end;
  595. end;
  596. procedure PolyPolygonFS_LCD2(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  597. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  598. var
  599. Renderer: TPolygonRenderer32LCD2;
  600. begin
  601. Renderer := TPolygonRenderer32LCD2.Create;
  602. try
  603. Renderer.Bitmap := Bitmap;
  604. Renderer.FillMode := FillMode;
  605. Renderer.Color := Color;
  606. Renderer.PolyPolygonFS(Points, FloatRect(Bitmap.ClipRect), Transformation);
  607. finally
  608. Renderer.Free;
  609. end;
  610. end;
  611. procedure PolyPolygonFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  612. ClipRect: TRect; Color: TColor32; FillMode: TPolyFillMode;
  613. Transformation: TTransformation);
  614. var
  615. Renderer: TPolygonRenderer32VPR;
  616. IntersectedClipRect: TRect;
  617. begin
  618. Renderer := TPolygonRenderer32VPR.Create;
  619. try
  620. Renderer.Bitmap := Bitmap;
  621. Renderer.Color := Color;
  622. Renderer.FillMode := FillMode;
  623. GR32.IntersectRect(IntersectedClipRect, Bitmap.ClipRect, ClipRect);
  624. Renderer.PolyPolygonFS(Points, FloatRect(IntersectedClipRect), Transformation);
  625. finally
  626. Renderer.Free;
  627. end;
  628. end;
  629. procedure PolygonFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  630. ClipRect: TRect; Color: TColor32; FillMode: TPolyFillMode;
  631. Transformation: TTransformation);
  632. var
  633. Renderer: TPolygonRenderer32VPR;
  634. IntersectedClipRect: TRect;
  635. begin
  636. Renderer := TPolygonRenderer32VPR.Create;
  637. try
  638. Renderer.Bitmap := Bitmap;
  639. Renderer.Color := Color;
  640. Renderer.FillMode := FillMode;
  641. GR32.IntersectRect(IntersectedClipRect, Bitmap.ClipRect, ClipRect);
  642. Renderer.PolygonFS(Points, FloatRect(IntersectedClipRect), Transformation);
  643. finally
  644. Renderer.Free;
  645. end;
  646. end;
  647. procedure PolyPolygonFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  648. ClipRect: TRect; Filler: TCustomPolygonFiller; FillMode: TPolyFillMode;
  649. Transformation: TTransformation);
  650. var
  651. Renderer: TPolygonRenderer32VPR;
  652. IntersectedClipRect: TRect;
  653. begin
  654. if not Assigned(Filler) then Exit;
  655. Renderer := TPolygonRenderer32VPR.Create;
  656. try
  657. Renderer.Bitmap := Bitmap;
  658. Renderer.Filler := Filler;
  659. Renderer.FillMode := FillMode;
  660. GR32.IntersectRect(IntersectedClipRect, Bitmap.ClipRect, ClipRect);
  661. Renderer.PolyPolygonFS(Points, FloatRect(IntersectedClipRect), Transformation);
  662. finally
  663. Renderer.Free;
  664. end;
  665. end;
  666. procedure PolygonFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  667. ClipRect: TRect; Filler: TCustomPolygonFiller; FillMode: TPolyFillMode;
  668. Transformation: TTransformation);
  669. var
  670. Renderer: TPolygonRenderer32VPR;
  671. IntersectedClipRect: TRect;
  672. begin
  673. if not Assigned(Filler) then Exit;
  674. Renderer := TPolygonRenderer32VPR.Create;
  675. try
  676. Renderer.Bitmap := Bitmap;
  677. Renderer.Filler := Filler;
  678. Renderer.FillMode := FillMode;
  679. GR32.IntersectRect(IntersectedClipRect, Bitmap.ClipRect, ClipRect);
  680. Renderer.PolygonFS(Points, FloatRect(IntersectedClipRect), Transformation);
  681. finally
  682. Renderer.Free;
  683. end;
  684. end;
  685. procedure PolygonFS_LCD(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  686. ClipRect: TRect; Color: TColor32; FillMode: TPolyFillMode;
  687. Transformation: TTransformation);
  688. var
  689. Renderer: TPolygonRenderer32LCD;
  690. IntersectedClipRect: TRect;
  691. begin
  692. Renderer := TPolygonRenderer32LCD.Create;
  693. try
  694. Renderer.Bitmap := Bitmap;
  695. Renderer.FillMode := FillMode;
  696. Renderer.Color := Color;
  697. GR32.IntersectRect(IntersectedClipRect, Bitmap.ClipRect, ClipRect);
  698. Renderer.PolygonFS(Points, FloatRect(IntersectedClipRect), Transformation);
  699. finally
  700. Renderer.Free;
  701. end;
  702. end;
  703. procedure PolyPolygonFS_LCD(Bitmap: TBitmap32;
  704. const Points: TArrayOfArrayOfFloatPoint; ClipRect: TRect; Color: TColor32;
  705. FillMode: TPolyFillMode; Transformation: TTransformation);
  706. var
  707. Renderer: TPolygonRenderer32LCD;
  708. IntersectedClipRect: TRect;
  709. begin
  710. Renderer := TPolygonRenderer32LCD.Create;
  711. try
  712. Renderer.Bitmap := Bitmap;
  713. Renderer.FillMode := FillMode;
  714. Renderer.Color := Color;
  715. GR32.IntersectRect(IntersectedClipRect, Bitmap.ClipRect, ClipRect);
  716. Renderer.PolyPolygonFS(Points, FloatRect(IntersectedClipRect), Transformation);
  717. finally
  718. Renderer.Free;
  719. end;
  720. end;
  721. procedure PolygonFS_LCD2(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  722. ClipRect: TRect; Color: TColor32; FillMode: TPolyFillMode;
  723. Transformation: TTransformation);
  724. var
  725. Renderer: TPolygonRenderer32LCD2;
  726. IntersectedClipRect: TRect;
  727. begin
  728. Renderer := TPolygonRenderer32LCD2.Create;
  729. try
  730. Renderer.Bitmap := Bitmap;
  731. Renderer.FillMode := FillMode;
  732. Renderer.Color := Color;
  733. GR32.IntersectRect(IntersectedClipRect, Bitmap.ClipRect, ClipRect);
  734. Renderer.PolygonFS(Points, FloatRect(IntersectedClipRect), Transformation);
  735. finally
  736. Renderer.Free;
  737. end;
  738. end;
  739. procedure PolyPolygonFS_LCD2(Bitmap: TBitmap32;
  740. const Points: TArrayOfArrayOfFloatPoint; ClipRect: TRect; Color: TColor32;
  741. FillMode: TPolyFillMode; Transformation: TTransformation);
  742. var
  743. Renderer: TPolygonRenderer32LCD2;
  744. IntersectedClipRect: TRect;
  745. begin
  746. Renderer := TPolygonRenderer32LCD2.Create;
  747. try
  748. Renderer.Bitmap := Bitmap;
  749. Renderer.FillMode := FillMode;
  750. Renderer.Color := Color;
  751. GR32.IntersectRect(IntersectedClipRect, Bitmap.ClipRect, ClipRect);
  752. Renderer.PolyPolygonFS(Points, FloatRect(IntersectedClipRect), Transformation);
  753. finally
  754. Renderer.Free;
  755. end;
  756. end;
  757. procedure PolyPolylineFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  758. Color: TColor32; Closed: Boolean; StrokeWidth: TFloat;
  759. JoinStyle: TJoinStyle; EndStyle: TEndStyle;
  760. MiterLimit: TFloat; Transformation: TTransformation);
  761. var
  762. Dst: TArrayOfArrayOfFloatPoint;
  763. begin
  764. Dst := BuildPolyPolyLine(Points, Closed, StrokeWidth, JoinStyle, EndStyle, MiterLimit);
  765. PolyPolygonFS(Bitmap, Dst, Color, pfWinding, Transformation);
  766. end;
  767. procedure PolyPolylineFS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFloatPoint;
  768. Filler: TCustomPolygonFiller; Closed: Boolean = False; StrokeWidth: TFloat = 1.0;
  769. JoinStyle: TJoinStyle = jsMiter; EndStyle: TEndStyle = esButt;
  770. MiterLimit: TFloat = 4.0; Transformation: TTransformation = nil);
  771. var
  772. Dst: TArrayOfArrayOfFloatPoint;
  773. begin
  774. Dst := BuildPolyPolyLine(Points, Closed, StrokeWidth, JoinStyle, EndStyle, MiterLimit);
  775. PolyPolygonFS(Bitmap, Dst, Filler, pfWinding, Transformation);
  776. end;
  777. procedure PolylineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  778. Color: TColor32; Closed: Boolean; StrokeWidth: TFloat;
  779. JoinStyle: TJoinStyle; EndStyle: TEndStyle;
  780. MiterLimit: TFloat; Transformation: TTransformation);
  781. begin
  782. PolyPolylineFS(Bitmap, PolyPolygon(Points), Color, Closed, StrokeWidth,
  783. JoinStyle, EndStyle, MiterLimit, Transformation);
  784. end;
  785. procedure PolylineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  786. Filler: TCustomPolygonFiller; Closed: Boolean = False; StrokeWidth: TFloat = 1.0;
  787. JoinStyle: TJoinStyle = jsMiter; EndStyle: TEndStyle = esButt;
  788. MiterLimit: TFloat = 4.0; Transformation: TTransformation = nil);
  789. begin
  790. PolyPolylineFS(Bitmap, PolyPolygon(Points), Filler, Closed, StrokeWidth,
  791. JoinStyle, EndStyle, MiterLimit, Transformation);
  792. end;
  793. procedure DashLineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  794. const Dashes: TArrayOfFloat; Color: TColor32;
  795. Closed: Boolean = False; Width: TFloat = 1.0);
  796. var
  797. MultiPoly: TArrayOfArrayOfFloatPoint;
  798. begin
  799. MultiPoly := GR32_VectorUtils.BuildDashedLine(Points, Dashes, 0, Closed);
  800. PolyPolylineFS(Bitmap, MultiPoly, Color, False, Width);
  801. end;
  802. procedure DashLineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  803. const Dashes: TArrayOfFloat; FillColor, StrokeColor: TColor32;
  804. Closed: Boolean; Width: TFloat; StrokeWidth: TFloat = 2.0);
  805. var
  806. MultiPoly: TArrayOfArrayOfFloatPoint;
  807. begin
  808. MultiPoly := GR32_VectorUtils.BuildDashedLine(Points, Dashes, 0, Closed);
  809. MultiPoly := BuildPolyPolyLine(MultiPoly, False, Width);
  810. PolyPolygonFS(Bitmap, MultiPoly, FillColor);
  811. PolyPolylineFS(Bitmap, MultiPoly, StrokeColor, True, StrokeWidth);
  812. end;
  813. procedure DashLineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  814. const Dashes: TArrayOfFloat; Filler: TCustomPolygonFiller;
  815. Closed: Boolean = False; Width: TFloat = 1.0);
  816. var
  817. MultiPoly: TArrayOfArrayOfFloatPoint;
  818. begin
  819. MultiPoly := GR32_VectorUtils.BuildDashedLine(Points, Dashes, 0, Closed);
  820. PolyPolylineFS(Bitmap, MultiPoly, Filler, False, Width);
  821. end;
  822. procedure DashLineFS(Bitmap: TBitmap32; const Points: TArrayOfFloatPoint;
  823. const Dashes: TArrayOfFloat; Filler: TCustomPolygonFiller; StrokeColor: TColor32;
  824. Closed: Boolean; Width: TFloat; StrokeWidth: TFloat = 2.0);
  825. var
  826. MultiPoly: TArrayOfArrayOfFloatPoint;
  827. begin
  828. MultiPoly := GR32_VectorUtils.BuildDashedLine(Points, Dashes, 0, Closed);
  829. MultiPoly := BuildPolyPolyLine(MultiPoly, False, Width);
  830. PolyPolygonFS(Bitmap, MultiPoly, Filler);
  831. PolyPolylineFS(Bitmap, MultiPoly, StrokeColor, True, StrokeWidth);
  832. end;
  833. procedure PolyPolygonXS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  834. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  835. var
  836. Renderer: TPolygonRenderer32VPR;
  837. begin
  838. Renderer := TPolygonRenderer32VPR.Create;
  839. try
  840. Renderer.Bitmap := Bitmap;
  841. Renderer.Color := Color;
  842. Renderer.FillMode := FillMode;
  843. Renderer.PolyPolygonFS(FixedPointToFloatPoint(Points),
  844. FloatRect(Bitmap.ClipRect), Transformation);
  845. finally
  846. Renderer.Free;
  847. end;
  848. end;
  849. procedure PolygonXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  850. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  851. var
  852. Renderer: TPolygonRenderer32VPR;
  853. begin
  854. Renderer := TPolygonRenderer32VPR.Create;
  855. try
  856. Renderer.Bitmap := Bitmap;
  857. Renderer.Color := Color;
  858. Renderer.FillMode := FillMode;
  859. Renderer.PolygonFS(FixedPointToFloatPoint(Points),
  860. FloatRect(Bitmap.ClipRect), Transformation);
  861. finally
  862. Renderer.Free;
  863. end;
  864. end;
  865. procedure PolyPolygonXS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  866. Filler: TCustomPolygonFiller; FillMode: TPolyFillMode; Transformation: TTransformation);
  867. var
  868. Renderer: TPolygonRenderer32VPR;
  869. begin
  870. Renderer := TPolygonRenderer32VPR.Create;
  871. try
  872. Renderer.Bitmap := Bitmap;
  873. Renderer.Filler := Filler;
  874. Renderer.FillMode := FillMode;
  875. Renderer.PolyPolygonFS(FixedPointToFloatPoint(Points),
  876. FloatRect(Bitmap.ClipRect), Transformation);
  877. finally
  878. Renderer.Free;
  879. end;
  880. end;
  881. procedure PolygonXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  882. Filler: TCustomPolygonFiller; FillMode: TPolyFillMode; Transformation: TTransformation);
  883. var
  884. Renderer: TPolygonRenderer32VPR;
  885. begin
  886. Renderer := TPolygonRenderer32VPR.Create;
  887. try
  888. Renderer.Bitmap := Bitmap;
  889. Renderer.Filler := Filler;
  890. Renderer.FillMode := FillMode;
  891. Renderer.PolygonFS(FixedPointToFloatPoint(Points),
  892. FloatRect(Bitmap.ClipRect), Transformation);
  893. finally
  894. Renderer.Free;
  895. end;
  896. end;
  897. procedure PolygonXS_LCD(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  898. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  899. var
  900. Renderer: TPolygonRenderer32LCD;
  901. begin
  902. Renderer := TPolygonRenderer32LCD.Create;
  903. try
  904. Renderer.Bitmap := Bitmap;
  905. Renderer.FillMode := FillMode;
  906. Renderer.Color := Color;
  907. Renderer.PolygonFS(FixedPointToFloatPoint(Points),
  908. FloatRect(Bitmap.ClipRect), Transformation);
  909. finally
  910. Renderer.Free;
  911. end;
  912. end;
  913. procedure PolyPolygonXS_LCD(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  914. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  915. var
  916. Renderer: TPolygonRenderer32LCD;
  917. begin
  918. Renderer := TPolygonRenderer32LCD.Create;
  919. try
  920. Renderer.Bitmap := Bitmap;
  921. Renderer.FillMode := FillMode;
  922. Renderer.Color := Color;
  923. Renderer.PolyPolygonFS(FixedPointToFloatPoint(Points),
  924. FloatRect(Bitmap.ClipRect), Transformation);
  925. finally
  926. Renderer.Free;
  927. end;
  928. end;
  929. procedure PolygonXS_LCD2(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  930. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  931. var
  932. Renderer: TPolygonRenderer32LCD2;
  933. begin
  934. Renderer := TPolygonRenderer32LCD2.Create;
  935. try
  936. Renderer.Bitmap := Bitmap;
  937. Renderer.FillMode := FillMode;
  938. Renderer.Color := Color;
  939. Renderer.PolygonFS(FixedPointToFloatPoint(Points),
  940. FloatRect(Bitmap.ClipRect), Transformation);
  941. finally
  942. Renderer.Free;
  943. end;
  944. end;
  945. procedure PolyPolygonXS_LCD2(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  946. Color: TColor32; FillMode: TPolyFillMode; Transformation: TTransformation);
  947. var
  948. Renderer: TPolygonRenderer32LCD2;
  949. begin
  950. Renderer := TPolygonRenderer32LCD2.Create;
  951. try
  952. Renderer.Bitmap := Bitmap;
  953. Renderer.FillMode := FillMode;
  954. Renderer.Color := Color;
  955. Renderer.PolyPolygonFS(FixedPointToFloatPoint(Points),
  956. FloatRect(Bitmap.ClipRect), Transformation);
  957. finally
  958. Renderer.Free;
  959. end;
  960. end;
  961. procedure PolyPolylineXS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  962. Color: TColor32; Closed: Boolean; StrokeWidth: TFixed;
  963. JoinStyle: TJoinStyle; EndStyle: TEndStyle;
  964. MiterLimit: TFixed; Transformation: TTransformation);
  965. var
  966. Dst: TArrayOfArrayOfFixedPoint;
  967. begin
  968. Dst := BuildPolyPolyLine(Points, Closed, StrokeWidth, JoinStyle, EndStyle,
  969. MiterLimit);
  970. PolyPolygonXS(Bitmap, Dst, Color, pfWinding, Transformation);
  971. end;
  972. procedure PolyPolylineXS(Bitmap: TBitmap32; const Points: TArrayOfArrayOfFixedPoint;
  973. Filler: TCustomPolygonFiller; Closed: Boolean = False;
  974. StrokeWidth: TFixed = $10000; JoinStyle: TJoinStyle = jsMiter;
  975. EndStyle: TEndStyle = esButt; MiterLimit: TFixed = $40000;
  976. Transformation: TTransformation = nil);
  977. var
  978. Dst: TArrayOfArrayOfFixedPoint;
  979. begin
  980. Dst := BuildPolyPolyLine(Points, Closed, StrokeWidth, JoinStyle, EndStyle,
  981. MiterLimit);
  982. PolyPolygonXS(Bitmap, Dst, Filler, pfWinding, Transformation);
  983. end;
  984. procedure PolylineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  985. Color: TColor32; Closed: Boolean; StrokeWidth: TFixed;
  986. JoinStyle: TJoinStyle; EndStyle: TEndStyle;
  987. MiterLimit: TFixed; Transformation: TTransformation);
  988. begin
  989. PolyPolylineXS(Bitmap, PolyPolygon(Points), Color,
  990. Closed, StrokeWidth, JoinStyle, EndStyle,
  991. MiterLimit, Transformation);
  992. end;
  993. procedure PolylineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  994. Filler: TCustomPolygonFiller; Closed: Boolean = False;
  995. StrokeWidth: TFixed = $10000; JoinStyle: TJoinStyle = jsMiter;
  996. EndStyle: TEndStyle = esButt; MiterLimit: TFixed = $40000;
  997. Transformation: TTransformation = nil);
  998. begin
  999. PolyPolylineXS(Bitmap, PolyPolygon(Points), Filler, Closed, StrokeWidth,
  1000. JoinStyle, EndStyle, MiterLimit, Transformation);
  1001. end;
  1002. procedure DashLineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  1003. const Dashes: TArrayOfFixed; Color: TColor32;
  1004. Closed: Boolean = False; Width: TFixed = $10000);
  1005. var
  1006. MultiPoly: TArrayOfArrayOfFixedPoint;
  1007. begin
  1008. MultiPoly := GR32_VectorUtils.BuildDashedLine(Points, Dashes, 0, Closed);
  1009. PolyPolylineXS(Bitmap, MultiPoly, Color, False, Width);
  1010. end;
  1011. procedure DashLineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  1012. const Dashes: TArrayOfFixed; FillColor, StrokeColor: TColor32;
  1013. Closed: Boolean; Width: TFixed; StrokeWidth: TFixed = $20000);
  1014. var
  1015. MultiPoly: TArrayOfArrayOfFixedPoint;
  1016. begin
  1017. MultiPoly := GR32_VectorUtils.BuildDashedLine(Points, Dashes, 0, Closed);
  1018. PolyPolylineXS(Bitmap, MultiPoly, FillColor, False, Width);
  1019. MultiPoly := BuildPolyPolyLine(MultiPoly, False, Width);
  1020. PolyPolylineXS(Bitmap, MultiPoly, StrokeColor, True, strokeWidth);
  1021. end;
  1022. procedure DashLineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  1023. const Dashes: TArrayOfFixed; Filler: TCustomPolygonFiller;
  1024. Closed: Boolean = False; Width: TFixed = $10000);
  1025. var
  1026. MultiPoly: TArrayOfArrayOfFixedPoint;
  1027. begin
  1028. MultiPoly := GR32_VectorUtils.BuildDashedLine(Points, Dashes, 0, Closed);
  1029. PolyPolylineXS(Bitmap, MultiPoly, Filler, False, Width);
  1030. end;
  1031. procedure DashLineXS(Bitmap: TBitmap32; const Points: TArrayOfFixedPoint;
  1032. const Dashes: TArrayOfFixed; Filler: TCustomPolygonFiller; StrokeColor: TColor32;
  1033. Closed: Boolean; Width: TFixed; StrokeWidth: TFixed = $20000);
  1034. var
  1035. MultiPoly: TArrayOfArrayOfFixedPoint;
  1036. begin
  1037. MultiPoly := GR32_VectorUtils.BuildDashedLine(Points, Dashes, 0, Closed);
  1038. PolyPolylineXS(Bitmap, MultiPoly, Filler, False, Width);
  1039. MultiPoly := BuildPolyPolyLine(MultiPoly, False, Width);
  1040. PolyPolylineXS(Bitmap, MultiPoly, StrokeColor, True, StrokeWidth);
  1041. end;
  1042. procedure FillBitmap(Bitmap: TBitmap32; Filler: TCustomPolygonFiller);
  1043. var
  1044. AlphaValues: PColor32;
  1045. Y: Integer;
  1046. begin
  1047. {$IFDEF USESTACKALLOC}
  1048. AlphaValues := StackAlloc(Bitmap.Width * SizeOf(TColor32));
  1049. {$ELSE}
  1050. GetMem(AlphaValues, Bitmap.Width * SizeOf(TColor32));
  1051. {$ENDIF}
  1052. FillLongword(AlphaValues^, Bitmap.Width, $FF);
  1053. Filler.BeginRendering;
  1054. for Y := 0 to Bitmap.Height - 1 do
  1055. Filler.FillLine(PColor32(Bitmap.ScanLine[y]), 0, y, Bitmap.Width, AlphaValues);
  1056. Filler.EndRendering;
  1057. {$IFDEF USESTACKALLOC}
  1058. StackFree(AlphaValues);
  1059. {$ELSE}
  1060. FreeMem(AlphaValues);
  1061. {$ENDIF}
  1062. end;
  1063. { LCD sub-pixel rendering (see http://www.grc.com/cttech.htm) }
  1064. type
  1065. PRGBTriple = ^TRGBTriple;
  1066. TRGBTriple = packed record
  1067. B, G, R: Byte;
  1068. end;
  1069. PRGBTripleArray = ^TRGBTripleArray;
  1070. TRGBTripleArray = array [0..0] of TRGBTriple;
  1071. TMakeAlphaProcLCD = procedure(Coverage: PSingleArray; AlphaValues: SysUtils.PByteArray;
  1072. Count: Integer; Color: TColor32);
  1073. procedure MakeAlphaNonZeroLCD(Coverage: PSingleArray; AlphaValues: SysUtils.PByteArray;
  1074. Count: Integer; Color: TColor32);
  1075. var
  1076. I: Integer;
  1077. M, V: Cardinal;
  1078. Last: TFloat;
  1079. C: TColor32Entry absolute Color;
  1080. begin
  1081. M := C.A * 86; // 86 = 258 / 3
  1082. Last := Infinity;
  1083. V := 0;
  1084. AlphaValues[0] := 0;
  1085. AlphaValues[1] := 0;
  1086. for I := 0 to Count - 1 do
  1087. begin
  1088. if PInteger(@Last)^ <> PInteger(@Coverage[I])^ then
  1089. begin
  1090. Last := Coverage[I];
  1091. V := Abs(Round(Last * $10000));
  1092. if V > $10000 then V := $10000;
  1093. V := V * M shr 24;
  1094. end;
  1095. Inc(AlphaValues[I], V);
  1096. {$IFDEF USEGR32GAMMA}
  1097. AlphaValues[I] := GAMMA_TABLE[AlphaValues[I]];
  1098. {$ENDIF}
  1099. Inc(AlphaValues[I + 1], V);
  1100. AlphaValues[I + 2] := V;
  1101. end;
  1102. AlphaValues[Count + 2] := 0;
  1103. AlphaValues[Count + 3] := 0;
  1104. end;
  1105. procedure MakeAlphaEvenOddLCD(Coverage: PSingleArray; AlphaValues: SysUtils.PByteArray;
  1106. Count: Integer; Color: TColor32);
  1107. var
  1108. I: Integer;
  1109. M, V: Cardinal;
  1110. Last: TFloat;
  1111. begin
  1112. M := Color shr 24 * 86; // 86 = 258 / 3
  1113. Last := Infinity;
  1114. V := 0;
  1115. AlphaValues[0] := 0;
  1116. AlphaValues[1] := 0;
  1117. for I := 0 to Count - 1 do
  1118. begin
  1119. if PInteger(@Last)^ <> PInteger(@Coverage[I])^ then
  1120. begin
  1121. Last := Coverage[I];
  1122. V := Abs(Round(Coverage[I] * $10000));
  1123. V := V and $01ffff;
  1124. if V >= $10000 then V := V xor $1ffff;
  1125. V := V * M shr 24;
  1126. end;
  1127. Inc(AlphaValues[I], V);
  1128. {$IFDEF USEGR32GAMMA}
  1129. AlphaValues[I] := GAMMA_TABLE[AlphaValues[I]];
  1130. {$ENDIF}
  1131. Inc(AlphaValues[I + 1], V);
  1132. AlphaValues[I + 2] := V;
  1133. end;
  1134. AlphaValues[Count + 2] := 0;
  1135. AlphaValues[Count + 3] := 0;
  1136. end;
  1137. procedure MakeAlphaNonZeroLCD2(Coverage: PSingleArray; AlphaValues: SysUtils.PByteArray;
  1138. Count: Integer; Color: TColor32);
  1139. var
  1140. I: Integer;
  1141. begin
  1142. MakeAlphaNonZeroLCD(Coverage, AlphaValues, Count, Color);
  1143. AlphaValues[Count + 2] := (AlphaValues[Count] + AlphaValues[Count + 1]) div 3;
  1144. AlphaValues[Count + 3] := AlphaValues[Count + 1] div 3;
  1145. for I := Count + 1 downto 2 do
  1146. begin
  1147. AlphaValues[I] := (AlphaValues[I] + AlphaValues[I - 1] + AlphaValues[I - 2]) div 3;
  1148. end;
  1149. AlphaValues[1] := (AlphaValues[0] + AlphaValues[1]) div 3;
  1150. AlphaValues[0] := AlphaValues[0] div 3;
  1151. end;
  1152. procedure MakeAlphaEvenOddLCD2(Coverage: PSingleArray; AlphaValues: SysUtils.PByteArray;
  1153. Count: Integer; Color: TColor32);
  1154. var
  1155. I: Integer;
  1156. begin
  1157. MakeAlphaEvenOddLCD(Coverage, AlphaValues, Count, Color);
  1158. AlphaValues[Count + 2] := (AlphaValues[Count] + AlphaValues[Count + 1]) div 3;
  1159. AlphaValues[Count + 3] := AlphaValues[Count + 1] div 3;
  1160. for I := Count + 1 downto 2 do
  1161. begin
  1162. AlphaValues[I] := (AlphaValues[I] + AlphaValues[I - 1] + AlphaValues[I - 2]) div 3;
  1163. end;
  1164. AlphaValues[1] := (AlphaValues[0] + AlphaValues[1]) div 3;
  1165. AlphaValues[0] := AlphaValues[0] div 3;
  1166. end;
  1167. procedure CombineLineLCD(Weights: PRGBTripleArray; Dst: PColor32Array; Color: TColor32; Count: Integer);
  1168. var
  1169. I: Integer;
  1170. {$IFDEF TEST_BLENDMEMRGB128SSE4}
  1171. Weights64: UInt64;
  1172. {$ENDIF}
  1173. begin
  1174. I := 0;
  1175. while Count <> 0 do
  1176. {$IFDEF TEST_BLENDMEMRGB128SSE4}
  1177. if (Count shr 1) = 0 then
  1178. {$ENDIF}
  1179. begin
  1180. if PColor32(@Weights[I])^ = $FFFFFFFF then
  1181. Dst[I] := Color
  1182. else
  1183. BlendMemRGB(Color, Dst[I], PColor32(@Weights[I])^);
  1184. Dec(Count);
  1185. Inc(I);
  1186. end
  1187. {$IFDEF TEST_BLENDMEMRGB128SSE4}
  1188. else
  1189. begin
  1190. Weights64 := (UInt64(PColor32(@Weights[I + 1])^) shl 32) or
  1191. PColor32(@Weights[I])^;
  1192. if Weights64 = $FFFFFFFFFFFFFFFF then
  1193. begin
  1194. Dst[I] := Color;
  1195. Dst[I + 1] := Color;
  1196. end
  1197. else
  1198. BlendMemRGB128(Color, Dst[I], Weights64);
  1199. Dec(Count, 2);
  1200. Inc(I, 2);
  1201. end
  1202. {$ENDIF};
  1203. EMMS;
  1204. end;
  1205. { TCustomPolygonFiller }
  1206. procedure TCustomPolygonFiller.BeginRendering;
  1207. begin
  1208. // implemented by descendants
  1209. end;
  1210. procedure TCustomPolygonFiller.EndRendering;
  1211. begin
  1212. // implemented by descendants
  1213. end;
  1214. { TCallbackPolygonFiller }
  1215. function TCallbackPolygonFiller.GetFillLine: TFillLineEvent;
  1216. begin
  1217. Result := FFillLineEvent;
  1218. end;
  1219. { TInvertPolygonFiller }
  1220. procedure TInvertPolygonFiller.FillLineBlend(Dst: PColor32; DstX, DstY,
  1221. Length: Integer; AlphaValues: PColor32);
  1222. var
  1223. X: Integer;
  1224. BlendMemEx: TBlendMemEx;
  1225. begin
  1226. BlendMemEx := BLEND_MEM_EX[cmBlend]^;
  1227. for X := DstX to DstX + Length - 1 do
  1228. begin
  1229. BlendMemEx(InvertColor(Dst^), Dst^, AlphaValues^);
  1230. EMMS;
  1231. Inc(Dst);
  1232. Inc(AlphaValues);
  1233. end;
  1234. end;
  1235. function TInvertPolygonFiller.GetFillLine: TFillLineEvent;
  1236. begin
  1237. Result := FillLineBlend;
  1238. end;
  1239. { TBitmapPolygonFiller }
  1240. procedure TBitmapPolygonFiller.FillLineOpaque(Dst: PColor32; DstX, DstY,
  1241. Length: Integer; AlphaValues: PColor32);
  1242. var
  1243. PatternX, PatternY, X: Integer;
  1244. OpaqueAlpha: TColor32;
  1245. Src: PColor32;
  1246. BlendMemEx: TBlendMemEx;
  1247. begin
  1248. PatternX := (DstX - OffsetX) mod FPattern.Width;
  1249. if PatternX < 0 then PatternX := (FPattern.Width + PatternX) mod FPattern.Width;
  1250. PatternY := (DstY - OffsetY) mod FPattern.Height;
  1251. if PatternY < 0 then PatternY := (FPattern.Height + PatternY) mod FPattern.Height;
  1252. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1253. if Assigned(AlphaValues) then
  1254. begin
  1255. OpaqueAlpha := TColor32($FF shl 24);
  1256. BlendMemEx := BLEND_MEM_EX[FPattern.CombineMode]^;
  1257. for X := DstX to DstX + Length - 1 do
  1258. begin
  1259. BlendMemEx(Src^ and $00FFFFFF or OpaqueAlpha, Dst^, AlphaValues^);
  1260. Inc(Dst); Inc(Src); Inc(PatternX);
  1261. if PatternX >= FPattern.Width then
  1262. begin
  1263. PatternX := 0;
  1264. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1265. end;
  1266. Inc(AlphaValues);
  1267. end
  1268. end
  1269. else
  1270. for X := DstX to DstX + Length - 1 do
  1271. begin
  1272. Dst^ := Src^;
  1273. Inc(Dst); Inc(Src); Inc(PatternX);
  1274. if PatternX >= FPattern.Width then
  1275. begin
  1276. PatternX := 0;
  1277. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1278. end;
  1279. end;
  1280. end;
  1281. procedure TBitmapPolygonFiller.FillLineBlend(Dst: PColor32; DstX, DstY, Length: Integer; AlphaValues: PColor32);
  1282. var
  1283. PatternX, PatternY, X: Integer;
  1284. Src: PColor32;
  1285. BlendMemEx: TBlendMemEx;
  1286. BlendMem: TBlendMem;
  1287. begin
  1288. PatternX := (DstX - OffsetX) mod FPattern.Width;
  1289. if PatternX < 0 then PatternX := (FPattern.Width + PatternX) mod FPattern.Width;
  1290. PatternY := (DstY - OffsetY) mod FPattern.Height;
  1291. if PatternY < 0 then PatternY := (FPattern.Height + PatternY) mod FPattern.Height;
  1292. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1293. if Assigned(AlphaValues) then
  1294. begin
  1295. BlendMemEx := BLEND_MEM_EX[FPattern.CombineMode]^;
  1296. for X := DstX to DstX + Length - 1 do
  1297. begin
  1298. BlendMemEx(Src^, Dst^, AlphaValues^);
  1299. Inc(Dst); Inc(Src); Inc(PatternX);
  1300. if PatternX >= FPattern.Width then
  1301. begin
  1302. PatternX := 0;
  1303. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1304. end;
  1305. Inc(AlphaValues);
  1306. end
  1307. end
  1308. else
  1309. begin
  1310. BlendMem := BLEND_MEM[FPattern.CombineMode]^;
  1311. for X := DstX to DstX + Length - 1 do
  1312. begin
  1313. BlendMem(Src^, Dst^);
  1314. Inc(Dst); Inc(Src); Inc(PatternX);
  1315. if PatternX >= FPattern.Width then
  1316. begin
  1317. PatternX := 0;
  1318. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1319. end;
  1320. end;
  1321. end;
  1322. end;
  1323. procedure TBitmapPolygonFiller.FillLineBlendMasterAlpha(Dst: PColor32; DstX, DstY,
  1324. Length: Integer; AlphaValues: PColor32);
  1325. var
  1326. PatternX, PatternY, X: Integer;
  1327. Src: PColor32;
  1328. BlendMemEx: TBlendMemEx;
  1329. begin
  1330. PatternX := (DstX - OffsetX) mod FPattern.Width;
  1331. if PatternX < 0 then PatternX := (FPattern.Width + PatternX) mod FPattern.Width;
  1332. PatternY := (DstY - OffsetY) mod FPattern.Height;
  1333. if PatternY < 0 then PatternY := (FPattern.Height + PatternY) mod FPattern.Height;
  1334. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1335. BlendMemEx := BLEND_MEM_EX[FPattern.CombineMode]^;
  1336. if Assigned(AlphaValues) then
  1337. for X := DstX to DstX + Length - 1 do
  1338. begin
  1339. BlendMemEx(Src^, Dst^, Div255(AlphaValues^ * FPattern.MasterAlpha));
  1340. Inc(Dst); Inc(Src); Inc(PatternX);
  1341. if PatternX >= FPattern.Width then
  1342. begin
  1343. PatternX := 0;
  1344. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1345. end;
  1346. Inc(AlphaValues);
  1347. end
  1348. else
  1349. for X := DstX to DstX + Length - 1 do
  1350. begin
  1351. BlendMemEx(Src^, Dst^, FPattern.MasterAlpha);
  1352. Inc(Dst); Inc(Src); Inc(PatternX);
  1353. if PatternX >= FPattern.Width then
  1354. begin
  1355. PatternX := 0;
  1356. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1357. end;
  1358. end;
  1359. end;
  1360. procedure TBitmapPolygonFiller.FillLineCustomCombine(Dst: PColor32; DstX, DstY,
  1361. Length: Integer; AlphaValues: PColor32);
  1362. var
  1363. PatternX, PatternY, X: Integer;
  1364. Src: PColor32;
  1365. begin
  1366. PatternX := (DstX - OffsetX) mod FPattern.Width;
  1367. if PatternX < 0 then PatternX := (FPattern.Width + PatternX) mod FPattern.Width;
  1368. PatternY := (DstY - OffsetY) mod FPattern.Height;
  1369. if PatternY < 0 then PatternY := (FPattern.Height + PatternY) mod FPattern.Height;
  1370. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1371. if Assigned(AlphaValues) then
  1372. for X := DstX to DstX + Length - 1 do
  1373. begin
  1374. FPattern.OnPixelCombine(Src^, Dst^, Div255(AlphaValues^ * FPattern.MasterAlpha));
  1375. Inc(Dst); Inc(Src); Inc(PatternX);
  1376. if PatternX >= FPattern.Width then
  1377. begin
  1378. PatternX := 0;
  1379. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1380. end;
  1381. Inc(AlphaValues);
  1382. end
  1383. else
  1384. for X := DstX to DstX + Length - 1 do
  1385. begin
  1386. FPattern.OnPixelCombine(Src^, Dst^, FPattern.MasterAlpha);
  1387. Inc(Dst); Inc(Src); Inc(PatternX);
  1388. if PatternX >= FPattern.Width then
  1389. begin
  1390. PatternX := 0;
  1391. Src := @FPattern.Bits[PatternX + PatternY * FPattern.Width];
  1392. end;
  1393. end;
  1394. end;
  1395. function TBitmapPolygonFiller.GetFillLine: TFillLineEvent;
  1396. begin
  1397. if not Assigned(FPattern) then
  1398. begin
  1399. Result := nil;
  1400. end
  1401. else if FPattern.DrawMode = dmOpaque then
  1402. Result := FillLineOpaque
  1403. else if FPattern.DrawMode = dmBlend then
  1404. begin
  1405. if FPattern.MasterAlpha = 255 then
  1406. Result := FillLineBlend
  1407. else
  1408. Result := FillLineBlendMasterAlpha;
  1409. end
  1410. else if (FPattern.DrawMode = dmCustom) and Assigned(FPattern.OnPixelCombine) then
  1411. begin
  1412. Result := FillLineCustomCombine;
  1413. end
  1414. else
  1415. Result := nil;
  1416. end;
  1417. { TSamplerFiller }
  1418. constructor TSamplerFiller.Create(Sampler: TCustomSampler = nil);
  1419. begin
  1420. inherited Create;
  1421. FSampler := Sampler;
  1422. SamplerChanged;
  1423. end;
  1424. procedure TSamplerFiller.EndRendering;
  1425. begin
  1426. if Assigned(FSampler) then
  1427. FSampler.FinalizeSampling
  1428. else
  1429. raise Exception.Create(RCStrNoSamplerSpecified);
  1430. end;
  1431. procedure TSamplerFiller.SampleLineOpaque(Dst: PColor32; DstX, DstY,
  1432. Length: Integer; AlphaValues: PColor32);
  1433. var
  1434. X: Integer;
  1435. BlendMemEx: TBlendMemEx;
  1436. begin
  1437. BlendMemEx := BLEND_MEM_EX[cmBlend]^;
  1438. for X := DstX to DstX + Length - 1 do
  1439. begin
  1440. BlendMemEx(FGetSample(X, DstY) and $00FFFFFF or $FF000000, Dst^, AlphaValues^);
  1441. EMMS;
  1442. Inc(Dst);
  1443. Inc(AlphaValues);
  1444. end;
  1445. end;
  1446. procedure TSamplerFiller.SamplerChanged;
  1447. begin
  1448. if Assigned(FSampler) then
  1449. FGetSample := FSampler.GetSampleInt;
  1450. end;
  1451. procedure TSamplerFiller.BeginRendering;
  1452. begin
  1453. if Assigned(FSampler) then
  1454. FSampler.PrepareSampling
  1455. else
  1456. raise Exception.Create(RCStrNoSamplerSpecified);
  1457. end;
  1458. function TSamplerFiller.GetFillLine: TFillLineEvent;
  1459. begin
  1460. Result := SampleLineOpaque;
  1461. end;
  1462. procedure TSamplerFiller.SetSampler(const Value: TCustomSampler);
  1463. begin
  1464. if FSampler <> Value then
  1465. begin
  1466. FSampler := Value;
  1467. SamplerChanged;
  1468. end;
  1469. end;
  1470. { TCustomPolygonRenderer }
  1471. constructor TCustomPolygonRenderer.Create;
  1472. begin
  1473. end;
  1474. procedure TCustomPolygonRenderer.PolygonFS(
  1475. const Points: TArrayOfFloatPoint; const ClipRect: TFloatRect;
  1476. Transformation: TTransformation);
  1477. begin
  1478. PolyPolygonFS(PolyPolygon(Points), ClipRect, Transformation);
  1479. end;
  1480. procedure TCustomPolygonRenderer.PolygonFS(
  1481. const Points: TArrayOfFloatPoint; const ClipRect: TFloatRect);
  1482. begin
  1483. PolyPolygonFS(PolyPolygon(Points), ClipRect);
  1484. end;
  1485. procedure TCustomPolygonRenderer.PolyPolygonFS(
  1486. const Points: TArrayOfArrayOfFloatPoint; const ClipRect: TFloatRect);
  1487. begin
  1488. // implemented by descendants
  1489. end;
  1490. procedure TCustomPolygonRenderer.PolyPolygonFS(
  1491. const Points: TArrayOfArrayOfFloatPoint; const ClipRect: TFloatRect;
  1492. Transformation: TTransformation);
  1493. var
  1494. APoints: TArrayOfArrayOfFloatPoint;
  1495. begin
  1496. if Assigned(Transformation) then
  1497. APoints := TransformPolyPolygon(Points, Transformation)
  1498. else
  1499. APoints := Points;
  1500. PolyPolygonFS(APoints, ClipRect);
  1501. end;
  1502. { TPolygonRenderer32 }
  1503. constructor TPolygonRenderer32.Create(Bitmap: TBitmap32;
  1504. Fillmode: TPolyFillMode);
  1505. begin
  1506. inherited Create;
  1507. FBitmap := Bitmap;
  1508. FFillMode := Fillmode;
  1509. end;
  1510. procedure TPolygonRenderer32.PolygonFS(const Points: TArrayOfFloatPoint);
  1511. begin
  1512. PolyPolygonFS(PolyPolygon(Points), FloatRect(FBitmap.ClipRect));
  1513. end;
  1514. procedure TPolygonRenderer32.PolyPolygonFS(const Points: TArrayOfArrayOfFloatPoint);
  1515. begin
  1516. PolyPolygonFS(Points, FloatRect(FBitmap.ClipRect));
  1517. end;
  1518. procedure TPolygonRenderer32.SetBitmap(const Value: TBitmap32);
  1519. begin
  1520. if FBitmap <> Value then
  1521. begin
  1522. FBitmap := Value;
  1523. Changed;
  1524. end;
  1525. end;
  1526. procedure TPolygonRenderer32.SetColor(const Value: TColor32);
  1527. begin
  1528. if FColor <> Value then
  1529. begin
  1530. FColor := Value;
  1531. Changed;
  1532. end;
  1533. end;
  1534. procedure TPolygonRenderer32.SetFiller(const Value: TCustomPolygonFiller);
  1535. begin
  1536. if FFiller <> Value then
  1537. begin
  1538. FFiller := Value;
  1539. Changed;
  1540. end;
  1541. end;
  1542. procedure TPolygonRenderer32.SetFillMode(const Value: TPolyFillMode);
  1543. begin
  1544. if FFillMode <> Value then
  1545. begin
  1546. FFillMode := Value;
  1547. Changed;
  1548. end;
  1549. end;
  1550. { TPolygonRenderer32VPR }
  1551. {$IFDEF USESTACKALLOC}
  1552. {$W+}
  1553. {$ENDIF}
  1554. procedure TPolygonRenderer32VPR.FillSpan(const Span: TValueSpan; DstY: Integer);
  1555. var
  1556. AlphaValues: PColor32Array;
  1557. Count: Integer;
  1558. begin
  1559. Count := Span.X2 - Span.X1 + 1;
  1560. {$IFDEF USESTACKALLOC}
  1561. AlphaValues := StackAlloc(Count * SizeOf(TColor32));
  1562. {$ELSE}
  1563. GetMem(AlphaValues, Count * SizeOf(TColor32));
  1564. {$ENDIF}
  1565. FFillProc(Span.Values, AlphaValues, Count, FColor);
  1566. FFiller.FillLine(@Bitmap.ScanLine[DstY][Span.X1], Span.X1, DstY, Count,
  1567. PColor32(AlphaValues));
  1568. EMMS;
  1569. {$IFDEF USESTACKALLOC}
  1570. StackFree(AlphaValues);
  1571. {$ELSE}
  1572. FreeMem(AlphaValues);
  1573. {$ENDIF}
  1574. end;
  1575. {$IFDEF USESTACKALLOC}
  1576. {$W-}
  1577. {$ENDIF}
  1578. function TPolygonRenderer32VPR.GetRenderSpan: TRenderSpanEvent;
  1579. begin
  1580. if Assigned(FFiller) then
  1581. Result := FillSpan
  1582. else
  1583. Result := RenderSpan;
  1584. end;
  1585. procedure TPolygonRenderer32VPR.PolyPolygonFS(
  1586. const Points: TArrayOfArrayOfFloatPoint; const ClipRect: TFloatRect);
  1587. {$IFDEF CHANGENOTIFICATIONS}
  1588. var
  1589. I: Integer;
  1590. {$ENDIF}
  1591. begin
  1592. UpdateFillProcs;
  1593. if Assigned(FFiller) then
  1594. begin
  1595. FFiller.BeginRendering;
  1596. RenderPolyPolygon(Points, ClipRect, GetRenderSpan());
  1597. FFiller.EndRendering;
  1598. end
  1599. else
  1600. RenderPolyPolygon(Points, ClipRect, GetRenderSpan());
  1601. {$IFDEF CHANGENOTIFICATIONS}
  1602. if TBitmap32Access(Bitmap).UpdateCount = 0 then
  1603. for I := 0 to High(Points) do
  1604. if Length(Points[I]) > 0 then
  1605. Bitmap.Changed(MakeRect(PolygonBounds(Points[I])));
  1606. {$ENDIF}
  1607. end;
  1608. {$W+}
  1609. procedure TPolygonRenderer32VPR.RenderSpan(const Span: TValueSpan;
  1610. DstY: Integer);
  1611. var
  1612. AlphaValues: PColor32Array;
  1613. Count: Integer;
  1614. begin
  1615. Count := Span.X2 - Span.X1 + 1;
  1616. {$IFDEF USESTACKALLOC}
  1617. AlphaValues := StackAlloc(Count * SizeOf(TColor32));
  1618. {$ELSE}
  1619. GetMem(AlphaValues, Count * SizeOf(TColor32));
  1620. {$ENDIF}
  1621. FFillProc(Span.Values, AlphaValues, Count, FColor);
  1622. if Bitmap.CombineMode = cmMerge then
  1623. MergeLine(@AlphaValues[0], @Bitmap.ScanLine[DstY][Span.X1], Count) else
  1624. BlendLine(@AlphaValues[0], @Bitmap.ScanLine[DstY][Span.X1], Count);
  1625. EMMS;
  1626. {$IFDEF USESTACKALLOC}
  1627. StackFree(AlphaValues);
  1628. {$ELSE}
  1629. FreeMem(AlphaValues);
  1630. {$ENDIF}
  1631. end;
  1632. {$W-}
  1633. procedure TPolygonRenderer32VPR.UpdateFillProcs;
  1634. const
  1635. FillProcs: array [Boolean, TPolyFillMode] of TFillProc = (
  1636. (MakeAlphaEvenOddUP, MakeAlphaNonZeroUP),
  1637. (MakeAlphaEvenOddUPF, MakeAlphaNonZeroUPF)
  1638. );
  1639. begin
  1640. FFillProc := FillProcs[Assigned(FFiller), FillMode];
  1641. end;
  1642. { TPolygonRenderer32LCD }
  1643. procedure TPolygonRenderer32LCD.PolyPolygonFS(
  1644. const Points: TArrayOfArrayOfFloatPoint; const ClipRect: TFloatRect);
  1645. var
  1646. R: TFloatRect;
  1647. APoints: TArrayOfArrayOfFloatPoint;
  1648. {$IFDEF CHANGENOTIFICATIONS}
  1649. I: Integer;
  1650. {$ENDIF}
  1651. begin
  1652. APoints := ScalePolyPolygon(Points, 3, 1);
  1653. R.Top := ClipRect.Top;
  1654. R.Bottom := ClipRect.Bottom;
  1655. R.Left := ClipRect.Left * 3;
  1656. R.Right := ClipRect.Right * 3;
  1657. RenderPolyPolygon(APoints, R, RenderSpan);
  1658. {$IFDEF CHANGENOTIFICATIONS}
  1659. if TBitmap32Access(Bitmap).UpdateCount = 0 then
  1660. for I := 0 to High(Points) do
  1661. if length(Points[I]) > 0 then
  1662. Bitmap.Changed(MakeRect(PolygonBounds(Points[I])));
  1663. {$ENDIF}
  1664. end;
  1665. {$W+}
  1666. procedure TPolygonRenderer32LCD.RenderSpan(const Span: TValueSpan;
  1667. DstY: Integer);
  1668. const
  1669. PADDING = 5;
  1670. var
  1671. AlphaValues: SysUtils.PByteArray;
  1672. Count: Integer;
  1673. X1, Offset: Integer;
  1674. const
  1675. MakeAlpha: array [TPolyFillMode] of TMakeAlphaProcLCD = (MakeAlphaEvenOddLCD, MakeAlphaNonZeroLCD);
  1676. begin
  1677. Count := Span.X2 - Span.X1 + 1;
  1678. X1 := DivMod(Span.X1, 3, Offset);
  1679. // Left Padding + Right Padding + Filter Width = 2 + 2 + 2 = 6
  1680. {$IFDEF USESTACKALLOC}
  1681. AlphaValues := StackAlloc((Count + 6 + PADDING) * SizeOf(Byte));
  1682. {$ELSE}
  1683. GetMem(AlphaValues, (Count + 6 + PADDING) * SizeOf(Byte));
  1684. {$ENDIF}
  1685. AlphaValues[0] := 0;
  1686. AlphaValues[1] := 0;
  1687. if (X1 > 0) then
  1688. begin
  1689. Dec(X1);
  1690. Inc(Offset, 3);
  1691. AlphaValues[2] := 0;
  1692. AlphaValues[3] := 0;
  1693. AlphaValues[4] := 0;
  1694. end;
  1695. MakeAlpha[FFillMode](Span.Values, PByteArray(@AlphaValues[PADDING]), Count, FColor);
  1696. CombineLineLCD(@AlphaValues[PADDING - Offset], PColor32Array(@Bitmap.ScanLine[DstY][X1]), FColor, (Count + Offset + 2) div 3);
  1697. {$IFDEF USESTACKALLOC}
  1698. StackFree(AlphaValues);
  1699. {$ELSE}
  1700. FreeMem(AlphaValues);
  1701. {$ENDIF}
  1702. end;
  1703. {$W-}
  1704. { TPolygonRenderer32LCD2 }
  1705. {$W+}
  1706. procedure TPolygonRenderer32LCD2.RenderSpan(const Span: TValueSpan;
  1707. DstY: Integer);
  1708. const
  1709. PADDING = 5;
  1710. var
  1711. AlphaValues: SysUtils.PByteArray;
  1712. Count: Integer;
  1713. X1, Offset: Integer;
  1714. const
  1715. MakeAlpha: array [TPolyFillMode] of TMakeAlphaProcLCD = (MakeAlphaEvenOddLCD2, MakeAlphaNonZeroLCD2);
  1716. begin
  1717. Count := Span.X2 - Span.X1 + 1;
  1718. X1 := DivMod(Span.X1, 3, Offset);
  1719. // Left Padding + Right Padding + Filter Width = 2 + 2 + 2 = 6
  1720. {$IFDEF USESTACKALLOC}
  1721. AlphaValues := StackAlloc((Count + 6 + PADDING) * SizeOf(Byte));
  1722. {$ELSE}
  1723. GetMem(AlphaValues, (Count + 6 + PADDING) * SizeOf(Byte));
  1724. {$ENDIF}
  1725. AlphaValues[0] := 0;
  1726. AlphaValues[1] := 0;
  1727. if (X1 > 0) then
  1728. begin
  1729. Dec(X1);
  1730. Inc(Offset, 3);
  1731. AlphaValues[2] := 0;
  1732. AlphaValues[3] := 0;
  1733. AlphaValues[4] := 0;
  1734. end;
  1735. Dec(Offset, 1);
  1736. MakeAlpha[FFillMode](Span.Values, PByteArray(@AlphaValues[PADDING]), Count, FColor);
  1737. Inc(Count);
  1738. CombineLineLCD(@AlphaValues[PADDING - Offset], PColor32Array(@Bitmap.ScanLine[DstY][X1]), FColor, (Count + Offset + 2) div 3);
  1739. {$IFDEF USESTACKALLOC}
  1740. StackFree(AlphaValues);
  1741. {$ELSE}
  1742. FreeMem(AlphaValues);
  1743. {$ENDIF}
  1744. end;
  1745. {$W-}
  1746. initialization
  1747. RegisterPolygonRenderer(TPolygonRenderer32VPR);
  1748. RegisterPolygonRenderer(TPolygonRenderer32LCD);
  1749. RegisterPolygonRenderer(TPolygonRenderer32LCD2);
  1750. finalization
  1751. PolygonRendererList.Free;
  1752. end.