Ejemplo n.º 1
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 = 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_);
            }


        }
Ejemplo n.º 2
0
 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)
     {
         //Length byte size depends on VR Encoding
         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);
 }
Ejemplo n.º 3
0
 public static void WriteLittleEndian(DICOMBinaryWriter dw, VR vr, DICOMWriteSettings settings,
                                      IDICOMElement toWrite)
 {
     byte[] data = DataComposer.GetDataLittleEndian(toWrite);
     LengthWriter.Write(dw, vr, settings, data != null ? data.Length : 0);
     dw.Write(data != null ? data : new byte[0]);
 }
Ejemplo n.º 4
0
 public static void WriteBigEndian(DICOMBinaryWriter dw, DICOMWriteSettings settings, IDICOMElement toWrite)
 {
     DICOMTagWriter.WriteBigEndian(dw, toWrite.Tag);
     VR vr = VRDictionary.GetVRFromType(toWrite);
     VRWriter.WriteVR(dw, settings, vr);
     DataWriter.WriteBigEndian(dw, vr, settings, toWrite);
 }
Ejemplo n.º 5
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 DICOMWriteSettings
                        {
                            TransferSyntax = tx,
                            DoWriteIndefiniteSequences = false
                        }, dicomObject);
                    dicomBytes = stream.ToArray();
                }
            }

            var split = new List<byte[]>();
            int i = 0;
            while (i < dicomBytes.Length)
            {
                int toTake = dicomBytes.Length >= (maxPduSize - 6) ? maxPduSize - 6 : dicomBytes.Length;
                byte[] fragment = dicomBytes.Skip(i).Take(toTake).ToArray();
                i += fragment.Length;
                split.Add(fragment);
            }
            return split;
        }
Ejemplo n.º 6
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);
 }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
 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)
     {
         //Length byte size depends on VR Encoding
         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;
         }
     }
     if (settings.TransferSyntax == TransferSyntax.EXPLICIT_VR_BIG_ENDIAN)
     {
         Array.Reverse(lengthBytes);
     }
     dw.Write(lengthBytes);
 }
Ejemplo n.º 9
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);
 }
Ejemplo n.º 10
0
 public static void WriteLittleEndian(DICOMBinaryWriter dw, VR vr, DICOMWriteSettings settings, int length)
 {
     byte[] 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((short)length);
                 break;
             case VREncoding.Implicit:
                 lengthBytes = BitConverter.GetBytes(length);
                 break;
         }
     }
     else
     {
         lengthBytes =BitConverter.GetBytes(length);
     }
     dw.Write(lengthBytes);
 }
Ejemplo n.º 11
0
        public static void Write(DICOMBinaryWriter dw, DICOMIOSettings settings, DICOMObject d,
                                 bool isSequenceItem = false)
        {
            if (!isSequenceItem)
            {
                TransferSyntaxHelper.SetSyntax(d, settings.TransferSyntax);
            }

            for (var i = 0; i < d.Elements.Count; i++)
            {
                var el = d.Elements[i];
                var currentSettings = IsFileMetaGroup(el) ? settings.GetFileMetaSettings() : settings;
                if (GroupWriter.IsGroupHeader(el))
                {
                    var skip = GroupWriter.WriteGroup(dw, currentSettings, d, el);
                    i += skip;
                }
                else
                {
                    _logger.LogInformation($"Writing element ${el.Tag.CompleteID}");
                    try
                    {
                        DICOMElementWriter.Write(dw, currentSettings, el);
                    }
                    catch (Exception e)
                    {
                        _logger.LogError($"Error writing :  ${el.Tag.CompleteID}\n{e}");
                        throw e;
                    }
                }
            }
        }
Ejemplo n.º 12
0
        public static void WriteBigEndian(DICOMBinaryWriter dw, DICOMWriteSettings settings, IDICOMElement toWrite)
        {
            DICOMTagWriter.WriteBigEndian(dw, toWrite.Tag);
            VR vr = VRDictionary.GetVRFromType(toWrite);

            VRWriter.WriteVR(dw, settings, vr);
        }
Ejemplo n.º 13
0
        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);
        }
Ejemplo n.º 14
0
 public static void WriteLittleEndian(DICOMBinaryWriter dw, VR vr, DICOMWriteSettings settings,
     IDICOMElement toWrite)
 {
     byte[] data = DataComposer.GetDataLittleEndian(toWrite);
     LengthWriter.Write(dw, vr, settings, data != null ? data.Length : 0);
     dw.Write(data != null ? data : new byte[0]);
 }
Ejemplo n.º 15
0
 private static void WriteIndefiniteLength(DICOMWriteSettings settings, DICOMBinaryWriter dw)
 {
     if (!(settings.TransferSyntax == TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN))
     {
         dw.WriteNullBytes(2);
     }
     dw.Write(new byte[] {0xFF, 0xFF, 0xFF, 0xFF});
 }
Ejemplo n.º 16
0
 public static void WriteVR(DICOMBinaryWriter dw, DICOMWriteSettings settings, VR vr)
 {
     if (!(settings.TransferSyntax == TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN))
     {
         string abbreviation = VRDictionary.GetAbbreviationFromVR(vr);
         dw.Write(abbreviation);
     }
 }
Ejemplo n.º 17
0
 public static void WriteVR(DICOMBinaryWriter dw, DICOMWriteSettings settings, VR vr)
 {
     if (settings.TransferSyntax != TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN)
     {
         string abbreviation = VRDictionary.GetAbbreviationFromVR(vr);
         dw.Write(abbreviation);
     }
 }
Ejemplo n.º 18
0
 private static void WriteIndefiniteLength(DICOMWriteSettings settings, DICOMBinaryWriter dw)
 {
     if (!(settings.TransferSyntax == TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN))
     {
         dw.WriteNullBytes(2);
     }
     dw.Write(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF });
 }
Ejemplo n.º 19
0
        public static void WriteBigEndian(DICOMBinaryWriter dw, VR vr, DICOMIOSettings settings,
                                          IDICOMElement toWrite)
        {
            var data = DataComposer.GetDataBigEndian(toWrite, settings.StringEncoding);

            LengthWriter.Write(dw, vr, settings, data != null ? data.Length : 0);
            dw.Write(data != null ? data : new byte[0]);
        }
Ejemplo n.º 20
0
 public static void Write(string filePath, DICOMWriteSettings settings, DICOMObject toWrite)
 {
     using (DICOMBinaryWriter dw = new DICOMBinaryWriter(filePath))
     {
         DICOMPreambleWriter.Write(dw);
         TransferSyntaxHelper.SetSyntax(toWrite, settings.TransferSyntax);
         DICOMObjectWriter.WriteObjectLittleEndian(dw, settings, toWrite);
     }
 }
Ejemplo n.º 21
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];
     bits.CopyTo(bytes, 0);
     dw.Write(bytes[0]);
 }
Ejemplo n.º 22
0
 public static void WriteVR(DICOMBinaryWriter dw, DICOMWriteSettings settings, IDICOMElement toWrite)
 {
     if (!(settings.TransferSyntax == TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN))
     {
         VR     vr           = VRDictionary.GetVRFromType(toWrite);
         string abbreviation = VRDictionary.GetAbbreviationFromVR(vr);
         dw.Write(abbreviation);
     }
 }
Ejemplo n.º 23
0
 public static void WriteLittleEndian(string filePath, DICOMWriteSettings settings, DICOMObject toWrite)
 {
     using (DICOMBinaryWriter dw = new DICOMBinaryWriter(filePath))
     {
         DICOMPreambleWriter.Write(dw);
         TransferSyntaxHelper.SetSyntax(toWrite, settings.TransferSyntax);
         DICOMObjectWriter.WriteObjectLittleEndian(dw, settings, toWrite);
     }
 }
Ejemplo n.º 24
0
 /// <summary>
 ///     Write DICOM file out (bytes) to a specified stream
 /// </summary>
 /// <param name="stream">the stream to which to write the file</param>
 /// <param name="settings">the write settings</param>
 /// <param name="toWrite">the object to write</param>
 public static void Write(Stream stream, DICOMWriteSettings settings, DICOMObject toWrite)
 {
     settings = settings ?? DICOMWriteSettings.Default();
     using (var dw = new DICOMBinaryWriter(stream))
     {
         DICOMPreambleWriter.Write(dw);
         TransferSyntaxHelper.SetSyntax(toWrite, settings.TransferSyntax);
         DICOMObjectWriter.Write(dw, settings, toWrite);
     }
 }
Ejemplo n.º 25
0
 /// <summary>
 ///     Write DICOM file out (bytes) to a specified stream
 /// </summary>
 /// <param name="stream">the stream to which to write the file</param>
 /// <param name="settings">the write settings</param>
 /// <param name="toWrite">the object to write</param>
 public static void Write(Stream stream, DICOMWriteSettings settings, DICOMObject toWrite)
 {
     settings = settings ?? DICOMWriteSettings.Default();
     using (var dw = new DICOMBinaryWriter(stream))
     {
         DICOMPreambleWriter.Write(dw);
         TransferSyntaxHelper.SetSyntax(toWrite, settings.TransferSyntax);
         DICOMObjectWriter.Write(dw, settings, toWrite);
     }
 }
Ejemplo n.º 26
0
 public static int WriteGroup(DICOMBinaryWriter dw, DICOMWriteSettings settings, DICOMObject d, IDICOMElement el)
 {
     byte[] groupBytes = WriteGroupBytes(d, settings, el.Tag.Group);
     int length = groupBytes.Length;
     var ul = el as UnsignedLong;
     ul.SetData((uint) length);
     DICOMElementWriter.Write(dw, settings, ul);
     dw.Write(groupBytes);
     return d.Elements.Where(elm => elm.Tag.Group == ul.Tag.Group).ToList().Count - 1;
 }
Ejemplo n.º 27
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);
     }
 }
Ejemplo n.º 28
0
 public static void Write(DICOMBinaryWriter dw, DICOMWriteSettings settings, IDICOMElement el)
 {
     if (settings.TransferSyntax == TransferSyntax.EXPLICIT_VR_BIG_ENDIAN)
     {
         WriteBigEndian(dw, settings, el);
     }
     else
     {
         WriteLittleEndian(dw, settings, el);
     }
 }
Ejemplo n.º 29
0
 public static void Write(DICOMBinaryWriter dw, DICOMWriteSettings settings, IDICOMElement el)
 {
     if (settings.TransferSyntax == TransferSyntax.EXPLICIT_VR_BIG_ENDIAN)
     {
         WriteBigEndian(dw, settings, el);
     }
     else
     {
         WriteLittleEndian(dw, settings, el);
     }
 }
Ejemplo n.º 30
0
        public static int WriteGroupLittleEndian(DICOMBinaryWriter dw, DICOMWriteSettings settings, DICOMObject d, IDICOMElement el)
        {
            byte[]       groupBytes = WriteGroupBytesLittleEndian(d, settings, el.Tag.Group);
            int          length     = groupBytes.Length;
            UnsignedLong ul         = el as UnsignedLong;

            ul.SetData((uint)length);
            DICOMElementWriter.WriteLittleEndian(dw, settings, ul);
            dw.Write(groupBytes);
            return(d.Elements.Where(elm => elm.Tag.Group == ul.Tag.Group).ToList().Count - 1);
        }
Ejemplo n.º 31
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);
     }
 }
Ejemplo n.º 32
0
        public static int WriteGroup(DICOMBinaryWriter dw, DICOMIOSettings settings, DICOMObject d, IDICOMElement el)
        {
            var groupBytes = WriteGroupBytes(d, settings, el.Tag.Group);
            var length     = groupBytes.Length;
            var ul         = el as UnsignedLong;

            ul.SetData((uint)length);
            DICOMElementWriter.Write(dw, settings, ul);
            dw.Write(groupBytes);
            return(d.Elements.Where(elm => elm.Tag.Group == ul.Tag.Group).ToList().Count - 1);
        }
Ejemplo n.º 33
0
 public static void WriteLittleEndian(DICOMBinaryWriter dw, int length, int numberOfBytes)
 {
     byte[] lengthBytes = new byte[0];
     switch (numberOfBytes)
     {
         case 2: lengthBytes = BitConverter.GetBytes((short)length);
             break;
         case 4: lengthBytes = BitConverter.GetBytes(length);
             break;
     }
     dw.Write(lengthBytes);
 }
Ejemplo n.º 34
0
        public static void WriteBigEndian(DICOMBinaryWriter dw, int length, int numberOfBytes)
        {
            byte[] lengthBytes = new byte[0];
            switch (numberOfBytes)
            {
            case 2: lengthBytes = BitConverter.GetBytes((short)length).Reverse().ToArray();
                break;

            case 4: lengthBytes = BitConverter.GetBytes(length).Reverse().ToArray();
                break;
            }
            dw.Write(lengthBytes);
        }
Ejemplo n.º 35
0
 public static void WriteBigEndian(DICOMBinaryWriter dw, int length, int numberOfBytes)
 {
     var lengthBytes = new byte[0];
     switch (numberOfBytes)
     {
         case 2:
             lengthBytes = BitConverter.GetBytes((ushort) length).Reverse().ToArray();
             break;
         case 4:
             lengthBytes = BitConverter.GetBytes(length).Reverse().ToArray();
             break;
     }
     dw.Write(lengthBytes);
 }
Ejemplo n.º 36
0
 public static void WriteLittleEndian(DICOMBinaryWriter dw, DICOMWriteSettings settings, IDICOMElement toWrite)
 {
     VR vr = VRDictionary.GetVRFromType(toWrite);
     if (vr == VR.Sequence)
     {
         SequenceWriter.WriteLittleEndian(dw, settings, toWrite);
     }
     else
     {
         DICOMTagWriter.WriteLittleEndian(dw, toWrite.Tag);
         VRWriter.WriteVR(dw, settings, vr);
         DataWriter.WriteLittleEndian(dw, vr, settings, toWrite);
     }
 }
Ejemplo n.º 37
0
 public static void WriteBigEndian(DICOMBinaryWriter dw, int length, int numberOfBytes)
 {
     byte[] lengthBytes=null;
     switch (numberOfBytes)
     {
         case 2:
             lengthBytes = BitConverter.GetBytes((ushort) length);
             break;
         case 4:
             lengthBytes = BitConverter.GetBytes(length);
             break;
     }
     Array.Reverse(lengthBytes);
     dw.Write(lengthBytes);
 }
Ejemplo n.º 38
0
        public static void WriteLittleEndian(DICOMBinaryWriter dw, DICOMWriteSettings settings, IDICOMElement toWrite)
        {
            VR vr = VRDictionary.GetVRFromType(toWrite);

            if (vr == VR.Sequence)
            {
                SequenceWriter.WriteLittleEndian(dw, settings, toWrite);
            }
            else
            {
                DICOMTagWriter.WriteLittleEndian(dw, toWrite.Tag);
                VRWriter.WriteVR(dw, settings, vr);
                DataWriter.WriteLittleEndian(dw, vr, settings, toWrite);
            }
        }
Ejemplo n.º 39
0
        public static void WriteLittleEndian(DICOMBinaryWriter dw, int length, int numberOfBytes)
        {
            var lengthBytes = new byte[0];

            switch (numberOfBytes)
            {
            case 2:
                lengthBytes = BitConverter.GetBytes((short)length);
                break;

            case 4:
                lengthBytes = BitConverter.GetBytes(length);
                break;
            }
            dw.Write(lengthBytes);
        }
Ejemplo n.º 40
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;
 }
Ejemplo n.º 41
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);
 }
Ejemplo n.º 42
0
 public static void WriteObjectLittleEndian(DICOMBinaryWriter dw, DICOMWriteSettings settings, DICOMObject d)
 {
     for (int i = 0; i < d.Elements.Count; i++)
     {
         IDICOMElement      el = d.Elements[i];
         DICOMWriteSettings currentSettings = IsFileMetaGroup(el) ? settings.GetFileMetaSettings() : settings;
         if (GroupWriter.IsGroupHeader(el))
         {
             int skip = GroupWriter.WriteGroupLittleEndian(dw, currentSettings, d, el);
             i += skip;
         }
         else
         {
             DICOMElementWriter.WriteLittleEndian(dw, currentSettings, el);
         }
     }
 }
Ejemplo n.º 43
0
        public static byte[] WriteItemsLittleEndian(DICOMWriteSettings settings, List <DICOMObject> items)
        {
            byte[] allItemBytes;
            using (var stream = new MemoryStream())
            {
                using (var itemDw = new DICOMBinaryWriter(stream))
                {
                    foreach (var d in items)
                    {
                        WriteItemLittleEndian(itemDw, settings, d);
                    }
                }

                allItemBytes = stream.ToArray();
            }
            return(allItemBytes);
        }
Ejemplo n.º 44
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_ACCEPT);
             dw.WriteNullBytes(1); //Reserved Null byte
             byte[] body = WriteBody();
             LengthWriter.WriteBigEndian(dw, body.Length, 4);
             dw.Write(body);
             written = stream.ToArray();
         }
     }
     return written;
 }
Ejemplo n.º 45
0
 /// <summary>
 /// Ignores the rule of writing metadata in explicit VR little endian and instead writes all elements with the same passed in syntax
 /// Used in PData writing
 /// </summary>
 /// <param name="dw"></param>
 /// <param name="settings"></param>
 /// <param name="d"></param>
 /// <param name="isSequenceItem"></param>
 public static void WriteSameSyntax(DICOMBinaryWriter dw, DICOMIOSettings settings, DICOMObject d,
                                    bool isSequenceItem = false)
 {
     for (var i = 0; i < d.Elements.Count; i++)
     {
         var el = d.Elements[i];
         if (GroupWriter.IsGroupHeader(el))
         {
             var skip = GroupWriter.WriteGroup(dw, settings, d, el);
             i += skip;
         }
         else
         {
             DICOMElementWriter.Write(dw, settings, el);
         }
     }
 }
Ejemplo n.º 46
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;
 }
Ejemplo n.º 47
0
        public static byte[] WriteItemsLittleEndian(DICOMWriteSettings settings, List<DICOMObject> items)
        {
            byte[] allItemBytes;
            using (var stream = new MemoryStream())
            {
                using (var itemDw = new DICOMBinaryWriter(stream))
                {
                    foreach (DICOMObject d in items)
                    {
                        WriteItemLittleEndian(itemDw, settings, d);
                    }
                }

                allItemBytes = stream.ToArray();
            }
            return allItemBytes;
        }
Ejemplo n.º 48
0
 public static void WriteObjectLittleEndian(DICOMBinaryWriter dw, DICOMWriteSettings settings, DICOMObject d)
 {
     for (int i = 0; i < d.Elements.Count; i++)
     {
         IDICOMElement el = d.Elements[i];
         DICOMWriteSettings currentSettings = IsFileMetaGroup(el) ? settings.GetFileMetaSettings() : settings;
         if (GroupWriter.IsGroupHeader(el))
         {
             int skip = GroupWriter.WriteGroupLittleEndian(dw, currentSettings, d, el);
             i += skip;
         }
         else
         {
             DICOMElementWriter.WriteLittleEndian(dw, currentSettings, el);
         }
     }
 }
Ejemplo n.º 49
0
 public static void Write(DICOMBinaryWriter dw, DICOMWriteSettings settings, DICOMObject d)
 {
     for (int i = 0; i < d.Elements.Count; i++)
     {
         IDICOMElement el = d.Elements[i];
         TransferSyntaxHelper.SetSyntax(d,settings.TransferSyntax);
         DICOMWriteSettings currentSettings = IsFileMetaGroup(el) ? settings.GetFileMetaSettings() : settings;
         if (GroupWriter.IsGroupHeader(el))
         {
             int skip = GroupWriter.WriteGroup(dw, currentSettings, d, el);
             i += skip;
         }
         else
         {
             DICOMElementWriter.Write(dw, currentSettings, el);
         }
     }
 }
Ejemplo n.º 50
0
 public static void WriteLittleEndian(DICOMBinaryWriter dw, DICOMWriteSettings settings, IDICOMElement toWrite)
 {
     var s = toWrite as Sequence;
     DICOMTagWriter.WriteLittleEndian(dw, toWrite.Tag);
     VRWriter.WriteVR(dw, settings, VR.Sequence);
     if (settings.DoWriteIndefiniteSequences)
     {
         WriteIndefiniteLength(settings, dw);
         SequenceItemWriter.WriteItemsLittleEndian(dw, settings, s.Items);
         WriteEndOfSequenceLittleEndian(dw);
     }
     else
     {
         byte[] itemsBytes = SequenceItemWriter.WriteItemsLittleEndian(settings, s.Items);
         LengthWriter.Write(dw, VR.Sequence, settings, itemsBytes.Length);
         dw.Write(itemsBytes);
     }
 }
Ejemplo n.º 51
0
        public static void WriteLittleEndian(DICOMBinaryWriter dw, DICOMWriteSettings settings, IDICOMElement toWrite)
        {
            Sequence s = toWrite as Sequence;

            DICOMTagWriter.WriteLittleEndian(dw, toWrite.Tag);
            VRWriter.WriteVR(dw, settings, VR.Sequence);
            if (settings.DoWriteIndefiniteSequences)
            {
                WriteIndefiniteLength(settings, dw);
                SequenceItemWriter.WriteItemsLittleEndian(dw, settings, s.Items);
                WriteEndOfSequenceLittleEndian(dw);
            }
            else
            {
                byte[] itemsBytes = SequenceItemWriter.WriteItemsLittleEndian(settings, s.Items);
                LengthWriter.Write(dw, VR.Sequence, settings, itemsBytes.Length);
                dw.Write(itemsBytes);
            }
        }
Ejemplo n.º 52
0
 /// <summary>
 /// Ignores the rule of writing metadata in explicit VR little endian and instead writes all elements with the passed in syntax
 /// </summary>
 /// <param name="dw"></param>
 /// <param name="settings"></param>
 /// <param name="d"></param>
 /// <param name="isSequenceItem"></param>
 public static void WriteSameSyntax(DICOMBinaryWriter dw, DICOMWriteSettings settings, DICOMObject d, bool isSequenceItem = false)
 {
     for (int i = 0; i < d.Elements.Count; i++)
     {
         IDICOMElement el = d.Elements[i];
         if (!isSequenceItem)
         {
             TransferSyntaxHelper.SetSyntax(d, settings.TransferSyntax);
         }
         if (GroupWriter.IsGroupHeader(el))
         {
             int skip = GroupWriter.WriteGroup(dw, settings, d, el);
             i += skip;
         }
         else
         {
             DICOMElementWriter.Write(dw, settings, el);
         }
     }
 }
Ejemplo n.º 53
0
 public static void WriteItemLittleEndian(DICOMBinaryWriter dw, DICOMWriteSettings settings, DICOMObject d)
 {
     DICOMTagWriter.WriteLittleEndian(dw, TagHelper.Item);
     using (var stream = new MemoryStream())
     {
         using (var itemDw = new DICOMBinaryWriter(stream))
         {
             DICOMObjectWriter.Write(itemDw, settings, d, true);
             if (!settings.DoWriteIndefiniteSequences)
             {
                 LengthWriter.Write(dw, VR.Null, settings, (int)stream.Length);
                 dw.Write(stream.ToArray());
             }
             else
             {
                 WriteIndefiniteLittleEndian(dw, stream.ToArray());
             }
         }
     }
 }
Ejemplo n.º 54
0
        public static byte[] WriteGroupBytes(DICOMObject d, DICOMIOSettings settings, string groupId)
        {
            var groupElements = d.Elements.Where(el => el.Tag.Group == groupId).ToList();

            byte[] groupBytes;
            using (var stream = new MemoryStream())
            {
                using (var groupDW = new DICOMBinaryWriter(stream))
                {
                    foreach (var el in groupElements)
                    {
                        if (!IsGroupHeader(el))
                        {
                            DICOMElementWriter.Write(groupDW, settings, el);
                        }
                    }
                }
                groupBytes = stream.ToArray();
            }
            return(groupBytes);
        }
Ejemplo n.º 55
0
        private static byte[] WriteGroupBytesLittleEndian(DICOMObject d, DICOMWriteSettings settings, string groupID)
        {
            List <IDICOMElement> groupElements = d.Elements.Where(el => el.Tag.Group == groupID).ToList();

            byte[] groupBytes;
            using (MemoryStream stream = new MemoryStream())
            {
                using (DICOMBinaryWriter groupDW = new DICOMBinaryWriter(stream))
                {
                    foreach (IDICOMElement el in groupElements)
                    {
                        if (!IsGroupHeader(el))
                        {
                            DICOMElementWriter.WriteLittleEndian(groupDW, settings, el);
                        }
                    }
                }
                groupBytes = stream.ToArray();
            }
            return(groupBytes);
        }
Ejemplo n.º 56
0
 public static void Write(DICOMBinaryWriter dw, DICOMWriteSettings settings, DICOMObject d,
                          bool isSequenceItem = false)
 {
     for (var i = 0; i < d.Elements.Count; i++)
     {
         var el = d.Elements[i];
         if (!isSequenceItem)
         {
             TransferSyntaxHelper.SetSyntax(d, settings.TransferSyntax);
         }
         var currentSettings = IsFileMetaGroup(el) ? settings.GetFileMetaSettings() : settings;
         if (GroupWriter.IsGroupHeader(el))
         {
             var skip = GroupWriter.WriteGroup(dw, currentSettings, d, el);
             i += skip;
         }
         else
         {
             DICOMElementWriter.Write(dw, currentSettings, el);
         }
     }
 }
Ejemplo n.º 57
0
 private static void WriteEndOfSequenceBigEndian(DICOMBinaryWriter dw)
 {
     dw.Write(_endOfSequence_BE);
 }
Ejemplo n.º 58
0
 private static void WriteEndOfSequenceLittleEndian(DICOMBinaryWriter dw)
 {
     dw.Write(_endOfSequence_LE);
 }
Ejemplo n.º 59
0
 public static void WriteItemsLittleEndian(DICOMBinaryWriter dw, DICOMWriteSettings settings,
                                           List <DICOMObject> items)
 {
     dw.Write(WriteItemsLittleEndian(settings, items));
 }
Ejemplo n.º 60
0
 private static void WriteIndefiniteLittleEndian(DICOMBinaryWriter dw, byte[] itemBytes)
 {
     dw.Write(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF });
     dw.Write(itemBytes);
     dw.Write(_endOfSequenceItem_LE);
 }