forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbgragtkbitmap.pas
378 lines (324 loc) · 11.2 KB
/
bgragtkbitmap.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
{ ***************************************************************************
* *
* 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 BGRAGtkBitmap;
//This unit should NOT be added to the 'uses' clause.
//It contains patches for Gtk
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, BGRALCLBitmap, Graphics,
GraphType;
type
{ TBGRAGtkBitmap }
TBGRAGtkBitmap = class(TBGRALCLBitmap)
private
FPixBuf: Pointer;
procedure DrawTransparent(ACanvas: TCanvas; Rect: TRect);
procedure DrawOpaque(ACanvas: TCanvas; ARect: TRect; ASourceRect: TRect);
procedure DrawOpaque(ACanvas: TCanvas; ARect: TRect);
protected
procedure ReallocData; override;
procedure FreeData; override;
public
procedure DataDrawTransparent(ACanvas: TCanvas; Rect: TRect;
AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
override;
procedure DrawPart(ARect: TRect; ACanvas: TCanvas; x, y: integer; Opaque: boolean); override;
procedure Draw(ACanvas: TCanvas; x, y: integer; Opaque: boolean = True); override;
procedure Draw(ACanvas: TCanvas; Rect: TRect; Opaque: boolean = True); override;
procedure DataDrawOpaque(ACanvas: TCanvas; ARect: TRect; AData: Pointer;
ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer); overload; override;
procedure DataDrawOpaque(ACanvas: TCanvas; ARect: TRect; ADataFirstRow: Pointer;
ARowStride: integer; AWidth, AHeight: integer); overload;
procedure GetImageFromCanvas(CanvasSource: TCanvas; x, y: integer); override;
end;
implementation
uses BGRABitmapTypes, BGRADefaultBitmap, BGRAFilterScanner, LCLType,
LCLIntf, IntfGraphics,
{$IFDEF LCLgtk2}
gdk2, gtk2def, gdk2pixbuf, glib2,
{$ENDIF}
{$IFDEF LCLgtk}
gdk, gtkdef, gtkProc, gdkpixbuf, glib,
{$ENDIF}
FPImage, Dialogs;
{$IFDEF LCLgtk2}
type TGtkDeviceContext = TGtk2DeviceContext;
{$ENDIF}
procedure TBGRAGtkBitmap.ReallocData;
begin
{$IFDEF LCLgtk2}
If FPixBuf <> nil then g_object_unref(FPixBuf);
{$ELSE}
If FPixBuf <> nil then gdk_pixbuf_unref(FPixBuf);
{$ENDIF}
FPixBuf := nil;
inherited ReallocData;
if (FWidth <> 0) and (FHeight <> 0) then
begin
FPixbuf := gdk_pixbuf_new_from_data(pguchar(FData),
GDK_COLORSPACE_RGB, True, 8, Width, Height, Width*Sizeof(TBGRAPixel), nil, nil);
if FPixbuf = nil then
raise Exception.Create('Error initializing Pixbuf');
end;
end;
procedure TBGRAGtkBitmap.FreeData;
begin
{$IFDEF LCLgtk2}
If FPixBuf <> nil then g_object_unref(FPixBuf);
{$ELSE}
If FPixBuf <> nil then gdk_pixbuf_unref(FPixBuf);
{$ENDIF}
FPixBuf := nil;
inherited FreeData;
end;
procedure TBGRAGtkBitmap.DrawTransparent(ACanvas: TCanvas; Rect: TRect);
var DrawWidth,DrawHeight: integer;
stretched: TBGRAGtkBitmap;
P: TPoint;
begin
DrawWidth := Rect.Right-Rect.Left;
DrawHeight := Rect.Bottom-Rect.Top;
if (Height = 0) or (Width = 0) or (DrawWidth <= 0) or (DrawHeight <= 0) then
exit;
if (DrawWidth <> Width) or (DrawHeight <> Height) then
begin
stretched := Resample(DrawWidth,DrawHeight,rmSimpleStretch) as TBGRAGtkBitmap;
stretched.DrawTransparent(ACanvas,Rect);
stretched.Free;
exit;
end;
LoadFromBitmapIfNeeded;
If not TBGRAPixel_RGBAOrder then SwapRedBlue;
P := Rect.TopLeft;
LPToDP(ACanvas.Handle, P, 1);
gdk_pixbuf_render_to_drawable(FPixBuf,
TGtkDeviceContext(ACanvas.Handle).Drawable,
TGtkDeviceContext(ACanvas.Handle).GC,
0,0, P.X,P.Y,
Width,Height,
GDK_RGB_DITHER_NORMAL,0,0);
If not TBGRAPixel_RGBAOrder then SwapRedBlue;
end;
procedure TBGRAGtkBitmap.DrawOpaque(ACanvas: TCanvas; ARect: TRect;
ASourceRect: TRect);
begin
DataDrawOpaque(ACanvas,ARect,Data,LineOrder,Width,Height);
end;
procedure TBGRAGtkBitmap.DrawOpaque(ACanvas: TCanvas; ARect: TRect);
begin
DrawOpaque(ACanvas, ARect, rect(0,0,Width,Height));
end;
procedure TBGRAGtkBitmap.DataDrawTransparent(ACanvas: TCanvas; Rect: TRect;
AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
var
TempGtk: TBGRAGtkBitmap;
temp: integer;
begin
if (AHeight = 0) or (AWidth = 0) or (Rect.Left = Rect.Right) or
(Rect.Top = Rect.Bottom) then
exit;
if Rect.Right < Rect.Left then
begin
temp := Rect.Left;
Rect.Left := Rect.Right;
Rect.Right := temp;
end;
if Rect.Bottom < Rect.Top then
begin
temp := Rect.Top;
Rect.Top := Rect.Bottom;
Rect.Bottom := temp;
end;
TempGtk := TBGRAGtkBitmap.Create(AWidth, AHeight);
Move(AData^,TempGtk.Data^,TempGtk.NbPixels*sizeof(TBGRAPixel));
if ALineOrder <> TempGtk.LineOrder then TempGtk.VerticalFlip;
TempGtk.DrawTransparent(ACanvas,Rect);
TempGtk.Free;
end;
procedure TBGRAGtkBitmap.DrawPart(ARect: TRect; ACanvas: TCanvas; x,
y: integer; Opaque: boolean);
var
rowStride,w,h: Integer;
begin
if Opaque then
begin
if LineOrder = riloTopToBottom then
rowStride := Width*sizeof(TBGRAPixel)
else
rowStride := -Width*sizeof(TBGRAPixel);
w:= ARect.Right-ARect.Left;
h:= ARect.Bottom-ARect.Top;
DataDrawOpaque(ACanvas, rect(x,y,x+w,y+h), Scanline[ARect.Top]+ARect.Left, rowStride, w,h);
end
else
inherited DrawPart(ARect, ACanvas, x, y, Opaque);
end;
procedure TBGRAGtkBitmap.Draw(ACanvas: TCanvas; x, y: integer; Opaque: boolean);
begin
if self = nil then
exit;
if Opaque then
DrawOpaque(ACanvas, Rect(X, Y, X + Width, Y + Height))
else
DrawTransparent(ACanvas, Rect(X, Y, X + Width, Y + Height));
end;
procedure TBGRAGtkBitmap.Draw(ACanvas: TCanvas; Rect: TRect; Opaque: boolean);
begin
if self = nil then
exit;
if Opaque then
DrawOpaque(ACanvas, Rect)
else
DrawTransparent(ACanvas, Rect);
end;
procedure TBGRAGtkBitmap.DataDrawOpaque(ACanvas: TCanvas; ARect: TRect;
AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
var
rowStride: Integer;
firstRow: Pointer;
begin
if ALineOrder = riloTopToBottom then
begin
rowStride := AWidth*sizeof(TBGRAPixel);
firstRow := AData;
end
else
begin
rowStride := -AWidth*sizeof(TBGRAPixel);
firstRow := PBGRAPixel(AData) + (AWidth*(AHeight-1));
end;
DataDrawOpaque(ACanvas, ARect, firstRow, rowStride, AWidth, AHeight);
end;
procedure TBGRAGtkBitmap.DataDrawOpaque(ACanvas: TCanvas; ARect: TRect;
ADataFirstRow: Pointer; ARowStride: integer; AWidth, AHeight: integer);
procedure DataSwapRedBlue;
var
y: Integer;
p: PByte;
begin
p := PByte(ADataFirstRow);
for y := 0 to AHeight-1 do
begin
TBGRAFilterScannerSwapRedBlue.ComputeFilterAt(PBGRAPixel(p),PBGRAPixel(p),AWidth,False);
inc(p, ARowStride);
end;
end;
procedure DrawStretched;
var
dataStart: Pointer;
ptr: TBGRAPtrBitmap;
stretched: TBGRACustomBitmap;
begin
if ARowStride < 0 then
dataStart := PByte(ADataFirstRow) + ARowStride*(Height-1)
else
dataStart := ADataFirstRow;
if ARowStride <> abs(AWidth*sizeof(TBGRAPixel)) then
raise exception.Create('DataDrawOpaque not supported when using custom row stride and resample');
ptr := TBGRAPtrBitmap.Create(AWidth,AHeight,dataStart);
if ARowStride < 0 then
ptr.LineOrder := riloBottomToTop
else
ptr.LineOrder := riloTopToBottom;
stretched := ptr.Resample(ARect.Right-ARect.Left,ARect.Bottom-ARect.Top);
ptr.free;
DataDrawOpaque(ACanvas,ARect,dataStart,stretched.LineOrder,stretched.Width,stretched.Height);
stretched.Free;
end;
var
temp: integer;
pos: TPoint;
dest: HDC;
begin
if (AHeight = 0) or (AWidth = 0) or (ARect.Left = ARect.Right) or
(ARect.Top = ARect.Bottom) then exit;
if ARect.Right < ARect.Left then
begin
temp := ARect.Left;
ARect.Left := ARect.Right;
ARect.Right := temp;
end;
if ARect.Bottom < ARect.Top then
begin
temp := ARect.Top;
ARect.Top := ARect.Bottom;
ARect.Bottom := temp;
end;
if (AWidth <> ARect.Right-ARect.Left) or (AHeight <> ARect.Bottom-ARect.Top) then
DrawStretched
else
begin
dest := ACanvas.Handle;
pos := ARect.TopLeft;
LPtoDP(dest, pos, 1);
if not TBGRAPixel_RGBAOrder then DataSwapRedBlue;
gdk_draw_rgb_32_image(TGtkDeviceContext(dest).Drawable,
TGtkDeviceContext(Dest).GC, pos.x,pos.y,
AWidth,AHeight, GDK_RGB_DITHER_NORMAL,
ADataFirstRow, ARowStride);
if not TBGRAPixel_RGBAOrder then DataSwapRedBlue;
end;
end;
procedure TBGRAGtkBitmap.GetImageFromCanvas(CanvasSource: TCanvas; x, y: integer);
var
subBmp: TBGRACustomBitmap;
subRect: TRect;
cw,ch: integer;
P: TPoint;
begin
cw := CanvasSource.Width;
ch := CanvasSource.Height;
if (x < 0) or (y < 0) or (x+Width > cw) or
(y+Height > ch) then
begin
FillTransparent;
if (x+Width <= 0) or (y+Height <= 0) or
(x >= cw) or (y >= ch) then
exit;
if (x > 0) then subRect.Left := x else subRect.Left := 0;
if (y > 0) then subRect.Top := y else subRect.Top := 0;
if (x+Width > cw) then subRect.Right := cw else
subRect.Right := x+Width;
if (y+Height > ch) then subRect.Bottom := ch else
subRect.Bottom := y+Height;
subBmp := NewBitmap(subRect.Right-subRect.Left,subRect.Bottom-subRect.Top);
subBmp.GetImageFromCanvas(CanvasSource,subRect.Left,subRect.Top);
PutImage(subRect.Left-x,subRect.Top-y,subBmp,dmSet);
subBmp.Free;
exit;
end;
P := Point(x,y);
LPToDP(CanvasSource.Handle, P, 1);
gdk_pixbuf_get_from_drawable(FPixBuf,
TGtkDeviceContext(CanvasSource.Handle).Drawable,
nil, P.X,P.Y,0,0,Width,Height);
If not TBGRAPixel_RGBAOrder then SwapRedBlue;
InvalidateBitmap;
end;
end.