~ubuntu-branches/debian/sid/pngphoon/sid

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
#include "moon.h"
#include "moondata.h"
#include "tws.h"
#include "phase.h"
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

static const unsigned char  leftmask[8] = { 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
static const unsigned char rightmask[8] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe };

static const unsigned char shades[16][8] = 
{
   { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff },
   { 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff },
   { 0x7f,0xff,0xdf,0xff,0xff,0xff,0xff,0xff },
   { 0x7f,0xff,0xdf,0xff,0xfe,0xff,0xff,0xff },
   { 0x7f,0xff,0xdf,0xff,0xfe,0xff,0xff,0xf7 },
   { 0x7f,0xfd,0xdf,0xff,0xfe,0xff,0xff,0xf7 },
   { 0x7f,0xfd,0xdf,0xfb,0xfe,0xff,0xff,0xf7 },
   { 0x7f,0xfd,0xdf,0xfb,0xfe,0xff,0x7f,0xf7 },
   { 0x7f,0xfd,0xdf,0xfb,0xfe,0xfb,0x7f,0xf7 },
   { 0x7f,0xfd,0xdf,0xfb,0xbe,0xfb,0x7f,0xf7 },
   { 0x7f,0xfd,0xdf,0xfb,0xbe,0xfb,0x5f,0xf7 },
   { 0x7f,0xfd,0xdf,0xfb,0xbe,0xfb,0x5f,0xf5 },
   { 0x7f,0xfd,0x5f,0xfb,0xbe,0xfb,0x5f,0xf5 },
   { 0x7f,0xf5,0x5f,0xfb,0xbe,0xfb,0x5f,0xf5 },
   { 0x7f,0xf5,0x5f,0xfb,0xae,0xfb,0x5f,0xf5 },
   { 0x5f,0xf5,0x5f,0xfb,0xae,0xfb,0x5f,0xf5 }
};


#define PI 3.14159265358979323846  /* assume not near black hole or in
                                      Tennessee */

moon_t *mooncreate()
{
   moon_t *moondata;
   
   moondata = (moon_t*)malloc(sizeof( moon_t ));
   moondata->bitmap = (png_bytep)moon_bits;
   moondata->width  = moon_width;
   moondata->height = moon_height;
   moondata->xbytes = (moon_width + 7) / 8;

   return moondata;
}


void moondestroy( moon_t *moon )
{
   free( moon );
   moon = NULL;
}


void mooncopy(image_t *image, moon_t *moondata, int cx, int cy, int blackflag)
{
   double jd = 0.0;
   double angphase = 0.0;
   double cphase = 0.0;
   double aom = 0.0;
   double cdist = 0.0;
   double cangdia = 0.0;
   double csund = 0.0;
   double csuang = 0.0;
   struct tws* t = 0;
   int r = moondata->width/2;
   int i = 0;
   register int x = 0;
   register int y = 0;
   int xleft = 0;
   int xright = 0;
   double fxleft = 0.0;
   double fxright = 0.0;
   double fy = 0.0;
   int bytexleft = 0;
   int bitxleft = 0;
   int bytexright = 0;
   int bitxright = 0;
   int moonoff = 0;
   int imageoff = 0;
   double cap = 0.0;
   double ratio = 0.0;
   int shadeindex = 0;
   unsigned char shade = 0;
   int xoffset = 0;
   int yoffset = 0;
   
   t = dtwstime();
   
   xoffset = (cx - moondata->width/2);
   yoffset = (cy - moondata->height/2);
   
   jd = jtime( t );
   
   angphase = phase( jd, &cphase, &aom, &cdist, &cangdia, &csund, &csuang );
   cap = cos( angphase );
   
   /* Hack to figure approximate earthlighting. */
   if ( cphase < 0.1 ) cphase = 0.1;
   if ( cphase > 0.9 ) cphase = 0.9;
   ratio = (1.0 - cphase) / cphase;        /* ratio varies from 9.0 to 0.111 */
   shadeindex = (int) ( ratio / 9.0 * 15.9999 );
   
   for ( i = 0; i < 2 * r; ++i )
   {
      y = moondata->height/2 - r + i;
      if ( y < 0 || y >= moondata->height )
      {
         continue;
      }
      fy = i - r;
      fxright = r * sqrt( 1.0 - ( fy * fy ) / ( r * r ) );
      fxleft = - fxright;

      if ( angphase >= 0.0 && angphase < M_PI )
      {
         fxright *= cap;
      }
      else
      {
         fxleft *= cap;
      }

      xleft = fxleft + moondata->width/2 + 0.5;
      xright = fxright + moondata->width/2 + 0.5;

      bytexleft = xleft / 8;
      bitxleft = xleft % 8;

      bytexright = xright / 8;
      bitxright = xright % 8;

      moonoff  = y * ( moondata->width ) / 8;
      imageoff = (y + yoffset) * image->xbytes + xoffset / 8;

      shade = blackflag ? shade = 0xff : shades[shadeindex][y % 8];

      /* first copy the stuff */
      for( x = 0; x < moondata->width/8; ++x )
      {
         if( moondata->bitmap[x + moonoff] )
         {
            image->bitmap[x + imageoff] = moondata->bitmap[x + moonoff];
         }
      }

      /* now, hack the bits */
      if( bytexleft == bytexright )
      {
         image->bitmap[bytexleft + imageoff] &=
               ~(leftmask[bitxleft] & shade & rightmask[bitxright]);
      }
      else
      {
         image->bitmap[bytexleft + imageoff] &= ~(leftmask[bitxleft] & shade);
         for( x = bytexleft + 1; x < bytexright; ++x )
         {
            image->bitmap[x + imageoff] &= ~shade;
         }
         image->bitmap[bytexright + imageoff] &= ~(rightmask[bitxright] & shade);
      }
   }
}