Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright (c) 2009 Tias Guns |
3 |
|
|
* Copyright (c) 2009 Soren Hauberg |
4 |
|
|
* |
5 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6 |
|
|
* of this software and associated documentation files (the "Software"), to deal |
7 |
|
|
* in the Software without restriction, including without limitation the rights |
8 |
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9 |
|
|
* copies of the Software, and to permit persons to whom the Software is |
10 |
|
|
* furnished to do so, subject to the following conditions: |
11 |
|
|
* |
12 |
|
|
* The above copyright notice and this permission notice shall be included in |
13 |
|
|
* all copies or substantial portions of the Software. |
14 |
|
|
* |
15 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 |
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18 |
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 |
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20 |
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21 |
|
|
* THE SOFTWARE. |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#include <stdlib.h> |
25 |
|
|
|
26 |
|
|
#include "calibrator.h" |
27 |
|
|
|
28 |
|
|
#define SWAP(valtype,x,y) \ |
29 |
|
|
G_STMT_START { \ |
30 |
|
|
valtype t; t = (x); x = (y); y = t; \ |
31 |
|
|
} G_STMT_END |
32 |
|
|
|
33 |
|
|
/* reset clicks */ |
34 |
|
|
void |
35 |
|
✗ |
reset (struct Calib *c) |
36 |
|
|
{ |
37 |
|
✗ |
c->num_clicks = 0; |
38 |
|
✗ |
} |
39 |
|
|
|
40 |
|
|
/* check whether the coordinates are along the respective axis */ |
41 |
|
|
static gboolean |
42 |
|
✗ |
along_axis (struct Calib *c, |
43 |
|
|
int xy, |
44 |
|
|
int x0, |
45 |
|
|
int y0) |
46 |
|
|
{ |
47 |
|
✗ |
return ((abs(xy - x0) <= c->threshold_misclick) || |
48 |
|
✗ |
(abs(xy - y0) <= c->threshold_misclick)); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
/* add a click with the given coordinates */ |
52 |
|
|
gboolean |
53 |
|
✗ |
add_click (struct Calib *c, |
54 |
|
|
int x, |
55 |
|
|
int y) |
56 |
|
|
{ |
57 |
|
✗ |
g_debug ("Trying to add click (%d, %d)", x, y); |
58 |
|
|
|
59 |
|
|
/* Double-click detection */ |
60 |
|
✗ |
if (c->threshold_doubleclick > 0 && c->num_clicks > 0) |
61 |
|
|
{ |
62 |
|
✗ |
int i = c->num_clicks-1; |
63 |
|
✗ |
while (i >= 0) |
64 |
|
|
{ |
65 |
|
✗ |
if (abs(x - c->clicked_x[i]) <= c->threshold_doubleclick && |
66 |
|
✗ |
abs(y - c->clicked_y[i]) <= c->threshold_doubleclick) |
67 |
|
|
{ |
68 |
|
✗ |
g_debug ("Detected double-click, ignoring"); |
69 |
|
✗ |
return FALSE; |
70 |
|
|
} |
71 |
|
✗ |
i--; |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
/* Mis-click detection */ |
76 |
|
✗ |
if (c->threshold_misclick > 0 && c->num_clicks > 0) |
77 |
|
|
{ |
78 |
|
✗ |
gboolean misclick = TRUE; |
79 |
|
|
|
80 |
|
✗ |
if (c->num_clicks == 1) |
81 |
|
|
{ |
82 |
|
|
/* check that along one axis of first point */ |
83 |
|
✗ |
if (along_axis(c, x,c->clicked_x[0],c->clicked_y[0]) || |
84 |
|
✗ |
along_axis(c, y,c->clicked_x[0],c->clicked_y[0])) |
85 |
|
|
{ |
86 |
|
✗ |
misclick = FALSE; |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
✗ |
else if (c->num_clicks == 2) |
90 |
|
|
{ |
91 |
|
|
/* check that along other axis of first point than second point */ |
92 |
|
✗ |
if ((along_axis(c, y,c->clicked_x[0],c->clicked_y[0]) && |
93 |
|
✗ |
along_axis(c, c->clicked_x[1],c->clicked_x[0],c->clicked_y[0])) || |
94 |
|
✗ |
(along_axis(c, x,c->clicked_x[0],c->clicked_y[0]) && |
95 |
|
✗ |
along_axis(c, c->clicked_y[1],c->clicked_x[0],c->clicked_y[0]))) |
96 |
|
|
{ |
97 |
|
✗ |
misclick = FALSE; |
98 |
|
|
} |
99 |
|
|
} |
100 |
|
✗ |
else if (c->num_clicks == 3) |
101 |
|
|
{ |
102 |
|
|
/* check that along both axis of second and third point */ |
103 |
|
✗ |
if ((along_axis(c, x,c->clicked_x[1],c->clicked_y[1]) && |
104 |
|
✗ |
along_axis(c, y,c->clicked_x[2],c->clicked_y[2])) || |
105 |
|
✗ |
(along_axis(c, y,c->clicked_x[1],c->clicked_y[1]) && |
106 |
|
✗ |
along_axis(c, x,c->clicked_x[2],c->clicked_y[2]))) |
107 |
|
|
{ |
108 |
|
✗ |
misclick = FALSE; |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
✗ |
if (misclick) |
113 |
|
|
{ |
114 |
|
✗ |
g_debug ("Detected misclick, resetting"); |
115 |
|
✗ |
reset(c); |
116 |
|
✗ |
return FALSE; |
117 |
|
|
} |
118 |
|
|
} |
119 |
|
|
|
120 |
|
✗ |
g_debug ("Click (%d, %d) added", x, y); |
121 |
|
✗ |
c->clicked_x[c->num_clicks] = x; |
122 |
|
✗ |
c->clicked_y[c->num_clicks] = y; |
123 |
|
✗ |
c->num_clicks++; |
124 |
|
|
|
125 |
|
✗ |
return TRUE; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
/* calculate and apply the calibration */ |
129 |
|
|
gboolean |
130 |
|
✗ |
finish (struct Calib *c, |
131 |
|
|
XYinfo *new_axis, |
132 |
|
|
gboolean *swap) |
133 |
|
|
{ |
134 |
|
|
gboolean swap_xy; |
135 |
|
|
float scale_x; |
136 |
|
|
float scale_y; |
137 |
|
|
float delta_x; |
138 |
|
|
float delta_y; |
139 |
|
✗ |
XYinfo axis = {-1, -1, -1, -1}; |
140 |
|
|
|
141 |
|
✗ |
if (c->num_clicks != 4) |
142 |
|
✗ |
return FALSE; |
143 |
|
|
|
144 |
|
|
/* Should x and y be swapped? If the device and output are wider |
145 |
|
|
* towards different axes, swapping must be performed |
146 |
|
|
* |
147 |
|
|
* FIXME: Would be even better to know the actual output orientation, |
148 |
|
|
* not just the direction. |
149 |
|
|
*/ |
150 |
|
✗ |
swap_xy = (c->geometry.width < c->geometry.height); |
151 |
|
|
|
152 |
|
|
/* Compute the scale to transform from pixel positions to [0..1]. */ |
153 |
|
✗ |
scale_x = 1 / (float)c->geometry.width; |
154 |
|
✗ |
scale_y = 1 / (float)c->geometry.height; |
155 |
|
|
|
156 |
|
✗ |
axis.x_min = ((((c->clicked_x[UL] + c->clicked_x[LL]) / 2)) * scale_x); |
157 |
|
✗ |
axis.x_max = ((((c->clicked_x[UR] + c->clicked_x[LR]) / 2)) * scale_x); |
158 |
|
✗ |
axis.y_min = ((((c->clicked_y[UL] + c->clicked_y[UR]) / 2)) * scale_y); |
159 |
|
✗ |
axis.y_max = ((((c->clicked_y[LL] + c->clicked_y[LR]) / 2)) * scale_y); |
160 |
|
|
|
161 |
|
|
/* Add/subtract the offset that comes from not having the points in the |
162 |
|
|
* corners (using the same coordinate system they are currently in) |
163 |
|
|
*/ |
164 |
|
✗ |
delta_x = (axis.x_max - axis.x_min) / (float)(NUM_BLOCKS - 2); |
165 |
|
✗ |
axis.x_min -= delta_x; |
166 |
|
✗ |
axis.x_max += delta_x; |
167 |
|
✗ |
delta_y = (axis.y_max - axis.y_min) / (float)(NUM_BLOCKS - 2); |
168 |
|
✗ |
axis.y_min -= delta_y; |
169 |
|
✗ |
axis.y_max += delta_y; |
170 |
|
|
|
171 |
|
|
/* If x and y has to be swapped we also have to swap the parameters */ |
172 |
|
✗ |
if (swap_xy) |
173 |
|
|
{ |
174 |
|
✗ |
SWAP (gdouble, axis.x_min, axis.y_min); |
175 |
|
✗ |
SWAP (gdouble, axis.x_max, axis.y_max); |
176 |
|
|
} |
177 |
|
|
|
178 |
|
✗ |
*new_axis = axis; |
179 |
|
✗ |
*swap = swap_xy; |
180 |
|
|
|
181 |
|
✗ |
return TRUE; |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
|