Example #1
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 #2
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;
        }
Example #3
0
        private static PresentationContext ReadPresentationCtxContents(byte[] contents, bool requestType = false)
        {
            var pc = new PresentationContext();

            pc.TransferSyntaxes = new List <string>();
            using (var dr = new DICOMBinaryReader(contents))
            {
                pc.Id = dr.Take(1)[0];
                dr.Skip(1); //Reserved Null Byte
                pc.Reason = (PresentationContextReason)Enum.ToObject(typeof(PresentationContextReason), dr.Take(1)[0]);
                dr.Skip(1); //Reserved Null Byte
                if (requestType)
                {
                    pc.AbstractSyntax = ReadAbstractSyntax(dr).Trim();
                }
                while (dr.StreamPosition < dr.StreamLength)
                {
                    var initPos = dr.StreamPosition;
                    pc.TransferSyntaxes.Add(ReadTransferSyntax(dr));
                    if (dr.StreamPosition == initPos)
                    {
                        break;
                    }
                }
            }
            return(pc);
        }
Example #4
0
 public PDataTF(byte[] data, bool isLastItem, bool isCommandObject, PresentationContext context)
     : this()
 {
     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 #5
0
        public static List<PDataTF> GetPDataTFs(AbstractDIMSEBase dimse, PresentationContext pContext, int maxPDULength = 16384)
        {
            var list = new List<PDataTF>();
            var commandEls = dimse.Elements;
            list.Add(new PDataTF(new DICOMObject(dimse.Elements), true, true, pContext));

            var dataDIMSE = dimse as AbstractDIMSE;
            if (dataDIMSE != null && dataDIMSE.Data != null)
            {
                List<byte[]> chunks = GetChunks(dataDIMSE.Data, maxPDULength, pContext);
                chunks
                    .Select((c, i) => new PDataTF(c, i == chunks.Count - 1, false, pContext))
                    .ToList()
                    .ForEach(list.Add);
            }
            return list;
        }
Example #6
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 #7
0
        public static List<PDataTF> GetPDataTFs(AbstractDIMSEBase dimse, Association asc, PresentationContext pContext = null)
        {
            pContext = pContext ?? asc.PresentationContexts.First(a => a.AbstractSyntax == dimse.AffectedSOPClassUID);
            var list = new List<PDataTF>();
            var commandEls = dimse.Elements;
            list.Add(new PDataTF(new DICOMObject(dimse.Elements), true, true, pContext));

            var dataDIMSE = dimse as AbstractDIMSE;
            if (dataDIMSE != null && dataDIMSE.Data != null)
            {
                List<byte[]> chunks = GetChunks(dataDIMSE.Data, asc.UserInfo.MaxPDULength, asc);
                chunks
                    .Select(
                        (c, i) =>
                            new PDataTF(c, i == chunks.Count - 1, false,
                                asc.PresentationContexts.First(a => a.AbstractSyntax == dimse.AffectedSOPClassUID)))
                    .ToList()
                    .ForEach(list.Add);
            }
            return list;
        }
 ///// <summary>
 ///// Sends a request for DIMSE association to a DICOM service
 ///// </summary>
 ///// <param name="asc">the underlying association</param>
 ///// <param name="abstractSyntax">the proposed abstract syntaxes (what should the service be able to do)</param>
 public static void SendRequest(Association asc, params string[] abstractSyntaxes)
 {
     var request = new Request {CalledEntityTitle = asc.AeTitle, CallingEntityTitle = asc.ServiceClass.ApplicationEntity.AeTitle};
     abstractSyntaxes.Select((a, i) => new {a, i})
         .ToList()
         .ForEach(a =>
         {
             var pres = new PresentationContext
             {
                 AbstractSyntax = a.a,
                 Id = a.i*2 + 1, //Convention of odd numbers
                 Reason = PresentationContextReason.ACCEPTANCE,
                 TransferSyntaxes = asc.ServiceClass.SupportedTransferSyntaxes
             };
             request.PresentationContexts.Add(pres);
         });
     asc.PresentationContexts.AddRange(request.PresentationContexts);
     request.UserInfo = new UserInfo();
     asc.Logger.Log("--> " + request);
     asc.SendMessage(request);
 }
Example #9
0
 public static void Send(AbstractDIMSEBase dimse, Association asc, PresentationContext pContext = null)
 {
     if (asc.State != NetworkState.TRANSPORT_CONNECTION_OPEN)
     {
         asc.OutboundMessages.Enqueue(dimse);
         AssociationMessenger.SendRequest(asc, dimse.AffectedSOPClassUID);
     }
     else
     {
         asc.Logger.Log("--> DIMSE" + dimse.GetLogString());
         dimse.LogData(asc);
         var stream = asc.Stream;
         List<PDataTF> pds = GetPDataTFs(dimse, asc, pContext);
         if (pds.Count > 0 && stream.CanWrite)
         {
             foreach (PDataTF pd in pds)
             {
                 byte[] message = pd.Write();
                 stream.Write(message, 0, message.Length);
             }
         }
     }
 }
Example #10
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 #11
0
 private static PresentationContext ReadPresentationCtxContents(byte[] contents, bool requestType = false)
 {
     var pc = new PresentationContext();
     pc.TransferSyntaxes = new List<string>();
     using (var dr = new DICOMBinaryReader(contents))
     {
         pc.Id = dr.Take(1)[0];
         dr.Skip(1); //Reserved Null Byte
         pc.Reason = (PresentationContextReason) Enum.ToObject(typeof (PresentationContextReason), dr.Take(1)[0]);
         dr.Skip(1); //Reserved Null Byte
         if (requestType)
         {
             pc.AbstractSyntax = ReadAbstractSyntax(dr).Trim();
         }
         while (dr.StreamPosition < dr.StreamLength)
         {
             long initPos = dr.StreamPosition;
             pc.TransferSyntaxes.Add(ReadTransferSyntax(dr));
             if (dr.StreamPosition == initPos)
             {
                 break;
             }
         }
     }
     return pc;
 }