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