1 //-*****************************************************************************
2 //
3 // Copyright (c) 2009-2011,
4 //  Sony Pictures Imageworks Inc. and
5 //  Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
6 //
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
12 // *       Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // *       Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following disclaimer
16 // in the documentation and/or other materials provided with the
17 // distribution.
18 // *       Neither the name of Sony Pictures Imageworks, nor
19 // Industrial Light & Magic, nor the names of their contributors may be used
20 // to endorse or promote products derived from this software without specific
21 // prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 //
35 //-*****************************************************************************
36 
37 #include <Alembic/AbcCoreHDF5/DataTypeRegistry.h>
38 #include <Alembic/AbcCoreHDF5/HDF5Util.h>
39 
40 namespace Alembic {
41 namespace AbcCoreHDF5 {
42 namespace ALEMBIC_VERSION_NS {
43 
44 //-*****************************************************************************
45 using namespace AbcA;
46 
47 //-*****************************************************************************
GetNativeBoolH5T()48 static hid_t GetNativeBoolH5T()
49 {
50     hid_t ret = H5Tcopy( H5T_NATIVE_UINT8 );
51     H5Tset_size( ret, 1 );
52     H5Tset_precision( ret, 1 );
53     H5Tset_sign( ret, H5T_SGN_NONE );
54     H5Tset_offset( ret, 0 );
55     H5Tset_pad( ret, H5T_PAD_ZERO, H5T_PAD_ZERO );
56     return ret;
57 }
58 
59 //-*****************************************************************************
GetFileBoolH5T()60 static hid_t GetFileBoolH5T()
61 {
62     hid_t ret = H5Tcopy( H5T_STD_U8LE );
63     H5Tset_size( ret, 1 );
64     H5Tset_precision( ret, 1 );
65     H5Tset_sign( ret, H5T_SGN_NONE );
66     H5Tset_offset( ret, 0 );
67     H5Tset_pad( ret, H5T_PAD_ZERO, H5T_PAD_ZERO );
68     return ret;
69 }
70 
71 //-*****************************************************************************
GetNativeHalfH5T()72 static hid_t GetNativeHalfH5T()
73 {
74     hid_t ret = H5Tcopy( H5T_NATIVE_FLOAT );
75     H5Tset_fields( ret,
76                    15,   // sign bit position
77                    10,   // exponent lsb position
78                    5,    // exponent size
79                    0,    // mantissa lsb position
80                    10 ); // mantissa size
81     H5Tset_size( ret, 2 );
82     return ret;
83 }
84 
85 //-*****************************************************************************
GetFileHalfH5T()86 static hid_t GetFileHalfH5T()
87 {
88     hid_t ret = H5Tcopy( H5T_IEEE_F32LE );
89     H5Tset_fields( ret,
90                    15,   // sign bit position
91                    10,   // exponent lsb position
92                    5,    // exponent size
93                    0,    // mantissa lsb position
94                    10 ); // mantissa size
95     H5Tset_size( ret, 2 );
96     return ret;
97 }
98 
99 //-*****************************************************************************
100 //-*****************************************************************************
101 //-*****************************************************************************
102 // SUPPORT FUNCTIONS
103 //-*****************************************************************************
104 //-*****************************************************************************
105 //-*****************************************************************************
106 // In order to read or write a slab of a given DataType, we need an H5::Datatype
107 // for the dataset in the file, and a 'native' one for how it will be
108 // stored in local memory. We are not bothering with named datatypes for now.
109 // Though, it may prove timesaving later on.
110 //
111 // So - we need some sort of structure which allows you to take an
112 // AlembicAsset::DataType and come up with an H5::Datatype for it.
GetNativeH5T(const AbcA::DataType & adt,bool & oCleanUp)113 hid_t GetNativeH5T( const AbcA::DataType &adt, bool &oCleanUp )
114 {
115     oCleanUp = false;
116 
117     hid_t baseDtype = -1;
118     switch ( adt.getPod() )
119     {
120     case kBooleanPOD:
121         oCleanUp = true;
122         baseDtype = GetNativeBoolH5T();
123         break;
124     case kUint8POD:     baseDtype = H5T_NATIVE_UINT8; break;
125     case kInt8POD:      baseDtype = H5T_NATIVE_INT8; break;
126     case kUint16POD:    baseDtype = H5T_NATIVE_UINT16; break;
127     case kInt16POD:     baseDtype = H5T_NATIVE_INT16; break;
128     case kUint32POD:    baseDtype = H5T_NATIVE_UINT32; break;
129     case kInt32POD:     baseDtype = H5T_NATIVE_INT32; break;
130     case kUint64POD:    baseDtype = H5T_NATIVE_UINT64; break;
131     case kInt64POD:     baseDtype = H5T_NATIVE_INT64; break;
132     case kFloat16POD:
133         oCleanUp = true;
134         baseDtype = GetNativeHalfH5T();
135         break;
136     case kFloat32POD:   baseDtype = H5T_NATIVE_FLOAT; break;
137     case kFloat64POD:   baseDtype = H5T_NATIVE_DOUBLE; break;
138     default:
139         ABCA_THROW( "Unsuppored POD type: " << PODName( adt.getPod() ) );
140     }
141 
142     ABCA_ASSERT( baseDtype >= 0, "Bad base datatype id" );
143 
144     return baseDtype;
145 }
146 
147 //-*****************************************************************************
148 // Same as above, only this time for the files.
149 // Alembic uses LittleEndian by default.
GetFileH5T(const AbcA::DataType & adt,bool & oCleanUp)150 hid_t GetFileH5T( const AbcA::DataType &adt, bool &oCleanUp )
151 {
152     oCleanUp = false;
153     hid_t baseDtype = -1;
154     switch ( adt.getPod() )
155     {
156     case kBooleanPOD:
157         oCleanUp = true;
158         baseDtype = GetFileBoolH5T(); break;
159     case kUint8POD:     baseDtype = H5T_STD_U8LE; break;
160     case kInt8POD:      baseDtype = H5T_STD_I8LE; break;
161     case kUint16POD:    baseDtype = H5T_STD_U16LE; break;
162     case kInt16POD:     baseDtype = H5T_STD_I16LE; break;
163     case kUint32POD:    baseDtype = H5T_STD_U32LE; break;
164     case kInt32POD:     baseDtype = H5T_STD_I32LE; break;
165     case kUint64POD:    baseDtype = H5T_STD_U64LE; break;
166     case kInt64POD:     baseDtype = H5T_STD_I64LE; break;
167     case kFloat16POD:
168         oCleanUp = true;
169         baseDtype = GetFileHalfH5T(); break;
170     case kFloat32POD:   baseDtype = H5T_IEEE_F32LE; break;
171     case kFloat64POD:   baseDtype = H5T_IEEE_F64LE; break;
172     default:
173         ABCA_THROW( "Unsuppored POD type: " << PODName( adt.getPod() ) );
174     }
175 
176     ABCA_ASSERT( baseDtype >= 0, "Bad base datatype id" );
177 
178     return baseDtype;
179 }
180 
181 } // End namespace ALEMBIC_VERSION_NS
182 } // End namespace AbcCoreHDF5
183 } // End namespace Alembic
184