Help

About FilterFX script

FilterFX script is a powerful scripting language to manipulate content (pixels) of an image. You can create, remove and change pixels from an image to whatever you want. It is based on the Pascal programming language and thus easy to learn and to understand. See http://www.pascal-programming.info for more about programming in Pascal.

A close look at a FilterFX script

The script
This FilterFX script applies a sharpen convolution to the luminance channel of an image. This script was originally created by Dan Ritchie and adjusted to be compatible with FilterFX.
// The variables declaration
var
  Kernel: array[0..2,0..2] of Integer;
  Divisor: Integer;
  Offset: Integer;

// The convolution filter function function Filter(const X,Y: Integer): Single; var i,j: Integer; Pixel: TPixelHSV; Sum: Single; begin Sum := 0.0; for i := -1 to 1 do begin for j := -1 to 1 do begin Pixel := Color.RGBAToHSV(FFX.Src.GetPixelRGBA(X+i,Y+j)); Sum := Sum + (Kernel[J+1][I+1] * Pixel.V); end; end; Result := (Sum / Divisor) + Offset; end;

// Script initialization before execution procedure FFXPrepare; begin Kernel[0,0] := -1; Kernel[0,1] := -1; Kernel[0,2] := -1; Kernel[1,0] := -1; Kernel[1,1] := 12; Kernel[1,2] := -1; Kernel[2,0] := -1; Kernel[2,1] := -1; Kernel[2,2] := -1; Divisor := 4; Offset := 0; end;

// Script execution procedure FFXApply(const Bounds: TBounds); var X,Y: Integer; Pixel: TPixelRGBA; HSV: TPixelHSV; begin for Y := Bounds.Y1+1 to Bounds.Y2-2 do begin for X := Bounds.X1+1 to Bounds.X2-2 do begin Pixel := FFX.Src.GetPixelRGBA(X,Y); if Pixel.A > 0 then begin HSV := Color.RGBAToHSV(Pixel); HSV.V := Filter(X,Y); FFX.Dst.SetPixelRGBA(X,Y,Color.HSVToRGBA(HSV)); end; end; end; end;

begin end.

The variables declaration
var
  Kernel: array[0..2,0..2] of Integer;
  Divisor: Integer;
  Offset: Integer;
Kernel is a two-dimensional array, which holds the 3x3-convolution matrix, which later is applied to the image and sharpens it. The other variables are also belong to the convolution processing. See http://en.wikipedia.org/wiki/Kernel_(image_processing) for more details about it.

The convolution filter function
function Filter(const X,Y: Integer): Single;
var
  i,j: Integer;
  Pixel: TPixelHSV;
  Sum: Single;
begin
  Sum := 0.0;
  for i := -1 to 1 do
  begin
    for j := -1 to 1 do
    begin
      Pixel := Color.RGBAToHSV(FFX.Src.GetPixelRGBA(X+i,Y+j));
      Sum := Sum + (Kernel[J+1][I+1] * Pixel.V);
    end;
  end;  
  Result := (Sum / Divisor) + Offset;
end;
This function applies the convolution matrix to the value channel of a pixel in the HSV color space at the specified position. For that, it fetches all surrounding pixel in the HSV color space of the source pixel, calculates the result pixel HSV value from them and returns it.

Script initialization before execution
procedure FFXPrepare;
begin
  Kernel[0,0] := -1;
  Kernel[0,1] := -1;
  Kernel[0,2] := -1;  
  Kernel[1,0] := -1;
  Kernel[1,1] :=  12;
  Kernel[1,2] := -1;  
  Kernel[2,0] := -1;
  Kernel[2,1] := -1;
  Kernel[2,2] := -1; 
  Divisor := 4;
  Offset := 0;
end;
This method is executed as first when the script is applied on an image. It initializes all variables with the needed values. Where Kernel is initialized with a general sharpen matrix.
FFXPrepare is one of three defined entry points, which are executed before, while, and after applying a FilterFX script on an image.

Script execution
procedure FFXApply(const Bounds: TBounds);
var
  X,Y: Integer;
  Pixel: TPixelRGBA;
  HSV: TPixelHSV;
begin
  for Y := Bounds.Y1+1 to Bounds.Y2-2 do
  begin   
    for X := Bounds.X1+1 to Bounds.X2-2 do
    begin
      Pixel := FFX.Src.GetPixelRGBA(X,Y);
      if Pixel.A > 0 then
      begin
        HSV := Color.RGBAToHSV(Pixel);
        HSV.V := Filter(X,Y);
        FFX.Dst.SetPixelRGBA(X,Y,Color.HSVToRGBA(HSV));
      end;
    end;
  end;
end;
This method is the main entry point of a FilterFX script where all the image data is manipulated. It loops through all pixels within the specified bounds of the image and applies the convolution matrix on it. As the source pixels are all in the RGB color space, they are converted first to the HSV color space to sharpen the value channel and afterwards converted back to the RGB color space to update the destination pixel.
Please note that the result of your image manipulations needs to bet set to the Dst pixel buffer of the FFX object. The Src pixel buffer only allows read-only access.

Feedback