/** @file  psType.h
*
*  @brief Contains support for basic types
*
*  This file defines common datatypes used throughout psLib.
*
*  @ingroup DataContainer
*
*  @author Robert DeSonia, MHPCC
*  @author Ross Harman, MHPCC
*
*  @version $Revision: 1.31 $ $Name: not supported by cvs2svn $
*  @date $Date: 2005-03-07 20:58:50 $
*
*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
*/

#ifndef PS_TYPE_H
#define PS_TYPE_H

#include <complex.h>
#include <stdint.h>
#include <float.h>
#include <stdbool.h>

/// @addtogroup DataContainer
/// @{

/******************************************************************************/

/*  TYPE DEFINITIONS                                                          */

/******************************************************************************/

/** Basic data types used by the containers.
 *
 * The basic types of the primitives used by psLib are defined within this enum. This enum is in turn used by
 * the psType struct.
 *
 */

typedef uint8_t psU8;                  ///< 8-bit unsigned int
typedef uint16_t psU16;                ///< 16-bit unsigned int
typedef uint32_t psU32;                ///< 32-bit unsigned int
typedef uint64_t psU64;                ///< 64-bit unsigned int
typedef int8_t psS8;                   ///< 8-bit signed int
typedef int16_t psS16;                 ///< 16-bit signed int
typedef int32_t psS32;                 ///< 32-bit signed int
typedef int64_t psS64;                 ///< 64-bit signed int
typedef float psF32;                   ///< 32-bit floating point
typedef double psF64;                  ///< 64-bit floating point

#ifdef SWIG
typedef struct
{
    float re, im;
}
psC32;
typedef struct
{
    double re,im;
}
psC64;
#else
typedef float _Complex psC32;          ///< complex with 32-bit floating point Real and Imagary numbers
typedef double _Complex psC64;         ///< complex with 64-bit floating point Real and Imagary numbers
#endif

typedef void* psPtr;                   ///< void pointer
typedef bool psBool;                   ///< boolean value

typedef enum {
    PS_TYPE_S8   = 0x0101,             ///< Character.
    PS_TYPE_S16  = 0x0102,             ///< Short integer.
    PS_TYPE_S32  = 0x0104,             ///< Integer.
    PS_TYPE_S64  = 0x0108,             ///< Long integer.
    PS_TYPE_U8   = 0x0301,             ///< Unsigned character.
    PS_TYPE_U16  = 0x0302,             ///< Unsigned psS16 integer.
    PS_TYPE_U32  = 0x0304,             ///< Unsigned integer.
    PS_TYPE_U64  = 0x0308,             ///< Unsigned psS64 integer.
    PS_TYPE_F32  = 0x0404,             ///< Single-precision Floating point.
    PS_TYPE_F64  = 0x0408,             ///< Double-precision floating point.
    PS_TYPE_C32  = 0x0808,             ///< Complex numbers consisting of single-precision floating point.
    PS_TYPE_C64  = 0x0810,             ///< Complex numbers consisting of double-precision floating point.
    PS_TYPE_BOOL = 0x1301,             ///< Boolean.
    PS_TYPE_PTR  = 0x0000              ///< Something else that's not supported for arithmetic.
} psElemType;

#define PS_TYPE_MASK PS_TYPE_U8        /**< the psElemType to use for mask image */
#define PS_TYPE_MASK_DATA U8           /**< the data member to use for mask image */
#define PS_TYPE_MASK_NAME "psU8"       /**< the data type for mask as a string */

typedef psU8 psMaskType;               ///< the C datatype for a mask image
typedef psBool psBOOL;                 ///< allow psBOOL to be used instead of psBool (for macros)

#define PS_MIN_S8        INT8_MIN      /**< minimum valid psS8 value */
#define PS_MIN_S16       INT16_MIN     /**< minimum valid psS16 value */
#define PS_MIN_S32       INT32_MIN     /**< minimum valid psS32 value */
#define PS_MIN_S64       INT64_MIN     /**< minimum valid psS64 value */
#define PS_MIN_U8        0             /**< minimum valid psU8 value */
#define PS_MIN_U16       0             /**< minimum valid psU16 value */
#define PS_MIN_U32       0             /**< minimum valid psU32 value */
#define PS_MIN_U64       0             /**< minimum valid psU64 value */
#define PS_MIN_F32       -FLT_MAX      /**< minimum valid psF32 value */
#define PS_MIN_F64       -DBL_MAX      /**< minimum valid psF64 value */
#define PS_MIN_C32       -FLT_MAX      /**< minimum valid real or imaginary psC32 value */
#define PS_MIN_C64       -DBL_MAX      /**< minimum valid real or imaginary psC32 value */

#define PS_MAX_S8        INT8_MAX      /**< maximum valid psS8 value */
#define PS_MAX_S16       INT16_MAX     /**< maximum valid psS16 value */
#define PS_MAX_S32       INT32_MAX     /**< maximum valid psS32 value */
#define PS_MAX_S64       INT64_MAX     /**< maximum valid psS64 value */
#define PS_MAX_U8        UINT8_MAX     /**< maximum valid psU8 value */
#define PS_MAX_U16       UINT16_MAX    /**< maximum valid psU16 value */
#define PS_MAX_U32       UINT32_MAX    /**< maximum valid psU32 value */
#define PS_MAX_U64       UINT64_MAX    /**< maximum valid psU64 value */
#define PS_MAX_F32       FLT_MAX       /**< maximum valid psF32 value */
#define PS_MAX_F64       DBL_MAX       /**< maximum valid psF64 value */
#define PS_MAX_C32       FLT_MAX       /**< maximum valid real or imaginary psC32 value */
#define PS_MAX_C64       DBL_MAX       /**< maximum valid real or imaginary psC32 value */

#define PS_TYPE_BOOL_NAME "psBool"
#define PS_TYPE_S8_NAME   "psS8"
#define PS_TYPE_S16_NAME  "psS16"
#define PS_TYPE_S32_NAME  "psS32"
#define PS_TYPE_S64_NAME  "psS64"
#define PS_TYPE_U8_NAME   "psU8"
#define PS_TYPE_U16_NAME  "psU16"
#define PS_TYPE_U32_NAME  "psU32"
#define PS_TYPE_U64_NAME  "psU64"
#define PS_TYPE_F32_NAME  "psF32"
#define PS_TYPE_F64_NAME  "psF64"
#define PS_TYPE_C32_NAME  "psC32"
#define PS_TYPE_C64_NAME  "psC64"
#define PS_TYPE_PTR_NAME  "psPtr"

#define PS_TYPE_NAME(value,type) \
switch(type) { \
case PS_TYPE_BOOL: \
    value = PS_TYPE_BOOL_NAME; \
    break; \
case PS_TYPE_S8: \
    value = PS_TYPE_S8_NAME; \
    break; \
case PS_TYPE_S16: \
    value = PS_TYPE_S16_NAME; \
    break; \
case PS_TYPE_S32: \
    value = PS_TYPE_S32_NAME; \
    break; \
case PS_TYPE_S64: \
    value = PS_TYPE_S64_NAME; \
    break; \
case PS_TYPE_U8: \
    value = PS_TYPE_U8_NAME; \
    break; \
case PS_TYPE_U16: \
    value = PS_TYPE_U16_NAME; \
    break; \
case PS_TYPE_U32: \
    value = PS_TYPE_U32_NAME; \
    break; \
case PS_TYPE_U64: \
    value = PS_TYPE_U64_NAME; \
    break; \
case PS_TYPE_F32: \
    value = PS_TYPE_F32_NAME; \
    break; \
case PS_TYPE_F64: \
    value = PS_TYPE_F64_NAME; \
    break; \
case PS_TYPE_C32: \
    value = PS_TYPE_C32_NAME; \
    break; \
case PS_TYPE_C64: \
    value = PS_TYPE_C64_NAME; \
    break; \
case PS_TYPE_PTR: \
    value = PS_TYPE_PTR_NAME; \
    break; \
default: \
    value = "unknown"; \
};

/// Macro to get the bad pixel reason code (stored as part of mask value)
#define PS_BADPIXEL_BITMASK 0x0f
#define PS_GET_BADPIXEL(maskValue) (maskValue & PS_BADPIXEL_BITMASK)

#define PS_IS_BADPIXEL(maskValue) (PS_GET_BADPIXEL(maskValue) != 0)

/// Macro to apply a bad pixel reason code to mask image
#define PS_SET_BADPIXEL(maskValue, reasonCode) \
{ \
    maskValue = (psMaskType)((reasonCode & PS_BADPIXEL_BITMASK) | (maskValue & ~PS_BADPIXEL_BITMASK)); \
}

/// Macro to determine if the psElemType is an integer.
#define PS_IS_PSELEMTYPE_INT(x) ((x & 0x100) == 0x100)
/// Macro to determine if the psElemType is unsigned.
#define PS_IS_PSELEMTYPE_UNSIGNED(x) ((x & 0x200) == 0x200)
/// Macro to determine if the psElemType is a real (non-complex) floating-point type.
#define PS_IS_PSELEMTYPE_REAL(x) ((x & 0x400) == 0x400)
/// Macro to determine if the psElemType is complex number type.
#define PS_IS_PSELEMTYPE_COMPLEX(x) ((x & 0x800) == 0x800)
/// Macro to determine if the psElemType is boolean type.
#define PS_IS_PSELEMTYPE_BOOL(x) ((x & 0x1000) == 0x1000)
/// Macro to determine the storage size, in bytes, of the psElemType.
#define PSELEMTYPE_SIZEOF(x) ( (x==(PS_TYPE_PTR&0xFFFF)) ? sizeof(psPtr) :(x & 0xFF) )

/** Dimensions of a data type.
 *
 * The dimensions of containers used by psLib are defined within this enum. This enum is used by the psType
struct. *
 */
typedef enum {
    PS_DIMEN_SCALAR,            ///< Scalar.
    PS_DIMEN_VECTOR,            ///< Vector.
    PS_DIMEN_TRANSV,            ///< Transposed vector.
    PS_DIMEN_IMAGE,             ///< Image.
    PS_DIMEN_OTHER              ///< Something else that's not supported for arithmetic.
} psDimen;

/** The type of a data type.
 *
 * All psLib complex types consist of primitive components. This struct provides the description of those
 * primitives.
 *
 */
typedef struct
{
    psElemType type;            ///< Primitive type.
    psDimen dimen;              ///< Dimensionality.
}
psType;

/** The type of a basic data type
 *
 *  All psLib complex types consist of primitive components.  This structure provides the ability to cast
 *  an unknown data structure to safely test the underlining data type.
 *
 */
typedef struct
{
    psType  type;              ///< Data type information
}
psMath;

/// @}

#endif
