-
Notifications
You must be signed in to change notification settings - Fork 67
/
xdevice.cpp
2467 lines (2198 loc) · 74.9 KB
/
xdevice.cpp
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
/*
** Astrolog (Version 7.70) File: xdevice.cpp
**
** IMPORTANT NOTICE: Astrolog and all chart display routines and anything
** not enumerated below used in this program are Copyright (C) 1991-2024 by
** Walter D. Pullen ([email protected], http://www.astrolog.org/astrolog.htm).
** Permission is granted to freely use, modify, and distribute these
** routines provided these credits and notices remain unmodified with any
** altered or distributed versions of the program.
**
** The main ephemeris databases and calculation routines are from the
** library SWISS EPHEMERIS and are programmed and copyright 1997-2008 by
** Astrodienst AG. Use of that source code is subject to license for Swiss
** Ephemeris Free Edition at https://www.astro.com/swisseph/swephinfo_e.htm.
** This copyright notice must not be changed or removed by any user of this
** program.
**
** Additional ephemeris databases and formulas are from the calculation
** routines in the program PLACALC and are programmed and Copyright (C)
** 1989,1991,1993 by Astrodienst AG and Alois Treindl ([email protected]). The
** use of that source code is subject to regulations made by Astrodienst
** Zurich, and the code is not in the public domain. This copyright notice
** must not be changed or removed by any user of this program.
**
** The original planetary calculation routines used in this program have
** been copyrighted and the initial core of this program was mostly a
** conversion to C of the routines created by James Neely as listed in
** 'Manual of Computer Programming for Astrologers', by Michael Erlewine,
** available from Matrix Software.
**
** Atlas composed using data from https://www.geonames.org/ licensed under a
** Creative Commons Attribution 4.0 License. Time zone changes composed using
** public domain TZ database: https://data.iana.org/time-zones/tz-link.html
**
** The PostScript code within the core graphics routines are programmed
** and Copyright (C) 1992-1993 by Brian D. Willoughby ([email protected]).
**
** More formally: 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 2 of the
** License, or (at your option) any later version. This program is
** distributed in the hope that it will be useful and inspiring, 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, a copy of which is in the
** LICENSE.HTM file included with Astrolog, and at http://www.gnu.org
**
** Initial programming 8/28-30/1991.
** X Window graphics initially programmed 10/23-29/1991.
** PostScript graphics initially programmed 11/29-30/1992.
** Last code change made 4/22/2024.
*/
#include "astrolog.h"
#ifdef GRAPH
/*
******************************************************************************
** Windows Bitmap Routines.
******************************************************************************
*/
#define cbPixelK 3
#define CbColmapRow(x) ((x)*cbPixelK + 3 & ~3)
#define CbColmap(x, y) ((y) * CbColmapRow(x))
#define zColmap 65535
// Functions to set or get a pixel within a 24 bit color bitmap.
INLINE long _IbXY(CONST Bitmap *b, int x, int y)
{ return y*(b->clRow << 2) + (x * cbPixelK); }
INLINE byte *_PbXY(CONST Bitmap *b, int x, int y)
{ return &(b->rgb)[_IbXY(b, x, y)]; }
INLINE void _SetRGB(byte *pb, int r, int g, int b)
{ *pb = b; *(pb+1) = g; *(pb+2) = r; }
INLINE KV _GetP(CONST byte *pb)
{ return (*pb << 16) | (*(pb+1) << 8) | *(pb+2); }
INLINE KV _GetXY(CONST Bitmap *b, int x, int y)
{ return _GetP(_PbXY(b, x, y)); }
INLINE void _GetRGB(CONST byte *pb, int *r, int *g, int *b)
{ *b = *pb; *g = *(pb+1); *r = *(pb+2); }
void BmpSetXY(Bitmap *b, int x, int y, KV kv)
{ _SetRGB(_PbXY(b, x, y), RgbR(kv), RgbG(kv), RgbB(kv)); }
KV BmpGetXY(Bitmap *b, int x, int y)
{ return _GetXY(b, x, y); }
void SetXY(int x, int y, KI ki)
{ if (!gi.fBmp) BmSet(gi.bm, x, y, ki); else BmpSetXY(&gi.bmp, x, y, ki); }
KI GetXY(int x, int y)
{ return !gi.fBmp ? FBmGet(gi.bm, x, y) : _GetXY(&gi.bmp, x, y); }
KI BmGetXY(int x, int y)
{ return !gi.fBmp ? FBmGet(gi.bm, x, y) : (_GetXY(&gi.bmp, x, y) > 0)*15; }
// Allocate or reallocate a 24 bit color bitmap to have a given size.
flag FAllocateBmp(Bitmap *b, int x, int y)
{
char sz[cchSzDef];
long cb;
byte *rgb;
if (x == b->x && y == b->y)
return fTrue;
if (x < 0 || y < 0 || x > zColmap || y > zColmap) {
sprintf(sz, "Can't create color bitmap larger than %d by %d!\n",
zColmap, zColmap);
PrintError(sz);
return fFalse;
}
cb = CbColmap(x, y);
if (cb < 0) {
sprintf(sz, "Can't allocate color bitmap of size %d by %d!\n", x, y);
PrintError(sz);
return fFalse;
}
if (cb != CbColmap(b->x, b->y)) {
rgb = (byte *)PAllocate(cb, "color bitmap");
if (rgb == NULL)
return fFalse;
DeallocatePIf(b->rgb);
b->rgb = rgb;
}
b->x = x; b->y = y;
b->clRow = CbColmapRow(x) >> 2;
return fTrue;
}
// Load a bitmap from file into a 24 bit bitmap structure. This supports
// Windows bitmap files stored with 4, 8, 16, 24, or 32 bits per pixel.
flag FReadBmp(Bitmap *b, FILE *file, flag fNoHeader)
{
byte *pb, bR, bG, bB, ch, ch2;
KV rgkv[256], kv;
int cbExtra, cb, x, y, z, k, i, m, n;
long l;
// BitmapFileHeader
if (!fNoHeader) {
ch = getbyte(); ch2 = getbyte();
if (ch != 'B' || ch2 != 'M') {
PrintError("This file does not look like a Windows bitmap.\n");
return fFalse;
}
skiplong();
skipword(); skipword();
skiplong();
}
// BitmapInfo / BitmapInfoHeader
cbExtra = getlong();
x = NAbs((int)getlong()); y = NAbs((int)getlong());
skipword(); z = getword();
l = getlong();
if (l != 0/*BI_RGB*/ && l != 3/*BI_BITFIELDS*/) {
PrintError("This Windows bitmap can't be uncompressed.\n");
return fFalse;
}
for (i = 0; i < 3; i++) {
skiplong();
}
k = getlong(); skiplong();
for (cbExtra -= 40; cbExtra > 0; cbExtra--)
skipbyte();
if (l == 3/*BI_BITFIELDS*/)
for (i = 0; i < 3; i++) {
skiplong();
}
if (!(z == 4 || z == 8 || z == 16 || z == 24 || z == 32)) {
PrintError("This Windows bitmap has bad number of bits per pixel.\n");
return fFalse;
}
// RgbQuad
// Data
if (!FAllocateBmp(b, x, y))
return fFalse;
// Figure out the bytes per row in this color bitmap.
if (z == 32)
cb = x << 2;
else if (z == 24)
cb = x * 3;
else if (z == 16) {
for (i = 0; i < 12; i++)
skipbyte();
cb = x << 1;
} else {
// Read in the color palette to translate indexes to RGB values.
Assert(k <= 256);
if (k == 0)
k = (z == 8) ? 256 : 16;
for (i = 0; i < k; i++) {
bB = getbyte(); bG = getbyte(); bR = getbyte();
skipbyte();
rgkv[i] = Rgb(bR, bG, bB);
}
if (z == 8)
cb = x;
else
cb = (x + 1) >> 1;
}
// Figure out the number of padding bytes after each row.
cb = (4 - (cb & 3)) & 3;
for (n = y-1; n >= 0; n--) {
pb = _PbXY(b, 0, n);
for (m = 0; m < x; m++) {
if (z >= 24) {
bB = getbyte(); bG = getbyte(); bR = getbyte();
if (z == 32)
skipbyte();
} else {
if (z == 16) {
i = WRead(file);
kv = Rgb((i >> 11) << 3, (i >> 5 & 63) << 2, (i & 31) << 3);
} else if (z == 8) {
ch = getbyte();
kv = rgkv[ch];
} else {
if (!FOdd(m))
ch = getbyte();
i = FOdd(m) ? (ch & 15) : (ch >> 4);
kv = rgkv[i];
}
bR = RgbR(kv); bG = RgbG(kv); bB = RgbB(kv);
}
_SetRGB(pb, bR, bG, bB);
pb += cbPixelK;
}
for (m = 0; m < cb; m++)
skipbyte();
}
return fTrue;
}
// Load a 24 bit bitmap given a filename.
flag FLoadBmp(CONST char *szFile, Bitmap *bmp, flag fNoHeader)
{
FILE *file;
flag fRet;
file = FileOpen(szFile, 3, NULL);
if (file == NULL)
return fFalse;
fRet = FReadBmp(bmp, file, fNoHeader);
fclose(file);
return fRet;
}
// Write a 24 bit bitmap to a previously opened file, in the 24 bit bitmap
// format used by Microsoft Windows for its .bmp extension files.
void WriteBmp2(CONST Bitmap *b, FILE *file)
{
int x, y, cb;
dword dw;
cb = (4 - ((b->x*3) & 3)) & 3;
// BitmapFileHeader
PutByte('B'); PutByte('M');
dw = 14+40 + 0 + b->y*(b->x*3+cb);
PutLong(dw);
PutWord(0); PutWord(0);
PutLong(14+40 + 0);
// BitmapInfo / BitmapInfoHeader
PutLong(40);
PutLong(b->x); PutLong(b->y);
PutWord(1); PutWord(24);
PutLong(0 /*BI_RGB*/); PutLong(0);
PutLong(0); PutLong(0);
PutLong(0); PutLong(0);
// RgbQuad
// Data
for (y = b->y-1; y >= 0; y--) {
for (x = 0; x < b->x; x++) {
dw = _GetXY(b, x, y);
PutByte(RgbB(dw)); PutByte(RgbG(dw)); PutByte(RgbR(dw));
}
for (x = 0; x < cb; x++)
PutByte(0);
}
}
// Set all pixels in a 24 bit bitmap to the specified RGB color value.
void BmpSetAll(Bitmap *b, KV kv)
{
int x, y, nR, nG, nB;
byte *pb;
nR = RgbR(kv); nG = RgbG(kv); nB = RgbB(kv);
for (y = 0; y < b->y; y++) {
pb = _PbXY(b, 0, y);
for (x = 0; x < b->x; x++) {
_SetRGB(pb, nR, nG, nB);
pb += cbPixelK;
}
}
}
// Copy a rectangle from one bitmap structure to a different rectangle in
// another, stretching pixels as needed. Like the Windows StretchBlt() API.
void BmpCopyBlock(CONST Bitmap *bs, int x1, int y1, int x2, int y2,
Bitmap *bd, int x3, int y3, int x4, int y4)
{
int xs = x2-x1+1, ys = y2-y1+1, xd = x4-x3+1, yd = y4-y3+1,
x, y, xT, yT, nR, nG, nB;
byte *pbDst;
// Sanity checks of coordinate bounds, which shouldn't ever fail.
Assert(FBetween(x1, 0, bs->x-1));
Assert(FBetween(y1, 0, bs->y-1));
Assert(FBetween(x2, 0, bs->x-1));
Assert(FBetween(y2, 0, bs->y-1));
Assert(FBetween(x3, 0, bd->x-1));
Assert(FBetween(y3, 0, bd->y-1));
Assert(FBetween(x4, 0, bd->x-1));
Assert(FBetween(y4, 0, bd->y-1));
for (y = y3; y <= y4; y++) {
pbDst = _PbXY(bd, x3, y);
yT = y1 + (y-y3) * ys / yd;
for (x = x3; x <= x4; x++) {
xT = x1 + (x-x3) * xs / xd;
_GetRGB(_PbXY(bs, xT, yT), &nR, &nG, &nB);
_SetRGB(pbDst, nR, nG, nB);
pbDst += cbPixelK;
}
}
}
// Like BmpCopyBlock() but the source rectangle coordinates are reals instead
// of integers. Can be used to render only subsections of source pixels.
void BmpCopyBlock2(CONST Bitmap *bs, real x1, real y1, real x2, real y2,
Bitmap *bd, int x3, int y3, int x4, int y4)
{
int xd = x4-x3+1, yd = y4-y3+1, x, y, xT, yT, nR, nG, nB;
real xs, ys;
byte *pbDst;
// Sanity checks of coordinate bounds, which shouldn't ever fail.
Assert(FBetween(x1, 0.0, (real)bs->x - rSmall));
Assert(FBetween(y1, 0.0, (real)bs->y - rSmall));
Assert(FBetween(x2, 0.0, (real)bs->x - rSmall));
Assert(FBetween(y2, 0.0, (real)bs->y - rSmall));
Assert(FBetween(x3, 0, bd->x-1));
Assert(FBetween(y3, 0, bd->y-1));
Assert(FBetween(x4, 0, bd->x-1));
Assert(FBetween(y4, 0, bd->y-1));
xs = (x2-x1) / (real)xd;
ys = (y2-y1) / (real)yd;
for (y = y3; y <= y4; y++) {
pbDst = _PbXY(bd, x3, y);
yT = (int)(y1 + (real)(y-y3) * ys);
for (x = x3; x <= x4; x++) {
xT = (int)(x1 + (real)(x-x3) * xs);
_GetRGB(_PbXY(bs, xT, yT), &nR, &nG, &nB);
_SetRGB(pbDst, nR, nG, nB);
pbDst += cbPixelK;
}
}
}
#ifdef WINANY
// Copy a 24 bit bitmap structure to a Windows DC. Since Astrolog's internal
// bitmap structure is the same as Windows, it can be done all at once.
void BmpCopyWin(CONST Bitmap *b, HDC hdc, int x, int y)
{
BITMAPINFO bi;
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = cbPixelK << 3;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = 0;
bi.bmiHeader.biXPelsPerMeter = bi.bmiHeader.biYPelsPerMeter = 1000;
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
bi.bmiColors[0].rgbBlue = bi.bmiColors[0].rgbGreen =
bi.bmiColors[0].rgbRed = bi.bmiColors[0].rgbReserved = 0;
bi.bmiHeader.biWidth = (b->x);
bi.bmiHeader.biHeight = -(b->y);
SetDIBitsToDevice(hdc, x, y, b->x, b->y, 0, 0, 0, b->y,
b->rgb, (BITMAPINFO *)&bi, DIB_RGB_COLORS);
}
#endif
// Draw the background bitmap onto the specified 24 bit bitmap. Implements the
// -XI switch features.
flag FBmpDrawBack(Bitmap *bDest)
{
Bitmap *b = &gi.bmpBack, *b2 = &gi.bmpBack2;
int nTrans, x, y, x1, y1, x2, y2, x3, y3, x4, y4, nR, nG, nB, nRT, nGT, nBT;
byte *pb, *pb2;
KV kv;
static KV kvLast = -1;
static int nTransLast = 0, xLast = 0, yLast = 0;
// Don't draw background if user doesn't want to.
if (!gi.fBmp)
return fFalse;
// Don't draw background if entire chart will be covered with world map.
if (gi.bmpWorld.rgb != NULL &&
(gi.nMode == gAstroGraph || (gi.nMode == gWorldMap && !gs.fMollewide)))
return fFalse;
// Don't do anything if bitmap empty or transparent enough to be invisible.
nTrans = (int)(gs.rBackPct * 256.0 / 100.0);
if (b->rgb == NULL || nTrans <= 0)
return fFalse;
// Cache bitmap with proper percentage blend with current background color.
kv = rgbbmp[gi.kiOff];
if (b2->x != b->x || b2->y != b->y || kv != kvLast || nTrans != nTransLast) {
if (!FAllocateBmp(b2, b->x, b->y))
return fFalse;
kvLast = kv;
nTransLast = nTrans;
nR = RgbR(kv); nG = RgbG(kv); nB = RgbB(kv);
for (y = 0; y < b->y; y++) {
pb = _PbXY(b, 0, y);
pb2 = _PbXY(b2, 0, y);
for (x = 0; x < b->x; x++) {
_GetRGB(pb, &nRT, &nGT, &nBT);
nRT = nR + ((nRT - nR) * nTrans >> 8);
nGT = nG + ((nGT - nG) * nTrans >> 8);
nBT = nB + ((nBT - nB) * nTrans >> 8);
_SetRGB(pb2, nRT, nGT, nBT);
pb += cbPixelK; pb2 += cbPixelK;
}
}
xLast = yLast = 0;
}
// Determine source (on bitmap) and destination (on chart) rectangles.
x1 = y1 = x3 = y3 = 0;
x2 = gs.xWin; y2 = gs.yWin;
x4 = b2->x; y4 = b2->y;
if (gs.nBackOrient < 0) {
if ((real)x2 / (real)y2 > (real)x4 / (real)y4) {
x2 = y2 * x4 / y4;
x1 = (gs.xWin - x2) >> 1;
} else {
y2 = x2 * y4 / x4;
y1 = (gs.yWin - y2) >> 1;
}
} else if (gs.nBackOrient > 0) {
if ((real)x4 / (real)y4 > (real)x2 / (real)y2) {
x4 = y4 * x2 / y2;
x3 = (b2->x - x4) >> 1;
} else {
y4 = x4 * y2 / x2;
y3 = (b2->y - y4) >> 1;
}
}
// For bitmaps, copy background to chart bitmap manually.
if (gi.fFile || bDest != NULL) {
if ((gs.ft == ftBmp && gi.fBmp) || bDest != NULL)
BmpCopyBlock(&gi.bmpBack2, x3, y3, x3+x4-1, y3+y4-1,
bDest != NULL ? bDest : &gi.bmp, x1, y1, x1+x2-1, y1+y2-1);
return fTrue;
}
#ifdef WINANY
// For Windows, draw background bitmap on window using Windows API.
if (wi.hdcBack == NULL) {
wi.hdcBack = CreateCompatibleDC(wi.hdc);
SetMapMode(wi.hdcBack, MM_TEXT);
SetWindowOrg(wi.hdcBack, 0, 0); SetViewportOrg(wi.hdcBack, 0, 0);
}
if (b2->x != xLast || b2->y != yLast) {
// If background has changed size, create a new Windows Bitmap for it.
SelectObject(wi.hdcBack, wi.hbmpPrev);
if (wi.hbmpBack != NULL)
DeleteObject(wi.hbmpBack);
wi.hbmpBack = CreateCompatibleBitmap(wi.hdc, b2->x, b2->y);
if (wi.hbmpBack == NULL) {
PrintError("Failed to create color background bitmap.");
return fFalse;
}
wi.hbmpPrev = (HBITMAP)SelectObject(wi.hdcBack, wi.hbmpBack);
xLast = b2->x; yLast = b2->y;
BmpCopyWin(b2, wi.hdcBack, 0, 0);
}
SetStretchBltMode(wi.hdc, COLORONCOLOR);
StretchBlt(wi.hdc, x1, y1, x2, y2, wi.hdcBack, x3, y3, x4, y4, SRCCOPY);
#endif
return fTrue;
}
// Draw the world map bitmap upon the specified 24 bit bitmap. This draws the
// world in the appropriate projection for various Astrolog charts.
flag FBmpDrawMap()
{
Bitmap *bmp = &gi.bmp;
int nScl = 1, xc, yc, zc, x1, x2, y1, y2, xi, yi, n, n2;
real deg = Mod(rDegMax - gs.rRot), lonS, latS, rxc, ryc, rzc,
lon, lat, lat0, rT, rLen, sint, cost, sina, cosa;
KV kv;
// Do nothing if not drawing bitmaps, or if the Earth bitmap fails to load.
if (!gi.fBmp || (gi.fFile && gs.ft != ftBmp))
return fFalse;
if (gi.bmpWorld.rgb == NULL && !FLoadBmp(BITMAP_EARTH, &gi.bmpWorld, fFalse))
return fFalse;
#ifdef WINANY
if (!gi.fFile) {
if (!FAllocateBmp(&wi.bmpWin, gs.xWin, gs.yWin))
return fFalse;
bmp = &wi.bmpWin;
}
#endif
// Compute center coordinates and horizontal map dimensions.
xc = (gs.xWin >> 1) - !FOdd(gs.xWin); yc = (gs.yWin >> 1) - !FOdd(gs.yWin);
zc = Max(xc, yc);
rxc = (real)xc; ryc = (real)yc; rzc = (real)zc;
x1 = (int)((real)gi.bmpWorld.x * deg / rDegMax);
x2 = (int)((real)gs.xWin * deg / rDegMax);
// Draw map on a -XW rectangular world map.
if (gi.nMode == gAstroGraph || (gi.nMode == gWorldMap && !gs.fMollewide)) {
if (x1 == 0 || x2 == 0)
BmpCopyBlock(&gi.bmpWorld, 0, 0, gi.bmpWorld.x-1, gi.bmpWorld.y-1,
bmp, 0, 0, gs.xWin-1, gs.yWin-1);
else {
BmpCopyBlock(&gi.bmpWorld, 0, 0, x1-1, gi.bmpWorld.y-1,
bmp, gs.xWin-x2-1, 0, gs.xWin-1, gs.yWin-1);
BmpCopyBlock(&gi.bmpWorld, x1, 0, gi.bmpWorld.x-1, gi.bmpWorld.y-1,
bmp, 0, 0, gs.xWin-x2, gs.yWin-1);
}
// Draw map on a -XW0 Mollewide projection world map.
} else if (gi.nMode == gWorldMap && gs.fMollewide) {
if (!FBmpDrawBack(bmp))
BmpSetAll(bmp, rgbbmp[gi.kiOff]);
for (y2 = 0; y2 < gs.yWin; y2++) {
y1 = y2 * gi.bmpWorld.y / gs.yWin;
rT = RMollewide((real)y2 * rDegHalf / (real)gs.yWin - rDegQuad);
n = (gs.xWin - (int)(rT * (real)gs.xWin / rDegHalf)) >> 1;
if (x1 == 0 || x2 == 0)
BmpCopyBlock(&gi.bmpWorld, 0, y1, gi.bmpWorld.x-1, y1,
bmp, n, y2, gs.xWin-1-n, y2);
else {
n2 = (int)(rT * (real)(x2 - xc) / rDegHalf);
n2 += xc;
BmpCopyBlock(&gi.bmpWorld, 0, y1, x1-1, y1,
bmp, gs.xWin-n2-1, y2, gs.xWin-1-n, y2);
BmpCopyBlock(&gi.bmpWorld, x1, y1, gi.bmpWorld.x-1, y1,
bmp, n, y2, gs.xWin-n2, y2);
}
}
// Draw map on a -XP polar globe.
} else if (gi.nMode == gPolar) {
if (!FBmpDrawBack(bmp))
BmpSetAll(bmp, rgbbmp[gi.kiOff]);
lonS = Tropical(planet[oSun]);
latS = planetalt[oSun];
EclToEqu(&lonS, &latS);
lonS = Mod(lonS - cp0.lonMC + rDegHalf - Lon);
for (y1 = 0; y1 < gs.yWin; y1++) {
yi = !FOdd(gs.yWin) && y1 > yc;
for (x1 = 0; x1 < gs.xWin; x1++) {
xi = !FOdd(gs.xWin) && x1 > xc;
n = xc - x1 + xi;
n2 = yc - y1 + yi;
if (xc > yc)
n2 = n2 * xc / yc;
else if (yc > xc)
n = n * yc / xc;
n = Sq(n) + Sq(n2);
if (n > Sq(zc))
continue;
lat = RAsinD(RSqr((real)n) / rzc) * 2.0;
if (gs.fSouth)
lat = rDegHalf - lat;
lon = RAngleD(x1 - xc, y1 - yc);
lon = Mod(270.0 - gs.rRot + (!gs.fSouth ? -lon : lon));
if (gs.fEcliptic) {
lon = Tropical(lon);
lat = rDegQuad - lat;
EclToEqu(&lon, &lat);
lon = Mod(lon - cp0.lonMC + rDegHalf - Lon);
lat = rDegQuad - lat;
}
x2 = (int)(lon * ((real)gi.bmpWorld.x - rSmall) / rDegMax);
y2 = (int)(lat * ((real)gi.bmpWorld.y - rSmall) / rDegHalf);
kv = _GetXY(&gi.bmpWorld, x2, y2);
if (gs.fMollewide &&
SphDistance(lonS, latS, lon, rDegQuad - lat) > rDegQuad)
kv = Rgb(RgbR(kv) / 3, RgbG(kv) / 3, RgbB(kv) / 3);
BmpSetXY(bmp, x1, y1, kv);
}
}
// Draw map on a -XG globe.
} else if (gi.nMode == gGlobe) {
if (!FBmpDrawBack(bmp))
BmpSetAll(bmp, rgbbmp[gi.kiOff]);
if (gs.rTilt != 0.0) {
sint = RSinD(-gs.rTilt);
cost = RCosD(-gs.rTilt);
}
lonS = Tropical(planet[oSun]);
latS = planetalt[oSun];
EclToEqu(&lonS, &latS);
lonS = Mod(lonS - cp0.lonMC + rDegHalf - Lon);
for (y1 = 0; y1 < gs.yWin; y1++) {
yi = !FOdd(gs.yWin) && y1 > yc;
rT = (ryc - (real)y1) / ryc;
if (rT < -1.0) // Roundoff may put it slightly outside Acos range.
rT = -1.0;
else if (rT > 1.0)
rT = 1.0;
lat0 = RAcosD(rT);
n = xc; n2 = yc - y1;
if (xc > yc)
n2 = n2 * xc / yc;
else if (yc > xc)
n = n * yc / xc;
rT = (real)(Sq(n) - Sq(n2));
rLen = rT >= 0.0 ? RSqr(rT) : rSmall;
if (rLen < rSmall)
rLen = 1.0;
sina = RSinD(rDegQuad - lat0);
cosa = RCosD(rDegQuad - lat0);
for (x1 = 0; x1 < gs.xWin; x1++) {
xi = !FOdd(gs.xWin) && x1 > xc;
n = xc - x1 + xi;
n2 = yc - y1 + yi;
if (xc > yc)
n2 = n2 * xc / yc;
else if (yc > xc)
n = n * yc / xc;
n = Sq(n) + Sq(n2);
if (n > Sq(zc))
continue;
lon = (rxc - (real)x1) / rxc;
rT = lon / rLen * rzc;
if (rT < -1.0) // Roundoff may put it slightly outside Acos range.
rT = -1.0;
else if (rT > 1.0)
rT = 1.0;
lon = Mod(RAcosD(rT));
lat = lat0;
if (gs.rTilt != 0.0) {
lat = rDegQuad - lat;
CoorXformFast(&lon, &lat, RSinD(lon), RCosD(lon),
sina, cosa, sint, cost);
lat = rDegQuad - lat;
}
lon = Mod(lon - gs.rRot);
if (gs.fEcliptic) {
lon = Tropical(lon);
lat = rDegQuad - lat;
EclToEqu(&lon, &lat);
lon = Mod(lon - cp0.lonMC + rDegHalf - Lon);
lat = rDegQuad - lat;
}
x2 = (int)(lon * ((real)gi.bmpWorld.x - rSmall) / rDegMax);
y2 = (int)(lat * ((real)gi.bmpWorld.y - rSmall) / rDegHalf);
kv = _GetXY(&gi.bmpWorld, x2, y2);
if (gs.fMollewide &&
SphDistance(lonS, latS, lon, rDegQuad - lat) > rDegQuad)
kv = Rgb(RgbR(kv) / 3, RgbG(kv) / 3, RgbB(kv) / 3);
BmpSetXY(bmp, x1, y1, kv);
}
}
}
#ifdef WINANY
if (!gi.fFile)
BmpCopyWin(bmp, wi.hdc, 0, 0);
#endif
return fTrue;
}
// Draw a subsection of the world map bitmap upon a section of the specified
// 24 bit bitmap. Called from the graphic -Nl switch local space chart.
flag FBmpDrawMap2(int x1, int y1, int x2, int y2,
real rx1, real ry1, real rx2, real ry2)
{
Bitmap *bmp = &gi.bmp;
int x12;
real rx, ry, x3, y3, x4, y4, x34;
if (!gi.fBmp || (gi.fFile && gs.ft != ftBmp))
return fFalse;
if (gi.bmpWorld.rgb == NULL && !FLoadBmp(BITMAP_EARTH, &gi.bmpWorld, fFalse))
return fFalse;
#ifdef WINANY
if (!gi.fFile) {
if (!FAllocateBmp(&wi.bmpWin, gs.xWin, gs.yWin))
return fFalse;
bmp = &wi.bmpWin;
}
#endif
rx = (real)gi.bmpWorld.x / rDegMax; ry = (real)gi.bmpWorld.y / rDegHalf;
BmpSetAll(bmp, rgbbmp[gi.kiOff]);
rx1 = Mod(rx1); rx2 = Mod(rx2);
x3 = rx1 * rx; y3 = ry1 * ry;
x4 = rx2 * rx; y4 = ry2 * ry;
if (x3 <= x4) {
// In most cases, just copy the entire rectangle all at once.
BmpCopyBlock2(&gi.bmpWorld, x3, y3, x4, y4, bmp, x1, y1, x2, y2);
} else {
// If viewport spans 180W/E, then have to copy twice from Earth bitmap.
x34 = (real)gi.bmpWorld.x - rSmall;
x12 = x1 + (int)((real)(x2-x1+1) * (x34 - x3) / (x34 - x3 + x4));
BmpCopyBlock2(&gi.bmpWorld, x3, y3, x34, y4, bmp, x1, y1, x12, y2);
BmpCopyBlock2(&gi.bmpWorld, 0, y3, x4, y4, bmp, x12+1, y1, x2, y2);
}
#ifdef WINANY
if (!gi.fFile)
BmpCopyWin(bmp, wi.hdc, 0, 0);
#endif
return fTrue;
}
/*
******************************************************************************
** Bitmap File Routines.
******************************************************************************
*/
// Write the bitmap array to a previously opened file in a format that can be
// read in by the Unix X11 commands bitmap and xsetroot. The 'mode' parameter
// defines how much white space is put in the file.
void WriteXBitmap(FILE *file, CONST char *name, char mode)
{
int x, y, i, temp = 0;
uint value;
char szT[cchSzDef], *pchStart, *pchEnd;
// Determine variable name from filename.
sprintf(szT, "%s", name);
for (pchEnd = szT; *pchEnd != chNull; pchEnd++)
;
for (pchStart = pchEnd; pchStart > szT &&
*(pchStart-1) != '/' && *(pchStart-1) != '\\'; pchStart--)
;
for (pchEnd = pchStart; *pchEnd != chNull && *pchEnd != '.'; pchEnd++)
;
*pchEnd = chNull;
// Output file header.
fprintf(file, "#define %s_width %d\n" , pchStart, gs.xWin);
fprintf(file, "#define %s_height %d\n", pchStart, gs.yWin);
fprintf(file, "static %s %s_bits[] = {",
mode != 'V' ? "char" : "short", pchStart);
for (y = 0; y < gs.yWin; y++) {
x = 0;
do {
// Process each row, eight columns at a time.
if (y + x > 0)
fprintf(file, ",");
if (temp == 0)
fprintf(file, "\n%s",
mode == 'N' ? " " : (mode == 'C' ? " " : ""));
value = 0;
for (i = (mode != 'V' ? 7 : 15); i >= 0; i--)
value = (value << 1) + (!(BmGetXY(x+i, y)^
(gs.fInverse*15))^gs.fInverse && (x + i < gs.xWin));
if (mode == 'N')
putc(' ', file);
fprintf(file, "0x");
if (mode == 'V')
fprintf(file, "%c%c",
ChHex(value >> 12), ChHex((value >> 8) & 15));
fprintf(file, "%c%c",
ChHex((value >> 4) & 15), ChHex(value & 15));
temp++;
// Is it time to skip to the next line while writing the file yet?
if ((mode == 'N' && temp >= 12) ||
(mode == 'C' && temp >= 15) ||
(mode == 'V' && temp >= 11))
temp = 0;
x += (mode != 'V' ? 8 : 16);
} while (x < gs.xWin);
}
fprintf(file, "};\n");
}
// Write the bitmap array to a previously opened file in a simple boolean
// Ascii rectangle, one char per pixel, in which '#' represents an off bit and
// '-' an on bit. The output format is identical to the format generated by
// the Unix bmtoa command, and it can be converted into a bitmap with atobm.
void WriteAscii(FILE *file)
{
int x, y, i;
for (y = 0; y < gs.yWin; y++) {
for (x = 0; x < gs.xWin; x++) {
i = BmGetXY(x, y);
if (gs.fColor)
putc(ChHex(i), file);
else
putc(i ? '-' : '#', file);
}
putc('\n', file);
}
}
// Write the bitmap array to a previously opened file in the bitmap format
// used in Microsoft Windows for its .bmp extension files. This is a pretty
// efficient format, only requiring a small header, and one bit per pixel
// for monochrome graphics, or four bits per pixel for 16 color bitmaps.
void WriteBmp(FILE *file)
{
int x, y;
dword value;
// BitmapFileHeader
PutByte('B'); PutByte('M');
PutLong(14+40 + (gs.fColor ? 64 : 8) +
(long)4*gs.yWin*(((gs.xWin-1) >> (gs.fColor ? 3 : 5))+1));
PutWord(0); PutWord(0);
PutLong(14+40 + (gs.fColor ? 64 : 8));
// BitmapInfo / BitmapInfoHeader
PutLong(40);
PutLong(gs.xWin); PutLong(gs.yWin);
PutWord(1); PutWord(gs.fColor ? 4 : 1);
PutLong(0 /*BI_RGB*/); PutLong(0);
PutLong(0); PutLong(0);
PutLong(0); PutLong(0);
// RgbQuad
if (gs.fColor)
for (x = 0; x < 16; x++) {
PutByte(RgbB(rgbbmp[x])); PutByte(RgbG(rgbbmp[x]));
PutByte(RgbR(rgbbmp[x])); PutByte(0);
}
else {
PutLong(0);
PutByte(255); PutByte(255); PutByte(255); PutByte(0);
}
// Data
for (y = gs.yWin-1; y >= 0; y--) {
value = 0;
for (x = 0; x < gs.xWin; x++) {
if ((x & (gs.fColor ? 7 : 31)) == 0 && x > 0) {
PutLong(value);
value = 0;
}
if (gs.fColor)
value |= (dword)FBmGet(gi.bm, x, y) << ((x & 7 ^ 1) << 2);
else
if (FBmGet(gi.bm, x, y))
value |= (dword)1 << (x & 31 ^ 7);
}
PutLong(value);
}
}
// Begin the work of creating a graphics file. Prompt for a filename if need
// be, and if valid, create the file and open it for writing.
flag BeginFileX()
{
char sz[cchSzMax];
if (us.fNoWrite)
return fFalse;
#ifdef WIN
if (gi.szFileOut == NULL)
return fFalse;
#endif
#ifndef WIN
if (gi.szFileOut == NULL && ((gs.ft == ftBmp && gs.chBmpMode == 'B') ||
#ifdef PS
gi.fEps ||
#endif
gs.ft == ftWmf || gs.ft == ftWire)) {
sprintf(sz, "(It is recommended to specify an extension of '.%s'.)\n",
gs.ft == ftBmp ? "bmp" :
#ifdef WIRE
(gs.ft == ftWire ? "dw" :
#endif
#ifdef PS
(gi.fEps ? "eps" : "wmf")
#else
"wmf"
#endif
#ifdef WIRE
)
#endif
);
PrintSzScreen(sz);
}
#endif // WIN
loop {
#ifndef WIN
if (gi.szFileOut == NULL) {
sprintf(sz, "Enter name of file to write %s to",
gs.ft == ftBmp ? "bitmap" : (gs.ft == ftPS ? "PostScript" :
(gs.ft == ftWmf ? "metafile" : "wireframe")));
InputString(sz, sz);
FCloneSz(sz, &gi.szFileOut);
}
#else
// If autosaving in potentially rapid succession, ensure the file isn't
// being opened by some other application before saving over it again.
if (wi.fAutoSave) {
if (wi.hMutex == NULL)
wi.hMutex = CreateMutex(NULL, fFalse, szAppName);
if (wi.hMutex != NULL)
WaitForSingleObject(wi.hMutex, 1000);
}
#endif
gi.file = fopen(gi.szFileOut, (gs.ft == ftBmp && gs.chBmpMode != 'B') ||
gs.ft == ftPS || gs.ft == ftWire ? "w" : "wb");
if (gi.file != NULL)
break;
#ifdef WIN
if (wi.fAutoSave)
break;
#endif
sprintf(sz, "Couldn't create output file: %s", gi.szFileOut);
PrintWarning(sz);
FCloneSz(NULL, &gi.szFileOut);
#ifdef WIN
break;
#endif
}
return gi.file != NULL;
}
// Finish up the work of creating a graphics file. This basically consists of
// just calling the appropriate routine to actually write the data in memory
// to a file for bitmaps and metafiles, although for PostScript just close the
// file as were already writing while creating the chart.
void EndFileX()
{
if (gi.file == NULL)
return;
if (gs.ft == ftBmp) {
PrintProgress("Writing chart bitmap to file.");
if (gs.chBmpMode == 'B') {
if (!gi.fBmp)
WriteBmp(gi.file);
else
WriteBmp2(&gi.bmp, gi.file);
} else if (gs.chBmpMode == 'A')
WriteAscii(gi.file);
else
WriteXBitmap(gi.file, gi.szFileOut, gs.chBmpMode);
}
#ifdef PS
else if (gs.ft == ftPS)
PsEnd();
#endif
#ifdef META
else if (gs.ft == ftWmf) {
PrintProgress("Writing metafile to file.");
WriteMeta(gi.file);
}
#endif
#ifdef WIRE
else if (gs.ft == ftWire) {
PrintProgress("Writing wireframe to file.");
WriteWire(gi.file);