public void WriteBAHeader(bool compressed, List <B45ChunkDataBase> chunkDataList) { fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Write); byte[] tmp = new byte[2]; if (compressed) { // compressed tmp[1] = (byte)(tmp[1] | 0x1); } else { } fs.Write(tmp, 0, 2); byte[] headerBuffer = new byte[FileHeaderOffsets.headerLength]; headerBuffer[FileHeaderOffsets.cellLength] = fileHeader.cellLength; headerBuffer[FileHeaderOffsets.chunkType] = fileHeader.chunkType; headerBuffer[FileHeaderOffsets.chunkSize] = fileHeader.chunkSize; headerBuffer[FileHeaderOffsets.chunkPrefix] = fileHeader.chunkPrefix; headerBuffer[FileHeaderOffsets.chunkPostfix] = fileHeader.chunkPostfix; ByteArrayHelper.ushort_to(headerBuffer, FileHeaderOffsets.chunkCountX, fileHeader.chunkCountX); ByteArrayHelper.ushort_to(headerBuffer, FileHeaderOffsets.chunkCountY, fileHeader.chunkCountY); ByteArrayHelper.ushort_to(headerBuffer, FileHeaderOffsets.chunkCountZ, fileHeader.chunkCountZ); ByteArrayHelper.ushort_to(headerBuffer, FileHeaderOffsets.voxelRes, fileHeader.voxelRes); headerBuffer[FileHeaderOffsets.chunkOffsetDesc] = fileHeader.chunkOffsetDesc; fs.Write(headerBuffer, 0, (int)FileHeaderOffsets.headerLength); //chkOfsUnitLen = fileHeader.chunkOffsetDesc & 0x7; chkOfsInVectorForm = ((fileHeader.chunkOffsetDesc >> 4) > 0); int chkOfsLen = 4 + chunkDataList.Count * ChunkOffsetStructOffsets.length; byte[] chunkOffsetBinaryBuffer = new byte[chkOfsLen]; chunkRawDataLength = fileHeader.chunkSize * fileHeader.chunkSize * fileHeader.chunkSize * fileHeader.cellLength; if (chkOfsInVectorForm) { // write the chunk count ByteArrayHelper.int_to(chunkOffsetBinaryBuffer, 0, chunkDataList.Count); chunkOffsets = new int[chunkDataList.Count + 1]; // calculate the chunk offsets first chunkOffsets[0] = 0; int i; int lastChunkLength; for (i = 1; i <= chunkDataList.Count; i++) { lastChunkLength = chunkDataList[i - 1].IsHollow ? ChunkBAHeaderOffsets.headerLength + fileHeader.cellLength : ChunkBAHeaderOffsets.headerLength + chunkRawDataLength; chunkOffsets[i] = chunkOffsets[i - 1] + lastChunkLength; } // write the last length to the extra offset slot for write chunk data reference. // write the offsets into the buffer. phase 1 for (i = 0; i < chunkDataList.Count; i++) { B45ChunkDataBase cd = chunkDataList[i]; // write the chunk pos // in "4 + i * (chkOfsUnitLen * 3 + 12)" the 4 is the length of the chunkcount int at the beginning ByteArrayHelper.IntVector3_to(chunkOffsetBinaryBuffer, 4 + i * ChunkOffsetStructOffsets.length, cd._chunkPos); // write the chunk offset relative to the end of the offset buffer ByteArrayHelper.int_to(chunkOffsetBinaryBuffer, 4 + i * ChunkOffsetStructOffsets.length + ChunkOffsetStructOffsets.Offset, chunkOffsets[i]); // write the svn number ByteArrayHelper.int_to(chunkOffsetBinaryBuffer, 4 + i * ChunkOffsetStructOffsets.length + ChunkOffsetStructOffsets.SVN_key_ba, cd.svn_key_ba); } fs.Write(chunkOffsetBinaryBuffer, 0, chkOfsLen); } else { // it's the chunk offset full listing mode } // phase 2 WriteChunkData(chunkDataList); fs.Close(); }
public void WriteUVHeader(bool compressed, List <B45ChunkDataBase> chunkDataList) { fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Write); byte headerByte = chunkOffsetDesc; if (compressed) { // compressed headerByte = (byte)(headerByte | 32); } else { } fs.WriteByte(headerByte); //chkOfsUnitLen = chunkOffsetDesc & 0x7; chkOfsInVectorForm = ((chunkOffsetDesc >> 4) > 0); int chkOfsLen = 4 + chunkDataList.Count * ChunkOffsetStructOffsets.length; byte[] chunkOffsetBinaryBuffer = new byte[chkOfsLen]; if (chkOfsInVectorForm) { // write the chunk count ByteArrayHelper.int_to(chunkOffsetBinaryBuffer, 0, chunkDataList.Count); chunkOffsets = new int[chunkDataList.Count + 1]; // calculate the chunk offsets first chunkOffsets[0] = 0; int i; int lastChunkLength; for (i = 1; i <= chunkDataList.Count; i++) { List <UVKeyCount> uvkc = chunkDataList[i - 1].uvVersionKeys; int uvkc_len = 0; if (uvkc != null) { uvkc_len = uvkc.Count * UVKeyCount.length; } List <UpdateVector> uvs = chunkDataList[i - 1].updateVectors; int uv_len = 0; if (uvs != null) { uv_len = uvs.Count * UpdateVector.length; } lastChunkLength = ChunkUVHeaderOffsets.headerLength + uvkc_len + uv_len; chunkOffsets[i] = chunkOffsets[i - 1] + lastChunkLength; } // write the last length to the extra offset slot for write chunk data reference. // write the offsets into the buffer. phase 1 for (i = 0; i < chunkDataList.Count; i++) { B45ChunkDataBase cd = chunkDataList[i]; // write the chunk pos // in "4 + i * (chkOfsUnitLen * 3 + 12)" the 4 is the length of the chunkcount int at the beginning ByteArrayHelper.IntVector3_to(chunkOffsetBinaryBuffer, 4 + i * ChunkOffsetStructOffsets.length, cd._chunkPos); // write the chunk offset relative to the end of the offset buffer ByteArrayHelper.int_to(chunkOffsetBinaryBuffer, 4 + i * ChunkOffsetStructOffsets.length + ChunkOffsetStructOffsets.Offset, chunkOffsets[i]); // write the svn number ByteArrayHelper.int_to(chunkOffsetBinaryBuffer, 4 + i * ChunkOffsetStructOffsets.length + ChunkOffsetStructOffsets.SVN_key, cd.svn_key); } fs.Write(chunkOffsetBinaryBuffer, 0, chkOfsLen); } else { // it's the chunk offset full listing mode } // phase 2 WriteUVData(chunkDataList); fs.Close(); }