aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/evas/src/lib/engines/common/evas_regionbuf.c
blob: f381da5cc82e0a82cc044d469a88068490212da3 (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
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
#include "evas_common.h"

#if 0
Regionbuf *
evas_common_regionbuf_new(int w, int h)
{
   Regionbuf *rb;

   rb = calloc(1, sizeof(Regionbuf) + (h * sizeof(Regionspan)));
   if (!rb) return NULL;
   rb->spans = (Regionspan **)(rb + sizeof(Regionbuf));
   rb->w = w;
   rb->h = h;
   return rb;
}

void
evas_common_regionbuf_free(Regionbuf *rb)
{
   evas_common_regionbuf_clear(rb);
   free(rb);
}

void
evas_common_regionbuf_clear(Regionbuf *rb)
{
   int y;

   for (y = 0; y < rb->h; y++)
     {
	while (rb->spans[y])
	  {
	     Regionspan *span;

	     span = rb->spans[y];
	     rb->spans[y] = eina_inlist_remove(rb->spans[y], rb->spans[y]);
	     free(span);
	  }
     }
}

void
evas_common_regionbuf_span_add(Regionbuf *rb, int x1, int x2, int y)
{
   Regionspan *span, *span2, *nspan, *sp_start, *sp_stop;

   /* abort if outside */
   if ((y < 0) ||
       (y >= rb->h) ||
       (x2 < 0) ||
       (x1 >= rb->w)) return;
   /* clip to horiz bounds */
   if (x1 < 0) x1 = 0;
   if (x2 < (rb->w - 1)) x2 = rb->w - 1;
   sp_start = NULL;
   sp_stop = NULL;
   EINA_INLIST_FOREACH(rb->spans[y], span)
     {
	nspan = (Regionspan *)(EINA_INLIST_GET(span))->next;
	/* we dont know what t do with the span yet */
	if (!sp_start)
	  {
	     /* if new span starts before or on this span or just after
	      * with no gap */
	     if (x1 <= (span->x2 + 1))
	       sp_start = span;
	     /* if there is no next span */
	     if (!nspan)
	       {
		  sp_stop = span;
		  break;
	       }
	     /* if new span ends before the next span starts with a gap of
	      * 1 pixel (or more) */
	     else if (x2 < (nspan->x1 - 1))
	       {
		  sp_stop = span;
		  break;
	       }
	  }
	/* we already know it already starts before or in sp_start */
	else
	  {
	     /* there is no span after this one, so this has to be the stop */
	     if (!nspan)
	       {
		  sp_stop = span;
		  break;
	       }
	     /* if new span ends before the next span starts with a gap of
	      * 1 pixel (or more) */
	     else if (x2 < (nspan->x1 - 1))
	       {
		  sp_stop = span;
		  break;
	       }
	  }
     }
   /* sp_start is where the new span starts in or before */
   /* sp_stop is where the new span stops in or after */
   if ((sp_start) && (sp_stop))
     {
	/* same start and stop */
	if (sp_start == sp_stop)
	  {
	     if (x2 < (sp_start->x1 - 1))
	       {
		  span2 = calloc(1, sizeof(Regionspan));
		  span2->x1 = x1;
		  span2->x2 = x2;
		  rb->spans[y] = eina_inlist_prepend_relative(rb->spans[y], span2, sp_start);
		  return;
	       }
	     if (x1 < sp_start->x1)
	       sp_start->x1 = x1;
	     if (x2 > sp_start->x2)
	       sp_start->x2 = x2;
	     return;
	  }
	else
	  {
	     Eina_Inlist *l;

	     /* remove all nodes after sp_start and before_sp_stop because
	      * the new  */
	     for (l = (EINA_INLIST_GET(sp_start))->next; l != EINA_INLIST_GET(sp_stop);)
	       {
		  span = (Regionspan *)l;
		  l = l->next;
		  rb->spans[y] = eina_inlist_remove(rb->spans[y], span);
		  free(span);
	       }
	     /* remove the end span */
	     rb->spans[y] = eina_inlist_remove(rb->spans[y], sp_stop);
	     /* if the new span is before the start span - extend */
	     if (x1 < sp_start->x1)
	       sp_start->x1 = x1;
	     /* if it goes beyond the stop span - extend stop span */
	     if (x2 > sp_stop->x2)
	       sp_stop->x2 = x2;
	     /* extend start span to stop span */
	     sp_start->x2 = sp_stop->x2;
	     /* don't need stop span anymore */
	     free(sp_stop);
	     return;
	  }
     }
   /* no start AND stop... just append */
   span2 = calloc(1, sizeof(Regionspan));
   span2->x1 = x1;
   span2->x2 = x2;
   rb->spans[y] = eina_inlist_append(rb->spans[y], span2);
}

void
evas_common_regionbuf_span_del(Regionbuf *rb, int x1, int x2, int y)
{
   /* FIXME: del span */
   Regionspan *span, *span2, *nspan, *sp_start, *sp_stop;

   /* abort if outside */
   if ((y < 0) ||
       (y >= rb->h) ||
       (x2 < 0) ||
       (x1 >= rb->w)) return;
   /* clip to horiz bounds */
   if (x1 < 0) x1 = 0;
   if (x2 < (rb->w - 1)) x2 = rb->w - 1;
   sp_start = NULL;
   sp_stop = NULL;
   EINA_INLIST_FOREACH(rb->spans[y], span)
     {
	nspan = (Regionspan *)(EINA_INLIST_GET(l))->next;
	/* we dont know what t do with the span yet */
	if (!sp_start)
	  {
	     /* if new span starts before or on this span or just after
	      * with no gap */
	     if (x1 <= (span->x2))
	       sp_start = span;
	     /* if there is no next span */
	     if (!nspan)
	       {
		  sp_stop = span;
		  break;
	       }
	     /* if new span ends before the next span starts with a gap of
	      * 1 pixel (or more) */
	     else if (x2 < nspan->x1)
	       {
		  sp_stop = span;
		  break;
	       }
	  }
	/* we already know it already starts before or in sp_start */
	else
	  {
	     /* there is no span after this one, so this has to be the stop */
	     if (!nspan)
	       {
		  sp_stop = span;
		  break;
	       }
	     /* if new span ends before the next span starts with a gap of
	      * 1 pixel (or more) */
	     else if (x2 < nspan->x1)
	       {
		  sp_stop = span;
		  break;
	       }
	  }
     }
   /* sp_start is where the new span starts in or before */
   /* sp_stop is where the new span stops in or after */
   if ((sp_start) && (sp_stop))
     {
	/* same start and stop */
	if (sp_start == sp_stop)
	  {
	     /* if it ends before this the span start starts... return */
	     if (x2 < sp_start->x1)
	       return;
	     /* it starts on or before this span */
	     else if (x1 <= sp_start->x1)
	       {
		  /* right edge is within the span */
		  if (x2 < sp_start->x2)
		    {
		       sp_start->x2 = x2;
		       return;
		    }
		  else
		    {
		       rb->spans[y] = eina_inlist_remove(rb->spans[y], sp_start);
		       return;
		    }
	       }
	     /* it ends on or after the end of this span */
	     else if (x2 >= sp_start->x2)
	       {
		  /* it starts after the start */
		  if (x1 > sp_start->x1)
		    {
		       sp_start->x1 = x1;
		       return;
		    }
		  /* remove it all */
		  else
		    {
		       rb->spans[y] = eina_inlist_remove(rb->spans[y], sp_start);
		       return;
		    }
		  return;
	       }
	     /* this breaks the span into 2 */
	     else
	       {
		  span2 = calloc(1, sizeof(Regionspan));
		  span2->x1 = sp_start->x1;
		  span2->x2 = x1 - 1;
		  rb->spans[y] = eina_inlist_prepend_relative(rb->spans[y], span2, sp_start);
		  sp_start->x1 = x2 + 1;
		  return;
	       }
	  }
	else
	  {
	     Eina_Inlist *l;

	     /* remove all nodes after sp_start and before_sp_stop because
	      * the new  */
	     for (l = (EINA_INLIST_GET(sp_start))->next; l != EINA_INLIST_GET(sp_stop);)
	       {
		  span = (Regionspan *)l;
		  l = l->next;
		  rb->spans[y] = eina_inlist_remove(rb->spans[y], span);
		  free(span);
	       }
	     /* all of the start span is cut out */
	     if (x1 <= sp_start->x1)
	       {
		  rb->spans[y] = eina_inlist_remove(rb->spans[y], sp_start);
		  free(sp_start);
	       }
	     /* chup it off at the new span start */
	     else
	       sp_start->x2 = x1 - 1;
	     /* all of the end span is cut out */
	     if (x2 >= sp_stop->x2)
	       {
		  rb->spans[y] = eina_inlist_remove(rb->spans[y], sp_stop);
		  free(sp_stop);
	       }
	     /* chop it up at the end */
	     else
	       sp_stop->x1 = x2 + 1;
	     return;
	  }
     }
}

Tilebuf_Rect *
evas_common_regionbuf_rects_get(Regionbuf *rb)
{
   Tilebuf_Rect *rects = NULL, *r;
   int y;

   /* FIXME: take spans, make rects */
   for (y = 0; y < rb->h; y++)
     {
	Regionspan *sp_start;
	Eina_Inlist *l, *ll;

	for (l = EINA_INLIST_GET(rb->spans[y]); l;)
	  {
	     Regionspan *span;
	     int yy;

	     sp_start = (Regionspan *)l;
	     l = l->next;
	     rb->spans[y] = eina_inlist_remove(rb->spans[y], sp_start);
	     for (yy = y + 1; yy < rb->h; yy++)
	       {
		  int match = 0;

		  for (ll = EINA_INLIST_GET(rb->spans[yy]); ll;)
		    {
		       span = (Regionspan *)ll;
		       ll = ll->next;
		       if (span->x1 == sp_start->x1)
			 {
			    if ((span->x1 != sp_start->x1) ||
				(span->x2 != sp_start->x2))
			      {
				 goto coallate;
			      }
			    match = 1;
			    rb->spans[yy] = eina_inlist_remove(rb->spans[yy], span);
			    free(span);
			 }
		    }
		  if (!match) goto coallate;
	       }
	     coallate:
	     r = calloc(1, sizeof(Tilebuf_Rect));
	     r->x = sp_start->x1;
	     r->y = y;
	     r->w = sp_start->x2 - sp_start->x1 + 1;
	     r->h = yy - y;
	     rects = eina_inlist_append(rects, r);
	     free(sp_start);
	  }
     }
   evas_common_regionbuf_clear(rb);
   return rects;
}
#endif