Ejemplo n.º 1
0
        private void _sendImage(PSImage image, Stream outputStream, BinaryWriter writer)
        {
            var transferSyntax = TransferSyntaxes.Lookup(image.TransferSyntaxID);

            if (transferSyntax.Compression != DICOMSharp.Data.Compression.CompressionInfo.None)
            {
                // Need to decompress
                var data = new DICOMData();
                data.ParseFile(PSUtils.GetParsedFilePath(image.Path), true, PSUtils.GetLogger());
                data.Uncompress();

                var memStream = new MemoryStream();
                data.WriteToStreamAsPart10File(memStream, PSUtils.GetLogger());

                writer.Write((UInt32)(memStream.Length + 4));
                memStream.Position = 0;
                memStream.CopyTo(outputStream);
            }
            else
            {
                // Write the file out directly.
                var fileInfo = new FileInfo(PSUtils.GetParsedFilePath(image.Path));
                var size     = fileInfo.Length;
                writer.Write((UInt32)(size + 4));
                fileInfo.OpenRead().CopyTo(outputStream);
            }
        }
Ejemplo n.º 2
0
        private TaskInfo SendStudy(string studyInstanceUid, string aeTarget)
        {
            var taskInfo = new TaskInfo()
            {
                Token = new CancellationTokenSource()
            };

            taskInfo.Task = Task.Run(() =>
            {
                taskInfo.Token.Token.ThrowIfCancellationRequested();

                this._logger.Log(LogLevel.Info, "Sending study to " + aeTarget + ": " + studyInstanceUid);

                var remoteAe = this._db.GetEntity(aeTarget);
                if (remoteAe == null)
                {
                    this._logger.Log(LogLevel.Error, "Unknown send target AE: " + aeTarget);
                    return;
                }

                var images = this._db.FetchStudyImages(studyInstanceUid);

                taskInfo.ProgressCount = 0;
                taskInfo.ProgressTotal = images.Count;

                TaskCompletionSource <bool> source = new TaskCompletionSource <bool>();
                var sender          = new DICOMSender(this._logger, aeTarget + "/" + studyInstanceUid, this._settings.VerboseLogging);
                sender.SCUFinished += (DICOMSCU scu, bool success) =>
                {
                    source.SetResult(success);
                };
                sender.SendUpdate += (DICOMSender senderx, ushort remaining, ushort completed, ushort warned, ushort failed) =>
                {
                    taskInfo.ProgressCount = completed + warned + failed;
                };
                sender.Send(this.GetHostingAE(), remoteAe, images.Select(image => new SendableImage
                {
                    FilePath       = _db.FixImagePath(image.Path),
                    AbstractSyntax = AbstractSyntaxes.Lookup(image.SOPClassID),
                    TransferSyntax = TransferSyntaxes.Lookup(image.TransferSyntaxID)
                }));

                source.Task.Wait();

                this._logger.Log(LogLevel.Info, "Done sending study to " + aeTarget + ": " + studyInstanceUid);
            }, taskInfo.Token.Token);

            return(taskInfo);
        }
Ejemplo n.º 3
0
        internal void ParsePacket(SwappableBinaryReader dataSource, int itemLength)
        {
            long startOffset = dataSource.BaseStream.Position;

            ContextID = dataSource.ReadByte();
            byte reserved = dataSource.ReadByte();

            Result   = (PresentationResult)dataSource.ReadByte();
            reserved = dataSource.ReadByte();

            while (dataSource.BaseStream.Position - startOffset < itemLength)
            {
                //read sub-item
                byte   subItemType     = dataSource.ReadByte();
                byte   subItemReserved = dataSource.ReadByte();
                ushort subItemLength   = dataSource.ReadUInt16();

                if (subItemType == 0x30)
                {
                    string rawSyntax = Uid.UidRawToString(dataSource.ReadBytes(subItemLength));
                    AbstractSyntaxSpecified = AbstractSyntaxes.Lookup(rawSyntax);
                }
                else if (subItemType == 0x40)
                {
                    string         rawSyntax = Uid.UidRawToString(dataSource.ReadBytes(subItemLength));
                    TransferSyntax syntax    = TransferSyntaxes.Lookup(rawSyntax);
                    if (syntax != null)
                    {
                        TransferSyntaxesProposed.Add(syntax);
                    }
                }
                else
                {
                    //no idea what it is, or we don't care
                    dataSource.ReadBytes(itemLength);
                }
            }
        }
Ejemplo n.º 4
0
        internal bool ParseStream(Stream stream, TransferSyntax transferSyntax, bool allowSyntaxChanges, bool loadImageData, ILogger logger)
        {
            SwappableBinaryReader sr = new SwappableBinaryReader(stream);
            long readPosition        = stream.Position;

            bool inGroup2 = false;

            if (transferSyntax.MSBSwap) //this should be applicable...
            {
                sr.ToggleSwapped();
            }

            TransferSyntax parsedSyntax = null;

            while (readPosition + 8 < stream.Length)
            {
                //Read in group/element info
                ushort group = sr.ReadUInt16();
                ushort elem  = sr.ReadUInt16();

                //Leaving the header?
                if (inGroup2 != (group == 2))
                {
                    if (transferSyntax.MSBSwap)
                    {
                        sr.ToggleSwapped();
                    }

                    inGroup2 = (group == 2);
                }

                //Stop loading if we're at image data and not supposed to read it
                var skipData = !loadImageData && group == 0x7FE0 && elem == 0x0010;

                //Make element
                uint         outLen;
                DICOMElement nelem;
                try
                {
                    nelem = DICOMElement.Parse(group, elem, logger, transferSyntax, sr, null, skipData, out outLen);
                }
                catch (Exception e)
                {
                    logger.Log(LogLevel.Error, "Exception in DICOMElement.Parse: " + e.ToString());
                    return(false);
                }

                //Debugging:
                //Console.WriteLine(nelem.Dump());

                //Store reading position in case it's useful later
                nelem.ReadPosition = readPosition;

                //Store element in lookup array
                Elements[nelem.Tag] = nelem;

                //Store transfer syntax change for after the header
                if (nelem.Tag == DICOMTags.TransferSyntaxUID)
                {
                    parsedSyntax = TransferSyntaxes.Lookup((string)nelem.Data);
                    if (allowSyntaxChanges)
                    {
                        transferSyntax = parsedSyntax;
                    }
                }

                //update read position pointer
                readPosition = stream.Position;
            }

            //Store whatever TS we ended up with
            TransferSyntax = (parsedSyntax != null) ? parsedSyntax : transferSyntax;

            return(true);
        }