CPLErr ComplexPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) { int ii, iLine, iCol; double adfPixVal[2]; void *pReal = papoSources[0]; void *pImag = papoSources[1]; /* ---- Init ---- */ if (nSources != 2) return CE_Failure; /* ---- Set pixels ---- */ for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { /* Source raster pixels may be obtained with SRCVAL macro */ adfPixVal[0] = SRCVAL(pReal, eSrcType, ii); /* re */ adfPixVal[1] = SRCVAL(pImag, eSrcType, ii); /* im */ GDALCopyWords(adfPixVal, GDT_CFloat64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } /* ---- Return success ---- */ return CE_None; } /* MakeComplexPixelFunc */
static CPLErr ComplexPixelFunc( void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace ) { /* ---- Init ---- */ if( nSources != 2 ) return CE_Failure; const void * const pReal = papoSources[0]; const void * const pImag = papoSources[1]; /* ---- Set pixels ---- */ for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { // Source raster pixels may be obtained with SRCVAL macro. const double adfPixVal[2] = { SRCVAL(pReal, eSrcType, ii), // re SRCVAL(pImag, eSrcType, ii) // im }; GDALCopyWords(adfPixVal, GDT_CFloat64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } /* ---- Return success ---- */ return CE_None; } // MakeComplexPixelFunc
static CPLErr SumPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) { /* ---- Init ---- */ if( nSources < 2 ) return CE_Failure; /* ---- Set pixels ---- */ if( GDALDataTypeIsComplex( eSrcType ) ) { const int nOffset = GDALGetDataTypeSizeBytes( eSrcType ) / 2; /* ---- Set pixels ---- */ for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { double adfSum[2] = { 0.0, 0.0 }; for( int iSrc = 0; iSrc < nSources; ++iSrc ) { const void * const pReal = papoSources[iSrc]; const void * const pImag = static_cast<const GByte *>(pReal) + nOffset; // Source raster pixels may be obtained with SRCVAL macro. adfSum[0] += SRCVAL(pReal, eSrcType, ii); adfSum[1] += SRCVAL(pImag, eSrcType, ii); } GDALCopyWords( adfSum, GDT_CFloat64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } else { /* ---- Set pixels ---- */ for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { double dfSum = 0; // Not complex. for( int iSrc = 0; iSrc < nSources; ++iSrc ) { // Source raster pixels may be obtained with SRCVAL macro. dfSum += SRCVAL(papoSources[iSrc], eSrcType, ii); } GDALCopyWords( &dfSum, GDT_Float64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } /* ---- Return success ---- */ return CE_None; } /* SumPixelFunc */
static CPLErr Log10PixelFuncHelper( void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace, double fact ) { /* ---- Init ---- */ if( nSources != 1 ) return CE_Failure; if( GDALDataTypeIsComplex( eSrcType ) ) { // Complex input datatype. const int nOffset = GDALGetDataTypeSizeBytes( eSrcType ) / 2; const void * const pReal = papoSources[0]; const void * const pImag = static_cast<GByte *>(papoSources[0]) + nOffset; /* ---- Set pixels ---- */ for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { // Source raster pixels may be obtained with SRCVAL macro. const double dfReal = SRCVAL(pReal, eSrcType, ii); const double dfImag = SRCVAL(pImag, eSrcType, ii); const double dfPixVal = fact * log10( sqrt( dfReal * dfReal + dfImag * dfImag ) ); GDALCopyWords( &dfPixVal, GDT_Float64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1 ); } } } else { /* ---- Set pixels ---- */ for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { // Source raster pixels may be obtained with SRCVAL macro. const double dfPixVal = fact * log10( fabs( SRCVAL(papoSources[0], eSrcType, ii) ) ); GDALCopyWords( &dfPixVal, GDT_Float64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1 ); } } } /* ---- Return success ---- */ return CE_None; } // Log10PixelFuncHelper
CPLErr InvPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) { int iLine, iCol, ii; /* ---- Init ---- */ if (nSources != 1) return CE_Failure; /* ---- Set pixels ---- */ if (GDALDataTypeIsComplex( eSrcType )) { double adfPixVal[2], dfReal, dfImag, dfAux; int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; void *pReal = papoSources[0]; void *pImag = ((GByte *)papoSources[0]) + nOffset; for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { /* Source raster pixels may be obtained with SRCVAL macro */ dfReal = SRCVAL(pReal, eSrcType, ii); dfImag = SRCVAL(pImag, eSrcType, ii); dfAux = dfReal * dfReal + dfImag * dfImag; adfPixVal[0] = +dfReal / dfAux; adfPixVal[1] = -dfImag / dfAux; GDALCopyWords(adfPixVal, GDT_CFloat64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } else { /* non complex */ double dfPixVal; /* ---- Set pixels ---- */ for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { /* Source raster pixels may be obtained with SRCVAL macro */ dfPixVal = 1. / SRCVAL(papoSources[0], eSrcType, ii); GDALCopyWords(&dfPixVal, GDT_Float64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } /* ---- Return success ---- */ return CE_None; } /* InvPixelFunc */
CPLErr Log10PixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) { int ii, iLine, iCol; /* ---- Init ---- */ if (nSources != 1) return CE_Failure; if (GDALDataTypeIsComplex( eSrcType )) { /* complex input datatype */ double dfReal, dfImag, dfPixVal; int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; void *pReal = papoSources[0]; void *pImag = ((GByte *)papoSources[0]) + nOffset; /* ---- Set pixels ---- */ for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { /* Source raster pixels may be obtained with SRCVAL macro */ dfReal = SRCVAL(pReal, eSrcType, ii); dfImag = SRCVAL(pImag, eSrcType, ii); dfPixVal = log10( dfReal * dfReal + dfImag * dfImag ); GDALCopyWords(&dfPixVal, GDT_Float64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } else { double dfPixVal; /* ---- Set pixels ---- */ for( iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { /* Source raster pixels may be obtained with SRCVAL macro */ dfPixVal = SRCVAL(papoSources[0], eSrcType, ii); dfPixVal = log10( abs( dfPixVal ) ); GDALCopyWords(&dfPixVal, GDT_Float64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } /* ---- Return success ---- */ return CE_None; } /* Log10PixelFunc */
static CPLErr InvPixelFunc( void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace ) { /* ---- Init ---- */ if( nSources != 1 ) return CE_Failure; /* ---- Set pixels ---- */ if( GDALDataTypeIsComplex( eSrcType ) ) { const int nOffset = GDALGetDataTypeSizeBytes( eSrcType ) / 2; const void * const pReal = papoSources[0]; const void * const pImag = static_cast<GByte *>(papoSources[0]) + nOffset; for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { // Source raster pixels may be obtained with SRCVAL macro. const double dfReal = SRCVAL(pReal, eSrcType, ii); const double dfImag = SRCVAL(pImag, eSrcType, ii); const double dfAux = dfReal * dfReal + dfImag * dfImag; const double adfPixVal[2] = { dfReal / dfAux, -dfImag / dfAux }; GDALCopyWords( adfPixVal, GDT_CFloat64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } else { /* ---- Set pixels ---- */ for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { // Source raster pixels may be obtained with SRCVAL macro. // Not complex. const double dfPixVal = 1.0 / SRCVAL(papoSources[0], eSrcType, ii); GDALCopyWords( &dfPixVal, GDT_Float64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1 ); } } } /* ---- Return success ---- */ return CE_None; } // InvPixelFunc
static CPLErr PhasePixelFunc( void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace ) { /* ---- Init ---- */ if( nSources != 1 ) return CE_Failure; if( GDALDataTypeIsComplex( eSrcType ) ) { const void * const pReal = papoSources[0]; const void * const pImag = static_cast<GByte *>(papoSources[0]) + GDALGetDataTypeSizeBytes( eSrcType ) / 2; /* ---- Set pixels ---- */ for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { // Source raster pixels may be obtained with SRCVAL macro. const double dfReal = SRCVAL(pReal, eSrcType, ii); const double dfImag = SRCVAL(pImag, eSrcType, ii); const double dfPixVal = atan2(dfImag, dfReal); GDALCopyWords( &dfPixVal, GDT_Float64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1 ); } } } else { /* ---- Set pixels ---- */ for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { const void * const pReal = papoSources[0]; // Source raster pixels may be obtained with SRCVAL macro. const double dfReal = SRCVAL(pReal, eSrcType, ii); const double dfPixVal = (dfReal < 0) ? M_PI : 0.0; GDALCopyWords( &dfPixVal, GDT_Float64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1 ); } } } /* ---- Return success ---- */ return CE_None; } // PhasePixelFunc
static CPLErr SqrtPixelFunc( void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace ) { /* ---- Init ---- */ if( nSources != 1 ) return CE_Failure; if( GDALDataTypeIsComplex( eSrcType ) ) return CE_Failure; /* ---- Set pixels ---- */ for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { // Source raster pixels may be obtained with SRCVAL macro. const double dfPixVal = sqrt( SRCVAL(papoSources[0], eSrcType, ii) ); GDALCopyWords( &dfPixVal, GDT_Float64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } /* ---- Return success ---- */ return CE_None; } // SqrtPixelFunc
CPLErr PowPixelFuncHelper(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace, double base, double fact) { int iLine, iCol, ii; double dfPixVal; /* ---- Init ---- */ if (nSources != 1) return CE_Failure; if (GDALDataTypeIsComplex( eSrcType )) return CE_Failure; /* ---- Set pixels ---- */ for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { /* Source raster pixels may be obtained with SRCVAL macro */ dfPixVal = SRCVAL(papoSources[0], eSrcType, ii); dfPixVal = pow(base, dfPixVal / fact); GDALCopyWords(&dfPixVal, GDT_Float64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } /* ---- Return success ---- */ return CE_None; } /* PowPixelFuncHelper */
static CPLErr ConjPixelFunc( void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace ) { /* ---- Init ---- */ if( nSources != 1 ) return CE_Failure; if( GDALDataTypeIsComplex( eSrcType ) && GDALDataTypeIsComplex( eBufType ) ) { const int nOffset = GDALGetDataTypeSizeBytes( eSrcType ) / 2; const void * const pReal = papoSources[0]; const void * const pImag = static_cast<GByte *>(papoSources[0]) + nOffset; /* ---- Set pixels ---- */ for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { // Source raster pixels may be obtained with SRCVAL macro. const double adfPixVal[2] = { +SRCVAL(pReal, eSrcType, ii), // re -SRCVAL(pImag, eSrcType, ii) // im }; GDALCopyWords( adfPixVal, GDT_CFloat64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1 ); } } } else { // No complex data type. return RealPixelFunc(papoSources, nSources, pData, nXSize, nYSize, eSrcType, eBufType, nPixelSpace, nLineSpace); } /* ---- Return success ---- */ return CE_None; } // ConjPixelFunc
CPLErr ConjPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) { /* ---- Init ---- */ if (nSources != 1) return CE_Failure; if (GDALDataTypeIsComplex( eSrcType ) && GDALDataTypeIsComplex( eBufType )) { int iLine, iCol, ii; double adfPixVal[2]; int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; void *pReal = papoSources[0]; void *pImag = ((GByte *)papoSources[0]) + nOffset; /* ---- Set pixels ---- */ for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { /* Source raster pixels may be obtained with SRCVAL macro */ adfPixVal[0] = +SRCVAL(pReal, eSrcType, ii); /* re */ adfPixVal[1] = -SRCVAL(pImag, eSrcType, ii); /* im */ GDALCopyWords(adfPixVal, GDT_CFloat64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } else { /* no complex data type */ return RealPixelFunc(papoSources, nSources, pData, nXSize, nYSize, eSrcType, eBufType, nPixelSpace, nLineSpace); } /* ---- Return success ---- */ return CE_None; } /* ConjPixelFunc */
CPLErr CMulPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) { int ii, iLine, iCol; /* ---- Init ---- */ if (nSources != 2) return CE_Failure; /* ---- Set pixels ---- */ if (GDALDataTypeIsComplex( eSrcType )) { double adfPixVal[2], dfReal0, dfImag0, dfReal1, dfImag1; int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; void *pReal0 = papoSources[0]; void *pImag0 = ((GByte *)papoSources[0]) + nOffset; void *pReal1 = papoSources[1]; void *pImag1 = ((GByte *)papoSources[1]) + nOffset; for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { /* Source raster pixels may be obtained with SRCVAL macro */ dfReal0 = SRCVAL(pReal0, eSrcType, ii); dfReal1 = SRCVAL(pReal1, eSrcType, ii); dfImag0 = SRCVAL(pImag0, eSrcType, ii); dfImag1 = SRCVAL(pImag1, eSrcType, ii); adfPixVal[0] = dfReal0 * dfReal1 + dfImag0 * dfImag1; adfPixVal[1] = dfReal1 * dfImag0 - dfReal0 * dfImag1; GDALCopyWords(adfPixVal, GDT_CFloat64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } else { /* non complex */ double adfPixVal[2] = {0, 0}; for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { /* Source raster pixels may be obtained with SRCVAL macro */ adfPixVal[0] = SRCVAL(papoSources[0], eSrcType, ii) * SRCVAL(papoSources[1], eSrcType, ii); GDALCopyWords(adfPixVal, GDT_CFloat64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } /* ---- Return success ---- */ return CE_None; } /* CMulPixelFunc */
void Slic::_ComputeNewCenterVector() { std::vector<int> centerCounts(_centerVector.size(),0); uchar *bufferSrc = new uchar[_width*_dataSize*_bandCount]; int *bufferDst = new int[_width]; for (unsigned int i=0;i<_centerVector.size();++i) { _centerVector[i].resize(_bandCount+2,0.); } for (int i=0;i<_height;++i) { _poSrcDS->RasterIO(GF_Read,0,i,_width,1,bufferSrc,_width,1,_dataType,_bandCount,0,0,0,0); _poDstDS->GetRasterBand(1)->RasterIO(GF_Read,0,i,_width,1,bufferDst,_width,1,GDT_Int32,0,0); for (int j=0;j<_width;++j) { uchar* p = bufferSrc; int nCenterID = bufferDst[j]; centerCounts[nCenterID]++; for(int k=0;k<_bandCount;++k,p+=_width*_dataSize) { _centerVector[nCenterID][k] += SRCVAL(p,_dataType,j); } _centerVector[nCenterID][_bandCount] += static_cast<double>(j); _centerVector[nCenterID][_bandCount+1] += static_cast<double>(i); } } for (unsigned int i=0;i<_centerVector.size();++i) { if (centerCounts[i]==0) std::cerr<<"The "<<i<<"th superpixel has no pixels!"<<std::endl; for(int k=0;k<_bandCount;++k) { _centerVector[i][k] = _centerVector[i][k]/centerCounts[i]/_regularizer; } _centerVector[i][_bandCount] /= centerCounts[i]*_regionSize; _centerVector[i][_bandCount+1] /= centerCounts[i]*_regionSize; } delete []bufferSrc; delete []bufferDst; }
void Slic::_InitCenterFeature(int i, int j, FeatureVector &featureVec) { featureVec.clear(); uchar *buffer = new uchar[5*5*_dataSize*_bandCount]; _poSrcDS->RasterIO(GF_Read,j-2,i-2,5,5,buffer,5,5,_dataType,_bandCount,0,0,0,0); double minEdge = std::numeric_limits<double>::max(); int minN; int minM; for (int n=1;n<4;++n) { for(int m=1;m<4;++m) { double edge = 0; edge += (buffer[(n-1)*5+m]-buffer[(n+1)*5+m])*(buffer[(n-1)*5+m]-buffer[(n+1)*5+m]); edge += (buffer[n*5+m-1]-buffer[n*5+m+1])*(buffer[n*5+m-1]-buffer[n*5+m+1]); if (edge < minEdge) { minEdge = edge; minN = n; minM = m; } } } int centerIndex = minN*5+minM; uchar* p = buffer; for(int k=0;k<_bandCount;++k,p += (5*5*_dataSize)) { featureVec.push_back( SRCVAL(p,_dataType,centerIndex)/_regularizer); } featureVec.push_back(static_cast<double>(j+minM-2)/_regionSize); //x featureVec.push_back(static_cast<double>(i+minN-2)/_regionSize); //y delete []buffer; }
void Slic::_GenerateSuperpixels() { const int MAX_ITER = 10; for(int I=0;I<MAX_ITER;++I) { clock_t t1 = clock(); std::cout<<"This is the "<<I<<"th circulation:"<<std::endl; omp_init_lock(&lock); #pragma omp parallel for for(int n=0;n<_N;++n) { for(int m=0;m<_M;++m) { // Init int nXOff = m*_regionSize; int nYOff = n*_regionSize; int nXSize = m==(_M-1)? _width - m*_regionSize : _regionSize; int nYSize = n==(_N-1)? _height - n*_regionSize : _regionSize; uchar *bufferSrc = new uchar[nXSize*nYSize*_dataSize*_bandCount]; int *bufferDst = new int[nXSize*nYSize]; omp_set_lock(&lock); _poSrcDS->RasterIO(GF_Read,nXOff,nYOff,nXSize,nYSize,bufferSrc,nXSize,nYSize,_dataType,_bandCount,0,0,0,0); _poDstDS->GetRasterBand(1)->RasterIO(GF_Read,nXOff,nYOff,nXSize,nYSize,bufferDst,nXSize,nYSize,GDT_Int32,0,0); omp_unset_lock(&lock); std::vector< int > candidateCenterID; for(int i=-1;i<2;++i) { for(int j=-1;j<2;++j) { if ((n+i)>=0 && (n+i)<_N && (m+j)>=0 && (m+j)<_M) candidateCenterID.push_back( (n+i) * _M + (m+j) ) ; } } // GetFeatureInfo FeatureVector featureVec(_bandCount + 2); for(int i=0,index=0;i<nYSize;++i) { for(int j=0;j<nXSize;++j,++index) { uchar* p = bufferSrc; for(int k=0;k<_bandCount;++k,p += nXSize*nYSize*_dataSize) { featureVec[k]= SRCVAL(p,_dataType,index)/_regularizer; } featureVec[_bandCount] = static_cast<double>(nXOff+j)/_regionSize; //x featureVec[_bandCount+1] = static_cast<double>(nYOff+i)/_regionSize; //y bufferDst[i*nXSize+j] = _GetNearestCenter(candidateCenterID,featureVec); } } omp_set_lock(&lock); _poDstDS->GetRasterBand(1)->RasterIO(GF_Write,nXOff,nYOff,nXSize,nYSize,bufferDst,nXSize,nYSize,GDT_Int32,0,0); omp_unset_lock(&lock); delete []bufferSrc; delete []bufferDst; } } omp_destroy_lock(&lock); _ComputeNewCenterVector(); std::cout<<"This circle cost: "<<static_cast<double>(clock()-t1)/CLOCKS_PER_SEC<<"s"<<std::endl; } }
CPLErr MulPixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) { int iLine, iCol, ii, iSrc; /* ---- Init ---- */ if (nSources < 2) return CE_Failure; /* ---- Set pixels ---- */ if (GDALDataTypeIsComplex( eSrcType )) { double adfPixVal[2], dfOldR, dfOldI, dfNewR, dfNewI; void *pReal, *pImag; int nOffset = GDALGetDataTypeSize( eSrcType ) / 8 / 2; /* ---- Set pixels ---- */ for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { adfPixVal[0] = 1.; adfPixVal[1] = 0.; for( iSrc = 0; iSrc < nSources; ++iSrc ) { pReal = papoSources[iSrc]; pImag = ((GByte *)pReal) + nOffset; dfOldR = adfPixVal[0]; dfOldI = adfPixVal[1]; /* Source raster pixels may be obtained with SRCVAL macro */ dfNewR = SRCVAL(pReal, eSrcType, ii); dfNewI = SRCVAL(pImag, eSrcType, ii); adfPixVal[0] = dfOldR * dfNewR - dfOldI * dfNewI; adfPixVal[1] = dfOldR * dfNewI + dfOldI * dfNewR; } GDALCopyWords(adfPixVal, GDT_CFloat64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } else { /* non complex */ double dfPixVal; /* ---- Set pixels ---- */ for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { dfPixVal = 1; for( iSrc = 0; iSrc < nSources; ++iSrc ) { /* Source raster pixels may be obtained with SRCVAL macro */ dfPixVal *= SRCVAL(papoSources[iSrc], eSrcType, ii); } GDALCopyWords(&dfPixVal, GDT_Float64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } /* ---- Return success ---- */ return CE_None; } /* MulPixelFunc */
CPLErr PhasePixelFunc(void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace) { int ii, iLine, iCol; double dfPixVal, dfReal; /* ---- Init ---- */ if (nSources != 1) return CE_Failure; if (GDALDataTypeIsComplex( eSrcType )) { double dfImag; void *pReal = papoSources[0]; void *pImag = ((GByte *)papoSources[0]) + GDALGetDataTypeSize( eSrcType ) / 8 / 2; /* ---- Set pixels ---- */ for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { /* Source raster pixels may be obtained with SRCVAL macro */ dfReal = SRCVAL(pReal, eSrcType, ii); dfImag = SRCVAL(pImag, eSrcType, ii); dfPixVal = atan2(dfImag, dfReal); GDALCopyWords(&dfPixVal, GDT_Float64, 0, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } else { /* ---- Set pixels ---- */ /* for( iLine = 0; iLine < nYSize; ++iLine ) { / * always copy from the same location * / GDALCopyWords(&dfImag, eSrcType, 0, ((GByte *)pData) + nLineSpace * iLine, eBufType, nPixelSpace, nXSize); } */ /* ---- Set pixels ---- */ double pi = atan2(0, -1); for( iLine = 0, ii= 0; iLine < nYSize; ++iLine ) { for( iCol = 0; iCol < nXSize; ++iCol, ++ii ) { void *pReal = papoSources[0]; /* Source raster pixels may be obtained with SRCVAL macro */ dfReal = SRCVAL(pReal, eSrcType, ii); dfPixVal = (dfReal < 0) ? pi : 0; GDALCopyWords(&dfPixVal, GDT_Float64, dfPixVal, ((GByte *)pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1); } } } /* ---- Return success ---- */ return CE_None; } /* PhasePixelFunc */
static CPLErr CMulPixelFunc( void **papoSources, int nSources, void *pData, int nXSize, int nYSize, GDALDataType eSrcType, GDALDataType eBufType, int nPixelSpace, int nLineSpace ) { /* ---- Init ---- */ if( nSources != 2 ) return CE_Failure; /* ---- Set pixels ---- */ if( GDALDataTypeIsComplex( eSrcType ) ) { const int nOffset = GDALGetDataTypeSizeBytes( eSrcType ) / 2; const void * const pReal0 = papoSources[0]; const void * const pImag0 = static_cast<GByte *>(papoSources[0]) + nOffset; const void * const pReal1 = papoSources[1]; const void * const pImag1 = static_cast<GByte *>(papoSources[1]) + nOffset; for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { // Source raster pixels may be obtained with SRCVAL macro. const double dfReal0 = SRCVAL(pReal0, eSrcType, ii); const double dfReal1 = SRCVAL(pReal1, eSrcType, ii); const double dfImag0 = SRCVAL(pImag0, eSrcType, ii); const double dfImag1 = SRCVAL(pImag1, eSrcType, ii); const double adfPixVal[2] = { dfReal0 * dfReal1 + dfImag0 * dfImag1, dfReal1 * dfImag0 - dfReal0 * dfImag1 }; GDALCopyWords( adfPixVal, GDT_CFloat64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1 ); } } } else { for( int iLine = 0, ii = 0; iLine < nYSize; ++iLine ) { for( int iCol = 0; iCol < nXSize; ++iCol, ++ii ) { // Source raster pixels may be obtained with SRCVAL macro. // Not complex. const double adfPixVal[2] = { SRCVAL(papoSources[0], eSrcType, ii) * SRCVAL(papoSources[1], eSrcType, ii), 0.0 }; GDALCopyWords( adfPixVal, GDT_CFloat64, 0, static_cast<GByte *>(pData) + nLineSpace * iLine + iCol * nPixelSpace, eBufType, nPixelSpace, 1 ); } } } /* ---- Return success ---- */ return CE_None; } // CMulPixelFunc
CPLErr GDALSimpleSURF::ConvertRGBToLuminosity( GDALRasterBand *red, GDALRasterBand *green, GDALRasterBand *blue, int nXSize, int nYSize, double **padfImg, int nHeight, int nWidth ) { if( red == nullptr || green == nullptr || blue == nullptr ) { CPLError(CE_Failure, CPLE_AppDefined, "Raster bands are not specified"); return CE_Failure; } if( nXSize > red->GetXSize() || nYSize > red->GetYSize() ) { CPLError(CE_Failure, CPLE_AppDefined, "Red band has less size than has been requested"); return CE_Failure; } if( padfImg == nullptr ) { CPLError(CE_Failure, CPLE_AppDefined, "Buffer isn't specified"); return CE_Failure; } const double forRed = 0.21; const double forGreen = 0.72; const double forBlue = 0.07; const GDALDataType eRedType = red->GetRasterDataType(); const GDALDataType eGreenType = green->GetRasterDataType(); const GDALDataType eBlueType = blue->GetRasterDataType(); const int dataRedSize = GDALGetDataTypeSizeBytes(eRedType); const int dataGreenSize = GDALGetDataTypeSizeBytes(eGreenType); const int dataBlueSize = GDALGetDataTypeSizeBytes(eBlueType); void *paRedLayer = CPLMalloc(dataRedSize * nWidth * nHeight); void *paGreenLayer = CPLMalloc(dataGreenSize * nWidth * nHeight); void *paBlueLayer = CPLMalloc(dataBlueSize * nWidth * nHeight); CPLErr eErr = red->RasterIO(GF_Read, 0, 0, nXSize, nYSize, paRedLayer, nWidth, nHeight, eRedType, 0, 0, nullptr); if( eErr == CE_None ) eErr = green->RasterIO(GF_Read, 0, 0, nXSize, nYSize, paGreenLayer, nWidth, nHeight, eGreenType, 0, 0, nullptr); if( eErr == CE_None ) eErr = blue->RasterIO(GF_Read, 0, 0, nXSize, nYSize, paBlueLayer, nWidth, nHeight, eBlueType, 0, 0, nullptr); double maxValue = 255.0; for( int row = 0; row < nHeight && eErr == CE_None; row++ ) for( int col = 0; col < nWidth; col++ ) { // Get RGB values. const double dfRedVal = SRCVAL(paRedLayer, eRedType, nWidth * row + col * dataRedSize); const double dfGreenVal = SRCVAL(paGreenLayer, eGreenType, nWidth * row + col * dataGreenSize); const double dfBlueVal = SRCVAL(paBlueLayer, eBlueType, nWidth * row + col * dataBlueSize); // Compute luminosity value. padfImg[row][col] = ( dfRedVal * forRed + dfGreenVal * forGreen + dfBlueVal * forBlue) / maxValue; } CPLFree(paRedLayer); CPLFree(paGreenLayer); CPLFree(paBlueLayer); return eErr; }