public void WriteDecimalString() { var ds = new DecimalString(); ds.DData_ = Enumerable.Range(1, 15000).Select(i => ((double)i) + 0.005).ToList(); ds.Tag = new Tag("00082130"); byte[] written; var settings = DICOMWriteSettings.Default(); using (var ms = new MemoryStream()) { using (var dw = new DICOMBinaryWriter(ms)) { DICOMElementWriter.Write(dw, DICOMWriteSettings.Default(), ds); } written = ms.ToArray(); } using (var dr = new DICOMBinaryReader(written)) { var read = DICOMElementReader.ReadElementImplicitLittleEndian(dr) as DecimalString; CollectionAssert.AreEqual(ds.DData_, read.Data_); } }
private static string ReadUIDItem(DICOMBinaryReader dr, string itemName, ItemType iType) { AssertItemType(dr, itemName, iType); dr.Skip(2); // PDU ID and Reserved Null Byte int length = LengthReader.ReadBigEndian(dr, 2); return dr.ReadString(length).Trim(); }
public static void SkipItemLittleEndian(DICOMBinaryReader dr, TransferSyntax syntax) { int length = LengthReader.ReadLittleEndian(VR.Null, dr.Skip(4)); if (length != -1) { dr.Skip(length); } else { if (syntax == TransferSyntax.EXPLICIT_VR_LITTLE_ENDIAN) { while (!IsEndOfSequenceItemLittleEndian(dr)) { dr.StreamPosition -= 8; DICOMElementReader.SkipElementExplicitLittleEndian(dr); } } else { while (!IsEndOfSequenceItemLittleEndian(dr)) { dr.StreamPosition -= 8; DICOMElementReader.SkipElementImplicitLittleEndian(dr); } } } }
public static int? ReadMaxLength(DICOMBinaryReader dr) { AssertItemType(dr, "Maximum Length", ItemType.MAXIMUM_LENGTH); dr.Skip(2); // PDU ID and Reserved Null Byte int length = LengthReader.ReadBigEndian(dr, 2); return LengthReader.ReadBigEndian(dr, 4); }
public static VR PeekVR(DICOMBinaryReader dr) { byte[] vrBytes = dr.Peek(2); char[] vr = Encoding.UTF8.GetChars(vrBytes); VR foundVR = VRDictionary.GetVRFromAbbreviation(new string(vr)); return foundVR; }
/// <summary> /// Read explicit VR little endian up to transfer syntax element and determines transfer syntax for rest of elements /// </summary> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <param name="syntax">the transfer syntax of the DICOM file</param> /// <returns>elements preceeding and including transfer syntax element</returns> private static List<IDICOMElement> ReadFileMetadata(DICOMBinaryReader dr, out TransferSyntax syntax) { List<IDICOMElement> elements = new List<IDICOMElement>(); syntax = TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN; while (dr.StreamPosition < dr.StreamLength) { long position = dr.StreamPosition; if (TagReader.ReadLittleEndian(dr).Group == _MetaGroup) { dr.StreamPosition = position; IDICOMElement el = DICOMElementReader.ReadElementExplicitLittleEndian(dr); if (el.Tag.CompleteID == TagHelper.TRANSFER_SYNTAX_UID.CompleteID) { syntax = TransferSyntaxHelper.GetSyntax(el); } elements.Add(el); } else { dr.StreamPosition = position; break; } } return elements; }
/// <summary> /// Read explicit VR little endian up to transfer syntax element and determines transfer syntax for rest of elements /// </summary> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <param name="syntax">the transfer syntax of the DICOM file</param> /// <returns>elements preceeding and including transfer syntax element</returns> public static List<IDICOMElement> ReadFileMetadata(DICOMBinaryReader dr, ref TransferSyntax syntax) { var elements = new List<IDICOMElement>(); syntax = syntax != TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN ? syntax : TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN; while (dr.StreamPosition < dr.StreamLength) { long position = dr.StreamPosition; if (TagReader.ReadLittleEndian(dr).Group == _metaGroup) { dr.StreamPosition = position; IDICOMElement el = DICOMElementReader.ReadElementExplicitLittleEndian(dr); Tag uid = TagHelper.TRANSFER_SYNTAX_UID; if (el.Tag == uid) { syntax = TransferSyntaxHelper.GetSyntax(el); } elements.Add(el); } else { dr.StreamPosition = position; break; } } return elements; }
public static PresentationContext ReadPresentationCtxRequest(DICOMBinaryReader dr) { AssertItemType(dr, "Presentation Context Request", ItemType.PRESENTATION_CONTEXT_REQUEST); dr.Skip(2); // PDU id Reserved Null Byte int length = LengthReader.ReadBigEndian(dr, 2); return ReadPresentationCtxContents(dr.Take(length), true); }
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; }
/// <summary> /// Reads and returns the next DICOM element starting at the current location in the DICOM binary reader /// </summary> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <returns>the next DICOM element</returns> public static IDICOMElement ReadElementExplicitBigEndian(DICOMBinaryReader dr) { Tag tag = TagReader.ReadBigEndian(dr); VR vr = VRReader.Read(dr); int length = LengthReader.ReadBigEndian(vr, dr); byte[] data = DataReader.ReadBigEndian(length, dr); return ElementFactory.GenerateElement(tag, vr, data, TransferSyntax.EXPLICIT_VR_BIG_ENDIAN); }
/// <summary> /// Reads and returns all elements in implicit little endian format /// </summary> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <returns>DICOM elements read</returns> public static List<IDICOMElement> ReadAllElementsImplicitLittleEndian(DICOMBinaryReader dr) { List<IDICOMElement> elements = new List<IDICOMElement>(); while (dr.StreamPosition < dr.StreamLength) { elements.Add(ReadElementImplicitLittleEndian(dr)); } return elements; }
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 and returns the next DICOM element starting at the current location in the DICOM binary reader /// </summary> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <returns>the next DICOM element</returns> public static IDICOMElement ReadElementImplicitLittleEndian(DICOMBinaryReader dr) { Tag tag = TagReader.ReadLittleEndian(dr); VR vr = TagDictionary.GetVRFromTag(tag); int length = LengthReader.ReadLittleEndian(VR.Null, dr); byte[] data = DataReader.ReadLittleEndian(length, dr, TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN); IDICOMElement el = ElementFactory.GenerateElement(tag, vr, data, TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN); return el; }
public static DICOMObject ReadObject(byte[] objectBytes, TransferSyntax syntax) { List<IDICOMElement> elements; using (DICOMBinaryReader dr = new DICOMBinaryReader(objectBytes)) { elements = DICOMElementReader.ReadAllElements(dr, syntax); } return new DICOMObject(elements); }
public static AsyncOperations ReadAsyncOperations(DICOMBinaryReader dr) { AssertItemType(dr, "Async Operations", ItemType.ASYNCHRONOUS_OPERATIONS_WINDOW); var ao = new AsyncOperations(); dr.Skip(2); // // PDU ID and Reserved Null Byte int length = LengthReader.ReadBigEndian(dr, 2); ao.MaxInvokeOperations = LengthReader.ReadBigEndian(dr, 2); ao.MaxPerformOperations = LengthReader.ReadBigEndian(dr, 2); return ao; }
/// <summary> /// Reads the length 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> /// <param name="syntax">the transfer syntax of this element</param> /// <returns></returns> public static int Read(VR vr, DICOMBinaryReader dr, TransferSyntax syntax) { switch (syntax) { case TransferSyntax.EXPLICIT_VR_BIG_ENDIAN: return ReadBigEndian(vr, dr); default: return ReadLittleEndian(vr, dr); } }
public static VR ReadVR(DICOMBinaryReader dr) { char[] vr = dr.ReadChars(2); VR foundVR = VRDictionary.GetVRFromAbbreviation(new string(vr)); if (foundVR == VR.Null) { throw new Exception(ExceptionHelper.VRReadException); } return foundVR; }
public static int ReadIndefiniteLengthLittleEndian(DICOMBinaryReader dr, TransferSyntax syntax) { long startingPos = dr.StreamPosition; while (!IsEndOfSequenceLittleEndian(dr)) { dr.StreamPosition -= 8; SequenceItemReader.SkipItemLittleEndian(dr, syntax); } return CalculateLength(dr, startingPos)-8; }
public static DICOMObject ReadObject(byte[] objectBytes, TransferSyntax syntax, out long bytesRead) { List<IDICOMElement> elements; using (var dr = new DICOMBinaryReader(objectBytes)) { elements = DICOMElementReader.ReadAllElements(dr, syntax); bytesRead = dr.StreamPosition; } return new DICOMObject(elements); }
public static int ReadIndefiniteLengthBigEndian(DICOMBinaryReader dr) { long startingPos = dr.StreamPosition; while (!IsEndOfSequenceBigEndian(dr)) { dr.StreamPosition -= 8; SequenceItemReader.SkipItemBigEndian(dr); } return CalculateLength(dr, startingPos)-8; }
/// <summary> /// Read the meta data from the DICOM object /// </summary> /// <param name="filePath">the path to the DICOM file</param> /// <returns>a DICOM object containing the metadata elements</returns> public static DICOMObject ReadFileMetadata(string filePath) { TransferSyntax syntax; List<IDICOMElement> metaElements; using (DICOMBinaryReader dr = new DICOMBinaryReader(filePath)) { DICOMPreambleReader.Read(dr); metaElements= ReadFileMetadata(dr, out syntax); } return new DICOMObject(metaElements); }
/// <summary> /// Read the meta data from the DICOM object /// </summary> /// <param name="filePath">the path to the DICOM file</param> /// <returns>a DICOM object containing the metadata elements</returns> public static DICOMObject ReadFileMetadata(string filePath) { var syntax = TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN; List<IDICOMElement> metaElements; using (var dr = new DICOMBinaryReader(filePath)) { DICOMPreambleReader.Read(dr); metaElements = ReadFileMetadata(dr, ref syntax); } return new DICOMObject(metaElements); }
/// <summary> /// Reads the data from an element encoded in big endian byte order /// </summary> /// <param name="lengthToRead">the length of the data</param> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <returns>the data from this element</returns> public static byte[] ReadBigEndian(int lengthToRead, DICOMBinaryReader dr) { if (lengthToRead != -1) { return dr.ReadBytes(lengthToRead); } int length = SequenceReader.ReadIndefiniteLengthBigEndian(dr); byte[] seqBytes = dr.ReadBytes(length); dr.Skip(8); return seqBytes; }
/// <summary> /// Reads the data from an element encoded in little endian byte order /// </summary> /// <param name="lengthToRead">the length of the data</param> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <returns>the data from this element</returns> public static byte[] ReadLittleEndian(int lengthToRead, DICOMBinaryReader dr, TransferSyntax syntax) { if (lengthToRead != -1) { return dr.ReadBytes(lengthToRead); } int length = SequenceReader.ReadIndefiniteLengthLittleEndian(dr, syntax); byte[] seqBytes = dr.ReadBytes(length); dr.Skip(8); return seqBytes; }
/// <summary> /// This method helps read non-compliant files. Sometimes, an supposed implicit is encoded explicitly. We'll check here /// Returns true if element is actually encoded explicitly (VR is written as starting characters). /// </summary> /// <param name="tag">the read tag</param> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <param name="vr">the determined VR from the tag</param> /// <returns></returns> private static bool CheckForExplicitness(Tag tag, DICOMBinaryReader dr, ref VR vr) { if (VRReader.PeekVR(dr) != VR.Null) { vr = VRReader.ReadVR(dr); Logging.EvilLogger.Instance.Log($"{tag} was expectd to be implicit LE but is explicit LE. Attempting to read..."); return true; } //Implicilty encoded - All is well return false; }
/// <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 a DICOM file from a byte array /// </summary> /// <param name="fileBytes">the bytes of the DICOM file</param> /// <returns>a DICOM object containing all elements</returns> public static DICOMObject Read(byte[] fileBytes, TransferSyntax trySyntax = TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN) { TransferSyntax syntax = trySyntax; //Will keep if metadata doesn't exist List<IDICOMElement> elements; using (var dr = new DICOMBinaryReader(fileBytes)) { DICOMPreambleReader.Read(dr); List<IDICOMElement> metaElements = ReadFileMetadata(dr, ref syntax); elements = metaElements.Concat(DICOMElementReader.ReadAllElements(dr, syntax)).ToList(); } return new DICOMObject(elements); }
/// <summary> /// Reads a DICOM file from a byte array /// </summary> /// <param name="fileBytes">the bytes of the DICOM file</param> /// <returns>a DICOM object containing all elements</returns> public static DICOMObject Read(byte[] fileBytes) { TransferSyntax syntax; List<IDICOMElement> elements; using (DICOMBinaryReader dr = new DICOMBinaryReader(fileBytes)) { DICOMPreambleReader.Read(dr); List<IDICOMElement> metaElements = ReadFileMetadata(dr, out syntax); elements = metaElements.Union(DICOMElementReader.ReadAllElements(dr, syntax)).ToList(); } return new DICOMObject(elements); }
/// <summary> /// Reads a DICOM file from a path /// </summary> /// <param name="stream">the stream to the DICOM file</param> /// <returns>a DICOM object containing all elements</returns> public static DICOMObject Read(Stream stream, TransferSyntax trySyntax = TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN) { TransferSyntax syntax = trySyntax; List<IDICOMElement> elements; using (var dr = new DICOMBinaryReader(stream)) { DICOMPreambleReader.Read(dr); List<IDICOMElement> metaElements = ReadFileMetadata(dr, ref syntax); elements = metaElements.Concat(DICOMElementReader.ReadAllElements(dr, syntax)).ToList(); } return new DICOMObject(elements); }
/// <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); } }
/// <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 a DICOM file from a path /// </summary> /// <param name="filePath">the path to the DICOM file</param> /// <returns>a DICOM object containing all elements</returns> public static DICOMObject Read(string filePath, TransferSyntax trySyntax = TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN) { var syntax = trySyntax; List <IDICOMElement> elements; using (var dr = new DICOMBinaryReader(filePath)) { DICOMPreambleReader.Read(dr); var metaElements = ReadFileMetadata(dr, ref syntax); elements = metaElements.Concat(DICOMElementReader.ReadAllElements(dr, syntax)).ToList(); } return(new DICOMObject(elements)); }
/// <summary> /// Reads the data from an element encoded in little endian byte order /// </summary> /// <param name="lengthToRead">the length of the data</param> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <returns>the data from this element</returns> public static byte[] ReadLittleEndian(int lengthToRead, DICOMBinaryReader dr, TransferSyntax syntax) { if (lengthToRead != -1) { return(dr.ReadBytes(lengthToRead)); } else { int length = SequenceReader.ReadIndefiniteLengthLittleEndian(dr, syntax); byte[] seqBytes = dr.ReadBytes(length); dr.Skip(8); return(seqBytes); } }
/// <summary> /// Reads the data from an element encoded in big endian byte order /// </summary> /// <param name="lengthToRead">the length of the data</param> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <returns>the data from this element</returns> public static byte[] ReadBigEndian(int lengthToRead, DICOMBinaryReader dr) { if (lengthToRead != -1) { return(dr.ReadBytes(lengthToRead)); } else { int length = SequenceReader.ReadIndefiniteLengthBigEndian(dr); byte[] seqBytes = dr.ReadBytes(length); dr.Skip(8); return(seqBytes); } }
/// <summary> /// Reads a DICOM file from a byte array /// </summary> /// <param name="fileBytes">the bytes of the DICOM file</param> /// <returns>a DICOM object containing all elements</returns> public static DICOMObject Read(byte[] fileBytes, TransferSyntax trySyntax = TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN) { TransferSyntax syntax = trySyntax; //Will keep if metadata doesn't exist List <IDICOMElement> elements; using (var dr = new DICOMBinaryReader(fileBytes)) { DICOMPreambleReader.Read(dr); List <IDICOMElement> metaElements = ReadFileMetadata(dr, ref syntax); elements = metaElements.Concat(DICOMElementReader.ReadAllElements(dr, syntax)).ToList(); } return(new DICOMObject(elements)); }
public static void SkipElementImplicitLittleEndian(DICOMBinaryReader dr) { var tag = TagReader.ReadLittleEndian(dr); var length = LengthReader.ReadLittleEndian(VR.Null, dr); if (length != -1) { dr.Skip(length); } else { dr.Skip(SequenceReader.ReadIndefiniteLengthLittleEndian(dr, TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN)); dr.Skip(8); } }
/// <summary> /// Asynchronously reads a DICOM file from a path /// </summary> /// <param name="filePath">the path to the DICOM file</param> /// <returns>a DICOM object containing all elements</returns> public static async Task <DICOMObject> ReadAsync(string filePath, TransferSyntax trySyntax = TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN) { return(await Task.Run(() => { TransferSyntax syntax = trySyntax; List <IDICOMElement> elements; using (var dr = new DICOMBinaryReader(filePath)) { DICOMPreambleReader.Read(dr); List <IDICOMElement> metaElements = ReadFileMetadata(dr, ref syntax); elements = metaElements.Concat(DICOMElementReader.ReadAllElements(dr, syntax)).ToList(); } return new DICOMObject(elements); })); }
public static void SkipElementExplicitBigEndian(DICOMBinaryReader dr) { var tag = TagReader.ReadBigEndian(dr); var vr = VRReader.ReadVR(dr); var length = LengthReader.ReadBigEndian(vr, dr); if (length != -1) { dr.Skip(length); } else { dr.Skip(SequenceReader.ReadIndefiniteLengthBigEndian(dr)); dr.Skip(8); } }
public static void SkipElementExplicitLittleEndian(DICOMBinaryReader dr) { Tag tag = TagReader.ReadLittleEndian(dr); VR vr = VRReader.ReadVR(dr); int length = LengthReader.ReadLittleEndian(vr, dr); if (length != -1) { dr.Skip(length); } else { dr.Skip(SequenceReader.ReadIndefiniteLengthLittleEndian(dr, TransferSyntax.EXPLICIT_VR_LITTLE_ENDIAN)); dr.Skip(8); } }
public static DICOMObject ReadBigEndian(DICOMBinaryReader dr, TransferSyntax syntax) { DICOMObject d; int length = LengthReader.ReadLittleEndian(VR.Null, dr.Skip(4)); if (LengthReader.IsIndefinite(length)) { d = ReadIndefiniteBigEndian(dr, syntax); } else { d = DICOMObjectReader.ReadObject(dr.ReadBytes(length), syntax); } return(d); }
public static void SkipItemBigEndian(DICOMBinaryReader dr) { var length = LengthReader.ReadBigEndian(VR.Null, dr.Skip(4)); if (length != -1) { dr.Skip(length); } else { while (!IsEndOfSequenceItemBigEndian(dr) && dr.StreamLength != dr.StreamPosition) { dr.StreamPosition -= 8; DICOMElementReader.SkipElementExplicitBigEndian(dr); } } }
/// <summary> /// Reads and returns the next DICOM element starting at the current location in the DICOM binary reader /// </summary> /// <param name="dr">the binary reader which is reading the DICOM object</param> /// <returns>the next DICOM element</returns> public static IDICOMElement ReadElementImplicitLittleEndian(DICOMBinaryReader dr) { Tag tag = TagReader.ReadLittleEndian(dr); VR vr = TagDictionary.GetVRFromTag(tag); if (CheckForExplicitness(tag, dr, ref vr)) { return(ReadElementExplicitLittleEndian(tag, vr, dr)); } else { int length = LengthReader.ReadLittleEndian(VR.Null, dr); byte[] data = DataReader.ReadLittleEndian(length, dr, TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN); IDICOMElement el = ElementFactory.GenerateElement(tag, vr, data, TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN); return(el); } }
private static DICOMObject ReadIndefiniteLittleEndian(DICOMBinaryReader dr, TransferSyntax syntax, StringEncoding enc) { var elements = new List <IDICOMElement>(); while (!IsEndOfSequenceItemLittleEndian(dr) && dr.StreamLength != dr.StreamPosition) { dr.StreamPosition -= 8; if (syntax == TransferSyntax.EXPLICIT_VR_LITTLE_ENDIAN) { elements.Add(DICOMElementReader.ReadElementExplicitLittleEndian(dr, enc)); } else { elements.Add(DICOMElementReader.ReadElementImplicitLittleEndian(dr, enc)); } } return(new DICOMObject(elements)); }
public static DICOMObject ReadLittleEndian(DICOMBinaryReader dr, TransferSyntax syntax) { DICOMObject d; //Skip tag dr.Skip(4); int length = LengthReader.ReadLittleEndian(VR.Null, dr); if (LengthReader.IsIndefinite(length)) { d = ReadIndefiniteLittleEndian(dr, syntax); } else { } return(null); }
public static DICOMObject ReadBigEndian(DICOMBinaryReader dr, TransferSyntax syntax, StringEncoding enc) { DICOMObject d; //Skip tag dr.Skip(4); var length = LengthReader.ReadBigEndian(VR.Null, dr); if (LengthReader.IsIndefinite(length)) { d = ReadIndefiniteBigEndian(dr, syntax, enc); } else { d = DICOMObjectReader.ReadObject(dr.ReadBytes(length), syntax); } return(d); }
/// <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 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); }
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));; }
public static List <IDICOMElement> ReadAllElements(DICOMBinaryReader dr, TransferSyntax syntax, StringEncoding enc) { List <IDICOMElement> elements; switch (syntax) { case TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN: elements = ReadAllElementsImplicitLittleEndian(dr, enc); break; case TransferSyntax.EXPLICIT_VR_BIG_ENDIAN: elements = ReadAllElementsExplicitBigEndian(dr, enc); break; default: elements = ReadAllElementsExplicitLittleEndian(dr, enc); break; } return(elements); }
/// <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)); }
/// <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) { byte[] preamble = new byte[132]; try { dr.ReadBytes(preamble, 0, 132); } catch (Exception) { throw new Exception("Could not read 128 null bit preamble. Perhaps file is too short"); } if (preamble[128] != 'D' || preamble[129] != 'I' || preamble[130] != 'C' || preamble[131] != 'M') { Console.WriteLine("Missing characters D I C M in bits 128-131. Technically not a valid DICOM file. Will try to read anyway."); dr.Reset(); return(false); } else { return(true); } }
public static List <DICOMObject> ReadItems(byte[] data, TransferSyntax syntax) { var objects = new List <DICOMObject>(); using (var dr = new DICOMBinaryReader(data)) { while (dr.StreamPosition < dr.StreamLength) { switch (syntax) { case TransferSyntax.EXPLICIT_VR_BIG_ENDIAN: objects.Add(SequenceItemReader.ReadBigEndian(dr, syntax)); break; default: objects.Add(SequenceItemReader.ReadLittleEndian(dr, syntax)); break; } } } return(objects); }
/// <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); }
public static DICOMObject ReadObject(DICOMBinaryReader dr, TransferSyntax syntax) { List <IDICOMElement> elements = DICOMElementReader.ReadAllElements(dr, syntax); return(new DICOMObject(elements)); }
private static bool IsEndOfSequenceItemLittleEndian(DICOMBinaryReader dr) { byte[] bytes = dr.ReadBytes(8); return(ByteHelper.AreEqual(bytes, _endOfSequenceItem_LE)); }
private static bool IsEndOfSequenceItemBigEndian(DICOMBinaryReader dr) { var bytes = dr.ReadBytes(8); return(ByteHelper.AreEqual(bytes, _endOfSequenceItem_BE)); }
public static Tag ReadBigEndian(DICOMBinaryReader dr) { byte[] tag = dr.ReadBytes(4); return(CreateTag(tag)); }
public static Tag ReadLittleEndian(DICOMBinaryReader dr) { byte[] tag = dr.ReadBytes(4); tag = new[] { tag[1], tag[0], tag[3], tag[2] }; return(CreateTag(tag)); }