forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base.Bitmaps.pas
475 lines (425 loc) · 10.3 KB
/
Base.Bitmaps.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
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
unit Base.Bitmaps;
{$include lem_directives.inc}
interface
uses
LCLIntf,
Types, Classes, SysUtils, Generics.Collections, Math,
Graphics,
GR32,
Base.Utils;
type
TColor32Helper = record helper for TColor32
// speed was tested, it is as fast as working directly with TColor32Entry
strict private
function GetR: Byte; inline;
function GetG: Byte; inline;
function GetB: Byte; inline;
function GetA: Byte; inline;
procedure SetR(aValue: Byte); inline;
procedure SetG(aValue: Byte); inline;
procedure SetB(aValue: Byte); inline;
procedure SetA(aValue: Byte); inline;
public
procedure Init(aR, aG, aB, aA: Byte); inline;
function Average: Byte; inline;
//function Lightness: Single; inline;
function AsGray: TColor32; inline;
//function Recolor(newColor: TColor32): TColor32;
public
property R: Byte read GetR write SetR;
property G: Byte read GetG write SetG;
property B: Byte read GetB write SetB;
property A: Byte read GetA write SetA;
end;
TBitmap32Helper = class helper for TBitmap32
public
function GetPixelCount: Integer; inline;
//function ToPng: TPngImage;
//function ToWic: TWicImage;
//procedure FromPng(png: TPngImage);
//procedure SaveToPng(const aFileName: string);
// some manipulations
procedure ReplaceColor(FromColor, ToColor: TColor32);
procedure ReplaceAllNonZeroColors(ToColor: TColor32);
procedure ReplaceAlphaForAllNonZeroColors(alpha: Byte);
procedure MakeGray;
//procedure Recolor(newColor: TColor32);
function CalcFrameRect(aFrameCount, aFrameIndex: Integer): TRect;
end;
// special class for irregular chars (sizes are not equal)
TBitmapFont = class(TBitmap32)
private
fFrameList: TList<TRect>;
fCharList: TList<Char>;
fTempList: TBitmaps;
fLock: LONGBOOL;
procedure CreateCompleteBitmap;
public
constructor Create; override;
destructor Destroy; override;
procedure FontBeginCreate;
procedure FontEndCreate;
procedure AddChar(C: Char; src: TBitmap32);
function GetCharRect(C: Char): TRect;
function DrawChar(C: Char; dst: TBitmap32; x, y: Integer): TSize;
function TextSize(const s: string): TSize;
function DrawText(const s: string; dst: TBitmap32; x, y: Integer): TSize;
end;
implementation
{ TColor32Helper }
function TColor32Helper.GetR: Byte;
begin
Result := TColor32Entry(Self).R;
end;
function TColor32Helper.GetG: Byte;
begin
Result := TColor32Entry(Self).G;
end;
function TColor32Helper.GetB: Byte;
begin
Result := TColor32Entry(Self).B;
end;
function TColor32Helper.GetA: Byte;
begin
Result := TColor32Entry(Self).A;
end;
procedure TColor32Helper.SetR(aValue: Byte);
begin
TColor32Entry(Self).R := aValue;
end;
procedure TColor32Helper.SetG(aValue: Byte);
begin
TColor32Entry(Self).G := aValue;
end;
procedure TColor32Helper.SetB(aValue: Byte);
begin
TColor32Entry(Self).B := aValue;
end;
procedure TColor32Helper.SetA(aValue: Byte);
begin
TColor32Entry(Self).A := aValue;
end;
procedure TColor32Helper.Init(aR, aG, aB, aA: Byte);
begin
R := aR;
G := aG;
B := aB;
A := aA;
end;
(*
function TColor32Helper.Lightness: Single;
// 0.0 = black
// 1.0 = white
begin
Result := (0.21 * R)/255 + (0.72 * G)/255 + (0.07 * B)/255;
end;
*)
function TColor32Helper.Average: Byte;
begin
Result := Byte((Integer(R) + Integer(G) + Integer(B)) div 3);
end;
function TColor32Helper.AsGray: TColor32;
var
avg: Byte;
begin
avg := Average;
Result.R := avg;
Result.G := avg;
Result.B := avg;
Result.A := Self.A;
end;
(*
function TColor32Helper.Recolor(newColor: TColor32): TColor32;
// r = 0.21 g = 0.72 b = 0.07
//var
// avg: Byte;
// h,s,l: byte;
// h2,s2,l2: byte;
begin
Result := 0;
// Result := newColor;//Self;
// Result.A := 0;
// if Result = 0 then
// Exit;
// avg := Average;
//
//// var a: single := self.Lightness;
//// if a = 0 then exit;
//// var b: single := newcolor.Lightness;
//
//
// RGBtoHSL(Self, H, S, L);
// RGBtoHSL(Result, H2, S2, L2);
// Result := HSLtoRGB(H2, s, l, 0);
// Result.R := Round(newColor.R * b/a * 0.21);
// Result.G := Round(newColor.G * b/a * 0.72);
// Result.B := Round(newColor.B * b/a * 0.07);
// Result.R := (Integer(newColor.R) * 255) div avg;
// Result.G := (Integer(newColor.G) * 255) div avg;
// Result.G := (Integer(newColor.B) * 255) div avg;
// myLightness := Lightness;
//// redLightness := 0.21 * R;
//// Result.R := Round(newColor.R * myLightness * (1/0.21));
//// Result.G := Round(newColor.G * myLightness * (1/0.72));
//// Result.B := Round(newColor.B * myLightness * (1/0.07));
// Result.R := Round(newColor.R * myLightness * (1/0.21));
// Result.G := Round(newColor.G * myLightness * (1/0.72));
// Result.B := Round(newColor.B * myLightness * (1/0.07));
// Result.A := A;
end;
*)
{ TBitmap32Helper }
function TBitmap32Helper.GetPixelCount: Integer;
begin
Result := Width * Height;
end;
procedure TBitmap32Helper.ReplaceAllNonZeroColors(ToColor: TColor32);
var
P: PColor32;
i: Integer;
begin
if Width + Height = 0 then
Exit;
P := PixelPtr[0, 0];
for i := 0 to Height * Width - 1 do begin
if P^ <> 0 then
P^ := ToColor;
Inc(P);
end;
end;
procedure TBitmap32Helper.ReplaceAlphaForAllNonZeroColors(alpha: Byte);
var
P: PColor32;
i: Integer;
begin
if Width + Height = 0 then
Exit;
P := PixelPtr[0, 0];
for i := 0 to Height * Width - 1 do begin
if P^ <> 0 then
P^.A := alpha;
Inc(P);
end;
end;
function TBitmap32Helper.CalcFrameRect(aFrameCount, aFrameIndex: Integer): TRect;
var
Y, H: Integer;
begin
H := Height div aFrameCount;
Y := H * aFrameIndex;
Result := Rect(0, Y, Width, Y + H);
end;
procedure TBitmap32Helper.ReplaceColor(FromColor, ToColor: TColor32);
var
P: PColor32;
i: Integer;
begin
if Width + Height = 0 then
Exit;
P := PixelPtr[0, 0];
for i := 0 to Height * Width - 1 do begin
if P^ = FromColor then
P^ := ToColor;
Inc(P);
end;
end;
procedure TBitmap32Helper.MakeGray;
var
P: PColor32;
i: Integer;
begin
if Width + Height = 0 then
Exit;
P := PixelPtr[0, 0];
for i := 0 to Height * Width - 1 do begin
P^ := P^.AsGray;
Inc(P);
end;
end;
(*
procedure TBitmap32Helper.Recolor(newColor: TColor32);
//var
// P: PColor32; // Result := (0.21 * R) + (0.72 * G) + (0.07 * B);
// i: Integer;
// avg: Byte;
begin
// if Width + Height = 0 then
// Exit;
// P := PixelPtr[0, 0];
// for i := 0 to Height * Width - 1 do begin
// P^ := P^.Recolor(newColor);
// Inc(P);
// end;
end;
*)
{function TBitmap32Helper.ToPng: TPngImage;
var
b: TBitmap;
begin
b := TBitmap.Create;
Result := TPngImage.Create;
try
b.Assign(Self);
Result.Assign(b);
finally
b.Free;
end;
end;
function TBitmap32Helper.ToWic: TWicImage;
var
b: TBitmap;
begin
b := TBitmap.Create;
Result := TWICImage.Create;
try
b.Assign(Self);
Result.Assign(b);
finally
b.Free;
end;
end;
procedure TBitmap32Helper.FromPng(png: TPngImage);
var
b: TBitmap;
begin
b := TBitmap.Create;
try
b.Assign(png);
Self.Assign(b);
finally
b.Free;
end;
end;}
{ TBitmapFont }
constructor TBitmapFont.Create;
begin
inherited Create;
fFrameList := TList<TRect>.Create;
fCharList := TList<Char>.Create;
end;
destructor TBitmapFont.Destroy;
begin
fFrameList.Free;
fCharList.Free;
if Assigned(fTempList) then
fTempList.Free;
inherited;
end;
procedure TBitmapFont.FontBeginCreate;
begin
if not fLock then
fTempList := TBitmaps.Create;
Inc(fLock);
end;
procedure TBitmapFont.FontEndCreate;
begin
if fLock then
Dec(fLock);
if not fLock then begin
CreateCompleteBitmap;
FreeAndNil(fTempList);
end;
end;
procedure TBitmapFont.CreateCompleteBitmap;
var
x, y: Integer;
neededWidth, neededHeight: Integer;
r: TRect;
ix: Integer;
begin
Assert(fTempList.Count = fFrameList.Count);
Assert(fCharList.Count = fFrameList.Count);
neededWidth := 0;
for r in fFrameList do begin
neededWidth := Max(neededWidth, r.Width);
end;
neededHeight := fFrameList[fFrameList.Count - 1].Bottom;
SetSize(neededWidth, neededHeight);
Clear(0);
for ix := 0 to fCharList.Count - 1 do begin
x := fFrameList[ix].Left;
y := fFrameList[ix].Top;
fTempList[ix].DrawTo(Self, x, y);
end;
end;
procedure TBitmapFont.AddChar(C: Char; src: TBitmap32);
var
inf: TRect;
tmp: TBitmap32;
begin
if not fLock then
Throw('Bitmapfont must be locked', 'AddChar');
if fFrameList.Count = 0 then begin
inf.Left := 0;
inf.Top := 0;
inf.Right := src.Width;
inf.Bottom := src.Height;
end
else begin
inf.Left := 0;
inf.Top := fFrameList.Last.Bottom;
inf.Right := src.Width;
inf.Bottom := inf.Top + src.Height;
end;
fCharList.Add(C);
fFrameList.Add(inf);
tmp := TBitmap32.Create;
tmp.Assign(src);
fTempList.Add(tmp);
end;
function TBitmapFont.GetCharRect(C: Char): TRect;
var
ix: Integer;
ch: Char;
begin
ix := 0;
for ch in fCharList do begin
if ch = C then
Exit(fFrameList[ix]);
inc(ix);
end;
Result := TRect.Empty;
end;
function TBitmapFont.DrawChar(C: Char; dst: TBitmap32; x, y: Integer): TSize;
var
R: TRect;
begin
R := GetCharRect(C);
if R.IsEmpty then begin
Result.cx := 0;
Result.cy := 0;
end
else begin
Self.DrawTo(dst, x, y);
Result.cx := R.Width;
Result.cy := R.Height;
end;
end;
function TBitmapFont.TextSize(const s: string): TSize;
var
C: Char;
R: TRect;
begin
Result.cx := 0;
Result.cy := 0;
for C in s do begin
R := GetCharRect(C);
Inc(Result.cx, R.Width);
Result.cy := Max(Result.cy, R.Height);
end;
end;
function TBitmapFont.DrawText(const s: string; dst: TBitmap32; x, y: Integer): TSize;
var
C: Char;
SrcRect, DstRect: TRect;
begin
DstRect := Rect(X, Y, X, Y);
for C in s do begin
SrcRect := GetCharRect(C);
DstRect.Width := SrcRect.Width;
DstRect.Height := SrcRect.Height;
Self.DrawTo(Dst, DstRect, SrcRect);
Inc(DstRect.Left, SrcRect.Width);
end;
end;
end.