IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 20, 2011, 3:55:38 PM (14 years ago)
Author:
eugene
Message:

create a pre-alloc version of image smooth to avoid excessive allocs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/imageops/psImageConvolve.c

    r32724 r32725  
    569569// Generate normalised Gaussian vector
    570570#define IMAGE_SMOOTH_GAUSS(TARGET, SIZE, SIGMA, TYPE) \
    571     psVector *TARGET = psVectorAlloc(2 * SIZE + 1, PS_TYPE_##TYPE); /* Gaussian */ \
     571    TARGET = psVectorAlloc(2 * SIZE + 1, PS_TYPE_##TYPE); /* Gaussian */ \
    572572    double sum = 0.0; /* Sum of Gaussian, for normalisation */ \
    573573    double factor = -0.5/PS_SQR(SIGMA); /* Multiplier for exponential */ \
     
    587587    psKernel *kernel = psKernelAlloc(-size, size, -size, size); // Kernel to return
    588588
     589    psVector *gaussNorm = NULL;
    589590    IMAGE_SMOOTH_GAUSS(gaussNorm, size, sigma, F32);
    590591    psF32 *gauss = &gaussNorm->data.F32[size]; // Convenient kernel-like indexing for Gaussian
     
    602603bool psImageSmooth(psImage *image, double  sigma, double  Nsigma)
    603604{
    604     PS_ASSERT_IMAGE_NON_NULL(image, NULL);
     605    PS_ASSERT_IMAGE_NON_NULL(image, false);
    605606
    606607    // relevant terms
     
    612613  case PS_TYPE_##TYPE: { \
    613614        /* generate normalized gaussian */ \
     615        psVector *gaussnorm = NULL; \
    614616        IMAGE_SMOOTH_GAUSS(gaussnorm, Nrange, sigma, TYPE) \
    615617        ps##TYPE *gauss = &gaussnorm->data.TYPE[Nrange]; \
     
    765767}
    766768
    767 // XXX NOTE : work to do : figure out the actual containers needed in there, pre-allocate them
    768 bool psImageSmooth_PreAlloc_F32(psImage *image, double sigma, double Nsigma)
    769 {
    770     PS_ASSERT_IMAGE_NON_NULL(image, NULL);
    771     // assert on data type
     769void psImageSmooth_PreAlloc_DataFree (psImageSmooth_PreAlloc_Data *smdata) {
     770    psFree (smdata->resultX);
     771    psFree (smdata->resultY);
     772    psFree (smdata->kernel);
     773}
     774
     775psImageSmooth_PreAlloc_Data *psImageSmooth_PreAlloc_DataAlloc (psImage *image, double sigma, double Nsigma) {
     776
     777    psImageSmooth_PreAlloc_Data *smdata = psAlloc(sizeof(psImageSmooth_PreAlloc_Data));
     778    psMemSetDeallocator(smdata, (psFreeFunc) psImageSmooth_PreAlloc_DataFree);
     779
     780    if (!image) {
     781        // relevant terms
     782        smdata->Nrange = sigma*Nsigma + 0.5;    // Number of pixels either side for convolution kernel
     783        smdata->Nx = 0;
     784        smdata->Ny = 0;
     785        smdata->kernel = NULL;
     786        smdata->resultX = NULL;
     787        smdata->resultY = NULL;
     788        return smdata;
     789    }
    772790
    773791    // relevant terms
    774     int Nrange = sigma*Nsigma + 0.5;    // Number of pixels either side for convolution kernel
    775     int Nx = image->numCols;            // Number of columns
    776     int Ny = image->numRows;            // Number of rows
    777 
    778     IMAGE_SMOOTH_GAUSS(gaussnorm, Nrange, sigma, F32);
    779     psF32 *gauss = &gaussnorm->data.F32[Nrange];
     792    smdata->Nrange = sigma*Nsigma + 0.5;    // Number of pixels either side for convolution kernel
     793    smdata->Nx = image->numCols;            // Number of columns
     794    smdata->Ny = image->numRows;            // Number of rows
     795
     796    IMAGE_SMOOTH_GAUSS(smdata->kernel, smdata->Nrange, sigma, F32);
    780797       
    781798    // use a temp running buffer for X and Y directions.
    782     psF32 *resultX = (psF32 *) psAlloc(Nx * sizeof(psF32));
    783     memset (resultX, 0, Nx*sizeof(psF32));
    784     psF32 *resultY = (psF32 *) psAlloc(Ny * sizeof(psF32));
    785     memset (resultY, 0, Ny*sizeof(psF32));
    786 
     799    smdata->resultX = psAlloc(smdata->Nx * sizeof(psF32));
     800    memset (smdata->resultX, 0, smdata->Nx*sizeof(psF32));
     801
     802    smdata->resultY = psAlloc(smdata->Ny * sizeof(psF32));
     803    memset (smdata->resultY, 0, smdata->Ny*sizeof(psF32));
     804
     805    return smdata;
     806}
     807
     808// we can use the same DATA structure on multiple images of the same size
     809bool psImageSmooth_PreAlloc_F32(psImage *image, psImageSmooth_PreAlloc_Data *smdata)
     810{
     811    PS_ASSERT_IMAGE_NON_NULL(image, false);
     812    // assert on data type
     813
     814    // relevant terms
     815    int Nrange = smdata->Nrange;    // Number of pixels either side for convolution kernel
     816    int Nx = smdata->Nx;            // Number of columns
     817    int Ny = smdata->Ny;            // Number of rows
     818
     819    psF32 *gauss = &smdata->kernel->data.F32[Nrange];
     820    psF32 *resultX = smdata->resultX;
     821    psF32 *resultY = smdata->resultY;
     822       
    787823    /* Smooth in X direction */
    788824    {
     
    881917        }
    882918    }
    883        
    884     psFree(resultX);
    885     psFree(resultY);
    886     psFree(gaussnorm);
    887 
    888919    return true;
    889920}
     
    9731004
    9741005    // Generate normalized gaussian
     1006    psVector *gaussNorm = NULL;
    9751007    IMAGE_SMOOTH_GAUSS(gaussNorm, size, sigma, F32);
    9761008
     
    10981130
    10991131    // Generate normalized gaussian
     1132    psVector *gaussNorm = NULL;
    11001133    IMAGE_SMOOTH_GAUSS(gaussNorm, size, sigma, F32);
    11011134    const psF32 *gauss = &gaussNorm->data.F32[size]; // Gaussian convolution kernel
     
    13091342
    13101343    // Generate normalized gaussian
     1344    psVector *gaussNorm = NULL;
    13111345    IMAGE_SMOOTH_GAUSS(gaussNorm, size, sigma, F32);
    13121346
     
    14451479
    14461480    /* generate normalized gaussian */
     1481    psVector *gaussnorm = NULL;
    14471482    IMAGE_SMOOTH_GAUSS(gaussnorm, Nrange, sigma, F32);
    14481483    psF32 *gauss = &gaussnorm->data.F32[Nrange];
Note: See TracChangeset for help on using the changeset viewer.