Пример #1
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);
            }

            _target.Write(buffer.Data, 0, buffer.Size);

            return true;
        }
Пример #2
0
            public override IByteBuffer GetFrame(int frame)
            {
                if (frame < 0 || frame >= NumberOfFrames) throw new IndexOutOfRangeException("Requested frame out of range!");

                IByteBuffer buffer = null;

                if (NumberOfFrames == 1)
                {
                    if (Element.Fragments.Count == 1) buffer = Element.Fragments[0];
                    else buffer = new CompositeByteBuffer(Element.Fragments.ToArray());
                }
                else if (Element.Fragments.Count == NumberOfFrames) buffer = Element.Fragments[frame];
                else if (Element.OffsetTable.Count == NumberOfFrames)
                {
                    uint start = Element.OffsetTable[frame];
                    uint stop = (Element.OffsetTable.Count == (frame + 1))
                                    ? uint.MaxValue
                                    : Element.OffsetTable[frame + 1];

                    var composite = new CompositeByteBuffer();

                    uint pos = 0;
                    int frag = 0;

                    while (pos < start && frag < Element.Fragments.Count)
                    {
                        pos += 8;
                        pos += Element.Fragments[frag].Size;
                        frag++;
                    }

                    if (pos != start) throw new DicomImagingException("Fragment start position does not match offset table.");

                    while (pos < stop && frag < Element.Fragments.Count)
                    {
                        composite.Buffers.Add(Element.Fragments[frag]);

                        pos += 8;
                        pos += Element.Fragments[frag].Size;
                        frag++;
                    }

                    if (pos < stop && stop != uint.MaxValue)
                        throw new DicomImagingException(
                            "Image frame truncated while reading fragments from offset table.");

                    buffer = composite;
                }
                else
                    throw new DicomImagingException(
                        "Support for multi-frame images with varying fragment sizes and no offset table has not been implemented.");

                // mainly for GE Private Implicit VR Little Endian
                if (!Syntax.IsEncapsulated && BitsAllocated == 16 && Syntax.SwapPixelData) buffer = new SwapByteBuffer(buffer, 2);

                return EndianByteBuffer.Create(buffer, Syntax.Endian, BytesAllocated);
            }
Пример #3
0
            public override void AddFrame(IByteBuffer data)
            {
                if (!(Element.Buffer is CompositeByteBuffer)) throw new DicomImagingException("Expected pixel data element to have a CompositeByteBuffer.");

                CompositeByteBuffer buffer = Element.Buffer as CompositeByteBuffer;

                if (Syntax.SwapPixelData) data = new SwapByteBuffer(data, 2);

                buffer.Buffers.Add(data);
                NumberOfFrames++;
            }
Пример #4
0
            public override IByteBuffer GetFrame(int frame)
            {
                if (frame < 0 || frame >= NumberOfFrames) throw new IndexOutOfRangeException("Requested frame out of range!");

                int offset = UncompressedFrameSize * frame;
                IByteBuffer buffer = new RangeByteBuffer(Element.Buffer, (uint)offset, (uint)UncompressedFrameSize);

                // mainly for GE Private Implicit VR Big Endian
                if (Syntax.SwapPixelData) buffer = new SwapByteBuffer(buffer, 2);

                return buffer;
            }
Пример #5
0
            public override IByteBuffer GetFrame(int frame)
            {
                int offset = UncompressedFrameSize * frame;
                IByteBuffer buffer = new RangeByteBuffer(Element.Buffer, (uint)offset, (uint)UncompressedFrameSize);

                //TODO: trace down the need for this additional byte swap
                if (Syntax.Endian == Endian.Big && !Syntax.SwapPixelData)
                    buffer = new SwapByteBuffer(buffer, 2);

                // mainly for GE Private Implicit VR Little Endian
                if (Syntax.SwapPixelData)
                    buffer = new SwapByteBuffer(buffer, 2);

                return buffer;
            }
Пример #6
0
        /// <summary>
        /// Asynchronous handler for traversing a DICOM element.
        /// </summary>
        /// <param name="element">Element to traverse.</param>
        /// <returns>true if traversing completed without issues, false otherwise.</returns>
        public async Task<bool> OnElementAsync(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 < this._options.LargeObjectSize)
            {
                _target.Write(buffer.Data, 0, buffer.Size);
            }
            else
            {
                await _target.WriteAsync(buffer.Data, 0, buffer.Size).ConfigureAwait(false);
            }

            return true;
        }
Пример #7
0
            public override IByteBuffer GetFrame(int frame)
            {
                if (frame < 0 || frame >= NumberOfFrames)
                    throw new IndexOutOfRangeException("Requested frame out of range!");

                int offset = UncompressedFrameSize * frame;
                IByteBuffer buffer = new RangeByteBuffer(Element.Buffer, (uint)offset, (uint)UncompressedFrameSize);

                //TODO: trace down the need for this additional byte swap
                if (Syntax.Endian == Endian.Big && !Syntax.SwapPixelData)
                    buffer = new SwapByteBuffer(buffer, 2);

                // mainly for GE Private Implicit VR Little Endian
                if (Syntax.SwapPixelData)
                    buffer = new SwapByteBuffer(buffer, 2);

                return buffer;
            }