Field3D
MIPFieldIO.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------//
2 
3 /*
4  * Copyright (c) 2009 Sony Pictures Imageworks Inc
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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 copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution. Neither the name of Sony Pictures Imageworks nor the
18  * names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior written
20  * permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 //----------------------------------------------------------------------------//
37 
42 //----------------------------------------------------------------------------//
43 
44 #include "MIPFieldIO.h"
45 
46 //----------------------------------------------------------------------------//
47 
48 using namespace boost;
49 using namespace std;
50 
51 //----------------------------------------------------------------------------//
52 
54 
55 //----------------------------------------------------------------------------//
56 // Field3D namespaces
57 //----------------------------------------------------------------------------//
58 
59 using namespace Exc;
60 using namespace Hdf5Util;
61 
62 //----------------------------------------------------------------------------//
63 // Static member initialization
64 //----------------------------------------------------------------------------//
65 
66 const int MIPFieldIO::k_versionNumber (1);
67 const std::string MIPFieldIO::k_versionAttrName ("version");
68 const std::string MIPFieldIO::k_extentsStr ("extents");
69 const std::string MIPFieldIO::k_dataWindowStr ("data_window");
70 const std::string MIPFieldIO::k_componentsStr ("components");
71 const std::string MIPFieldIO::k_bitsPerComponentStr("bits_per_component");
72 const std::string MIPFieldIO::k_mipGroupStr ("mip_levels");
73 const std::string MIPFieldIO::k_levelGroupStr ("level");
74 const std::string MIPFieldIO::k_levelsStr ("levels");
75 const std::string MIPFieldIO::k_baseTypeStr ("base_type");
76 
77 //----------------------------------------------------------------------------//
78 // MIPFieldIO
79 //----------------------------------------------------------------------------//
80 
82 MIPFieldIO::read(hid_t layerGroup, const std::string &filename,
83  const std::string &layerPath,
84  DataTypeEnum typeEnum)
85 {
86  Box3i extents, dataW;
87  int components;
88 
89  if (layerGroup == -1)
90  throw BadHdf5IdException("Bad layer group in MIPFieldIO::read");
91 
92  int version;
93  if (!readAttribute(layerGroup, k_versionAttrName, 1, version))
94  throw MissingAttributeException("Couldn't find attribute " +
95  k_versionAttrName);
96 
97  if (version != k_versionNumber)
98  throw UnsupportedVersionException("MIPField version not supported: " +
99  lexical_cast<std::string>(version));
100 
101  if (!readAttribute(layerGroup, k_componentsStr, 1, components))
102  throw MissingAttributeException("Couldn't find attribute " +
103  k_componentsStr);
104 
105  // Check data type ---
106 
107  int bits;
108  if (!readAttribute(layerGroup, k_bitsPerComponentStr, 1, bits))
109  throw MissingAttributeException("Couldn't find attribute: " +
110  k_bitsPerComponentStr);
111 
112  std::string baseType;
113  if (!readAttribute(layerGroup, k_baseTypeStr, baseType)) {
114  throw MissingAttributeException("Couldn't find attribute: " +
115  k_baseTypeStr);
116  }
117 
118  bool isHalf = false;
119  bool isFloat = false;
120  bool isDouble = false;
121 
122  switch (bits) {
123  case 16:
124  isHalf = true;
125  break;
126  case 64:
127  isDouble = true;
128  break;
129  case 32:
130  default:
131  isFloat = true;
132  }
133 
134  bool isSparse = false;
135  bool isDense = false;
136 
137  if (baseType == "SparseField") {
138  isSparse = true;
139  } else if (baseType == "DenseField") {
140  isDense = true;
141  }
142 
143  // Read the data ---
144 
145  FieldBase::Ptr result;
146 
147  if (isDense && isHalf && components == 1 && typeEnum == DataTypeHalf)
148  result = readInternal<DenseField, half>(layerGroup, filename,
149  layerPath, typeEnum);
150  if (isDense && isFloat && components == 1 && typeEnum == DataTypeFloat)
151  result = readInternal<DenseField, float>(layerGroup, filename,
152  layerPath, typeEnum);
153  if (isDense && isDouble && components == 1 && typeEnum == DataTypeDouble)
154  result = readInternal<DenseField, double>(layerGroup, filename,
155  layerPath, typeEnum);
156  if (isDense && isHalf && components == 3 && typeEnum == DataTypeVecHalf)
157  result = readInternal<DenseField, V3h>(layerGroup, filename,
158  layerPath, typeEnum);
159  if (isDense && isFloat && components == 3 && typeEnum == DataTypeVecFloat)
160  result = readInternal<DenseField, V3f>(layerGroup, filename,
161  layerPath, typeEnum);
162  if (isDense && isDouble && components == 3 && typeEnum == DataTypeVecDouble)
163  result = readInternal<DenseField, V3d>(layerGroup, filename,
164  layerPath, typeEnum);
165  if (isSparse && isHalf && components == 1 && typeEnum == DataTypeHalf)
166  result = readInternal<SparseField, half>(layerGroup, filename,
167  layerPath, typeEnum);
168  if (isSparse && isFloat && components == 1 && typeEnum == DataTypeFloat)
169  result = readInternal<SparseField, float>(layerGroup, filename,
170  layerPath, typeEnum);
171  if (isSparse && isDouble && components == 1 && typeEnum == DataTypeDouble)
172  result = readInternal<SparseField, double>(layerGroup, filename,
173  layerPath, typeEnum);
174  if (isSparse && isHalf && components == 3 && typeEnum == DataTypeVecHalf)
175  result = readInternal<SparseField, V3h>(layerGroup, filename,
176  layerPath, typeEnum);
177  if (isSparse && isFloat && components == 3 && typeEnum == DataTypeVecFloat)
178  result = readInternal<SparseField, V3f>(layerGroup, filename,
179  layerPath, typeEnum);
180  if (isSparse && isDouble && components == 3 && typeEnum == DataTypeVecDouble)
181  result = readInternal<SparseField, V3d>(layerGroup, filename,
182  layerPath, typeEnum);
183 
184  return result;
185 }
186 
187 //----------------------------------------------------------------------------//
188 
189 bool
190 MIPFieldIO::write(hid_t layerGroup, FieldBase::Ptr field)
191 {
192  if (layerGroup == -1) {
193  throw BadHdf5IdException("Bad layer group in MIPFieldIO::write");
194  }
195 
196  // Add version attribute
197  if (!writeAttribute(layerGroup, k_versionAttrName,
198  1, k_versionNumber)) {
199  throw WriteAttributeException("Couldn't write attribute " +
200  k_versionAttrName);
201  }
202 
203  MIPField<DenseField<half> >::Ptr halfDenseField =
205  MIPField<DenseField<float> >::Ptr floatDenseField =
207  MIPField<DenseField<double> >::Ptr doubleDenseField =
209  MIPField<DenseField<V3h> >::Ptr vecHalfDenseField =
211  MIPField<DenseField<V3f> >::Ptr vecFloatDenseField =
213  MIPField<DenseField<V3d> >::Ptr vecDoubleDenseField =
215  MIPField<SparseField<half> >::Ptr halfSparseField =
217  MIPField<SparseField<float> >::Ptr floatSparseField =
219  MIPField<SparseField<double> >::Ptr doubleSparseField =
221  MIPField<SparseField<V3h> >::Ptr vecHalfSparseField =
223  MIPField<SparseField<V3f> >::Ptr vecFloatSparseField =
225  MIPField<SparseField<V3d> >::Ptr vecDoubleSparseField =
227 
228  bool success = true;
229 
230  if (floatDenseField) {
231  success = writeInternal<DenseField, float>(layerGroup, floatDenseField);
232  }
233  else if (halfDenseField) {
234  success = writeInternal<DenseField, half>(layerGroup, halfDenseField);
235  }
236  else if (doubleDenseField) {
237  success = writeInternal<DenseField, double>(layerGroup, doubleDenseField);
238  }
239  else if (vecFloatDenseField) {
240  success = writeInternal<DenseField, V3f>(layerGroup, vecFloatDenseField);
241  }
242  else if (vecHalfDenseField) {
243  success = writeInternal<DenseField, V3h>(layerGroup, vecHalfDenseField);
244  }
245  else if (vecDoubleDenseField) {
246  success = writeInternal<DenseField, V3d>(layerGroup, vecDoubleDenseField);
247  }
248  else if (floatSparseField) {
249  success = writeInternal<SparseField, float>(layerGroup, floatSparseField);
250  }
251  else if (halfSparseField) {
252  success = writeInternal<SparseField, half>(layerGroup, halfSparseField);
253  }
254  else if (doubleSparseField) {
255  success = writeInternal<SparseField, double>(layerGroup, doubleSparseField);
256  }
257  else if (vecFloatSparseField) {
258  success = writeInternal<SparseField, V3f>(layerGroup, vecFloatSparseField);
259  }
260  else if (vecHalfSparseField) {
261  success = writeInternal<SparseField, V3h>(layerGroup, vecHalfSparseField);
262  }
263  else if (vecDoubleSparseField) {
264  success = writeInternal<SparseField, V3d>(layerGroup, vecDoubleSparseField);
265  }
266  else {
267  throw WriteLayerException("MIPFieldIO does not support the given "
268  "MIPField template parameter");
269  }
270 
271  return success;
272 }
273 
274 //----------------------------------------------------------------------------//
275 
277 
278 //----------------------------------------------------------------------------//
Field_T::Ptr field_dynamic_cast(RefBase::Ptr field)
Dynamic cast that uses string-comparison in order to be safe even after an object crosses a shared li...
Definition: RefCount.h:256
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition: ns.h:60
Namespace for Exception objects.
Definition: Exception.h:57
Contains the MIPFieldIO class.
static const std::string k_dataWindowStr
Definition: MIPFieldIO.h:149
FIELD3D_API bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
boost::intrusive_ptr< FieldBase > Ptr
Definition: Field.h:97
static const std::string k_levelsStr
Definition: MIPFieldIO.h:154
static const std::string k_baseTypeStr
Definition: MIPFieldIO.h:155
This subclass stores a MIP representation of a Field_T field.
Definition: MIPField.h:109
static const std::string k_levelGroupStr
Definition: MIPFieldIO.h:153
static const std::string k_mipGroupStr
Definition: MIPFieldIO.h:152
static const std::string k_extentsStr
Definition: MIPFieldIO.h:148
static const std::string k_versionAttrName
Definition: MIPFieldIO.h:147
virtual FieldBase::Ptr read(hid_t layerGroup, const std::string &filename, const std::string &layerPath, DataTypeEnum typeEnum)
Reads the field at the given location and tries to create a MIPField object from it. Calls out to readData() for template-specific work.
Definition: MIPFieldIO.cpp:82
static const std::string k_componentsStr
Definition: MIPFieldIO.h:150
boost::intrusive_ptr< FieldIO > Ptr
Definition: FieldIO.h:90
static const std::string k_bitsPerComponentStr
Definition: MIPFieldIO.h:151
DataTypeEnum
Definition: Traits.h:66
virtual bool write(hid_t layerGroup, FieldBase::Ptr field)
Writes the given field to disk. This function calls out to writeInternal once the template type has b...
Definition: MIPFieldIO.cpp:190
static const int k_versionNumber
Definition: MIPFieldIO.h:146