-
Notifications
You must be signed in to change notification settings - Fork 4
/
DSE_Bitmap.pas
1968 lines (1700 loc) · 49.9 KB
/
DSE_Bitmap.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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit DSE_Bitmap;
interface
uses
Windows, Messages, vcl.Forms, Classes, vcl.StdCtrls, vcl.Graphics, vcl.Controls, Contnrs, SysUtils, vcl.extctrls, DSE_defs ;
type
SE_Bitmap = class;
SE_Bitmap = class
private
fBmpName: string;
fBitmap: TBitmap;
fAlpha : double;
fWidth, fHeight: integer;
function AllocateImage: boolean;
function GetPixel(x, y: integer): Tcolor;
function GetPixel24(x, y: integer): TRGB;
function GetPPixel24(x, y: integer): PRGB;
procedure SetPixel(x, y: integer; value: TRGB);
procedure SetPixel24(x, y: integer; Value: TRGB);
procedure DestroyBitmapScanlines;
procedure CreateBitmapScanlines;
function GetMemory(): pointer;
procedure CopyToTBitmap(Dest: TBitmap);
procedure CopyBitmap(Source, Dest: TBitmap);
function GetRow(Row: integer): pointer;
protected
function GetCanvas: TCanvas;
procedure SetAlpha(Value: double);
procedure SetWidth(Value: integer);
procedure SetHeight(Value: integer);
function GetScanLine(Row: integer): pointer;
procedure Render24(dbitmapscanline: ppointerarray; var ABitmap: SE_Bitmap; XLUT, YLUT: pinteger; xSrc, ySrc: integer; xDst, yDst: integer; cx1, cy1, cx2, cy2: integer; rx, ry: integer); virtual;
procedure SetBitmap(bmp: TBitmap);
public
fbitmapAlpha: TBitmap;
fBitmapScanlines: ppointerarray;
fdata: pointer;
info: TBitmapInfo;
DIB_SectionHandle: HBITMAP;
fRowLen: integer;
BlendMode: SE_BlendMode;
constructor Create(); overload;
constructor Create(aWidth, aHeight: integer; aColor:Tcolor); overload;
constructor Create(aWidth, aHeight: integer); overload;
constructor Create(const FileName: string); overload;
constructor Create(image: SE_Bitmap); overload;
constructor Create(image: SE_Bitmap; Rect: TRect); overload;
constructor Create(image: TBitmap; Rect: TRect); overload;
destructor Destroy; override;
procedure TakeTBitmap(aBitmap: TBitmap);
function LoadFromStreamBMP(Stream: TStream): TBitmap;
function LoadFromFileBMP(const FileName: WideString): TBitmap;
property BmpName: string read FBmpName write FBmpName;
property Alpha: double read fAlpha write SetAlpha;
property Width: integer read fWidth write SetWidth;
property Height: integer read fHeight write SetHeight;
procedure Assign(Source: TObject); // for SE_Bitmap and TBitmap
procedure AssignImage(Source: SE_Bitmap); // assign without alpha channel
property ScanLine[Row: integer]: pointer read GetScanLine;
procedure UpdateFromTBitmap;
function GetSegment(Row: integer; Col: integer; Width: integer): pointer;
property Memory: pointer read GetMemory;
property TBitmapScanlines: ppointerarray read fBitmapScanlines;
property Canvas: TCanvas read GetCanvas;
function Allocate(aWidth, aHeight: integer): boolean;
procedure FreeImage;
procedure CopyFromTBitmap(Source: TBitmap);
procedure CopyRectTo(DestBitmap: SE_Bitmap; SrcX, SrcY, DstX, DstY: integer; RectWidth, RectHeight: integer; Transparent: boolean;wTrans: integer); overload;
// procedure CopyRectTo(DestBitmap: SE_Bitmap; DstX, DstY: integer; RectSource: Trect); overload;
procedure Resize(NewWidth, NewHeight: integer; BackgroundColor: double);
procedure InternalRender(ABitmap: SE_Bitmap; var ABitmapScanline: ppointerarray;
xDst, yDst, dxDst, dyDst: integer; xSrc, ySrc, dxSrc, dySrc: integer);
procedure Flip(Direction: TFlipDirection);
procedure Rotate(Angle: double); overload;
function Rotate(Angle: integer): SE_Bitmap; overload;
procedure Stretch ( NewWidth, Newheight : integer );
procedure GrayScale;
procedure Blend(var src: PRGB; var dst: PRGB; BlendMode: SE_BlendMode; var PixAlpha: PRGB);
property Pixel[x, y: integer]: TColor read GetPixel;
property Pixel24[x, y: integer]: TRGB read GetPixel24 write SetPixel24;
property PPixel24[x, y: integer]: PRGB read GetPPixel24;
procedure Fill(Value: double); overload;
procedure FillRect(x1, y1, x2, y2: integer; Value: double);
property Bitmap: TBitmap read fBitmap write SetBitmap;
end;
procedure BMPReadStream(fs: TStream; Bitmap: SE_Bitmap);
function GetNextZoomValue(CurrentZoom: double; bZoomIn: boolean; SuggestedZoom: double) : double;
function BmpRowLen(Width: integer): int64;
implementation
uses math, DSE_misc, DSE_Theater ;
var
SE_CosineTab: array[0..255] of integer;
{$R-}
function BmpRowLen(Width: integer): int64;
begin
result := (((Width * 24) + 31) shr 5) shl 2; // row byte length
// result := (Width*3);
// result := (((Width * 24) + (24 - 1)) div 24) * 3;
// result := (((Width * 32) + (32 - 1)) div 32) * 4;
end;
procedure Stretch(BitsPerPixel: integer; dest: pbyte; dxDst, dyDst, xSrc, ySrc, dxSrc, dySrc: integer; src: pbyte; srcWidth, srcHeight: integer; fx1, fy1, fx2, fy2: integer);
var
rx, ry, sy: integer;
y2, x2: integer;
x, y: integer;
px1, px2, px3: pbyte;
destRowlen, srcRowLen: integer;
ffx1, ffy1, ffx2, ffy2: integer;
zx, zy: double;
arx, arxp: pinteger;
begin
if (dxDst < 1) or (dyDst < 1) then
exit;
destRowlen := (((DxDst * 24) + (24 - 1)) div 24) * 3;
zeromemory(dest, destRowlen * dyDst);
srcRowLen := (((srcWidth * 24) + (24 - 1)) div 24) * 3;
ry := trunc((dySrc / dyDst) * 16384);
rx := trunc((dxSrc / dxDst) * 16384);
y2 := dyDst - 1;
x2 := dxDst - 1;
zx := dxDst / dxSrc;
zy := dyDst / dySrc;
ffy1 := imax(trunc(zy * (fy1 - ySrc)), 0);
ffx1 := imax(trunc(zx * (fx1 - xSrc)), 0);
ffy2 := imin(trunc(zy * (fy2 - ySrc + 1)), y2);
ffx2 := imin(trunc(zx * (fx2 - xSrc + 1)), x2);
if (ffx2-ffx1+1)<=0 then
exit;
zeromemory(dest, (((DxDst * 24) + (24 - 1)) div 24) * 3);
getmem(arx, sizeof(integer) * (ffx2 - ffx1 + 1));
arxp := arx;
for x := ffx1 to ffx2 do begin
arxp^ := ((rx * x) shr 14) + xSrc;
inc(arxp);
end;
for y := ffy1 to ffy2 do begin
px2 := pbyte(uint64(dest) + (dyDst - y - 1) * destRowlen);
sy := imin( SrcHeight-1, ((ry * y) shr 14) + ySrc );
px1 := pbyte(uint64(src) + (srcHeight - sy - 1) * srcRowlen);
arxp := arx;
for x := ffx1 to ffx2 do begin
pbytearray(px2)^[x] := pbytearray(px1)^[arxp^];
inc(arxp);
end;
end;
freemem(arx);
end;
constructor SE_Bitmap.Create();
var
i: integer;
begin
inherited;
fWidth := 0;
fHeight := 0;
fBitmap := nil;
fBitmapScanlines := nil;
for i := 0 to 255 do
SE_CosineTab[i] := Round(64 - Cos(i * Pi / 255) * 64);
end;
constructor SE_Bitmap.Create(aWidth, aHeight: integer);
begin
Create();
Allocate(aWidth, aHeight);
end;
constructor SE_Bitmap.Create(aWidth, aHeight: integer; aColor: TColor);
begin
Create();
Allocate(aWidth, aHeight);
Bitmap.Canvas.Brush.Color := aColor;
Bitmap.Canvas.Brush.Style := bsSolid;
Bitmap.Canvas.FillRect(rect(0,0,bitmap.Width,bitmap.Height));
end;
constructor SE_Bitmap.Create(const FileName: string);
begin
Create();
LoadFromFileBMP(filename);
FBmpName:= Filename;
end;
constructor SE_Bitmap.Create(image: SE_Bitmap);
begin
Create();
Assign(image);
end;
constructor SE_Bitmap.Create(image: SE_Bitmap; Rect: TRect);
begin
Create();
Allocate(Rect.Right - Rect.Left + 1, Rect.Bottom - Rect.Top + 1);
image.CopyRectTo(self, Rect.Left, Rect.Top, 0, 0, Width, Height,false,0);
end;
constructor SE_Bitmap.Create(image: TBitmap; Rect: TRect);
var
bmp: SE_Bitmap;
begin
Create();
Allocate(Rect.Right - Rect.Left + 1, Rect.Bottom - Rect.Top + 1);
bmp := SE_Bitmap.Create();
try
bmp.CopyFromTBitmap (image);
bmp.CopyRectTo(self, Rect.Left, Rect.Top, 0, 0, Width, Height,false,0);
finally
bmp.Free();
end;
end;
procedure SE_Bitmap.UpdateFromTBitmap;
begin
if assigned(fBitmap) then
if (fWidth <> fBitmap.Width) or (fHeight <> fBitmap.Height) then
begin
fWidth := fBitmap.Width;
fHeight := fBitmap.Height;
fRowLen := BmpRowLen(fWidth);
CreateBitmapScanlines;
end;
if (fHeight > 0) and assigned(fBitmapScanlines) and (fBitmapScanlines[0] <> fBitmap.Scanline[0]) then
CreateBitmapScanlines;
end;
destructor SE_Bitmap.Destroy;
begin
FreeImage;
inherited;
end;
procedure SE_Bitmap.DestroyBitmapScanlines;
begin
if fBitmapScanlines <> nil then
freemem(fBitmapScanlines);
fBitmapScanlines := nil;
end;
procedure SE_Bitmap.CreateBitmapScanlines;
var
i: integer;
begin
DestroyBitmapScanlines;
if assigned(fBitmap) then begin
getmem(fBitmapScanlines, (sizeof(pointer) * fHeight) );
for i := 0 to fHeight - 1 do begin
fBitmapScanlines[i] := fBitmap.Scanline[i];
end;
end;
end;
procedure SE_Bitmap.SetAlpha(Value: double);
begin
fAlpha := value;
end;
procedure SE_Bitmap.SetWidth(Value: integer);
begin
if Value <> fWidth then begin
if fBitmap = nil then fBitmap := TBitmap.Create;
fBitmap.Width := Value;
fWidth := fBitmap.Width;
fRowLen := BmpRowLen(fWidth);
CreateBitmapScanlines;
end;
end;
procedure SE_Bitmap.SetHeight(Value: integer);
begin
if Value <> fHeight then begin
if fBitmap = nil then fBitmap := TBitmap.Create;
fBitmap.Height := Value;
fHeight := fBitmap.Height;
CreateBitmapScanlines;
end;
end;
procedure DoAlignAfter(Bitmap: SE_Bitmap; OldWidth, OldHeight: integer; BackgroundColor: double );
begin
if Bitmap.Width > OldWidth then
begin
Bitmap.FillRect(OldWidth, 0, Bitmap.Width - 1, Bitmap.Height - 1, BackgroundColor);
end;
if Bitmap.Height > OldHeight then
begin
Bitmap.FillRect(0, OldHeight, Bitmap.Width - 1, Bitmap.Height - 1, BackgroundColor);
end;
end;
procedure SE_Bitmap.Resize(NewWidth, NewHeight: integer; BackgroundColor: double);
var
lw, lh: integer;
begin
lw := Width;
lh := Height;
SetWidth(NewWidth);
SetHeight(NewHeight);
DoAlignAfter(self, lw, lh, BackgroundColor);
end;
procedure SE_Bitmap.Stretch ( NewWidth, Newheight : integer );
var
Dst: SE_Bitmap;
x, y: Integer;
zx, zy: Double;
sxarr: array of Integer;
dst_rgb: PRGB;
src_rgb: PRGBROW;
begin
dst:= SE_Bitmap.Create (NewWidth,Newheight);
dst.Bitmap.PixelFormat := pf24bit;
// StretchBlt(dst.Bitmap.Canvas.Handle,0,0,NewWidth,Newheight, fbitmap.Canvas.Handle ,0,0,fbitmap.Width,fbitmap.Height,SRCCOPY );
zx := fbitmap.Width / NewWidth;
zy := fbitmap.Height / Newheight;
SetLength(sxarr, NewWidth);
for x := 0 to NewWidth - 1 do
sxarr[x] := trunc(x * zx);
for y := 0 to Newheight - 1 do begin
src_rgb := fbitmap.Scanline[trunc(y * zy)];
dst_rgb := Dst.Scanline[y];
for x := 0 to Dst.Width - 1 do begin
dst_rgb^ := src_rgb[sxarr[x]];
inc(dst_rgb);
end;
end;
// sxarr := nil;
AssignImage(dst);
FreeAndNil(dst);
end;
procedure SE_Bitmap.Rotate(Angle: double);
var
dst: SE_Bitmap;
parx1, parx2: pinteger;
a, tsin, tcos, cxSrc, cySrc, cxDest, cyDest: Double;
fx, fy: Integer;
dw, dh, x, y: Integer;
px: pbyte;
arx1, arx2: pintegerarray;
ary1, ary2: Integer;
ps, pd: pbyte;
dw1, dh1: Integer;
prgb_s, prgb_d: PRGB;
srcrows: ppointerarray;
iangle: Integer;
aTRGB:trgb;
wtrans: double;
procedure Rot90(inv: Boolean);
var
x, y: Integer;
mulx, muly, addx, addy: Integer;
begin
dw := height; dw1 := dw-1;
dh := Width; dh1 := dh-1;
dst:= SE_Bitmap.Create (dw,dh);
aTRGB:= Pixel24 [0,0];
wtrans:= RGB2TColor (aTRGB.b, aTRGB.g , aTRGB.r) ;
dst.Fill(wtrans);
if inv then begin
mulx := -1;
muly := 1;
addx := dw1;
addy := 0;
end
else begin
mulx := 1;
muly := -1;
addx := 0;
addy := dh1;
end;
for x := 0 to dw1 do begin
ps := ScanLine[addx+x*mulx];
prgb_s := PRGB(ps);
for y := 0 to dh1 do begin
prgb_d := dst.Scanline[addy+y*muly];
inc(prgb_d, x);
prgb_d^ := prgb_s^;
inc(prgb_s);
end;
end;
end;
procedure Rot180;
var
x, y: Integer;
begin
dw := width; dw1 := dw-1;
dh := height; dh1 := dh-1;
dst:= SE_Bitmap.Create (dw,dh);
aTRGB:= Pixel24 [0,0];
wtrans:= RGB2TColor (aTRGB.b, aTRGB.g , aTRGB.r) ;
dst.Fill(wtrans);
for y := 0 to dh1 do begin
pd := dst.ScanLine[dh1 - y];
ps := Scanline[y];
prgb_d := PRGB(pd);
prgb_s := PRGB(ps);
inc(prgb_s, dw1);
for x := 0 to dw1 do begin
prgb_d^ := prgb_s^;
inc(prgb_d);
dec(prgb_s);
end;
end;
end;
begin
if (Frac(angle) = 0) and ((trunc(angle) mod 90) = 0) then
begin
iangle := trunc(angle) mod 360;
case iangle of
90 : Rot90(false);
180 : Rot180;
270 : Rot90(true);
-90 : Rot90(true);
-180 : Rot180;
-270 : Rot90(false);
end;
AssignImage(dst);
FreeAndNil(dst);
exit;
end;
a := angle * pi / 180;
dw := round(abs(width * cos(a)) + abs(height * sin(a)));
dh := round(abs(width * sin(a)) + abs(height * cos(a)));
dw1 := dw-1;
dh1 := dh-1;
{ TODO -cbug : verificare bug fill color non clblack }
dst:= SE_Bitmap.Create (dw,dh);
aTRGB:= Pixel24 [0,0];
wtrans:= RGB2TColor (aTRGB.b, aTRGB.g , aTRGB.r) ;
dst.Fill(wtrans);
tsin := sin(a);
tcos := cos(a);
cxSrc := (Width - 1) / 2;
cySrc := (Height - 1) / 2;
cxDest := (dst.Width - 1) / 2;
cyDest := (dst.Height - 1) / 2;
getmem(arx1, sizeof(integer) * dst.Width);
getmem(arx2, sizeof(integer) * dst.Width);
for x := 0 to dst.Width - 1 do begin
arx1[x] := round( cxSrc + (x - cxDest) * tcos );
arx2[x] := round( cySrc + (x - cxDest) * tsin );
end;
// per := 100 / (dst.Height);
getmem(srcrows, height*sizeof(pointer));
for y := 0 to height-1 do
srcrows[y] := GetRow(y);
for y := 0 to dh1 do begin
px := dst.Scanline[y];
ary1 := round( (y - cyDest) * tsin );
ary2 := round( (y - cyDest) * tcos );
parx1 := @arx1[0];
parx2 := @arx2[0];
prgb_d := prgb(px);
for x := 0 to dw1 do begin
fx := parx1^ - ary1;
if (fx >= 0) and (fx < width )then begin
fy := parx2^ + ary2;
if (fy >= 0) and (fy < height) then begin
prgb_s := srcrows[fy];
inc(prgb_s, fx);
prgb_d^ := prgb_s^;
end;
end;
inc(prgb_d);
inc(parx1);
inc(parx2);
end;
end;
freemem(srcrows);
freemem(arx1);
freemem(arx2);
AssignImage(dst);
FreeAndNil(dst);
end;
function SE_Bitmap.Rotate(Angle: integer): SE_Bitmap;
var
dst: SE_Bitmap;
parx1, parx2: pinteger;
a, tsin, tcos, cxSrc, cySrc, cxDest, cyDest: Double;
fx, fy: Integer;
dw, dh, x, y: Integer;
px: pbyte;
arx1, arx2: pintegerarray;
ary1, ary2: Integer;
ps, pd: pbyte;
dw1, dh1: Integer;
prgb_s, prgb_d: PRGB;
srcrows: ppointerarray;
iangle: Integer;
aTRGB:trgb;
wtrans: double;
procedure Rot90(inv: Boolean);
var
x, y: Integer;
mulx, muly, addx, addy: Integer;
begin
dw := height; dw1 := dw-1;
dh := Width; dh1 := dh-1;
dst:= SE_Bitmap.Create (dw,dh);
aTRGB:= Pixel24 [0,0];
wtrans:= RGB2TColor (aTRGB.b, aTRGB.g , aTRGB.r) ;
dst.Fill(wtrans);
if inv then begin
mulx := -1;
muly := 1;
addx := dw1;
addy := 0;
end
else begin
mulx := 1;
muly := -1;
addx := 0;
addy := dh1;
end;
for x := 0 to dw1 do begin
ps := ScanLine[addx+x*mulx];
prgb_s := PRGB(ps);
for y := 0 to dh1 do begin
prgb_d := dst.Scanline[addy+y*muly];
inc(prgb_d, x);
prgb_d^ := prgb_s^;
inc(prgb_s);
end;
end;
end;
procedure Rot180;
var
x, y: Integer;
begin
dw := width; dw1 := dw-1;
dh := height; dh1 := dh-1;
dst:= SE_Bitmap.Create (dw,dh);
aTRGB:= Pixel24 [0,0];
wtrans:= RGB2TColor (aTRGB.b, aTRGB.g , aTRGB.r) ;
dst.Fill(wtrans);
for y := 0 to dh1 do begin
pd := dst.ScanLine[dh1 - y];
ps := Scanline[y];
prgb_d := PRGB(pd);
prgb_s := PRGB(ps);
inc(prgb_s, dw1);
for x := 0 to dw1 do begin
prgb_d^ := prgb_s^;
inc(prgb_d);
dec(prgb_s);
end;
end;
end;
begin
if (Frac(angle) = 0) and ((trunc(angle) mod 90) = 0) then begin
iangle := trunc(angle) mod 360;
case iangle of
90 : Rot90(false);
180 : Rot180;
270 : Rot90(true);
-90 : Rot90(true);
-180 : Rot180;
-270 : Rot90(false);
end;
AssignImage(dst);
FreeAndNil(dst);
exit;
end;
a := angle * pi / 180;
dw := round(abs(width * cos(a)) + abs(height * sin(a)));
dh := round(abs(width * sin(a)) + abs(height * cos(a)));
dw1 := dw-1;
dh1 := dh-1;
{ TODO -cbug : verificare bug fill color non clblack }
dst:= SE_Bitmap.Create (dw,dh);
aTRGB:= Pixel24 [0,0];
wtrans:= RGB2TColor (aTRGB.b, aTRGB.g , aTRGB.r) ;
dst.Fill(wtrans);
tsin := sin(a);
tcos := cos(a);
cxSrc := (Width - 1) / 2;
cySrc := (Height - 1) / 2;
cxDest := (dst.Width - 1) / 2;
cyDest := (dst.Height - 1) / 2;
getmem(arx1, sizeof(integer) * dst.Width);
getmem(arx2, sizeof(integer) * dst.Width);
for x := 0 to dst.Width - 1 do begin
arx1[x] := round( cxSrc + (x - cxDest) * tcos );
arx2[x] := round( cySrc + (x - cxDest) * tsin );
end;
getmem(srcrows, height*sizeof(pointer));
for y := 0 to height-1 do
srcrows[y] := GetRow(y);
for y := 0 to dh1 do begin
px := dst.Scanline[y];
ary1 := round( (y - cyDest) * tsin );
ary2 := round( (y - cyDest) * tcos );
parx1 := @arx1[0];
parx2 := @arx2[0];
prgb_d := prgb(px);
for x := 0 to dw1 do begin
fx := parx1^ - ary1;
if (fx >= 0) and (fx < width )then begin
fy := parx2^ + ary2;
if (fy >= 0) and (fy < height) then begin
prgb_s := srcrows[fy];
inc(prgb_s, fx);
prgb_d^ := prgb_s^;
end;
end;
inc(prgb_d);
inc(parx1);
inc(parx2);
end;
end;
freemem(srcrows);
freemem(arx1);
freemem(arx2);
Result := dst;
end;
procedure SE_Bitmap.Flip(Direction: TFlipDirection);
var
x, y, w, h: Integer;
newbitmap: SE_Bitmap;
newpx, oldpx: PRGB;
begin
newbitmap := SE_Bitmap.create;
newbitmap.Allocate(Width, Height);
w := width - 1;
h := height - 1;
case direction of
FlipH:
for y := 0 to h do begin
newpx := newbitmap.ScanLine[y];
oldpx := ScanLine[y];
inc(oldpx, w);
for x := 0 to w do begin
newpx^ := oldpx^;
inc(newpx);
dec(oldpx);
end;
end;
FlipV:
for y := 0 to h do
copymemory(newbitmap.scanline[y], scanline[h - y], 3 * Width)
end;
AssignImage(newbitmap);
FreeAndNil(newbitmap);
end;
procedure SE_Bitmap.FreeImage;
begin
DestroyBitmapScanlines;
if fBitmap <> nil then FreeAndNil(fBitmap);
fBitmap := nil;
fWidth := 0;
fHeight := 0;
fRowlen := 0;
end;
function SE_Bitmap.AllocateImage: boolean;
begin
result := false;
if (fWidth > 0) and (fHeight > 0) then begin
fRowLen := BmpRowLen(fWidth);
fBitmap := TBitmap.Create;
fBitmap.Width := fWidth;
fBitmap.Height := fHeight;
fBitmap.PixelFormat := pf24bit;
CreateBitmapScanlines;
result := true;
end;
end;
function SE_Bitmap.Allocate(aWidth, aHeight: integer): boolean;
begin
if fbitmap <> nil then fBitmap.PixelFormat := pf24bit;
FreeImage;
fWidth := aWidth;
fHeight := aHeight;
result := AllocateImage;
if result=false then begin
fWidth := 0;
fHeight := 0;
end;
end;
procedure SE_Bitmap.Assign(Source: TObject);
var
src: SE_Bitmap;
row, mi: integer;
begin
if Source is SE_Bitmap then begin
src := Source as SE_Bitmap;
fWidth := src.fWidth;
fHeight := src.fHeight;
if fBitmap = nil then
fBitmap := TBitmap.Create;
fBitmap.Width := 4;
fBitmap.Height := 4;
fbitmap.PixelFormat := pf24bit;
fBitmap.Width := fWidth;
fBitmap.Height := fHeight;
fRowLen := BmpRowLen(fWidth);
CreateBitmapScanlines;
mi := imin(fRowLen, src.fRowLen);
for row := 0 to fHeight - 1 do
copymemory(ScanLine[row], src.ScanLine[row], mi);
end
else
if Source is TBitmap then
CopyFromTBitmap(Source as TBitmap);
end;
procedure SE_Bitmap.AssignImage(Source: SE_Bitmap);
var
row, mi: integer;
begin
fWidth := Source.fWidth;
fHeight := Source.fHeight;
if fBitmap = nil then
fBitmap := TBitmap.Create;
fBitmap.Width := fWidth;
fBitmap.Height := fHeight;
fBitmap.PixelFormat := pf24bit;
fRowLen := BmpRowLen(fWidth);
CreateBitmapScanlines;
mi := imin(fRowLen, Source.fRowLen);
for row := 0 to fHeight - 1 do
CopyMemory(ScanLine[row], Source.ScanLine[row], mi);
end;
function SE_Bitmap.GetSegment(Row: integer; Col: integer; Width: integer): pointer;
begin
result := Scanline[Row];
inc(pbyte(result), Col * 3);
end;
function SE_Bitmap.GetScanLine(Row: integer): pointer;
begin
result := fBitmapScanlines[row];
end;
function SE_Bitmap.GetRow(Row: integer): pointer;
begin
result := fBitmapScanlines[row];
end;
procedure SE_Bitmap.GrayScale;
var
x, y,v: integer;
ppx: PRGB;
begin
for y := 0 to height - 1 do begin
ppx := ScanLine[y];
for x := 0 to Width -1 do begin
with ppx^ do begin
v := (r * 21 + g * 71 + b * 8) div 100;
r := v;
g := v;
b := v;
end;
inc(ppx);
end;
end;
end;
procedure SE_Bitmap.CopyToTBitmap(Dest: TBitmap);
var
row, mi: integer;
begin
Dest.Width := fWidth;
Dest.Height := fHeight;
Dest.PixelFormat := pf24bit;
mi := imin(fRowLen, BmpRowLen(Dest.Width));
for row := 0 to fHeight - 1 do
CopyMemory(Dest.Scanline[row], Scanline[row], mi);
end;
procedure SE_Bitmap.CopyRectTo(DestBitmap: SE_Bitmap; SrcX, SrcY, DstX, DstY: integer; RectWidth, RectHeight: integer; Transparent: boolean;wTrans: integer);
var
y,x: integer;
ps, pd: pbyte;
rl: integer;
ppRGBCurrentFrame,ppVirtualRGB,ppRGBCurrentFrameAlpha: pRGB;
aTrgb: Trgb;
label skip;
begin
if Transparent then begin
aTRGB:= DSE_Misc.TColor2TRGB (wtrans);
SrcX:=0; // arbitrario
SrcY:=0;
// DstX:= DrawingRect.Left ;
// DstY:= DrawingRect.Top;
// RectWidth:=fbitmap.width;
// rectheight:=fbitmap.height;
if DstX < 0 then begin
inc(SrcX, -DstX);
dec(RectWidth, -DstX);
DstX := 0;
end;
if DstY < 0 then begin
inc(SrcY, -DstY);
dec(RectHeight, -DstY);
DstY := 0;
end;
DstX := imin(DstX, DestBitmap.Width - 1);
DstY := imin(DstY, DestBitmap.Height - 1);
SrcX := imin(imax(SrcX, 0), fbitmap.Width - 1);
SrcY := imin(imax(SrcY, 0), fbitmap.Height - 1);
// if SrcX + RectWidth > Width then //begin
// exit;
// if SrcY + RectHeight > Height then //begin
// exit;
if SrcX + RectWidth > fbitmap.Width then
RectWidth := fbitmap.Width - SrcX;
if SrcY + RectHeight > fbitmap.Height then
RectHeight := fbitmap.Height - SrcY;
if DstX + RectWidth > DestBitmap.Width then
RectWidth := DestBitmap.Width - DstX;
if DstY + RectHeight > DestBitmap.Height then
RectHeight := DestBitmap.Height - DstY;
for y := 0 to RectHeight - 1 do begin
ppRGBCurrentFrame := GetSegment(SrcY + y, SrcX, RectWidth);
if Alpha <> 0 then begin
ppRGBCurrentFrameAlpha := fBitmapAlpha.Scanline[SrcY + y];
inc(pbyte(ppRGBCurrentFrameAlpha), SrcX * 3);
end;
ppVirtualRGB := DestBitmap.GetSegment(DstY + y, DstX, RectWidth);
for x := SrcX to SrcX + RectWidth - 1 do begin
// if not((ppRGBCurrentFrame.b = aTRGB.b) and (ppRGBCurrentFrame.g = aTRGB.g) and (ppRGBCurrentFrame.r = aTRGB.r)) then begin
if (ppRGBCurrentFrame.b <> aTRGB.b) or (ppRGBCurrentFrame.g <> aTRGB.g) or (ppRGBCurrentFrame.r <> aTRGB.r) then begin
if fAlpha <> 0 then begin
if (ppRGBCurrentFrameAlpha.B <> 255) and (ppRGBCurrentFrameAlpha.G <> 255) and (ppRGBCurrentFrameAlpha.R <> 255) then begin
// se non è 0 in alpha
// Blend ( ppRGBCurrentFrame,ppVirtualRGB,SE_BlendAlpha,0); ; // trovare alpha AND ????
Blend ( ppRGBCurrentFrame,ppVirtualRGB,SE_BlendAlpha,ppRGBCurrentFrameAlpha ); ; // trovare alpha AND ????
end
else begin // se è 0 in alpha
if BlendMode = SE_BlendNormal then begin
ppVirtualRGB.b := ppRGBCurrentFrame.b;
ppVirtualRGB.g:= ppRGBCurrentFrame.g;
ppVirtualRGB.r:= ppRGBCurrentFrame.r;
end
else begin
Blend ( ppRGBCurrentFrame,ppVirtualRGB,BlendMode,ppRGBCurrentFrame); ;
end;
end;
end
else begin
if BlendMode = SE_BlendNormal then begin
ppVirtualRGB.b := ppRGBCurrentFrame.b;
ppVirtualRGB.g:= ppRGBCurrentFrame.g;
ppVirtualRGB.r:= ppRGBCurrentFrame.r;
end
else begin
Blend ( ppRGBCurrentFrame,ppVirtualRGB,BlendMode,ppRGBCurrentFrame); ;
end;
end;
end;
SKIP:
if fAlpha <> 0 then inc(pbyte(ppRGBCurrentFrameAlpha),3);
inc(pbyte(ppRGBCurrentFrame),3);
inc(pbyte(ppVirtualRGB),3);
end;
end;
end
else begin // non transparent
// RectWidth:=fbitmap.width;
// rectheight:=fbitmap.height;
if DstX < 0 then begin
inc(SrcX, -DstX);
dec(RectWidth, -DstX);
DstX := 0;
end;
if DstY < 0 then begin
inc(SrcY, -DstY);
dec(RectHeight, -DstY);
DstY := 0;
end;
DstX := imin(DstX, DestBitmap.Width - 1);
DstY := imin(DstY, DestBitmap.Height - 1);
SrcX := imin(imax(SrcX, 0), Width - 1);
SrcY := imin(imax(SrcY, 0), Height - 1);
// if SrcX + RectWidth > Width then //begin
// exit;
// if SrcY + RectHeight > Height then //begin
// exit;
// end;
if SrcX + RectWidth > Width then