2 * Copyright © 2008 Keith Packard
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.
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
24 autoload Nichrome::Box;
25 autoload Nichrome::Label;
26 autoload Nichrome::Button;
28 extend namespace Nichrome {
29 public namespace Quad {
30 public typedef quad_t;
31 public typedef widget_t + struct {
35 rgba_color_t line_color;
36 rgba_color_t corner_color;
40 void(&quad_t) callback;
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);
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);
59 protected void draw (cairo_t cr, &quad_t quad) {
61 quad.p[2].x = quad.p[1].x = quad.geometry.width;
62 quad.p[3].y = quad.p[2].y = quad.geometry.height;
65 rectangle (cr, 0, 0, quad.geometry.width, quad.geometry.height);
66 set_source_rgba (cr, 0, 0, 0, .25);
68 for (int i = 0; i < dim (quad.p); i++)
69 line_to (cr, quad.p[i].x, quad.p[i].y);
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);
76 set_source_rgba (cr, quad.corner_color.red, quad.corner_color.green,
77 quad.corner_color.blue, quad.corner_color.alpha);
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));
85 int nearest (&quad_t quad, point_t p) {
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) {
100 protected void button (&quad_t quad, &button_event_t event) {
101 enum switch (event.type) {
104 quad.active = nearest (&quad, event);
114 protected void motion (&quad_t quad, &motion_event_t motion) {
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);
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;
132 quad.button = button;
133 quad.motion = motion;
134 quad.p = (point_t[4]) {
137 quad.line_color = (rgba_color_t) {
138 red = 1, green = 0, blue = 0, alpha = .5
140 quad.line_width = 10;
141 quad.corner_color = (rgba_color_t) {
142 red = 0, green = 0, blue = 1, alpha = 0.75
144 quad.corner_diameter = 20;
147 quad.callback = callback;
148 quad.started = false;
151 protected *quad_t new (&nichrome_t nichrome, void(&quad_t) callback) {
154 init (&quad, &nichrome, callback);
160 import Nichrome::Box;
161 import Nichrome::Label;
162 import Nichrome::Button;
163 import Nichrome::Quad;
166 typedef real[3,3] m_t;
167 typedef point_t[4] q_t;
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
185 * Set m22 to 1, and solve:
187 * | m02 , m12 , 1 | = | q0x, q0y, 1 |
189 * | m00 * w + q0x m10 * w + q0y |
190 * | ------------- , ------------- , 1 | = | q1x, q1y, 1 |
191 * | m20 * w + 1 m20 * w + 1 |
193 * m00*w + q0x = q1x*(m20*w + 1)
194 * m00 = m20*q1x + (q1x - q0x) / w;
196 * m10*w + q0y = q1y*(m20*w + 1)
197 * m10 = m20*q1y + (q1y - q0y) / w;
199 * m01*h + q0x = q3x*(m21*h + 1)
200 * m01 = m21*q3x + (q3x - q0x) / h;
202 * m11*h + q0y = q3y*(m21*h + 1)
203 * m11 = m21*q3y + (q3y - q0y) / h
205 * m00*w + m01*h + q0x = q2x*(m20*w + m21*h + 1)
207 * m20*q1x*w + q1x - q0x + m21*q3x*h + q3x - q0x + q0x = m20*q2x*w + m21*q2x*h + q2x
209 * m20*q1x*w - m20*q2x*w = m21*q2x*h - m21*q3x*h + q2x - q1x + q0x - q3x + q0x - q0x
211 * m20*(q1x - q2x)*w = m21*(q2x - q3x)*h + q2x - q1x - q3x + q0x
214 * m10*w + m11*h + q0y = q2y*(m20*w + m21*h + 1)
216 * m20*q1y*w + q1y - q0y + m21*q3y*h + q3y - q0y + q0y = m20*q2y*w + m21*q2y*h + q2y
218 * m20*q1y*w - m20*q2y*w = m21*q2y*h - m21*q3y*h + q2y - q1y + q0y - q3y + q0y - q0y
220 * m20*(q1y - q2y)*w = m21*(q2y - q3y)*h + q2y - q1y - q3y + q0y
223 * m20*(q1x - q2x)*(q1y - q2y)*w = m21*(q2x - q3x)*(q1y - q2y)*h + (q2x - q1x - q3x + q0x)*(q1y - q2y)
225 * m20*(q1y - q2y)*(q1x - q2x)*w = m21*(q2y - q3y)*(q1x - q2x)*h + (q2y - q1y - q3y + q0y)*(q1x - q2x)
227 * 0 = m21*((q2x - q3x)*(q1y - q2y) - (q2y - q3y)*(q1x - q2x))*h + (stuff)
230 * m21 = -(stuff) / (other stuff)
242 m_t solve (q_t q, real w, real h)
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;
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);
261 m20 = (m21 * (q2x - q3x) * h + q2x - q1x - q3x + q0x) / ((q1x - q2x) * w);
263 m20 = (m21 * (q2y - q3y) * h + q2y - q1y - q3y + q0y) / ((q1y - q2y) * w);
265 m00 = m20 * q1x + (q1x - q0x) / w;
266 m10 = m20 * q1y + (q1y - q0y) / w;
268 m01 = m21 * q3x + (q3x - q0x) / h;
269 m11 = m21 * q3y + (q3y - q0y) / h;
284 static int[3] a = { 2, 2, 1 };
285 static int[3] b = { 1, 0, 0 };
288 for (i = 0; i < 3; i++) {
292 p = m[i,0] * (m[ai,2] * m[bi,1] - m[ai,1] * m[bi,2]);
298 for (j = 0; j < 3; j++) {
299 for (i = 0; i < 3; i++) {
306 p = m[ai,aj] * m[bi,bj] - m[ai,bj] * m[bi,aj];
307 if (((i + j) & 1) != 0)
316 rescale (m_t m, real limit)
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)
323 real scale = limit / max;
324 for (int j = 0; j < 3; j++)
325 for (int i = 0; i < 3; i++)
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]);
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]);
347 m_row (m_t m, int row)
349 return sprintf ("%10.5f %10.5f %10.5f",
350 m[row,0],m[row,1],m[row,2]);
353 Cairo::point_t[*] scale(Cairo::point_t[*] p, real w, real h)
355 for (int i = 0; i < dim (p); i++) {
365 bool m_available = false;
366 real target_width = 1024;
367 real target_height = 768;
369 &nichrome_t nichrome = Nichrome::new ("Keystone Correction", 400, 350);
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";
376 void callback (&quad_t quad) {
377 real w = quad.geometry.width;
378 real h = quad.geometry.height;
381 m = solve (scale (quad.p, target_width / w, target_height / h),
382 target_width, target_height);
384 m_r = rescale (m_i, 16384);
385 for (int i = 0; i < 3; i++)
386 text[i] = m_row (m_i,i);
388 } catch divide_by_zero (real a, real b) {
389 text = (string[3]) { "no solution", "" ... };
392 for (int i = 0; i < 3; i++)
393 Label::relabel (label[i], text[i]);
395 &quad_t quad = Quad::new (&nichrome, callback);
397 void doit (&widget_t widget, bool state)
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));
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;
413 &box_t hbox = Box::new (Box::dir_t.horizontal,
414 Box::widget_item (&button, 0),
415 Box::widget_item (&quit, 0),
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);