1 unit Unit1;
2 
3 {$mode objfpc}{$H+}
4 
5 interface
6 
7 uses
8   Classes, SysUtils, FileUtil, strings, Forms, Controls, Graphics, Dialogs, StdCtrls,
9   BGRABitmap, BGRABitmapTypes, BGRAVirtualScreen, dynlibs;
10 
11 const
12   {$IFDEF WINDOWS}
13   LIBRARYEXT = '*.dll';
14   {$ENDIF}
15   {$IFDEF LINUX}
16   LIBRARYEXT = '*.so';
17   {$ENDIF}
18   {$IFDEF DARWIN}
19   LIBRARYEXT = '*.dylib';
20   {$ENDIF}
21 
22 type
23   TFilterName = procedure(s: PChar); cdecl;
24   TApplyFilter = procedure(BGRA: TBGRABitmap); cdecl;
25 
26 type
27 
28   { TForm1 }
29 
30   TForm1 = class(TForm)
31     BGRAVirtualScreen1: TBGRAVirtualScreen;
32     ComboBox1: TComboBox;
33     procedure BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
34     procedure ComboBox1Select(Sender: TObject);
35     procedure FormCreate(Sender: TObject);
36     procedure FormDestroy(Sender: TObject);
37   private
38     { private declarations }
39     DLLnames: TStringList;
40     CurrentFilter: TApplyFilter;
41     dll: TLibHandle;
42   public
43     { public declarations }
44   end;
45 
46 var
47   Form1: TForm1;
48 
49 implementation
50 
51 {$R *.lfm}
52 
53 { TForm1 }
54 
55 procedure TForm1.BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
56 begin
57   Bitmap.Fill(BGRA(255, 0, 0));
58   if Assigned(CurrentFilter) then
59     CurrentFilter(Bitmap);
60 end;
61 
62 procedure TForm1.ComboBox1Select(Sender: TObject);
63 begin
64   if dll <> dynlibs.NilHandle then
65     if FreeLibrary(dll) then
66       dll := dynlibs.NilHandle;
67   dll := LoadLibrary(DLLnames[ComboBox1.ItemIndex]);
68   if dll <> dynlibs.NilHandle then
69   begin
70     CurrentFilter := TApplyFilter(GetProcAddress(dll, 'ApplyFilter'));
71   end;
72   BGRAVirtualScreen1.DiscardBitmap;
73 end;
74 
75 procedure TForm1.FormCreate(Sender: TObject);
76 var
77   i: integer;
78   GetName: TFilterName;
79   s: PChar;
80 begin
81   DLLnames := FindAllFiles(ProgramDirectory, LIBRARYEXT, False);
82   for i := 0 to DLLnames.Count - 1 do
83   begin
84     DLLnames[i] := ExtractFileName(DLLnames[i]);
85   end;
86 
87   s := stralloc(50);
88   for i := 0 to DLLnames.Count - 1 do
89   begin
90     dll := LoadLibrary(DLLnames[i]);
91     if dll <> dynlibs.NilHandle then
92     begin
93       GetName := TFilterName(GetProcAddress(dll, 'FilterName'));
94       if Assigned(GetName) then
95       begin
96         GetName(s);
97         ComboBox1.Items.Add(string(s));
98       end;
99       if FreeLibrary(dll) then
100         dll := dynlibs.NilHandle;
101     end;
102   end;
103   strdispose(s);
104 end;
105 
106 procedure TForm1.FormDestroy(Sender: TObject);
107 begin
108   DLLnames.Free;
109 end;
110 
111 end.
112 
113