Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program new;
- {$R-}
- procedure TMufasaBitmap.GaussianBlur(Radius:Int32; Sigma:Single);
- type TFRGB = record R,G,B:Single; end;
- var
- x,y,wid,hei,xx,yy,s,offset,dia,width: Int32;
- ptr: ^TFRGB; f: TFRGB;
- cl: TRGB32;
- tmp: array of TFRGB;
- kernel: array of Single;
- function GaussKernel1D(KernelRadius:Int32; Sigma:Single): array of Single;
- var sum:Single; i,size:Int32;
- begin
- size := 2*KernelRadius;
- SetLength(Result, size+1);
- sum := 0.0;
- for i:=0 to size do begin
- Result[i] := Exp(-(Sqr((i-KernelRadius) / sigma)) / 2.0);
- sum += Result[i];
- end;
- for i:=0 to size do Result[i] /= sum;
- end;
- begin
- wid := Self.GetWidth()-1;
- hei := Self.GetHeight()-1;
- dia := radius * 2 + 1;
- width := wid + 1;
- s := (hei+1) * width;
- kernel := GaussKernel1D(radius, sigma);
- SetLength(tmp, s);
- // y direction
- offset := 0;
- repeat
- ptr := @tmp[0];
- for y:=0 to hei do
- for x:=0 to wid do
- begin
- xx := (x-radius)+offset;
- if (xx < 0) then xx := 0 else if (xx > wid) then xx := wid;
- cl := TRGB32(self.GetPixel(xx,y));
- ptr^.R += cl.R * kernel[offset];
- ptr^.G += cl.G * kernel[offset];
- ptr^.B += cl.B * kernel[offset];
- inc(ptr);
- end;
- inc(offset);
- until offset = dia;
- // x direction + result
- for y:=0 to hei do
- for x:=0 to wid do
- begin
- f.R := 0; f.G := 0; f.B := 0;
- ptr := @f;
- offset := 0;
- repeat
- yy := (y-radius)+offset;
- if (yy < 0) then yy := 0 else if (yy > hei) then yy := hei;
- ptr^.R += tmp[yy*width+x].R * kernel[offset];
- ptr^.G += tmp[yy*width+x].G * kernel[offset];
- ptr^.B += tmp[yy*width+x].B * kernel[offset];
- inc(offset);
- until offset = dia;
- Self.SetPixel(x,y, (Round(ptr^.B)) or (Round(ptr^.G) shl 8) or (Round(ptr^.R) shl 16));
- end;
- end;
- {$R+}
- var
- BMP:TMufasaBitmap;
- begin
- BMP := GetMufasaBitmap(BitmapFromClient(0,0,500,500));
- BMP.GaussianBlur(3,1.5);
- DisplayDebugImgWindow(501,501);
- DrawBitmapDebugImg(bmp.GetIndex());
- BMP.Free();
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement