/// <summary> /// Reads the length in little endian byte format from a series of bytes in a stream /// </summary> /// <param name="dr">the binary stream with a current position on the length parameter</param> /// <param name="length">the number of bytes containing the length</param> /// <returns>the length</returns> public static int ReadLittleEndian(DICOMBinaryReader dr, int length) { switch (length) { case 2: return BitConverter.ToUInt16(dr.Take(2), 0); case 4: return BitConverter.ToInt32(dr.Take(4), 0); default: return 0; } }
/// <summary> /// Reads the length in big endian byte format from a series of bytes in a stream /// </summary> /// <param name="dr">the binary stream with a current position on the length parameter</param> /// <param name="length">the number of bytes containing the length</param> /// <returns>the length</returns> public static int ReadBigEndian(DICOMBinaryReader dr, int length) { switch (length) { case 2: return BitConverter.ToInt16(dr.Take(2).Reverse().ToArray(), 0); case 4: return BitConverter.ToInt32(dr.Take(4).Reverse().ToArray(), 0); default: return 0; } }
/// <summary> /// Reads the length in little endian byte format from a series of bytes in a stream /// </summary> /// <param name="dr">the binary stream with a current position on the length parameter</param> /// <param name="length">the number of bytes containing the length</param> /// <returns>the length</returns> public static int ReadLittleEndian(DICOMBinaryReader dr, int length) { switch (length) { case 2: return(BitConverter.ToUInt16(dr.Take(2), 0)); case 4: return(BitConverter.ToInt32(dr.Take(4), 0)); default: return(0); } }
/// <summary> /// Reads the length in big endian byte format from a series of bytes in a stream /// </summary> /// <param name="dr">the binary stream with a current position on the length parameter</param> /// <param name="length">the number of bytes containing the length</param> /// <returns>the length</returns> public static int ReadBigEndian(DICOMBinaryReader dr, int length) { switch (length) { case 2: return(BitConverter.ToUInt16(dr.Take(2).Reverse().ToArray(), 0)); case 4: return(BitConverter.ToInt32(dr.Take(4).Reverse().ToArray(), 0)); default: return(0); } }
public static PDVItem ReadPDVItem(DICOMBinaryReader dr) { var pi = new PDVItem(); int length = LengthReader.ReadBigEndian(dr, 4); pi.PresentationContextID = dr.Take(1)[0]; pi.Fragment = ReadPDVFragment(dr, length - 1); return pi; }
public static PDVItemFragment ReadPDVFragment(DICOMBinaryReader dr, int length) { var pif = new PDVItemFragment(); byte messageHeader = dr.Take(1)[0]; pif.IsCommandObject = messageHeader.GetBit(0); pif.IsLastItem = messageHeader.GetBit(1); pif.Data = dr.ReadBytes(length - 1); return pif; }
/// <summary> /// Reads the first 132 bits of a file to check if it contains the DICOM preamble. /// </summary> /// <param name="dr">a stream containing the bits of the file</param> /// <returns>a boolean indicating whether or not the DICOM preamble was in the file</returns> public static bool Read(DICOMBinaryReader dr) { if (dr.StreamLength > 132) { byte[] nullPreamble = dr.Take(128); if (nullPreamble.Any(b => b != 0x00)) { L.Instance.Log("Missing 128 byte null byte preamble.", LogPriority.WARNING); } //READ D I C M byte[] dcm = dr.Take(4); if (dcm[0] != 'D' || dcm[1] != 'I' || dcm[2] != 'C' || dcm[3] != 'M') { L.Instance.Log("Missing characters D I C M in bits 128-131.", LogPriority.WARNING); dr.StreamPosition -= 132; //Rewind return(false); } } return(true); }
/// <summary> /// Reads the first 132 bits of a file to check if it contains the DICOM preamble. /// </summary> /// <param name="dr">a stream containing the bits of the file</param> /// <returns>a boolean indicating whether or not the DICOM preamble was in the file</returns> public static bool Read(DICOMBinaryReader dr) { if (dr.StreamLength > 132) { var nullPreamble = dr.Take(128); if (nullPreamble.Any(b => b != 0x00)) { _logger.LogWarning("Missing 128 byte null byte preamble."); } //READ D I C M var dcm = dr.Take(4); if (dcm[0] != 'D' || dcm[1] != 'I' || dcm[2] != 'C' || dcm[3] != 'M') { _logger.LogWarning("Missing characters D I C M in bits 128-131."); dr.StreamPosition -= 132; //Rewind return(false); } } return(true); }
/// <summary> /// Reads the first 132 bits of a file to check if it contains the DICOM preamble. /// </summary> /// <param name="dr">a stream containing the bits of the file</param> /// <returns>a boolean indicating whether or not the DICOM preamble was in the file</returns> public static bool Read(DICOMBinaryReader dr) { if (dr.StreamLength > 132) { byte[] nullPreamble = dr.Take(128); if (nullPreamble.Any(b => b != 0x00)) { L.Instance.Log("Missing 132 null byte preamble.", LogPriority.WARNING); dr.StreamPosition -= 128; //rewind return false; } //READ D I C M byte[] dcm = dr.Take(4); if (dcm[0] != 'D' || dcm[1] != 'I' || dcm[2] != 'C' || dcm[3] != 'M') { L.Instance.Log("Missing characters D I C M in bits 128-131.", LogPriority.WARNING); return false; } } return true; }
private static PresentationContext ReadPresentationCtxContents(byte[] contents, bool requestType = false) { var pc = new PresentationContext(); pc.TransferSyntaxes = new List<string>(); using (var dr = new DICOMBinaryReader(contents)) { pc.Id = dr.Take(1)[0]; dr.Skip(1); //Reserved Null Byte pc.Reason = (PresentationContextReason) Enum.ToObject(typeof (PresentationContextReason), dr.Take(1)[0]); dr.Skip(1); //Reserved Null Byte if (requestType) { pc.AbstractSyntax = ReadAbstractSyntax(dr).Trim(); } while (dr.StreamPosition < dr.StreamLength) { long initPos = dr.StreamPosition; pc.TransferSyntaxes.Add(ReadTransferSyntax(dr)); if (dr.StreamPosition == initPos) { break; } } } return pc; }
public static PresentationContext ReadPresentationCtxAccept(DICOMBinaryReader dr) { AssertItemType(dr, "Presentation Context Accept", ItemType.PRESENTATION_CONTEXT_ACCEPT); dr.Skip(2); // PDU id Reserved Null Byte int length = LengthReader.ReadBigEndian(dr, 2); return ReadPresentationCtxContents(dr.Take(length)); }