-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBlendModes.pas
More file actions
604 lines (501 loc) · 18.5 KB
/
BlendModes.pas
File metadata and controls
604 lines (501 loc) · 18.5 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
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
(*
Greenfish Icon Editor Pro
Copyright (c) 2012-13 B. Szalkai
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
unit BlendModes;
interface
uses
LclIntf, LclType, SysUtils, Math, BitmapEx, bmExUtils,
gfMath, Types, ColSpaces;
type
TBlendMode = (bmNormal, bmMask, bmBehind, bmDissolve,
bmHue, bmHueShift, bmSaturation,
bmDarken, bmMultiply, bmColorBurn, bmLinearBurn, bmDarkerColor,
bmLighten, bmScreen, bmColorDodge, bmLinearDodge, bmLighterColor,
bmOverlay, bmSoftLight, bmHardLight,
bmVividLight, bmLinearLight, bmPinLight, bmHardMix,
bmDifference, bmExclusion);
TBlendFunc = procedure(Src, Dest: PByteArray);
procedure bfNormal(Src, Dest: PByteArray);
procedure bfMask(Src, Dest: PByteArray);
procedure bfBehind(Src, Dest: PByteArray);
// Dissolve is a special case, because the blended value depends on X and Y
// procedure bfDissolve;
procedure bfHue(Src, Dest: PByteArray);
procedure bfHueShift(Src, Dest: PByteArray);
procedure bfSaturation(Src, Dest: PByteArray);
procedure bfDarken(Src, Dest: PByteArray);
procedure bfMultiply(Src, Dest: PByteArray);
procedure bfColorBurn(Src, Dest: PByteArray);
procedure bfLinearBurn(Src, Dest: PByteArray);
procedure bfDarkerColor(Src, Dest: PByteArray);
procedure bfLighten(Src, Dest: PByteArray);
procedure bfScreen(Src, Dest: PByteArray);
procedure bfColorDodge(Src, Dest: PByteArray);
procedure bfLinearDodge(Src, Dest: PByteArray);
procedure bfLighterColor(Src, Dest: PByteArray);
procedure bfOverlay(Src, Dest: PByteArray);
procedure bfSoftLight(Src, Dest: PByteArray);
procedure bfHardLight(Src, Dest: PByteArray);
procedure bfVividLight(Src, Dest: PByteArray);
procedure bfLinearLight(Src, Dest: PByteArray);
procedure bfPinLight(Src, Dest: PByteArray);
procedure bfHardMix(Src, Dest: PByteArray);
procedure bfDifference(Src, Dest: PByteArray);
procedure bfExclusion(Src, Dest: PByteArray);
const
BlendFunctions: array[TBlendMode] of TBlendFunc =
(bfNormal, bfMask, bfBehind, nil,
bfHue, bfHueShift, bfSaturation,
bfDarken, bfMultiply, bfColorBurn, bfLinearBurn, bfDarkerColor,
bfLighten, bfScreen, bfColorDodge, bfLinearDodge, bfLighterColor,
bfOverlay, bfSoftLight, bfHardLight,
bfVividLight, bfLinearLight, bfPinLight, bfHardMix,
bfDifference, bfExclusion);
// Accepts 0..255 Opacity values
// Clips Src by rClip, the draws the clipped bitmap to Dest,
// but at position (0, 0)!
procedure DrawBlended(Dest: TBitmap32; rClip: TRect;
Src: TBitmap32; Opacity: integer;
BlendMode: TBlendMode; DestIsClear: PBoolean);
// Accepts 0..255 Opacity values
// Clips Src by r, then erases Dest according to the opacity of the pixels
// in the resulting bitmap, but does not move the eraser bitmap to (0, 0) for Dest
// (as DrawBlended does)!
procedure EraseWith(Dest, Src: TBitmap32; const r: TRect; Opacity: integer);
implementation
procedure bfNormal;
begin
PColor32(Dest)^ := PutPixel32(PColor32(Src)^, PColor32(Dest)^);
end;
procedure bfMask;
begin
if PColor32(Dest)^ = cl32Inverted then
begin
if Src[3] < $80 then PColor32(Dest)^ := cl32Transparent;
end else
begin
// multiply alphas
Dest[3] := (Alpha255to256(Src[3]) * Dest[3]) shr 8;
// avoid getting inverted
if Dest[3] = 0 then PColor32(Dest)^ := cl32Transparent;
end;
end;
procedure bfBehind;
begin
PColor32(Dest)^ := PutPixel32(PColor32(Dest)^, PColor32(Src)^);
end;
// Accepts: bmHue, bmHueShift, bmSaturation
procedure bfHSBRelated(Src, Dest: PByteArray; Mode: TBlendMode); inline;
var
bc: packed array[0..3] of byte;
a1, a2: TInt3;
begin
// TODO: maybe we should get and set only the affected component --
// that would be faster
// gray -> domain error for Hue
if (Mode <> bmSaturation) and (Src[0] = Src[1]) and (Src[0] = Src[2]) then Exit;
// convert both to HSB
a1 := RGBtoHSB(Src[0], Src[1], Src[2]);
a2 := RGBtoHSB(Dest[0], Dest[1], Dest[2]);
// modify
case Mode of
bmHue: a2[0] := a1[0];
bmHueShift: begin
inc(a2[0], a1[0]);
if (a2[0] >= HSBMAX) then dec(a2[0], HSBMAX);
end;
bmSaturation: a2[1] := a1[1];
end;
// convert the modified color back to RGB
a1 := HSBtoRGB(a2[0], a2[1], a2[2]);
// this is the resulting color
bc[0] := a1[0];
bc[1] := a1[1];
bc[2] := a1[2];
bc[3] := Dest[3];
// produce blending color and do blending
PColor32(Dest)^ := BlendColors(PColor32(Dest)^, TColor32(bc), Src[3]);
end;
procedure bfHue;
begin
bfHSBRelated(Src, Dest, bmHue);
end;
procedure bfHueShift;
begin
bfHSBRelated(Src, Dest, bmHueShift);
end;
procedure bfSaturation;
begin
bfHSBRelated(Src, Dest, bmSaturation);
end;
procedure bfDarken;
var
bc: packed array[0..3] of byte;
begin
bc[0] := Min(Src[0], Dest[0]);
bc[1] := Min(Src[1], Dest[1]);
bc[2] := Min(Src[2], Dest[2]);
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfMultiply;
var
bc: packed array[0..3] of byte;
begin
bc[0] := (Alpha255to256(Src[0]) * Dest[0]) shr 8;
bc[1] := (Alpha255to256(Src[1]) * Dest[1]) shr 8;
bc[2] := (Alpha255to256(Src[2]) * Dest[2]) shr 8;
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfColorBurn;
var
bc: packed array[0..3] of byte;
begin
if $ff xor Dest[0] >= Src[0] then bc[0] := 0 else
bc[0] := $ff xor ((Integer($ff xor Dest[0]) shl 8) div Alpha255to256(Src[0]));
if $ff xor Dest[1] >= Src[1] then bc[1] := 0 else
bc[1] := $ff xor ((Integer($ff xor Dest[1]) shl 8) div Alpha255to256(Src[1]));
if $ff xor Dest[2] >= Src[2] then bc[2] := 0 else
bc[2] := $ff xor ((Integer($ff xor Dest[2]) shl 8) div Alpha255to256(Src[2]));
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfLinearBurn;
var
bc: packed array[0..3] of byte;
begin
bc[0] := Max(0, Integer(Src[0]) + Dest[0] - $ff);
bc[1] := Max(0, Integer(Src[1]) + Dest[1] - $ff);
bc[2] := Max(0, Integer(Src[2]) + Dest[2] - $ff);
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfDarkerColor;
begin
if GRAY_COEFF_RED * (Src[0] - Dest[0]) + GRAY_COEFF_GREEN * (Src[1] - Dest[1]) +
GRAY_COEFF_BLUE * (Src[2] - Dest[2]) < 0 then
bfNormal(Src, Dest) else bfBehind(Src, Dest);
end;
procedure bfLighten;
var
bc: packed array[0..3] of byte;
begin
// arithmetics
bc[0] := Max(Src[0], Dest[0]);
bc[1] := Max(Src[1], Dest[1]);
bc[2] := Max(Src[2], Dest[2]);
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfScreen;
var
bc: packed array[0..3] of byte;
begin
// arithmetics
bc[0] := $ff xor ((Alpha255to256($ff xor Src[0]) * ($ff xor Dest[0])) shr 8);
bc[1] := $ff xor ((Alpha255to256($ff xor Src[1]) * ($ff xor Dest[1])) shr 8);
bc[2] := $ff xor ((Alpha255to256($ff xor Src[2]) * ($ff xor Dest[2])) shr 8);
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfColorDodge;
var
bc: packed array[0..3] of byte;
begin
if Dest[0] >= $ff xor Src[0] then bc[0] := $ff else
bc[0] := (Integer(Dest[0]) shl 8) div Alpha255to256($ff xor Src[0]);
if Dest[1] >= $ff xor Src[1] then bc[1] := $ff else
bc[1] := (Integer(Dest[1]) shl 8) div Alpha255to256($ff xor Src[1]);
if Dest[2] >= $ff xor Src[2] then bc[2] := $ff else
bc[2] := (Integer(Dest[2]) shl 8) div Alpha255to256($ff xor Src[2]);
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfLinearDodge;
var
bc: packed array[0..3] of byte;
begin
// arithmetics
bc[0] := Min($ff, Integer(Src[0]) + Dest[0]);
bc[1] := Min($ff, Integer(Src[1]) + Dest[1]);
bc[2] := Min($ff, Integer(Src[2]) + Dest[2]);
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfLighterColor;
begin
if GRAY_COEFF_RED * (Src[0] - Dest[0]) + GRAY_COEFF_GREEN * (Src[1] - Dest[1]) +
GRAY_COEFF_BLUE * (Src[2] - Dest[2]) > 0 then
bfNormal(Src, Dest) else bfBehind(Src, Dest);
end;
procedure bfOverlay;
var
bc: packed array[0..3] of byte;
begin
if Dest[0] < $80 then bc[0] := Min($ff, (Alpha255to256(Dest[0]) * Src[0]) shr 7) else
bc[0] := Max(0, $ff - (Alpha255to256($ff xor Dest[0]) * ($ff xor Src[0])) shr 7);
if Dest[1] < $80 then bc[1] := Min($ff, (Alpha255to256(Dest[1]) * Src[1]) shr 7) else
bc[1] := Max(0, $ff - (Alpha255to256($ff xor Dest[1]) * ($ff xor Src[1])) shr 7);
if Dest[2] < $80 then bc[2] := Min($ff, (Alpha255to256(Dest[2]) * Src[2]) shr 7) else
bc[2] := Max(0, $ff - (Alpha255to256($ff xor Dest[2]) * ($ff xor Src[2])) shr 7);
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
var
SoftLight_DFunction: array[0..255] of word; // 0..256 values
procedure bfSoftLight;
var
i: integer;
bc: packed array[0..3] of byte;
begin
for i := 0 to 2 do
if Src[i] < $80 then
bc[i] := Dest[i] - (Alpha255to256($ff xor (Src[i] shl 1)) *
Alpha255to256($ff xor Dest[i]) * Dest[i]) shr 16 else
bc[i] := Dest[i] + ( ((Integer(Src[i]) shl 1) - $ff) *
SoftLight_DFunction[Dest[i]] ) shr 8;
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfHardLight;
var
bc: packed array[0..3] of byte;
begin
if Src[0] < $80 then bc[0] := Min($ff, (Alpha255to256(Src[0]) * Dest[0]) shr 7) else
bc[0] := Max(0, $ff - (Alpha255to256($ff xor Src[0]) * ($ff xor Dest[0])) shr 7);
if Src[1] < $80 then bc[1] := Min($ff, (Alpha255to256(Src[1]) * Dest[1]) shr 7) else
bc[1] := Max(0, $ff - (Alpha255to256($ff xor Src[1]) * ($ff xor Dest[1])) shr 7);
if Src[2] < $80 then bc[2] := Min($ff, (Alpha255to256(Src[2]) * Dest[2]) shr 7) else
bc[2] := Max(0, $ff - (Alpha255to256($ff xor Src[2]) * ($ff xor Dest[2])) shr 7);
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfVividLight;
var
i: integer;
bc: packed array[0..3] of byte;
begin
for i := 0 to 2 do
if Src[i] < $80 then
begin
// use some color burn
if $ff xor Dest[i] >= Integer(Src[i]) shl 1 then
bc[i] := 0 else
bc[i] := $ff xor (( (Integer($ff xor Dest[i]) shl 8) div Alpha255to256(Src[i]) ) shr 1);
end else
begin
// use some color dodge
if Dest[i] >= Integer($ff xor Src[i]) shl 1 then
bc[i] := $ff else
bc[i] := ( (Integer(Dest[i]) shl 8) div Alpha255to256($ff xor Src[i]) ) shr 1;
end;
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfLinearLight;
var
bc: packed array[0..3] of byte;
begin
bc[0] := Max(0, Min($ff, 2*Integer(Src[0]) + Dest[0] - $ff));
bc[1] := Max(0, Min($ff, 2*Integer(Src[1]) + Dest[1] - $ff));
bc[2] := Max(0, Min($ff, 2*Integer(Src[2]) + Dest[2] - $ff));
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfPinLight;
var
bc: packed array[0..3] of byte;
begin
if Src[0] < $80 then bc[0] := Min(Dest[0], Src[0] shl 1) else
bc[0] := Max(Dest[0], Byte(Src[0] shl 1));
if Src[1] < $80 then bc[1] := Min(Dest[1], Src[1] shl 1) else
bc[1] := Max(Dest[1], Byte(Src[1] shl 1));
if Src[2] < $80 then bc[2] := Min(Dest[2], Src[2] shl 1) else
bc[2] := Max(Dest[2], Byte(Src[2] shl 1));
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfHardMix;
var
bc: packed array[0..3] of byte;
begin
if Src[0] > $ff xor Dest[0] then bc[0] := $ff else bc[0] := 0;
if Src[1] > $ff xor Dest[1] then bc[1] := $ff else bc[1] := 0;
if Src[2] > $ff xor Dest[2] then bc[2] := $ff else bc[2] := 0;
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfDifference;
var
bc: packed array[0..3] of byte;
begin
bc[0] := Abs(Integer(Src[0]) - Dest[0]);
bc[1] := Abs(Integer(Src[1]) - Dest[1]);
bc[2] := Abs(Integer(Src[2]) - Dest[2]);
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
procedure bfExclusion;
var
bc: packed array[0..3] of byte;
begin
bc[0] := Min($ff, Integer(Src[0]) + Dest[0] - (Integer(Src[0]) * Dest[0]) shr 7);
bc[1] := Min($ff, Integer(Src[1]) + Dest[1] - (Integer(Src[1]) * Dest[1]) shr 7);
bc[2] := Min($ff, Integer(Src[2]) + Dest[2] - (Integer(Src[2]) * Dest[2]) shr 7);
bc[3] := Src[3];
// produce blending color and do blending
PColor32(Dest)^ := PutPixel32(BlendColors(PColor32(Src)^, TColor32(bc), Dest[3]), PColor32(Dest)^);
end;
var
DissolveTable: array[byte] of byte; // random values
procedure DrawBlended(Dest: TBitmap32; rClip: TRect;
Src: TBitmap32; Opacity: integer;
BlendMode: TBlendMode; DestIsClear: PBoolean);
var
x, y: integer;
c: packed array[0..3] of byte;
pSrc, pDest: PColor32;
BlendFunc: TBlendFunc;
begin
if Opacity = 0 then
begin
if BlendMode <> bmMask then Exit;
// mask with 0 opacity -> clear image
if Assigned(DestIsClear) and DestIsClear^ then Exit;
Dest.Rectangle(Rect(0, 0, rClip.Width, rClip.Height), cl32Transparent, True, 1);
if Assigned(DestIsClear) then DestIsClear^ := True;
Exit;
end;
rClip.Left := Max(0, rClip.Left);
rClip.Top := Max(0, rClip.Top);
rClip.Right := Min3i(rClip.Left + Dest.Width, Src.Width, rClip.Right);
rClip.Bottom := Min3i(rClip.Top + Dest.Height, Src.Height, rClip.Bottom);
if (rClip.Left >= rClip.Right) or (rClip.Top >= rClip.Bottom) then Exit;
// dest is clear?
if Assigned(DestIsClear) and DestIsClear^ then
begin
// these blend modes do not do anything here
if BlendMode in [bmMask, bmHue, bmHueShift, bmSaturation] then Exit;
// not a mask -> draw
DestIsClear^ := False;
if (Opacity = 255) and (BlendMode <> bmDissolve) then
begin
Dest.CopyRectOverwrite(0, 0, Src, rClip);
Exit;
end;
end;
if (Opacity = 255) and (BlendMode = bmNormal) then
begin
Dest.CopyRect(0, 0, Src, rClip);
Exit;
end;
Opacity := Alpha255to256(Opacity);
BlendFunc := BlendFunctions[BlendMode];
for y := rClip.Top to rClip.Bottom - 1 do
begin
pSrc := Src.PixelAddr(rClip.Left, y);
pDest := Dest.PixelAddr(0, y - rClip.Top);
for x := rClip.Left to rClip.Right - 1 do
begin
// use Opacity
TColor32(c) := pSrc^;
if TColor32(c) = cl32Inverted then
begin
if Opacity < $80 then TColor32(c) := cl32Transparent;
end else
begin
c[3] := (Opacity * c[3]) shr 8;
if c[3] = 0 then TColor32(c) := cl32Transparent;
end;
// blend
if BlendMode = bmDissolve then
begin
if (c[3] = $ff) then pDest^ := TColor32(c) else
// compare pixel value to a coordinate-dependent random value
if (c[3] <> 0) and (DissolveTable[DissolveTable[Byte(x)] xor Byte(y)] < c[3]) then
pDest^ := TColor32(c) or cl32Opaque;
end else
BlendFunc(@c, PByteArray(pDest));
// iterate
inc(pSrc);
inc(pDest);
end; // for x
end; // for y
end;
procedure EraseWith;
var
x, y, xMin, xMax, a: integer;
pSrc, pDest: PByteArray;
begin
Opacity := Alpha255to256(Opacity);
xMin := Max(0, r.Left); xMax := Min3i(Src.Width, Dest.Width, r.Right) - 1;
for y := Max(0, r.Top) to Min3i(Src.Height, Dest.Height, r.Bottom) - 1 do
begin
pSrc := PByteArray(Src.PixelAddr(xMin, y));
pDest := PByteArray(Dest.PixelAddr(xMin, y));
for x := xMin to xMax do
begin
a := $ff xor ((pSrc[3] * Opacity) shr 8);
if PColor32(pDest)^ = cl32Inverted then
begin
if a < $80 then PColor32(pDest)^ := cl32Transparent;
end else
begin
pDest[3] := (pDest[3] * Alpha255to256(a)) shr 8;
if pDest[3] = 0 then PColor32(pDest)^ := cl32Transparent;
end;
// iterate
inc(PColor32(pSrc));
inc(PColor32(pDest));
end;
end;
end;
///////////////////////////////////////////////
procedure InitBlendModes;
var
i: integer;
x: double;
begin
// Dissolve blend mode hash table
for i := 0 to $ff do DissolveTable[i] := Random($100);
// Calculate D(x) - x for all channel values
// This is used by the soft light blend mode
for i := 0 to $ff do
begin
x := i / $ff;
if x <= 0.25 then
SoftLight_DFunction[i] := Round( ((16*x - 12)*x + 3)*x * $100 ) else
SoftLight_DFunction[i] := Round( (Sqrt(x) - x) * $100 );
end;
end;
initialization
InitBlendModes;
end.