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
|
unit SBitmap;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LCLProc, LResources, Forms, Controls, Graphics, Dialogs, FileCtrl, StdCtrls, ExtCtrls, ComCtrls,
IntfGraphics, GraphType, FPImage, LCLType;
type
TSColor =
packed record
B: Byte;
G: Byte;
R: Byte;
A: Byte;
end;
type
TSBitmap = class
private
{ private declarations }
// vars
FWidth: Integer;
FHeight: Integer;
RID: TRawImageDescription;
// img
FBitmap: TBitmap;
FBitmapCore: TLazIntfImage;
FBitmapTemp: TLazIntfImage;
PLine: Pointer;
PLinePByte: PByte;
FLine: Integer;
FColor: TSColor;
public
{ public declarations }
// object
constructor Create;
destructor Destroy; override;
// load / get bitmap
procedure LoadBitmap(aBitmap: TBitmap);
function Bitmap:TBitmap;
// Color
procedure Fill(Color: TSColor);
end;
implementation
{ private declarations }
{ public declarations }
/////////////////////////////////////////////////////
// object
/////////////////////////////////////////////////////
constructor TSBitmap.Create;
begin
FWidth := 10;
FHeight := 10;
FBitmap := TBitmap.Create;
FBitmapCore := TLazIntfImage.Create(FWidth,FHeight);
RID.Init_BPP24_B8G8R8_BIO_TTB(FWidth,FHeight);
FBitmapCore.DataDescription := RID;
FLine := 0;
PLine := FBitmapCore.GetDataLineStart(FLine);
PLinePByte := PByte(PLine);
end;
destructor TSBitmap.Destroy;
begin
FBitmapCore.Free;
FBitmap.Free;
inherited;
end;
/////////////////////////////////////////////////////
// load / get bitmap
/////////////////////////////////////////////////////
procedure TSBitmap.LoadBitmap(aBitmap: TBitmap);
begin
FBitmapTemp := TLazIntfImage.Create(0,0);
FBitmapTemp.LoadFromBitmap(aBitmap.Handle,aBitmap.MaskHandle);
FWidth := FBitmapTemp.Width;
FHeight := FBitmapTemp.Height;
FBitmapCore.Width := FWidth;
FBitmapCore.Height := Fheight;
RID.Init_BPP24_B8G8R8_BIO_TTB(FWidth,FHeight);
FBitmapCore.DataDescription := RID;
FBitmapCore.CopyPixels(FBitmapTemp);
FLine := 0;
PLine := FBitmapCore.GetDataLineStart(FLine);
PLinePByte := PByte(PLine);
FBitmapTemp.Free;
end;
function TSBitmap.Bitmap:TBitmap;
begin
FBitmap.Width := FBitmapCore.Width;
FBitmap.Height := FBitmapCore.Height;
FBitmapTemp := FBitmap.CreateIntfImage;
FBitmapTemp.CopyPixels(FBitmapCore);
FBitmap.LoadFromIntfImage(FBitmapTemp);
// result
Result := FBitmap;
// free
FBitmapTemp.Free;
end;
/////////////////////////////////////////////////////
// color
/////////////////////////////////////////////////////
procedure TSBitmap.Fill(Color: TSColor);
var
X,Y,R,G,B,A,AI: Integer;
begin
// alpha
A := Color.A;
AI := 255 - A;
//
for Y := 0 to FHeight-1 do
begin
FLine := Y;
PLine := FBitmapCore.GetDataLineStart(Y);
PLinePByte := PByte(PLine);
for X := 0 to FWidth-1 do
begin
// blend
B := Round(((Color.B*A)+(PLinePByte[X * 3]*AI))/255);
G := Round(((Color.G*A)+(PLinePByte[X * 3 + 1]*AI))/255);
R := Round(((Color.R*A)+(PLinePByte[X * 3 + 2]*AI))/255);
// secure color
if B < 0 then B := 0;
if B > 255 then B := 255;
if G < 0 then G := 0;
if G > 255 then G := 255;
if R < 0 then R := 0;
if R > 255 then R := 255;
// set new
PLinePByte[X * 3] := B;
PLinePByte[X * 3 + 1] := G;
PLinePByte[X * 3 + 2] := R;
end;
end;
end;
initialization
end. |
Partager