Example #1
0
 public static void WriteMaxLength(DICOMBinaryWriter dw, int length)
 {
     dw.Write((byte)ItemType.MAXIMUM_LENGTH);
     dw.WriteNullBytes(1); // Reserved Null Byte
     LengthWriter.WriteBigEndian(dw, 4, 2);
     LengthWriter.WriteBigEndian(dw, length, 4);
 }
Example #2
0
        public PDataTF(DICOMObject dicom, bool isLastItem, bool isCommandObject, PresentationContext context)
            : this()
        {
            byte[] data;
            using (var stream = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(stream))
                {
                    var settings = new DICOMWriteSettings();
                    settings.TransferSyntax = isCommandObject
                        ? TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN
                        : TransferSyntaxHelper.GetSyntax(context.TransferSyntaxes[0]);
                    DICOMObjectWriter.Write(dw, settings, dicom);
                    data = stream.ToArray();
                }
            }
            var frag = new PDVItemFragment();

            frag.Data            = data;
            frag.IsLastItem      = isLastItem;
            frag.IsCommandObject = isCommandObject;
            var item = new PDVItem();

            item.Fragment = frag;
            item.PresentationContextID = context.Id;
            Items.Add(item);
        }
Example #3
0
        /// <summary>
        ///     Splits the DICOM object into chunks that are within the max PDU size
        /// </summary>
        /// <param name="dicomObject"> the DICOM objec to be split</param>
        /// <param name="maxPduSize">the max length (in bytes) for a PDU</param>
        /// <param name="asc">the association that the file will be sent</param>
        /// <returns></returns>
        public static List <byte[]> GetChunks(DICOMObject dicomObject, int maxPduSize, PresentationContext pc)
        {
            byte[] dicomBytes;
            using (var stream = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(stream))
                {
                    var tx = TransferSyntaxHelper.GetSyntax(pc.TransferSyntaxes.First());
                    DICOMObjectWriter.WriteSameSyntax(dw,
                                                      new DICOMIOSettings
                    {
                        TransferSyntax             = tx,
                        DoWriteIndefiniteSequences = false
                    }, dicomObject);
                    dicomBytes = stream.ToArray();
                }
            }

            var split = new List <byte[]>();
            var i     = 0;

            while (i < dicomBytes.Length)
            {
                var toTake   = dicomBytes.Length >= maxPduSize - 6 ? maxPduSize - 6 : dicomBytes.Length;
                var fragment = dicomBytes.Skip(i).Take(toTake).ToArray();
                i += fragment.Length;
                split.Add(fragment);
            }
            return(split);
        }
Example #4
0
        /// <summary>
        ///     Splits the DICOM object into chunks that are within the max PDU size
        /// </summary>
        /// <param name="dicomObject"> the DICOM objec to be split</param>
        /// <param name="maxPduSize">the max length (in bytes) for a PDU</param>
        /// <param name="asc">the association that the file will be sent</param>
        /// <returns></returns>
        private static List <byte[]> GetChunks(DICOMObject dicomObject, int maxPduSize, Association asc)
        {
            byte[] dicomBytes;
            using (var stream = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(stream))
                {
                    DICOMObjectWriter.Write(dw,
                                            new DICOMWriteSettings
                    {
                        TransferSyntax =
                            TransferSyntaxHelper.GetSyntax(asc.PresentationContexts.First().TransferSyntaxes.First()),
                        DoWriteIndefiniteSequences = false
                    }, dicomObject);
                    dicomBytes = stream.ToArray();
                }
            }

            var split = new List <byte[]>();
            int i     = 0;

            while (i < dicomBytes.Length)
            {
                int    toTake   = dicomBytes.Length >= (maxPduSize - 6) + i ? maxPduSize - 6 : dicomBytes.Length - i;
                byte[] fragment = dicomBytes.Skip(i).Take(toTake).ToArray();
                i += fragment.Length;
                split.Add(fragment);
            }
            return(split);
        }
Example #5
0
        public void WriteMultipleFl()
        {
            var vssd = DICOMForge.VirtualSourceAxisDistances();

            vssd.Data_ = new System.Collections.Generic.List <float>()
            {
                2538.4199f, 2541.00f
            };
            byte[] elBytes = null;
            using (var stream = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(stream))
                {
                    DICOMElementWriter.Write(dw, DICOMWriteSettings.DefaultExplicit(), vssd);
                    elBytes = stream.ToArray();
                }
            }

            AbstractElement <float> readVssd = null;

            using (var dr = new DICOMBinaryReader(elBytes))
            {
                readVssd = DICOMElementReader.ReadElementExplicitLittleEndian(dr) as AbstractElement <float>;
            }

            Assert.AreEqual(readVssd.Data_.Count, 2);
            Assert.AreEqual(readVssd.Data_[0], 2538.4199f);
            Assert.AreEqual(readVssd.Data_[1], 2541.00f);
        }
Example #6
0
        private byte[] WriteBody()
        {
            var body = new byte[0];

            using (var stream = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(stream))
                {
                    //Main body
                    LengthWriter.WriteBigEndian(dw, ProtocolVersion, 2); //Protocol Version
                    dw.WriteNullBytes(2);                                //Reserved Null bytes
                    dw.Write(CalledEntityTitle.PadRight(16));
                    dw.Write(CallingEntityTitle.PadRight(16));
                    dw.WriteNullBytes(32); //Reserved Null bytes
                    ItemWriter.WriteApplicationContext(dw, ApplicationContext);
                    foreach (var pc in PresentationContexts)
                    {
                        ItemWriter.WritePresentationCtxRequestType(dw, pc);
                    }
                    ItemWriter.WriteUserInfo(dw, UserInfo);
                    body = stream.ToArray();
                }
            }
            return(body);
        }
Example #7
0
        public void WriteBigEndianLength3()
        {
            var len = 10;

            byte[] bytes;
            using (var ms = new MemoryStream())
            {
                var             dw       = new DICOMBinaryWriter(ms);
                VR              vr       = VR.CodeString;
                DICOMIOSettings settings = new DICOMIOSettings()
                {
                    TransferSyntax = TransferSyntax.EXPLICIT_VR_BIG_ENDIAN, DoWriteIndefiniteSequences = false
                };
                var data = new byte[10];
                LengthWriter.Write(dw, vr, settings, data != null ? data.Length : 0);
                bytes = ms.ToArray();
            }

            var read = LengthReader.ReadLittleEndian(bytes);

            Assert.AreNotEqual(len, read);

            read = LengthReader.ReadBigEndian(bytes);
            Assert.AreEqual(len, read);
        }
Example #8
0
 private static void WriteUIDItem(DICOMBinaryWriter dw, ItemType iType, string uid)
 {
     if (!string.IsNullOrEmpty(uid))
     {
         dw.Write((byte)iType);
         dw.WriteNullBytes(1); // Reserved Null Byte
         LengthWriter.WriteBigEndian(dw, uid.Length, 2);
         dw.Write(uid);
     }
 }
Example #9
0
        private static void WritePDVFragmentMessageHeader(DICOMBinaryWriter dw, PDVItemFragment frag)
        {
            var bits = new BitArray(8);

            bits.Set(0, frag.IsCommandObject);
            bits.Set(1, frag.IsLastItem);
            var bytes = new byte[1];

            ((ICollection)bits).CopyTo(bytes, 0);
            dw.Write(bytes[0]);
        }
Example #10
0
 public static void WriteAsyncOperations(DICOMBinaryWriter dw, AsyncOperations ao)
 {
     if (ao != null)
     {
         dw.Write((byte)ItemType.ASYNCHRONOUS_OPERATIONS_WINDOW);
         dw.WriteNullBytes(1); // Reserved Null Byte
         LengthWriter.WriteBigEndian(dw, 4, 2);
         LengthWriter.WriteBigEndian(dw, ao.MaxInvokeOperations, 2);
         LengthWriter.WriteBigEndian(dw, ao.MaxPerformOperations, 2);
     }
 }
Example #11
0
        public void WriteLittleEndian2()
        {
            var len = 2500;

            byte[] bytes;
            using (var ms = new MemoryStream())
            {
                var dw = new DICOMBinaryWriter(ms);
                LengthWriter.WriteLittleEndian(dw, len, 2);
                bytes = ms.ToArray();
            }
            var read = LengthReader.ReadLittleEndian(bytes);

            Assert.AreEqual(len, read);
        }
Example #12
0
        public byte[] Write()
        {
            var written = new byte[0];
            var stream  = new MemoryStream();

            using (var dw = new DICOMBinaryWriter(stream))
            {
                dw.Write((byte)PDUType.A_ABORT);
                dw.WriteNullBytes(1); //Reserved Null byte
                LengthWriter.WriteBigEndian(dw, 4, 4);
                dw.WriteNullBytes(2); //Reserved Null bytes
                dw.Write((byte)Source);
                dw.Write((byte)Reason);
                written = stream.ToArray();
            }
            return(written);
        }
Example #13
0
        private byte[] WriteItems()
        {
            var written = new byte[0];

            using (var stream = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(stream))
                {
                    foreach (PDVItem item in Items)
                    {
                        ItemWriter.WritePDVItem(dw, item);
                    }
                    written = stream.ToArray();
                }
            }
            return(written);
        }
Example #14
0
        public static void WritePDVItem(DICOMBinaryWriter dw, PDVItem pdv)
        {
            //Write fragment first so we have length
            var fragment = new byte[0];

            using (var stream = new MemoryStream())
            {
                using (var fragDw = new DICOMBinaryWriter(stream))
                {
                    WritePDVFragment(fragDw, pdv.Fragment);
                    fragment = stream.ToArray();
                }
            }
            LengthWriter.WriteBigEndian(dw, fragment.Length + 1, 4);
            dw.Write((byte)pdv.PresentationContextID);
            dw.Write(fragment);
        }
Example #15
0
        public byte[] Write()
        {
            var written = new byte[0];

            using (var stream = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(stream))
                {
                    dw.Write((byte)PDUType.P_DATA_TRANSFER);
                    dw.WriteNullBytes(1); //Reserved Null byte
                    byte[] items = WriteItems();
                    LengthWriter.WriteBigEndian(dw, items.Length, 4);
                    dw.Write(items);
                    written = stream.ToArray();
                }
            }
            return(written);
        }
Example #16
0
        public byte[] Write()
        {
            var written = new byte[0];

            using (var stream = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(stream))
                {
                    dw.Write((byte)PDUType.A_ASSOC_REQUEST);
                    dw.WriteNullBytes(1); //Reserved Null byte
                    var body = WriteBody();
                    LengthWriter.WriteBigEndian(dw, body.Length, 4);
                    dw.Write(body);
                    written = stream.ToArray();
                }
            }
            return(written);
        }
Example #17
0
 public static void WritePresentationCtxAcceptType(DICOMBinaryWriter dw, PresentationContext pc)
 {
     dw.Write((byte)ItemType.PRESENTATION_CONTEXT_ACCEPT);
     dw.WriteNullBytes(1); //Reserved Null Byte
     byte[] internBytes;   //Will use to get length
     using (var stream = new MemoryStream())
     {
         using (var intern = new DICOMBinaryWriter(stream))
         {
             intern.Write((byte)pc.Id);
             intern.WriteNullBytes(1);
             intern.Write((byte)pc.Reason);
             intern.WriteNullBytes(1);
             WriteTransferSyntax(intern, pc.TransferSyntaxes.First());
             internBytes = stream.ToArray();
         }
     }
     LengthWriter.WriteBigEndian(dw, internBytes.Length, 2);
     dw.Write(internBytes);
 }
Example #18
0
        public static void WriteUserInfo(DICOMBinaryWriter dw, UserInfo info)
        {
            dw.Write((byte)ItemType.USER_INFO);
            dw.WriteNullBytes(1); // Reserved Null Byte
            var body = new byte[0];

            using (var stream = new MemoryStream()) //Will write inner object to get length
            {
                using (var wr = new DICOMBinaryWriter(stream))
                {
                    WriteMaxLength(wr, info.MaxPDULength);
                    WriteImplementationClassUID(wr, info.ImplementationUID);
                    WriteAsyncOperations(wr, info.AsynchronousOperations);
                    WriteImplementationVersion(wr, info.ImplementationVersion);
                    body = stream.ToArray();
                }
            }
            LengthWriter.WriteBigEndian(dw, body.Length, 2);
            dw.Write(body);
        }
Example #19
0
        /// <summary>
        /// Creates a clone of this DICOM object
        /// </summary>
        /// <returns>a new copied DICOM object</returns>
        public DICOMObject Clone()
        {
            List <IDICOMElement> copy = new List <IDICOMElement>();

            foreach (var el in Elements)
            {
                using (var ms = new MemoryStream())
                {
                    using (var dw = new DICOMBinaryWriter(ms))
                    {
                        DICOMElementWriter.Write(dw, DICOMIOSettings.Default(), el);
                    }
                    using (var dr = new DICOMBinaryReader(ms.ToArray()))
                    {
                        copy.Add(DICOMElementReader.ReadElementImplicitLittleEndian(dr));
                    }
                }
            }

            return(new DICOMObject(copy));
        }
        public void CanAddMultipleStringsTest()
        {
            var dcm = new DICOMObject();
            dcm.ReplaceOrAdd(DICOMForge.ImageType("Type1", "Type2", "Type3"));

            //Memory test
            var expected = 3;
            var data = dcm.GetSelector().ImageType.Data_;
            var actual = data.Count;
            Assert.AreEqual(expected, actual);

            Assert.AreEqual("Type1", data[0]);
            Assert.AreEqual("Type2", data[1]);
            Assert.AreEqual("Type3", data[2]);


            var bytes = new byte[0];
            //Test IO
            using (var ms = new MemoryStream())
            {
                //WRITE
                using (var bw = new DICOMBinaryWriter(ms))
                {
                    DICOMObjectWriter.Write(bw, DICOMIOSettings.Default(), dcm);
                }
                bytes = ms.ToArray();
            }

            //READ
            var dcmRead = DICOMObject.Read(bytes);
            expected = 3;
            data = dcmRead.GetSelector().ImageType.Data_;
            actual = data.Count;
            Assert.AreEqual(expected, actual);

            Assert.AreEqual("Type1", data[0]);
            Assert.AreEqual("Type2", data[1]);
            Assert.AreEqual("Type3", data[2]);

        }
Example #21
0
        public void WriteUnknownTest()
        {
            var dcm = new DICOMObject();

            dcm.Add(new LongString(new Tag("32751000"), "Online3D"));

            var dcmBytes = new byte[0]; //Start empty

            using (var ms = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(ms))
                {
                    //Explicity
                    DICOMObjectWriter.Write(dw, DICOMIOSettings.DefaultExplicit(), dcm);
                }
                dcmBytes = ms.ToArray(); //Store to read back
            }

            var dcmBack = DICOMObject.Read(dcmBytes);

            //This passes
            Assert.IsTrue(dcmBack.FindFirst("32751000").VR == EvilDICOM.Core.Enums.VR.LongString);

            using (var ms = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(ms))
                {
                    //Implicit
                    DICOMObjectWriter.Write(dw, DICOMIOSettings.Default(), dcm);
                }
                dcmBytes = ms.ToArray(); //Store to read back
            }

            dcmBack = DICOMObject.Read(dcmBytes);
            //This fails
            Assert.IsFalse(dcmBack.FindFirst("32751000").VR == EvilDICOM.Core.Enums.VR.LongString);
        }
Example #22
0
        public void WriteBigEndian()
        {
            var dcm = DICOMFileReader.Read(Resources.explicitLittleEndian);
            var els = dcm.Elements.Count;

            byte[] bytes;

            using (var ms = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(ms))
                {
                    var settings = new DICOMWriteSettings()
                    {
                        TransferSyntax = Enums.TransferSyntax.EXPLICIT_VR_BIG_ENDIAN, DoWriteIndefiniteSequences = false
                    };
                    DICOMObjectWriter.Write(dw, settings, dcm);
                }
                bytes = ms.ToArray();
            }

            var dcm2 = DICOMFileReader.Read(bytes);

            Assert.AreEqual(dcm2.Elements.Count, els);
        }
Example #23
0
        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 = DICOMIOSettings.Default();

            using (var ms = new MemoryStream())
            {
                using (var dw = new DICOMBinaryWriter(ms))
                {
                    DICOMElementWriter.Write(dw, DICOMIOSettings.Default(), ds);
                }
                written = ms.ToArray();
            }

            using (var dr = new DICOMBinaryReader(written))
            {
                var read = DICOMElementReader.ReadElementImplicitLittleEndian(dr) as DecimalString;
                CollectionAssert.AreEqual(ds.DData_, read.Data_);
            }
        }
Example #24
0
 public static void WriteApplicationContext(DICOMBinaryWriter dw, string uid)
 {
     WriteUIDItem(dw, ItemType.APPLICATION_CONTEXT, uid);
 }
Example #25
0
 public static void WriteAbstractSyntax(DICOMBinaryWriter dw, string uid)
 {
     WriteUIDItem(dw, ItemType.ABSTRACT_SYNTAX, uid);
 }
Example #26
0
 public static void WriteTransferSyntax(DICOMBinaryWriter dw, string uid)
 {
     WriteUIDItem(dw, ItemType.TRANSFER_SYNTAX, uid);
 }
 public static void Write(DICOMBinaryWriter dw)
 {
     dw.WriteNullBytes(128);
     dw.Write("DICM");
 }
Example #28
0
 public static void WritePDVFragment(DICOMBinaryWriter dw, PDVItemFragment frag)
 {
     WritePDVFragmentMessageHeader(dw, frag);
     dw.Write(frag.Data);
 }
Example #29
0
 public static void WriteImplementationClassUID(DICOMBinaryWriter dw, string uid)
 {
     WriteUIDItem(dw, ItemType.IMPLEMENTATION_CLASS_UID, uid);
 }
 public static void Write(DICOMBinaryWriter dw)
 {
     dw.WriteNullBytes(128);
     dw.Write("DICM");
 }
Example #31
0
 public static void WriteImplementationVersion(DICOMBinaryWriter dw, string uid)
 {
     WriteUIDItem(dw, ItemType.IMPLEMENTATION_VERSION_NAME, uid);
 }