bool loadStackHDF5( const char* fileName, Image4DSimple& img ) { #ifdef USE_HDF5 H5::Exception::dontPrint(); H5::H5File file( fileName, H5F_ACC_RDONLY ); for ( size_t i = 0; i < file.getObjCount(); i++ ) { H5std_string name = file.getObjnameByIdx( i ); if ( name == "Channels" ) { H5::Group channels = file.openGroup( name ); // Grab the attributes H5::Attribute attr = channels.openAttribute( "width" ); H5::DataType type = attr.getDataType(); long width, height; attr.read( type, &width ); attr.close(); attr = channels.openAttribute( "height" ); attr.read( type, &height ); attr.close(); int num_channels = 0; // Count the number of channels for ( size_t obj = 0; obj < channels.getNumObjs(); obj++ ) if ( channels.getObjTypeByIdx( obj ) == H5G_DATASET ) num_channels++; int channel_idx = 0; for ( size_t obj = 0; obj < channels.getNumObjs(); obj++ ) { if ( channels.getObjTypeByIdx( obj ) == H5G_DATASET ) { H5std_string ds_name = channels.getObjnameByIdx( obj ); H5::DataSet data = channels.openDataSet( ds_name ); uint8_t* buffer = new uint8_t[ data.getStorageSize() ]; data.read( buffer, data.getDataType() ); QByteArray qbarray( ( const char* )buffer, data.getStorageSize() ); data.close(); if ( !loadIndexedStackFFMpeg( &qbarray, img, channel_idx++, num_channels, width, height ) ) { v3d_msg( "Error happened in HDF file reading. Stop. \n", false ); return false; } delete [] buffer; } } } } #endif return true; }
/********************************************************* FunctionName:RecursiveVisitNode FunctionDesc:递归创建Group InputParam: OutputParam: Return: Author:xiaowei.han *********************************************************/ void RecursiveVisitNode(H5::Group& Node, Hdf5_Wrapper::LP_HDF5_DATA pData) { using namespace H5; if (nullptr == pData) { return; } //获取节点名称 pData->strKeyName = Utility::UTF8ToGB2312(Node.getObjName()); //获取属性个数 int nAttrNum = static_cast<int>(Node.getNumAttrs()); for (int i = 0; i < nAttrNum; ++i) { auto childAttr = Node.openAttribute(i); //解析读取属性数据 //获取数据类型 auto AttrDataType = childAttr.getDataType(); //获取数据类型大小 unsigned int nDataByte = (unsigned int)AttrDataType.getSize(); boost::scoped_ptr<CAbstractAttrHanlder> pHandler(CreateAttrHandler(childAttr.getTypeClass(), nDataByte)); if (pHandler) { pHandler->ReadAttr(childAttr, AttrDataType, pData->AttributeArray); } childAttr.close(); } //获取节点对象个数 int nObjNum = static_cast<int>(Node.getNumObjs()); //遍历子节点信息 for (int i = 0; i < nObjNum; ++i) { //NECESSARY_LOG("the node name is [%s],the type is [%d].", Node.getObjnameByIdx(i).c_str(), Node.getObjTypeByIdx(i)); auto SubNodeType = Node.getObjTypeByIdx(i); switch (SubNodeType) { case H5G_GROUP: { Hdf5_Wrapper::LP_HDF5_DATA pSubData = new Hdf5_Wrapper::HDF5_DATA; if (nullptr != pSubData) { //添加进去 pData->SubDataArray.push_back(pSubData); auto ChildGroupNode = Node.openGroup(Node.getObjnameByIdx(i)); RecursiveVisitNode(ChildGroupNode,pSubData); ChildGroupNode.close(); } } break; case H5G_DATASET: { auto childDataset = Node.openDataSet(Node.getObjnameByIdx(i)); Hdf5_Wrapper::LP_HDF5_DATA pSubData = new Hdf5_Wrapper::HDF5_DATA; if (nullptr != pSubData) { pSubData->strKeyName = childDataset.getObjName(); //添加进去 pData->SubDataArray.push_back(pSubData); //获取数据类型 auto ChildDataSetDataType = childDataset.getDataType(); //获取类型占用字节数 unsigned int nDataByte = (unsigned int)ChildDataSetDataType.getSize(); //判断数据类型 boost::scoped_ptr<CAbstractDataTypeHandler> pHandlder(CDataTypeHandlerFactory::CreateDataTypeHandler(childDataset.getTypeClass(),nDataByte)); if (pHandlder) { pHandlder->ReadDataSet(childDataset, pSubData); } } } break; default: break; } } }
void ossim_hdf5::iterateGroupForDatasetNames( H5::H5File* file, const std::string& groupName, std::vector<std::string>& datasetNames, ossim_uint32& recursedCount ) { if ( file && groupName.size() ) { ++recursedCount; // std::cout << "iterateGroup: " << groupName << std::endl; H5::Group* group = new H5::Group( file->openGroup(groupName) ); const hsize_t OBJ_COUNT = group->getNumObjs(); for ( hsize_t i = 0; i < OBJ_COUNT; ++i ) { std::string objName = group->getObjnameByIdx(i); if ( objName.size() ) { char separator = '/'; std::string combinedName; combine( groupName, objName, separator, combinedName ); H5G_obj_t objType = group->getObjTypeByIdx(i); #if 0 std::cout << "combinedName: " << combinedName << "\ngetObjnameByIdx[" << i << "]: " << objName << "\ngetObjTypeByIdx[" << i << "]: " << objType << std::endl; #endif if ( objType == H5G_GROUP ) { // Recursive call: if ( recursedCount < ossim_hdf5::MAX_RECURSION_LEVEL ) { ossim_hdf5::iterateGroupForDatasetNames( file, combinedName, datasetNames, recursedCount ); } else { ossimNotify(ossimNotifyLevel_WARN) << "ossim_hdf5::iterateGroupForDatasetNames WARNING!" << "\nMax iterations reached!" << std::endl; } } else if ( objType == H5G_DATASET ) { datasetNames.push_back( combinedName ); } else { ossimNotify(ossimNotifyLevel_WARN) << "ossim_hdf5::iterateGroupForDatasetNames WARNING!" << "\nUnhandled object type: " << objType << std::endl; } } } group->close(); delete group; group = 0; --recursedCount; } // Matches: if ( file ) } // End: void ossim_hdf5::iterateGroupForDatasetNames
void ossim_hdf5::printIterative( H5::H5File* file, const std::string& groupName, const std::string& prefix, ossim_uint32& recursedCount, std::ostream& out ) { if ( file && groupName.size() ) { ++recursedCount; H5::Group* group = new H5::Group( file->openGroup(groupName) ); // Print attributes: const ossim_uint32 ATTRS_COUNT = group->getNumAttrs(); for ( ossim_uint32 aIdx = 0; aIdx < ATTRS_COUNT; ++aIdx ) { H5::Attribute attr( group->openAttribute( aIdx ) ); ossim_hdf5::printAttribute( attr, prefix, out ); attr.close(); } const hsize_t OBJ_COUNT = group->getNumObjs(); for ( hsize_t i = 0; i < OBJ_COUNT; ++i ) { std::string objName = group->getObjnameByIdx(i); if ( objName.size() ) { char separator = '/'; std::string combinedName; combine( groupName, objName, separator, combinedName ); separator = '.'; std::string combinedPrefix; combine( prefix, objName, separator, combinedPrefix ); H5G_obj_t objType = group->getObjTypeByIdx(i); #if 0 std::cout << "combinedName: " << combinedName << "\ncombinedPrefix: " << combinedPrefix << "\ngetObjnameByIdx[" << i << "]: " << objName << "\ngetObjTypeByIdx[" << i << "]: " << objType << std::endl; #endif if ( objType == H5G_GROUP ) { // Recursive call: if ( recursedCount < ossim_hdf5::MAX_RECURSION_LEVEL ) { ossim_hdf5::printIterative( file, combinedName, combinedPrefix, recursedCount, out ); } else { ossimNotify(ossimNotifyLevel_WARN) << "ossim_hdf5::printIterative WARNING!" << "\nMax iterations reached!" << std::endl; } } else if ( objType == H5G_DATASET ) { printObject( file, combinedName, combinedPrefix, out ); } else { ossimNotify(ossimNotifyLevel_WARN) << "ossim_hdf5::printIterative WARNING!" << "\nUnhandled object type: " << objType << std::endl; } } } group->close(); delete group; group = 0; --recursedCount; } // Matches: if ( file ) } // End: void ossim_hdf5::printIterative method.