IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 16422


Ignore:
Timestamp:
Feb 13, 2008, 11:46:06 AM (18 years ago)
Author:
Paul Price
Message:

Cleaning up some of the pixels operations.

File:
1 edited

Legend:

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

    r13470 r16422  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.36 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2007-05-22 19:46:46 $
     9 *  @version $Revision: 1.37 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2008-02-13 21:46:06 $
    1111 *
    1212 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    182182    float y1 = region.y1;
    183183
    184     if ( (x0 < 0 || x1 < 0) || (y0 < 0 || y1 < 0) ) {
     184    if (x0 < 0 || x1 < 0 || y0 < 0 || y1 < 0 || x1 < x0 || y1 < y0) {
    185185        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    186186                _("Specified psRegion, [%d:%d,%d:%d], does not specify a valid region."),
    187                 (int)y0,(int)y1,(int)x0,(int)x1);
     187                (int)x0,(int)x1,(int)y0,(int)y1);
    188188        psFree(out);
    189189        return NULL;
    190190    }
    191191
    192     // determine the output image size
    193     int numRows = y1-y0;
    194     int numCols = x1-x0;
    195     numRows += 1;
    196     numCols += 1;
    197     if (numRows < 1 || numCols < 1) {
    198         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    199                 _("Specified psRegion, [%d:%d,%d:%d], does not specify a valid region."),
    200                 (int)y0,(int)y1,(int)x0,(int)x1);
    201         psFree(out);
    202         return NULL;
    203     }
    204 
    205     //  allocate the output image
     192    int numRows = y1 - y0 + 1, numCols = x1 - x0 + 1; // Size of image
     193
    206194    out = psImageRecycle(out, numCols, numRows, PS_TYPE_MASK);
    207     if (out == NULL) {
    208         psError(PS_ERR_UNKNOWN, false,
    209                 _("Failed to create image of size %dx%d."),
    210                 numCols, numRows);
     195    if (!out) {
     196        psError(PS_ERR_UNKNOWN, false, _("Failed to create image of size %dx%d."), numCols, numRows);
    211197        return NULL;
    212198    }
    213199    P_PSIMAGE_SET_COL0(out, (int)x0);
    214200    P_PSIMAGE_SET_ROW0(out, (int)y0);
    215 
    216     // initialize image to all zeros
    217     int columnByteSize = sizeof(psMaskType)*numCols;
    218     for (int row = 0; row < numRows; row++) {
    219         memset(out->data.U8[row],0,columnByteSize);
    220     }
    221 
    222     // determine the length of the pixel vector
    223     long length = pixels->n;
     201    psImageInit(out, 0);
     202
    224203
    225204    // cycle through the vector of pixels and insert pixels into image
     205    long length = pixels->n;            // Length of pixel array
    226206    psMaskType** outData = out->data.PS_TYPE_MASK_DATA;
    227207    for (int p = 0; p < length; p++) {
     
    244224    PS_ASSERT_IMAGE_TYPE(mask, PS_TYPE_MASK, NULL);
    245225
    246     int numRows = mask->numRows;
    247     int numCols = mask->numCols;
    248 
    249     // assumption: number of masked pixels is relatively small compared to
    250     // total pixels, so it is best to just start with a guess and resize if
    251     // necessary
    252     long minPixels = numRows*numCols/100; // initial guess, 1% of pixels masked
    253     if (minPixels < 32) { // enforce a minimum size
    254         minPixels = 32;
    255     }
    256 
    257     // check out's validity (allocated/minimum size)
    258     if (out == NULL || out->data == NULL || out->nalloc < minPixels) {
    259         out = psPixelsRealloc(out,minPixels);
    260     }
    261 
    262     // start with a blank list of pixels
     226    int numRows = mask->numRows, numCols = mask->numCols; // Size of image
     227
     228    // assumption: number of masked pixels is relatively small compared to total pixels, so it is best to just
     229    // start with a guess and resize if necessary
     230    long minPixels = PS_MAX(numRows * numCols / 100, 32); // initial guess: 1% of pixels masked;
     231    if (!out || !out->data || out->nalloc < minPixels) {
     232        out = psPixelsRealloc(out, minPixels);
     233    }
    263234    out->n = 0;
    264235
    265236    // find the mask pixels in the image
    266     long numPixels = 0;
    267     psPixelCoord* data = out->data;
    268     int nalloc = out->nalloc;
    269     for (int row=0; row<numRows; row++) {
    270         psMaskType* maskRow = mask->data.PS_TYPE_MASK_DATA[row];
    271         for (int col=0; col<numCols; col++) {
    272             if ( (maskRow[col] & maskVal) != 0 ) {
    273                 // check the vector sizes, and expand if necessary
    274                 if (nalloc <= numPixels) {
    275                     out = psPixelsRealloc(out, 2*nalloc);
    276                     nalloc = out->nalloc;
    277                     data = out->data;
    278                 }
    279 
    280                 data[numPixels].x = col;
    281                 data[numPixels].y = row;
    282                 numPixels++;
     237    for (int y = 0; y < numRows; y++) {
     238        for (int x = 0; x < numCols; x++) {
     239            if (mask->data.PS_TYPE_MASK_DATA[y][x] & maskVal) {
     240                psPixelsAdd(out, out->nalloc, x, y);
    283241            }
    284242        }
    285243    }
    286     out->n = numPixels;
    287244
    288245    return out;
Note: See TracChangeset for help on using the changeset viewer.