IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 1407 for trunk/psLib/src/image


Ignore:
Timestamp:
Aug 6, 2004, 2:06:06 PM (22 years ago)
Author:
desonia
Message:

another attempt to get astyle to get it right.

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

Legend:

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

    r1406 r1407  
     1
    12/** @file  psImage.c
    23 *
     
    910 *  @author Ross Harman, MHPCC
    1011 *
    11  *  @version $Revision: 1.37 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-08-06 22:34:05 $
     12 *  @version $Revision: 1.38 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-08-07 00:06:06 $
    1314 *
    1415 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1819
    1920/******************************************************************************/
     21
    2022/*  INCLUDE FILES                                                             */
     23
    2124/******************************************************************************/
    2225
     
    2932#include "psImage.h"
    3033
    31 static void imageFree(psImage* image);
     34static void imageFree(psImage * image);
    3235
    3336/*****************************************************************************/
     37
    3438/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
     39
    3540/*****************************************************************************/
    3641
    37 psImage *psImageAlloc(unsigned int numCols, unsigned int numRows,
    38                       const psElemType type)
     42psImage *psImageAlloc(unsigned int numCols, unsigned int numRows, const psElemType type)
    3943{
    4044    int area = 0;
    41     int elementSize = PSELEMTYPE_SIZEOF(type); // element size in bytes
    42     int rowSize = numCols*elementSize;  // row size in bytes.
    43 
    44     area = numCols*numRows;
     45    int elementSize = PSELEMTYPE_SIZEOF(type);  // element
     46
     47    // size in
     48    // bytes
     49    int rowSize = numCols * elementSize;        // row
     50
     51    // size
     52
     53    // in bytes.
     54
     55    area = numCols * numRows;
    4556
    4657    if (area < 1) {
    47         psError(__func__, "Invalid value for number of rows or columns "
    48                 "(numRows=%d, numCols=%d).", numRows, numCols);
     58        psError(__func__,
     59                "Invalid value for number of rows or columns " "(numRows=%d, numCols=%d).", numRows, numCols);
    4960        return NULL;
    5061    }
    5162
    52     psImage *image = (psImage *)psAlloc(sizeof(psImage));
    53     if(image == NULL) {
    54         psAbort(__func__," : Line %d - Failed to allocate memory", __LINE__);
    55     }
    56 
    57     p_psMemSetDeallocator(image,(psFreeFcn)imageFree);
    58 
    59     image->data.V = psAlloc(sizeof(void*)*numRows);
    60     if(image->data.V == NULL) {
    61         psAbort(__func__," : Line %d - Failed to allocate memory", __LINE__);
    62     }
    63 
    64 
    65     image->data.V[0] = psAlloc(area*elementSize);
    66     if(image->data.V[0] == NULL) {
    67         psAbort(__func__," : Line %d - Failed to allocate memory", __LINE__);
    68     }
    69 
    70     for(int i = 1; i < numRows; i++) {
    71         image->data.V[i] = (void*)((int8_t*)image->data.V[i-1]+rowSize);
    72     }
    73 
    74     *(int*)&image->col0 = 0;
    75     *(int*)&image->row0 = 0;
    76     *(unsigned int*)&image->numCols = numCols;
    77     *(unsigned int*)&image->numRows = numRows;
    78     *(psDimen*)&image->type.dimen = PS_DIMEN_IMAGE;
    79     *(psElemType*)&image->type.type = type;
     63    psImage *image = (psImage *) psAlloc(sizeof(psImage));
     64
     65    if (image == NULL) {
     66        psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);
     67    }
     68
     69    p_psMemSetDeallocator(image, (psFreeFcn) imageFree);
     70
     71    image->data.V = psAlloc(sizeof(void *) * numRows);
     72    if (image->data.V == NULL) {
     73        psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);
     74    }
     75
     76    image->data.V[0] = psAlloc(area * elementSize);
     77    if (image->data.V[0] == NULL) {
     78        psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);
     79    }
     80
     81    for (int i = 1; i < numRows; i++) {
     82        image->data.V[i] = (void *)((int8_t *) image->data.V[i - 1] + rowSize);
     83    }
     84
     85    *(int *)&image->col0 = 0;
     86    *(int *)&image->row0 = 0;
     87    *(unsigned int *)&image->numCols = numCols;
     88    *(unsigned int *)&image->numRows = numRows;
     89    *(psDimen *) & image->type.dimen = PS_DIMEN_IMAGE;
     90    *(psElemType *) & image->type.type = type;
    8091    image->parent = NULL;
    8192    image->nChildren = 0;
     
    8596}
    8697
    87 static void imageFree(psImage* image)
     98static void imageFree(psImage * image)
    8899{
    89100    if (image == NULL) {
     
    92103
    93104    if (image->type.type == PS_TYPE_PTR) {
    94         // 2-D array of pointers -- must dereference
     105        // 2-D array of pointers -- must
     106        // dereference
    95107        unsigned int oldNumRows = image->numRows;
    96108        unsigned int oldNumCols = image->numCols;
    97         psPTR* rowPtr;
    98 
    99         for(unsigned int row=0;row<oldNumRows;row++) {
     109        psPTR *rowPtr;
     110
     111        for (unsigned int row = 0; row < oldNumRows; row++) {
    100112            rowPtr = image->data.PTR[row];
    101             for (unsigned int col=0;col<oldNumCols;col++) {
     113            for (unsigned int col = 0; col < oldNumCols; col++) {
    102114                psMemDecrRefCounter(rowPtr[col]);
    103115            }
     
    112124}
    113125
    114 psImage* psImageRecycle(psImage* old,
    115                         unsigned int numCols,
    116                         unsigned int numRows,
    117                         const psElemType type)
     126psImage *psImageRecycle(psImage * old, unsigned int numCols, unsigned int numRows, const psElemType type)
    118127{
    119     int elementSize = PSELEMTYPE_SIZEOF(type); // element size in bytes
    120     int rowSize = numCols*elementSize;  // row size in bytes.
     128    int elementSize = PSELEMTYPE_SIZEOF(type);  // element
     129
     130    // size in
     131    // bytes
     132    int rowSize = numCols * elementSize;        // row
     133
     134    // size
     135
     136    // in bytes.
    121137
    122138    if (old == NULL) {
    123         old = psImageAlloc(numCols,numRows,type);
     139        old = psImageAlloc(numCols, numRows, type);
    124140        return old;
    125141    }
    126142
    127143    if (old->type.dimen != PS_DIMEN_IMAGE) {
    128         psError(__func__,"Can not realloc image because input is not an image.");
     144        psError(__func__, "Can not realloc image because input is not an image.");
    129145        return NULL;
    130146    }
    131147
    132148    if (old->type.type == PS_TYPE_PTR) {
    133         // 2-D array of pointers -- must dereference
     149        // 2-D array of pointers -- must
     150        // dereference
    134151        unsigned int oldNumRows = old->numRows;
    135152        unsigned int oldNumCols = old->numCols;
    136         psPTR* rowPtr;
    137 
    138         for(unsigned int row=0;row<oldNumRows;row++) {
     153        psPTR *rowPtr;
     154
     155        for (unsigned int row = 0; row < oldNumRows; row++) {
    139156            rowPtr = old->data.PTR[row];
    140             for (unsigned int col=0;col<oldNumCols;col++) {
     157            for (unsigned int col = 0; col < oldNumCols; col++) {
    141158                psMemDecrRefCounter(rowPtr[col]);
    142159                rowPtr[col] = NULL;
     
    146163
    147164    /* image already the right size/type? */
    148     if (numCols == old->numCols && numRows == old->numRows &&
    149             type == old->type.type) {
     165    if (numCols == old->numCols && numRows == old->numRows && type == old->type.type) {
    150166        return old;
    151167    }
    152 
    153168    // Resize the image buffer
    154     old->data.V[0] = psRealloc(old->data.V[0],numCols * numRows * elementSize);
    155     old->data.V = (void**) psRealloc(old->data.V,numRows * sizeof(void*));
     169    old->data.V[0] = psRealloc(old->data.V[0], numCols * numRows * elementSize);
     170    old->data.V = (void **)psRealloc(old->data.V, numRows * sizeof(void *));
    156171
    157172    // recreate the row pointers
    158     for(int i = 1; i < numRows; i++) {
    159         old->data.V[i] = (void*)((int8_t*)old->data.V[i-1]+rowSize);
    160     }
    161 
    162     *(unsigned int*)&old->numCols = numCols;
    163     *(unsigned int*)&old->numRows = numRows;
    164     *(psElemType*)&old->type.type = type;
     173    for (int i = 1; i < numRows; i++) {
     174        old->data.V[i] = (void *)((int8_t *) old->data.V[i - 1] + rowSize);
     175    }
     176
     177    *(unsigned int *)&old->numCols = numCols;
     178    *(unsigned int *)&old->numRows = numRows;
     179    *(psElemType *) & old->type.type = type;
    165180
    166181    return old;
    167182}
    168183
    169 int psImageFreeChildren(psImage* image)
     184int psImageFreeChildren(psImage * image)
    170185{
    171186    int i = 0;
     
    181196    children = image->children;
    182197
    183     for(i=0; i<nChildren; i++) {
     198    for (i = 0; i < nChildren; i++) {
    184199        if (children[i] != NULL) {
    185200            numFreed++;
     
    191206    image->nChildren = 0;
    192207    image->children = NULL;
    193 
    194208
    195209    return numFreed;
     
    202216linear interpolation is performed on the image.
    203217 *****************************************************************************/
    204 psF32 psImagePixelInterpolate(
    205     const psImage *input,
    206     float x,
    207     float y,
    208     psF32 unexposedValue,
    209     psImageInterpolateMode mode)
     218psF32 psImagePixelInterpolate(const psImage * input,
     219                              float x, float y, psF32 unexposedValue, psImageInterpolateMode mode)
    210220{
    211221
    212222    if (input == NULL) {
    213         psError(__func__,"Image can not be NULL.");
     223        psError(__func__, "Image can not be NULL.");
    214224        return unexposedValue;
    215225    }
    216 
    217226    #define PSIMAGE_PIXEL_INTERPOLATE_CASE(TYPE) \
    218227case PS_TYPE_##TYPE: \
     
    243252        PSIMAGE_PIXEL_INTERPOLATE_CASE(C64);
    244253    default:
    245         psError(__func__,"Unsupported image datatype (%d)",input->type.type);
     254        psError(__func__, "Unsupported image datatype (%d)", input->type.type);
    246255    }
    247256
     
    303312PSIMAGE_PIXEL_INTERPOLATE_FLAT_COMPLEX(C32)
    304313PSIMAGE_PIXEL_INTERPOLATE_FLAT_COMPLEX(C64)
    305 
    306314#define PSIMAGE_PIXEL_INTERPOLATE_BILINEAR(TYPE) \
    307315inline psF64 p_psImagePixelInterpolateBILINEAR_##TYPE(const psImage *input, \
     
    359367    return(pixel); \
    360368}
    361 
    362369#define PSIMAGE_PIXEL_INTERPOLATE_BILINEAR_COMPLEX(TYPE) \
    363370inline psC64 p_psImagePixelInterpolateBILINEAR_##TYPE(const psImage *input, \
     
    428435PSIMAGE_PIXEL_INTERPOLATE_BILINEAR_COMPLEX(C32)
    429436PSIMAGE_PIXEL_INTERPOLATE_BILINEAR_COMPLEX(C64)
    430 
    431 
  • trunk/psLib/src/image/psImage.h

    r1406 r1407  
     1
    12/** @file  psImage.h
    23 *
     
    1112 *  @author Ross Harman, MHPCC
    1213 *
    13  *  @version $Revision: 1.27 $ $Name: not supported by cvs2svn $
    14  *  @date $Date: 2004-08-06 22:34:05 $
     14 *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2004-08-07 00:06:06 $
    1516 *
    1617 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1718 */
    18 # ifndef PS_IMAGE_H
    19 # define PS_IMAGE_H
     19#ifndef PS_IMAGE_H
     20#    define PS_IMAGE_H
    2021
    21 #include <complex.h>
     22#    include <complex.h>
    2223
    23 #include "psType.h"
     24#    include "psType.h"
    2425
    2526/// @addtogroup Image
     
    3940typedef struct psImage
    4041{
    41     const psType type;                  ///< Image data type and dimension.
    42     const unsigned int numCols;         ///< Number of columns in image
    43     const unsigned int numRows;         ///< Number of rows in image.
    44     const int col0;                     ///< Column position relative to parent.
    45     const int row0;                     ///< Row position relative to parent.
     42    const psType type;          // /< Image data type and dimension.
     43    const unsigned int numCols; // /< Number of columns in image
     44    const unsigned int numRows; // /< Number of rows in image.
     45    const int col0;             // /< Column position relative to parent.
     46    const int row0;             // /< Row position relative to parent.
    4647
    4748    union {
    48         psU8    **U8;                   ///< Unsigned 8-bit integer data.
    49         psU16   **U16;                  ///< Unsigned 16-bit integer data.
    50         psU32   **U32;                  ///< Unsigned 32-bit integer data.
    51         psU64   **U64;                  ///< Unsigned 64-bit integer data.
    52         psS8    **S8;                   ///< Signed 8-bit integer data.
    53         psS16   **S16;                  ///< Signed 16-bit integer data.
    54         psS32   **S32;                  ///< Signed 32-bit integer data.
    55         psS64   **S64;                  ///< Signed 64-bit integer data.
    56         psF32   **F32;                  ///< Single-precision float data.
    57         psF64   **F64;                  ///< Double-precision float data.
    58         psC32   **C32;                  ///< Single-precision complex data.
    59         psC64   **C64;                  ///< Double-precision complex data.
    60         psPTR   **PTR;                  ///< Void pointers.
    61         psPTR    *V;                    ///< Pointer to data.
    62     } data;                             ///< Union for data types.
    63     const struct psImage *parent;       ///< Parent, if a subimage.
    64     int nChildren;                      ///< Number of subimages.
    65     struct psImage** children;          ///< Children of this region.
     49        psU8 **U8;              // /< Unsigned 8-bit integer data.
     50        psU16 **U16;            // /< Unsigned 16-bit integer data.
     51        psU32 **U32;            // /< Unsigned 32-bit integer data.
     52        psU64 **U64;            // /< Unsigned 64-bit integer data.
     53        psS8 **S8;              // /< Signed 8-bit integer data.
     54        psS16 **S16;            // /< Signed 16-bit integer data.
     55        psS32 **S32;            // /< Signed 32-bit integer data.
     56        psS64 **S64;            // /< Signed 64-bit integer data.
     57        psF32 **F32;            // /< Single-precision float data.
     58        psF64 **F64;            // /< Double-precision float data.
     59        psC32 **C32;            // /< Single-precision complex data.
     60        psC64 **C64;            // /< Double-precision complex data.
     61        psPTR **PTR;            // /< Void pointers.
     62        psPTR *V;               // /< Pointer to data.
     63    } data;                     // /< Union for data types.
     64    const struct psImage *parent;       // /< Parent, if a subimage.
     65    int nChildren;              // /< Number of subimages.
     66    struct psImage **children;  // /< Children of this region.
    6667}
    6768psImage;
    6869
    6970/*****************************************************************************/
     71
    7072/* FUNCTION PROTOTYPES                                                       */
     73
    7174/*****************************************************************************/
    72 
    7375
    7476/** Create an image of the specified size and type.
     
    8082 *
    8183 */
    82 psImage *psImageAlloc(
    83     unsigned int numCols,               ///< Number of rows in image.
    84     unsigned int numRows,               ///< Number of columns in image.
    85     const psElemType type               ///< Type of data for image.
    86 );
     84psImage *psImageAlloc(unsigned int numCols,     // /< Number of rows in image.
     85                      unsigned int numRows,     // /< Number of columns in image.
     86                      const psElemType type     // /< Type of data for image.
     87                     );
    8788
    8889/** Resize a given image to the given size/type.
     
    9192 *
    9293 */
    93 psImage* psImageRecycle(
    94     psImage* old,                       ///< the psImage to recycle by resizing image buffer
    95     unsigned int numCols,               ///< the desired number of columns in image
    96     unsigned int numRows,               ///< the desired number of rows in image
    97     const psElemType type               ///< the desired datatype of the image
    98 );
    99 
     94psImage *psImageRecycle(psImage * old,  // /< the psImage to recycle by resizing image buffer
     95                        unsigned int numCols,   // /< the desired number of columns in image
     96                        unsigned int numRows,   // /< the desired number of rows in image
     97                        const psElemType type   // /< the desired datatype of the image
     98                       );
    10099
    101100/** Frees all children of a psImage.
     
    104103 *
    105104 */
    106 int psImageFreeChildren(
    107     psImage* image
    108     /**< psImage in which all children shall be deallocated */
    109 );
     105int psImageFreeChildren(psImage * image
    110106
    111 psF32 psImagePixelInterpolate(
    112     const psImage *input,
    113     float x,
    114     float y,
    115     psF32 unexposedValue,
    116     psImageInterpolateMode mode
    117 );
     107                        /**< psImage in which all children shall be deallocated */
     108                       );
    118109
    119 #define p_psImagePixelInterpolateFcns(TYPE) \
     110psF32 psImagePixelInterpolate(const psImage * input,
     111                              float x, float y, psF32 unexposedValue, psImageInterpolateMode mode);
     112
     113#    define p_psImagePixelInterpolateFcns(TYPE) \
    120114inline psF64 p_psImagePixelInterpolateFLAT_##TYPE( \
    121115        const psImage *input, \
     
    131125                                                     );
    132126
    133 #define p_psImagePixelInterpolateComplexFcns(TYPE) \
     127#    define p_psImagePixelInterpolateComplexFcns(TYPE) \
    134128inline psC64 p_psImagePixelInterpolateFLAT_##TYPE( \
    135129        const psImage *input, \
     
    157151p_psImagePixelInterpolateComplexFcns(C32)
    158152p_psImagePixelInterpolateComplexFcns(C64)
    159 
    160153/// @}
    161 
    162154#endif
  • trunk/psLib/src/image/psImageExtraction.c

    r1406 r1407  
     1
    12/** @file  psImageExtraction.c
    23*
     
    910*  @author Robert DeSonia, MHPCC
    1011*
    11 *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
    12 *  @date $Date: 2004-08-06 22:34:05 $
     12*  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
     13*  @date $Date: 2004-08-07 00:06:06 $
    1314*
    1415*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2223#include "psError.h"
    2324
    24 psImage *psImageSubset( psImage *out, psImage *image, unsigned int numCols,
    25                         unsigned int numRows, unsigned int col0,
    26                         unsigned int row0 )
     25psImage *psImageSubset(psImage * out,
     26                       psImage * image,
     27                       unsigned int numCols, unsigned int numRows, unsigned int col0, unsigned int row0)
    2728{
    28     unsigned int elementSize;           // size of image element in bytes
    29     unsigned int outputRowSize;         // output row size in bytes
    30     unsigned int inputColOffset;        // offset in bytes to first subset pixel in input row
    31 
    32     if ( image == NULL || image->data.V == NULL ) {
    33         psError( __func__, "Can not subset image because input image or its pixel buffer is NULL." );
    34         return NULL;
    35     }
    36 
    37     if ( image->type.dimen != PS_DIMEN_IMAGE ) {
    38         psError( __func__, "Can not subset image because input image is not an image." );
    39         return NULL;
    40     }
    41 
    42     if ( numCols < 1 || numRows < 1 ) {
    43         psError( __func__, "Can not subset image because number of rows or columns are zero (%dx%d).",
    44                  numCols, numRows );
    45         return NULL;
    46     }
    47 
    48     if ( col0 >= image->numCols || row0 >= image->numRows ) {
    49         psError( __func__, "Can not subset image because col0,row0 (%d,%d) is not a valid pixel location.",
    50                  col0, row0 );
     29    unsigned int elementSize;   // size of image
     30
     31    // element in
     32    // bytes
     33    unsigned int outputRowSize; // output row
     34
     35    // size in bytes
     36    unsigned int inputColOffset;        // offset
     37
     38    // in
     39    // bytes
     40    // to
     41    // first
     42    // subset
     43
     44    // pixel in input row
     45
     46    if (image == NULL || image->data.V == NULL) {
     47        psError(__func__, "Can not subset image because input image or its pixel buffer is NULL.");
     48        return NULL;
     49    }
     50
     51    if (image->type.dimen != PS_DIMEN_IMAGE) {
     52        psError(__func__, "Can not subset image because input image is not an image.");
     53        return NULL;
     54    }
     55
     56    if (numCols < 1 || numRows < 1) {
     57        psError(__func__,
     58                "Can not subset image because number of rows or columns are zero (%dx%d).", numCols, numRows);
     59        return NULL;
     60    }
     61
     62    if (col0 >= image->numCols || row0 >= image->numRows) {
     63        psError(__func__,
     64                "Can not subset image because col0,row0 (%d,%d) is not a valid pixel location.", col0, row0);
    5165        return NULL;
    5266    }
    5367
    5468    /* validate subimage size */
    55     if ( col0 + numCols >= image->numCols || row0 + numRows >= image->numRows ) {
    56         psError( __func__, "Can not subset image outside of image boundaries (size=%dx%d, "
    57                  "subset=[%d:%d,%d:%d]).", image->numCols, image->numRows, col0,
    58                  col0 + numCols, row0, row0 + numRows );
    59         return NULL;
    60     }
    61 
    62 
    63     elementSize = PSELEMTYPE_SIZEOF( image->type.type );
    64 
    65     out = psImageRecycle( out, numCols, numRows, image->type.type );
    66 
    67     // set the parent information into the child output image
    68     *( int* ) & out->row0 = row0;
    69     *( int* ) & out->col0 = col0;
    70     *( psImage** ) & out->parent = ( psImage* ) image;
    71 
    72     // add output image as a child of the input image.
     69    if (col0 + numCols >= image->numCols || row0 + numRows >= image->numRows) {
     70        psError(__func__,
     71                "Can not subset image outside of image boundaries (size=%dx%d, "
     72                "subset=[%d:%d,%d:%d]).",
     73                image->numCols, image->numRows, col0, col0 + numCols, row0, row0 + numRows);
     74        return NULL;
     75    }
     76
     77    elementSize = PSELEMTYPE_SIZEOF(image->type.type);
     78
     79    out = psImageRecycle(out, numCols, numRows, image->type.type);
     80
     81    // set the parent information into the child
     82    // output image
     83    *(int *)&out->row0 = row0;
     84    *(int *)&out->col0 = col0;
     85    *(psImage **) & out->parent = (psImage *) image;
     86
     87    // add output image as a child of the input
     88    // image.
    7389    image->nChildren++;
    74     image->children = ( psImage ** ) psRealloc( image->children,
    75                       image->nChildren * sizeof( psImage* ) );
    76     image->children[ image->nChildren - 1 ] = out;
     90    image->children = (psImage **) psRealloc(image->children, image->nChildren * sizeof(psImage *));
     91    image->children[image->nChildren - 1] = out;
    7792
    7893    inputColOffset = elementSize * col0;
    7994    outputRowSize = elementSize * numCols;
    8095
    81     for ( int row = 0; row < numRows; row++ ) {
    82         memcpy( out->data.V[ row ], image->data.U8[ row0 + row ] + inputColOffset,
    83                 outputRowSize );
    84     }
    85 
    86     return ( out );
     96    for (int row = 0; row < numRows; row++) {
     97        memcpy(out->data.V[row], image->data.U8[row0 + row] + inputColOffset, outputRowSize);
     98    }
     99
     100    return (out);
    87101}
    88102
    89 
    90 psImage *psImageCopy( psImage* restrict output, const psImage *input,
    91                       psElemType type )
     103psImage *psImageCopy(psImage * restrict output, const psImage * input, psElemType type)
    92104{
    93105    psElemType inDatatype;
     
    97109    int numCols;
    98110
    99     if ( input == NULL || input->data.V == NULL ) {
    100         psError( __func__, "Can not copy image because input image or its pixel buffer is NULL." );
    101         psFree( output );
    102         return NULL;
    103     }
    104 
    105     if ( input == output ) {
    106         psError( __func__, "Can not copy image because given input and output "
    107                  "parameter reference the same psImage struct." );
    108         psFree( output );
    109         return NULL;
    110     }
    111 
    112     if ( input->type.dimen != PS_DIMEN_IMAGE ) {
    113         psError( __func__, "Can not copy image because input image is not actually an image." );
    114         psFree( output );
     111    if (input == NULL || input->data.V == NULL) {
     112        psError(__func__, "Can not copy image because input image or its pixel buffer is NULL.");
     113        psFree(output);
     114        return NULL;
     115    }
     116
     117    if (input == output) {
     118        psError(__func__,
     119                "Can not copy image because given input and output "
     120                "parameter reference the same psImage struct.");
     121        psFree(output);
     122        return NULL;
     123    }
     124
     125    if (input->type.dimen != PS_DIMEN_IMAGE) {
     126        psError(__func__, "Can not copy image because input image is not actually an image.");
     127        psFree(output);
    115128        return NULL;
    116129    }
     
    120133    numCols = input->numCols;
    121134    elements = numRows * numCols;
    122     elementSize = PSELEMTYPE_SIZEOF( inDatatype );
    123 
    124     if ( inDatatype == PS_TYPE_PTR || type == PS_TYPE_PTR ) {
    125         psError( __func__, "Can not copy image to/from a void* matrix" );
    126         psFree( output );
    127         return NULL;
    128     }
    129 
    130     output = psImageRecycle( output, numCols, numRows, type );
    131 
    132     // cover the trival case of copy of the same datatype.
    133     if ( type == inDatatype ) {
    134         memcpy( output->data.V[ 0 ], input->data.V[ 0 ], elementSize * elements );
     135    elementSize = PSELEMTYPE_SIZEOF(inDatatype);
     136
     137    if (inDatatype == PS_TYPE_PTR || type == PS_TYPE_PTR) {
     138        psError(__func__, "Can not copy image to/from a void* matrix");
     139        psFree(output);
     140        return NULL;
     141    }
     142
     143    output = psImageRecycle(output, numCols, numRows, type);
     144
     145    // cover the trival case of copy of the same
     146    // datatype.
     147    if (type == inDatatype) {
     148        memcpy(output->data.V[0], input->data.V[0], elementSize * elements);
    135149        return output;
    136150    }
    137 
    138151    #define PSIMAGE_ELEMENT_COPY(IN,INTYPE,OUT,OUTTYPE,ELEMENTS) { \
    139152        ps##INTYPE *in = IN->data.INTYPE[0]; \
     
    186199    }
    187200
    188     switch ( type ) {
     201    switch (type) {
    189202    case PS_TYPE_S8:
    190         PSIMAGE_COPY_CASE( output, S8 );
     203        PSIMAGE_COPY_CASE(output, S8);
    191204        break;
    192205    case PS_TYPE_S16:
    193         PSIMAGE_COPY_CASE( output, S16 );
     206        PSIMAGE_COPY_CASE(output, S16);
    194207        break;
    195208    case PS_TYPE_S32:
    196         PSIMAGE_COPY_CASE( output, S32 );
     209        PSIMAGE_COPY_CASE(output, S32);
    197210        break;
    198211    case PS_TYPE_S64:
    199         PSIMAGE_COPY_CASE( output, S64 );
     212        PSIMAGE_COPY_CASE(output, S64);
    200213        break;
    201214    case PS_TYPE_U8:
    202         PSIMAGE_COPY_CASE( output, U8 );
     215        PSIMAGE_COPY_CASE(output, U8);
    203216        break;
    204217    case PS_TYPE_U16:
    205         PSIMAGE_COPY_CASE( output, U16 );
     218        PSIMAGE_COPY_CASE(output, U16);
    206219        break;
    207220    case PS_TYPE_U32:
    208         PSIMAGE_COPY_CASE( output, U32 );
     221        PSIMAGE_COPY_CASE(output, U32);
    209222        break;
    210223    case PS_TYPE_U64:
    211         PSIMAGE_COPY_CASE( output, U64 );
     224        PSIMAGE_COPY_CASE(output, U64);
    212225        break;
    213226    case PS_TYPE_F32:
    214         PSIMAGE_COPY_CASE( output, F32 );
     227        PSIMAGE_COPY_CASE(output, F32);
    215228        break;
    216229    case PS_TYPE_F64:
    217         PSIMAGE_COPY_CASE( output, F64 );
     230        PSIMAGE_COPY_CASE(output, F64);
    218231        break;
    219232    case PS_TYPE_C32:
    220         PSIMAGE_COPY_CASE( output, C32 );
     233        PSIMAGE_COPY_CASE(output, C32);
    221234        break;
    222235    case PS_TYPE_C64:
    223         PSIMAGE_COPY_CASE( output, C64 );
     236        PSIMAGE_COPY_CASE(output, C64);
    224237        break;
    225238    default:
     
    229242}
    230243
    231 psVector* psImageSlice( psVector* out,
    232                         psVector* slicePositions,
    233                         const psImage* restrict in,
    234                         const psImage* restrict mask,
    235                         unsigned int maskVal,
    236                         unsigned int col,
    237                         unsigned int row,
    238                         unsigned int numCols,
    239                         unsigned int numRows,
    240                         psImageCutDirection direction,
    241                         const psStats* stats )
     244psVector *psImageSlice(psVector * out,
     245                       psVector * slicePositions,
     246                       const psImage * restrict in,
     247                       const psImage * restrict mask,
     248                       unsigned int maskVal,
     249                       unsigned int col,
     250                       unsigned int row,
     251                       unsigned int numCols,
     252                       unsigned int numRows, psImageCutDirection direction, const psStats * stats)
    242253{
    243254    double statVal;
    244     psStats* myStats;
     255    psStats *myStats;
    245256    psElemType type;
    246257    int inRows;
    247258    int inCols;
    248259    int delta = 1;
    249     psF64* outData;
    250 
    251     if ( in == NULL || in->data.V == NULL ) {
    252         psError( __func__, "Input image can not be NULL." );
    253         psFree( out );
    254         return NULL;
    255     }
    256 
    257     if ( numRows == 0 || numCols == 0 ) {
    258         psError( __func__, "The specified region contains no data (%dx%d)",
    259                  numCols, numRows );
    260         psFree( out );
     260    psF64 *outData;
     261
     262    if (in == NULL || in->data.V == NULL) {
     263        psError(__func__, "Input image can not be NULL.");
     264        psFree(out);
     265        return NULL;
     266    }
     267
     268    if (numRows == 0 || numCols == 0) {
     269        psError(__func__, "The specified region contains no data (%dx%d)", numCols, numRows);
     270        psFree(out);
    261271        return NULL;
    262272    }
     
    266276    inCols = in->numCols;
    267277
    268     if ( direction == PS_CUT_X_NEG || direction == PS_CUT_Y_NEG ) {
     278    if (direction == PS_CUT_X_NEG || direction == PS_CUT_Y_NEG) {
    269279        delta = -1;
    270280    }
    271 
    272     // if numRows/numCols is negative, invert the problem to give positive
    273     // numRows/numCols (and cut in opposite direction).
    274     if ( numRows < 0 ) {
     281    // if numRows/numCols is negative, invert the
     282    // problem to give positive
     283    // numRows/numCols (and cut in opposite
     284    // direction).
     285    if (numRows < 0) {
    275286        numRows = -numRows;
    276         row -= ( numRows - 1 );
     287        row -= (numRows - 1);
    277288        delta = -delta;
    278289    }
    279290
    280     if ( numCols < 0 ) {
     291    if (numCols < 0) {
    281292        numCols = -numCols;
    282         col -= ( numCols - 1 );
     293        col -= (numCols - 1);
    283294        delta = -delta;
    284295    }
    285296
    286     if ( mask != NULL ) {
    287         if ( inRows != mask->numRows || inCols != mask->numCols ) {
    288             psError( __func__, "The mask and image dimensions did not match (%dx%d vs %dx%d)",
    289                      mask->numCols, mask->numRows, in->numCols, in->numRows );
    290             psFree( out );
    291         }
    292         if ( mask->type.type != PS_TYPE_MASK ) {
    293             psError( __func__, "The mask datatype (%d) must be %s.",
    294                      mask->type.type, PS_TYPE_MASK_NAME );
    295             psFree( out );
    296         }
    297     }
    298 
    299     if ( row >= inRows || col >= inCols ||
    300             col + numCols > in->numCols || row + numRows > in->numRows ) {
    301         psError( __func__, "The specified image region (%d,%d to %d,%d) is outside of image area (0,0 to %d,%d).",
    302                  col, row, col + numCols - 1, row + numRows - 1, in->numCols - 1, in->numRows - 1 );
    303         psFree( out );
    304         return NULL;
    305     }
    306 
    307     // verify that the stats struct specifies a single stats operation
    308     if ( stats == NULL || p_psGetStatValue( stats, &statVal ) == false ) {
    309         psError( __func__, "The stat options didn't specify a single supported statistic type." );
    310         psFree( out );
    311         return NULL;
    312     }
    313 
    314     // since stats input is const, I need to create a 'scratch' stats struct
    315     myStats = psAlloc( sizeof( psStats ) );
     297    if (mask != NULL) {
     298        if (inRows != mask->numRows || inCols != mask->numCols) {
     299            psError(__func__,
     300                    "The mask and image dimensions did not match (%dx%d vs %dx%d)",
     301                    mask->numCols, mask->numRows, in->numCols, in->numRows);
     302            psFree(out);
     303        }
     304        if (mask->type.type != PS_TYPE_MASK) {
     305            psError(__func__, "The mask datatype (%d) must be %s.", mask->type.type, PS_TYPE_MASK_NAME);
     306            psFree(out);
     307        }
     308    }
     309
     310    if (row >= inRows || col >= inCols || col + numCols > in->numCols || row + numRows > in->numRows) {
     311        psError(__func__,
     312                "The specified image region (%d,%d to %d,%d) is outside of image area (0,0 to %d,%d).",
     313                col, row, col + numCols - 1, row + numRows - 1, in->numCols - 1, in->numRows - 1);
     314        psFree(out);
     315        return NULL;
     316    }
     317    // verify that the stats struct specifies a
     318    // single stats operation
     319    if (stats == NULL || p_psGetStatValue(stats, &statVal) == false) {
     320        psError(__func__, "The stat options didn't specify a single supported statistic type.");
     321        psFree(out);
     322        return NULL;
     323    }
     324    // since stats input is const, I need to
     325    // create a 'scratch' stats struct
     326    myStats = psAlloc(sizeof(psStats));
    316327    *myStats = *stats;
    317328
    318 
    319 
    320     if ( direction == PS_CUT_X_POS || direction == PS_CUT_X_NEG ) {
    321         psVector * imgVec = psVectorAlloc( numRows, type );
    322         psVector* maskVec = NULL;
    323         psMaskType* maskData = NULL;
    324         psU32* outPosition = NULL;
    325 
    326         // recycle output to make a proper sized/type output structure
    327         // n.b. type is double as that is the type given for all stats in psStats.
    328         out = psVectorRecycle( out, numCols, PS_TYPE_F64);
     329    if (direction == PS_CUT_X_POS || direction == PS_CUT_X_NEG) {
     330        psVector *imgVec = psVectorAlloc(numRows, type);
     331        psVector *maskVec = NULL;
     332        psMaskType *maskData = NULL;
     333        psU32 *outPosition = NULL;
     334
     335        // recycle output to make a proper
     336        // sized/type output structure
     337        // n.b. type is double as that is the
     338        // type given for all stats in
     339        // psStats.
     340        out = psVectorRecycle(out, numCols, PS_TYPE_F64);
    329341        if (slicePositions != NULL) {
    330             slicePositions = psVectorRecycle(slicePositions,numCols,PS_TYPE_U32);
     342            slicePositions = psVectorRecycle(slicePositions, numCols, PS_TYPE_U32);
    331343            outPosition = slicePositions->data.U32;
    332344        }
    333345        outData = out->data.F64;
    334         if ( delta < 0 ) {
     346        if (delta < 0) {
    335347            outData += numCols - 1;
    336348            if (outPosition != NULL) {
     
    339351        }
    340352
    341         if ( mask != NULL ) {
    342             maskVec = psVectorAlloc( numRows, mask->type.type );
    343         }
    344 
     353        if (mask != NULL) {
     354            maskVec = psVectorAlloc(numRows, mask->type.type);
     355        }
    345356        #define PSIMAGE_CUT_VERTICAL(TYPE) \
    346357    case PS_TYPE_##TYPE: { \
     
    373384        }
    374385
    375         switch ( type ) {
    376             PSIMAGE_CUT_VERTICAL( U8 );
    377             PSIMAGE_CUT_VERTICAL( U16 );
    378             PSIMAGE_CUT_VERTICAL( U32 );
    379             PSIMAGE_CUT_VERTICAL( U64 );
    380             PSIMAGE_CUT_VERTICAL( S8 );
    381             PSIMAGE_CUT_VERTICAL( S16 );
    382             PSIMAGE_CUT_VERTICAL( S32 );
    383             PSIMAGE_CUT_VERTICAL( S64 );
    384             PSIMAGE_CUT_VERTICAL( F32 );
    385             PSIMAGE_CUT_VERTICAL( F64 );
    386             PSIMAGE_CUT_VERTICAL( C32 );
    387             PSIMAGE_CUT_VERTICAL( C64 );
     386        switch (type) {
     387            PSIMAGE_CUT_VERTICAL(U8);
     388            PSIMAGE_CUT_VERTICAL(U16);
     389            PSIMAGE_CUT_VERTICAL(U32);
     390            PSIMAGE_CUT_VERTICAL(U64);
     391            PSIMAGE_CUT_VERTICAL(S8);
     392            PSIMAGE_CUT_VERTICAL(S16);
     393            PSIMAGE_CUT_VERTICAL(S32);
     394            PSIMAGE_CUT_VERTICAL(S64);
     395            PSIMAGE_CUT_VERTICAL(F32);
     396            PSIMAGE_CUT_VERTICAL(F64);
     397            PSIMAGE_CUT_VERTICAL(C32);
     398            PSIMAGE_CUT_VERTICAL(C64);
    388399        default:
    389             psError( __func__, "Unsupported datatype (%d)", type );
    390             psFree( out );
     400            psError(__func__, "Unsupported datatype (%d)", type);
     401            psFree(out);
    391402            out = NULL;
    392403        }
    393         psFree( imgVec );
    394         psFree( maskVec );
    395     } else
    396         if ( direction == PS_CUT_Y_POS || direction == PS_CUT_Y_NEG ) { // Cut in Y direction
    397             psVector * imgVec = NULL;
    398             psVector* maskVec = NULL;
    399             int elementSize = PSELEMTYPE_SIZEOF( type );
    400             psU32* outPosition = NULL;
    401 
    402             // fill in psVectors to fake out the statistics functions.
    403             imgVec = psAlloc( sizeof( psVector ) );
    404             imgVec->type = in->type;
    405             imgVec->n = imgVec->nalloc = numCols;
    406             if ( mask != NULL ) {
    407                 maskVec = psAlloc( sizeof( psVector ) );
    408                 maskVec->type = mask->type;
    409                 maskVec->n = maskVec->nalloc = numCols;
     404        psFree(imgVec);
     405        psFree(maskVec);
     406    } else if (direction == PS_CUT_Y_POS || direction == PS_CUT_Y_NEG) {        // Cut
     407        //
     408        //
     409        //
     410        //
     411        //
     412        //
     413        //
     414        //
     415        //
     416        // in
     417        // Y
     418        // direction
     419        psVector *imgVec = NULL;
     420        psVector *maskVec = NULL;
     421        int elementSize = PSELEMTYPE_SIZEOF(type);
     422        psU32 *outPosition = NULL;
     423
     424        // fill in psVectors to fake out the
     425        // statistics functions.
     426        imgVec = psAlloc(sizeof(psVector));
     427        imgVec->type = in->type;
     428        imgVec->n = imgVec->nalloc = numCols;
     429        if (mask != NULL) {
     430            maskVec = psAlloc(sizeof(psVector));
     431            maskVec->type = mask->type;
     432            maskVec->n = maskVec->nalloc = numCols;
     433        }
     434        // recycle output to make a proper
     435        // sized/type output structure
     436        // n.b. type is double as that is the
     437        // type given for all stats in
     438        // psStats.
     439        out = psVectorRecycle(out, numRows, PS_TYPE_F64);
     440        if (slicePositions != NULL) {
     441            slicePositions = psVectorRecycle(slicePositions, numRows, PS_TYPE_U32);
     442            outPosition = slicePositions->data.U32;
     443        }
     444        outData = out->data.F64;
     445        if (delta < 0) {
     446            outData += numRows - 1;
     447            if (outPosition != NULL) {
     448                outPosition += numRows - 1;
    410449            }
    411 
    412             // recycle output to make a proper sized/type output structure
    413             // n.b. type is double as that is the type given for all stats in psStats.
    414             out = psVectorRecycle( out, numRows, PS_TYPE_F64 );
    415             if (slicePositions != NULL) {
    416                 slicePositions = psVectorRecycle(slicePositions,numRows,PS_TYPE_U32);
    417                 outPosition = slicePositions->data.U32;
     450        }
     451
     452        for (int r = 0; r < numRows; r++) {
     453            // point the vector struct to the
     454            // data to calculate the stats
     455            imgVec->data.V = (void *)(in->data.U8[row + r] + col * elementSize);
     456            if (maskVec != NULL) {
     457                maskVec->data.V = (void *)(mask->data.U8[row + r] + col * sizeof(psMaskType));
    418458            }
    419             outData = out->data.F64;
    420             if ( delta < 0 ) {
    421                 outData += numRows - 1;
    422                 if (outPosition != NULL) {
    423                     outPosition += numRows - 1;
    424                 }
     459            myStats = psVectorStats(myStats, imgVec, maskVec, maskVal);
     460            (void)p_psGetStatValue(myStats, &statVal);  // we
     461            // know
     462            // it
     463            // works
     464            // cause we tested it
     465            // above
     466            *outData = statVal;
     467            if (outPosition != NULL) {
     468                *outPosition = row + r;
     469                outPosition += delta;
     470
    425471            }
    426 
    427             for ( int r = 0;r < numRows;r++ ) {
    428                 // point the vector struct to the data to calculate the stats
    429                 imgVec->data.V = ( void* ) ( in->data.U8[ row + r ] + col * elementSize );
    430                 if ( maskVec != NULL ) {
    431                     maskVec->data.V = ( void* ) ( mask->data.U8[ row + r ] + col * sizeof( psMaskType ) );
    432                 }
    433                 myStats = psVectorStats( myStats, imgVec, maskVec, maskVal );
    434                 ( void ) p_psGetStatValue( myStats, &statVal ); // we know it works cause we tested it above
    435                 *outData = statVal;
    436                 if (outPosition != NULL) {
    437                     *outPosition = row+r;
    438                     outPosition += delta;
    439                     \
    440                 }
    441                 outData += delta;
    442             }
    443             psFree( imgVec );
    444             psFree( maskVec );
    445         } else { // don't know what the direction flag is
    446             psError( __func__, "Invalid direction flag (%d)", direction );
    447             psFree( out );
    448             out = NULL;
    449         }
    450 
    451     psFree( myStats );
     472            outData += delta;
     473        }
     474        psFree(imgVec);
     475        psFree(maskVec);
     476    } else {                               // don't
     477        // know
     478        // what
     479        // the
     480        // direction
     481        // flag
     482        // is
     483        psError(__func__, "Invalid direction flag (%d)", direction);
     484        psFree(out);
     485        out = NULL;
     486    }
     487
     488    psFree(myStats);
    452489
    453490    return out;
    454491}
    455 
  • trunk/psLib/src/image/psImageExtraction.h

    r1404 r1407  
     1
    12/** @file  psImageExtraction.h
    23*
     
    910*  @author Robert DeSonia, MHPCC
    1011*
    11 *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
    12 *  @date $Date: 2004-08-06 21:50:13 $
     12*  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
     13*  @date $Date: 2004-08-07 00:06:06 $
    1314*
    1415*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1617
    1718#ifndef PSIMAGEEXTRACTION_H
    18 #define PSIMAGEEXTRACTION_H
     19#    define PSIMAGEEXTRACTION_H
    1920
    20 #include "psImage.h"
    21 #include "psVector.h"
    22 #include "psStats.h"
     21#    include "psImage.h"
     22#    include "psVector.h"
     23#    include "psStats.h"
    2324
    2425/// @addtogroup Image
     
    4041*
    4142*/
    42 psImage *psImageSubset(
    43     psImage *out,                         ///< Subimage to return, or NULL.
    44     psImage *image,                       ///< Parent image.
    45     unsigned int numCols,                 ///< Subimage width (<= image.nCols - col0).
    46     unsigned int numRows,                 ///< Subimage height (<= image.nRows - row0).
    47     unsigned int col0,                    ///< Subimage col-offset (0 <= col0 < nCol).
    48     unsigned int row0                   ///< Subimage row-offset (0 <= row0 < nCol).
    49 );
     43psImage *psImageSubset(psImage * out,   // /< Subimage to return, or NULL.
     44                       psImage * image, // /< Parent image.
     45                       unsigned int numCols,    // /< Subimage width (<= image.nCols - col0).
     46                       unsigned int numRows,    // /< Subimage height (<= image.nRows - row0).
     47                       unsigned int col0,       // /< Subimage col-offset (0 <= col0 < nCol).
     48                       unsigned int row0        // /< Subimage row-offset (0 <= row0 < nCol).
     49                      );
    5050
    5151/** Makes a copy of a psImage
     
    5555 *
    5656 */
    57 psImage *psImageCopy(
    58     psImage* restrict output,
    59     /**< if not NULL, a psImage that could be recycled.  If it can not be used,
    60      *   it will be freed via psImageFree
    61      */
    62     const psImage *input,
    63     /**< the psImage to copy */
    64     psElemType type
    65     /**< the desired datatype of the returned copy */
    66 );
     57psImage *psImageCopy(psImage * restrict output,
    6758
    68 psVector* psImageSlice(
    69     psVector* out,
    70     psVector* slicePositions,
    71     const psImage* restrict input,
    72     const psImage* restrict mask,
    73     unsigned int maskVal,
    74     unsigned int col,
    75     unsigned int row,
    76     unsigned int numCols,
    77     unsigned int numRows,
    78     psImageCutDirection direction,
    79     const psStats* stats
    80 );
     59                     /**< if not NULL, a psImage that could be recycled.  If it can not be used,
     60                      *   it will be freed via psImageFree
     61                      */
     62                     const psImage * input,
    8163
    82 psVector* psImageCut(
    83     psVector* out,
    84     const psImage* input,
    85     const psImage* restrict mask,
    86     unsigned int maskVal,
    87     float startCol,
    88     float startRow,
    89     float endCol,
    90     float endRow,
    91     float width,
    92     const psStats* stats
    93 );
     64                     /**< the psImage to copy */
     65                     psElemType type
    9466
    95 psVector* psImageRadialCut(
    96     psVector* out,
    97     const psImage* input,
    98     const psImage* restrict mask,
    99     unsigned int maskVal,
    100     float centerCol,
    101     float centerRow,
    102     const psVector* radii,
    103     const psStats* stats
    104 );
     67                     /**< the desired datatype of the returned copy */
     68                    );
     69
     70psVector *psImageSlice(psVector * out,
     71                       psVector * slicePositions,
     72                       const psImage * restrict input,
     73                       const psImage * restrict mask,
     74                       unsigned int maskVal,
     75                       unsigned int col,
     76                       unsigned int row,
     77                       unsigned int numCols,
     78                       unsigned int numRows, psImageCutDirection direction, const psStats * stats);
     79
     80psVector *psImageCut(psVector * out,
     81                     const psImage * input,
     82                     const psImage * restrict mask,
     83                     unsigned int maskVal,
     84                     float startCol,
     85                     float startRow, float endCol, float endRow, float width, const psStats * stats);
     86
     87psVector *psImageRadialCut(psVector * out,
     88                           const psImage * input,
     89                           const psImage * restrict mask,
     90                           unsigned int maskVal,
     91                           float centerCol, float centerRow, const psVector * radii, const psStats * stats);
    10592
    10693/// @}
  • trunk/psLib/src/image/psImageIO.c

    r1406 r1407  
     1
    12/** @file  psImageIO.c
    23 *
     
    78 *  @author Robert DeSonia, MHPCC
    89 *
    9  *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2004-08-06 22:34:05 $
     10 *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-08-07 00:06:06 $
    1112 *
    1213 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2122#include "psMemory.h"
    2223
    23 psImage* psImageReadSection(psImage* output, int col, int row, int numCols,
    24                             int numRows, int z, char* extname, int extnum, char* filename)
     24psImage *psImageReadSection(psImage * output,
     25                            int col,
     26                            int row,
     27                            int numCols, int numRows, int z, char *extname, int extnum, char *filename)
    2528{
    26     fitsfile    *fptr=NULL;                 /*Pointer to the FITS file*/
    27     int         status=0;                   /*CFITSIO file vars*/
    28     int         nAxis=0;
    29     int         anynull=0;
    30     int         bitPix=0;                   /*Pixel type*/
    31     long        nAxes[3];
    32     long        firstPixel[3];              /* lower-left corner of image subset */
    33     long        lastPixel[3];               /* upper-right corner of image subset */
    34     long        increment[3];               /* increment for image subset */
    35     char        fitsErr[80] = "";           /*CFITSIO error message string */
    36     int         hduType = IMAGE_HDU;
    37     int         fitsDatatype = 0;
    38     int         datatype = 0;
     29    fitsfile *fptr = NULL;      /* Pointer to the FITS file */
     30    int status = 0;             /* CFITSIO file vars */
     31    int nAxis = 0;
     32    int anynull = 0;
     33    int bitPix = 0;             /* Pixel type */
     34    long nAxes[3];
     35    long firstPixel[3];         /* lower-left corner of image subset */
     36    long lastPixel[3];          /* upper-right corner of image subset */
     37    long increment[3];          /* increment for image subset */
     38    char fitsErr[80] = "";      /* CFITSIO error message string */
     39    int hduType = IMAGE_HDU;
     40    int fitsDatatype = 0;
     41    int datatype = 0;
    3942
    4043    if (filename == NULL) {
    41         psError(__func__,"Must specify filename; it can not be NULL.");
     44        psError(__func__, "Must specify filename; it can not be NULL.");
    4245        psFree(output);
    4346        return NULL;
     
    4750    (void)fits_open_file(&fptr, filename, READONLY, &status);
    4851    if (fptr == NULL || status != 0) {
    49         fits_get_errstatus(status,fitsErr);
    50         psError(__func__,"Could not open file '%s'. (%s)",
    51                 filename, fitsErr);
     52        fits_get_errstatus(status, fitsErr);
     53        psError(__func__, "Could not open file '%s'. (%s)", filename, fitsErr);
    5254        psFree(output);
    5355        return NULL;
     
    5658    /* find the specified extension */
    5759    if (extname != NULL) {
    58         if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
     60        if (fits_movnam_hdu(fptr, hduType, extname, 0, &status) != 0) {
    5961            fits_get_errstatus(status, fitsErr);
    6062            status = 0;
    6163            (void)fits_close_file(fptr, &status);
    62             psError(__func__,"Could not index to '%s' HDU for file %s. (%s)",
    63                     extname, filename, fitsErr);
     64            psError(__func__, "Could not index to '%s' HDU for file %s. (%s)", extname, filename, fitsErr);
    6465            psFree(output);
    6566            return NULL;
    6667        }
    6768    } else {
    68         if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
     69        if (fits_movabs_hdu(fptr, extnum + 1, &hduType, &status) != 0) {
    6970            fits_get_errstatus(status, fitsErr);
    7071            status = 0;
    7172            (void)fits_close_file(fptr, &status);
    72             psError(__func__,"Could not index to HDU #%d for file %s. (%s)",
    73                     extnum, filename, fitsErr);
     73            psError(__func__, "Could not index to HDU #%d for file %s. (%s)", extnum, filename, fitsErr);
    7474            psFree(output);
    7575            return NULL;
     
    8282        status = 0;
    8383        (void)fits_close_file(fptr, &status);
    84         psError("Could not determine image data type of '%s'. (%s)",
    85                 filename, fitsErr);
     84        psError("Could not determine image data type of '%s'. (%s)", filename, fitsErr);
    8685        psFree(output);
    8786        return NULL;
     
    9392        status = 0;
    9493        (void)fits_close_file(fptr, &status);
    95         psError("Could not determine dimensions of '%s'. (%s)",
    96                 filename,fitsErr);
     94        psError("Could not determine dimensions of '%s'. (%s)", filename, fitsErr);
    9795        psFree(output);
    9896        return NULL;
     
    10098
    10199    /* Validate the number of axis */
    102     if ( (nAxis < 2) || (nAxis > 3) ) {
    103         status=0;
    104         (void)fits_close_file(fptr, &status);
    105         psError("Dimensions of '%s' are not supported (NAXIS=%i).",
    106                 filename, nAxis);
    107         psFree(output);
    108         return NULL;
    109     }
    110 
    111     /* Get the Image size from the FITS file  */
    112     if ( fits_get_img_size(fptr, nAxis, nAxes, &status) != 0) {
     100    if ((nAxis < 2) || (nAxis > 3)) {
     101        status = 0;
     102        (void)fits_close_file(fptr, &status);
     103        psError("Dimensions of '%s' are not supported (NAXIS=%i).", filename, nAxis);
     104        psFree(output);
     105        return NULL;
     106    }
     107
     108    /* Get the Image size from the FITS file */
     109    if (fits_get_img_size(fptr, nAxis, nAxes, &status) != 0) {
    113110        (void)fits_get_errstatus(status, fitsErr);
    114111        status = 0;
    115112        (void)fits_close_file(fptr, &status);
    116         psError("Could not determine image size of '%s'. (%s)",
    117                 filename,fitsErr);
     113        psError("Could not determine image size of '%s'. (%s)", filename, fitsErr);
    118114        psFree(output);
    119115        return NULL;
     
    127123    }
    128124
    129     firstPixel[0] = col+1;
    130     firstPixel[1] = row+1;
    131     firstPixel[2] = z+1;
     125    firstPixel[0] = col + 1;
     126    firstPixel[1] = row + 1;
     127    firstPixel[2] = z + 1;
    132128
    133129    lastPixel[0] = firstPixel[0] + numCols - 1;
    134130    lastPixel[1] = firstPixel[1] + numRows - 1;
    135     lastPixel[2] = z+1;
     131    lastPixel[2] = z + 1;
    136132
    137133    increment[0] = 1;
     
    139135    increment[2] = 1;
    140136
    141     // turn off the BSCALE/BZERO processing in CFITSIO
    142     // (void)fits_set_bscale(fptr, 1.0,0.0,&status);
     137    // turn off the BSCALE/BZERO processing in
     138    // CFITSIO
     139    // (void)fits_set_bscale(fptr,
     140    // 1.0,0.0,&status);
    143141
    144142    switch (bitPix) {
     
    180178        break;
    181179    default:
    182         psError(__func__,"Unsupported bitpix value (%d) in FITS file %s.",
    183                 bitPix,filename);
    184         psFree(output);
    185         return NULL;
    186     }
    187     output = psImageRecycle(output,numCols,numRows,datatype);
    188     if (fits_read_subset(fptr, fitsDatatype, firstPixel, lastPixel, increment,
    189                          NULL, output->data.V[0], &anynull, &status) != 0) {
     180        psError(__func__, "Unsupported bitpix value (%d) in FITS file %s.", bitPix, filename);
     181        psFree(output);
     182        return NULL;
     183    }
     184    output = psImageRecycle(output, numCols, numRows, datatype);
     185    if (fits_read_subset
     186            (fptr, fitsDatatype, firstPixel,
     187             lastPixel, increment, NULL, output->data.V[0], &anynull, &status) != 0) {
    190188        psFree(output);
    191189        (void)fits_get_errstatus(status, fitsErr);
    192190        status = 0;
    193191        (void)fits_close_file(fptr, &status);
    194         psError(__func__,"Failed to read image [%s]",
    195                 filename, fitsErr);
     192        psError(__func__, "Failed to read image [%s]", filename, fitsErr);
    196193        return NULL;
    197194    }
     
    202199}
    203200
    204 
    205 bool psImageWriteSection(psImage* input, int col0,int row0,int z,
    206                          char* extname, int extnum, char* filename)
     201bool psImageWriteSection(psImage * input, int col0, int row0, int z, char *extname, int extnum,
     202                         char *filename)
    207203{
    208     int         numCols = 0;
    209     int         numRows = 0;
    210 
    211     int         status=0;               /* CFITSIO status */
    212     fitsfile    *fptr=NULL;             /* pointer to the FITS file */
    213     long        nAxes[3];               /* Image axis vars */
    214     long        firstPixel[3];          /* First Pixel to read */
    215     long        lastPixel[3];           /* Last Pixel to read */
    216     char        fitsErr[80];            /* FITSIO message string */
    217     int         datatype = 0;           /* the datatype of the image */
    218     int         bitPix = 0;             /* FITS bitPix value */
    219     int         hduType = IMAGE_HDU;    /* the HDU type (image,table, etc.) */
    220     double      bscale = 1.0;
    221     double      bzero = 0.0;
    222     bool        createNewHDU = false;
     204    int numCols = 0;
     205    int numRows = 0;
     206
     207    int status = 0;             /* CFITSIO status */
     208    fitsfile *fptr = NULL;      /* pointer to the FITS file */
     209    long nAxes[3];              /* Image axis vars */
     210    long firstPixel[3];         /* First Pixel to read */
     211    long lastPixel[3];          /* Last Pixel to read */
     212    char fitsErr[80];           /* FITSIO message string */
     213    int datatype = 0;           /* the datatype of the image */
     214    int bitPix = 0;             /* FITS bitPix value */
     215    int hduType = IMAGE_HDU;    /* the HDU type (image,table, etc.) */
     216    double bscale = 1.0;
     217    double bzero = 0.0;
     218    bool createNewHDU = false;
    223219
    224220    /* need a valid image to write */
    225     if(input==NULL) {
    226         psError(__func__, "Can not write %s.  Input psImage is NULL.",
    227                 filename);
     221    if (input == NULL) {
     222        psError(__func__, "Can not write %s.  Input psImage is NULL.", filename);
    228223        return false;
    229224    }
     
    244239    case PS_TYPE_U16:
    245240        bitPix = SHORT_IMG;
    246         bzero = -1.0f*INT16_MIN;
     241        bzero = -1.0f * INT16_MIN;
    247242        datatype = TUSHORT;
    248243        break;
     
    253248    case PS_TYPE_U32:
    254249        bitPix = LONG_IMG;
    255         bzero = -1.0f*INT32_MIN;
     250        bzero = -1.0f * INT32_MIN;
    256251        datatype = TUINT;
    257252        break;
     
    269264        break;
    270265    default:
    271         psError(__func__, "psImage datatype (%d) not supported.  File %s not written.",
    272                 input->type.type,filename);
     266        psError(__func__,
     267                "psImage datatype (%d) not supported.  File %s not written.", input->type.type, filename);
    273268        return false;
    274269    }
    275270
    276271    /* Open the FITS file */
    277     if (access(filename, F_OK) == 0) { // file exists
     272    if (access(filename, F_OK) == 0) {     // file
     273        // exists
    278274        (void)fits_open_file(&fptr, filename, READWRITE, &status);
    279275        if (fptr == NULL || status != 0) {
    280             fits_get_errstatus(status,fitsErr);
    281             psError(__func__,"Could not open file '%s'. FITS error:%s",
    282                     filename, fitsErr);
     276            fits_get_errstatus(status, fitsErr);
     277            psError(__func__, "Could not open file '%s'. FITS error:%s", filename, fitsErr);
    283278            return false;
    284279        }
     
    286281        /* find the specified extension */
    287282        if (extname != NULL) {
    288             if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
     283            if (fits_movnam_hdu(fptr, hduType, extname, 0, &status) != 0) {
    289284                fits_get_errstatus(status, fitsErr);
    290285                status = 0;
    291286                (void)fits_close_file(fptr, &status);
    292                 psError(__func__,"Could not index to '%s' HDU for file %s. (%s)",
    293                         extname, filename, fitsErr);
     287                psError(__func__,
     288                        "Could not index to '%s' HDU for file %s. (%s)", extname, filename, fitsErr);
    294289                return false;
    295290            }
    296291        } else {
    297292            int numHDUs = 0;
    298             fits_get_num_hdus(fptr,&numHDUs,&status);
     293
     294            fits_get_num_hdus(fptr, &numHDUs, &status);
    299295            if (numHDUs < extnum) {
    300296                status = 0;
    301297                (void)fits_close_file(fptr, &status);
    302                 psError(__func__,"extnum (%d) must not exceed number of HDUs (%d) by more than one.",
    303                         extnum, numHDUs);
     298                psError(__func__,
     299                        "extnum (%d) must not exceed number of HDUs (%d) by more than one.", extnum, numHDUs);
    304300                return false;
    305             } else
    306                 if (numHDUs == extnum) {
    307                     createNewHDU = true;
    308                 } else
    309                     if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
    310                         fits_get_errstatus(status, fitsErr);
    311                         status = 0;
    312                         (void)fits_close_file(fptr, &status);
    313                         psError(__func__,"Could not index to HDU #%d for file %s. (%s)",
    314                                 extnum, filename, fitsErr);
    315                         return false;
    316                     }
    317         }
    318 
    319     } else { // file does not exist
    320 
    321         (void)fits_create_file(&fptr,filename,&status);
     301            } else if (numHDUs == extnum) {
     302                createNewHDU = true;
     303            } else if (fits_movabs_hdu(fptr, extnum + 1, &hduType, &status) != 0) {
     304                fits_get_errstatus(status, fitsErr);
     305                status = 0;
     306                (void)fits_close_file(fptr, &status);
     307                psError(__func__, "Could not index to HDU #%d for file %s. (%s)", extnum, filename, fitsErr);
     308                return false;
     309            }
     310        }
     311
     312    } else {                               // file
     313        // does
     314        // not
     315        // exist
     316
     317        (void)fits_create_file(&fptr, filename, &status);
    322318        if (fptr == NULL || status != 0) {
    323             fits_get_errstatus(status,fitsErr);
    324             psError(__func__,"Could not create file '%s'. (%s)",
    325                     filename, fitsErr);
     319            fits_get_errstatus(status, fitsErr);
     320            psError(__func__, "Could not create file '%s'. (%s)", filename, fitsErr);
    326321            return false;
    327322        }
     
    330325
    331326    if (createNewHDU) {
    332         /*  create the mandatory image keywords */
    333         nAxes[0] = col0+numCols;
    334         nAxes[1] = row0+numRows;
    335         nAxes[2] = z+1;
     327        /* create the mandatory image keywords */
     328        nAxes[0] = col0 + numCols;
     329        nAxes[1] = row0 + numRows;
     330        nAxes[2] = z + 1;
    336331        if (fits_create_img(fptr, bitPix, 3, nAxes, &status) != 0) {
    337332            (void)fits_get_errstatus(status, fitsErr);
    338333            status = 0;
    339334            (void)fits_close_file(fptr, &status);
    340             psError(__func__,"Could not create image HDU in FITS file '%s'. %s",
    341                     filename, fitsErr);
     335            psError(__func__, "Could not create image HDU in FITS file '%s'. %s", filename, fitsErr);
    342336            return false;
    343337        }
    344 
    345338        // set the bscale/bzero
    346         fits_write_key_dbl(fptr, "BZERO",bzero,12,"Pixel Value Offset",&status);
    347         fits_write_key_dbl(fptr, "BSCALE",bscale,12,"Pixel Value Scale",&status);
    348         fits_set_bscale(fptr,bscale,bzero,&status);
     339        fits_write_key_dbl(fptr, "BZERO", bzero, 12, "Pixel Value Offset", &status);
     340        fits_write_key_dbl(fptr, "BSCALE", bscale, 12, "Pixel Value Scale", &status);
     341        fits_set_bscale(fptr, bscale, bzero, &status);
    349342
    350343        if (extname != NULL) {
     
    354347                status = 0;
    355348                (void)fits_close_file(fptr, &status);
    356                 psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. (%s)",
    357                         filename, fitsErr);
     349                psError(__func__,
     350                        "Could not create EXTNAME keyword in FITS file '%s'. (%s)", filename, fitsErr);
    358351                return false;
    359352            }
     
    361354    }
    362355
    363     firstPixel[0] = col0+1;
    364     firstPixel[1] = row0+1;
    365     firstPixel[2] = z+1;
     356    firstPixel[0] = col0 + 1;
     357    firstPixel[1] = row0 + 1;
     358    firstPixel[2] = z + 1;
    366359
    367360    lastPixel[0] = firstPixel[0] + numCols - 1;
    368361    lastPixel[1] = firstPixel[1] + numRows - 1;
    369     lastPixel[2] = z+1;
    370 
    371     if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.V[0], &status) != 0) {
     362    lastPixel[2] = z + 1;
     363
     364    if (fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.V[0], &status) != 0) {
    372365        (void)fits_get_errstatus(status, fitsErr);
    373366        status = 0;
    374367        (void)fits_close_file(fptr, &status);
    375         psError(__func__, "Could not write image data to '%s'. (%s)",
    376                 filename, fitsErr);
     368        psError(__func__, "Could not write image data to '%s'. (%s)", filename, fitsErr);
    377369        return false;
    378370    }
  • trunk/psLib/src/image/psImageIO.h

    r1241 r1407  
     1
    12/** @file  psImageIO.h
    23 *
     
    910 *  @author Robert DeSonia, MHPCC
    1011 *
    11  *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-07-19 22:01:19 $
     12 *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-08-07 00:06:06 $
    1314 *
    1415 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1516 */
    16 # ifndef PS_IMAGEIO_H
    17 # define PS_IMAGEIO_H
     17#ifndef PS_IMAGEIO_H
     18#    define PS_IMAGEIO_H
    1819
    19 #include <stdbool.h>
     20#    include <stdbool.h>
    2021
    21 #include "psImage.h"
     22#    include "psImage.h"
    2223
    2324/// @addtogroup ImageIO
     
    2930 *                          signifies that a problem had occured.
    3031 */
    31 psImage* psImageReadSection(
    32     psImage* output,
    33     /**< the output psImage to recycle, or NULL if new psImage desired */
    34     int col0,
    35     /**< the column index of the origin to start reading */
    36     int row0,
    37     /**< the row index of the origin to start reading */
    38     int numCols,
    39     /**< the number of desired columns to read */
    40     int numRows,
    41     /**< the number of desired rows to read */
    42     int z,
    43     /**< the z index to read if file is organized as a 3D image cube. */
    44     char* extname,
    45     /**< the image extension to read (this should match the EXTNAME keyword in
    46      *   the extension If NULL, the extnum parameter is to be used instead
    47      */
    48     int extnum,
    49     /**< the image extension to read (0=PHU, 1=first extension, etc.)  This is
    50      *   only used if extname is NULL
    51      */
    52     char* filename
    53     /**< the filename of the FITS image file to read */
    54 );
     32psImage *psImageReadSection(psImage * output,
     33
     34                            /**< the output psImage to recycle, or NULL if new psImage desired */
     35                            int col0,
     36
     37                            /**< the column index of the origin to start reading */
     38                            int row0,
     39
     40                            /**< the row index of the origin to start reading */
     41                            int numCols,
     42
     43                            /**< the number of desired columns to read */
     44                            int numRows,
     45
     46                            /**< the number of desired rows to read */
     47                            int z,
     48
     49                            /**< the z index to read if file is organized as a 3D image cube. */
     50                            char *extname,
     51
     52                            /**< the image extension to read (this should match the EXTNAME keyword in
     53                             *   the extension If NULL, the extnum parameter is to be used instead
     54                             */
     55                            int extnum,
     56
     57                            /**< the image extension to read (0=PHU, 1=first extension, etc.)  This is
     58                             *   only used if extname is NULL
     59                             */
     60                            char *filename
     61
     62                            /**< the filename of the FITS image file to read */
     63                           );
    5564
    5665/** Read an image or subimage from a FITS file specified by a filename.
     
    5867 *  return bool         TRUE is successful, otherwise FALSE.
    5968 */
    60 bool psImageWriteSection(
    61     psImage* input,
    62     /**< the psImage to write */
    63     int col0,
    64     /**< the column index of the origin to start writing */
    65     int row0,
    66     /**< the row index of the origin to start writing */
    67     int z,
    68     /**< the z index to start writing */
    69     char* extname,
    70     /**< the image extension to write (this should match the EXTNAME keyword in
    71      *   the extension If NULL, the extnum parameter is to be used instead
    72      */
    73     int extnum,
    74     /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
    75      *   only used if extname is NULL.
    76      */
    77     char* filename
    78     /**< the filename of the FITS image file to write */
    79 );
     69bool psImageWriteSection(psImage * input,
     70
     71                         /**< the psImage to write */
     72                         int col0,
     73
     74                         /**< the column index of the origin to start writing */
     75                         int row0,
     76
     77                         /**< the row index of the origin to start writing */
     78                         int z,
     79
     80                         /**< the z index to start writing */
     81                         char *extname,
     82
     83                         /**< the image extension to write (this should match the EXTNAME keyword in
     84                          *   the extension If NULL, the extnum parameter is to be used instead
     85                          */
     86                         int extnum,
     87
     88                         /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
     89                          *   only used if extname is NULL.
     90                          */
     91                         char *filename
     92
     93                         /**< the filename of the FITS image file to write */
     94                        );
    8095
    8196/// @}
  • trunk/psLib/src/image/psImageManip.c

    r1406 r1407  
     1
    12/** @file  psImageManip.c
    23 *
     
    1011 *  @author Ross Harman, MHPCC
    1112 *
    12  *  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-08-06 22:34:05 $
     13 *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
     14 *  @date $Date: 2004-08-07 00:06:06 $
    1415 *
    1516 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1617 */
    17 #include <math.h>                      // for isfinite(), etc.
     18#include <math.h>                          // for
     19// isfinite(),
     20// etc.
    1821#include <stdlib.h>
    1922#include <stdbool.h>
    20 #include <string.h>                    // for memcpy, etc.
     23#include <string.h>                        // for
     24// memcpy,
     25// etc.
    2126
    2227#include "psError.h"
     
    2631#include "psImageExtraction.h"
    2732
    28 int psImageClip(psImage* input, psF64 min, psF64 vmin, psF64 max, psF64 vmax)
     33int psImageClip(psImage * input, psF64 min, psF64 vmin, psF64 max, psF64 vmax)
    2934{
    3035    int numClipped = 0;
     
    3742
    3843    if (max < min) {
    39         psError(__func__,"psImageClip can not be invoked with max < min.");
     44        psError(__func__, "psImageClip can not be invoked with max < min.");
    4045        return 0;
    4146    }
     
    100105        break;
    101106
    102         psImageClipCase(S8,"psS8")
    103         psImageClipCase(S16,"psS16")
    104         psImageClipCase(S32,"psS32")
    105         psImageClipCase(S64,"psS64")
    106         psImageClipCase(U8,"psU8")
    107         psImageClipCase(U16,"psU16")
    108         psImageClipCase(U32,"psU32")
    109         psImageClipCase(U64,"psU64")
    110         psImageClipCase(F32,"psF32")
    111         psImageClipCase(F64,"psF64")
    112         psImageClipCaseComplex(C32,"psC32",cabsf)
    113         psImageClipCaseComplex(C64,"psC64",cabs)
     107        psImageClipCase(S8, "psS8")
     108        psImageClipCase(S16, "psS16")
     109        psImageClipCase(S32, "psS32")
     110        psImageClipCase(S64, "psS64")
     111        psImageClipCase(U8, "psU8")
     112        psImageClipCase(U16, "psU16")
     113        psImageClipCase(U32, "psU32")
     114        psImageClipCase(U64, "psU64")
     115        psImageClipCase(F32, "psF32")
     116        psImageClipCase(F64, "psF64")
     117        psImageClipCaseComplex(C32, "psC32", cabsf)
     118        psImageClipCaseComplex(C64, "psC64", cabs)
    114119
    115120    default:
    116         psError(__func__,"psImageClip does not support the given datatype (%d)",
    117                 input->type.type);
     121        psError(__func__, "psImageClip does not support the given datatype (%d)", input->type.type);
    118122    }
    119123
     
    121125}
    122126
    123 int psImageClipNaN(psImage* input,psF64 value)
     127int psImageClipNaN(psImage * input, psF64 value)
    124128{
    125129    int numClipped = 0;
     
    154158
    155159    default:
    156         psError(__func__,"psImageClip does not support the given datatype (%d)",
    157                 input->type.type);
     160        psError(__func__, "psImageClip does not support the given datatype (%d)", input->type.type);
    158161    }
    159162
     
    161164}
    162165
    163 int psImageOverlaySection(psImage* image, const psImage* overlay, int col0,
    164                           int row0, const char* op)
     166int psImageOverlaySection(psImage * image, const psImage * overlay, int col0, int row0, const char *op)
    165167{
    166168    unsigned int imageNumRows;
     
    170172    unsigned int imageRowLimit;
    171173    unsigned int imageColLimit;
    172     psElemType  type;
     174    psElemType type;
    173175
    174176    if (image == NULL || overlay == NULL) {
    175         psError(__func__,"one of the input images was NULL.");
     177        psError(__func__, "one of the input images was NULL.");
    176178        return 1;
    177179    }
    178180
    179181    if (op == NULL) {
    180         psError(__func__,"Operation can not be NULL.");
     182        psError(__func__, "Operation can not be NULL.");
    181183        return 1;
    182184    }
     
    185187
    186188    if (type != overlay->type.type) {
    187         psError(__func__,"Image and overlay datatypes must match. (%d vs %d)",
    188                 type,overlay->type.type);
     189        psError(__func__, "Image and overlay datatypes must match. (%d vs %d)", type, overlay->type.type);
    189190        return 2;
    190191    }
     
    197198    /* check row0/col0 to see if it is within the image size */
    198199    if (row0 < 0 || col0 < 0 || row0 >= imageNumRows || col0 >= imageNumCols) {
    199         psError(__func__, "Overlay origin of (%d,%d) is outside of the image dimensions (%d x %d).",
     200        psError(__func__,
     201                "Overlay origin of (%d,%d) is outside of the image dimensions (%d x %d).",
    200202                col0, row0, imageNumCols, imageNumRows);
    201203        return 3;
     
    203205
    204206    /* check if overlay is totally withing input image */
    205     imageRowLimit = row0+overlayNumRows;
    206     imageColLimit = col0+overlayNumCols;
     207    imageRowLimit = row0 + overlayNumRows;
     208    imageColLimit = col0 + overlayNumCols;
    207209    if (imageRowLimit > imageNumRows || imageColLimit > imageNumCols) {
    208         psError(__func__, "Overlay image (%d,%d -> %d,%d) is partially outside"
     210        psError(__func__,
     211                "Overlay image (%d,%d -> %d,%d) is partially outside"
    209212                " of the input image (%d x %d).",
    210                 col0, row0, col0+overlayNumCols-1, row0+overlayNumRows-1,
    211                 imageNumCols,imageNumRows);
     213                col0, row0, col0 + overlayNumCols - 1, row0 + overlayNumRows - 1, imageNumCols, imageNumRows);
    212214        return 4;
    213215    }
     
    259261
    260262    default:
    261         psError(__func__,"Can not operate on type %d.",type);
     263        psError(__func__, "Can not operate on type %d.", type);
    262264    }
    263265
     
    265267}
    266268
    267 int psImageClipComplexRegion(psImage* input, psC64 min, psC64 vmin, psC64 max, psC64 vmax)
     269int psImageClipComplexRegion(psImage * input, psC64 min, psC64 vmin, psC64 max, psC64 vmax)
    268270{
    269271    int numClipped = 0;
     
    276278
    277279    if (input == NULL) {
    278         psError(__func__,"Can not perform clip on NULL image");
     280        psError(__func__, "Can not perform clip on NULL image");
    279281        return 0;
    280282    }
    281283
    282     if ( realMax < realMin ) {
    283         psError(__func__,"psImageClipComplexRegion can not be invoked with "
    284                 "max < min in the real image space.");
     284    if (realMax < realMin) {
     285        psError(__func__,
     286                "psImageClipComplexRegion can not be invoked with " "max < min in the real image space.");
    285287        return 0;
    286288    }
    287     if ( imagMax < imagMin ) {
    288         psError(__func__,"psImageClipComplexRegion can not be invoked with "
     289    if (imagMax < imagMin) {
     290        psError(__func__,
     291                "psImageClipComplexRegion can not be invoked with "
    289292                "max < min in the imaginary image space.");
    290293        return 0;
     
    294297    numCols = input->numCols;
    295298
     299    #define psImageClipComplexRegionCase(type,typename,realfcn,imagfcn) \
     300case PS_TYPE_##type: { \
     301        if (realfcn(vmin) < PS_MIN_##type || imagfcn(vmin) < PS_MIN_##type || \
     302                realfcn(vmin) > PS_MAX_##type || imagfcn(vmin) > PS_MAX_##type ) { \
     303            psError(__func__, "Specified vmin (%g%+gi) is outside of image's " \
     304                    typename " pixel range", \
     305                    creal(vmin),cimag(vmin)); \
     306            break; \
     307        } \
     308        if (realfcn(vmax) > PS_MAX_##type || imagfcn(vmax) > PS_MAX_##type || \
     309                realfcn(vmax) < PS_MIN_##type || imagfcn(vmax) < PS_MIN_##type ) { \
     310            psError(__func__, "Specified vmax (%g%+gi) is outside of image's " \
     311                    typename " pixel range", \
     312                    creal(vmax),cimag(vmax)); \
     313            break; \
     314        } \
     315        for (unsigned int row = 0;row<numRows;row++) { \
     316            ps##type* inputRow = input->data.type[row]; \
     317            for (unsigned int col = 0; col < numCols; col++) { \
     318                if ( (realfcn(inputRow[col]) > realMax) || (imagfcn(inputRow[col]) > imagMax) ) { \
     319                    inputRow[col] = (ps##type)vmax; \
     320                    numClipped++; \
     321                } else if ( (realfcn(inputRow[col]) < realMin) || (imagfcn(inputRow[col]) < imagMin) ){ \
     322                    inputRow[col] = (ps##type)vmin; \
     323                    numClipped++; \
     324                } \
     325            } \
     326        } \
     327    } \
     328    break;
     329
    296330    switch (input->type.type) {
    297331
    298         #define psImageClipComplexRegionCase(type,typename,realfcn,imagfcn) \
    299     case PS_TYPE_##type: { \
    300             if (realfcn(vmin) < PS_MIN_##type || imagfcn(vmin) < PS_MIN_##type || \
    301                     realfcn(vmin) > PS_MAX_##type || imagfcn(vmin) > PS_MAX_##type ) { \
    302                 psError(__func__, "Specified vmin (%g%+gi) is outside of image's " \
    303                         typename " pixel range", \
    304                         creal(vmin),cimag(vmin)); \
    305                 break; \
    306             } \
    307             if (realfcn(vmax) > PS_MAX_##type || imagfcn(vmax) > PS_MAX_##type || \
    308                     realfcn(vmax) < PS_MIN_##type || imagfcn(vmax) < PS_MIN_##type ) { \
    309                 psError(__func__, "Specified vmax (%g%+gi) is outside of image's " \
    310                         typename " pixel range", \
    311                         creal(vmax),cimag(vmax)); \
    312                 break; \
    313             } \
    314             for (unsigned int row = 0;row<numRows;row++) { \
    315                 ps##type* inputRow = input->data.type[row]; \
    316                 for (unsigned int col = 0; col < numCols; col++) { \
    317                     if ( (realfcn(inputRow[col]) > realMax) || (imagfcn(inputRow[col]) > imagMax) ) { \
    318                         inputRow[col] = (ps##type)vmax; \
    319                         numClipped++; \
    320                     } else if ( (realfcn(inputRow[col]) < realMin) || (imagfcn(inputRow[col]) < imagMin) ){ \
    321                         inputRow[col] = (ps##type)vmin; \
    322                         numClipped++; \
    323                     } \
    324                 } \
    325             } \
    326         } \
    327         break;
    328 
    329         psImageClipComplexRegionCase(C32,"psC32",crealf,cimagf)
    330         psImageClipComplexRegionCase(C64,"psC64",creal,cimag)
     332        psImageClipComplexRegionCase(C32, "psC32", crealf, cimagf)
     333        psImageClipComplexRegionCase(C64, "psC64", creal, cimag)
    331334
    332335    default:
    333         psError(__func__,"psImageClip does not support the given datatype (%d)",
    334                 input->type.type);
     336        psError(__func__, "psImageClip does not support the given datatype (%d)", input->type.type);
    335337    }
    336338
     
    338340}
    339341
    340 
    341 psImage* psImageRebin(psImage* out,const psImage* in,unsigned int scale,const psStats* stats)
     342psImage *psImageRebin(psImage * out, const psImage * in, unsigned int scale, const psStats * stats)
    342343{
    343344    int inRows;
     
    345346    int outRows;
    346347    int outCols;
    347     psVector* vec;            // vector to hold the values of a single bin.
    348     psStats* myStats;
     348    psVector *vec;              // vector to hold
     349
     350    // the values of
     351    // a single bin.
     352    psStats *myStats;
    349353    double statVal;
    350354
    351355    if (in == NULL) {
    352         psError(__func__,"Input image is NULL.");
     356        psError(__func__, "Input image is NULL.");
    353357        psFree(out);
    354358        return NULL;
     
    356360
    357361    if (scale < 1) {
    358         psError(__func__,"The scale must be positive.");
     362        psError(__func__, "The scale must be positive.");
    359363        psFree(out);
    360364        return NULL;
     
    362366
    363367    if (stats == NULL) {
    364         psError(__func__,"The stats input can not be NULL.");
     368        psError(__func__, "The stats input can not be NULL.");
    365369        psFree(out);
    366370        return NULL;
    367371    }
    368372
    369     if (p_psGetStatValue(stats,&statVal) == false) {
    370         psError(__func__,"The stat options didn't specify a single supported statistic type.");
     373    if (p_psGetStatValue(stats, &statVal) == false) {
     374        psError(__func__, "The stat options didn't specify a single supported statistic type.");
    371375        psFree(out);
    372376        return NULL;
     
    376380    *myStats = *stats;
    377381
    378     vec = psVectorAlloc(scale*scale,in->type.type);
     382    vec = psVectorAlloc(scale * scale, in->type.type);
    379383
    380384    // create output image.
    381385    inRows = in->numRows;
    382386    inCols = in->numCols;
    383     outRows = (inRows+scale-1) / scale;   // round-up for remainders
    384     outCols = (inCols+scale-1) / scale;   // round-up for remainders
    385     out = psImageRecycle(out,outCols,outRows,in->type.type);
     387    outRows = (inRows + scale - 1) / scale;     // round-up
     388    // for
     389    // remainders
     390    outCols = (inCols + scale - 1) / scale;     // round-up
     391    // for
     392    // remainders
     393    out = psImageRecycle(out, outCols, outRows, in->type.type);
    386394
    387395    #define PS_IMAGE_REBIN_CASE(type) \
     
    425433        PS_IMAGE_REBIN_CASE(C64);
    426434    default:
    427         psError(__func__,"Input image type not supported.");
     435        psError(__func__, "Input image type not supported.");
    428436        psFree(out);
    429437        out = NULL;
     
    435443    return out;
    436444}
    437 psImage* psImageResample(psImage* out, const psImage* in, int scale, psImageInterpolateMode mode)
     445
     446psImage *psImageResample(psImage * out, const psImage * in, int scale, psImageInterpolateMode mode)
    438447{
    439448    int outRows;
     
    442451
    443452    if (in == NULL) {
    444         psError(__func__,"Input image can not be NULL.");
     453        psError(__func__, "Input image can not be NULL.");
    445454        psFree(out);
    446455        return NULL;
    447456    }
    448 
    449     // create an output image of the same size and type
    450     outRows = in->numRows*scale;
    451     outCols = in->numCols*scale;
     457    // create an output image of the same size
     458    // and type
     459    outRows = in->numRows * scale;
     460    outCols = in->numCols * scale;
    452461    invScale = 1.0f / (float)scale;
    453 
    454462
    455463    #define PSIMAGE_RESAMPLE_CASE(TYPE) \
     
    466474    }
    467475
    468     switch(in->type.type) {
     476    switch (in->type.type) {
    469477        PSIMAGE_RESAMPLE_CASE(U8)
    470478        PSIMAGE_RESAMPLE_CASE(U16)
     
    480488        PSIMAGE_RESAMPLE_CASE(C64)
    481489    default:
    482         psError(__func__,"Unsupported type (%d)",in->type.type);
     490        psError(__func__, "Unsupported type (%d)", in->type.type);
    483491        psFree(out);
    484492        return NULL;
     
    488496}
    489497
    490 psImage* psImageRoll(psImage* out, const psImage* in, int dx, int dy)
     498psImage *psImageRoll(psImage * out, const psImage * in, int dx, int dy)
    491499{
    492500    int outRows;
     
    495503
    496504    if (in == NULL) {
    497         psError(__func__,"Input image can not be NULL.");
     505        psError(__func__, "Input image can not be NULL.");
    498506        psFree(out);
    499507        return NULL;
    500508    }
    501 
    502     // create an output image of the same size and type
     509    // create an output image of the same size
     510    // and type
    503511    outRows = in->numRows;
    504512    outCols = in->numCols;
    505513    elementSize = PSELEMTYPE_SIZEOF(in->type.type);
    506     out = psImageRecycle(out,outCols, outRows, in->type.type);
    507 
    508     // make dx and dy between 0 and outCols or outRows, respectively
     514    out = psImageRecycle(out, outCols, outRows, in->type.type);
     515
     516    // make dx and dy between 0 and outCols or
     517    // outRows, respectively
    509518    dx = dx % outCols;
    510519    dy = dy % outRows;
     
    516525    }
    517526
    518     int segment1Size = elementSize*(outCols-dx);
    519     int segment2Size = elementSize*dx;
    520 
    521     for (int row=0;row<outRows;row++) {
    522         int inRowNumber = row+dy;
     527    int segment1Size = elementSize * (outCols - dx);
     528    int segment2Size = elementSize * dx;
     529
     530    for (int row = 0; row < outRows; row++) {
     531        int inRowNumber = row + dy;
     532
    523533        if (inRowNumber >= outRows) {
    524534            inRowNumber -= outRows;
    525535        }
    526         psU8* inRow = in->data.U8[inRowNumber]; // to allow byte arithmetic, but for all types
    527         psU8* outRow = out->data.U8[row];
    528         memcpy(outRow,inRow+segment2Size,segment1Size);
    529         memcpy(outRow+segment1Size,inRow,segment2Size);
     536        psU8 *inRow = in->data.U8[inRowNumber]; // to
     537
     538        // allow
     539
     540        // byte
     541        // arithmetic,
     542
     543        // but for all types
     544        psU8 *outRow = out->data.U8[row];
     545
     546        memcpy(outRow, inRow + segment2Size, segment1Size);
     547        memcpy(outRow + segment1Size, inRow, segment2Size);
    530548    }
    531549
     
    533551}
    534552
    535 psImage* psImageRotate(psImage* out, const psImage* in, float angle, float unexposedValue, psImageInterpolateMode mode)
     553psImage *psImageRotate(psImage * out,
     554                       const psImage * in, float angle, float unexposedValue, psImageInterpolateMode mode)
    536555{
    537556    if (in == NULL) {
    538         psError(__func__,"The input image was NULL.");
     557        psError(__func__, "The input image was NULL.");
    539558        psFree(out);
    540559        return NULL;
    541560    }
    542 
    543561    // put the angle in the range of 0...360.
    544     angle = angle - 360.0f*floor(angle/360.0f);
    545 
    546     if (fabsf(angle-90.0f) < FLT_EPSILON) {
     562    angle = angle - 360.0f * floor(angle / 360.0f);
     563
     564    if (fabsf(angle - 90.0f) < FLT_EPSILON) {
    547565        // perform 1/4 rotate counter-clockwise
    548566        int numRows = in->numCols;
     
    550568        int lastCol = numCols - 1;
    551569        psElemType type = in->type.type;
    552         out = psImageRecycle(out,numCols,numRows,type);
     570
     571        out = psImageRecycle(out, numCols, numRows, type);
    553572
    554573        #define PSIMAGE_ROTATE_LEFT_90(TYPE) \
     
    578597            PSIMAGE_ROTATE_LEFT_90(C64);
    579598        default:
    580             psError(__func__,"Unsupported type (%d)",type);
     599            psError(__func__, "Unsupported type (%d)", type);
    581600            psFree(out);
    582601            return NULL;
    583602        }
    584     } else
    585         if (fabsf(angle-180.0f) < FLT_EPSILON) {
    586             // perform 1/2 rotate
    587             int numRows = in->numRows;
    588             int lastRow = numRows - 1;
    589             int numCols = in->numCols;
    590             int lastCol = numCols - 1;
    591             psElemType type = in->type.type;
    592             out = psImageRecycle(out,numCols,numRows,type);
    593 
    594             #define PSIMAGE_ROTATE_180_CASE(TYPE) \
    595         case PS_TYPE_##TYPE: { \
    596                 for (int row=0;row<numRows;row++) { \
    597                     ps##TYPE* outRow = out->data.TYPE[row]; \
    598                     ps##TYPE* inRow = in->data.TYPE[lastRow-row]; \
    599                     for (int col=0;col<numCols;col++) { \
    600                         outRow[col] = inRow[lastCol - col]; \
    601                     } \
    602                 } \
    603             } \
    604             break;
    605 
    606             switch (type) {
    607                 PSIMAGE_ROTATE_180_CASE(U8);
    608                 PSIMAGE_ROTATE_180_CASE(U16);
    609                 PSIMAGE_ROTATE_180_CASE(U32);
    610                 PSIMAGE_ROTATE_180_CASE(U64);
    611                 PSIMAGE_ROTATE_180_CASE(S8);
    612                 PSIMAGE_ROTATE_180_CASE(S16);
    613                 PSIMAGE_ROTATE_180_CASE(S32);
    614                 PSIMAGE_ROTATE_180_CASE(S64);
    615                 PSIMAGE_ROTATE_180_CASE(F32);
    616                 PSIMAGE_ROTATE_180_CASE(F64);
    617                 PSIMAGE_ROTATE_180_CASE(C32);
    618                 PSIMAGE_ROTATE_180_CASE(C64);
    619             default:
    620                 psError(__func__,"Unsupported type (%d)",type);
    621                 psFree(out);
    622                 return NULL;
    623             }
    624         } else
    625             if (fabsf(angle-270.0f) < FLT_EPSILON) {
    626                 // perform 1/4 rotate clockwise
    627                 int numRows = in->numCols;
    628                 int lastRow = numRows - 1;
    629                 int numCols = in->numRows;
    630                 psElemType type = in->type.type;
    631                 out = psImageRecycle(out,numCols,numRows,type);
    632 
    633                 #define PSIMAGE_ROTATE_RIGHT_90(TYPE) \
    634             case PS_TYPE_##TYPE: { \
    635                     ps##TYPE** inData = in->data.TYPE; \
    636                     for (int row=0;row<numRows;row++) { \
    637                         ps##TYPE* outRow = out->data.TYPE[row]; \
    638                         for (int col=0;col<numCols;col++) { \
    639                             outRow[col] = inData[col][lastRow-row]; \
    640                         } \
    641                     } \
    642                 } \
    643                 break;
    644 
    645                 switch (type) {
    646                     PSIMAGE_ROTATE_RIGHT_90(U8);
    647                     PSIMAGE_ROTATE_RIGHT_90(U16);
    648                     PSIMAGE_ROTATE_RIGHT_90(U32);
    649                     PSIMAGE_ROTATE_RIGHT_90(U64);
    650                     PSIMAGE_ROTATE_RIGHT_90(S8);
    651                     PSIMAGE_ROTATE_RIGHT_90(S16);
    652                     PSIMAGE_ROTATE_RIGHT_90(S32);
    653                     PSIMAGE_ROTATE_RIGHT_90(S64);
    654                     PSIMAGE_ROTATE_RIGHT_90(F32);
    655                     PSIMAGE_ROTATE_RIGHT_90(F64);
    656                     PSIMAGE_ROTATE_RIGHT_90(C32);
    657                     PSIMAGE_ROTATE_RIGHT_90(C64);
    658                 default:
    659                     psError(__func__,"Unsupported type (%d)",type);
    660                     psFree(out);
    661                     return NULL;
    662                 }
    663             } else
    664                 if (fabsf(angle) < FLT_EPSILON) {
    665                     out = psImageCopy(out,in,in->type.type);
    666                 } else {
    667                     psElemType type = in->type.type;
    668                     int numRows = in->numRows;
    669                     int numCols = in->numCols;
    670                     double centerX = (float)(numCols) / 2.0f;
    671                     float centerY = (float)(numRows) / 2.0f;
    672                     float t = angle*(3.14159265358f/180.0f);
    673                     float cosT = cosf(t);
    674                     float sinT = sinf(t);
    675 
    676                     // calculate the corners of the rotated image so we know the proper output image size.
    677                     //    x' = x cos(t) + y sin(t);  i.e, x' = (x-centerX)*cosT + (y-centerY)*sinT;
    678                     //    y' = y cos(t) - x sin(t);  i.e. y' = (y-centerY)*cosT - (x-centerX)*sinT;
    679 
    680 
    681                     int outCols = ceil(abs(numCols*cosT)+abs(numRows*sinT))+1;
    682                     int outRows = ceil(abs(numCols*sinT)+abs(numRows*cosT))+1;
    683                     float minX = (float)outCols/-2.0f;
    684                     int intMinY = outRows/-2;
    685 
    686                     out = psImageRecycle(out,outCols,outRows,type);
    687 
    688                     /* optimized public domain rotation routine by Karl Lager
    689                     float cosT,sinT;
    690                     cosT = cos(t);
    691                     sinT = sin(t);
    692                     for (y = min_y; y <= max_y; y++)
    693                      { x' = min_x * cosT + y * sinT + x1';
    694                        y' = y * cosT - min_x * sinT + y1';
    695                        for (x = min_x; x <= max_x; x++)
    696                         { if (x', y') is in the bounds of the bitmap,
    697                              get pixel(x', y') and plot the pixel to
    698                              (x, y) on screen.
    699                           x' += cosT;
    700                           y' -= sinT;
    701                         }
    702                      }
    703                     */
    704 
    705                     // precalculate some figures that are used within loop
    706                     float minXTimesCosTPlusCenterX = minX*cosT+centerX;
    707                     float CenterYMinusminXTimesSinT = centerY-minX*sinT;
    708 
    709                     #define PSIMAGE_ROTATE_ARBITRARY_LOOP(TYPE,MODE) { \
    710                         if (unexposedValue < PS_MIN_##TYPE || unexposedValue > PS_MAX_##TYPE) { \
    711                             psError(__func__,"The given unexposedValue (%g) is outside of the " \
    712                                     "image type's range (%g->%g).", \
    713                                     unexposedValue, (double)PS_MIN_##TYPE,(double)PS_MAX_##TYPE); \
    714                             psFree(out); \
    715                             out = NULL; \
    716                             break; \
    717                         } \
    718                         float inX; \
    719                         float inY; \
    720                         ps##TYPE* outRow; \
    721                         for (int y = 0; y < outRows; y++) { \
    722                             inX = minXTimesCosTPlusCenterX + (y+intMinY) * sinT; \
    723                             inY = CenterYMinusminXTimesSinT + (y+intMinY) * cosT; \
    724                             outRow = out->data.TYPE[y]; \
    725                             for (int x = 0; x < outCols; x++) { \
    726                                 outRow[x] = p_psImagePixelInterpolate##MODE##_##TYPE(in,inX,inY,unexposedValue); \
    727                                 inX += cosT; \
    728                                 inY -= sinT; \
    729                             } \
    730                         } \
    731                     }
    732 
    733                     #define PSIMAGE_ROTATE_ARBITRARY_CASE(MODE) \
    734                 case PS_INTERPOLATE_##MODE: \
    735                     switch (type) { \
    736                     case PS_TYPE_U8: \
    737                         PSIMAGE_ROTATE_ARBITRARY_LOOP(U8,MODE); \
    738                         break; \
    739                     case PS_TYPE_U16: \
    740                         PSIMAGE_ROTATE_ARBITRARY_LOOP(U16,MODE); \
    741                         break; \
    742                     case PS_TYPE_U32: \
    743                         PSIMAGE_ROTATE_ARBITRARY_LOOP(U32,MODE); \
    744                         break; \
    745                     case PS_TYPE_U64: \
    746                         PSIMAGE_ROTATE_ARBITRARY_LOOP(U64,MODE); \
    747                         break; \
    748                     case PS_TYPE_S8: \
    749                         PSIMAGE_ROTATE_ARBITRARY_LOOP(S8,MODE); \
    750                         break; \
    751                     case PS_TYPE_S16: \
    752                         PSIMAGE_ROTATE_ARBITRARY_LOOP(S16,MODE); \
    753                         break; \
    754                     case PS_TYPE_S32: \
    755                         PSIMAGE_ROTATE_ARBITRARY_LOOP(S32,MODE); \
    756                         break; \
    757                     case PS_TYPE_S64: \
    758                         PSIMAGE_ROTATE_ARBITRARY_LOOP(S64,MODE); \
    759                         break; \
    760                     case PS_TYPE_F32: \
    761                         PSIMAGE_ROTATE_ARBITRARY_LOOP(F32,MODE); \
    762                         break; \
    763                     case PS_TYPE_F64: \
    764                         PSIMAGE_ROTATE_ARBITRARY_LOOP(F64,MODE); \
    765                         break; \
    766                     case PS_TYPE_C32: \
    767                         PSIMAGE_ROTATE_ARBITRARY_LOOP(C32,MODE); \
    768                         break; \
    769                     case PS_TYPE_C64: \
    770                         PSIMAGE_ROTATE_ARBITRARY_LOOP(C64,MODE); \
    771                         break; \
    772                     default: \
    773                         psError(__func__,"Image type (%d) not supported",type); \
    774                         psFree(out); \
    775                         out = NULL; \
    776                     } \
    777                     break;
    778 
    779                     switch (mode) {
    780                         PSIMAGE_ROTATE_ARBITRARY_CASE(FLAT);
    781                         PSIMAGE_ROTATE_ARBITRARY_CASE(BILINEAR);
    782                     default:
    783                         psError(__func__,"Unsupported interpolation mode (%d)",mode);
    784                         psFree(out);
    785                         out = NULL;
    786                     }
    787                 }
     603    } else if (fabsf(angle - 180.0f) < FLT_EPSILON) {
     604        // perform 1/2 rotate
     605        int numRows = in->numRows;
     606        int lastRow = numRows - 1;
     607        int numCols = in->numCols;
     608        int lastCol = numCols - 1;
     609        psElemType type = in->type.type;
     610
     611        out = psImageRecycle(out, numCols, numRows, type);
     612
     613        #define PSIMAGE_ROTATE_180_CASE(TYPE) \
     614    case PS_TYPE_##TYPE: { \
     615            for (int row=0;row<numRows;row++) { \
     616                ps##TYPE* outRow = out->data.TYPE[row]; \
     617                ps##TYPE* inRow = in->data.TYPE[lastRow-row]; \
     618                for (int col=0;col<numCols;col++) { \
     619                    outRow[col] = inRow[lastCol - col]; \
     620                } \
     621            } \
     622        } \
     623        break;
     624
     625        switch (type) {
     626            PSIMAGE_ROTATE_180_CASE(U8);
     627            PSIMAGE_ROTATE_180_CASE(U16);
     628            PSIMAGE_ROTATE_180_CASE(U32);
     629            PSIMAGE_ROTATE_180_CASE(U64);
     630            PSIMAGE_ROTATE_180_CASE(S8);
     631            PSIMAGE_ROTATE_180_CASE(S16);
     632            PSIMAGE_ROTATE_180_CASE(S32);
     633            PSIMAGE_ROTATE_180_CASE(S64);
     634            PSIMAGE_ROTATE_180_CASE(F32);
     635            PSIMAGE_ROTATE_180_CASE(F64);
     636            PSIMAGE_ROTATE_180_CASE(C32);
     637            PSIMAGE_ROTATE_180_CASE(C64);
     638        default:
     639            psError(__func__, "Unsupported type (%d)", type);
     640            psFree(out);
     641            return NULL;
     642        }
     643    } else if (fabsf(angle - 270.0f) < FLT_EPSILON) {
     644        // perform 1/4 rotate clockwise
     645        int numRows = in->numCols;
     646        int lastRow = numRows - 1;
     647        int numCols = in->numRows;
     648        psElemType type = in->type.type;
     649
     650        out = psImageRecycle(out, numCols, numRows, type);
     651
     652        #define PSIMAGE_ROTATE_RIGHT_90(TYPE) \
     653    case PS_TYPE_##TYPE: { \
     654            ps##TYPE** inData = in->data.TYPE; \
     655            for (int row=0;row<numRows;row++) { \
     656                ps##TYPE* outRow = out->data.TYPE[row]; \
     657                for (int col=0;col<numCols;col++) { \
     658                    outRow[col] = inData[col][lastRow-row]; \
     659                } \
     660            } \
     661        } \
     662        break;
     663
     664        switch (type) {
     665            PSIMAGE_ROTATE_RIGHT_90(U8);
     666            PSIMAGE_ROTATE_RIGHT_90(U16);
     667            PSIMAGE_ROTATE_RIGHT_90(U32);
     668            PSIMAGE_ROTATE_RIGHT_90(U64);
     669            PSIMAGE_ROTATE_RIGHT_90(S8);
     670            PSIMAGE_ROTATE_RIGHT_90(S16);
     671            PSIMAGE_ROTATE_RIGHT_90(S32);
     672            PSIMAGE_ROTATE_RIGHT_90(S64);
     673            PSIMAGE_ROTATE_RIGHT_90(F32);
     674            PSIMAGE_ROTATE_RIGHT_90(F64);
     675            PSIMAGE_ROTATE_RIGHT_90(C32);
     676            PSIMAGE_ROTATE_RIGHT_90(C64);
     677        default:
     678            psError(__func__, "Unsupported type (%d)", type);
     679            psFree(out);
     680            return NULL;
     681        }
     682    } else if (fabsf(angle) < FLT_EPSILON) {
     683        out = psImageCopy(out, in, in->type.type);
     684    } else {
     685        psElemType type = in->type.type;
     686        int numRows = in->numRows;
     687        int numCols = in->numCols;
     688        double centerX = (float)(numCols) / 2.0f;
     689        float centerY = (float)(numRows) / 2.0f;
     690        float t = angle * (3.14159265358f / 180.0f);
     691        float cosT = cosf(t);
     692        float sinT = sinf(t);
     693
     694        // calculate the corners of the rotated
     695        // image so we know the proper
     696        // output image size.
     697        // x' = x cos(t) + y sin(t); i.e, x' =
     698        // (x-centerX)*cosT +
     699        // (y-centerY)*sinT;
     700        // y' = y cos(t) - x sin(t); i.e. y' =
     701        // (y-centerY)*cosT -
     702        // (x-centerX)*sinT;
     703
     704        int outCols = ceil(abs(numCols * cosT) + abs(numRows * sinT)) + 1;
     705        int outRows = ceil(abs(numCols * sinT) + abs(numRows * cosT)) + 1;
     706        float minX = (float)outCols / -2.0f;
     707        int intMinY = outRows / -2;
     708
     709        out = psImageRecycle(out, outCols, outRows, type);
     710
     711        /* optimized public domain rotation routine by Karl Lager float cosT,sinT; cosT = cos(t); sinT =
     712         * sin(t); for (y = min_y; y <= max_y; y++) { x' = min_x * cosT + y * sinT + x1'; y' = y * cosT -
     713         * min_x * sinT + y1'; for (x = min_x; x <= max_x; x++) { if (x', y') * * * * * * * * is in the
     714         * bounds of the bitmap, get pixel(x', y') and plot the pixel to (x, y) on screen. x' += cosT; y' -=
     715         * sinT; } } */
     716
     717        // precalculate some figures that are
     718        // used within loop
     719        float minXTimesCosTPlusCenterX = minX * cosT + centerX;
     720        float CenterYMinusminXTimesSinT = centerY - minX * sinT;
     721
     722        #define PSIMAGE_ROTATE_ARBITRARY_LOOP(TYPE,MODE) { \
     723            if (unexposedValue < PS_MIN_##TYPE || unexposedValue > PS_MAX_##TYPE) { \
     724                psError(__func__,"The given unexposedValue (%g) is outside of the " \
     725                        "image type's range (%g->%g).", \
     726                        unexposedValue, (double)PS_MIN_##TYPE,(double)PS_MAX_##TYPE); \
     727                psFree(out); \
     728                out = NULL; \
     729                break; \
     730            } \
     731            float inX; \
     732            float inY; \
     733            ps##TYPE* outRow; \
     734            for (int y = 0; y < outRows; y++) { \
     735                inX = minXTimesCosTPlusCenterX + (y+intMinY) * sinT; \
     736                inY = CenterYMinusminXTimesSinT + (y+intMinY) * cosT; \
     737                outRow = out->data.TYPE[y]; \
     738                for (int x = 0; x < outCols; x++) { \
     739                    outRow[x] = p_psImagePixelInterpolate##MODE##_##TYPE(in,inX,inY,unexposedValue); \
     740                    inX += cosT; \
     741                    inY -= sinT; \
     742                } \
     743            } \
     744        }
     745
     746        #define PSIMAGE_ROTATE_ARBITRARY_CASE(MODE) \
     747    case PS_INTERPOLATE_##MODE: \
     748        switch (type) { \
     749        case PS_TYPE_U8: \
     750            PSIMAGE_ROTATE_ARBITRARY_LOOP(U8,MODE); \
     751            break; \
     752        case PS_TYPE_U16: \
     753            PSIMAGE_ROTATE_ARBITRARY_LOOP(U16,MODE); \
     754            break; \
     755        case PS_TYPE_U32: \
     756            PSIMAGE_ROTATE_ARBITRARY_LOOP(U32,MODE); \
     757            break; \
     758        case PS_TYPE_U64: \
     759            PSIMAGE_ROTATE_ARBITRARY_LOOP(U64,MODE); \
     760            break; \
     761        case PS_TYPE_S8: \
     762            PSIMAGE_ROTATE_ARBITRARY_LOOP(S8,MODE); \
     763            break; \
     764        case PS_TYPE_S16: \
     765            PSIMAGE_ROTATE_ARBITRARY_LOOP(S16,MODE); \
     766            break; \
     767        case PS_TYPE_S32: \
     768            PSIMAGE_ROTATE_ARBITRARY_LOOP(S32,MODE); \
     769            break; \
     770        case PS_TYPE_S64: \
     771            PSIMAGE_ROTATE_ARBITRARY_LOOP(S64,MODE); \
     772            break; \
     773        case PS_TYPE_F32: \
     774            PSIMAGE_ROTATE_ARBITRARY_LOOP(F32,MODE); \
     775            break; \
     776        case PS_TYPE_F64: \
     777            PSIMAGE_ROTATE_ARBITRARY_LOOP(F64,MODE); \
     778            break; \
     779        case PS_TYPE_C32: \
     780            PSIMAGE_ROTATE_ARBITRARY_LOOP(C32,MODE); \
     781            break; \
     782        case PS_TYPE_C64: \
     783            PSIMAGE_ROTATE_ARBITRARY_LOOP(C64,MODE); \
     784            break; \
     785        default: \
     786            psError(__func__,"Image type (%d) not supported",type); \
     787            psFree(out); \
     788            out = NULL; \
     789        } \
     790        break;
     791
     792        switch (mode) {
     793            PSIMAGE_ROTATE_ARBITRARY_CASE(FLAT);
     794            PSIMAGE_ROTATE_ARBITRARY_CASE(BILINEAR);
     795        default:
     796            psError(__func__, "Unsupported interpolation mode (%d)", mode);
     797            psFree(out);
     798            out = NULL;
     799        }
     800    }
    788801
    789802    return out;
    790803}
    791804
    792 psImage* psImageShift(psImage* out, const psImage* in, float dx, float dy, psF64 unexposedValue, psImageInterpolateMode mode)
     805psImage *psImageShift(psImage * out,
     806                      const psImage * in,
     807                      float dx, float dy, psF64 unexposedValue, psImageInterpolateMode mode)
    793808{
    794809    int outRows;
     
    798813
    799814    if (in == NULL) {
    800         psError(__func__,"Input image can not be NULL.");
     815        psError(__func__, "Input image can not be NULL.");
    801816        return NULL;
    802817    }
    803 
    804     // create an output image of the same size and type
     818    // create an output image of the same size
     819    // and type
    805820    outRows = in->numRows;
    806821    outCols = in->numCols;
    807822    type = in->type.type;
    808823    elementSize = PSELEMTYPE_SIZEOF(type);
    809     out = psImageRecycle(out,outCols, outRows, type);
     824    out = psImageRecycle(out, outCols, outRows, type);
    810825
    811826    #define PSIMAGE_SHIFT_CASE(TYPE) \
     
    842857        PSIMAGE_SHIFT_CASE(C64);
    843858    default:
    844         psError(__func__,"Image type (%d) not supported.",type);
     859        psError(__func__, "Image type (%d) not supported.", type);
    845860        psFree(out);
    846861        out = NULL;
  • trunk/psLib/src/image/psImageManip.h

    r1263 r1407  
     1
    12/** @file  psImageManip.h
    23 *
     
    1011 *  @author Ross Harman, MHPCC
    1112 *
    12  *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-07-22 20:42:22 $
     13 *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
     14 *  @date $Date: 2004-08-07 00:06:06 $
    1415 *
    1516 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1617 */
    1718#ifndef PS_IMAGE_MANIP_H
    18 #define PS_IMAGE_MANIP_H
     19#    define PS_IMAGE_MANIP_H
    1920
    20 #include "psImage.h"
     21#    include "psImage.h"
    2122
    2223/// @addtogroup Image
     
    3132 *  @return int     The number of clipped pixels
    3233 */
    33 int psImageClip(
    34     psImage* input,                 ///< the image to clip
    35     psF64 min,                      ///< the minimum image value allowed
    36     psF64 vmin,                     ///< the value pixels < min are set to
    37     psF64 max,                      ///< the maximum image value allowed
    38     psF64 vmax                      ///< the value pixels > max are set to
    39 );
     34int psImageClip(psImage * input,        // /< the image to clip
     35                psF64 min,      // /< the minimum image value allowed
     36                psF64 vmin,     // /< the value pixels < min are set to
     37                psF64 max,      // /< the maximum image value allowed
     38                psF64 vmax      // /< the value pixels > max are set to
     39               );
    4040
    4141/** Clip image values outside of a specified complex region
     
    4848 *  @return int     The number of clipped pixels
    4949 */
    50 int psImageClipComplexRegion(
    51     psImage* input,                 ///< the image to clip
    52     psC64 min,                      ///< the minimum image value allowed
    53     psC64 vmin,                     ///< the value pixels < min are set to
    54     psC64 max,                      ///< the maximum image value allowed
    55     psC64 vmax                      ///< the value pixels > max are set to
    56 );
     50int psImageClipComplexRegion(psImage * input,   // /< the image to clip
     51                             psC64 min, // /< the minimum image value allowed
     52                             psC64 vmin,        // /< the value pixels < min are set to
     53                             psC64 max, // /< the maximum image value allowed
     54                             psC64 vmax // /< the value pixels > max are set to
     55                            );
    5756
    5857/** Clip NaN image pixels to given value.
     
    6362 *  @return int     The number of clipped pixels
    6463 */
    65 int psImageClipNaN(
    66     psImage* input,                 ///< the image to clip
    67     psF64 value                     ///< the value to set all NaN/Inf values to
    68 );
     64int psImageClipNaN(psImage * input,     // /< the image to clip
     65                   psF64 value  // /< the value to set all NaN/Inf values to
     66                  );
    6967
    7068/** Overlay subregion of image with another image
     
    7977 *  @return int         0 if success, non-zero if failed.
    8078 */
    81 int psImageOverlaySection(
    82     psImage* image,                 ///< target image
    83     const psImage* overlay,         ///< the overlay image
    84     int col0,                       ///< the column to start overlay
    85     int row0,                       ///< the row to start overlay
    86     const char* op                  ///< the operation to perform for overlay
    87 );
     79int psImageOverlaySection(psImage * image,      // /< target image
     80                          const psImage * overlay,      // /< the overlay image
     81                          int col0,     // /< the column to start overlay
     82                          int row0,     // /< the row to start overlay
     83                          const char *op        // /< the operation to perform for overlay
     84                         );
    8885
    8986/** Rebin image to new scale.
     
    9794 *  @return psImage    new image formed by rebinning input image.
    9895 */
    99 psImage* psImageRebin(
    100     psImage* out,                   ///< an psImage to recycle.  If NULL, a new image is created
    101     const psImage* in,              ///< input image
    102     unsigned int scale,             ///< the scale to rebin for each dimension
    103     const psStats* stats            ///< the statistic to perform when rebinning.  Only one method should be set.
    104 );
     96psImage *psImageRebin(psImage * out,    // /< an psImage to recycle.  If NULL, a new image is created
     97                      const psImage * in,       // /< input image
     98                      unsigned int scale,       // /< the scale to rebin for each dimension
     99                      const psStats * stats     // /< the statistic to perform when rebinning.  Only one
     100                      // method should be set.
     101                     );
    105102
    106 psImage* psImageResample(
    107     psImage* out,                   ///< an psImage to recycle.  If NULL, a new image is created
    108     const psImage* in,              ///< input image
    109     int scale,
    110     psImageInterpolateMode mode
    111 );
     103psImage *psImageResample(psImage * out, // /< an psImage to recycle.  If NULL, a new image is created
     104                         const psImage * in,    // /< input image
     105                         int scale, psImageInterpolateMode mode);
    112106
    113 psImage* psImageRotate(
    114     psImage* out,                   ///< an psImage to recycle.  If NULL, a new image is created
    115     const psImage* in,              ///< input image
    116     float angle,
    117     float unexposedValue,
    118     psImageInterpolateMode mode
    119 );
     107psImage *psImageRotate(psImage * out,   // /< an psImage to recycle.  If NULL, a new image is created
     108                       const psImage * in,      // /< input image
     109                       float angle, float unexposedValue, psImageInterpolateMode mode);
    120110
    121 psImage* psImageShift(
    122     psImage* out,                   ///< an psImage to recycle.  If NULL, a new image is created
    123     const psImage* in,              ///< input image
    124     float dx,
    125     float dy,
    126     float unexposedValue,
    127     psImageInterpolateMode mode
    128 );
     111psImage *psImageShift(psImage * out,    // /< an psImage to recycle.  If NULL, a new image is created
     112                      const psImage * in,       // /< input image
     113                      float dx, float dy, float unexposedValue, psImageInterpolateMode mode);
    129114
    130115/** Roll image by an integer number of pixels in either direction.
     
    136121 *  @return psImage*    the rolled version of the input image.
    137122 */
    138 psImage* psImageRoll(
    139     psImage* out,                   ///< an psImage to recycle.  If NULL, a new image is created
    140     const psImage* in,              ///< input image
    141     int dx,                         ///< number of pixels to roll in the x-dimension
    142     int dy                          ///< number of pixels to roll in the y-dimension
    143 );
     123psImage *psImageRoll(psImage * out,     // /< an psImage to recycle.  If NULL, a new image is created
     124                     const psImage * in,        // /< input image
     125                     int dx,    // /< number of pixels to roll in the x-dimension
     126                     int dy     // /< number of pixels to roll in the y-dimension
     127                    );
    144128
    145129#endif
    146 
  • trunk/psLib/src/image/psImageStats.c

    r1406 r1407  
     1
    12/** @file psImageStats.c
    23*  \brief Routines for calculating statistics on images.
     
    910*  @author George Gusciora, MHPCC
    1011*
    11 *  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
    12 *  @date $Date: 2004-08-06 22:34:05 $
     12*  @version $Revision: 1.30 $ $Name: not supported by cvs2svn $
     13*  @date $Date: 2004-08-07 00:06:06 $
    1314*
    1415*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3233
    3334/// This routine must determine the various statistics for the image.
     35
    3436/*****************************************************************************
    3537    NOTE: verify that image/mask have the correct types, and sizes.
    3638 *****************************************************************************/
    37 psStats *psImageStats(psStats *stats,
    38                       psImage *in,
    39                       psImage *mask,
    40                       int maskVal)
    41 {
    42     psVector* junkData=NULL;
    43     psVector* junkMask=NULL;
     39psStats *psImageStats(psStats * stats, psImage * in, psImage * mask, int maskVal)
     40{
     41    psVector *junkData = NULL;
     42    psVector *junkMask = NULL;
    4443
    4544    if (stats == NULL) {
    46         psError(__func__,"The input psStats struct can not be NULL.");
     45        psError(__func__, "The input psStats struct can not be NULL.");
    4746        return NULL;
    4847    }
    4948
    5049    if (in == NULL) {
    51         psError(__func__,"The input image can not be NULL.");
     50        psError(__func__, "The input image can not be NULL.");
    5251        return NULL;
    5352    }
    5453
    5554    if (stats->options == 0) {
    56         psError(__func__,"No statistic option/operation was specified.");
     55        psError(__func__, "No statistic option/operation was specified.");
    5756        return stats;
    5857    }
    59 
    60     // stuff the image data into a psVector struct.
     58    // stuff the image data into a psVector
     59    // struct.
    6160    junkData = psAlloc(sizeof(psVector));
    6261    junkData->type = in->type;
    63     junkData->nalloc = in->numRows*in->numCols;
     62    junkData->nalloc = in->numRows * in->numCols;
    6463    junkData->n = junkData->nalloc;
    65     junkData->data.V = in->data.V[0];    // since psImage data is contiguous...
     64    junkData->data.V = in->data.V[0];      // since
     65    // psImage
     66    // data
     67    // is
     68    // contiguous...
    6669
    6770    if (mask != NULL) {
    6871        if (mask->type.type != PS_TYPE_MASK) {
    69             psError(__func__, "Expected the mask image type not found (type=%x)",
    70                     mask->type.type);
     72            psError(__func__, "Expected the mask image type not found (type=%x)", mask->type.type);
    7173            psFree(junkData);
    7274            return NULL;
    7375        }
    74 
    75         // stuff the mask data into a psVector struct.
     76        // stuff the mask data into a psVector
     77        // struct.
    7678        junkMask = psAlloc(sizeof(psVector));
    7779        junkMask->type = mask->type;
    78         junkMask->nalloc = mask->numRows*mask->numCols;
     80        junkMask->nalloc = mask->numRows * mask->numCols;
    7981        junkMask->n = junkMask->nalloc;
    8082        junkMask->data.V = mask->data.V[0];
     
    8587    psFree(junkMask);
    8688    psFree(junkData);
    87     return(stats);
     89    return (stats);
    8890}
    8991
     
    9395    NOTE: verify that image/mask have the, correct types and  sizes.
    9496 *****************************************************************************/
    95 psHistogram *psImageHistogram(psHistogram *out,
    96                               psImage *in,
    97                               psImage *mask,
    98                               unsigned int maskVal)
    99 {
    100     psVector *junkData=NULL;
    101     psVector *junkMask=NULL;
     97psHistogram *psImageHistogram(psHistogram * out, psImage * in, psImage * mask, unsigned int maskVal)
     98{
     99    psVector *junkData = NULL;
     100    psVector *junkMask = NULL;
    102101
    103102    // NOTE: Verify this action.
    104     if ((out == NULL) ||
    105             (in == NULL)) {
    106         return(NULL);
     103    if ((out == NULL) || (in == NULL)) {
     104        return (NULL);
    107105    }
    108106
    109107    junkData = psAlloc(sizeof(psVector));
    110108    junkData->type = in->type;
    111     junkData->nalloc = in->numRows*in->numCols;
     109    junkData->nalloc = in->numRows * in->numCols;
    112110    junkData->n = junkData->nalloc;
    113     junkData->data.V = in->data.V[0];    // since psImage data is contiguous...
     111    junkData->data.V = in->data.V[0];      // since
     112    // psImage
     113    // data
     114    // is
     115    // contiguous...
    114116
    115117    if (mask != NULL) {
    116118        if (mask->type.type != PS_TYPE_MASK) {
    117             psError(__func__, "Expected the mask image type not found (type=%x)",
    118                     mask->type.type);
     119            psError(__func__, "Expected the mask image type not found (type=%x)", mask->type.type);
    119120            psFree(junkData);
    120121            return NULL;
    121122        }
    122 
    123         // stuff the mask data into a psVector struct.
     123        // stuff the mask data into a psVector
     124        // struct.
    124125        junkMask = psAlloc(sizeof(psVector));
    125126        junkMask->type = mask->type;
    126         junkMask->nalloc = mask->numRows*mask->numCols;
     127        junkMask->nalloc = mask->numRows * mask->numCols;
    127128        junkMask->n = junkMask->nalloc;
    128129        junkMask->data.V = mask->data.V[0];
     
    134135    psFree(junkData);
    135136
    136     return(out);
     137    return (out);
    137138}
    138139
     
    141142    int i = 0;
    142143    float tmp = 0.0;
    143     float *scalingFactors = (float *) psAlloc(n * sizeof(float));
    144 
    145     for (i=0;i<n;i++) {
    146         //     ((2.0 * (float) i) / ((float) (n-1))) - 1.0;
    147         //        tmp = (float) (i + 1);
    148         tmp = (float) (n - i);
    149         tmp = (M_PI * (tmp - 0.5)) / ((float) n);
     144    float *scalingFactors = (float *)psAlloc(n * sizeof(float));
     145
     146    for (i = 0; i < n; i++) {
     147        // ((2.0 * (float) i) / ((float) (n-1)))
     148        // - 1.0;
     149        // tmp = (float) (i + 1);
     150        tmp = (float)(n - i);
     151        tmp = (M_PI * (tmp - 0.5)) / ((float)n);
    150152        scalingFactors[i] = cos(tmp);
    151153    }
    152154
    153     return(scalingFactors);
     155    return (scalingFactors);
    154156}
    155157
     
    165167    int i = 0;
    166168    float tmp = 0.0;
     169
    167170    return p_psCalcScaleFactorsFit(n);
    168171
    169172    printf("Should not get here\n");
    170     float *scalingFactors = (float *) psAlloc(n * sizeof(float));
    171     for (i=0;i<n;i++) {
    172         //          scalingFactors[i] = ((2.0 * (float) i) / ((float) (n-1))) - 1.0;
    173         tmp = (float) (n - i);
    174         tmp = (M_PI * (tmp - 0.5)) / ((float) n);
     173    float *scalingFactors = (float *)psAlloc(n * sizeof(float));
     174
     175    for (i = 0; i < n; i++) {
     176        // scalingFactors[i] = ((2.0 * (float) i)
     177        // / ((float) (n-1))) - 1.0;
     178        tmp = (float)(n - i);
     179        tmp = (M_PI * (tmp - 0.5)) / ((float)n);
    175180        scalingFactors[i] = cos(tmp);
    176181    }
    177     return(scalingFactors);
     182    return (scalingFactors);
    178183}
    179184
     
    184189    int j = 0;
    185190
    186     chebPolys = (psPolynomial1D **) psAlloc(maxChebyPoly *
    187                                             sizeof(psPolynomial1D *));
    188     for (i=0;i<maxChebyPoly;i++) {
    189         chebPolys[i] = psPolynomial1DAlloc(i+1);
    190     }
    191 
    192     // Create the Chebyshev polynomials.  Polynomial i has i-th order.
     191    chebPolys = (psPolynomial1D **) psAlloc(maxChebyPoly * sizeof(psPolynomial1D *));
     192    for (i = 0; i < maxChebyPoly; i++) {
     193        chebPolys[i] = psPolynomial1DAlloc(i + 1);
     194    }
     195
     196    // Create the Chebyshev polynomials.
     197    // Polynomial i has i-th order.
    193198    chebPolys[0]->coeff[0] = 1;
    194199    chebPolys[1]->coeff[1] = 1;
    195     for (i=2;i<maxChebyPoly;i++) {
    196         for (j=0;j<chebPolys[i-1]->n;j++) {
    197             chebPolys[i]->coeff[j+1] = 2 * chebPolys[i-1]->coeff[j];
    198         }
    199         for (j=0;j<chebPolys[i-2]->n;j++) {
    200             chebPolys[i]->coeff[j]-= chebPolys[i-2]->coeff[j];
    201         }
    202     }
    203 
    204     return(chebPolys);
    205 }
    206 
     200    for (i = 2; i < maxChebyPoly; i++) {
     201        for (j = 0; j < chebPolys[i - 1]->n; j++) {
     202            chebPolys[i]->coeff[j + 1] = 2 * chebPolys[i - 1]->coeff[j];
     203        }
     204        for (j = 0; j < chebPolys[i - 2]->n; j++) {
     205            chebPolys[i]->coeff[j] -= chebPolys[i - 2]->coeff[j];
     206        }
     207    }
     208
     209    return (chebPolys);
     210}
    207211
    208212/*****************************************************************************
     
    220224        over all pixels (x,y) in the image.
    221225 *****************************************************************************/
    222 psPolynomial2D *
    223 psImageFitPolynomial(const psImage *input,
    224                      psPolynomial2D *coeffs)
     226psPolynomial2D *psImageFitPolynomial(const psImage * input, psPolynomial2D * coeffs)
    225227{
    226228    int x = 0;
     
    235237    float tmp = 0.0;
    236238
    237     // Create the sums[][] data structure.  This will hold the LHS of equation
    238     // 29 in the ADD: sums[k][l] = SUM { image(x,y) * Tk(x) * Tl(y) }
    239     sums = (float **) psAlloc(coeffs->nX * sizeof(float *));
    240     for (i=0;i<coeffs->nX;i++) {
    241         sums[i] = (float *) psAlloc(coeffs->nY * sizeof(float));
    242     }
    243 
    244     // We scale the pixel positions to values between -1.0 and 1.0
     239    // Create the sums[][] data structure.  This
     240    // will hold the LHS of
     241    // equation
     242    // 29 in the ADD: sums[k][l] = SUM {
     243    // image(x,y) * Tk(x) * Tl(y) }
     244    sums = (float **)psAlloc(coeffs->nX * sizeof(float *));
     245    for (i = 0; i < coeffs->nX; i++) {
     246        sums[i] = (float *)psAlloc(coeffs->nY * sizeof(float));
     247    }
     248
     249    // We scale the pixel positions to values
     250    // between -1.0 and 1.0
    245251    rScalingFactors = p_psCalcScaleFactorsFit(input->numRows);
    246252    cScalingFactors = p_psCalcScaleFactorsFit(input->numCols);
    247253
    248     // Determine how many Chebyshev polynomials are needed, then create them.
     254    // Determine how many Chebyshev polynomials
     255    // are needed, then create them.
    249256    maxChebyPoly = coeffs->nX;
    250257    if (coeffs->nY > coeffs->nX) {
     
    254261
    255262    // Sanity check for the Chebyshevs.
    256     for (i=0;i<coeffs->nX;i++) {
    257         for (j=0;j<coeffs->nY;j++) {
     263    for (i = 0; i < coeffs->nX; i++) {
     264        for (j = 0; j < coeffs->nY; j++) {
    258265            tmp = 0.0;
    259             for (x=0;x<input->numRows;x++) {
    260                 tmp+= psPolynomial1DEval(rScalingFactors[x], chebPolys[i]) *
    261                       psPolynomial1DEval(rScalingFactors[x], chebPolys[j]);
     266            for (x = 0; x < input->numRows; x++) {
     267                tmp +=
     268                    psPolynomial1DEval
     269                    (rScalingFactors[x], chebPolys[i]) * psPolynomial1DEval(rScalingFactors[x], chebPolys[j]);
    262270
    263271            }
    264             //printf("SUM(Cheby(%d) * Cheby(%d)) is %f\n", i, j, tmp);
     272            // printf("SUM(Cheby(%d) * Cheby(%d))
     273            // is %f\n", i, j, tmp);
    265274        }
    266275    }
    267276
    268277    // Compute the sums[][] data structure.
    269     for (i=0;i<coeffs->nX;i++) {
    270         for (j=0;j<coeffs->nY;j++) {
     278    for (i = 0; i < coeffs->nX; i++) {
     279        for (j = 0; j < coeffs->nY; j++) {
    271280            sums[i][j] = 0.0;
    272             for (x=0;x<input->numRows;x++) {
    273                 for (y=0;y<input->numCols;y++) {
    274                     sums[i][j]+= input->data.F32[x][y] *
    275                                  psPolynomial1DEval(rScalingFactors[x], chebPolys[i]) *
    276                                  psPolynomial1DEval(cScalingFactors[y], chebPolys[j]);
     281            for (x = 0; x < input->numRows; x++) {
     282                for (y = 0; y < input->numCols; y++) {
     283                    sums[i][j] +=
     284                        input->data.F32[x][y] *
     285                        psPolynomial1DEval
     286                        (rScalingFactors[x],
     287                         chebPolys[i]) * psPolynomial1DEval(cScalingFactors[y], chebPolys[j]);
    277288                }
    278289            }
     
    280291    }
    281292
    282     for (i=0;i<coeffs->nX;i++) {
    283         for (j=0;j<coeffs->nY;j++) {
     293    for (i = 0; i < coeffs->nX; i++) {
     294        for (j = 0; j < coeffs->nY; j++) {
    284295            coeffs->coeff[i][j] = sums[i][j];
    285             coeffs->coeff[i][j]/= (float) (input->numRows * input->numCols);
     296            coeffs->coeff[i][j] /= (float)(input->numRows * input->numCols);
    286297
    287298            if ((i != 0) && (j != 0)) {
    288                 coeffs->coeff[i][j]*= 4.0;
    289             } else
    290                 if ((i == 0) && (j == 0)) {
    291                     coeffs->coeff[i][j]*= 1.0;
    292                 } else {
    293                     coeffs->coeff[i][j]*= 2.0;
    294                 }
    295         }
    296     }
    297 
    298     // Free the Chebyshev polynomials that were created in this routine.
    299     for (i=0;i<maxChebyPoly;i++) {
     299                coeffs->coeff[i][j] *= 4.0;
     300            } else if ((i == 0) && (j == 0)) {
     301                coeffs->coeff[i][j] *= 1.0;
     302            } else {
     303                coeffs->coeff[i][j] *= 2.0;
     304            }
     305        }
     306    }
     307
     308    // Free the Chebyshev polynomials that were
     309    // created in this routine.
     310    for (i = 0; i < maxChebyPoly; i++) {
    300311        psFree(chebPolys[i]);
    301312    }
     
    303314
    304315    // Free some data
    305     for (i=0;i<coeffs->nX;i++) {
     316    for (i = 0; i < coeffs->nX; i++) {
    306317        psFree(sums[i]);
    307318    }
     
    310321    psFree(rScalingFactors);
    311322
    312     return(coeffs);
     323    return (coeffs);
    313324}
    314325
     
    316327 
    317328 *****************************************************************************/
    318 int
    319 psImageEvalPolynomial(const psImage *input,
    320                       const psPolynomial2D *coeffs)
     329int psImageEvalPolynomial(const psImage * input, const psPolynomial2D * coeffs)
    321330{
    322331    int x = 0;
     
    331340    float polySum = 0.0;
    332341
    333     // Create the sums[][] data structure.  This will hold the LHS of equation
    334     // 29 in the ADD: sums[k][l] = SUM { image(x,y) * Tk(x) * Tl(y) }
    335     sums = (float **) psAlloc(coeffs->nX * sizeof(float *));
    336     for (i=0;i<coeffs->nX;i++) {
    337         sums[i] = (float *) psAlloc(coeffs->nY * sizeof(float));
    338     }
    339     for (i=0;i<coeffs->nX;i++) {
    340         for (j=0;j<coeffs->nY;j++) {
     342    // Create the sums[][] data structure.  This
     343    // will hold the LHS of
     344    // equation
     345    // 29 in the ADD: sums[k][l] = SUM {
     346    // image(x,y) * Tk(x) * Tl(y) }
     347    sums = (float **)psAlloc(coeffs->nX * sizeof(float *));
     348    for (i = 0; i < coeffs->nX; i++) {
     349        sums[i] = (float *)psAlloc(coeffs->nY * sizeof(float));
     350    }
     351    for (i = 0; i < coeffs->nX; i++) {
     352        for (j = 0; j < coeffs->nY; j++) {
    341353            sums[i][j] = 0.0;
    342354        }
    343355    }
    344356
    345     // We scale the pixel positions to values between -1.0 and 1.0
     357    // We scale the pixel positions to values
     358    // between -1.0 and 1.0
    346359    rScalingFactors = p_psCalcScaleFactorsEval(input->numRows);
    347360    cScalingFactors = p_psCalcScaleFactorsEval(input->numCols);
    348361
    349     // Determine how many Chebyshev polynomials are needed, then create them.
     362    // Determine how many Chebyshev polynomials
     363    // are needed, then create them.
    350364    maxChebyPoly = coeffs->nX;
    351365    if (coeffs->nY > coeffs->nX) {
     
    355369    chebPolys = p_psCreateChebyshevPolys(maxChebyPoly);
    356370
    357     for (x=0;x<input->numRows;x++) {
    358         for (y=0;y<input->numCols;y++) {
     371    for (x = 0; x < input->numRows; x++) {
     372        for (y = 0; y < input->numCols; y++) {
    359373            polySum = 0.0;
    360             for (i=0;i<coeffs->nX;i++) {
    361                 for (j=0;j<coeffs->nY;j++) {
    362                     polySum+= psPolynomial1DEval(rScalingFactors[x], chebPolys[i]) *
    363                               psPolynomial1DEval(cScalingFactors[y], chebPolys[j]) *
    364                               coeffs->coeff[i][j];
     374            for (i = 0; i < coeffs->nX; i++) {
     375                for (j = 0; j < coeffs->nY; j++) {
     376                    polySum +=
     377                        psPolynomial1DEval
     378                        (rScalingFactors[x],
     379                         chebPolys[i]) *
     380                        psPolynomial1DEval(cScalingFactors[y], chebPolys[j]) * coeffs->coeff[i][j];
    365381
    366382                }
     
    370386    }
    371387
    372     // Free the Chebyshev polynomials that were created in this routine.
    373     for (i=0;i<maxChebyPoly;i++) {
     388    // Free the Chebyshev polynomials that were
     389    // created in this routine.
     390    for (i = 0; i < maxChebyPoly; i++) {
    374391        psFree(chebPolys[i]);
    375392    }
     
    377394
    378395    // Free some data
    379     for (i=0;i<coeffs->nX;i++) {
     396    for (i = 0; i < coeffs->nX; i++) {
    380397        psFree(sums[i]);
    381398    }
     
    384401    psFree(rScalingFactors);
    385402
    386     return(0);
    387 }
     403    return (0);
     404}
  • trunk/psLib/src/image/psImageStats.h

    r1374 r1407  
     1
    12/** @file psImageStats.h
    23*  \brief Routines for calculating statistics on images.
     
    910*  @author George Gusciora, MHPCC
    1011*
    11 *  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
    12 *  @date $Date: 2004-08-04 00:55:17 $
     12*  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
     13*  @date $Date: 2004-08-07 00:06:06 $
    1314*
    1415*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1516*/
    1617#if !defined(PS_IMAGE_STATS_H)
    17 #define PS_IMAGE_STATS_H
     18#    define PS_IMAGE_STATS_H
    1819
    19 
    20 #include "psType.h"
    21 #include "psVector.h"
    22 #include "psImage.h"
    23 #include "psStats.h"
    24 #include "psFunctions.h"
     20#    include "psType.h"
     21#    include "psVector.h"
     22#    include "psImage.h"
     23#    include "psStats.h"
     24#    include "psFunctions.h"
    2525
    2626/// @addtogroup ImageStats
     
    2828
    2929/// This routine must determine the various statistics for the image.
    30 psStats *psImageStats( psStats *stats,  ///< defines statistics to be calculated
    31                        psImage *in,     ///< image (or subimage) to calculate stats
    32                        psImage *mask,   ///< mask data for image (NULL ok)
    33                        int maskVal );   ///< mask Mask for mask
    34                        
    35                        
    36 psHistogram *psImageHistogram( psHistogram *out,   ///< input histogram description & target
    37                                psImage *in,        ///< Image data to be histogramed.
    38                                psImage *mask,      ///< mask data for image (NULL ok)
    39                                unsigned int maskVal );  ///< mask Mask for mask
    40                                
     30psStats *psImageStats(psStats * stats,  // /< defines statistics to be calculated
     31                      psImage * in,     // /< image (or subimage) to calculate stats
     32                      psImage * mask,   // /< mask data for image (NULL ok)
     33                      int maskVal);     // /< mask Mask for mask
     34
     35psHistogram *psImageHistogram(psHistogram * out,        // /< input histogram description & target
     36                              psImage * in,     // /< Image data to be histogramed.
     37                              psImage * mask,   // /< mask data for image (NULL ok)
     38                              unsigned int maskVal);    // /< mask Mask for mask
     39
    4140/// Fit a 2-D polynomial surface to an image.
    42 psPolynomial2D *
    43 psImageFitPolynomial( const psImage *input,  ///< image to fit
    44                       psPolynomial2D *coeffs ///< coefficient structure carries in desired terms & target
    45                     );
    46                    
     41psPolynomial2D *psImageFitPolynomial(const psImage * input,     // /< image to fit
     42                                     psPolynomial2D * coeffs    // /< coefficient structure carries in
     43                                     // desired terms & target
     44                                    );
     45
    4746/// Evaluate a 2-D polynomial surface to image pixels.
    48 int
    49 psImageEvalPolynomial( const psImage *input,  ///< image to fit
    50                        const psPolynomial2D *coeffs ///< coefficient structure carries in desired terms
    51                      );
    52                      
     47int psImageEvalPolynomial(const psImage * input,        // /< image to fit
     48                          const psPolynomial2D * coeffs // /< coefficient structure carries in desired terms
     49                         );
     50
    5351/// @}
    5452
Note: See TracChangeset for help on using the changeset viewer.