| 1 |
unit Img32.Draw; |
| 2 |
|
| 3 |
(******************************************************************************* |
| 4 |
* Author : Angus Johnson * |
| 5 |
* Version : 4.8 * |
| 6 |
* Date : 10 January 2025 * |
| 7 |
* Website : http://www.angusj.com * |
| 8 |
* Copyright : Angus Johnson 2019-2025 * |
| 9 |
* * |
| 10 |
* Purpose : Polygon renderer for TImage32 * |
| 11 |
* * |
| 12 |
* License : Use, modification & distribution is subject to * |
| 13 |
* Boost Software License Ver 1 * |
| 14 |
* http://www.boost.org/LICENSE_1_0.txt * |
| 15 |
*******************************************************************************) |
| 16 |
|
| 17 |
interface |
| 18 |
|
| 19 |
{$I Img32.inc} |
| 20 |
|
| 21 |
uses |
| 22 |
SysUtils, Classes, Types, Math, Img32, Img32.Vector; |
| 23 |
|
| 24 |
type |
| 25 |
TFillRule = Img32.Vector.TFillRule; |
| 26 |
|
| 27 |
// TGradientColor: used internally by both |
| 28 |
// TLinearGradientRenderer and TRadialGradientRenderer |
| 29 |
TGradientColor = record |
| 30 |
offset: double; |
| 31 |
color: TColor32; |
| 32 |
end; |
| 33 |
TArrayOfGradientColor = array of TGradientColor; |
| 34 |
|
| 35 |
TGradientFillStyle = (gfsClamp, gfsMirror, gfsRepeat); |
| 36 |
|
| 37 |
// TBoundsProc: Function template for TCustomRenderer. |
| 38 |
TBoundsProc = function(dist, colorsCnt: integer): integer; |
| 39 |
TBoundsProcD = function(dist: double; colorsCnt: integer): integer; |
| 40 |
|
| 41 |
TImage32ChangeProc = procedure of object; |
| 42 |
|
| 43 |
// TCustomRenderer: can accommodate pixels of any size |
| 44 |
TCustomRenderer = class {$IFDEF ABSTRACT_CLASSES} abstract {$ENDIF} |
| 45 |
private |
| 46 |
fImgWidth : integer; |
| 47 |
fImgHeight : integer; |
| 48 |
fImgBase : Pointer; |
| 49 |
fCurrY : integer; |
| 50 |
fCurrLinePtr : Pointer; |
| 51 |
fPixelSize : integer; |
| 52 |
fChangeProc : TImage32ChangeProc; |
| 53 |
fOpacity : Byte; |
| 54 |
protected |
| 55 |
procedure NotifyChange; |
| 56 |
function Initialize(imgBase: Pointer; |
| 57 |
imgWidth, imgHeight, pixelSize: integer): Boolean; overload; virtual; |
| 58 |
function Initialize(targetImage: TImage32): Boolean; overload; virtual; |
| 59 |
function GetDstPixel(x,y: integer): Pointer; |
| 60 |
// RenderProc: x & y refer to pixel coords in the destination image and |
| 61 |
// where x1 is the start (and left) and x2 is the end of the render |
| 62 |
procedure RenderProc(x1, x2, y: integer; alpha: PByte); virtual; abstract; |
| 63 |
// RenderProcSkip: is called for every skipped line block if |
| 64 |
// SupportsRenderProcSkip=True and the Rasterize() function skips scanlines. |
| 65 |
procedure RenderProcSkip(const skippedRect: TRect); virtual; |
| 66 |
// SetClipRect is called by the Rasterize() function with the |
| 67 |
// rasterization clipRect. The default implementation does nothing. |
| 68 |
procedure SetClipRect(const clipRect: TRect); virtual; |
| 69 |
// If SupportsRenderProcSkip returns True the Rasterize() function |
| 70 |
// will call RenderProcSkip() for every scanline where it didn't have |
| 71 |
// anything to rasterize. |
| 72 |
function SupportsRenderProcSkip: Boolean; virtual; |
| 73 |
public |
| 74 |
constructor Create; virtual; |
| 75 |
property ImgWidth: integer read fImgWidth; |
| 76 |
property ImgHeight: integer read fImgHeight; |
| 77 |
property ImgBase: Pointer read fImgBase; |
| 78 |
property PixelSize: integer read fPixelSize; |
| 79 |
property Opacity: Byte read fOpacity write fOpacity; |
| 80 |
end; |
| 81 |
|
| 82 |
TCustomColorRenderer = class(TCustomRenderer) |
| 83 |
private |
| 84 |
fColor: TColor32; |
| 85 |
protected |
| 86 |
property Color: TColor32 read fColor write fColor; |
| 87 |
public |
| 88 |
procedure SetColor(value: TColor32); virtual; |
| 89 |
end; |
| 90 |
|
| 91 |
TColorRenderer = class(TCustomColorRenderer) |
| 92 |
private |
| 93 |
fAlpha: Byte; |
| 94 |
protected |
| 95 |
procedure RenderProc(x1, x2, y: integer; alpha: PByte); override; |
| 96 |
function Initialize(targetImage: TImage32): Boolean; override; |
| 97 |
public |
| 98 |
constructor Create(color: TColor32 = clNone32); reintroduce; |
| 99 |
procedure SetColor(value: TColor32); override; |
| 100 |
end; |
| 101 |
|
| 102 |
TAliasedColorRenderer = class(TCustomColorRenderer) |
| 103 |
protected |
| 104 |
function Initialize(targetImage: TImage32): Boolean; override; |
| 105 |
procedure RenderProc(x1, x2, y: integer; alpha: PByte); override; |
| 106 |
public |
| 107 |
constructor Create(color: TColor32 = clNone32); reintroduce; |
| 108 |
end; |
| 109 |
|
| 110 |
// TMaskRenderer masks all pixels inside the clipRect area |
| 111 |
// where the alpha[]-array is zero. |
| 112 |
TMaskRenderer = class(TCustomRenderer) |
| 113 |
private |
| 114 |
fClipRect: TRect; |
| 115 |
protected |
| 116 |
procedure SetClipRect(const clipRect: TRect); override; |
| 117 |
procedure RenderProc(x1, x2, y: integer; alpha: PByte); override; |
| 118 |
procedure RenderProcSkip(const skippedRect: TRect); override; |
| 119 |
function SupportsRenderProcSkip: Boolean; override; |
| 120 |
end; |
| 121 |
|
| 122 |
// TCustomRendererCache is used to not create Renderer |
| 123 |
// objects for every DrawPolygon/DrawLine function call. The color |
| 124 |
// of the TCustomColorRenderer will be changed by the DrawPolygon/ |
| 125 |
// DrawLine method. |
| 126 |
TCustomRendererCache = class(TObject) |
| 127 |
private |
| 128 |
fColorRenderer: TColorRenderer; |
| 129 |
fAliasedColorRenderer: TAliasedColorRenderer; |
| 130 |
fMaskRenderer: TMaskRenderer; |
| 131 |
public |
| 132 |
constructor Create; |
| 133 |
destructor Destroy; override; |
| 134 |
function GetColorRenderer(color: TColor32): TColorRenderer; |
| 135 |
|
| 136 |
property ColorRenderer: TColorRenderer read fColorRenderer; |
| 137 |
property AliasedColorRenderer: TAliasedColorRenderer read fAliasedColorRenderer; |
| 138 |
property MaskRenderer: TMaskRenderer read fMaskRenderer; |
| 139 |
end; |
| 140 |
|
| 141 |
TEraseRenderer = class(TCustomRenderer) |
| 142 |
protected |
| 143 |
procedure RenderProc(x1, x2, y: integer; alpha: PByte); override; |
| 144 |
end; |
| 145 |
|
| 146 |
TInverseRenderer = class(TCustomRenderer) |
| 147 |
protected |
| 148 |
procedure RenderProc(x1, x2, y: integer; alpha: PByte); override; |
| 149 |
end; |
| 150 |
|
| 151 |
TImageRenderer = class(TCustomRenderer) |
| 152 |
private |
| 153 |
fImage : TImage32; |
| 154 |
fOffset : TPoint; |
| 155 |
fBrushPixel : PARGB; |
| 156 |
fLastYY : integer; |
| 157 |
fMirrorY : Boolean; |
| 158 |
fBoundsProc : TBoundsProc; |
| 159 |
function GetFirstBrushPixel(x, y: integer): PColor32; |
| 160 |
protected |
| 161 |
procedure RenderProc(x1, x2, y: integer; alpha: PByte); override; |
| 162 |
function Initialize(targetImage: TImage32): Boolean; override; |
| 163 |
public |
| 164 |
constructor Create(tileFillStyle: TTileFillStyle = tfsRepeat; |
| 165 |
brushImage: TImage32 = nil); reintroduce; |
| 166 |
destructor Destroy; override; |
| 167 |
procedure SetTileFillStyle(value: TTileFillStyle); |
| 168 |
property Image: TImage32 read fImage; |
| 169 |
property Offset: TPoint read fOffset write fOffset; |
| 170 |
end; |
| 171 |
|
| 172 |
// TCustomGradientRenderer is also an abstract class |
| 173 |
TCustomGradientRenderer = class(TCustomRenderer) |
| 174 |
private |
| 175 |
fBoundsProc : TBoundsProc; |
| 176 |
fGradientColors : TArrayOfGradientColor; |
| 177 |
protected |
| 178 |
fColors : TArrayOfColor32; |
| 179 |
fColorsCnt : integer; |
| 180 |
procedure SetGradientFillStyle(value: TGradientFillStyle); virtual; |
| 181 |
public |
| 182 |
constructor Create; override; |
| 183 |
procedure SetParameters(startColor, endColor: TColor32; |
| 184 |
gradFillStyle: TGradientFillStyle = gfsClamp); virtual; |
| 185 |
procedure InsertColorStop(offsetFrac: double; color: TColor32); |
| 186 |
procedure Clear; |
| 187 |
end; |
| 188 |
|
| 189 |
TLinearGradientRenderer = class(TCustomGradientRenderer) |
| 190 |
private |
| 191 |
fStartPt : TPointD; |
| 192 |
fEndPt : TPointD; |
| 193 |
fPerpendicOffsets: TArrayOfInteger; |
| 194 |
fIsVert : Boolean; |
| 195 |
protected |
| 196 |
procedure RenderProc(x1, x2, y: integer; alpha: PByte); override; |
| 197 |
function Initialize(targetImage: TImage32): Boolean; override; |
| 198 |
public |
| 199 |
procedure SetParameters(const startPt, endPt: TPointD; |
| 200 |
startColor, endColor: TColor32; |
| 201 |
gradFillStyle: TGradientFillStyle = gfsClamp); reintroduce; |
| 202 |
end; |
| 203 |
|
| 204 |
TRadialGradientRenderer = class(TCustomGradientRenderer) |
| 205 |
private |
| 206 |
fCenterPt : TPointD; |
| 207 |
fScaleX : double; |
| 208 |
fScaleY : double; |
| 209 |
fColors : TArrayOfColor32; |
| 210 |
protected |
| 211 |
procedure RenderProc(x1, x2, y: integer; alpha: PByte); override; |
| 212 |
function Initialize(targetImage: TImage32): Boolean; override; |
| 213 |
public |
| 214 |
procedure SetParameters(const focalRect: TRect; |
| 215 |
innerColor, outerColor: TColor32; |
| 216 |
gradientFillStyle: TGradientFillStyle = gfsClamp); reintroduce; |
| 217 |
end; |
| 218 |
|
| 219 |
TSvgRadialGradientRenderer = class(TCustomGradientRenderer) |
| 220 |
private |
| 221 |
fA, fB : double; |
| 222 |
fAA, fBB : double; |
| 223 |
fCenterPt : TPointD; |
| 224 |
fFocusPt : TPointD; |
| 225 |
fBoundsProcD : TBoundsProcD; |
| 226 |
protected |
| 227 |
procedure RenderProc(x1, x2, y: integer; alpha: PByte); override; |
| 228 |
function Initialize(targetImage: TImage32): Boolean; override; |
| 229 |
public |
| 230 |
procedure SetParameters(const ellipseRect: TRect; |
| 231 |
const focus: TPoint; innerColor, outerColor: TColor32; |
| 232 |
gradientFillStyle: TGradientFillStyle = gfsClamp); reintroduce; |
| 233 |
end; |
| 234 |
|
| 235 |
// Barycentric rendering colorizes inside triangles |
| 236 |
TBarycentricRenderer = class(TCustomRenderer) |
| 237 |
private |
| 238 |
a: TPointD; |
| 239 |
c1, c2, c3: TARGB; |
| 240 |
v0, v1: TPointD; |
| 241 |
d00, d01, d11, invDenom: double; |
| 242 |
function GetColor(const pt: TPointD): TColor32; |
| 243 |
protected |
| 244 |
procedure RenderProc(x1, x2, y: integer; alpha: PByte); override; |
| 245 |
public |
| 246 |
procedure SetParameters(const a, b, c: TPointD; c1, c2, c3: TColor32); |
| 247 |
end; |
| 248 |
|
| 249 |
// ///////////////////////////////////////////////////////////////////////// |
| 250 |
// DRAWING FUNCTIONS |
| 251 |
// ///////////////////////////////////////////////////////////////////////// |
| 252 |
|
| 253 |
procedure DrawPoint(img: TImage32; const pt: TPointD; |
| 254 |
radius: double; color: TColor32); overload; |
| 255 |
procedure DrawPoint(img: TImage32; const pt: TPointD; |
| 256 |
radius: double; renderer: TCustomRenderer); overload; |
| 257 |
procedure DrawPoint(img: TImage32; const points: TPathD; |
| 258 |
radius: double; color: TColor32); overload; |
| 259 |
procedure DrawPoint(img: TImage32; const paths: TPathsD; |
| 260 |
radius: double; color: TColor32); overload; |
| 261 |
|
| 262 |
procedure DrawInvertedPoint(img: TImage32; const pt: TPointD; radius: double); |
| 263 |
|
| 264 |
procedure DrawLine(img: TImage32; |
| 265 |
const pt1, pt2: TPointD; lineWidth: double; color: TColor32); overload; |
| 266 |
procedure DrawLine(img: TImage32; |
| 267 |
const line: TPathD; lineWidth: double; color: TColor32; |
| 268 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto; |
| 269 |
miterLimit: double = 2); overload; |
| 270 |
procedure DrawLine(img: TImage32; |
| 271 |
const line: TPathD; lineWidth: double; color: TColor32; |
| 272 |
rendererCache: TCustomRendererCache; |
| 273 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto; |
| 274 |
miterLimit: double = 2); overload; |
| 275 |
procedure DrawLine(img: TImage32; |
| 276 |
const line: TPathD; lineWidth: double; renderer: TCustomRenderer; |
| 277 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto; |
| 278 |
miterLimit: double = 2); overload; |
| 279 |
procedure DrawLine(img: TImage32; const lines: TPathsD; |
| 280 |
lineWidth: double; color: TColor32; |
| 281 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto; |
| 282 |
miterLimit: double = 2); overload; |
| 283 |
procedure DrawLine(img: TImage32; const lines: TPathsD; |
| 284 |
lineWidth: double; color: TColor32; rendererCache: TCustomRendererCache; |
| 285 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto; |
| 286 |
miterLimit: double = 2); overload; |
| 287 |
procedure DrawLine(img: TImage32; const lines: TPathsD; |
| 288 |
lineWidth: double; renderer: TCustomRenderer; |
| 289 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto; |
| 290 |
miterLimit: double = 2); overload; |
| 291 |
|
| 292 |
procedure DrawInvertedLine(img: TImage32; |
| 293 |
const line: TPathD; lineWidth: double; |
| 294 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto); overload; |
| 295 |
procedure DrawInvertedLine(img: TImage32; |
| 296 |
const lines: TPathsD; lineWidth: double; |
| 297 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto); overload; |
| 298 |
|
| 299 |
procedure DrawDashedLine(img: TImage32; const line: TPathD; |
| 300 |
dashPattern: TArrayOfDouble; patternOffset: PDouble; |
| 301 |
lineWidth: double; color: TColor32; |
| 302 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto; |
| 303 |
rendererCache: TCustomRendererCache = nil); overload; |
| 304 |
procedure DrawDashedLine(img: TImage32; const lines: TPathsD; |
| 305 |
dashPattern: TArrayOfDouble; patternOffset: PDouble; |
| 306 |
lineWidth: double; color: TColor32; endStyle: TEndStyle; |
| 307 |
joinStyle: TJoinStyle = jsAuto; |
| 308 |
rendererCache: TCustomRendererCache = nil); overload; |
| 309 |
procedure DrawDashedLine(img: TImage32; const line: TPathD; |
| 310 |
dashPattern: TArrayOfDouble; patternOffset: PDouble; |
| 311 |
lineWidth: double; renderer: TCustomRenderer; endStyle: TEndStyle; |
| 312 |
joinStyle: TJoinStyle = jsAuto); overload; |
| 313 |
procedure DrawDashedLine(img: TImage32; const lines: TPathsD; |
| 314 |
dashPattern: TArrayOfDouble; patternOffset: PDouble; |
| 315 |
lineWidth: double; renderer: TCustomRenderer; |
| 316 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto); overload; |
| 317 |
|
| 318 |
procedure DrawInvertedDashedLine(img: TImage32; |
| 319 |
const line: TPathD; dashPattern: TArrayOfDouble; |
| 320 |
patternOffset: PDouble; lineWidth: double; endStyle: TEndStyle; |
| 321 |
joinStyle: TJoinStyle = jsAuto); overload; |
| 322 |
procedure DrawInvertedDashedLine(img: TImage32; |
| 323 |
const lines: TPathsD; dashPattern: TArrayOfDouble; |
| 324 |
patternOffset: PDouble; lineWidth: double; |
| 325 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto); overload; |
| 326 |
|
| 327 |
procedure DrawPolygon(img: TImage32; const polygon: TPathD; |
| 328 |
fillRule: TFillRule; color: TColor32); overload; |
| 329 |
procedure DrawPolygon(img: TImage32; const polygon: TPathD; |
| 330 |
fillRule: TFillRule; renderer: TCustomRenderer); overload; |
| 331 |
procedure DrawPolygon(img: TImage32; const polygons: TPathsD; |
| 332 |
fillRule: TFillRule; color: TColor32); overload; |
| 333 |
procedure DrawPolygon(img: TImage32; const polygons: TPathsD; |
| 334 |
fillRule: TFillRule; color: TColor32; |
| 335 |
rendererCache: TCustomRendererCache); overload; |
| 336 |
procedure DrawPolygon(img: TImage32; const polygons: TPathsD; |
| 337 |
fillRule: TFillRule; renderer: TCustomRenderer); overload; |
| 338 |
|
| 339 |
procedure DrawInvertedPolygon(img: TImage32; const polygon: TPathD; |
| 340 |
fillRule: TFillRule); overload; |
| 341 |
procedure DrawInvertedPolygon(img: TImage32; const polygons: TPathsD; |
| 342 |
fillRule: TFillRule); overload; |
| 343 |
|
| 344 |
// 'Clear Type' text rendering is quite useful for low resolution |
| 345 |
// displays (96 ppi). However it's of little to no benefit on higher |
| 346 |
// resolution displays and becomes unnecessary overhead. See also: |
| 347 |
// https://en.wikipedia.org/wiki/Subpixel_rendering |
| 348 |
// https://www.grc.com/ctwhat.htm |
| 349 |
// https://www.grc.com/cttech.htm |
| 350 |
procedure DrawPolygon_ClearType(img: TImage32; const polygons: TPathsD; |
| 351 |
fillRule: TFillRule; color: TColor32; backColor: TColor32 = clWhite32); |
| 352 |
|
| 353 |
// ///////////////////////////////////////////////////////////////////////// |
| 354 |
// MISCELLANEOUS FUNCTIONS |
| 355 |
// ///////////////////////////////////////////////////////////////////////// |
| 356 |
|
| 357 |
procedure ErasePolygon(img: TImage32; const polygon: TPathD; |
| 358 |
fillRule: TFillRule); overload; |
| 359 |
procedure ErasePolygon(img: TImage32; const polygons: TPathsD; |
| 360 |
fillRule: TFillRule); overload; |
| 361 |
|
| 362 |
// Both DrawBoolMask and DrawAlphaMask require |
| 363 |
// 'mask' length to equal 'img' width * height |
| 364 |
procedure DrawBoolMask(img: TImage32; |
| 365 |
const mask: TArrayOfByte; color: TColor32 = clBlack32); |
| 366 |
procedure DrawAlphaMask(img: TImage32; |
| 367 |
const mask: TArrayOfByte; color: TColor32 = clBlack32); |
| 368 |
|
| 369 |
procedure Rasterize(const paths: TPathsD; |
| 370 |
const clipRec: TRect; fillRule: TFillRule; renderer: TCustomRenderer); overload; |
| 371 |
procedure Rasterize(img: TImage32; const paths: TPathsD; |
| 372 |
const clipRec: TRect; fillRule: TFillRule; renderer: TCustomRenderer); overload; |
| 373 |
|
| 374 |
implementation |
| 375 |
|
| 376 |
{$IFDEF CPUX86} |
| 377 |
const |
| 378 |
// Use faster Trunc for x86 code in this unit. |
| 379 |
Trunc: function(Value: Double): Integer = __Trunc; |
| 380 |
{$ENDIF CPUX86} |
| 381 |
|
| 382 |
type |
| 383 |
|
| 384 |
// A horizontal scanline contains any number of line fragments. A fragment |
| 385 |
// can be a number of pixels wide but it can't be more than one pixel high. |
| 386 |
PFragment = ^TFragment; |
| 387 |
TFragment = record |
| 388 |
botX, topX, dy, dydx: double; // ie x at bottom and top of scanline |
| 389 |
end; |
| 390 |
|
| 391 |
TScanLine = record |
| 392 |
Y: integer; |
| 393 |
minX, maxX: integer; |
| 394 |
fragCnt: integer; |
| 395 |
fragOffset: integer; |
| 396 |
end; |
| 397 |
PScanline = ^TScanline; |
| 398 |
TArrayOfScanline = array of TScanline; |
| 399 |
|
| 400 |
// ------------------------------------------------------------------------------ |
| 401 |
// ApplyClearType (see DrawPolygon_ClearType below) |
| 402 |
// ------------------------------------------------------------------------------ |
| 403 |
|
| 404 |
type |
| 405 |
PArgbs = ^TArgbs; |
| 406 |
TArgbs = array [0.. (Maxint div SizeOf(TARGB)) -1] of TARGB; |
| 407 |
|
| 408 |
procedure ApplyClearType(img: TImage32; textColor: TColor32 = clBlack32; |
| 409 |
bkColor: TColor32 = clWhite32); |
| 410 |
const |
| 411 |
centerWeighting = 5; //0 <= centerWeighting <= 25 |
| 412 |
var |
| 413 |
h, w: integer; |
| 414 |
src, dst: PARGB; |
| 415 |
srcArr: PArgbs; |
| 416 |
fgColor: TARGB absolute textColor; |
| 417 |
bgColor: TARGB absolute bkColor; |
| 418 |
diff_R, diff_G, diff_B: integer; |
| 419 |
bg8_R, bg8_G, bg8_B: integer; |
| 420 |
rowBuffer: TArrayOfARGB; |
| 421 |
primeTbl, nearTbl, FarTbl: PByteArray; |
| 422 |
begin |
| 423 |
// Precondition: the background to text drawn onto 'img' must be transparent |
| 424 |
|
| 425 |
// 85 + (2 * 57) + (2 * 28) == 255 |
| 426 |
primeTbl := PByteArray(@MulTable[85 + centerWeighting *2]); |
| 427 |
nearTbl := PByteArray(@MulTable[57]); |
| 428 |
farTbl := PByteArray(@MulTable[28 - centerWeighting]); |
| 429 |
SetLength(rowBuffer, img.Width +4); |
| 430 |
|
| 431 |
for h := 0 to img.Height -1 do |
| 432 |
begin |
| 433 |
// each row of the image is copied into a temporary buffer ... |
| 434 |
// noting that while 'dst' (img.Pixels) is initially the source |
| 435 |
// it will later be destination (during image compression). |
| 436 |
dst := PARGB(@img.Pixels[h * img.Width]); |
| 437 |
src := PARGB(@rowBuffer[2]); |
| 438 |
Move(dst^, src^, img.Width * SizeOf(TColor32)); |
| 439 |
srcArr := PArgbs(rowBuffer); |
| 440 |
|
| 441 |
// using this buffer compress the image ... |
| 442 |
w := 2; |
| 443 |
while w < img.Width do |
| 444 |
begin |
| 445 |
dst.R := primeTbl[srcArr[w].A] + |
| 446 |
nearTbl[srcArr[w-1].A] + farTbl[srcArr[w-2].A] + |
| 447 |
nearTbl[srcArr[w+1].A] + farTbl[srcArr[w+2].A]; |
| 448 |
inc(w); |
| 449 |
dst.G := primeTbl[srcArr[w].A] + |
| 450 |
nearTbl[srcArr[w-1].A] + farTbl[srcArr[w-2].A] + |
| 451 |
nearTbl[srcArr[w+1].A] + farTbl[srcArr[w+2].A]; |
| 452 |
inc(w); |
| 453 |
dst.B := primeTbl[srcArr[w].A] + |
| 454 |
nearTbl[srcArr[w-1].A] + farTbl[srcArr[w-2].A] + |
| 455 |
nearTbl[srcArr[w+1].A] + farTbl[srcArr[w+2].A]; |
| 456 |
inc(w); |
| 457 |
dst.A := 255; |
| 458 |
inc(dst); |
| 459 |
end; |
| 460 |
end; |
| 461 |
|
| 462 |
// Following compression the right 2/3 of the image is redundant |
| 463 |
img.Crop(Types.Rect(0,0, img.Width div 3, img.Height)); |
| 464 |
|
| 465 |
// currently text is white and the background is black |
| 466 |
// so blend in the text and background colors ... |
| 467 |
diff_R := fgColor.R - bgColor.R; |
| 468 |
diff_G := fgColor.G - bgColor.G; |
| 469 |
diff_B := fgColor.B - bgColor.B; |
| 470 |
bg8_R := bgColor.R shl 8; |
| 471 |
bg8_G := bgColor.G shl 8; |
| 472 |
bg8_B := bgColor.B shl 8; |
| 473 |
dst := PARGB(img.PixelBase); |
| 474 |
for h := 0 to img.Width * img.Height -1 do |
| 475 |
begin |
| 476 |
if dst.R = 0 then |
| 477 |
dst.Color := bkColor |
| 478 |
else |
| 479 |
begin |
| 480 |
// blend front (text) and background colors ... |
| 481 |
dst.R := (bg8_R + diff_R * dst.R) shr 8; |
| 482 |
dst.G := (bg8_G + diff_G * dst.G) shr 8; |
| 483 |
dst.B := (bg8_B + diff_B * dst.B) shr 8; |
| 484 |
end; |
| 485 |
inc(dst); |
| 486 |
end; |
| 487 |
end; |
| 488 |
|
| 489 |
// ------------------------------------------------------------------------------ |
| 490 |
// Other miscellaneous functions |
| 491 |
// ------------------------------------------------------------------------------ |
| 492 |
|
| 493 |
function ClampByte(val: double): byte; {$IFDEF INLINE} inline; {$ENDIF} |
| 494 |
begin |
| 495 |
if val < 0 then result := 0 |
| 496 |
else if val > 255 then result := 255 |
| 497 |
else result := Round(val); |
| 498 |
end; |
| 499 |
// ------------------------------------------------------------------------------ |
| 500 |
|
| 501 |
function GetPixel(current: PARGB; delta: integer): PARGB; |
| 502 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 503 |
begin |
| 504 |
Result := current; |
| 505 |
inc(Result, delta); |
| 506 |
end; |
| 507 |
// ------------------------------------------------------------------------------ |
| 508 |
|
| 509 |
// Here "const" is used for opimization reasons, to skip the |
| 510 |
// dyn-array reference counting. "const" for dyn-arrays doesn't |
| 511 |
// prevent one from changing the array's content. |
| 512 |
procedure ReverseColors(const colors: TArrayOfGradientColor); |
| 513 |
var |
| 514 |
highI: integer; |
| 515 |
dst, src: ^TGradientColor; |
| 516 |
// Not using a TGradientColor record for the temporary value |
| 517 |
// allows the 64-bit compiler to use an XMM register for it. |
| 518 |
tmpOffset: double; |
| 519 |
tmpColor: TColor32; |
| 520 |
begin |
| 521 |
highI := High(colors); |
| 522 |
|
| 523 |
dst := @colors[0]; |
| 524 |
src := @colors[highI]; |
| 525 |
while PByte(dst) < PByte(src) do |
| 526 |
begin |
| 527 |
tmpColor := dst.color; |
| 528 |
tmpOffset := dst.offset; |
| 529 |
|
| 530 |
dst.color := src.color; |
| 531 |
dst.offset := 1 - src.offset; |
| 532 |
|
| 533 |
src.color := tmpColor; |
| 534 |
src.offset := 1 - tmpOffset; |
| 535 |
|
| 536 |
inc(dst); |
| 537 |
dec(src); |
| 538 |
end; |
| 539 |
end; |
| 540 |
// ------------------------------------------------------------------------------ |
| 541 |
|
| 542 |
procedure SwapColors(var color1, color2: TColor32); |
| 543 |
var |
| 544 |
c: TColor32; |
| 545 |
begin |
| 546 |
c := color1; |
| 547 |
color1 := color2; |
| 548 |
color2 := c; |
| 549 |
end; |
| 550 |
// ------------------------------------------------------------------------------ |
| 551 |
|
| 552 |
procedure SwapPoints(var point1, point2: TPoint); overload; |
| 553 |
var |
| 554 |
pt: TPoint; |
| 555 |
begin |
| 556 |
pt := point1; |
| 557 |
point1 := point2; |
| 558 |
point2 := pt; |
| 559 |
end; |
| 560 |
// ------------------------------------------------------------------------------ |
| 561 |
|
| 562 |
procedure SwapPoints(var point1, point2: TPointD); overload; |
| 563 |
var |
| 564 |
pt: TPointD; |
| 565 |
begin |
| 566 |
pt := point1; |
| 567 |
point1 := point2; |
| 568 |
point2 := pt; |
| 569 |
end; |
| 570 |
// ------------------------------------------------------------------------------ |
| 571 |
|
| 572 |
function ClampQ(q, endQ: integer): integer; |
| 573 |
begin |
| 574 |
if q < 0 then result := 0 |
| 575 |
else if q >= endQ then result := endQ -1 |
| 576 |
else result := q; |
| 577 |
end; |
| 578 |
// ------------------------------------------------------------------------------ |
| 579 |
|
| 580 |
function ClampD(d: double; colorCnt: integer): integer; |
| 581 |
begin |
| 582 |
dec(colorCnt); |
| 583 |
if d < 0 then result := 0 |
| 584 |
else if d >= 1 then result := colorCnt |
| 585 |
else result := Round(d * colorCnt); |
| 586 |
end; |
| 587 |
// ------------------------------------------------------------------------------ |
| 588 |
|
| 589 |
function MirrorQ(q, endQ: integer): integer; |
| 590 |
begin |
| 591 |
result := q mod endQ; |
| 592 |
if (result < 0) then result := -result; |
| 593 |
if Odd(q div endQ) then |
| 594 |
result := (endQ -1) - result; |
| 595 |
end; |
| 596 |
// ------------------------------------------------------------------------------ |
| 597 |
|
| 598 |
function MirrorD(d: double; colorCnt: integer): integer; |
| 599 |
begin |
| 600 |
dec(colorCnt); |
| 601 |
if Odd(Trunc(d)) then |
| 602 |
result := Trunc((1 - frac(d)) * colorCnt) else |
| 603 |
result := Trunc(frac(d) * colorCnt); |
| 604 |
end; |
| 605 |
// ------------------------------------------------------------------------------ |
| 606 |
|
| 607 |
function RepeatQ(q, endQ: integer): integer; |
| 608 |
begin |
| 609 |
if (q < 0) or (q >= endQ) then |
| 610 |
begin |
| 611 |
endQ := Abs(endQ); |
| 612 |
result := q mod endQ; |
| 613 |
if result < 0 then inc(result, endQ); |
| 614 |
end |
| 615 |
else result := q; |
| 616 |
end; |
| 617 |
// ------------------------------------------------------------------------------ |
| 618 |
|
| 619 |
function SoftRptQ(q, endQ: integer): integer; |
| 620 |
begin |
| 621 |
if (q < 0) then |
| 622 |
result := endQ + (q mod endQ) else |
| 623 |
result := (q mod endQ); |
| 624 |
if result = 0 then result := endQ div 2; |
| 625 |
end; |
| 626 |
// ------------------------------------------------------------------------------ |
| 627 |
|
| 628 |
function RepeatD(d: double; colorCnt: integer): integer; |
| 629 |
begin |
| 630 |
dec(colorCnt); |
| 631 |
if (d < 0) then |
| 632 |
result := Trunc((1 + frac(d)) * colorCnt) else |
| 633 |
result := Trunc(frac(d) * colorCnt); |
| 634 |
end; |
| 635 |
// ------------------------------------------------------------------------------ |
| 636 |
|
| 637 |
function BlendColorUsingMask(bgColor, fgColor: TColor32; mask: Byte): TColor32; |
| 638 |
var |
| 639 |
bg: TARGB absolute bgColor; |
| 640 |
fg: TARGB absolute fgColor; |
| 641 |
res: TARGB absolute Result; |
| 642 |
R, invR: PByteArray; |
| 643 |
begin |
| 644 |
if fg.A = 0 then |
| 645 |
begin |
| 646 |
Result := bgColor; |
| 647 |
res.A := MulTable[res.A, not mask]; |
| 648 |
end |
| 649 |
else if bg.A = 0 then |
| 650 |
begin |
| 651 |
Result := fgColor; |
| 652 |
res.A := MulTable[res.A, mask]; |
| 653 |
end |
| 654 |
else if (mask = 0) then |
| 655 |
Result := bgColor |
| 656 |
else if (mask = 255) then |
| 657 |
Result := fgColor |
| 658 |
else |
| 659 |
begin |
| 660 |
R := PByteArray(@MulTable[mask]); |
| 661 |
InvR := PByteArray(@MulTable[not mask]); |
| 662 |
res.A := R[fg.A] + InvR[bg.A]; |
| 663 |
res.R := R[fg.R] + InvR[bg.R]; |
| 664 |
res.G := R[fg.G] + InvR[bg.G]; |
| 665 |
res.B := R[fg.B] + InvR[bg.B]; |
| 666 |
end; |
| 667 |
end; |
| 668 |
// ------------------------------------------------------------------------------ |
| 669 |
|
| 670 |
// MakeColorGradient: using the supplied array of TGradientColor, |
| 671 |
// create an array of TColor32 of the specified length |
| 672 |
procedure MakeColorGradient(const gradColors: TArrayOfGradientColor; |
| 673 |
len: integer; var result: TArrayOfColor32); |
| 674 |
var |
| 675 |
i,j, lenC: integer; |
| 676 |
dist, offset1, offset2, step, pos, reciprocalDistTimes255: double; |
| 677 |
color1, color2: TColor32; |
| 678 |
begin |
| 679 |
lenC := length(gradColors); |
| 680 |
if (len = 0) or (lenC < 2) then Exit; |
| 681 |
if Length(result) <> len then // we can reuse the array |
| 682 |
SetLength(result, len); |
| 683 |
|
| 684 |
color2 := gradColors[0].color; |
| 685 |
result[0] := color2; |
| 686 |
if len = 1 then Exit; |
| 687 |
|
| 688 |
reciprocalDistTimes255 := 0; |
| 689 |
step := 1/(len-1); |
| 690 |
pos := step; |
| 691 |
offset2 := 0; |
| 692 |
i := 1; j := 1; |
| 693 |
repeat |
| 694 |
offset1 := offset2; |
| 695 |
offset2 := gradColors[i].offset; |
| 696 |
dist := offset2 - offset1; |
| 697 |
color1 := color2; |
| 698 |
color2 := gradColors[i].color; |
| 699 |
if dist > 0 then |
| 700 |
reciprocalDistTimes255 := 255/dist; // 1/dist*255 |
| 701 |
while (pos <= dist) and (j < len) do |
| 702 |
begin |
| 703 |
result[j] := BlendColorUsingMask(color1, color2, Round(pos * reciprocalDistTimes255)); |
| 704 |
inc(j); |
| 705 |
pos := pos + step; |
| 706 |
end; |
| 707 |
pos := pos - dist; |
| 708 |
inc(i); |
| 709 |
until i = lenC; |
| 710 |
if j < len then result[j] := result[j-1]; |
| 711 |
end; |
| 712 |
|
| 713 |
// ------------------------------------------------------------------------------ |
| 714 |
// Rasterize() support functions |
| 715 |
// ------------------------------------------------------------------------------ |
| 716 |
|
| 717 |
procedure AllocateScanlines(const polygons: TPathsD; |
| 718 |
const scanlines: TArrayOfScanline; var fragments: PFragment; clipBottom, clipRight: integer); |
| 719 |
var |
| 720 |
i,j, highI, highJ: integer; |
| 721 |
y1, y2: integer; |
| 722 |
fragOff: Cardinal; |
| 723 |
psl: PScanline; |
| 724 |
begin |
| 725 |
// first count how often each edge intersects with each horizontal scanline |
| 726 |
for i := 0 to high(polygons) do |
| 727 |
begin |
| 728 |
highJ := high(polygons[i]); |
| 729 |
if highJ < 2 then continue; |
| 730 |
y1 := Trunc(polygons[i][highJ].Y); |
| 731 |
for j := 0 to highJ do |
| 732 |
begin |
| 733 |
y2 := Trunc(polygons[i][j].Y); |
| 734 |
if y1 < y2 then |
| 735 |
begin |
| 736 |
// descending (but ignore edges outside the clipping range) |
| 737 |
if (y2 >= 0) and (y1 <= clipBottom) then |
| 738 |
begin |
| 739 |
if (y1 > 0) then |
| 740 |
dec(scanlines[y1 -1].fragCnt); |
| 741 |
if y2 >= clipBottom then |
| 742 |
inc(scanlines[clipBottom].fragCnt) else |
| 743 |
inc(scanlines[y2].fragCnt); |
| 744 |
end; |
| 745 |
end else |
| 746 |
begin |
| 747 |
// ascending (but ignore edges outside the clipping range) |
| 748 |
if (y1 >= 0) and (y2 <= clipBottom) then |
| 749 |
begin |
| 750 |
if (y2 > 0) then |
| 751 |
dec(scanlines[y2 -1].fragCnt); |
| 752 |
if y1 >= clipBottom then |
| 753 |
inc(scanlines[clipBottom].fragCnt) else |
| 754 |
inc(scanlines[y1].fragCnt); |
| 755 |
end; |
| 756 |
end; |
| 757 |
y1 := y2; |
| 758 |
end; |
| 759 |
end; |
| 760 |
|
| 761 |
// convert 'count' accumulators into real counts and allocate storage |
| 762 |
j := 0; |
| 763 |
fragOff := 0; |
| 764 |
highI := high(scanlines); |
| 765 |
psl := @scanlines[highI]; |
| 766 |
|
| 767 |
// 'fragments' is a pointer and not a dynamic array because |
| 768 |
// dynamic arrays are zero initialized (hence slower than GetMem). |
| 769 |
for i := highI downto 0 do |
| 770 |
begin |
| 771 |
inc(j, psl.fragCnt); // nb: psl.fragCnt may be < 0 here! |
| 772 |
if j > 0 then |
| 773 |
begin |
| 774 |
psl.fragOffset := fragOff; |
| 775 |
inc(fragOff, j); |
| 776 |
end else |
| 777 |
psl.fragOffset := -1; |
| 778 |
psl.fragCnt := 0; // reset for later |
| 779 |
psl.minX := clipRight; |
| 780 |
psl.maxX := 0; |
| 781 |
psl.Y := i; |
| 782 |
dec(psl); |
| 783 |
end; |
| 784 |
// allocate fragments as a single block of memory |
| 785 |
GetMem(fragments, fragOff * sizeOf(TFragment)); |
| 786 |
end; |
| 787 |
// ------------------------------------------------------------------------------ |
| 788 |
|
| 789 |
procedure SplitEdgeIntoFragments(const pt1, pt2: TPointD; |
| 790 |
const scanlines: TArrayOfScanline; fragments: PFragment; const clipRec: TRect); |
| 791 |
var |
| 792 |
x,y, dx,dy, absDx, dydx, dxdy: double; |
| 793 |
i, scanlineY, maxY, maxX: integer; |
| 794 |
psl: PScanLine; |
| 795 |
pFrag: PFragment; |
| 796 |
bot, top: TPointD; |
| 797 |
begin |
| 798 |
dy := pt1.Y - pt2.Y; |
| 799 |
|
| 800 |
if dy > 0 then |
| 801 |
begin |
| 802 |
// ASCENDING EDGE (+VE WINDING DIR) |
| 803 |
if dy < 0.0001 then Exit; //ignore near horizontals |
| 804 |
bot := pt1; top := pt2; |
| 805 |
end else |
| 806 |
begin |
| 807 |
// DESCENDING EDGE (-VE WINDING DIR) |
| 808 |
if dy > -0.0001 then Exit; //ignore near horizontals |
| 809 |
bot := pt2; top := pt1; |
| 810 |
end; |
| 811 |
// exclude edges that are completely outside the top or bottom clip region |
| 812 |
RectWidthHeight(clipRec, maxX, maxY); |
| 813 |
if (top.Y >= maxY) or (bot.Y <= 0) then Exit; |
| 814 |
|
| 815 |
dx := pt2.X - pt1.X; |
| 816 |
absDx := abs(dx); |
| 817 |
|
| 818 |
if absDx < 0.000001 then |
| 819 |
begin |
| 820 |
// VERTICAL EDGE |
| 821 |
top.X := bot.X; //this circumvents v. rare rounding issues. |
| 822 |
|
| 823 |
// exclude vertical edges that are outside the right clip region |
| 824 |
// but still update maxX for each scanline the edge passes |
| 825 |
if bot.X > maxX then |
| 826 |
begin |
| 827 |
for i := Min(maxY, Trunc(bot.Y)) downto Max(0, Trunc(top.Y)) do |
| 828 |
scanlines[i].maxX := maxX; |
| 829 |
Exit; |
| 830 |
end; |
| 831 |
|
| 832 |
dxdy := 0; |
| 833 |
if dy > 0 then dydx := 1 else dydx := -1; |
| 834 |
end else |
| 835 |
begin |
| 836 |
dxdy := dx/dy; |
| 837 |
dydx := dy/absDx; |
| 838 |
end; |
| 839 |
|
| 840 |
// TRIM EDGES THAT CROSS CLIPPING BOUNDARIES (EXCEPT THE LEFT BOUNDARY) |
| 841 |
if bot.X >= maxX then |
| 842 |
begin |
| 843 |
if top.X >= maxX then |
| 844 |
begin |
| 845 |
for i := Min(maxY, Trunc(bot.Y)) downto Max(0, Trunc(top.Y)) do |
| 846 |
scanlines[i].maxX := maxX; |
| 847 |
Exit; |
| 848 |
end; |
| 849 |
// here the edge must be oriented bottom-right to top-left |
| 850 |
y := bot.Y - (bot.X - maxX) * Abs(dydx); |
| 851 |
for i := Min(maxY, Trunc(bot.Y)) downto Max(0, Trunc(y)) do |
| 852 |
scanlines[i].maxX := maxX; |
| 853 |
bot.Y := y; |
| 854 |
if bot.Y <= 0 then Exit; |
| 855 |
bot.X := maxX; |
| 856 |
end |
| 857 |
else if top.X > maxX then |
| 858 |
begin |
| 859 |
// here the edge must be oriented bottom-left to top-right |
| 860 |
y := top.Y + (top.X - maxX) * Abs(dydx); |
| 861 |
for i := Min(maxY, Trunc(y)) downto Max(0, Trunc(top.Y)) do |
| 862 |
scanlines[i].maxX := maxX; |
| 863 |
top.Y := y; |
| 864 |
if top.Y >= maxY then Exit; |
| 865 |
top.X := maxX; |
| 866 |
end; |
| 867 |
if bot.Y > maxY then |
| 868 |
begin |
| 869 |
bot.X := bot.X + dxdy * (bot.Y - maxY); |
| 870 |
if (bot.X > maxX) then Exit; //nb: no clipping on the left |
| 871 |
bot.Y := maxY; |
| 872 |
end; |
| 873 |
if top.Y < 0 then |
| 874 |
begin |
| 875 |
top.X := top.X + (dxdy * top.Y); |
| 876 |
if (top.X > maxX) then Exit; //nb: no clipping on the left |
| 877 |
top.Y := 0; |
| 878 |
end; |
| 879 |
|
| 880 |
// SPLIT THE EDGE INTO MULTIPLE SCANLINE FRAGMENTS |
| 881 |
scanlineY := Trunc(bot.Y); |
| 882 |
if bot.Y = scanlineY then dec(scanlineY); |
| 883 |
|
| 884 |
// at the lower-most extent of the edge 'split' the first fragment |
| 885 |
if scanlineY < 0 then Exit; |
| 886 |
|
| 887 |
psl := @scanlines[scanlineY]; |
| 888 |
if psl.fragOffset < 0 then Exit; //a very rare event |
| 889 |
|
| 890 |
pFrag := fragments; |
| 891 |
inc(pFrag, psl.fragOffset + psl.fragCnt); |
| 892 |
inc(psl.fragCnt); |
| 893 |
|
| 894 |
pFrag.botX := bot.X; |
| 895 |
if scanlineY <= top.Y then |
| 896 |
begin |
| 897 |
// the whole edge is within 1 scanline |
| 898 |
pFrag.topX := top.X; |
| 899 |
pFrag.dy := bot.Y - top.Y; |
| 900 |
pFrag.dydx := dydx; |
| 901 |
Exit; |
| 902 |
end; |
| 903 |
|
| 904 |
x := bot.X + (bot.Y - scanlineY) * dxdy; |
| 905 |
pFrag.topX := x; |
| 906 |
pFrag.dy := bot.Y - scanlineY; |
| 907 |
pFrag.dydx := dydx; |
| 908 |
// 'split' subsequent fragments until the top fragment |
| 909 |
dec(psl); |
| 910 |
while psl.Y > top.Y do |
| 911 |
begin |
| 912 |
pFrag := fragments; |
| 913 |
inc(pFrag, psl.fragOffset + psl.fragCnt); |
| 914 |
inc(psl.fragCnt); |
| 915 |
pFrag.botX := x; |
| 916 |
x := x + dxdy; |
| 917 |
pFrag.topX := x; |
| 918 |
pFrag.dy := 1; |
| 919 |
pFrag.dydx := dydx; |
| 920 |
dec(psl); |
| 921 |
end; |
| 922 |
// and finally the top fragment |
| 923 |
pFrag := fragments; |
| 924 |
inc(pFrag, psl.fragOffset + psl.fragCnt); |
| 925 |
inc(psl.fragCnt); |
| 926 |
pFrag.botX := x; |
| 927 |
pFrag.topX := top.X; |
| 928 |
pFrag.dy := psl.Y + 1 - top.Y; |
| 929 |
pFrag.dydx := dydx; |
| 930 |
end; |
| 931 |
// ------------------------------------------------------------------------------ |
| 932 |
|
| 933 |
procedure InitializeScanlines(const polygons: TPathsD; |
| 934 |
const scanlines: TArrayOfScanline; fragments: PFragment; const clipRec: TRect); |
| 935 |
var |
| 936 |
i,j, highJ: integer; |
| 937 |
pt1, pt2: PPointD; |
| 938 |
begin |
| 939 |
for i := 0 to high(polygons) do |
| 940 |
begin |
| 941 |
highJ := high(polygons[i]); |
| 942 |
if highJ < 2 then continue; |
| 943 |
pt1 := @polygons[i][highJ]; |
| 944 |
pt2 := @polygons[i][0]; |
| 945 |
for j := 0 to highJ do |
| 946 |
begin |
| 947 |
SplitEdgeIntoFragments(pt1^, pt2^, scanlines, fragments, clipRec); |
| 948 |
pt1 := pt2; |
| 949 |
inc(pt2); |
| 950 |
end; |
| 951 |
end; |
| 952 |
end; |
| 953 |
// ------------------------------------------------------------------------------ |
| 954 |
|
| 955 |
procedure ProcessScanlineFragments(var scanline: TScanLine; |
| 956 |
fragments: PFragment; const buffer: TArrayOfDouble); |
| 957 |
var |
| 958 |
i,j, leftXi,rightXi: integer; |
| 959 |
fracX, yy, q{, windDir}: double; |
| 960 |
left, right, dy, dydx: double; |
| 961 |
frag: PFragment; |
| 962 |
pd: PDouble; |
| 963 |
begin |
| 964 |
frag := fragments; |
| 965 |
inc(frag, scanline.fragOffset); |
| 966 |
for i := 1 to scanline.fragCnt do |
| 967 |
begin |
| 968 |
left := frag.botX; |
| 969 |
right := frag.topX; |
| 970 |
dy := frag.dy; |
| 971 |
dydx := frag.dydx; |
| 972 |
inc(frag); |
| 973 |
|
| 974 |
// converting botX & topX to left & right simplifies code |
| 975 |
if {botX > topX} left > right then |
| 976 |
begin |
| 977 |
q := left; |
| 978 |
left := right; |
| 979 |
right := q; |
| 980 |
end; |
| 981 |
|
| 982 |
leftXi := Max(0, Trunc(left)); |
| 983 |
rightXi := Max(0, Trunc(right)); |
| 984 |
if (leftXi = rightXi) then |
| 985 |
begin |
| 986 |
// the fragment is only one pixel wide |
| 987 |
//if dydx < 0 then windDir := -1.0 else windDir := 1.0; |
| 988 |
if dydx < 0 then dy := -dy; |
| 989 |
|
| 990 |
if leftXi < scanline.minX then |
| 991 |
scanline.minX := leftXi; |
| 992 |
if rightXi > scanline.maxX then |
| 993 |
scanline.maxX := rightXi; |
| 994 |
pd := @buffer[leftXi]; |
| 995 |
if (left <= 0) then |
| 996 |
begin |
| 997 |
pd^ := pd^ + dy {* windDir}; |
| 998 |
end else |
| 999 |
begin |
| 1000 |
q := (left + right) * 0.5 - leftXi; |
| 1001 |
pd^ := pd^ + (1-q) * dy {* windDir}; |
| 1002 |
inc(pd); |
| 1003 |
pd^ := pd^ + q * dy {* windDir}; |
| 1004 |
end; |
| 1005 |
end else |
| 1006 |
begin |
| 1007 |
if leftXi < scanline.minX then |
| 1008 |
scanline.minX := leftXi; |
| 1009 |
if rightXi > scanline.maxX then |
| 1010 |
scanline.maxX := rightXi; |
| 1011 |
pd := @buffer[leftXi]; |
| 1012 |
// left pixel |
| 1013 |
fracX := leftXi + 1 - left; |
| 1014 |
yy := dydx * fracX; |
| 1015 |
q := fracX * yy * 0.5; |
| 1016 |
pd^ := pd^ + q; |
| 1017 |
q := yy - q; |
| 1018 |
inc(pd); |
| 1019 |
// middle pixels |
| 1020 |
for j := leftXi +1 to rightXi -1 do |
| 1021 |
begin |
| 1022 |
pd^ := pd^ + q + dydx * 0.5; |
| 1023 |
q := dydx * 0.5; |
| 1024 |
inc(pd); |
| 1025 |
end; |
| 1026 |
// right pixel |
| 1027 |
fracX := right - rightXi; |
| 1028 |
yy := fracX * dydx; |
| 1029 |
pd^ := pd^ + q + (1 - fracX * 0.5) * yy; |
| 1030 |
inc(pd); |
| 1031 |
// overflow |
| 1032 |
pd^ := pd^ + fracX * 0.5 * yy; |
| 1033 |
end; |
| 1034 |
end; |
| 1035 |
end; |
| 1036 |
// ------------------------------------------------------------------------------ |
| 1037 |
|
| 1038 |
{$RANGECHECKS OFF} // negative array index is used |
| 1039 |
{ CPU register optimized implementations. Every data type must be exactly the one used. } |
| 1040 |
procedure FillByteBufferEvenOdd(byteBuffer: PByte; |
| 1041 |
windingAccum: PDouble; count: nativeint); |
| 1042 |
var |
| 1043 |
accum: double; |
| 1044 |
lastValue: integer; |
| 1045 |
start: nativeint; |
| 1046 |
buf: PByteArray; |
| 1047 |
begin |
| 1048 |
accum := 0; //winding count accumulator |
| 1049 |
lastValue := 0; |
| 1050 |
// Copy byteBuffer to a local variable, so Delphi's 32bit compiler |
| 1051 |
// can put buf into a CPU register. |
| 1052 |
buf := PByteArray(byteBuffer); |
| 1053 |
|
| 1054 |
// Use the negative offset trick to only increment "count" |
| 1055 |
// until it reaches zero. And by offsetting the arrays, "count" |
| 1056 |
// also becomes the index for those. |
| 1057 |
inc(PByte(buf), count); |
| 1058 |
inc(windingAccum, count); |
| 1059 |
count := -count; |
| 1060 |
while count < 0 do |
| 1061 |
begin |
| 1062 |
// lastValue can be used if accum doesn't change |
| 1063 |
if PInt64Array(windingAccum)[count] = 0 then |
| 1064 |
begin |
| 1065 |
start := count; |
| 1066 |
repeat |
| 1067 |
inc(count); |
| 1068 |
until (count = 0) or (PInt64Array(windingAccum)[count] <> 0); |
| 1069 |
FillChar(buf[start], count - start, Byte(lastValue)); |
| 1070 |
if count = 0 then break; |
| 1071 |
end; |
| 1072 |
|
| 1073 |
accum := accum + PDoubleArray(windingAccum)[count]; |
| 1074 |
|
| 1075 |
// EvenOdd |
| 1076 |
lastValue := Trunc(Abs(accum) * 1275) mod 2550; // mul 5 |
| 1077 |
if lastValue > 1275 then |
| 1078 |
lastValue := (2550 - lastValue) shr 2 else // div 4 |
| 1079 |
lastValue := lastValue shr 2; // div 4 |
| 1080 |
if lastValue > 255 then lastValue := 255; |
| 1081 |
|
| 1082 |
buf[count] := Byte(lastValue); |
| 1083 |
PDoubleArray(windingAccum)[count] := 0; |
| 1084 |
inc(count); // walk towards zero |
| 1085 |
end; |
| 1086 |
end; |
| 1087 |
|
| 1088 |
procedure FillByteBufferNonZero(byteBuffer: PByte; |
| 1089 |
windingAccum: PDouble; count: nativeint); |
| 1090 |
var |
| 1091 |
accum: double; |
| 1092 |
lastValue: integer; |
| 1093 |
start: nativeint; |
| 1094 |
buf: PByteArray; |
| 1095 |
begin |
| 1096 |
accum := 0; //winding count accumulator |
| 1097 |
lastValue := 0; |
| 1098 |
// Copy byteBuffer to a local variable, so Delphi's 32bit compiler |
| 1099 |
// can put buf into a CPU register. |
| 1100 |
buf := PByteArray(byteBuffer); |
| 1101 |
|
| 1102 |
// Use the negative offset trick to only increment "count" |
| 1103 |
// until it reaches zero. And by offsetting the arrays, "count" |
| 1104 |
// also becomes the index for those. |
| 1105 |
inc(PByte(buf), count); |
| 1106 |
inc(windingAccum, count); |
| 1107 |
count := -count; |
| 1108 |
while count < 0 do |
| 1109 |
begin |
| 1110 |
// lastValue can be used if accum doesn't change |
| 1111 |
if PInt64Array(windingAccum)[count] = 0 then |
| 1112 |
begin |
| 1113 |
start := count; |
| 1114 |
repeat |
| 1115 |
inc(count); |
| 1116 |
until (count = 0) or (PInt64Array(windingAccum)[count] <> 0); |
| 1117 |
FillChar(buf[start], count - start, Byte(lastValue)); |
| 1118 |
if count = 0 then break; |
| 1119 |
end; |
| 1120 |
|
| 1121 |
accum := accum + PDoubleArray(windingAccum)[count]; |
| 1122 |
|
| 1123 |
// NonZero |
| 1124 |
lastValue := Trunc(Abs(accum) * 318); |
| 1125 |
if lastValue > 255 then lastValue := 255; |
| 1126 |
|
| 1127 |
buf[count] := Byte(lastValue); |
| 1128 |
PDoubleArray(windingAccum)[count] := 0; |
| 1129 |
inc(count); // walk towards zero |
| 1130 |
end; |
| 1131 |
end; |
| 1132 |
|
| 1133 |
procedure FillByteBufferPositive(byteBuffer: PByte; |
| 1134 |
windingAccum: PDouble; count: nativeint); |
| 1135 |
var |
| 1136 |
accum: double; |
| 1137 |
lastValue: integer; |
| 1138 |
start: nativeint; |
| 1139 |
buf: PByteArray; |
| 1140 |
begin |
| 1141 |
accum := 0; //winding count accumulator |
| 1142 |
lastValue := 0; |
| 1143 |
// Copy byteBuffer to a local variable, so Delphi's 32bit compiler |
| 1144 |
// can put buf into a CPU register. |
| 1145 |
buf := PByteArray(byteBuffer); |
| 1146 |
|
| 1147 |
// Use the negative offset trick to only increment "count" |
| 1148 |
// until it reaches zero. And by offsetting the arrays, "count" |
| 1149 |
// also becomes the index for those. |
| 1150 |
inc(PByte(buf), count); |
| 1151 |
inc(windingAccum, count); |
| 1152 |
count := -count; |
| 1153 |
while count < 0 do |
| 1154 |
begin |
| 1155 |
// lastValue can be used if accum doesn't change |
| 1156 |
if PInt64Array(windingAccum)[count] = 0 then |
| 1157 |
begin |
| 1158 |
start := count; |
| 1159 |
repeat |
| 1160 |
inc(count); |
| 1161 |
until (count = 0) or (PInt64Array(windingAccum)[count] <> 0); |
| 1162 |
FillChar(buf[start], count - start, Byte(lastValue)); |
| 1163 |
if count = 0 then break; |
| 1164 |
end; |
| 1165 |
|
| 1166 |
accum := accum + PDoubleArray(windingAccum)[count]; |
| 1167 |
|
| 1168 |
// Positive |
| 1169 |
lastValue := 0; |
| 1170 |
if accum > 0.002 then |
| 1171 |
begin |
| 1172 |
lastValue := Trunc(accum * 318); |
| 1173 |
if lastValue > 255 then lastValue := 255; |
| 1174 |
end; |
| 1175 |
|
| 1176 |
buf[count] := Byte(lastValue); |
| 1177 |
PDoubleArray(windingAccum)[count] := 0; |
| 1178 |
inc(count); // walk towards zero |
| 1179 |
end; |
| 1180 |
end; |
| 1181 |
|
| 1182 |
procedure FillByteBufferNegative(byteBuffer: PByte; |
| 1183 |
windingAccum: PDouble; count: nativeint); |
| 1184 |
var |
| 1185 |
accum: double; |
| 1186 |
lastValue: integer; |
| 1187 |
start: nativeint; |
| 1188 |
buf: PByteArray; |
| 1189 |
begin |
| 1190 |
accum := 0; //winding count accumulator |
| 1191 |
lastValue := 0; |
| 1192 |
// Copy byteBuffer to a local variable, so Delphi's 32bit compiler |
| 1193 |
// can put buf into a CPU register. |
| 1194 |
buf := PByteArray(byteBuffer); |
| 1195 |
|
| 1196 |
// Use the negative offset trick to only increment "count" |
| 1197 |
// until it reaches zero. And by offsetting the arrays, "count" |
| 1198 |
// also becomes the index for those. |
| 1199 |
inc(PByte(buf), count); |
| 1200 |
inc(windingAccum, count); |
| 1201 |
count := -count; |
| 1202 |
while count < 0 do |
| 1203 |
begin |
| 1204 |
// lastValue can be used if accum doesn't change |
| 1205 |
if PInt64Array(windingAccum)[count] = 0 then |
| 1206 |
begin |
| 1207 |
start := count; |
| 1208 |
repeat |
| 1209 |
inc(count); |
| 1210 |
until (count = 0) or (PInt64Array(windingAccum)[count] <> 0); |
| 1211 |
FillChar(buf[start], count - start, Byte(lastValue)); |
| 1212 |
if count = 0 then break; |
| 1213 |
end; |
| 1214 |
|
| 1215 |
accum := accum + PDoubleArray(windingAccum)[count]; |
| 1216 |
|
| 1217 |
// Negative |
| 1218 |
lastValue := 0; |
| 1219 |
if accum < -0.002 then |
| 1220 |
begin |
| 1221 |
lastValue := Trunc(accum * -318); |
| 1222 |
if lastValue > 255 then lastValue := 255; |
| 1223 |
end; |
| 1224 |
|
| 1225 |
buf[count] := Byte(lastValue); |
| 1226 |
PDoubleArray(windingAccum)[count] := 0; |
| 1227 |
inc(count); // walk towards zero |
| 1228 |
end; |
| 1229 |
end; |
| 1230 |
{$IFDEF RANGECHECKS_ENABLED} |
| 1231 |
{$RANGECHECKS ON} |
| 1232 |
{$ENDIF} |
| 1233 |
|
| 1234 |
procedure Rasterize(const paths: TPathsD; const clipRec: TRect; |
| 1235 |
fillRule: TFillRule; renderer: TCustomRenderer); |
| 1236 |
var |
| 1237 |
i, xli,xri, maxW, maxH: integer; |
| 1238 |
clipRec2: TRect; |
| 1239 |
paths2: TPathsD; |
| 1240 |
windingAccum: TArrayOfDouble; |
| 1241 |
byteBuffer: PByteArray; |
| 1242 |
scanlines: TArrayOfScanline; |
| 1243 |
fragments: PFragment; |
| 1244 |
scanline: PScanline; |
| 1245 |
skippedScanlines: integer; |
| 1246 |
skipRenderer: boolean; |
| 1247 |
|
| 1248 |
// FPC generates wrong code if "count" isn't NativeInt |
| 1249 |
FillByteBuffer: procedure(byteBuffer: PByte; windingAccum: PDouble; count: nativeint); |
| 1250 |
begin |
| 1251 |
// See also https://nothings.org/gamedev/rasterize/ |
| 1252 |
if not assigned(renderer) then Exit; |
| 1253 |
renderer.SetClipRect(clipRec); |
| 1254 |
skipRenderer := renderer.SupportsRenderProcSkip; |
| 1255 |
|
| 1256 |
Types.IntersectRect(clipRec2, clipRec, GetBounds(paths)); |
| 1257 |
if IsEmptyRect(clipRec2) then |
| 1258 |
begin |
| 1259 |
if skipRenderer then renderer.RenderProcSkip(clipRec); |
| 1260 |
Exit; |
| 1261 |
end; |
| 1262 |
|
| 1263 |
if (clipRec2.Left = 0) and (clipRec2.Top = 0) then |
| 1264 |
paths2 := paths |
| 1265 |
else |
| 1266 |
paths2 := TranslatePath(paths, -clipRec2.Left, -clipRec2.Top); |
| 1267 |
|
| 1268 |
// Delphi's Round() function is *much* faster than Trunc(), |
| 1269 |
// and even a little faster than Trunc() above (except |
| 1270 |
// when the FastMM4 memory manager is enabled.) |
| 1271 |
fragments := nil; |
| 1272 |
byteBuffer := nil; |
| 1273 |
try |
| 1274 |
RectWidthHeight(clipRec2, maxW, maxH); |
| 1275 |
if maxW <= 0 then Exit; |
| 1276 |
GetMem(byteBuffer, maxW); // no need for dyn. array zero initialize |
| 1277 |
SetLength(scanlines, maxH +1); |
| 1278 |
SetLength(windingAccum, maxW +2); |
| 1279 |
AllocateScanlines(paths2, scanlines, fragments, maxH, maxW-1); |
| 1280 |
InitializeScanlines(paths2, scanlines, fragments, clipRec2); |
| 1281 |
|
| 1282 |
case fillRule of |
| 1283 |
frEvenOdd: |
| 1284 |
FillByteBuffer := FillByteBufferEvenOdd; |
| 1285 |
frNonZero: |
| 1286 |
FillByteBuffer := FillByteBufferNonZero; |
| 1287 |
{$IFDEF REVERSE_ORIENTATION} |
| 1288 |
frPositive: |
| 1289 |
{$ELSE} |
| 1290 |
frNegative: |
| 1291 |
{$ENDIF} |
| 1292 |
FillByteBuffer := FillByteBufferPositive; |
| 1293 |
{$IFDEF REVERSE_ORIENTATION} |
| 1294 |
frNegative: |
| 1295 |
{$ELSE} |
| 1296 |
frPositive: |
| 1297 |
{$ENDIF} |
| 1298 |
FillByteBuffer := FillByteBufferNegative; |
| 1299 |
else |
| 1300 |
if skipRenderer then renderer.RenderProcSkip(clipRec); |
| 1301 |
Exit; |
| 1302 |
end; |
| 1303 |
|
| 1304 |
// Notify the renderer about the parts at the top |
| 1305 |
// that we didn't touch. |
| 1306 |
if skipRenderer and (clipRec2.Top > clipRec.Top) then |
| 1307 |
begin |
| 1308 |
renderer.RenderProcSkip(Rect(clipRec.Left, clipRec.Top, |
| 1309 |
clipRec.Right, clipRec2.Top - 1)); |
| 1310 |
end; |
| 1311 |
|
| 1312 |
skippedScanlines := 0; |
| 1313 |
scanline := @scanlines[0]; |
| 1314 |
for i := 0 to high(scanlines) do |
| 1315 |
begin |
| 1316 |
if scanline.fragCnt = 0 then |
| 1317 |
begin |
| 1318 |
inc(scanline); |
| 1319 |
if skipRenderer then inc(skippedScanlines); |
| 1320 |
Continue; |
| 1321 |
end; |
| 1322 |
|
| 1323 |
// If we have skipped some scanlines, we must notify the renderer. |
| 1324 |
if skipRenderer and (skippedScanlines > 0) then |
| 1325 |
begin |
| 1326 |
renderer.RenderProcSkip(Rect(clipRec.Left, clipRec2.Top + i - skippedScanlines, |
| 1327 |
clipRec.Right, clipRec2.Top + i - 1)); |
| 1328 |
skippedScanlines := 0; |
| 1329 |
end; |
| 1330 |
|
| 1331 |
// process each scanline to fill the winding count accumulation buffer |
| 1332 |
ProcessScanlineFragments(scanline^, fragments, windingAccum); |
| 1333 |
// it's faster to process only the modified sub-array of windingAccum |
| 1334 |
xli := scanline.minX; |
| 1335 |
xri := Min(maxW -1, scanline.maxX +1); |
| 1336 |
|
| 1337 |
// a 25% weighting has been added to the alpha channel to minimize any |
| 1338 |
// background bleed-through where polygons join with a common edge. |
| 1339 |
|
| 1340 |
// FillByteBuffer overwrites every byte in byteBuffer[xli..xri] and also resets |
| 1341 |
// windingAccum[xli..xri] to 0. |
| 1342 |
FillByteBuffer(@byteBuffer[xli], @windingAccum[xli], xri - xli +1); |
| 1343 |
|
| 1344 |
renderer.RenderProc(clipRec2.Left + xli, clipRec2.Left + xri, |
| 1345 |
clipRec2.Top + i, @byteBuffer[xli]); |
| 1346 |
|
| 1347 |
inc(scanline); |
| 1348 |
end; |
| 1349 |
|
| 1350 |
// Notify the renderer about the last skipped scanlines |
| 1351 |
if skipRenderer then |
| 1352 |
begin |
| 1353 |
clipRec2.Bottom := clipRec2.top + High(scanlines) - skippedScanlines; |
| 1354 |
if clipRec2.Bottom < clipRec.Bottom then |
| 1355 |
begin |
| 1356 |
renderer.RenderProcSkip(Rect(clipRec.Left, clipRec2.Bottom + 1, |
| 1357 |
clipRec.Right, clipRec.Bottom)); |
| 1358 |
end; |
| 1359 |
end; |
| 1360 |
finally |
| 1361 |
// cleanup and deallocate memory |
| 1362 |
FreeMem(fragments); |
| 1363 |
FreeMem(byteBuffer); |
| 1364 |
end; |
| 1365 |
end; |
| 1366 |
// ------------------------------------------------------------------------------ |
| 1367 |
|
| 1368 |
procedure Rasterize(img: TImage32; const paths: TPathsD; |
| 1369 |
const clipRec: TRect; fillRule: TFillRule; renderer: TCustomRenderer); |
| 1370 |
begin |
| 1371 |
if renderer.Initialize(img) then |
| 1372 |
begin |
| 1373 |
Rasterize(paths, clipRec, fillRule, renderer); |
| 1374 |
renderer.NotifyChange; |
| 1375 |
end; |
| 1376 |
end; |
| 1377 |
|
| 1378 |
// ------------------------------------------------------------------------------ |
| 1379 |
// TAbstractRenderer |
| 1380 |
// ------------------------------------------------------------------------------ |
| 1381 |
|
| 1382 |
constructor TCustomRenderer.Create; |
| 1383 |
begin |
| 1384 |
inherited; |
| 1385 |
fOpacity := 255; |
| 1386 |
end; |
| 1387 |
// ------------------------------------------------------------------------------ |
| 1388 |
|
| 1389 |
function TCustomRenderer.Initialize(imgBase: Pointer; |
| 1390 |
imgWidth, imgHeight, pixelSize: integer): Boolean; |
| 1391 |
begin |
| 1392 |
fImgBase := imgBase; |
| 1393 |
fImgWidth := ImgWidth; |
| 1394 |
fImgHeight := ImgHeight; |
| 1395 |
fPixelSize := pixelSize; |
| 1396 |
|
| 1397 |
fCurrLinePtr := fImgBase; |
| 1398 |
fCurrY := 0; |
| 1399 |
result := true; |
| 1400 |
end; |
| 1401 |
// ------------------------------------------------------------------------------ |
| 1402 |
|
| 1403 |
procedure TCustomRenderer.NotifyChange; |
| 1404 |
begin |
| 1405 |
if assigned(fChangeProc) then fChangeProc; |
| 1406 |
end; |
| 1407 |
// ------------------------------------------------------------------------------ |
| 1408 |
|
| 1409 |
type THackedImage32 = class(TImage32); //exposes protected Changed method. |
| 1410 |
|
| 1411 |
function TCustomRenderer.Initialize(targetImage: TImage32): Boolean; |
| 1412 |
begin |
| 1413 |
fChangeProc := THackedImage32(targetImage).Changed; |
| 1414 |
with targetImage do |
| 1415 |
result := Initialize(PixelBase, Width, Height, SizeOf(TColor32)); |
| 1416 |
end; |
| 1417 |
// ------------------------------------------------------------------------------ |
| 1418 |
|
| 1419 |
function TCustomRenderer.GetDstPixel(x, y: integer): Pointer; |
| 1420 |
begin |
| 1421 |
if (y <> fCurrY) then |
| 1422 |
begin |
| 1423 |
fCurrY := y; |
| 1424 |
fCurrLinePtr := fImgBase; |
| 1425 |
inc(PByte(fCurrLinePtr), fCurrY * fImgWidth * fPixelSize); |
| 1426 |
end; |
| 1427 |
Result := fCurrLinePtr; |
| 1428 |
inc(PByte(Result), x * fPixelSize); |
| 1429 |
end; |
| 1430 |
// ------------------------------------------------------------------------------ |
| 1431 |
|
| 1432 |
procedure TCustomRenderer.SetClipRect(const clipRect: TRect); |
| 1433 |
begin |
| 1434 |
// default: do nothing |
| 1435 |
end; |
| 1436 |
// ------------------------------------------------------------------------------ |
| 1437 |
|
| 1438 |
procedure TCustomRenderer.RenderProcSkip(const skippedRect: TRect); |
| 1439 |
begin |
| 1440 |
// default: do nothing |
| 1441 |
end; |
| 1442 |
// ------------------------------------------------------------------------------ |
| 1443 |
|
| 1444 |
function TCustomRenderer.SupportsRenderProcSkip: Boolean; |
| 1445 |
begin |
| 1446 |
Result := False; |
| 1447 |
end; |
| 1448 |
|
| 1449 |
// ------------------------------------------------------------------------------ |
| 1450 |
// TCustomColorRenderer |
| 1451 |
// ------------------------------------------------------------------------------ |
| 1452 |
|
| 1453 |
procedure TCustomColorRenderer.SetColor(value: TColor32); |
| 1454 |
begin |
| 1455 |
fColor := value; |
| 1456 |
end; |
| 1457 |
|
| 1458 |
// ------------------------------------------------------------------------------ |
| 1459 |
// TColorRenderer |
| 1460 |
// ------------------------------------------------------------------------------ |
| 1461 |
|
| 1462 |
constructor TColorRenderer.Create(color: TColor32 = clNone32); |
| 1463 |
begin |
| 1464 |
inherited Create; |
| 1465 |
if color <> clNone32 then SetColor(color); |
| 1466 |
end; |
| 1467 |
// ------------------------------------------------------------------------------ |
| 1468 |
|
| 1469 |
function TColorRenderer.Initialize(targetImage: TImage32): Boolean; |
| 1470 |
begin |
| 1471 |
// there's no point rendering if the color is fully transparent |
| 1472 |
result := (fAlpha > 0) and inherited Initialize(targetImage); |
| 1473 |
end; |
| 1474 |
// ------------------------------------------------------------------------------ |
| 1475 |
|
| 1476 |
procedure TColorRenderer.SetColor(value: TColor32); |
| 1477 |
begin |
| 1478 |
fColor := value and $FFFFFF; |
| 1479 |
fAlpha := GetAlpha(value); |
| 1480 |
end; |
| 1481 |
// ------------------------------------------------------------------------------ |
| 1482 |
|
| 1483 |
{$RANGECHECKS OFF} // negative array index usage (Delphi 7-2007 have no pointer math) |
| 1484 |
type |
| 1485 |
// Used to reduce the number of parameters to help the compiler's |
| 1486 |
// optimizer. |
| 1487 |
TRenderProcData = record |
| 1488 |
dst: PColor32Array; |
| 1489 |
alpha: PByteArray; |
| 1490 |
end; |
| 1491 |
|
| 1492 |
function RenderProcBlendToAlpha255(count: nativeint; dstColor: TColor32; |
| 1493 |
var data: TRenderProcData): nativeint; |
| 1494 |
// CPU register optimized |
| 1495 |
var |
| 1496 |
a: byte; |
| 1497 |
dst: PColor32Array; |
| 1498 |
alpha: PByteArray; |
| 1499 |
begin |
| 1500 |
Result := count; |
| 1501 |
dst := data.dst; |
| 1502 |
alpha := data.alpha; |
| 1503 |
|
| 1504 |
a := alpha[Result]; |
| 1505 |
dst[Result] := dstColor; |
| 1506 |
inc(Result); |
| 1507 |
|
| 1508 |
while (Result < 0) and (alpha[Result] = a) do |
| 1509 |
begin |
| 1510 |
dst[Result] := dstColor; |
| 1511 |
inc(Result); |
| 1512 |
end; |
| 1513 |
end; |
| 1514 |
|
| 1515 |
procedure RenderProcBlendToAlpha(dst: PColor32Array; alpha: PByteArray; |
| 1516 |
count: nativeint; color: TColor32; alphaTable: PByteArray); |
| 1517 |
var |
| 1518 |
a: byte; |
| 1519 |
lastDst, dstColor: TColor32; |
| 1520 |
data: TRenderProcData; |
| 1521 |
begin |
| 1522 |
// Use negative offset trick. |
| 1523 |
alpha := @alpha[count]; |
| 1524 |
dst := @dst[count]; |
| 1525 |
count := -count; |
| 1526 |
|
| 1527 |
// store pointers for RenderProcBlendToAlpha255 |
| 1528 |
data.dst := dst; |
| 1529 |
data.alpha := alpha; |
| 1530 |
|
| 1531 |
while count < 0 do |
| 1532 |
begin |
| 1533 |
a := alpha[count]; |
| 1534 |
if a > 1 then |
| 1535 |
begin |
| 1536 |
a := alphaTable[a]; |
| 1537 |
dstColor := (a shl 24) or color; |
| 1538 |
|
| 1539 |
// Special handling for alpha channel 255 (copy dstColor into dst) |
| 1540 |
if a = 255 then |
| 1541 |
count := RenderProcBlendToAlpha255(count, dstColor, data) |
| 1542 |
else |
| 1543 |
begin |
| 1544 |
lastDst := dst[count]; |
| 1545 |
dstColor := BlendToAlpha(lastDst, dstColor); |
| 1546 |
|
| 1547 |
a := alpha[count]; |
| 1548 |
dst[count] := dstColor; |
| 1549 |
inc(count); |
| 1550 |
|
| 1551 |
// if we have the same dst-pixel and the same alpha channel, we can |
| 1552 |
// just copy the already calculated BlendToAlpha color. |
| 1553 |
while (count < 0) and (a = alpha[count]) and (dst[count] = lastDst) do |
| 1554 |
begin |
| 1555 |
dst[count] := dstColor; |
| 1556 |
inc(count); |
| 1557 |
end; |
| 1558 |
end; |
| 1559 |
end |
| 1560 |
else |
| 1561 |
inc(count); |
| 1562 |
end; |
| 1563 |
end; |
| 1564 |
{$IFDEF RANGECHECKS_ENABLED} |
| 1565 |
{$RANGECHECKS ON} |
| 1566 |
{$ENDIF} |
| 1567 |
|
| 1568 |
procedure TColorRenderer.RenderProc(x1, x2, y: integer; alpha: PByte); |
| 1569 |
begin |
| 1570 |
// Help the compiler to get better CPU register allocation. |
| 1571 |
// Without the hidden Self parameter the compiler optimizes |
| 1572 |
// better. |
| 1573 |
RenderProcBlendToAlpha(PColor32Array(GetDstPixel(x1, y)), |
| 1574 |
PByteArray(alpha), x2 - x1 + 1, fColor, |
| 1575 |
PByteArray(@MulTable[fAlpha])); |
| 1576 |
end; |
| 1577 |
|
| 1578 |
// ------------------------------------------------------------------------------ |
| 1579 |
// TAliasedColorRenderer |
| 1580 |
// ------------------------------------------------------------------------------ |
| 1581 |
|
| 1582 |
constructor TAliasedColorRenderer.Create(color: TColor32 = clNone32); |
| 1583 |
begin |
| 1584 |
inherited Create; |
| 1585 |
fColor := color; |
| 1586 |
end; |
| 1587 |
// ------------------------------------------------------------------------------ |
| 1588 |
|
| 1589 |
function TAliasedColorRenderer.Initialize(targetImage: TImage32): Boolean; |
| 1590 |
begin |
| 1591 |
// there's no point rendering if the color is fully transparent |
| 1592 |
result := (GetAlpha(fColor) > 0) and |
| 1593 |
inherited Initialize(targetImage); |
| 1594 |
end; |
| 1595 |
// ------------------------------------------------------------------------------ |
| 1596 |
|
| 1597 |
procedure TAliasedColorRenderer.RenderProc(x1, x2, y: integer; alpha: PByte); |
| 1598 |
var |
| 1599 |
i: integer; |
| 1600 |
dst: PColor32; |
| 1601 |
c: TColor32; |
| 1602 |
begin |
| 1603 |
dst := GetDstPixel(x1,y); |
| 1604 |
c := fColor; // copy fColor to local variable |
| 1605 |
for i := x1 to x2 do |
| 1606 |
begin |
| 1607 |
if Ord(alpha^) > 127 then dst^ := c; //ie no blending |
| 1608 |
inc(dst); inc(alpha); |
| 1609 |
end; |
| 1610 |
end; |
| 1611 |
|
| 1612 |
// ------------------------------------------------------------------------------ |
| 1613 |
// TMaskRenderer |
| 1614 |
// ------------------------------------------------------------------------------ |
| 1615 |
|
| 1616 |
procedure TMaskRenderer.SetClipRect(const clipRect: TRect); |
| 1617 |
begin |
| 1618 |
fClipRect := clipRect; |
| 1619 |
// clipping to the image size |
| 1620 |
if fClipRect.Left < 0 then fClipRect.Left := 0; |
| 1621 |
if fClipRect.Top < 0 then fClipRect.Top := 0; |
| 1622 |
if fClipRect.Right > fImgWidth then fClipRect.Right := fImgWidth; |
| 1623 |
if fClipRect.Bottom > fImgHeight then fClipRect.Bottom := fImgHeight; |
| 1624 |
end; |
| 1625 |
// ------------------------------------------------------------------------------ |
| 1626 |
|
| 1627 |
procedure TMaskRenderer.RenderProc(x1, x2, y: integer; alpha: PByte); |
| 1628 |
var |
| 1629 |
p: PColor32; |
| 1630 |
i: integer; |
| 1631 |
begin |
| 1632 |
// CopyBlend excludes ClipRect.Right/Bottom, so we also |
| 1633 |
// need to exclude it. |
| 1634 |
if (y < fClipRect.Top) or (y >= fClipRect.Bottom) then Exit; |
| 1635 |
if x2 >= fClipRect.Right then x2 := fClipRect.Right - 1; |
| 1636 |
|
| 1637 |
if x1 < fClipRect.Left then |
| 1638 |
begin |
| 1639 |
inc(alpha, fClipRect.Left - x1); |
| 1640 |
x1 := fClipRect.Left; |
| 1641 |
end; |
| 1642 |
|
| 1643 |
p := GetDstPixel(fClipRect.Left, y); |
| 1644 |
|
| 1645 |
// Clear the area before x1 (inside OutsideBounds) |
| 1646 |
FillChar(p^, (x1 - fClipRect.Left) * SizeOf(TColor32), 0); |
| 1647 |
inc(p, x1 - fClipRect.Left); |
| 1648 |
|
| 1649 |
// Fill the area between x1 and x2 |
| 1650 |
for i := x1 to x2 do |
| 1651 |
begin |
| 1652 |
if p^ <> 0 then |
| 1653 |
begin |
| 1654 |
if Ord(alpha^) = 0 then |
| 1655 |
p^ := 0 |
| 1656 |
else if Ord(alpha^) <> 255 then |
| 1657 |
p^ := BlendMask(p^, Ord(alpha^) shl 24); |
| 1658 |
end; |
| 1659 |
inc(p); |
| 1660 |
inc(alpha); |
| 1661 |
end; |
| 1662 |
|
| 1663 |
// Clear the area after x2 (inside OutsideBounds) |
| 1664 |
FillChar(p^, (fClipRect.Right - (x2 + 1)) * SizeOf(TColor32), 0); |
| 1665 |
end; |
| 1666 |
// ------------------------------------------------------------------------------ |
| 1667 |
|
| 1668 |
procedure TMaskRenderer.RenderProcSkip(const skippedRect: TRect); |
| 1669 |
var |
| 1670 |
i, h, w: integer; |
| 1671 |
p: PColor32; |
| 1672 |
r: TRect; |
| 1673 |
begin |
| 1674 |
r := skippedRect; |
| 1675 |
if r.Left < fClipRect.Left then r.Left := fClipRect.Left; |
| 1676 |
if r.Top < fClipRect.Top then r.Top := fClipRect.Top; |
| 1677 |
// CopyBlend excludes ClipRect.Right/Bottom, so we also |
| 1678 |
// need to exclude it. |
| 1679 |
if r.Right >= fClipRect.Right then r.Right := fClipRect.Right - 1; |
| 1680 |
if r.Bottom >= fClipRect.Bottom then r.Bottom := fClipRect.Bottom - 1; |
| 1681 |
|
| 1682 |
if r.Right < r.Left then Exit; |
| 1683 |
if r.Bottom < r.Top then Exit; |
| 1684 |
|
| 1685 |
w := r.Right - r.Left + 1; |
| 1686 |
h := r.Bottom - r.Top + 1; |
| 1687 |
p := GetDstPixel(r.Left, r.Top); |
| 1688 |
if w = fImgWidth then |
| 1689 |
FillChar(p^, w * h * SizeOf(TColor32), 0) |
| 1690 |
else |
| 1691 |
begin |
| 1692 |
for i := 1 to h do |
| 1693 |
begin |
| 1694 |
FillChar(p^, w * SizeOf(TColor32), 0); |
| 1695 |
inc(p, fImgWidth); |
| 1696 |
end; |
| 1697 |
end; |
| 1698 |
end; |
| 1699 |
|
| 1700 |
// ------------------------------------------------------------------------------ |
| 1701 |
function TMaskRenderer.SupportsRenderProcSkip: Boolean; |
| 1702 |
begin |
| 1703 |
Result := True; |
| 1704 |
end; |
| 1705 |
|
| 1706 |
// ------------------------------------------------------------------------------ |
| 1707 |
// TCustomRendererCache |
| 1708 |
// ------------------------------------------------------------------------------ |
| 1709 |
|
| 1710 |
constructor TCustomRendererCache.Create; |
| 1711 |
begin |
| 1712 |
inherited Create; |
| 1713 |
fColorRenderer := TColorRenderer.Create; |
| 1714 |
fAliasedColorRenderer := TAliasedColorRenderer.Create; |
| 1715 |
fMaskRenderer := TMaskRenderer.Create; |
| 1716 |
end; |
| 1717 |
// ------------------------------------------------------------------------------ |
| 1718 |
|
| 1719 |
destructor TCustomRendererCache.Destroy; |
| 1720 |
begin |
| 1721 |
fColorRenderer.Free; |
| 1722 |
fAliasedColorRenderer.Free; |
| 1723 |
fMaskRenderer.Free; |
| 1724 |
end; |
| 1725 |
// ------------------------------------------------------------------------------ |
| 1726 |
|
| 1727 |
function TCustomRendererCache.GetColorRenderer(color: TColor32): TColorRenderer; |
| 1728 |
begin |
| 1729 |
Result := fColorRenderer; |
| 1730 |
Result.SetColor(color); |
| 1731 |
end; |
| 1732 |
|
| 1733 |
// ------------------------------------------------------------------------------ |
| 1734 |
// TBrushImageRenderer |
| 1735 |
// ------------------------------------------------------------------------------ |
| 1736 |
|
| 1737 |
constructor TImageRenderer.Create(tileFillStyle: TTileFillStyle; |
| 1738 |
brushImage: TImage32); |
| 1739 |
begin |
| 1740 |
inherited Create; |
| 1741 |
fImage := TImage32.Create(brushImage); |
| 1742 |
SetTileFillStyle(tileFillStyle); |
| 1743 |
end; |
| 1744 |
// ------------------------------------------------------------------------------ |
| 1745 |
|
| 1746 |
destructor TImageRenderer.Destroy; |
| 1747 |
begin |
| 1748 |
fImage.Free; |
| 1749 |
inherited; |
| 1750 |
end; |
| 1751 |
// ------------------------------------------------------------------------------ |
| 1752 |
|
| 1753 |
procedure TImageRenderer.SetTileFillStyle(value: TTileFillStyle); |
| 1754 |
begin |
| 1755 |
case value of |
| 1756 |
tfsRepeat: fBoundsProc := RepeatQ; |
| 1757 |
tfsMirrorHorz: fBoundsProc := MirrorQ; |
| 1758 |
tfsMirrorVert: fBoundsProc := RepeatQ; |
| 1759 |
tfsRotate180 : fBoundsProc := MirrorQ; |
| 1760 |
end; |
| 1761 |
fMirrorY := value in [tfsMirrorVert, tfsRotate180]; |
| 1762 |
end; |
| 1763 |
// ------------------------------------------------------------------------------ |
| 1764 |
|
| 1765 |
function TImageRenderer.Initialize(targetImage: TImage32): Boolean; |
| 1766 |
begin |
| 1767 |
result := inherited Initialize(targetImage) and (not fImage.IsEmpty); |
| 1768 |
if not result then Exit; |
| 1769 |
fLastYY := 0; |
| 1770 |
fBrushPixel := PARGB(fImage.PixelBase); |
| 1771 |
end; |
| 1772 |
// ------------------------------------------------------------------------------ |
| 1773 |
|
| 1774 |
procedure TImageRenderer.RenderProc(x1, x2, y: integer; alpha: PByte); |
| 1775 |
var |
| 1776 |
i: integer; |
| 1777 |
pDst: PColor32; |
| 1778 |
pImg: PColor32; |
| 1779 |
opacityTable: PByteArray; |
| 1780 |
begin |
| 1781 |
pDst := GetDstPixel(x1,y); |
| 1782 |
dec(x1, fOffset.X); |
| 1783 |
dec(x2, fOffset.X); |
| 1784 |
dec(y, fOffset.Y); |
| 1785 |
pImg := GetFirstBrushPixel(x1, y); |
| 1786 |
if Opacity < 255 then |
| 1787 |
begin |
| 1788 |
opacityTable := PByteArray(@MulTable[Opacity]); |
| 1789 |
for i := x1 to x2 do |
| 1790 |
begin |
| 1791 |
pDst^ := BlendToAlpha3(pDst^, pImg^, opacityTable[Ord(alpha^)]); |
| 1792 |
inc(pDst); inc(alpha); |
| 1793 |
pImg := PColor32(GetPixel(fBrushPixel, fBoundsProc(i, fImage.Width))); |
| 1794 |
end; |
| 1795 |
end else |
| 1796 |
for i := x1 to x2 do |
| 1797 |
begin |
| 1798 |
pDst^ := BlendToAlpha3(pDst^, pImg^, Ord(alpha^)); |
| 1799 |
inc(pDst); inc(alpha); |
| 1800 |
pImg := PColor32(GetPixel(fBrushPixel, fBoundsProc(i, fImage.Width))); |
| 1801 |
end; |
| 1802 |
end; |
| 1803 |
// ------------------------------------------------------------------------------ |
| 1804 |
|
| 1805 |
function TImageRenderer.GetFirstBrushPixel(x, y: integer): PColor32; |
| 1806 |
begin |
| 1807 |
if fMirrorY then |
| 1808 |
y := MirrorQ(y, fImage.Height) else |
| 1809 |
y := RepeatQ(y, fImage.Height); |
| 1810 |
if y <> fLastYY then |
| 1811 |
begin |
| 1812 |
fBrushPixel := PARGB(fImage.PixelRow[y]); |
| 1813 |
fLastYY := y; |
| 1814 |
end; |
| 1815 |
x := fBoundsProc(x, fImage.Width); |
| 1816 |
result := PColor32(GetPixel(fBrushPixel, x)); |
| 1817 |
end; |
| 1818 |
|
| 1819 |
// ------------------------------------------------------------------------------ |
| 1820 |
// TGradientRenderer |
| 1821 |
// ------------------------------------------------------------------------------ |
| 1822 |
|
| 1823 |
constructor TCustomGradientRenderer.Create; |
| 1824 |
begin |
| 1825 |
inherited Create; |
| 1826 |
fBoundsProc := ClampQ; //default proc |
| 1827 |
end; |
| 1828 |
// ------------------------------------------------------------------------------ |
| 1829 |
|
| 1830 |
procedure TCustomGradientRenderer.Clear; |
| 1831 |
begin |
| 1832 |
fGradientColors := nil; |
| 1833 |
fColors := nil; |
| 1834 |
end; |
| 1835 |
// ------------------------------------------------------------------------------ |
| 1836 |
|
| 1837 |
procedure TCustomGradientRenderer.SetGradientFillStyle(value: TGradientFillStyle); |
| 1838 |
begin |
| 1839 |
case value of |
| 1840 |
gfsClamp: fBoundsProc := ClampQ; |
| 1841 |
gfsMirror: fBoundsProc := MirrorQ; |
| 1842 |
else fBoundsProc := RepeatQ; |
| 1843 |
end; |
| 1844 |
end; |
| 1845 |
// ------------------------------------------------------------------------------ |
| 1846 |
|
| 1847 |
procedure TCustomGradientRenderer.SetParameters(startColor, endColor: TColor32; |
| 1848 |
gradFillStyle: TGradientFillStyle = gfsClamp); |
| 1849 |
begin |
| 1850 |
SetGradientFillStyle(gradFillStyle); |
| 1851 |
// reset gradient colors if perviously set |
| 1852 |
SetLength(fGradientColors, 2); |
| 1853 |
fGradientColors[0].offset := 0; |
| 1854 |
fGradientColors[0].color := startColor; |
| 1855 |
fGradientColors[1].offset := 1; |
| 1856 |
fGradientColors[1].color := endColor; |
| 1857 |
end; |
| 1858 |
// ------------------------------------------------------------------------------ |
| 1859 |
|
| 1860 |
procedure TCustomGradientRenderer.InsertColorStop(offsetFrac: double; color: TColor32); |
| 1861 |
var |
| 1862 |
i, len: integer; |
| 1863 |
gradColor: TGradientColor; |
| 1864 |
begin |
| 1865 |
len := Length(fGradientColors); |
| 1866 |
// colorstops can only be inserted after calling SetParameters |
| 1867 |
if len = 0 then Exit; |
| 1868 |
|
| 1869 |
if offsetFrac < 0 then offsetFrac := 0 |
| 1870 |
else if offsetFrac > 1 then offsetFrac := 1; |
| 1871 |
|
| 1872 |
if offsetFrac = 0 then |
| 1873 |
begin |
| 1874 |
fGradientColors[0].color := color; |
| 1875 |
Exit; |
| 1876 |
end |
| 1877 |
else if offsetFrac = 1 then |
| 1878 |
begin |
| 1879 |
fGradientColors[len -1].color := color; |
| 1880 |
Exit; |
| 1881 |
end; |
| 1882 |
gradColor.offset := offsetFrac; |
| 1883 |
gradColor.color := color; |
| 1884 |
|
| 1885 |
i := 1; |
| 1886 |
while (i < len-1) and |
| 1887 |
(fGradientColors[i].offset <= offsetFrac) do inc(i); |
| 1888 |
SetLength(fGradientColors, len +1); |
| 1889 |
Move(fGradientColors[i], |
| 1890 |
fGradientColors[i+1], (len -i) * SizeOf(TGradientColor)); |
| 1891 |
fGradientColors[i] := gradColor; |
| 1892 |
end; |
| 1893 |
|
| 1894 |
// ------------------------------------------------------------------------------ |
| 1895 |
// TLinearGradientRenderer |
| 1896 |
// ------------------------------------------------------------------------------ |
| 1897 |
|
| 1898 |
procedure TLinearGradientRenderer.SetParameters(const startPt, endPt: TPointD; |
| 1899 |
startColor, endColor: TColor32; gradFillStyle: TGradientFillStyle); |
| 1900 |
begin |
| 1901 |
inherited SetParameters(startColor, endColor, gradFillStyle); |
| 1902 |
fStartPt := startPt; |
| 1903 |
fEndPt := endPt; |
| 1904 |
end; |
| 1905 |
// ------------------------------------------------------------------------------ |
| 1906 |
|
| 1907 |
function TLinearGradientRenderer.Initialize(targetImage: TImage32): Boolean; |
| 1908 |
var |
| 1909 |
i: integer; |
| 1910 |
dx,dy, dxdy,dydx: double; |
| 1911 |
begin |
| 1912 |
result := inherited Initialize(targetImage) and assigned(fGradientColors); |
| 1913 |
if not result then Exit; |
| 1914 |
|
| 1915 |
if abs(fEndPt.Y - fStartPt.Y) > abs(fEndPt.X - fStartPt.X) then |
| 1916 |
begin |
| 1917 |
// gradient > 45 degrees |
| 1918 |
if (fEndPt.Y < fStartPt.Y) then |
| 1919 |
begin |
| 1920 |
ReverseColors(fGradientColors); |
| 1921 |
SwapPoints(fStartPt, fEndPt); |
| 1922 |
end; |
| 1923 |
fIsVert := true; |
| 1924 |
dx := (fEndPt.X - fStartPt.X); |
| 1925 |
dy := (fEndPt.Y - fStartPt.Y); |
| 1926 |
dxdy := dx/dy; |
| 1927 |
|
| 1928 |
fColorsCnt := Ceil(dy + dxdy * (fEndPt.X - fStartPt.X)); |
| 1929 |
MakeColorGradient(fGradientColors, fColorsCnt, fColors); |
| 1930 |
// get a list of perpendicular offsets for each |
| 1931 |
NewIntegerArray(fPerpendicOffsets, ImgWidth, True); |
| 1932 |
// from an imaginary line that's through fStartPt and perpendicular to |
| 1933 |
// the gradient line, get a list of Y offsets for each X in image width |
| 1934 |
for i := 0 to ImgWidth -1 do |
| 1935 |
fPerpendicOffsets[i] := Round(dxdy * (fStartPt.X - i) + fStartPt.Y); |
| 1936 |
end |
| 1937 |
else //gradient <= 45 degrees |
| 1938 |
begin |
| 1939 |
if (fEndPt.X = fStartPt.X) then |
| 1940 |
begin |
| 1941 |
Result := false; |
| 1942 |
Exit; |
| 1943 |
end; |
| 1944 |
if (fEndPt.X < fStartPt.X) then |
| 1945 |
begin |
| 1946 |
ReverseColors(fGradientColors); |
| 1947 |
SwapPoints(fStartPt, fEndPt); |
| 1948 |
end; |
| 1949 |
fIsVert := false; |
| 1950 |
dx := (fEndPt.X - fStartPt.X); |
| 1951 |
dy := (fEndPt.Y - fStartPt.Y); |
| 1952 |
dydx := dy/dx; //perpendicular slope |
| 1953 |
|
| 1954 |
fColorsCnt := Ceil(dx + dydx * (fEndPt.Y - fStartPt.Y)); |
| 1955 |
MakeColorGradient(fGradientColors, fColorsCnt, fColors); |
| 1956 |
NewIntegerArray(fPerpendicOffsets, ImgHeight, True); |
| 1957 |
// from an imaginary line that's through fStartPt and perpendicular to |
| 1958 |
// the gradient line, get a list of X offsets for each Y in image height |
| 1959 |
for i := 0 to ImgHeight -1 do |
| 1960 |
fPerpendicOffsets[i] := Round(dydx * (fStartPt.Y - i) + fStartPt.X); |
| 1961 |
end; |
| 1962 |
end; |
| 1963 |
// ------------------------------------------------------------------------------ |
| 1964 |
|
| 1965 |
procedure TLinearGradientRenderer.RenderProc(x1, x2, y: integer; alpha: PByte); |
| 1966 |
var |
| 1967 |
i, colorsCnt: integer; |
| 1968 |
pDst: PColor32; |
| 1969 |
color: TColor32; |
| 1970 |
boundsProc: TBoundsProc; |
| 1971 |
offset: Integer; |
| 1972 |
colors: PColor32Array; |
| 1973 |
perpendicOffsets: PIntegerArray; |
| 1974 |
opacityTable: PByteArray; |
| 1975 |
begin |
| 1976 |
pDst := GetDstPixel(x1,y); |
| 1977 |
// optimize self fields access |
| 1978 |
colorsCnt := fColorsCnt; |
| 1979 |
colors := @fColors[0]; |
| 1980 |
boundsProc := fBoundsProc; |
| 1981 |
if fIsVert then |
| 1982 |
begin |
| 1983 |
perpendicOffsets := @fPerpendicOffsets[0]; // optimize self field access |
| 1984 |
if Opacity < 255 then |
| 1985 |
begin |
| 1986 |
opacityTable := PByteArray(@MulTable[Opacity]); |
| 1987 |
for i := x1 to x2 do |
| 1988 |
begin |
| 1989 |
// when fIsVert = true, fPerpendicOffsets is an array of Y for each X |
| 1990 |
color := colors[boundsProc(y - perpendicOffsets[i], colorsCnt)]; |
| 1991 |
pDst^ := BlendToAlpha3(pDst^, color, opacityTable[Ord(alpha^)]); |
| 1992 |
inc(pDst); inc(alpha); |
| 1993 |
end; |
| 1994 |
end else |
| 1995 |
begin |
| 1996 |
for i := x1 to x2 do |
| 1997 |
begin |
| 1998 |
// when fIsVert = true, fPerpendicOffsets is an array of Y for each X |
| 1999 |
color := colors[boundsProc(y - perpendicOffsets[i], colorsCnt)]; |
| 2000 |
pDst^ := BlendToAlpha3(pDst^, color, Ord(alpha^)); |
| 2001 |
inc(pDst); inc(alpha); |
| 2002 |
end; |
| 2003 |
end; |
| 2004 |
end |
| 2005 |
else |
| 2006 |
begin |
| 2007 |
// when fIsVert = false, fPerpendicOffsets is an array of X for each Y |
| 2008 |
offset := fPerpendicOffsets[y]; |
| 2009 |
if Opacity < 255 then |
| 2010 |
begin |
| 2011 |
opacityTable := PByteArray(@MulTable[Opacity]); |
| 2012 |
for i := x1 to x2 do |
| 2013 |
begin |
| 2014 |
color := colors[boundsProc(i - offset, colorsCnt)]; |
| 2015 |
pDst^ := BlendToAlpha3(pDst^, color, opacityTable[Ord(alpha^)]); |
| 2016 |
inc(pDst); inc(alpha); |
| 2017 |
end; |
| 2018 |
end else |
| 2019 |
begin |
| 2020 |
for i := x1 to x2 do |
| 2021 |
begin |
| 2022 |
color := colors[boundsProc(i - offset, colorsCnt)]; |
| 2023 |
pDst^ := BlendToAlpha3(pDst^, color, Ord(alpha^)); |
| 2024 |
inc(pDst); inc(alpha); |
| 2025 |
end; |
| 2026 |
end; |
| 2027 |
end; |
| 2028 |
end; |
| 2029 |
|
| 2030 |
// ------------------------------------------------------------------------------ |
| 2031 |
// TRadialGradientRenderer |
| 2032 |
// ------------------------------------------------------------------------------ |
| 2033 |
|
| 2034 |
function TRadialGradientRenderer.Initialize(targetImage: TImage32): Boolean; |
| 2035 |
begin |
| 2036 |
result := inherited Initialize(targetImage) and (fColorsCnt > 1); |
| 2037 |
if result then |
| 2038 |
MakeColorGradient(fGradientColors, fColorsCnt, fColors); |
| 2039 |
end; |
| 2040 |
// ------------------------------------------------------------------------------ |
| 2041 |
|
| 2042 |
procedure TRadialGradientRenderer.SetParameters(const focalRect: TRect; |
| 2043 |
innerColor, outerColor: TColor32; |
| 2044 |
gradientFillStyle: TGradientFillStyle); |
| 2045 |
var |
| 2046 |
w,h: integer; |
| 2047 |
radX,radY: double; |
| 2048 |
begin |
| 2049 |
inherited SetParameters(innerColor, outerColor, gradientFillStyle); |
| 2050 |
fColorsCnt := 0; |
| 2051 |
if IsEmptyRect(focalRect) then Exit; |
| 2052 |
|
| 2053 |
fCenterPt.X := (focalRect.Left + focalRect.Right) * 0.5; |
| 2054 |
fCenterPt.Y := (focalRect.Top + focalRect.Bottom) * 0.5; |
| 2055 |
RectWidthHeight(focalRect, w, h); |
| 2056 |
radX := w * 0.5; |
| 2057 |
radY := h * 0.5; |
| 2058 |
if radX >= radY then |
| 2059 |
begin |
| 2060 |
fScaleX := 1; |
| 2061 |
fScaleY := radX/radY; |
| 2062 |
fColorsCnt := Ceil(radX) +1; |
| 2063 |
end else |
| 2064 |
begin |
| 2065 |
fScaleX := radY/radX; |
| 2066 |
fScaleY := 1; |
| 2067 |
fColorsCnt := Ceil(radY) +1; |
| 2068 |
end; |
| 2069 |
end; |
| 2070 |
// ------------------------------------------------------------------------------ |
| 2071 |
|
| 2072 |
procedure TRadialGradientRenderer.RenderProc(x1, x2, y: integer; alpha: PByte); |
| 2073 |
var |
| 2074 |
i: integer; |
| 2075 |
dist: double; |
| 2076 |
color: TColor32; |
| 2077 |
pDst: PColor32; |
| 2078 |
opacityTable: PByteArray; |
| 2079 |
begin |
| 2080 |
pDst := GetDstPixel(x1,y); |
| 2081 |
if Opacity < 255 then |
| 2082 |
begin |
| 2083 |
opacityTable := PByteArray(@MulTable[Opacity]); |
| 2084 |
for i := x1 to x2 do |
| 2085 |
begin |
| 2086 |
dist := Hypot((y - fCenterPt.Y) *fScaleY, (i - fCenterPt.X) *fScaleX); |
| 2087 |
color := fColors[fBoundsProc(Trunc(dist), fColorsCnt)]; |
| 2088 |
pDst^ := BlendToAlpha3(pDst^, color, opacityTable[Ord(alpha^)]); |
| 2089 |
inc(pDst); inc(alpha); |
| 2090 |
end; |
| 2091 |
end else |
| 2092 |
begin |
| 2093 |
for i := x1 to x2 do |
| 2094 |
begin |
| 2095 |
dist := Hypot((y - fCenterPt.Y) *fScaleY, (i - fCenterPt.X) *fScaleX); |
| 2096 |
color := fColors[fBoundsProc(Trunc(dist), fColorsCnt)]; |
| 2097 |
pDst^ := BlendToAlpha3(pDst^, color, Ord(alpha^)); |
| 2098 |
inc(pDst); inc(alpha); |
| 2099 |
end; |
| 2100 |
end; |
| 2101 |
end; |
| 2102 |
|
| 2103 |
// ------------------------------------------------------------------------------ |
| 2104 |
// TSvgRadialGradientRenderer |
| 2105 |
// ------------------------------------------------------------------------------ |
| 2106 |
|
| 2107 |
function TSvgRadialGradientRenderer.Initialize(targetImage: TImage32): Boolean; |
| 2108 |
begin |
| 2109 |
result := inherited Initialize(targetImage) and (fColorsCnt > 1); |
| 2110 |
if result then |
| 2111 |
MakeColorGradient(fGradientColors, fColorsCnt, fColors); |
| 2112 |
end; |
| 2113 |
// ------------------------------------------------------------------------------ |
| 2114 |
|
| 2115 |
procedure TSvgRadialGradientRenderer.SetParameters(const ellipseRect: TRect; |
| 2116 |
const focus: TPoint; innerColor, outerColor: TColor32; |
| 2117 |
gradientFillStyle: TGradientFillStyle = gfsClamp); |
| 2118 |
var |
| 2119 |
w, h : integer; |
| 2120 |
begin |
| 2121 |
inherited SetParameters(innerColor, outerColor); |
| 2122 |
case gradientFillStyle of |
| 2123 |
gfsMirror: fBoundsProcD := MirrorD; |
| 2124 |
gfsRepeat: fBoundsProcD := RepeatD; |
| 2125 |
else fBoundsProcD := ClampD; |
| 2126 |
end; |
| 2127 |
|
| 2128 |
fColorsCnt := 0; |
| 2129 |
if IsEmptyRect(ellipseRect) then Exit; |
| 2130 |
|
| 2131 |
fCenterPt := RectD(ellipseRect).MidPoint; |
| 2132 |
RectWidthHeight(ellipseRect, w, h); |
| 2133 |
fA := w * 0.5; |
| 2134 |
fB := h * 0.5; |
| 2135 |
|
| 2136 |
fFocusPt.X := focus.X - fCenterPt.X; |
| 2137 |
fFocusPt.Y := focus.Y - fCenterPt.Y; |
| 2138 |
fColorsCnt := Ceil(Hypot(fA*2, fB*2)) +1; |
| 2139 |
fAA := fA * fA; |
| 2140 |
fBB := fB * fB; |
| 2141 |
end; |
| 2142 |
// ------------------------------------------------------------------------------ |
| 2143 |
|
| 2144 |
procedure TSvgRadialGradientRenderer.RenderProc(x1, x2, y: integer; alpha: PByte); |
| 2145 |
var |
| 2146 |
i: integer; |
| 2147 |
q,qq, m,c, qa,qb,qc,qs: double; |
| 2148 |
dist, dist2: double; |
| 2149 |
color: TColor32; |
| 2150 |
pDst: PColor32; |
| 2151 |
pt, ellipsePt: TPointD; |
| 2152 |
opacityTable: PByteArray; |
| 2153 |
begin |
| 2154 |
opacityTable := PByteArray(@MulTable[Opacity]); |
| 2155 |
// get the left-most pixel to render |
| 2156 |
pDst := GetDstPixel(x1,y); |
| 2157 |
pt.X := x1 - fCenterPt.X; pt.Y := y - fCenterPt.Y; |
| 2158 |
for i := x1 to x2 do |
| 2159 |
begin |
| 2160 |
// equation of ellipse = (x*x)/aa + (y*y)/bb = 1 |
| 2161 |
// equation of line = y = mx + c; |
| 2162 |
if (pt.X = fFocusPt.X) then //vertical line |
| 2163 |
begin |
| 2164 |
// let x = pt.X, then y*y = b*b(1 - Sqr(pt.X)/aa) |
| 2165 |
qq := (1 - Sqr(pt.X)/fAA); |
| 2166 |
if (qq > 1) then qq := 1 |
| 2167 |
else if (qq < 0) then qq := 0; |
| 2168 |
q := Sqrt(fBB*qq); |
| 2169 |
ellipsePt.X := pt.X; |
| 2170 |
if pt.Y >= fFocusPt.Y then |
| 2171 |
ellipsePt.Y := q else |
| 2172 |
ellipsePt.Y := -q; |
| 2173 |
dist := abs(pt.Y - fFocusPt.Y); |
| 2174 |
dist2 := abs(ellipsePt.Y - fFocusPt.Y); |
| 2175 |
if dist2 = 0 then |
| 2176 |
q := 1 else |
| 2177 |
q := dist/ dist2; |
| 2178 |
end else |
| 2179 |
begin |
| 2180 |
// using simultaneous equations and substitution |
| 2181 |
// given y = mx + c |
| 2182 |
m := (pt.Y - fFocusPt.Y)/(pt.X - fFocusPt.X); |
| 2183 |
c := pt.Y - m * pt.X; |
| 2184 |
// given (x*x)/aa + (y*y)/bb = 1 |
| 2185 |
// (x*x)/aa*bb + (y*y) = bb |
| 2186 |
// bb/aa *(x*x) + Sqr(m*x +c) = bb |
| 2187 |
// bb/aa *(x*x) + (m*m)*(x*x) + 2*m*x*c +c*c = b*b |
| 2188 |
// (bb/aa +(m*m)) *(x*x) + 2*m*c*(x) + (c*c) - bb = 0 |
| 2189 |
// solving quadratic equation |
| 2190 |
qa := (fBB/fAA +(m*m)); |
| 2191 |
qb := 2*m*c; |
| 2192 |
qc := (c*c) - fBB; |
| 2193 |
qs := (qb*qb) - 4*qa*qc; |
| 2194 |
if qs >= 0 then |
| 2195 |
begin |
| 2196 |
qs := Sqrt(qs); |
| 2197 |
if pt.X <= fFocusPt.X then |
| 2198 |
ellipsePt.X := (-qb -qs)/(2 * qa) else |
| 2199 |
ellipsePt.X := (-qb +qs)/(2 * qa); |
| 2200 |
ellipsePt.Y := m * ellipsePt.X + c; |
| 2201 |
|
| 2202 |
// Use sqr'ed distances (Sqrt(a^2+b^2)/Sqrt(x^2+y^2) => Sqrt((a^2+b^2)/(x^2+y^2)) |
| 2203 |
dist := Sqr(pt.X - fFocusPt.X) + Sqr(pt.Y - fFocusPt.Y); |
| 2204 |
dist2 := Sqr(ellipsePt.X - fFocusPt.X) + Sqr(ellipsePt.Y - fFocusPt.Y); |
| 2205 |
if dist2 = 0 then |
| 2206 |
q := 1 else |
| 2207 |
q := Sqrt(dist/dist2); |
| 2208 |
end else |
| 2209 |
q := 1; //shouldn't happen :) |
| 2210 |
end; |
| 2211 |
color := fColors[fBoundsProcD(Abs(q), fColorsCnt)]; |
| 2212 |
pDst^ := BlendToAlpha3(pDst^, color, opacityTable[Ord(alpha^)]); |
| 2213 |
inc(pDst); pt.X := pt.X + 1; inc(alpha); |
| 2214 |
end; |
| 2215 |
end; |
| 2216 |
|
| 2217 |
// ------------------------------------------------------------------------------ |
| 2218 |
// TEraseRenderer |
| 2219 |
// ------------------------------------------------------------------------------ |
| 2220 |
|
| 2221 |
procedure TEraseRenderer.RenderProc(x1, x2, y: integer; alpha: PByte); |
| 2222 |
var |
| 2223 |
i: integer; |
| 2224 |
dst: PARGB; |
| 2225 |
begin |
| 2226 |
dst := PARGB(GetDstPixel(x1,y)); |
| 2227 |
for i := x1 to x2 do |
| 2228 |
begin |
| 2229 |
{$IFDEF PBYTE} |
| 2230 |
dst.A := MulTable[dst.A, not alpha^]; |
| 2231 |
{$ELSE} |
| 2232 |
dst.A := MulTable[dst.A, not Ord(alpha^)]; |
| 2233 |
{$ENDIF} |
| 2234 |
inc(dst); inc(alpha); |
| 2235 |
end; |
| 2236 |
end; |
| 2237 |
|
| 2238 |
// ------------------------------------------------------------------------------ |
| 2239 |
// TInverseRenderer |
| 2240 |
// ------------------------------------------------------------------------------ |
| 2241 |
|
| 2242 |
procedure TInverseRenderer.RenderProc(x1, x2, y: integer; alpha: PByte); |
| 2243 |
var |
| 2244 |
i: integer; |
| 2245 |
dst: PARGB; |
| 2246 |
c: TARGB; |
| 2247 |
begin |
| 2248 |
dst := PARGB(GetDstPixel(x1,y)); |
| 2249 |
for i := x1 to x2 do |
| 2250 |
begin |
| 2251 |
c.Color := not dst.Color; |
| 2252 |
c.A := MulTable[dst.A, Ord(alpha^)]; |
| 2253 |
dst.Color := BlendToAlpha(dst.Color, c.Color); |
| 2254 |
inc(dst); inc(alpha); |
| 2255 |
end; |
| 2256 |
end; |
| 2257 |
|
| 2258 |
// ------------------------------------------------------------------------------ |
| 2259 |
|
| 2260 |
procedure TBarycentricRenderer.SetParameters(const a, b, c: TPointD; |
| 2261 |
c1, c2, c3: TColor32); |
| 2262 |
begin |
| 2263 |
|
| 2264 |
self.a := a; |
| 2265 |
self.c1.Color := c1; |
| 2266 |
self.c2.Color := c2; |
| 2267 |
self.c3.Color := c3; |
| 2268 |
|
| 2269 |
v0.X := b.X - a.X; |
| 2270 |
v0.Y := b.Y - a.Y; |
| 2271 |
v1.X := c.X - a.X; |
| 2272 |
v1.Y := c.Y - a.Y; |
| 2273 |
d00 := (v0.X * v0.X + v0.Y * v0.Y); |
| 2274 |
d01 := (v0.X * v1.X + v0.Y * v1.Y); |
| 2275 |
d11 := (v1.X * v1.X + v1.Y * v1.Y); |
| 2276 |
invDenom := 1/(d00 * d11 - d01 * d01); |
| 2277 |
end; |
| 2278 |
// ------------------------------------------------------------------------------ |
| 2279 |
|
| 2280 |
function TBarycentricRenderer.GetColor(const pt: TPointD): TColor32; |
| 2281 |
var |
| 2282 |
v2: TPointD; |
| 2283 |
d20, d21, v, w, u: Double; |
| 2284 |
res: TARGB absolute Result; |
| 2285 |
begin |
| 2286 |
Result := 0; |
| 2287 |
v2.X := pt.X - a.X; |
| 2288 |
v2.Y := pt.Y - a.Y; |
| 2289 |
d20 := (v2.X * v0.X + v2.Y * v0.Y); |
| 2290 |
d21 := (v2.X * v1.X + v2.Y * v1.Y); |
| 2291 |
|
| 2292 |
v := (d11 * d20 - d01 * d21) * invDenom; |
| 2293 |
w := (d00 * d21 - d01 * d20) * invDenom; |
| 2294 |
u := 1.0 - v - w; |
| 2295 |
|
| 2296 |
Res.A := ClampByte(c1.A * u + c2.A * v + c3.A * w); |
| 2297 |
Res.R := ClampByte(c1.R * u + c2.R * v + c3.R * w); |
| 2298 |
Res.G := ClampByte(c1.G * u + c2.G * v + c3.G * w); |
| 2299 |
Res.B := ClampByte(c1.B * u + c2.B * v + c3.B * w); |
| 2300 |
end; |
| 2301 |
// ------------------------------------------------------------------------------ |
| 2302 |
|
| 2303 |
procedure TBarycentricRenderer.RenderProc(x1, x2, y: integer; alpha: PByte); |
| 2304 |
var |
| 2305 |
x: integer; |
| 2306 |
p: PARGB; |
| 2307 |
c: TARGB; |
| 2308 |
opacityTable: PByteArray; |
| 2309 |
begin |
| 2310 |
p := PARGB(fImgBase); |
| 2311 |
inc(p, y * ImgWidth + x1); |
| 2312 |
if Opacity < 255 then |
| 2313 |
begin |
| 2314 |
opacityTable := PByteArray(@MulTable[Opacity]); |
| 2315 |
for x := x1 to x2 do |
| 2316 |
begin |
| 2317 |
c.Color := GetColor(PointD(x, y)); |
| 2318 |
c.A := opacityTable[MulTable[c.A, Ord(alpha^)]]; |
| 2319 |
p.Color := BlendToAlpha(p.Color, c.Color); |
| 2320 |
inc(p); inc(alpha); |
| 2321 |
end |
| 2322 |
end |
| 2323 |
else |
| 2324 |
for x := x1 to x2 do |
| 2325 |
begin |
| 2326 |
c.Color := GetColor(PointD(x, y)); |
| 2327 |
c.A := MulTable[c.A, Ord(alpha^)]; |
| 2328 |
p.Color := BlendToAlpha(p.Color, c.Color); |
| 2329 |
inc(p); inc(alpha); |
| 2330 |
end |
| 2331 |
|
| 2332 |
|
| 2333 |
end; |
| 2334 |
|
| 2335 |
// ------------------------------------------------------------------------------ |
| 2336 |
// Draw functions |
| 2337 |
// ------------------------------------------------------------------------------ |
| 2338 |
|
| 2339 |
procedure DrawPoint(img: TImage32; |
| 2340 |
const pt: TPointD; radius: double; color: TColor32); |
| 2341 |
var |
| 2342 |
path: TPathD; |
| 2343 |
begin |
| 2344 |
if radius <= 1 then |
| 2345 |
path := Rectangle(pt.X-radius, pt.Y-radius, pt.X+radius, pt.Y+radius) else |
| 2346 |
path := Ellipse(RectD(pt.X-radius, pt.Y-radius, pt.X+radius, pt.Y+radius)); |
| 2347 |
DrawPolygon(img, path, frEvenOdd, color); |
| 2348 |
end; |
| 2349 |
// ------------------------------------------------------------------------------ |
| 2350 |
|
| 2351 |
procedure DrawPoint(img: TImage32; const pt: TPointD; |
| 2352 |
radius: double; renderer: TCustomRenderer); |
| 2353 |
var |
| 2354 |
path: TPathD; |
| 2355 |
begin |
| 2356 |
path := Ellipse(RectD(pt.X -radius, pt.Y -radius, pt.X +radius, pt.Y +radius)); |
| 2357 |
DrawPolygon(img, path, frEvenOdd, renderer); |
| 2358 |
end; |
| 2359 |
// ------------------------------------------------------------------------------ |
| 2360 |
|
| 2361 |
procedure DrawInvertedPoint(img: TImage32; const pt: TPointD; radius: double); |
| 2362 |
var |
| 2363 |
cr: TCustomRenderer; |
| 2364 |
begin |
| 2365 |
cr := TInverseRenderer.Create; |
| 2366 |
try |
| 2367 |
DrawPoint(img, pt, radius, cr); |
| 2368 |
finally |
| 2369 |
cr.Free; |
| 2370 |
end; |
| 2371 |
end; |
| 2372 |
// ------------------------------------------------------------------------------ |
| 2373 |
|
| 2374 |
procedure DrawPoint(img: TImage32; const points: TPathD; |
| 2375 |
radius: double; color: TColor32); |
| 2376 |
var |
| 2377 |
i: integer; |
| 2378 |
begin |
| 2379 |
for i := 0 to high(points) do |
| 2380 |
DrawPoint(img, points[i], radius, color); |
| 2381 |
end; |
| 2382 |
// ------------------------------------------------------------------------------ |
| 2383 |
|
| 2384 |
procedure DrawPoint(img: TImage32; const paths: TPathsD; |
| 2385 |
radius: double; color: TColor32); |
| 2386 |
var |
| 2387 |
i: integer; |
| 2388 |
begin |
| 2389 |
for i := 0 to high(paths) do |
| 2390 |
DrawPoint(img, paths[i], radius, color); |
| 2391 |
end; |
| 2392 |
// ------------------------------------------------------------------------------ |
| 2393 |
|
| 2394 |
procedure DrawLine(img: TImage32; |
| 2395 |
const pt1, pt2: TPointD; lineWidth: double; color: TColor32); |
| 2396 |
var |
| 2397 |
lines: TPathsD; |
| 2398 |
begin |
| 2399 |
setLength(lines, 1); |
| 2400 |
NewPointDArray(lines[0], 2, True); |
| 2401 |
lines[0][0] := pt1; |
| 2402 |
lines[0][1] := pt2; |
| 2403 |
DrawLine(img, lines, lineWidth, color, esRound); |
| 2404 |
end; |
| 2405 |
// ------------------------------------------------------------------------------ |
| 2406 |
|
| 2407 |
procedure DrawLine(img: TImage32; const line: TPathD; lineWidth: double; |
| 2408 |
color: TColor32; endStyle: TEndStyle; joinStyle: TJoinStyle; |
| 2409 |
miterLimit: double); |
| 2410 |
var |
| 2411 |
lines: TPathsD; |
| 2412 |
begin |
| 2413 |
setLength(lines, 1); |
| 2414 |
lines[0] := line; |
| 2415 |
DrawLine(img, lines, lineWidth, color, endStyle, joinStyle, miterLimit); |
| 2416 |
end; |
| 2417 |
// ------------------------------------------------------------------------------ |
| 2418 |
|
| 2419 |
procedure DrawLine(img: TImage32; const line: TPathD; lineWidth: double; |
| 2420 |
color: TColor32; rendererCache: TCustomRendererCache; |
| 2421 |
endStyle: TEndStyle; joinStyle: TJoinStyle; miterLimit: double); |
| 2422 |
var |
| 2423 |
lines: TPathsD; |
| 2424 |
begin |
| 2425 |
setLength(lines, 1); |
| 2426 |
lines[0] := line; |
| 2427 |
DrawLine(img, lines, lineWidth, color, rendererCache, endStyle, joinStyle, |
| 2428 |
miterLimit); |
| 2429 |
end; |
| 2430 |
// ------------------------------------------------------------------------------ |
| 2431 |
|
| 2432 |
procedure DrawLine(img: TImage32; const line: TPathD; lineWidth: double; |
| 2433 |
renderer: TCustomRenderer; endStyle: TEndStyle; joinStyle: TJoinStyle; |
| 2434 |
miterLimit: double); |
| 2435 |
var |
| 2436 |
lines: TPathsD; |
| 2437 |
begin |
| 2438 |
setLength(lines, 1); |
| 2439 |
lines[0] := line; |
| 2440 |
DrawLine(img, lines, lineWidth, renderer, endStyle, joinStyle, miterLimit); |
| 2441 |
end; |
| 2442 |
// ------------------------------------------------------------------------------ |
| 2443 |
|
| 2444 |
procedure DrawInvertedLine(img: TImage32; const line: TPathD; |
| 2445 |
lineWidth: double; endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto); |
| 2446 |
var |
| 2447 |
lines: TPathsD; |
| 2448 |
begin |
| 2449 |
setLength(lines, 1); |
| 2450 |
lines[0] := line; |
| 2451 |
DrawInvertedLine(img, lines, lineWidth, endStyle, joinStyle); |
| 2452 |
end; |
| 2453 |
// ------------------------------------------------------------------------------ |
| 2454 |
|
| 2455 |
procedure DrawLine(img: TImage32; const lines: TPathsD; |
| 2456 |
lineWidth: double; color: TColor32; |
| 2457 |
endStyle: TEndStyle; joinStyle: TJoinStyle; miterLimit: double); |
| 2458 |
var |
| 2459 |
cr: TCustomColorRenderer; |
| 2460 |
begin |
| 2461 |
if not assigned(lines) then exit; |
| 2462 |
|
| 2463 |
if img.AntiAliased then |
| 2464 |
cr := TColorRenderer.Create(color) else |
| 2465 |
cr := TAliasedColorRenderer.Create(color); |
| 2466 |
try |
| 2467 |
DrawLine(img, lines, lineWidth, cr, endStyle, joinStyle, miterLimit); |
| 2468 |
finally |
| 2469 |
cr.free; |
| 2470 |
end; |
| 2471 |
end; |
| 2472 |
// ------------------------------------------------------------------------------ |
| 2473 |
|
| 2474 |
procedure DrawLine(img: TImage32; const lines: TPathsD; |
| 2475 |
lineWidth: double; color: TColor32; rendererCache: TCustomRendererCache; |
| 2476 |
endStyle: TEndStyle; joinStyle: TJoinStyle; miterLimit: double); |
| 2477 |
var |
| 2478 |
cr: TCustomColorRenderer; |
| 2479 |
begin |
| 2480 |
if not assigned(lines) then exit; |
| 2481 |
if rendererCache = nil then |
| 2482 |
DrawLine(img, lines, lineWidth, color, endStyle, joinStyle, miterLimit) |
| 2483 |
else |
| 2484 |
begin |
| 2485 |
if img.AntiAliased then |
| 2486 |
cr := rendererCache.ColorRenderer else |
| 2487 |
cr := rendererCache.AliasedColorRenderer; |
| 2488 |
DrawLine(img, lines, lineWidth, cr, endStyle, joinStyle, miterLimit); |
| 2489 |
end; |
| 2490 |
end; |
| 2491 |
// ------------------------------------------------------------------------------ |
| 2492 |
|
| 2493 |
procedure DrawLine(img: TImage32; const lines: TPathsD; |
| 2494 |
lineWidth: double; renderer: TCustomRenderer; |
| 2495 |
endStyle: TEndStyle; joinStyle: TJoinStyle; |
| 2496 |
miterLimit: double); |
| 2497 |
var |
| 2498 |
lines2: TPathsD; |
| 2499 |
begin |
| 2500 |
if (not assigned(lines)) or (not assigned(renderer)) then exit; |
| 2501 |
if (lineWidth < MinStrokeWidth) then lineWidth := MinStrokeWidth; |
| 2502 |
lines2 := RoughOutline(lines, lineWidth, joinStyle, endStyle, miterLimit); |
| 2503 |
Rasterize(img, lines2, img.bounds, frNonZero, renderer); |
| 2504 |
end; |
| 2505 |
// ------------------------------------------------------------------------------ |
| 2506 |
|
| 2507 |
procedure DrawInvertedLine(img: TImage32; |
| 2508 |
const lines: TPathsD; lineWidth: double; |
| 2509 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto); |
| 2510 |
var |
| 2511 |
lines2: TPathsD; |
| 2512 |
ir: TInverseRenderer; |
| 2513 |
begin |
| 2514 |
if not assigned(lines) then exit; |
| 2515 |
if (lineWidth < MinStrokeWidth) then lineWidth := MinStrokeWidth; |
| 2516 |
lines2 := RoughOutline(lines, lineWidth, joinStyle, endStyle, 2); |
| 2517 |
ir := TInverseRenderer.Create; |
| 2518 |
try |
| 2519 |
Rasterize(img, lines2, img.bounds, frNonZero, ir); |
| 2520 |
finally |
| 2521 |
ir.free; |
| 2522 |
end; |
| 2523 |
end; |
| 2524 |
// ------------------------------------------------------------------------------ |
| 2525 |
|
| 2526 |
procedure DrawDashedLine(img: TImage32; const line: TPathD; |
| 2527 |
dashPattern: TArrayOfDouble; patternOffset: PDouble; lineWidth: double; |
| 2528 |
color: TColor32; endStyle: TEndStyle; joinStyle: TJoinStyle; |
| 2529 |
rendererCache: TCustomRendererCache); |
| 2530 |
var |
| 2531 |
lines: TPathsD; |
| 2532 |
cr: TColorRenderer; |
| 2533 |
i: integer; |
| 2534 |
begin |
| 2535 |
if (lineWidth < MinStrokeWidth) then lineWidth := MinStrokeWidth; |
| 2536 |
if not assigned(line) then exit; |
| 2537 |
|
| 2538 |
for i := 0 to High(dashPattern) do |
| 2539 |
if dashPattern[i] <= 0 then dashPattern[i] := 1; |
| 2540 |
|
| 2541 |
lines := GetDashedPath(line, endStyle = esPolygon, dashPattern, patternOffset); |
| 2542 |
if Length(lines) = 0 then Exit; |
| 2543 |
|
| 2544 |
case joinStyle of |
| 2545 |
jsAuto: |
| 2546 |
if endStyle = esRound then |
| 2547 |
joinStyle := jsRound else |
| 2548 |
joinStyle := jsSquare; |
| 2549 |
jsSquare, jsMiter: |
| 2550 |
endStyle := esSquare; |
| 2551 |
jsRound: |
| 2552 |
endStyle := esRound; |
| 2553 |
jsButt: |
| 2554 |
endStyle := esButt; |
| 2555 |
end; |
| 2556 |
lines := RoughOutline(lines, lineWidth, joinStyle, endStyle); |
| 2557 |
|
| 2558 |
if rendererCache = nil then |
| 2559 |
cr := TColorRenderer.Create(color) else |
| 2560 |
cr := rendererCache.GetColorRenderer(color); |
| 2561 |
try |
| 2562 |
Rasterize(img, lines, img.bounds, frNonZero, cr); |
| 2563 |
finally |
| 2564 |
if rendererCache = nil then |
| 2565 |
cr.free; |
| 2566 |
end; |
| 2567 |
end; |
| 2568 |
// ------------------------------------------------------------------------------ |
| 2569 |
|
| 2570 |
procedure DrawDashedLine(img: TImage32; const lines: TPathsD; |
| 2571 |
dashPattern: TArrayOfDouble; patternOffset: PDouble; lineWidth: double; |
| 2572 |
color: TColor32; endStyle: TEndStyle; joinStyle: TJoinStyle; |
| 2573 |
rendererCache: TCustomRendererCache); |
| 2574 |
var |
| 2575 |
i: integer; |
| 2576 |
begin |
| 2577 |
if not assigned(lines) then exit; |
| 2578 |
for i := 0 to high(lines) do |
| 2579 |
DrawDashedLine(img, lines[i], |
| 2580 |
dashPattern, patternOffset, lineWidth, color, endStyle, joinStyle, |
| 2581 |
rendererCache); |
| 2582 |
end; |
| 2583 |
// ------------------------------------------------------------------------------ |
| 2584 |
|
| 2585 |
procedure DrawDashedLine(img: TImage32; const line: TPathD; |
| 2586 |
dashPattern: TArrayOfDouble; patternOffset: PDouble; lineWidth: double; |
| 2587 |
renderer: TCustomRenderer; endStyle: TEndStyle; joinStyle: TJoinStyle); |
| 2588 |
var |
| 2589 |
i: integer; |
| 2590 |
lines: TPathsD; |
| 2591 |
begin |
| 2592 |
if (not assigned(line)) or (not assigned(renderer)) then exit; |
| 2593 |
if (lineWidth < MinStrokeWidth) then lineWidth := MinStrokeWidth; |
| 2594 |
|
| 2595 |
for i := 0 to High(dashPattern) do |
| 2596 |
if dashPattern[i] <= 0 then dashPattern[i] := 1; |
| 2597 |
|
| 2598 |
lines := GetDashedPath(line, endStyle = esPolygon, dashPattern, patternOffset); |
| 2599 |
if Length(lines) = 0 then Exit; |
| 2600 |
lines := RoughOutline(lines, lineWidth, joinStyle, endStyle); |
| 2601 |
Rasterize(img, lines, img.bounds, frNonZero, renderer); |
| 2602 |
end; |
| 2603 |
// ------------------------------------------------------------------------------ |
| 2604 |
|
| 2605 |
procedure DrawDashedLine(img: TImage32; const lines: TPathsD; |
| 2606 |
dashPattern: TArrayOfDouble; patternOffset: PDouble; lineWidth: double; |
| 2607 |
renderer: TCustomRenderer; endStyle: TEndStyle; joinStyle: TJoinStyle); |
| 2608 |
var |
| 2609 |
i: integer; |
| 2610 |
begin |
| 2611 |
if not assigned(lines) then exit; |
| 2612 |
for i := 0 to high(lines) do |
| 2613 |
DrawDashedLine(img, lines[i], |
| 2614 |
dashPattern, patternOffset, lineWidth, renderer, endStyle, joinStyle); |
| 2615 |
end; |
| 2616 |
// ------------------------------------------------------------------------------ |
| 2617 |
|
| 2618 |
procedure DrawInvertedDashedLine(img: TImage32; |
| 2619 |
const line: TPathD; dashPattern: TArrayOfDouble; |
| 2620 |
patternOffset: PDouble; lineWidth: double; endStyle: TEndStyle; |
| 2621 |
joinStyle: TJoinStyle = jsAuto); |
| 2622 |
var |
| 2623 |
i: integer; |
| 2624 |
lines: TPathsD; |
| 2625 |
renderer: TInverseRenderer; |
| 2626 |
begin |
| 2627 |
if not assigned(line) then exit; |
| 2628 |
if (lineWidth < MinStrokeWidth) then lineWidth := MinStrokeWidth; |
| 2629 |
|
| 2630 |
for i := 0 to High(dashPattern) do |
| 2631 |
if dashPattern[i] <= 0 then dashPattern[i] := 1; |
| 2632 |
|
| 2633 |
lines := GetDashedPath(line, endStyle = esPolygon, dashPattern, patternOffset); |
| 2634 |
if Length(lines) = 0 then Exit; |
| 2635 |
lines := RoughOutline(lines, lineWidth, joinStyle, endStyle); |
| 2636 |
renderer := TInverseRenderer.Create; |
| 2637 |
try |
| 2638 |
Rasterize(img, lines, img.bounds, frNonZero, renderer); |
| 2639 |
finally |
| 2640 |
renderer.Free; |
| 2641 |
end; |
| 2642 |
end; |
| 2643 |
// ------------------------------------------------------------------------------ |
| 2644 |
|
| 2645 |
procedure DrawInvertedDashedLine(img: TImage32; |
| 2646 |
const lines: TPathsD; dashPattern: TArrayOfDouble; |
| 2647 |
patternOffset: PDouble; lineWidth: double; |
| 2648 |
endStyle: TEndStyle; joinStyle: TJoinStyle = jsAuto); |
| 2649 |
var |
| 2650 |
i: integer; |
| 2651 |
begin |
| 2652 |
if not assigned(lines) then exit; |
| 2653 |
for i := 0 to high(lines) do |
| 2654 |
DrawInvertedDashedLine(img, lines[i], |
| 2655 |
dashPattern, patternOffset, lineWidth, endStyle, joinStyle); |
| 2656 |
end; |
| 2657 |
// ------------------------------------------------------------------------------ |
| 2658 |
|
| 2659 |
procedure DrawPolygon(img: TImage32; const polygon: TPathD; |
| 2660 |
fillRule: TFillRule; color: TColor32); |
| 2661 |
var |
| 2662 |
polygons: TPathsD; |
| 2663 |
begin |
| 2664 |
if not assigned(polygon) then exit; |
| 2665 |
setLength(polygons, 1); |
| 2666 |
polygons[0] := polygon; |
| 2667 |
DrawPolygon(img, polygons, fillRule, color); |
| 2668 |
end; |
| 2669 |
// ------------------------------------------------------------------------------ |
| 2670 |
|
| 2671 |
procedure DrawPolygon(img: TImage32; const polygon: TPathD; |
| 2672 |
fillRule: TFillRule; renderer: TCustomRenderer); |
| 2673 |
var |
| 2674 |
polygons: TPathsD; |
| 2675 |
begin |
| 2676 |
if (not assigned(polygon)) or (not assigned(renderer)) then exit; |
| 2677 |
setLength(polygons, 1); |
| 2678 |
polygons[0] := polygon; |
| 2679 |
Rasterize(img, polygons, img.Bounds, fillRule, renderer); |
| 2680 |
end; |
| 2681 |
// ------------------------------------------------------------------------------ |
| 2682 |
|
| 2683 |
procedure DrawPolygon(img: TImage32; const polygons: TPathsD; |
| 2684 |
fillRule: TFillRule; color: TColor32); |
| 2685 |
var |
| 2686 |
cr: TCustomRenderer; |
| 2687 |
begin |
| 2688 |
if not assigned(polygons) then exit; |
| 2689 |
if img.AntiAliased then |
| 2690 |
cr := TColorRenderer.Create(color) else |
| 2691 |
cr := TAliasedColorRenderer.Create(color); |
| 2692 |
try |
| 2693 |
Rasterize(img, polygons, img.bounds, fillRule, cr); |
| 2694 |
finally |
| 2695 |
cr.free; |
| 2696 |
end; |
| 2697 |
end; |
| 2698 |
// ------------------------------------------------------------------------------ |
| 2699 |
|
| 2700 |
procedure DrawPolygon(img: TImage32; const polygons: TPathsD; |
| 2701 |
fillRule: TFillRule; color: TColor32; |
| 2702 |
rendererCache: TCustomRendererCache); |
| 2703 |
var |
| 2704 |
cr: TCustomColorRenderer; |
| 2705 |
begin |
| 2706 |
if not assigned(polygons) then exit; |
| 2707 |
if rendererCache = nil then |
| 2708 |
DrawPolygon(img, polygons, fillRule, color) |
| 2709 |
else |
| 2710 |
begin |
| 2711 |
if img.AntiAliased then |
| 2712 |
cr := rendererCache.ColorRenderer else |
| 2713 |
cr := rendererCache.AliasedColorRenderer; |
| 2714 |
cr.SetColor(color); |
| 2715 |
Rasterize(img, polygons, img.bounds, fillRule, cr); |
| 2716 |
end; |
| 2717 |
end; |
| 2718 |
// ------------------------------------------------------------------------------ |
| 2719 |
|
| 2720 |
procedure DrawPolygon(img: TImage32; const polygons: TPathsD; |
| 2721 |
fillRule: TFillRule; renderer: TCustomRenderer); |
| 2722 |
begin |
| 2723 |
if (not assigned(polygons)) or (not assigned(renderer)) then exit; |
| 2724 |
Rasterize(img, polygons, img.bounds, fillRule, renderer); |
| 2725 |
end; |
| 2726 |
// ------------------------------------------------------------------------------ |
| 2727 |
|
| 2728 |
procedure DrawInvertedPolygon(img: TImage32; const polygon: TPathD; |
| 2729 |
fillRule: TFillRule); |
| 2730 |
var |
| 2731 |
polygons: TPathsD; |
| 2732 |
begin |
| 2733 |
if not assigned(polygon) then exit; |
| 2734 |
setLength(polygons, 1); |
| 2735 |
polygons[0] := polygon; |
| 2736 |
DrawInvertedPolygon(img, polygons, fillRule); |
| 2737 |
end; |
| 2738 |
// ------------------------------------------------------------------------------ |
| 2739 |
|
| 2740 |
procedure DrawInvertedPolygon(img: TImage32; const polygons: TPathsD; |
| 2741 |
fillRule: TFillRule); |
| 2742 |
var |
| 2743 |
cr: TCustomRenderer; |
| 2744 |
begin |
| 2745 |
if not assigned(polygons) then exit; |
| 2746 |
cr := TInverseRenderer.Create; |
| 2747 |
try |
| 2748 |
Rasterize(img, polygons, img.bounds, fillRule, cr); |
| 2749 |
finally |
| 2750 |
cr.free; |
| 2751 |
end; |
| 2752 |
end; |
| 2753 |
// ------------------------------------------------------------------------------ |
| 2754 |
|
| 2755 |
procedure DrawPolygon_ClearType(img: TImage32; const polygons: TPathsD; |
| 2756 |
fillRule: TFillRule; color: TColor32; backColor: TColor32); |
| 2757 |
var |
| 2758 |
w, h: integer; |
| 2759 |
tmpImg: TImage32; |
| 2760 |
rec: TRect; |
| 2761 |
tmpPolygons: TPathsD; |
| 2762 |
cr: TColorRenderer; |
| 2763 |
begin |
| 2764 |
if not assigned(polygons) then exit; |
| 2765 |
|
| 2766 |
rec := GetBounds(polygons); |
| 2767 |
RectWidthHeight(rec, w, h); |
| 2768 |
tmpImg := TImage32.Create(w *3, h); |
| 2769 |
try |
| 2770 |
tmpPolygons := TranslatePath(polygons, -rec.Left, -rec.Top); |
| 2771 |
tmpPolygons := ScalePath(tmpPolygons, 3, 1); |
| 2772 |
cr := TColorRenderer.Create(clBlack32); |
| 2773 |
try |
| 2774 |
Rasterize(tmpImg, tmpPolygons, tmpImg.bounds, fillRule, cr); |
| 2775 |
finally |
| 2776 |
cr.Free; |
| 2777 |
end; |
| 2778 |
ApplyClearType(tmpImg, color, backColor); |
| 2779 |
img.CopyBlend(tmpImg, tmpImg.Bounds, rec, BlendToAlphaLine); |
| 2780 |
finally |
| 2781 |
tmpImg.Free; |
| 2782 |
end; |
| 2783 |
end; |
| 2784 |
// ------------------------------------------------------------------------------ |
| 2785 |
|
| 2786 |
procedure ErasePolygon(img: TImage32; const polygon: TPathD; |
| 2787 |
fillRule: TFillRule); |
| 2788 |
var |
| 2789 |
polygons: TPathsD; |
| 2790 |
begin |
| 2791 |
if not assigned(polygon) then exit; |
| 2792 |
setLength(polygons, 1); |
| 2793 |
polygons[0] := polygon; |
| 2794 |
ErasePolygon(img, polygons, fillRule); |
| 2795 |
end; |
| 2796 |
// ------------------------------------------------------------------------------ |
| 2797 |
|
| 2798 |
procedure ErasePolygon(img: TImage32; const polygons: TPathsD; |
| 2799 |
fillRule: TFillRule); |
| 2800 |
var |
| 2801 |
er: TEraseRenderer; |
| 2802 |
begin |
| 2803 |
er := TEraseRenderer.Create; |
| 2804 |
try |
| 2805 |
Rasterize(img, polygons, img.bounds, fillRule, er); |
| 2806 |
finally |
| 2807 |
er.Free; |
| 2808 |
end; |
| 2809 |
end; |
| 2810 |
// ------------------------------------------------------------------------------ |
| 2811 |
|
| 2812 |
procedure DrawBoolMask(img: TImage32; const mask: TArrayOfByte; color: TColor32); |
| 2813 |
var |
| 2814 |
i, len: integer; |
| 2815 |
pc: PColor32; |
| 2816 |
pb: PByte; |
| 2817 |
begin |
| 2818 |
len := Length(mask); |
| 2819 |
if (len = 0) or (len <> img.Width * img.Height) then Exit; |
| 2820 |
pc := img.PixelBase; |
| 2821 |
pb := @mask[0]; |
| 2822 |
for i := 0 to len -1 do |
| 2823 |
begin |
| 2824 |
{$IFDEF PBYTE} |
| 2825 |
if pb^ > 0 then |
| 2826 |
{$ELSE} |
| 2827 |
if pb^ > #0 then |
| 2828 |
{$ENDIF} |
| 2829 |
pc^ := color else |
| 2830 |
pc^ := clNone32; |
| 2831 |
inc(pc); inc(pb); |
| 2832 |
end; |
| 2833 |
end; |
| 2834 |
// ------------------------------------------------------------------------------ |
| 2835 |
|
| 2836 |
procedure DrawAlphaMask(img: TImage32; const mask: TArrayOfByte; color: TColor32); |
| 2837 |
var |
| 2838 |
i, len: integer; |
| 2839 |
pc: PColor32; |
| 2840 |
pb: PByte; |
| 2841 |
begin |
| 2842 |
len := Length(mask); |
| 2843 |
if (len = 0) or (len <> img.Width * img.Height) then Exit; |
| 2844 |
color := color and $FFFFFF; //strip alpha value |
| 2845 |
pc := img.PixelBase; |
| 2846 |
pb := @mask[0]; |
| 2847 |
for i := 0 to len -1 do |
| 2848 |
begin |
| 2849 |
{$IFDEF PBYTE} |
| 2850 |
if pb^ > 0 then |
| 2851 |
pc^ := color or pb^ shl 24 else |
| 2852 |
pc^ := clNone32; |
| 2853 |
{$ELSE} |
| 2854 |
if pb^ > #0 then |
| 2855 |
pc^ := color or Ord(pb^) shl 24 else |
| 2856 |
pc^ := clNone32; |
| 2857 |
{$ENDIF} |
| 2858 |
inc(pc); inc(pb); |
| 2859 |
end; |
| 2860 |
end; |
| 2861 |
// ------------------------------------------------------------------------------ |
| 2862 |
|
| 2863 |
end. |