Field3D
MACFieldIO.cpp
Go to the documentation of this file.
00001 //----------------------------------------------------------------------------//
00002 
00003 /*
00004  * Copyright (c) 2009 Sony Pictures Imageworks Inc
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  * Redistributions of source code must retain the above copyright
00013  * notice, this list of conditions and the following disclaimer.
00014  * Redistributions in binary form must reproduce the above copyright
00015  * notice, this list of conditions and the following disclaimer in the
00016  * documentation and/or other materials provided with the
00017  * distribution.  Neither the name of Sony Pictures Imageworks nor the
00018  * names of its contributors may be used to endorse or promote
00019  * products derived from this software without specific prior written
00020  * permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
00026  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00028  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00029  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00031  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00033  * OF THE POSSIBILITY OF SUCH DAMAGE.
00034  */
00035 
00036 //----------------------------------------------------------------------------//
00037 
00042 //----------------------------------------------------------------------------//
00043 
00044 #include "MACFieldIO.h"
00045 
00046 //----------------------------------------------------------------------------//
00047 
00048 using namespace boost;
00049 using namespace std;
00050 
00051 //----------------------------------------------------------------------------//
00052 
00053 FIELD3D_NAMESPACE_OPEN
00054 
00055 //----------------------------------------------------------------------------//
00056 // Field3D namespaces
00057 //----------------------------------------------------------------------------//
00058 
00059 using namespace Exc;
00060 using namespace Hdf5Util;
00061 
00062 //----------------------------------------------------------------------------//
00063 // Static members
00064 //----------------------------------------------------------------------------//
00065 
00066 const int         MACFieldIO::k_versionNumber(1);
00067 const std::string MACFieldIO::k_versionAttrName("version");
00068 const std::string MACFieldIO::k_extentsStr("extents");
00069 const std::string MACFieldIO::k_dataWindowStr("data_window");
00070 const std::string MACFieldIO::k_componentsStr("components");
00071 const std::string MACFieldIO::k_bitsPerComponentStr("bits_per_component");
00072 const std::string MACFieldIO::k_uDataStr("u_data");
00073 const std::string MACFieldIO::k_vDataStr("v_data");
00074 const std::string MACFieldIO::k_wDataStr("w_data");
00075 
00076 //----------------------------------------------------------------------------//
00077 
00078 FieldBase::Ptr
00079 MACFieldIO::read(hid_t layerGroup, const std::string &filename, 
00080                  const std::string &layerPath,
00081                  DataTypeEnum typeEnum)
00082 {
00083   Box3i extents, dataW;
00084   int components;
00085 
00086   //hsize_t dims[1];
00087   
00088   if (layerGroup == -1)
00089     throw BadHdf5IdException("Bad layer group in MACFieldIO::read");
00090 
00091   int version;
00092   if (!readAttribute(layerGroup, k_versionAttrName, 1, version))
00093     throw MissingAttributeException("Couldn't find attribute " + 
00094                                     k_versionAttrName);
00095 
00096   if (version != k_versionNumber)
00097     throw UnsupportedVersionException("MACField version not supported: " + 
00098                                       lexical_cast<std::string>(version));
00099 
00100   if (!readAttribute(layerGroup, k_extentsStr, 6, extents.min.x)) 
00101     throw MissingAttributeException("Couldn't find attribute " + 
00102                                     k_extentsStr);
00103 
00104   if (!readAttribute(layerGroup, k_dataWindowStr, 6, dataW.min.x)) 
00105     throw MissingAttributeException("Couldn't find attribute " + 
00106                                     k_dataWindowStr);
00107   
00108   if (!readAttribute(layerGroup, k_componentsStr, 1, components)) 
00109     throw MissingAttributeException("Couldn't find attribute " + 
00110                                     k_componentsStr);
00111   // Check the data type ---
00112   int bits;
00113   if (!readAttribute(layerGroup, k_bitsPerComponentStr, 1, bits)) 
00114     throw MissingAttributeException("Couldn't find attribute: " +
00115                                     k_bitsPerComponentStr);  
00116 
00117   // Build a MACField to store everything in
00118   FieldBase::Ptr result;
00119   switch (bits) {
00120   case 16:
00121     {
00122       if (typeEnum != DataTypeVecHalf) break;
00123       MACField<V3h>::Ptr field(new MACField<V3h>);
00124       field->setSize(extents, dataW);
00125       readData<V3h>(layerGroup, field);
00126 
00127       result = field;      
00128     }
00129     break;
00130   case 64:
00131     {
00132       if (typeEnum != DataTypeVecDouble) break;
00133       MACField<V3d>::Ptr field(new MACField<V3d>);
00134       field->setSize(extents, dataW);
00135       readData<V3d>(layerGroup, field);
00136 
00137       result = field;      
00138     }
00139     break;
00140   case 32:
00141   default:
00142     {
00143       if (typeEnum != DataTypeVecFloat) break;
00144       MACField<V3f>::Ptr field(new MACField<V3f>);
00145       field->setSize(extents, dataW);
00146       readData<V3f>(layerGroup, field);
00147 
00148       result = field;      
00149     }
00150   }
00151 
00152   return result;
00153 }
00154 
00155 //----------------------------------------------------------------------------//
00156 
00157 bool
00158 MACFieldIO::write(hid_t layerGroup, FieldBase::Ptr field)
00159 {
00160   if (layerGroup == -1) {
00161     throw BadHdf5IdException("Bad layer group in MACFieldIO::write");
00162   }
00163 
00164   // Add version attribute
00165   if (!writeAttribute(layerGroup, k_versionAttrName, 
00166                       1, k_versionNumber)) {
00167     throw WriteAttributeException("Couldn't write attribute " + 
00168                                   k_versionAttrName);
00169   }
00170 
00171   MACField<V3h>::Ptr vecHalfField = 
00172     field_dynamic_cast<MACField<V3h> >(field);
00173   MACField<V3f>::Ptr vecFloatField = 
00174     field_dynamic_cast<MACField<V3f> >(field);
00175   MACField<V3d>::Ptr vecDoubleField = 
00176     field_dynamic_cast<MACField<V3d> >(field);
00177 
00178   bool success = true;
00179   if (vecFloatField) {
00180     success = writeInternal<V3f>(layerGroup, vecFloatField);
00181   } else if (vecHalfField) {
00182     success = writeInternal<V3h>(layerGroup, vecHalfField);
00183   } else if (vecDoubleField) {
00184     success = writeInternal<V3d>(layerGroup, vecDoubleField);
00185   } else {
00186     throw WriteLayerException("MACFieldIO does not support the given "
00187                               "MACField template parameter");
00188   }
00189 
00190   return success;
00191 }
00192 
00193 //----------------------------------------------------------------------------//
00194 
00195 FIELD3D_NAMESPACE_SOURCE_CLOSE
00196 
00197 //----------------------------------------------------------------------------//