bgrapascalscript.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // SPDX-License-Identifier: LGPL-3.0-linking-exception
  2. {
  3. Created by BGRA Controls Team
  4. Dibo, Circular, lainz (007) and contributors.
  5. For detailed information see readme.txt
  6. Site: https://sourceforge.net/p/bgra-controls/
  7. Wiki: http://wiki.lazarus.freepascal.org/BGRAControls
  8. Forum: http://forum.lazarus.freepascal.org/index.php/board,46.0.html
  9. }
  10. {******************************* CONTRIBUTOR(S) ******************************
  11. - Edivando S. Santos Brasil | [email protected]
  12. (Compatibility with delphi VCL 11/2018)
  13. ***************************** END CONTRIBUTOR(S) *****************************}
  14. unit BGRAPascalScript;
  15. // Note: overloaded procedures not supported, use unique identifiers
  16. {$I bgracontrols.inc}
  17. interface
  18. uses
  19. Classes, SysUtils, BGRABitmap, BGRABitmapTypes;
  20. type
  21. TBGRAColor = longword;
  22. var
  23. BitmapArray: array of TBGRABitmap;
  24. {Internal use only}
  25. procedure bgra_Initialization;
  26. procedure bgra_Finalization;
  27. procedure bgra_AddBitmap(id: integer);
  28. function bgra_GetHighestID: integer;
  29. function BGRAColorToBGRAPixel(AColor: TBGRAColor): TBGRAPixel;
  30. function rgb(red, green, blue: byte): TBGRAColor;
  31. function rgba(red, green, blue, alpha: byte): TBGRAColor;
  32. function getBlue(AColor: TBGRAColor): byte;
  33. function getGreen(AColor: TBGRAColor): byte;
  34. function getRed(AColor: TBGRAColor): byte;
  35. function getAlpha(AColor: TBGRAColor): byte;
  36. function setBlue(AColor: TBGRAColor; AValue: byte): TBGRAColor;
  37. function setGreen(AColor: TBGRAColor; AValue: byte): TBGRAColor;
  38. function setRed(AColor: TBGRAColor; AValue: byte): TBGRAColor;
  39. function setAlpha(AColor: TBGRAColor; AValue: byte): TBGRAColor;
  40. {Constructors}
  41. procedure bgra_Create(id: integer);
  42. procedure bgra_CreateWithSize(id: integer; AWidth, AHeight: integer);
  43. procedure bgra_CreateFromFile(id: integer; AFilename: string);
  44. procedure bgra_Destroy(id: integer);
  45. procedure bgra_DestroyAll;
  46. procedure bgra_Fill(id: integer; AColor: TBGRAColor);
  47. procedure bgra_SetPixel(id: integer; x, y: integer; AColor: TBGRAColor);
  48. function bgra_GetPixel(id: integer; x, y: integer): TBGRAColor;
  49. {Loading functions}
  50. procedure bgra_SaveToFile(id: integer; const filename: string);
  51. {Filters - direct apply}
  52. procedure bgra_FilterSmartZoom3(id: integer; Option: TMedianOption);
  53. procedure bgra_FilterMedian(id: integer; Option: TMedianOption);
  54. procedure bgra_FilterSmooth(id: integer);
  55. procedure bgra_FilterSharpen(id: integer; Amount: single);
  56. procedure bgra_FilterSharpenRect(id: integer; ABounds: TRect; Amount: single);
  57. procedure bgra_FilterContour(id: integer);
  58. procedure bgra_FilterPixelate(id: integer; pixelSize: integer;
  59. useResample: boolean; filter: TResampleFilter);
  60. procedure bgra_FilterBlurRadial(id: integer; radius: integer; blurType: TRadialBlurType);
  61. procedure bgra_FilterBlurRadialRect(id: integer; ABounds: TRect;
  62. radius: integer; blurType: TRadialBlurType);
  63. procedure bgra_FilterBlurMotion(id: integer; distance: integer;
  64. angle: single; oriented: boolean);
  65. procedure bgra_FilterBlurMotionRect(id: integer; ABounds: TRect;
  66. distance: integer; angle: single; oriented: boolean);
  67. procedure bgra_FilterCustomBlur(id: integer; mask: integer);
  68. procedure bgra_FilterCustomBlurRect(id: integer; ABounds: TRect; mask: integer);
  69. procedure bgra_FilterEmboss(id: integer; angle: single);
  70. procedure bgra_FilterEmbossRect(id: integer; angle: single; ABounds: TRect);
  71. procedure bgra_FilterEmbossHighlight(id: integer; FillSelection: boolean);
  72. procedure bgra_FilterEmbossHighlightBorder(id: integer; FillSelection: boolean;
  73. BorderColor: TBGRAColor);
  74. procedure bgra_FilterEmbossHighlightBorderAndOffset(id: integer;
  75. FillSelection: boolean; BorderColor: TBGRAColor; Offset: TPoint);
  76. procedure bgra_FilterGrayscale(id: integer);
  77. procedure bgra_FilterGrayscaleRect(id: integer; ABounds: TRect);
  78. procedure bgra_FilterNormalize(id: integer; eachChannel: boolean);
  79. procedure bgra_FilterNormalizeRect(id: integer; ABounds: TRect; eachChannel: boolean);
  80. procedure bgra_FilterRotate(id: integer; origin: TPointF; angle: single;
  81. correctBlur: boolean);
  82. procedure bgra_FilterSphere(id: integer);
  83. procedure bgra_FilterTwirl(id: integer; ACenter: TPoint; ARadius: single;
  84. ATurn: single; AExponent: single);
  85. procedure bgra_FilterTwirlRect(id: integer; ABounds: TRect; ACenter: TPoint;
  86. ARadius: single; ATurn: single; AExponent: single);
  87. procedure bgra_FilterCylinder(id: integer);
  88. procedure bgra_FilterPlane(id: integer);
  89. implementation
  90. procedure bgra_Initialization;
  91. begin
  92. end;
  93. procedure bgra_Finalization;
  94. var
  95. i: integer;
  96. begin
  97. for i := 0 to High(BitmapArray) do
  98. FreeAndNil(BitmapArray[i]);
  99. BitmapArray := nil;
  100. end;
  101. procedure bgra_AddBitmap(id: integer);
  102. begin
  103. if id + 1 > length(BitmapArray) then
  104. SetLength(BitmapArray, id + 1);
  105. FreeAndNil(BitmapArray[id]);
  106. end;
  107. function bgra_GetHighestID: integer;
  108. begin
  109. Result := High(BitmapArray);
  110. end;
  111. function BGRAColorToBGRAPixel(AColor: TBGRAColor): TBGRAPixel;
  112. begin
  113. Result := TBGRAPixel(
  114. {$IFDEF ENDIAN_BIG}
  115. SwapEndian
  116. {$ENDIF}
  117. (AColor));
  118. end;
  119. function rgb(red, green, blue: byte): TBGRAColor;
  120. begin
  121. Result := blue + (green shl 8) + (red shl 16) + $ff000000;
  122. end;
  123. function rgba(red, green, blue, alpha: byte): TBGRAColor;
  124. begin
  125. Result := blue + (green shl 8) + (red shl 16) + (alpha shl 24);
  126. end;
  127. function getBlue(AColor: TBGRAColor): byte;
  128. begin
  129. Result := AColor and $ff;
  130. end;
  131. function getGreen(AColor: TBGRAColor): byte;
  132. begin
  133. Result := (AColor shr 8) and $ff;
  134. end;
  135. function getRed(AColor: TBGRAColor): byte;
  136. begin
  137. Result := (AColor shr 16) and $ff;
  138. end;
  139. function getAlpha(AColor: TBGRAColor): byte;
  140. begin
  141. Result := AColor shr 24;
  142. end;
  143. function setBlue(AColor: TBGRAColor; AValue: byte): TBGRAColor;
  144. begin
  145. Result := (AColor and $ffffff00) or AValue;
  146. end;
  147. function setGreen(AColor: TBGRAColor; AValue: byte): TBGRAColor;
  148. begin
  149. Result := (AColor and $ffff00ff) or (AValue shl 8);
  150. end;
  151. function setRed(AColor: TBGRAColor; AValue: byte): TBGRAColor;
  152. begin
  153. Result := (AColor and $ff00ffff) or (AValue shl 16);
  154. end;
  155. function setAlpha(AColor: TBGRAColor; AValue: byte): TBGRAColor;
  156. begin
  157. Result := (AColor and $00ffffff) or (AValue shl 24);
  158. end;
  159. procedure bgra_Create(id: integer);
  160. begin
  161. bgra_AddBitmap(id);
  162. BitmapArray[id] := TBGRABitmap.Create;
  163. end;
  164. procedure bgra_CreateWithSize(id: integer; AWidth, AHeight: integer);
  165. begin
  166. bgra_AddBitmap(id);
  167. BitmapArray[id] := TBGRABitmap.Create(AWidth, AHeight);
  168. end;
  169. procedure bgra_CreateFromFile(id: integer; AFilename: string);
  170. begin
  171. bgra_AddBitmap(id);
  172. BitmapArray[id] := TBGRABitmap.Create(AFilename);
  173. end;
  174. procedure bgra_Destroy(id: integer);
  175. begin
  176. FreeAndNil(BitmapArray[id]);
  177. end;
  178. procedure bgra_DestroyAll;
  179. var
  180. id: integer;
  181. begin
  182. for id := 0 to bgra_GetHighestID do
  183. bgra_Destroy(id);
  184. SetLength(BitmapArray, 0);
  185. end;
  186. procedure bgra_Fill(id: integer; AColor: TBGRAColor);
  187. begin
  188. if Assigned(BitmapArray[id]) then
  189. BitmapArray[id].Fill(TBGRAPixel(
  190. {$IFDEF ENDIAN_BIG}
  191. SwapEndian
  192. {$ENDIF}
  193. (AColor)));
  194. end;
  195. procedure bgra_SetPixel(id: integer; x, y: integer; AColor: TBGRAColor);
  196. begin
  197. if Assigned(BitmapArray[id]) then
  198. BitmapArray[id].SetPixel(x, y, TBGRAPixel(
  199. {$IFDEF ENDIAN_BIG}
  200. SwapEndian
  201. {$ENDIF}
  202. (AColor)));
  203. end;
  204. function bgra_GetPixel(id: integer; x, y: integer): TBGRAColor;
  205. begin
  206. if Assigned(BitmapArray[id]) then
  207. Result :=
  208. {$IFDEF ENDIAN_BIG}
  209. SwapEndian
  210. {$ENDIF}
  211. (TBGRAColor(BitmapArray[id].GetPixel(x, y)))
  212. else
  213. Result := 0;
  214. end;
  215. procedure bgra_SaveToFile(id: integer; const filename: string);
  216. begin
  217. BitmapArray[id].SaveToFile(filename);
  218. end;
  219. procedure bgra_FilterSmartZoom3(id: integer; Option: TMedianOption);
  220. begin
  221. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterSmartZoom3(Option) as TBGRABitmap);
  222. end;
  223. procedure bgra_FilterMedian(id: integer; Option: TMedianOption);
  224. begin
  225. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterMedian(Option) as TBGRABitmap);
  226. end;
  227. procedure bgra_FilterSmooth(id: integer);
  228. begin
  229. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterSmooth as TBGRABitmap);
  230. end;
  231. procedure bgra_FilterSharpen(id: integer; Amount: single);
  232. begin
  233. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterSharpen(Amount) as TBGRABitmap);
  234. end;
  235. procedure bgra_FilterSharpenRect(id: integer; ABounds: TRect; Amount: single);
  236. begin
  237. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterSharpen(ABounds, Amount) as
  238. TBGRABitmap);
  239. end;
  240. procedure bgra_FilterContour(id: integer);
  241. begin
  242. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterContour as TBGRABitmap);
  243. end;
  244. procedure bgra_FilterPixelate(id: integer; pixelSize: integer;
  245. useResample: boolean; filter: TResampleFilter);
  246. begin
  247. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterPixelate(pixelSize,
  248. useResample, filter) as TBGRABitmap);
  249. end;
  250. procedure bgra_FilterBlurRadial(id: integer; radius: integer;
  251. blurType: TRadialBlurType);
  252. begin
  253. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterBlurRadial(radius, blurType) as
  254. TBGRABitmap);
  255. end;
  256. procedure bgra_FilterBlurRadialRect(id: integer; ABounds: TRect;
  257. radius: integer; blurType: TRadialBlurType);
  258. begin
  259. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterBlurRadial(ABounds,
  260. radius, blurType) as TBGRABitmap);
  261. end;
  262. procedure bgra_FilterBlurMotion(id: integer; distance: integer;
  263. angle: single; oriented: boolean);
  264. begin
  265. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterBlurMotion(distance,
  266. angle, oriented) as TBGRABitmap);
  267. end;
  268. procedure bgra_FilterBlurMotionRect(id: integer; ABounds: TRect;
  269. distance: integer; angle: single; oriented: boolean);
  270. begin
  271. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterBlurMotion(ABounds,
  272. distance, angle, oriented) as TBGRABitmap);
  273. end;
  274. procedure bgra_FilterCustomBlur(id: integer; mask: integer);
  275. begin
  276. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterCustomBlur(BitmapArray[mask]) as
  277. TBGRABitmap);
  278. end;
  279. procedure bgra_FilterCustomBlurRect(id: integer; ABounds: TRect; mask: integer);
  280. begin
  281. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterCustomBlur(ABounds,
  282. BitmapArray[mask]) as TBGRABitmap);
  283. end;
  284. procedure bgra_FilterEmboss(id: integer; angle: single);
  285. begin
  286. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterEmboss(angle) as TBGRABitmap);
  287. end;
  288. procedure bgra_FilterEmbossRect(id: integer; angle: single; ABounds: TRect);
  289. begin
  290. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterEmboss(angle, ABounds) as
  291. TBGRABitmap);
  292. end;
  293. procedure bgra_FilterEmbossHighlight(id: integer; FillSelection: boolean);
  294. begin
  295. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterEmbossHighlight(FillSelection) as
  296. TBGRABitmap);
  297. end;
  298. procedure bgra_FilterEmbossHighlightBorder(id: integer; FillSelection: boolean;
  299. BorderColor: TBGRAColor);
  300. begin
  301. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterEmbossHighlight(
  302. FillSelection, BGRAColorToBGRAPixel(BorderColor)) as TBGRABitmap);
  303. end;
  304. procedure bgra_FilterEmbossHighlightBorderAndOffset(id: integer;
  305. FillSelection: boolean; BorderColor: TBGRAColor; Offset: TPoint);
  306. begin
  307. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterEmbossHighlight(
  308. FillSelection, BGRAColorToBGRAPixel(BorderColor), Offset) as TBGRABitmap);
  309. end;
  310. procedure bgra_FilterGrayscale(id: integer);
  311. begin
  312. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterGrayscale as TBGRABitmap);
  313. end;
  314. procedure bgra_FilterGrayscaleRect(id: integer; ABounds: TRect);
  315. begin
  316. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterGrayscale(ABounds) as TBGRABitmap);
  317. end;
  318. procedure bgra_FilterNormalize(id: integer; eachChannel: boolean);
  319. begin
  320. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterNormalize(eachChannel) as
  321. TBGRABitmap);
  322. end;
  323. procedure bgra_FilterNormalizeRect(id: integer; ABounds: TRect; eachChannel: boolean);
  324. begin
  325. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterNormalize(ABounds, eachChannel) as
  326. TBGRABitmap);
  327. end;
  328. procedure bgra_FilterRotate(id: integer; origin: TPointF; angle: single;
  329. correctBlur: boolean);
  330. begin
  331. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterRotate(origin,
  332. angle, correctBlur) as TBGRABitmap);
  333. end;
  334. procedure bgra_FilterSphere(id: integer);
  335. begin
  336. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterSphere as TBGRABitmap);
  337. end;
  338. procedure bgra_FilterTwirl(id: integer; ACenter: TPoint; ARadius: single;
  339. ATurn: single; AExponent: single);
  340. begin
  341. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterTwirl(ACenter,
  342. ARadius, ATurn, AExponent) as TBGRABitmap);
  343. end;
  344. procedure bgra_FilterTwirlRect(id: integer; ABounds: TRect; ACenter: TPoint;
  345. ARadius: single; ATurn: single; AExponent: single);
  346. begin
  347. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterTwirl(ABounds,
  348. ACenter, ARadius, ATurn, AExponent) as TBGRABitmap);
  349. end;
  350. procedure bgra_FilterCylinder(id: integer);
  351. begin
  352. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterCylinder as TBGRABitmap);
  353. end;
  354. procedure bgra_FilterPlane(id: integer);
  355. begin
  356. BGRAReplace(BitmapArray[id], BitmapArray[id].FilterPlane as TBGRABitmap);
  357. end;
  358. initialization
  359. bgra_Initialization;
  360. finalization
  361. bgra_Finalization;
  362. end.