summaryrefslogtreecommitdiff
path: root/src/lib/evas/common/evas_map_image_aa.c
blob: 79ff4a34eb1893cb170e2108e37db32ff5728fb9 (plain)
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
#define  READY_TX() \
{ \
  if (eidx == 0) \
    { \
       tx[0] = edge2.x; \
       tx[1] = spans[y].span[0].x[0]; \
    } \
  else \
    { \
       tx[0] = spans[y].span[0].x[1]; \
       tx[1] = edge2.x; \
    } \
}

#define READY_TX2() \
{ \
   if (eidx == 0) \
     { \
        tx2[0] = edge2.x; \
        tx2[1] = edge1.x; \
     } \
   else \
    { \
       tx2[0] = edge1.x; \
       tx2[1] = edge2.x; \
    } \
}

#define PUSH_EDGES(xx) \
{ \
   if (!leftover) \
     { \
        edge1.x = edge2.x; \
        edge1.y = edge2.y; \
        edge2.x = (xx); \
        edge2.y = y; \
     } \
   else \
     { \
        edge1.y = edge2.y; \
        edge2.y = y; \
     } \
   reset_tx2 = EINA_TRUE; \
}

//Vertical Inside Direction
#define VERT_INSIDE(rewind, y_advance) \
{ \
   int cov_range = edge2.y - edge1.y; \
   int coverage = (256 / (cov_range + 1)); \
   int ry; \
   int val; \
   for (ry = 1; ry < ((rewind) + 1); ry++) \
     { \
        int ridx = (y - ry) + (y_advance); \
        if (spans[ridx].aa_len[eidx] > 1) continue; \
        if (eidx == 1) \
          val = (256 - (coverage * (ry + (cov_range - (rewind))))); \
        else \
          val = (coverage * (ry + (cov_range - (rewind)))); \
        if ((spans[ridx].aa_len[eidx] == 0) || \
            (val < spans[ridx].aa_cov[eidx])) \
          spans[ridx].aa_cov[eidx] = val; \
        spans[ridx].aa_len[eidx] = 1; \
     } \
   prev_aa = 4; \
}

//Vertical Outside Direction
#define VERT_OUTSIDE(rewind, y_advance, cov_range) \
{ \
   int coverage = (256 / ((cov_range) + 1)); \
   int ry = 1; \
   for (; ry < ((rewind) + 1); ry++) \
     { \
        int ridx = (y - ry) + (y_advance); \
        if (spans[ridx].aa_len[(eidx)] > 1) continue; \
        spans[ridx].aa_len[(eidx)] = 1; \
        if (eidx == 1) \
          { \
             spans[ridx].aa_cov[(eidx)] = \
                (coverage * (ry + (cov_range - (rewind)))); \
          } \
        else \
          { \
             spans[ridx].aa_cov[(eidx)] = \
                (256 - (coverage * (ry + ((cov_range) - (rewind))))); \
          } \
     } \
   prev_aa = 2; \
}

//Horizontal Inside Direction
#define HORIZ_INSIDE(yy, xx, xx2) \
{ \
   if (((xx) - (xx2)) > spans[(yy)].aa_len[(eidx)]) \
     { \
        spans[(yy)].aa_len[(eidx)] = ((xx) - (xx2)); \
        spans[(yy)].aa_cov[(eidx)] = (256 / (spans[(yy)].aa_len[(eidx)] + 1)); \
     } \
}

//Horizontal Outside Direction
#define HORIZ_OUTSIDE(yy, xx, xx2) \
{ \
   if (((xx) - (xx2)) > spans[(yy)].aa_len[(eidx)]) \
     { \
        spans[(yy)].aa_len[(eidx)] = ((xx) - (xx2)); \
        spans[(yy)].aa_cov[(eidx)] = (256 / (spans[(yy)].aa_len[(eidx)] + 1)); \
     } \
}

static inline DATA32
_aa_coverage_apply(Line *line, int ww, int w, DATA32 val)
{
   //Left Edge Anti Anliasing
   if ((w - line->aa_len[0]) < ww)
     {
        return MUL_256((line->aa_cov[0] * (w - ww + 1)), val);
     }
   //Right Edge Anti Aliasing
   if (line->aa_len[1] >= ww)
     {
        return MUL_256(256 - (line->aa_cov[1] * (line->aa_len[1] - ww + 1)),
                       val);
     }
   return val;
}

void
_calc_aa_edges_internal(Line *spans, int eidx, int ystart, int yend)
{
   int y;
   Evas_Coord_Point edge1 = { -1, -1 }; //prev-previous edge pixel
   Evas_Coord_Point edge2 = { -1, -1 }; //previous edge pixel

   /* store larger to tx[0] between prev and current edge's x positions. */
   int tx[2] = {0, 0};

   /* store lager to tx2[0] between edge1 and edge2's x positions. */
   int tx2[2] = {0, 0};

   /* previous edge anti-aliased type.
      2: vertical outside
      4: vertical inside */
   int prev_aa = 0;

   Eina_Bool reset_tx2 = EINA_TRUE;

   yend -= ystart;

   //Find Start Edge
   for (y = 0; y < yend; y++)
     {
        if (spans[y].span[0].x[0] == -1) continue;
        edge1.x = edge2.x = spans[y].span[0].x[eidx];
        edge1.y = edge2.y = y;
        break;
     }

   //Calculates AA Edges
   for (y++; y <= yend; y++)
     {
       Eina_Bool leftover = EINA_FALSE;

       if (spans[y].span[0].x[eidx] == -1) leftover = EINA_TRUE;

       if (!leftover) READY_TX()

       //Case1. Outside Incremental
       if (tx[0] > tx[1])
         {
            //Horizontal Edge
            if ((y - edge2.y) == 1)
              {
                  HORIZ_OUTSIDE(y, tx[0], tx[1])
              }
            //Vertical Edge
            else if (tx[0] > tx[1])
              {
                 VERT_OUTSIDE((y - edge2.y), 0, (y - edge2.y))

                 //Just in case: 1 pixel alias next to vertical edge?
                 if (abs(spans[(y + 1)].span[0].x[eidx] -
                         spans[y].span[0].x[eidx]) >= 1)
                   {
                      HORIZ_OUTSIDE(y, tx[0], tx[1])
                   }
              }
            PUSH_EDGES(spans[y].span[0].x[eidx])
         }
       //Case2. Inside Incremental
       else if (tx[1] > tx[0])
         {
            //Just in case: direction is reversed at the outside vertical edge?
            if (prev_aa == 2)
              {
                 VERT_OUTSIDE((y - edge2.y), 0, (y - edge2.y))
                 edge1.x = spans[y - 1].span[0].x[eidx];
                 edge1.y = y - 1;
                 edge2.x = spans[y].span[0].x[eidx];
                 edge2.y = y;
              }
            else
              PUSH_EDGES(spans[y].span[0].x[eidx])

            /* Find next edge. We go forward 2 more index since this logic
               computes aa edges by looking back in advance 2 spans. */
            for (y++; y <= (yend + 2); y++)
              {
                 leftover = EINA_FALSE;

                 if ((spans[y].span[0].x[eidx] == -1) || (y > yend))
                   leftover = EINA_TRUE;

                 if (!leftover) READY_TX()
                 if (reset_tx2) READY_TX2()

                 //Case 1. Inside Direction
                 if (tx[1] > tx[0])
                   {
                      //Horizontal Edge
                      if ((edge2.y - edge1.y) == 1)
                        {
                           HORIZ_INSIDE(edge1.y, tx2[0], tx2[1]);
                        }
                      //Vertical Edge
                      else if ((tx2[0] - tx2[1]) == 1)
                        {
                           VERT_INSIDE((edge2.y - edge1.y), -(y - edge2.y))
                        }
                      //Just in case: Right Side Square Edge...?
                      else if (prev_aa == 4)
                        {
                           VERT_INSIDE((edge2.y - edge1.y), -(y - edge2.y))
                           if ((y - edge2.y) == 1)
                             {
                                HORIZ_INSIDE((edge2.y - 1), edge2.x,
                                             spans[y].span[0].x[eidx]);
                             }
                        }
                      PUSH_EDGES(spans[y].span[0].x[eidx])
                   }
                 //Case 2. Reversed. Outside Direction
                 else if (tx[1] < tx[0])
                   {
                      //Horizontal Edge
                      if ((edge2.y - edge1.y) == 1)
                        HORIZ_INSIDE(edge1.y, tx2[0], tx2[1])
                      //Vertical Edge
                      else
                        VERT_INSIDE((edge2.y - edge1.y), -(y - edge2.y))

                      PUSH_EDGES(spans[y].span[0].x[eidx])
                      break;
                   }
              }
         }
     }

   y = yend;

   //Leftovers for verticals.
   if (prev_aa == 2)
     {
      if (((eidx == 0) && (edge1.x > edge2.x)) ||
          ((eidx == 1) && (edge1.x < edge2.x)))
        VERT_OUTSIDE((y - edge2.y + 1), 1, (edge2.y - edge1.y));
     }
   else if (prev_aa == 4)
     {
        if (((eidx == 0) && (edge1.x < edge2.x)) ||
           ((eidx == 1) && (edge1.x > edge2.x)))
          {
             VERT_INSIDE((edge2.y - edge1.y), -(y - edge2.y))
             VERT_INSIDE((y - edge2.y) + 1, 1);
          }
     }
}

static void
_calc_aa_edges(Line *spans, int ystart, int yend)
{
   //FIXME: support 2 span case.

   //left side
   _calc_aa_edges_internal(spans, 0, ystart, yend);
   //right side
   _calc_aa_edges_internal(spans, 1, ystart, yend);
}