IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 3760


Ignore:
Timestamp:
Apr 22, 2005, 1:56:04 PM (21 years ago)
Author:
desonia
Message:

reformated psPixels according to bug#371.

-rdd

Location:
trunk/psLib/src/image
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/image/psPixels.c

    r3746 r3760  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2005-04-22 00:06:41 $
     9 *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2005-04-22 23:56:04 $
    1111 *
    1212 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    1919#include "psMemory.h"
    2020
    21 typedef struct
    22 {
    23     psS32 x;
    24     psS32 y;
    25 }
    26 p_psPixelCoord;
    27 
    2821typedef int(*qsortCompareFcn)(const void *, const void *);
    2922
     
    3124{
    3225    if (pixels != NULL) {
    33         psFree(pixels->x);
    34         psFree(pixels->y);
     26        psFree(pixels->data);
    3527    }
    3628}
    3729
    3830// for use by qsort, etc.
    39 static int comparePixelCoord(p_psPixelCoord* coord1, p_psPixelCoord* coord2)
     31static int comparePixelCoord(psPixelCoord* coord1, psPixelCoord* coord2)
    4032{
    4133    // check row first
     
    6557
    6658    if (size > 0) {
    67         out->x = psVectorAlloc(size, PS_TYPE_S32);
    68         out->y = psVectorAlloc(size, PS_TYPE_S32);
     59        out->data = psAlloc(sizeof(psPixelCoord)*size);
    6960    } else {
    70         out->x = NULL;
    71         out->y = NULL;
    72     }
     61        out->data = NULL;
     62    }
     63    out->n = 0;
     64    out->nalloc = size;
    7365
    7466    psMemSetDeallocator(out, (psFreeFcn)pixelsFree);
     
    8375    }
    8476
    85     pixels->x = psVectorRealloc(pixels->x, size);
    86     pixels->y = psVectorRealloc(pixels->y, size);
     77    pixels->data = psRealloc(pixels->data, sizeof(psPixelCoord)*size);
     78
     79    pixels->nalloc = size;
     80    if (pixels->n > pixels->nalloc) {
     81        pixels->n = pixels->nalloc;
     82    }
    8783
    8884    return pixels;
     85}
     86
     87psPixels* psPixelsCopy(psPixels* out, const psPixels* in)
     88{
     89    if (in == NULL) {
     90        return NULL;
     91    }
     92
     93    out = psPixelsRealloc(out, in->n);
     94
     95    memcpy(in->data,out->data, in->n*sizeof(psPixelCoord));
     96    out->n = in->n;
     97
     98    return out;
    8999}
    90100
     
    97107        return NULL;
    98108    }
    99     psVector* xVec = pixels->x;
    100     psVector* yVec = pixels->y;
    101     if (xVec == NULL || yVec == NULL) {
    102         // XXX: Error message
    103         psFree(out);
    104         return NULL;
    105     }
    106     if (xVec->type.type != PS_TYPE_S32) {
    107         // XXX: Error message
    108         psFree(out);
    109         return NULL;
    110     }
    111     if (yVec->type.type != PS_TYPE_S32) {
     109    psPixelCoord* data = pixels->data;
     110    if (data == NULL) {
    112111        // XXX: Error message
    113112        psFree(out);
     
    151150
    152151    // determine the length of the pixel vector
    153     int length = pixels->x->n;
    154     if (pixels->y->n != length) {
    155         // XXX: warning message
    156         if (pixels->y->n < length) {
    157             length = pixels->y->n;
    158         }
    159     }
     152    int length = pixels->n;
    160153
    161154    // cycle through the vector of pixels and insert pixels into image
    162155    psMaskType** outData = out->data.PS_TYPE_MASK_DATA;
    163156    for (int p = 0; p < length; p++) {
    164         psS32 x = xVec->data.S32[p];
    165         psS32 y = yVec->data.S32[p];
     157        psS32 x = data[p].x;
     158        psS32 y = data[p].y;
    166159        // pixel in region?
    167160        if (x >= x0 && x < x1 && y >= y0 && y < y1) {
     
    195188        minPixels = 32;
    196189    }
    197     if (out == NULL) {
    198         out = psPixelsAlloc(minPixels);
    199     }
    200     psVector* xVec = out->x;
    201     psVector* yVec = out->y;
    202 
    203     // check the x and y vector validity (type/minimum size)
    204     if (xVec == NULL || xVec->type.type != PS_TYPE_S32 || xVec->nalloc < minPixels) {
    205         xVec = psVectorRecycle(xVec,minPixels,PS_TYPE_S32);
    206     }
    207     if (yVec == NULL || yVec->type.type != PS_TYPE_S32 || yVec->nalloc < minPixels) {
    208         yVec = psVectorRecycle(yVec, minPixels,PS_TYPE_S32);
     190
     191    // check out's validity (allocated/minimum size)
     192    if (out == NULL || out->data == NULL || out->nalloc < minPixels) {
     193        out = psPixelsRealloc(out,minPixels);
    209194    }
    210195
    211196    // start with a blank list of pixels
    212     xVec->n = 0;
    213     yVec->n = 0;
     197    out->n = 0;
    214198
    215199    // find the mask pixels in the image
    216200    int numPixels = 0;
     201    psPixelCoord* data = out->data;
     202    int nalloc = out->nalloc;
    217203    for (int row=0; row<numRows; row++) {
    218204        psMaskType* maskRow = mask->data.PS_TYPE_MASK_DATA[row];
     
    220206            if ( (maskRow[col] | maskVal) != 0 ) {
    221207                // check the vector sizes, and expand if necessary
    222                 if (xVec->nalloc >= numPixels) {
    223                     xVec = psVectorRealloc(xVec, 2*xVec->nalloc);
     208                if (nalloc >= numPixels) {
     209                    out = psPixelsRealloc(out, 2*nalloc);
     210                    nalloc = out->nalloc;
     211                    data = out->data;
    224212                }
    225                 if (yVec->nalloc >= numPixels) {
    226                     yVec = psVectorRealloc(yVec, 2*yVec->nalloc);
    227                 }
    228 
    229                 xVec->data.S32[numPixels] = col;
    230                 yVec->data.S32[numPixels] = row;
     213
     214                data[numPixels].x = col;
     215                data[numPixels].y = row;
    231216                numPixels++;
    232217            }
     
    234219    }
    235220
    236     // return the vectors to the psPixels struct
    237     // (n.b., not assuming psVectorRealloc/psVectorRecycle, etc., didn't
    238     //  relocate the vectors)
    239     out->x = xVec;
    240     out->y = yVec;
    241 
    242221    return out;
    243222}
     
    249228        return NULL;
    250229    }
    251     psVector* xPixels = pixels->x;
    252     psVector* yPixels = pixels->y;
    253 
    254     // verify that pixels has well formed vectors (type)
    255     if (xPixels->type.type != PS_TYPE_S32 ||
    256             yPixels->type.type != PS_TYPE_S32) {
    257         // XXX: Error message
    258         return NULL;
    259     }
    260 
    261     // determine the length of the pixel vector
    262     int pixelsLen = xPixels->n;
    263     if (yPixels->n != pixelsLen) {
    264         // XXX: warning message
    265         if (yPixels->n < pixelsLen) {
    266             pixelsLen = yPixels->n;
     230    int pixelsN = pixels->n;
     231    psPixelCoord* pixelsData = pixels->data;
     232
     233    if (out == NULL) {
     234        // simple copy pixels
     235        out = psPixelsCopy(out,pixels);
     236        return out;
     237    }
     238
     239    // make sure the out is large enough to fit the result
     240    int outN = out->n;
     241    out = psPixelsRealloc(out,outN + pixelsN);
     242    psPixelCoord* outData = out->data;
     243
     244    // sort the OUT array to help in searching for duplicates later
     245    qsort(outData, sizeof(psPixelCoord), outN,
     246          (qsortCompareFcn)comparePixelCoord);
     247
     248    // add non-duplicate values in pixels to out
     249    psPixelCoord pCoord;
     250    int end = outN;
     251    for (int n = 0; n < pixelsN; n++) {
     252        pCoord = pixelsData[n];
     253        if (bsearch(&pCoord, outData, sizeof(psPixelCoord), outN,
     254                    (qsortCompareFcn)comparePixelCoord) == NULL) {
     255            // no match in OUT array of this value
     256            outData[end++] = pCoord;
    267257        }
    268258    }
    269 
    270     if (out == NULL) {
    271         // simple copy of pixels
    272         out = psPixelsAlloc(0); // let psVectorCopy allocate the vector
    273         out->x = psVectorCopy(out->x,pixels->x,PS_TYPE_S32);
    274         out->y = psVectorCopy(out->y,pixels->y,PS_TYPE_S32);
    275 
    276         return out;
    277     }
    278 
    279     // make sure the out vectors are allocated
    280     psVector* xVec = out->x;
    281     psVector* yVec = out->y;
    282     if (xVec == NULL) {
    283         out->x = xVec = psVectorAlloc(pixelsLen,PS_TYPE_S32);
    284         xVec->n = 0;
    285     }
    286     if (yVec == NULL) {
    287         out->y = yVec = psVectorAlloc(pixelsLen,PS_TYPE_S32);
    288         yVec->n = 0;
    289     }
    290 
    291     // verify that out has well formed vectors (type/size)
    292     if (xVec->type.type != PS_TYPE_S32 ||
    293             yVec->type.type != PS_TYPE_S32) {
    294         // XXX: Error message
    295         return NULL;
    296     }
    297     if (xVec->n != yVec->n) {
    298         // XXX: Error message
    299         return NULL;
    300     }
    301     int outLen = xVec->n;
    302 
    303 
    304     // populate an array of psPixelCoord structs with the out values
    305     p_psPixelCoord* coordinates = psAlloc(sizeof(p_psPixelCoord)*(pixelsLen+outLen));
    306     psS32* outXData = xVec->data.S32;
    307     psS32* outYData = yVec->data.S32;
    308     for (int n = 0; n < outLen; n++) {
    309         coordinates[n].x = outXData[n];
    310         coordinates[n].y = outYData[n];
    311     }
    312 
    313     // sort the coordinates array
    314     qsort(coordinates, sizeof(p_psPixelCoord), outLen,
    315           (qsortCompareFcn)comparePixelCoord);
    316 
    317     // search out for coordinates in pixels
    318     int end = outLen;
    319     psS32* pixelsXData = xPixels->data.S32;
    320     psS32* pixelsYData = yPixels->data.S32;
    321     p_psPixelCoord pCoord;
    322     for (int n = 0; n < pixelsLen; n++) {
    323         pCoord.x = pixelsXData[n];
    324         pCoord.y = pixelsYData[n];
    325         if (bsearch(&pCoord, coordinates, sizeof(p_psPixelCoord), outLen,
    326                     (qsortCompareFcn)comparePixelCoord) == NULL) {
    327             coordinates[end++] = pCoord;
    328         }
    329     }
    330 
    331     // transfer the coordinates data back to psPixels
    332     out = psPixelsRealloc(out, end);
    333     outXData = xVec->data.S32;
    334     outYData = yVec->data.S32;
    335     for (int n = 0; n < end; n++) {
    336         outXData[n] = coordinates[n].x;
    337         outYData[n] = coordinates[n].y;
    338     }
    339 
    340     psFree(coordinates);
    341 
    342     return out;
    343 }
     259    out->n = end; // set number of elements to reflect added data
     260
     261    return out;
     262}
  • trunk/psLib/src/image/psPixels.h

    r3746 r3760  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2005-04-22 00:06:41 $
     9 *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2005-04-22 23:56:04 $
    1111 *
    1212 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    2020/// @addtogroup Image
    2121/// @{
     22
     23typedef struct
     24{
     25    psS32 x;
     26    psS32 y;
     27}
     28psPixelCoord;
    2229
    2330/** list of pixel coordinates
     
    3340typedef struct
    3441{
    35     psVector *x;                       ///< x coordinate
    36     psVector *y;                       ///< y coordinate
     42    int n;
     43    int nalloc;
     44    psPixelCoord* data;
    3745}
    3846psPixels;
     
    5462    psPixels* pixels,                  ///< psPixels to resize, or NULL to create new psPixels
    5563    int size                           ///< the size of the coordinate vectors
     64);
     65
     66/** Copies a psPixels object
     67 *
     68 *  Makes a deep copy of the data in a psPixels object.  Any data in the OUT
     69 *  parameter will be destroyed and OUT will be resized, if necessary.
     70 *
     71 *  @return psPixels*   a new psPixels that is a duplicate to IN
     72 */
     73psPixels* psPixelsCopy(
     74    psPixels* out,                     ///< psPixels struct to recycle, or NULL
     75    const psPixels* in                 ///< psPixels struct to copy
    5676);
    5777
Note: See TracChangeset for help on using the changeset viewer.