9 typedef struct _ibbox {
10 int xmin,ymin,xmax,ymax;
14 ibbox_t* ibbox_new(int x1, int y1, int x2, int y2)
16 ibbox_t*b = (ibbox_t*)rfx_calloc(sizeof(ibbox_t));
24 void ibbox_destroy(ibbox_t*b)
27 ibbox_t*next = b->next;
33 ibbox_t*get_bitmap_bboxes_simple(unsigned char*alpha, int width, int height)
41 for(y=0;y<height;y++) {
42 unsigned char*a = &alpha[y*width];
43 for(x=0;x<width;x++) {
46 int left = x; //first occupied pixel from left
47 int right = x+1; //last non-occupied pixel from right
56 if(left<xmin) xmin = left;
57 if(right>xmax) xmax = right;
61 if(xmin<xmax || ymin<ymax) {
62 bbox = ibbox_new(xmin, ymin, xmax, ymax);
67 typedef struct _head {
79 typedef struct _context {
88 #define HEAD_MAGIC ((ptroff_t)-1)
90 static head_t*head_new(context_t*context, int x, int y)
92 int pos = context->width*y+x;
93 head_t*h = rfx_calloc(sizeof(head_t));
94 h->magic = HEAD_MAGIC;
95 h->nr = context->count++;
99 h->bbox.xmin = h->bbox.xmax = x;
100 h->bbox.ymin = h->bbox.ymax = y;
101 h->next = context->heads;
109 static void head_delete(context_t*context, head_t*h)
112 h->prev->next = h->next;
115 h->next->prev = h->prev;
117 if(h==context->heads) {
119 context->heads = h->next;
124 #define POINTS_TO_HEAD(ptr) (((head_t*)(ptr))->magic==HEAD_MAGIC)
126 static inline void link_to(context_t*context, int from, int to)
129 void**data = context->group;
132 while(!POINTS_TO_HEAD(data[head])) {
133 assert(data[head]!=(void*)&data[head]); // check that we're not in an infinite loop
134 head=(void**)data[head]-(void**)data;
136 head_t*h = (head_t*)data[head];
137 int x = from%context->width;
138 int y = from/context->width;
139 if(x < h->bbox.xmin) h->bbox.xmin = x;
140 if(y < h->bbox.ymin) h->bbox.ymin = y;
141 if(x > h->bbox.xmax) h->bbox.xmax = x;
142 if(y > h->bbox.ymax) h->bbox.ymax = y;
144 data[from] = (void*)&data[head];
146 static char ibbox_does_overlap(ibbox_t*b1, ibbox_t*b2)
148 if(b1->xmax < b2->xmin) return 0;
149 if(b2->xmax < b1->xmin) return 0;
150 if(b1->ymax < b2->ymin) return 0;
151 if(b2->ymax < b1->ymin) return 0;
154 static void ibbox_expand(ibbox_t*src, ibbox_t*add)
156 if(add->xmin < src->xmin)
157 src->xmin = add->xmin;
158 if(add->ymin < src->ymin)
159 src->ymin = add->ymin;
160 if(add->xmax > src->xmax)
161 src->xmax = add->xmax;
162 if(add->ymax > src->ymax)
163 src->ymax = add->ymax;
165 static inline void merge(context_t*context, int set1, int set2)
167 void**data = context->group;
172 while(!POINTS_TO_HEAD(data[head1])) {
173 head1=(void**)data[head1]-(void**)data;
175 while(!POINTS_TO_HEAD(data[head2])) {
176 head2=(void**)data[head2]-(void**)data;
178 head_t*h1 = (head_t*)data[head1];
179 head_t*h2 = (head_t*)data[head2];
183 if(h1->rank>h2->rank) {
185 ibbox_expand(&h1->bbox,&h2->bbox);
186 data[head2] = (void*)&data[head1];
187 head_delete(context, h2);
190 ibbox_expand(&h2->bbox,&h1->bbox);
191 data[head1] = (void*)&data[head2];
192 head_delete(context, h1);
196 static void** annotate(context_t*context)
198 unsigned char*alpha = context->alpha;
199 int width = context->width;
200 int height = context->height;
201 void** group = rfx_calloc(width*height*sizeof(void*));
202 context->group = group;
205 for(x=1;x<width;x++) {
208 link_to(context,x,x-1);
210 group[x]=head_new(context,x,0);
214 for(y=1;y<height;y++) {
218 link_to(context,pos,pos-width);
220 group[pos]=head_new(context,0,y);
222 for(x=1;x<width;x++) {
223 /* once this code is stable we should copy&paste it
224 out of the loop, change the loop end to width-1 and
225 add the pos-width+1 case */
227 if(group[pos+x-width]) {
228 link_to(context,pos+x,pos+x-width);
230 merge(context,pos+x,pos+x-1);
231 } else if(group[pos+x-1]) {
232 link_to(context,pos+x,pos+x-1);
233 } else if(group[pos+x-width-1]) {
234 link_to(context,pos+x,pos+x-width-1);
236 group[pos+x]=head_new(context,x,y);
244 static void overlap_bboxes(context_t*context)
248 head_t*h1 = context->heads;
251 head_t*next = h1->next;
252 head_t*h2 = context->heads;
255 if(ibbox_does_overlap(&h1->bbox, &h2->bbox)) {
256 merge(context, h1->pos, h2->pos);
268 typedef struct _circle_coord {
272 static int compare_circle_coord(const void *_v1, const void *_v2)
274 circle_coord_t*v1=(circle_coord_t*)_v1;
275 circle_coord_t*v2=(circle_coord_t*)_v2;
276 return (v1->x*v1->x + v1->y*v1->y) - (v2->x*v2->x + v2->y*v2->y);
279 static head_t* search_vicinity(context_t*context, head_t*h, int max_radius, double*cos, double*sin)
281 static circle_coord_t*circle_order = 0;
282 static int circle_order_size = 0;
285 circle_order_size = (max_radius*(max_radius+1))/2;
286 circle_order = malloc(sizeof(circle_coord_t)*circle_order_size);
289 for(y=0;y<max_radius;y++) {
296 assert(i==circle_order_size);
297 qsort(circle_order, circle_order_size, sizeof(circle_coord_t), compare_circle_coord);
301 void**data = context->group;
302 int signx[4] = {-1,1,-1,1};
303 int signy[4] = {-1,-1,1,1};
304 for(t=1;t<circle_order_size;t++) {
305 int xx = circle_order[t].x;
306 int yy = circle_order[t].y;
309 int x=h->x+xx*signx[s];
310 int y=h->y+yy*signy[s];
311 if(x>=0 && y>=0 && x<context->width && y<context->height) {
312 int pos = y*context->width+x;
314 while(!POINTS_TO_HEAD(data[pos])) {
315 pos=(void**)data[pos]-(void**)data;
317 head_t*new_head = (head_t*)data[pos];
328 static void fix_small_boxes(context_t*context)
334 sintab[t] = sin(t*M_PI/128);
335 costab[t] = cos(t*M_PI/128);
338 head_t*h = context->heads;
347 head_t*h = context->heads;
349 head_t*next = h->next;
351 if(h->bbox.xmax - h->bbox.xmin < 32
352 || h->bbox.ymax - h->bbox.ymin < 32) {
353 head_t*other = search_vicinity(context, h, 64, costab, sintab);
355 merge(context, h->pos, other->pos);
359 //printf("nothing in the vicinity of %d,%d,%d,%d\n", h->bbox);
363 printf("area %d,%d,%d,%d is large enough (%dx%d)\n",
368 h->bbox.xmax - h->bbox.xmin,
369 h->bbox.ymax - h->bbox.ymin);
377 static void display(context_t*context)
379 int width = context->width;
380 int height = context->height;
381 void**group = context->group;
384 for(y=0;y<height;y++) {
385 for(x=0;x<width;x++) {
386 if(!group[y*width+x]) {
388 } else if(POINTS_TO_HEAD(group[y*width+x])) {
389 printf("g%02d ", ((head_t*)group[y*width+x])->nr);
391 printf("x%02d ", (void**)group[y*width+x]-(void**)group);
397 head_t*h = context->heads;
399 printf("head: %d\n", h->nr);
400 printf(" pos: %d/%d\n", h->pos%width, h->pos/width);
401 printf(" bbox: [%d/%d,%d/%d]\n", h->bbox.xmin, h->bbox.ymin, h->bbox.xmax, h->bbox.ymax);
406 ibbox_t*get_bitmap_bboxes(unsigned char*alpha, int width, int height)
408 int size = width*height;
409 if(width<=1 || height<=1)
410 return get_bitmap_bboxes_simple(alpha, width, height);
413 context.alpha = alpha;
414 context.width = width;
415 context.height = height;
419 void**group = annotate(&context);
420 fix_small_boxes(&context);
421 overlap_bboxes(&context);
428 head_t*h = context.heads;
430 head_t*next = h->next;
431 ibbox_t*bbox = malloc(sizeof(ibbox_t));
432 memcpy(bbox, &h->bbox, sizeof(ibbox_t));
434 /* ibbox_t defines the open upper bound */
448 int main(int argn, char*argv[])
450 unsigned char alpha[8*8]=
460 get_bitmap_bboxes(alpha, 8,8);