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;
123 #define POINTS_TO_HEAD(ptr) (((head_t*)(ptr))->magic==HEAD_MAGIC)
125 static inline void link_to(context_t*context, int from, int to)
128 void**data = context->group;
131 while(!POINTS_TO_HEAD(data[head])) {
132 assert(data[head]!=(void*)&data[head]); // check that we're not in an infinite loop
133 head=(void**)data[head]-(void**)data;
135 head_t*h = (head_t*)data[head];
136 int x = from%context->width;
137 int y = from/context->width;
138 if(x < h->bbox.xmin) h->bbox.xmin = x;
139 if(y < h->bbox.ymin) h->bbox.ymin = y;
140 if(x > h->bbox.xmax) h->bbox.xmax = x;
141 if(y > h->bbox.ymax) h->bbox.ymax = y;
143 data[from] = (void*)&data[head];
145 static char ibbox_does_overlap(ibbox_t*b1, ibbox_t*b2)
147 if(b1->xmax < b2->xmin) return 0;
148 if(b2->xmax < b1->xmin) return 0;
149 if(b1->ymax < b2->ymin) return 0;
150 if(b2->ymax < b1->ymin) return 0;
153 static void ibbox_expand(ibbox_t*src, ibbox_t*add)
155 if(add->xmin < src->xmin)
156 src->xmin = add->xmin;
157 if(add->ymin < src->ymin)
158 src->ymin = add->ymin;
159 if(add->xmax > src->xmax)
160 src->xmax = add->xmax;
161 if(add->ymax > src->ymax)
162 src->ymax = add->ymax;
164 static inline void merge(context_t*context, int set1, int set2)
166 void**data = context->group;
171 while(!POINTS_TO_HEAD(data[head1])) {
172 head1=(void**)data[head1]-(void**)data;
174 while(!POINTS_TO_HEAD(data[head2])) {
175 head2=(void**)data[head2]-(void**)data;
177 head_t*h1 = (head_t*)data[head1];
178 head_t*h2 = (head_t*)data[head2];
182 if(h1->rank>h2->rank) {
184 ibbox_expand(&h1->bbox,&h2->bbox);
185 data[head2] = (void*)&data[head1];
186 head_delete(context, h2);
189 ibbox_expand(&h2->bbox,&h1->bbox);
190 data[head1] = (void*)&data[head2];
191 head_delete(context, h1);
195 static void** annotate(context_t*context)
197 unsigned char*alpha = context->alpha;
198 int width = context->width;
199 int height = context->height;
200 void** group = rfx_calloc(width*height*sizeof(void*));
201 context->group = group;
204 for(x=1;x<width;x++) {
207 link_to(context,x,x-1);
209 group[x]=head_new(context,x,0);
213 for(y=1;y<height;y++) {
217 link_to(context,pos,pos-width);
219 group[pos]=head_new(context,0,y);
221 for(x=1;x<width;x++) {
222 /* once this code is stable we should copy&paste it
223 out of the loop, change the loop end to width-1 and
224 add the pos-width+1 case */
226 if(group[pos+x-width]) {
227 link_to(context,pos+x,pos+x-width);
229 merge(context,pos+x,pos+x-1);
230 } else if(group[pos+x-1]) {
231 link_to(context,pos+x,pos+x-1);
232 } else if(group[pos+x-width-1]) {
233 link_to(context,pos+x,pos+x-width-1);
235 group[pos+x]=head_new(context,x,y);
243 static void overlap_bboxes(context_t*context)
247 head_t*h1 = context->heads;
250 head_t*next = h1->next;
251 head_t*h2 = context->heads;
254 if(ibbox_does_overlap(&h1->bbox, &h2->bbox)) {
255 merge(context, h1->pos, h2->pos);
267 static head_t* search_vicinity(context_t*context, head_t*h, int max_radius, double*cos, double*sin)
270 void**data = context->group;
271 /* FIXME: this function isn't very efficient. We need a better way to iterate
272 through all points, sorted by distance, in the neighborhood of a coordinate */
273 for(t=3;t<max_radius*2;t++) {
276 int x=h->x+cos[s]*t/4.0+0.5;
277 int y=h->y+sin[s]*t/4.0+0.5;
278 if(x>=0 && y>=0 && x<context->width && y<context->height) {
279 int pos = y*context->width+x;
281 while(!POINTS_TO_HEAD(data[pos])) {
282 pos=(void**)data[pos]-(void**)data;
284 head_t*new_head = (head_t*)data[pos];
295 static void fix_small_boxes(context_t*context)
301 sintab[t] = sin(t*M_PI/128);
302 costab[t] = cos(t*M_PI/128);
305 head_t*h = context->heads;
314 head_t*h = context->heads;
316 head_t*next = h->next;
318 if(h->bbox.xmax - h->bbox.ymin < 16
319 && h->bbox.ymax - h->bbox.ymin < 16) {
320 head_t*other = search_vicinity(context, h, 64, costab, sintab);
322 merge(context, h->pos, other->pos);
335 static void display(context_t*context)
337 int width = context->width;
338 int height = context->height;
339 void**group = context->group;
342 for(y=0;y<height;y++) {
343 for(x=0;x<width;x++) {
344 if(!group[y*width+x]) {
346 } else if(POINTS_TO_HEAD(group[y*width+x])) {
347 printf("g%02d ", ((head_t*)group[y*width+x])->nr);
349 printf("x%02d ", (void**)group[y*width+x]-(void**)group);
355 head_t*h = context->heads;
357 printf("head: %d\n", h->nr);
358 printf(" pos: %d/%d\n", h->pos%width, h->pos/width);
359 printf(" bbox: [%d/%d,%d/%d]\n", h->bbox.xmin, h->bbox.ymin, h->bbox.xmax, h->bbox.ymax);
364 ibbox_t*get_bitmap_bboxes(unsigned char*alpha, int width, int height)
366 int size = width*height;
367 if(width<=1 || height<=1)
368 return get_bitmap_bboxes_simple(alpha, width, height);
371 context.alpha = alpha;
372 context.width = width;
373 context.height = height;
377 void**group = annotate(&context);
378 fix_small_boxes(&context);
379 overlap_bboxes(&context);
383 head_t*h = context.heads;
385 head_t*next = h->next;
386 ibbox_t*bbox = malloc(sizeof(ibbox_t));
387 memcpy(bbox, &h->bbox, sizeof(ibbox_t));
389 /* ibbox_t defines the open upper bound */
403 int main(int argn, char*argv[])
405 unsigned char alpha[8*8]=
415 get_bitmap_bboxes(alpha, 8,8);