-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphics.FastBitmap.pas
More file actions
265 lines (211 loc) · 5.1 KB
/
Graphics.FastBitmap.pas
File metadata and controls
265 lines (211 loc) · 5.1 KB
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
unit Graphics.FastBitmap;
interface
uses
Winapi.Windows;
type
TFastBitmap = class
public
type
TFColor = record
B, G, R: Byte;
constructor Create(AR, AG, AB: Byte); overload;
constructor Create(AColor: Cardinal); overload;
end;
TLine = array [0..0] of TFColor;
PLine = ^TLine;
TPLines = array [0..0] of PLine;
PPLines = ^TPLines;
private
const
hSection = 0;
private
FInitialized: Boolean;
FGap, // space between scanlines
FRowInc, // distance to next scanline
FSize, // size of Bits
FWidth, FHeight: Integer;
FPixels: PPLines;
FBits: Pointer;
FHandle: THandle;
FDC: HDC;
FBMInfo: TBitmapInfo;
FBMHeader: TBitmapInfoHeader;
procedure Initialize;
public
property Pixels: PPLines read FPixels;
property Initialized: Boolean read FInitialized;
public
constructor Create(AWidth, AHeight: Integer); overload;
constructor Create(AHBmp: Integer); overload;
destructor Destroy; override;
procedure ReleaseImg;
procedure Draw(ADest: HDC; AX, AY: Integer);
procedure StretchDraw(ADest: HDC; AX, AY, AW, AH: Integer); overload;
procedure StretchDraw(ADest: HDC; ARect: TRect); overload;
procedure LineTo(AX1, AY1, AX2, AY2: Integer; AColor: TFColor);
procedure Square(AX1, AY1, AX2, AY2: Integer; AColor: TFColor);
end;
implementation
{ TFastBMP }
procedure TFastBitmap.Initialize;
var
i: Integer;
x: Longint;
begin
FPixels := VirtualAlloc(nil, FHeight * SizeOf(PLine), MEM_COMMIT, PAGE_READWRITE);
FGap := FWidth mod 4;
FRowInc := (FWidth * 3) + FGap;
FSize := FRowInc * FHeight;
x := Integer(FBits);
for i := 0 to FHeight - 1 do
begin
FPixels[i] := Pointer(x);
Inc(x, FRowInc);
end;
FDC := CreateCompatibleDC(0);
SelectObject(FDC, FHandle);
FInitialized := True;
end;
procedure TFastBitmap.LineTo(AX1, AY1, AX2, AY2: Integer;
AColor: TFColor);
var
v7, v8, v9, v10, v11,
xEnda, xEndb, yEnda: Integer;
begin
v7 := AX2 - AX1;
v11 := 1;
if v7 <= 0 then
begin
v11 := -1;
v7 := -v7;
end;
v8 := AY2 - AY1;
yEnda := 1;
if v8 <= 0 then
begin
yEnda := -1;
v8 := -v8;
end;
if v8 < v7 then
begin
v10 := v7 div 2;
xEndb := v7;
while xEndb <> 0 do
begin
Dec(xEndb);
Pixels[AY1, AX1] := AColor;
Inc(AX1, v11);
Dec(v10, v8);
if ( v10 < 0 ) then
begin
Inc(v10, v7);
Inc(AY1, yEnda);
end;
end;
end else
begin
v9 := v8 div 2;
xEnda := v8;
while xEnda <> 0 do
begin
Dec(xEnda);
Pixels[AY1, AX1] := AColor;
Inc(AY1, yEnda);
Dec(v9, v7);
if v9 < 0 then
begin
Inc(v9, v8);
Inc(AX1, v11);
end;
end;
end;
end;
procedure TFastBitmap.ReleaseImg;
begin
DeleteDC(FDC);
DeleteObject(FHandle);
VirtualFree(FPixels, 0, MEM_RELEASE);
FInitialized := False;
end;
procedure TFastBitmap.StretchDraw(ADest: HDC; AX, AY, AW, AH: Integer);
begin
SetStretchBltMode(ADest, STRETCH_DELETESCANS);
StretchBlt(ADest, AX, AY, AW, AH, FDC, 0, 0, FWidth, FHeight, SRCCOPY);
end;
procedure TFastBitmap.Square(AX1, AY1, AX2, AY2: Integer; AColor: TFColor);
begin
LineTo(AX1, AY1, AX1, AY2, AColor);
LineTo(AX1, AY1, AX2, AY1, AColor);
LineTo(AX2-1, AY1, AX2-1, AY2, AColor);
LineTo(AX1, AY2-1, AX2, AY2-1, AColor);
end;
procedure TFastBitmap.StretchDraw(ADest: HDC; ARect: TRect);
begin
StretchDraw(ADest, ARect.Left, ARect.Top, ARect.Width, ARect.Height);
end;
constructor TFastBitmap.Create(AWidth, AHeight: Integer);
begin
FWidth := AWidth;
FHeight := AHeight;
with FBMHeader do
begin
biSize := SizeOf(FBMHeader);
biWidth := FWidth;
biHeight := -FHeight;
biPlanes := 1;
biBitCount := 24;
biCompression := BI_RGB;
end;
FBMInfo.bmiHeader := FBMHeader;
FHandle := CreateDIBSection(0, FBMInfo, DIB_RGB_COLORS, FBits, hSection, 0);
Initialize;
end;
constructor TFastBitmap.Create(AHBmp: Integer);
var
bmpRec: TBITMAP;
memDC: Integer;
begin
GetObject(AHBmp, SizeOf(bmpRec), @bmpRec);
FWidth := bmpRec.bmWidth;
FHeight := bmpRec.bmHeight;
FSize := ((FWidth * 3) + (FWidth mod 4)) * FHeight;
with FBMHeader do
begin
biSize := SizeOf(FBMHeader);
biWidth := FWidth;
biHeight := -FHeight;
biPlanes := 1;
biBitCount := 24;
biCompression := BI_RGB;
end;
FBMInfo.bmiHeader := FBMHeader;
FHandle := CreateDIBSection(0, FBMInfo, DIB_RGB_COLORS, FBits, hSection, 0);
memDC := GetDC(0);
GetDIBits(memDC, AHBmp, 0, FHeight, FBits, FBMInfo, DIB_RGB_COLORS);
ReleaseDC(0, memDC);
Initialize;
end;
procedure TFastBitmap.Draw(ADest: HDC; AX, AY: Integer);
begin
BitBlt(ADest, AX, AY, FWidth, FHeight, FDC, 0, 0, SRCCOPY);
end;
destructor TFastBitmap.Destroy;
begin
ReleaseImg;
end;
{ TFastBMP.TFColor }
constructor TFastBitmap.TFColor.Create(AR, AG, AB: Byte);
begin
r := AR;
g := AG;
b := AB;
end;
constructor TFastBitmap.TFColor.Create(AColor: Cardinal);
begin
r := Byte(AColor);
AColor := AColor shr 8;
g := Byte(AColor);
AColor := AColor shr 8;
b := Byte(AColor);
end;
end.