-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbound_box.c
64 lines (58 loc) · 1.12 KB
/
bound_box.c
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
#include"image.h"
extern int opt_color;
int is_black(pixcel_t p)
{
if(opt_color)
return p.c.r!=0x00 && p.c.g!=0x00 && p.c.b!=0x00;
else
return p.g!=0x00;
}
image_t* make_bound_box(image_t *img)
{
uint32_t i,j;
uint32_t x=img->width/2;
uint32_t y=img->height/2;
uint32_t maxx=0;
uint32_t maxy=0;
uint32_t minx=0;
uint32_t miny=0;
for(i=0;i<y;i++)
for(j=0;j<img->width;j++)
if(is_black(img->map[i][j]))
{
miny=i;
i=y;
break;
}
for(i=img->height-1;i>y;i--)
for(j=0;j<img->width;j++)
if(is_black(img->map[i][j]))
{
maxy=i;
i=y;
break;
}
for(i=0;i<x;i++)
for(j=0;j<img->height;j++)
if(is_black(img->map[j][i]))
{
minx=i;
i=x;
break;
}
for(i=img->width-1;i>x;i--)
for(j=0;j<img->height;j++)
if(is_black(img->map[j][i]))
{
maxx=i;
i=x;
break;
}
uint32_t width=maxx-minx+1;
uint32_t height=maxy-miny+1;
image_t *bound_box=allocate_image(width,height,opt_color?COLOR_TYPE_RGB:COLOR_TYPE_GRAY);
for(i=0;i<height;i++)
for(j=0;j<width;j++)
bound_box->map[i][j]=img->map[miny+i][minx+j];
return bound_box;
}