00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00034
00035
00036 #ifndef OPENIMAGEIO_TYPEDESC_H
00037 #define OPENIMAGEIO_TYPEDESC_H
00038
00039 #ifndef NULL
00040 #define NULL 0
00041 #endif
00042
00043 #include <limits>
00044 #include <cmath>
00045 #include <cstddef>
00046
00047 #include "export.h"
00048
00049
00050 #ifdef OPENIMAGEIO_NAMESPACE
00051 namespace OPENIMAGEIO_NAMESPACE {
00052 #endif
00053
00054
00055
00056
00057
00058
00072
00073 struct DLLPUBLIC TypeDesc {
00076 enum BASETYPE { UNKNOWN, NONE,
00077 UCHAR, UINT8=UCHAR, CHAR, INT8=CHAR,
00078 USHORT, UINT16=USHORT, SHORT, INT16=SHORT,
00079 UINT, UINT32=UINT, INT, INT32=INT,
00080 ULONGLONG, UINT64=ULONGLONG, LONGLONG, INT64=LONGLONG,
00081 HALF, FLOAT, DOUBLE, STRING, PTR, LASTBASE };
00084 enum AGGREGATE { SCALAR=1, VEC2=2, VEC3=3, VEC4=4, MATRIX44=16 };
00089 enum VECSEMANTICS { NOXFORM=0, COLOR, POINT, VECTOR, NORMAL };
00090
00091 unsigned char basetype;
00092 unsigned char aggregate;
00093 unsigned char vecsemantics;
00094 unsigned char reserved;
00095 int arraylen;
00096
00099 TypeDesc (BASETYPE btype=UNKNOWN, AGGREGATE agg=SCALAR,
00100 VECSEMANTICS xform=NOXFORM)
00101 : basetype(btype), aggregate(agg), vecsemantics(xform), reserved(0),
00102 arraylen(0)
00103 { }
00104
00107 TypeDesc (BASETYPE btype, int arraylength)
00108 : basetype(btype), aggregate(SCALAR), vecsemantics(NOXFORM),
00109 reserved(0), arraylen(arraylength)
00110 { }
00111
00114 TypeDesc (BASETYPE btype, AGGREGATE agg, int arraylength)
00115 : basetype(btype), aggregate(agg), vecsemantics(NOXFORM), reserved(0),
00116 arraylen(arraylength)
00117 { }
00118
00121 TypeDesc (BASETYPE btype, AGGREGATE agg,
00122 VECSEMANTICS xform, int arraylength)
00123 : basetype(btype), aggregate(agg), vecsemantics(xform), reserved(0),
00124 arraylen(arraylength)
00125 { }
00126
00129 TypeDesc (const char *typestring);
00130
00133 const char *c_str() const;
00134
00137 size_t numelements () const {
00138 return (arraylen >= 1 ? arraylen : 1);
00139 }
00140
00143 size_t size () const {
00144 size_t a = (size_t) (arraylen > 0 ? arraylen : 1);
00145 if (sizeof(size_t) > sizeof(int)) {
00146
00147 return a * elementsize();
00148 } else {
00149
00150 unsigned long long s = (unsigned long long) a * elementsize();
00151 const size_t toobig = std::numeric_limits<size_t>::max();
00152 return s < toobig ? (size_t)s : toobig;
00153 }
00154 }
00155
00158 TypeDesc elementtype () const {
00159 TypeDesc t (*this); t.arraylen = 0; return t;
00160 }
00161
00164 size_t elementsize () const { return aggregate * basesize(); }
00165
00166
00167
00168
00169
00172 size_t basesize () const;
00173
00179 int fromstring (const char *typestring, char *shortname=NULL);
00180
00183 bool operator== (const TypeDesc &t) const {
00184 return basetype == t.basetype && aggregate == t.aggregate &&
00185 vecsemantics == t.vecsemantics && arraylen == t.arraylen;
00186 }
00187
00190 bool operator!= (const TypeDesc &t) const { return ! (*this == t); }
00191
00194 friend bool operator== (const TypeDesc &t, BASETYPE b) {
00195 return (BASETYPE)t.basetype == b && (AGGREGATE)t.aggregate == SCALAR && t.arraylen == 0;
00196 }
00197 friend bool operator== (BASETYPE b, const TypeDesc &t) {
00198 return (BASETYPE)t.basetype == b && (AGGREGATE)t.aggregate == SCALAR && t.arraylen == 0;
00199 }
00200
00203 friend bool operator!= (const TypeDesc &t, BASETYPE b) {
00204 return (BASETYPE)t.basetype != b || (AGGREGATE)t.aggregate != SCALAR || t.arraylen != 0;
00205 }
00206 friend bool operator!= (BASETYPE b, const TypeDesc &t) {
00207 return (BASETYPE)t.basetype != b || (AGGREGATE)t.aggregate != SCALAR || t.arraylen != 0;
00208 }
00209
00212 void unarray (void) { arraylen = 0; }
00213
00214 static const TypeDesc TypeFloat;
00215 static const TypeDesc TypeColor;
00216 static const TypeDesc TypeString;
00217 static const TypeDesc TypeInt;
00218 static const TypeDesc TypePoint;
00219 static const TypeDesc TypeVector;
00220 static const TypeDesc TypeNormal;
00221 static const TypeDesc TypeMatrix;
00222 };
00223
00224
00225
00228 template<typename T> struct BaseTypeFromC {};
00229 template<> struct BaseTypeFromC<unsigned char> { static const TypeDesc::BASETYPE value = TypeDesc::UINT8; };
00230 template<> struct BaseTypeFromC<char> { static const TypeDesc::BASETYPE value = TypeDesc::INT8; };
00231 template<> struct BaseTypeFromC<unsigned short> { static const TypeDesc::BASETYPE value = TypeDesc::UINT16; };
00232 template<> struct BaseTypeFromC<short> { static const TypeDesc::BASETYPE value = TypeDesc::INT16; };
00233 template<> struct BaseTypeFromC<unsigned int> { static const TypeDesc::BASETYPE value = TypeDesc::UINT; };
00234 template<> struct BaseTypeFromC<int> { static const TypeDesc::BASETYPE value = TypeDesc::INT; };
00235 template<> struct BaseTypeFromC<unsigned long long> { static const TypeDesc::BASETYPE value = TypeDesc::UINT64; };
00236 template<> struct BaseTypeFromC<long long> { static const TypeDesc::BASETYPE value = TypeDesc::INT64; };
00237 #ifdef _HALF_H_
00238 template<> struct BaseTypeFromC<half> { static const TypeDesc::BASETYPE value = TypeDesc::HALF; };
00239 #endif
00240 template<> struct BaseTypeFromC<float> { static const TypeDesc::BASETYPE value = TypeDesc::FLOAT; };
00241 template<> struct BaseTypeFromC<double> { static const TypeDesc::BASETYPE value = TypeDesc::DOUBLE; };
00242
00243
00244
00247 template<int b> struct CType {};
00248 template<> struct CType<(int)TypeDesc::UINT8> { typedef unsigned char type; };
00249 template<> struct CType<(int)TypeDesc::INT8> { typedef char type; };
00250 template<> struct CType<(int)TypeDesc::UINT16> { typedef unsigned short type; };
00251 template<> struct CType<(int)TypeDesc::INT16> { typedef short type; };
00252 template<> struct CType<(int)TypeDesc::UINT> { typedef unsigned int type; };
00253 template<> struct CType<(int)TypeDesc::INT> { typedef int type; };
00254 template<> struct CType<(int)TypeDesc::UINT64> { typedef unsigned long long type; };
00255 template<> struct CType<(int)TypeDesc::INT64> { typedef long long type; };
00256 #ifdef _HALF_H_
00257 template<> struct CType<(int)TypeDesc::HALF> { typedef half type; };
00258 #endif
00259 template<> struct CType<(int)TypeDesc::FLOAT> { typedef float type; };
00260 template<> struct CType<(int)TypeDesc::DOUBLE> { typedef double type; };
00261
00262
00263
00264
00265 typedef TypeDesc ParamType;
00266 typedef TypeDesc ParamBaseType;
00267 #define PT_FLOAT TypeDesc::FLOAT
00268 #define PT_UINT8 TypeDesc::UCHAR
00269 #define PT_INT8 TypeDesc::CHAR
00270 #define PT_UINT16 TypeDesc::USHORT
00271 #define PT_INT16 TypeDesc::SHORT
00272 #define PT_UINT TypeDesc::UINT
00273 #define PT_INT TypeDesc::INT
00274 #define PT_FLOAT TypeDesc::FLOAT
00275 #define PT_DOUBLE TypeDesc::DOUBLE
00276 #define PT_HALF TypeDesc::HALF
00277 #define PT_MATRIX TypeDesc(TypeDesc::FLOAT,TypeDesc::MATRIX44)
00278 #define PT_STRING TypeDesc::STRING
00279 #define PT_UNKNOWN TypeDesc::UNKNOWN
00280 #define PT_COLOR TypeDesc(TypeDesc::FLOAT,TypeDesc::VEC3,TypeDesc::COLOR)
00281 #define PT_POINT TypeDesc(TypeDesc::FLOAT,TypeDesc::VEC3,TypeDesc::POINT)
00282 #define PT_VECTOR TypeDesc(TypeDesc::FLOAT,TypeDesc::VEC3,TypeDesc::VECTOR)
00283 #define PT_NORMAL TypeDesc(TypeDesc::FLOAT,TypeDesc::VEC3,TypeDesc::NORMAL)
00284
00285
00286
00287
00288
00289
00290
00291 #ifdef OPENIMAGEIO_NAMESPACE
00292 };
00293 using namespace OPENIMAGEIO_NAMESPACE;
00294 #endif
00295
00296 #endif