add --transform none to reset to identity
[xorg/xrandr] / keystone.5c
1 /*
2  * Copyright © 2008 Keith Packard
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 autoload Nichrome;
24 autoload Nichrome::Box;
25 autoload Nichrome::Label;
26 autoload Nichrome::Button;
27
28 extend namespace Nichrome {
29     public namespace Quad {
30         public typedef quad_t;
31         public typedef widget_t + struct {
32             point_t[4]      p;
33             real            line_width;
34             real            corner_diameter;
35             rgba_color_t    line_color;
36             rgba_color_t    corner_color;
37             bool            down;
38             bool            started;
39             int             active;
40             void(&quad_t)   callback;
41         } quad_t;
42
43         protected void outline (cairo_t cr, &quad_t quad) {
44             for (int i = 0; i < dim (quad.p); i++) {
45                 arc (cr, quad.p[i].x, quad.p[i].y,
46                      quad.corner_diameter / 2, 0, 2 * pi);
47                 close_path (cr);
48             }
49         }
50
51         void text_at (cairo_t cr, point_t p, string text) {
52             text_extents_t  e = text_extents (cr, text);
53             p.x = p.x - e.width / 2 - e.x_bearing;
54             p.y = p.y - e.height / 2 - e.y_bearing;
55             move_to (cr, p.x, p.y);
56             show_text (cr, text);
57         }
58
59         protected void draw (cairo_t cr, &quad_t quad) {
60             if (!quad.started) {
61                 quad.p[2].x = quad.p[1].x = quad.geometry.width;
62                 quad.p[3].y = quad.p[2].y = quad.geometry.height;
63                 quad.started = true;
64             }
65             rectangle (cr, 0, 0, quad.geometry.width, quad.geometry.height);
66             set_source_rgba (cr, 0, 0, 0, .25);
67             fill (cr);
68             for (int i = 0; i < dim (quad.p); i++)
69                 line_to (cr, quad.p[i].x, quad.p[i].y);
70             close_path (cr);
71             set_line_width (cr, quad.line_width);
72             set_source_rgba (cr, quad.line_color.red, quad.line_color.green,
73                              quad.line_color.blue, quad.line_color.alpha);
74             set_line_join (cr, line_join_t.ROUND);
75             stroke (cr);
76             set_source_rgba (cr, quad.corner_color.red, quad.corner_color.green,
77                              quad.corner_color.blue, quad.corner_color.alpha);
78             outline (cr, &quad);
79             fill (cr);
80             set_source_rgba (cr, 1, 1, 1, 1);
81             for (int i = 0; i < dim (quad.p); i++)
82                 text_at (cr, quad.p[i], sprintf ("%d", i));
83         }
84
85         int nearest (&quad_t quad, point_t p) {
86             real    best_dist2 = 0;
87             int     best = 0;
88
89             for (int i = 0; i < dim (quad.p); i++) {
90                 real dist2 = ((p.x - quad.p[i].x) ** 2 +
91                               (p.y - quad.p[i].y) ** 2);
92                 if (i == 0 || dist2 < best_dist2) {
93                     best_dist2 = dist2;
94                     best = i;
95                 }
96             }
97             return best;
98         }
99
100         protected void button (&quad_t quad, &button_event_t event) {
101             enum switch (event.type) {
102             case press:
103                 quad.down = true;
104                 quad.active = nearest (&quad, event);
105                 break;
106             case release:
107                 quad.down = false;
108                 break;
109             default:
110                 break;
111             }
112         }
113
114         protected void motion (&quad_t quad, &motion_event_t motion) {
115             if (quad.down) {
116                 motion.x = max (0, min (quad.geometry.width, motion.x));
117                 motion.y = max (0, min (quad.geometry.height, motion.y));
118                 quad.p[quad.active].x = motion.x;
119                 quad.p[quad.active].y = motion.y;
120                 quad.callback (&quad);
121                 Widget::reoutline (&quad);
122                 Widget::redraw (&quad);
123             }
124         }
125
126         protected void init (&quad_t quad,
127                              &nichrome_t nichrome,
128                              void (&quad_t) callback) {
129             Widget::init (&nichrome, &quad);
130             quad.outline = outline;
131             quad.draw = draw;
132             quad.button = button;
133             quad.motion = motion;
134             quad.p = (point_t[4]) {
135                 { x = 0, y = 0 } ...
136             };
137             quad.line_color = (rgba_color_t) {
138                 red = 1, green = 0, blue = 0, alpha = .5
139             };
140             quad.line_width = 10;
141             quad.corner_color = (rgba_color_t) {
142                 red = 0, green = 0, blue = 1, alpha = 0.75
143             };
144             quad.corner_diameter = 20;
145             quad.down = false;
146             quad.active = -1;
147             quad.callback = callback;
148             quad.started = false;
149         }
150
151         protected *quad_t new (&nichrome_t nichrome, void(&quad_t) callback) {
152             quad_t  quad;
153
154             init (&quad, &nichrome, callback);
155             return &quad;
156         }
157     }
158 }
159 import Nichrome;
160 import Nichrome::Box;
161 import Nichrome::Label;
162 import Nichrome::Button;
163 import Nichrome::Quad;
164
165 import Cairo;
166 typedef real[3,3]   m_t;
167 typedef point_t[4]  q_t;
168
169 /*
170  * Ok, given an source quad and a dest rectangle, compute
171  * a transform that maps the rectangle to q. That's easier
172  * as the rectangle has some nice simple properties. Invert
173  * the matrix to find the opposite mapping
174  *
175  *  q0    q1
176  *
177  *  q3    q2
178  *
179  *  | m00 m01 m02 |
180  *  | m10 m11 m12 |
181  *  | m20 m21 m22 |
182  *
183  *  m [ 0 0 1 ] = q[0]
184  *
185  * Set m22 to 1, and solve:
186  *
187  *  |     m02       ,     m12       , 1 | = | q0x, q0y, 1 |
188  *
189  *  | m00 * w + q0x   m10 * w + q0y     |
190  *  | ------------- , ------------- , 1 | = | q1x, q1y, 1 |
191  *  |  m20 * w + 1     m20 * w + 1      |
192
193  *   m00*w + q0x = q1x*(m20*w + 1)
194  *   m00 = m20*q1x + (q1x - q0x) / w;
195  *
196  *   m10*w + q0y = q1y*(m20*w + 1)
197  *   m10 = m20*q1y + (q1y - q0y) / w;
198  *
199  *   m01*h + q0x = q3x*(m21*h + 1)
200  *   m01 = m21*q3x + (q3x - q0x) / h;
201  *
202  *   m11*h + q0y = q3y*(m21*h + 1)
203  *   m11 = m21*q3y + (q3y - q0y) / h
204  *
205  *   m00*w +                 m01*h +                 q0x = q2x*(m20*w + m21*h + 1)
206  *
207  *   m20*q1x*w + q1x - q0x + m21*q3x*h + q3x - q0x + q0x = m20*q2x*w + m21*q2x*h + q2x
208  *
209  *   m20*q1x*w - m20*q2x*w = m21*q2x*h - m21*q3x*h + q2x - q1x + q0x - q3x + q0x - q0x
210  *
211  *   m20*(q1x - q2x)*w     = m21*(q2x - q3x)*h     + q2x - q1x - q3x + q0x
212  *
213  *
214  *   m10*w +                 m11*h +                 q0y = q2y*(m20*w + m21*h + 1)
215  *
216  *   m20*q1y*w + q1y - q0y + m21*q3y*h + q3y - q0y + q0y = m20*q2y*w + m21*q2y*h + q2y
217  *
218  *   m20*q1y*w - m20*q2y*w = m21*q2y*h - m21*q3y*h + q2y - q1y + q0y - q3y + q0y - q0y
219  *
220  *   m20*(q1y - q2y)*w     = m21*(q2y - q3y)*h     + q2y - q1y - q3y + q0y
221  *
222  *
223  *   m20*(q1x - q2x)*(q1y - q2y)*w     = m21*(q2x - q3x)*(q1y - q2y)*h     + (q2x - q1x - q3x + q0x)*(q1y - q2y)
224  *
225  *   m20*(q1y - q2y)*(q1x - q2x)*w     = m21*(q2y - q3y)*(q1x - q2x)*h     + (q2y - q1y - q3y + q0y)*(q1x - q2x)
226  *
227  *   0                                 = m21*((q2x - q3x)*(q1y - q2y) - (q2y - q3y)*(q1x - q2x))*h + (stuff)
228  *                                     = m21 * a + b;
229  *
230  *   m21 = -(stuff) / (other stuff)
231  *
232  *   m20 = f(m21)
233  *
234  *  m00 = f(m20)
235  *  m10 = f(m20)
236  *
237  *  m01 = f(m21)
238  *  m11 = f(m21)
239  *
240  * done.
241  */
242 m_t solve (q_t q, real w, real h)
243 {
244     real    q0x = q[0].x, q0y = q[0].y;
245     real    q1x = q[1].x, q1y = q[1].y;
246     real    q2x = q[2].x, q2y = q[2].y;
247     real    q3x = q[3].x, q3y = q[3].y;
248     real    m00, m01, m02;
249     real    m10, m11, m12;
250     real    m20, m21, m22;
251
252     m02 = q0x;
253     m12 = q0y;
254     m22 = 1;
255
256     real    a = ((q2x - q3x)*(q1y - q2y) - (q2y - q3y)*(q1x - q2x)) * h;
257     real    b = (q2x - q1x - q3x + q0x) * (q1y - q2y) - (q2y - q1y - q3y + q0y) * (q1x - q2x);
258     m21 = - b / a;
259
260     if (q1x != q2x)
261         m20 = (m21 * (q2x - q3x) * h + q2x - q1x - q3x + q0x) / ((q1x - q2x) * w);
262     else
263         m20 = (m21 * (q2y - q3y) * h + q2y - q1y - q3y + q0y) / ((q1y - q2y) * w);
264
265     m00 = m20 * q1x + (q1x - q0x) / w;
266     m10 = m20 * q1y + (q1y - q0y) / w;
267
268     m01 = m21 * q3x + (q3x - q0x) / h;
269     m11 = m21 * q3y + (q3y - q0y) / h;
270
271     return (m_t) {
272         { m00, m01, m02 },
273         { m10, m11, m12 },
274         { m20, m21, m22 } };
275 }
276
277 m_t
278 invert (m_t m)
279 {
280     real  det;
281     int     i, j;
282     m_t r;
283
284     static int[3]       a = { 2, 2, 1 };
285     static int[3]       b = { 1, 0, 0 };
286
287     det = 0;
288     for (i = 0; i < 3; i++) {
289         real    p;
290         int     ai = a[i];
291         int     bi = b[i];
292         p = m[i,0] * (m[ai,2] * m[bi,1] - m[ai,1] * m[bi,2]);
293         if (i == 1)
294             p = -p;
295         det += p;
296     }
297     det = 1/det;
298     for (j = 0; j < 3; j++) {
299         for (i = 0; i < 3; i++) {
300             real  p;
301             int     ai = a[i];
302             int     aj = a[j];
303             int     bi = b[i];
304             int     bj = b[j];
305
306             p = m[ai,aj] * m[bi,bj] - m[ai,bj] * m[bi,aj];
307             if (((i + j) & 1) != 0)
308                 p = -p;
309             r[j,i] = det * p;
310         }
311     }
312     return r;
313 }
314
315 m_t
316 rescale (m_t m, real limit)
317 {
318     real    max = 0;
319     for (int j = 0; j < 3; j++)
320         for (int i = 0; i < 3; i++)
321             if ((real v = abs (m[j,i])) > max)
322                 max = v;
323     real scale = limit / max;
324     for (int j = 0; j < 3; j++)
325         for (int i = 0; i < 3; i++)
326             m[j,i] *= scale;
327     return m;
328     
329 }
330
331 string
332 m_print (m_t m)
333 {
334     /*
335     return sprintf ("%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f,%.8f",
336                     m[0,0],m[0,1],m[0,2],
337                     m[1,0],m[1,1],m[1,2],
338                     m[2,0],m[2,1],m[2,2]);
339      */
340     return sprintf ("%v,%v,%v,%v,%v,%v,%v,%v,%v",
341                     m[0,0],m[0,1],m[0,2],
342                     m[1,0],m[1,1],m[1,2],
343                     m[2,0],m[2,1],m[2,2]);
344 }
345
346 string
347 m_row (m_t m, int row)
348 {
349     return sprintf ("%10.5f %10.5f %10.5f",
350                     m[row,0],m[row,1],m[row,2]);
351 }
352
353 Cairo::point_t[*] scale(Cairo::point_t[*] p, real w, real h)
354 {
355     for (int i = 0; i < dim (p); i++) {
356         p[i].x *= w;
357         p[i].y *= h;
358     }
359     return p;
360 }
361
362 void test ()
363 {
364     m_t m, m_i, m_r;
365     bool m_available = false;
366     real target_width = 1024;
367     real target_height = 768;
368
369     &nichrome_t nichrome = Nichrome::new ("Keystone Correction", 400, 350);
370     (*label_t)[3]       label;
371     &label_t    space = Label::new (&nichrome, "");
372     for (int i = 0; i < 3; i++) {
373         label[i] = Label::new (&nichrome, "matrix");
374         label[i]->font = "sans-9";
375     }
376     void callback (&quad_t quad) {
377         real    w = quad.geometry.width;
378         real    h = quad.geometry.height;
379         string[3]       text;
380         try {
381             m = solve (scale (quad.p, target_width / w, target_height / h),
382                        target_width, target_height);
383             m_i = invert (m);
384             m_r = rescale (m_i, 16384);
385             for (int i = 0; i < 3; i++)
386                 text[i] = m_row (m_i,i);
387             m_available = true;
388         } catch divide_by_zero (real a, real b) {
389             text = (string[3]) { "no solution", "" ... };
390             m_available = false;
391         }
392         for (int i = 0; i < 3; i++)
393             Label::relabel (label[i], text[i]);
394     }
395     &quad_t     quad = Quad::new (&nichrome, callback);
396
397     void doit (&widget_t widget, bool state)
398     {
399         if (m_available)
400         {
401             printf ("normal:  %s\n", m_print (m));
402             printf ("inverse: %s\n", m_print (m_i));
403             printf ("scaled:  %s\n", m_print (m_r));
404         }
405     }
406
407     &button_t   button = Button::new (&nichrome, "doit", doit);
408     &button_t   quit = Button::new (&nichrome, "quit",
409                                      void func (&widget_t w, bool state) {
410                                         w.nichrome.running = false;
411                                      });
412
413     &box_t      hbox = Box::new (Box::dir_t.horizontal,
414                                  Box::widget_item (&button, 0),
415                                  Box::widget_item (&quit, 0),
416                                  Box::glue_item (1));
417     &box_t      box = Box::new (Box::dir_t.vertical,
418                                 Box::box_item (&hbox),
419                                 Box::widget_item (label[0], 0),
420                                 Box::widget_item (label[1], 0),
421                                 Box::widget_item (label[2], 0),
422                                 Box::widget_item (&space, 0),
423                                 Box::widget_item (&quad, 1));
424     Nichrome::set_box (&nichrome, &box);
425     Nichrome::main_loop (&nichrome);
426 }
427
428 test ();