1 #include "img_conv.h"
2
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7 Bool
img_region_foreach(PBoxRegionRec region,int dstX,int dstY,int dstW,int dstH,RegionCallbackFunc callback,void * param)8 img_region_foreach(
9 PBoxRegionRec region,
10 int dstX, int dstY, int dstW, int dstH,
11 RegionCallbackFunc callback, void * param
12 ) {
13 Box * r;
14 int j, right, top;
15 if ( region == NULL )
16 return callback( dstX, dstY, dstW, dstH, param);
17 right = dstX + dstW;
18 top = dstY + dstH;
19 r = region-> boxes;
20 for ( j = 0; j < region-> n_boxes; j++, r++) {
21 int xx = r->x;
22 int yy = r->y;
23 int ww = r->width;
24 int hh = r->height;
25 if ( xx + ww > right ) ww = right - xx;
26 if ( yy + hh > top ) hh = top - yy;
27 if ( xx < dstX ) {
28 ww -= dstX - xx;
29 xx = dstX;
30 }
31 if ( yy < dstY ) {
32 hh -= dstY - yy;
33 yy = dstY;
34 }
35 if ( xx + ww >= dstX && yy + hh >= dstY && ww > 0 && hh > 0 )
36 if ( !callback( xx, yy, ww, hh, param ))
37 return false;
38 }
39 return true;
40 }
41
42 PBoxRegionRec
img_region_alloc(PBoxRegionRec old_region,int n_boxes)43 img_region_alloc(PBoxRegionRec old_region, int n_boxes)
44 {
45 PBoxRegionRec ret = NULL;
46 ssize_t size = sizeof(BoxRegionRec) + n_boxes * sizeof(Box);
47 if ( old_region ) {
48 if (( ret = realloc(old_region, size)) == NULL)
49 return NULL;
50 } else {
51 if (( ret = malloc(size)) == NULL)
52 return NULL;
53 bzero(ret, sizeof(BoxRegionRec));
54 }
55 ret->boxes = (Box*) (((Byte*)ret) + sizeof(BoxRegionRec));
56 return ret;
57 }
58
59 Box
img_region_box(PBoxRegionRec region)60 img_region_box(PBoxRegionRec region)
61 {
62 int i, n = 0;
63 Box ret, *curr = NULL;
64 Rect r;
65
66 if ( region != NULL && region-> n_boxes > 0 ) {
67 n = region-> n_boxes;
68 curr = region->boxes;
69 r.left = curr->x;
70 r.bottom = curr->y;
71 r.right = curr->x + curr->width;
72 r.top = curr->y + curr->height;
73 curr++;
74 } else
75 bzero(&r, sizeof(r));
76
77 for ( i = 1; i < n; i++, curr++) {
78 int right = curr->x + curr->width, top = curr->y + curr->height;
79 if ( curr-> x < r.left) r.left = curr->x;
80 if ( curr-> y < r.bottom) r.bottom = curr->y;
81 if ( right > r.right) r.right = right;
82 if ( top > r.top ) r.top = top;
83 }
84 ret.x = r.left;
85 ret.y = r.bottom;
86 ret.width = r.right - r.left;
87 ret.height = r.top - r.bottom;
88 return ret;
89 }
90
91 Bool
img_point_in_region(int x,int y,PBoxRegionRec region)92 img_point_in_region( int x, int y, PBoxRegionRec region)
93 {
94 int i;
95 Box * b;
96 for ( i = 0, b = region->boxes; i < region->n_boxes; i++, b++) {
97 if ( x >= b->x && y >= b->y && x < b->x + b->width && y < b->y + b->height)
98 return true;
99 }
100 return false;
101 }
102
103
104 #ifdef __cplusplus
105 }
106 #endif
107
108