00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef MAX
00022 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00023 #endif
00024 #ifndef MIN
00025 #define MIN(a,b) ((a) < (b) ? (a) : (b))
00026 #endif
00027
00028
00029
00030 void dgels_(const char *, int *, int *, int *, double *, int *, double *,
00031 int *, double *, int *, int *);
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 void cheby_basis(n, order, x, cheb, ldc)
00042 int n, order;
00043 double x[], cheb[];
00044 int ldc;
00045 {
00046 int i, s;
00047
00048 for (i = 0; i < n; i++) {
00049 cheb[i] = 1;
00050 cheb[i + ldc] = x[i];
00051 }
00052 for (s = 2; s < order; s++) {
00053 for (i = 0; i < n; i++) {
00054 cheb[s * ldc + i] =
00055 2 * x[i] * cheb[(s - 1) * ldc + i] - cheb[(s - 2) * ldc + i];
00056 }
00057 }
00058 }
00059
00060
00061
00062
00063
00064
00065 void fitimage(image, m, n, order, fill)
00066 double image[];
00067 int m, n, order, fill;
00068 {
00069 int i, j, k, l, s, t, npix, lda, mn;
00070 int nterms, info, lwork, *map[2], nimgs;
00071 double *A, *B, *x, *y, *work, *chebx, *cheby;
00072 double pix;
00073
00074 if (m <= 0 || n <= 0 || order <= 1)
00075 return;
00076 nimgs = 1;
00077
00078
00079 lda = m * n;
00080 A = (double *)malloc(m * n * order * order * sizeof(double));
00081 B = (double *)malloc(m * n * sizeof(double));
00082 map[0] = (int *)malloc(n * m * sizeof(int));
00083 map[1] = (int *)malloc(n * m * sizeof(int));
00084 x = (double *)malloc(n * sizeof(double));
00085 y = (double *)malloc(m * sizeof(double));
00086 chebx = (double *)malloc(order * n * sizeof(double));
00087 cheby = (double *)malloc(order * m * sizeof(double));
00088 nterms = order * order;
00089
00090
00091 for (i = 0; i < n; i++)
00092 x[i] = (double)(2.0 * i) / (n - 1) - 1;
00093 cheby_basis(n, order, x, chebx, n);
00094 for (j = 0; j < m; j++)
00095 y[j] = (double)(2.0 * j) / (m - 1) - 1;
00096 cheby_basis(m, order, y, cheby, m);
00097
00098
00099
00100 npix = 0;
00101 for (i = 0; i < n; i++)
00102 for (j = 0; j < m; j++) {
00103 pix = image[i * m + j];
00104 if (!isnan(pix)) {
00105 B[npix] = pix;
00106 map[0][npix] = i;
00107 map[1][npix] = j;
00108 npix++;
00109 }
00110 }
00111 for (s = 0; s < order; s++)
00112 for (t = 0; t < order; t++)
00113 for (l = 0; l < npix; l++) {
00114 i = map[0][l];
00115 j = map[1][l];
00116 A[(m * n) * (s * order + t) + l] = chebx[n * s + i] * cheby[m * t + j];
00117 }
00118
00119
00120 for (i = 0; i < npix; i++) {
00121 j = map[0][i] * m + map[1][i];
00122 B[i] = image[j];
00123 }
00124
00125
00126 mn = MIN(npix, nterms);
00127 lwork = MAX(1, mn + MAX(npix, MAX(nterms, 1)) * 32);
00128 work = (double *)malloc(lwork * sizeof(double));
00129
00130 dgels_("n", &npix, &nterms, &nimgs, A, &lda, B, &lda, work, &lwork, &info);
00131
00132 if (info < 0) {
00133 fprintf(stderr, "DGELS: The %dth argument had an illegal value.\n", -info);
00134 exit(1);
00135 }
00136
00137
00138
00139 if (fill) {
00140 for (i = 0; i < n; i++)
00141 for (j = 0; j < m; j++) {
00142 image[i * m + j] = 0;
00143 }
00144 for (s = 0; s < order; s++)
00145 for (t = 0; t < order; t++)
00146 for (i = 0; i < n; i++)
00147 for (j = 0; j < m; j++) {
00148 image[i * m + j] +=
00149 B[s * order + t] * chebx[n * s + i] * cheby[m * t + j];
00150 }
00151 } else {
00152 for (l = 0; l < npix; l++) {
00153 i = map[0][l];
00154 j = map[1][l];
00155 image[i * m + j] = 0;
00156 }
00157 for (s = 0; s < order; s++)
00158 for (t = 0; t < order; t++)
00159 for (l = 0; l < npix; l++) {
00160 i = map[0][l];
00161 j = map[1][l];
00162 image[i * m + j] +=
00163 B[s * order + t] * chebx[n * s + i] * cheby[m * t + j];
00164 }
00165 }
00166
00167
00168 free(map[0]); free(map[1]);
00169 free(x); free(y);
00170 free(chebx); free(cheby);
00171 free(A); free(B); free(work);
00172 }
00173
00174
00175 #undef MAX
00176 #undef MIN