psImage.h

Go to the documentation of this file.
00001 /* @file  psImage.h
00002  * @brief Basic image definitions and operations
00003  *
00004  * This file defines the basic type for an image struct and functions useful
00005  * in manupulating images.
00006  *
00007  * @author Robert DeSonia, MHPCC
00008  * @author Ross Harman, MHPCC
00009  *
00010  * @version $Revision: 1.84 $ $Name:  $
00011  * @date $Date: 2007/01/23 22:47:23 $
00012  * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
00013  */
00014 
00015 #ifndef PS_IMAGE_H
00016 #define PS_IMAGE_H
00017 
00018 /// @addtogroup MathOps Mathematical Operations
00019 /// @{
00020 
00021 #include <complex.h>
00022 #include <stdio.h>
00023 #include "psType.h"
00024 #include "psArray.h"
00025 #include "psConstants.h"
00026 
00027 /** enumeration of options in interpolation
00028  *
00029  */
00030 typedef enum {
00031     PS_INTERPOLATE_FLAT,               ///< 'flat' interpolation (nearest pixel)
00032     PS_INTERPOLATE_BILINEAR,           ///< bi-linear interpolation
00033     PS_INTERPOLATE_BICUBE,             ///< bi-cubic interpolation with 3x3 region (EAM)
00034     PS_INTERPOLATE_GAUSS,              ///< bi-cubic interpolation with 3x3 region (EAM)
00035     PS_INTERPOLATE_LANCZOS2,           ///< Sinc interpolation with 4x4 pixel kernel
00036     PS_INTERPOLATE_LANCZOS3,           ///< Sinc interpolation with 6x6 pixel kernel
00037     PS_INTERPOLATE_LANCZOS4,           ///< Sinc interpolation with 8x8 pixel kernel
00038     PS_INTERPOLATE_BILINEAR_VARIANCE,  ///< Variance version of PS_INTERPOLATE_BILINEAR
00039     PS_INTERPOLATE_LANCZOS2_VARIANCE,  ///< Variance version of PS_INTERPOLATE_LANCZOS2
00040     PS_INTERPOLATE_LANCZOS3_VARIANCE,  ///< Variance version of PS_INTERPOLATE_LANCZOS3
00041     PS_INTERPOLATE_LANCZOS4_VARIANCE   ///< Variance version of PS_INTERPOLATE_LANCZOS4
00042     //    PS_INTERPOLATE_NUM_MODES           ///< enum end-marker; does not coorespond to a interpolation mode
00043 } psImageInterpolateMode;
00044 
00045 /** Basic image data structure.
00046  *
00047  * Struct for maintaining image data of varying types. It also contains
00048  * information about image size, parent images and children images.
00049  *
00050  */
00051 typedef struct psImage
00052 {
00053     const psMathType type;             ///< Image data type and dimension.
00054     const int numCols;                 ///< Number of columns in image
00055     const int numRows;                 ///< Number of rows in image.
00056     int col0;                          ///< Column position relative to parent.
00057     int row0;                          ///< Row position relative to parent.
00058 
00059     union {
00060         psS8**  S8;                    ///< Signed 8-bit integer data.
00061         psS16** S16;                   ///< Signed 16-bit integer data.
00062         psS32** S32;                   ///< Signed 32-bit integer data.
00063         psS64** S64;                   ///< Signed 64-bit integer data.
00064         psU8**  U8;                    ///< Unsigned 8-bit integer data.
00065         psU16** U16;                   ///< Unsigned 16-bit integer data.
00066         psU32** U32;                   ///< Unsigned 32-bit integer data.
00067         psU64** U64;                   ///< Unsigned 64-bit integer data.
00068         psF32** F32;                   ///< Single-precision float data.
00069         psF64** F64;                   ///< Double-precision float data.
00070         psC32** C32;                   ///< Single-precision complex data.
00071         psC64** C64;                   ///< Double-precision complex data.
00072         psPtr*  V;                     ///< Pointer to data.
00073     } data;                            ///< Union for data types.
00074     const struct psImage* parent;      ///< Parent, if a subimage.
00075     psPtr p_rawDataBuffer;             ///< Raw data buffer for Allocating/Freeing Images; private
00076     psArray* children;                 ///< Children of this region.
00077     void *lock;                        ///< Optional lock for thread safety
00078 }
00079 psImage;
00080 
00081 #define P_PSIMAGE_SET_NUMCOLS(img,nc) {*(int*)&img->numCols = nc;}
00082 #define P_PSIMAGE_SET_NUMROWS(img,nr) {*(int*)&img->numRows = nr;}
00083 #define P_PSIMAGE_SET_TYPE(img,t) {*(psMathType*)&img->type = t;}
00084 
00085 /** Create an image of the specified size and type.
00086  *
00087  * Uses psLib memory allocation functions to create an image struct of the
00088  * specified size and type.
00089  *
00090  * @return psImage* : Pointer to psImage.
00091  *
00092  */
00093 psImage* psImageAlloc(
00094     int numCols,                       ///< Number of columns in image.
00095     int numRows,                       ///< Number of rows in image.
00096     psElemType type                    ///< Type of data for image.
00097 )
00098 ;
00099 
00100 /** Checks the type of a particular pointer.
00101  *
00102  *  Uses the appropriate deallocation function in psMemBlock to check the ptr datatype.
00103  *
00104  *  @return bool:       True if the pointer matches a psImage structure, false otherwise.
00105  */
00106 bool psMemCheckImage(
00107     psPtr ptr                          ///< the pointer whose type to check
00108 );
00109 
00110 /** Initializes the image with the given value.
00111  *
00112  *  The input data is cast to match the image datatype.
00113  *
00114  *  @return bool:       True on success, otherwise false.
00115  */
00116 bool psImageInit(
00117     psImage *image,                    ///< the image to be initialized
00118     ...                                ///< Variable argument list for initialization
00119 );
00120 
00121 /** Sets the value of the image at the specified x,y position to value.
00122  *
00123  *  A negative value for the x or y positions means index from the end.
00124  *
00125  *  @return bool:       True on success, otherwise false.
00126  */
00127 bool psImageSet(
00128     psImage *image,                     ///< the image to set
00129     int x,                             ///< x-position
00130     int y,                             ///< y-position
00131     double complex value               ///< specified value to set
00132 );
00133 
00134 /** Returns the value of the image at the specified x,y position.
00135  *
00136  *  A negative value for the x or y positions means index from the end.
00137  *
00138  *  @return complex: The value at the specified x,y position.
00139  */
00140 double complex psImageGet(
00141     const psImage *image,              ///< the image from which to get
00142     int x,                             ///< x-position
00143     int y                              ///< y-position
00144 );
00145 
00146 /** Resize a given image to the given size/type.
00147  *
00148  *  @return psImage* Resized psImage.
00149  */
00150 psImage* psImageRecycle(
00151     psImage* old,                      ///< the psImage to recycle by resizing image buffer
00152     int numCols,                       ///< the desired number of columns in image
00153     int numRows,                       ///< the desired number of rows in image
00154     const psElemType type              ///< the desired datatype of the image
00155 );
00156 
00157 /** Copy an image to a new buffer
00158  *
00159  *  @return True if image copied or false if error
00160  */
00161 bool p_psImageCopyToRawBuffer(
00162     void* buffer,                      ///< the buffer used to copy the image
00163     const psImage* input,              ///< the input image to be copied
00164     psElemType type                    ///< the datatype of the image to be copied
00165 );
00166 
00167 /** Frees all children of a psImage.
00168  *
00169  *  @return int      Number of children freed.
00170  */
00171 int psImageFreeChildren(
00172     psImage* image                     ///< psImage in which all children shall be deallocated
00173 );
00174 
00175 /** get an element of an image as a psF64.
00176  *
00177  *  @return psF64   pixel value at specified location
00178  */
00179 psF64 p_psImageGetElementF64(
00180     psImage* image,                    ///< input image
00181     int col,                           ///< pixel column
00182     int row                            ///< pixel row
00183 );
00184 
00185 /** print image pixel values.
00186  *
00187  *  @return bool    TRUE is successful, otherwise FALSE.
00188  */
00189 bool p_psImagePrint(
00190     int fd,                            ///< Destination file descriptor
00191     psImage *a,                        ///< image to print
00192     char *name                         ///< name of the image (for title)
00193 );
00194 
00195 /** Interpolate image pixel value given floating point coordinates.
00196  *
00197  *  @return complex    Pixel value interpolated from image or unexposedValue if
00198  *                   given x,y doesn't coorespond to a valid image location
00199  */
00200 double complex psImagePixelInterpolate(
00201     const psImage* input,              ///< input image for interpolation
00202     float x,                           ///< column location to derive value of
00203     float y,                           ///< row location ot derive value of
00204     const psImage* mask,               ///< if not NULL, the mask of the input image
00205     psMaskType maskVal,                ///< the mask value
00206     double complex unexposedValue,            ///< return value if x,y location is not in image.
00207     psImageInterpolateMode mode        ///< interpolation mode
00208 );
00209 
00210 #define PIXEL_INTERPOLATE_FCN_PROTOTYPE(SUFFIX, RETURNTYPE) \
00211 inline RETURNTYPE p_psImagePixelInterpolate##SUFFIX( \
00212         const psImage* input,          /**< input image for interpolation */ \
00213         float x,                       /**< column location to derive value of */ \
00214         float y,                       /**< row location ot derive value of */ \
00215         const psImage* mask,           /**< if not NULL, the mask of the input image */ \
00216         psU32 maskVal,                 /**< the mask value */ \
00217         RETURNTYPE unexposedValue      /**< return value if x,y location is not in image. */ \
00218                                                    );
00219 
00220 #define PIXEL_INTERPOLATE_FCNS(MODE) \
00221 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_U8,psF64)  \
00222 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_U16,psF64) \
00223 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_U32,psF64) \
00224 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_U64,psF64) \
00225 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_S8,psF64)  \
00226 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_S16,psF64) \
00227 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_S32,psF64) \
00228 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_S64,psF64) \
00229 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_F32,psF64) \
00230 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_F64,psF64) \
00231 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_C32,psC64) \
00232 PIXEL_INTERPOLATE_FCN_PROTOTYPE(MODE##_C64,psC64)
00233 
00234 #ifndef SWIG
00235 PIXEL_INTERPOLATE_FCNS(FLAT)
00236 PIXEL_INTERPOLATE_FCNS(BILINEAR)
00237 PIXEL_INTERPOLATE_FCNS(BILINEAR_VARIANCE)
00238 PIXEL_INTERPOLATE_FCNS(BICUBE)
00239 #endif // ! SWIG
00240 
00241 #undef PIXEL_INTERPOLATE_FCN_PROTOTYPE
00242 #undef PIXEL_INTERPOLATE_FCNS
00243 
00244 /*****************************************************************************
00245     PS_IMAGE macros:
00246 *****************************************************************************/
00247 #define PS_ASSERT_IMAGE_NON_NULL(NAME, RVAL) PS_ASSERT_GENERAL_IMAGE_NON_NULL(NAME, return RVAL)
00248 #define PS_ASSERT_GENERAL_IMAGE_NON_NULL(NAME, CLEANUP) \
00249 if ((NAME) == NULL || (NAME)->data.V == NULL) { \
00250     psError(PS_ERR_BAD_PARAMETER_NULL, true, \
00251             "Unallowable operation: psImage %s or its data is NULL.", \
00252             #NAME); \
00253     CLEANUP; \
00254 }
00255 
00256 #define PS_ASSERT_IMAGE_NON_EMPTY(NAME, RVAL) PS_ASSERT_GENERAL_IMAGE_NON_EMPTY(NAME, return RVAL)
00257 #define PS_ASSERT_GENERAL_IMAGE_NON_EMPTY(NAME, CLEANUP) \
00258 if ((NAME)->numCols < 1 || (NAME)->numRows < 1) { \
00259     psError(PS_ERR_BAD_PARAMETER_SIZE, true, \
00260             "Unallowable operation: psImage %s has zero rows or columns (%dx%d).", \
00261             #NAME, (NAME)->numCols, (NAME)->numRows); \
00262     CLEANUP; \
00263 }
00264 
00265 #define PS_ASSERT_IMAGE_TYPE(NAME, TYPE, RVAL) \
00266 if ((NAME)->type.type != TYPE) { \
00267     psError(PS_ERR_BAD_PARAMETER_TYPE, true, \
00268             "Unallowable operation: psImage %s has incorrect type.", \
00269             #NAME); \
00270     return(RVAL); \
00271 }
00272 
00273 #define PS_ASSERT_IMAGES_SIZE_EQUAL(NAME1, NAME2, RVAL) \
00274 if (((NAME1)->numCols != (NAME2)->numCols) || \
00275         ((NAME1)->numRows != (NAME2)->numRows)) { \
00276     psError(PS_ERR_BAD_PARAMETER_SIZE, true, \
00277             "Unallowable operation: psImages %s and %s are not the same size.", \
00278             #NAME1, #NAME2); \
00279     return(RVAL); \
00280 }
00281 
00282 #define PS_ASSERT_IMAGE_SIZE(NAME1, NUM_COLS, NUM_ROWS, RVAL) \
00283 if (((NAME1)->numCols != NUM_COLS) || \
00284         ((NAME1)->numRows != NUM_ROWS)) { \
00285     psError(PS_ERR_BAD_PARAMETER_SIZE, true, \
00286             "Unallowable operation: psImages %s is not the correct size.", \
00287             #NAME1); \
00288     return(RVAL); \
00289 }
00290 
00291 #define PS_IMAGE_PRINT_F32(NAME) \
00292 printf("======== printing %s ========\n", #NAME); \
00293 for (int i = 0 ; i < (NAME)->numRows ; i++) { \
00294     for (int j = 0 ; j < (NAME)->numCols ; j++) { \
00295         printf("%.2f ", (NAME)->data.F32[i][j]); \
00296     } \
00297     printf("\n"); \
00298 }\
00299 
00300 #define PS_IMAGE_PRINT_F64(NAME) \
00301 printf("======== printing %s ========\n", #NAME); \
00302 for (int i = 0 ; i < (NAME)->numRows ; i++) { \
00303     for (int j = 0 ; j < (NAME)->numCols ; j++) { \
00304         printf("%.2f ", (NAME)->data.F64[i][j]); \
00305     } \
00306     printf("\n"); \
00307 }\
00308 
00309 /// @}
00310 #endif // PS_IMAGE_H

Generated on Fri Feb 2 22:24:35 2007 for pslib by  doxygen 1.5.1