Exemplo n.º 1
0
        /// <summary>
        /// Write dataset to stream
        /// </summary>
        /// <param name="dataset">Dataset</param>
        /// <param name="options">DICOM write options</param>
        /// <returns>Status code</returns>
        public DicomWriteStatus Write(DcmDataset dataset, DicomWriteOptions options)
        {
            TransferSyntax = dataset.InternalTransferSyntax;
            dataset.SelectByteOrder(_syntax.Endian);

            foreach (DcmItem item in dataset.Elements)
            {
                if (item.Tag.Element == 0x0000)
                {
                    continue;
                }

                if (Flags.IsSet(options, DicomWriteOptions.CalculateGroupLengths) &&
                    item.Tag.Group != _group && item.Tag.Group <= 0x7fe0)
                {
                    _group = item.Tag.Group;
                    _writer.Write((ushort)_group);
                    _writer.Write((ushort)0x0000);
                    if (_syntax.IsExplicitVR)
                    {
                        _writer.Write((byte)'U');
                        _writer.Write((byte)'L');
                        _writer.Write((ushort)4);
                    }
                    else
                    {
                        _writer.Write((uint)4);
                    }
                    _writer.Write((uint)dataset.CalculateGroupWriteLength(_group, _syntax, options));
                }

                _writer.Write((ushort)item.Tag.Group);
                _writer.Write((ushort)item.Tag.Element);

                if (_syntax.IsExplicitVR)
                {
                    _writer.Write((byte)item.VR.VR[0]);
                    _writer.Write((byte)item.VR.VR[1]);
                }

                if (item is DcmItemSequence)
                {
                    DcmItemSequence sq = item as DcmItemSequence;

                    if (_syntax.IsExplicitVR)
                    {
                        _writer.Write((ushort)0x0000);
                    }

                    if (Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence) || (item.Tag.IsPrivate && !_syntax.IsExplicitVR))
                    {
                        int hl = _syntax.IsExplicitVR ? 12 : 8;
                        _writer.Write((uint)sq.CalculateWriteLength(_syntax, options & ~DicomWriteOptions.CalculateGroupLengths) - (uint)hl);
                    }
                    else
                    {
                        _writer.Write((uint)UndefinedLength);
                    }

                    foreach (DcmItemSequenceItem ids in sq.SequenceItems)
                    {
                        ids.Dataset.ChangeTransferSyntax(dataset.InternalTransferSyntax, null);

                        _writer.Write((ushort)DicomTags.Item.Group);
                        _writer.Write((ushort)DicomTags.Item.Element);

                        if (Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem))
                        {
                            _writer.Write((uint)ids.CalculateWriteLength(_syntax, options & ~DicomWriteOptions.CalculateGroupLengths) - (uint)8);
                        }
                        else
                        {
                            _writer.Write((uint)UndefinedLength);
                        }

                        Write(ids.Dataset, options & ~DicomWriteOptions.CalculateGroupLengths);

                        if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem))
                        {
                            _writer.Write((ushort)DicomTags.ItemDelimitationItem.Group);
                            _writer.Write((ushort)DicomTags.ItemDelimitationItem.Element);
                            _writer.Write((uint)0x00000000);
                        }
                    }

                    if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence) && !(item.Tag.IsPrivate && !_syntax.IsExplicitVR))
                    {
                        _writer.Write((ushort)DicomTags.SequenceDelimitationItem.Group);
                        _writer.Write((ushort)DicomTags.SequenceDelimitationItem.Element);
                        _writer.Write((uint)0x00000000);
                    }
                }

                else if (item is DcmFragmentSequence)
                {
                    DcmFragmentSequence fs = item as DcmFragmentSequence;

                    if (_syntax.IsExplicitVR)
                    {
                        _writer.Write((ushort)0x0000);
                    }
                    _writer.Write((uint)UndefinedLength);

                    _writer.Write((ushort)DicomTags.Item.Group);
                    _writer.Write((ushort)DicomTags.Item.Element);

                    if (Flags.IsSet(options, DicomWriteOptions.WriteFragmentOffsetTable) && fs.HasOffsetTable)
                    {
                        _writer.Write((uint)fs.OffsetTableBuffer.Length);
                        fs.OffsetTableBuffer.CopyTo(_writer.BaseStream);
                    }
                    else
                    {
                        _writer.Write((uint)0x00000000);
                    }

                    foreach (ByteBuffer bb in fs.Fragments)
                    {
                        _writer.Write((ushort)DicomTags.Item.Group);
                        _writer.Write((ushort)DicomTags.Item.Element);
                        _writer.Write((uint)bb.Length);
                        bb.CopyTo(_writer.BaseStream);
                    }

                    _writer.Write((ushort)DicomTags.SequenceDelimitationItem.Group);
                    _writer.Write((ushort)DicomTags.SequenceDelimitationItem.Element);
                    _writer.Write((uint)0x00000000);
                }

                else
                {
                    DcmElement de = item as DcmElement;

                    if (_syntax.IsExplicitVR)
                    {
                        if (de.VR.Is16BitLengthField)
                        {
                            _writer.Write((ushort)de.Length);
                        }
                        else
                        {
                            _writer.Write((ushort)0x0000);
                            _writer.Write((uint)de.Length);
                        }
                    }
                    else
                    {
                        _writer.Write((uint)de.Length);
                    }

                    de.ByteBuffer.CopyTo(_writer.BaseStream);
                }
            }

            return(DicomWriteStatus.Success);
        }