00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <math.h>
00039 #include "projection.h"
00040
00041 char *cvsinfo_imageinterp = "cvsinfo: $Header: /home/cvsuser/cvsroot/JSOC/proj/globalhs/apps/imageinterp.c,v 1.4 2013/05/03 20:47:15 tplarson Exp $";
00042
00043 const int kMaxCols = 8192;
00044 const double kOmegaCarr = (360 / 27.27527);
00045 const double kRadsPerDegree = M_PI/180.;
00046
00047 static void Ccker(double *u, double s);
00048 static double Ccintd(double *f, int nx, int ny, double x, double y);
00049 static double Linintd(double *f, int nx, int ny, double x, double y);
00050 static void Distort(double x,
00051 double y,
00052 double rsun,
00053 int sign,
00054 double *xd,
00055 double *yd,
00056 const LIBPROJECTION_Dist_t *distpars);
00057
00058
00059 void Ccker(double *u, double s)
00060 {
00061 double s2, s3;
00062
00063 s2= s * s;
00064 s3= s2 * s;
00065 u[0] = s2 - 0.5 * (s3 + s);
00066 u[1] = 1.5*s3 - 2.5*s2 + 1.0;
00067 u[2] = -1.5*s3 + 2.0*s2 + 0.5*s;
00068 u[3] = 0.5 * (s3 - s2);
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 double Ccintd(double *f, int nx, int ny, double x, double y)
00090 {
00091 double ux[4], uy[4];
00092
00093 double sum;
00094
00095 int ix, iy, ix1, iy1, i, j;
00096
00097 if (x < 1. || x >= (double)(nx-2) ||
00098 y < 1. || y >= (double)(ny-2))
00099 return 0.0;
00100
00101 ix = (int)x;
00102 iy = (int)y;
00103 Ccker(ux, x - (double)ix);
00104 Ccker(uy, y - (double)iy);
00105
00106 ix1 = ix - 1;
00107 iy1 = iy - 1;
00108 sum = 0.;
00109 for (i = 0; i < 4; i++)
00110 for (j = 0; j < 4; j++)
00111 sum = sum + f[(iy1+i) * nx + ix1 + j] * uy[i] * ux[j];
00112 return sum;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 double Linintd(double *f, int nx, int ny, double x, double y)
00130 {
00131 double p0, p1, q0, q1;
00132 int ilow, jlow;
00133 double *fptr;
00134 double u;
00135
00136 ilow = (int)floor(x);
00137 jlow = (int)floor(y);
00138 if (ilow < 0) ilow = 0;
00139 if (ilow+1 >= nx) ilow -= 1;
00140 if (jlow < 0) jlow = 0;
00141 if (jlow+1 >= ny) jlow -= 1;
00142 p1 = x - ilow;
00143 p0 = 1.0 - p1;
00144 q1 = y - jlow;
00145 q0 = 1.0 - q1;
00146
00147
00148 u = 0.0;
00149 fptr = f + jlow*nx + ilow;
00150 u += p0 * q0 * *fptr++;
00151 u += p1 * q0 * *fptr;
00152 fptr += nx - 1;
00153 u += p0 * q1 * *fptr++;
00154 u += p1 * q1 * *fptr;
00155 return u;
00156 }
00157
00158
00159 int SetDistort(int dist,
00160 double cubic,
00161 double alpha,
00162 double beta,
00163 double feff,
00164 LIBPROJECTION_Dist_t *dOut)
00165 {
00166 int error = 0;
00167
00168 if (dOut != NULL)
00169 {
00170 int status = 0;
00171
00172 dOut->disttype = dist;
00173
00174
00175 if (dOut->disttype != 0)
00176 {
00177 if (dOut->disttype == 1)
00178 {
00179
00180 dOut->xc = 511.5;
00181 dOut->yc = 511.5;
00182 dOut->scale = 1.0;
00183 }
00184 else if (dOut->disttype == 2)
00185 {
00186
00187 dOut->xc = 95.1;
00188 dOut->yc = 95.1;
00189 dOut->scale = 5.0;
00190 }
00191 else
00192 {
00193 error = 1;
00194 }
00195
00196 if (!error)
00197 {
00198 dOut->feff = feff;
00199 dOut->alpha = alpha * kRadsPerDegree;
00200 beta *= kRadsPerDegree;
00201 dOut->cosbeta = cos(beta);
00202 dOut->sinbeta = sin(beta);
00203 dOut->cdist = cubic;
00204
00205 if (status != CMDPARAMS_SUCCESS)
00206 {
00207 error = 1;
00208 }
00209
00210 }
00211
00212 }
00213 }
00214 else
00215 {
00216 error = 1;
00217 }
00218
00219 return error;
00220 }
00221
00222
00223 void Distort(double x,
00224 double y,
00225 double rsun,
00226 int sign,
00227 double *xd,
00228 double *yd,
00229 const LIBPROJECTION_Dist_t *distpars)
00230 {
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 double xx,yy,xxl,yyl,x0,y0,x00,y00,x0l,y0l,epsx,epsy,rdist;
00241
00242 if (distpars->disttype == 0)
00243 {
00244 *xd=x;
00245 *yd=y;
00246 return;
00247 }
00248
00249
00250 rsun=rsun*distpars->scale;
00251
00252
00253
00254
00255
00256
00257 x00=distpars->scale*(x-distpars->xc);
00258 y00=distpars->scale*(y-distpars->yc);
00259
00260
00261
00262 rdist=distpars->cdist*(x00*x00+y00*y00-rsun*rsun);
00263
00264 x0=x00+rdist*x00;
00265 y0=y00+rdist*y00;
00266
00267
00268
00269
00270 x0l=x0*distpars->cosbeta+y0*distpars->sinbeta;
00271 y0l=-x0*distpars->sinbeta+y0*distpars->cosbeta;
00272
00273
00274 xxl=x0l*(1.-distpars->alpha*distpars->alpha/4.+y0l/distpars->feff*distpars->alpha);
00275 yyl=y0l*(1.+distpars->alpha*distpars->alpha/4.+y0l/distpars->feff*distpars->alpha) -
00276 rsun*rsun/distpars->feff*distpars->alpha;
00277
00278
00279 xx=xxl*distpars->cosbeta-yyl*distpars->sinbeta;
00280 yy=xxl*distpars->sinbeta+yyl*distpars->cosbeta;
00281
00282
00283
00284 epsx=xx-x00;
00285 epsy=yy-y00;
00286
00287
00288
00289 *xd=x+sign*epsx/distpars->scale;
00290 *yd=y+sign*epsy/distpars->scale;
00291 }
00292
00293
00294 int imageinterp(
00295 float *V,
00296
00297 float *U,
00298
00299 int xpixels,
00300 int ypixels,
00301 double x0,
00302 double y0,
00303 double P,
00304
00305 double rsun,
00306 double Rmax,
00307 int NaN_beyond_rmax,
00308 int interpolation,
00309 int cols,
00310 int rows,
00311 double vsign,
00312 const LIBPROJECTION_Dist_t *distpars)
00313 {
00314
00315 float u;
00316 double x, y;
00317 int row, col;
00318 int rowoffset;
00319 double xratio, yratio, Rmax2;
00320
00321 long i;
00322 double *vdp;
00323 float *vp;
00324 double xtmp, ytmp;
00325
00326 double *VD;
00327 VD=(double *)(malloc(xpixels*ypixels*sizeof(double)));
00328 vdp=VD;
00329 vp=V;
00330 for (i=0;i<xpixels*ypixels;i++) *vdp++=(double)*vp++;
00331
00332 Rmax2 = Rmax*Rmax*rsun*rsun;
00333
00334 if (cols > kMaxCols)
00335 {
00336 return kLIBPROJECTION_DimensionMismatch;
00337 }
00338
00339
00340 if (interpolation != kLIBPROJECTION_InterCubic && interpolation != kLIBPROJECTION_InterBilinear)
00341 {
00342 return kLIBPROJECTION_UnsupportedInterp;
00343 }
00344
00345 xratio=(double)xpixels/cols;
00346 yratio=(double)ypixels/rows;
00347
00348 for (row = 0; row < rows; row++) {
00349
00350 rowoffset = row*cols;
00351
00352 for (col = 0; col < cols; col++) {
00353
00354 xtmp = (col + 0.5)*xratio - 0.5 - x0;
00355 ytmp = (row + 0.5)*yratio - 0.5 - y0;
00356 if ((xtmp*xtmp + ytmp*ytmp) >= Rmax2)
00357 {
00358 *(U + rowoffset + col) = (NaN_beyond_rmax) ? DRMS_MISSING_FLOAT : (float)0.0;
00359 continue;
00360 }
00361 x= x0 + xtmp*cos(P) - ytmp*sin(P);
00362 y= y0 + xtmp*sin(P) + ytmp*cos(P);
00363
00364 Distort(x, y, rsun, +1, &x, &y, distpars);
00365
00366 if (interpolation == kLIBPROJECTION_InterCubic)
00367 {
00368 u = (float)(vsign * Ccintd (VD,xpixels,ypixels,x,y));
00369 }
00370 else if (interpolation == kLIBPROJECTION_InterBilinear)
00371 {
00372 u = (float)(vsign * Linintd (VD,xpixels,ypixels,x,y));
00373 }
00374
00375 *(U + rowoffset + col) = u;
00376 }
00377 }
00378 free(VD);
00379 return kLIBPROJECTION_Success;
00380 }