IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 25, 2005, 1:13:43 PM (21 years ago)
Author:
eugene
Message:

updated to use psLib.v8, dropping _EAM code everywhere

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot/src/psLibUtils.c

    r5351 r5593  
    11# include <strings.h>  // for strncasecmp
     2# include <unistd.h>   // for write
    23# include <pslib.h>
    34# include "psLibUtils.h"
    45
    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
    948// we have log levels 1 (Error), 2 (Warning), 3 (Info), 4 (Details), 5 (Minutiae)
    959// 2 = default, -v = 3, -vv = 4, -vvv = 5
     
    15165}
    15266
    153 # if (0)
    154 // alternate implementation of this function from pmObjects.c
    155 // now defined in psLib SDRS as psImageRow
    156 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 # endif
    167 
    168 // XXX EAM : this is now in psLib
    169 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 terms
    176     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 gaussian
    184     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 direction
    191     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 direction
    210     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 image
    225         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 value
    284 // XXX EAM : version in psLib is broken
    285 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 coordinate
    300 // XXX EAM : this is now in psLib
    301 # 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 # endif
    309 
    310 // set actual region based on image parameters:
    311 // compensate for negative upper limits
    312 // XXX this is inconsistent: the coordindates should always be in the parent
    313 //     frame, which means the negative values should subtract from Nx,Ny of
    314 //     the parent, not the child.  but, we don't carry the dimensions of the
    315 //     parent in the psImage container.  for now, use the child Nx,Ny
    316 //     force range to be on this subimage
    317 // XXX EAM : this needs to be changed to use psRegion rather than psRegion*
    318 // XXX EAM : this is now in psLib
    319 # if (0)
    320 psRegion *psRegionForImage (psRegion *out, psImage *image, psRegion *in) {
    321    
    322     // x0,y0, x1,y1 are in *parent* units
    323 
    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 child
    338     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 child
    342     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 # endif
    347 
    348 // mask the area contained by the region
    349 // the region is defined wrt the parent image
    350 // XXX EAM : this is now in psLib
    351 # 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 # endif
    369 
    370 // mask the area not contained by the region
    371 // the region is defined wrt the parent image
    372 // XXX EAM : this is now in psLib
    373 # 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 # endif
    393 
    394 // mask the area contained by the region
    395 // the region is defined wrt the parent image
    396 // XXX EAM : this is now in psLib
    397 # 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 # endif
    419 
    420 // mask the area contained by the region
    421 // the region is defined wrt the parent image
    422 // XXX EAM : this is now in psLib
    423 # 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 # endif
    445 
    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 # endif
    460 
    46167// XXX EAM a utility function
    46268bool p_psVectorPrintRow (int fd, psVector *a, char *name)
    46369{
    46470
    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));
    46875
    46976    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));
    47179    }
    472     fprintf (f, "\n");
    473     fclose(f);
     80    write (fd, "\n", 1);
    47481    return (true);
    47582}
    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.