-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobe.c
3201 lines (3042 loc) · 136 KB
/
Globe.c
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
#define WIN32_LEAN_AND_MEAN // discard rarely used components from Windows header
#include <SDL.h>
#include <SDL_main.h> // only include this one in the source file with main()!
#include <stdio.h>
#include <windows.h>
#include <mathimf.h>
#include <c-hashmap.h>
#include <stdlib.h>
#include <winhttp.h>
#include <turbojpeg.h>
#include <miniLZO.h>
#include <direct.h>
#pragma warning( disable : 4996 4244 ) // "safe" print functions, int-float-double conversion
//#define DEBUG
#ifdef DEBUG
#define LOG(x) printf x
#else
#define LOG(x)
#endif
#define _Atomic volatile
int WIDTH = 720;
int HEIGHT = 720;
const int rasterTileSize = 256;
const char idFormat[] = "%d/%d/%d";
const unsigned char zero3[] = { '\0', '\0', '\0' };
const long double PI = 3.141592653589793238462643383279L;
const long double PIHalf = 1.570796326794896619231321691639L;
const long double PIDouble = 6.28318530717958647692528676655L;
// approximations, within 1 period length, precision is not bad but it is not good enough for the sizes and calculations involved here
/*
long double sinl(long double t) {
long double s = t < 0.0L ? -1.0L : 1.0L;
t = fabsl(t);
return s * (t < 0.4292L ?
t : (
t < (PI - 0.4292L) ?
(1.0L - (PI / 4.74701335L) * (PI / 4.74701335L) * (t - PIHalf) * (t - PIHalf)) : (
t < (PI + 0.4292L) ?
(PI - t) : (
t < (PIDouble - 0.4292L) ?
-(1.0L - (PI / 4.74701335L) * (PI / 4.74701335L) * (t - (3.0L * PIHalf)) * (t - (3.0L * PIHalf))) :
(-PIDouble + t)
)
)
)
);
}
long double cosl(long double t) {
t = fabsl(t);
return t < (PIHalf - 0.4292L) ?
(1.0L - (PI / 4.74701335L) * (PI / 4.74701335L) * t * t) : (
t < (PIHalf + 0.4292L) ?
(PIHalf - t) : (
t < (3.0L * PIHalf - 0.4292L) ?
-(1.0L - (PI / 4.74701335L) * (PI / 4.74701335L) * (t - PI) * (t - PI)) : (
t < (3.0L * PIHalf + 0.4292L) ?
(-3.0L * PIHalf + t) : (1.0L - (PI / 4.74701335L) * (PI / 4.74701335L) * (t - PIDouble) * (t - PIDouble))
)
)
);
}
long double asinl(long double s) {
return s < -0.4292L ? -PIHalf + 1.511L * sqrtl(1.0L + s) : (s < 0.4292L ? s : (PIHalf - 1.511L * sqrtl(1.0L - s)));
}
long double acosl(long double s) {
return s < -0.4292L ? PI - 1.511L * sqrtl(1.0L + s) : (s < 0.4292L ? PIHalf - s : (1.511L * sqrtl(1.0L - s)));
}
*/
int centerX, centerY;
long double rScale;
long double rScaleSqr;
long double phiLeft = 0.0L;
long double axisTilt = 0.0L;
typedef struct PT
{
long double p;
long double t;
} pt;
pt at(int x, int y)
{
int xC = x - centerX;
int yC = y - centerY;
long double r2Sqr = rScaleSqr - xC * xC;
if (yC * yC <= r2Sqr)
{
if (r2Sqr > 0.0L)
{
long double r2Sqrt = sqrtl(r2Sqr);
long double yC2 = yC / r2Sqrt;
long double cAT = cosl(axisTilt);
long double ySpace = r2Sqrt * (yC2 * cAT - copysignl(sqrtl((1.0L - yC2 * yC2) * (1.0L - cAT * cAT)), axisTilt)); // == r2Sqrt * sinl(asinl(yC / r2Sqrt) - axisTilt);
long double r2ScAT = r2Sqrt * cAT;
long double r3Sqr = rScaleSqr - ySpace * ySpace;
pt value;
value.p = fmodl(phiLeft + (((axisTilt > 0.0L && -yC > r2ScAT) || (axisTilt < 0.0L && yC > r2ScAT)) ? -1.0L : 1.0L) * (r3Sqr > 0.0L ? sqrtl(r3Sqr) > abs(xC) ? acosl(-xC / sqrtl(r3Sqr)) : (PIHalf + copysignl(PIHalf, xC)) : PIHalf) + PIDouble, PIDouble);
value.t = asinl(ySpace / rScale);
return value;
}
else
{
pt value;
value.p = fmodl(phiLeft + (xC > 0 ? PI : 0.0L) + PIDouble, PIDouble);
value.t = 0.0L;
return value;
}
}
pt value;
value.p = 0.0L;
value.t = 2.0L;
return value;
}
pt getOffsetsFrom(int x, int y, pt sc) {
int xC = x - centerX;
int yC = y - centerY;
long double r2Sqr = rScaleSqr - xC * xC;
if (yC * yC <= r2Sqr)
{
if (r2Sqr > 0.0L)
{
long double r2Sqrt = sqrtl(r2Sqr);
long double yC2 = yC / r2Sqrt;
long double sT = sinl(sc.t);
long double arg2Sqr = rScaleSqr * sT * sT / r2Sqr;
long double arg1Co = 1.0L - yC2 * yC2;
long double axisTilt = asinl(yC2 * sqrtl(1.0L - arg2Sqr) - copysignl(sqrtl(arg2Sqr * arg1Co), sc.t)); // == asinl(yC2) - asinl(rScale * sinl(sc.t) / r2Sqrt);
long double cAT = cosl(axisTilt);
long double r2ScAT = r2Sqrt * cAT;
long double ySpace = r2Sqrt * (yC2 * cAT - copysignl(sqrtl(arg1Co * (1.0L - cAT * cAT)), axisTilt));
long double r3Sqr = rScaleSqr - ySpace * ySpace;
pt value;
value.p = fmodl((sc.p - (((axisTilt > 0.0L && -yC > r2ScAT) || (axisTilt < 0.0L && yC > r2ScAT)) ? -1.0L : 1.0L) * (r3Sqr > 0.0L ? sqrtl(r3Sqr) > abs(xC) ? acosl(-xC / sqrtl(r3Sqr)) : (PIHalf + copysignl(PIHalf, xC)) : PIHalf)) + PIDouble, PIDouble);
value.t = axisTilt;
return value;
}
else {
pt value;
value.p = fmodl(sc.p - (xC > 0 ? PI : 0.0L) + PIDouble, PIDouble);
value.t = 0.0L;
return value;
}
}
pt value;
value.p = 0.0L;
value.t = 2.0L;
return value;
}
pt getOffsetsFromWithRadius(int x, int y, pt sc, long double rScale) {
int xC = x - centerX;
int yC = y - centerY;
long double rScaleSqr = rScale * rScale;
long double r2Sqr = rScaleSqr - xC * xC;
if (yC * yC <= r2Sqr)
{
if (r2Sqr > 0.0L)
{
long double r2Sqrt = sqrtl(r2Sqr);
long double yC2 = yC / r2Sqrt;
long double sT = sinl(sc.t);
long double arg2Sqr = rScaleSqr * sT * sT / r2Sqr;
long double arg1Co = 1.0L - yC2 * yC2;
long double axisTilt = asinl(yC2 * sqrtl(1.0L - arg2Sqr) - copysignl(sqrtl(arg2Sqr * arg1Co), sc.t)); // == asinl(yC2) - asinl(rScale * sinl(sc.t) / r2Sqrt);
long double cAT = cosl(axisTilt);
long double r2ScAT = r2Sqrt * cAT;
long double ySpace = r2Sqrt * (yC2 * cAT - copysignl(sqrtl(arg1Co * (1.0L - cAT * cAT)), axisTilt));
long double r3Sqr = rScaleSqr - ySpace * ySpace;
pt value;
value.p = fmodl((sc.p - (((axisTilt > 0.0L && -yC > r2ScAT) || (axisTilt < 0.0L && yC > r2ScAT)) ? -1.0L : 1.0L) * (r3Sqr > 0.0L ? sqrtl(r3Sqr) > abs(xC) ? acosl(-xC / sqrtl(r3Sqr)) : (PIHalf + copysignl(PIHalf, xC)) : PIHalf)) + PIDouble, PIDouble);
value.t = axisTilt;
return value;
}
else {
pt value;
value.p = fmodl(sc.p - (xC > 0 ? PI : 0.0L) + PIDouble, PIDouble);
value.t = 0.0L;
return value;
}
}
pt value;
value.p = 0.0L;
value.t = 2.0L;
return value;
}
float zoomF;
int zoom;
long double stepSize;
void determineZoom() {
pt middleLeft = at(0, HEIGHT / 2);
float newZoom;
long double newStepSize;
if (middleLeft.t == 2.0L) {
newZoom = log2l(rScale / rasterTileSize); // == log2l(2 * rScale * 2 / rasterTileSize);
newZoom += 2.0F; // == above continued
newStepSize = PIDouble / 48.0L;
} else {
long double deltaP = at(WIDTH - 1, HEIGHT / 2).p - middleLeft.p;
deltaP += (deltaP <= 0.0L ? PIDouble : 0.0L);
newZoom = log2l((WIDTH / deltaP) * PIDouble / rasterTileSize);
newStepSize = deltaP / 16.0L;
}
if (newZoom >= 0.0F && newZoom <= 30.0F) {
zoomF = newZoom;
zoom = (int)ceilf(newZoom);
stepSize = newStepSize;
}
}
const double PID = 3.141592653589793238462643383279;
const double PIHalfD = 1.570796326794896619231321691639;
const double PIDoubleD = 6.28318530717958647692528676655;
/*
double sin(double t) {
double s = t < 0.0 ? -1.0 : 1.0;
t = fabs(t);
return s * (t < 0.4292 ?
t : (
t < (PID - 0.4292) ?
(1.0 - (PID / 4.74701335) * (PID / 4.74701335) * (t - PIHalfD) * (t - PIHalfD)) : (
t < (PID + 0.4292) ?
(PID - t) : (
t < (PIDoubleD - 0.4292) ?
-(1.0 - (PID / 4.74701335) * (PID / 4.74701335) * (t - (3.0 * PIHalfD)) * (t - (3.0 * PIHalfD))) :
(-PIDoubleD + t)
)
)
)
);
}
double cos(double t) {
t = fabs(t);
return t < (PIHalfD - 0.4292) ?
(1.0 - (PID / 4.74701335) * (PID / 4.74701335) * t * t) : (
t < (PIHalfD + 0.4292) ?
(PIHalfD - t) : (
t < (3.0 * PIHalfD - 0.4292) ?
-(1.0 - (PID / 4.74701335) * (PID / 4.74701335) * (t - PID) * (t - PID)) : (
t < (3.0 * PIHalfD + 0.4292) ?
(-3.0 * PIHalfD + t) : (1.0 - (PID / 4.74701335) * (PID / 4.74701335) * (t - PIDoubleD) * (t - PIDoubleD))
)
)
);
}
double asin(double s) {
return s < -0.4292 ? -PIHalfD + 1.511 * sqrt(1.0 + s) : (s < 0.4292 ? s : (PIHalfD - 1.511 * sqrt(1.0 - s)));
}
double acos(double s) {
return s < -0.4292 ? PID - 1.511 * sqrt(1.0 + s) : (s < 0.4292 ? PIHalfD - s : (1.511 * sqrt(1.0 - s)));
}
*/
double rScaleD;
double rScaleSqrD;
double phiLeftD = 0.0;
double axisTiltD = 0.0;
typedef struct PTD
{
double p;
double t;
} ptD;
ptD atD(int x, int y)
{
int xC = x - centerX;
int yC = y - centerY;
double r2Sqr = rScaleSqrD - xC * xC;
if (yC * yC <= r2Sqr)
{
if (r2Sqr > 0.0)
{
double r2Sqrt = sqrt(r2Sqr);
double yC2 = yC / r2Sqrt;
double cAT = cos(axisTiltD);
double ySpace = r2Sqrt * (yC2 * cAT - copysign(sqrt((1.0 - yC2 * yC2) * (1.0 - cAT * cAT)), axisTiltD)); // == r2Sqrt * sin(asin(yC / r2Sqrt) - axisTiltD);
double r2ScAT = r2Sqrt * cAT;
double r3Sqr = rScaleSqrD - ySpace * ySpace;
ptD value;
value.p = fmod(phiLeftD + (((axisTiltD > 0.0 && -yC > r2ScAT) || (axisTiltD < 0.0 && yC > r2ScAT)) ? -1.0 : 1.0) * (r3Sqr > 0.0 ? sqrt(r3Sqr) > abs(xC) ? acos(-xC / sqrt(r3Sqr)) : (PIHalfD + copysign(PIHalfD, xC)) : PIHalfD) + PIDoubleD, PIDoubleD);
value.t = asin(ySpace / rScaleD);
return value;
}
else
{
ptD value;
value.p = fmod(phiLeftD + (xC > 0 ? PID : 0.0) + PIDoubleD, PIDoubleD);
value.t = 0.0;
return value;
}
}
ptD value;
value.p = 0.0;
value.t = 2.0;
return value;
}
const float PIF = 3.141592653589793238462643383279F;
const float PIHalfF = 1.570796326794896619231321691639F;
const float PIDoubleF = 6.28318530717958647692528676655F;
/*
float sinf(float t) {
float s = t < 0.0F ? -1.0F : 1.0F;
t = fabsf(t);
return s * (t < 0.4292F ?
t : (
t < (PIF - 0.4292F) ?
(1.0F - (PIF / 4.74701335F) * (PIF / 4.74701335F) * (t - PIHalfF) * (t - PIHalfF)) : (
t < (PIF + 0.4292F) ?
(PIF - t) : (
t < (PIDoubleF - 0.4292F) ?
-(1.0F - (PIF / 4.74701335F) * (PIF / 4.74701335F) * (t - (3.0F * PIHalfF)) * (t - (3.0F * PIHalfF))) :
(-PIDoubleF + t)
)
)
)
);
}
float cosf(float t) {
t = fabsf(t);
return t < (PIHalfF - 0.4292F) ?
(1.0F - (PIF / 4.74701335F) * (PIF / 4.74701335F) * t * t) : (
t < (PIHalfF + 0.4292F) ?
(PIHalfF - t) : (
t < (3.0F * PIHalfF - 0.4292F) ?
-(1.0F - (PIF / 4.74701335F) * (PIF / 4.74701335F) * (t - PIF) * (t - PIF)) : (
t < (3.0F * PIHalfF + 0.4292F) ?
(-3.0F * PIHalfF + t) : (1.0F - (PIF / 4.74701335F) * (PIF / 4.74701335F) * (t - PIDoubleF) * (t - PIDoubleF))
)
)
);
}
float asinf(float s) {
return s < -0.4292F ? -PIHalfF + 1.511F * sqrtf(1.0F + s) : (s < 0.4292F ? s : (PIHalfF - 1.511F * sqrtf(1.0F - s)));
}
float acosf(float s) {
return s < -0.4292F ? PIF - 1.511F * sqrtf(1.0F + s) : (s < 0.4292F ? PIHalfF - s : (1.511F * sqrtf(1.0F - s)));
}
*/
float rScaleF;
float rScaleSqrF;
float phiLeftF = 0.0F;
float axisTiltF = 0.0F;
typedef struct PTF
{
float p;
float t;
} ptF;
ptF atF(int x, int y)
{
int xC = x - centerX;
int yC = y - centerY;
float r2Sqr = rScaleSqrF - xC * xC;
if (yC * yC <= r2Sqr)
{
if (r2Sqr > 0.0F)
{
float r2Sqrt = sqrtf(r2Sqr);
float yC2 = yC / r2Sqrt;
float cAT = cosf(axisTiltF);
float ySpace = r2Sqrt * (yC2 * cAT - copysignf(sqrtf((1.0F - yC2 * yC2) * (1.0F - cAT * cAT)), axisTiltF)); // == r2Sqrt * sinf(asinf(yC / r2Sqrt) - axisTiltF);
float r2ScAT = r2Sqrt * cAT;
float r3Sqr = rScaleSqrF - ySpace * ySpace;
ptF value;
value.p = fmodf(phiLeftF + (((axisTiltF > 0.0F && -yC > r2ScAT) || (axisTiltF < 0.0F && yC > r2ScAT)) ? -1.0F : 1.0F) * (r3Sqr > 0.0F ? sqrtf(r3Sqr) > abs(xC) ? acosf(-xC / sqrtf(r3Sqr)) : (PIHalfF + copysignf(PIHalfF, xC)) : PIHalfF) + PIDoubleF, PIDoubleF);
value.t = asinf(ySpace / rScaleF);
return value;
}
else
{
ptF value;
value.p = fmodf(phiLeftF + (xC > 0 ? PIF : 0.0F) + PIDoubleF, PIDoubleF);
value.t = 0.0F;
return value;
}
}
ptF value;
value.p = 0.0F;
value.t = 2.0F;
return value;
}
ptF atFWithoutOffsets(float x, float y)
{
float xC = x - centerX;
float yC = y - centerY;
float r2Sqr = rScaleSqrF - xC * xC;
float yC2 = yC * yC;
if (yC2 <= r2Sqr)
{
if (r2Sqr > 0.0F)
{
float r3Sqr = rScaleSqrF - yC2;
ptF value;
value.p = r3Sqr > 0.0F ? sqrtf(r3Sqr) > fabsf(xC) ? acosf(-xC / sqrtf(r3Sqr)) : (PIHalfF + copysignf(PIHalfF, xC)) : PIHalfF;
value.t = asinf(yC / rScaleF);
return value;
}
else
{
ptF value;
value.p = xC > 0.0F ? PIF : 0.0F;
value.t = 0.0F;
return value;
}
}
ptF value;
value.p = 0.0F;
value.t = 2.0F;
return value;
}
// apparently at zoomLevel around before 30, float cannot store the values involved correctly anymore
void determineZoomF() {
ptF middleLeft = atF(0, HEIGHT / 2);
float newZoom;
float newStepSize;
if (middleLeft.t == 2.0F) {
newZoom = log2f(rScaleF / rasterTileSize); // == log2f(2 * rScaleF * 2 / rasterTileSize);
newZoom += 2.0F; // == above continued
newStepSize = PIDoubleF / 48.0F;
}
else {
float deltaP = atF(WIDTH - 1, HEIGHT / 2).p - middleLeft.p;
deltaP += (deltaP <= 0.0F ? PIDoubleF : 0.0F);
newZoom = log2f((WIDTH / deltaP) * PIDoubleF / rasterTileSize);
newStepSize = deltaP / 16.0F;
}
if (newZoom >= 0.0F && newZoom <= 30.0F) {
zoomF = newZoom;
zoom = (int)ceilf(newZoom);
stepSize = newStepSize; // use non-float stepSize on purpose
}
}
typedef struct QueueFillLevel {
unsigned long long count;
} queueFillLevel;
void measurePresence(void* key, size_t ksize, uintptr_t value, void* usr) {
((queueFillLevel*)usr)->count += (unsigned long long)value;
}
_Atomic int allImagesRequestedPresent;
hashmap* imgPresent;
hashmap* imgRequested;
typedef struct AsyncId {
char* id;
int idLength;
HINTERNET hRequest, hConnect, hSession;
unsigned char* buffer;
int bytesRead;
} asyncId;
_Atomic int notquitrequested;
char* cachePath;
size_t cachePathLength;
void onImageLoading(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength) {
asyncId* aId = (asyncId*)dwContext;
if (notquitrequested) {
if (dwInternetStatus == WINHTTP_CALLBACK_STATUS_READ_COMPLETE) {
if (dwStatusInformationLength > 0) {
aId->bytesRead += dwStatusInformationLength;
int numberBytesToRead = rasterTileSize * rasterTileSize * 3 - aId->bytesRead;
if (numberBytesToRead > 0) {
WinHttpReadData(aId->hRequest, aId->buffer + aId->bytesRead, numberBytesToRead, NULL);
return;
}
}
else {
tjhandle tjInstance = tj3Init(TJINIT_DECOMPRESS);
if (tjInstance != NULL) {
if (tj3DecompressHeader(tjInstance, aId->buffer, aId->bytesRead) == 0) {
unsigned char* pixels = malloc(TJSCALED(tj3Get(tjInstance, TJPARAM_JPEGWIDTH), TJUNSCALED) * TJSCALED(tj3Get(tjInstance, TJPARAM_JPEGHEIGHT), TJUNSCALED) * tjPixelSize[TJPF_RGB]); // malloced length expectation == rasterTileSize * rasterTileSize * 3
if (pixels != NULL) {
if (tj3Decompress8(tjInstance, aId->buffer, aId->bytesRead, pixels, 0, TJPF_RGB) == 0) {
hashmap_set(imgPresent, aId->id, aId->idLength, (uintptr_t)pixels);
}
else {
free(pixels);
}
}
}
tj3Destroy(tjInstance);
}
}
hashmap_set(imgRequested, (void*)(aId->id), aId->idLength, (uintptr_t)0);
queueFillLevel counts;
counts.count = 0;
hashmap_iterate(imgRequested, measurePresence, (void*)&counts);
if (counts.count == 0) {
allImagesRequestedPresent = 1;
}
char* cacheFilePath = malloc(cachePathLength + 24 + 1);
if (cacheFilePath != NULL) {
char* idStartInCachePath = cacheFilePath + cachePathLength;
memcpy(cacheFilePath, cachePath, cachePathLength);
int number = 0;
while (number < aId->idLength) {
char digit = aId->id[number];
if (digit == '/') {
digit = '-';
}
idStartInCachePath[number] = digit;
++number;
}
idStartInCachePath[number] = '\0';
FILE* cacheFile = fopen(cacheFilePath, "wb");
if (cacheFile != NULL) {
fwrite(aId->buffer, 1, aId->bytesRead, cacheFile);
fclose(cacheFile);
}
free(cacheFilePath);
}
goto CLOSE_OPEN;
}
else if (dwInternetStatus == WINHTTP_CALLBACK_FLAG_DATA_AVAILABLE || dwInternetStatus == WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE) {
int numberBytesToRead = rasterTileSize * rasterTileSize * 3 - aId->bytesRead;
if (numberBytesToRead > 0) {
WinHttpReadData(aId->hRequest, aId->buffer + aId->bytesRead, numberBytesToRead, NULL);
}
else {
goto CLOSE_OPEN;
}
}
else if (dwInternetStatus == WINHTTP_CALLBACK_FLAG_SENDREQUEST_COMPLETE) {
aId->buffer = malloc(rasterTileSize * rasterTileSize * 3); // using size of uncompressed image hoping it is sufficient (checked above)
if (aId->buffer != NULL) {
WinHttpReceiveResponse(aId->hRequest, NULL);
}
else {
goto CLOSE_OPEN;
}
}
else if (dwInternetStatus == WINHTTP_CALLBACK_STATUS_REQUEST_ERROR) {
hashmap_set(imgRequested, (void*)(aId->id), aId->idLength, (uintptr_t)0);
queueFillLevel counts;
counts.count = 0;
hashmap_iterate(imgRequested, measurePresence, (void*)&counts);
if (counts.count == 0) {
allImagesRequestedPresent = 1;
}
goto CLOSE_OPEN;
}
}
else {
CLOSE_OPEN:
if (aId->buffer != NULL) {
free(aId->buffer);
}
WinHttpSetStatusCallback(aId->hSession,
NULL,
WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
(DWORD_PTR)NULL);
WinHttpCloseHandle(aId->hRequest);
WinHttpCloseHandle(aId->hConnect);
WinHttpCloseHandle(aId->hSession);
free(aId);
}
}
_Atomic int doCollecting;
_Atomic int collecting;
_Atomic int rastered;
_Atomic int notScheduled;
_Atomic int wantsCompletion;
_Atomic int checkingImageRequests;
_Atomic int dontWaitForCollector;
int maxThreads;
typedef struct IdData {
char id[25]; // max length of id is 24 + \0 (digits for zoom level 30, xy 2^30, 2 /)
int idLength;
struct IdData* next;
} idData;
typedef struct ThreadData {
int yStart;
int yEnd;
idData dId;
_Atomic int rastering;
_Atomic int imageRequestRequested;
hashmap* lastQueue;
HANDLE hThread;
HANDLE hComplete;
} threadData;
threadData* threadsData;
wchar_t* host;
wchar_t* pathFormat;
wchar_t* path;
char* cachePathCollector;
char* idStartInCachePath;
unsigned __stdcall collector(void* data) {
int processPossibleAdditions = 1;
do {
do {
collecting = 1;
for (int i = 0; i < maxThreads; ++i) {
if (threadsData[i].imageRequestRequested) {
threadsData[i].imageRequestRequested = 0;
idData* dId = &(threadsData[i].dId);
do {
int idLength = dId->idLength;
if (idLength > 0) {
char* id = dId->id;
uintptr_t result;
if (!hashmap_get(imgRequested, (void*)id, idLength, &result)) {
int number = 0;
while (number < idLength) {
char digit = id[number];
if (digit == '/') {
digit = '-';
}
idStartInCachePath[number] = digit;
++number;
}
idStartInCachePath[number] = '\0';
FILE* cacheFile = fopen(cachePathCollector, "rb");
if (cacheFile != NULL) {
unsigned char* cachedImage = malloc(rasterTileSize * rasterTileSize * 3); // using uncompressed size hoping it suffices, checked below
if (cachedImage != NULL) {
size_t sizeRead = fread(cachedImage, sizeof(unsigned char), rasterTileSize * rasterTileSize * 3, cacheFile);
if (sizeRead != 0 && (sizeRead == rasterTileSize * rasterTileSize * 3 || feof(cacheFile))) {
fclose(cacheFile);
tjhandle tjInstance = tj3Init(TJINIT_DECOMPRESS);
if (tjInstance != NULL) {
if (tj3DecompressHeader(tjInstance, cachedImage, sizeRead) == 0) {
unsigned char* pixels = malloc(TJSCALED(tj3Get(tjInstance, TJPARAM_JPEGWIDTH), TJUNSCALED) * TJSCALED(tj3Get(tjInstance, TJPARAM_JPEGHEIGHT), TJUNSCALED) * tjPixelSize[TJPF_RGB]); // malloced length expectation == rasterTileSize * rasterTileSize * 3
if (pixels != NULL) {
if (tj3Decompress8(tjInstance, cachedImage, sizeRead, pixels, 0, TJPF_RGB) == 0) {
free(cachedImage);
char* pId = malloc(idLength);
if (pId != NULL) {
memcpy(pId, id, idLength);
if (hashmap_sets_left_before_resize(imgPresent) <= 2) { // <= 1 should suffice but crash was observed in hashmap_get(imgPresent, ...)
dontWaitForCollector = 0;
int countRastering;
do {
if (!notquitrequested) {
goto LIKE_AIDNULL;
}
countRastering = 0;
for (int j = 0; j < maxThreads; ++j) {
countRastering += threadsData[j].rastering;
}
} while (countRastering || !notScheduled || wantsCompletion || !allImagesRequestedPresent);
hashmap_set(imgPresent, (void*)pId, idLength, (uintptr_t)pixels);
hashmap_set(imgRequested, (void*)pId, idLength, (uintptr_t)0);
dontWaitForCollector = 1;
goto AFTER_IMG_REQUEST;
}
else {
hashmap_set(imgPresent, (void*)pId, idLength, (uintptr_t)pixels);
hashmap_set(imgRequested, (void*)pId, idLength, (uintptr_t)0);
LOG(("from cache: %s\n", id));
goto AFTER_IMG_REQUEST;
}
}
}
free(pixels);
}
}
tj3Destroy(tjInstance);
}
}
else {
fclose(cacheFile);
}
free(cachedImage);
}
else {
fclose(cacheFile);
}
}
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(L"WinHTTP Globe/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
WINHTTP_FLAG_ASYNC);
// Specify an HTTP server.
if (hSession) {
if (WinHttpSetStatusCallback(hSession, (WINHTTP_STATUS_CALLBACK)onImageLoading, WINHTTP_CALLBACK_FLAG_SENDREQUEST_COMPLETE | WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE | WINHTTP_CALLBACK_FLAG_DATA_AVAILABLE | WINHTTP_CALLBACK_STATUS_READ_COMPLETE | WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, (DWORD_PTR)NULL) == NULL) {
hConnect = WinHttpConnect(hSession, host, INTERNET_DEFAULT_HTTPS_PORT, 0);
}
}
// Create an HTTP request handle.
if (hConnect) {
wchar_t wId[25];
mbstowcs(wId, id, 25);
_swprintf(path, pathFormat, wId);
hRequest = WinHttpOpenRequest(hConnect, L"GET", path,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
}
else {
if (hSession) {
WinHttpSetStatusCallback(hSession,
NULL,
WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
(DWORD_PTR)NULL);
WinHttpCloseHandle(hSession);
}
}
// Send a request.
if (hRequest) {
asyncId* aId = malloc(sizeof(asyncId));
if (aId != NULL) {
aId->id = malloc(idLength);
if (aId->id != NULL) {
memcpy(aId->id, id, idLength);
aId->idLength = idLength;
aId->hRequest = hRequest;
aId->hConnect = hConnect;
aId->hSession = hSession;
aId->buffer = NULL;
aId->bytesRead = 0;
LOG(("%s\n", id));
if (hashmap_sets_left_before_resize(imgPresent) <= 2) { // <= 1 should suffice but crash was observed in hashmap_get(imgPresent, ...)
dontWaitForCollector = 0;
int countRastering;
do {
if (!notquitrequested) {
free(aId->id);
free(aId);
goto LIKE_AIDNULL;
}
countRastering = 0;
for (int j = 0; j < maxThreads; ++j) {
countRastering += threadsData[j].rastering;
}
} while (countRastering || !notScheduled || wantsCompletion || !allImagesRequestedPresent);
hashmap_set(imgRequested, (void*)(aId->id), aId->idLength, (uintptr_t)aId);
hashmap_set(imgPresent, (void*)(aId->id), aId->idLength, (uintptr_t)NULL);
dontWaitForCollector = 1;
}
else {
hashmap_set(imgRequested, (void*)(aId->id), aId->idLength, (uintptr_t)aId);
hashmap_set(imgPresent, (void*)(aId->id), aId->idLength, (uintptr_t)NULL); // preset memory for it be already present during async callbacks
}
allImagesRequestedPresent = 0;
if (!WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, (DWORD_PTR)aId)) {
hashmap_set(imgRequested, (void*)(aId->id), aId->idLength, (uintptr_t)0);
queueFillLevel counts;
counts.count = 0;
hashmap_iterate(imgRequested, measurePresence, (void*)&counts);
if (counts.count == 0) {
allImagesRequestedPresent = 1;
}
free(aId);
goto LIKE_AIDNULL;
}
}
else {
free(aId);
goto LIKE_AIDNULL;
}
}
else {
LIKE_AIDNULL: ;
WinHttpSetStatusCallback(hSession,
NULL,
WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
(DWORD_PTR)NULL);
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
if (!notquitrequested) {
return 0;
}
}
}
else {
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) {
WinHttpSetStatusCallback(hSession,
NULL,
WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
(DWORD_PTR)NULL);
WinHttpCloseHandle(hSession);
}
}
}
AFTER_IMG_REQUEST: ;
dId->idLength = 0;
}
dId = dId->next;
} while (dId != NULL);
}
}
collecting = 0;
if (doCollecting && (!rastered || !notScheduled)) {
processPossibleAdditions = 1;
}
else {
break;
}
} while (true);
} while (doCollecting && processPossibleAdditions--);
checkingImageRequests = 0;
return 0;
}
_Atomic int queued;
typedef enum MovementDirection {
ZIN, ZOUT, SIDE, INIT, REFRESH
} movementDirection;
movementDirection dir;
void* buffer;
void* region;
int pitch;
unsigned char* elevationData;
int elevationDataAvailable = 0;
typedef struct Pixel {
int sourceX, sourceY;
int targetX, targetY;
} pixel;
void pickPixel(pixel* p, unsigned char* pixels) {
memcpy((void*)(((unsigned char*)buffer) + (p->targetY * pitch + p->targetX * 3)), (void*)(pixels + (p->sourceY * rasterTileSize + p->sourceX) * 3), 3);
}
const double sx = 0.57735;
const double sy = 0.57735;
const double sz = -0.57735;
const unsigned char w = 255;
const unsigned int r = 0;
const unsigned int g = 1;
const unsigned int b = 2;
const float maxZoomLighting = 2.5F;
/// <summary>
/// lights a pixel from a predefined light ray
/// </summary>
/// <param name="x">x coordinate of the pixel in the window</param>
/// <param name="y">y coordinate of the pixel in the window</param>
/// <param name="rgb">array of 3 unsigned char containing the original color, gets overwritten to the lighted color</param>
void lightPixel(int x, int y, unsigned char* rgb) {
ptF angles = atFWithoutOffsets(x, y);
double ct = cos(-angles.t); // doubles to alleviate banding, does not avail
double nx = ct * cos(angles.p + PID);
double ny = ct * sin(angles.p + PID);
double nz = sin(-angles.t);
double sn = 2.0 * (sx * nx + sy * ny + sz * nz);
double rx = sx - sn * nx;
double ry = sy - sn * ny;
double rz = sz - sn * nz;
double a = -ry / sqrt(rx * rx + ry * ry + rz * rz);
a = a < 0.0 ? 0.0 : a;
double f;
unsigned char m = max(rgb[r], max(rgb[g], rgb[b]));
if (m == rgb[r])
f = 0.275;
else
if (m == rgb[g])
f = 0.35;
else
f = 0.5;
double t = f * a * a * (1.0F - zoomF / maxZoomLighting);
rgb[r] += (unsigned char)((w - rgb[r]) * t);
rgb[g] += (unsigned char)((w - rgb[g]) * t);
rgb[b] += (unsigned char)((w - rgb[b]) * t);
}
void pickPixelWithLighting(pixel* p, unsigned char* pixels) {
unsigned char rgb[3];
memcpy((void*)rgb, (void*)(pixels + (p->sourceY * rasterTileSize + p->sourceX) * 3), 3);
lightPixel(p->targetX, p->targetY, rgb);
memcpy((void*)(((unsigned char*)buffer) + (p->targetY * pitch + p->targetX * 3)), (void*)rgb, 3);
}
typedef struct Link {
pixel* p;
struct Link* l;
} link;
void pickPixels(void* key, size_t ksize, uintptr_t value, void* usr) {
unsigned char* pixels;
hashmap_get(imgPresent, key, ksize, (uintptr_t*)&pixels);
if (pixels != NULL) {
link* l = (link*)value;
do {
pickPixel(l->p, pixels);
free(l->p);
link* currentLink = l;
l = l->l;
free(currentLink);
} while (l != NULL);
}
else {
link* l = (link*)value;
do {
free(l->p);
link* currentLink = l;
l = l->l;
free(currentLink);
} while (l != NULL);
}
free((char*)key);
}
void pickPixelsWithLighting(void* key, size_t ksize, uintptr_t value, void* usr) {
unsigned char* pixels;
hashmap_get(imgPresent, key, ksize, (uintptr_t*)&pixels);
if (pixels != NULL) {
link* l = (link*)value;
do {
pickPixelWithLighting(l->p, pixels);
free(l->p);
link* currentLink = l;
l = l->l;
free(currentLink);
} while (l != NULL);
}
else {
link* l = (link*)value;
do {
free(l->p);
link* currentLink = l;
l = l->l;
free(currentLink);
} while (l != NULL);
}
free((char*)key);
}
void clearQueue(void* key, size_t ksize, uintptr_t value, void* usr) {
link* l = (link*)value;
do {
free(l->p);
link* currentLink = l;
l = l->l;
free(currentLink);
} while (l != NULL);
free((char*)key);
}
// t->t from [0, +/- pi/2] to [0, +/- pi/2] is stretched/mapped to t -> 1/2 * ln(tan(t/2 + pi/4)) from [0, +/- pi/2] to [0, +/- 1.75]