📄 vr.cs
字号:
// use data set and sequence delimiters return true; } else return tag.TransferSyntax.IsImplicitVR; } /// <summary> /// Re-creates this instance from a DICOM output stream. /// </summary> public void LoadFrom(Stream stream) { streamPosition = stream.Position; DicomContext.Set(stream, tag); if (IsImplicit) { if (Tag.IsUserDefined) // implicit but unknown value representation vrName = "UN"; else // implicit but known value representation; // return new instance, dictionary entry do not have a // transfer syntax vrName = Tag.GetDictionaryEntry().VR.Name; } else { // explicit value representation byte[] buffer = new byte[2]; stream.Read(buffer, 0, 2); vrName = ByteConvert.ToString(buffer, CharacterRepertoire.Default); } DicomContext.Reset(); } /// <summary> /// Creates a new VR instance from a DICOM output stream. /// </summary> /// <param name="stream"> /// Any kind of DICOM output stream. /// </param> /// <returns> /// Output stream position of this instance. /// </returns> public static ValueRepresentation LoadFrom(Stream stream, Tag tag) { DicomContext.Set(stream, tag); if (IsImplicitBy(tag)) { if (tag.IsUserDefined) // implicit but unknown value representation return GetBy("UN", tag); else // implicit but known value representation; // return new instance, dictionary entry do not have a // transfer syntax return GetBy(tag.GetDictionaryEntry().VR.Name, tag); } else { // explicit value representation byte[] buffer = new byte[2]; stream.Read(buffer, 0, 2); string name = ByteConvert.ToString(buffer, CharacterRepertoire.Default); return GetBy(name, tag); } DicomContext.Reset(); } /// <summary> /// DICOM VR string representation. /// </summary> /// <returns> /// A DICOM VR as string of the format "VR". If this instance /// is an undefined VR, an empty string will be returned. /// </returns> public override string ToString() { return Name; } /// <summary> /// DICOM VR detailed string representation. /// </summary> /// <returns> /// A DICOM VR as string of the format "Value Representation (VR)". /// </returns> public virtual string ToLongString() { if ( ! Name.Equals("")) return "Undefined (" + Name + ")"; else return "Undefined"; } protected byte[][] ToImproperMultiValue(byte[] jointMultiValue, int valueLength) { byte[][] result = null; if (jointMultiValue.Length > valueLength) { int count = (int) Math.Floor(jointMultiValue.Length / valueLength); result = new byte[count][]; for (int i = 0; i < count; i++) { result[i] = new byte[valueLength]; Array.Copy(jointMultiValue, i * valueLength, result[i], 0, valueLength); } } else { if (jointMultiValue.Length > 0) result = new byte[1][] {jointMultiValue}; else result = new byte[0][]; } return result; } protected byte[][] ToProperMultiValue(byte[] jointMultiValue, int valueLength) { byte[][] result = null; if (jointMultiValue.Length > valueLength) { int count = 0; if (jointMultiValue.Length % valueLength == 0) count = jointMultiValue.Length / valueLength; else throw new EncodingException( "Joint multi value cannot be seperated into single " + "multi values by the specified value length.", Name + "/jointMultiValue.Length, " + Name + "/valueLength", jointMultiValue.Length.ToString() + ", " + valueLength.ToString()); if (jointMultiValue.Length > 0 && ! Tag.GetDictionaryEntry().VM.IsValid(count)) throw new EncodingException("Count of values is invalid.", Tag, Name + "/VM, " + Name + "/count", Tag.GetDictionaryEntry().VM + ", " + count.ToString()); result = new byte[count][]; for (int i = 0; i < count; i++) { result[i] = new byte[valueLength]; Array.Copy(jointMultiValue, i * valueLength, result[i], 0, valueLength); } } else result = new byte[1][] {jointMultiValue}; return result; } protected byte[] ToJointMultiValue(byte[][] multiValue) { int jointLength = 0; foreach (byte[] value in multiValue) jointLength += value.Length; byte[] result = new byte[jointLength]; int resultIndex = 0; int multiValueIndex = 0; while (multiValueIndex < multiValue.Length) { byte[] value = multiValue[multiValueIndex]; Array.Copy(value, 0, result, resultIndex, value.Length); resultIndex += value.Length; multiValueIndex++; } if (result.Length == 0 || Tag.GetDictionaryEntry().VM.IsValid(multiValue.Length)) return result; else throw new EncodingException("Count of values is invalid.", Tag, Name + "/VM, " + Name + "/multiValue.Length", Tag.GetDictionaryEntry().VM + ", " + multiValue.Length.ToString()); } protected string[] ToImproperMultiValue(string jointMultiValue) { string[] result = jointMultiValue.Split('\\'); return result; } protected string[] ToProperMultiValue(string jointMultiValue) { string[] result = jointMultiValue.Split('\\'); if (jointMultiValue.Length == 0 || Tag.GetDictionaryEntry().VM.IsValid(result.Length)) return result; else throw new EncodingException("Count of values is invalid.", Tag, Name + "/VM, " + Name + "/result.Length", Tag.GetDictionaryEntry().VM + ", " + result.Length.ToString()); } protected string ToJointMultiValue(string[] multiValue) { string result = ""; foreach (string value in multiValue) { if (result.Equals("")) result = value; else result += "\\" + value; } if (result.Length == 0 || Tag.GetDictionaryEntry().VM.IsValid(multiValue.Length)) return result; else throw new EncodingException("Count of values is invalid.", Tag, Name + "/VM, " + Name + "/multiValue.Length", Tag.GetDictionaryEntry().VM + ", " + multiValue.Length.ToString()); } protected virtual Array DecodeProper(byte[] bytes) { return new byte[1][] { bytes }; } protected virtual Array DecodeImproper(byte[] bytes) { return new byte[1][] { bytes }; } /// <summary> /// Determines the correct type and multiplicity of a DICOM value. /// </summary> /// <remarks> /// This method is overwritten by all specific DICOM VR /// implementations. /// </remarks> /// <param name="bytes"> /// DICOM byte array. /// </param> /// <returns> /// DICOM value as array of a specific type. /// </returns> public Array Decode(byte[] bytes) { if (IsStrictDecoded) return DecodeProper(bytes); else return DecodeImproper(bytes); } /// <summary> /// Determines the correct type and multiplicity of a DICOM value /// and converts it to a DICOM value class. /// </summary> /// <param name="bytes"> /// DICOM byte array. /// </param> /// <returns> /// DICOM Value class instance. /// </returns> public Value DecodeToValue(byte[] bytes) { return new Value(this, new ValueLength(this, bytes.Length), Decode(bytes)); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -