public static void Write(DICOMBinaryWriter dw, VR vr, DICOMWriteSettings settings, int length) { var lengthBytes = BitConverter.GetBytes(length); if (settings.TransferSyntax != TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN) { switch (VRDictionary.GetEncodingFromVR(vr)) { case VREncoding.ExplicitLong: dw.WriteNullBytes(2); lengthBytes = BitConverter.GetBytes(length); break; case VREncoding.ExplicitShort: lengthBytes = BitConverter.GetBytes((ushort)length); if (length > 65536) { throw new ArgumentOutOfRangeException( "Length is greater than allowed for explicit VR syntax. Try using implicit VR"); } break; case VREncoding.Implicit: lengthBytes = BitConverter.GetBytes(length); break; } } if (settings.TransferSyntax == TransferSyntax.EXPLICIT_VR_BIG_ENDIAN) { Array.Reverse(lengthBytes); } dw.Write(lengthBytes); }
public static void Write(DICOMBinaryWriter dw, VR vr, DICOMWriteSettings settings, int length) { var lengthBytes = new byte[0]; if (!(settings.TransferSyntax == TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN)) { switch (VRDictionary.GetEncodingFromVR(vr)) { case VREncoding.ExplicitLong: dw.WriteNullBytes(2); lengthBytes = BitConverter.GetBytes(length); break; case VREncoding.ExplicitShort: lengthBytes = BitConverter.GetBytes((ushort)length); break; case VREncoding.Implicit: lengthBytes = BitConverter.GetBytes(length); break; } } else if (settings.TransferSyntax == TransferSyntax.EXPLICIT_VR_BIG_ENDIAN) { lengthBytes = BitConverter.GetBytes(length); lengthBytes.Reverse(); } else { //Explicit VR Little Endian lengthBytes = BitConverter.GetBytes(length); } dw.Write(lengthBytes); }
/// <summary> /// Reads the length in big endian byte format from a series of bytes in a stream. The number of bytes is automatically /// determined from /// VR. /// </summary> /// <param name="vr">the value representation of the element</param> /// <param name="dr">the binary stream with a current position on the length parameter</param> /// <returns></returns> public static int ReadBigEndian(VR vr, DICOMBinaryReader dr) { byte[] bytes = null; switch (VRDictionary.GetEncodingFromVR(vr)) { case VREncoding.Implicit: bytes = dr.ReadBytes(4); break; case VREncoding.ExplicitLong: bytes = dr.Skip(2).ReadBytes(4); break; case VREncoding.ExplicitShort: bytes = dr.ReadBytes(2); break; } return(ReadBigEndian(bytes)); }
public static int PeekBigEndian(VR vr, DICOMBinaryReader dr) { var bytes = new byte[0]; switch (VRDictionary.GetEncodingFromVR(vr)) { case VREncoding.Implicit: bytes = dr.Peek(4); break; case VREncoding.ExplicitLong: bytes = dr.Peek(6).Skip(2).Take(4).ToArray(); break; case VREncoding.ExplicitShort: bytes = dr.Peek(2); break; } return(ReadBigEndian(bytes));; }
/// <summary> /// Reads the length in little endian byte format from a series of bytes in a stream. The number of bytes is /// automatically determined from /// VR. /// </summary> /// <param name="vr">the value representation of the element</param> /// <param name="dr">the binary stream with a current position on the length parameter</param> /// <returns></returns> public static int ReadLittleEndian(VR vr, DICOMBinaryReader dr) { int length = 0; switch (VRDictionary.GetEncodingFromVR(vr)) { case VREncoding.Implicit: byte[] byteLength = dr.ReadBytes(4); length = BitConverter.ToInt32(byteLength, 0); break; case VREncoding.ExplicitLong: byteLength = dr.Skip(2).ReadBytes(4); length = BitConverter.ToInt32(byteLength, 0); break; case VREncoding.ExplicitShort: byteLength = dr.ReadBytes(2); length = BitConverter.ToUInt16(byteLength, 0); break; } return(length); }
public static int PeekLittleEndian(VR vr, DICOMBinaryReader dr) { var length = 0; switch (VRDictionary.GetEncodingFromVR(vr)) { case VREncoding.Implicit: var byteLength = dr.Peek(4); length = BitConverter.ToInt32(byteLength, 0); break; case VREncoding.ExplicitLong: byteLength = dr.Peek(6).Skip(2).Take(4).ToArray(); length = BitConverter.ToInt32(byteLength, 0); break; case VREncoding.ExplicitShort: byteLength = dr.Peek(2); length = BitConverter.ToUInt16(byteLength, 0); break; } return(length); }