123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103 |
- unit GR32_Blurs;
- (* BEGIN LICENSE BLOCK *********************************************************
- * Version: MPL 1.1 *
- * *
- * The contents of this file are subject to the Mozilla Public License Version *
- * 1.1 (the "License"); you may not use this file except in compliance with *
- * the License. You may obtain a copy of the License at *
- * http://www.mozilla.org/MPL/ *
- * *
- * Software distributed under the License is distributed on an "AS IS" basis, *
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License *
- * for the specific language governing rights and limitations under the *
- * License. *
- * *
- * Alternatively, the contents of this file may be used under the terms of the *
- * Free Pascal modified version of the GNU Lesser General Public License *
- * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions *
- * of this license are applicable instead of those above. *
- * Please see the file LICENSE.txt for additional information concerning this *
- * license. *
- * *
- * The Original Code is GR32_Blurs. The Gaussian blur algorithm was inspired *
- * by code published by Mario Klingemann and has been used with his permission. *
- * See also http://incubator.quasimondo.com *
- * *
- * Copyright 2012 - Angus Johnson *
- * *
- * Version 5.0 (Last updated 25-Sep-2012) *
- * *
- * END LICENSE BLOCK ***********************************************************)
- interface
- {$I GR32.inc}
- uses
- {$IFDEF FPC}
- LCLIntf,
- {$ELSE}
- Windows, Types,
- {$ENDIF}
- SysUtils, Classes, Math, GR32;
- type
- TBlurFunction = procedure(Bitmap32: TBitmap32; Radius: TFloat);
- TBlurFunctionBounds = procedure(Bitmap32: TBitmap32; Radius: TFloat;
- const Bounds: TRect);
- TBlurFunctionRegion = procedure(Bitmap32: TBitmap32; Radius: TFloat;
- const BlurRegion: TArrayOfFloatPoint);
- procedure GaussianBlur(Bitmap32: TBitmap32; Radius: TFloat); overload;
- procedure GaussianBlur(Bitmap32: TBitmap32; Radius: TFloat; const Bounds: TRect); overload;
- procedure GaussianBlur(Bitmap32: TBitmap32; Radius: TFloat;
- const BlurRegion: TArrayOfFloatPoint); overload;
- procedure GaussianBlurGamma(Bitmap32: TBitmap32; Radius: TFloat); overload;
- procedure GaussianBlurGamma(Bitmap32: TBitmap32; Radius: TFloat; const Bounds: TRect); overload;
- procedure GaussianBlurGamma(Bitmap32: TBitmap32; Radius: TFloat;
- const BlurRegion: TArrayOfFloatPoint); overload;
- procedure FastBlur(Bitmap32: TBitmap32; Radius: TFloat); overload;
- procedure FastBlur(Bitmap32: TBitmap32; Radius: TFloat; const Bounds: TRect); overload;
- procedure FastBlur(Bitmap32: TBitmap32; Radius: TFloat;
- const BlurRegion: TArrayOfFloatPoint); overload;
- procedure FastBlurGamma(Bitmap32: TBitmap32; Radius: TFloat); overload;
- procedure FastBlurGamma(Bitmap32: TBitmap32; Radius: TFloat; const Bounds: TRect); overload;
- procedure FastBlurGamma(Bitmap32: TBitmap32; Radius: TFloat;
- const BlurRegion: TArrayOfFloatPoint); overload;
- procedure MotionBlur(Bitmap32: TBitmap32;
- Dist, AngleDeg: TFloat; Bidirectional: Boolean = True); overload;
- procedure MotionBlur(Bitmap32: TBitmap32; Dist, AngleDeg: TFloat;
- const Bounds: TRect; Bidirectional: Boolean = True); overload;
- procedure MotionBlur(Bitmap32: TBitmap32; Dist, AngleDeg: TFloat;
- const BlurRegion: TArrayOfFloatPoint; Bidirectional: Boolean = True); overload;
- procedure MotionBlurGamma(Bitmap32: TBitmap32;
- Dist, AngleDeg: TFloat; Bidirectional: Boolean = True); overload;
- procedure MotionBlurGamma(Bitmap32: TBitmap32; Dist, AngleDeg: TFloat;
- const Bounds: TRect; Bidirectional: Boolean = True); overload;
- procedure MotionBlurGamma(Bitmap32: TBitmap32; Dist, AngleDeg: TFloat;
- const BlurRegion: TArrayOfFloatPoint; Bidirectional: Boolean = True); overload;
- const
- GaussianBlurSimple: array [Boolean] of TBlurFunction = (GaussianBlur, GaussianBlurGamma);
- GaussianBlurBounds: array [Boolean] of TBlurFunctionBounds = (GaussianBlur, GaussianBlurGamma);
- GaussianBlurRegion: array [Boolean] of TBlurFunctionRegion = (GaussianBlur, GaussianBlurGamma);
- FastBlurSimple: array [Boolean] of TBlurFunction = (FastBlur, FastBlurGamma);
- FastBlurBounds: array [Boolean] of TBlurFunctionBounds = (FastBlur, FastBlurGamma);
- FastBlurRegion: array [Boolean] of TBlurFunctionRegion = (FastBlur, FastBlurGamma);
- implementation
- uses
- GR32_Blend, GR32_Gamma, GR32_Resamplers, GR32_Polygons, GR32_LowLevel,
- GR32_VectorUtils, GR32_Transforms;
- type
- TSumRecInt64 = record
- B, G, R, A: Int64;
- Sum: Integer;
- end;
- TSumRecord = record
- B, G, R, A: Integer;
- Sum: Integer;
- end;
- const
- ChannelSize = 256; // ie 1 byte for each of A,R,G & B in TColor32
- ChannelSizeMin1 = ChannelSize - 1;
- procedure ResetSumRecord(var SumRecord: TSumRecInt64); overload;
- {$IFDEF USEINLINING} inline; {$ENDIF}
- begin
- FillChar(SumRecord, SizeOf(SumRecord), 0);
- end;
- procedure ResetSumRecord(var SumRecord: TSumRecord); overload;
- {$IFDEF USEINLINING} inline; {$ENDIF}
- begin
- FillChar(SumRecord, SizeOf(SumRecord), 0);
- end;
- function Divide(SumRecord: TSumRecInt64): TSumRecInt64; overload;
- {$IFDEF USEINLINING} inline; {$ENDIF}
- begin
- Result.A := SumRecord.A div SumRecord.Sum;
- Result.R := SumRecord.R div SumRecord.Sum;
- Result.G := SumRecord.G div SumRecord.Sum;
- Result.B := SumRecord.B div SumRecord.Sum;
- end;
- function Divide(SumRecord: TSumRecord): TSumRecord; overload;
- {$IFDEF USEINLINING} inline; {$ENDIF}
- begin
- Result.A := SumRecord.A div SumRecord.Sum;
- Result.R := SumRecord.R div SumRecord.Sum;
- Result.G := SumRecord.G div SumRecord.Sum;
- Result.B := SumRecord.B div SumRecord.Sum;
- end;
- function DivideToColor32(SumRecord: TSumRecInt64): TColor32Entry; overload;
- {$IFDEF USEINLINING} inline; {$ENDIF}
- begin
- Result.A := SumRecord.A div SumRecord.Sum;
- Result.R := SumRecord.R div SumRecord.Sum;
- Result.G := SumRecord.G div SumRecord.Sum;
- Result.B := SumRecord.B div SumRecord.Sum;
- end;
- function DivideToColor32(SumRecord: TSumRecord): TColor32Entry; overload;
- {$IFDEF USEINLINING} inline; {$ENDIF}
- begin
- Result.A := SumRecord.A div SumRecord.Sum;
- Result.R := SumRecord.R div SumRecord.Sum;
- Result.G := SumRecord.G div SumRecord.Sum;
- Result.B := SumRecord.B div SumRecord.Sum;
- end;
- { GaussianBlur }
- {$R-}
- procedure GaussianBlur(Bitmap32: TBitmap32; Radius: TFloat);
- begin
- GaussianBlur(Bitmap32, Radius, Bitmap32.BoundsRect);
- end;
- procedure GaussianBlur(Bitmap32: TBitmap32; Radius: TFloat; const Bounds: TRect);
- var
- Q, I, J, X, Y, ImageWidth, RowOffset, RadiusI: Integer;
- RecLeft, RecTop, RecRight, RecBottom: Integer;
- ImagePixels: PColor32EntryArray;
- RadiusSq, RadiusRevSq, KernelSize: Integer;
- SumRec: TSumRecInt64;
- PreMulArray: array of TColor32Entry;
- SumArray: array of TSumRecInt64;
- GaussLUT: array of array of Cardinal;
- begin
- RadiusI := Round(Radius);
- if RadiusI < 1 then
- Exit
- else if RadiusI > 128 then
- RadiusI := 128; // nb: performance degrades exponentially with >> Radius
- // initialize the look-up-table ...
- KernelSize := RadiusI * 2 + 1;
- SetLength(GaussLUT, KernelSize);
- for I := 0 to KernelSize - 1 do
- SetLength(GaussLUT[I], ChannelSize);
- for I := 1 to RadiusI do
- begin
- RadiusRevSq := Round((Radius + 1 - I) * (Radius + 1 - I));
- for J := 0 to ChannelSizeMin1 do
- begin
- GaussLUT[RadiusI - I][J] := RadiusRevSq * J;
- GaussLUT[RadiusI + I][J] := GaussLUT[RadiusI - I][J];
- end;
- end;
- RadiusSq := Round((Radius + 1) * (Radius + 1));
- for J := 0 to ChannelSizeMin1 do
- GaussLUT[RadiusI][J] := RadiusSq * J;
- ImageWidth := Bitmap32.Width;
- SetLength(SumArray, ImageWidth * Bitmap32.Height);
- ImagePixels := PColor32EntryArray(Bitmap32.Bits);
- RecLeft := Max(Bounds.Left, 0);
- RecTop := Max(Bounds.Top, 0);
- RecRight := Min(Bounds.Right, ImageWidth - 1);
- RecBottom := Min(Bounds.Bottom, Bitmap32.Height - 1);
- RowOffset := RecTop * ImageWidth;
- SetLength(PreMulArray, Bitmap32.Width);
- for Y := RecTop to RecBottom do
- begin
- // initialize PreMulArray for the row ...
- Q := (Y * ImageWidth) + RecLeft;
- for X := RecLeft to RecRight do
- with ImagePixels[Q] do
- begin
- PreMulArray[X].A := A;
- PreMulArray[X].R := DivTable[R, A];
- PreMulArray[X].G := DivTable[G, A];
- PreMulArray[X].B := DivTable[B, A];
- Inc(Q);
- end;
- for X := RecLeft to RecRight do
- begin
- ResetSumRecord(SumRec);
- I := Max(X - RadiusI, RecLeft);
- Q := I - (X - RadiusI);
- for I := I to Min(X + RadiusI, RecRight) do
- with PreMulArray[I] do
- begin
- Inc(SumRec.A, GaussLUT[Q][A]);
- Inc(SumRec.R, GaussLUT[Q][R]);
- Inc(SumRec.G, GaussLUT[Q][G]);
- Inc(SumRec.B, GaussLUT[Q][B]);
- Inc(SumRec.Sum, GaussLUT[Q][1]);
- Inc(Q);
- end;
- Q := RowOffset + X;
- SumArray[Q] := Divide(SumRec);
- end;
- Inc(RowOffset, ImageWidth);
- end;
- RowOffset := RecTop * ImageWidth;
- for Y := RecTop to RecBottom do
- begin
- for X := RecLeft to RecRight do
- begin
- ResetSumRecord(SumRec);
- I := Max(Y - RadiusI, RecTop);
- Q := I - (Y - RadiusI);
- for I := I to Min(Y + RadiusI, RecBottom) do
- with SumArray[X + I * ImageWidth] do
- begin
- Inc(SumRec.A, GaussLUT[Q][A]);
- Inc(SumRec.R, GaussLUT[Q][R]);
- Inc(SumRec.G, GaussLUT[Q][G]);
- Inc(SumRec.B, GaussLUT[Q][B]);
- Inc(SumRec.Sum, GaussLUT[Q][1]);
- Inc(Q);
- end;
- with ImagePixels[RowOffset + X] do
- begin
- A := (SumRec.A div SumRec.Sum);
- R := RcTable[A, (SumRec.R div SumRec.Sum)];
- G := RcTable[A, (SumRec.G div SumRec.Sum)];
- B := RcTable[A, (SumRec.B div SumRec.Sum)];
- end;
- end;
- Inc(RowOffset, ImageWidth);
- end;
- end;
- procedure GaussianBlur(Bitmap32: TBitmap32; Radius: TFloat;
- const BlurRegion: TArrayOfFloatPoint);
- var
- Q, I, J, X, Y, ImageWidth, RowOffset, RadiusI: Integer;
- RecLeft, RecTop, RecRight, RecBottom: Integer;
- ImagePixels: PColor32EntryArray;
- RadiusSq, RadiusRevSq, KernelSize: Integer;
- SumRec: TSumRecInt64;
- SumArray: array of TSumRecInt64;
- GaussLUT: array of array of Cardinal;
- PreMulArray: array of TColor32Entry;
- Alpha: Cardinal;
- Mask: TBitmap32;
- Clr, MaskClr: TColor32Entry;
- Pts: TArrayOfFloatPoint;
- Bounds: TRect;
- begin
- with PolygonBounds(BlurRegion) do
- Bounds := Rect(Floor(Left), Floor(Top), Ceil(Right), Ceil(Bottom));
- if Bounds.Left < 0 then Bounds.Left := 0;
- if Bounds.Top < 0 then Bounds.Top := 0;
- if Bounds.Right >= Bitmap32.Width then Bounds.Right := Bitmap32.Width - 1;
- if Bounds.Bottom >= Bitmap32.Height then Bounds.Bottom := Bitmap32.Height - 1;
- RadiusI := round(Radius);
- if (RadiusI < 1) or (Bounds.Right <= Bounds.Left) or (Bounds.Bottom <= Bounds.Top) then
- Exit
- else if RadiusI > 128 then
- RadiusI := 128; // nb: performance degrades exponentially with >> Radius
- Mask := TBitmap32.Create;
- try
- Mask.SetSize(Bounds.Right - Bounds.Left + 1, Bounds.Bottom - Bounds.Top + 1);
- SetLength(Pts, Length(BlurRegion));
- for I := 0 to High(BlurRegion) do
- begin
- Pts[I].X := BlurRegion[I].X - Bounds.Left;
- Pts[I].Y := BlurRegion[I].Y - Bounds.Top;
- end;
- PolygonFS(Mask, Pts, clWhite32);
- // initialize the look-up-table ...
- KernelSize := RadiusI * 2 + 1;
- SetLength(GaussLUT, KernelSize);
- for I := 0 to KernelSize - 1 do
- SetLength(GaussLUT[I], ChannelSize);
- for I := 1 to RadiusI do
- begin
- RadiusRevSq := Round((Radius + 1 - I) * (Radius + 1 - I));
- for J := 0 to ChannelSizeMin1 do
- begin
- GaussLUT[RadiusI - I][J] := RadiusRevSq * J;
- GaussLUT[RadiusI + I][J] := GaussLUT[RadiusI - I][J];
- end;
- end;
- RadiusSq := Round((Radius + 1) * (Radius + 1));
- for J := 0 to ChannelSizeMin1 do
- GaussLUT[RadiusI][J] := RadiusSq * J;
- ImageWidth := Bitmap32.Width;
- SetLength(SumArray, ImageWidth * Bitmap32.Height);
- ImagePixels := PColor32EntryArray(Bitmap32.Bits);
- RecLeft := Max(Bounds.Left, 0);
- RecTop := Max(Bounds.Top, 0);
- RecRight := Min(Bounds.Right, ImageWidth - 1);
- RecBottom := Min(Bounds.Bottom, Bitmap32.Height - 1);
- RowOffset := RecTop * ImageWidth;
- SetLength(PreMulArray, Bitmap32.Width);
- for Y := RecTop to RecBottom do
- begin
- // initialize PreMulArray for the current row ...
- Q := (Y * ImageWidth) + RecLeft;
- for X := RecLeft to RecRight do
- with ImagePixels[Q] do
- begin
- PreMulArray[X].A := A;
- PreMulArray[X].R := DivTable[R, A];
- PreMulArray[X].G := DivTable[G, A];
- PreMulArray[X].B := DivTable[B, A];
- Inc(Q);
- end;
- for X := RecLeft to RecRight do
- begin
- ResetSumRecord(SumRec);
- I := Max(X - RadiusI, RecLeft);
- Q := I - (X - RadiusI);
- for I := I to Min(X + RadiusI, RecRight) do
- with PreMulArray[I] do
- begin
- Inc(SumRec.A, GaussLUT[Q][A]);
- Inc(SumRec.R, GaussLUT[Q][R]);
- Inc(SumRec.G, GaussLUT[Q][G]);
- Inc(SumRec.B, GaussLUT[Q][B]);
- Inc(SumRec.Sum, GaussLUT[Q][1]);
- Inc(Q);
- end;
- Q := RowOffset + X;
- SumArray[Q] := Divide(SumRec);
- end;
- Inc(RowOffset, ImageWidth);
- end;
- RowOffset := RecTop * ImageWidth;
- for Y := RecTop to RecBottom do
- begin
- for X := RecLeft to RecRight do
- begin
- MaskClr.ARGB := Mask.Pixel[X - RecLeft, Y - RecTop];
- if (MaskClr.A = 0) then Continue;
- ResetSumRecord(SumRec);
- I := Max(Y - RadiusI, RecTop);
- Q := I - (Y - RadiusI);
- for I := I to Min(Y + RadiusI, RecBottom) do
- with SumArray[X + I * ImageWidth] do
- begin
- Inc(SumRec.A, GaussLUT[Q][A]);
- Inc(SumRec.R, GaussLUT[Q][R]);
- Inc(SumRec.G, GaussLUT[Q][G]);
- Inc(SumRec.B, GaussLUT[Q][B]);
- Inc(SumRec.Sum, GaussLUT[Q][1]);
- Inc(Q);
- end;
- with ImagePixels[RowOffset + X] do
- if (MaskClr.A < 255) then
- begin
- Clr.A := SumRec.A div SumRec.Sum;
- Clr.R := RcTable[Clr.A, SumRec.R div SumRec.Sum];
- Clr.G := RcTable[Clr.A, SumRec.G div SumRec.Sum];
- Clr.B := RcTable[Clr.A, SumRec.B div SumRec.Sum];
- BlendMemEx(Clr.ARGB, ARGB, MaskClr.A);
- end else
- begin
- A := SumRec.A div SumRec.Sum;
- R := RcTable[A, SumRec.R div SumRec.Sum];
- G := RcTable[A, SumRec.G div SumRec.Sum];
- B := RcTable[A, SumRec.B div SumRec.Sum];
- end;
- end;
- Inc(RowOffset, ImageWidth);
- end;
- EMMS;
- finally
- Mask.Free;
- end;
- end;
- procedure GaussianBlurGamma(Bitmap32: TBitmap32; Radius: TFloat);
- begin
- GaussianBlurGamma(Bitmap32, Radius, Bitmap32.BoundsRect);
- end;
- procedure GaussianBlurGamma(Bitmap32: TBitmap32; Radius: TFloat; const Bounds: TRect);
- var
- Q, I, J, X, Y, ImageWidth, RowOffset, RadiusI: Integer;
- RecLeft, RecTop, RecRight, RecBottom: Integer;
- ImagePixels: PColor32EntryArray;
- RadiusSq, RadiusRevSq, KernelSize: Integer;
- SumRec: TSumRecInt64;
- PreMulArray: array of TColor32Entry;
- SumArray: array of TSumRecInt64;
- GaussLUT: array of array of Cardinal;
- begin
- RadiusI := Round(Radius);
- if RadiusI < 1 then
- Exit
- else if RadiusI > 128 then
- RadiusI := 128; // nb: performance degrades exponentially with >> Radius
- // initialize the look-up-table ...
- KernelSize := RadiusI * 2 + 1;
- SetLength(GaussLUT, KernelSize);
- for I := 0 to KernelSize - 1 do
- SetLength(GaussLUT[I], ChannelSize);
- for I := 1 to RadiusI do
- begin
- RadiusRevSq := Round((Radius + 1 - I) * (Radius + 1 - I));
- for J := 0 to ChannelSizeMin1 do
- begin
- GaussLUT[RadiusI - I][J] := RadiusRevSq * J;
- GaussLUT[RadiusI + I][J] := GaussLUT[RadiusI - I][J];
- end;
- end;
- RadiusSq := Round((Radius + 1) * (Radius + 1));
- for J := 0 to ChannelSizeMin1 do
- GaussLUT[RadiusI][J] := RadiusSq * J;
- ImageWidth := Bitmap32.Width;
- SetLength(SumArray, ImageWidth * Bitmap32.Height);
- ImagePixels := PColor32EntryArray(Bitmap32.Bits);
- RecLeft := Max(Bounds.Left, 0);
- RecTop := Max(Bounds.Top, 0);
- RecRight := Min(Bounds.Right, ImageWidth - 1);
- RecBottom := Min(Bounds.Bottom, Bitmap32.Height - 1);
- RowOffset := RecTop * ImageWidth;
- SetLength(PreMulArray, Bitmap32.Width);
- for Y := RecTop to RecBottom do
- begin
- // initialize PreMulArray for the row ...
- Q := (Y * ImageWidth) + RecLeft;
- for X := RecLeft to RecRight do
- with ImagePixels[Q] do
- begin
- PreMulArray[X].A := A;
- PreMulArray[X].R := DivTable[GAMMA_DECODING_TABLE[R], A];
- PreMulArray[X].G := DivTable[GAMMA_DECODING_TABLE[G], A];
- PreMulArray[X].B := DivTable[GAMMA_DECODING_TABLE[B], A];
- Inc(Q);
- end;
- for X := RecLeft to RecRight do
- begin
- ResetSumRecord(SumRec);
- I := Max(X - RadiusI, RecLeft);
- Q := I - (X - RadiusI);
- for I := I to Min(X + RadiusI, RecRight) do
- with PreMulArray[I] do
- begin
- Inc(SumRec.A, GaussLUT[Q][A]);
- Inc(SumRec.R, GaussLUT[Q][R]);
- Inc(SumRec.G, GaussLUT[Q][G]);
- Inc(SumRec.B, GaussLUT[Q][B]);
- Inc(SumRec.Sum, GaussLUT[Q][1]);
- Inc(Q);
- end;
- Q := RowOffset + X;
- SumArray[Q] := Divide(SumRec);
- end;
- Inc(RowOffset, ImageWidth);
- end;
- RowOffset := RecTop * ImageWidth;
- for Y := RecTop to RecBottom do
- begin
- for X := RecLeft to RecRight do
- begin
- ResetSumRecord(SumRec);
- I := Max(Y - RadiusI, RecTop);
- Q := I - (Y - RadiusI);
- for I := I to Min(Y + RadiusI, RecBottom) do
- with SumArray[X + I * ImageWidth] do
- begin
- Inc(SumRec.A, GaussLUT[Q][A]);
- Inc(SumRec.R, GaussLUT[Q][R]);
- Inc(SumRec.G, GaussLUT[Q][G]);
- Inc(SumRec.B, GaussLUT[Q][B]);
- Inc(SumRec.Sum, GaussLUT[Q][1]);
- Inc(Q);
- end;
- with ImagePixels[RowOffset + X] do
- begin
- A := (SumRec.A div SumRec.Sum);
- R := GAMMA_ENCODING_TABLE[RcTable[A, (SumRec.R div SumRec.Sum)]];
- G := GAMMA_ENCODING_TABLE[RcTable[A, (SumRec.G div SumRec.Sum)]];
- B := GAMMA_ENCODING_TABLE[RcTable[A, (SumRec.B div SumRec.Sum)]];
- end;
- end;
- Inc(RowOffset, ImageWidth);
- end;
- end;
- procedure GaussianBlurGamma(Bitmap32: TBitmap32; Radius: TFloat;
- const BlurRegion: TArrayOfFloatPoint);
- var
- Q, I, J, X, Y, ImageWidth, RowOffset, RadiusI: Integer;
- RecLeft, RecTop, RecRight, RecBottom: Integer;
- ImagePixels: PColor32EntryArray;
- RadiusSq, RadiusRevSq, KernelSize: Integer;
- SumRec: TSumRecInt64;
- SumArray: array of TSumRecInt64;
- GaussLUT: array of array of Cardinal;
- PreMulArray: array of TColor32Entry;
- Alpha: Cardinal;
- Mask: TBitmap32;
- Clr, MaskClr: TColor32Entry;
- Pts: TArrayOfFloatPoint;
- Bounds: TRect;
- begin
- with PolygonBounds(BlurRegion) do
- Bounds := Rect(Floor(Left), Floor(Top), Ceil(Right), Ceil(Bottom));
- if Bounds.Left < 0 then Bounds.Left := 0;
- if Bounds.Top < 0 then Bounds.Top := 0;
- if Bounds.Right >= Bitmap32.Width then Bounds.Right := Bitmap32.Width - 1;
- if Bounds.Bottom >= Bitmap32.Height then Bounds.Bottom := Bitmap32.Height - 1;
- RadiusI := round(Radius);
- if (RadiusI < 1) or (Bounds.Right <= Bounds.Left) or (Bounds.Bottom <= Bounds.Top) then
- Exit
- else if RadiusI > 128 then
- RadiusI := 128; // nb: performance degrades exponentially with >> Radius
- Mask := TBitmap32.Create;
- try
- Mask.SetSize(Bounds.Right - Bounds.Left + 1, Bounds.Bottom - Bounds.Top + 1);
- SetLength(Pts, Length(BlurRegion));
- for I := 0 to High(BlurRegion) do
- begin
- Pts[I].X := BlurRegion[I].X - Bounds.Left;
- Pts[I].Y := BlurRegion[I].Y - Bounds.Top;
- end;
- PolygonFS(Mask, Pts, clWhite32);
- // initialize the look-up-table ...
- KernelSize := RadiusI * 2 + 1;
- SetLength(GaussLUT, KernelSize);
- for I := 0 to KernelSize - 1 do
- SetLength(GaussLUT[I], ChannelSize);
- for I := 1 to RadiusI do
- begin
- RadiusRevSq := Round((Radius + 1 - I) * (Radius + 1 - I));
- for J := 0 to ChannelSizeMin1 do
- begin
- GaussLUT[RadiusI - I][J] := RadiusRevSq * J;
- GaussLUT[RadiusI + I][J] := GaussLUT[RadiusI - I][J];
- end;
- end;
- RadiusSq := Round((Radius + 1) * (Radius + 1));
- for J := 0 to ChannelSizeMin1 do
- GaussLUT[RadiusI][J] := RadiusSq * J;
- ImageWidth := Bitmap32.Width;
- SetLength(SumArray, ImageWidth * Bitmap32.Height);
- ImagePixels := PColor32EntryArray(Bitmap32.Bits);
- RecLeft := Max(Bounds.Left, 0);
- RecTop := Max(Bounds.Top, 0);
- RecRight := Min(Bounds.Right, ImageWidth - 1);
- RecBottom := Min(Bounds.Bottom, Bitmap32.Height - 1);
- RowOffset := RecTop * ImageWidth;
- SetLength(PreMulArray, Bitmap32.Width);
- for Y := RecTop to RecBottom do
- begin
- // initialize PreMulArray for the current row ...
- Q := (Y * ImageWidth) + RecLeft;
- for X := RecLeft to RecRight do
- with ImagePixels[Q] do
- begin
- PreMulArray[X].A := A;
- PreMulArray[X].R := DivTable[GAMMA_DECODING_TABLE[R], A];
- PreMulArray[X].G := DivTable[GAMMA_DECODING_TABLE[G], A];
- PreMulArray[X].B := DivTable[GAMMA_DECODING_TABLE[B], A];
- Inc(Q);
- end;
- for X := RecLeft to RecRight do
- begin
- ResetSumRecord(SumRec);
- I := Max(X - RadiusI, RecLeft);
- Q := I - (X - RadiusI);
- for I := I to Min(X + RadiusI, RecRight) do
- with PreMulArray[I] do
- begin
- Inc(SumRec.A, GaussLUT[Q][A]);
- Inc(SumRec.R, GaussLUT[Q][R]);
- Inc(SumRec.G, GaussLUT[Q][G]);
- Inc(SumRec.B, GaussLUT[Q][B]);
- Inc(SumRec.Sum, GaussLUT[Q][1]);
- Inc(Q);
- end;
- Q := RowOffset + X;
- SumArray[Q] := Divide(SumRec);
- end;
- Inc(RowOffset, ImageWidth);
- end;
- RowOffset := RecTop * ImageWidth;
- for Y := RecTop to RecBottom do
- begin
- for X := RecLeft to RecRight do
- begin
- MaskClr.ARGB := Mask.Pixel[X - RecLeft, Y - RecTop];
- if (MaskClr.A = 0) then Continue;
- ResetSumRecord(SumRec);
- I := Max(Y - RadiusI, RecTop);
- Q := I - (Y - RadiusI);
- for I := I to Min(Y + RadiusI, RecBottom) do
- with SumArray[X + I * ImageWidth] do
- begin
- Inc(SumRec.A, GaussLUT[Q][A]);
- Inc(SumRec.R, GaussLUT[Q][R]);
- Inc(SumRec.G, GaussLUT[Q][G]);
- Inc(SumRec.B, GaussLUT[Q][B]);
- Inc(SumRec.Sum, GaussLUT[Q][1]);
- Inc(Q);
- end;
- with ImagePixels[RowOffset + X] do
- if (MaskClr.A < 255) then
- begin
- Clr.A := SumRec.A div SumRec.Sum;
- Clr.R := GAMMA_ENCODING_TABLE[RcTable[Clr.A, SumRec.R div SumRec.Sum]];
- Clr.G := GAMMA_ENCODING_TABLE[RcTable[Clr.A, SumRec.G div SumRec.Sum]];
- Clr.B := GAMMA_ENCODING_TABLE[RcTable[Clr.A, SumRec.B div SumRec.Sum]];
- BlendMemEx(Clr.ARGB, ARGB, MaskClr.A);
- end else
- begin
- A := SumRec.A div SumRec.Sum;
- R := GAMMA_ENCODING_TABLE[RcTable[A, SumRec.R div SumRec.Sum]];
- G := GAMMA_ENCODING_TABLE[RcTable[A, SumRec.G div SumRec.Sum]];
- B := GAMMA_ENCODING_TABLE[RcTable[A, SumRec.B div SumRec.Sum]];
- end;
- end;
- Inc(RowOffset, ImageWidth);
- end;
- EMMS;
- finally
- Mask.Free;
- end;
- end;
- { FastBlur }
- procedure FastBlur(Bitmap32: TBitmap32; Radius: TFloat);
- begin
- FastBlur(Bitmap32, Radius, Bitmap32.BoundsRect);
- end;
- procedure FastBlur(Bitmap32: TBitmap32; Radius: TFloat; const Bounds: TRect);
- var
- LL, RR, TT, BB, XX, YY, I, J, X, Y, RadiusI, Passes: Integer;
- RecLeft, RecTop, RecRight, RecBottom: Integer;
- ImagePixel: PColor32Entry;
- SumRec: TSumRecord;
- ImgPixel: PColor32Entry;
- Pixels: array of TColor32Entry;
- begin
- if Radius < 1 then
- Exit
- else if Radius > 256 then
- Radius := 256;
- RadiusI := Round(Radius / Sqrt(-2 * Ln(COne255th)));
- if RadiusI < 2 then
- begin
- Passes := Round(Radius);
- RadiusI := 1;
- end else
- Passes := 3;
- RecLeft := Max(Bounds.Left, 0);
- RecTop := Max(Bounds.Top, 0);
- RecRight := Min(Bounds.Right, Bitmap32.Width - 1);
- RecBottom := Min(Bounds.Bottom, Bitmap32.Height - 1);
- SetLength(Pixels, Max(Bitmap32.Width, Bitmap32.Height) + 1);
- // pre-multiply alphas ...
- for Y := RecTop to RecBottom do
- begin
- ImgPixel := PColor32Entry(Bitmap32.ScanLine[Y]);
- Inc(ImgPixel, RecLeft);
- for X := RecLeft to RecRight do
- with ImgPixel^ do
- begin
- R := DivTable[R, A];
- G := DivTable[G, A];
- B := DivTable[B, A];
- Inc(ImgPixel);
- end;
- end;
- for I := 1 to Passes do
- begin
- // horizontal pass...
- for Y := RecTop to RecBottom do
- begin
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[Y][RecLeft]);
- // fill the Pixels buffer with a copy of the row's pixels ...
- MoveLongword(ImagePixel^, Pixels[RecLeft], RecRight - RecLeft + 1);
- ResetSumRecord(SumRec);
- LL := RecLeft;
- RR := RecLeft + RadiusI;
- if RR > RecRight then RR := RecRight;
- // update first in row ...
- for XX := LL to RR do
- with Pixels[XX] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum);
- end;
- ImagePixel^ := DivideToColor32(SumRec);
- // update the remaining pixels in the row ...
- for X := RecLeft + 1 to RecRight do
- begin
- Inc(ImagePixel);
- LL := X - RadiusI - 1;
- RR := X + RadiusI;
- if LL >= RecLeft then
- with Pixels[LL] do
- begin
- Dec(SumRec.A, A);
- Dec(SumRec.R, R);
- Dec(SumRec.G, G);
- Dec(SumRec.B, B);
- Dec(SumRec.Sum);
- end;
- if RR <= RecRight then
- with Pixels[RR] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum);
- end;
- ImagePixel^ := DivideToColor32(SumRec);
- end;
- end;
- // vertical pass...
- for X := RecLeft to RecRight do
- begin
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[RecTop][X]);
- for J := RecTop to RecBottom do
- begin
- Pixels[J] := ImagePixel^;
- Inc(ImagePixel, Bitmap32.Width);
- end;
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[RecTop][X]);
- TT := RecTop;
- BB := RecTop + RadiusI;
- if BB > RecBottom then BB := RecBottom;
- ResetSumRecord(SumRec);
- // update first in col ...
- for YY := TT to BB do
- with Pixels[YY] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum);
- end;
- ImagePixel^ := DivideToColor32(SumRec);
- // update remainder in col ...
- for Y := RecTop + 1 to RecBottom do
- begin
- Inc(ImagePixel, Bitmap32.Width);
- TT := Y - RadiusI - 1;
- BB := Y + RadiusI;
- if TT >= RecTop then
- with Pixels[TT] do
- begin
- Dec(SumRec.A, A);
- Dec(SumRec.R, R);
- Dec(SumRec.G, G);
- Dec(SumRec.B, B);
- Dec(SumRec.Sum);
- end;
- if BB <= RecBottom then
- with Pixels[BB] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum);
- end;
- ImagePixel^ := DivideToColor32(SumRec);
- end;
- end;
- end;
- // extract alphas ...
- for Y := RecTop to RecBottom do
- begin
- ImgPixel := PColor32Entry(@Bitmap32.ScanLine[Y][RecLeft]);
- for X := RecLeft to RecRight do
- begin
- ImgPixel.R := RcTable[ImgPixel.A, ImgPixel.R];
- ImgPixel.G := RcTable[ImgPixel.A, ImgPixel.G];
- ImgPixel.B := RcTable[ImgPixel.A, ImgPixel.B];
- Inc(ImgPixel);
- end;
- end;
- end;
- procedure FastBlur(Bitmap32: TBitmap32; Radius: TFloat; const BlurRegion: TArrayOfFloatPoint);
- var
- LL, RR, TT, BB, XX, YY, I, J, X, Y, RadiusI, Passes: Integer;
- RecLeft, RecTop, RecRight, RecBottom: Integer;
- ImagePixel: PColor32Entry;
- SumRec: TSumRecord;
- ImgPixel: PColor32Entry;
- Pixels: array of TSumRecord;
- Mask: TBitmap32;
- Clr, MaskClr: TColor32Entry;
- Pts: TArrayOfFloatPoint;
- Bounds: TRect;
- begin
- if Radius < 1 then
- Exit
- else if Radius > 256 then
- Radius := 256;
- RadiusI := Round(Radius / Sqrt(-2 * Ln(COne255th)));
- if RadiusI < 2 then
- begin
- Passes := Round(Radius);
- RadiusI := 1;
- end else
- Passes := 3;
- with PolygonBounds(BlurRegion) do
- Bounds := Rect(Floor(Left), Floor(Top), Ceil(Right), Ceil(Bottom));
- if Bounds.Left < 0 then Bounds.Left := 0;
- if Bounds.Top < 0 then Bounds.Top := 0;
- if Bounds.Right >= Bitmap32.Width then Bounds.Right := Bitmap32.Width - 1;
- if Bounds.Bottom >= Bitmap32.Height then Bounds.Bottom := Bitmap32.Height - 1;
- RecLeft := Max(Bounds.Left, 0);
- RecTop := Max(Bounds.Top, 0);
- RecRight := Min(Bounds.Right, Bitmap32.Width - 1);
- RecBottom := Min(Bounds.Bottom, Bitmap32.Height - 1);
- SetLength(Pixels, Max(Bitmap32.Width, Bitmap32.Height) + 1);
- // pre-multiply alphas ...
- for Y := RecTop to RecBottom do
- begin
- ImgPixel := PColor32Entry(Bitmap32.ScanLine[Y]);
- Inc(ImgPixel, RecLeft);
- for X := RecLeft to RecRight do
- begin
- ImgPixel.R := DivTable[ImgPixel.R, ImgPixel.A];
- ImgPixel.G := DivTable[ImgPixel.G, ImgPixel.A];
- ImgPixel.B := DivTable[ImgPixel.B, ImgPixel.A];
- Inc(ImgPixel);
- end;
- end;
- Mask := TBitmap32.Create;
- try
- Mask.SetSize(Bounds.Right - Bounds.Left + 1, Bounds.Bottom - Bounds.Top + 1);
- SetLength(Pts, Length(BlurRegion));
- for I := 0 to High(BlurRegion) do
- begin
- Pts[I].X := BlurRegion[I].X - Bounds.Left;
- Pts[I].Y := BlurRegion[I].Y - Bounds.Top;
- end;
- PolygonFS(Mask, Pts, clWhite32);
- for I := 1 to Passes do
- begin
- // horizontal pass...
- for Y := RecTop to RecBottom do
- begin
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[Y][RecLeft]);
- // fill the Pixels buffer with a copy of the row's pixels ...
- for J := RecLeft to RecRight do
- begin
- MaskClr.ARGB := Mask.Pixel[J - RecLeft, Y - RecTop];
- if (MaskClr.A = 0) then
- begin
- Pixels[J].A := 0;
- Pixels[J].R := 0;
- Pixels[J].G := 0;
- Pixels[J].B := 0;
- Pixels[J].Sum := 0;
- end else
- with ImagePixel^ do
- begin
- Pixels[J].A := A;
- Pixels[J].R := R;
- Pixels[J].G := G;
- Pixels[J].B := B;
- Pixels[J].Sum := 1;
- end;
- Inc(ImagePixel);
- end;
- LL := RecLeft;
- RR := RecLeft + RadiusI;
- if RR > RecRight then RR := RecRight;
- ResetSumRecord(SumRec);
- // update first in row ...
- for XX := LL to RR do
- with Pixels[XX] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[Y][RecLeft]);
- MaskClr.ARGB := Mask.Pixel[0, Y - RecTop];
- if (MaskClr.A > 0) and (SumRec.Sum > 0) then
- ImagePixel^ := DivideToColor32(SumRec);
- // update the remaining pixels in the row ...
- for X := RecLeft + 1 to RecRight do
- begin
- Inc(ImagePixel);
- LL := X - RadiusI - 1;
- RR := X + RadiusI;
- if LL >= RecLeft then
- with Pixels[LL] do
- begin
- Dec(SumRec.A, A);
- Dec(SumRec.R, R);
- Dec(SumRec.G, G);
- Dec(SumRec.B, B);
- Dec(SumRec.Sum, Sum);
- end;
- if RR <= RecRight then
- with Pixels[RR] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- MaskClr.ARGB := Mask.Pixel[X - RecLeft, Y - RecTop];
- if (SumRec.Sum > 0) and (MaskClr.A = 255) then
- ImagePixel^ := DivideToColor32(SumRec);
- end;
- end;
- // vertical pass...
- for X := RecLeft to RecRight do
- begin
- // fill the Pixels buffer with a copy of the col's pixels ...
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[RecTop][X]);
- for J := RecTop to RecBottom do
- begin
- MaskClr.ARGB := Mask.Pixel[X - RecLeft, J - RecTop];
- if (MaskClr.A = 0) then
- begin
- Pixels[J].A := 0;
- Pixels[J].R := 0;
- Pixels[J].G := 0;
- Pixels[J].B := 0;
- Pixels[J].Sum := 0;
- end else
- with ImagePixel^ do
- begin
- Pixels[J].A := A;
- Pixels[J].R := R;
- Pixels[J].G := G;
- Pixels[J].B := B;
- Pixels[J].Sum := 1;
- end;
- Inc(ImagePixel, Bitmap32.Width);
- end;
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[RecTop][X]);
- TT := RecTop;
- BB := RecTop + RadiusI;
- if BB > RecBottom then BB := RecBottom;
- ResetSumRecord(SumRec);
- // update first in col ...
- for YY := TT to BB do
- with Pixels[YY] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- MaskClr.ARGB := Mask.Pixel[X - RecLeft, 0];
- if (MaskClr.A > 0) and (SumRec.Sum > 0) then
- ImagePixel^ := DivideToColor32(SumRec);
- // update remainder in col ...
- for Y := RecTop + 1 to RecBottom do
- begin
- Inc(ImagePixel, Bitmap32.Width);
- TT := Y - RadiusI - 1;
- BB := Y + RadiusI;
- if TT >= RecTop then
- with Pixels[TT] do
- begin
- Dec(SumRec.A, A);
- Dec(SumRec.R, R);
- Dec(SumRec.G, G);
- Dec(SumRec.B, B);
- Dec(SumRec.Sum, Sum);
- end;
- if BB <= RecBottom then
- with Pixels[BB] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- MaskClr.ARGB := Mask.Pixel[X - RecLeft, Y - RecTop];
- if (SumRec.Sum = 0) or (MaskClr.A = 0) then
- // do nothing
- else if (I = Passes) then
- begin
- Clr := DivideToColor32(SumRec);
- BlendMemEx(Clr.ARGB, ImagePixel^.ARGB, MaskClr.A);
- end
- else if (MaskClr.A = 255) then
- ImagePixel^ := DivideToColor32(SumRec);
- end;
- EMMS;
- end;
- end;
- // extract alphas ...
- for Y := RecTop to RecBottom do
- begin
- ImgPixel := PColor32Entry(Bitmap32.ScanLine[Y]);
- Inc(ImgPixel, RecLeft);
- for X := RecLeft to RecRight do
- begin
- ImgPixel.R := RcTable[ImgPixel.A, ImgPixel.R];
- ImgPixel.G := RcTable[ImgPixel.A, ImgPixel.G];
- ImgPixel.B := RcTable[ImgPixel.A, ImgPixel.B];
- Inc(ImgPixel);
- end;
- end;
- finally
- Mask.Free;
- end;
- end;
- procedure FastBlurGamma(Bitmap32: TBitmap32; Radius: TFloat);
- begin
- FastBlurGamma(Bitmap32, Radius, Bitmap32.BoundsRect);
- end;
- procedure FastBlurGamma(Bitmap32: TBitmap32; Radius: TFloat; const Bounds: TRect);
- var
- LL, RR, TT, BB, XX, YY, I, J, X, Y, RadiusI, Passes: Integer;
- RecLeft, RecTop, RecRight, RecBottom: Integer;
- ImagePixel: PColor32Entry;
- SumRec: TSumRecord;
- ImgPixel: PColor32Entry;
- Pixels: array of TColor32Entry;
- begin
- if Radius < 1 then
- Exit
- else if Radius > 256 then
- Radius := 256;
- RadiusI := Round(Radius / Sqrt(-2 * Ln(COne255th)));
- if RadiusI < 2 then
- begin
- Passes := Round(Radius);
- RadiusI := 1;
- end else
- Passes := 3;
- RecLeft := Max(Bounds.Left, 0);
- RecTop := Max(Bounds.Top, 0);
- RecRight := Min(Bounds.Right, Bitmap32.Width - 1);
- RecBottom := Min(Bounds.Bottom, Bitmap32.Height - 1);
- SetLength(Pixels, Max(Bitmap32.Width, Bitmap32.Height) + 1);
- // pre-multiply alphas ...
- for Y := RecTop to RecBottom do
- begin
- ImgPixel := PColor32Entry(Bitmap32.ScanLine[Y]);
- Inc(ImgPixel, RecLeft);
- for X := RecLeft to RecRight do
- with ImgPixel^ do
- begin
- R := DivTable[GAMMA_DECODING_TABLE[R], A];
- G := DivTable[GAMMA_DECODING_TABLE[G], A];
- B := DivTable[GAMMA_DECODING_TABLE[B], A];
- Inc(ImgPixel);
- end;
- end;
- for I := 1 to Passes do
- begin
- // horizontal pass...
- for Y := RecTop to RecBottom do
- begin
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[Y][RecLeft]);
- // fill the Pixels buffer with a copy of the row's pixels ...
- MoveLongword(ImagePixel^, Pixels[RecLeft], RecRight - RecLeft + 1);
- ResetSumRecord(SumRec);
- LL := RecLeft;
- RR := RecLeft + RadiusI;
- if RR > RecRight then RR := RecRight;
- // update first in row ...
- for XX := LL to RR do
- with Pixels[XX] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum);
- end;
- ImagePixel^ := DivideToColor32(SumRec);
- // update the remaining pixels in the row ...
- for X := RecLeft + 1 to RecRight do
- begin
- Inc(ImagePixel);
- LL := X - RadiusI - 1;
- RR := X + RadiusI;
- if LL >= RecLeft then
- with Pixels[LL] do
- begin
- Dec(SumRec.A, A);
- Dec(SumRec.R, R);
- Dec(SumRec.G, G);
- Dec(SumRec.B, B);
- Dec(SumRec.Sum);
- end;
- if RR <= RecRight then
- with Pixels[RR] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum);
- end;
- ImagePixel^ := DivideToColor32(SumRec);
- end;
- end;
- // vertical pass...
- for X := RecLeft to RecRight do
- begin
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[RecTop][X]);
- for J := RecTop to RecBottom do
- begin
- Pixels[J] := ImagePixel^;
- Inc(ImagePixel, Bitmap32.Width);
- end;
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[RecTop][X]);
- TT := RecTop;
- BB := RecTop + RadiusI;
- if BB > RecBottom then BB := RecBottom;
- ResetSumRecord(SumRec);
- // update first in col ...
- for YY := TT to BB do
- with Pixels[YY] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum);
- end;
- ImagePixel^ := DivideToColor32(SumRec);
- // update remainder in col ...
- for Y := RecTop + 1 to RecBottom do
- begin
- Inc(ImagePixel, Bitmap32.Width);
- TT := Y - RadiusI - 1;
- BB := Y + RadiusI;
- if TT >= RecTop then
- with Pixels[TT] do
- begin
- Dec(SumRec.A, A);
- Dec(SumRec.R, R);
- Dec(SumRec.G, G);
- Dec(SumRec.B, B);
- Dec(SumRec.Sum);
- end;
- if BB <= RecBottom then
- with Pixels[BB] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum);
- end;
- ImagePixel^ := DivideToColor32(SumRec);
- end;
- end;
- end;
- // extract alphas ...
- for Y := RecTop to RecBottom do
- begin
- ImgPixel := PColor32Entry(@Bitmap32.ScanLine[Y][RecLeft]);
- for X := RecLeft to RecRight do
- begin
- ImgPixel.R := GAMMA_ENCODING_TABLE[RcTable[ImgPixel.A, ImgPixel.R]];
- ImgPixel.G := GAMMA_ENCODING_TABLE[RcTable[ImgPixel.A, ImgPixel.G]];
- ImgPixel.B := GAMMA_ENCODING_TABLE[RcTable[ImgPixel.A, ImgPixel.B]];
- Inc(ImgPixel);
- end;
- end;
- end;
- procedure FastBlurGamma(Bitmap32: TBitmap32; Radius: TFloat; const BlurRegion: TArrayOfFloatPoint);
- var
- LL, RR, TT, BB, XX, YY, I, J, X, Y, RadiusI, Passes: Integer;
- RecLeft, RecTop, RecRight, RecBottom: Integer;
- ImagePixel: PColor32Entry;
- SumRec: TSumRecord;
- ImgPixel: PColor32Entry;
- Pixels: array of TSumRecord;
- Mask: TBitmap32;
- Clr, MaskClr: TColor32Entry;
- Pts: TArrayOfFloatPoint;
- Bounds: TRect;
- begin
- if Radius < 1 then
- Exit
- else if Radius > 256 then
- Radius := 256;
- RadiusI := Round(Radius / Sqrt(-2 * Ln(COne255th)));
- if RadiusI < 2 then
- begin
- Passes := Round(Radius);
- RadiusI := 1;
- end else
- Passes := 3;
- with PolygonBounds(BlurRegion) do
- Bounds := Rect(Floor(Left), Floor(Top), Ceil(Right), Ceil(Bottom));
- if Bounds.Left < 0 then Bounds.Left := 0;
- if Bounds.Top < 0 then Bounds.Top := 0;
- if Bounds.Right >= Bitmap32.Width then Bounds.Right := Bitmap32.Width - 1;
- if Bounds.Bottom >= Bitmap32.Height then Bounds.Bottom := Bitmap32.Height - 1;
- RecLeft := Max(Bounds.Left, 0);
- RecTop := Max(Bounds.Top, 0);
- RecRight := Min(Bounds.Right, Bitmap32.Width - 1);
- RecBottom := Min(Bounds.Bottom, Bitmap32.Height - 1);
- SetLength(Pixels, Max(Bitmap32.Width, Bitmap32.Height) + 1);
- // pre-multiply alphas ...
- for Y := RecTop to RecBottom do
- begin
- ImgPixel := PColor32Entry(Bitmap32.ScanLine[Y]);
- Inc(ImgPixel, RecLeft);
- for X := RecLeft to RecRight do
- begin
- ImgPixel.R := DivTable[GAMMA_DECODING_TABLE[ImgPixel.R], ImgPixel.A];
- ImgPixel.G := DivTable[GAMMA_DECODING_TABLE[ImgPixel.G], ImgPixel.A];
- ImgPixel.B := DivTable[GAMMA_DECODING_TABLE[ImgPixel.B], ImgPixel.A];
- Inc(ImgPixel);
- end;
- end;
- Mask := TBitmap32.Create;
- try
- Mask.SetSize(Bounds.Right - Bounds.Left + 1, Bounds.Bottom - Bounds.Top + 1);
- SetLength(Pts, Length(BlurRegion));
- for I := 0 to High(BlurRegion) do
- begin
- Pts[I].X := BlurRegion[I].X - Bounds.Left;
- Pts[I].Y := BlurRegion[I].Y - Bounds.Top;
- end;
- PolygonFS(Mask, Pts, clWhite32);
- for I := 1 to Passes do
- begin
- // horizontal pass...
- for Y := RecTop to RecBottom do
- begin
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[Y][RecLeft]);
- // fill the Pixels buffer with a copy of the row's pixels ...
- for J := RecLeft to RecRight do
- begin
- MaskClr.ARGB := Mask.Pixel[J - RecLeft, Y - RecTop];
- if (MaskClr.A = 0) then
- begin
- Pixels[J].A := 0;
- Pixels[J].R := 0;
- Pixels[J].G := 0;
- Pixels[J].B := 0;
- Pixels[J].Sum := 0;
- end else
- with ImagePixel^ do
- begin
- Pixels[J].A := A;
- Pixels[J].R := R;
- Pixels[J].G := G;
- Pixels[J].B := B;
- Pixels[J].Sum := 1;
- end;
- Inc(ImagePixel);
- end;
- LL := RecLeft;
- RR := RecLeft + RadiusI;
- if RR > RecRight then RR := RecRight;
- ResetSumRecord(SumRec);
- // update first in row ...
- for XX := LL to RR do
- with Pixels[XX] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[Y][RecLeft]);
- MaskClr.ARGB := Mask.Pixel[0, Y - RecTop];
- if (MaskClr.A > 0) and (SumRec.Sum > 0) then
- ImagePixel^ := DivideToColor32(SumRec);
- // update the remaining pixels in the row ...
- for X := RecLeft + 1 to RecRight do
- begin
- Inc(ImagePixel);
- LL := X - RadiusI - 1;
- RR := X + RadiusI;
- if LL >= RecLeft then
- with Pixels[LL] do
- begin
- Dec(SumRec.A, A);
- Dec(SumRec.R, R);
- Dec(SumRec.G, G);
- Dec(SumRec.B, B);
- Dec(SumRec.Sum, Sum);
- end;
- if RR <= RecRight then
- with Pixels[RR] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- MaskClr.ARGB := Mask.Pixel[X - RecLeft, Y - RecTop];
- if (SumRec.Sum > 0) and (MaskClr.A = 255) then
- ImagePixel^ := DivideToColor32(SumRec);
- end;
- end;
- // vertical pass...
- for X := RecLeft to RecRight do
- begin
- // fill the Pixels buffer with a copy of the col's pixels ...
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[RecTop][X]);
- for J := RecTop to RecBottom do
- begin
- MaskClr.ARGB := Mask.Pixel[X - RecLeft, J - RecTop];
- if (MaskClr.A = 0) then
- begin
- Pixels[J].A := 0;
- Pixels[J].R := 0;
- Pixels[J].G := 0;
- Pixels[J].B := 0;
- Pixels[J].Sum := 0;
- end else
- with ImagePixel^ do
- begin
- Pixels[J].A := A;
- Pixels[J].R := R;
- Pixels[J].G := G;
- Pixels[J].B := B;
- Pixels[J].Sum := 1;
- end;
- Inc(ImagePixel, Bitmap32.Width);
- end;
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[RecTop][X]);
- TT := RecTop;
- BB := RecTop + RadiusI;
- if BB > RecBottom then BB := RecBottom;
- ResetSumRecord(SumRec);
- // update first in col ...
- for YY := TT to BB do
- with Pixels[YY] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- MaskClr.ARGB := Mask.Pixel[X - RecLeft, 0];
- if (MaskClr.A > 0) and (SumRec.Sum > 0) then
- ImagePixel^ := DivideToColor32(SumRec);
- // update remainder in col ...
- for Y := RecTop + 1 to RecBottom do
- begin
- Inc(ImagePixel, Bitmap32.Width);
- TT := Y - RadiusI - 1;
- BB := Y + RadiusI;
- if TT >= RecTop then
- with Pixels[TT] do
- begin
- Dec(SumRec.A, A);
- Dec(SumRec.R, R);
- Dec(SumRec.G, G);
- Dec(SumRec.B, B);
- Dec(SumRec.Sum, Sum);
- end;
- if BB <= RecBottom then
- with Pixels[BB] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- MaskClr.ARGB := Mask.Pixel[X - RecLeft, Y - RecTop];
- if (SumRec.Sum = 0) or (MaskClr.A = 0) then
- // do nothing
- else if (I = Passes) then
- begin
- Clr := DivideToColor32(SumRec);
- BlendMemEx(Clr.ARGB, ImagePixel^.ARGB, MaskClr.A);
- end
- else if (MaskClr.A = 255) then
- ImagePixel^ := DivideToColor32(SumRec);
- end;
- EMMS;
- end;
- end;
- // extract alphas ...
- for Y := RecTop to RecBottom do
- begin
- ImgPixel := PColor32Entry(Bitmap32.ScanLine[Y]);
- Inc(ImgPixel, RecLeft);
- for X := RecLeft to RecRight do
- begin
- ImgPixel.R := GAMMA_ENCODING_TABLE[RcTable[ImgPixel.A, ImgPixel.R]];
- ImgPixel.G := GAMMA_ENCODING_TABLE[RcTable[ImgPixel.A, ImgPixel.G]];
- ImgPixel.B := GAMMA_ENCODING_TABLE[RcTable[ImgPixel.A, ImgPixel.B]];
- Inc(ImgPixel);
- end;
- end;
- finally
- Mask.Free;
- end;
- end;
- { MotionBlur }
- procedure MotionBlur(Bitmap32: TBitmap32;
- Dist, AngleDeg: TFloat; Bidirectional: Boolean = True);
- var
- Pts: TArrayOfFloatPoint;
- begin
- SetLength(Pts, 4);
- with Bitmap32.BoundsRect do
- begin
- Pts[0] := FloatPoint(Left, Top);
- Pts[1] := FloatPoint(Right, Top);
- Pts[2] := FloatPoint(Right, Bottom);
- Pts[3] := FloatPoint(Left, Bottom);
- end;
- MotionBlur(Bitmap32, Dist, AngleDeg, Pts, Bidirectional);
- end;
- procedure MotionBlur(Bitmap32: TBitmap32; Dist, AngleDeg: TFloat;
- const Bounds: TRect; Bidirectional: Boolean = True);
- var
- Pts: TArrayOfFloatPoint;
- begin
- SetLength(Pts, 4);
- with Bounds do
- begin
- Pts[0] := FloatPoint(Left, Top);
- Pts[1] := FloatPoint(Right, Top);
- Pts[2] := FloatPoint(Right, Bottom);
- Pts[3] := FloatPoint(Left, Bottom);
- end;
- MotionBlur(Bitmap32, Dist, AngleDeg, Pts, Bidirectional);
- end;
- procedure MotionBlur(Bitmap32: TBitmap32; Dist, AngleDeg: TFloat;
- const BlurRegion: TArrayOfFloatPoint; Bidirectional: Boolean = True);
- var
- LL, RR, XX, I, X, Y, RadiusI, Passes: Integer;
- ImagePixel, ImagePixel2, ImagePixel3: PColor32Entry;
- ImagePixels, ImagePixels2: PColor32EntryArray;
- SumRec: TSumRecord;
- Pixels: array of TSumRecord;
- Mask: TBitmap32;
- Clr, MaskClr: TColor32Entry;
- Pts: TArrayOfFloatPoint;
- Bounds: TRect;
- Dx, Dy: Double;
- Affine: TAffineTransformation;
- BmpCutout: TBitmap32;
- BmpRotated: TBitmap32;
- PrevIsBlank, ThisIsBlank: boolean;
- begin
- if Dist < 1 then
- Exit
- else if Dist > 256 then
- Dist := 256;
- RadiusI := Round(Sqrt(-Dist * Dist / (2 * Ln(COne255th))));
- if RadiusI < 2 then
- begin
- Passes := Round(Dist);
- RadiusI := 1;
- end else
- Passes := 3;
- with PolygonBounds(BlurRegion) do
- Bounds := Rect(Floor(Left), Floor(Top), Ceil(Right), Ceil(Bottom));
- Bounds.Left := Max(Bounds.Left, 0);
- Bounds.Top := Max(Bounds.Top, 0);
- Bounds.Right := Min(Bounds.Right, Bitmap32.Width - 1);
- Bounds.Bottom := Min(Bounds.Bottom, Bitmap32.Height - 1);
- Affine := TAffineTransformation.Create;
- BmpCutout := TBitmap32.Create;
- BmpRotated := TBitmap32.Create;
- BmpRotated.Resampler := TLinearResampler.Create(BmpRotated);
- Mask := TBitmap32.Create;
- try
- // copy the region to be blurred into the BmpCutout image buffer ...
- BmpCutout.SetSize(Bounds.Right - Bounds.Left, Bounds.Bottom - Bounds.Top);
- for Y := 0 to BmpCutout.Height - 1 do
- begin
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[Y + Bounds.Top][Bounds.Left]);
- ImagePixel2 := PColor32Entry(BmpCutout.ScanLine[Y]);
- MoveLongword(ImagePixel^, ImagePixel2^, BmpCutout.Width);
- end;
- // pre-multiply alphas in BmpCutout ...
- for Y := 0 to BmpCutout.Height - 1 do
- begin
- ImagePixel := PColor32Entry(BmpCutout.ScanLine[Y]);
- for X := 0 to BmpCutout.Width - 1 do
- begin
- ImagePixel.R := DivTable[ImagePixel.R, ImagePixel.A];
- ImagePixel.G := DivTable[ImagePixel.G, ImagePixel.A];
- ImagePixel.B := DivTable[ImagePixel.B, ImagePixel.A];
- Inc(ImagePixel);
- end;
- end;
- // Rotate BmpCutout into BmpRotated ...
- Affine.SrcRect := FloatRect(BmpCutout.BoundsRect);
- Affine.Rotate(180 - AngleDeg);
- with Affine.GetTransformedBounds do
- begin
- Mask.SetSize(Round(Right - Left) + 1, Round(Bottom - Top) + 1);
- BmpRotated.SetSize(Mask.Width, Mask.Height);
- Dx := Left; Dy := Top;
- Affine.Translate(-Dx, -Dy);
- end;
- Transform(BmpRotated, BmpCutout, Affine);
- // Create a rotated mask ...
- Affine.Clear;
- Affine.Translate(-Bounds.Left, -Bounds.Top);
- Affine.SrcRect := FloatRect(BmpCutout.BoundsRect);
- Affine.Rotate(180 - AngleDeg);
- Affine.Translate(-Dx, -Dy);
- Pts := TransformPolygon(BlurRegion, Affine);
- PolygonFS(Mask, Pts, clWhite32);
- SetLength(Pixels, BmpRotated.Width);
- // Now blur horizontally the rotated image ...
- for I := 1 to Passes do
- // Horizontal blur only ...
- for Y := 0 to BmpRotated.Height - 1 do
- begin
- ImagePixel := PColor32Entry(BmpRotated.ScanLine[Y]);
- // fill the Pixels buffer with a copy of the row's pixels ...
- for X := 0 to BmpRotated.Width - 1 do
- begin
- MaskClr.ARGB := Mask.Pixel[X, Y];
- if (MaskClr.A = 0) then
- begin
- Pixels[X].A := 0;
- Pixels[X].R := 0;
- Pixels[X].G := 0;
- Pixels[X].B := 0;
- Pixels[X].Sum := 0;
- end else
- with ImagePixel^ do
- begin
- Pixels[X].A := A;
- Pixels[X].R := R;
- Pixels[X].G := G;
- Pixels[X].B := B;
- Pixels[X].Sum := 1;
- end;
- Inc(ImagePixel);
- end;
- LL := 0;
- RR := RadiusI;
- if RR >= BmpRotated.Width then RR := BmpRotated.Width - 1;
- ResetSumRecord(SumRec);
- // update first in row ...
- for XX := LL to RR do
- with Pixels[XX] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- ImagePixel := PColor32Entry(BmpRotated.ScanLine[Y]);
- MaskClr.ARGB := Mask.Pixel[0, Y];
- if (MaskClr.A > 0) and (SumRec.Sum > 0) then
- ImagePixel^ := DivideToColor32(SumRec);
- // update the remaining pixels in the row ...
- for X := 1 to BmpRotated.Width - 1 do
- begin
- Inc(ImagePixel);
- if Bidirectional then
- LL := X - RadiusI - 1
- else
- LL := X - 1;
- RR := X + RadiusI;
- if LL >= 0 then
- with Pixels[LL] do
- begin
- Dec(SumRec.A, A);
- Dec(SumRec.R, R);
- Dec(SumRec.G, G);
- Dec(SumRec.B, B);
- Dec(SumRec.Sum, Sum);
- end;
- if RR < BmpRotated.Width then
- with Pixels[RR] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- MaskClr.ARGB := Mask.Pixel[X, Y];
- if (SumRec.Sum = 0) or (MaskClr.A = 0) then
- Continue
- else if (I = Passes) then
- begin
- Clr := DivideToColor32(SumRec);
- BlendMemEx(Clr.ARGB, ImagePixel^.ARGB, MaskClr.A);
- end
- else if (MaskClr.A = 255) then
- ImagePixel^ := DivideToColor32(SumRec);
- end;
- EMMS;
- end;
- // un-rotate the now blurred image back into BmpCutout ...
- Affine.Clear;
- Affine.SrcRect := FloatRect(BmpRotated.BoundsRect);
- Affine.Translate(Dx, Dy);
- Affine.Rotate(AngleDeg + 180);
- Transform(BmpCutout, BmpRotated, Affine);
- // extract alphas ...
- for Y := 0 to BmpCutout.Height - 1 do
- begin
- ImagePixel := PColor32Entry(BmpCutout.ScanLine[Y]);
- for X := 0 to BmpCutout.Width - 1 do
- begin
- ImagePixel.R := RcTable[ImagePixel.A, ImagePixel.R];
- ImagePixel.G := RcTable[ImagePixel.A, ImagePixel.G];
- ImagePixel.B := RcTable[ImagePixel.A, ImagePixel.B];
- Inc(ImagePixel);
- end;
- end;
- // Create an un-rotated mask and copy masked pixels from BmpCutout
- // back to the original image (Bitmap32) ...
- Mask.SetSize(BmpCutout.Width, BmpCutout.Height);
- Pts := TranslatePolygon(BlurRegion, -Bounds.Left, -Bounds.Top);
- PolygonFS(Mask, Pts, clWhite32);
- for Y := 0 to BmpCutout.Height - 1 do
- begin
- ImagePixel := PColor32Entry(BmpCutout.ScanLine[Y]);
- ImagePixel2 := PColor32Entry(Mask.ScanLine[Y]);
- ImagePixel3 := PColor32Entry(@Bitmap32.ScanLine[Y + Bounds.Top][Bounds.Left]);
- for X := 0 to BmpCutout.Width - 1 do
- begin
- if ImagePixel2.A > 0 then
- ImagePixel3.ARGB := ImagePixel.ARGB;
- Inc(ImagePixel);
- Inc(ImagePixel2);
- Inc(ImagePixel3);
- end;
- end;
- finally
- Affine.Free;
- BmpCutout.Free;
- BmpRotated.Free;
- Mask.Free;
- end;
- end;
- procedure MotionBlurGamma(Bitmap32: TBitmap32;
- Dist, AngleDeg: TFloat; Bidirectional: Boolean = True);
- var
- Pts: TArrayOfFloatPoint;
- begin
- SetLength(Pts, 4);
- with Bitmap32.BoundsRect do
- begin
- Pts[0] := FloatPoint(Left, Top);
- Pts[1] := FloatPoint(Right, Top);
- Pts[2] := FloatPoint(Right, Bottom);
- Pts[3] := FloatPoint(Left, Bottom);
- end;
- MotionBlurGamma(Bitmap32, Dist, AngleDeg, Pts, Bidirectional);
- end;
- procedure MotionBlurGamma(Bitmap32: TBitmap32; Dist, AngleDeg: TFloat;
- const Bounds: TRect; Bidirectional: Boolean = True);
- var
- Pts: TArrayOfFloatPoint;
- begin
- SetLength(Pts, 4);
- with Bounds do
- begin
- Pts[0] := FloatPoint(Left, Top);
- Pts[1] := FloatPoint(Right, Top);
- Pts[2] := FloatPoint(Right, Bottom);
- Pts[3] := FloatPoint(Left, Bottom);
- end;
- MotionBlurGamma(Bitmap32, Dist, AngleDeg, Pts, Bidirectional);
- end;
- procedure MotionBlurGamma(Bitmap32: TBitmap32; Dist, AngleDeg: TFloat;
- const BlurRegion: TArrayOfFloatPoint; Bidirectional: Boolean = True);
- var
- LL, RR, XX, I, X, Y, RadiusI, Passes: Integer;
- ImagePixel, ImagePixel2, ImagePixel3: PColor32Entry;
- ImagePixels, ImagePixels2: PColor32EntryArray;
- SumRec: TSumRecord;
- Pixels: array of TSumRecord;
- Mask: TBitmap32;
- Clr, MaskClr: TColor32Entry;
- Pts: TArrayOfFloatPoint;
- Bounds: TRect;
- Dx, Dy: Double;
- Affine: TAffineTransformation;
- BmpCutout: TBitmap32;
- BmpRotated: TBitmap32;
- PrevIsBlank, ThisIsBlank: boolean;
- begin
- if Dist < 1 then
- Exit
- else if Dist > 256 then
- Dist := 256;
- RadiusI := Round(Sqrt(-Dist * Dist / (2 * Ln(COne255th))));
- if RadiusI < 2 then
- begin
- Passes := Round(Dist);
- RadiusI := 1;
- end else
- Passes := 3;
- with PolygonBounds(BlurRegion) do
- Bounds := Rect(Floor(Left), Floor(Top), Ceil(Right), Ceil(Bottom));
- Bounds.Left := Max(Bounds.Left, 0);
- Bounds.Top := Max(Bounds.Top, 0);
- Bounds.Right := Min(Bounds.Right, Bitmap32.Width - 1);
- Bounds.Bottom := Min(Bounds.Bottom, Bitmap32.Height - 1);
- Affine := TAffineTransformation.Create;
- BmpCutout := TBitmap32.Create;
- BmpRotated := TBitmap32.Create;
- BmpRotated.Resampler := TLinearResampler.Create(BmpRotated);
- Mask := TBitmap32.Create;
- try
- // copy the region to be blurred into the BmpCutout image buffer ...
- BmpCutout.SetSize(Bounds.Right - Bounds.Left, Bounds.Bottom - Bounds.Top);
- for Y := 0 to BmpCutout.Height - 1 do
- begin
- ImagePixel := PColor32Entry(@Bitmap32.ScanLine[Y + Bounds.Top][Bounds.Left]);
- ImagePixel2 := PColor32Entry(BmpCutout.ScanLine[Y]);
- MoveLongword(ImagePixel^, ImagePixel2^, BmpCutout.Width);
- end;
- // pre-multiply alphas in BmpCutout ...
- for Y := 0 to BmpCutout.Height - 1 do
- begin
- ImagePixel := PColor32Entry(BmpCutout.ScanLine[Y]);
- for X := 0 to BmpCutout.Width - 1 do
- begin
- ImagePixel.R := DivTable[GAMMA_DECODING_TABLE[ImagePixel.R], ImagePixel.A];
- ImagePixel.G := DivTable[GAMMA_DECODING_TABLE[ImagePixel.G], ImagePixel.A];
- ImagePixel.B := DivTable[GAMMA_DECODING_TABLE[ImagePixel.B], ImagePixel.A];
- Inc(ImagePixel);
- end;
- end;
- // Rotate BmpCutout into BmpRotated ...
- Affine.SrcRect := FloatRect(BmpCutout.BoundsRect);
- Affine.Rotate(180 - AngleDeg);
- with Affine.GetTransformedBounds do
- begin
- Mask.SetSize(Round(Right - Left) + 1, Round(Bottom - Top) + 1);
- BmpRotated.SetSize(Mask.Width, Mask.Height);
- Dx := Left; Dy := Top;
- Affine.Translate(-Dx, -Dy);
- end;
- Transform(BmpRotated, BmpCutout, Affine);
- // Create a rotated mask ...
- Affine.Clear;
- Affine.Translate(-Bounds.Left, -Bounds.Top);
- Affine.SrcRect := FloatRect(BmpCutout.BoundsRect);
- Affine.Rotate(180 - AngleDeg);
- Affine.Translate(-Dx, -Dy);
- Pts := TransformPolygon(BlurRegion, Affine);
- PolygonFS(Mask, Pts, clWhite32);
- SetLength(Pixels, BmpRotated.Width);
- // Now blur horizontally the rotated image ...
- for I := 1 to Passes do
- // Horizontal blur only ...
- for Y := 0 to BmpRotated.Height - 1 do
- begin
- ImagePixel := PColor32Entry(BmpRotated.ScanLine[Y]);
- // fill the Pixels buffer with a copy of the row's pixels ...
- for X := 0 to BmpRotated.Width - 1 do
- begin
- MaskClr.ARGB := Mask.Pixel[X, Y];
- if (MaskClr.A = 0) then
- begin
- Pixels[X].A := 0;
- Pixels[X].R := 0;
- Pixels[X].G := 0;
- Pixels[X].B := 0;
- Pixels[X].Sum := 0;
- end else
- with ImagePixel^ do
- begin
- Pixels[X].A := A;
- Pixels[X].R := R;
- Pixels[X].G := G;
- Pixels[X].B := B;
- Pixels[X].Sum := 1;
- end;
- Inc(ImagePixel);
- end;
- LL := 0;
- RR := RadiusI;
- if RR >= BmpRotated.Width then RR := BmpRotated.Width - 1;
- ResetSumRecord(SumRec);
- // update first in row ...
- for XX := LL to RR do
- with Pixels[XX] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- ImagePixel := PColor32Entry(BmpRotated.ScanLine[Y]);
- MaskClr.ARGB := Mask.Pixel[0, Y];
- if (MaskClr.A > 0) and (SumRec.Sum > 0) then
- ImagePixel^ := DivideToColor32(SumRec);
- // update the remaining pixels in the row ...
- for X := 1 to BmpRotated.Width - 1 do
- begin
- Inc(ImagePixel);
- if Bidirectional then
- LL := X - RadiusI - 1
- else
- LL := X - 1;
- RR := X + RadiusI;
- if LL >= 0 then
- with Pixels[LL] do
- begin
- Dec(SumRec.A, A);
- Dec(SumRec.R, R);
- Dec(SumRec.G, G);
- Dec(SumRec.B, B);
- Dec(SumRec.Sum, Sum);
- end;
- if RR < BmpRotated.Width then
- with Pixels[RR] do
- begin
- Inc(SumRec.A, A);
- Inc(SumRec.R, R);
- Inc(SumRec.G, G);
- Inc(SumRec.B, B);
- Inc(SumRec.Sum, Sum);
- end;
- MaskClr.ARGB := Mask.Pixel[X, Y];
- if (SumRec.Sum = 0) or (MaskClr.A = 0) then
- Continue
- else if (I = Passes) then
- begin
- Clr := DivideToColor32(SumRec);
- BlendMemEx(Clr.ARGB, ImagePixel^.ARGB, MaskClr.A);
- end
- else if (MaskClr.A = 255) then
- ImagePixel^ := DivideToColor32(SumRec);
- end;
- EMMS;
- end;
- // un-rotate the now blurred image back into BmpCutout ...
- Affine.Clear;
- Affine.SrcRect := FloatRect(BmpRotated.BoundsRect);
- Affine.Translate(Dx, Dy);
- Affine.Rotate(AngleDeg + 180);
- Transform(BmpCutout, BmpRotated, Affine);
- // extract alphas ...
- for Y := 0 to BmpCutout.Height - 1 do
- begin
- ImagePixel := PColor32Entry(BmpCutout.ScanLine[Y]);
- for X := 0 to BmpCutout.Width - 1 do
- begin
- ImagePixel.R := GAMMA_ENCODING_TABLE[RcTable[ImagePixel.A, ImagePixel.R]];
- ImagePixel.G := GAMMA_ENCODING_TABLE[RcTable[ImagePixel.A, ImagePixel.G]];
- ImagePixel.B := GAMMA_ENCODING_TABLE[RcTable[ImagePixel.A, ImagePixel.B]];
- Inc(ImagePixel);
- end;
- end;
- // Create an un-rotated mask and copy masked pixels from BmpCutout
- // back to the original image (Bitmap32) ...
- Mask.SetSize(BmpCutout.Width, BmpCutout.Height);
- Pts := TranslatePolygon(BlurRegion, -Bounds.Left, -Bounds.Top);
- PolygonFS(Mask, Pts, clWhite32);
- for Y := 0 to BmpCutout.Height - 1 do
- begin
- ImagePixel := PColor32Entry(BmpCutout.ScanLine[Y]);
- ImagePixel2 := PColor32Entry(Mask.ScanLine[Y]);
- ImagePixel3 := PColor32Entry(@Bitmap32.ScanLine[Y + Bounds.Top][Bounds.Left]);
- for X := 0 to BmpCutout.Width - 1 do
- begin
- if ImagePixel2.A > 0 then
- ImagePixel3.ARGB := ImagePixel.ARGB;
- Inc(ImagePixel);
- Inc(ImagePixel2);
- Inc(ImagePixel3);
- end;
- end;
- finally
- Affine.Free;
- BmpCutout.Free;
- BmpRotated.Free;
- Mask.Free;
- end;
- end;
- end.
|