| 1 | {
|
|---|
| 2 | /**************************************************************************\
|
|---|
| 3 | bgraqtbitmap.pas
|
|---|
| 4 | -----------------
|
|---|
| 5 | This unit should NOT be added to the 'uses' clause.
|
|---|
| 6 | It contains patches for Qt.
|
|---|
| 7 |
|
|---|
| 8 | ****************************************************************************
|
|---|
| 9 | * *
|
|---|
| 10 | * This file is part of BGRABitmap library which is distributed under the *
|
|---|
| 11 | * modified LGPL. *
|
|---|
| 12 | * *
|
|---|
| 13 | * See the file COPYING.modifiedLGPL.txt, included in this distribution, *
|
|---|
| 14 | * for details about the copyright. *
|
|---|
| 15 | * *
|
|---|
| 16 | * This program is distributed in the hope that it will be useful, *
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|---|
| 19 | * *
|
|---|
| 20 | ****************************************************************************
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | unit BGRAQtBitmap;
|
|---|
| 24 |
|
|---|
| 25 | {$mode objfpc}{$H+}
|
|---|
| 26 |
|
|---|
| 27 | interface
|
|---|
| 28 |
|
|---|
| 29 | uses
|
|---|
| 30 | Classes, SysUtils, BGRALCLBitmap, Graphics,
|
|---|
| 31 | GraphType, BGRABitmapTypes;
|
|---|
| 32 |
|
|---|
| 33 | type
|
|---|
| 34 | { TBGRAQtBitmap }
|
|---|
| 35 |
|
|---|
| 36 | TBGRAQtBitmap = class(TBGRALCLBitmap)
|
|---|
| 37 | private
|
|---|
| 38 | procedure SlowDrawTransparent(ABitmap: TBGRACustomBitmap;
|
|---|
| 39 | ACanvas: TCanvas; ARect: TRect);
|
|---|
| 40 | public
|
|---|
| 41 | procedure DataDrawTransparent(ACanvas: TCanvas; Rect: TRect;
|
|---|
| 42 | AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer); override;
|
|---|
| 43 | procedure DataDrawOpaque(ACanvas: TCanvas; ARect: TRect; AData: Pointer;
|
|---|
| 44 | ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer); override;
|
|---|
| 45 | procedure Draw(ACanvas: TCanvas; x, y: integer; Opaque: boolean = True); override;
|
|---|
| 46 | procedure Draw(ACanvas: TCanvas; Rect: TRect; Opaque: boolean = True); override;
|
|---|
| 47 | procedure GetImageFromCanvas(CanvasSource: TCanvas; x, y: integer); override;
|
|---|
| 48 | end;
|
|---|
| 49 |
|
|---|
| 50 | implementation
|
|---|
| 51 |
|
|---|
| 52 | uses LCLType,
|
|---|
| 53 | LCLIntf, IntfGraphics,
|
|---|
| 54 | qtobjects, qt4,
|
|---|
| 55 | FPImage;
|
|---|
| 56 |
|
|---|
| 57 | procedure TBGRAQtBitmap.SlowDrawTransparent(ABitmap: TBGRACustomBitmap;
|
|---|
| 58 | ACanvas: TCanvas; ARect: TRect);
|
|---|
| 59 | begin
|
|---|
| 60 | ACanvas.StretchDraw(ARect, ABitmap.Bitmap);
|
|---|
| 61 | end;
|
|---|
| 62 |
|
|---|
| 63 | procedure TBGRAQtBitmap.DataDrawTransparent(ACanvas: TCanvas; Rect: TRect;
|
|---|
| 64 | AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
|
|---|
| 65 | var
|
|---|
| 66 | Temp: TBGRALCLPtrBitmap;
|
|---|
| 67 | begin
|
|---|
| 68 | Temp := TBGRALCLPtrBitmap.Create(AWidth, AHeight, AData);
|
|---|
| 69 | Temp.LineOrder := ALineOrder;
|
|---|
| 70 | SlowDrawTransparent(Temp, ACanvas, Rect);
|
|---|
| 71 | Temp.Free;
|
|---|
| 72 | end;
|
|---|
| 73 |
|
|---|
| 74 | procedure TBGRAQtBitmap.DataDrawOpaque(ACanvas: TCanvas; ARect: TRect;
|
|---|
| 75 | AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
|
|---|
| 76 | var psrc,pdest: PBGRAPixel;
|
|---|
| 77 | bmp: TBGRAQtBitmap;
|
|---|
| 78 | begin
|
|---|
| 79 | {$IFDEF DARWIN}
|
|---|
| 80 | bmp := TBGRAQtBitmap.Create(AWidth,AHeight);
|
|---|
| 81 | try
|
|---|
| 82 | if ALineOrder = riloTopToBottom then psrc := AData
|
|---|
| 83 | else psrc := PBGRAPixel(AData) + (AWidth*AHeight);
|
|---|
| 84 | for y := 0 to AHeight-1 do
|
|---|
| 85 | begin
|
|---|
| 86 | pdest := bmp.ScanLine[y];
|
|---|
| 87 | for x := 0 to AWidth-1 do
|
|---|
| 88 | begin
|
|---|
| 89 | pdest^.red := psrc^.red;
|
|---|
| 90 | pdest^.green:= psrc^.green;
|
|---|
| 91 | pdest^.blue := psrc^.blue;
|
|---|
| 92 | pdest^.alpha := 255;
|
|---|
| 93 | end;
|
|---|
| 94 | if ALineOrder = riloBottomToTop then psrc -= 2*AWidth;
|
|---|
| 95 | end;
|
|---|
| 96 | bmp.Draw(ACanvas, ARect, false);
|
|---|
| 97 | finally
|
|---|
| 98 | bmp.Free;
|
|---|
| 99 | end;
|
|---|
| 100 | {$ELSE}
|
|---|
| 101 | inherited DataDrawOpaque(ACanvas, ARect, AData, ALineOrder, AWidth, AHeight);
|
|---|
| 102 | {$ENDIF}
|
|---|
| 103 | end;
|
|---|
| 104 |
|
|---|
| 105 | procedure TBGRAQtBitmap.Draw(ACanvas: TCanvas; x, y: integer; Opaque: boolean);
|
|---|
| 106 | begin
|
|---|
| 107 | if self = nil then
|
|---|
| 108 | exit;
|
|---|
| 109 | if Opaque then
|
|---|
| 110 | DataDrawOpaque(ACanvas, Rect(X, Y, X + Width, Y + Height), Data, FLineOrder,
|
|---|
| 111 | FWidth, FHeight)
|
|---|
| 112 | else
|
|---|
| 113 | SlowDrawTransparent(Self, ACanvas, Rect(X, Y, X + Width, Y + Height));
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | procedure TBGRAQtBitmap.Draw(ACanvas: TCanvas; Rect: TRect; Opaque: boolean);
|
|---|
| 117 | begin
|
|---|
| 118 | if self = nil then
|
|---|
| 119 | exit;
|
|---|
| 120 | if Opaque then
|
|---|
| 121 | DataDrawOpaque(ACanvas, Rect, Data, FLineOrder, FWidth, FHeight)
|
|---|
| 122 | else
|
|---|
| 123 | SlowDrawTransparent(Self, ACanvas, Rect);
|
|---|
| 124 | end;
|
|---|
| 125 |
|
|---|
| 126 | procedure TBGRAQtBitmap.GetImageFromCanvas(CanvasSource: TCanvas; x, y: integer);
|
|---|
| 127 | var
|
|---|
| 128 | bmp: TBitmap;
|
|---|
| 129 | Ofs: TPoint;
|
|---|
| 130 | SrcX, SrcY: integer;
|
|---|
| 131 | dcSource, dcDest: TQtDeviceContext;
|
|---|
| 132 | B: Boolean;
|
|---|
| 133 | begin
|
|---|
| 134 | DiscardBitmapChange;
|
|---|
| 135 | bmp := TBitmap.Create;
|
|---|
| 136 | bmp.PixelFormat := pf24bit;
|
|---|
| 137 | bmp.Width := Width;
|
|---|
| 138 | bmp.Height := Height;
|
|---|
| 139 | dcDest := TQtDeviceContext(bmp.Canvas.handle);
|
|---|
| 140 |
|
|---|
| 141 | dcSource := TQtDeviceContext(CanvasSource.Handle);
|
|---|
| 142 | LCLIntf.GetWindowOrgEx(CanvasSource.Handle, @Ofs);
|
|---|
| 143 |
|
|---|
| 144 | SrcX := x + Ofs.X;
|
|---|
| 145 | SrcY := y + Ofs.Y;
|
|---|
| 146 |
|
|---|
| 147 | if (dcSource.vImage <> nil) and (dcSource.vImage.Handle <> nil) then
|
|---|
| 148 | begin
|
|---|
| 149 | // we must stop painting on device
|
|---|
| 150 | B := QPainter_isActive(dcDest.Widget);
|
|---|
| 151 | if B then
|
|---|
| 152 | QPainter_end(dcDest.Widget);
|
|---|
| 153 | TQtImage(bmp.Handle).CopyFrom(dcSource.vImage.Handle,
|
|---|
| 154 | SrcX, SrcY, Width, Height);
|
|---|
| 155 | if B then
|
|---|
| 156 | QPainter_begin(dcDest.Widget, TQtImage(bmp.Handle).Handle);
|
|---|
| 157 | end;
|
|---|
| 158 |
|
|---|
| 159 | LoadFromRawImage(bmp.RawImage, 255, True);
|
|---|
| 160 | bmp.Free;
|
|---|
| 161 | InvalidateBitmap;
|
|---|
| 162 | end;
|
|---|
| 163 |
|
|---|
| 164 | end.
|
|---|
| 165 |
|
|---|