source: trunk/Packages/bgrabitmap/bgrafpguibitmap.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 7.5 KB
Line 
1unit BGRAfpGUIBitmap;
2
3{$mode objfpc}{$H+}
4
5interface
6
7uses
8 SysUtils, Classes, BGRAGraphics, BGRABitmapTypes, BGRADefaultBitmap,
9 BGRAFreeType, EasyLazFreeType, LazFreeTypeFontCollection,
10 BGRACanvas;
11
12type
13
14 { TBGRAfpGUIBitmap }
15
16 TBGRAfpGUIBitmap = class(TBGRADefaultBitmap)
17 private
18 FPseudoCanvas: TBGRACanvas;
19 function GetPseudoCanvas: TBGRACanvas;
20 function GetBitmapTransparent: boolean;
21 procedure SetBitmapTransparent(AValue: boolean);
22 protected
23 procedure RebuildBitmap; override;
24 function CreateDefaultFontRenderer: TBGRACustomFontRenderer; override;
25 function LoadFromRawImage(ARawImage: TRawImage; DefaultOpacity: byte;
26 AlwaysReplaceAlpha: boolean=False; {%H-}RaiseErrorOnInvalidPixelFormat: boolean
27 =True): boolean; override;
28 procedure Init; override;
29 procedure FreeData; override;
30 procedure ReallocData; override;
31 procedure FreeBitmap; override;
32 procedure NotAvailable;
33 public
34 destructor Destroy; override;
35 class procedure AddFreeTypeFontFolder(ADirectory: string; AUTF8: boolean = false);
36 class procedure AddFreeTypeFontFile(AFilename: string; AUTF8: boolean = false);
37 procedure Draw(ACanvas: TCanvas; x, y: integer; {%H-}Opaque: boolean=True); override;
38 procedure Draw(ACanvas: TCanvas; Rect: TRect; {%H-}Opaque: boolean=True); override;
39 procedure Draw(ACanvas: TGUICanvas; x, y: integer; {%H-}Opaque: boolean=True); overload;
40 procedure Draw(ACanvas: TGUICanvas; Rect: TRect; {%H-}Opaque: boolean=True); overload;
41 procedure GetImageFromCanvas({%H-}CanvasSource: TCanvas; {%H-}x, {%H-}y: integer); override; //not available
42 procedure DataDrawTransparent(ACanvas: TCanvas; Rect: TRect; AData: Pointer;
43 ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer); override;
44 procedure DataDrawOpaque(ACanvas: TCanvas; Rect: TRect; AData: Pointer;
45 ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer); override;
46 procedure TakeScreenshot({%H-}ARect: TRect); override; //not available
47 procedure TakeScreenshotOfPrimaryMonitor; override; //not available
48 procedure LoadFromDevice({%H-}DC: HDC); override; //not available
49 procedure LoadFromDevice({%H-}DC: HDC; {%H-}ARect: TRect); override; //not available
50 property BitmapTransparent: boolean read GetBitmapTransparent write SetBitmapTransparent;
51 property Canvas: TBGRACanvas read GetPseudoCanvas;
52 end;
53
54implementation
55
56{ TBGRAfpGUIBitmap }
57
58function TBGRAfpGUIBitmap.GetBitmapTransparent: boolean;
59begin
60 result := FBitmap.Transparent;
61end;
62
63function TBGRAfpGUIBitmap.GetPseudoCanvas: TBGRACanvas;
64begin
65 if FPseudoCanvas = nil then
66 begin
67 FPseudoCanvas := TBGRACanvas.Create(self);
68 FPseudoCanvas.AntialiasingMode := amOff;
69 end;
70 result := FPseudoCanvas;
71end;
72
73procedure TBGRAfpGUIBitmap.SetBitmapTransparent(AValue: boolean);
74begin
75 if FBitmap.Transparent <> AValue then
76 begin
77 FBitmap.Transparent:= AValue;
78 InvalidateBitmap;
79 end;
80end;
81
82procedure TBGRAfpGUIBitmap.RebuildBitmap;
83var pmask, pmaskline: PByte;
84 pdata: PBGRAPixel;
85 raw: TRawImage;
86 x,y,bit,masklinesize,curmaskbyte: NativeUInt;
87begin
88 if FBitmap.Transparent then
89 begin
90 raw := FBitmap.RawImage;
91 masklinesize := ((Width+31) div 32)*4;
92 pmaskline := FBitmap.RawImage.MaskData;
93 pdata := raw.ImageData;
94 for y := 0 to Height-1 do
95 begin
96 pmask:= pmaskline;
97 bit := $80;
98 curmaskbyte := 0;
99 for x := Width-1 downto 0 do
100 begin
101 if pdata^.alpha >= $80 then
102 curmaskbyte := curmaskbyte or bit;
103 bit := bit shr 1;
104 if bit = 0 then
105 begin
106 bit := $80;
107 pmask^ := curmaskbyte;
108 inc(pmask);
109 curmaskbyte := 0;
110 end;
111 inc(pdata);
112 end;
113 if bit <> $80 then
114 pmask^ := curmaskbyte;
115 inc(pmaskline, masklinesize);
116 end;
117 end;
118 FBitmap.RawImage.UpdateImage;
119end;
120
121function TBGRAfpGUIBitmap.CreateDefaultFontRenderer: TBGRACustomFontRenderer;
122begin
123 result := TBGRAFreeTypeFontRenderer.Create;
124end;
125
126function TBGRAfpGUIBitmap.LoadFromRawImage(ARawImage: TRawImage;
127 DefaultOpacity: byte; AlwaysReplaceAlpha: boolean;
128 RaiseErrorOnInvalidPixelFormat: boolean): boolean;
129var
130 lineSize: integer;
131 y: Integer;
132begin
133 if (ARawImage.Width <> Width) or
134 (ARawImage.Height <> Height) then
135 raise Exception.Create('Bitmap size is inconsistant');
136
137 lineSize := Width*sizeof(TBGRAPixel);
138 for y := 0 to Height-1 do
139 move(ARawImage.ScanLine[y]^, ScanLine[y]^, lineSize);
140 if AlwaysReplaceAlpha then AlphaFill(DefaultOpacity);
141 result := true;
142end;
143
144procedure TBGRAfpGUIBitmap.Init;
145begin
146 inherited Init;
147 FBitmap := TBitmap.Create;
148 FontAntialias:= true;
149end;
150
151procedure TBGRAfpGUIBitmap.ReallocData;
152begin
153 FBitmap.Width := Width;
154 FBitmap.Height:= Height;
155 FData := FBitmap.RawImage.ImageData;
156 InvalidateBitmap;
157 FScanPtr := nil;
158end;
159
160procedure TBGRAfpGUIBitmap.FreeData;
161begin
162 //nothing
163end;
164
165procedure TBGRAfpGUIBitmap.FreeBitmap;
166begin
167 //nothing
168end;
169
170procedure TBGRAfpGUIBitmap.NotAvailable;
171begin
172 raise exception.Create('Function not available with fpGUI');
173end;
174
175destructor TBGRAfpGUIBitmap.Destroy;
176begin
177 FreeAndNil(FBitmap);
178 FreeAndNil(FPseudoCanvas);
179 inherited Destroy;
180end;
181
182class procedure TBGRAfpGUIBitmap.AddFreeTypeFontFolder(ADirectory: string; AUTF8: boolean);
183begin
184 if AUTF8 then ADirectory:= Utf8ToAnsi(ADirectory);
185 EasyLazFreeType.FontCollection.AddFolder(ADirectory);
186end;
187
188class procedure TBGRAfpGUIBitmap.AddFreeTypeFontFile(AFilename: string; AUTF8: boolean);
189begin
190 if AUTF8 then AFilename:= Utf8ToAnsi(AFilename);
191 EasyLazFreeType.FontCollection.AddFile(AFilename);
192end;
193
194procedure TBGRAfpGUIBitmap.Draw(ACanvas: TCanvas; x, y: integer; Opaque: boolean);
195begin
196 Draw(ACanvas.GUICanvas, x, y, Opaque);
197end;
198
199procedure TBGRAfpGUIBitmap.Draw(ACanvas: TCanvas; Rect: TRect; Opaque: boolean);
200begin
201 Draw(ACanvas.GUICanvas, Rect, Opaque);
202end;
203
204procedure TBGRAfpGUIBitmap.Draw(ACanvas: TGUICanvas; x, y: integer;
205 Opaque: boolean);
206begin
207 BitmapTransparent := not Opaque;
208 ACanvas.DrawImage(x,y, Bitmap.RawImage);
209end;
210
211procedure TBGRAfpGUIBitmap.Draw(ACanvas: TGUICanvas; Rect: TRect;
212 Opaque: boolean);
213begin
214 BitmapTransparent := not Opaque;
215 ACanvas.StretchDraw(rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top, FBitmap.RawImage);
216end;
217
218procedure TBGRAfpGUIBitmap.GetImageFromCanvas(CanvasSource: TCanvas; x,
219 y: integer);
220begin
221 NotAvailable;
222end;
223
224procedure TBGRAfpGUIBitmap.DataDrawTransparent(ACanvas: TCanvas; Rect: TRect;
225 AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
226var temp: TBGRAfpGUIBitmap;
227begin
228 temp := TBGRAfpGUIBitmap.Create(AWidth,AHeight);
229 move(AData^, temp.Data^, temp.NbPixels*sizeof(TBGRAPixel));
230 if ALineOrder <> temp.LineOrder then temp.VerticalFlip;
231 temp.Draw(ACanvas, Rect, False);
232 temp.Free;
233end;
234
235procedure TBGRAfpGUIBitmap.DataDrawOpaque(ACanvas: TCanvas; Rect: TRect;
236 AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
237var temp: TBGRAfpGUIBitmap;
238begin
239 temp := TBGRAfpGUIBitmap.Create(AWidth,AHeight);
240 move(AData^, temp.Data^, temp.NbPixels*sizeof(TBGRAPixel));
241 if ALineOrder <> temp.LineOrder then temp.VerticalFlip;
242 temp.Draw(ACanvas, Rect, True);
243 temp.Free;
244end;
245
246procedure TBGRAfpGUIBitmap.TakeScreenshot(ARect: TRect);
247begin
248 NotAvailable;
249end;
250
251procedure TBGRAfpGUIBitmap.TakeScreenshotOfPrimaryMonitor;
252begin
253 NotAvailable;
254end;
255
256procedure TBGRAfpGUIBitmap.LoadFromDevice(DC: HDC);
257begin
258 NotAvailable;
259end;
260
261procedure TBGRAfpGUIBitmap.LoadFromDevice(DC: HDC; ARect: TRect);
262begin
263 NotAvailable;
264end;
265
266end.
267
Note: See TracBrowser for help on using the repository browser.