template <class T> void ossimTiledElevationDatabase::fillGrid(T /* dummyTemplate */, ossimRefPtr<ossimImageData> data) { if ( data.valid() ) { // Copy to grid reversing the lines as the ossimDblGrid's origin is the SW corner. const ossim_float64 NP = data->getNullPix(0); const T* buf = static_cast<T*>(data->getBuf(0)); if ( buf ) { const ossimIpt SIZE( data->getWidth(), data->getHeight() ); ossim_int32 bufIdx = (SIZE.y-1) * data->getWidth(); ossim_int32 grdY = 0; for (ossim_int32 y = SIZE.y-1; y >= 0; --y) { for (ossim_int32 x = 0; x < SIZE.x; ++ x) { ossim_float64 v = static_cast<ossim_float64>(buf[bufIdx+x]); m_grid->setNode(x, grdY, (v!=NP?v:ossim::nan()) ); } bufIdx -= data->getWidth(); ++grdY; } } } }
void ossimEdgeFilter::runLocalMax8Filter(T /* dummyVariable */, ossimRefPtr<ossimImageData> inputData) { ossim_uint32 bandIdx = 0; ossim_uint32 numberOfBands = inputData->getNumberOfBands(); ossim_uint32 x = 0; ossim_uint32 y = 0; ossim_uint32 width = theTile->getWidth(); ossim_uint32 height = theTile->getHeight(); ossim_int32 rowIncrement = inputData->getWidth(); ossim_int32 rowIncrement2 = 2*inputData->getWidth(); for(bandIdx = 0; bandIdx < numberOfBands; ++bandIdx) { //inputBuf has a 1 pixel edge compared to outputBuf T* inputBuf = static_cast<T*>(inputData->getBuf(bandIdx)); T* outputBuf = static_cast<T*>(theTile->getBuf(bandIdx)); T np = static_cast<T>(inputData->getNullPix(bandIdx)); //changed to input Null if(inputBuf&&outputBuf) { //one pass: maybe faster if changed to two passes T* outB; T* inB; outB = outputBuf; inB = inputBuf; for(y = 0; y < height; ++y) { for(x = 0; x < width; ++x) { if (inB[1+rowIncrement] != np) { *outB = max<T>( max<T>( max<T>(inB[0],inB[1]), max<T>(inB[2],inB[rowIncrement])), max<T>( max<T>(inB[rowIncrement+2],inB[rowIncrement2]), max<T>(inB[rowIncrement2+1],inB[rowIncrement2+2]) )); } else { *outB = np; } ++outB; ++inB; } inB+=2; //go to next line, jump due to edge } } } theTile->validate(); }
void ossimFftFilter::runFft(ossimRefPtr<ossimImageData>& input, ossimRefPtr<ossimImageData>& output) { NEWMAT::Matrix* realIn = new NEWMAT::Matrix(input->getHeight(), input->getWidth()); NEWMAT::Matrix* imgIn = new NEWMAT::Matrix(input->getHeight(), input->getWidth()); NEWMAT::Matrix* realOut = new NEWMAT::Matrix(input->getHeight(), input->getWidth()); NEWMAT::Matrix* imgOut = new NEWMAT::Matrix(input->getHeight(), input->getWidth()); ossim_uint32 bandIdx = 0; ossim_uint32 w = input->getWidth(); ossim_uint32 h = input->getHeight(); ossim_uint32 x = 0; ossim_uint32 y = 0; if(theDirectionType == FORWARD) { ossim_uint32 bands = input->getNumberOfBands(); for(bandIdx = 0; bandIdx < bands; ++bandIdx) { ossim_float64* bandReal = 0; ossim_float64* bandImg = 0; fillMatrixForward((ossim_float64*)input->getBuf(bandIdx), (ossim_float64)input->getNullPix(bandIdx), *realIn, *imgIn); NEWMAT::FFT2(*realIn, *imgIn, *realOut, *imgOut); bandReal = (ossim_float64*)output->getBuf(2*bandIdx); bandImg = (ossim_float64*)output->getBuf(2*bandIdx + 1); if(bandReal&&bandImg) { for(y = 0; y < h; ++y) { for(x = 0; x < w; ++x) { *bandReal = (ossim_float64)((*realOut)[y][x]); *bandImg = (ossim_float64)((*imgOut)[y][x]); ++bandReal; ++bandImg; } } } } } else { ossim_float64* bandReal = 0; ossim_uint32 bands = input->getNumberOfBands(); for(bandIdx = 0; bandIdx < bands; bandIdx+=2) { bandReal = (ossim_float64*)output->getBuf(bandIdx/2); if(input->getBuf(bandIdx)&& input->getBuf(bandIdx+1)) { fillMatrixInverse((double*)input->getBuf(bandIdx), (double*)input->getBuf(bandIdx+1), *realIn, *imgIn); NEWMAT::FFT2I(*realIn, *imgIn, *realOut, *imgOut); for(y = 0; y < h; ++y) { for(x = 0; x < w; ++x) { *bandReal = (ossim_float64)((*realOut)[y][x]); // if(*bandReal > 1.0) // { // *bandReal = 1.0; // } // if(*bandReal < 0.0) // { // *bandReal = 0.0; // } ++bandReal; } } } } } delete realIn; delete imgIn; delete realOut; delete imgOut; }
ossimRefPtr<ossimImageData> ossimHistogramEqualization::runEqualizationAlgorithm(T, ossimRefPtr<ossimImageData> tile) { if(!theAccumulationHistogram || !getHistogram()) { return tile; } // for now we will always pull from res 0 information ossimRefPtr<ossimMultiBandHistogram> histo = getHistogram()->getMultiBandHistogram(0); if(histo.valid()) { ossim_uint32 maxBands = ( (histo->getNumberOfBands() > tile->getNumberOfBands())? tile->getNumberOfBands(): histo->getNumberOfBands()); long offsetUpperBound = tile->getHeight()*tile->getWidth(); for(ossim_uint32 band = 0; band < maxBands; ++band) { ossimRefPtr<ossimHistogram> bandHisto = histo->getHistogram(band); T* buf = static_cast<T*>(tile->getBuf(band)); double *histoLut = band<theForwardLut.size()?theForwardLut[band]:NULL; ossim_uint32 actualBand = theBandList[band]; if(bandHisto.valid()) { if(buf&&histoLut&&(actualBand < histo->getNumberOfBands())) { if(theInverseFlag) { histoLut = theInverseLut[actualBand]; } if(histoLut) { if(tile->getDataObjectStatus() == OSSIM_FULL) { T minPix = (T)tile->getMinPix(actualBand); T maxPix = (T)tile->getMaxPix(actualBand); for(long offset = 0; offset < offsetUpperBound; ++offset) { ossim_int32 idx = bandHisto->GetIndex(buf[offset]); if(idx>=0) { T value = (T)(histoLut[idx]); //--- // Assign clamping to min max. // // ESH 03/2009 -- Clamping to within min-max fixed //--- buf[offset] = value < minPix ? minPix : (value > maxPix ? maxPix : value); } } } else { T minPix = (T)tile->getMinPix(actualBand); T maxPix = (T)tile->getMaxPix(actualBand); T nullPix = (T)tile->getNullPix(actualBand); for(long offset = 0; offset < offsetUpperBound; ++offset) { ossim_int32 idx = bandHisto->GetIndex(buf[offset]); if((buf[offset]!=nullPix)&&(idx>=0)) { T value = (T)(histoLut[idx]); //--- // Assign clamping to min max. // // ESH 03/2009 -- Clamping to within min-max fixed //--- buf[offset] = value < minPix ? minPix : (value > maxPix ? maxPix : value); } else { buf[offset] = nullPix; } } } } } } } tile->validate(); } return tile; }
template <class T> void ossimImageToPlaneNormalFilter::computeNormalsTemplate( T /* inputScalarTypeDummy */, ossimRefPtr<ossimImageData>& inputTile, ossimRefPtr<ossimImageData>& outputTile) { T inputNull = (T)inputTile->getNullPix(0); T* inbuf = (T*)inputTile->getBuf(); double* normX = (double*)outputTile->getBuf(0); double* normY = (double*)outputTile->getBuf(1); double* normZ = (double*)outputTile->getBuf(2); ossim_int32 inbuf_width = inputTile->getWidth(); ossim_int32 normbuf_width = outputTile->getWidth(); ossim_int32 normbuf_height = outputTile->getHeight(); ossimColumnVector3d normal; for (ossim_int32 y=0; y<normbuf_height; y++) { // Establish offsets into the image and output normals buffers given row: ossim_uint32 n = y*normbuf_width; ossim_uint32 i = (y+1)*inbuf_width + 1; // Loop to compute the gradient (normal) vector [dh/dx, dh/dy, 1]: for (ossim_int32 x=0; x<normbuf_width; x++) { // Default in case of null inputs is a flat earth: normal[0] = 0; normal[1] = 0; normal[2] = 1.0; // Compute the x-direction differential: if (inbuf[i+1] != inputNull) { if (inbuf[i-1] != inputNull) normal[0] = theXScale*theSmoothnessFactor*(inbuf[i+1] - inbuf[i-1]) / 2.0; else if (inbuf[i] != inputNull) normal[0] = theXScale*theSmoothnessFactor*(inbuf[i+1] - inbuf[i]); } else if ((inbuf[i] != inputNull) && (inbuf[i-1] != inputNull)) { normal[0] = theXScale*theSmoothnessFactor*(inbuf[i] - inbuf[i-1]); } // Compute the y-direction differential: if (inbuf[i+inbuf_width] != inputNull) { if (inbuf[i-inbuf_width] != inputNull) normal[1] = theYScale*theSmoothnessFactor*(inbuf[i+inbuf_width] - inbuf[i-inbuf_width]) / 2.0; else if (inbuf[i] != inputNull) normal[1] = theYScale*theSmoothnessFactor*(inbuf[i+inbuf_width] - inbuf[i]); } else if ((inbuf[i] != inputNull) && (inbuf[i-inbuf_width] != inputNull)) { normal[1] = theYScale*theSmoothnessFactor*(inbuf[i] - inbuf[i-inbuf_width]); } // Stuff the normalized gradient vector into the output buffers: normal = normal.unit(); normX[n] = normal[0]; normY[n] = normal[1]; normZ[n] = normal[2]; ++n; ++i; } } }
template<class T> void ossimCFARFilter::convolvePartial( T, ossimRefPtr<ossimImageData> inputData, ossimRefPtr<ossimImageData> outputData) { // let's set up some temporary variables so we don't // have to call the functions in loops. Iknow that compilers // typically optimize this out but if we are in debug mode // with no optimization it will still run fast // double sum = 0.0,sqrsum = 0.0, variance = 0.0; ossim_int32 inputW = (ossim_int32)inputData->getWidth(); ossim_int32 outputW = (ossim_int32)outputData->getWidth(); ossim_int32 outputH = (ossim_int32)outputData->getHeight(); ossim_int32 numberOfBands = (ossim_int32)inputData->getNumberOfBands(); ossimIpt outputOrigin = outputData->getOrigin(); ossimIpt inputOrigin = inputData->getOrigin(); ossim_int32 startInputOffset = std::abs(outputOrigin.y - inputOrigin.y)* inputW + std::abs(outputOrigin.x - inputOrigin.x); ossim_int32 ulKernelStart = -(2*inputW) - 2; ossim_int32 ul1KernelStart = -inputW - 1; ossim_int32 leftKernelStart = -2; ossim_int32 ll1KernelStart = inputW - 1; ossim_int32 llKernelStart = (2*inputW) - 2; //populate kernel offset indices ossim_int32 KernelStart[BOXSIZE]; T* KernelStartBuf[BOXSIZE]; for(ossim_uint16 i=0;i<BOXSIZE;i++) { int offset = i-(BOXSIZE/2); KernelStart[i] = offset*inputW + offset; KernelStartBuf[i] = NULL; } T* ulKernelStartBuf = NULL; T* ul1KernelStartBuf = NULL; T* leftKernelStartBuf = NULL; T* ll1KernelStartBuf = NULL; T* llKernelStartBuf = NULL; for(ossim_int32 band = 0; band < numberOfBands; ++band) { T* inputBuf = static_cast<T*>(inputData->getBuf(band))+startInputOffset; T* outputBuf = static_cast<T*>(outputData->getBuf(band)); T maxPix = static_cast<T>(getMaxPixelValue(band)); T minPix = static_cast<T>(getMinPixelValue(band)); T nullPix = static_cast<T>(inputData->getNullPix(band)); T oNullPix = static_cast<T>(getNullPixelValue(band)); if(inputBuf&&outputBuf) { for(ossim_int32 row = 0; row < outputW; ++row) { ossim_int32 rowOffset = inputW*row; ulKernelStartBuf = inputBuf + (rowOffset + ulKernelStart); ul1KernelStartBuf = inputBuf + (rowOffset + ul1KernelStart); leftKernelStartBuf = inputBuf + (rowOffset + leftKernelStart); ll1KernelStartBuf = inputBuf + (rowOffset + ll1KernelStart); llKernelStartBuf = inputBuf + (rowOffset + llKernelStart); for(ossim_uint16 i=0;i<BOXSIZE;i++) { KernelStartBuf[i] = inputBuf + (rowOffset + KernelStart[i]); } for(ossim_int32 col = 0; col < outputH; ++col) { //TODO: Need to have an efficient means of detecting nulls if((ulKernelStartBuf[0] != nullPix)&& (ulKernelStartBuf[1] != nullPix)&& (ulKernelStartBuf[2] != nullPix)&& (leftKernelStartBuf[0] != nullPix)&& (leftKernelStartBuf[1] != nullPix)&& (leftKernelStartBuf[2] != nullPix)&& (llKernelStartBuf[0] != nullPix)&& (llKernelStartBuf[1] != nullPix)&& (llKernelStartBuf[2] != nullPix)) { //calculate mean sum = 0.0; sqrsum = 0.0; for(ossim_uint32 r=0; r<5; ++r) sum += theKernel[0][r]*(double)ulKernelStartBuf[r]; for(ossim_uint32 r=0; r<5; ++r) sum += theKernel[1][r]*(double)ul1KernelStartBuf[r]; for(ossim_uint32 r=0; r<5; ++r) sum += theKernel[2][r]*(double)leftKernelStartBuf[r]; for(ossim_uint32 r=0; r<5; ++r) sum += theKernel[3][r]*(double)ll1KernelStartBuf[r]; for(ossim_uint32 r=0; r<5; ++r) sum += theKernel[4][r]*(double)llKernelStartBuf[r]; /* for(ossim_uint16 i=0;i<BOXSIZE;i++) { for(ossim_uint32 r=0; r<5; ++r) { sum += theKernel[i][r]*(double)KernelStartBuf[i][r]; sqrsum += theKernel[i][r]*(double)KernelStartBuf[i][r] *(double)KernelStartBuf[i][r]; } } */ //calculate mean of squares for(ossim_uint32 r=0; r<5; ++r) sqrsum += theKernel[0][r]*(double)ulKernelStartBuf[r] *(double)ulKernelStartBuf[r]; for(ossim_uint32 r=0; r<5; ++r) sqrsum += theKernel[1][r]*(double)ul1KernelStartBuf[r] *(double)ul1KernelStartBuf[r]; for(ossim_uint32 r=0; r<5; ++r) sqrsum += theKernel[2][r]*(double)leftKernelStartBuf[r] *(double)leftKernelStartBuf[r]; for(ossim_uint32 r=0; r<5; ++r) sqrsum += theKernel[3][r]*(double)ll1KernelStartBuf[r] *(double)ll1KernelStartBuf[r]; for(ossim_uint32 r=0; r<5; ++r) sqrsum += theKernel[4][r]*(double)llKernelStartBuf[r] *(double)llKernelStartBuf[r]; //calculate variance variance = sqrsum - (sum*sum); //calculate k-value sum = ((double)leftKernelStartBuf[2] - sum)/sqrt(variance);; //Threshold k-value if(sum < theThreshold) sum = minPix; else sum = maxPix; /* sum = theKernel[0][0]*(double)ulKernelStartBuf[0] + theKernel[0][1]*(double)ulKernelStartBuf[1] + theKernel[0][2]*(double)ulKernelStartBuf[2] + theKernel[1][0]*(double)leftKernelStartBuf[0] + theKernel[1][1]*(double)leftKernelStartBuf[1] + theKernel[1][2]*(double)leftKernelStartBuf[2] + theKernel[2][0]*(double)llKernelStartBuf[0] + theKernel[2][1]*(double)llKernelStartBuf[1] + theKernel[2][2]*(double)llKernelStartBuf[2]; */ if(sum > maxPix) { *outputBuf = maxPix; } else if(sum < minPix) { *outputBuf = minPix; } else { *outputBuf = static_cast<T>(sum); } } else { *outputBuf = oNullPix; } // // Need to implement the convolution here // ++ulKernelStartBuf; ++ul1KernelStartBuf; ++leftKernelStartBuf; ++ll1KernelStartBuf; ++llKernelStartBuf; ++outputBuf; } } } } }
//************************************************************************************************** template <class T> void ossimScaleFilter::runVerticalFilterTemplate( T /* dummy */, const ossimRefPtr<ossimImageData>& input, ossimRefPtr<ossimImageData>& output) { ossimIrect viewRect = output->getImageRectangle(); ossimIrect imageRect = input->getImageRectangle(); ossim_int32 vw = viewRect.width(); ossim_int32 vh = viewRect.height(); ossim_int32 iw = imageRect.width(); ossimIpt origin(viewRect.ul()); ossimIpt imageOrigin(imageRect.ul()); ossimIpt inputUl = m_InputRect.ul(); ossimIpt inputLr = m_InputRect.lr(); double scale = 0.0; double support = 0.0; ossim_int32 x = 0; ossim_int32 y = 0; ossim_int32 start = 0; ossim_int32 stop = 0; ossim_int32 kernelIdx = 0; const ossimFilter* filter = getVerticalFilter(); ossim_float64 center = 0.0; ossim_int32 bandIdx = 0; ossim_int32 numberOfBands = m_Tile->getNumberOfBands(); scale = m_BlurFactor*ossim::max(1.0/m_ScaleFactor.y, 1.0); support=scale*filter->getSupport(); if (support <= 0.5) { support = .5 + FLT_EPSILON; scale = 1.0; } scale=1.0/scale; for(bandIdx = 0; bandIdx < numberOfBands; ++bandIdx) { T* imageBuf = (T*)input->getBuf(bandIdx); T* viewBuf = (T*)output->getBuf(bandIdx); T np = (T)input->getNullPix(bandIdx); T outNp = (T)output->getNullPix(bandIdx); T outMinPix = (T)output->getMinPix(bandIdx); T outMaxPix = (T)output->getMaxPix(bandIdx); for(y = 0; y < vh; ++y) { center=(double) ((y + origin.y+0.5)/m_ScaleFactor.y); start=ossim::max((ossim_int32)ossim::round<int>(center-support), (ossim_int32)inputUl.y); stop=ossim::min((ossim_int32)ossim::round<int>(center+support), (ossim_int32)inputLr.y); ossim_int32 delta = stop-start; if (delta <= 0) { break; } vector<double> kernel(delta); double density = 0.0; for(kernelIdx = 0; kernelIdx < delta; ++kernelIdx) { kernel[kernelIdx] = filter->filter(scale*(start + kernelIdx - center + .5), filter->getSupport()); density += kernel[kernelIdx]; } if ((density != 0.0) && (density != 1.0)) { /* Normalize. */ density=1.0/density; for (kernelIdx=0; kernelIdx < delta; kernelIdx++) kernel[kernelIdx]*=density; } ossim_int32 offset = ((start - imageOrigin.y)*iw); ossim_int32 offsetCenter = ((((ossim_int32)center) - imageOrigin.y)*iw); for(x = 0; x < vw; ++x) { T* yptr = imageBuf + offset + x; T* yCenterptr = imageBuf + offsetCenter + x; double result = 0.0; density = 0.0; if((*yCenterptr) == np) { *viewBuf = outNp; } else { for(kernelIdx = 0; kernelIdx < delta; ++kernelIdx) { if((*yptr != np)&& (kernel[kernelIdx] != 0.0)) { result += ((*yptr)*kernel[kernelIdx]); density += kernel[kernelIdx]; } yptr += iw; } if(density != 0.0) { result /= density; if(result < outMinPix) result = outMinPix; if(result > outMaxPix) result = outMaxPix; *viewBuf = (T)result; } else { *viewBuf = outNp; } } ++viewBuf; } } } }
void ossimTileToIplFilter::CopyIplImageToTile(T dummyVariable, ossimRefPtr<ossimImageData> inputTile, IplImage *output) { // Determine if tile is full or partially filled ossimDataObjectStatus status = inputTile->getDataObjectStatus(); uchar *outputData = (uchar *)output->imageData; int outputStep = output->widthStep/sizeof(uchar); int pixVal; long outputOffset = 0; T maxPix = static_cast<T>(getMaxPixelValue(0)); T minPix = static_cast<T>(getMinPixelValue(0)); T np = static_cast<T>(inputTile->getNullPix(0)); if (status == OSSIM_PARTIAL) { for (ossim_uint32 bandIdx = 0; bandIdx < inputTile->getNumberOfBands(); ++bandIdx) { T* outBuf = static_cast<T*>(inputTile->getBuf(bandIdx)); if (outBuf) { outputOffset = 0; for (long y = 0; y < output->height; ++y) { for (long x = 0; x < output->width; ++x) { if (!inputTile->isNull(outputOffset)) { pixVal = (int)round(outputData[y * outputStep + x]); if (pixVal > maxPix) *outBuf = maxPix; else if (pixVal < 0) *outBuf = minPix; else *outBuf = static_cast<T>(pixVal); } else inputTile->setNull(outputOffset); ++outBuf; ++outputOffset; } } } } } else { for (ossim_uint32 bandIdx = 0; bandIdx < inputTile->getNumberOfBands(); ++bandIdx) { T* outBuf = (T*)(inputTile->getBuf(bandIdx)); if (outBuf) { for (int y = 0; y < output->height; ++y) { for (int x = 0; x < output->width; ++x) { pixVal = (int)round(outputData[y * outputStep + x]); if (pixVal > maxPix) *outBuf = (maxPix); else if (pixVal < 0) *outBuf = (minPix); else *outBuf = static_cast<T>(pixVal); // Increment the output buffer to the next pixel value ++outBuf; } } } else *outBuf = np; } } }