| 1 |
unit Img32.Extra; |
| 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 |
* Purpose : Miscellaneous routines that don't belong in other modules. * |
| 10 |
* License : http://www.boost.org/LICENSE_1_0.txt * |
| 11 |
*******************************************************************************) |
| 12 |
|
| 13 |
interface |
| 14 |
{$I Img32.inc} |
| 15 |
|
| 16 |
uses |
| 17 |
SysUtils, Classes, Math, Types, |
| 18 |
Img32, Img32.Draw, Img32.Vector; |
| 19 |
|
| 20 |
type |
| 21 |
TButtonShape = (bsRound, bsSquare, bsDiamond); |
| 22 |
TButtonAttribute = (baShadow, ba3D, baEraseBeneath); |
| 23 |
TButtonAttributes = set of TButtonAttribute; |
| 24 |
|
| 25 |
procedure DrawEdge(img: TImage32; const rec: TRect; |
| 26 |
topLeftColor, bottomRightColor: TColor32; penWidth: double = 1.0); overload; |
| 27 |
procedure DrawEdge(img: TImage32; const rec: TRectD; |
| 28 |
topLeftColor, bottomRightColor: TColor32; penWidth: double = 1.0); overload; |
| 29 |
procedure DrawEdge(img: TImage32; const path: TPathD; |
| 30 |
topLeftColor, bottomRightColor: TColor32; |
| 31 |
penWidth: double = 1.0; closePath: Boolean = true); overload; |
| 32 |
|
| 33 |
//DrawShadowRect: is **much** faster than DrawShadow |
| 34 |
procedure DrawShadowRect(img: TImage32; const rec: TRect; depth: double; |
| 35 |
angle: double = angle45; color: TColor32 = $80000000); |
| 36 |
procedure DrawShadow(img: TImage32; const polygon: TPathD; |
| 37 |
fillRule: TFillRule; depth: double; angleRads: double = angle45; |
| 38 |
color: TColor32 = $80000000; cutoutInsideShadow: Boolean = false); overload; |
| 39 |
procedure DrawShadow(img: TImage32; const polygons: TPathsD; |
| 40 |
fillRule: TFillRule; depth: double; angleRads: double = angle45; |
| 41 |
color: TColor32 = $80000000; cutoutInsideShadow: Boolean = false); overload; |
| 42 |
|
| 43 |
procedure DrawGlow(img: TImage32; const polygon: TPathD; |
| 44 |
fillRule: TFillRule; color: TColor32; blurRadius: integer); overload; |
| 45 |
procedure DrawGlow(img: TImage32; const polygons: TPathsD; |
| 46 |
fillRule: TFillRule; color: TColor32; blurRadius: integer); overload; |
| 47 |
|
| 48 |
//FloodFill: If no CompareFunc is provided, FloodFill will fill whereever |
| 49 |
//adjoining pixels exactly match the starting pixel - Point(x,y). |
| 50 |
procedure FloodFill(img: TImage32; x, y: Integer; newColor: TColor32; |
| 51 |
tolerance: Byte = 0; compareFunc: TCompareFunctionEx = nil); |
| 52 |
|
| 53 |
procedure FastGaussianBlur(img: TImage32; |
| 54 |
const rec: TRect; stdDev: integer; repeats: integer = 2); overload; |
| 55 |
procedure FastGaussianBlur(img: TImage32; |
| 56 |
const rec: TRect; stdDevX, stdDevY: integer; repeats: integer); overload; |
| 57 |
|
| 58 |
procedure GaussianBlur(img: TImage32; rec: TRect; radius: Integer); |
| 59 |
|
| 60 |
//Emboss: A smaller radius is sharper. Increasing depth increases contrast. |
| 61 |
//Luminance changes grayscale balance (unless preserveColor = true) |
| 62 |
procedure Emboss(img: TImage32; radius: Integer = 1; depth: Integer = 10; |
| 63 |
luminance: Integer = 75; preserveColor: Boolean = false); |
| 64 |
|
| 65 |
//Sharpen: Radius range is 1 - 10; amount range is 1 - 50.<br> |
| 66 |
//see https://en.wikipedia.org/wiki/Unsharp_masking |
| 67 |
procedure Sharpen(img: TImage32; radius: Integer = 2; amount: Integer = 10); |
| 68 |
|
| 69 |
//HatchBackground: Assumes the current image is semi-transparent. |
| 70 |
procedure HatchBackground(img: TImage32; color1: TColor32 = clWhite32; |
| 71 |
color2: TColor32= $FFE8E8E8; hatchSize: Integer = 10); overload; |
| 72 |
procedure HatchBackground(img: TImage32; const rec: TRect; |
| 73 |
color1: TColor32 = clWhite32; color2: TColor32= $FFE8E8E8; |
| 74 |
hatchSize: Integer = 10); overload; |
| 75 |
|
| 76 |
procedure GridBackground(img: TImage32; majorInterval, minorInterval: integer; |
| 77 |
fillColor: TColor32 = clWhite32; |
| 78 |
majColor: TColor32 = $30000000; minColor: TColor32 = $20000000); |
| 79 |
|
| 80 |
procedure ReplaceExactColor(img: TImage32; oldColor, newColor: TColor32); |
| 81 |
|
| 82 |
//RemoveColor: Removes the specified color from the image, even from |
| 83 |
//pixels that are a blend of colors including the specified color.<br> |
| 84 |
//see https://stackoverflow.com/questions/9280902/ |
| 85 |
procedure RemoveColor(img: TImage32; color: TColor32); |
| 86 |
|
| 87 |
//FilterOnColor: Removes everything not nearly matching 'color' |
| 88 |
//This uses an algorithm that's very similar to the one in RemoveColor. |
| 89 |
procedure FilterOnColor(img: TImage32; color: TColor32); |
| 90 |
|
| 91 |
procedure FilterOnExactColor(img: TImage32; color: TColor32); |
| 92 |
|
| 93 |
procedure FilterOnAlpha(img: TImage32; alpha: byte; tolerance: byte); |
| 94 |
|
| 95 |
//RedEyeRemove: Removes 'red eye' from flash photo images. |
| 96 |
procedure RedEyeRemove(img: TImage32; const rect: TRect); |
| 97 |
|
| 98 |
procedure PencilEffect(img: TImage32; intensity: integer = 0); |
| 99 |
|
| 100 |
procedure TraceContours(img: TImage32; intensity: integer); |
| 101 |
|
| 102 |
procedure EraseInsidePath(img: TImage32; |
| 103 |
const path: TPathD; fillRule: TFillRule); |
| 104 |
procedure EraseInsidePaths(img: TImage32; |
| 105 |
const paths: TPathsD; fillRule: TFillRule); |
| 106 |
|
| 107 |
procedure EraseOutsidePath(img: TImage32; const path: TPathD; |
| 108 |
fillRule: TFillRule; const outsideBounds: TRect); |
| 109 |
procedure EraseOutsidePaths(img: TImage32; const paths: TPathsD; |
| 110 |
fillRule: TFillRule; const outsideBounds: TRect; |
| 111 |
rendererCache: TCustomRendererCache = nil); overload; |
| 112 |
|
| 113 |
procedure Draw3D(img: TImage32; const polygon: TPathD; |
| 114 |
fillRule: TFillRule; height, blurRadius: double; |
| 115 |
colorLt: TColor32 = $DDFFFFFF; colorDk: TColor32 = $80000000; |
| 116 |
angleRads: double = angle225); overload; |
| 117 |
procedure Draw3D(img: TImage32; const polygons: TPathsD; |
| 118 |
fillRule: TFillRule; height, blurRadius: double; |
| 119 |
colorLt: TColor32 = $DDFFFFFF; colorDk: TColor32 = $80000000; |
| 120 |
angleRads: double = angle225); overload; |
| 121 |
|
| 122 |
function RainbowColor(fraction: double; luminance: byte = 128): TColor32; |
| 123 |
function GradientColor(color1, color2: TColor32; frac: single): TColor32; |
| 124 |
function MakeDarker(color: TColor32; percent: cardinal): TColor32; |
| 125 |
function MakeLighter(color: TColor32; percent: cardinal): TColor32; |
| 126 |
|
| 127 |
function DrawButton(img: TImage32; const pt: TPointD; |
| 128 |
size: double; color: TColor32 = clNone32; |
| 129 |
buttonShape: TButtonShape = bsRound; |
| 130 |
buttonAttributes: TButtonAttributes = [baShadow, ba3D, baEraseBeneath]): TPathD; |
| 131 |
|
| 132 |
// RamerDouglasPeucker: simplifies paths, recursively removing vertices where |
| 133 |
// they deviate no more than 'epsilon' from their adjacent vertices. |
| 134 |
function RamerDouglasPeucker(const path: TPathD; |
| 135 |
epsilon: double): TPathD; overload; |
| 136 |
function RamerDouglasPeucker(const paths: TPathsD; |
| 137 |
epsilon: double): TPathsD; overload; |
| 138 |
|
| 139 |
{$IFDEF USE_OLD_SIMPLIFYPATHS} |
| 140 |
// SimplifyPath: Better than RDP when simplifying closed paths |
| 141 |
function SimplifyPath(const path: TPathD; |
| 142 |
shapeTolerance: double = 0.1; isOpenPath: Boolean = false): TPathD; |
| 143 |
function SimplifyPaths(const paths: TPathsD; |
| 144 |
shapeTolerance: double = 0.1; isOpenPath: Boolean = false): TPathsD; |
| 145 |
{$ELSE} |
| 146 |
// SimplifyPath: Better than RDP when simplifying closed paths |
| 147 |
function SimplifyPath(const path: TPathD; |
| 148 |
shapeTolerance: double = 0.1; isClosedPath: Boolean = true): TPathD; |
| 149 |
function SimplifyPaths(const paths: TPathsD; |
| 150 |
shapeTolerance: double = 0.1; isClosedPath: Boolean = true): TPathsD; |
| 151 |
{$ENDIF} |
| 152 |
|
| 153 |
// SimplifyPathEx: this is particularly useful following Vectorize() |
| 154 |
// because it also removes very short zig-zag segments |
| 155 |
function SimplifyPathEx(const path: TPathD; shapeTolerance: double): TPathD; |
| 156 |
function SimplifyPathsEx(const paths: TPathsD; shapeTolerance: double): TPathsD; |
| 157 |
|
| 158 |
// SmoothToCubicBezier and SmoothToCubicBezier2 have been deprecated in |
| 159 |
// favour of SmoothPath that's much simpler |
| 160 |
function SmoothToCubicBezier(const path: TPathD; |
| 161 |
pathIsClosed: Boolean; maxOffset: integer = 0): TPathD; overload; deprecated; |
| 162 |
function SmoothToCubicBezier(const paths: TPathsD; |
| 163 |
pathIsClosed: Boolean; maxOffset: integer = 0): TPathsD; overload; deprecated; |
| 164 |
function SmoothToCubicBezier2(const path: TPathD; |
| 165 |
pathIsClosed: Boolean; maxOffset: integer = 0): TPathD; overload; deprecated; |
| 166 |
function SmoothToCubicBezier2(const paths: TPathsD; |
| 167 |
pathIsClosed: Boolean; maxOffset: integer = 0): TPathsD; overload; deprecated; |
| 168 |
|
| 169 |
// SmoothPath - smooths a path using bicubic interpolation |
| 170 |
// tension (range -1 to 1): from least to most curve constraint |
| 171 |
function SmoothPath(const path: TPathD; isClosedPath: Boolean; |
| 172 |
tension: double = 0; shapeTolerance: double = 0.1): TPathD; |
| 173 |
function SmoothPaths(const paths: TPathsD; isClosedPath: Boolean; |
| 174 |
tension: double = 0; shapeTolerance: double = 0.1): TPathsD; |
| 175 |
|
| 176 |
function GetFloodFillMask(imgIn, imgMaskOut: TImage32; x, y: Integer; |
| 177 |
tolerance: Byte; compareFunc: TCompareFunctionEx): Boolean; |
| 178 |
|
| 179 |
procedure SymmetricCropTransparent(img: TImage32); |
| 180 |
|
| 181 |
//3 additional blend functions (see TImage32.CopyBlend) |
| 182 |
function BlendAverage(bgColor, fgColor: TColor32): TColor32; |
| 183 |
function BlendLinearBurn(bgColor, fgColor: TColor32): TColor32; |
| 184 |
function BlendColorDodge(bgColor, fgColor: TColor32): TColor32; |
| 185 |
|
| 186 |
implementation |
| 187 |
|
| 188 |
uses |
| 189 |
{$IFDEF USING_FMX} |
| 190 |
Img32.FMX, |
| 191 |
{$ENDIF} |
| 192 |
Img32.Transform; |
| 193 |
|
| 194 |
const |
| 195 |
FloodFillDefaultRGBTolerance: byte = 64; |
| 196 |
MaxBlur = 100; |
| 197 |
|
| 198 |
type |
| 199 |
PColor32Array = ^TColor32Array; |
| 200 |
TColor32Array = array [0.. maxint div SizeOf(TColor32) -1] of TColor32; |
| 201 |
PWeightedColorArray = ^TWeightedColorArray; |
| 202 |
TWeightedColorArray = array [0.. $FFFFFF] of TWeightedColor; |
| 203 |
|
| 204 |
// SimplifyPathsEx structures |
| 205 |
PVertex = ^TVertex; |
| 206 |
TVertex = record |
| 207 |
pt : TPointD; |
| 208 |
uvec : TPointD; |
| 209 |
dist : double; |
| 210 |
perpD : double; |
| 211 |
next : PVertex; |
| 212 |
prev: PVertex; |
| 213 |
end; |
| 214 |
TArrayOfVertices = array of TVertex; |
| 215 |
|
| 216 |
|
| 217 |
//------------------------------------------------------------------------------ |
| 218 |
// Miscellaneous functions |
| 219 |
//------------------------------------------------------------------------------ |
| 220 |
|
| 221 |
function Clamp(val, endVal: integer): integer; |
| 222 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 223 |
begin |
| 224 |
if val < 0 then Result := 0 |
| 225 |
else if val >= endVal then Result := endVal -1 |
| 226 |
else Result := val; |
| 227 |
end; |
| 228 |
//------------------------------------------------------------------------------ |
| 229 |
|
| 230 |
function ModEx(val, endVal: integer): integer; |
| 231 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 232 |
begin |
| 233 |
Result := val mod endVal; |
| 234 |
if Result < 0 then Result := endVal + Result; |
| 235 |
end; |
| 236 |
//------------------------------------------------------------------------------ |
| 237 |
|
| 238 |
function GetSymmetricCropTransparentRect(img: TImage32): TRect; |
| 239 |
var |
| 240 |
w,h, x,y, x1,y1: Integer; |
| 241 |
p1,p2: PARGB; |
| 242 |
opaquePxlFound: Boolean; |
| 243 |
begin |
| 244 |
Result := img.Bounds; |
| 245 |
w := img.Width; |
| 246 |
y1 := 0; |
| 247 |
opaquePxlFound := false; |
| 248 |
for y := 0 to (img.Height div 2) -1 do |
| 249 |
begin |
| 250 |
p1 := PARGB(img.PixelRow[y]); |
| 251 |
p2 := PARGB(img.PixelRow[img.Height - y -1]); |
| 252 |
for x := 0 to w -1 do |
| 253 |
begin |
| 254 |
if (p1.A > 0) or (p2.A > 0) then |
| 255 |
begin |
| 256 |
y1 := y; |
| 257 |
opaquePxlFound := true; |
| 258 |
break; |
| 259 |
end; |
| 260 |
inc(p1); inc(p2); |
| 261 |
end; |
| 262 |
if opaquePxlFound then break; |
| 263 |
end; |
| 264 |
// probably safeset not to resize empty images |
| 265 |
if not opaquePxlFound then Exit; |
| 266 |
if y1 > 0 then |
| 267 |
begin |
| 268 |
inc(Result.Top, y1); |
| 269 |
dec(Result.Bottom, y1); |
| 270 |
end; |
| 271 |
x1 := 0; |
| 272 |
h := RectHeight(Result); |
| 273 |
opaquePxlFound := false; |
| 274 |
for x := 0 to (w div 2) -1 do |
| 275 |
begin |
| 276 |
p1 := PARGB(@img.Pixels[Result.Top * w + x]); |
| 277 |
p2 := PARGB(@img.Pixels[Result.Top * w + (w -1) - x]); |
| 278 |
for y := 0 to h -1 do |
| 279 |
begin |
| 280 |
if (p1.A > 0) or (p2.A > 0) then |
| 281 |
begin |
| 282 |
x1 := x; |
| 283 |
opaquePxlFound := true; |
| 284 |
break; |
| 285 |
end; |
| 286 |
inc(p1, w); inc(p2, w); |
| 287 |
end; |
| 288 |
if opaquePxlFound then break; |
| 289 |
end; |
| 290 |
if not opaquePxlFound then Exit; |
| 291 |
inc(Result.Left, x1); |
| 292 |
dec(Result.Right, x1); |
| 293 |
end; |
| 294 |
//------------------------------------------------------------------------------ |
| 295 |
|
| 296 |
//SymmetricCropTransparent: after cropping, the image's midpoint |
| 297 |
//will be the same pixel as before cropping. (Important for rotating.) |
| 298 |
procedure SymmetricCropTransparent(img: TImage32); |
| 299 |
var |
| 300 |
rec: TRect; |
| 301 |
begin |
| 302 |
rec := GetSymmetricCropTransparentRect(img); |
| 303 |
if (rec.Top > 0) or (rec.Left > 0) then img.Crop(rec); |
| 304 |
end; |
| 305 |
//------------------------------------------------------------------------------ |
| 306 |
|
| 307 |
procedure DrawEdge(img: TImage32; const rec: TRect; |
| 308 |
topLeftColor, bottomRightColor: TColor32; penWidth: double = 1.0); |
| 309 |
begin |
| 310 |
DrawEdge(img, RectD(rec), topLeftColor, bottomRightColor, penWidth); |
| 311 |
end; |
| 312 |
//------------------------------------------------------------------------------ |
| 313 |
|
| 314 |
procedure DrawEdge(img: TImage32; const rec: TRectD; |
| 315 |
topLeftColor, bottomRightColor: TColor32; penWidth: double = 1.0); |
| 316 |
var |
| 317 |
p: TPathD; |
| 318 |
c: TColor32; |
| 319 |
begin |
| 320 |
if penWidth = 0 then Exit |
| 321 |
else if penWidth < 0 then |
| 322 |
begin |
| 323 |
c := topLeftColor; |
| 324 |
topLeftColor := bottomRightColor; |
| 325 |
bottomRightColor := c; |
| 326 |
penWidth := -penWidth; |
| 327 |
end; |
| 328 |
if topLeftColor <> bottomRightColor then |
| 329 |
begin |
| 330 |
with rec do |
| 331 |
begin |
| 332 |
p := Img32.Vector.MakePath([left, bottom, left, top, right, top]); |
| 333 |
DrawLine(img, p, penWidth, topLeftColor, esButt); |
| 334 |
p := Img32.Vector.MakePath([right, top, right, bottom, left, bottom]); |
| 335 |
DrawLine(img, p, penWidth, bottomRightColor, esButt); |
| 336 |
end; |
| 337 |
end else |
| 338 |
DrawLine(img, Rectangle(rec), penWidth, topLeftColor, esPolygon); |
| 339 |
end; |
| 340 |
//------------------------------------------------------------------------------ |
| 341 |
|
| 342 |
procedure DrawEdge(img: TImage32; const path: TPathD; |
| 343 |
topLeftColor, bottomRightColor: TColor32; |
| 344 |
penWidth: double = 1.0; closePath: Boolean = true); |
| 345 |
var |
| 346 |
i, highI, deg: integer; |
| 347 |
frac: double; |
| 348 |
c: TColor32; |
| 349 |
p: TPathD; |
| 350 |
const |
| 351 |
RadToDeg = 180/PI; |
| 352 |
begin |
| 353 |
if penWidth = 0 then Exit |
| 354 |
else if penWidth < 0 then |
| 355 |
begin |
| 356 |
c := topLeftColor; |
| 357 |
topLeftColor := bottomRightColor; |
| 358 |
bottomRightColor := c; |
| 359 |
penWidth := -penWidth; |
| 360 |
end; |
| 361 |
highI := high(path); |
| 362 |
if highI < 2 then Exit; |
| 363 |
p := path; |
| 364 |
if closePath and not PointsNearEqual(p[0], p[highI], 0.01) then |
| 365 |
begin |
| 366 |
AppendPoint(p, p[0]); |
| 367 |
inc(highI); |
| 368 |
end; |
| 369 |
for i := 1 to highI do |
| 370 |
begin |
| 371 |
deg := Round(GetAngle(p[i-1], p[i]) * RadToDeg); |
| 372 |
case deg of |
| 373 |
-180..-136: frac := (-deg-135)/45; |
| 374 |
-135..0 : frac := 0; |
| 375 |
1..44 : frac := deg/45; |
| 376 |
else frac := 1; |
| 377 |
end; |
| 378 |
c := GradientColor(topLeftColor, bottomRightColor, frac); |
| 379 |
DrawLine(img, p[i-1], p[i], penWidth, c); |
| 380 |
end; |
| 381 |
end; |
| 382 |
//------------------------------------------------------------------------------ |
| 383 |
|
| 384 |
procedure FillColorHorz(img: TImage32; x, endX, y: integer; color: TColor32); |
| 385 |
var |
| 386 |
i,dx: integer; |
| 387 |
p: PColor32; |
| 388 |
begin |
| 389 |
if (x < 0) or (x >= img.Width) then Exit; |
| 390 |
if (y < 0) or (y >= img.Height) then Exit; |
| 391 |
p := img.PixelRow[y]; inc(p, x); |
| 392 |
if endX >= img.Width then endX := img.Width -1 |
| 393 |
else if endX < 0 then endX := 0; |
| 394 |
if endX < x then dx := -1 else dx := 1; |
| 395 |
for i := 0 to Abs(x-endX) do |
| 396 |
begin |
| 397 |
p^ := color; |
| 398 |
inc(p, dx); |
| 399 |
end; |
| 400 |
end; |
| 401 |
//------------------------------------------------------------------------------ |
| 402 |
|
| 403 |
procedure FillColorVert(img: TImage32; x, y, endY: integer; color: TColor32); |
| 404 |
var |
| 405 |
i, dy: integer; |
| 406 |
p: PColor32; |
| 407 |
begin |
| 408 |
if (x < 0) or (x >= img.Width) then Exit; |
| 409 |
if (y < 0) or (y >= img.Height) then Exit; |
| 410 |
p := img.PixelRow[y]; inc(p, x); |
| 411 |
if endY >= img.Height then |
| 412 |
endY := img.Height -1 else if endY < 0 then endY := 0; |
| 413 |
dy := img.Width; |
| 414 |
if endY < y then dy := -dy; |
| 415 |
for i := 0 to Abs(y - endY) do |
| 416 |
begin |
| 417 |
p^ := color; |
| 418 |
inc(p, dy); |
| 419 |
end; |
| 420 |
end; |
| 421 |
//------------------------------------------------------------------------------ |
| 422 |
|
| 423 |
procedure DrawShadowRect(img: TImage32; const rec: TRect; depth: double; |
| 424 |
angle: double = angle45; color: TColor32 = $80000000); |
| 425 |
var |
| 426 |
i,j, sX,sY: integer; |
| 427 |
l,t,r,b: integer; |
| 428 |
tmpImg: TImage32; |
| 429 |
tmpRec: TRect; |
| 430 |
xx,yy: double; |
| 431 |
ss: TPointD; |
| 432 |
c: TColor32; |
| 433 |
begin |
| 434 |
GetSinCos(angle, yy, xx); |
| 435 |
ss.X := depth * xx; |
| 436 |
ss.Y := depth * yy; |
| 437 |
sX := Abs(Round(ss.X)); |
| 438 |
sY := Abs(Round(ss.Y)); |
| 439 |
if rec.Left + ss.X < 0 then ss.X := -rec.Left |
| 440 |
else if rec.Right + ss.X > img.Width then ss.X := img.Width - rec.Right -1; |
| 441 |
if rec.Top + ss.Y < 0 then ss.Y := -rec.Top |
| 442 |
else if rec.Bottom + ss.Y > img.Height then ss.Y := img.Height -rec.Bottom -1; |
| 443 |
tmpImg := TImage32.Create(sX*3 +1, sY*3 +1); |
| 444 |
try |
| 445 |
i := sX div 2; j := sY div 2; |
| 446 |
DrawPolygon(tmpImg, Rectangle(i,j,i+sX*2,j+sY*2), frNonZero, color); |
| 447 |
FastGaussianBlur(tmpImg, tmpImg.Bounds, Round(sX/4),Round(sY/4), 1); |
| 448 |
// t-l corner |
| 449 |
if (ss.X < 0) or (ss.Y < 0) then |
| 450 |
begin |
| 451 |
tmpRec := Rect(0, 0, sX, sY); |
| 452 |
l := rec.Left; t := rec.Top; |
| 453 |
if ss.X < 0 then dec(l, sX); |
| 454 |
if ss.Y < 0 then dec(t, sY); |
| 455 |
img.Copy(tmpImg, tmpRec, Rect(l,t,l+sX,t+sY)); |
| 456 |
end; |
| 457 |
// t-r corner |
| 458 |
if (ss.X > 0) or (ss.Y < 0) then |
| 459 |
begin |
| 460 |
tmpRec := Rect(sX*2+1, 0, sX*3+1, sY); |
| 461 |
l := rec.Right; t := rec.Top; |
| 462 |
if ss.X < 0 then dec(l, sX); |
| 463 |
if ss.Y < 0 then dec(t, sY); |
| 464 |
img.Copy(tmpImg, tmpRec, Rect(l,t,l+sX,t+sY)); |
| 465 |
end; |
| 466 |
// b-l corner |
| 467 |
if (ss.X < 0) or (ss.Y > 0) then |
| 468 |
begin |
| 469 |
tmpRec := Rect(0, sY*2+1, sX, sY*3+1); |
| 470 |
l := rec.Left; t := rec.Bottom; |
| 471 |
if ss.X < 0 then dec(l, sX); |
| 472 |
if ss.Y < 0 then dec(t, sY); |
| 473 |
img.Copy(tmpImg, tmpRec, Rect(l,t,l+sX,t+sY)); |
| 474 |
end; |
| 475 |
// b-r corner |
| 476 |
if (ss.X > 0) or (ss.Y > 0) then |
| 477 |
begin |
| 478 |
tmpRec := Rect(sX*2+1, sY*2+1, sX*3+1, sY*3+1); |
| 479 |
l := rec.Right; t := rec.Bottom; |
| 480 |
if ss.X < 0 then dec(l, sX); |
| 481 |
if ss.Y < 0 then dec(t, sY); |
| 482 |
img.Copy(tmpImg, tmpRec, Rect(l,t,l+sX,t+sY)); |
| 483 |
end; |
| 484 |
// l-edge |
| 485 |
if (ss.X < 0) then |
| 486 |
begin |
| 487 |
l := rec.Left; t := rec.Top+sY; b := rec.Bottom-1; |
| 488 |
if ss.Y < 0 then begin dec(t, sY); dec(b,sY); end; |
| 489 |
for i := 1 to sX do |
| 490 |
begin |
| 491 |
c := tmpImg.Pixel[sX-i, sY+1]; |
| 492 |
FillColorVert(img, l-i, t, b, c); |
| 493 |
end; |
| 494 |
end; |
| 495 |
// t-edge |
| 496 |
if (ss.Y < 0) then |
| 497 |
begin |
| 498 |
l := rec.Left+sX; r := rec.Right-1; t := rec.Top; |
| 499 |
if ss.X < 0 then begin dec(l, sX); dec(r,sX); end; |
| 500 |
for i := 1 to sY do |
| 501 |
begin |
| 502 |
c := tmpImg.Pixel[sX+1, sY-i]; |
| 503 |
FillColorHorz(img, l, r, t-i, c); |
| 504 |
end; |
| 505 |
end; |
| 506 |
// r-edge |
| 507 |
if (ss.X > 0) then |
| 508 |
begin |
| 509 |
r := rec.Right-1; t := rec.Top+sY; b := rec.Bottom-1; |
| 510 |
if ss.Y < 0 then begin dec(t, sY); dec(b,sY); end; |
| 511 |
for i := 1 to sX do |
| 512 |
begin |
| 513 |
c := tmpImg.Pixel[sX*2+i, sY+1]; |
| 514 |
FillColorVert(img, r+i, t, b, c); |
| 515 |
end; |
| 516 |
end; |
| 517 |
// b-edge |
| 518 |
if (ss.Y > 0) then |
| 519 |
begin |
| 520 |
l := rec.Left+sX; r := rec.Right-1; b := rec.Bottom-1; |
| 521 |
if ss.X < 0 then begin dec(l, sX); dec(r,sX); end; |
| 522 |
for i := 1 to sY do |
| 523 |
begin |
| 524 |
c := tmpImg.Pixel[sX+1, sY*2+i]; |
| 525 |
FillColorHorz(img, l, r, b+i, c); |
| 526 |
end; |
| 527 |
end; |
| 528 |
finally |
| 529 |
tmpImg.Free; |
| 530 |
end; |
| 531 |
end; |
| 532 |
//------------------------------------------------------------------------------ |
| 533 |
|
| 534 |
procedure DrawShadow(img: TImage32; const polygon: TPathD; |
| 535 |
fillRule: TFillRule; depth: double; angleRads: double; |
| 536 |
color: TColor32; cutoutInsideShadow: Boolean); |
| 537 |
var |
| 538 |
polygons: TPathsD; |
| 539 |
begin |
| 540 |
setlength(polygons, 1); |
| 541 |
polygons[0] := polygon; |
| 542 |
DrawShadow(img, polygons, fillRule, depth, |
| 543 |
angleRads, color, cutoutInsideShadow); |
| 544 |
end; |
| 545 |
//------------------------------------------------------------------------------ |
| 546 |
|
| 547 |
procedure DrawShadow(img: TImage32; const polygons: TPathsD; |
| 548 |
fillRule: TFillRule; depth: double; angleRads: double; |
| 549 |
color: TColor32; cutoutInsideShadow: Boolean); |
| 550 |
var |
| 551 |
x, y: double; |
| 552 |
blurSize, w,h: integer; |
| 553 |
rec: TRect; |
| 554 |
polys, shadowPolys: TPathsD; |
| 555 |
shadowImg: TImage32; |
| 556 |
begin |
| 557 |
rec := GetBounds(polygons); |
| 558 |
if IsEmptyRect(rec) or (depth < 1) then Exit; |
| 559 |
{$IFDEF CLOCKWISE_ROTATION_WITH_NEGATIVE_ANGLES} |
| 560 |
angleRads := -angleRads; |
| 561 |
{$ENDIF} |
| 562 |
NormalizeAngle(angleRads); |
| 563 |
GetSinCos(angleRads, y, x); |
| 564 |
depth := depth * 0.5; |
| 565 |
x := depth * x; |
| 566 |
y := depth * y; |
| 567 |
blurSize := Max(1,Round(depth / 2)); |
| 568 |
Img32.Vector.InflateRect(rec, Ceil(depth*2), Ceil(depth*2)); |
| 569 |
polys := TranslatePath(polygons, -rec.Left, -rec.Top); |
| 570 |
shadowPolys := TranslatePath(polys, x, y); |
| 571 |
RectWidthHeight(rec, w, h); |
| 572 |
shadowImg := TImage32.Create(w, h); |
| 573 |
try |
| 574 |
DrawPolygon(shadowImg, shadowPolys, fillRule, color); |
| 575 |
FastGaussianBlur(shadowImg, shadowImg.Bounds, blurSize, 1); |
| 576 |
if cutoutInsideShadow then EraseInsidePaths(shadowImg, polys, fillRule); |
| 577 |
img.CopyBlend(shadowImg, shadowImg.Bounds, rec, BlendToAlphaLine); |
| 578 |
finally |
| 579 |
shadowImg.Free; |
| 580 |
end; |
| 581 |
end; |
| 582 |
//------------------------------------------------------------------------------ |
| 583 |
|
| 584 |
procedure DrawGlow(img: TImage32; const polygon: TPathD; |
| 585 |
fillRule: TFillRule; color: TColor32; blurRadius: integer); |
| 586 |
var |
| 587 |
polygons: TPathsD; |
| 588 |
begin |
| 589 |
setlength(polygons, 1); |
| 590 |
polygons[0] := polygon; |
| 591 |
DrawGlow(img, polygons, fillRule, color, blurRadius); |
| 592 |
end; |
| 593 |
//------------------------------------------------------------------------------ |
| 594 |
|
| 595 |
procedure DrawGlow(img: TImage32; const polygons: TPathsD; |
| 596 |
fillRule: TFillRule; color: TColor32; blurRadius: integer); |
| 597 |
var |
| 598 |
w,h: integer; |
| 599 |
rec: TRect; |
| 600 |
glowPolys: TPathsD; |
| 601 |
glowImg: TImage32; |
| 602 |
begin |
| 603 |
rec := GetBounds(polygons); |
| 604 |
glowPolys := TranslatePath(polygons, |
| 605 |
blurRadius -rec.Left +1, blurRadius -rec.Top +1); |
| 606 |
Img32.Vector.InflateRect(rec, blurRadius +1, blurRadius +1); |
| 607 |
RectWidthHeight(rec, w, h); |
| 608 |
glowImg := TImage32.Create(w, h); |
| 609 |
try |
| 610 |
DrawPolygon(glowImg, glowPolys, fillRule, color); |
| 611 |
FastGaussianBlur(glowImg, glowImg.Bounds, blurRadius, 2); |
| 612 |
glowImg.ScaleAlpha(4); |
| 613 |
img.CopyBlend(glowImg, glowImg.Bounds, rec, BlendToAlphaLine); |
| 614 |
finally |
| 615 |
glowImg.Free; |
| 616 |
end; |
| 617 |
end; |
| 618 |
//------------------------------------------------------------------------------ |
| 619 |
|
| 620 |
procedure Sharpen(img: TImage32; radius: Integer; amount: Integer); |
| 621 |
var |
| 622 |
i: Integer; |
| 623 |
amt: double; |
| 624 |
weightAmount: array [-255 .. 255] of Integer; |
| 625 |
bmpBlur: TImage32; |
| 626 |
pColor, pBlur: PARGB; |
| 627 |
begin |
| 628 |
if radius = 0 then Exit; |
| 629 |
amt := ClampRange(amount/10, 0.1, 5); |
| 630 |
radius := ClampRange(radius, 1, 10); |
| 631 |
for i := -255 to 255 do |
| 632 |
weightAmount[i] := Round(amt * i); |
| 633 |
bmpBlur := TImage32.Create(img); // clone self |
| 634 |
try |
| 635 |
pColor := PARGB(img.pixelBase); |
| 636 |
FastGaussianBlur(bmpBlur, bmpBlur.Bounds, radius, 2); |
| 637 |
pBlur := PARGB(bmpBlur.pixelBase); |
| 638 |
for i := 1 to img.Width * img.Height do |
| 639 |
begin |
| 640 |
if (pColor.A > 0) then |
| 641 |
begin |
| 642 |
pColor.R := ClampByte(pColor.R + weightAmount[pColor.R - pBlur.R]); |
| 643 |
pColor.G := ClampByte(pColor.G + weightAmount[pColor.G - pBlur.G]); |
| 644 |
pColor.B := ClampByte(pColor.B + weightAmount[pColor.B - pBlur.B]); |
| 645 |
end; |
| 646 |
Inc(pColor); Inc(pBlur); |
| 647 |
end; |
| 648 |
finally |
| 649 |
bmpBlur.Free; |
| 650 |
end; |
| 651 |
end; |
| 652 |
//------------------------------------------------------------------------------ |
| 653 |
|
| 654 |
procedure InternalHatchBackground(img: TImage32; const rec: TRect; |
| 655 |
color1, color2: TColor32; hatchSize: Integer = 10); |
| 656 |
var |
| 657 |
i, j, imgWidth: Integer; |
| 658 |
pc: PColor32; |
| 659 |
colors: array[boolean] of TColor32; |
| 660 |
hatch: Boolean; |
| 661 |
x: integer; |
| 662 |
begin |
| 663 |
colors[false] := color1; |
| 664 |
colors[true] := color2; |
| 665 |
imgWidth := img.Width; |
| 666 |
|
| 667 |
for i := rec.Top to rec.Bottom -1 do |
| 668 |
begin |
| 669 |
pc := @img.Pixels[i * imgWidth + rec.Left]; |
| 670 |
hatch := Odd(i div hatchSize); |
| 671 |
|
| 672 |
x := (rec.Left + 1) mod hatchSize; |
| 673 |
if x = 0 then hatch := not hatch; |
| 674 |
for j := rec.Left to rec.Right -1 do |
| 675 |
begin |
| 676 |
if pc^ = 0 then |
| 677 |
pc^ := colors[hatch] |
| 678 |
else if GetAlpha(pc^) < 255 then |
| 679 |
pc^ := BlendToOpaque(colors[hatch], pc^); |
| 680 |
inc(pc); |
| 681 |
inc(x); |
| 682 |
if x >= hatchSize then |
| 683 |
begin |
| 684 |
x := 0; |
| 685 |
hatch := not hatch; |
| 686 |
end; |
| 687 |
end; |
| 688 |
end; |
| 689 |
end; |
| 690 |
//------------------------------------------------------------------------------ |
| 691 |
|
| 692 |
procedure HatchBackground(img: TImage32; const rec: TRect; |
| 693 |
color1: TColor32 = clWhite32; color2: TColor32= $FFE8E8E8; |
| 694 |
hatchSize: Integer = 10); overload; |
| 695 |
begin |
| 696 |
if (rec.Right <= rec.Left) or (rec.Bottom - rec.Top <= 0) then Exit; |
| 697 |
img.BeginUpdate; |
| 698 |
try |
| 699 |
InternalHatchBackground(img, rec, color1, color2, hatchSize); |
| 700 |
finally |
| 701 |
img.EndUpdate; |
| 702 |
end; |
| 703 |
end; |
| 704 |
//------------------------------------------------------------------------------ |
| 705 |
|
| 706 |
procedure HatchBackground(img: TImage32; |
| 707 |
color1: TColor32; color2: TColor32; hatchSize: Integer); |
| 708 |
begin |
| 709 |
HatchBackground(img, img.Bounds, color1, color2, hatchSize); |
| 710 |
end; |
| 711 |
//------------------------------------------------------------------------------ |
| 712 |
|
| 713 |
procedure GridBackground(img: TImage32; majorInterval, minorInterval: integer; |
| 714 |
fillColor: TColor32; majColor: TColor32; minColor: TColor32); |
| 715 |
var |
| 716 |
i, x,y, w,h: integer; |
| 717 |
path: TPathD; |
| 718 |
cr: TCustomColorRenderer; |
| 719 |
begin |
| 720 |
img.Clear(fillColor); |
| 721 |
w := img.Width; h := img.Height; |
| 722 |
NewPointDArray(path, 2, True); |
| 723 |
|
| 724 |
if img.AntiAliased then |
| 725 |
cr := TColorRenderer.Create(minColor) else |
| 726 |
cr := TAliasedColorRenderer.Create(minColor); |
| 727 |
try |
| 728 |
if minorInterval > 0 then |
| 729 |
begin |
| 730 |
//cr.SetColor(minColor); |
| 731 |
|
| 732 |
x := minorInterval; |
| 733 |
path[0] := PointD(x, 0); path[1] := PointD(x, h);; |
| 734 |
for i := 1 to (w div minorInterval) do |
| 735 |
begin |
| 736 |
Img32.Draw.DrawLine(img, path, 1, cr, esSquare); |
| 737 |
path[0].X := path[0].X + minorInterval; |
| 738 |
path[1].X := path[1].X + minorInterval; |
| 739 |
end; |
| 740 |
y := minorInterval; |
| 741 |
path[0] := PointD(0, y); path[1] := PointD(w, y); |
| 742 |
for i := 1 to (h div minorInterval) do |
| 743 |
begin |
| 744 |
Img32.Draw.DrawLine(img, path, 1, cr, esSquare); |
| 745 |
path[0].Y := path[0].Y + minorInterval; |
| 746 |
path[1].Y := path[1].Y + minorInterval; |
| 747 |
end; |
| 748 |
end; |
| 749 |
if majorInterval > minorInterval then |
| 750 |
begin |
| 751 |
cr.SetColor(majColor); |
| 752 |
|
| 753 |
x := majorInterval; |
| 754 |
path[0] := PointD(x, 0); path[1] := PointD(x, h);; |
| 755 |
for i := 1 to (w div majorInterval) do |
| 756 |
begin |
| 757 |
Img32.Draw.DrawLine(img, path, 1, cr, esSquare); |
| 758 |
path[0].X := path[0].X + majorInterval; |
| 759 |
path[1].X := path[1].X + majorInterval; |
| 760 |
end; |
| 761 |
y := majorInterval; |
| 762 |
path[0] := PointD(0, y); path[1] := PointD(w, y); |
| 763 |
for i := 1 to (h div majorInterval) do |
| 764 |
begin |
| 765 |
Img32.Draw.DrawLine(img, path, 1, cr, esSquare); |
| 766 |
path[0].Y := path[0].Y + majorInterval; |
| 767 |
path[1].Y := path[1].Y + majorInterval; |
| 768 |
end; |
| 769 |
end; |
| 770 |
finally |
| 771 |
cr.Free; |
| 772 |
end; |
| 773 |
end; |
| 774 |
//------------------------------------------------------------------------------ |
| 775 |
|
| 776 |
function ColorDifference(color1, color2: TColor32): cardinal; |
| 777 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 778 |
var |
| 779 |
c1: TARGB absolute color1; |
| 780 |
c2: TARGB absolute color2; |
| 781 |
begin |
| 782 |
result := Abs(c1.R - c2.R) + Abs(c1.G - c2.G) + Abs(c1.B - c2.B); |
| 783 |
result := (result * 341) shr 10; // divide by 3 |
| 784 |
end; |
| 785 |
//------------------------------------------------------------------------------ |
| 786 |
|
| 787 |
procedure ReplaceExactColor(img: TImage32; oldColor, newColor: TColor32); |
| 788 |
var |
| 789 |
color: PColor32; |
| 790 |
i: Integer; |
| 791 |
begin |
| 792 |
color := img.PixelBase; |
| 793 |
for i := 0 to img.Width * img.Height -1 do |
| 794 |
begin |
| 795 |
if color^ = oldColor then color^ := newColor; |
| 796 |
inc(color); |
| 797 |
end; |
| 798 |
end; |
| 799 |
//------------------------------------------------------------------------------ |
| 800 |
|
| 801 |
procedure RemoveColor(img: TImage32; color: TColor32); |
| 802 |
var |
| 803 |
fg: TARGB absolute color; |
| 804 |
bg: PARGB; |
| 805 |
i: Integer; |
| 806 |
Q: byte; |
| 807 |
begin |
| 808 |
if fg.A = 0 then Exit; |
| 809 |
bg := PARGB(img.PixelBase); |
| 810 |
for i := 0 to img.Width * img.Height -1 do |
| 811 |
begin |
| 812 |
if bg.A > 0 then |
| 813 |
begin |
| 814 |
// red |
| 815 |
if (bg.R > fg.R) then Q := bg.R - fg.R |
| 816 |
else if (bg.R < fg.R) then Q := DivTable[fg.R - bg.R, fg.R] |
| 817 |
else Q := 0; |
| 818 |
// green |
| 819 |
if (bg.G > fg.G) then Q := Max(Q, bg.G - fg.G) |
| 820 |
else if (bg.G < fg.G) then Q := Max(Q, DivTable[fg.G - bg.G, fg.G]); |
| 821 |
// blue |
| 822 |
if (bg.B > fg.B) then Q := Max(Q, bg.B - fg.B) |
| 823 |
else if (bg.B < fg.B) then Q := Max(Q, DivTable[fg.B - bg.B, fg.B]); |
| 824 |
|
| 825 |
// weight Q toward either fully opaque or fully translucent |
| 826 |
Q := Sigmoid[Q]; |
| 827 |
|
| 828 |
if (Q = 0) then |
| 829 |
bg.Color := clNone32 |
| 830 |
else if (Q < 255) then |
| 831 |
begin |
| 832 |
bg.A := MulTable[bg.A, Q]; |
| 833 |
bg.R := DivTable[bg.R - MulTable[not Q, fg.R], Q]; |
| 834 |
bg.G := DivTable[bg.G - MulTable[not Q, fg.G], Q]; |
| 835 |
bg.B := DivTable[bg.B - MulTable[not Q, fg.B], Q]; |
| 836 |
end; |
| 837 |
end; |
| 838 |
inc(bg); |
| 839 |
end; |
| 840 |
end; |
| 841 |
//------------------------------------------------------------------------------ |
| 842 |
|
| 843 |
procedure FilterOnColor(img: TImage32; color: TColor32); |
| 844 |
var |
| 845 |
fg: TARGB absolute color; |
| 846 |
bg: PARGB; |
| 847 |
i: Integer; |
| 848 |
Q: byte; |
| 849 |
begin |
| 850 |
if fg.A = 0 then Exit; |
| 851 |
bg := PARGB(img.PixelBase); |
| 852 |
for i := 0 to img.Width * img.Height -1 do |
| 853 |
begin |
| 854 |
if bg.A > 0 then |
| 855 |
begin |
| 856 |
// red |
| 857 |
if (bg.R > fg.R) then |
| 858 |
Q := bg.R - fg.R |
| 859 |
else if (bg.R < fg.R) then |
| 860 |
Q := DivTable[fg.R - bg.R, fg.R] |
| 861 |
else |
| 862 |
Q := 0; |
| 863 |
|
| 864 |
// green |
| 865 |
if (bg.G > fg.G) then |
| 866 |
Q := Max(Q, bg.G - fg.G) |
| 867 |
else if (bg.G < fg.G) then |
| 868 |
Q := Max(Q, DivTable[fg.G - bg.G, fg.G]); |
| 869 |
|
| 870 |
// blue |
| 871 |
if (bg.B > fg.B) then |
| 872 |
Q := Max(Q, bg.B - fg.B) |
| 873 |
else if (bg.B < fg.B) then |
| 874 |
Q := Max(Q, DivTable[fg.B - bg.B, fg.B]); |
| 875 |
|
| 876 |
// weight Q toward either fully opaque or fully translucent |
| 877 |
Q := Sigmoid[Q]; |
| 878 |
|
| 879 |
Q := MulTable[bg.A, not Q]; |
| 880 |
bg.Color := color; |
| 881 |
bg.A := Q; // note: fg.A is ignored |
| 882 |
end; |
| 883 |
inc(bg); |
| 884 |
end; |
| 885 |
end; |
| 886 |
//------------------------------------------------------------------------------ |
| 887 |
|
| 888 |
procedure FilterOnExactColor(img: TImage32; color: TColor32); |
| 889 |
var |
| 890 |
pc: PColor32; |
| 891 |
i: Integer; |
| 892 |
mask: TColor32; |
| 893 |
begin |
| 894 |
// alpha channel is ignored |
| 895 |
mask := $FFFFFF; |
| 896 |
color := color and mask; |
| 897 |
|
| 898 |
pc := img.PixelBase; |
| 899 |
for i := 0 to img.Width * img.Height -1 do |
| 900 |
begin |
| 901 |
if (pc^ and mask) <> color then pc^ := clNone32; |
| 902 |
inc(pc); |
| 903 |
end; |
| 904 |
end; |
| 905 |
//------------------------------------------------------------------------------ |
| 906 |
|
| 907 |
procedure FilterOnAlpha(img: TImage32; alpha: byte; tolerance: byte); |
| 908 |
var |
| 909 |
bg: PARGB; |
| 910 |
i: Integer; |
| 911 |
begin |
| 912 |
bg := PARGB(img.PixelBase); |
| 913 |
for i := 0 to img.Width * img.Height -1 do |
| 914 |
begin |
| 915 |
if abs(bg.A - alpha) > tolerance then bg.A := 0; |
| 916 |
inc(bg); |
| 917 |
end; |
| 918 |
end; |
| 919 |
//------------------------------------------------------------------------------ |
| 920 |
|
| 921 |
procedure RedEyeRemove(img: TImage32; const rect: TRect); |
| 922 |
var |
| 923 |
k: integer; |
| 924 |
cutout, mask: TImage32; |
| 925 |
path: TPathD; |
| 926 |
cutoutRec, rect3: TRect; |
| 927 |
radGrad: TRadialGradientRenderer; |
| 928 |
begin |
| 929 |
k := RectWidth(rect) * RectHeight(rect); |
| 930 |
if k < 120 then k := 2 |
| 931 |
else if k < 230 then k := 3 |
| 932 |
else k := 4; |
| 933 |
cutoutRec := rect; |
| 934 |
Img32.Vector.InflateRect(cutoutRec, k, k); |
| 935 |
cutout := TImage32.Create(img, cutoutRec); |
| 936 |
mask := TImage32.Create(cutout.Width, cutout.Height); |
| 937 |
radGrad := TRadialGradientRenderer.Create; |
| 938 |
try |
| 939 |
// fill behind the cutout with black also |
| 940 |
// blurring the fill to soften its edges |
| 941 |
rect3 := cutout.Bounds; |
| 942 |
Img32.Vector.InflateRect(rect3, -k, -k); |
| 943 |
path := Ellipse(rect3); |
| 944 |
DrawPolygon(mask, path, frNonZero, clBlack32); |
| 945 |
// given the very small area and small radius of the blur, the |
| 946 |
// speed improvement of BoxBlur over GaussianBlur is inconsequential. |
| 947 |
GaussianBlur(mask, mask.Bounds, k); |
| 948 |
img.CopyBlend(mask, mask.Bounds, cutoutRec, BlendToOpaque); |
| 949 |
// gradient fill to clNone32 a mask to soften cutout's edges |
| 950 |
path := Ellipse(cutoutRec); |
| 951 |
radGrad.SetParameters(rect3, clBlack32, clNone32); |
| 952 |
DrawPolygon(mask, path, frNonZero, radGrad); |
| 953 |
cutout.CopyBlend(mask, mask.Bounds, cutout.Bounds, BlendMaskLine); |
| 954 |
// now remove red from the cutout |
| 955 |
RemoveColor(cutout, clRed32); |
| 956 |
// finally replace the cutout ... |
| 957 |
img.CopyBlend(cutout, cutout.Bounds, cutoutRec, BlendToOpaque); |
| 958 |
finally |
| 959 |
mask.Free; |
| 960 |
cutout.Free; |
| 961 |
radGrad.Free; |
| 962 |
end; |
| 963 |
end; |
| 964 |
//------------------------------------------------------------------------------ |
| 965 |
|
| 966 |
procedure EraseInsidePath(img: TImage32; const path: TPathD; fillRule: TFillRule); |
| 967 |
begin |
| 968 |
if assigned(path) then |
| 969 |
ErasePolygon(img, path, fillRule); |
| 970 |
end; |
| 971 |
//------------------------------------------------------------------------------ |
| 972 |
|
| 973 |
procedure EraseInsidePaths(img: TImage32; const paths: TPathsD; fillRule: TFillRule); |
| 974 |
begin |
| 975 |
if assigned(paths) then |
| 976 |
ErasePolygon(img, paths, fillRule); |
| 977 |
end; |
| 978 |
//------------------------------------------------------------------------------ |
| 979 |
|
| 980 |
procedure EraseOutsideRect(img: TImage32; const r, outsideBounds: TRect); |
| 981 |
begin |
| 982 |
// Fill the parts, that are in outsideBounds but not in r with zeros |
| 983 |
|
| 984 |
// whole top block |
| 985 |
if r.Top > outsideBounds.Top then |
| 986 |
img.FillRect(Rect(outsideBounds.Left, outsideBounds.Top, outsideBounds.Right, r.Top - 1), 0); |
| 987 |
// whole bottom block |
| 988 |
if r.Bottom < outsideBounds.Bottom then |
| 989 |
img.FillRect(Rect(outsideBounds.Left, r.Bottom + 1, outsideBounds.Right, outsideBounds.Bottom), 0); |
| 990 |
|
| 991 |
// remaining left block |
| 992 |
if r.Left > outsideBounds.Left then |
| 993 |
img.FillRect(Rect(outsideBounds.Left, r.Top, r.Left - 1, r.Bottom), 0); |
| 994 |
// remaining right block |
| 995 |
if r.Right < outsideBounds.Right then |
| 996 |
img.FillRect(Rect(r.Right + 1, r.Top, outsideBounds.Right, r.Bottom), 0); |
| 997 |
end; |
| 998 |
//------------------------------------------------------------------------------ |
| 999 |
|
| 1000 |
procedure EraseOutsidePath(img: TImage32; const path: TPathD; |
| 1001 |
fillRule: TFillRule; const outsideBounds: TRect); |
| 1002 |
var |
| 1003 |
w, h: integer; |
| 1004 |
renderer: TMaskRenderer; |
| 1005 |
r: TRect; |
| 1006 |
polygons: TPathsD; |
| 1007 |
begin |
| 1008 |
if not assigned(path) then Exit; |
| 1009 |
RectWidthHeight(outsideBounds, w, h); |
| 1010 |
if (w <= 0) or (h <= 0) then Exit; |
| 1011 |
|
| 1012 |
// We can skip the costly polygon rasterization if the path is |
| 1013 |
// a rectangle |
| 1014 |
if (fillRule in [frEvenOdd, frNonZero]) and IsSimpleRectanglePath(path, r) then |
| 1015 |
begin |
| 1016 |
EraseOutsideRect(img, r, outsideBounds); |
| 1017 |
Exit; |
| 1018 |
end; |
| 1019 |
|
| 1020 |
renderer := TMaskRenderer.Create; |
| 1021 |
try |
| 1022 |
SetLength(polygons, 1); |
| 1023 |
polygons[0] := path; |
| 1024 |
Rasterize(img, polygons, outsideBounds, fillRule, renderer); |
| 1025 |
finally |
| 1026 |
renderer.Free; |
| 1027 |
end; |
| 1028 |
end; |
| 1029 |
//------------------------------------------------------------------------------ |
| 1030 |
|
| 1031 |
procedure EraseOutsidePaths(img: TImage32; const paths: TPathsD; |
| 1032 |
fillRule: TFillRule; const outsideBounds: TRect; |
| 1033 |
rendererCache: TCustomRendererCache); |
| 1034 |
var |
| 1035 |
w, h: integer; |
| 1036 |
renderer: TMaskRenderer; |
| 1037 |
r: TRect; |
| 1038 |
begin |
| 1039 |
if not assigned(paths) then Exit; |
| 1040 |
RectWidthHeight(outsideBounds, w, h); |
| 1041 |
if (w <= 0) or (h <= 0) then Exit; |
| 1042 |
|
| 1043 |
// We can skip the costly polygon rasterization if the path is |
| 1044 |
// a rectangle. |
| 1045 |
if (fillRule in [frEvenOdd, frNonZero]) and IsSimpleRectanglePath(paths, r) then |
| 1046 |
begin |
| 1047 |
EraseOutsideRect(img, r, outsideBounds); |
| 1048 |
Exit; |
| 1049 |
end; |
| 1050 |
|
| 1051 |
if rendererCache = nil then |
| 1052 |
renderer := TMaskRenderer.Create |
| 1053 |
else |
| 1054 |
renderer := rendererCache.MaskRenderer; |
| 1055 |
try |
| 1056 |
Rasterize(img, paths, outsideBounds, fillRule, renderer); |
| 1057 |
finally |
| 1058 |
if rendererCache = nil then |
| 1059 |
renderer.Free; |
| 1060 |
end; |
| 1061 |
end; |
| 1062 |
//------------------------------------------------------------------------------ |
| 1063 |
|
| 1064 |
procedure Draw3D(img: TImage32; const polygon: TPathD; |
| 1065 |
fillRule: TFillRule; height, blurRadius: double; |
| 1066 |
colorLt: TColor32; colorDk: TColor32; angleRads: double); |
| 1067 |
var |
| 1068 |
polygons: TPathsD; |
| 1069 |
begin |
| 1070 |
setLength(polygons, 1); |
| 1071 |
polygons[0] := polygon; |
| 1072 |
Draw3D(img, polygons, fillRule, height, blurRadius, colorLt, colorDk, angleRads); |
| 1073 |
end; |
| 1074 |
//------------------------------------------------------------------------------ |
| 1075 |
|
| 1076 |
procedure Draw3D(img: TImage32; const polygons: TPathsD; |
| 1077 |
fillRule: TFillRule; height, blurRadius: double; |
| 1078 |
colorLt: TColor32; colorDk: TColor32; angleRads: double); |
| 1079 |
var |
| 1080 |
tmp: TImage32; |
| 1081 |
rec: TRect; |
| 1082 |
paths, paths2: TPathsD; |
| 1083 |
w,h: integer; |
| 1084 |
x,y: double; |
| 1085 |
begin |
| 1086 |
rec := GetBounds(polygons); |
| 1087 |
if IsEmptyRect(rec) then Exit; |
| 1088 |
{$IFDEF CLOCKWISE_ROTATION_WITH_NEGATIVE_ANGLES} |
| 1089 |
angleRads := -angleRads; |
| 1090 |
{$ENDIF} |
| 1091 |
GetSinCos(angleRads, y, x); |
| 1092 |
paths := TranslatePath(polygons, -rec.Left, -rec.Top); |
| 1093 |
RectWidthHeight(rec, w, h); |
| 1094 |
tmp := TImage32.Create(w, h); |
| 1095 |
try |
| 1096 |
if GetAlpha(colorLt) > 0 then |
| 1097 |
begin |
| 1098 |
tmp.Clear(colorLt); |
| 1099 |
paths2 := TranslatePath(paths, -height*x, -height*y); |
| 1100 |
EraseInsidePaths(tmp, paths2, fillRule); |
| 1101 |
FastGaussianBlur(tmp, tmp.Bounds, Round(blurRadius), 0); |
| 1102 |
EraseOutsidePaths(tmp, paths, fillRule, tmp.Bounds); |
| 1103 |
img.CopyBlend(tmp, tmp.Bounds, rec, BlendToAlphaLine); |
| 1104 |
end; |
| 1105 |
if GetAlpha(colorDk) > 0 then |
| 1106 |
begin |
| 1107 |
tmp.Clear(colorDk); |
| 1108 |
paths2 := TranslatePath(paths, height*x, height*y); |
| 1109 |
EraseInsidePaths(tmp, paths2, fillRule); |
| 1110 |
FastGaussianBlur(tmp, tmp.Bounds, Round(blurRadius), 0); |
| 1111 |
EraseOutsidePaths(tmp, paths, fillRule, tmp.Bounds); |
| 1112 |
img.CopyBlend(tmp, tmp.Bounds, rec, BlendToAlphaLine); |
| 1113 |
end; |
| 1114 |
finally |
| 1115 |
tmp.Free; |
| 1116 |
end; |
| 1117 |
end; |
| 1118 |
//------------------------------------------------------------------------------ |
| 1119 |
|
| 1120 |
function RainbowColor(fraction: double; luminance: byte = 128): TColor32; |
| 1121 |
var |
| 1122 |
hsl: THsl; |
| 1123 |
begin |
| 1124 |
if (fraction < 0) or (fraction > 1) then |
| 1125 |
fraction := frac(fraction); |
| 1126 |
|
| 1127 |
hsl.hue := Round(fraction * 255); |
| 1128 |
hsl.sat := 255; |
| 1129 |
hsl.lum := luminance; |
| 1130 |
hsl.alpha := 255; |
| 1131 |
Result := HslToRgb(hsl); |
| 1132 |
end; |
| 1133 |
//------------------------------------------------------------------------------ |
| 1134 |
|
| 1135 |
function GradientColor(color1, color2: TColor32; frac: single): TColor32; |
| 1136 |
var |
| 1137 |
hsl1, hsl2: THsl; |
| 1138 |
begin |
| 1139 |
if (frac <= 0) then result := color1 |
| 1140 |
else if (frac >= 1) then result := color2 |
| 1141 |
else |
| 1142 |
begin |
| 1143 |
hsl1 := RgbToHsl(color1); hsl2 := RgbToHsl(color2); |
| 1144 |
hsl1.hue := ClampByte(hsl1.hue*(1-frac) + hsl2.hue*frac); |
| 1145 |
hsl1.sat := ClampByte(hsl1.sat*(1-frac) + hsl2.sat*frac); |
| 1146 |
hsl1.lum := ClampByte(hsl1.lum*(1-frac) + hsl2.lum*frac); |
| 1147 |
hsl1.alpha := ClampByte(hsl1.alpha*(1-frac) + hsl2.alpha*frac); |
| 1148 |
Result := HslToRgb(hsl1); |
| 1149 |
end; |
| 1150 |
end; |
| 1151 |
//------------------------------------------------------------------------------ |
| 1152 |
|
| 1153 |
function MakeDarker(color: TColor32; percent: cardinal): TColor32; |
| 1154 |
var |
| 1155 |
hsl: THsl; |
| 1156 |
begin |
| 1157 |
hsl := RgbToHsl(color); |
| 1158 |
hsl.lum := ClampByte(hsl.lum - (percent/100 * hsl.lum)); |
| 1159 |
Result := HslToRgb(hsl); |
| 1160 |
end; |
| 1161 |
//------------------------------------------------------------------------------ |
| 1162 |
|
| 1163 |
function MakeLighter(color: TColor32; percent: cardinal): TColor32; |
| 1164 |
var |
| 1165 |
hsl: THsl; |
| 1166 |
begin |
| 1167 |
hsl := RgbToHsl(color); |
| 1168 |
hsl.lum := ClampByte(hsl.lum + percent/100 * (255 - hsl.lum)); |
| 1169 |
Result := HslToRgb(hsl); |
| 1170 |
end; |
| 1171 |
//------------------------------------------------------------------------------ |
| 1172 |
|
| 1173 |
function DrawButton(img: TImage32; const pt: TPointD; |
| 1174 |
size: double; color: TColor32; buttonShape: TButtonShape; |
| 1175 |
buttonAttributes: TButtonAttributes): TPathD; |
| 1176 |
var |
| 1177 |
i: integer; |
| 1178 |
radius: double; |
| 1179 |
rec: TRectD; |
| 1180 |
lightSize, lightAngle: double; |
| 1181 |
begin |
| 1182 |
if (size < 5) then Exit; |
| 1183 |
radius := size * 0.5; |
| 1184 |
lightSize := radius * 0.25; |
| 1185 |
rec := RectD(pt.X -radius, pt.Y -radius, pt.X +radius, pt.Y +radius); |
| 1186 |
if baEraseBeneath in buttonAttributes then |
| 1187 |
img.Clear(Rect(rec)); |
| 1188 |
case buttonShape of |
| 1189 |
bsDiamond: |
| 1190 |
begin |
| 1191 |
NewPointDArray(Result, 4, True); |
| 1192 |
for i := 0 to 3 do Result[i] := pt; |
| 1193 |
Result[0].X := Result[0].X -radius; |
| 1194 |
Result[1].Y := Result[1].Y -radius; |
| 1195 |
Result[2].X := Result[2].X +radius; |
| 1196 |
Result[3].Y := Result[3].Y +radius; |
| 1197 |
end; |
| 1198 |
bsSquare: |
| 1199 |
begin |
| 1200 |
Img32.Vector.InflateRect(rec, -1,-1); |
| 1201 |
Result := Rectangle(rec); |
| 1202 |
end; |
| 1203 |
else |
| 1204 |
Result := Ellipse(rec); |
| 1205 |
end; |
| 1206 |
lightAngle := angle225; |
| 1207 |
img.BeginUpdate; |
| 1208 |
try |
| 1209 |
// nb: only need to cutout the inside shadow if |
| 1210 |
// the pending color fill is semi-transparent |
| 1211 |
if baShadow in buttonAttributes then |
| 1212 |
DrawShadow(img, Result, frNonZero, lightSize *2, |
| 1213 |
(lightAngle + angle180), $AA000000, GetAlpha(color) < $FE); |
| 1214 |
if GetAlpha(color) > 2 then |
| 1215 |
DrawPolygon(img, Result, frNonZero, color); |
| 1216 |
if ba3D in buttonAttributes then |
| 1217 |
Draw3D(img, Result, frNonZero, lightSize*2, |
| 1218 |
Ceil(lightSize), $CCFFFFFF, $AA000000, lightAngle); |
| 1219 |
DrawLine(img, Result, dpiAware1, clBlack32, esPolygon); |
| 1220 |
finally |
| 1221 |
img.EndUpdate; |
| 1222 |
end; |
| 1223 |
end; |
| 1224 |
//------------------------------------------------------------------------------ |
| 1225 |
|
| 1226 |
function AlphaAverage(color1, color2: TColor32): cardinal; |
| 1227 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 1228 |
var |
| 1229 |
c1: TARGB absolute color1; |
| 1230 |
c2: TARGB absolute color2; |
| 1231 |
begin |
| 1232 |
result := (c1.A + c2.A) shr 1; |
| 1233 |
end; |
| 1234 |
//------------------------------------------------------------------------------ |
| 1235 |
|
| 1236 |
function BlendAverage(bgColor, fgColor: TColor32): TColor32; |
| 1237 |
var |
| 1238 |
res: TARGB absolute Result; |
| 1239 |
bg: TARGB absolute bgColor; |
| 1240 |
fg: TARGB absolute fgColor; |
| 1241 |
begin |
| 1242 |
res.A := (fg.A + bg.A) shr 1; |
| 1243 |
res.R := (fg.R + bg.R) shr 1; |
| 1244 |
res.G := (fg.G + bg.G) shr 1; |
| 1245 |
res.B := (fg.B + bg.B) shr 1; |
| 1246 |
end; |
| 1247 |
//------------------------------------------------------------------------------ |
| 1248 |
|
| 1249 |
function BlendLinearBurn(bgColor, fgColor: TColor32): TColor32; |
| 1250 |
var |
| 1251 |
res: TARGB absolute Result; |
| 1252 |
bg: TARGB absolute bgColor; |
| 1253 |
fg: TARGB absolute fgColor; |
| 1254 |
begin |
| 1255 |
res.A := 255; |
| 1256 |
res.R := Max(0, bg.R + fg.R - 255); |
| 1257 |
res.G := Max(0, bg.G + fg.G - 255); |
| 1258 |
res.B := Max(0, bg.B + fg.B - 255); |
| 1259 |
end; |
| 1260 |
//------------------------------------------------------------------------------ |
| 1261 |
|
| 1262 |
function BlendColorDodge(bgColor, fgColor: TColor32): TColor32; |
| 1263 |
var |
| 1264 |
res: TARGB absolute Result; |
| 1265 |
bg: TARGB absolute bgColor; |
| 1266 |
fg: TARGB absolute fgColor; |
| 1267 |
begin |
| 1268 |
res.A := 255; |
| 1269 |
res.R := DivTable[bg.R, not fg.R]; |
| 1270 |
res.G := DivTable[bg.G, not fg.G]; |
| 1271 |
res.B := DivTable[bg.B, not fg.B]; |
| 1272 |
end; |
| 1273 |
//------------------------------------------------------------------------------ |
| 1274 |
|
| 1275 |
procedure PencilEffect(img: TImage32; intensity: integer); |
| 1276 |
var |
| 1277 |
img2: TImage32; |
| 1278 |
begin |
| 1279 |
if img.IsEmpty then Exit; |
| 1280 |
intensity := max(1, min(10, intensity)); |
| 1281 |
img.Grayscale; |
| 1282 |
img2 := TImage32.Create(img); |
| 1283 |
try |
| 1284 |
img2.InvertColors; |
| 1285 |
FastGaussianBlur(img2, img2.Bounds, intensity, 2); |
| 1286 |
img.CopyBlend(img2, img2.Bounds, img.Bounds, BlendColorDodge); |
| 1287 |
finally |
| 1288 |
img2.Free; |
| 1289 |
end; |
| 1290 |
end; |
| 1291 |
//------------------------------------------------------------------------------ |
| 1292 |
|
| 1293 |
procedure TraceContours(img: TImage32; intensity: integer); |
| 1294 |
var |
| 1295 |
i,j, w,h: integer; |
| 1296 |
tmp, tmp2: TArrayOfColor32; |
| 1297 |
s, s2: PColor32; |
| 1298 |
d: PARGB; |
| 1299 |
begin |
| 1300 |
w := img.Width; h := img.Height; |
| 1301 |
if w * h = 0 then Exit; |
| 1302 |
NewColor32Array(tmp, w * h); |
| 1303 |
NewColor32Array(tmp2, w * h); |
| 1304 |
s := img.PixelRow[0]; d := @tmp[0]; |
| 1305 |
for j := 0 to h-1 do |
| 1306 |
begin |
| 1307 |
s2 := IncPColor32(s, 1); |
| 1308 |
for i := 0 to w-2 do |
| 1309 |
begin |
| 1310 |
d.A := ColorDifference(s^, s2^); |
| 1311 |
inc(s); inc(s2); inc(d); |
| 1312 |
end; |
| 1313 |
inc(s); inc(d); |
| 1314 |
end; |
| 1315 |
for j := 0 to w-1 do |
| 1316 |
begin |
| 1317 |
s := @tmp[j]; d := @tmp2[j]; |
| 1318 |
s2 := IncPColor32(s, w); |
| 1319 |
for i := 0 to h-2 do |
| 1320 |
begin |
| 1321 |
d.A := AlphaAverage(s^, s2^); |
| 1322 |
inc(s, w); inc(s2, w); inc(d, w); |
| 1323 |
end; |
| 1324 |
end; |
| 1325 |
|
| 1326 |
img.BlockNotify; |
| 1327 |
img.AssignPixelArray(tmp2, w, h); |
| 1328 |
img.UnblockNotify; |
| 1329 |
|
| 1330 |
if intensity < 1 then Exit; |
| 1331 |
if intensity > 10 then |
| 1332 |
intensity := 10; // range = 1-10 |
| 1333 |
img.ScaleAlpha(intensity); |
| 1334 |
end; |
| 1335 |
//------------------------------------------------------------------------------ |
| 1336 |
// FLOODFILL - AND SUPPORT FUNCTIONS |
| 1337 |
//------------------------------------------------------------------------------ |
| 1338 |
type |
| 1339 |
PFloodFillRec = ^TFloodFillRec; |
| 1340 |
TFloodFillRec = record |
| 1341 |
xLeft : Integer; |
| 1342 |
xRight : Integer; |
| 1343 |
y : Integer; |
| 1344 |
dirY : Integer; |
| 1345 |
next : PFloodFillRec; |
| 1346 |
end; |
| 1347 |
TFloodFillStack = class |
| 1348 |
first : PFloodFillRec; |
| 1349 |
maxY : integer; |
| 1350 |
constructor Create(maxY: integer); |
| 1351 |
destructor Destroy; override; |
| 1352 |
procedure Push(xLeft, xRight,y, direction: Integer); |
| 1353 |
procedure Pop(out xLeft, xRight,y, direction: Integer); |
| 1354 |
function IsEmpty: Boolean; |
| 1355 |
end; |
| 1356 |
TFloodFillMask = class |
| 1357 |
private |
| 1358 |
img : TImage32; |
| 1359 |
mask : TImage32; |
| 1360 |
colorsRow : PColor32Array; |
| 1361 |
maskRow : PColor32Array; |
| 1362 |
initialColor : TColor32; |
| 1363 |
compareFunc : TCompareFunctionEx; |
| 1364 |
tolerance : Integer; |
| 1365 |
public |
| 1366 |
function Execute(imgIn, imgMaskOut: TImage32; x,y: integer; |
| 1367 |
aTolerance: Byte = 0; compFunc: TCompareFunctionEx = nil): Boolean; |
| 1368 |
procedure SetCurrentY(y: Integer); |
| 1369 |
function IsMatch(x: Integer): Boolean; |
| 1370 |
end; |
| 1371 |
//------------------------------------------------------------------------------ |
| 1372 |
// TFloodFillStack methods |
| 1373 |
//------------------------------------------------------------------------------ |
| 1374 |
constructor TFloodFillStack.Create(maxY: integer); |
| 1375 |
begin |
| 1376 |
self.maxY := maxY; |
| 1377 |
end; |
| 1378 |
//------------------------------------------------------------------------------ |
| 1379 |
destructor TFloodFillStack.Destroy; |
| 1380 |
var |
| 1381 |
ffr: PFloodFillRec; |
| 1382 |
begin |
| 1383 |
while assigned(first) do |
| 1384 |
begin |
| 1385 |
ffr := first; |
| 1386 |
first := first.next; |
| 1387 |
dispose(ffr); |
| 1388 |
end; |
| 1389 |
end; |
| 1390 |
//------------------------------------------------------------------------------ |
| 1391 |
|
| 1392 |
procedure TFloodFillStack.Push(xLeft, xRight, y, direction: Integer); |
| 1393 |
var |
| 1394 |
ffr: PFloodFillRec; |
| 1395 |
begin |
| 1396 |
if ((y <= 0) and (direction = -1)) or |
| 1397 |
((y >= maxY) and (direction = 1)) then Exit; |
| 1398 |
new(ffr); |
| 1399 |
ffr.xLeft := xLeft; |
| 1400 |
ffr.xRight := xRight; |
| 1401 |
ffr.y := y; |
| 1402 |
ffr.dirY := direction; |
| 1403 |
ffr.next := first; |
| 1404 |
first := ffr; |
| 1405 |
end; |
| 1406 |
//------------------------------------------------------------------------------ |
| 1407 |
|
| 1408 |
procedure TFloodFillStack.Pop(out xLeft, xRight, y, direction: Integer); |
| 1409 |
var |
| 1410 |
ffr: PFloodFillRec; |
| 1411 |
begin |
| 1412 |
xLeft := first.xLeft; |
| 1413 |
xRight := first.xRight; |
| 1414 |
direction := first.dirY; |
| 1415 |
y := first.y + direction; |
| 1416 |
ffr := first; |
| 1417 |
first := first.next; |
| 1418 |
dispose(ffr); |
| 1419 |
end; |
| 1420 |
//------------------------------------------------------------------------------ |
| 1421 |
|
| 1422 |
function TFloodFillStack.IsEmpty: Boolean; |
| 1423 |
begin |
| 1424 |
result := not assigned(first); |
| 1425 |
end; |
| 1426 |
//------------------------------------------------------------------------------ |
| 1427 |
// TFloodFillMask methods |
| 1428 |
//------------------------------------------------------------------------------ |
| 1429 |
|
| 1430 |
function TFloodFillMask.Execute(imgIn, imgMaskOut: TImage32; x,y: integer; |
| 1431 |
aTolerance: Byte; compFunc: TCompareFunctionEx): Boolean; |
| 1432 |
var |
| 1433 |
ffs : TFloodFillStack; |
| 1434 |
w,h : integer; |
| 1435 |
xl, xr, xr2 : Integer; |
| 1436 |
maxX : Integer; |
| 1437 |
dirY : Integer; |
| 1438 |
begin |
| 1439 |
Result := Assigned(imgIn) and Assigned(imgMaskOut) and |
| 1440 |
InRange(x,0,imgIn.Width -1) and InRange(y,0,imgIn.Height -1); |
| 1441 |
if not Result then Exit; |
| 1442 |
w := imgIn.Width; h := imgIn.Height; |
| 1443 |
// make sure the mask is the size of the image |
| 1444 |
imgMaskOut.SetSize(w,h); |
| 1445 |
img := imgIn; |
| 1446 |
mask := imgMaskOut; |
| 1447 |
compareFunc := compFunc; |
| 1448 |
tolerance := aTolerance; |
| 1449 |
maxX := w -1; |
| 1450 |
ffs := TFloodFillStack.create(h -1); |
| 1451 |
try |
| 1452 |
initialColor := imgIn.Pixel[x, y]; |
| 1453 |
xl := x; xr := x; |
| 1454 |
SetCurrentY(y); |
| 1455 |
IsMatch(x); |
| 1456 |
while (xl > 0) and IsMatch(xl -1) do dec(xl); |
| 1457 |
while (xr < maxX) and IsMatch(xr +1) do inc(xr); |
| 1458 |
ffs.Push(xl, xr, y, -1); // down |
| 1459 |
ffs.Push(xl, xr, y, 1); // up |
| 1460 |
while not ffs.IsEmpty do |
| 1461 |
begin |
| 1462 |
ffs.Pop(xl, xr, y, dirY); |
| 1463 |
SetCurrentY(y); |
| 1464 |
xr2 := xl; |
| 1465 |
// check left ... |
| 1466 |
if IsMatch(xl) then |
| 1467 |
begin |
| 1468 |
while (xl > 0) and IsMatch(xl-1) do dec(xl); |
| 1469 |
if xl <= xr2 -2 then |
| 1470 |
ffs.Push(xl, xr2-2, y, -dirY); |
| 1471 |
while (xr2 < maxX) and IsMatch(xr2+1) do inc(xr2); |
| 1472 |
ffs.Push(xl, xr2, y, dirY); |
| 1473 |
if xr2 >= xr +2 then |
| 1474 |
ffs.Push(xr+2, xr2, y, -dirY); |
| 1475 |
xl := xr2 +2; |
| 1476 |
end; |
| 1477 |
// check right ... |
| 1478 |
while (xl <= xr) and not IsMatch(xl) do inc(xl); |
| 1479 |
while (xl <= xr) do |
| 1480 |
begin |
| 1481 |
xr2 := xl; |
| 1482 |
while (xr2 < maxX) and IsMatch(xr2+1) do inc(xr2); |
| 1483 |
ffs.Push(xl, xr2, y, dirY); |
| 1484 |
if xr2 >= xr +2 then |
| 1485 |
begin |
| 1486 |
ffs.Push(xr+2, xr2, y, -dirY); |
| 1487 |
break; |
| 1488 |
end; |
| 1489 |
inc(xl, 2); |
| 1490 |
while (xl <= xr) and not IsMatch(xl) do inc(xl); |
| 1491 |
end; |
| 1492 |
end; |
| 1493 |
finally |
| 1494 |
ffs.Free; |
| 1495 |
end; |
| 1496 |
end; |
| 1497 |
//------------------------------------------------------------------------------ |
| 1498 |
|
| 1499 |
procedure TFloodFillMask.SetCurrentY(y: Integer); |
| 1500 |
begin |
| 1501 |
colorsRow := PColor32Array(img.PixelRow[y]); |
| 1502 |
maskRow := PColor32Array(mask.PixelRow[y]); |
| 1503 |
end; |
| 1504 |
//------------------------------------------------------------------------------ |
| 1505 |
|
| 1506 |
function TFloodFillMask.IsMatch(x: Integer): Boolean; |
| 1507 |
var |
| 1508 |
b: Byte; |
| 1509 |
begin |
| 1510 |
if (maskRow[x] > 0) then |
| 1511 |
result := false |
| 1512 |
else |
| 1513 |
begin |
| 1514 |
b := compareFunc(initialColor, colorsRow[x]); |
| 1515 |
result := b < tolerance; |
| 1516 |
if Result then |
| 1517 |
maskRow[x] := tolerance - b else |
| 1518 |
maskRow[x] := 1; |
| 1519 |
end; |
| 1520 |
end; |
| 1521 |
//------------------------------------------------------------------------------ |
| 1522 |
|
| 1523 |
function GetFloodFillMask(imgIn, imgMaskOut: TImage32; x, y: Integer; |
| 1524 |
tolerance: Byte; compareFunc: TCompareFunctionEx): Boolean; |
| 1525 |
var |
| 1526 |
ffm: TFloodFillMask; |
| 1527 |
begin |
| 1528 |
if not Assigned(compareFunc) then compareFunc := CompareRGBEx; |
| 1529 |
ffm := TFloodFillMask.Create; |
| 1530 |
try |
| 1531 |
Result := ffm.Execute(imgIn, imgMaskOut, x, y, tolerance, compareFunc); |
| 1532 |
finally |
| 1533 |
ffm.Free; |
| 1534 |
end; |
| 1535 |
end; |
| 1536 |
//------------------------------------------------------------------------------ |
| 1537 |
|
| 1538 |
procedure FloodFill(img: TImage32; x, y: Integer; newColor: TColor32; |
| 1539 |
tolerance: Byte; compareFunc: TCompareFunctionEx); |
| 1540 |
var |
| 1541 |
i: Integer; |
| 1542 |
pc, pm: PColor32; |
| 1543 |
mask: TImage32; |
| 1544 |
begin |
| 1545 |
if not assigned(compareFunc) then |
| 1546 |
begin |
| 1547 |
compareFunc := CompareRGBEx; |
| 1548 |
if tolerance = 0 then |
| 1549 |
tolerance := FloodFillDefaultRGBTolerance; |
| 1550 |
end; |
| 1551 |
mask := TImage32.Create; |
| 1552 |
try |
| 1553 |
if not GetFloodFillMask(img, mask, x, y, tolerance, compareFunc) then |
| 1554 |
Exit; |
| 1555 |
pc := img.PixelBase; |
| 1556 |
pm := mask.PixelBase; |
| 1557 |
for i := 0 to img.Width * img.Height -1 do |
| 1558 |
begin |
| 1559 |
if (pm^ > 1) then pc^ := newColor; |
| 1560 |
inc(pm); inc(pc); |
| 1561 |
end; |
| 1562 |
finally |
| 1563 |
mask.free; |
| 1564 |
end; |
| 1565 |
end; |
| 1566 |
//------------------------------------------------------------------------------ |
| 1567 |
// EMBOSS - AND SUPPORT FUNCTIONS |
| 1568 |
//------------------------------------------------------------------------------ |
| 1569 |
|
| 1570 |
function IncPWeightColor(pwc: PWeightedColor; cnt: Integer): PWeightedColor; |
| 1571 |
begin |
| 1572 |
result := PWeightedColor(PByte(pwc) + cnt * SizeOf(TWeightedColor)); |
| 1573 |
end; |
| 1574 |
//------------------------------------------------------------------------------ |
| 1575 |
|
| 1576 |
function Intensity(color: TColor32): byte; |
| 1577 |
var |
| 1578 |
c: TARGB absolute color; |
| 1579 |
begin |
| 1580 |
Result := (c.R * 61 + c.G * 174 + c.B * 21) shr 8; |
| 1581 |
end; |
| 1582 |
//------------------------------------------------------------------------------ |
| 1583 |
|
| 1584 |
function Gray(color: TColor32): TColor32; |
| 1585 |
var |
| 1586 |
c: TARGB absolute color; |
| 1587 |
res: TARGB absolute Result; |
| 1588 |
begin |
| 1589 |
res.A := c.A; |
| 1590 |
res.R := Intensity(color); |
| 1591 |
res.G := res.R; |
| 1592 |
res.B := res.R; |
| 1593 |
end; |
| 1594 |
//------------------------------------------------------------------------------ |
| 1595 |
|
| 1596 |
procedure Emboss(img: TImage32; radius: Integer; |
| 1597 |
depth: Integer; luminance: Integer; preserveColor: Boolean); |
| 1598 |
var |
| 1599 |
yy,xx, x,y, w,h: Integer; |
| 1600 |
b: byte; |
| 1601 |
kernel: array [0 .. MaxBlur, 0 .. MaxBlur] of Integer; |
| 1602 |
wca: TArrayOfWeightedColor; |
| 1603 |
pc0, pcf, pcb: PColor32; // pointers to pixels (forward & backward in kernel) |
| 1604 |
pw0, pw: PWeightedColor; // pointers to weight |
| 1605 |
customGray: TColor32; |
| 1606 |
pc: PColor32; |
| 1607 |
const |
| 1608 |
maxDepth = 50; |
| 1609 |
begin |
| 1610 |
// grayscale luminance as percent where 0% is black and 100% is white |
| 1611 |
//(luminance is ignored when preserveColor = true) |
| 1612 |
luminance := ClampRange(luminance, 0, 100); |
| 1613 |
b := luminance *255 div 100; |
| 1614 |
customGray := $FF000000 + b shl 16 + b shl 8 + b; |
| 1615 |
ClampRange(radius, 1, 5); |
| 1616 |
inc(depth); |
| 1617 |
ClampRange(depth, 2, maxDepth); |
| 1618 |
kernel[0][0] := 1; |
| 1619 |
for y := 1 to radius do |
| 1620 |
for x := 1 to radius do |
| 1621 |
kernel[y][x] := depth; |
| 1622 |
w := img.Width; h := img.Height; |
| 1623 |
// nb: dynamic arrays are zero-initialized (unless they're a function result) |
| 1624 |
SetLength(wca, w * h); |
| 1625 |
pc0 := IncPColor32(img.PixelBase, radius * w); |
| 1626 |
pw0 := @wca[radius * w]; |
| 1627 |
for y := radius to h -1 - radius do |
| 1628 |
begin |
| 1629 |
for x := radius to w -1 - radius do |
| 1630 |
begin |
| 1631 |
pw := IncPWeightColor(pw0, x); |
| 1632 |
pcb := IncPColor32(pc0, x - 1); |
| 1633 |
if preserveColor then |
| 1634 |
begin |
| 1635 |
pcf := IncPColor32(pc0, x); |
| 1636 |
pw^.Add(pcf^, kernel[0,0]); |
| 1637 |
inc(pcf); |
| 1638 |
end else |
| 1639 |
begin |
| 1640 |
pw^.Add(customGray, kernel[0,0]); |
| 1641 |
pcf := IncPColor32(pc0, x + 1); |
| 1642 |
end; |
| 1643 |
// parse the kernel ... |
| 1644 |
for yy := 1 to radius do |
| 1645 |
begin |
| 1646 |
for xx := 1 to radius do |
| 1647 |
begin |
| 1648 |
pw^.Subtract(Gray(pcf^), kernel[yy,xx]); |
| 1649 |
pw^.Add(Gray(pcb^), kernel[yy,xx]); |
| 1650 |
dec(pcb); inc(pcf); |
| 1651 |
end; |
| 1652 |
dec(pcb, img.Width - radius); |
| 1653 |
inc(pcf, img.Width - radius); |
| 1654 |
end; |
| 1655 |
end; |
| 1656 |
inc(pc0, img.Width); |
| 1657 |
inc(pw0, img.Width); |
| 1658 |
end; |
| 1659 |
pc := @img.Pixels[0]; pw := @wca[0]; |
| 1660 |
for x := 0 to img.width * img.Height - 1 do |
| 1661 |
begin |
| 1662 |
pc^ := pw.Color or $FF000000; |
| 1663 |
inc(pc); inc(pw); |
| 1664 |
end; |
| 1665 |
end; |
| 1666 |
|
| 1667 |
//------------------------------------------------------------------------------ |
| 1668 |
// RamerDouglasPeucker - and support functions |
| 1669 |
//------------------------------------------------------------------------------ |
| 1670 |
|
| 1671 |
procedure RDP(const path: TPathD; startIdx, endIdx: integer; |
| 1672 |
epsilonSqrd: double; var flags: TArrayOfInteger); |
| 1673 |
var |
| 1674 |
i, idx: integer; |
| 1675 |
d, maxD: double; |
| 1676 |
begin |
| 1677 |
idx := 0; |
| 1678 |
maxD := 0; |
| 1679 |
for i := startIdx +1 to endIdx -1 do |
| 1680 |
begin |
| 1681 |
// PerpendicularDistSqrd - avoids expensive Sqrt() |
| 1682 |
d := PerpendicularDistSqrd(path[i], path[startIdx], path[endIdx]); |
| 1683 |
if d <= maxD then Continue; |
| 1684 |
maxD := d; |
| 1685 |
idx := i; |
| 1686 |
end; |
| 1687 |
if maxD < epsilonSqrd then Exit; |
| 1688 |
flags[idx] := 1; |
| 1689 |
if idx > startIdx + 1 then RDP(path, startIdx, idx, epsilonSqrd, flags); |
| 1690 |
if endIdx > idx + 1 then RDP(path, idx, endIdx, epsilonSqrd, flags); |
| 1691 |
end; |
| 1692 |
//------------------------------------------------------------------------------ |
| 1693 |
|
| 1694 |
function RamerDouglasPeucker(const path: TPathD; |
| 1695 |
epsilon: double): TPathD; |
| 1696 |
var |
| 1697 |
i,j, len: integer; |
| 1698 |
buffer: TArrayOfInteger; |
| 1699 |
begin |
| 1700 |
len := length(path); |
| 1701 |
if len < 5 then |
| 1702 |
begin |
| 1703 |
result := Copy(path, 0, len); |
| 1704 |
Exit; |
| 1705 |
end; |
| 1706 |
SetLength(buffer, len); // buffer is zero initialized |
| 1707 |
buffer[0] := 1; |
| 1708 |
buffer[len -1] := 1; |
| 1709 |
RDP(path, 0, len -1, Sqr(epsilon), buffer); |
| 1710 |
j := 0; |
| 1711 |
SetLength(Result, len); |
| 1712 |
for i := 0 to len -1 do |
| 1713 |
if buffer[i] = 1 then |
| 1714 |
begin |
| 1715 |
Result[j] := path[i]; |
| 1716 |
inc(j); |
| 1717 |
end; |
| 1718 |
SetLength(Result, j); |
| 1719 |
end; |
| 1720 |
//------------------------------------------------------------------------------ |
| 1721 |
|
| 1722 |
function RamerDouglasPeucker(const paths: TPathsD; |
| 1723 |
epsilon: double): TPathsD; |
| 1724 |
var |
| 1725 |
i,j, len: integer; |
| 1726 |
begin |
| 1727 |
j := 0; |
| 1728 |
len := length(paths); |
| 1729 |
setLength(Result, len); |
| 1730 |
for i := 0 to len -1 do |
| 1731 |
begin |
| 1732 |
Result[j] := RamerDouglasPeucker(paths[i], epsilon); |
| 1733 |
if Result[j] <> nil then inc(j); |
| 1734 |
end; |
| 1735 |
setLength(Result, j); |
| 1736 |
end; |
| 1737 |
//------------------------------------------------------------------------------ |
| 1738 |
|
| 1739 |
function GetNext(current, high: integer; var flags: array of Boolean): integer; |
| 1740 |
begin |
| 1741 |
Result := current +1; |
| 1742 |
while (Result <= high) and flags[Result] do inc(Result); |
| 1743 |
if (Result <= high) then Exit; |
| 1744 |
Result := 0; |
| 1745 |
while (flags[Result]) do inc(Result); |
| 1746 |
end; |
| 1747 |
//--------------------------------------------------------------------------- |
| 1748 |
|
| 1749 |
function GetPrior(current, high: integer; var flags: array of Boolean): integer; |
| 1750 |
begin |
| 1751 |
Result := current; |
| 1752 |
if (Result = 0) then Result := high |
| 1753 |
else dec(Result); |
| 1754 |
while (Result > 0) and flags[Result] do dec(Result); |
| 1755 |
if not flags[Result] then Exit; |
| 1756 |
Result := high; |
| 1757 |
while flags[Result] do dec(Result); |
| 1758 |
end; |
| 1759 |
//--------------------------------------------------------------------------- |
| 1760 |
|
| 1761 |
type |
| 1762 |
PSimplifyRec = ^TSimplifyRec; |
| 1763 |
TSimplifyRec = record |
| 1764 |
pt : TPointD; |
| 1765 |
pdSqrd : double; |
| 1766 |
prev : PSimplifyRec; |
| 1767 |
next : PSimplifyRec; |
| 1768 |
isEndPt : Boolean; |
| 1769 |
end; |
| 1770 |
|
| 1771 |
function SimplifyPath(const path: TPathD; |
| 1772 |
shapeTolerance: double; isClosedPath: Boolean): TPathD; |
| 1773 |
var |
| 1774 |
i, iPrev, iNext, len, minLen: integer; |
| 1775 |
tolSqrd: double; |
| 1776 |
srArray: array of TSimplifyRec; |
| 1777 |
current, last: PSimplifyRec; |
| 1778 |
begin |
| 1779 |
Result := nil; |
| 1780 |
len := Length(path); |
| 1781 |
if not isClosedPath then minLen := 2 else minLen := 3; |
| 1782 |
if len < minLen then Exit; |
| 1783 |
|
| 1784 |
SetLength(srArray, len); |
| 1785 |
for i := 0 to len -1 do |
| 1786 |
with srArray[i] do |
| 1787 |
begin |
| 1788 |
iPrev := ModEx(i-1, len); |
| 1789 |
iNext := ModEx(i+1, len); |
| 1790 |
pt := path[i]; |
| 1791 |
prev := @srArray[iPrev]; |
| 1792 |
next := @srArray[iNext]; |
| 1793 |
pdSqrd := PerpendicularDistSqrd(path[i], path[iPrev], path[iNext]); |
| 1794 |
isEndPt := not isClosedPath and ((i = 0) or (i = len -1)); |
| 1795 |
end; |
| 1796 |
|
| 1797 |
current := @srArray[0]; |
| 1798 |
last := current.prev; |
| 1799 |
|
| 1800 |
tolSqrd := Sqr(shapeTolerance); |
| 1801 |
while current <> last do |
| 1802 |
begin |
| 1803 |
if not current.isEndPt and |
| 1804 |
((current.pdSqrd < tolSqrd) and (current.next.pdSqrd > current.pdSqrd)) then |
| 1805 |
begin |
| 1806 |
current.prev.next := current.next; |
| 1807 |
current.next.prev := current.prev; |
| 1808 |
last := current.prev; |
| 1809 |
dec(len); |
| 1810 |
if last.next = last.prev then break; |
| 1811 |
last.pdSqrd := PerpendicularDistSqrd(last.pt, last.prev.pt, last.next.pt); |
| 1812 |
current := last.next; |
| 1813 |
current.pdSqrd := PerpendicularDistSqrd(current.pt, current.prev.pt, current.next.pt); |
| 1814 |
end |
| 1815 |
else |
| 1816 |
current := current.next; |
| 1817 |
end; |
| 1818 |
|
| 1819 |
if len < minLen then Exit; |
| 1820 |
if not isClosedPath then current := @srArray[0]; |
| 1821 |
NewPointDArray(Result, len, True); |
| 1822 |
for i := 0 to len -1 do |
| 1823 |
begin |
| 1824 |
Result[i] := current.pt; |
| 1825 |
current := current.next; |
| 1826 |
end; |
| 1827 |
end; |
| 1828 |
//------------------------------------------------------------------------------ |
| 1829 |
|
| 1830 |
function SimplifyPaths(const paths: TPathsD; |
| 1831 |
shapeTolerance: double; isClosedPath: Boolean): TPathsD; |
| 1832 |
var |
| 1833 |
i,j, len: integer; |
| 1834 |
begin |
| 1835 |
len := Length(paths); |
| 1836 |
SetLength(Result, len); |
| 1837 |
j := 0; |
| 1838 |
for i := 0 to len -1 do |
| 1839 |
begin |
| 1840 |
result[j] := SimplifyPath(paths[i], shapeTolerance, isClosedPath); |
| 1841 |
if Length(result[j]) > 0 then inc(j); |
| 1842 |
end; |
| 1843 |
SetLength(Result, j); |
| 1844 |
end; |
| 1845 |
|
| 1846 |
//--------------------------------------------------------------------------- |
| 1847 |
//--------------------------------------------------------------------------- |
| 1848 |
|
| 1849 |
type |
| 1850 |
PSimplifyExRec = ^TSimplifyExRec; |
| 1851 |
TSimplifyExRec = record |
| 1852 |
pt : TPointD; |
| 1853 |
pdSqrd : double; |
| 1854 |
segLenSq : double; |
| 1855 |
prev : PSimplifyExRec; |
| 1856 |
next : PSimplifyExRec; |
| 1857 |
end; |
| 1858 |
|
| 1859 |
function DeleteCurrent(var current: PSimplifyExRec): Boolean; |
| 1860 |
var |
| 1861 |
next: PSimplifyExRec; |
| 1862 |
begin |
| 1863 |
current.prev.next := current.next; |
| 1864 |
current.next.prev := current.prev; |
| 1865 |
current := current.prev; |
| 1866 |
next := current.next; |
| 1867 |
Result := next <> current.prev; |
| 1868 |
if not Result then Exit; |
| 1869 |
next.pdSqrd := PerpendicularDistSqrd(next.pt, next.prev.pt, next.next.pt); |
| 1870 |
current.segLenSq := DistanceSqrd(current.pt, current.next.pt); |
| 1871 |
current.pdSqrd := PerpendicularDistSqrd(current.pt, current.prev.pt, current.next.pt); |
| 1872 |
end; |
| 1873 |
//--------------------------------------------------------------------------- |
| 1874 |
|
| 1875 |
function SimplifyPathEx(const path: TPathD; shapeTolerance: double): TPathD; |
| 1876 |
var |
| 1877 |
i, prevI, nextI, len: integer; |
| 1878 |
shapeTolSqr: double; |
| 1879 |
srArray: array of TSimplifyExRec; |
| 1880 |
current, start: PSimplifyExRec; |
| 1881 |
begin |
| 1882 |
Result := nil; |
| 1883 |
len := Length(path); |
| 1884 |
if len < 3 then Exit; |
| 1885 |
|
| 1886 |
shapeTolSqr := Sqr(shapeTolerance); |
| 1887 |
SetLength(srArray, len); |
| 1888 |
|
| 1889 |
for i := 0 to len -1 do |
| 1890 |
begin |
| 1891 |
prevI := i -1; |
| 1892 |
nextI := i +1; |
| 1893 |
if i = 0 then prevI := len -1 |
| 1894 |
else if i = len -1 then nextI := 0; |
| 1895 |
|
| 1896 |
with srArray[i] do |
| 1897 |
begin |
| 1898 |
pt := path[i]; |
| 1899 |
segLenSq:= DistanceSqrd(path[i], path[nextI]); |
| 1900 |
pdSqrd := PerpendicularDistSqrd(path[i], path[prevI], path[nextI]); |
| 1901 |
prev := @srArray[prevI]; |
| 1902 |
next := @srArray[nextI]; |
| 1903 |
end; |
| 1904 |
end; |
| 1905 |
|
| 1906 |
current := @srArray[0]; |
| 1907 |
start := current.prev; |
| 1908 |
|
| 1909 |
while current <> start do |
| 1910 |
begin |
| 1911 |
// Irrespective of segment length, remove vertices that deviate very little |
| 1912 |
// from imaginary lines that pass through their adjacent vertices. |
| 1913 |
// However, if the following vertex has an even sorter distance from its |
| 1914 |
// respective imaginary line, its important to remove that vertex first. |
| 1915 |
if ((current.pdSqrd < shapeTolSqr) and |
| 1916 |
(current.pdSqrd < current.next.pdSqrd)) then |
| 1917 |
begin |
| 1918 |
dec(len); |
| 1919 |
if not DeleteCurrent(current) then Break; |
| 1920 |
start := current.prev; |
| 1921 |
end |
| 1922 |
// also remove insignificant path zig-zags |
| 1923 |
else if (current.prev.segLenSq < shapeTolSqr) and |
| 1924 |
(current.segLenSq < shapeTolSqr) and |
| 1925 |
((CrossProduct(current.prev.pt, current.pt, current.next.pt) > 0) <> |
| 1926 |
(CrossProduct(current.pt, current.next.pt, current.next.next.pt) > 0)) then |
| 1927 |
begin |
| 1928 |
dec(len); |
| 1929 |
if not DeleteCurrent(current) then Break; |
| 1930 |
start := current.prev; |
| 1931 |
end else |
| 1932 |
current := current.next; |
| 1933 |
end; |
| 1934 |
|
| 1935 |
if len < 3 then Exit; |
| 1936 |
NewPointDArray(Result, len, True); |
| 1937 |
for i := 0 to len -1 do |
| 1938 |
begin |
| 1939 |
Result[i] := current.pt; |
| 1940 |
current := current.next; |
| 1941 |
end; |
| 1942 |
end; |
| 1943 |
//------------------------------------------------------------------------------ |
| 1944 |
|
| 1945 |
function SimplifyPathsEx(const paths: TPathsD; shapeTolerance: double): TPathsD; |
| 1946 |
var |
| 1947 |
i,j, len: integer; |
| 1948 |
begin |
| 1949 |
len := Length(paths); |
| 1950 |
SetLength(Result, len); |
| 1951 |
j := 0; |
| 1952 |
for i := 0 to len -1 do |
| 1953 |
begin |
| 1954 |
Result[j] := SimplifyPathEx(paths[i], shapeTolerance); |
| 1955 |
if Length(Result[j]) > 0 then inc(j); |
| 1956 |
end; |
| 1957 |
SetLength(Result, len); |
| 1958 |
end; |
| 1959 |
//--------------------------------------------------------------------------- |
| 1960 |
//--------------------------------------------------------------------------- |
| 1961 |
|
| 1962 |
|
| 1963 |
function DotProdVecs(const vec1, vec2: TPointD): double; |
| 1964 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 1965 |
begin |
| 1966 |
result := (vec1.X * vec2.X + vec1.Y * vec2.Y); |
| 1967 |
end; |
| 1968 |
//--------------------------------------------------------------------------- |
| 1969 |
|
| 1970 |
function SmoothToCubicBezier(const path: TPathD; |
| 1971 |
pathIsClosed: Boolean; maxOffset: integer): TPathD; |
| 1972 |
var |
| 1973 |
i, j, len, prev: integer; |
| 1974 |
vec: TPointD; |
| 1975 |
pl: TArrayOfDouble; |
| 1976 |
unitVecs: TPathD; |
| 1977 |
d, angle, d1,d2: double; |
| 1978 |
begin |
| 1979 |
// SmoothToCubicBezier - returns cubic bezier control points |
| 1980 |
Result := nil; |
| 1981 |
len := Length(path); |
| 1982 |
if len < 3 then Exit; |
| 1983 |
|
| 1984 |
NewPointDArray(Result, len *3 +1, True); |
| 1985 |
prev := len-1; |
| 1986 |
SetLength(pl, len); |
| 1987 |
SetLength(unitVecs, len); |
| 1988 |
pl[0] := Distance(path[prev], path[0]); |
| 1989 |
unitVecs[0] := GetUnitVector(path[prev], path[0]); |
| 1990 |
for i := 0 to len -1 do |
| 1991 |
begin |
| 1992 |
if i = prev then |
| 1993 |
begin |
| 1994 |
j := 0; |
| 1995 |
end else |
| 1996 |
begin |
| 1997 |
j := i +1; |
| 1998 |
pl[j] := Distance(path[i], path[j]); |
| 1999 |
unitVecs[j] := GetUnitVector(path[i], path[j]); |
| 2000 |
end; |
| 2001 |
vec := GetAvgUnitVector(unitVecs[i], unitVecs[j]); |
| 2002 |
|
| 2003 |
angle := arccos(Max(-1,Min(1,(DotProdVecs(unitVecs[i], unitVecs[j]))))); |
| 2004 |
d := abs(Pi-angle)/TwoPi; |
| 2005 |
d1 := pl[i] * d; |
| 2006 |
d2 := pl[j] * d; |
| 2007 |
|
| 2008 |
if maxOffset > 0 then |
| 2009 |
begin |
| 2010 |
d1 := Min(maxOffset, d1); |
| 2011 |
d2 := Min(maxOffset, d2); |
| 2012 |
end; |
| 2013 |
|
| 2014 |
if i = 0 then |
| 2015 |
Result[len*3-1] := TranslatePoint(path[0], -vec.X * d1, -vec.Y * d1) |
| 2016 |
else |
| 2017 |
Result[i*3-1] := TranslatePoint(path[i], -vec.X * d1, -vec.Y * d1); |
| 2018 |
Result[i*3] := path[i]; |
| 2019 |
Result[i*3+1] := TranslatePoint(path[i], vec.X * d2, vec.Y * d2); |
| 2020 |
end; |
| 2021 |
Result[len*3] := path[0]; |
| 2022 |
|
| 2023 |
if pathIsClosed then Exit; |
| 2024 |
Result[1] := Result[0]; |
| 2025 |
dec(len); |
| 2026 |
Result[len*3-1] := Result[len*3]; |
| 2027 |
SetLength(Result, Len*3 +1); |
| 2028 |
end; |
| 2029 |
//------------------------------------------------------------------------------ |
| 2030 |
|
| 2031 |
function SmoothToCubicBezier(const paths: TPathsD; |
| 2032 |
pathIsClosed: Boolean; maxOffset: integer = 0): TPathsD; |
| 2033 |
var |
| 2034 |
i, len: integer; |
| 2035 |
begin |
| 2036 |
len := Length(paths); |
| 2037 |
SetLength(Result, len); |
| 2038 |
for i := 0 to len -1 do |
| 2039 |
Result[i] := SmoothToCubicBezier(paths[i], pathIsClosed, maxOffset); |
| 2040 |
end; |
| 2041 |
//------------------------------------------------------------------------------ |
| 2042 |
|
| 2043 |
function SmoothToCubicBezier2(const path: TPathD; |
| 2044 |
pathIsClosed: Boolean; maxOffset: integer): TPathD; |
| 2045 |
var |
| 2046 |
i, j, len, prev: integer; |
| 2047 |
vec: TPointD; |
| 2048 |
pl: TArrayOfDouble; |
| 2049 |
unitVecs: TPathD; |
| 2050 |
d1,d2: double; |
| 2051 |
begin |
| 2052 |
// SmoothToCubicBezier2 - returns cubic bezier control points |
| 2053 |
Result := nil; |
| 2054 |
len := Length(path); |
| 2055 |
if len < 3 then Exit; |
| 2056 |
|
| 2057 |
NewPointDArray(Result, len *3 +1); |
| 2058 |
prev := len-1; |
| 2059 |
SetLength(pl, len); |
| 2060 |
SetLength(unitVecs, len); |
| 2061 |
pl[0] := Distance(path[prev], path[0]); |
| 2062 |
unitVecs[0] := GetUnitVector(path[prev], path[0]); |
| 2063 |
for i := 0 to len -1 do |
| 2064 |
begin |
| 2065 |
if i = prev then |
| 2066 |
begin |
| 2067 |
j := 0; |
| 2068 |
end else |
| 2069 |
begin |
| 2070 |
j := i +1; |
| 2071 |
pl[j] := Distance(path[i], path[j]); |
| 2072 |
unitVecs[j] := GetUnitVector(path[i], path[j]); |
| 2073 |
end; |
| 2074 |
vec := GetAvgUnitVector(unitVecs[i], unitVecs[j]); |
| 2075 |
|
| 2076 |
d1 := pl[i]/2; |
| 2077 |
d2 := pl[j]/2; |
| 2078 |
|
| 2079 |
if maxOffset > 0 then |
| 2080 |
begin |
| 2081 |
d1 := Min(maxOffset, d1); |
| 2082 |
d2 := Min(maxOffset, d2); |
| 2083 |
end; |
| 2084 |
|
| 2085 |
if i = 0 then |
| 2086 |
Result[len*3-1] := TranslatePoint(path[0], -vec.X * d1, -vec.Y * d1) |
| 2087 |
else |
| 2088 |
Result[i*3-1] := TranslatePoint(path[i], -vec.X * d1, -vec.Y * d1); |
| 2089 |
Result[i*3] := path[i]; |
| 2090 |
Result[i*3+1] := TranslatePoint(path[i], vec.X * d2, vec.Y * d2); |
| 2091 |
end; |
| 2092 |
Result[len*3] := path[0]; |
| 2093 |
|
| 2094 |
if pathIsClosed then Exit; |
| 2095 |
Result[1] := Result[0]; |
| 2096 |
dec(len); |
| 2097 |
Result[len*3-1] := Result[len*3]; |
| 2098 |
SetLength(Result, Len*3 +1); |
| 2099 |
end; |
| 2100 |
//------------------------------------------------------------------------------ |
| 2101 |
|
| 2102 |
function SmoothToCubicBezier2(const paths: TPathsD; |
| 2103 |
pathIsClosed: Boolean; maxOffset: integer = 0): TPathsD; |
| 2104 |
var |
| 2105 |
i, len: integer; |
| 2106 |
begin |
| 2107 |
len := Length(paths); |
| 2108 |
SetLength(Result, len); |
| 2109 |
for i := 0 to len -1 do |
| 2110 |
Result[i] := SmoothToCubicBezier2(paths[i], pathIsClosed, maxOffset); |
| 2111 |
end; |
| 2112 |
//------------------------------------------------------------------------------ |
| 2113 |
|
| 2114 |
function CubicInterpolate(v1, v2, v3, v4: double; |
| 2115 |
t: double; tension: double = 0): double; |
| 2116 |
var |
| 2117 |
m0, m1, tt, ttt, tensionEx: double; |
| 2118 |
a, b: double; |
| 2119 |
begin |
| 2120 |
tt := t * t; |
| 2121 |
ttt := tt * t; |
| 2122 |
tensionEx := (1-tension) * 0.5; |
| 2123 |
m0 := (v3 - v1)*tensionEx; |
| 2124 |
m1 := (v4 - v2)*tensionEx; |
| 2125 |
a := 2*v2 - 2*v3 + m0 + m1; |
| 2126 |
b := 3*v3 -3*v2 -2*m0 - m1; |
| 2127 |
Result := a*ttt + b*tt + m0*t + v2; |
| 2128 |
end; |
| 2129 |
//------------------------------------------------------------------------------ |
| 2130 |
|
| 2131 |
procedure Append(var path: TPathD; const pt: TPointD); |
| 2132 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 2133 |
var |
| 2134 |
len: integer; |
| 2135 |
begin |
| 2136 |
len := Length(path); |
| 2137 |
SetLengthUninit(path, len +1); |
| 2138 |
path[len] := pt; |
| 2139 |
end; |
| 2140 |
//------------------------------------------------------------------------------ |
| 2141 |
|
| 2142 |
function SmoothPath(const path: TPathD; isClosedPath: Boolean; |
| 2143 |
tension: double; shapeTolerance: double): TPathD; |
| 2144 |
var |
| 2145 |
i, j, highI, len, cnt: integer; |
| 2146 |
pt: TPointD; |
| 2147 |
dists: TArrayOfDouble; |
| 2148 |
const |
| 2149 |
maxInterval = 1.5; |
| 2150 |
begin |
| 2151 |
Result := nil; |
| 2152 |
len := Length(path); |
| 2153 |
if len < 3 then Exit; |
| 2154 |
SetLength(dists, len); |
| 2155 |
highI := len -1; |
| 2156 |
dists[highI] := Distance(path[highI], path[0]); |
| 2157 |
for i := 0 to highI-1 do |
| 2158 |
dists[i] := Distance(path[i], path[i+1]); |
| 2159 |
|
| 2160 |
if tension > 1 then tension := 1 |
| 2161 |
else if tension < -1 then tension := -1; |
| 2162 |
if tension > 0.9 then |
| 2163 |
begin |
| 2164 |
Result := path; |
| 2165 |
Exit; |
| 2166 |
end; |
| 2167 |
|
| 2168 |
if isClosedPath then |
| 2169 |
for i := 0 to highI do |
| 2170 |
begin |
| 2171 |
cnt := Ceil(dists[i]/maxInterval); |
| 2172 |
Append(Result, path[i]); |
| 2173 |
for j := 1 to cnt -1 do |
| 2174 |
begin |
| 2175 |
pt.X := CubicInterpolate( |
| 2176 |
path[ModEx(i-1, len)].X, |
| 2177 |
path[i].X, |
| 2178 |
path[ModEx(i+1, len)].X, |
| 2179 |
path[ModEx(i+2, len)].X, j/cnt, tension); |
| 2180 |
pt.Y := CubicInterpolate( |
| 2181 |
path[ModEx(i-1, len)].Y, |
| 2182 |
path[i].Y, |
| 2183 |
path[ModEx(i+1, len)].Y, |
| 2184 |
path[ModEx(i+2, len)].Y, j/cnt, tension); |
| 2185 |
Append(Result, pt); |
| 2186 |
end; |
| 2187 |
end |
| 2188 |
else |
| 2189 |
begin |
| 2190 |
for i := 0 to highI -1 do |
| 2191 |
begin |
| 2192 |
cnt := Ceil(dists[i]/maxInterval); |
| 2193 |
Append(Result, path[i]); |
| 2194 |
for j := 1 to cnt -1 do |
| 2195 |
begin |
| 2196 |
pt.X := CubicInterpolate( |
| 2197 |
path[Clamp(i-1, len)].X, |
| 2198 |
path[Clamp(i, len)].X, |
| 2199 |
path[Clamp(i+1, len)].X, |
| 2200 |
path[Clamp(i+2, len)].X, j/cnt, tension); |
| 2201 |
pt.Y := CubicInterpolate( |
| 2202 |
path[Clamp(i-1, len)].Y, |
| 2203 |
path[Clamp(i, len)].Y, |
| 2204 |
path[Clamp(i+1, len)].Y, |
| 2205 |
path[Clamp(i+2, len)].Y, j/cnt, tension); |
| 2206 |
Append(Result, pt); |
| 2207 |
end; |
| 2208 |
end; |
| 2209 |
Append(Result, path[highi]); |
| 2210 |
end; |
| 2211 |
Result := SimplifyPath(Result, shapeTolerance, false); |
| 2212 |
end; |
| 2213 |
//------------------------------------------------------------------------------ |
| 2214 |
|
| 2215 |
function SmoothPaths(const paths: TPathsD; isClosedPath: Boolean; |
| 2216 |
tension: double = 0; shapeTolerance: double = 0.1): TPathsD; |
| 2217 |
var |
| 2218 |
i, len: integer; |
| 2219 |
begin |
| 2220 |
len := Length(paths); |
| 2221 |
SetLength(Result, len); |
| 2222 |
for i := 0 to len -1 do |
| 2223 |
Result[i] := SmoothPath(paths[i], isClosedPath, tension, shapeTolerance); |
| 2224 |
end; |
| 2225 |
|
| 2226 |
//------------------------------------------------------------------------------ |
| 2227 |
// GaussianBlur |
| 2228 |
//------------------------------------------------------------------------------ |
| 2229 |
|
| 2230 |
procedure GaussianBlur(img: TImage32; rec: TRect; radius: Integer); |
| 2231 |
var |
| 2232 |
i, w,h, highX, x,y,yy,z,startz: Integer; |
| 2233 |
expConst: double; |
| 2234 |
gaussTable: array [-MaxBlur .. MaxBlur] of integer; |
| 2235 |
wc: TWeightedColor; |
| 2236 |
wca: TArrayOfWeightedColor; |
| 2237 |
wcaColor: TArrayOfColor32; |
| 2238 |
row: PColor32Array; |
| 2239 |
wcRow: PWeightedColorArray; |
| 2240 |
imgWidth: Integer; |
| 2241 |
dst, pc: PColor32; |
| 2242 |
const |
| 2243 |
tableConst = 1024; |
| 2244 |
sigma = 3; |
| 2245 |
begin |
| 2246 |
Types.IntersectRect(rec, rec, img.Bounds); |
| 2247 |
if IsEmptyRect(rec) or (radius < 1) then Exit |
| 2248 |
else if radius > MaxBlur then radius := MaxBlur; |
| 2249 |
|
| 2250 |
expConst := - 1 / (Sqr(radius) * 2 * Sqr(sigma)); |
| 2251 |
gaussTable[0] := Round(tableConst * Exp(expConst)); |
| 2252 |
for i := 1 to radius do |
| 2253 |
begin |
| 2254 |
gaussTable[i] := Round(tableConst * Exp(expConst * Sqr(i))); |
| 2255 |
gaussTable[-i] := gaussTable[i]; |
| 2256 |
end; |
| 2257 |
|
| 2258 |
RectWidthHeight(rec, w, h); |
| 2259 |
setLength(wca, w * h); |
| 2260 |
NewColor32Array(wcaColor, w * h, True); |
| 2261 |
imgWidth := img.Width; |
| 2262 |
highX := imgWidth -1; |
| 2263 |
for y := 0 to h -1 do |
| 2264 |
begin |
| 2265 |
row := PColor32Array(@img.Pixels[(y + rec.Top) * imgWidth + rec.Left]); |
| 2266 |
wcRow := PWeightedColorArray(@wca[y * w]); |
| 2267 |
for x := 0 to w -1 do |
| 2268 |
for z := max(0, x - radius) to min(highX, x + radius) do |
| 2269 |
wcRow[x].Add(row[z], gaussTable[x-z]); |
| 2270 |
end; |
| 2271 |
|
| 2272 |
// calculate colors |
| 2273 |
for x := 0 to w * h - 1 do |
| 2274 |
wcaColor[x] := wca[x].Color; |
| 2275 |
|
| 2276 |
dst := @img.Pixels[rec.Left + rec.Top * imgWidth]; |
| 2277 |
imgWidth := imgWidth * SizeOf(TColor32); // convert to byte size |
| 2278 |
for x := 0 to w -1 do |
| 2279 |
begin |
| 2280 |
pc := dst; |
| 2281 |
inc(pc, x); |
| 2282 |
for y := 0 to h -1 do |
| 2283 |
begin |
| 2284 |
wc.Reset; |
| 2285 |
startz := max(0, y - radius); |
| 2286 |
yy := startz * w; |
| 2287 |
for z := startz to min(h -1, y + radius) do |
| 2288 |
begin |
| 2289 |
wc.Add(wcaColor[x + yy], gaussTable[y-z]); |
| 2290 |
inc(yy, w); |
| 2291 |
end; |
| 2292 |
pc^ := wc.Color; |
| 2293 |
inc(PByte(pc), imgWidth); // increment by byte size |
| 2294 |
end; |
| 2295 |
end; |
| 2296 |
end; |
| 2297 |
|
| 2298 |
//------------------------------------------------------------------------------ |
| 2299 |
// FastGaussian blur - and support functions |
| 2300 |
//------------------------------------------------------------------------------ |
| 2301 |
|
| 2302 |
//http://blog.ivank.net/fastest-gaussian-blur.html |
| 2303 |
//https://www.peterkovesi.com/papers/FastGaussianSmoothing.pdf |
| 2304 |
|
| 2305 |
function BoxesForGauss(stdDev, boxCnt: integer): TArrayOfInteger; |
| 2306 |
var |
| 2307 |
i, wl, wu, m: integer; |
| 2308 |
wIdeal, mIdeal: double; |
| 2309 |
begin |
| 2310 |
NewIntegerArray(Result, boxCnt, True); |
| 2311 |
wIdeal := Sqrt((12*stdDev*stdDev/boxCnt)+1); // Ideal averaging filter width |
| 2312 |
wl := Floor(wIdeal); if not Odd(wl) then dec(wl); |
| 2313 |
mIdeal := |
| 2314 |
(-3*stdDev*stdDev +0.25*boxCnt*wl*wl +boxCnt*wl +0.75*boxCnt)/(wl+1); |
| 2315 |
m := Floor(mIdeal) div 2; // nb: variation on Ivan Kutskir's code. |
| 2316 |
wl := (wl -1) div 2; // It's better to do this here |
| 2317 |
wu := wl+1; // than later in both BoxBlurH & BoxBlurV |
| 2318 |
for i := 0 to boxCnt -1 do |
| 2319 |
if i < m then |
| 2320 |
Result[i] := wl else |
| 2321 |
Result[i] := wu; |
| 2322 |
end; |
| 2323 |
//------------------------------------------------------------------------------ |
| 2324 |
|
| 2325 |
procedure FastGaussianBlur(img: TImage32; |
| 2326 |
const rec: TRect; stdDev: integer; repeats: integer); |
| 2327 |
begin |
| 2328 |
FastGaussianBlur(img, rec, stdDev, stdDev, repeats); |
| 2329 |
end; |
| 2330 |
//------------------------------------------------------------------------------ |
| 2331 |
|
| 2332 |
procedure BoxBlurHLine(src, dst: PColor32; srcRiOffset: nativeint; |
| 2333 |
count, w: integer; dstLast: PColor32; var v: TWeightedColor); |
| 2334 |
var |
| 2335 |
lastColor: TColor32; |
| 2336 |
val: PWeightedColor; |
| 2337 |
s, d: PColor32; |
| 2338 |
begin |
| 2339 |
lastColor := v.Color; |
| 2340 |
if count > w then |
| 2341 |
count := w; |
| 2342 |
w := w - count; |
| 2343 |
|
| 2344 |
// The Delphi compiler sometimes is really stupid with |
| 2345 |
// the CPU register allocation. With this, even if no actual |
| 2346 |
// code is produced, the compiler happens to make better |
| 2347 |
// decisions. |
| 2348 |
val := @v; |
| 2349 |
s := src; |
| 2350 |
d := dst; |
| 2351 |
|
| 2352 |
if count > 0 then |
| 2353 |
begin |
| 2354 |
while count > 0 do |
| 2355 |
begin |
| 2356 |
if val.AddSubtract(PColor32Array(s)[srcRiOffset], s^) then |
| 2357 |
lastColor := val.Color; |
| 2358 |
inc(s); |
| 2359 |
d^ := lastColor; |
| 2360 |
inc(d); |
| 2361 |
dec(count); |
| 2362 |
end; |
| 2363 |
|
| 2364 |
count := w; |
| 2365 |
while count > 0 do |
| 2366 |
begin |
| 2367 |
d^ := lastColor; |
| 2368 |
inc(d); |
| 2369 |
dec(count); |
| 2370 |
end; |
| 2371 |
end; |
| 2372 |
|
| 2373 |
while PByte(d) <= PByte(dstLast) do |
| 2374 |
begin |
| 2375 |
if val.AddNoneSubtract(s^) then |
| 2376 |
lastColor := val.Color; |
| 2377 |
inc(s); |
| 2378 |
d^ := lastColor; |
| 2379 |
inc(d); |
| 2380 |
end; |
| 2381 |
end; |
| 2382 |
//------------------------------------------------------------------------------ |
| 2383 |
|
| 2384 |
procedure BoxBlurH(const src, dst: TArrayOfColor32; w,h, stdDev: integer); |
| 2385 |
var |
| 2386 |
i,j, ti, li, ri, re, ovr: integer; |
| 2387 |
fv, val: TWeightedColor; |
| 2388 |
lastColor: TColor32; |
| 2389 |
stdDevW: integer; |
| 2390 |
begin |
| 2391 |
ovr := Max(0, stdDev - w); |
| 2392 |
for i := 0 to h -1 do |
| 2393 |
begin |
| 2394 |
ti := i * w; |
| 2395 |
li := ti; |
| 2396 |
ri := ti +stdDev; |
| 2397 |
re := ti +w -1; // idx of last pixel in row |
| 2398 |
fv.Reset(src[ti]); |
| 2399 |
val.Reset(src[ti], stdDev +1); |
| 2400 |
for j := 0 to stdDev -1 - ovr do |
| 2401 |
val.Add(src[ti + j]); |
| 2402 |
if ovr > 0 then val.Add(clNone32, ovr); |
| 2403 |
for j := 0 to stdDev do |
| 2404 |
begin |
| 2405 |
if ri <= re then |
| 2406 |
val.Add(src[ri]) else |
| 2407 |
val.Add(src[re]); // color of last pixel in row |
| 2408 |
inc(ri); |
| 2409 |
val.Subtract(fv); |
| 2410 |
if ti <= re then |
| 2411 |
dst[ti] := val.Color; |
| 2412 |
inc(ti); |
| 2413 |
end; |
| 2414 |
|
| 2415 |
// Skip "val.Color" calculation if both for-loops are skipped anyway |
| 2416 |
stdDevW := w - stdDev*2 - 1; |
| 2417 |
if (ti <= re) or (stdDevW > 0) then |
| 2418 |
begin |
| 2419 |
if w > 4 then // prevent the call-overhead if it would be slower than the inline version |
| 2420 |
BoxBlurHLine(@src[li], @dst[ti], ri - li, re - ri + 1, stdDevW, @dst[re], val) |
| 2421 |
else |
| 2422 |
begin |
| 2423 |
lastColor := val.Color; |
| 2424 |
for j := stdDevW downto 1 do |
| 2425 |
begin |
| 2426 |
if ri <= re then |
| 2427 |
begin |
| 2428 |
if val.AddSubtract(src[ri], src[li]) then |
| 2429 |
lastColor := val.Color; |
| 2430 |
inc(ri); |
| 2431 |
inc(li); |
| 2432 |
end; |
| 2433 |
dst[ti] := lastColor; |
| 2434 |
inc(ti); |
| 2435 |
end; |
| 2436 |
while ti <= re do |
| 2437 |
begin |
| 2438 |
if val.AddNoneSubtract(src[li]) then |
| 2439 |
lastColor := val.Color; |
| 2440 |
inc(li); |
| 2441 |
dst[ti] := lastColor; |
| 2442 |
inc(ti); |
| 2443 |
end; |
| 2444 |
end; |
| 2445 |
end; |
| 2446 |
end; |
| 2447 |
end; |
| 2448 |
//------------------------------------------------------------------------------ |
| 2449 |
|
| 2450 |
procedure BoxBlurVLine(src, dst: PColor32; srcRiOffset: nativeint; |
| 2451 |
widthBytes, count, h: integer; dstLast: PColor32; var v: TWeightedColor); |
| 2452 |
var |
| 2453 |
lastColor: TColor32; |
| 2454 |
val: PWeightedColor; |
| 2455 |
s, d: PColor32; |
| 2456 |
begin |
| 2457 |
lastColor := v.Color; |
| 2458 |
if count > h then |
| 2459 |
count := h; |
| 2460 |
h := h - count; |
| 2461 |
|
| 2462 |
// The Delphi compiler sometimes is really stupid with |
| 2463 |
// the CPU register allocation. With this, even if no actual |
| 2464 |
// code is produced, the compiler happens to make better |
| 2465 |
// decisions. |
| 2466 |
val := @v; |
| 2467 |
s := src; |
| 2468 |
d := dst; |
| 2469 |
|
| 2470 |
if count > 0 then |
| 2471 |
begin |
| 2472 |
while count > 0 do |
| 2473 |
begin |
| 2474 |
if val.AddSubtract(PColor32Array(s)[srcRiOffset], s^) then |
| 2475 |
lastColor := val.Color; |
| 2476 |
inc(PByte(s), widthBytes); |
| 2477 |
d^ := lastColor; |
| 2478 |
inc(PByte(d), widthBytes); |
| 2479 |
dec(count); |
| 2480 |
end; |
| 2481 |
|
| 2482 |
count := h; |
| 2483 |
while count > 0 do |
| 2484 |
begin |
| 2485 |
d^ := lastColor; |
| 2486 |
inc(PByte(d), widthBytes); |
| 2487 |
dec(count); |
| 2488 |
end; |
| 2489 |
end; |
| 2490 |
|
| 2491 |
while PByte(d) <= PByte(dstLast) do |
| 2492 |
begin |
| 2493 |
if val.AddNoneSubtract(s^) then |
| 2494 |
lastColor := val.Color; |
| 2495 |
inc(PByte(s), widthBytes); |
| 2496 |
d^ := lastColor; |
| 2497 |
inc(PByte(d), widthBytes); |
| 2498 |
end; |
| 2499 |
end; |
| 2500 |
//------------------------------------------------------------------------------ |
| 2501 |
|
| 2502 |
procedure BoxBlurV(const src, dst: TArrayOfColor32; w, h, stdDev: integer); |
| 2503 |
var |
| 2504 |
i,j, ti, li, ri, re, ovr: integer; |
| 2505 |
fv, val: TWeightedColor; |
| 2506 |
lastColor: TColor32; |
| 2507 |
stdDevH: integer; |
| 2508 |
begin |
| 2509 |
ovr := Max(0, stdDev - h); |
| 2510 |
for i := 0 to w -1 do |
| 2511 |
begin |
| 2512 |
ti := i; |
| 2513 |
li := ti; |
| 2514 |
ri := ti + stdDev * w; |
| 2515 |
re := ti +w *(h-1); // idx of last pixel in column |
| 2516 |
fv.Reset(src[ti]); |
| 2517 |
val.Reset(src[ti], stdDev +1); |
| 2518 |
for j := 0 to stdDev -1 -ovr do |
| 2519 |
val.Add(src[ti + j *w]); |
| 2520 |
if ovr > 0 then val.Add(clNone32, ovr); |
| 2521 |
for j := 0 to stdDev do |
| 2522 |
begin |
| 2523 |
if ri <= re then |
| 2524 |
val.Add(src[ri]) else |
| 2525 |
val.Add(src[re]); // color of last pixel in column |
| 2526 |
inc(ri, w); |
| 2527 |
val.Subtract(fv); |
| 2528 |
if ti <= re then |
| 2529 |
dst[ti] := val.Color; |
| 2530 |
inc(ti, w); |
| 2531 |
end; |
| 2532 |
|
| 2533 |
// Skip "val.Color" calculation if both for-loops are skipped anyway |
| 2534 |
stdDevH := h - stdDev*2 - 1; |
| 2535 |
if (ti <= re) or (stdDevH > 0) then |
| 2536 |
begin |
| 2537 |
if stdDevH > 4 then // prevent the call-overhead if it would be slower than the inline version |
| 2538 |
BoxBlurVLine(@src[li], @dst[ti], ri - li, w * SizeOf(TColor32), re - ri + 1, stdDevH, @dst[re], val) |
| 2539 |
else |
| 2540 |
begin |
| 2541 |
lastColor := val.Color; |
| 2542 |
for j := stdDevH downto 1 do |
| 2543 |
begin |
| 2544 |
if ri <= re then |
| 2545 |
begin |
| 2546 |
if val.AddSubtract(src[ri], src[li]) then |
| 2547 |
lastColor := val.Color; |
| 2548 |
inc(ri, w); |
| 2549 |
inc(li, w); |
| 2550 |
end; |
| 2551 |
|
| 2552 |
dst[ti] := lastColor; |
| 2553 |
inc(ti, w); |
| 2554 |
end; |
| 2555 |
while ti <= re do |
| 2556 |
begin |
| 2557 |
if val.AddNoneSubtract(src[li]) then |
| 2558 |
lastColor := val.Color; |
| 2559 |
inc(li, w); |
| 2560 |
dst[ti] := lastColor; |
| 2561 |
inc(ti, w); |
| 2562 |
end; |
| 2563 |
end; |
| 2564 |
end; |
| 2565 |
end; |
| 2566 |
end; |
| 2567 |
//------------------------------------------------------------------------------ |
| 2568 |
|
| 2569 |
procedure FastGaussianBlur(img: TImage32; |
| 2570 |
const rec: TRect; stdDevX, stdDevY: integer; repeats: integer); |
| 2571 |
var |
| 2572 |
i,j,len, w,h: integer; |
| 2573 |
rec2: TRect; |
| 2574 |
boxesH: TArrayOfInteger; |
| 2575 |
boxesV: TArrayOfInteger; |
| 2576 |
src, dst: TArrayOfColor32; |
| 2577 |
blurFullImage: Boolean; |
| 2578 |
pSrc, pDst: PColor32; |
| 2579 |
begin |
| 2580 |
if not Assigned(img) then Exit; |
| 2581 |
Types.IntersectRect(rec2, rec, img.Bounds); |
| 2582 |
if IsEmptyRect(rec2) then Exit; |
| 2583 |
blurFullImage := RectsEqual(rec2, img.Bounds); |
| 2584 |
RectWidthHeight(rec2, w, h); |
| 2585 |
if (Min(w, h) < 2) or ((stdDevX < 1) and (stdDevY < 1)) then Exit; |
| 2586 |
len := w * h; |
| 2587 |
NewColor32Array(src, len, True); // content is overwritten in BoxBlurH |
| 2588 |
if blurFullImage then |
| 2589 |
begin |
| 2590 |
// Use the img.Pixels directly instead of copying the entire image into 'dst'. |
| 2591 |
// The first thing the code does is BoxBlurH({source:=}dst, {dest:=}src, ...). |
| 2592 |
dst := img.Pixels; |
| 2593 |
end |
| 2594 |
else |
| 2595 |
begin |
| 2596 |
// copy a rectangular region into 'dst' |
| 2597 |
NewColor32Array(dst, len, True); |
| 2598 |
pSrc := img.PixelRow[rec2.Top]; |
| 2599 |
inc(pSrc, rec2.Left); |
| 2600 |
pDst := @dst[0]; |
| 2601 |
for i := 0 to h -1 do |
| 2602 |
begin |
| 2603 |
Move(pSrc^, pDst^, w * SizeOf(TColor32)); |
| 2604 |
inc(pSrc, img.Width); |
| 2605 |
inc(pDst, w); |
| 2606 |
end; |
| 2607 |
end; |
| 2608 |
|
| 2609 |
// do the blur |
| 2610 |
inc(repeats); // now represents total iterations |
| 2611 |
boxesH := BoxesForGauss(stdDevX, repeats); |
| 2612 |
if stdDevY = stdDevX then |
| 2613 |
boxesV := boxesH else |
| 2614 |
boxesV := BoxesForGauss(stdDevY, repeats); |
| 2615 |
|
| 2616 |
img.BeginUpdate; |
| 2617 |
try |
| 2618 |
for j := 0 to repeats -1 do |
| 2619 |
begin |
| 2620 |
BoxBlurH(dst, src, w, h, boxesH[j]); |
| 2621 |
BoxBlurV(src, dst, w, h, boxesV[j]); |
| 2622 |
end; |
| 2623 |
|
| 2624 |
if not blurFullImage then |
| 2625 |
begin |
| 2626 |
// copy dst array back to image rect |
| 2627 |
pDst := img.PixelRow[rec2.Top]; |
| 2628 |
inc(pDst, rec2.Left); |
| 2629 |
pSrc := @dst[0]; |
| 2630 |
for i := 0 to h -1 do |
| 2631 |
begin |
| 2632 |
Move(pSrc^, pDst^, w * SizeOf(TColor32)); |
| 2633 |
inc(pSrc, w); |
| 2634 |
inc(pDst, img.Width); |
| 2635 |
end; |
| 2636 |
end; |
| 2637 |
finally |
| 2638 |
img.EndUpdate; |
| 2639 |
end; |
| 2640 |
end; |
| 2641 |
//------------------------------------------------------------------------------ |
| 2642 |
|
| 2643 |
end. |