File: bgragtkbitmap.pas

package info (click to toggle)
lazpaint 7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 56,000 kB
  • sloc: pascal: 277,538; python: 2,494; makefile: 233; sh: 221
file content (606 lines) | stat: -rw-r--r-- 17,303 bytes parent folder | download
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// SPDX-License-Identifier: LGPL-3.0-linking-exception

{ Implementation of BGRABitmap for Gtk }
unit BGRAGtkBitmap;
{ This unit should NOT be added to the **uses** clause. }

{$mode objfpc}{$H+}

interface

uses
  BGRAClasses, SysUtils, BGRALCLBitmap, Graphics,
  GraphType;

type
  { Implementation of TBGRABitmap for Gtk }
  TBGRAGtkBitmap = class(TBGRALCLBitmap)
  private
    FPixBuf: Pointer;
    procedure DrawTransparent(ACanvas: TCanvas; ARect: TRect);
    procedure DrawOpaque(ACanvas: TCanvas; ARect: TRect);
  protected
    procedure ReallocData; override;
    procedure FreeData; override;
  public
    procedure DataDrawTransparent(ACanvas: TCanvas; ARect: TRect;
      AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
      override;
    procedure DataDrawTransparent(ACanvas: TCanvas; ARect: TRect; ADataFirstRow: Pointer;
      ARowStride: integer; AWidth, AHeight: integer); overload;
    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 math, BGRABitmapTypes, BGRADefaultBitmap, BGRAFilterScanner, LCLType,
  LCLIntf, IntfGraphics,
  {$IFDEF LCLgtk3}
  LazGdk3, Gtk3Objects, LazGdkPixbuf2, LazGObject2, LazGLib2, Lazcairo1,
  {$ENDIF}
  {$IFDEF LCLgtk2}
  gdk2, gtk2def, gdk2pixbuf, glib2,
  {$ENDIF}
  {$IFDEF LCLgtk}
  gdk, gtkdef, gtkProc, gdkpixbuf, glib,
  {$ENDIF}
  FPImage, Dialogs;

procedure TBGRAGtkBitmap.ReallocData;
begin
  {$IFNDEF LCLgtk}
  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(FDataByte),
      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
  {$IFNDEF LCLgtk}
  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; ARect: TRect);
var DrawWidth,DrawHeight: integer;
    stretched: TBGRAGtkBitmap;
    P: TPoint;
    {$IFDEF LCLgtk3}
    cr: Pcairo_t;
    {$ENDIF}
begin
  DrawWidth := abs(ARect.Right-ARect.Left);
  DrawHeight := abs(ARect.Bottom-ARect.Top);
  if (Height = 0) or (Width = 0) or (DrawWidth = 0) or (DrawHeight = 0)
     or (ACanvas.Handle = 0) then
    exit;

  {$IFNDEF LCLgtk3}
  if (DrawWidth <> Width) or (DrawHeight <> Height) then
  begin
    stretched := Resample(DrawWidth,DrawHeight,rmSimpleStretch) as TBGRAGtkBitmap;
    try
      stretched.DrawTransparent(ACanvas,ARect);
    finally
      stretched.Free;
    end;
    exit;
  end;
  {$ENDIF}

  LoadFromBitmapIfNeeded;

  {$PUSH}{$WARNINGS OFF}If not TBGRAPixel_RGBAOrder then SwapRedBlue;{$POP}

  {$IFDEF LCLgtk3}
  P := ARect.TopLeft;
  LPToDP(ACanvas.Handle, P, 1);
  cr := TGtk3DeviceContext(ACanvas.Handle).pcr;
  cairo_save(cr);
  cairo_translate(cr, P.X, P.Y);
  cairo_scale(cr,
    (ARect.Right - ARect.Left) / Width,
    (ARect.Bottom - ARect.Top) / Height);
  gdk_cairo_set_source_pixbuf(cr, FPixBuf, 0, 0);
  cairo_paint(cr);
  cairo_restore(cr);
  {$ELSE}
  P := Point(min(ARect.Left,ARect.Right), min(ARect.Top,ARect.Bottom));
  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_PIXBUF_ALPHA_FULL, 1,
    GDK_RGB_DITHER_NORMAL,0,0);
  {$ENDIF}

  {$PUSH}{$WARNINGS OFF}If not TBGRAPixel_RGBAOrder then SwapRedBlue;{$POP}
end;

procedure TBGRAGtkBitmap.DrawOpaque(ACanvas: TCanvas; ARect: TRect);
begin
  DataDrawOpaque(ACanvas,ARect,Data,LineOrder,Width,Height);
end;

procedure TBGRAGtkBitmap.DataDrawTransparent(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;

  DataDrawTransparent(ACanvas, ARect, firstRow, rowStride, AWidth, AHeight);
end;

procedure TBGRAGtkBitmap.DataDrawTransparent(ACanvas: TCanvas; ARect: TRect;
  ADataFirstRow: Pointer; ARowStride: integer; AWidth, AHeight: integer);

  {$IFDEF LCLgtk3}
  procedure DrawGtk3;

    function IsDataOpaque: boolean;
    var
      curRow: PByte;
      y, x: Integer;
      p: PBGRAPixel;
    begin
      curRow := PByte(ADataFirstRow);
      for y := AHeight-1 downto 0 do
      begin
        p := PBGRAPixel(curRow);
        for x := AWidth-1 downto 0 do
        begin
          if p^.alpha <> 255 then exit(false);
          inc(p);
        end;
        curRow += ARowStride;
      end;
      exit(true);
    end;

    function PremultiplyByAlpha: Pointer;
    var
      curRow: PByte;
      y, x: Integer;
      p, pDest: PBGRAPixel;
      alpha256: Byte;
    begin
      getmem(result, AWidth*AHeight*sizeof(TBGRAPixel));
      pDest := PBGRAPixel(result);

      curRow := PByte(ADataFirstRow);
      for y := AHeight-1 downto 0 do
      begin
        p := PBGRAPixel(curRow);
        for x := AWidth-1 downto 0 do
        begin
          alpha256 := p^.alpha + byte(p^.alpha >= 128);
          pDest^.red := (p^.red * alpha256 + 128) shr 8;
          pDest^.green := (p^.green * alpha256 + 128) shr 8;
          pDest^.blue := (p^.blue * alpha256 + 128) shr 8;
          pDest^.alpha := p^.alpha;
          inc(p);
        end;
        curRow += ARowStride;
      end;
    end;

  var
    temp: integer;
    dataStart, premultipliedData: Pointer;
    cr: Pcairo_t;
    surface: Pcairo_surface_t;
  begin
    if IsDataOpaque then
    begin
      // avoid premultiplication by alpha if possible
      DataDrawOpaque(ACanvas, ARect, ADataFirstRow, ARowStride, AWidth, AHeight);
      exit;
    end;

    LPtoDP(ACanvas.Handle, ARect, 2);

    if ARowStride < 0 then
    begin
      dataStart := PByte(ADataFirstRow) + ARowStride * (AHeight - 1);
      temp := ARect.Top;
      ARect.Top := ARect.Bottom;
      ARect.Bottom := temp;
    end else
      dataStart := ADataFirstRow;

    premultipliedData := PremultiplyByAlpha;

    surface := cairo_image_surface_create_for_data(PByte(premultipliedData),
      CAIRO_FORMAT_ARGB32, AWidth, AHeight, Abs(ARowStride));
    if cairo_surface_status(surface) <> CAIRO_STATUS_SUCCESS then
    begin
      freemem(premultipliedData);
      raise Exception.Create('Error creating Cairo surface from pixel data');
    end;

    cr := TGtk3DeviceContext(ACanvas.Handle).pcr;
    cairo_save(cr);
    cairo_translate(cr, ARect.Left, ARect.Top);
    cairo_scale(cr,
      (ARect.Right - ARect.Left) / AWidth,
      (ARect.Bottom - ARect.Top) / AHeight);
    cairo_set_source_surface(cr, surface, 0, 0);
    cairo_paint(cr);
    cairo_restore(cr);

    cairo_surface_destroy(surface);
    freemem(premultipliedData);
    ACanvas.Changed;
  end;
  {$ELSE}
  procedure DrawGtk2;

    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 DrawStretchedSoftware;
    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('DataDrawTransparent 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, rmSimpleStretch);
      ptr.free;
      try
        DataDrawTransparent(ACanvas,ARect,stretched.Data,stretched.LineOrder,stretched.Width,stretched.Height);
      finally
        stretched.Free;
      end;
    end;

  var
    temp: integer;
    tempPixbuf: PGdkPixbuf;
  begin
    {$PUSH}{$WARNINGS OFF}if not TBGRAPixel_RGBAOrder then DataSwapRedBlue;{$POP}

    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
      DrawStretchedSoftware
    else
    begin
      LPtoDP(ACanvas.Handle, ARect, 2);
      tempPixbuf := gdk_pixbuf_new_from_data(pguchar(ADataFirstRow),
        GDK_COLORSPACE_RGB, True, 8, AWidth, AHeight, AWidth*Sizeof(TBGRAPixel), nil, nil);
      if tempPixbuf = nil then
        raise Exception.Create('Error initializing Pixbuf');

      gdk_pixbuf_render_to_drawable(tempPixbuf,
        TGtkDeviceContext(ACanvas.Handle).Drawable,
        TGtkDeviceContext(ACanvas.Handle).GC,
        0,0, ARect.Left,ARect.Top,
        AWidth,AHeight,
        //GDK_PIXBUF_ALPHA_FULL, 1,
        GDK_RGB_DITHER_NORMAL,0,0);

      {$IFNDEF LCLgtk}
      If tempPixbuf <> nil then g_object_unref(tempPixbuf);
      {$ELSE}
      If tempPixbuf <> nil then gdk_pixbuf_unref(tempPixbuf);
      {$ENDIF}
      ACanvas.Changed;
    end;
    {$PUSH}{$WARNINGS OFF}if not TBGRAPixel_RGBAOrder then DataSwapRedBlue;{$POP}
  end;
  {$ENDIF}

begin
  if (AHeight = 0) or (AWidth = 0) or (ARect.Left = ARect.Right) or
    (ARect.Top = ARect.Bottom) or (ACanvas.Handle = 0) then exit;

  {$IFDEF LCLgtk3}
  DrawGtk3;
  {$ELSE}
  DrawGtk2;
  {$ENDIF}
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);

  {$IFDEF LCLgtk3}
  procedure DrawGtk3;
  var
    temp: integer;
    dataStart: Pointer;
    cr: Pcairo_t;
    surface: Pcairo_surface_t;
  begin
    LPtoDP(ACanvas.Handle, ARect, 2);

    if ARowStride < 0 then
    begin
      dataStart := PByte(ADataFirstRow) + ARowStride * (AHeight - 1);
      temp := ARect.Top;
      ARect.Top := ARect.Bottom;
      ARect.Bottom := temp;
    end else
      dataStart := ADataFirstRow;

    surface := cairo_image_surface_create_for_data(PByte(dataStart),
      CAIRO_FORMAT_RGB24, AWidth, AHeight, Abs(ARowStride));
    if cairo_surface_status(surface) <> CAIRO_STATUS_SUCCESS then
      raise Exception.Create('Error creating Cairo surface from pixel data');

    cr := TGtk3DeviceContext(ACanvas.Handle).pcr;
    cairo_save(cr);
    cairo_translate(cr, ARect.Left, ARect.Top);
    cairo_scale(cr,
      (ARect.Right - ARect.Left) / AWidth,
      (ARect.Bottom - ARect.Top) / AHeight);
    cairo_set_source_surface(cr, surface, 0, 0);
    cairo_paint(cr);
    cairo_restore(cr);

    cairo_surface_destroy(surface);
    ACanvas.Changed;
  end;
  {$ELSE}
  procedure DrawGtk2;

    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 DrawStretchedSoftware;
    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, rmSimpleStretch);
      ptr.free;
      DataDrawOpaque(ACanvas,ARect,stretched.Data,stretched.LineOrder,stretched.Width,stretched.Height);
      stretched.Free;
    end;

  var
    temp: integer;
  begin
    {$PUSH}{$WARNINGS OFF}if not TBGRAPixel_RGBAOrder then DataSwapRedBlue;{$POP}

    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
      DrawStretchedSoftware
    else
    begin
      LPtoDP(ACanvas.Handle, ARect, 2);
      gdk_draw_rgb_32_image(TGtkDeviceContext(ACanvas.Handle).Drawable,
        TGtkDeviceContext(ACanvas.Handle).GC, ARect.Left, ARect.Top,
        AWidth,AHeight, GDK_RGB_DITHER_NORMAL,
        ADataFirstRow, ARowStride);
      ACanvas.Changed;
    end;
    {$PUSH}{$WARNINGS OFF}if not TBGRAPixel_RGBAOrder then DataSwapRedBlue;{$POP}
  end;
  {$ENDIF}

begin
  if (AHeight = 0) or (AWidth = 0) or (ARect.Left = ARect.Right) or
    (ARect.Top = ARect.Bottom) or (ACanvas.Handle = 0) then exit;

  {$IFDEF LCLgtk3}
  DrawGtk3;
  {$ELSE}
  DrawGtk2;
  {$ENDIF}
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);
  {$IFDEF LCLgtk3}
  gdk_pixbuf_get_from_window(TGtk3DeviceContext(
    CanvasSource.Handle).Window, p.X, p.Y, Width, Height);
  {$ELSE}
  gdk_pixbuf_get_from_drawable(FPixBuf,
    TGtkDeviceContext(CanvasSource.Handle).Drawable,
    nil, P.X,P.Y,0,0,Width,Height);
  {$ENDIF}
  {$PUSH}{$WARNINGS OFF}If not TBGRAPixel_RGBAOrder then SwapRedBlue;{$POP}
  InvalidateBitmap;
end;


end.