private C3DFile(Stream stream) { C3DReader reader = null; try { reader = new C3DReader(stream); this._processorType = reader.CreateProcessorType; this._header = reader.ReadHeader(); this._parameterDictionary = reader.ReadParameters(); this._frameCollection = new C3DFrameCollection(); try { C3DParameterCache paramCache = C3DParameterCache.CreateCache(this); C3DFrame frame = null; while ((frame = reader.ReadNextFrame(paramCache)) != null) { this._frameCollection.Add(frame); } } catch { } } finally { if (reader != null) { reader.Close(); } } }
private C3DFile() { this._processorType = C3DConstants.FILE_DEFAULT_PROCESSOR_TYPE; this._header = new C3DHeader(); this._parameterDictionary = C3DParameterDictionary.CreateNewParameterDictionary(); this._frameCollection = new C3DFrameCollection(); }
/// <summary> /// 获取是否需要反转数组 /// </summary> /// <param name="type">处理器类型</param> /// <returns>是否需要反转数组</returns> private static Boolean IsNeedReverse(C3DProcessorType type) { if (type == C3DProcessorType.MIPS) { return(BitConverter.IsLittleEndian); //MIPS是Big-Endian,如果机器是Little-Endian需要反转 } return(!BitConverter.IsLittleEndian); //其他情况当机器是Big-Endian才需要反转 }
/// <summary> /// 从字节数组中获取16位浮点数字 /// </summary> /// <param name="type">处理器类型</param> /// <param name="data">字节数组</param> /// <param name="startIndex">数据起始位置</param> /// <returns>16位浮点数字</returns> internal static Int16 ToInt16(C3DProcessorType type, Byte[] data, Int32 startIndex) { if (C3DBitConverter.IsNeedReverse(type)) { Array.Reverse(data, startIndex, 2); } return(BitConverter.ToInt16(data, startIndex)); }
/// <summary> /// 更新事件时间 /// </summary> /// <param name="oldType">原处理器类型</param> private void UpdateHeaderEventTimes(C3DProcessorType oldType) { Int16 count = this.HeaderEventsCount; for (Int16 index = 153; index < 153 + count * 2; index += 2) { this.UpdateSingleRecord(oldType, index); } }
/// <summary> /// 从字节数组中获取32位单精度浮点数字 /// </summary> /// <param name="type">处理器类型</param> /// <param name="data">字节数组</param> /// <param name="startIndex">数据起始位置</param> /// <returns>32位单精度浮点数字</returns> internal static Single ToSingle(C3DProcessorType type, Byte[] data, Int32 startIndex) { if (C3DBitConverter.IsNeedReverse(type)) { Array.Reverse(data, startIndex, 4); } if (type == C3DProcessorType.DEC) { return(new VAXSingle(data, startIndex).ToSingle()); } else { return(BitConverter.ToSingle(data, startIndex)); } }
/// <summary> /// 修正不同处理器文档格式问题 /// </summary> /// <param name="processorType">原处理器类型</param> private void UpdateHeaderContent(C3DProcessorType processorType) { this.UpdateInt16Record(processorType, 2); this.UpdateInt16Record(processorType, 3); this.UpdateInt16Record(processorType, 4); this.UpdateInt16Record(processorType, 5); this.UpdateInt16Record(processorType, 6); this.UpdateSingleRecord(processorType, 7); this.UpdateInt16Record(processorType, 9); this.UpdateInt16Record(processorType, 10); this.UpdateSingleRecord(processorType, 11); this.UpdateInt16Record(processorType, 148); this.UpdateInt16Record(processorType, 149); this.UpdateInt16Record(processorType, 150); this.UpdateInt16Record(processorType, 151); this.UpdateHeaderEventTimes(processorType); }
/// <summary> /// 初始化新的C3D参数项 /// </summary> /// <param name="processorType">原处理器类型</param> /// <param name="id">参数项ID</param> /// <param name="name">参数项名称数据块</param> /// <param name="isLocked">是否锁定</param> /// <param name="lastData">最后一部分数据块</param> internal C3DParameter(C3DProcessorType processorType, SByte id, Byte[] name, Boolean isLocked, Byte[] lastData) : base(id, name, isLocked) { if (lastData == null) { this._parameterType = C3DParameterType.Invalid; this._dimensions = null; this._parameterData = null; return; } this._parameterType = (C3DParameterType)lastData[0]; Int32 dataLength = 1, dimension = lastData[1]; if (dimension > 0) { this._dimensions = new Byte[dimension]; for (Byte i = 0; i < dimension; i++) { this._dimensions[i] = lastData[2 + i]; dataLength *= this._dimensions[i]; } dataLength *= GetParameterDataSize(this._parameterType); } else { this._dimensions = null; dataLength = GetParameterDataSize(this._parameterType); } this._parameterData = new Byte[dataLength]; Array.Copy(lastData, 2 + dimension, this._parameterData, 0, this._parameterData.Length); //修正不同处理器文档格式问题 if (this._parameterType == C3DParameterType.Int16 || this._parameterType == C3DParameterType.Single) { this.UpdateData(this._parameterType, processorType, dimension); } this.SetDescription(lastData, 2 + dimension + this._parameterData.Length); }
public C3DReader(Stream stream) { this._reader = new C3DBinaryReader(stream); this._currentFrameIndex = -1; this._firstParameterBlockIndex = this._reader.ReadByte(); Byte signature = this._reader.ReadByte(); if (signature != C3DConstants.FILE_SIGNATURE) { throw new C3DException("This is not C3D file."); } Int32 paramsStartPosition = (this._firstParameterBlockIndex - 1) * C3DConstants.FILE_SECTION_SIZE; this._reader.BaseStream.Seek(paramsStartPosition + 3, SeekOrigin.Begin);//跳过signature和paramsBlocksCount this._processorType = (C3DProcessorType)this._reader.ReadByte(); this._reader.SetProcessorType(this._processorType); this._reader.BaseStream.Seek(0, SeekOrigin.Begin); }
/// <summary> /// 更新32位单精度浮点数记录 /// </summary> /// <param name="oldType">原处理器类型</param> /// <param name="index">记录索引(从1开始计数,2字节单位)</param> private void UpdateSingleRecord(C3DProcessorType oldType, Int16 index) { Single value = C3DBitConverter.ToSingle(oldType, this._data, (index - 1) * 2); this.SetSingleRecord(index, value); }
/// <summary> /// 更新16位整数记录 /// </summary> /// <param name="oldType">原处理器类型</param> /// <param name="index">记录索引(从1开始计数,2字节单位)</param> private void UpdateInt16Record(C3DProcessorType oldType, Int16 index) { Int16 value = C3DBitConverter.ToInt16(oldType, this._data, (index - 1) * 2); this.SetInt16Record(index, value); }
/// <summary> /// 从已有的数据中初始化C3D文件头 /// </summary> /// <param name="processorType">原处理器类型</param> /// <param name="rawData">原始头数据</param> internal C3DHeader(C3DProcessorType processorType, Byte[] rawData) { this._data = rawData; this.UpdateHeaderContent(processorType); }
/// <summary> /// 更新参数内数据 /// </summary> /// <param name="parameterType">参数类型</param> /// <param name="processorType">处理器类型</param> /// <param name="dimensionCount">维数</param> private void UpdateData(C3DParameterType parameterType, C3DProcessorType processorType, Int32 dimensionCount) { #region Basic Type if (dimensionCount == 0) { if (parameterType == C3DParameterType.Int16) { this.InternalSetData <Int16>(C3DBitConverter.ToInt16(processorType, this._parameterData)); } else if (parameterType == C3DParameterType.Single) { this.InternalSetData <Single>(C3DBitConverter.ToSingle(processorType, this._parameterData)); } return; } #endregion #region 1D-Array if (dimensionCount == 1) { if (parameterType == C3DParameterType.Int16) { Byte[][] datas = this.Get1DByteArray(sizeof(Int16)); Int16[] newdata = new Int16[datas.Length]; for (Int32 i = 0; i < datas.Length; i++) { newdata[i] = C3DBitConverter.ToInt16(processorType, datas[i]); } this.InternalSetData <Int16[]>(newdata); } else if (parameterType == C3DParameterType.Single) { Byte[][] datas = this.Get1DByteArray(sizeof(Single)); Single[] newdata = new Single[datas.Length]; for (Int32 i = 0; i < datas.Length; i++) { newdata[i] = C3DBitConverter.ToSingle(processorType, datas[i]); } this.InternalSetData <Single[]>(newdata); } return; } #endregion #region 2D-Array if (dimensionCount == 2) { if (parameterType == C3DParameterType.Int16) { Byte[, ][] datas = this.Get2DByteArray(sizeof(Int16)); Int16[,] newdata = new Int16[datas.GetLength(0), datas.GetLength(1)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { newdata[x, y] = C3DBitConverter.ToInt16(processorType, datas[x, y]); } } this.InternalSetData <Int16[, ]>(newdata); } else if (parameterType == C3DParameterType.Single) { Byte[, ][] datas = this.Get2DByteArray(sizeof(Single)); Single[,] newdata = new Single[datas.GetLength(0), datas.GetLength(1)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { newdata[x, y] = C3DBitConverter.ToSingle(processorType, datas[x, y]); } } this.InternalSetData <Single[, ]>(newdata); } return; } #endregion #region 3D-Array if (dimensionCount == 3) { if (parameterType == C3DParameterType.Int16) { Byte[, , ][] datas = this.Get3DByteArray(sizeof(Int16)); Int16[, ,] newdata = new Int16[datas.GetLength(0), datas.GetLength(1), datas.GetLength(2)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { for (Int32 z = 0; z < datas.GetLength(2); z++) { newdata[x, y, z] = C3DBitConverter.ToInt16(processorType, datas[x, y, z]); } } } this.InternalSetData <Int16[, , ]>(newdata); } else if (parameterType == C3DParameterType.Single) { Byte[, , ][] datas = this.Get3DByteArray(sizeof(Single)); Single[, ,] newdata = new Single[datas.GetLength(0), datas.GetLength(1), datas.GetLength(2)]; for (Int32 x = 0; x < datas.GetLength(0); x++) { for (Int32 y = 0; y < datas.GetLength(1); y++) { for (Int32 z = 0; z < datas.GetLength(2); z++) { newdata[x, y, z] = C3DBitConverter.ToSingle(processorType, datas[x, y, z]); } } } this.InternalSetData <Single[, , ]>(newdata); } return; } #endregion }
/// <summary> /// 设置处理器类型 /// </summary> /// <param name="type">处理器类型</param> internal void SetProcessorType(C3DProcessorType type) { this._processorType = type; }
/// <summary> /// 从字节数组中获取16位浮点数字 /// </summary> /// <param name="type">处理器类型</param> /// <param name="data">字节数组</param> /// <returns>16位浮点数字</returns> internal static Int16 ToInt16(C3DProcessorType type, Byte[] data) { return(ToInt16(type, data, 0)); }
/// <summary> /// 从字节数组中获取32位单精度浮点数字 /// </summary> /// <param name="type">处理器类型</param> /// <param name="data">字节数组</param> /// <returns>32位单精度浮点数字</returns> internal static Single ToSingle(C3DProcessorType type, Byte[] data) { return(ToSingle(type, data, 0)); }
/// <summary> /// 初始化新的C3D二进制文件读取器 /// </summary> /// <param name="input">C3D文件流</param> internal C3DBinaryReader(Stream input) { this._stream = input; this._processorType = C3DConstants.FILE_DEFAULT_PROCESSOR_TYPE; }