bool PtexMainWriter::writeFace(int faceid, const FaceInfo& f, const void* data, int stride) { if (!_ok) return 0; // auto-compute stride if (stride == 0) stride = f.res.u()*_pixelSize; // handle constant case if (PtexUtils::isConstant(data, stride, f.res.u(), f.res.v(), _pixelSize)) return writeConstantFace(faceid, f, data); // non-constant case, ... // check and store face info if (!storeFaceInfo(faceid, _faceinfo[faceid], f)) return 0; // record position of current face _levels.front().pos[faceid] = ftello(_tmpfp); // write face data writeFaceData(_tmpfp, data, stride, f.res, _levels.front().fdh[faceid]); if (!_ok) return 0; // premultiply (if needed) before making reductions; use temp copy of data uint8_t* temp = 0; if (_header.hasAlpha()) { // first copy to temp buffer int rowlen = f.res.u() * _pixelSize, nrows = f.res.v(); temp = (uint8_t*) malloc(rowlen * nrows); PtexUtils::copy(data, stride, temp, rowlen, nrows, rowlen); // multiply alpha PtexUtils::multalpha(temp, f.res.size(), _header.datatype, _header.nchannels, _header.alphachan); // override source buffer data = temp; stride = rowlen; } // generate first reduction (if needed) if (_genmipmaps && (f.res.ulog2 > MinReductionLog2 && f.res.vlog2 > MinReductionLog2)) { _rpos[faceid] = ftello(_tmpfp); writeReduction(_tmpfp, data, stride, f.res); } else { storeConstValue(faceid, data, stride, f.res); } if (temp) free(temp); _hasNewData = true; return 1; }
void PtexMainWriter::generateReductions() { // first generate "rfaceids", reduction faceids, // which are faceids reordered by decreasing smaller dimension int nfaces = _header.nfaces; _rfaceids.resize(nfaces); _faceids_r.resize(nfaces); PtexUtils::genRfaceids(&_faceinfo[0], nfaces, &_rfaceids[0], &_faceids_r[0]); // determine how many faces in each level, and resize _levels // traverse in reverse rfaceid order to find number of faces // larger than cutoff size of each level for (int rfaceid = nfaces-1, cutoffres = MinReductionLog2; rfaceid >= 0; rfaceid--) { int faceid = _faceids_r[rfaceid]; FaceInfo& face = _faceinfo[faceid]; Res res = face.res; int min = face.isConstant() ? 1 : PtexUtils::min(res.ulog2, res.vlog2); while (min > cutoffres) { // i == last face for current level int size = rfaceid+1; _levels.push_back(LevelRec()); LevelRec& level = _levels.back(); level.pos.resize(size); level.fdh.resize(size); cutoffres++; } } // generate and cache reductions (including const data) // first, find largest face and allocate tmp buffer int buffsize = 0; for (int i = 0; i < nfaces; i++) buffsize = PtexUtils::max(buffsize, _faceinfo[i].res.size()); buffsize *= _pixelSize; char* buff = (char*) malloc(buffsize); int nlevels = int(_levels.size()); for (int i = 1; i < nlevels; i++) { LevelRec& level = _levels[i]; int nextsize = (i+1 < nlevels)? int(_levels[i+1].fdh.size()) : 0; for (int rfaceid = 0, size = int(level.fdh.size()); rfaceid < size; rfaceid++) { // output current reduction for face (previously generated) int faceid = _faceids_r[rfaceid]; Res res = _faceinfo[faceid].res; res.ulog2 -= i; res.vlog2 -= i; int stride = res.u() * _pixelSize; int blocksize = res.size() * _pixelSize; fseeko(_tmpfp, _rpos[faceid], SEEK_SET); readBlock(_tmpfp, buff, blocksize); fseeko(_tmpfp, 0, SEEK_END); level.pos[rfaceid] = ftello(_tmpfp); writeFaceData(_tmpfp, buff, stride, res, level.fdh[rfaceid]); if (!_ok) return; // write a new reduction if needed for next level if (rfaceid < nextsize) { fseeko(_tmpfp, _rpos[faceid], SEEK_SET); writeReduction(_tmpfp, buff, stride, res); } else { // the last reduction for each face is its constant value storeConstValue(faceid, buff, stride, res); } } } fseeko(_tmpfp, 0, SEEK_END); free(buff); }
bool PtexIncrWriter::writeFace(int faceid, const FaceInfo& f, const void* data, int stride) { if (stride == 0) stride = f.res.u()*_pixelSize; // handle constant case if (PtexUtils::isConstant(data, stride, f.res.u(), f.res.v(), _pixelSize)) return writeConstantFace(faceid, f, data); // init headers uint8_t edittype = et_editfacedata; uint32_t editsize; EditFaceDataHeader efdh; efdh.faceid = faceid; // check and store face info if (!storeFaceInfo(faceid, efdh.faceinfo, f)) return 0; // record position and skip headers FilePos pos = ftello(_fp); writeBlank(_fp, sizeof(edittype) + sizeof(editsize) + sizeof(efdh)); // must compute constant (average) val first uint8_t* constval = (uint8_t*) malloc(_pixelSize); if (_header.hasAlpha()) { // must premult alpha before averaging // first copy to temp buffer int rowlen = f.res.u() * _pixelSize, nrows = f.res.v(); uint8_t* temp = (uint8_t*) malloc(rowlen * nrows); PtexUtils::copy(data, stride, temp, rowlen, nrows, rowlen); // multiply alpha PtexUtils::multalpha(temp, f.res.size(), _header.datatype, _header.nchannels, _header.alphachan); // average PtexUtils::average(temp, rowlen, f.res.u(), f.res.v(), constval, _header.datatype, _header.nchannels); // unmult alpha PtexUtils::divalpha(constval, 1, _header.datatype, _header.nchannels, _header.alphachan); free(temp); } else { // average PtexUtils::average(data, stride, f.res.u(), f.res.v(), constval, _header.datatype, _header.nchannels); } // write const val writeBlock(_fp, constval, _pixelSize); free(constval); // write face data writeFaceData(_fp, data, stride, f.res, efdh.fdh); // update editsize in header editsize = sizeof(efdh) + _pixelSize + efdh.fdh.blocksize(); // rewind and write headers fseeko(_fp, pos, SEEK_SET); writeBlock(_fp, &edittype, sizeof(edittype)); writeBlock(_fp, &editsize, sizeof(editsize)); writeBlock(_fp, &efdh, sizeof(efdh)); fseeko(_fp, 0, SEEK_END); return 1; }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void VoxelDataContainerWriter::execute() { int err = 0; std::stringstream ss; setErrorCondition(err); VoxelDataContainer* m = getVoxelDataContainer(); if(NULL == m) { setErrorCondition(-999); notifyErrorMessage("The Voxel DataContainer Object was NULL", -999); return; } setErrorCondition(0); dataCheck(false, 1, 1, 1); hid_t dcGid = -1; // Create the HDF5 Group for the Data Container err = H5Utilities::createGroupsFromPath(DREAM3D::HDF5::VoxelDataContainerName.c_str(), m_HdfFileId); if (err < 0) { ss.str(""); ss << "Error creating HDF Group " << DREAM3D::HDF5::VoxelDataContainerName << std::endl; setErrorCondition(-60); notifyErrorMessage( ss.str(), err); return; } dcGid = H5Gopen(m_HdfFileId, DREAM3D::HDF5::VoxelDataContainerName.c_str(), H5P_DEFAULT ); if (dcGid < 0) { ss.str(""); ss << "Error opening Group " << DREAM3D::HDF5::VoxelDataContainerName << std::endl; setErrorCondition(-61); notifyErrorMessage( ss.str(), err); return; } // This just writes the header information int64_t volDims[3] = { m->getXPoints(), m->getYPoints(), m->getZPoints() }; float spacing[3] = { m->getXRes(), m->getYRes(), m->getZRes() }; float origin[3] = { 0.0f, 0.0f, 0.0f }; m->getOrigin(origin); err = writeMetaInfo(DREAM3D::HDF5::VoxelDataContainerName, volDims, spacing, origin); if (err < 0) { ss.str(""); ss << ":Error Writing header information to output file" << std::endl; setErrorCondition(-62); notifyErrorMessage( ss.str(), err); H5Gclose(dcGid); // Close the Data Container Group return; } err = writeVertexData(dcGid); if (err < 0) { H5Gclose(dcGid); // Close the Data Container Group return; } err = writeEdgeData(dcGid); if (err < 0) { H5Gclose(dcGid); // Close the Data Container Group return; } err = writeFaceData(dcGid); if (err < 0) { H5Gclose(dcGid); // Close the Data Container Group return; } err = writeCellData(dcGid); if (err < 0) { H5Gclose(dcGid); // Close the Data Container Group return; } err = writeFieldData(dcGid); if (err < 0) { H5Gclose(dcGid); // Close the Data Container Group return; } err = writeEnsembleData(dcGid); if (err < 0) { H5Gclose(dcGid); // Close the Data Container Group return; } // Now finally close the group and the HDf5 File H5Gclose(dcGid); // Close the Data Container Group notifyStatusMessage("Complete"); }