Esempio n. 1
0
        public void writeToTarget(IByteTarget _target, IByteBuffer fb)
        {
            const int seg = 4096000;
            int       gcd = (int)(fb.Size / seg);
            int       gcf = (int)(fb.Size % seg);

            if (gcd == 0)
            {
                _target.Write(fb.Data, 0, fb.Size);
            }
            else
            {
                for (int i = 0; i < gcd; i++)
                {
                    var rgBuffer = fb.GetByteRange(i * seg, seg);
                    _target.Write(rgBuffer, 0, seg);
                    rgBuffer = null;
                }

                if (gcf > 0)
                {
                    var rgBuffer = fb.GetByteRange(gcd * seg, gcf);

                    _target.Write(rgBuffer, 0, (uint)gcf);

                    rgBuffer = null;
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Handler for traversing beginning of fragment.
 /// </summary>
 /// <param name="fragment">Fragment sequence.</param>
 /// <returns>true if traversing completed without issues, false otherwise.</returns>
 /// <remarks>On false return value, the method will invoke the callback method passed in <see cref="IDicomDatasetWalker.OnBeginWalk"/> before returning.</remarks>
 public bool OnBeginFragment(DicomFragmentSequence fragment)
 {
     WriteTagHeader(fragment.Tag, fragment.ValueRepresentation, UndefinedLength);
     WriteTagHeader(DicomTag.Item, DicomVR.NONE, (uint)(fragment.OffsetTable.Count * 4));
     foreach (uint offset in fragment.OffsetTable)
     {
         _target.Write(offset);
     }
     return(true);
 }
Esempio n. 3
0
        public void writeToTarget(IByteTarget _target, IByteBuffer buffer)
        {
            int count = 0;

            byte[] copyBuff = new byte[1024 * 1024];
            Stream.Position = Position;
            while (count + 1024 * 1024 < Size)
            {
                Stream.Read(copyBuff, 0, 1024 * 1024);
                count += 1024 * 1024;
                _target.Write(copyBuff, 0, 1024 * 1024);
            }
            Stream.Read(copyBuff, 0, (int)(Size - count));
            _target.Write(copyBuff, 0, (uint)(Size - count));
        }
Esempio n. 4
0
        private static void WriteBuffer(IByteTarget target, IByteBuffer buffer, uint largeObjectSize)
        {
            var offset        = 0;
            var remainingSize = buffer.Size;

            while (remainingSize > largeObjectSize)
            {
                var range = buffer.GetByteRange(offset, (int)largeObjectSize);
                target.Write(range, 0, largeObjectSize);

                offset        += (int)largeObjectSize;
                remainingSize -= largeObjectSize;
            }

            target.Write(buffer.GetByteRange(offset, (int)remainingSize), 0, remainingSize);
        }
        /// <summary>
        /// Write DICOM dataset.
        /// </summary>
        /// <param name="target">Byte target subject to writing.</param>
        /// <param name="syntax">Transfer syntax applicable to dataset.</param>
        /// <param name="dataset">Dataset.</param>
        /// <param name="options">Writer options.</param>
        private static void WriteDataset(
            IByteTarget target,
            DicomTransferSyntax syntax,
            DicomDataset dataset,
            DicomWriteOptions options)
        {
            UpdateDatasetGroupLengths(syntax, dataset, options);

            if (syntax.IsDeflate)
            {
                using (var uncompressed = new MemoryStream())
                {
                    var temp = new StreamByteTarget(uncompressed);
                    WalkDataset(temp, syntax, dataset, options);

                    uncompressed.Seek(0, SeekOrigin.Begin);
                    using (var compressed = new MemoryStream())
                    {
                        using (var compressor = new DeflateStream(compressed, CompressionMode.Compress, true))
                        {
                            uncompressed.CopyTo(compressor);
                        }

                        target.Write(compressed.ToArray(), 0, (uint)compressed.Length);
                    }
                }
            }
            else
            {
                WalkDataset(target, syntax, dataset, options);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Write DICOM dataset.
        /// </summary>
        /// <param name="target">Byte target subject to writing.</param>
        /// <param name="syntax">Transfer syntax applicable to dataset.</param>
        /// <param name="dataset">Dataset.</param>
        /// <param name="options">Writer options.</param>
        private static async Task WriteDatasetAsync(
            IByteTarget target,
            DicomTransferSyntax syntax,
            DicomDataset dataset,
            DicomWriteOptions options)
        {
            dataset.OnBeforeSerializing();
            UpdateDatasetGroupLengths(syntax, dataset, options);

            if (syntax.IsDeflate)
            {
                using (var uncompressed = new MemoryStream())
                {
                    var temp = new StreamByteTarget(uncompressed);
                    await WalkDatasetAsync(temp, syntax, dataset, options).ConfigureAwait(false);

                    uncompressed.Seek(0, SeekOrigin.Begin);
                    using (var compressed = new MemoryStream())
                    {
                        using (var compressor = new DeflateStream(compressed, CompressionMode.Compress, true))
                        {
                            uncompressed.CopyTo(compressor);
                        }

                        target.Write(compressed.ToArray(), 0, (uint)compressed.Length);
                    }
                }
            }
            else
            {
                await WalkDatasetAsync(target, syntax, dataset, options).ConfigureAwait(false);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Write DICOM dataset.
        /// </summary>
        /// <param name="target">Byte target subject to writing.</param>
        /// <param name="syntax">Transfer syntax applicable to dataset.</param>
        /// <param name="dataset">Dataset.</param>
        /// <param name="options">Writer options.</param>
        private static void WriteDataset(
            IByteTarget target,
            DicomTransferSyntax syntax,
            DicomDataset dataset,
            DicomWriteOptions options)
        {
            UpdateDatasetGroupLengths(syntax, dataset, options);

            if (syntax.IsDeflate)
            {
#if NET35
                throw new NotSupportedException("Deflated datasets not supported in Unity.");
#else
                using (var uncompressed = new MemoryStream())
                {
                    var temp = new StreamByteTarget(uncompressed);
                    WalkDataset(temp, syntax, dataset, options);

                    uncompressed.Seek(0, SeekOrigin.Begin);
                    using (var compressed = new MemoryStream())
                        using (var compressor = new DeflateStream(compressed, CompressionMode.Compress))
                        {
                            uncompressed.CopyTo(compressor);
                            target.Write(compressed.ToArray(), 0, (uint)compressed.Length);
                        }
                }
#endif
            }
            else
            {
                WalkDataset(target, syntax, dataset, options);
            }
        }
Esempio n. 8
0
        private static void WriteBuffer(IByteTarget target, IByteBuffer buffer, uint largeObjectSize)
        {
            uint offset        = 0;
            uint remainingSize = buffer.Size;

            while (remainingSize > largeObjectSize)
            {
                byte[] range = GetBufferRange(buffer, offset, (int)largeObjectSize);
                target.Write(range, 0, largeObjectSize);

                offset        += largeObjectSize;
                remainingSize -= largeObjectSize;
            }
            byte[] endRange = GetBufferRange(buffer, offset, (int)remainingSize);
            target.Write(endRange, 0, remainingSize);
        }
Esempio n. 9
0
        /// <summary>
        /// Write DICOM file preamble.
        /// </summary>
        /// <param name="target">Byte target subject to writing.</param>
        private static void WritePreamble(IByteTarget target)
        {
            var preamble = new byte[132];
            preamble[128] = (byte)'D';
            preamble[129] = (byte)'I';
            preamble[130] = (byte)'C';
            preamble[131] = (byte)'M';

            target.Write(preamble, 0, 132);
        }
Esempio n. 10
0
        ////该函数在.net4中不支持
        ///// <summary>
        ///// Write DICOM Part 10 object to <paramref name="target"/> asynchronously.
        ///// </summary>
        ///// <param name="target">Byte target subject to writing.</param>
        ///// <param name="fileMetaInfo">File meta information.</param>
        ///// <param name="dataset">Dataset.</param>
        ///// <returns>Awaitable <see cref="Task"/>.</returns>
        //public async Task WriteAsync(IByteTarget target, DicomFileMetaInformation fileMetaInfo, DicomDataset dataset)
        //{
        //    await WritePreambleAsync(target).ConfigureAwait(false);
        //    await WriteFileMetaInfoAsync(target, fileMetaInfo, this.options).ConfigureAwait(false);
        //    await WriteDatasetAsync(target, fileMetaInfo.TransferSyntax, dataset, this.options).ConfigureAwait(false);
        //}

        /// <summary>
        /// Write DICOM file preamble.
        /// </summary>
        /// <param name="target">Byte target subject to writing.</param>
        private static void WritePreamble(IByteTarget target)
        {
            var preamble = new byte[132];

            preamble[128] = (byte)'D';
            preamble[129] = (byte)'I';
            preamble[130] = (byte)'C';
            preamble[131] = (byte)'M';

            target.Write(preamble, 0, 132);
        }
        public void writeToTarget(IByteTarget _target, IByteBuffer buffer)
        {
            var pb        = buffer as PInvokeByteBuffer;
            var rowBuffer = new byte[pb.RowBufferSize];

            for (int i = 0; i < pb.Height; i++)
            {
                pb.GetByteRange(i, rowBuffer);
                _target.Write(rowBuffer, 0, (uint)rowBuffer.Length);
            }
            rowBuffer = null;
        }
Esempio n. 12
0
 public void writeToTarget(IByteTarget target, IByteBuffer buff)
 {
     foreach (IByteBuffer buffer in Buffers)
     {
         if (buffer is StreamByteBuffer || buffer is FileByteBuffer || buffer is PInvokeByteBuffer)
         {
             buffer.writeToTarget(target, buff);
         }
         else
         {
             target.Write(buffer.Data, 0, buffer.Size);
         }
     }
 }
Esempio n. 13
0
        //直接写入target,避免调用 public byte[] Data 占用内存
        public void writeToTarget(IByteTarget target)
        {
            foreach (IByteBuffer buffer in Buffers)
            {
                if (buffer is PInvokeByteBuffer)
                {
                    var pb = buffer as PInvokeByteBuffer;

                    var rowBuffer = new byte[pb.RowBufferSize];
                    for (int i = 0; i < pb.Height; i++)
                    {
                        pb.GetByteRange(i, rowBuffer);
                        target.Write(rowBuffer, 0, (uint)rowBuffer.Length);
                    }
                    rowBuffer = null;
                }
                else
                {
                    var data = buffer.Data;
                    target.Write(data, 0, (uint)data.Length);
                }
            }
        }
Esempio n. 14
0
        private static async Task WriteBufferAsync(IByteTarget target, IByteBuffer buffer, uint largeObjectSize)
        {
            var offset        = 0;
            var remainingSize = buffer.Size;

            while (remainingSize > largeObjectSize)
            {
                var range = buffer.GetByteRange(offset, (int)largeObjectSize);
                await target.WriteAsync(range, 0, largeObjectSize).ConfigureAwait(false);

                offset        += (int)largeObjectSize;
                remainingSize -= largeObjectSize;
            }

            target.Write(buffer.GetByteRange(offset, (int)remainingSize), 0, remainingSize);
        }
Esempio n. 15
0
		public IAsyncResult BeginWrite(IByteTarget target, DicomFileMetaInformation fileMetaInfo, DicomDataset dataset, AsyncCallback callback, object state) {
			_target = target;
			_fileMetaInfo = fileMetaInfo;
			_dataset = dataset;

			_async = new EventAsyncResult(callback, state);

			byte[] preamble = new byte[132];
			preamble[128] = (byte)'D';
			preamble[129] = (byte)'I';
			preamble[130] = (byte)'C';
			preamble[131] = (byte)'M';

			_target.Write(preamble, 0, 132, OnCompletePreamble, null);

			return _async;
		}
Esempio n. 16
0
        public IAsyncResult BeginWrite(IByteTarget target, DicomFileMetaInformation fileMetaInfo, DicomDataset dataset, AsyncCallback callback, object state)
        {
            _target       = target;
            _fileMetaInfo = fileMetaInfo;
            _dataset      = dataset;

            _async = new EventAsyncResult(callback, state);

            byte[] preamble = new byte[132];
            preamble[128] = (byte)'D';
            preamble[129] = (byte)'I';
            preamble[130] = (byte)'C';
            preamble[131] = (byte)'M';

            _target.Write(preamble, 0, 132, OnCompletePreamble, null);

            return(_async);
        }
Esempio n. 17
0
        /// <summary>
        /// Write DICOM dataset.
        /// </summary>
        /// <param name="target">Byte target subject to writing.</param>
        /// <param name="syntax">Transfer syntax applicable to dataset.</param>
        /// <param name="dataset">Dataset.</param>
        /// <param name="options">Writer options.</param>
        private static void WriteDataset(
            IByteTarget target,
            DicomTransferSyntax syntax,
            DicomDataset dataset,
            DicomWriteOptions options)
        {
            UpdateDatasetGroupLengths(syntax, dataset, options);

            if (syntax.IsDeflate)
            {
#if NET35
                throw new NotSupportedException("Deflated datasets not supported in Unity.");
#else
                using (var uncompressed = new MemoryStream())
                {
                    var temp = new StreamByteTarget(uncompressed);
                    WalkDataset(temp, syntax, dataset, options);

                    uncompressed.Seek(0, SeekOrigin.Begin);
                    using (var compressed = new MemoryStream())
                    using (var compressor = new DeflateStream(compressed, CompressionMode.Compress))
                    {
                        uncompressed.CopyTo(compressor);
                        target.Write(compressed.ToArray(), 0, (uint)compressed.Length);
                    }
                }
#endif
            }
            else
            {
                WalkDataset(target, syntax, dataset, options);
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Write DICOM dataset.
        /// </summary>
        /// <param name="target">Byte target subject to writing.</param>
        /// <param name="syntax">Transfer syntax applicable to dataset.</param>
        /// <param name="dataset">Dataset.</param>
        /// <param name="options">Writer options.</param>
        private static async Task WriteDatasetAsync(
            IByteTarget target,
            DicomTransferSyntax syntax,
            DicomDataset dataset,
            DicomWriteOptions options)
        {
            UpdateDatasetGroupLengths(syntax, dataset, options);


            if (syntax.IsDeflate)
            {
                using (var uncompressed = new MemoryStream())
                {
                    var temp = new StreamByteTarget(uncompressed);
                    await WalkDatasetAsync(temp, syntax, dataset, options).ConfigureAwait(false);

                    uncompressed.Seek(0, SeekOrigin.Begin);
                    using (var compressed = new MemoryStream())
                    using (var compressor = new DeflateStream(compressed, CompressionMode.Compress))
                    {
                        uncompressed.CopyTo(compressor);
                        target.Write(compressed.ToArray(), 0, (uint)compressed.Length);
                    }
                }
            }
            else
            {
                await WalkDatasetAsync(target, syntax, dataset, options).ConfigureAwait(false);
            }
        }
Esempio n. 19
0
        /// <summary>
        /// Handler for traversing a DICOM element.
        /// </summary>
        /// <param name="element">Element to traverse.</param>
        /// <returns>true if traversing completed without issues, false otherwise.</returns>
        /// <remarks>On false return value, the method will invoke the callback method passed in <see cref="IDicomDatasetWalker.OnBeginWalk"/> before returning.</remarks>
        public bool OnElement(DicomElement element)
        {
            WriteTagHeader(element.Tag, element.ValueRepresentation, element.Length);

            IByteBuffer buffer = element.Buffer;

            if (buffer is EndianByteBuffer)
            {
                EndianByteBuffer ebb = buffer as EndianByteBuffer;
                if (ebb.Endian != Endian.LocalMachine && ebb.Endian == _target.Endian)
                {
                    buffer = ebb.Internal;
                }
            }
            else if (_target.Endian != Endian.LocalMachine)
            {
                if (element.ValueRepresentation.UnitSize > 1)
                {
                    buffer = new SwapByteBuffer(buffer, element.ValueRepresentation.UnitSize);
                }
            }

            if (buffer is CompositeByteBuffer || buffer is StreamByteBuffer || buffer is FileByteBuffer || buffer is PInvokeByteBuffer)
            {
                buffer.writeToTarget(_target, buffer);
            }
            else
            {
                _target.Write(buffer.Data, 0, buffer.Size);
            }
            //if (buffer is CompositeByteBuffer)// 对CompositeByteBuffer避免调用buffer.Data 以降低内存占用
            //{
            //    var tmpbuffer = buffer as CompositeByteBuffer;

            //    tmpbuffer.writeToTarget(_target);
            //}
            //else if (buffer is StreamByteBuffer)
            //{
            //    var tmpbuffer = buffer as StreamByteBuffer;
            //    tmpbuffer.writeToTarget(_target);
            //}
            //else if (buffer is FileByteBuffer)
            //{
            //    var fb = buffer as FileByteBuffer;
            //    const int seg = 4096000;
            //    int gcd = (int)(fb.Size / seg);
            //    int gcf = (int)(fb.Size % seg);

            //    if (gcd == 0)
            //    {
            //        _target.Write(fb.Data, 0, fb.Size);
            //    }
            //    else
            //    {
            //        for (int i = 0; i < gcd; i++)
            //        {

            //            var rgBuffer = fb.GetByteRange(i * seg, seg);
            //            _target.Write(rgBuffer, 0, seg);
            //            rgBuffer = null;
            //        }

            //        if (gcf > 0)
            //        {

            //            var rgBuffer = fb.GetByteRange(gcd * seg, gcf);

            //            _target.Write(rgBuffer, 0, (uint)gcf);

            //            rgBuffer = null;
            //        }


            //    }

            //}
            //else if (buffer is PInvokeByteBuffer)
            //{

            //    var pb = buffer as PInvokeByteBuffer;

            //    var rowBuffer = new byte[pb.RowBufferSize];
            //    for (int i = 0; i < pb.Height; i++)
            //    {
            //        pb.GetByteRange(i, rowBuffer);
            //        _target.Write(rowBuffer, 0, (uint)rowBuffer.Length);
            //    }
            //    rowBuffer = null;
            //}
            //else
            //{
            //    _target.Write(buffer.Data, 0, buffer.Size);
            //}
            return(true);
        }
Esempio n. 20
0
        public bool OnElement(DicomElement element)
        {
            WriteTagHeader(element.Tag, element.ValueRepresentation, element.Length);

            IByteBuffer buffer = element.Buffer;

            if (buffer is EndianByteBuffer)
            {
                EndianByteBuffer ebb = buffer as EndianByteBuffer;
                if (ebb.Endian != Endian.LocalMachine && ebb.Endian == _target.Endian)
                {
                    buffer = ebb.Internal;
                }
            }
            else if (_target.Endian != Endian.LocalMachine)
            {
                if (element.ValueRepresentation.UnitSize > 1)
                {
                    buffer = new SwapByteBuffer(buffer, element.ValueRepresentation.UnitSize);
                }
            }

            if (element.Length >= _options.LargeObjectSize)
            {
                _target.Write(buffer.Data, 0, buffer.Size, OnEndWriteBuffer, null);
                return(false);
            }
            else
            {
                _target.Write(buffer.Data);
                return(true);
            }
        }