-
Notifications
You must be signed in to change notification settings - Fork 12
/
bgrafpguibitmap.pas
302 lines (258 loc) · 9.5 KB
/
bgrafpguibitmap.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | mailedivando@gmail.com
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRAfpGUIBitmap;
{$i bgrabitmap.inc}{$H+}
interface
uses
SysUtils, Classes, BGRAGraphics, BGRABitmapTypes, BGRADefaultBitmap,
BGRAFreeType, {$IFDEF FPC} EasyLazFreeType, LazFreeTypeFontCollection,{$ENDIF}
BGRACanvas;
type
{ TBGRAfpGUIBitmap }
TBGRAfpGUIBitmap = class(TBGRADefaultBitmap)
private
FPseudoCanvas: TBGRACanvas;
function GetPseudoCanvas: TBGRACanvas;
function GetBitmapTransparent: boolean;
procedure SetBitmapTransparent(AValue: boolean);
protected
procedure RebuildBitmap; override;
function CreateDefaultFontRenderer: TBGRACustomFontRenderer; override;
function LoadFromRawImage(ARawImage: TRawImage; DefaultOpacity: byte;
AlwaysReplaceAlpha: boolean=False; {%H-}RaiseErrorOnInvalidPixelFormat: boolean
=True): boolean; override;
procedure Init; override;
procedure FreeData; override;
procedure ReallocData; override;
procedure FreeBitmap; override;
procedure NotAvailable;
public
destructor Destroy; override;
class procedure AddFreeTypeFontFolder(ADirectory: string; AUTF8: boolean = false);
class procedure AddFreeTypeFontFile(AFilename: string; AUTF8: boolean = false);
procedure Draw(ACanvas: TCanvas; x, y: integer; {%H-}Opaque: boolean=True); override;
procedure Draw(ACanvas: TCanvas; Rect: TRect; {%H-}Opaque: boolean=True); override;
procedure Draw(ACanvas: TGUICanvas; x, y: integer; {%H-}Opaque: boolean=True); overload;
procedure Draw(ACanvas: TGUICanvas; Rect: TRect; {%H-}Opaque: boolean=True); overload;
procedure GetImageFromCanvas({%H-}CanvasSource: TCanvas; {%H-}x, {%H-}y: integer); override; //not available
procedure DataDrawTransparent(ACanvas: TCanvas; Rect: TRect; AData: Pointer;
ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer); override;
procedure DataDrawOpaque(ACanvas: TCanvas; Rect: TRect; AData: Pointer;
ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer); override;
procedure TakeScreenshot({%H-}ARect: TRect); override; //not available
procedure TakeScreenshotOfPrimaryMonitor; override; //not available
procedure LoadFromDevice({%H-}DC: System.THandle); override; //not available
procedure LoadFromDevice({%H-}DC: System.THandle; {%H-}ARect: TRect); override; //not available
property BitmapTransparent: boolean read GetBitmapTransparent write SetBitmapTransparent;
property Canvas: TBGRACanvas read GetPseudoCanvas;
end;
implementation
{ TBGRAfpGUIBitmap }
function TBGRAfpGUIBitmap.GetBitmapTransparent: boolean;
begin
result := FBitmap.Transparent;
end;
function TBGRAfpGUIBitmap.GetPseudoCanvas: TBGRACanvas;
begin
if FPseudoCanvas = nil then
begin
FPseudoCanvas := TBGRACanvas.Create(self);
FPseudoCanvas.AntialiasingMode := amOff;
end;
result := FPseudoCanvas;
end;
procedure TBGRAfpGUIBitmap.SetBitmapTransparent(AValue: boolean);
begin
if FBitmap.Transparent <> AValue then
begin
FBitmap.Transparent:= AValue;
InvalidateBitmap;
end;
end;
procedure TBGRAfpGUIBitmap.RebuildBitmap;
var pmask, pmaskline: PByte;
pdata: PBGRAPixel;
raw: TRawImage;
x,y,bit,masklinesize,curmaskbyte: BGRANativeUInt;
begin
if FBitmap.Transparent then
begin
raw := FBitmap.RawImage;
masklinesize := ((Width+31) div 32)*4;
pmaskline := FBitmap.RawImage.MaskData;
pdata := raw.ImageData;
for y := 0 to Height-1 do
begin
pmask:= pmaskline;
bit := $80;
curmaskbyte := 0;
for x := Width-1 downto 0 do
begin
if pdata^.alpha >= $80 then
curmaskbyte := curmaskbyte or bit;
bit := bit shr 1;
if bit = 0 then
begin
bit := $80;
pmask^ := curmaskbyte;
inc(pmask);
curmaskbyte := 0;
end;
inc(pdata);
end;
if bit <> $80 then
pmask^ := curmaskbyte;
inc(pmaskline, masklinesize);
end;
end;
FBitmap.RawImage.UpdateImage;
end;
function TBGRAfpGUIBitmap.CreateDefaultFontRenderer: TBGRACustomFontRenderer;
begin
result := TBGRAFreeTypeFontRenderer.Create;
end;
function TBGRAfpGUIBitmap.LoadFromRawImage(ARawImage: TRawImage;
DefaultOpacity: byte; AlwaysReplaceAlpha: boolean;
RaiseErrorOnInvalidPixelFormat: boolean): boolean;
var
lineSize: integer;
y: Integer;
begin
if (ARawImage.Width <> Width) or
(ARawImage.Height <> Height) then
raise Exception.Create('Bitmap size is inconsistant');
lineSize := Width*sizeof(TBGRAPixel);
for y := 0 to Height-1 do
move(ARawImage.ScanLine[y]^, ScanLine[y]^, lineSize);
if AlwaysReplaceAlpha then AlphaFill(DefaultOpacity);
result := true;
end;
procedure TBGRAfpGUIBitmap.Init;
begin
inherited Init;
FBitmap := TBitmap.Create;
FontAntialias:= true;
end;
procedure TBGRAfpGUIBitmap.ReallocData;
begin
FBitmap.Width := Width;
FBitmap.Height:= Height;
FData := FBitmap.RawImage.ImageData;
InvalidateBitmap;
FScanPtr := nil;
end;
procedure TBGRAfpGUIBitmap.FreeData;
begin
//nothing
end;
procedure TBGRAfpGUIBitmap.FreeBitmap;
begin
//nothing
end;
procedure TBGRAfpGUIBitmap.NotAvailable;
begin
raise exception.Create('Function not available with fpGUI');
end;
destructor TBGRAfpGUIBitmap.Destroy;
begin
FreeAndNil(FBitmap);
FreeAndNil(FPseudoCanvas);
inherited Destroy;
end;
class procedure TBGRAfpGUIBitmap.AddFreeTypeFontFolder(ADirectory: string; AUTF8: boolean);
begin
if AUTF8 then ADirectory:= Utf8ToAnsi(ADirectory);
EasyLazFreeType.FontCollection.AddFolder(ADirectory);
end;
class procedure TBGRAfpGUIBitmap.AddFreeTypeFontFile(AFilename: string; AUTF8: boolean);
begin
if AUTF8 then AFilename:= Utf8ToAnsi(AFilename);
EasyLazFreeType.FontCollection.AddFile(AFilename);
end;
procedure TBGRAfpGUIBitmap.Draw(ACanvas: TCanvas; x, y: integer; Opaque: boolean);
begin
Draw(ACanvas.GUICanvas, x, y, Opaque);
end;
procedure TBGRAfpGUIBitmap.Draw(ACanvas: TCanvas; Rect: TRect; Opaque: boolean);
begin
Draw(ACanvas.GUICanvas, Rect, Opaque);
end;
procedure TBGRAfpGUIBitmap.Draw(ACanvas: TGUICanvas; x, y: integer;
Opaque: boolean);
begin
BitmapTransparent := not Opaque;
ACanvas.DrawImage(x,y, Bitmap.RawImage);
end;
procedure TBGRAfpGUIBitmap.Draw(ACanvas: TGUICanvas; Rect: TRect;
Opaque: boolean);
begin
BitmapTransparent := not Opaque;
ACanvas.StretchDraw(rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top, FBitmap.RawImage);
end;
procedure TBGRAfpGUIBitmap.GetImageFromCanvas(CanvasSource: TCanvas; x,
y: integer);
begin
NotAvailable;
end;
procedure TBGRAfpGUIBitmap.DataDrawTransparent(ACanvas: TCanvas; Rect: TRect;
AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
var temp: TBGRAfpGUIBitmap;
begin
temp := TBGRAfpGUIBitmap.Create(AWidth,AHeight);
move(AData^, temp.Data^, temp.NbPixels*sizeof(TBGRAPixel));
if ALineOrder <> temp.LineOrder then temp.VerticalFlip;
temp.Draw(ACanvas, Rect, False);
temp.Free;
end;
procedure TBGRAfpGUIBitmap.DataDrawOpaque(ACanvas: TCanvas; Rect: TRect;
AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
var temp: TBGRAfpGUIBitmap;
begin
temp := TBGRAfpGUIBitmap.Create(AWidth,AHeight);
move(AData^, temp.Data^, temp.NbPixels*sizeof(TBGRAPixel));
if ALineOrder <> temp.LineOrder then temp.VerticalFlip;
temp.Draw(ACanvas, Rect, True);
temp.Free;
end;
procedure TBGRAfpGUIBitmap.TakeScreenshot(ARect: TRect);
begin
NotAvailable;
end;
procedure TBGRAfpGUIBitmap.TakeScreenshotOfPrimaryMonitor;
begin
NotAvailable;
end;
procedure TBGRAfpGUIBitmap.LoadFromDevice(DC: System.THandle);
begin
NotAvailable;
end;
procedure TBGRAfpGUIBitmap.LoadFromDevice(DC: System.THandle; ARect: TRect);
begin
NotAvailable;
end;
end.