void Lanczose_Scale(IplImage *in, IplImage *out, int owidth, int oheight) { int channels = in->nChannels; int iwidth = in->width; int iheight = in->height; int i,j,k,x,y; for(x=0;x<owidth;x++) for(y=0;y<oheight;y++) { double x0 = floor(x*1.0*iwidth/owidth); double y0 = floor(y*1.0*iheight/oheight); CvScalar temp, temp2; for(k=0;k<channels;k++) temp2.val[k] = 0; for(i=x0-a+1;i<=x0+a;i++) for(j=y0-a+1;j<=y0+a;j++) if(i>=0 && i<iwidth && j>=0 && j<iheight) { temp = cvGet2D(in,j,i); for(k=0;k<channels;k++) { temp2.val[k] += temp.val[k]*Lanczos(x0-i)*Lanczos(y0-j); } } cvSet2D(out,y,x,temp2); } }
void BaseArray::ResampleLanczos(int sizex, int sizey, int n){ double xratio, yratio, sum, weight, x, y, Lx, Ly; std::pair<int, int> size = Size(); std::vector< std::vector<double> > tmp (sizex, std::vector<double>(sizey)); xratio = sizex/(double)size.first; yratio = sizey/(double)size.second; for (int i = 0; i < sizex; i++){ for (int j = 0; j < sizey; j++){ x = i/xratio; y = j/yratio; sum = 0; weight = 0; for (int r = -n+1; r < n; r++){ for (int s = -n+1; s < n; s++){ int cx = floor(x)+r, cy = floor(y)+s; Lx = Lanczos(cx-x, n); Ly = Lanczos(cy-y, n); weight += Lx * Ly; sum += At(cx, cy, true) * Lx * Ly; } } sum = sum/weight; sum = (sum < 0) ? 0 : sum; sum = (sum > 255) ? 255 : sum; tmp[i][j] = sum; } } data = tmp; }
pair<Base<F>,Base<F>> HermitianHelper( const DistSparseMatrix<F>& A, Int basisSize ) { typedef Base<F> Real; Grid grid( A.Comm() ); DistMatrix<Real,STAR,STAR> T(grid); Lanczos( A, T, basisSize ); const Int k = T.Height(); if( k == 0 ) return pair<Real,Real>(0,0); auto d = GetDiagonal( T.Matrix() ); auto dSub = GetDiagonal( T.Matrix(), -1 ); Matrix<Real> w; HermitianTridiagEig( d, dSub, w ); pair<Real,Real> extremal; extremal.second = MaxNorm(w); extremal.first = extremal.second; for( Int i=0; i<k; ++i ) extremal.first = Min(extremal.first,Abs(w.Get(i,0))); return extremal; }
pair<Base<F>,Base<F>> HermitianExtremalSingValEst( const SparseMatrix<F>& A, Int basisSize ) { EL_DEBUG_CSE typedef Base<F> Real; Matrix<Real> T; Lanczos( A, T, basisSize ); const Int k = T.Height(); if( k == 0 ) return pair<Real,Real>(0,0); Matrix<Real> d, dSub; d = GetDiagonal( T ); dSub = GetDiagonal( T, -1 ); Matrix<Real> w; HermitianTridiagEig( d, dSub, w ); pair<Real,Real> extremal; extremal.second = MaxNorm(w); extremal.first = extremal.second; for( Int i=0; i<k; ++i ) extremal.first = Min(extremal.first,Abs(w(i))); return extremal; }
/*------------------------------------------------------------------------------ * Constructor is used as the main driver *----------------------------------------------------------------------------*/ Green::Green(const int ntm, const int sdim, const int niter, const double min, const double max, const int ndos, const double eps, double **Hessian, const int itm, double **lpdos) { const double tpi = 8.*atan(1.); natom = ntm; sysdim = sdim; nit = niter; epson = eps; wmin = min*tpi; wmax = max*tpi; nw = ndos + (ndos+1)%2; H = Hessian; iatom = itm; ldos = lpdos; memory = new Memory(); if (natom < 1 || iatom < 0 || iatom >= natom){ printf("\nError: Wrong number of total atoms or wrong index of interested atom!\n"); return; } ndim = natom * sysdim; if (nit < 1){printf("\nError: Wrong input of maximum iterations!\n"); return;} if (nit > ndim){printf("\nError: # Lanczos iterations is not expected to exceed the degree of freedom!\n"); return;} if (nw < 1){printf("\nError: Wrong input of points in LDOS!\n"); return;} // initialize variables and allocate local memories dw = (wmax - wmin)/double(nw-1); memory->create(alpha, sysdim,nit, "Green_Green:alpha"); memory->create(beta, sysdim,nit+1,"Green_Green:beta"); //memory->create(ldos, nw,sysdim, "Green_Green:ldos"); // use Lanczos algorithm to diagonalize the Hessian Lanczos(); // Get the inverser of the treated hessian by continued fractional method Recursion(); return; }
void Img::ResampleLanczos(int sizex, int sizey, int n){ double xratio, yratio, sum, weight, x, y, Lx, Ly; std::pair<int, int> size = SizeOriginal(); std::vector< std::vector<double> > tmp (sizex, std::vector<double>(sizey)); xratio = sizex/(double)size.first; yratio = sizey/(double)size.second; for (int i = 0; i < sizex; i++){ for (int j = 0; j < sizey; j++){ x = i/xratio; y = j/yratio; sum = 0; weight = 0; for (int r = -n+1; r < n; r++){ for (int s = -n+1; s < n; s++){ int cx = floor(x)+r; int cy = floor(y)+s; Lx = Lanczos(cx-x, n); Ly = Lanczos(cy-y, n); weight += (Lx * Ly); sum += (AtOriginal(cx, cy, true) * Lx * Ly); } } sum = sum/weight; /*if (sum < 0){ x = floor(x); y = floor(y); sum = AtOriginal(x, y, true); //sum = 0.25 * (AtOriginal(x-1, y-1, true) + AtOriginal(x-1, y+1, true) + AtOriginal(x+1, y-1, true) + AtOriginal(x+1, y+1, true)); } else if (sum > 255) { x = floor(x); y = floor(y); sum = AtOriginal(x, y, true); //sum = 0.25 * (AtOriginal(x-1, y-1, true) + AtOriginal(x-1, y+1, true) + AtOriginal(x+1, y-1, true) + AtOriginal(x+1, y+1, true)); } */ sum = (sum < 0) ? 0 : sum; sum = (sum > 255) ? 255 : sum; tmp[i][j] = sum; } } data = tmp; }
// Thread-safe routine void PCL_FUNC PCL_InitializeLanczosLUT( float*& LUT, int n ) { static Mutex mutex; volatile AutoLock lock( mutex ); if ( LUT == 0 ) { LUT = new float[ n*__PCL_LANCZOS_LUT_RESOLUTION + 1 ]; for ( int i = 0, k = 0; i < n; ++i ) for ( int j = 0; j < __PCL_LANCZOS_LUT_RESOLUTION; ++j, ++k ) LUT[k] = Lanczos( i + double( j )/__PCL_LANCZOS_LUT_RESOLUTION, n ); LUT[n*__PCL_LANCZOS_LUT_RESOLUTION] = 0; } }
const float* PCL_FUNC PCL_InitializeLanczosIntLUT( int n ) { ValidateLanczosLUTOrder( n ); { static Mutex mutex; volatile AutoLock lock( mutex ); float*& LUT = PCL_Lanczos_Int_LUT[n-1]; if ( LUT == nullptr ) { LUT = new float[ n*__PCL_LANCZOS_LUT_INT_RESOLUTION + 1 ]; for ( int i = 0, k = 0; i < n; ++i ) for ( int j = 0; j < __PCL_LANCZOS_LUT_INT_RESOLUTION; ++j, ++k ) LUT[k] = Lanczos( i + double( j )/__PCL_LANCZOS_LUT_INT_RESOLUTION, n ); LUT[n*__PCL_LANCZOS_LUT_INT_RESOLUTION] = 0; } return reinterpret_cast<const float*>( LUT ); } }
const double** PCL_FUNC PCL_InitializeLanczosRealLUT( int n ) { ValidateLanczosLUTOrder( n ); { static Mutex mutex; volatile AutoLock lock( mutex ); double**& LUT = PCL_Lanczos_LUT[n-1]; if ( LUT == nullptr ) { LUT = new double*[ 2*n ]; for ( int j = -n + 1, k = 0; j <= n; ++j, ++k ) { LUT[k] = new double[ __PCL_LANCZOS_LUT_REAL_RESOLUTION ]; for ( int i = 0; i < __PCL_LANCZOS_LUT_REAL_RESOLUTION; ++i ) LUT[k][i] = Lanczos( j - double( i )/__PCL_LANCZOS_LUT_REAL_RESOLUTION, n ); } } return const_cast<const double**>( reinterpret_cast<double**>( LUT ) ); } }
static inline void Resample(SDL_Surface * src, SDL_Surface * dst, int filter) { const double blur = 1.0; double factor = dst->w / (double)src->w; double scale = std::min(factor, 1.0) / blur; int FilterRadius = filter; if (filter < 1 ) FilterRadius = 1; if (filter > 3) //automatically determine fastest filter setting { FilterRadius = 3; if (scale < 0.67) FilterRadius = 2; if (scale <= 0.5) FilterRadius = 1; } double support = FilterRadius / scale; std::vector<double> contribution_x(std::min((size_t)src->w, 5+(size_t)(2*support))); /* 5 = room for rounding up in calculations of start, stop and support */ Uint32 ** temp = new Uint32 * [src->h]; //array of source->height * dest->width for (int i = 0 ; i < src->h; i++) temp[i] = new Uint32 [dst->w]; if (support <= 0.5) { support = 0.5 + 1E-12; scale = 1.0; } for (int x = 0; x < dst->w; ++x) { double center = (x + 0.5) / factor; size_t start = (size_t)std::max(center - support + 0.5, (double)0); size_t stop = (size_t)std::min(center + support + 0.5, (double)src->w); double density = 0.0; size_t nmax = stop - start; double s = start - center + 0.5; double point[4] = {0,0,0,0}; Uint8 v; double diff; for (int y = 0; y < src->h; y++) { for (size_t n = 0; n < nmax; ++n) { if (y == 0) { //only come up with the contribution list once per column. contribution_x[n] = Lanczos (s * scale, FilterRadius); density += contribution_x[n]; s++; } //it MUST be a 32-bit surface for following code to work correctly Uint8 * p = (Uint8 *)src->pixels + y * src->pitch + (start+n) * 4; for (int c = 0; c < 4; c++) point[c] += p[c] * contribution_x[n]; } /* Normalize. Truncate to Uint8 values. Place in temp array*/ Uint8 * p = (Uint8 *)&temp[y][x]; for (size_t c = 0; c < 4; c++) { if (density != 0.0 && density != 1.0) point[c] /= density; if (point[c] < 0) point[c] = 0; if (point[c] > 255) point[c] = 255; v = (Uint8) point[c]; diff = point[c] - (double)v; if (diff < 0) diff = -diff; if (diff >= 0.5) v++; p[c] = v; point[c] = 0; //reset value for next loop } } } factor = dst->h / (double)src->h; scale = std::min(factor, 1.0) / blur; if (filter > 3) //automatically determine fastest filter setting { FilterRadius = 3; if (scale < 0.67) FilterRadius = 2; if (scale <= 0.5) FilterRadius = 1; } support = FilterRadius / scale; std::vector<double> contribution_y(std::min((size_t)src->h, 5+(size_t)(2*support))); if (support <= 0.5) { support = 0.5 + 1E-12; scale = 1.0; } for (int y = 0; y<dst->h; ++y) { double center = (y + 0.5) / factor; size_t start = (size_t)std::max(center - support + 0.5, (double)0); size_t stop = (size_t)std::min(center + support + 0.5, (double)src->h); double density = 0.0; size_t nmax = stop-start; double s = start - center+0.5; double point[4] = {0,0,0,0}; Uint8 v; double diff; for (int x=0; x<dst->w; x++) { for (size_t n=0; n<nmax; ++n) { if (x == 0) { contribution_y[n] = Lanczos(s * scale, FilterRadius); density += contribution_y[n]; s++; } Uint8 * p = (Uint8 *)&temp[start+n][x]; for (int c = 0; c < 4; c++) point[c] += p[c] * contribution_y[n]; } //destination must also be a 32 bit surface for this to work! Uint8 * p = (Uint8 *)dst->pixels + y * dst->pitch + x * 4; for (size_t c = 0; c < 4; c++) { if (density != 0.0 && density != 1.0) point[c] /= density; if (point[c] < 0) point[c] = 0; if (point[c] > 255) point[c] = 255; v = (Uint8) point[c]; diff = point[c] - (double)v; if (diff < 0) diff = -diff; if (diff >= 0.5) v++; p[c] = v; point[c] = 0; } } } //free the temp array, so we don't leak any memory for (int i = 0 ; i < src->h; i++) delete [] temp[i]; delete [] temp; }