-
Notifications
You must be signed in to change notification settings - Fork 34
/
ImageProcess.pas
2384 lines (2323 loc) · 72.8 KB
/
ImageProcess.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 ImageProcess;
interface
uses Windows, Graphics, TypeDefinitions, AmbShadowCalcThreadN, Math3D, ExtCtrls;
type
LPresetRec = packed record
Zpos, LXangle, LYangle: Integer;
end;
PLPresetRec = ^LPresetRec;
//TGetLightMapPixel = function(const x, y: Single; LM: TPLightMap; bSqr: LongBool; WrapAround: Integer): TSVec;
//TGetLightMapPixelSphere = function(const svec: TSVec; SM: TPSMatrix3; LM: TPLightMap; bSqr: LongBool): TSVec;
TCol2SVec = procedure(c: T4Cardinal; sv, svout: TPSVec);
TCol2SVecX87 = function(c: T4Cardinal): T4SVec;
function MakeColPresetGlyph(PresetNr: Integer): TBitmap;
function SetImageSize: LongBool;
procedure UpdateScaledImage(StartYh, EndYh: Integer);
procedure doAA(StartSLfullBMP, StartSLhalfBMP, OffsetFullBMP, OffsetHalfBMP,
Hwidth, Hheight, Scale, Sharpen: Integer);
function CalcAmbShadowT(Header: TPMandHeader10; PsiLight: TPsiLight5; aSLoffset: Integer;
PCTS: TPCalcThreadStats; PATlevel: TPATlevel; cRect: TRect): Boolean;
procedure MakeZbufBMP(var BMP: TBitmap);
procedure CalcLightStrokes(Seed: Integer);
procedure NormalsOnZbuf(Header: TPMandHeader10; PLight: TPsiLight5);
function GetLightMapPixel(const x, y: Single; LM: TPLightMap; bSqr: LongBool; WrapAround: Integer): TSVec;
function GetLightMapPixelSphere(const svec: TSVec; SM: TPSMatrix3; LM: TPLightMap; bSqr: LongBool): TSVec;
function GetLightMapPixelNN(const x, y: Single; LM: TPLightMap; bSqr: LongBool; WrapAround: Integer): TSVec;
function GetLightMapPixelSphereNN(const svec: TSVec; SM: TPSMatrix3; LM: TPLightMap; bSqr: LongBool): TSVec;
function ColToSVecFlipRBc4(c: T4Cardinal): T4SVec;
procedure ColToSVecSSE2(c: T4Cardinal; sv, svout: TPSVec);
procedure MakeLMPreviewImage(image: TImage; LM: TPLightMap);
function ColToSVecFlipRBc416(c: T4Cardinal): T4SVec;
procedure ColToSVecSSE2_16(c: T4Cardinal; sv, svout: TPSVec);
var
ATrousWL: TATlevel;
ATrousWLprevAni: TATlevel;
ATrousWLAni: TATlevel;
LPresetArr: array of LPresetRec;
const
sva1: array[0..3] of Integer = ($FF, $FF, $FF, $FF);
sva16: array[0..3] of Integer = ($FFFF, $FFFF, $FFFF, $FFFF);
implementation
uses Mand, Math, Forms, Controls, Types, LightAdjust, CalcThread, SysUtils,
DivUtils, FileHandling, Interpolation, CalcAmbShadowDE, Tiling, Calc,
HeaderTrafos;
procedure CalcPixel(SL: PCardinal; Zpos: Integer; var P: TLpreset20;
colIndex, dAmbShad: Single; LXangle, LYangle: Integer; dY: Single); //for preview images on buttons
var dTmp, dCtmp, dCtmp4, dTmp3: Single;
ir, iL1, iL2, iL3: Integer;
iAmb, iDif, iSpe, sLiDif, sLiSpe, DepC: TSVec;
dAmb2: Single;
const SpecF: array[0..7] of Single = (1, 1.4142, 2, 2.8284, 4, 5.6568, 8, 11.313);
begin
with P do
begin
SL^ := SwapColor(InterpolateColor(DepthCol, DepthCol2, 1 - dY, dY));
if Zpos < 32768 then
begin
iL2 := 5;
while (iL2 < 10) and (LCols[iL2].Position < colIndex) do Inc(iL2);
while (iL2 > 1) and (LCols[iL2 - 1].Position >= colIndex) do Dec(iL2);
iL1 := iL2 - 1;
ir := LCols[iL2].Position;
if iL2 > 9 then
begin
iL2 := 0;
ir := 32767;
end;
if Lights[3].FreeByte <> 0 then
begin
iSpe := ScaleSVector(ColToSVecNoScale(LCols[iL1].ColorSpe), s1d255);
iDif := ScaleSVector(ColToSVecNoScale(LCols[iL1].ColorDif), s1d255);
end
else
begin
dTmp3 := (colIndex - LCols[iL1].Position) * s1d255 / Max(1, ir - LCols[iL1].Position);
dTmp := s1d255 - dTmp3;
iSpe := AddSVectors(ScaleSVector(ColToSVecNoScale(LCols[iL1].ColorSpe), dTmp),
ScaleSVector(ColToSVecNoScale(LCols[iL2].ColorSpe), dTmp3));
iDif := AddSVectors(ScaleSVector(ColToSVecNoScale(LCols[iL1].ColorDif), dTmp),
ScaleSVector(ColToSVecNoScale(LCols[iL2].ColorDif), dTmp3));
end;
ClearSVec(sLiDif);
ClearSVec(sLiSpe);
dTmp3 := Max0S((Zpos - 20000) * 0.0001 + 1);
dTmp := Max0S(1 - dTmp3);
DepC := ScaleSVector(ColToSVecNoScale(SL^), dTmp);
dTmp3 := dTmp3 * dAmbShad;
dAmb2 := TB578pos[2] * 0.005; //di,Sp,Am
dCtmp4 := TB578pos[0] * 0.005;
dCtmp := TB578pos[1] * 0.005;
ir := 0;
while ir < 4 do
begin
if Lights[ir].Loption = 0 then
begin
iL2 := Lights[ir].LFunction shr 4; //Diff function
dTmp := SpecF[Lights[ir].LFunction and 7];
iL1 := (Round(D7BtoDouble(Lights[ir].LXpos) * M16384dPi * s05) + LXangle) and $7FFF; //if PosLight change LXpos before
iL3 := (Round(D7BtoDouble(Lights[ir].LYpos) * M16384dPi * s05) + LYangle) and $7FFF;
if iL1 > 16383 then iL1 := 32767 - iL1;
if iL3 > 16383 then iL3 := 32767 - iL3;
iL1 := Round(dTmp * iL1);
iL3 := Round(dTmp * iL3);
if (iL1 or iL3) < 16384 then
sLiSpe := AddSVectors(sLiSpe, RGBtoSVecScale(Lights[ir].Lcolor, GetGaussFuncNavi(iL1, iL3) * dCtmp4));
dTmp := GetCosTabValNavi(iL2, Round(D7BtoDouble(Lights[ir].LXpos) * M16384dPi) + LXangle,
Round(D7BtoDouble(Lights[ir].LYpos) * M16384dPi) + LYangle) * dCtmp;
sLiDif := AddSVectors(sLiDif, RGBtoSVecScale(Lights[ir].Lcolor, dTmp));
end;
Inc(ir);
end;
dTmp := 1 - dY;
if dAmbShad > 1 then dAmbShad := 1;
iAmb := AddSVectors(MultiplySVectors(iDif, AddSVectors(sLiDif, ScaleSVector(
AddSVectors(ScaleSVector(ColToSVecNoScale(AmbCol), dTmp),
ScaleSVector(ColToSVecNoScale(AmbCol2), dY)), dAmb2))),
MultiplySVectors(iSpe, sLiSpe));
SL^ := SVecToColNoScale(FlipXZsvec(AddSVectors(ScaleSVector(
MultiplySVectors(iAmb, AddSVecS(ScaleSVector(AddSVectors(iDif,
ScaleSVector(iSpe, s05)), 1 - dAmbShad), dAmbShad)), dTmp3), DepC)));
end;
end;
end;
procedure HybridQuat(var x, y, z, w: Double; PIteration3D: TPIteration3D);
var xt, yt, zt: Double;
begin
with PIteration3D^ do
begin
xt := x;
yt := y;
zt := z;
x := x * x - y * y - z * z - w * w + C1;
y := 2 * (y * xt + z * w) + C2;
z := 2 * (z * xt - yt * w) + C3;
w := 2 * (w * xt + yt * zt);
end;
end;
procedure isMemberQuat(PIteration3D: TPIteration3D);
var X1, X2, X3, X4, Rold: Double;
begin
if SupportSSE2 then
asm
push esi
push edi
push ecx
mov esi, PIteration3D
xor ecx, ecx
mov edi, [esi + 48]
@u: movupd xmm0, [esi] // C1, C2 = X1, X2
movsd xmm1, [esi + 16] // C3, 0 = X3, X4
movapd xmm2, xmm0
movapd xmm3, xmm1
mulpd xmm2, xmm0 // X1*X1, X2*X2
mulpd xmm3, xmm1 // X3*X3, X4*X4
movapd xmm4, xmm2
addpd xmm4, xmm3 // X1*X1 + X3*X3, X2*X2 + X4*X4
pshufd xmm5, xmm4, $4E // X2*X2 + X4*X4, X1*X1 + X3*X3
addsd xmm4, xmm5 // Rout
@a: addsd xmm3, xmm5 // X3*X3 + X2*X2 + X4*X4
movlpd Rold, xmm4
pshufd xmm7, xmm0, $4E // X2, X1
subsd xmm2, xmm3 // X1*X1 - X2*X2 - X3*X3 - X4*X4
movapd xmm5, xmm0 // X1, X2
mulsd xmm7, xmm0 // X2*X1
pshufd xmm6, xmm1, $4E // X4, X3
addsd xmm2, [esi]
movapd xmm3, xmm6 // X4, X3
movapd xmm0, xmm2 // X1 = X1*X1 - X2*X2 - X3*X3 - X4*X4 + C1;
mulpd xmm3, xmm5 // X4*X1, X3*X2
mulsd xmm6, xmm1 // X4*X3
mulpd xmm5, xmm1 // X1*X3, X2*X4
addsd xmm7, xmm6 // X2*X1 + X4*X3
pshufd xmm1, xmm5, $4E // X2*X4, X1*X3
addsd xmm7, xmm7 // 2 * (X2*X1 + X4*X3)
addsd xmm7, [esi + 8] // X2 = 2 * (X2*X1 + X3*X4) + C2
subsd xmm5, xmm1 // X2*X4*O1 + X1*X3 sub
shufpd xmm0, xmm7, 0 // X1, X2
addsd xmm5, xmm5 // 2 * (X2*X4*O1 + X1*X3)
pshufd xmm6, xmm3, $4E // X3*X2, X4*X1
addsd xmm5, [esi + 16] // X3 = 2 * (X2*X4*O1 + X1*X3) + C3
addsd xmm6, xmm3 // X3*X2 + X4*X1
movsd xmm1, xmm5
addsd xmm6, xmm6 // X4 = 2 * (X4*X1 + X3*X2)
shufpd xmm1, xmm6, 0 // X3, X4
movapd xmm2, xmm0
movapd xmm3, xmm1
mulpd xmm2, xmm0 // X1*X1, X2*X2
mulpd xmm3, xmm1 // X3*X3, X4*X4
movapd xmm4, xmm2
addpd xmm4, xmm3 // X1*X1 + X3*X3, X2*X2 + X4*X4
pshufd xmm5, xmm4, $4E //
addsd xmm4, xmm5 // Rout
inc ecx
cmp ecx, [esi + 68]
jge @c
ucomisd xmm4, [edi + 160] //>8?
jb @a
@c: movlpd [esi + 56], xmm4 // Rout = double
mov [esi + 64], ecx // ItResultI
pop ecx
pop edi
pop esi
end
else with PIteration3D^ do
begin
X1 := C1;
X2 := C2;
X3 := C3;
X4 := 0;
ItResultI := 0;
Rout := X1 * X1 + X2 * X2 + X3 * X3;
repeat
Rold := Rout;
HybridQuat(X1, X2, X3, X4, PIteration3D);
Rout := X1 * X1 + X2 * X2 + X3 * X3 + X4 * X4;
Inc(ItResultI);
until
(ItResultI >= maxIt) or (Rout > 8);
end;
with PIteration3D^ do
begin
if Rout <= 1 then SmoothItD := ItResultI
else SmoothItD := ItResultI - Ln(s05 * Ln(Rout)) * 1.4427;
end;
end;
procedure RenderPresetQuat;
var x, y: Integer;
DE, N1, N2, N3: Single;
Iteration3D: TIteration3D;
PRec: PLPresetRec;
begin
SetLength(LPresetArr, 34 * 34);
PRec := @LPresetArr[0];
for y := 0 to 33 do
with Iteration3D do
begin
PVar := PAligned16;
Rstop := 8;
MaxIt := 10;
for x := 1 to 34 do
begin
C1 := x * 0.07 - 1.75;
C2 := y * 0.07 - 1.156;
C3 := -1.5;
DE := 0.055;
repeat
C3 := C3 + DE * s03;
isMemberQuat(@Iteration3D);
DE := Abs(1 / (1 + Exp(SmoothItD - 3))) + 0.02;
until (C3 > s05) or (DE < 0.04);
if C3 > 0.499 then
begin
PRec.Zpos := 32768;
PRec.LYangle := 0;
PRec.LXangle := 0;
end else begin
PRec.Zpos := 32767 - Round((1 - C3) * 9000);
DE := SmoothItD;
C3 := C3 - s0001;
isMemberQuat(@Iteration3D);
N3 := DE - SmoothItD;
C3 := C3 + s0001;
C2 := C2 + s0001;
isMemberQuat(@Iteration3D);
N2 := DE - SmoothItD;
C2 := C2 - s0001;
C1 := C1 - s0001;
isMemberQuat(@Iteration3D);
N1 := SmoothItD - DE;
DE := 1 / Sqrt(N1 * N1 + N2 * N2 + N3 * N3 + s001);
PRec.LYangle := Round(ArcSinSafe(N2 * DE) * 5215.1891752352) and $7FFF;
PRec.LXangle := Round(ArcSinSafe(N1 * DE) * 5215.1891752352) and $7FFF;
end;
Inc(PRec);
end;
end;
end;
procedure RenderLittleImage(var BMP: TBitmap; var P: TLpreset20);
var x, y: Integer;
sl: pCardinal;
amb: Single;
PRec: PLPresetRec;
begin
if Length(LPresetArr) <> 34 * 34 then RenderPresetQuat;
PRec := @LPresetArr[0];
for y := 0 to 33 do
begin
sl := BMP.ScanLine[y];
for x := 1 to 34 do
begin
if PRec.Zpos = 32768 then amb := 0.5 else
amb := 0.6 - (3.3 - ((32767 - PRec.Zpos) * 3.6666e-4));
CalcPixel(sl, PRec.Zpos, P, x * 1058 - 7800, amb, PRec.LXangle,
PRec.LYangle, y * 0.03);
Inc(sl);
Inc(PRec);
end;
end;
end;
function MakeColPresetGlyph(PresetNr: Integer): TBitmap;
var x, y: Integer;
BMP2: TBitmap;
sl1, sl2, sl3: PByte;
P: TLpreset20;
begin
Result := TBitmap.Create;
Result.PixelFormat := pf32bit;
Result.Width := 19;
Result.Height := 17;
Result.Transparent := False;
Result.Canvas.Brush.Color := $FEFDFF;
Result.Canvas.FillRect(Result.Canvas.ClipRect);
BMP2 := TBitmap.Create;
try
BMP2.PixelFormat := pf32bit;
BMP2.Width := 34;
BMP2.Height := 34;
BMP2.Transparent := False;
with BMP2.Canvas do
begin
if Presetnr > 5 then P := CustomPresets[Presetnr]
else P := ConvertColPreset164To20(Presets[Presetnr]);
Brush.Color := 0;
FillRect(ClipRect);
RenderLittleImage(BMP2, P);
for y := 0 to 16 do
begin
sl1 := Result.ScanLine[y];
sl2 := BMP2.ScanLine[y * 2];
sl3 := BMP2.ScanLine[y * 2 + 1];
Inc(sl1, 4);
for x := 1 to 17 do
begin
sl1^ := (sl2^ + PByte(Integer(sl2) + 4)^ + sl3^ + PByte(Integer(sl3) + 4)^) shr 2;
Inc(sl1);
Inc(sl2);
Inc(sl3);
sl1^ := (sl2^ + PByte(Integer(sl2) + 4)^ + sl3^ + PByte(Integer(sl3) + 4)^) shr 2;
Inc(sl1);
Inc(sl2);
Inc(sl3);
sl1^ := (sl2^ + PByte(Integer(sl2) + 4)^ + sl3^ + PByte(Integer(sl3) + 4)^) shr 2;
Inc(sl1, 2);
Inc(sl2, 6);
Inc(sl3, 6);
end;
end;
end;
finally
BMP2.Free;
end;
end;
procedure doAA(StartSLfullBMP, StartSLhalfBMP, OffsetFullBMP, OffsetHalfBMP,
Hwidth, Hheight, Scale, Sharpen: Integer);
var x, y, c, x2, iThr, n: Integer;
sVal, sWei, sSh: Single;
PB1, PB2, PBh: PByte;
Buf: array of Byte;
aLP: array[0..24] of Single;
const cLP2: array[0..16] of Single = (129.447, 80.065, -1.5584, -24.16,
0.793, 12.267, -0.1375, -7.096, -0.2744, 4.324, 0.472, -2.69, -0.526, 1.68,
0.5, -1, -0.4);
cLP3: array[0..24] of Single = (86.299, 70.497, 33.722, -1.0383, -16.482,
-11.906, 0.529, 8.0127, 6.317, -0.091, -4.458, -3.818, -0.183, 2.585, 2.446,
0.3147, -1.512, -1.609, -0.35, 0.875, 1.068, 0.332, -0.493, -0.708, -0.28);
//sharpen filter:
cSP2: array[0..16] of Single = (0.74581218, 0.307845, -0.135913, -0.12739,
0.03, 0.07615, 0.00947, -0.0345, -0.0174, 0.0131, 0.0135, -0.00189, -0.0084, -0.00164,
0.00383, 0.00252, -0.00142);
cSP3: array[0..24] of Single = (0.508389, 0.338648, 0.07211, -0.08756, -0.1117,
-0.04961, 0.0207, 0.05232, 0.0394, -0.0184, -0.0229, -0.0114, 0.0035, 0.0111, 0.0092,
0.0021, -0.0039, -0.0055, -0.003, 0.0005, 0.0026, 0.0024, 0.0007, 0, 0);
begin
Screen.Cursor := crHourGlass;
try
n := Scale * 8;
if Scale = 2 then sSh := Sharpen * 200 / 3 else sSh := Sharpen * 180 / 3;
if Scale = 2 then for y := 0 to 16 do aLP[y] := cLP2[y] + sSh * cSP2[y]
else for y := 0 to 24 do aLP[y] := cLP3[y] + sSh * cSP3[y];
SetLength(Buf, Hwidth * Hheight * 3 * Scale);
for y := 0 to Hheight * Scale - 1 do //first horizontal lowpass
begin
PB1 := PByte(StartSLfullBMP + OffsetFullBMP * y);
PBh := @Buf[y * Hwidth * 3];
for x := 1 to Hwidth do
begin
for c := 0 to 2 do
begin
iThr := Abs(PB1^ - PByte(Integer(PB1) + 4)^); //todo: if scale=3 then take from 1 pixel to the right
if x < Hwidth then
begin
x2 := Abs(PB1^ - PByte(Integer(PB1) + 8)^);
if x2 > iThr then iThr := x2;
if (Scale = 3) and (x < Hwidth - 1) then
begin
x2 := Abs(PB1^ - PByte(Integer(PB1) + 12)^);
if x2 > iThr then iThr := x2;
end;
end;
if x > 1 then
begin
x2 := Abs(PB1^ - PByte(Integer(PB1) - 4)^);
if x2 > iThr then iThr := x2;
x2 := Abs(PB1^ - PByte(Integer(PB1) - 8)^);
if x2 > iThr then iThr := x2;
if (Scale = 3) and (x > 2) then
begin
x2 := Abs(PB1^ - PByte(Integer(PB1) - 12)^);
if x2 > iThr then iThr := x2;
end;
end;
iThr := iThr * 10;
sVal := 0;
sWei := 0;
PB2 := PB1;
for x2 := 0 to Min(n, (Hwidth - x) * Scale) do
begin
if Abs(PB2^ - PB1^) > iThr then Break; //read of adr... imagescale=2
sVal := sVal + aLP[x2] * Integer(PB2^); //(PB2^ * 255.0 + Sqr(Integer(PB2^))); //sqr because of gamma? not really better?
sWei := sWei + aLP[x2];
Inc(PB2, 4);
end;
PB2 := PB1;
for x2 := 1 to Min(n, (x - 1) * Scale) do
begin
Dec(PB2, 4);
if Abs(PB2^ - PB1^) > iThr then Break;
sVal := sVal + aLP[x2] * Integer(PB2^); //(PB2^ * 255.0 + Sqr(Integer(PB2^))); //
sWei := sWei + aLP[x2];
end;
if sWei > 0 then
begin
x2 := Round(sVal / sWei); //Sqrt(sVal / sWei + 16256.25) - 127.5); //
if x2 < 0 then x2 := 0 else if x2 > 255 then x2 := 255;
PBh^ := x2;
end;
Inc(PB1);
Inc(PBh);
end;
Inc(PB1, 4 * Scale - 3);
end;
end;
for x := 0 to Hwidth - 1 do // vertical lowpass
begin
PB1 := @Buf[x * 3];
PBh := PByte(StartSLhalfBMP + x * 4);
for y := 1 to Hheight do
begin
for c := 0 to 2 do
begin
iThr := Abs(PB1^ - PByte(Integer(PB1) + 3 * Hwidth)^);
if y < Hheight then
begin
x2 := Abs(PB1^ - PByte(Integer(PB1) + 6 * Hwidth)^);
if x2 > iThr then iThr := x2;
if (Scale = 3) and (y < Hheight - 1) then
begin
x2 := Abs(PB1^ - PByte(Integer(PB1) + 9 * Hwidth)^);
if x2 > iThr then iThr := x2;
end;
end;
if y > 1 then
begin
x2 := Abs(PB1^ - PByte(Integer(PB1) - 3 * Hwidth)^);
if x2 > iThr then iThr := x2;
x2 := Abs(PB1^ - PByte(Integer(PB1) - 6 * Hwidth)^);
if x2 > iThr then iThr := x2;
if (Scale = 3) and (y > 2) then
begin
x2 := Abs(PB1^ - PByte(Integer(PB1) - 9 * Hwidth)^);
if x2 > iThr then iThr := x2;
end;
end;
iThr := iThr * 10;
sVal := 0;
sWei := 0;
PB2 := PB1;
for x2 := 0 to Min(n, (Hheight - y) * Scale) do
begin
if Abs(PB2^ - PB1^) > iThr then Break;
sVal := sVal + aLP[x2] * Integer(PB2^); //(PB2^ * 255.0 + Sqr(Integer(PB2^))); //
sWei := sWei + aLP[x2];
Inc(PB2, 3 * Hwidth);
end;
PB2 := PB1;
for x2 := 1 to Min(n, (y - 1) * Scale) do
begin
Dec(PB2, 3 * Hwidth);
if Abs(PB2^ - PB1^) > iThr then Break;
sVal := sVal + aLP[x2] * Integer(PB2^); //(PB2^ * 255.0 + Sqr(Integer(PB2^))); //
sWei := sWei + aLP[x2];
end;
if sWei > 0 then
begin
x2 := Round(sVal / sWei); //Sqrt(sVal / sWei + 16256.25) - 127.5); //
if x2 < 0 then x2 := 0 else if x2 > 255 then x2 := 255;
PBh^ := x2;
end;
Inc(PB1);
Inc(PBh);
end;
Inc(PB1, Hwidth * 3 * Scale - 3);
Inc(PBh, OffsetHalfBMP - 3);
end;
end;
SetLength(Buf, 0);
Mand3DForm.Image1.Picture.Bitmap.Modified := True;
Mand3DForm.Image1.Invalidate;
finally
Screen.Cursor := crDefault;
end;
end;
procedure UpdateScaledImage(StartYh, EndYh: Integer);
var a, x, y, y2, wid, w2, itmp: Integer;
PBh, PB1: PByte;
PC1, PC2: PCardinal;
b: Byte;
Buf: array of Cardinal;
begin
if (ImageScale < 1) or (ImageScale > 10) then Exit;
b := Sqr(ImageScale);
a := b div 2;
if EndYh >= Mand3DForm.Image1.Picture.Bitmap.Height then
EndYh := Mand3DForm.Image1.Picture.Bitmap.Height - 1;
{ if Mand3DForm.bCalcTile then
wid := TilingForm.brTileW div ImageScale
else }
wid := Mand3DForm.Image1.Picture.Bitmap.Width;
if (wid < 1) or (wid * Mand3DForm.Image1.Picture.Bitmap.Height * b > Length(fullSizeImage)) then Exit;
if ImageScale > 3 then SetLength(Buf, wid * ImageScale * 3);
for y := StartYh to EndYh do
begin
PB1 := PByte(mFSIstart + mFSIoffset * y * ImageScale);
PBh := PByte(I1BMPstartSL + I1BMPoffset * y);
if ImageScale = 2 then
asm
push eax
push ebx
push ecx
push edx
push edi
push esi
mov ecx, wid
mov esi, PB1
mov edi, PBh
mov ebx, mFSIoffset
@ll: movzx eax, byte ptr [esi]
movzx edx, byte ptr [esi + 4]
add eax, edx
movzx edx, byte ptr [esi + ebx]
add eax, edx
movzx edx, byte ptr [esi + ebx + 4]
lea eax, [eax + edx + 2]
shr eax, 2
mov [edi], al
movzx eax, byte ptr [esi + 1]
movzx edx, byte ptr [esi + 5]
add eax, edx
movzx edx, byte ptr [esi + ebx + 1]
add eax, edx
movzx edx, byte ptr [esi + ebx + 5]
lea eax, [eax + edx + 2]
shr eax, 2
mov [edi + 1], al
movzx eax, byte ptr [esi + 2]
movzx edx, byte ptr [esi + 6]
add eax, edx
movzx edx, byte ptr [esi + ebx + 2]
add eax, edx
movzx edx, byte ptr [esi + ebx + 6]
lea eax, [eax + edx + 2]
shr eax, 2
mov [edi + 2], al
add esi, 8
add edi, 4
dec ecx
jnz @ll
pop esi
pop edi
pop edx
pop ecx
pop ebx
pop eax
end
else if ImageScale = 1 then
begin
PC1 := PCardinal(PB1);
PC2 := PCardinal(PBh);
for x := 1 to wid do
begin
PC2^ := PC1^;
Inc(PC2);
Inc(PC1);
end;
end
else if ImageScale = 3 then
asm
push eax
push ebx
push ecx
push edx
push edi
push esi
mov ecx, wid
mov esi, PB1
mov edi, PBh
mov ebx, mFSIoffset
@ll: movzx eax, byte ptr [esi]
movzx edx, byte ptr [esi + 4]
add eax, edx
movzx edx, byte ptr [esi + 8]
add eax, edx
movzx edx, byte ptr [esi + ebx]
add eax, edx
movzx edx, byte ptr [esi + ebx + 4]
add eax, edx
movzx edx, byte ptr [esi + ebx + 8]
add eax, edx
movzx edx, byte ptr [esi + ebx * 2]
add eax, edx
movzx edx, byte ptr [esi + ebx * 2 + 4]
add eax, edx
movzx edx, byte ptr [esi + ebx * 2 + 8]
lea eax, [eax + edx + 4]
div b
mov [edi], al
movzx eax, byte ptr [esi + 1]
movzx edx, byte ptr [esi + 5]
add eax, edx
movzx edx, byte ptr [esi + 9]
add eax, edx
movzx edx, byte ptr [esi + ebx + 1]
add eax, edx
movzx edx, byte ptr [esi + ebx + 5]
add eax, edx
movzx edx, byte ptr [esi + ebx + 9]
add eax, edx
movzx edx, byte ptr [esi + ebx * 2 + 1]
add eax, edx
movzx edx, byte ptr [esi + ebx * 2 + 5]
add eax, edx
movzx edx, byte ptr [esi + ebx * 2 + 9]
lea eax, [eax + edx + 4]
div b
mov [edi + 1], al
movzx eax, byte ptr [esi + 2]
movzx edx, byte ptr [esi + 6]
add eax, edx
movzx edx, byte ptr [esi + 10]
add eax, edx
movzx edx, byte ptr [esi + ebx + 2]
add eax, edx
movzx edx, byte ptr [esi + ebx + 6]
add eax, edx
movzx edx, byte ptr [esi + ebx + 10]
add eax, edx
movzx edx, byte ptr [esi + ebx * 2 + 2]
add eax, edx
movzx edx, byte ptr [esi + ebx * 2 + 6]
add eax, edx
movzx edx, byte ptr [esi + ebx * 2 + 10]
lea eax, [eax + edx + 4]
div b
mov [edi + 2], al
add esi, 12
add edi, 4
dec ecx
jnz @ll
pop esi
pop edi
pop edx
pop ecx
pop ebx
pop eax
end
else //ImageScale > 3
begin
PC1 := @Buf[0];
asm //sum rows to buf
push eax
push ebx
push ecx
push edx
push edi
push esi
mov ebx, ImageScale
dec ebx
mov y2, ebx
mov edi, PC1
lea eax, ebx * 4 - 1
mov itmp, eax
@@0: mov ecx, wid
mov w2, ecx
mov esi, PB1
mov eax, mFSIoffset
mul y2
add esi, eax
@ll: mov ecx, ebx
movzx eax, byte ptr [esi]
@@1: add esi, 4
movzx edx, byte ptr [esi]
add eax, edx
dec ecx
jnz @@1
mov [edi], eax
sub esi, itmp
mov ecx, ebx
movzx eax, byte ptr [esi]
@@2: add esi, 4
movzx edx, byte ptr [esi]
add eax, edx
dec ecx
jnz @@2
mov [edi + 4], eax
sub esi, itmp
mov ecx, ebx
movzx eax, byte ptr [esi]
@@3: add esi, 4
movzx edx, byte ptr [esi]
add eax, edx
dec ecx
jnz @@3
mov [edi + 8], eax
add edi, 12
add esi, 2
dec w2
jnz @ll
dec y2
jns @@0
pop esi
pop edi
pop edx
pop ecx
pop ebx
pop eax
end;
PBh := PByte(I1BMPstartSL + I1BMPoffset * y);
asm //sum columns
push eax
push ebx
push ecx
push edx
push edi
push esi
mov eax, ImageScale
dec eax
mov y2, eax
mov edx, PC1
mov ebx, wid
mov w2, ebx
shl ebx, 2
lea ebx, ebx * 2 + ebx
mov edi, PBh
@ll: mov ecx, y2
mov esi, edx
mov eax, [esi]
@@1: add esi, ebx
add eax, [esi]
dec ecx
jnz @@1
add eax, a
div b
mov [edi], al
add edx, 4
mov ecx, y2
mov esi, edx
mov eax, [esi]
@@2: add esi, ebx
add eax, [esi]
dec ecx
jnz @@2
add eax, a
div b
mov [edi + 1], al
add edx, 4
mov ecx, y2
mov esi, edx
mov eax, [esi]
@@3: add esi, ebx
add eax, [esi]
dec ecx
jnz @@3
add eax, a
div b
mov [edi + 2], al
add edx, 4
add edi, 4
dec w2
jnz @ll
pop esi
pop edi
pop edx
pop ecx
pop ebx
pop eax
end;
end;
end;
Mand3DForm.Image1.Picture.Bitmap.Modified := True;
SetLength(Buf, 0);
end;
procedure MakeZbufBMP(var BMP: TBitmap);
var x, y, xx, yy, wid, i, scale, add, add2, ww, ystart, xstart: Integer;
PB: PByte;
PW, PW2, PW3: PWord;
TileRect: TRect;
Crop: Integer;
begin
scale := Max(2, Min(10, ImageScale));
add := 16 * Sqr(scale);
if Mand3DForm.MHeader.TilingOptions <> 0 then
begin
GetTilingInfosFromHeader(@Mand3DForm.MHeader, TileRect, Crop);
ystart := Crop div ImageScale;
xstart := Crop;
add2 := (TileRect.Right - TileRect.Left + 1) * 9;
ww := (TileRect.Right - TileRect.Left + 1) * ImageScale; //ok?
end else begin
ystart := 0;
xstart := 0;
add2 := Mand3DForm.MHeader.Width * 9;
ww := Mand3DForm.MHeader.Width * ImageScale;
end;
wid := BMP.Width;
for y := 0 to BMP.Height - 1 do
begin
PB := BMP.ScanLine[y];
PW := @Mand3DForm.siLight5[(y + ystart) * ww + xstart];
Inc(PW, 4); //Zpos word
if ImageScale = 1 then
begin
for x := 1 to wid do
begin
if PW^ > 32767 then PB^ := 0 else PB^ := PW^ div 137 + 16;
Inc(PB);
Inc(PW, 9);
end;
end else
begin
for x := 1 to wid do
begin
PW2 := PW;
i := 0;
for yy := 1 to scale do
begin
PW3 := PW2;
for xx := 1 to scale do
begin
if PW3^ < 32768 then i := i + PW3^ + add;
Inc(PW3, 9);
end;
Inc(PW2, add2);
end;
Inc(PW, 9 * scale);
PB^ := i div (Sqr(scale) * 137);
Inc(PB);
end;
end
end;
end;
function SetImageSize: LongBool;
var w, h, OrigScale: Integer;
success: LongBool;
TileSize: TPoint;
begin
with Mand3DForm do
begin
Result := False;
OrigScale := ImageScale;
Image1.Picture.Bitmap.PixelFormat := pf32bit;
repeat
ImageScale := Max(1, Min(10, ImageScale));
if MHeader.TilingOptions <> 0 then
begin
TileSize := GetTileSize(@MHeader);
w := TileSize.X; //TilingForm.brTileW;
h := TileSize.Y; //TilingForm.brTileH;
end else begin
w := MHeader.Width;
h := MHeader.Height;
end;
success := True;
try
Image1.Picture.Bitmap.Width := w div ImageScale;
Image1.Picture.Bitmap.Height := h div ImageScale;
getI1BMPSLs;
if Length(fullSizeImage) <> w * h then
begin
SetLength(fullSizeImage, w * h);
mFSIstart := Integer(@fullSizeImage[0]);
end;
Result := OrigScale = ImageScale;
except
success := ImageScale < 10;
ImageScale := Min(10, ImageScale + 1);
end;
until success;
SpeedButton35.Caption := '1:' + IntToStr(ImageScale);
UpDown1.Position := 11 - ImageScale;
mFSIoffset := w * 4;
DrawRect := Rect(MHeader.Width, MHeader.Height, 0, 0);
end;
end;
{function CalcBGShadowT(Header: TPMandHeader10; PsiLight: TPsiLight5;
PCTS: TPCalcThreadStats; PBGlevel: TPBGlevel): Boolean;
var BGSparameter: TBGSparameter; // + OffsetM200
ThreadCount, x: Integer;
BGSThread: array[1..4] of TBGshadowThread;
const
cTCstep: array[1..4] of Integer = (14, 7, 4, 3);
begin
Result := False;
PCTS.cCalcTime := GetTickCount;
ThreadCount := Min(4, Mand3DForm.SpinEdit1.Value);
BGSparameter.bgsWidth := Header.Width;
BGSparameter.bgsHeight := Header.Height;
BGSparameter.bgsZHfactor := Abs(Header.dZstart - Header.dZend) *
Header.dZoom * Header.Width / 3000;
BGSparameter.bgsPsiLight := PsiLight;
BGSparameter.bgsLEnd := 0;
BGSparameter.BGSoffset := 0;//Header.BGSoffset * 200;
BGSparameter.bgsPCTS := PCTS;
BGSparameter.PBGlevel := PBGlevel;
for x := 1 to ThreadCount do
begin
BGSparameter.bgsThreadID := x;
BGSparameter.bgsLStart := BGSparameter.bgsLEnd + 1;
BGSparameter.bgsLEnd := Min(15, BGSparameter.bgsLStart + cTCstep[ThreadCount]);
try
BGSThread[x] := TBGshadowThread.Create(True);
BGSThread[x].FreeOnTerminate := True;
BGSThread[x].BGSparameter := BGSparameter;
BGSThread[x].Priority := cTPrio[Header.iThreadPriority];
PCTS.iActualYpos[x] := 0;
PCTS.isActive[x] := 1;
except
ThreadCount := x - 1;
if ThreadCount > 0 then
BGSThread[ThreadCount].BGSparameter.bgsLEnd := 15;
Break;
end;
end;
PCTS.iTotalThreadCount := ThreadCount;
for x := 1 to ThreadCount do BGSThread[x].Resume;
Result := ThreadCount > 0;
end; }
function CalcAmbShadowT(Header: TPMandHeader10; PsiLight: TPsiLight5; aSLoffset: Integer;
PCTS: TPCalcThreadStats; PATlevel: TPATlevel; cRect: TRect): Boolean;
var x, y, MWidth, MHeight, ThreadCount: Integer;
ASCparameter: TASCparameter;
Zcorr, ZcMul, ZZstmitDif: Double;
ascThread: array of TAmbShadowCalc;
ascThreadT0: array of TAmbShadowCalcT0;
bT0calc: LongBool;
begin
if (Header.bCalcAmbShadowAutomatic and 12) = 12 then
begin
Result := CalcAmbShadowDET(Header, PCTS, PsiLight, aSLoffset, cRect);
Exit;
end;
Result := False;
MWidth := Header.Width;
MHeight := Header.Height;
ThreadCount := Min(Mand3DForm.UpDown3.Position, MHeight);
bT0calc := (Header.bCalcAmbShadowAutomatic and 2) > 0;
if bT0calc then SetLength(ascThreadT0, ThreadCount)
else SetLength(ascThread, ThreadCount);
CalcPPZvals(Header^, Zcorr, ZcMul, ZZstmitDif);
PCTS.cCalcTime := GetTickCount;
ASCparameter.aZScaleFactor := (Sqr(256 / ZcMul + 1) - 1) / Zcorr;
ASCparameter.aPsiLight := PsiLight;
ASCparameter.aZRThreshold := Header.sAmbShadowThreshold;
ASCparameter.aATlevelCount := BuildATlevels(Integer(PsiLight), MWidth, MHeight, PATlevel, ASCparameter.aCorrMul, ASCparameter.aZsub);
if ASCparameter.aATlevelCount < 1 then
begin
Mand3DForm.OutMessage('Could not build wavelet levels, no ambient shadow calced.');
Exit;
end;