2
0

GR32.Blur.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. unit GR32.Blur;
  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 Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Anders Melander <[email protected]>
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2008-2024
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * ***** END LICENSE BLOCK ***** *)
  31. interface
  32. {$include GR32.inc}
  33. uses
  34. Classes,
  35. GR32_Bindings,
  36. GR32;
  37. //------------------------------------------------------------------------------
  38. // Note that all blur functions operate on all channels (R, G, B, and A).
  39. // If you don't want the Alpha channel blurred, reset/restore the Alpha of the
  40. // result bitmap after it has been blurred.
  41. //------------------------------------------------------------------------------
  42. //------------------------------------------------------------------------------
  43. //
  44. // Gaussian Blur
  45. //
  46. //------------------------------------------------------------------------------
  47. // Note that although it is common for Gaussian blurs to specify the amount of
  48. // blur as the Gaussian standard deviation parameter (most often referred to
  49. // as "Sigma"), most users would probably prefer to specify the blur amount as
  50. // the radius of the blur.
  51. //
  52. // The problem with that is that the Gaussian curve has an infinite radius
  53. // regardless of the value of Sigma. A common solution, and the one we use here,
  54. // is to simply clip the Gaussian curve to ignore the part that has values
  55. // smaller than one pixel.
  56. //
  57. // So, even though the blur function internally works with Sigma values the
  58. // public wrapper function requires the blur amount to be specified as a pixel
  59. // radius.
  60. // If you need to specify the blur as Sigma, simply convert between Sigma and
  61. // pixels with the GaussianSigmaToRadius and GaussianRadiusToSigma constants:
  62. //
  63. // Radius = Sigma * GaussianSigmaToRadius
  64. // Sigma = Radius * GaussianRadiusToSigma
  65. //
  66. //------------------------------------------------------------------------------
  67. procedure Blur32(ASource, ADest: TBitmap32; Radius: TFloat); overload;
  68. procedure Blur32(Bitmap: TBitmap32; Radius: TFloat); overload;
  69. procedure Blur32(Bitmap: TBitmap32; Radius: TFloat; const Bounds: TRect); overload;
  70. procedure Blur32(Bitmap: TBitmap32; Radius: TFloat; const Region: TArrayOfFloatPoint); overload;
  71. // Variants that take Gamma into acount
  72. procedure GammaBlur32(ASource, ADest: TBitmap32; Radius: TFloat); overload;
  73. procedure GammaBlur32(Bitmap: TBitmap32; Radius: TFloat); overload;
  74. procedure GammaBlur32(Bitmap: TBitmap32; Radius: TFloat; const Bounds: TRect); overload;
  75. procedure GammaBlur32(Bitmap: TBitmap32; Radius: TFloat; const Region: TArrayOfFloatPoint); overload;
  76. const
  77. // Ratio between Radius and Sigma.
  78. GaussianRadiusToSigma = 0.300386630413846; // See TGaussianKernel for the rationale behind this value
  79. GaussianSigmaToRadius = 1 / GaussianRadiusToSigma;
  80. var
  81. Blur32MinRadius: TFloat = 0.5;
  82. // Bindings
  83. type
  84. TBlur32Proc = procedure(ASource, ADest: TBitmap32; Radius: TFloat);
  85. TBlurInplace32Proc = procedure(Bitmap: TBitmap32; Radius: TFloat);
  86. var
  87. Blur32Proc: TBlur32Proc;
  88. BlurInplace32Proc: TBlurInplace32Proc;
  89. GammaBlur32Proc: TBlur32Proc;
  90. GammaBlurInplace32Proc: TBlurInplace32Proc;
  91. //------------------------------------------------------------------------------
  92. //
  93. // Horizontal Blur
  94. //
  95. //------------------------------------------------------------------------------
  96. // Blurs in the horizontal direction only.
  97. // Can be used to implement effects such as motion blur.
  98. //------------------------------------------------------------------------------
  99. var
  100. HorizontalBlur32: TBlur32Proc;
  101. GammaHorizontalBlur32: TBlur32Proc;
  102. //------------------------------------------------------------------------------
  103. //
  104. // Box Blur
  105. //
  106. //------------------------------------------------------------------------------
  107. //
  108. // One way to reduce the cost of a gaussian blur is to use a three-pass box blur
  109. // approach. This means that you convolve the image with a box filter three
  110. // times in a row. The width of the box filter should be the same in each pass.
  111. // This will correspond to convolving the image with a second-order B-spline
  112. // filter, which is very similar to a Gaussian filter.
  113. //
  114. //------------------------------------------------------------------------------
  115. //
  116. // A fast algorithm for performing box-blur is to compute the cumulative sum of
  117. // each scanline and then to determine the convolved pixel value by computing
  118. //
  119. // (CSum[i + r] - CSum[i - r]) / (2*r + 1)
  120. //
  121. // where CSum is the cumulative sum.
  122. //
  123. //------------------------------------------------------------------------------
  124. // When approximating a gaussian blur with a three-pass box blur, be aware that:
  125. // - The cost of a box blur grows exponentially with the blur radius.
  126. // - The current gaussian blur implementation is most likely faster than the
  127. // box blur implementation.
  128. //------------------------------------------------------------------------------
  129. (* Since our default Gaussian blur currently outperforms all known box blur
  130. ** implementations (including variants such as stackblur), the box blur
  131. ** implementations has not been included and the bindings are not made available.
  132. type
  133. TBoxBlur32Proc = procedure(ASource, ADest: TBitmap32; Radius: integer);
  134. TBoxBlurDiscrete32Proc = procedure(ASource, ADest: TBitmap32; Radius: integer; Passes: integer = 3);
  135. var
  136. BoxBlur32: TBoxBlur32Proc deprecated;
  137. BoxBlurDiscrete32: TBoxBlurDiscrete32Proc deprecated;
  138. *)
  139. //------------------------------------------------------------------------------
  140. //
  141. // Bindings
  142. //
  143. //------------------------------------------------------------------------------
  144. function BlurRegistry: TFunctionRegistry;
  145. //------------------------------------------------------------------------------
  146. //------------------------------------------------------------------------------
  147. //------------------------------------------------------------------------------
  148. implementation
  149. uses
  150. Types,
  151. SysUtils,
  152. GR32_Backends_Generic,
  153. GR32_Blend,
  154. GR32_Resamplers,
  155. GR32_Polygons,
  156. GR32_VectorUtils,
  157. GR32.Blur.RecursiveGaussian;
  158. //------------------------------------------------------------------------------
  159. //
  160. // Gaussian Blur
  161. //
  162. //------------------------------------------------------------------------------
  163. //------------------------------------------------------------------------------
  164. // Pixel combiner for use by the Bitmap polygon filler
  165. //------------------------------------------------------------------------------
  166. type
  167. TBlurCombiner = class
  168. public
  169. class procedure PixelCombineHandler(F: TColor32; var B: TColor32; M: Cardinal);
  170. end;
  171. class procedure TBlurCombiner.PixelCombineHandler(F: TColor32; var B: TColor32; M: Cardinal);
  172. begin
  173. CombineMem(F, B, M);
  174. end;
  175. //------------------------------------------------------------------------------
  176. // Abstract blur of region.
  177. // Handles both with and without gamma via delegates.
  178. //------------------------------------------------------------------------------
  179. procedure BlurRegion32(Bitmap: TBitmap32; Radius: TFloat; const Region: TArrayOfFloatPoint; BlurDelegate: TBlur32Proc; BlurInplaceDelegate: TBlurInplace32Proc);
  180. var
  181. Bounds: TRect;
  182. BlurBlock: boolean;
  183. Dest: TBitmap32;
  184. Points: TArrayOfArrayOfFloatPoint;
  185. Filler: TBitmapPolygonFiller;
  186. begin
  187. Bounds := MakeRect(PolygonBounds(Region), rrOutside);
  188. // If we are blurring less than 75% of the bitmap, do it via a temporary bitmap
  189. BlurBlock := (Bitmap.Width*Bitmap.Height * 0.75 > Bounds.Width * Bounds.Height);
  190. Dest := TBitmap32.Create(TMemoryBackend);
  191. try
  192. Dest.DrawMode := dmCustom;
  193. Dest.OnPixelCombine := TBlurCombiner.PixelCombineHandler;
  194. if (BlurBlock) then
  195. begin
  196. // The temporary bitmap contains just the area to be blurred
  197. Dest.SetSize(Bounds.Width, Bounds.Height);
  198. // Copy the target area
  199. BlockTransfer(Dest, 0, 0, Dest.BoundsRect, Bitmap, Bounds, dmOpaque);
  200. // Blur just the target area
  201. BlurInplaceDelegate(Dest, Radius);
  202. // Use a polygon filler to transfer the pixels covered by the region back
  203. // into the target bitmap
  204. Filler := TBitmapPolygonFiller.Create;
  205. try
  206. Filler.Pattern := Dest;
  207. Filler.OffsetX := Bounds.Left;
  208. Filler.OffsetY := Bounds.Top;
  209. Points := [Region];
  210. PolyPolygonFS(Bitmap, Points, Filler);
  211. finally
  212. Filler.Free;
  213. end;
  214. end else
  215. begin
  216. // Blur the whole source bitmap into the temporary bitmap
  217. BlurDelegate(Bitmap, Dest, Radius);
  218. // Use a polygon filler to transfer the pixels covered by the region
  219. // back into the target bitmap
  220. Filler := TBitmapPolygonFiller.Create;
  221. try
  222. Filler.Pattern := Dest;
  223. Points := [Region];
  224. PolyPolygonFS(Bitmap, Points, Filler);
  225. finally
  226. Filler.Free;
  227. end;
  228. end;
  229. finally
  230. Dest.Free;
  231. end;
  232. end;
  233. //------------------------------------------------------------------------------
  234. // Abstract blur of rectagular area.
  235. // Handles both with and without gamma via delegates.
  236. //------------------------------------------------------------------------------
  237. procedure BlurRect32(Bitmap: TBitmap32; Radius: TFloat; const Bounds: TRect; BlurDelegate: TBlur32Proc; BlurInplaceDelegate: TBlurInplace32Proc);
  238. var
  239. Dest: TBitmap32;
  240. Points: TArrayOfFloatPoint;
  241. begin
  242. // If we are blurring less than 75% of the bitmap, do it via a temporary bitmap
  243. if (Bitmap.Width*Bitmap.Height * 0.75 > Bounds.Width * Bounds.Height) then
  244. begin
  245. // Create a temporary bitmap containing just the area to be blurred
  246. Dest := TBitmap32.Create(TMemoryBackend);
  247. try
  248. Dest.SetSize(Bounds.Width, Bounds.Height);
  249. // Copy the target area
  250. BlockTransfer(Dest, 0, 0, Dest.BoundsRect, Bitmap, Bounds, dmOpaque);
  251. // Blur just the target area
  252. BlurInplaceDelegate(Dest, Radius);
  253. // Copy the blurred area back into the source bitmap
  254. BlockTransfer(Bitmap, Bounds.Left, Bounds.Top, Bounds, Dest, Dest.BoundsRect, dmOpaque);
  255. finally
  256. Dest.Free;
  257. end;
  258. end else
  259. begin
  260. // Masked blur via polygon filler
  261. Points := Rectangle(Bounds);
  262. BlurRegion32(Bitmap, Radius, Points, BlurDelegate, BlurInplaceDelegate);
  263. end;
  264. end;
  265. //------------------------------------------------------------------------------
  266. // Blur32 API
  267. //------------------------------------------------------------------------------
  268. procedure Blur32(ASource, ADest: TBitmap32; Radius: TFloat);
  269. begin
  270. if (Radius < Blur32MinRadius) then
  271. begin
  272. ASource.CopyMapTo(ADest);
  273. exit;
  274. end;
  275. if (Assigned(Blur32Proc)) then
  276. Blur32Proc(ASource, ADest, Radius)
  277. else
  278. if (Assigned(BlurInplace32Proc)) then
  279. begin
  280. ASource.CopyMapTo(ADest);
  281. BlurInplace32Proc(ADest, Radius);
  282. end else
  283. raise Exception.Create('Missing Blur32 implementation');
  284. end;
  285. procedure Blur32(Bitmap: TBitmap32; Radius: TFloat);
  286. var
  287. Dest: TBitmap32;
  288. begin
  289. if (Radius < Blur32MinRadius) then
  290. exit;
  291. if (Assigned(BlurInplace32Proc)) then
  292. BlurInplace32Proc(Bitmap, Radius)
  293. else
  294. if (Assigned(Blur32Proc)) then
  295. begin
  296. Dest := TBitmap32.Create(TMemoryBackend);
  297. try
  298. Blur32Proc(Bitmap, Dest, Radius);
  299. Dest.CopyMapTo(Bitmap);
  300. finally
  301. Dest.Free;
  302. end;
  303. end else
  304. raise Exception.Create('Missing Blur32 implementation');
  305. end;
  306. procedure Blur32(Bitmap: TBitmap32; Radius: TFloat; const Bounds: TRect);
  307. begin
  308. if (Radius < Blur32MinRadius) then
  309. exit;
  310. BlurRect32(Bitmap, Radius, Bounds, Blur32, Blur32);
  311. end;
  312. procedure Blur32(Bitmap: TBitmap32; Radius: TFloat; const Region: TArrayOfFloatPoint);
  313. begin
  314. if (Radius < Blur32MinRadius) then
  315. exit;
  316. BlurRegion32(Bitmap, Radius, Region, Blur32, Blur32);
  317. end;
  318. //------------------------------------------------------------------------------
  319. procedure GammaBlur32(ASource, ADest: TBitmap32; Radius: TFloat);
  320. begin
  321. if (Radius < Blur32MinRadius) then
  322. begin
  323. ASource.CopyMapTo(ADest);
  324. exit;
  325. end;
  326. if (Assigned(GammaBlur32Proc)) then
  327. GammaBlur32Proc(ASource, ADest, Radius)
  328. else
  329. if (Assigned(GammaBlurInplace32Proc)) then
  330. begin
  331. ASource.CopyMapTo(ADest);
  332. GammaBlurInplace32Proc(ADest, Radius);
  333. end else
  334. raise Exception.Create('Missing GammaBlur32 implementation');
  335. end;
  336. procedure GammaBlur32(Bitmap: TBitmap32; Radius: TFloat);
  337. var
  338. Dest: TBitmap32;
  339. begin
  340. if (Radius < Blur32MinRadius) then
  341. exit;
  342. if (Assigned(GammaBlurInplace32Proc)) then
  343. GammaBlurInplace32Proc(Bitmap, Radius)
  344. else
  345. if (Assigned(GammaBlur32Proc)) then
  346. begin
  347. Dest := TBitmap32.Create(TMemoryBackend);
  348. try
  349. GammaBlur32Proc(Bitmap, Dest, Radius);
  350. Dest.CopyMapTo(Bitmap);
  351. finally
  352. Dest.Free;
  353. end;
  354. end else
  355. raise Exception.Create('Missing GammaBlur32 implementation');
  356. end;
  357. procedure GammaBlur32(Bitmap: TBitmap32; Radius: TFloat; const Bounds: TRect);
  358. begin
  359. if (Radius < Blur32MinRadius) then
  360. exit;
  361. BlurRect32(Bitmap, Radius, Bounds, GammaBlur32, GammaBlur32);
  362. end;
  363. procedure GammaBlur32(Bitmap: TBitmap32; Radius: TFloat; const Region: TArrayOfFloatPoint);
  364. begin
  365. if (Radius < Blur32MinRadius) then
  366. exit;
  367. BlurRegion32(Bitmap, Radius, Region, GammaBlur32, GammaBlur32);
  368. end;
  369. //------------------------------------------------------------------------------
  370. //
  371. // Bindings
  372. //
  373. //------------------------------------------------------------------------------
  374. procedure Blur32NotImplemented(ASource, ADest: TBitmap32; Radius: TFloat);
  375. begin
  376. raise Exception.Create('This blur function has not been implemented');
  377. end;
  378. procedure BlurInplace32NotImplemented(Bitmap: TBitmap32; Radius: TFloat);
  379. begin
  380. raise Exception.Create('This blur function has not been implemented');
  381. end;
  382. //------------------------------------------------------------------------------
  383. var
  384. FBlurRegistry: TFunctionRegistry;
  385. procedure RegisterBindings;
  386. begin
  387. FBlurRegistry.RegisterBinding(@@Blur32Proc, 'Blur32Proc');
  388. FBlurRegistry.RegisterBinding(@@BlurInplace32Proc, 'BlurInplace32Proc');
  389. FBlurRegistry.RegisterBinding(@@GammaBlur32Proc, 'GammaBlur32Proc');
  390. FBlurRegistry.RegisterBinding(@@GammaBlurInplace32Proc, 'GammaBlurInplace32Proc');
  391. FBlurRegistry.RegisterBinding(@@HorizontalBlur32, 'HorizontalBlur32');
  392. FBlurRegistry.RegisterBinding(@@GammaHorizontalBlur32, 'GammaHorizontalBlur32');
  393. // Default fallback stubs for unimplemented functions
  394. FBlurRegistry[@@HorizontalBlur32].Add(@Blur32NotImplemented, [isPascal], FBlurRegistry.WORST_PRIORITY);
  395. FBlurRegistry[@@GammaHorizontalBlur32].Add(@Blur32NotImplemented, [isPascal], FBlurRegistry.WORST_PRIORITY);
  396. end;
  397. function BlurRegistry: TFunctionRegistry;
  398. begin
  399. if (FBlurRegistry = nil) then
  400. begin
  401. FBlurRegistry := NewRegistry('GR32.Blur bindings');
  402. RegisterBindings;
  403. end;
  404. Result := FBlurRegistry;
  405. end;
  406. initialization
  407. BlurRegistry.RebindAll;
  408. end.