1 #include <stdio.h>
2 #include <math.h>
3 #include <tina/sys.h>
4 #include <tina/sysfuncs.h>
5 #include <tina/math.h>
6 #include <tina/mathfuncs.h>
7 #include <tina/vision.h>
8 #include <tina/visionfuncs.h>
9 #include <tina/tv.h>
10 #include <tina/tvfuncs.h>
11 #include <tina/draw.h>
12 #include <tina/drawfuncs.h>
13 #include <tina/toolsfuncs.h>
14
15 static Imrect *connmask = NULL;
16 static float old_x,old_y;
17 static Vec2 p1 = {Vec2_id};
18 static Vec2 p2 = {Vec2_id};
19 static Tv *display_tv = NULL;
20
21 static void pixelxy(int x, int y)
22 {
23 Vec2 v = {Vec2_id} ;
24
25 v.el[0] = x+0.5;
26 v.el[1] = y+0.5;
27 tv_pixel2(display_tv,v);
28 }
29
30 static void conn_add_regions(Tv *tv, Ipos pos)
31 {
32 Imrect *mask = NULL, *fmask=NULL, *fconnmask;
33 Imregion roi;
34 Vec2 v = {Vec2_id};
35 int i, j;
36 int x, y, x1, y1, x2, y2, n, type;
37
38 display_tv = tv;
39 tv_reset_draw(tv);
40
41 if (stack_check_types(IMRECT, NULL) == false)
42 {
43 error("conn_add_regions: wrong type on stack", warning);
44 return;
45 }
46 mask = (Imrect *) stack_inspect(&type);
47
48 roi = *(mask->region);
49 roi.lx -= 1;
50 roi.ux += 1;
51 roi.ly -= 1;
52 roi.uy += 1;
53
54
55 if (mask->vtype == complex_v)
56 {
57 fmask = im_im(mask);
58 mask = fmask;
59 }
60 if (connmask == NULL)
61 connmask = im_alloc(mask->height+2, mask->width+2, &roi, char_v);
62 x1 = tina_int(vec2_x(p1));
63 y1 = tina_int(vec2_y(p1));
64 x2 = tina_int(vec2_x(p2));
65 y2 = tina_int(vec2_y(p2));
66 if (x1>x2)
67 {
68 x1 = tina_int(vec2_x(p2));
69 x2 = tina_int(vec2_x(p1));
70 }
71 if (y1>y2)
72 {
73 y1 = tina_int(vec2_y(p2));
74 y2 = tina_int(vec2_y(p1));
75 }
76 tv_color_set(tv, red);
77 for (x = x1; x<=x2; x++)
78 {
79 for (y = y1; y<=y2 ; y++)
80 {
81 if (im_get_pix(mask, y, x) != 0 && im_get_pix(connmask, y, x)==0)
82 imc_connpixels(mask, connmask, roi, x, y, pixelxy);
83 }
84 }
85 if (fmask!=NULL) im_free(fmask);
86 }
87
88 static void conn_delete_pixel(Tv *tv, Ipos pos)
89 {
90 Imrect *mask;
91 Vec2 v = {Vec2_id};
92 int x, y, type;
93 Complex pixval;
94
95 mask = (Imrect *) stack_inspect(&type);
96 if (mask==NULL) return;
97 imcalc_undo(im_copy(mask),NULL,false);
98 v = tv_backproj2(tv, pos);
99 x = tina_int(vec2_x(v));
100 y = tina_int(vec2_y(v));
101 pixval.x =0;
102 pixval.y =0;
103 im_put_pixz(pixval,mask,y,x);
104 tv_color_set(tv, red);
105 tv_pixel2(tv,v);
106 old_x = x;
107 old_y = y;
108 }
109
110 static void conn_delete_line(Tv *tv, Ipos pos)
111 {
112 Imrect *mask;
113 Vec2 v = {Vec2_id};
114 int x, y, type;
115 float frac_x, frac_y;
116 Complex pixval;
117
118 mask = (Imrect *) stack_inspect(&type);
119 if (mask==NULL) return;
120 v = tv_backproj2(tv, pos);
121 x = tina_int(vec2_x(v));
122 y = tina_int(vec2_y(v));
123 pixval.x = 0;
124 pixval.y = 0;
125 tv_color_set(tv, red);
126 if (fabs(x-old_x)>fabs(y-old_y))
127 {
128 if (x<old_x)
129 for (frac_x=x;frac_x<old_x;frac_x++)
130 {
131 frac_y = y + (frac_x - x)*(old_y - y)/(old_x - x);
132 im_put_pixz(pixval,mask,tina_int(frac_y),tina_int(frac_x));
133 vec2_x(v) = tina_int(frac_x);
134 vec2_y(v) = tina_int(frac_y);
135 tv_pixel2(tv,v);
136 }
137 else
138 for (frac_x=x;frac_x>old_x;frac_x--)
139 {
140 frac_y = y + (frac_x - x)*(old_y - y)/(old_x - x);
141 im_put_pixz(pixval,mask,tina_int(frac_y),tina_int(frac_x));
142 vec2_x(v) = tina_int(frac_x);
143 vec2_y(v) = tina_int(frac_y);
144 tv_pixel2(tv,v);
145 }
146 }
147 else
148 {
149 if (y<old_y)
150 for (frac_y=y;frac_y<old_y;frac_y++)
151 {
152 frac_x = x + (frac_y - y)*(old_x - x)/(old_y - y);
153 im_put_pixz(pixval,mask,tina_int(frac_y),tina_int(frac_x));
154 vec2_x(v) = tina_int(frac_x);
155 vec2_y(v) = tina_int(frac_y);
156 tv_pixel2(tv,v);
157 }
158 else
159 for (frac_y=y;frac_y>old_y;frac_y--)
160 {
161 frac_x = x + (frac_y - y)*(old_x - x)/(old_y - y);
162 im_put_pixz(pixval,mask,tina_int(frac_y),tina_int(frac_x));
163 vec2_x(v) = tina_int(frac_x);
164 vec2_y(v) = tina_int(frac_y);
165 tv_pixel2(tv,v);
166 }
167 }
168 old_x = x;
169 old_y = y;
170 }
171
172 static void conn_redraw(Tv *tv, Ipos pos)
173 {
174 imcalc_draw(tv);
175 }
176
177 static void conn_seg_regions(Tv *tv, Ipos pos)
178 {
179 Imrect *mask,*fmask,*xor_mask,*improd;
180 int type;
181
182 if (stack_check_types(IMRECT, NULL) == false)
183 {
184 error("conn_add_regions: wrong type on stack", warning);
185 return;
186 }
187 if (connmask==NULL)
188 {
189 format("no region selected \n");
190 return;
191 }
192 mask = (Imrect *) stack_pop(&type);
193 if (mask->vtype == complex_v)
194 {
195 fmask = im_im(mask);
196 xor_mask = im_re(mask);
197 improd = imf_prod(xor_mask,connmask);
198 imf_accum_inplace(connmask,-2.0,improd);
199 im_free(improd);
200 imf_accum_inplace(connmask,1.0,xor_mask);
201 im_free(mask);
202 im_free(xor_mask);
203 mask = fmask;
204 }
205
206 stack_push((void *)imz_cmplx(connmask,mask), IMRECT, im_free);
207 im_free(mask);
208 im_free(connmask);
209 connmask = NULL;
210
211 imcalc_draw(tv);
212 image_choice_reset();
213 }
214
215 static void conn_roi(Tv *tv, Ipos pos)
216 {
217 tv_set_overlay(tv);
218 p1 = p2 = tv_backproj2(tv, pos);
219 tv_rect2(tv, p1, p2);
220 }
221
222 static void conn_drag(Tv *tv, Ipos pos)
223 {
224 tv_rect2(tv, p1, p2);
225 p2 = tv_backproj2(tv, pos);
226 tv_rect2(tv, p1, p2);
227 }
228
229 Tv_mouse connmask_mouse(void)
230 {
231 return (mouse_define(MOUSE_NAME, "connect",
232 LEFT_NAME, "add",
233 LEFT_DOWN, conn_roi,
234 LEFT_DRAG, conn_drag,
235 LEFT_UP, conn_add_regions,
236 MIDDLE_NAME, "delete",
237 MIDDLE_DOWN, conn_delete_pixel,
238 MIDDLE_DRAG, conn_delete_line,
239 MIDDLE_UP, conn_redraw,
240 RIGHT_NAME, "segment",
241 RIGHT_DOWN, conn_seg_regions,
242 NULL));
243 }
244
245
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.