Changeset 5593 for trunk/psphot/src/psLibUtils.c
- Timestamp:
- Nov 25, 2005, 1:13:43 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/psphot/src/psLibUtils.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psphot/src/psLibUtils.c
r5351 r5593 1 1 # include <strings.h> // for strncasecmp 2 # include <unistd.h> // for write 2 3 # include <pslib.h> 3 4 # include "psLibUtils.h" 4 5 5 // XXX EAM : this is NOT included in the C99 headers ?? 6 FILE *fdopen(int fildes, const char *mode); 7 8 // XXX EAM : these utility functions should be added back into PSLib 9 10 # if (0) // now in psLib (v8) 11 static psHash *timers = NULL; 12 13 // start/restart a named timer 14 bool psTimerStart (char *name) { 15 16 psTime *start; 17 18 if (timers == NULL) timers = psHashAlloc (16); 19 20 start = psTimeGetNow (PS_TIME_UTC); 21 psHashAdd (timers, name, start); 22 psFree (start); 23 return (TRUE); 24 } 25 26 bool psTimerClear (char *name) { 27 28 bool status; 29 30 if (name == NULL) return false; 31 32 status = psHashRemove (timers, name); 33 return (status); 34 } 35 36 // get current elapsed time on named timer 37 psF64 psTimerMark (char *name) { 38 39 psTime *start; 40 psTime *mark; 41 psF64 delta; 42 43 if (timers == NULL) return (0); 44 45 start = psHashLookup (timers, name); 46 if (start == NULL) return (0); 47 48 mark = psTimeGetNow (PS_TIME_UTC); 49 delta = psTimeDelta (mark, start); 50 psFree (mark); 51 // psFree (start); -- XXX is psHashLookup not psMemCopying? 52 53 return (delta); 54 } 55 56 void psTimerFree () { 57 58 psFree (timers); 59 p_psTimeFinalize(); 60 return; 61 } 62 # endif 63 64 # if (0) 65 // find the location of the specified argument 66 int psArgumentGet (int argc, char **argv, char *arg) { 67 68 int i; 69 70 for (i = 0; i < argc; i++) { 71 if (!strcmp(argv[i], arg)) 72 return (i); 73 } 74 75 return ((int)NULL); 76 } 77 78 // remove the specified argument (by location) 79 int psArgumentRemove (int N, int *argc, char **argv) { 80 81 int i; 82 83 if ((N != (int)NULL) && (N != 0)) { 84 (*argc)--; 85 for (i = N; i < *argc; i++) { 86 argv[i] = argv[i+1]; 87 } 88 } 89 90 return (N); 91 } 92 # endif 93 6 // XXX EAM : These two functions (psLogArguments and psTraceArguments) have been 7 // rolled together into a single function in psLib 94 8 // we have log levels 1 (Error), 2 (Warning), 3 (Info), 4 (Details), 5 (Minutiae) 95 9 // 2 = default, -v = 3, -vv = 4, -vvv = 5 … … 151 65 } 152 66 153 # if (0)154 // alternate implementation of this function from pmObjects.c155 // now defined in psLib SDRS as psImageRow156 psVector *psGetRowVectorFromImage(psImage *image,157 psU32 row)158 {159 PS_ASSERT_IMAGE_NON_NULL(image, NULL);160 PS_ASSERT_IMAGE_TYPE(image, PS_TYPE_F32, NULL);161 162 psVector *tmpVector = psVectorAlloc(image->numCols, PS_TYPE_F32);163 memcpy (tmpVector->data.F32, image->data.F32[row], image->numCols*sizeof(psF32));164 return(tmpVector);165 }166 # endif167 168 // XXX EAM : this is now in psLib169 void psImageSmooth_EAM (psImage *image, float sigma, float Nsigma) {170 171 int Nx, Ny, Npixel, Nrange;172 float factor, g, s;173 psVector *temp;174 175 // relevant terms176 Nrange = sigma*Nsigma + 0.5;177 Npixel = 2*Nrange + 1;178 factor = -0.5/(sigma*sigma);179 180 Nx = image->numCols;181 Ny = image->numRows;182 183 // generate gaussian184 psVector *gaussnorm = psVectorAlloc (Npixel, PS_TYPE_F32);185 for (int i = -Nrange; i < Nrange + 1; i++) {186 gaussnorm->data.F32[i+Nrange] = exp (factor*i*i);187 }188 psF32 *gauss = &gaussnorm->data.F32[Nrange];189 190 // smooth in X direction191 temp = psVectorAlloc (Nx, PS_TYPE_F32);192 for (int j = 0; j < Ny; j++) {193 psF32 *vi = image->data.F32[j];194 psF32 *vo = temp->data.F32;195 for (int i = 0; i < Nx; i++) {196 g = s = 0;197 for (int n = -Nrange; n < Nrange + 1; n++) {198 if (i+n < 0) continue;199 if (i+n >= Nx) continue;200 s += gauss[n]*vi[i+n];201 g += gauss[n];202 }203 vo[i] = s / g;204 }205 memcpy (image->data.F32[j], temp->data.F32, Nx*sizeof(psF32));206 }207 psFree (temp);208 209 // smooth in Y direction210 temp = psVectorAlloc (image->numRows, PS_TYPE_F32);211 for (int i = 0; i < Nx; i++) {212 psF32 *vo = temp->data.F32;213 psF32 **vi = image->data.F32;214 for (int j = 0; j < Ny; j++) {215 g = s = 0;216 for (int n = -Nrange; n < Nrange + 1; n++) {217 if (j+n < 0) continue;218 if (j+n >= Ny) continue;219 s += gauss[n]*vi[j+n][i];220 g += gauss[n];221 }222 vo[j] = s / g;223 }224 // replace temp in image225 for (int j = 0; j < Ny; j++) {226 vi[j][i] = vo[j];227 }228 }229 psFree (temp);230 psFree (gaussnorm);231 }232 233 bool psImageInit (psImage *image,...) {234 235 va_list argp;236 psU8 vU8;237 psF32 vF32;238 psF64 vF64;239 240 if (image == NULL) return (false);241 242 va_start (argp, image);243 244 switch (image->type.type) {245 case PS_TYPE_U8:246 vU8 = va_arg (argp, psU32);247 248 for (int iy = 0; iy < image->numRows; iy++) {249 for (int ix = 0; ix < image->numCols; ix++) {250 image->data.U8[iy][ix] = vU8;251 }252 }253 break;254 255 case PS_TYPE_F32:256 vF32 = va_arg (argp, psF64);257 258 for (int iy = 0; iy < image->numRows; iy++) {259 for (int ix = 0; ix < image->numCols; ix++) {260 image->data.F32[iy][ix] = vF32;261 }262 }263 return (true);264 265 case PS_TYPE_F64:266 vF64 = va_arg (argp, psF64);267 268 for (int iy = 0; iy < image->numRows; iy++) {269 for (int ix = 0; ix < image->numCols; ix++) {270 image->data.F64[iy][ix] = vF64;271 }272 }273 return (true);274 275 default:276 psAbort ("psphot.psUtils", "datatype %d not defined in psImageInit\n", image->type);277 return (false);278 }279 return (false);280 }281 282 // now in psLib (v8)283 // count number of pixels with given mask value284 // XXX EAM : version in psLib is broken285 int psImageCountPixelMask_EAM (psImage *mask, psU8 value)286 {287 int Npixels = 0;288 289 for (int i = 0; i < mask->numRows; i++) {290 for (int j = 0; j < mask->numCols; j++) {291 if (mask->data.U8[i][j] & value) {292 Npixels ++;293 }294 }295 }296 return (Npixels);297 }298 299 // define a square region centered on the given coordinate300 // XXX EAM : this is now in psLib301 # if (0)302 psRegion *psRegionSquare (psF32 x, psF32 y, psF32 radius) {303 psRegion *region;304 region = psRegionAlloc (x - radius, x + radius + 1,305 y - radius, y + radius + 1);306 return (region);307 }308 # endif309 310 // set actual region based on image parameters:311 // compensate for negative upper limits312 // XXX this is inconsistent: the coordindates should always be in the parent313 // frame, which means the negative values should subtract from Nx,Ny of314 // the parent, not the child. but, we don't carry the dimensions of the315 // parent in the psImage container. for now, use the child Nx,Ny316 // force range to be on this subimage317 // XXX EAM : this needs to be changed to use psRegion rather than psRegion*318 // XXX EAM : this is now in psLib319 # if (0)320 psRegion *psRegionForImage (psRegion *out, psImage *image, psRegion *in) {321 322 // x0,y0, x1,y1 are in *parent* units323 324 if (out == NULL) {325 out = psRegionAlloc(in->x0, in->x1, in->y0, in->y1);326 } else {327 *out = *in;328 }329 // XXX these are probably wrong (see above)330 if (out->x1 <= 0) {331 out->x1 = image->col0 + image->numCols + out->x1;332 }333 if (out->y1 <= 0) {334 out->y1 = image->row0 + image->numRows + out->y1;335 }336 337 // force the lower-limits to be on the child338 out->x0 = PS_MAX(image->col0, out->x0);339 out->y0 = PS_MAX(image->row0, out->y0);340 341 // force the upper-limits to be on the child342 out->x1 = PS_MIN(image->col0 + image->numCols, out->x1);343 out->y1 = PS_MIN(image->row0 + image->numRows, out->y1);344 return (out);345 }346 # endif347 348 // mask the area contained by the region349 // the region is defined wrt the parent image350 // XXX EAM : this is now in psLib351 # if (0)352 void psImageMaskRegion (psImage *image, psRegion *region, bool logical_and, int maskValue) {353 354 for (int iy = 0; iy < image->numRows; iy++) {355 for (int ix = 0; ix < image->numCols; ix++) {356 if (ix + image->col0 < region->x0) continue;357 if (ix + image->col0 >= region->x1) continue;358 if (iy + image->row0 < region->y0) continue;359 if (iy + image->row0 >= region->y1) continue;360 if (logical_and) {361 image->data.U8[iy][ix] &= maskValue;362 } else {363 image->data.U8[iy][ix] |= maskValue;364 }365 }366 }367 }368 # endif369 370 // mask the area not contained by the region371 // the region is defined wrt the parent image372 // XXX EAM : this is now in psLib373 # if (0)374 void psImageKeepRegion (psImage *image, psRegion *region, bool logical_and, int maskValue) {375 376 for (int iy = 0; iy < image->numRows; iy++) {377 for (int ix = 0; ix < image->numCols; ix++) {378 if (ix + image->col0 < region->x0) goto maskit;379 if (ix + image->col0 >= region->x1) goto maskit;380 if (iy + image->row0 < region->y0) goto maskit;381 if (iy + image->row0 >= region->y1) goto maskit;382 continue;383 maskit:384 if (logical_and) {385 image->data.U8[iy][ix] &= maskValue;386 } else {387 image->data.U8[iy][ix] |= maskValue;388 }389 }390 }391 }392 # endif393 394 // mask the area contained by the region395 // the region is defined wrt the parent image396 // XXX EAM : this is now in psLib397 # if (0)398 void psImageMaskCircle (psImage *image, double x, double y, double radius, bool logical_and, int maskValue) {399 400 double dx, dy, r2, R2;401 402 R2 = PS_SQR(radius);403 404 for (int iy = 0; iy < image->numRows; iy++) {405 for (int ix = 0; ix < image->numCols; ix++) {406 dx = ix + image->col0 - x;407 dy = iy + image->row0 - y;408 r2 = PS_SQR(dx) + PS_SQR(dy);409 if (r2 > R2) continue;410 if (logical_and) {411 image->data.U8[iy][ix] &= maskValue;412 } else {413 image->data.U8[iy][ix] |= maskValue;414 }415 }416 }417 }418 # endif419 420 // mask the area contained by the region421 // the region is defined wrt the parent image422 // XXX EAM : this is now in psLib423 # if (0)424 void psImageKeepCircle (psImage *image, double x, double y, double radius, bool logical_and, int maskValue) {425 426 double dx, dy, r2, R2;427 428 R2 = PS_SQR(radius);429 430 for (int iy = 0; iy < image->numRows; iy++) {431 for (int ix = 0; ix < image->numCols; ix++) {432 dx = ix + image->col0 - x;433 dy = iy + image->row0 - y;434 r2 = PS_SQR(dx) + PS_SQR(dy);435 if (r2 < R2) continue;436 if (logical_and) {437 image->data.U8[iy][ix] &= maskValue;438 } else {439 image->data.U8[iy][ix] |= maskValue;440 }441 }442 }443 }444 # endif445 446 # if (0)447 psVector *psVectorCreate (double lower, double upper, double delta, psElemType type) {448 449 int nBin = (upper - lower) / delta;450 451 psVector *out = psVectorAlloc (nBin, type);452 453 for (int i = 0; i < nBin; i++) {454 out->data.F64[i] = lower + i * delta;455 }456 457 return (out);458 }459 # endif460 461 67 // XXX EAM a utility function 462 68 bool p_psVectorPrintRow (int fd, psVector *a, char *name) 463 69 { 464 70 465 FILE *f; 466 f = fdopen(fd, "a+"); 467 fprintf (f, "vector: %s\n", name); 71 char line[1024]; 72 73 sprintf (line, "vector: %s\n", name); 74 write (fd, line, strlen(line)); 468 75 469 76 for (int i = 0; i < a[0].n; i++) { 470 fprintf (f, "%f ", p_psVectorGetElementF64(a, i)); 77 sprintf (line, "%f ", p_psVectorGetElementF64(a, i)); 78 write (fd, line, strlen(line)); 471 79 } 472 fprintf (f, "\n"); 473 fclose(f); 80 write (fd, "\n", 1); 474 81 return (true); 475 82 } 476 // XXX EAM is the use of fdopen here a good way to do this?
Note:
See TracChangeset
for help on using the changeset viewer.
