File: bbox.c

package info (click to toggle)
swftools 0.9.2%2Bgit20130725-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,680 kB
  • ctags: 17,348
  • sloc: ansic: 108,712; sh: 8,494; cpp: 8,040; yacc: 2,260; lisp: 904; makefile: 601; python: 300
file content (481 lines) | stat: -rw-r--r-- 10,891 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include "../types.h"
#include "../mem.h"

typedef struct _ibbox {
    int xmin,ymin,xmax,ymax;
    struct _ibbox*next;
} ibbox_t;

ibbox_t* ibbox_new(int x1, int y1, int x2, int y2)
{
    ibbox_t*b = (ibbox_t*)rfx_calloc(sizeof(ibbox_t)); 
    b->xmin = x1;
    b->ymin = y1;
    b->xmax = x2;
    b->ymax = y2;
    return b;
}

void ibbox_destroy(ibbox_t*b)
{
    while(b) {
	ibbox_t*next = b->next;
	free(b);
	b = next;
    }
}

ibbox_t*get_bitmap_bboxes_simple(unsigned char*alpha, int width, int height, int rowsize)
{
    int ymin = -1;
    int ymax = -1;
    int xmin = width;
    int xmax = 0;

    int x,y;
    for(y=0;y<height;y++) {
	unsigned char*a = &alpha[y*rowsize];
	for(x=0;x<width;x++) {
	    if(a[x]) break;
	}
	int left = x; //first occupied pixel from left
	int right = x+1; //last non-occupied pixel from right
	for(;x<width;x++) {
	    if(a[x]) right=x+1;
	}

	if(left!=width) {
	    if(ymin<0) 
		ymin=y;
	    ymax=y+1;
	    if(left<xmin) xmin = left;
	    if(right>xmax) xmax = right;
	}
    }
    ibbox_t* bbox = 0;
    if(xmin<xmax || ymin<ymax) {
	bbox = ibbox_new(xmin, ymin, xmax, ymax);
    }
    return bbox;
}

typedef struct _head {
    ptroff_t magic;
    ibbox_t bbox;
    int nr;
    int pos;
    int rank;
    int x,y;
    char seen;
    struct _head*next;
    struct _head*prev;
} head_t;

typedef struct _context {
    void**group;
    unsigned char*alpha;
    int rowsize;
    int width;
    int height;
    head_t*heads;
    int count;
} context_t;

#define HEAD_MAGIC ((ptroff_t)-1)

static head_t*head_new(context_t*context, int x, int y)
{
    int pos = context->width*y+x;
    head_t*h = rfx_calloc(sizeof(head_t));
    h->magic = HEAD_MAGIC;
    h->nr = context->count++;
    h->pos = pos;
    h->x = x;
    h->y = y;
    h->bbox.xmin = h->bbox.xmax = x;
    h->bbox.ymin = h->bbox.ymax = y;
    h->next = context->heads;
    context->heads = h;
    if(h->next) {
	h->next->prev = h;
    }
    return h;
}

static void head_delete(context_t*context, head_t*h)
{
    if(h->prev) {
	h->prev->next = h->next;
    }
    if(h->next) {
	h->next->prev = h->prev;
    }
    if(h==context->heads) {
	assert(!h->prev);
	context->heads = h->next;
    }
    free(h);
}

#define POINTS_TO_HEAD(ptr) (((head_t*)(ptr))->magic==HEAD_MAGIC)

static inline void link_to(context_t*context, int from, int to)
{
    // path compression
    void**data = context->group;
    int head = to;
    assert(data[head]);
    while(!POINTS_TO_HEAD(data[head])) {
	assert(data[head]!=(void*)&data[head]); // check that we're not in an infinite loop
	head=(void**)data[head]-(void**)data;
    }
    head_t*h = (head_t*)data[head];
    int x = from%context->width;
    int y = from/context->width;
    if(x < h->bbox.xmin) h->bbox.xmin = x;
    if(y < h->bbox.ymin) h->bbox.ymin = y;
    if(x > h->bbox.xmax) h->bbox.xmax = x;
    if(y > h->bbox.ymax) h->bbox.ymax = y;
    
    data[from] = (void*)&data[head];
}
static char ibbox_does_overlap(ibbox_t*b1, ibbox_t*b2)
{
    if(b1->xmax < b2->xmin) return 0;
    if(b2->xmax < b1->xmin) return 0;
    if(b1->ymax < b2->ymin) return 0;
    if(b2->ymax < b1->ymin) return 0;
    return 1;
}
static void ibbox_expand(ibbox_t*src, ibbox_t*add) 
{
    if(add->xmin < src->xmin)
	src->xmin = add->xmin;
    if(add->ymin < src->ymin)
	src->ymin = add->ymin;
    if(add->xmax > src->xmax)
	src->xmax = add->xmax;
    if(add->ymax > src->ymax)
	src->ymax = add->ymax;
}
static inline void merge(context_t*context, int set1, int set2)
{
    void**data = context->group;
    assert(data[set1]);
    assert(data[set2]);
    int head1 = set1;
    int head2 = set2;
    while(!POINTS_TO_HEAD(data[head1])) {
	head1=(void**)data[head1]-(void**)data;
    }
    while(!POINTS_TO_HEAD(data[head2])) {
	head2=(void**)data[head2]-(void**)data;
    }
    head_t*h1 = (head_t*)data[head1];
    head_t*h2 = (head_t*)data[head2];
    if(h1==h2)
	return;

    if(h1->rank>h2->rank) {
	h1->rank++;
	ibbox_expand(&h1->bbox,&h2->bbox);
	data[head2] = (void*)&data[head1];
	head_delete(context, h2);
    } else {
	h2->rank++;
	ibbox_expand(&h2->bbox,&h1->bbox);
	data[head1] = (void*)&data[head2];
	head_delete(context, h1);
    }
}

ibbox_t ibbox_clip(ibbox_t* outer, ibbox_t* inner)
{
    ibbox_t i = {inner->xmin, inner->ymin, inner->xmax, inner->ymax, 0};
    if(i.xmax > outer->xmax) i.xmax = outer->xmax;
    if(i.ymax > outer->ymax) i.ymax = outer->ymax;
    if(i.xmax < outer->xmin) i.xmax = outer->xmin;
    if(i.ymax < outer->ymin) i.ymax = outer->ymin;
    
    if(i.xmin > outer->xmax) i.xmin = outer->xmax;
    if(i.ymin > outer->ymax) i.ymin = outer->ymax;
    if(i.xmin < outer->xmin) i.xmin = outer->xmin;
    if(i.ymin < outer->ymin) i.ymin = outer->ymin;
    return i;
}

static void** annotate(context_t*context)
{
    unsigned char*alpha = context->alpha;
    int width = context->width;
    int height = context->height;
    void** group = rfx_calloc(width*height*sizeof(void*));
    context->group = group;
    int x,y;

    for(x=1;x<width;x++) {
	if(alpha[x]) {
	    if(group[x-1])
		link_to(context,x,x-1);
	    else
		group[x]=head_new(context,x,0);
	}
    }
    int pos = 0;
    int apos = 0;
    for(y=1;y<height;y++) {
	pos += width;
	apos += context->rowsize;
	if(alpha[apos]) {
	    if(group[pos-width])
		link_to(context,pos,pos-width);
	    else
		group[pos]=head_new(context,0,y);
	}
	for(x=1;x<width;x++) {
	    /* once this code is stable we should copy&paste it
	       out of the loop, change the loop end to width-1 and
	       add the pos-width+1 case */
	    if(alpha[apos+x]) {
		if(group[pos+x-width]) {
		    link_to(context,pos+x,pos+x-width);
		    if(group[pos+x-1])
			merge(context,pos+x,pos+x-1);
		} else if(group[pos+x-1]) {
		    link_to(context,pos+x,pos+x-1);
		} else if(group[pos+x-width-1]) {
		    link_to(context,pos+x,pos+x-width-1);
		} else {
		    group[pos+x]=head_new(context,x,y);
		}
	    }
	}
    }
    return group;
}

static void overlap_bboxes(context_t*context)
{
    char changed;
    do {
	head_t*h1 = context->heads;
	changed = 0;
	while(h1) {
	    head_t*next = h1->next;
	    head_t*h2 = context->heads;
	    while(h2) {
		if(h1!=h2) {
		    if(ibbox_does_overlap(&h1->bbox, &h2->bbox)) {
			merge(context, h1->pos, h2->pos);
			changed = 1;
			break;
		    }
		}
		h2 = h2->next;
	    }
	    h1 = next;
	}
    } while(changed);
}

typedef struct _circle_coord {
    S16 x,y;
} circle_coord_t;

static int compare_circle_coord(const void *_v1, const void *_v2)
{
    circle_coord_t*v1=(circle_coord_t*)_v1;
    circle_coord_t*v2=(circle_coord_t*)_v2;
    return (v1->x*v1->x + v1->y*v1->y) - (v2->x*v2->x + v2->y*v2->y);
}

static head_t* search_vicinity(context_t*context, head_t*h, int max_radius, double*cos, double*sin)
{
    static circle_coord_t*circle_order = 0;
    static int circle_order_size = 0;
    
    if(!circle_order) {
	circle_order_size = (max_radius*(max_radius+1))/2;
	circle_order = malloc(sizeof(circle_coord_t)*circle_order_size);
	int x,y;
	int i = 0;
	for(y=0;y<max_radius;y++) {
	    for(x=0;x<=y;x++) {
		circle_order[i].x=x;
		circle_order[i].y=y;
		i++;
	    }
	}
	assert(i==circle_order_size);
	qsort(circle_order, circle_order_size, sizeof(circle_coord_t), compare_circle_coord);
    }

    int t;
    void**data = context->group;
    int signx[4] = {-1,1,-1,1};
    int signy[4] = {-1,-1,1,1};
    for(t=1;t<circle_order_size;t++) {
	int xx = circle_order[t].x;
	int yy = circle_order[t].y;
	int s;
	for(s=0;s<4;s++) {
	    int x=h->x+xx*signx[s];
	    int y=h->y+yy*signy[s];
	    if(x>=0 && y>=0 && x<context->width && y<context->height) {
		int pos = y*context->width+x;
		if(data[pos]) {
		    while(!POINTS_TO_HEAD(data[pos])) {
			pos=(void**)data[pos]-(void**)data;
		    }
		    head_t*new_head = (head_t*)data[pos];
		    if(new_head != h) {
			return new_head;
		    }
		}
	    }
	}
    }
    return 0;
}

static void fix_small_boxes(context_t*context)
{
    double sintab[256];
    double costab[256];
    int t;
    for(t=0;t<256;t++) {
	sintab[t] = sin(t*M_PI/128);
	costab[t] = cos(t*M_PI/128);
    }
	
    head_t*h = context->heads;
    while(h) {
	h->seen = 0;
	h = h->next;
    }

    char changed;
    do {
	changed = 0;
	head_t*h = context->heads;
	while(h) {
	    head_t*next = h->next;
	    if(!h->seen) {
		if(h->bbox.xmax - h->bbox.xmin < 32
		|| h->bbox.ymax - h->bbox.ymin < 32) {
		    head_t*other = search_vicinity(context, h, 64, costab, sintab);
		    if(other) {
			merge(context, h->pos, other->pos);
			changed = 1;
			break;
		    } else {
			//printf("nothing in the vicinity of %d,%d,%d,%d\n", h->bbox);
			h->seen = 1;
		    }
		} /*else {
		    printf("area %d,%d,%d,%d is large enough (%dx%d)\n", 
			    h->bbox.xmin,
			    h->bbox.ymin,
			    h->bbox.xmax,
			    h->bbox.ymax,
			    h->bbox.xmax - h->bbox.xmin,
			    h->bbox.ymax - h->bbox.ymin);
		} */
	    }
	    h = next;
	}
    } while(changed);
}

static void display(context_t*context)
{
    int width = context->width;
    int height = context->height;
    void**group = context->group;

    int x,y;
    for(y=0;y<height;y++) {
	for(x=0;x<width;x++) {
	    if(!group[y*width+x]) {
		printf(" -- ");
	    } else if(POINTS_TO_HEAD(group[y*width+x])) {
		printf("g%02d ", ((head_t*)group[y*width+x])->nr);
	    } else {
		printf("x%02d ", (void**)group[y*width+x]-(void**)group);
	    }
	}
	printf("\n");
    }

    head_t*h = context->heads;
    while(h) {
	printf("head: %d\n", h->nr);
	printf(" pos: %d/%d\n", h->pos%width, h->pos/width);
	printf(" bbox: [%d/%d,%d/%d]\n", h->bbox.xmin, h->bbox.ymin, h->bbox.xmax, h->bbox.ymax);
	h = h->next;
    }
}

ibbox_t*get_bitmap_bboxes(unsigned char*alpha, int width, int height, int rowsize)
{
    int size = width*height;
    if(width<=1 || height<=1)
	return get_bitmap_bboxes_simple(alpha, width, height, rowsize);
    
    context_t context;
    context.alpha = alpha;
    context.rowsize = rowsize;
    context.width = width;
    context.height = height;
    context.heads = 0;
    context.count = 1;

    void**group = annotate(&context);
    fix_small_boxes(&context);
    overlap_bboxes(&context);
#ifdef MAIN
    display(&context);
#endif

    ibbox_t*bboxes = 0;

    head_t*h = context.heads;
    while(h) {
	head_t*next = h->next;
	ibbox_t*bbox = malloc(sizeof(ibbox_t));
	memcpy(bbox, &h->bbox, sizeof(ibbox_t));

	/* ibbox_t defines the open upper bound */
	bbox->xmax++;
	bbox->ymax++;

	bbox->next = bboxes;
	bboxes = bbox;
	free(h);
	h = next;
    }
    free(context.group);
    return bboxes;
}

#ifdef MAIN
int main(int argn, char*argv[])
{
    unsigned char alpha[8*8]=
	"\0\0\1\0\0\0\0\0"
	"\1\0\0\1\0\1\0\0"
	"\0\0\0\0\0\0\1\0"
	"\0\0\1\0\1\0\0\0"
	"\1\0\1\0\1\0\0\0"
	"\1\0\1\1\1\0\0\1"
	"\1\0\0\0\0\0\1\0"
	"\1\1\1\0\0\0\0\0";

    get_bitmap_bboxes(alpha, 8,8, 8);
}
#endif