calibrate.c

Go to the documentation of this file.
00001 /*
00002  * File Name: calibrate.c
00003  */
00004 
00005 /*
00006  * This file is part of gtktscal.
00007  *
00008  * gtktscal is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * gtktscal is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021  
00022 /*
00023  * This code is originally from:
00024  * 
00025  *  tslib/tests/ts_calibrate.c
00026  *
00027  *  Copyright (C) 2001 Russell King.
00028  *
00029  * Modifications:
00030  *
00031  *  Copyright (C) 2008 iRex Technologies B.V.
00032  */
00033 
00034 #include <stdio.h>
00035 
00036 #include "calibrate.h"
00037 
00038 extern int flag_debug;
00039 
00040 int perform_calibration(calibration *cal) {
00041    int j;
00042    float n, x, y, x2, y2, xy, z, zx, zy;
00043    float det, a, b, c, e, f, i;
00044    float scaling = 65536.0;
00045 
00046 // Get sums for matrix
00047    n = x = y = x2 = y2 = xy = 0;
00048    for(j=0;j<5;j++) {
00049       n += 1.0;
00050       x += (float)cal->x[j];
00051       y += (float)cal->y[j];
00052       x2 += (float)(cal->x[j]*cal->x[j]);
00053       y2 += (float)(cal->y[j]*cal->y[j]);
00054       xy += (float)(cal->x[j]*cal->y[j]);
00055    }
00056 
00057 // Get determinant of matrix -- check if determinant is too small
00058    det = n*(x2*y2 - xy*xy) + x*(xy*y - x*y2) + y*(x*xy - y*x2);
00059    if(det < 0.1 && det > -0.1) {
00060       fprintf(stderr, "ts_calibrate: determinant is too small -- %f\n",det);
00061       return 0;
00062    }
00063 
00064 // Get elements of inverse matrix
00065    a = (x2*y2 - xy*xy)/det;
00066    b = (xy*y - x*y2)/det;
00067    c = (x*xy - y*x2)/det;
00068    e = (n*y2 - y*y)/det;
00069    f = (x*y - n*xy)/det;
00070    i = (n*x2 - x*x)/det;
00071 
00072 // Get sums for x calibration
00073    z = zx = zy = 0;
00074    for(j=0;j<5;j++) {
00075       z += (float)cal->xfb[j];
00076       zx += (float)(cal->xfb[j]*cal->x[j]);
00077       zy += (float)(cal->xfb[j]*cal->y[j]);
00078    }
00079 
00080 // Now multiply out to get the calibration for framebuffer x coord
00081    cal->a[0] = (int)((a*z + b*zx + c*zy)*(scaling));
00082    cal->a[1] = (int)((b*z + e*zx + f*zy)*(scaling));
00083    cal->a[2] = (int)((c*z + f*zx + i*zy)*(scaling));
00084 
00085    if (flag_debug)
00086      fprintf(stderr, "%f %f %f\n",(a*z + b*zx + c*zy),
00087         (b*z + e*zx + f*zy),
00088         (c*z + f*zx + i*zy));
00089 
00090 // Get sums for y calibration
00091    z = zx = zy = 0;
00092    for(j=0;j<5;j++) {
00093       z += (float)cal->yfb[j];
00094       zx += (float)(cal->yfb[j]*cal->x[j]);
00095       zy += (float)(cal->yfb[j]*cal->y[j]);
00096    }
00097 
00098 // Now multiply out to get the calibration for framebuffer y coord
00099    cal->a[3] = (int)((a*z + b*zx + c*zy)*(scaling));
00100    cal->a[4] = (int)((b*z + e*zx + f*zy)*(scaling));
00101    cal->a[5] = (int)((c*z + f*zx + i*zy)*(scaling));
00102 
00103    if (flag_debug)
00104      fprintf(stderr, "%f %f %f\n",(a*z + b*zx + c*zy),
00105         (b*z + e*zx + f*zy),
00106         (c*z + f*zx + i*zy));
00107 
00108 // If we got here, we're OK, so assign scaling to a[6] and return
00109    cal->a[6] = (int)scaling;
00110    return 1;
00111 /* 
00112 // This code was here originally to just insert default values
00113    for(j=0;j<7;j++) {
00114       c->a[j]=0;
00115    }
00116    c->a[1] = c->a[5] = c->a[6] = 1;
00117    return 1;
00118 */
00119 
00120 }
Generated by  doxygen 1.6.2-20100208