Reads in DICOM elements from a DICOM object
Example #1
0
        public static void SkipItemLittleEndian(DICOMBinaryReader dr, TransferSyntax syntax)
        {
            var 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.StreamLength != dr.StreamPosition)
                    {
                        dr.StreamPosition -= 8;
                        DICOMElementReader.SkipElementExplicitLittleEndian(dr);
                    }
                }
                else
                {
                    while (!IsEndOfSequenceItemLittleEndian(dr) && dr.StreamLength != dr.StreamPosition)
                    {
                        dr.StreamPosition -= 8;
                        DICOMElementReader.SkipElementImplicitLittleEndian(dr);
                    }
                }
            }
        }
Example #2
0
        /// <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);
        }
Example #3
0
        /// <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);
        }
Example #4
0
        /// <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, ref StringEncoding enc)
        {
            var elements = new List <IDICOMElement>();

            while (dr.StreamPosition < dr.StreamLength)
            {
                var position = dr.StreamPosition;
                if (TagReader.ReadLittleEndian(dr).Group == _metaGroup)
                {
                    dr.StreamPosition = position;
                    var el = DICOMElementReader.ReadElementExplicitLittleEndian(dr, enc);
                    if (el.Tag == TagHelper.TransferSyntaxUID)
                    {
                        syntax = TransferSyntaxHelper.GetSyntax(el);
                    }
                    elements.Add(el);
                }
                else
                {
                    dr.StreamPosition = position;
                    break;
                }
            }

            return(elements);
        }
        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));
        }
Example #6
0
        private static DICOMObject ReadIndefiniteBigEndian(DICOMBinaryReader dr, TransferSyntax syntax, StringEncoding enc)
        {
            var elements = new List <IDICOMElement>();

            while (!IsEndOfSequenceItemLittleEndian(dr) && dr.StreamLength != dr.StreamPosition)
            {
                dr.StreamPosition -= 8;
                elements.Add(DICOMElementReader.ReadElementExplicitBigEndian(dr, enc));
            }
            return(new DICOMObject(elements));
        }
        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));
        }
Example #8
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       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));
        }
Example #9
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));
        }
Example #10
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));
        }
Example #11
0
 /// <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);
     }));
 }
Example #12
0
        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);
                }
            }
        }
Example #13
0
        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 ReadObject(DICOMBinaryReader dr, TransferSyntax syntax)
        {
            List <IDICOMElement> elements = DICOMElementReader.ReadAllElements(dr, syntax);

            return(new DICOMObject(elements));
        }
Example #15
0
        public static DICOMObject ReadObject(DICOMBinaryReader dr, TransferSyntax syntax, StringEncoding enc = StringEncoding.ISO_IR_192)
        {
            var elements = DICOMElementReader.ReadAllElements(dr, syntax, enc);

            return(new DICOMObject(elements));
        }