Esempio n. 1
0
 internal DicomFragmentSequence(DicomFragmentSequence attrib)
     : base(attrib)
 {
     _isNull = attrib._isNull;
     foreach (DicomFragment fragment in attrib._fragments)
     {
         _fragments.Add(new DicomFragment(fragment));
     }
 }
 public DicomCompressedPixelData(DicomMessageBase msg, byte[] frameData) : base(msg)
 {
     _sq = new DicomFragmentSequence(DicomTags.PixelData);
     AddFrameFragment(frameData);
     //ByteBuffer buffer = new ByteBuffer(frameData);
     //DicomFragment fragment = new DicomFragment(buffer);
     //_sq.AddFragment(fragment);
     NumberOfFrames = 1;
 }
        public void AddFrameFragment(ByteBuffer bb)
        {
            DicomFragmentSequence sequence = _sq;

            if ((bb.Length % 2) == 1)
            {
                throw new DicomCodecException("Fragment being appended is incorrectly an odd length: " + bb.Length);
            }

            uint offset = 0;

            foreach (DicomFragment fragment in sequence.Fragments)
            {
                offset += (8 + fragment.Length);
            }
            sequence.OffsetTable.Add(offset);

            sequence.Fragments.Add(new DicomFragment(bb));
        }
        /// <summary>
        /// Append a compressed pixel data fragment's worth of data.  It is assumed an entire frame is
        /// contained within <paramref name="data"/>.
        /// </summary>
        /// <param name="data">The fragment data.</param>
        public void AddFrameFragment(byte[] data)
        {
            DicomFragmentSequence sequence = _sq;

            if ((data.Length % 2) == 1)
            {
                throw new DicomCodecException("Fragment being appended is incorrectly an odd length: " + data.Length);
            }

            uint offset = 0;

            foreach (DicomFragment fragment in sequence.Fragments)
            {
                offset += (8 + fragment.Length);
            }
            sequence.OffsetTable.Add(offset);

            ByteBuffer buffer = new ByteBuffer();

            buffer.Append(data, 0, data.Length);
            sequence.Fragments.Add(new DicomFragment(buffer));
        }
Esempio n. 5
0
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            DicomFragmentSequence sq = (DicomFragmentSequence)obj;

            if (sq.Count != this.Count)
            {
                return(false);
            }

            for (int i = 0; i < Count; i++)
            {
                if (!_fragments[i].Equals(sq._fragments[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
 /// <summary>
 /// Constructor from a <see cref="DicomUncompressedPixelData"/> instance.
 /// </summary>
 /// <param name="pd">The uuncompressed pixel data attribute to initialize the object from.</param>
 public DicomCompressedPixelData(DicomUncompressedPixelData pd) : base(pd)
 {
     _sq = new DicomFragmentSequence(DicomTags.PixelData);
 }
 /// <summary>
 /// Constructor from a <see cref="DicomAttributeCollection"/> instance.
 /// </summary>
 /// <param name="collection">The collection to initialize the object from.</param>
 public DicomCompressedPixelData(DicomAttributeCollection collection) : base(collection)
 {
     _sq = (DicomFragmentSequence)collection[DicomTags.PixelData];
 }
 /// <summary>
 /// Constructor from a <see cref="DicomMessageBase"/> instance.
 /// </summary>
 /// <param name="msg">The message to initialize the object from.</param>
 public DicomCompressedPixelData(DicomMessageBase msg)
     : base(msg)
 {
     _sq = (DicomFragmentSequence)msg.DataSet[DicomTags.PixelData];
 }
        /// <summary>
        /// Get the pixel data fragments for a frame.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Note that if an offset table was not included within the pixel data, this method will
        /// attempt to guess which fragments are contained within a frame.
        /// </para>
        /// </remarks>
        /// <param name="frame">The zero offset frame to get the fragments for.</param>
        /// <returns>A list of fragments associated with the frame.</returns>
        public List <DicomFragment> GetFrameFragments(int frame)
        {
            if (frame < 0 || frame >= NumberOfFrames)
            {
                throw new IndexOutOfRangeException("Requested frame out of range!");
            }

            List <DicomFragment>  fragments = new List <DicomFragment>();
            DicomFragmentSequence sequence  = _sq;
            int fragmentCount = sequence.Fragments.Count;

            if (NumberOfFrames == 1)
            {
                foreach (DicomFragment frag in sequence.Fragments)
                {
                    fragments.Add(frag);
                }
                return(fragments);
            }

            if (fragmentCount == NumberOfFrames)
            {
                fragments.Add(sequence.Fragments[frame]);
                return(fragments);
            }

            if (sequence.HasOffsetTable && sequence.OffsetTable.Count == NumberOfFrames)
            {
                uint offset = sequence.OffsetTable[frame];
                uint stop   = 0xffffffff;
                uint pos    = 0;

                if ((frame + 1) < NumberOfFrames)
                {
                    stop = sequence.OffsetTable[frame + 1];
                }

                int i = 0;
                while (pos < offset && i < fragmentCount)
                {
                    pos += (8 + sequence.Fragments[i].Length);
                    i++;
                }

                // check for invalid offset table
                if (pos != offset)
                {
                    goto GUESS_FRAME_OFFSET;
                }

                while (offset < stop && i < fragmentCount)
                {
                    fragments.Add(sequence.Fragments[i]);
                    offset += (8 + sequence.Fragments[i].Length);
                    i++;
                }

                return(fragments);
            }

GUESS_FRAME_OFFSET:
            if (sequence.Fragments.Count > 0)
            {
                uint fragmentSize = sequence.Fragments[0].Length;

                bool allSameLength = true;
                for (int i = 0; i < fragmentCount; i++)
                {
                    if (sequence.Fragments[i].Length != fragmentSize)
                    {
                        allSameLength = false;
                        break;
                    }
                }

                if (allSameLength)
                {
                    if ((fragmentCount % NumberOfFrames) != 0)
                    {
                        throw new DicomDataException("Unable to determine frame length from pixel data sequence!");
                    }

                    int count = fragmentCount / NumberOfFrames;
                    int start = frame * count;

                    for (int i = 0; i < fragmentCount; i++)
                    {
                        fragments.Add(sequence.Fragments[start + i]);
                    }

                    return(fragments);
                }
                else
                {
                    // what if a single frame ends on a fragment boundary?

                    int count = 0;
                    int start = 0;

                    for (int i = 0; i < fragmentCount && count < frame; i++, start++)
                    {
                        if (sequence.Fragments[i].Length != fragmentSize)
                        {
                            count++;
                        }
                    }

                    for (int i = start; i < fragmentCount; i++)
                    {
                        fragments.Add(sequence.Fragments[i]);
                        if (sequence.Fragments[i].Length != fragmentSize)
                        {
                            break;
                        }
                    }

                    return(fragments);
                }
            }

            return(fragments);
        }