Advertisement
WarPie90

GaussianBlur

Jun 6th, 2017
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.11 KB | None | 0 0
  1. program new;
  2.  
  3. {$R-}
  4. procedure TMufasaBitmap.GaussianBlur(Radius:Int32; Sigma:Single);
  5. type TFRGB = record R,G,B:Single; end;
  6. var
  7.   x,y,wid,hei,xx,yy,s,offset,dia,width: Int32;
  8.   ptr: ^TFRGB; f: TFRGB;
  9.   cl: TRGB32;
  10.   tmp: array of TFRGB;
  11.   kernel: array of Single;
  12.  
  13.   function GaussKernel1D(KernelRadius:Int32; Sigma:Single): array of Single;
  14.   var sum:Single; i,size:Int32;
  15.   begin
  16.     size := 2*KernelRadius;
  17.     SetLength(Result, size+1);
  18.     sum := 0.0;
  19.     for i:=0 to size do begin
  20.       Result[i] := Exp(-(Sqr((i-KernelRadius) / sigma)) / 2.0);
  21.       sum += Result[i];
  22.     end;
  23.     for i:=0 to size do Result[i] /= sum;
  24.   end;
  25. begin
  26.   wid := Self.GetWidth()-1;
  27.   hei := Self.GetHeight()-1;
  28.   dia := radius * 2 + 1;
  29.   width := wid + 1;
  30.   s := (hei+1) * width;
  31.  
  32.   kernel := GaussKernel1D(radius, sigma);
  33.   SetLength(tmp, s);
  34.  
  35.   // y direction
  36.   offset := 0;
  37.   repeat
  38.     ptr := @tmp[0];
  39.     for y:=0 to hei do
  40.       for x:=0 to wid do
  41.       begin
  42.         xx := (x-radius)+offset;
  43.         if (xx < 0) then xx := 0 else if (xx > wid) then xx := wid;
  44.         cl := TRGB32(self.GetPixel(xx,y));
  45.         ptr^.R += cl.R * kernel[offset];
  46.         ptr^.G += cl.G * kernel[offset];
  47.         ptr^.B += cl.B * kernel[offset];
  48.         inc(ptr);
  49.       end;
  50.     inc(offset);
  51.   until offset = dia;
  52.  
  53.   // x direction + result
  54.   for y:=0 to hei do
  55.     for x:=0 to wid do
  56.     begin
  57.       f.R := 0; f.G := 0; f.B := 0;
  58.       ptr := @f;
  59.       offset := 0;
  60.       repeat
  61.         yy := (y-radius)+offset;
  62.         if (yy < 0) then yy := 0 else if (yy > hei) then yy := hei;
  63.         ptr^.R += tmp[yy*width+x].R * kernel[offset];
  64.         ptr^.G += tmp[yy*width+x].G * kernel[offset];
  65.         ptr^.B += tmp[yy*width+x].B * kernel[offset];
  66.         inc(offset);
  67.       until offset = dia;
  68.       Self.SetPixel(x,y, (Round(ptr^.B)) or (Round(ptr^.G) shl 8) or (Round(ptr^.R) shl 16));
  69.     end;
  70. end;
  71. {$R+}
  72.  
  73. var
  74.   BMP:TMufasaBitmap;
  75. begin
  76.   BMP := GetMufasaBitmap(BitmapFromClient(0,0,500,500));
  77.   BMP.GaussianBlur(3,1.5);
  78.   DisplayDebugImgWindow(501,501);
  79.   DrawBitmapDebugImg(bmp.GetIndex());
  80.   BMP.Free();
  81. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement