Esempio n. 1
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;
                    }
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 ///     Writes DICOM file out as a file of a specified path
 /// </summary>
 /// <param name="filePath">the path 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(string filePath, DICOMIOSettings settings, DICOMObject toWrite)
 {
     using (var fs = new FileStream(filePath, FileMode.Create))
     {
         Write(fs, settings, toWrite);
     }
 }
Esempio n. 3
0
        public static void Write(DICOMBinaryWriter dw, VR vr, DICOMIOSettings 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);
        }
Esempio n. 4
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]);
        }
Esempio n. 5
0
 private static void WriteIndefiniteLength(DICOMIOSettings settings, DICOMBinaryWriter dw)
 {
     if (!(settings.TransferSyntax == TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN))
     {
         dw.WriteNullBytes(2);
     }
     dw.Write(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF });
 }
Esempio n. 6
0
 public static void WriteVR(DICOMBinaryWriter dw, DICOMIOSettings settings, VR vr)
 {
     if (settings.TransferSyntax != TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN)
     {
         var abbreviation = VRDictionary.GetAbbreviationFromVR(vr);
         dw.Write(abbreviation);
     }
 }
Esempio n. 7
0
        public static void WriteBigEndian(DICOMBinaryWriter dw, DICOMIOSettings settings, IDICOMElement toWrite)
        {
            DICOMTagWriter.WriteBigEndian(dw, toWrite.Tag);
            var vr = VRDictionary.GetVRFromType(toWrite);

            VRWriter.WriteVR(dw, settings, vr);
            DataWriter.WriteBigEndian(dw, vr, settings, toWrite);
        }
Esempio n. 8
0
 public static void WriteVR(DICOMBinaryWriter dw, DICOMIOSettings settings, IDICOMElement toWrite)
 {
     if (!(settings.TransferSyntax == TransferSyntax.IMPLICIT_VR_LITTLE_ENDIAN))
     {
         var vr           = VRDictionary.GetVRFromType(toWrite);
         var abbreviation = VRDictionary.GetAbbreviationFromVR(vr);
         dw.Write(abbreviation);
     }
 }
Esempio n. 9
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, DICOMIOSettings settings, DICOMObject toWrite)
 {
     settings = settings ?? DICOMIOSettings.Default();
     using (var dw = new DICOMBinaryWriter(stream))
     {
         DICOMPreambleWriter.Write(dw);
         TransferSyntaxHelper.SetSyntax(toWrite, settings.TransferSyntax);
         DICOMObjectWriter.Write(dw, settings, toWrite);
     }
 }
Esempio n. 10
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);
        }
Esempio n. 11
0
 public static void Write(DICOMBinaryWriter dw, DICOMIOSettings settings, IDICOMElement el)
 {
     if (settings.TransferSyntax == TransferSyntax.EXPLICIT_VR_BIG_ENDIAN)
     {
         WriteBigEndian(dw, settings, el);
     }
     else
     {
         WriteLittleEndian(dw, settings, el);
     }
 }
Esempio n. 12
0
        public static void WriteLittleEndian(DICOMBinaryWriter dw, DICOMIOSettings settings, IDICOMElement toWrite)
        {
            var 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);
            }
        }
Esempio n. 13
0
        public static byte[] WriteItemsLittleEndian(DICOMIOSettings 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);
        }
Esempio n. 14
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);
         }
     }
 }
Esempio n. 15
0
        public static void WriteLittleEndian(DICOMBinaryWriter dw, DICOMIOSettings 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
            {
                var itemsBytes = SequenceItemWriter.WriteItemsLittleEndian(settings, s.Items);
                LengthWriter.Write(dw, VR.Sequence, settings, itemsBytes.Length);
                dw.Write(itemsBytes);
            }
        }
Esempio n. 16
0
 public static void WriteItemLittleEndian(DICOMBinaryWriter dw, DICOMIOSettings 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());
             }
         }
     }
 }
Esempio n. 17
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);
        }
Esempio n. 18
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
                {
                    DICOMElementWriter.Write(dw, currentSettings, el);
                }
            }
        }
Esempio n. 19
0
 public static void WriteItemsLittleEndian(DICOMBinaryWriter dw, DICOMIOSettings settings,
                                           List <DICOMObject> items)
 {
     dw.Write(WriteItemsLittleEndian(settings, items));
 }