Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2127 lines
70 KiB

3 years ago
  1. {
  2. Vampyre Imaging Library
  3. by Marek Mauder
  4. http://imaginglib.sourceforge.net
  5. The contents of this file are used with permission, subject to the Mozilla
  6. Public License Version 1.1 (the "License"); you may not use this file except
  7. in compliance with the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/MPL-1.1.html
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  11. the specific language governing rights and limitations under the License.
  12. Alternatively, the contents of this file may be used under the terms of the
  13. GNU Lesser General Public License (the "LGPL License"), in which case the
  14. provisions of the LGPL License are applicable instead of those above.
  15. If you wish to allow use of your version of this file only under the terms
  16. of the LGPL License and not to allow others to use your version of this file
  17. under the MPL, indicate your decision by deleting the provisions above and
  18. replace them with the notice and other provisions required by the LGPL
  19. License. If you do not delete the provisions above, a recipient may use
  20. your version of this file under either the MPL or the LGPL License.
  21. For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
  22. }
  23. { This unit contains canvas classes for drawing and applying effects.}
  24. unit ImagingCanvases;
  25. {$I ImagingOptions.inc}
  26. interface
  27. uses
  28. SysUtils, Types, Classes, ImagingTypes, Imaging, ImagingClasses,
  29. ImagingFormats, ImagingUtility;
  30. const
  31. { Color constants in ifA8R8G8B8 format.}
  32. pcClear = $00000000;
  33. pcBlack = $FF000000;
  34. pcWhite = $FFFFFFFF;
  35. pcMaroon = $FF800000;
  36. pcGreen = $FF008000;
  37. pcOlive = $FF808000;
  38. pcNavy = $FF000080;
  39. pcPurple = $FF800080;
  40. pcTeal = $FF008080;
  41. pcGray = $FF808080;
  42. pcSilver = $FFC0C0C0;
  43. pcRed = $FFFF0000;
  44. pcLime = $FF00FF00;
  45. pcYellow = $FFFFFF00;
  46. pcBlue = $FF0000FF;
  47. pcFuchsia = $FFFF00FF;
  48. pcAqua = $FF00FFFF;
  49. pcLtGray = $FFC0C0C0;
  50. pcDkGray = $FF808080;
  51. MaxPenWidth = 256;
  52. type
  53. EImagingCanvasError = class(EImagingError);
  54. EImagingCanvasBlendingError = class(EImagingError);
  55. { Fill mode used when drawing filled objects on canvas.}
  56. TFillMode = (
  57. fmSolid, // Solid fill using current fill color
  58. fmClear // No filling done
  59. );
  60. { Pen mode used when drawing lines, object outlines, and similar on canvas.}
  61. TPenMode = (
  62. pmSolid, // Draws solid lines using current pen color.
  63. pmClear // No drawing done
  64. );
  65. { Source and destination blending factors for drawing functions with blending.
  66. Blending formula: SrcColor * SrcFactor + DestColor * DestFactor }
  67. TBlendingFactor = (
  68. bfIgnore, // Don't care
  69. bfZero, // For Src and Dest, Factor = (0, 0, 0, 0)
  70. bfOne, // For Src and Dest, Factor = (1, 1, 1, 1)
  71. bfSrcAlpha, // For Src and Dest, Factor = (Src.A, Src.A, Src.A, Src.A)
  72. bfOneMinusSrcAlpha, // For Src and Dest, Factor = (1 - Src.A, 1 - Src.A, 1 - Src.A, 1 - Src.A)
  73. bfDstAlpha, // For Src and Dest, Factor = (Dest.A, Dest.A, Dest.A, Dest.A)
  74. bfOneMinusDstAlpha, // For Src and Dest, Factor = (1 - Dest.A, 1 - Dest.A, 1 - Dest.A, 1 - Dest.A)
  75. bfSrcColor, // For Dest, Factor = (Src.R, Src.R, Src.B, Src.A)
  76. bfOneMinusSrcColor, // For Dest, Factor = (1 - Src.R, 1 - Src.G, 1 - Src.B, 1 - Src.A)
  77. bfDstColor, // For Src, Factor = (Dest.R, Dest.G, Dest.B, Dest.A)
  78. bfOneMinusDstColor // For Src, Factor = (1 - Dest.R, 1 - Dest.G, 1 - Dest.B, 1 - Dest.A)
  79. );
  80. { Procedure for custom pixel write modes with blending.}
  81. TPixelWriteProc = procedure(const SrcPix: TColorFPRec; DestPtr: PByte;
  82. DestInfo: PImageFormatInfo; SrcFactor, DestFactor: TBlendingFactor);
  83. { Represents 3x3 convolution filter kernel.}
  84. TConvolutionFilter3x3 = record
  85. Kernel: array[0..2, 0..2] of LongInt;
  86. Divisor: LongInt;
  87. Bias: Single;
  88. end;
  89. { Represents 5x5 convolution filter kernel.}
  90. TConvolutionFilter5x5 = record
  91. Kernel: array[0..4, 0..4] of LongInt;
  92. Divisor: LongInt;
  93. Bias: Single;
  94. end;
  95. TPointTransformFunction = function(const Pixel: TColorFPRec;
  96. Param1, Param2, Param3: Single): TColorFPRec;
  97. TDynFPPixelArray = array of TColorFPRec;
  98. THistogramArray = array[Byte] of Integer;
  99. TSelectPixelFunction = function(var Pixels: TDynFPPixelArray): TColorFPRec;
  100. { Base canvas class for drawing objects, applying effects, and other.
  101. Constructor takes TBaseImage (or pointer to TImageData). Source image
  102. bits are not copied but referenced so all canvas functions affect
  103. source image and vice versa. When you change format or resolution of
  104. source image you must call UpdateCanvasState method (so canvas could
  105. recompute some data size related stuff).
  106. TImagingCanvas works for all image data formats except special ones
  107. (compressed). Because of this its methods are quite slow (they usually work
  108. with colors in ifA32R32G32B32F format). If you want fast drawing you
  109. can use one of fast canvas clases. These descendants of TImagingCanvas
  110. work only for few select formats (or only one) but they are optimized thus
  111. much faster.
  112. }
  113. TImagingCanvas = class(TObject)
  114. private
  115. FDataSizeOnUpdate: LongInt;
  116. FLineRecursion: Boolean;
  117. function GetPixel32(X, Y: LongInt): TColor32; virtual;
  118. function GetPixelFP(X, Y: LongInt): TColorFPRec; virtual;
  119. function GetValid: Boolean; {$IFDEF USE_INLINE}inline;{$ENDIF}
  120. procedure SetPixel32(X, Y: LongInt; const Value: TColor32); virtual;
  121. procedure SetPixelFP(X, Y: LongInt; const Value: TColorFPRec); virtual;
  122. procedure SetPenColor32(const Value: TColor32); {$IFDEF USE_INLINE}inline;{$ENDIF}
  123. procedure SetPenColorFP(const Value: TColorFPRec); {$IFDEF USE_INLINE}inline;{$ENDIF}
  124. procedure SetPenWidth(const Value: LongInt); {$IFDEF USE_INLINE}inline;{$ENDIF}
  125. procedure SetFillColor32(const Value: TColor32); {$IFDEF USE_INLINE}inline;{$ENDIF}
  126. procedure SetFillColorFP(const Value: TColorFPRec); {$IFDEF USE_INLINE}inline;{$ENDIF}
  127. procedure SetClipRect(const Value: TRect);
  128. procedure CheckBeforeBlending(SrcFactor, DestFactor: TBlendingFactor; DestCanvas: TImagingCanvas);
  129. protected
  130. FPData: PImageData;
  131. FClipRect: TRect;
  132. FPenColorFP: TColorFPRec;
  133. FPenColor32: TColor32;
  134. FPenMode: TPenMode;
  135. FPenWidth: LongInt;
  136. FFillColorFP: TColorFPRec;
  137. FFillColor32: TColor32;
  138. FFillMode: TFillMode;
  139. FNativeColor: TColorFPRec;
  140. FFormatInfo: TImageFormatInfo;
  141. { Returns pointer to pixel at given position.}
  142. function GetPixelPointer(X, Y: LongInt): Pointer; {$IFDEF USE_INLINE}inline;{$ENDIF}
  143. { Translates given FP color to native format of canvas and stores it
  144. in FNativeColor field (its bit copy) or user pointer (in overloaded method).}
  145. procedure TranslateFPToNative(const Color: TColorFPRec); overload; {$IFDEF USE_INLINE}inline;{$ENDIF}
  146. procedure TranslateFPToNative(const Color: TColorFPRec; Native: Pointer); overload; {$IFDEF USE_INLINE}inline;{$ENDIF}
  147. { Clipping function used by horizontal and vertical line drawing functions.}
  148. function ClipAxisParallelLine(var A1, A2, B: LongInt;
  149. AStart, AStop, BStart, BStop: LongInt): Boolean;
  150. { Internal horizontal line drawer used mainly for filling inside of objects
  151. like ellipses and circles.}
  152. procedure HorzLineInternal(X1, X2, Y: LongInt; Color: Pointer; Bpp: LongInt); virtual;
  153. procedure CopyPixelInternal(X, Y: LongInt; Pixel: Pointer; Bpp: LongInt); {$IFDEF USE_INLINE}inline;{$ENDIF}
  154. procedure DrawInternal(const SrcRect: TRect; DestCanvas: TImagingCanvas;
  155. DestX, DestY: Integer; SrcFactor, DestFactor: TBlendingFactor; PixelWriteProc: TPixelWriteProc);
  156. procedure StretchDrawInternal(const SrcRect: TRect; DestCanvas: TImagingCanvas;
  157. const DestRect: TRect; SrcFactor, DestFactor: TBlendingFactor;
  158. Filter: TResizeFilter; PixelWriteProc: TPixelWriteProc);
  159. public
  160. constructor CreateForData(ImageDataPointer: PImageData);
  161. constructor CreateForImage(Image: TBaseImage);
  162. destructor Destroy; override;
  163. { Call this method when you change size or format of image this canvas
  164. operates on (like calling ResizeImage, ConvertImage, or changing Format
  165. property of TBaseImage descendants).}
  166. procedure UpdateCanvasState; virtual;
  167. { Resets clipping rectangle to Rect(0, 0, ImageWidth, ImageHeight).}
  168. procedure ResetClipRect;
  169. { Clears entire canvas with current fill color (ignores clipping rectangle
  170. and always uses fmSolid fill mode).}
  171. procedure Clear;
  172. { Draws horizontal line with current pen settings.}
  173. procedure HorzLine(X1, X2, Y: LongInt); virtual;
  174. { Draws vertical line with current pen settings.}
  175. procedure VertLine(X, Y1, Y2: LongInt); virtual;
  176. { Draws line from [X1, Y1] to [X2, Y2] with current pen settings.}
  177. procedure Line(X1, Y1, X2, Y2: LongInt); virtual;
  178. { Draws a rectangle using current pen settings.}
  179. procedure FrameRect(const Rect: TRect);
  180. { Fills given rectangle with current fill settings.}
  181. procedure FillRect(const Rect: TRect); virtual;
  182. { Fills given rectangle with current fill settings and pixel blending.}
  183. procedure FillRectBlend(const Rect: TRect; SrcFactor, DestFactor: TBlendingFactor);
  184. { Draws rectangle which is outlined by using the current pen settings and
  185. filled by using the current fill settings.}
  186. procedure Rectangle(const Rect: TRect);
  187. { Draws ellipse which is outlined by using the current pen settings and
  188. filled by using the current fill settings. Rect specifies bounding rectangle
  189. of ellipse to be drawn.}
  190. procedure Ellipse(const Rect: TRect);
  191. { Fills area of canvas with current fill color starting at point [X, Y] and
  192. coloring its neighbors. Default flood fill mode changes color of all
  193. neighbors with the same color as pixel [X, Y]. With BoundaryFillMode
  194. set to True neighbors are recolored regardless of their old color,
  195. but area which will be recolored has boundary (specified by current pen color).}
  196. procedure FloodFill(X, Y: Integer; BoundaryFillMode: Boolean = False);
  197. { Draws contents of this canvas onto another canvas with pixel blending.
  198. Blending factors are chosen using TBlendingFactor parameters.
  199. Resulting destination pixel color is:
  200. SrcColor * SrcFactor + DstColor * DstFactor}
  201. procedure DrawBlend(const SrcRect: TRect; DestCanvas: TImagingCanvas;
  202. DestX, DestY: Integer; SrcFactor, DestFactor: TBlendingFactor);
  203. { Draws contents of this canvas onto another one with typical alpha
  204. blending (Src 'over' Dest, factors are bfSrcAlpha and bfOneMinusSrcAlpha.)}
  205. procedure DrawAlpha(const SrcRect: TRect; DestCanvas: TImagingCanvas; DestX, DestY: Integer); virtual;
  206. { Draws contents of this canvas onto another one using additive blending
  207. (source and dest factors are bfOne).}
  208. procedure DrawAdd(const SrcRect: TRect; DestCanvas: TImagingCanvas; DestX, DestY: Integer);
  209. { Draws stretched and filtered contents of this canvas onto another canvas
  210. with pixel blending. Blending factors are chosen using TBlendingFactor parameters.
  211. Resulting destination pixel color is:
  212. SrcColor * SrcFactor + DstColor * DstFactor}
  213. procedure StretchDrawBlend(const SrcRect: TRect; DestCanvas: TImagingCanvas;
  214. const DestRect: TRect; SrcFactor, DestFactor: TBlendingFactor;
  215. Filter: TResizeFilter = rfBilinear);
  216. { Draws contents of this canvas onto another one with typical alpha
  217. blending (Src 'over' Dest, factors are bfSrcAlpha and bfOneMinusSrcAlpha.)}
  218. procedure StretchDrawAlpha(const SrcRect: TRect; DestCanvas: TImagingCanvas;
  219. const DestRect: TRect; Filter: TResizeFilter = rfBilinear); virtual;
  220. { Draws contents of this canvas onto another one using additive blending
  221. (source and dest factors are bfOne).}
  222. procedure StretchDrawAdd(const SrcRect: TRect; DestCanvas: TImagingCanvas;
  223. const DestRect: TRect; Filter: TResizeFilter = rfBilinear);
  224. { Convolves canvas' image with given 3x3 filter kernel. You can use
  225. predefined filter kernels or define your own.}
  226. procedure ApplyConvolution3x3(const Filter: TConvolutionFilter3x3);
  227. { Convolves canvas' image with given 5x5 filter kernel. You can use
  228. predefined filter kernels or define your own.}
  229. procedure ApplyConvolution5x5(const Filter: TConvolutionFilter5x5);
  230. { Computes 2D convolution of canvas' image and given filter kernel.
  231. Kernel is in row format and KernelSize must be odd number >= 3. Divisor
  232. is normalizing value based on Kernel (usually sum of all kernel's cells).
  233. The Bias number shifts each color value by a fixed amount (color values
  234. are usually in range [0, 1] during processing). If ClampChannels
  235. is True all output color values are clamped to [0, 1]. You can use
  236. predefined filter kernels or define your own.}
  237. procedure ApplyConvolution(Kernel: PLongInt; KernelSize, Divisor: LongInt;
  238. Bias: Single = 0.0; ClampChannels: Boolean = True); virtual;
  239. { Applies custom non-linear filter. Filter size is diameter of pixel
  240. neighborhood. Typical values are 3, 5, or 7. }
  241. procedure ApplyNonLinearFilter(FilterSize: Integer; SelectFunc: TSelectPixelFunction);
  242. { Applies median non-linear filter with user defined pixel neighborhood.
  243. Selects median pixel from the neighborhood as new pixel
  244. (current implementation is quite slow).}
  245. procedure ApplyMedianFilter(FilterSize: Integer);
  246. { Applies min non-linear filter with user defined pixel neighborhood.
  247. Selects min pixel from the neighborhood as new pixel.}
  248. procedure ApplyMinFilter(FilterSize: Integer);
  249. { Applies max non-linear filter with user defined pixel neighborhood.
  250. Selects max pixel from the neighborhood as new pixel.}
  251. procedure ApplyMaxFilter(FilterSize: Integer);
  252. { Transforms pixels one by one by given function. Pixel neighbors are
  253. not taken into account. Param 1-3 are optional parameters
  254. for transform function.}
  255. procedure PointTransform(Transform: TPointTransformFunction;
  256. Param1, Param2, Param3: Single);
  257. { Modifies image contrast and brightness. Parameters should be
  258. in range <-100; 100>.}
  259. procedure ModifyContrastBrightness(Contrast, Brightness: Single);
  260. { Gamma correction of individual color channels. Range is (0, +inf),
  261. 1.0 means no change.}
  262. procedure GammaCorection(Red, Green, Blue: Single);
  263. { Inverts colors of all image pixels, makes negative image. Ignores alpha channel.}
  264. procedure InvertColors; virtual;
  265. { Simple single level thresholding with threshold level (in range [0, 1])
  266. for each color channel.}
  267. procedure Threshold(Red, Green, Blue: Single);
  268. { Adjusts the color levels of the image by scaling the
  269. colors falling between specified white and black points to full [0, 1] range.
  270. The black point specifies the darkest color in the image, white point
  271. specifies the lightest color, and mid point is gamma aplied to image.
  272. Black and white point must be in range [0, 1].}
  273. procedure AdjustColorLevels(BlackPoint, WhitePoint: Single; MidPoint: Single = 1.0);
  274. { Premultiplies color channel values by alpha. Needed for some platforms/APIs
  275. to display images with alpha properly.}
  276. procedure PremultiplyAlpha;
  277. { Reverses PremultiplyAlpha operation.}
  278. procedure UnPremultiplyAlpha;
  279. { Calculates image histogram for each channel and also gray values. Each
  280. channel has 256 values available. Channel values of data formats with higher
  281. precision are scaled and rounded. Example: Red[126] specifies number of pixels
  282. in image with red channel = 126.}
  283. procedure GetHistogram(out Red, Green, Blue, Alpha, Gray: THistogramArray);
  284. { Fills image channel with given value leaving other channels intact.
  285. Use ChannelAlpha, ChannelRed, etc. constants from ImagingTypes as
  286. channel identifier.}
  287. procedure FillChannel(ChannelId: Integer; NewChannelValue: Byte); overload;
  288. { Fills image channel with given value leaving other channels intact.
  289. Use ChannelAlpha, ChannelRed, etc. constants from ImagingTypes as
  290. channel identifier.}
  291. procedure FillChannelFP(ChannelId: Integer; NewChannelValue: Single); overload;
  292. { Color used when drawing lines, frames, and outlines of objects.}
  293. property PenColor32: TColor32 read FPenColor32 write SetPenColor32;
  294. { Color used when drawing lines, frames, and outlines of objects.}
  295. property PenColorFP: TColorFPRec read FPenColorFP write SetPenColorFP;
  296. { Pen mode used when drawing lines, object outlines, and similar on canvas.}
  297. property PenMode: TPenMode read FPenMode write FPenMode;
  298. { Width with which objects like lines, frames, etc. (everything which uses
  299. PenColor) are drawn.}
  300. property PenWidth: LongInt read FPenWidth write SetPenWidth;
  301. { Color used for filling when drawing various objects.}
  302. property FillColor32: TColor32 read FFillColor32 write SetFillColor32;
  303. { Color used for filling when drawing various objects.}
  304. property FillColorFP: TColorFPRec read FFillColorFP write SetFillColorFP;
  305. { Fill mode used when drawing filled objects on canvas.}
  306. property FillMode: TFillMode read FFillMode write FFillMode;
  307. { Specifies the current color of the pixels of canvas. Native pixel is
  308. read from canvas and then translated to 32bit ARGB. Reverse operation
  309. is made when setting pixel color.}
  310. property Pixels32[X, Y: LongInt]: TColor32 read GetPixel32 write SetPixel32;
  311. { Specifies the current color of the pixels of canvas. Native pixel is
  312. read from canvas and then translated to FP ARGB. Reverse operation
  313. is made when setting pixel color.}
  314. property PixelsFP[X, Y: LongInt]: TColorFPRec read GetPixelFP write SetPixelFP;
  315. { Clipping rectangle of this canvas. No pixels outside this rectangle are
  316. altered by canvas methods if Clipping property is True. Clip rect gets
  317. reseted when UpdateCanvasState is called.}
  318. property ClipRect: TRect read FClipRect write SetClipRect;
  319. { Extended format information.}
  320. property FormatInfo: TImageFormatInfo read FFormatInfo;
  321. { Indicates that this canvas is in valid state. If False canvas oprations
  322. may crash.}
  323. property Valid: Boolean read GetValid;
  324. { Returns all formats supported by this canvas class.}
  325. class function GetSupportedFormats: TImageFormats; virtual;
  326. end;
  327. TImagingCanvasClass = class of TImagingCanvas;
  328. TScanlineArray = array[0..MaxInt div SizeOf(Pointer) - 1] of PColor32RecArray;
  329. PScanlineArray = ^TScanlineArray;
  330. { Fast canvas class for ifA8R8G8B8 format images.}
  331. TFastARGB32Canvas = class(TImagingCanvas)
  332. protected
  333. FScanlines: PScanlineArray;
  334. procedure AlphaBlendPixels(SrcPix, DestPix: PColor32Rec); {$IFDEF USE_INLINE}inline;{$ENDIF}
  335. function GetPixel32(X, Y: LongInt): TColor32; override;
  336. procedure SetPixel32(X, Y: LongInt; const Value: TColor32); override;
  337. public
  338. destructor Destroy; override;
  339. procedure UpdateCanvasState; override;
  340. procedure DrawAlpha(const SrcRect: TRect; DestCanvas: TImagingCanvas; DestX, DestY: Integer); override;
  341. procedure StretchDrawAlpha(const SrcRect: TRect; DestCanvas: TImagingCanvas;
  342. const DestRect: TRect; Filter: TResizeFilter = rfBilinear); override;
  343. procedure InvertColors; override;
  344. property Scanlines: PScanlineArray read FScanlines;
  345. class function GetSupportedFormats: TImageFormats; override;
  346. end;
  347. const
  348. { Kernel for 3x3 average smoothing filter.}
  349. FilterAverage3x3: TConvolutionFilter3x3 = (
  350. Kernel: ((1, 1, 1),
  351. (1, 1, 1),
  352. (1, 1, 1));
  353. Divisor: 9;
  354. Bias: 0);
  355. { Kernel for 5x5 average smoothing filter.}
  356. FilterAverage5x5: TConvolutionFilter5x5 = (
  357. Kernel: ((1, 1, 1, 1, 1),
  358. (1, 1, 1, 1, 1),
  359. (1, 1, 1, 1, 1),
  360. (1, 1, 1, 1, 1),
  361. (1, 1, 1, 1, 1));
  362. Divisor: 25;
  363. Bias: 0);
  364. { Kernel for 3x3 Gaussian smoothing filter.}
  365. FilterGaussian3x3: TConvolutionFilter3x3 = (
  366. Kernel: ((1, 2, 1),
  367. (2, 4, 2),
  368. (1, 2, 1));
  369. Divisor: 16;
  370. Bias: 0);
  371. { Kernel for 5x5 Gaussian smoothing filter.}
  372. FilterGaussian5x5: TConvolutionFilter5x5 = (
  373. Kernel: ((1, 4, 6, 4, 1),
  374. (4, 16, 24, 16, 4),
  375. (6, 24, 36, 24, 6),
  376. (4, 16, 24, 16, 4),
  377. (1, 4, 6, 4, 1));
  378. Divisor: 256;
  379. Bias: 0);
  380. { Kernel for 3x3 Sobel horizontal edge detection filter (1st derivative approximation).}
  381. FilterSobelHorz3x3: TConvolutionFilter3x3 = (
  382. Kernel: (( 1, 2, 1),
  383. ( 0, 0, 0),
  384. (-1, -2, -1));
  385. Divisor: 1;
  386. Bias: 0);
  387. { Kernel for 3x3 Sobel vertical edge detection filter (1st derivative approximation).}
  388. FilterSobelVert3x3: TConvolutionFilter3x3 = (
  389. Kernel: ((-1, 0, 1),
  390. (-2, 0, 2),
  391. (-1, 0, 1));
  392. Divisor: 1;
  393. Bias: 0);
  394. { Kernel for 3x3 Prewitt horizontal edge detection filter.}
  395. FilterPrewittHorz3x3: TConvolutionFilter3x3 = (
  396. Kernel: (( 1, 1, 1),
  397. ( 0, 0, 0),
  398. (-1, -1, -1));
  399. Divisor: 1;
  400. Bias: 0);
  401. { Kernel for 3x3 Prewitt vertical edge detection filter.}
  402. FilterPrewittVert3x3: TConvolutionFilter3x3 = (
  403. Kernel: ((-1, 0, 1),
  404. (-1, 0, 1),
  405. (-1, 0, 1));
  406. Divisor: 1;
  407. Bias: 0);
  408. { Kernel for 3x3 Kirsh horizontal edge detection filter.}
  409. FilterKirshHorz3x3: TConvolutionFilter3x3 = (
  410. Kernel: (( 5, 5, 5),
  411. (-3, 0, -3),
  412. (-3, -3, -3));
  413. Divisor: 1;
  414. Bias: 0);
  415. { Kernel for 3x3 Kirsh vertical edge detection filter.}
  416. FilterKirshVert3x3: TConvolutionFilter3x3 = (
  417. Kernel: ((5, -3, -3),
  418. (5, 0, -3),
  419. (5, -3, -3));
  420. Divisor: 1;
  421. Bias: 0);
  422. { Kernel for 3x3 Laplace omni-directional edge detection filter
  423. (2nd derivative approximation).}
  424. FilterLaplace3x3: TConvolutionFilter3x3 = (
  425. Kernel: ((-1, -1, -1),
  426. (-1, 8, -1),
  427. (-1, -1, -1));
  428. Divisor: 1;
  429. Bias: 0);
  430. { Kernel for 5x5 Laplace omni-directional edge detection filter
  431. (2nd derivative approximation).}
  432. FilterLaplace5x5: TConvolutionFilter5x5 = (
  433. Kernel: ((-1, -1, -1, -1, -1),
  434. (-1, -1, -1, -1, -1),
  435. (-1, -1, 24, -1, -1),
  436. (-1, -1, -1, -1, -1),
  437. (-1, -1, -1, -1, -1));
  438. Divisor: 1;
  439. Bias: 0);
  440. { Kernel for 3x3 spharpening filter (Laplacian + original color).}
  441. FilterSharpen3x3: TConvolutionFilter3x3 = (
  442. Kernel: ((-1, -1, -1),
  443. (-1, 9, -1),
  444. (-1, -1, -1));
  445. Divisor: 1;
  446. Bias: 0);
  447. { Kernel for 5x5 spharpening filter (Laplacian + original color).}
  448. FilterSharpen5x5: TConvolutionFilter5x5 = (
  449. Kernel: ((-1, -1, -1, -1, -1),
  450. (-1, -1, -1, -1, -1),
  451. (-1, -1, 25, -1, -1),
  452. (-1, -1, -1, -1, -1),
  453. (-1, -1, -1, -1, -1));
  454. Divisor: 1;
  455. Bias: 0);
  456. { Kernel for 5x5 glow filter.}
  457. FilterGlow5x5: TConvolutionFilter5x5 = (
  458. Kernel: (( 1, 2, 2, 2, 1),
  459. ( 2, 0, 0, 0, 2),
  460. ( 2, 0, -20, 0, 2),
  461. ( 2, 0, 0, 0, 2),
  462. ( 1, 2, 2, 2, 1));
  463. Divisor: 8;
  464. Bias: 0);
  465. { Kernel for 3x3 edge enhancement filter.}
  466. FilterEdgeEnhance3x3: TConvolutionFilter3x3 = (
  467. Kernel: ((-1, -2, -1),
  468. (-2, 16, -2),
  469. (-1, -2, -1));
  470. Divisor: 4;
  471. Bias: 0);
  472. { Kernel for 3x3 contour enhancement filter.}
  473. FilterTraceControur3x3: TConvolutionFilter3x3 = (
  474. Kernel: ((-6, -6, -2),
  475. (-1, 32, -1),
  476. (-6, -2, -6));
  477. Divisor: 4;
  478. Bias: 240/255);
  479. { Kernel for filter that negates all images pixels.}
  480. FilterNegative3x3: TConvolutionFilter3x3 = (
  481. Kernel: ((0, 0, 0),
  482. (0, -1, 0),
  483. (0, 0, 0));
  484. Divisor: 1;
  485. Bias: 1);
  486. { Kernel for 3x3 horz/vert embossing filter.}
  487. FilterEmboss3x3: TConvolutionFilter3x3 = (
  488. Kernel: ((2, 0, 0),
  489. (0, -1, 0),
  490. (0, 0, -1));
  491. Divisor: 1;
  492. Bias: 0.5);
  493. { You can register your own canvas class. List of registered canvases is used
  494. by FindBestCanvasForImage functions to find best canvas for given image.
  495. If two different canvases which support the same image data format are
  496. registered then the one that was registered later is returned (so you can
  497. override builtin Imaging canvases).}
  498. procedure RegisterCanvas(CanvasClass: TImagingCanvasClass);
  499. { Returns best canvas for given TImageFormat.}
  500. function FindBestCanvasForImage(ImageFormat: TImageFormat): TImagingCanvasClass; overload;
  501. { Returns best canvas for given TImageData.}
  502. function FindBestCanvasForImage(const ImageData: TImageData): TImagingCanvasClass; overload;
  503. { Returns best canvas for given TBaseImage.}
  504. function FindBestCanvasForImage(Image: TBaseImage): TImagingCanvasClass; overload;
  505. implementation
  506. resourcestring
  507. SConstructorInvalidPointer = 'Invalid pointer (%p) to TImageData passed to TImagingCanvas constructor.';
  508. SConstructorInvalidImage = 'Invalid image data passed to TImagingCanvas constructor (%s).';
  509. SConstructorUnsupportedFormat = 'Image passed to TImagingCanvas constructor is in unsupported format (%s)';
  510. var
  511. // list with all registered TImagingCanvas classes
  512. CanvasClasses: TList = nil;
  513. procedure RegisterCanvas(CanvasClass: TImagingCanvasClass);
  514. begin
  515. Assert(CanvasClass <> nil);
  516. if CanvasClasses = nil then
  517. CanvasClasses := TList.Create;
  518. if CanvasClasses.IndexOf(CanvasClass) < 0 then
  519. CanvasClasses.Add(CanvasClass);
  520. end;
  521. function FindBestCanvasForImage(ImageFormat: TImageFormat): TImagingCanvasClass; overload;
  522. var
  523. I: LongInt;
  524. begin
  525. for I := CanvasClasses.Count - 1 downto 0 do
  526. begin
  527. if ImageFormat in TImagingCanvasClass(CanvasClasses[I]).GetSupportedFormats then
  528. begin
  529. Result := TImagingCanvasClass(CanvasClasses[I]);
  530. Exit;
  531. end;
  532. end;
  533. Result := TImagingCanvas;
  534. end;
  535. function FindBestCanvasForImage(const ImageData: TImageData): TImagingCanvasClass;
  536. begin
  537. Result := FindBestCanvasForImage(ImageData.Format);
  538. end;
  539. function FindBestCanvasForImage(Image: TBaseImage): TImagingCanvasClass;
  540. begin
  541. Result := FindBestCanvasForImage(Image.Format);
  542. end;
  543. { Canvas helper functions }
  544. procedure PixelBlendProc(const SrcPix: TColorFPRec; DestPtr: PByte;
  545. DestInfo: PImageFormatInfo; SrcFactor, DestFactor: TBlendingFactor);
  546. var
  547. DestPix, FSrc, FDst: TColorFPRec;
  548. begin
  549. // Get set pixel color
  550. DestPix := DestInfo.GetPixelFP(DestPtr, DestInfo, nil);
  551. // Determine current blending factors
  552. case SrcFactor of
  553. bfZero: FSrc := ColorFP(0, 0, 0, 0);
  554. bfOne: FSrc := ColorFP(1, 1, 1, 1);
  555. bfSrcAlpha: FSrc := ColorFP(SrcPix.A, SrcPix.A, SrcPix.A, SrcPix.A);
  556. bfOneMinusSrcAlpha: FSrc := ColorFP(1 - SrcPix.A, 1 - SrcPix.A, 1 - SrcPix.A, 1 - SrcPix.A);
  557. bfDstAlpha: FSrc := ColorFP(DestPix.A, DestPix.A, DestPix.A, DestPix.A);
  558. bfOneMinusDstAlpha: FSrc := ColorFP(1 - DestPix.A, 1 - DestPix.A, 1 - DestPix.A, 1 - DestPix.A);
  559. bfDstColor: FSrc := ColorFP(DestPix.A, DestPix.R, DestPix.G, DestPix.B);
  560. bfOneMinusDstColor: FSrc := ColorFP(1 - DestPix.A, 1 - DestPix.R, 1 - DestPix.G, 1 - DestPix.B);
  561. end;
  562. case DestFactor of
  563. bfZero: FDst := ColorFP(0, 0, 0, 0);
  564. bfOne: FDst := ColorFP(1, 1, 1, 1);
  565. bfSrcAlpha: FDst := ColorFP(SrcPix.A, SrcPix.A, SrcPix.A, SrcPix.A);
  566. bfOneMinusSrcAlpha: FDst := ColorFP(1 - SrcPix.A, 1 - SrcPix.A, 1 - SrcPix.A, 1 - SrcPix.A);
  567. bfDstAlpha: FDst := ColorFP(DestPix.A, DestPix.A, DestPix.A, DestPix.A);
  568. bfOneMinusDstAlpha: FDst := ColorFP(1 - DestPix.A, 1 - DestPix.A, 1 - DestPix.A, 1 - DestPix.A);
  569. bfSrcColor: FDst := ColorFP(SrcPix.A, SrcPix.R, SrcPix.G, SrcPix.B);
  570. bfOneMinusSrcColor: FDst := ColorFP(1 - SrcPix.A, 1 - SrcPix.R, 1 - SrcPix.G, 1 - SrcPix.B);
  571. end;
  572. // Compute blending formula
  573. DestPix.R := SrcPix.R * FSrc.R + DestPix.R * FDst.R;
  574. DestPix.G := SrcPix.G * FSrc.G + DestPix.G * FDst.G;
  575. DestPix.B := SrcPix.B * FSrc.B + DestPix.B * FDst.B;
  576. DestPix.A := SrcPix.A * FSrc.A + DestPix.A * FDst.A;
  577. // Write blended pixel
  578. DestInfo.SetPixelFP(DestPtr, DestInfo, nil, DestPix);
  579. end;
  580. procedure PixelAlphaProc(const SrcPix: TColorFPRec; DestPtr: PByte;
  581. DestInfo: PImageFormatInfo; SrcFactor, DestFactor: TBlendingFactor);
  582. var
  583. DestPix: TColorFPRec;
  584. SrcAlpha, DestAlpha: Single;
  585. begin
  586. DestPix := DestInfo.GetPixelFP(DestPtr, DestInfo, nil);
  587. // Blend the two pixels (Src 'over' Dest alpha composition operation)
  588. DestPix.A := SrcPix.A + DestPix.A - SrcPix.A * DestPix.A;
  589. if DestPix.A = 0 then
  590. SrcAlpha := 0
  591. else
  592. SrcAlpha := SrcPix.A / DestPix.A;
  593. DestAlpha := 1.0 - SrcAlpha;
  594. DestPix.R := SrcPix.R * SrcAlpha + DestPix.R * DestAlpha;
  595. DestPix.G := SrcPix.G * SrcAlpha + DestPix.G * DestAlpha;
  596. DestPix.B := SrcPix.B * SrcAlpha + DestPix.B * DestAlpha;
  597. // Write blended pixel
  598. DestInfo.SetPixelFP(DestPtr, DestInfo, nil, DestPix);
  599. end;
  600. procedure PixelAddProc(const SrcPix: TColorFPRec; DestPtr: PByte;
  601. DestInfo: PImageFormatInfo; SrcFactor, DestFactor: TBlendingFactor);
  602. var
  603. DestPix: TColorFPRec;
  604. begin
  605. // Just add Src and Dest
  606. DestPix := DestInfo.GetPixelFP(DestPtr, DestInfo, nil);
  607. DestPix.R := SrcPix.R + DestPix.R;
  608. DestPix.G := SrcPix.G + DestPix.G;
  609. DestPix.B := SrcPix.B + DestPix.B;
  610. DestPix.A := SrcPix.A + DestPix.A;
  611. DestInfo.SetPixelFP(DestPtr, DestInfo, nil, DestPix);
  612. end;
  613. function CompareColors(const C1, C2: TColorFPRec): Single; {$IFDEF USE_INLINE}inline;{$ENDIF}
  614. begin
  615. Result := (C1.R * GrayConv.R + C1.G * GrayConv.G + C1.B * GrayConv.B) -
  616. (C2.R * GrayConv.R + C2.G * GrayConv.G + C2.B * GrayConv.B);
  617. end;
  618. function MedianSelect(var Pixels: TDynFPPixelArray): TColorFPRec;
  619. procedure QuickSort(L, R: Integer);
  620. var
  621. I, J: Integer;
  622. P, Temp: TColorFPRec;
  623. begin
  624. repeat
  625. I := L;
  626. J := R;
  627. P := Pixels[(L + R) shr 1];
  628. repeat
  629. while CompareColors(Pixels[I], P) < 0 do Inc(I);
  630. while CompareColors(Pixels[J], P) > 0 do Dec(J);
  631. if I <= J then
  632. begin
  633. Temp := Pixels[I];
  634. Pixels[I] := Pixels[J];
  635. Pixels[J] := Temp;
  636. Inc(I);
  637. Dec(J);
  638. end;
  639. until I > J;
  640. if L < J then
  641. QuickSort(L, J);
  642. L := I;
  643. until I >= R;
  644. end;
  645. begin
  646. // First sort pixels
  647. QuickSort(0, High(Pixels));
  648. // Select middle pixel
  649. Result := Pixels[Length(Pixels) div 2];
  650. end;
  651. function MinSelect(var Pixels: TDynFPPixelArray): TColorFPRec;
  652. var
  653. I: Integer;
  654. begin
  655. Result := Pixels[0];
  656. for I := 1 to High(Pixels) do
  657. begin
  658. if CompareColors(Pixels[I], Result) < 0 then
  659. Result := Pixels[I];
  660. end;
  661. end;
  662. function MaxSelect(var Pixels: TDynFPPixelArray): TColorFPRec;
  663. var
  664. I: Integer;
  665. begin
  666. Result := Pixels[0];
  667. for I := 1 to High(Pixels) do
  668. begin
  669. if CompareColors(Pixels[I], Result) > 0 then
  670. Result := Pixels[I];
  671. end;
  672. end;
  673. function TransformContrastBrightness(const Pixel: TColorFPRec; C, B, P3: Single): TColorFPRec;
  674. begin
  675. Result.A := Pixel.A;
  676. Result.R := Pixel.R * C + B;
  677. Result.G := Pixel.G * C + B;
  678. Result.B := Pixel.B * C + B;
  679. end;
  680. function TransformGamma(const Pixel: TColorFPRec; R, G, B: Single): TColorFPRec;
  681. begin
  682. Result.A := Pixel.A;
  683. Result.R := Power(Pixel.R, 1.0 / R);
  684. Result.G := Power(Pixel.G, 1.0 / G);
  685. Result.B := Power(Pixel.B, 1.0 / B);
  686. end;
  687. function TransformInvert(const Pixel: TColorFPRec; P1, P2, P3: Single): TColorFPRec;
  688. begin
  689. Result.A := Pixel.A;
  690. Result.R := 1.0 - Pixel.R;
  691. Result.G := 1.0 - Pixel.G;
  692. Result.B := 1.0 - Pixel.B;
  693. end;
  694. function TransformThreshold(const Pixel: TColorFPRec; R, G, B: Single): TColorFPRec;
  695. begin
  696. Result.A := Pixel.A;
  697. Result.R := IffFloat(Pixel.R >= R, 1.0, 0.0);
  698. Result.G := IffFloat(Pixel.G >= G, 1.0, 0.0);
  699. Result.B := IffFloat(Pixel.B >= B, 1.0, 0.0);
  700. end;
  701. function TransformLevels(const Pixel: TColorFPRec; BlackPoint, WhitePoint, Exp: Single): TColorFPRec;
  702. begin
  703. Result.A := Pixel.A;
  704. if Pixel.R > BlackPoint then
  705. Result.R := Power((Pixel.R - BlackPoint) / (WhitePoint - BlackPoint), Exp)
  706. else
  707. Result.R := 0.0;
  708. if Pixel.G > BlackPoint then
  709. Result.G := Power((Pixel.G - BlackPoint) / (WhitePoint - BlackPoint), Exp)
  710. else
  711. Result.G := 0.0;
  712. if Pixel.B > BlackPoint then
  713. Result.B := Power((Pixel.B - BlackPoint) / (WhitePoint - BlackPoint), Exp)
  714. else
  715. Result.B := 0.0;
  716. end;
  717. function TransformPremultiplyAlpha(const Pixel: TColorFPRec; P1, P2, P3: Single): TColorFPRec;
  718. begin
  719. Result.A := Pixel.A;
  720. Result.R := Result.R * Pixel.A;
  721. Result.G := Result.G * Pixel.A;
  722. Result.B := Result.B * Pixel.A;
  723. end;
  724. function TransformUnPremultiplyAlpha(const Pixel: TColorFPRec; P1, P2, P3: Single): TColorFPRec;
  725. begin
  726. Result.A := Pixel.A;
  727. if Pixel.A <> 0.0 then
  728. begin
  729. Result.R := Result.R / Pixel.A;
  730. Result.G := Result.G / Pixel.A;
  731. Result.B := Result.B / Pixel.A;
  732. end
  733. else
  734. begin
  735. Result.R := 0;
  736. Result.G := 0;
  737. Result.B := 0;
  738. end;
  739. end;
  740. { TImagingCanvas class implementation }
  741. constructor TImagingCanvas.CreateForData(ImageDataPointer: PImageData);
  742. begin
  743. if ImageDataPointer = nil then
  744. raise EImagingCanvasError.CreateFmt(SConstructorInvalidPointer, [ImageDataPointer]);
  745. if not TestImage(ImageDataPointer^) then
  746. raise EImagingCanvasError.CreateFmt(SConstructorInvalidImage, [Imaging.ImageToStr(ImageDataPointer^)]);
  747. if not (ImageDataPointer.Format in GetSupportedFormats) then
  748. raise EImagingCanvasError.CreateFmt(SConstructorUnsupportedFormat, [Imaging.ImageToStr(ImageDataPointer^)]);
  749. FPData := ImageDataPointer;
  750. FPenWidth := 1;
  751. SetPenColor32(pcWhite);
  752. SetFillColor32(pcBlack);
  753. FFillMode := fmSolid;
  754. UpdateCanvasState;
  755. end;
  756. constructor TImagingCanvas.CreateForImage(Image: TBaseImage);
  757. begin
  758. CreateForData(Image.ImageDataPointer);
  759. end;
  760. destructor TImagingCanvas.Destroy;
  761. begin
  762. inherited Destroy;
  763. end;
  764. function TImagingCanvas.GetPixel32(X, Y: LongInt): TColor32;
  765. begin
  766. Result := Imaging.GetPixel32(FPData^, X, Y).Color;
  767. end;
  768. function TImagingCanvas.GetPixelFP(X, Y: LongInt): TColorFPRec;
  769. begin
  770. Result := Imaging.GetPixelFP(FPData^, X, Y);
  771. end;
  772. function TImagingCanvas.GetValid: Boolean;
  773. begin
  774. Result := (FPData <> nil) and (FDataSizeOnUpdate = FPData.Size);
  775. end;
  776. procedure TImagingCanvas.SetPixel32(X, Y: LongInt; const Value: TColor32);
  777. begin
  778. if (X >= FClipRect.Left) and (Y >= FClipRect.Top) and
  779. (X < FClipRect.Right) and (Y < FClipRect.Bottom) then
  780. begin
  781. Imaging.SetPixel32(FPData^, X, Y, TColor32Rec(Value));
  782. end;
  783. end;
  784. procedure TImagingCanvas.SetPixelFP(X, Y: LongInt; const Value: TColorFPRec);
  785. begin
  786. if (X >= FClipRect.Left) and (Y >= FClipRect.Top) and
  787. (X < FClipRect.Right) and (Y < FClipRect.Bottom) then
  788. begin
  789. Imaging.SetPixelFP(FPData^, X, Y, TColorFPRec(Value));
  790. end;
  791. end;
  792. procedure TImagingCanvas.SetPenColor32(const Value: TColor32);
  793. begin
  794. FPenColor32 := Value;
  795. TranslatePixel(@FPenColor32, @FPenColorFP, ifA8R8G8B8, ifA32R32G32B32F, nil, nil);
  796. end;
  797. procedure TImagingCanvas.SetPenColorFP(const Value: TColorFPRec);
  798. begin
  799. FPenColorFP := Value;
  800. TranslatePixel(@FPenColorFP, @FPenColor32, ifA32R32G32B32F, ifA8R8G8B8, nil, nil);
  801. end;
  802. procedure TImagingCanvas.SetPenWidth(const Value: LongInt);
  803. begin
  804. FPenWidth := ClampInt(Value, 0, MaxPenWidth);
  805. end;
  806. procedure TImagingCanvas.SetFillColor32(const Value: TColor32);
  807. begin
  808. FFillColor32 := Value;
  809. TranslatePixel(@FFillColor32, @FFillColorFP, ifA8R8G8B8, ifA32R32G32B32F, nil, nil);
  810. end;
  811. procedure TImagingCanvas.SetFillColorFP(const Value: TColorFPRec);
  812. begin
  813. FFillColorFP := Value;
  814. TranslatePixel(@FFillColorFP, @FFillColor32, ifA32R32G32B32F, ifA8R8G8B8, nil, nil);
  815. end;
  816. procedure TImagingCanvas.SetClipRect(const Value: TRect);
  817. begin
  818. FClipRect := Value;
  819. SwapMin(FClipRect.Left, FClipRect.Right);
  820. SwapMin(FClipRect.Top, FClipRect.Bottom);
  821. IntersectRect(FClipRect, FClipRect, Rect(0, 0, FPData.Width, FPData.Height));
  822. end;
  823. procedure TImagingCanvas.CheckBeforeBlending(SrcFactor,
  824. DestFactor: TBlendingFactor; DestCanvas: TImagingCanvas);
  825. begin
  826. if SrcFactor in [bfSrcColor, bfOneMinusSrcColor] then
  827. raise EImagingCanvasBlendingError.Create('Invalid source blending factor. Check the documentation for TBlendingFactor.');
  828. if DestFactor in [bfDstColor, bfOneMinusDstColor] then
  829. raise EImagingCanvasBlendingError.Create('Invalid destination blending factor. Check the documentation for TBlendingFactor.');
  830. if DestCanvas.FormatInfo.IsIndexed then
  831. raise EImagingCanvasBlendingError.Create('Blending destination canvas cannot be in indexed mode.');
  832. end;
  833. function TImagingCanvas.GetPixelPointer(X, Y: LongInt): Pointer;
  834. begin
  835. Result := @PByteArray(FPData.Bits)[(Y * FPData.Width + X) * FFormatInfo.BytesPerPixel]
  836. end;
  837. procedure TImagingCanvas.TranslateFPToNative(const Color: TColorFPRec);
  838. begin
  839. TranslateFPToNative(Color, @FNativeColor);
  840. end;
  841. procedure TImagingCanvas.TranslateFPToNative(const Color: TColorFPRec;
  842. Native: Pointer);
  843. begin
  844. ImagingFormats.TranslatePixel(@Color, Native, ifA32R32G32B32F,
  845. FPData.Format, nil, FPData.Palette);
  846. end;
  847. procedure TImagingCanvas.UpdateCanvasState;
  848. begin
  849. FDataSizeOnUpdate := FPData.Size;
  850. ResetClipRect;
  851. Imaging.GetImageFormatInfo(FPData.Format, FFormatInfo)
  852. end;
  853. procedure TImagingCanvas.ResetClipRect;
  854. begin
  855. FClipRect := Rect(0, 0, FPData.Width, FPData.Height)
  856. end;
  857. procedure TImagingCanvas.Clear;
  858. begin
  859. TranslateFPToNative(FFillColorFP);
  860. Imaging.FillRect(FPData^, 0, 0, FPData.Width, FPData.Height, @FNativeColor);
  861. end;
  862. function TImagingCanvas.ClipAxisParallelLine(var A1, A2, B: LongInt;
  863. AStart, AStop, BStart, BStop: LongInt): Boolean;
  864. begin
  865. if (B >= BStart) and (B < BStop) then
  866. begin
  867. SwapMin(A1, A2);
  868. if A1 < AStart then A1 := AStart;
  869. if A2 >= AStop then A2 := AStop - 1;
  870. Result := True;
  871. end
  872. else
  873. Result := False;
  874. end;
  875. procedure TImagingCanvas.HorzLineInternal(X1, X2, Y: LongInt; Color: Pointer;
  876. Bpp: LongInt);
  877. var
  878. I, WidthBytes: LongInt;
  879. PixelPtr: PByte;
  880. begin
  881. if (Y >= FClipRect.Top) and (Y < FClipRect.Bottom) then
  882. begin
  883. SwapMin(X1, X2);
  884. X1 := Max(X1, FClipRect.Left);
  885. X2 := Min(X2, FClipRect.Right);
  886. PixelPtr := GetPixelPointer(X1, Y);
  887. WidthBytes := (X2 - X1) * Bpp;
  888. case Bpp of
  889. 1: FillMemoryByte(PixelPtr, WidthBytes, PByte(Color)^);
  890. 2: FillMemoryWord(PixelPtr, WidthBytes, PWord(Color)^);
  891. 4: FillMemoryLongWord(PixelPtr, WidthBytes, PLongWord(Color)^);
  892. else
  893. for I := X1 to X2 do
  894. begin
  895. ImagingFormats.CopyPixel(Color, PixelPtr, Bpp);
  896. Inc(PixelPtr, Bpp);
  897. end;
  898. end;
  899. end;
  900. end;
  901. procedure TImagingCanvas.CopyPixelInternal(X, Y: LongInt; Pixel: Pointer;
  902. Bpp: LongInt);
  903. begin
  904. if (X >= FClipRect.Left) and (Y >= FClipRect.Top) and
  905. (X < FClipRect.Right) and (Y < FClipRect.Bottom) then
  906. begin
  907. ImagingFormats.CopyPixel(Pixel, GetPixelPointer(X, Y), Bpp);
  908. end;
  909. end;
  910. procedure TImagingCanvas.HorzLine(X1, X2, Y: LongInt);
  911. var
  912. DstRect: TRect;
  913. begin
  914. if FPenMode = pmClear then Exit;
  915. SwapMin(X1, X2);
  916. if IntersectRect(DstRect, Rect(X1, Y - FPenWidth div 2, X2,
  917. Y + FPenWidth div 2 + FPenWidth mod 2), FClipRect) then
  918. begin
  919. TranslateFPToNative(FPenColorFP);
  920. Imaging.FillRect(FPData^, DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left,
  921. DstRect.Bottom - DstRect.Top, @FNativeColor);
  922. end;
  923. end;
  924. procedure TImagingCanvas.VertLine(X, Y1, Y2: LongInt);
  925. var
  926. DstRect: TRect;
  927. begin
  928. if FPenMode = pmClear then Exit;
  929. SwapMin(Y1, Y2);
  930. if IntersectRect(DstRect, Rect(X - FPenWidth div 2, Y1,
  931. X + FPenWidth div 2 + FPenWidth mod 2, Y2), FClipRect) then
  932. begin
  933. TranslateFPToNative(FPenColorFP);
  934. Imaging.FillRect(FPData^, DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left,
  935. DstRect.Bottom - DstRect.Top, @FNativeColor);
  936. end;
  937. end;
  938. procedure TImagingCanvas.Line(X1, Y1, X2, Y2: LongInt);
  939. var
  940. Steep: Boolean;
  941. Error, YStep, DeltaX, DeltaY, X, Y, I, Bpp, W1, W2, Code1, Code2: LongInt;
  942. begin
  943. if FPenMode = pmClear then Exit;
  944. // If line is vertical or horizontal just call appropriate method
  945. if X2 = X1 then
  946. begin
  947. VertLine(X1, Y1, Y2);
  948. Exit;
  949. end;
  950. if Y2 = Y1 then
  951. begin
  952. HorzLine(X1, X2, Y1);
  953. Exit;
  954. end;
  955. // Determine if line is steep (angle with X-axis > 45 degrees)
  956. Steep := Abs(Y2 - Y1) > Abs(X2 - X1);
  957. // If we need to draw thick line we just draw more 1 pixel lines around
  958. // the one we already drawn. Setting FLineRecursion assures that we
  959. // won't be doing recursions till the end of the world.
  960. if (FPenWidth > 1) and not FLineRecursion then
  961. begin
  962. FLineRecursion := True;
  963. W1 := FPenWidth div 2;
  964. W2 := W1;
  965. if FPenWidth mod 2 = 0 then
  966. Dec(W1);
  967. if Steep then
  968. begin
  969. // Add lines left/right
  970. for I := 1 to W1 do
  971. Line(X1, Y1 - I, X2, Y2 - I);
  972. for I := 1 to W2 do
  973. Line(X1, Y1 + I, X2, Y2 + I);
  974. end
  975. else
  976. begin
  977. // Add lines above/under
  978. for I := 1 to W1 do
  979. Line(X1 - I, Y1, X2 - I, Y2);
  980. for I := 1 to W2 do
  981. Line(X1 + I, Y1, X2 + I, Y2);
  982. end;
  983. FLineRecursion := False;
  984. end;
  985. with FClipRect do
  986. begin
  987. // Use part of Cohen-Sutherland line clipping to determine if any part of line
  988. // is in ClipRect
  989. Code1 := Ord(X1 < Left) + Ord(X1 > Right) shl 1 + Ord(Y1 < Top) shl 2 + Ord(Y1 > Bottom) shl 3;
  990. Code2 := Ord(X2 < Left) + Ord(X2 > Right) shl 1 + Ord(Y2 < Top) shl 2 + Ord(Y2 > Bottom) shl 3;
  991. end;
  992. if (Code1 and Code2) = 0 then
  993. begin
  994. TranslateFPToNative(FPenColorFP);
  995. Bpp := FFormatInfo.BytesPerPixel;
  996. // If line is steep swap X and Y coordinates so later we just have one loop
  997. // of two (where only one is used according to steepness).
  998. if Steep then
  999. begin
  1000. SwapValues(X1, Y1);
  1001. SwapValues(X2, Y2);
  1002. end;
  1003. if X1 > X2 then
  1004. begin
  1005. SwapValues(X1, X2);
  1006. SwapValues(Y1, Y2);
  1007. end;
  1008. DeltaX := X2 - X1;
  1009. DeltaY := Abs(Y2 - Y1);
  1010. YStep := Iff(Y2 > Y1, 1, -1);
  1011. Error := 0;
  1012. Y := Y1;
  1013. // Draw line using Bresenham algorithm. No real line clipping here,
  1014. // just don't draw pixels outsize clip rect.
  1015. for X := X1 to X2 do
  1016. begin
  1017. if Steep then
  1018. CopyPixelInternal(Y, X, @FNativeColor, Bpp)
  1019. else
  1020. CopyPixelInternal(X, Y, @FNativeColor, Bpp);
  1021. Error := Error + DeltaY;
  1022. if Error * 2 >= DeltaX then
  1023. begin
  1024. Inc(Y, YStep);
  1025. Dec(Error, DeltaX);
  1026. end;
  1027. end;
  1028. end;
  1029. end;
  1030. procedure TImagingCanvas.FrameRect(const Rect: TRect);
  1031. var
  1032. HalfPen, PenMod: LongInt;
  1033. begin
  1034. if FPenMode = pmClear then Exit;
  1035. HalfPen := FPenWidth div 2;
  1036. PenMod := FPenWidth mod 2;
  1037. HorzLine(Rect.Left - HalfPen, Rect.Right + HalfPen + PenMod - 1, Rect.Top);
  1038. HorzLine(Rect.Left - HalfPen, Rect.Right + HalfPen + PenMod - 1, Rect.Bottom - 1);
  1039. VertLine(Rect.Left, Rect.Top, Rect.Bottom);
  1040. VertLine(Rect.Right - 1, Rect.Top, Rect.Bottom);
  1041. end;
  1042. procedure TImagingCanvas.FillRect(const Rect: TRect);
  1043. var
  1044. DstRect: TRect;
  1045. begin
  1046. if (FFillMode <> fmClear) and IntersectRect(DstRect, Rect, FClipRect) then
  1047. begin
  1048. TranslateFPToNative(FFillColorFP);
  1049. Imaging.FillRect(FPData^, DstRect.Left, DstRect.Top, DstRect.Right - DstRect.Left,
  1050. DstRect.Bottom - DstRect.Top, @FNativeColor);
  1051. end;
  1052. end;
  1053. procedure TImagingCanvas.FillRectBlend(const Rect: TRect; SrcFactor,
  1054. DestFactor: TBlendingFactor);
  1055. var
  1056. DstRect: TRect;
  1057. X, Y: Integer;
  1058. Line: PByte;
  1059. begin
  1060. if (FFillMode <> fmClear) and IntersectRect(DstRect, Rect, FClipRect) then
  1061. begin
  1062. CheckBeforeBlending(SrcFactor, DestFactor, Self);
  1063. for Y := DstRect.Top to DstRect.Bottom - 1 do
  1064. begin
  1065. Line := @PByteArray(FPData.Bits)[(Y * FPData.Width + DstRect.Left) * FFormatInfo.BytesPerPixel];
  1066. for X := DstRect.Left to DstRect.Right - 1 do
  1067. begin
  1068. PixelBlendProc(FFillColorFP, Line, @FFormatInfo, SrcFactor, DestFactor);
  1069. Inc(Line, FFormatInfo.BytesPerPixel);
  1070. end;
  1071. end;
  1072. end;
  1073. end;
  1074. procedure TImagingCanvas.Rectangle(const Rect: TRect);
  1075. begin
  1076. FillRect(Rect);
  1077. FrameRect(Rect);
  1078. end;
  1079. procedure TImagingCanvas.Ellipse(const Rect: TRect);
  1080. var
  1081. RadX, RadY, DeltaX, DeltaY, R, RX, RY: LongInt;
  1082. X1, X2, Y1, Y2, Bpp, OldY: LongInt;
  1083. Fill, Pen: TColorFPRec;
  1084. begin
  1085. // TODO: Use PenWidth
  1086. X1 := Rect.Left;
  1087. X2 := Rect.Right;
  1088. Y1 := Rect.Top;
  1089. Y2 := Rect.Bottom;
  1090. TranslateFPToNative(FPenColorFP, @Pen);
  1091. TranslateFPToNative(FFillColorFP, @Fill);
  1092. Bpp := FFormatInfo.BytesPerPixel;
  1093. SwapMin(X1, X2);
  1094. SwapMin(Y1, Y2);
  1095. RadX := (X2 - X1) div 2;
  1096. RadY := (Y2 - Y1) div 2;
  1097. Y1 := Y1 + RadY;
  1098. Y2 := Y1;
  1099. OldY := Y1;
  1100. DeltaX := (RadX * RadX);
  1101. DeltaY := (RadY * RadY);
  1102. R := RadX * RadY * RadY;
  1103. RX := R;
  1104. RY := 0;
  1105. if (FFillMode <> fmClear) then
  1106. HorzLineInternal(X1, X2, Y1, @Fill, Bpp);
  1107. CopyPixelInternal(X1, Y1, @Pen, Bpp);
  1108. CopyPixelInternal(X2, Y1, @Pen, Bpp);
  1109. while RadX > 0 do
  1110. begin
  1111. if R > 0 then
  1112. begin
  1113. Inc(Y1);
  1114. Dec(Y2);
  1115. Inc(RY, DeltaX);
  1116. Dec(R, RY);
  1117. end;
  1118. if R <= 0 then
  1119. begin
  1120. Dec(RadX);
  1121. Inc(X1);
  1122. Dec(X2);
  1123. Dec(RX, DeltaY);
  1124. Inc(R, RX);
  1125. end;
  1126. if (OldY <> Y1) and (FFillMode <> fmClear) then
  1127. begin
  1128. HorzLineInternal(X1, X2, Y1, @Fill, Bpp);
  1129. HorzLineInternal(X1, X2, Y2, @Fill, Bpp);
  1130. end;
  1131. OldY := Y1;
  1132. CopyPixelInternal(X1, Y1, @Pen, Bpp);
  1133. CopyPixelInternal(X2, Y1, @Pen, Bpp);
  1134. CopyPixelInternal(X1, Y2, @Pen, Bpp);
  1135. CopyPixelInternal(X2, Y2, @Pen, Bpp);
  1136. end;
  1137. end;
  1138. procedure TImagingCanvas.FloodFill(X, Y: Integer; BoundaryFillMode: Boolean);
  1139. var
  1140. Stack: array of TPoint;
  1141. StackPos, Y1: Integer;
  1142. OldColor: TColor32;
  1143. SpanLeft, SpanRight: Boolean;
  1144. procedure Push(AX, AY: Integer);
  1145. begin
  1146. if StackPos < High(Stack) then
  1147. begin
  1148. Inc(StackPos);
  1149. Stack[StackPos].X := AX;
  1150. Stack[StackPos].Y := AY;
  1151. end
  1152. else
  1153. begin
  1154. SetLength(Stack, Length(Stack) + FPData.Width);
  1155. Push(AX, AY);
  1156. end;
  1157. end;
  1158. function Pop(out AX, AY: Integer): Boolean;
  1159. begin
  1160. if StackPos > 0 then
  1161. begin
  1162. AX := Stack[StackPos].X;
  1163. AY := Stack[StackPos].Y;
  1164. Dec(StackPos);
  1165. Result := True;
  1166. end
  1167. else
  1168. Result := False;
  1169. end;
  1170. function Compare(AX, AY: Integer): Boolean;
  1171. var
  1172. Color: TColor32;
  1173. begin
  1174. Color := GetPixel32(AX, AY);
  1175. if BoundaryFillMode then
  1176. Result := (Color <> FFillColor32) and (Color <> FPenColor32)
  1177. else
  1178. Result := Color = OldColor;
  1179. end;
  1180. begin
  1181. // Scanline Floodfill Algorithm With Stack
  1182. // http://student.kuleuven.be/~m0216922/CG/floodfill.html
  1183. if not PtInRect(FClipRect, Point(X, Y)) then Exit;
  1184. SetLength(Stack, FPData.Width * 4);
  1185. StackPos := 0;
  1186. OldColor := GetPixel32(X, Y);
  1187. Push(X, Y);
  1188. while Pop(X, Y) do
  1189. begin
  1190. Y1 := Y;
  1191. while (Y1 >= FClipRect.Top) and Compare(X, Y1) do
  1192. Dec(Y1);
  1193. Inc(Y1);
  1194. SpanLeft := False;
  1195. SpanRight := False;
  1196. while (Y1 < FClipRect.Bottom) and Compare(X, Y1) do
  1197. begin
  1198. SetPixel32(X, Y1, FFillColor32);
  1199. if not SpanLeft and (X > FClipRect.Left) and Compare(X - 1, Y1) then
  1200. begin
  1201. Push(X - 1, Y1);
  1202. SpanLeft := True;
  1203. end
  1204. else if SpanLeft and (X > FClipRect.Left) and not Compare(X - 1, Y1) then
  1205. SpanLeft := False
  1206. else if not SpanRight and (X < FClipRect.Right - 1) and Compare(X + 1, Y1)then
  1207. begin
  1208. Push(X + 1, Y1);
  1209. SpanRight := True;
  1210. end
  1211. else if SpanRight and (X < FClipRect.Right - 1) and not Compare(X + 1, Y1) then
  1212. SpanRight := False;
  1213. Inc(Y1);
  1214. end;
  1215. end;
  1216. end;
  1217. procedure TImagingCanvas.DrawInternal(const SrcRect: TRect;
  1218. DestCanvas: TImagingCanvas; DestX, DestY: Integer; SrcFactor,
  1219. DestFactor: TBlendingFactor; PixelWriteProc: TPixelWriteProc);
  1220. var
  1221. X, Y, SrcX, SrcY, Width, Height, SrcBpp, DestBpp: Integer;
  1222. PSrc: TColorFPRec;
  1223. SrcPointer, DestPointer: PByte;
  1224. begin
  1225. CheckBeforeBlending(SrcFactor, DestFactor, DestCanvas);
  1226. SrcX := SrcRect.Left;
  1227. SrcY := SrcRect.Top;
  1228. Width := SrcRect.Right - SrcRect.Left;
  1229. Height := SrcRect.Bottom - SrcRect.Top;
  1230. SrcBpp := FFormatInfo.BytesPerPixel;
  1231. DestBpp := DestCanvas.FFormatInfo.BytesPerPixel;
  1232. // Clip src and dst rects
  1233. ClipCopyBounds(SrcX, SrcY, Width, Height, DestX, DestY,
  1234. FPData.Width, FPData.Height, DestCanvas.ClipRect);
  1235. for Y := 0 to Height - 1 do
  1236. begin
  1237. // Get src and dst scanlines
  1238. SrcPointer := @PByteArray(FPData.Bits)[((SrcY + Y) * FPData.Width + SrcX) * SrcBpp];
  1239. DestPointer := @PByteArray(DestCanvas.FPData.Bits)[((DestY + Y) * DestCanvas.FPData.Width + DestX) * DestBpp];
  1240. for X := 0 to Width - 1 do
  1241. begin
  1242. PSrc := FFormatInfo.GetPixelFP(SrcPointer, @FFormatInfo, FPData.Palette);
  1243. // Call pixel writer procedure - combine source and dest pixels
  1244. PixelWriteProc(PSrc, DestPointer, @DestCanvas.FFormatInfo, SrcFactor, DestFactor);
  1245. // Increment pixel pointers
  1246. Inc(SrcPointer, SrcBpp);
  1247. Inc(DestPointer, DestBpp);
  1248. end;
  1249. end;
  1250. end;
  1251. procedure TImagingCanvas.DrawBlend(const SrcRect: TRect; DestCanvas: TImagingCanvas;
  1252. DestX, DestY: Integer; SrcFactor, DestFactor: TBlendingFactor);
  1253. begin
  1254. DrawInternal(SrcRect, DestCanvas, DestX, DestY, SrcFactor, DestFactor, PixelBlendProc);
  1255. end;
  1256. procedure TImagingCanvas.DrawAlpha(const SrcRect: TRect; DestCanvas: TImagingCanvas;
  1257. DestX, DestY: Integer);
  1258. begin
  1259. DrawInternal(SrcRect, DestCanvas, DestX, DestY, bfIgnore, bfIgnore, PixelAlphaProc);
  1260. end;
  1261. procedure TImagingCanvas.DrawAdd(const SrcRect: TRect;
  1262. DestCanvas: TImagingCanvas; DestX, DestY: Integer);
  1263. begin
  1264. DrawInternal(SrcRect, DestCanvas, DestX, DestY, bfIgnore, bfIgnore, PixelAddProc);
  1265. end;
  1266. procedure TImagingCanvas.StretchDrawInternal(const SrcRect: TRect;
  1267. DestCanvas: TImagingCanvas; const DestRect: TRect;
  1268. SrcFactor, DestFactor: TBlendingFactor; Filter: TResizeFilter;
  1269. PixelWriteProc: TPixelWriteProc);
  1270. const
  1271. FilterMapping: array[TResizeFilter] of TSamplingFilter =
  1272. (sfNearest, sfLinear, DefaultCubicFilter, sfLanczos);
  1273. var
  1274. X, Y, I, J, SrcX, SrcY, SrcWidth, SrcHeight: Integer;
  1275. DestX, DestY, DestWidth, DestHeight, SrcBpp, DestBpp: Integer;
  1276. SrcPix: TColorFPRec;
  1277. MapX, MapY: TMappingTable;
  1278. XMinimum, XMaximum: Integer;
  1279. LineBuffer: array of TColorFPRec;
  1280. ClusterX, ClusterY: TCluster;
  1281. Weight, AccumA, AccumR, AccumG, AccumB: Single;
  1282. DestLine: PByte;
  1283. FilterFunction: TFilterFunction;
  1284. Radius: Single;
  1285. begin
  1286. CheckBeforeBlending(SrcFactor, DestFactor, DestCanvas);
  1287. SrcX := SrcRect.Left;
  1288. SrcY := SrcRect.Top;
  1289. SrcWidth := SrcRect.Right - SrcRect.Left;
  1290. SrcHeight := SrcRect.Bottom - SrcRect.Top;
  1291. DestX := DestRect.Left;
  1292. DestY := DestRect.Top;
  1293. DestWidth := DestRect.Right - DestRect.Left;
  1294. DestHeight := DestRect.Bottom - DestRect.Top;
  1295. SrcBpp := FFormatInfo.BytesPerPixel;
  1296. DestBpp := DestCanvas.FFormatInfo.BytesPerPixel;
  1297. // Get actual resampling filter and radius
  1298. FilterFunction := SamplingFilterFunctions[FilterMapping[Filter]];
  1299. Radius := SamplingFilterRadii[FilterMapping[Filter]];
  1300. // Clip src and dst rects
  1301. ClipStretchBounds(SrcX, SrcY, SrcWidth, SrcHeight, DestX, DestY, DestWidth, DestHeight,
  1302. FPData.Width, FPData.Height, DestCanvas.ClipRect);
  1303. // Generate mapping tables
  1304. MapX := BuildMappingTable(DestX, DestX + DestWidth, SrcX, SrcX + SrcWidth,
  1305. FPData.Width, FilterFunction, Radius, False);
  1306. MapY := BuildMappingTable(DestY, DestY + DestHeight, SrcY, SrcY + SrcHeight,
  1307. FPData.Height, FilterFunction, Radius, False);
  1308. FindExtremes(MapX, XMinimum, XMaximum);
  1309. SetLength(LineBuffer, XMaximum - XMinimum + 1);
  1310. for J := 0 to DestHeight - 1 do
  1311. begin
  1312. ClusterY := MapY[J];
  1313. for X := XMinimum to XMaximum do
  1314. begin
  1315. AccumA := 0.0;
  1316. AccumR := 0.0;
  1317. AccumG := 0.0;
  1318. AccumB := 0.0;
  1319. for Y := 0 to Length(ClusterY) - 1 do
  1320. begin
  1321. Weight := ClusterY[Y].Weight;
  1322. SrcPix := FFormatInfo.GetPixelFP(@PByteArray(FPData.Bits)[(ClusterY[Y].Pos * FPData.Width + X) * SrcBpp],
  1323. @FFormatInfo, FPData.Palette);
  1324. AccumB := AccumB + SrcPix.B * Weight;
  1325. AccumG := AccumG + SrcPix.G * Weight;
  1326. AccumR := AccumR + SrcPix.R * Weight;
  1327. AccumA := AccumA + SrcPix.A * Weight;
  1328. end;
  1329. with LineBuffer[X - XMinimum] do
  1330. begin
  1331. A := AccumA;
  1332. R := AccumR;
  1333. G := AccumG;
  1334. B := AccumB;
  1335. end;
  1336. end;
  1337. DestLine := @PByteArray(DestCanvas.FPData.Bits)[((J + DestY) * DestCanvas.FPData.Width + DestX) * DestBpp];
  1338. for I := 0 to DestWidth - 1 do
  1339. begin
  1340. ClusterX := MapX[I];
  1341. AccumA := 0.0;
  1342. AccumR := 0.0;
  1343. AccumG := 0.0;
  1344. AccumB := 0.0;
  1345. for X := 0 to Length(ClusterX) - 1 do
  1346. begin
  1347. Weight := ClusterX[X].Weight;
  1348. with LineBuffer[ClusterX[X].Pos - XMinimum] do
  1349. begin
  1350. AccumB := AccumB + B * Weight;
  1351. AccumG := AccumG + G * Weight;
  1352. AccumR := AccumR + R * Weight;
  1353. AccumA := AccumA + A * Weight;
  1354. end;
  1355. end;
  1356. SrcPix.A := AccumA;
  1357. SrcPix.R := AccumR;
  1358. SrcPix.G := AccumG;
  1359. SrcPix.B := AccumB;
  1360. // Write resulting blended pixel
  1361. PixelWriteProc(SrcPix, DestLine, @DestCanvas.FFormatInfo, SrcFactor, DestFactor);
  1362. Inc(DestLine, DestBpp);
  1363. end;
  1364. end;
  1365. end;
  1366. procedure TImagingCanvas.StretchDrawBlend(const SrcRect: TRect;
  1367. DestCanvas: TImagingCanvas; const DestRect: TRect;
  1368. SrcFactor, DestFactor: TBlendingFactor; Filter: TResizeFilter);
  1369. begin
  1370. StretchDrawInternal(SrcRect, DestCanvas, DestRect, SrcFactor, DestFactor, Filter, PixelBlendProc);
  1371. end;
  1372. procedure TImagingCanvas.StretchDrawAlpha(const SrcRect: TRect;
  1373. DestCanvas: TImagingCanvas; const DestRect: TRect; Filter: TResizeFilter);
  1374. begin
  1375. StretchDrawInternal(SrcRect, DestCanvas, DestRect, bfIgnore, bfIgnore, Filter, PixelAlphaProc);
  1376. end;
  1377. procedure TImagingCanvas.StretchDrawAdd(const SrcRect: TRect;
  1378. DestCanvas: TImagingCanvas; const DestRect: TRect; Filter: TResizeFilter);
  1379. begin
  1380. StretchDrawInternal(SrcRect, DestCanvas, DestRect, bfIgnore, bfIgnore, Filter, PixelAddProc);
  1381. end;
  1382. procedure TImagingCanvas.ApplyConvolution(Kernel: PLongInt; KernelSize,
  1383. Divisor: LongInt; Bias: Single; ClampChannels: Boolean);
  1384. var
  1385. X, Y, I, J, PosY, PosX, SizeDiv2, KernelValue, WidthBytes, Bpp: LongInt;
  1386. R, G, B, DivFloat: Single;
  1387. Pixel: TColorFPRec;
  1388. TempImage: TImageData;
  1389. DstPointer, SrcPointer: PByte;
  1390. begin
  1391. SizeDiv2 := KernelSize div 2;
  1392. DivFloat := IffFloat(Divisor > 1, 1.0 / Divisor, 1.0);
  1393. Bpp := FFormatInfo.BytesPerPixel;
  1394. WidthBytes := FPData.Width * Bpp;
  1395. InitImage(TempImage);
  1396. CloneImage(FPData^, TempImage);
  1397. try
  1398. // For every pixel in clip rect
  1399. for Y := FClipRect.Top to FClipRect.Bottom - 1 do
  1400. begin
  1401. DstPointer := @PByteArray(FPData.Bits)[Y * WidthBytes + FClipRect.Left * Bpp];
  1402. for X := FClipRect.Left to FClipRect.Right - 1 do
  1403. begin
  1404. // Reset accumulators
  1405. R := 0.0;
  1406. G := 0.0;
  1407. B := 0.0;
  1408. for J := 0 to KernelSize - 1 do
  1409. begin
  1410. PosY := ClampInt(Y + J - SizeDiv2, FClipRect.Top, FClipRect.Bottom - 1);
  1411. for I := 0 to KernelSize - 1 do
  1412. begin
  1413. PosX := ClampInt(X + I - SizeDiv2, FClipRect.Left, FClipRect.Right - 1);
  1414. SrcPointer := @PByteArray(TempImage.Bits)[PosY * WidthBytes + PosX * Bpp];
  1415. // Get pixels from neighbourhood of current pixel and add their
  1416. // colors to accumulators weighted by filter kernel values
  1417. Pixel := FFormatInfo.GetPixelFP(SrcPointer, @FFormatInfo, TempImage.Palette);
  1418. KernelValue := PLongIntArray(Kernel)[J * KernelSize + I];
  1419. R := R + Pixel.R * KernelValue;
  1420. G := G + Pixel.G * KernelValue;
  1421. B := B + Pixel.B * KernelValue;
  1422. end;
  1423. end;
  1424. Pixel := FFormatInfo.GetPixelFP(DstPointer, @FFormatInfo, FPData.Palette);
  1425. Pixel.R := R * DivFloat + Bias;
  1426. Pixel.G := G * DivFloat + Bias;
  1427. Pixel.B := B * DivFloat + Bias;
  1428. if ClampChannels then
  1429. ClampFloatPixel(Pixel);
  1430. // Set resulting pixel color
  1431. FFormatInfo.SetPixelFP(DstPointer, @FFormatInfo, FPData.Palette, Pixel);
  1432. Inc(DstPointer, Bpp);
  1433. end;
  1434. end;
  1435. finally
  1436. FreeImage(TempImage);
  1437. end;
  1438. end;
  1439. procedure TImagingCanvas.ApplyConvolution3x3(const Filter: TConvolutionFilter3x3);
  1440. begin
  1441. ApplyConvolution(@Filter.Kernel, 3, Filter.Divisor, Filter.Bias, True);
  1442. end;
  1443. procedure TImagingCanvas.ApplyConvolution5x5(const Filter: TConvolutionFilter5x5);
  1444. begin
  1445. ApplyConvolution(@Filter.Kernel, 5, Filter.Divisor, Filter.Bias, True);
  1446. end;
  1447. procedure TImagingCanvas.ApplyNonLinearFilter(FilterSize: Integer; SelectFunc: TSelectPixelFunction);
  1448. var
  1449. X, Y, I, J, PosY, PosX, SizeDiv2, WidthBytes, Bpp: LongInt;
  1450. Pixel: TColorFPRec;
  1451. TempImage: TImageData;
  1452. DstPointer, SrcPointer: PByte;
  1453. NeighPixels: TDynFPPixelArray;
  1454. begin
  1455. SizeDiv2 := FilterSize div 2;
  1456. Bpp := FFormatInfo.BytesPerPixel;
  1457. WidthBytes := FPData.Width * Bpp;
  1458. SetLength(NeighPixels, FilterSize * FilterSize);
  1459. InitImage(TempImage);
  1460. CloneImage(FPData^, TempImage);
  1461. try
  1462. // For every pixel in clip rect
  1463. for Y := FClipRect.Top to FClipRect.Bottom - 1 do
  1464. begin
  1465. DstPointer := @PByteArray(FPData.Bits)[Y * WidthBytes + FClipRect.Left * Bpp];
  1466. for X := FClipRect.Left to FClipRect.Right - 1 do
  1467. begin
  1468. for J := 0 to FilterSize - 1 do
  1469. begin
  1470. PosY := ClampInt(Y + J - SizeDiv2, FClipRect.Top, FClipRect.Bottom - 1);
  1471. for I := 0 to FilterSize - 1 do
  1472. begin
  1473. PosX := ClampInt(X + I - SizeDiv2, FClipRect.Left, FClipRect.Right - 1);
  1474. SrcPointer := @PByteArray(TempImage.Bits)[PosY * WidthBytes + PosX * Bpp];
  1475. // Get pixels from neighbourhood of current pixel and store them
  1476. Pixel := FFormatInfo.GetPixelFP(SrcPointer, @FFormatInfo, TempImage.Palette);
  1477. NeighPixels[J * FilterSize + I] := Pixel;
  1478. end;
  1479. end;
  1480. // Choose pixel using custom function
  1481. Pixel := SelectFunc(NeighPixels);
  1482. // Set resulting pixel color
  1483. FFormatInfo.SetPixelFP(DstPointer, @FFormatInfo, FPData.Palette, Pixel);
  1484. Inc(DstPointer, Bpp);
  1485. end;
  1486. end;
  1487. finally
  1488. FreeImage(TempImage);
  1489. end;
  1490. end;
  1491. procedure TImagingCanvas.ApplyMedianFilter(FilterSize: Integer);
  1492. begin
  1493. ApplyNonLinearFilter(FilterSize, MedianSelect);
  1494. end;
  1495. procedure TImagingCanvas.ApplyMinFilter(FilterSize: Integer);
  1496. begin
  1497. ApplyNonLinearFilter(FilterSize, MinSelect);
  1498. end;
  1499. procedure TImagingCanvas.ApplyMaxFilter(FilterSize: Integer);
  1500. begin
  1501. ApplyNonLinearFilter(FilterSize, MaxSelect);
  1502. end;
  1503. procedure TImagingCanvas.PointTransform(Transform: TPointTransformFunction;
  1504. Param1, Param2, Param3: Single);
  1505. var
  1506. X, Y, Bpp, WidthBytes: Integer;
  1507. PixPointer: PByte;
  1508. Pixel: TColorFPRec;
  1509. begin
  1510. Bpp := FFormatInfo.BytesPerPixel;
  1511. WidthBytes := FPData.Width * Bpp;
  1512. // For every pixel in clip rect
  1513. for Y := FClipRect.Top to FClipRect.Bottom - 1 do
  1514. begin
  1515. PixPointer := @PByteArray(FPData.Bits)[Y * WidthBytes + FClipRect.Left * Bpp];
  1516. for X := FClipRect.Left to FClipRect.Right - 1 do
  1517. begin
  1518. Pixel := FFormatInfo.GetPixelFP(PixPointer, @FFormatInfo, FPData.Palette);
  1519. FFormatInfo.SetPixelFP(PixPointer, @FFormatInfo, FPData.Palette,
  1520. Transform(Pixel, Param1, Param2, Param3));
  1521. Inc(PixPointer, Bpp);
  1522. end;
  1523. end;
  1524. end;
  1525. procedure TImagingCanvas.ModifyContrastBrightness(Contrast, Brightness: Single);
  1526. begin
  1527. PointTransform(TransformContrastBrightness, 1.0 + Contrast / 100,
  1528. Brightness / 100, 0);
  1529. end;
  1530. procedure TImagingCanvas.GammaCorection(Red, Green, Blue: Single);
  1531. begin
  1532. PointTransform(TransformGamma, Red, Green, Blue);
  1533. end;
  1534. procedure TImagingCanvas.InvertColors;
  1535. begin
  1536. PointTransform(TransformInvert, 0, 0, 0);
  1537. end;
  1538. procedure TImagingCanvas.Threshold(Red, Green, Blue: Single);
  1539. begin
  1540. PointTransform(TransformThreshold, Red, Green, Blue);
  1541. end;
  1542. procedure TImagingCanvas.AdjustColorLevels(BlackPoint, WhitePoint, MidPoint: Single);
  1543. begin
  1544. PointTransform(TransformLevels, BlackPoint, WhitePoint, 1.0 / MidPoint);
  1545. end;
  1546. procedure TImagingCanvas.PremultiplyAlpha;
  1547. begin
  1548. PointTransform(TransformPremultiplyAlpha, 0, 0, 0);
  1549. end;
  1550. procedure TImagingCanvas.UnPremultiplyAlpha;
  1551. begin
  1552. PointTransform(TransformUnPremultiplyAlpha, 0, 0, 0);
  1553. end;
  1554. procedure TImagingCanvas.GetHistogram(out Red, Green, Blue, Alpha,
  1555. Gray: THistogramArray);
  1556. var
  1557. X, Y, Bpp: Integer;
  1558. PixPointer: PByte;
  1559. Color32: TColor32Rec;
  1560. begin
  1561. FillChar(Red, SizeOf(Red), 0);
  1562. FillChar(Green, SizeOf(Green), 0);
  1563. FillChar(Blue, SizeOf(Blue), 0);
  1564. FillChar(Alpha, SizeOf(Alpha), 0);
  1565. FillChar(Gray, SizeOf(Gray), 0);
  1566. Bpp := FFormatInfo.BytesPerPixel;
  1567. for Y := FClipRect.Top to FClipRect.Bottom - 1 do
  1568. begin
  1569. PixPointer := @PByteArray(FPData.Bits)[Y * FPData.Width * Bpp + FClipRect.Left * Bpp];
  1570. for X := FClipRect.Left to FClipRect.Right - 1 do
  1571. begin
  1572. Color32 := FFormatInfo.GetPixel32(PixPointer, @FFormatInfo, FPData.Palette);
  1573. Inc(Red[Color32.R]);
  1574. Inc(Green[Color32.G]);
  1575. Inc(Blue[Color32.B]);
  1576. Inc(Alpha[Color32.A]);
  1577. Inc(Gray[Round(GrayConv.R * Color32.R + GrayConv.G * Color32.G + GrayConv.B * Color32.B)]);
  1578. Inc(PixPointer, Bpp);
  1579. end;
  1580. end;
  1581. end;
  1582. procedure TImagingCanvas.FillChannel(ChannelId: Integer; NewChannelValue: Byte);
  1583. var
  1584. X, Y, Bpp: Integer;
  1585. PixPointer: PByte;
  1586. Color32: TColor32Rec;
  1587. begin
  1588. Bpp := FFormatInfo.BytesPerPixel;
  1589. for Y := FClipRect.Top to FClipRect.Bottom - 1 do
  1590. begin
  1591. PixPointer := @PByteArray(FPData.Bits)[Y * FPData.Width * Bpp + FClipRect.Left * Bpp];
  1592. for X := FClipRect.Left to FClipRect.Right - 1 do
  1593. begin
  1594. Color32 := FFormatInfo.GetPixel32(PixPointer, @FFormatInfo, FPData.Palette);
  1595. Color32.Channels[ChannelId] := NewChannelValue;
  1596. FFormatInfo.SetPixel32(PixPointer, @FFormatInfo, FPData.Palette, Color32);
  1597. Inc(PixPointer, Bpp);
  1598. end;
  1599. end;
  1600. end;
  1601. procedure TImagingCanvas.FillChannelFP(ChannelId: Integer; NewChannelValue: Single);
  1602. var
  1603. X, Y, Bpp: Integer;
  1604. PixPointer: PByte;
  1605. ColorFP: TColorFPRec;
  1606. begin
  1607. Bpp := FFormatInfo.BytesPerPixel;
  1608. for Y := FClipRect.Top to FClipRect.Bottom - 1 do
  1609. begin
  1610. PixPointer := @PByteArray(FPData.Bits)[Y * FPData.Width * Bpp + FClipRect.Left * Bpp];
  1611. for X := FClipRect.Left to FClipRect.Right - 1 do
  1612. begin
  1613. ColorFP := FFormatInfo.GetPixelFP(PixPointer, @FFormatInfo, FPData.Palette);
  1614. ColorFP.Channels[ChannelId] := NewChannelValue;
  1615. FFormatInfo.SetPixelFP(PixPointer, @FFormatInfo, FPData.Palette, ColorFP);
  1616. Inc(PixPointer, Bpp);
  1617. end;
  1618. end;
  1619. end;
  1620. class function TImagingCanvas.GetSupportedFormats: TImageFormats;
  1621. begin
  1622. Result := [ifIndex8..Pred(ifDXT1)];
  1623. end;
  1624. { TFastARGB32Canvas }
  1625. destructor TFastARGB32Canvas.Destroy;
  1626. begin
  1627. FreeMem(FScanlines);
  1628. inherited Destroy;
  1629. end;
  1630. procedure TFastARGB32Canvas.AlphaBlendPixels(SrcPix, DestPix: PColor32Rec);
  1631. var
  1632. SrcAlpha, DestAlpha, FinalAlpha: Integer;
  1633. begin
  1634. FinalAlpha := SrcPix.A + 1 + (DestPix.A * (256 - SrcPix.A)) shr 8;
  1635. if FinalAlpha = 0 then
  1636. SrcAlpha := 0
  1637. else
  1638. SrcAlpha := (SrcPix.A shl 8) div FinalAlpha;
  1639. DestAlpha := 256 - SrcAlpha;
  1640. DestPix.A := ClampToByte(FinalAlpha);
  1641. DestPix.R := (SrcPix.R * SrcAlpha + DestPix.R * DestAlpha) shr 8;
  1642. DestPix.G := (SrcPix.G * SrcAlpha + DestPix.G * DestAlpha) shr 8;
  1643. DestPix.B := (SrcPix.B * SrcAlpha + DestPix.B * DestAlpha) shr 8;
  1644. end;
  1645. procedure TFastARGB32Canvas.DrawAlpha(const SrcRect: TRect;
  1646. DestCanvas: TImagingCanvas; DestX, DestY: Integer);
  1647. var
  1648. X, Y, SrcX, SrcY, Width, Height: Integer;
  1649. SrcPix, DestPix: PColor32Rec;
  1650. begin
  1651. if DestCanvas.ClassType <> Self.ClassType then
  1652. begin
  1653. inherited;
  1654. Exit;
  1655. end;
  1656. SrcX := SrcRect.Left;
  1657. SrcY := SrcRect.Top;
  1658. Width := SrcRect.Right - SrcRect.Left;
  1659. Height := SrcRect.Bottom - SrcRect.Top;
  1660. ClipCopyBounds(SrcX, SrcY, Width, Height, DestX, DestY,
  1661. FPData.Width, FPData.Height, DestCanvas.ClipRect);
  1662. for Y := 0 to Height - 1 do
  1663. begin
  1664. SrcPix := @FScanlines[SrcY + Y, SrcX];
  1665. DestPix := @TFastARGB32Canvas(DestCanvas).FScanlines[DestY + Y, DestX];
  1666. for X := 0 to Width - 1 do
  1667. begin
  1668. AlphaBlendPixels(SrcPix, DestPix);
  1669. Inc(SrcPix);
  1670. Inc(DestPix);
  1671. end;
  1672. end;
  1673. end;
  1674. function TFastARGB32Canvas.GetPixel32(X, Y: LongInt): TColor32;
  1675. begin
  1676. Result := FScanlines[Y, X].Color;
  1677. end;
  1678. procedure TFastARGB32Canvas.SetPixel32(X, Y: LongInt; const Value: TColor32);
  1679. begin
  1680. if (X >= FClipRect.Left) and (Y >= FClipRect.Top) and
  1681. (X < FClipRect.Right) and (Y < FClipRect.Bottom) then
  1682. begin
  1683. FScanlines[Y, X].Color := Value;
  1684. end;
  1685. end;
  1686. procedure TFastARGB32Canvas.StretchDrawAlpha(const SrcRect: TRect;
  1687. DestCanvas: TImagingCanvas; const DestRect: TRect; Filter: TResizeFilter);
  1688. var
  1689. X, Y, ScaleX, ScaleY, Yp, Xp, Weight1, Weight2, Weight3, Weight4, InvFracY, T1, T2: Integer;
  1690. FracX, FracY: Cardinal;
  1691. SrcX, SrcY, SrcWidth, SrcHeight: Integer;
  1692. DestX, DestY, DestWidth, DestHeight: Integer;
  1693. SrcLine, SrcLine2: PColor32RecArray;
  1694. DestPix: PColor32Rec;
  1695. Accum: TColor32Rec;
  1696. begin
  1697. if (Filter = rfBicubic) or (DestCanvas.ClassType <> Self.ClassType) then
  1698. begin
  1699. inherited;
  1700. Exit;
  1701. end;
  1702. SrcX := SrcRect.Left;
  1703. SrcY := SrcRect.Top;
  1704. SrcWidth := SrcRect.Right - SrcRect.Left;
  1705. SrcHeight := SrcRect.Bottom - SrcRect.Top;
  1706. DestX := DestRect.Left;
  1707. DestY := DestRect.Top;
  1708. DestWidth := DestRect.Right - DestRect.Left;
  1709. DestHeight := DestRect.Bottom - DestRect.Top;
  1710. // Clip src and dst rects
  1711. ClipStretchBounds(SrcX, SrcY, SrcWidth, SrcHeight, DestX, DestY, DestWidth, DestHeight,
  1712. FPData.Width, FPData.Height, DestCanvas.ClipRect);
  1713. ScaleX := (SrcWidth shl 16) div DestWidth;
  1714. ScaleY := (SrcHeight shl 16) div DestHeight;
  1715. // Nearest and linear filtering using fixed point math
  1716. if Filter = rfNearest then
  1717. begin
  1718. Yp := 0;
  1719. for Y := DestY to DestY + DestHeight - 1 do
  1720. begin
  1721. Xp := 0;
  1722. SrcLine := @FScanlines[SrcY + Yp shr 16, SrcX];
  1723. DestPix := @TFastARGB32Canvas(DestCanvas).FScanlines[Y, DestX];
  1724. for X := 0 to DestWidth - 1 do
  1725. begin
  1726. AlphaBlendPixels(@SrcLine[Xp shr 16], DestPix);
  1727. Inc(DestPix);
  1728. Inc(Xp, ScaleX);
  1729. end;
  1730. Inc(Yp, ScaleY);
  1731. end;
  1732. end
  1733. else
  1734. begin
  1735. Yp := (ScaleY shr 1) - $8000;
  1736. for Y := DestY to DestY + DestHeight - 1 do
  1737. begin
  1738. DestPix := @TFastARGB32Canvas(DestCanvas).FScanlines[Y, DestX];
  1739. if Yp < 0 then
  1740. begin
  1741. T1 := 0;
  1742. FracY := 0;
  1743. InvFracY := $10000;
  1744. end
  1745. else
  1746. begin
  1747. T1 := Yp shr 16;
  1748. FracY := Yp and $FFFF;
  1749. InvFracY := (not Yp and $FFFF) + 1;
  1750. end;
  1751. T2 := Iff(T1 < SrcHeight - 1, T1 + 1, T1);
  1752. SrcLine := @Scanlines[T1 + SrcY, SrcX];
  1753. SrcLine2 := @Scanlines[T2 + SrcY, SrcX];
  1754. Xp := (ScaleX shr 1) - $8000;
  1755. for X := 0 to DestWidth - 1 do
  1756. begin
  1757. if Xp < 0 then
  1758. begin
  1759. T1 := 0;
  1760. FracX := 0;
  1761. end
  1762. else
  1763. begin
  1764. T1 := Xp shr 16;
  1765. FracX := Xp and $FFFF;
  1766. end;
  1767. T2 := Iff(T1 < SrcWidth - 1, T1 + 1, T1);
  1768. Weight2:= Integer((Cardinal(InvFracY) * FracX) shr 16); // cast to Card, Int can overflow here
  1769. Weight1:= InvFracY - Weight2;
  1770. Weight4:= Integer((Cardinal(FracY) * FracX) shr 16);
  1771. Weight3:= FracY - Weight4;
  1772. Accum.B := (SrcLine[T1].B * Weight1 + SrcLine[T2].B * Weight2 +
  1773. SrcLine2[T1].B * Weight3 + SrcLine2[T2].B * Weight4 + $8000) shr 16;
  1774. Accum.G := (SrcLine[T1].G * Weight1 + SrcLine[T2].G * Weight2 +
  1775. SrcLine2[T1].G * Weight3 + SrcLine2[T2].G * Weight4 + $8000) shr 16;
  1776. Accum.R := (SrcLine[T1].R * Weight1 + SrcLine[T2].R * Weight2 +
  1777. SrcLine2[T1].R * Weight3 + SrcLine2[T2].R * Weight4 + $8000) shr 16;
  1778. Accum.A := (SrcLine[T1].A * Weight1 + SrcLine[T2].A * Weight2 +
  1779. SrcLine2[T1].A * Weight3 + SrcLine2[T2].A * Weight4 + $8000) shr 16;
  1780. AlphaBlendPixels(@Accum, DestPix);
  1781. Inc(Xp, ScaleX);
  1782. Inc(DestPix);
  1783. end;
  1784. Inc(Yp, ScaleY);
  1785. end;
  1786. end;
  1787. end;
  1788. procedure TFastARGB32Canvas.UpdateCanvasState;
  1789. var
  1790. I: LongInt;
  1791. ScanPos: PLongWord;
  1792. begin
  1793. inherited UpdateCanvasState;
  1794. // Realloc and update scanline array
  1795. ReallocMem(FScanlines, FPData.Height * SizeOf(PColor32RecArray));
  1796. ScanPos := FPData.Bits;
  1797. for I := 0 to FPData.Height - 1 do
  1798. begin
  1799. FScanlines[I] := PColor32RecArray(ScanPos);
  1800. Inc(ScanPos, FPData.Width);
  1801. end;
  1802. end;
  1803. class function TFastARGB32Canvas.GetSupportedFormats: TImageFormats;
  1804. begin
  1805. Result := [ifA8R8G8B8];
  1806. end;
  1807. procedure TFastARGB32Canvas.InvertColors;
  1808. var
  1809. X, Y: Integer;
  1810. PixPtr: PColor32Rec;
  1811. begin
  1812. for Y := FClipRect.Top to FClipRect.Bottom - 1 do
  1813. begin
  1814. PixPtr := @FScanlines[Y, FClipRect.Left];
  1815. for X := FClipRect.Left to FClipRect.Right - 1 do
  1816. begin
  1817. PixPtr.R := not PixPtr.R;
  1818. PixPtr.G := not PixPtr.G;
  1819. PixPtr.B := not PixPtr.B;
  1820. Inc(PixPtr);
  1821. end;
  1822. end;
  1823. end;
  1824. initialization
  1825. RegisterCanvas(TFastARGB32Canvas);
  1826. finalization
  1827. FreeAndNil(CanvasClasses);
  1828. {
  1829. File Notes:
  1830. -- TODOS ----------------------------------------------------
  1831. - more more more ...
  1832. - implement pen width everywhere
  1833. - more objects (arc, polygon)
  1834. -- 0.26.5 Changes/Bug Fixes ---------------------------------
  1835. - Fixed bug that could raise floating point error in DrawAlpha
  1836. and StretchDrawAlpha.
  1837. - Fixed bug in TImagingCanvas.Line that caused not drawing
  1838. of horz or vert lines.
  1839. -- 0.26.3 Changes/Bug Fixes ---------------------------------
  1840. - Added some methods to TFastARGB32Canvas (InvertColors, DrawAlpha/StretchDrawAlpha)
  1841. - Fixed DrawAlpha/StretchDrawAlpha destination alpha calculation.
  1842. - Added PremultiplyAlpha and UnPremultiplyAlpha methods.
  1843. -- 0.26.1 Changes/Bug Fixes ---------------------------------
  1844. - Added FillChannel methods.
  1845. - Added FloodFill method.
  1846. - Added GetHistogram method.
  1847. - Fixed "Invalid FP operation" in AdjustColorLevels in FPC compiled exes
  1848. (thanks to Carlos Gonz�lez).
  1849. - Added TImagingCanvas.AdjustColorLevels method.
  1850. -- 0.25.0 Changes/Bug Fixes ---------------------------------
  1851. - Fixed error that could cause AV in linear and nonlinear filters.
  1852. - Added blended rect filling function FillRectBlend.
  1853. - Added drawing function with blending (DrawAlpha, StretchDrawAlpha,
  1854. StretchDrawAdd, DrawBlend, StretchDrawBlend, ...)
  1855. - Added non-linear filters (min, max, median).
  1856. - Added point transforms (invert, contrast, gamma, brightness).
  1857. -- 0.21 Changes/Bug Fixes -----------------------------------
  1858. - Added some new filter kernels for convolution.
  1859. - Added FillMode and PenMode properties.
  1860. - Added FrameRect, Rectangle, Ellipse, and Line methods.
  1861. - Removed HorzLine and VertLine from TFastARGB32Canvas - new versions
  1862. in general canvas is now as fast as those in TFastARGB32Canvas
  1863. (only in case of A8R8G8B8 images of course).
  1864. - Added PenWidth property, updated HorzLine and VertLine to use it.
  1865. -- 0.19 Changes/Bug Fixes -----------------------------------
  1866. - added TFastARGB32Canvas
  1867. - added convolutions, hline, vline
  1868. - unit created, intial stuff added
  1869. }
  1870. end.