Changeset 3760
- Timestamp:
- Apr 22, 2005, 1:56:04 PM (21 years ago)
- Location:
- trunk/psLib/src/image
- Files:
-
- 2 edited
-
psPixels.c (modified) (11 diffs)
-
psPixels.h (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/image/psPixels.c
r3746 r3760 7 7 * @author Robert DeSonia, MHPCC 8 8 * 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 $ 11 11 * 12 12 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 19 19 #include "psMemory.h" 20 20 21 typedef struct22 {23 psS32 x;24 psS32 y;25 }26 p_psPixelCoord;27 28 21 typedef int(*qsortCompareFcn)(const void *, const void *); 29 22 … … 31 24 { 32 25 if (pixels != NULL) { 33 psFree(pixels->x); 34 psFree(pixels->y); 26 psFree(pixels->data); 35 27 } 36 28 } 37 29 38 30 // for use by qsort, etc. 39 static int comparePixelCoord(p _psPixelCoord* coord1, p_psPixelCoord* coord2)31 static int comparePixelCoord(psPixelCoord* coord1, psPixelCoord* coord2) 40 32 { 41 33 // check row first … … 65 57 66 58 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); 69 60 } else { 70 out->x = NULL; 71 out->y = NULL; 72 } 61 out->data = NULL; 62 } 63 out->n = 0; 64 out->nalloc = size; 73 65 74 66 psMemSetDeallocator(out, (psFreeFcn)pixelsFree); … … 83 75 } 84 76 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 } 87 83 88 84 return pixels; 85 } 86 87 psPixels* 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; 89 99 } 90 100 … … 97 107 return NULL; 98 108 } 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) { 112 111 // XXX: Error message 113 112 psFree(out); … … 151 150 152 151 // 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; 160 153 161 154 // cycle through the vector of pixels and insert pixels into image 162 155 psMaskType** outData = out->data.PS_TYPE_MASK_DATA; 163 156 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; 166 159 // pixel in region? 167 160 if (x >= x0 && x < x1 && y >= y0 && y < y1) { … … 195 188 minPixels = 32; 196 189 } 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); 209 194 } 210 195 211 196 // start with a blank list of pixels 212 xVec->n = 0; 213 yVec->n = 0; 197 out->n = 0; 214 198 215 199 // find the mask pixels in the image 216 200 int numPixels = 0; 201 psPixelCoord* data = out->data; 202 int nalloc = out->nalloc; 217 203 for (int row=0; row<numRows; row++) { 218 204 psMaskType* maskRow = mask->data.PS_TYPE_MASK_DATA[row]; … … 220 206 if ( (maskRow[col] | maskVal) != 0 ) { 221 207 // 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; 224 212 } 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; 231 216 numPixels++; 232 217 } … … 234 219 } 235 220 236 // return the vectors to the psPixels struct237 // (n.b., not assuming psVectorRealloc/psVectorRecycle, etc., didn't238 // relocate the vectors)239 out->x = xVec;240 out->y = yVec;241 242 221 return out; 243 222 } … … 249 228 return NULL; 250 229 } 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; 267 257 } 268 258 } 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 7 7 * @author Robert DeSonia, MHPCC 8 8 * 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 $ 11 11 * 12 12 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 20 20 /// @addtogroup Image 21 21 /// @{ 22 23 typedef struct 24 { 25 psS32 x; 26 psS32 y; 27 } 28 psPixelCoord; 22 29 23 30 /** list of pixel coordinates … … 33 40 typedef struct 34 41 { 35 psVector *x; ///< x coordinate 36 psVector *y; ///< y coordinate 42 int n; 43 int nalloc; 44 psPixelCoord* data; 37 45 } 38 46 psPixels; … … 54 62 psPixels* pixels, ///< psPixels to resize, or NULL to create new psPixels 55 63 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 */ 73 psPixels* psPixelsCopy( 74 psPixels* out, ///< psPixels struct to recycle, or NULL 75 const psPixels* in ///< psPixels struct to copy 56 76 ); 57 77
Note:
See TracChangeset
for help on using the changeset viewer.
