public void TestSequenceImplicitVr2() { var syntax = TransferSyntax.ImplicitVrLittleEndian; var sq = new DicomAttributeSQ(DicomTags.IconImageSequence); var item1 = new DicomSequenceItem(); item1[DicomTags.AccessionNumber].SetStringValue("ABCDEFG"); sq.AddSequenceItem(item1); var item2 = new DicomSequenceItem(); item2[DicomTags.AccessionNumber].SetStringValue("XYZ"); sq.AddSequenceItem(item2); // note: both sequence and item delimiter tags are calculated as part of the sequence - the item considers only the total length of its contents // item1_length = contents(16) Assert.AreEqual(16, item1[DicomTags.AccessionNumber].CalculateWriteLength(syntax, DicomWriteOptions.None), "Attr1 Length"); Assert.AreEqual(16, item1.CalculateWriteLength(syntax, DicomWriteOptions.None), "Item1 Length"); // item2_length = contents(12) Assert.AreEqual(12, item2[DicomTags.AccessionNumber].CalculateWriteLength(syntax, DicomWriteOptions.None), "Attr2 Length"); Assert.AreEqual(12, item2.CalculateWriteLength(syntax, DicomWriteOptions.None), "Item2 Length"); // sq_length = tag(4) + len(4) + item_tag(4) + item_len(4) + item1_length(16) + item_delimiter_tag(4) + item_delimiter_len(4) // + item_tag(4) + item_len(4) + item2_length(12) + item_delimiter_tag(4) + item_delimiter_len(4) // + sq_delimiter_tag(4) + sq_delimiter_len(4) var options = DicomWriteOptions.None; Assert.AreEqual(76, sq.CalculateWriteLength(syntax, options), "SQ Length (Options={0})", options); // sq_length = tag(4) + len(4) + item_tag(4) + item_len(4) + item1_length(16) + item_delimiter_tag(4) + item_delimiter_len(4) // + item_tag(4) + item_len(4) + item2_length(12) + item_delimiter_tag(4) + item_delimiter_len(4) options = DicomWriteOptions.ExplicitLengthSequence; Assert.AreEqual(68, sq.CalculateWriteLength(syntax, options), "SQ Length (Options={0})", options); // sq_length = tag(4) + len(4) + item_tag(4) + item_len(4) + item1_length(16) // + item_tag(4) + item_len(4) + item2_length(12) // + sq_delimiter_tag(4) + sq_delimiter_len(4) options = DicomWriteOptions.ExplicitLengthSequenceItem; Assert.AreEqual(60, sq.CalculateWriteLength(syntax, options), "SQ Length (Options={0})", options); // sq_length = tag(4) + len(4) + item_tag(4) + item_len(4) + item1_length(16) // + item_tag(4) + item_len(4) + item2_length(12) options = DicomWriteOptions.ExplicitLengthSequence | DicomWriteOptions.ExplicitLengthSequenceItem; Assert.AreEqual(52, sq.CalculateWriteLength(syntax, options), "SQ Length (Options={0})", options); }
public void TestSequenceImplicitVr() { var syntax = TransferSyntax.ImplicitVrLittleEndian; var sq = new DicomAttributeSQ(DicomTags.IconImageSequence); // note: we handle an empty sequence (0 items) by explicitly encoding zero length regardless of options // sq_length = tag(4) + len(4) var options = DicomWriteOptions.None; Assert.AreEqual(8, sq.CalculateWriteLength(syntax, options), "SQ Length (Options={0})", options); // sq_length = tag(4) + len(4) options = DicomWriteOptions.ExplicitLengthSequence; Assert.AreEqual(8, sq.CalculateWriteLength(syntax, options), "SQ Length (Options={0})", options); // sq_length = tag(4) + len(4) options = DicomWriteOptions.ExplicitLengthSequenceItem; Assert.AreEqual(8, sq.CalculateWriteLength(syntax, options), "SQ Length (Options={0})", options); // sq_length = tag(4) + len(4) options = DicomWriteOptions.ExplicitLengthSequence | DicomWriteOptions.ExplicitLengthSequenceItem; Assert.AreEqual(8, sq.CalculateWriteLength(syntax, options), "SQ Length (Options={0})", options); }
public DicomWriteStatus Write(TransferSyntax syntax, DicomAttributeCollection dataset, DicomWriteOptions options) { TransferSyntax = syntax; foreach (DicomAttribute item in dataset) { if (item.Tag.Element == 0x0000) { continue; } if (item.IsEmpty) { 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.ExplicitVr) { _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.ExplicitVr) { _writer.Write((byte)item.Tag.VR.Name[0]); _writer.Write((byte)item.Tag.VR.Name[1]); } if (item is DicomAttributeSQ) { DicomAttributeSQ sq = item as DicomAttributeSQ; if (_syntax.ExplicitVr) { _writer.Write((ushort)0x0000); } if (Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence)) { int hl = _syntax.ExplicitVr ? 12 : 8; _writer.Write((uint)sq.CalculateWriteLength(_syntax, options & ~DicomWriteOptions.CalculateGroupLengths) - (uint)hl); } else { _writer.Write((uint)UndefinedLength); } foreach (DicomSequenceItem ids in item.Values as DicomSequenceItem[]) { _writer.Write((ushort)DicomTag.Item.Group); _writer.Write((ushort)DicomTag.Item.Element); if (Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem)) { _writer.Write((uint)ids.CalculateWriteLength(_syntax, options & ~DicomWriteOptions.CalculateGroupLengths)); } else { _writer.Write((uint)UndefinedLength); } Write(this.TransferSyntax, ids, options & ~DicomWriteOptions.CalculateGroupLengths); if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem)) { _writer.Write((ushort)DicomTag.ItemDelimitationItem.Group); _writer.Write((ushort)DicomTag.ItemDelimitationItem.Element); _writer.Write((uint)0x00000000); } } if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence)) { _writer.Write((ushort)DicomTag.SequenceDelimitationItem.Group); _writer.Write((ushort)DicomTag.SequenceDelimitationItem.Element); _writer.Write((uint)0x00000000); } } else if (item is DicomFragmentSequence) { DicomFragmentSequence fs = item as DicomFragmentSequence; if (_syntax.ExplicitVr) { _writer.Write((ushort)0x0000); } _writer.Write((uint)UndefinedLength); _writer.Write((ushort)DicomTag.Item.Group); _writer.Write((ushort)DicomTag.Item.Element); if (Flags.IsSet(options, DicomWriteOptions.WriteFragmentOffsetTable) && fs.HasOffsetTable) { _writer.Write((uint)fs.OffsetTableBuffer.Length); fs.OffsetTableBuffer.CopyTo(_writer); } else { _writer.Write((uint)0x00000000); } foreach (DicomFragment bb in fs.Fragments) { _writer.Write((ushort)DicomTag.Item.Group); _writer.Write((ushort)DicomTag.Item.Element); _writer.Write((uint)bb.Length); bb.GetByteBuffer(_syntax).CopyTo(_writer); } _writer.Write((ushort)DicomTag.SequenceDelimitationItem.Group); _writer.Write((ushort)DicomTag.SequenceDelimitationItem.Element); _writer.Write((uint)0x00000000); } else { DicomAttribute de = item; ByteBuffer theData = de.GetByteBuffer(_syntax, dataset.SpecificCharacterSet); if (_syntax.ExplicitVr) { if (de.Tag.VR.Is16BitLengthField) { _writer.Write((ushort)theData.Length); } else { _writer.Write((ushort)0x0000); _writer.Write((uint)theData.Length); } } else { _writer.Write((uint)theData.Length); } if (theData.Length > 0) { theData.CopyTo(_writer); } } } return(DicomWriteStatus.Success); }
public DicomWriteStatus Write(TransferSyntax syntax, DicomAttributeCollection dataset, DicomWriteOptions options) { TransferSyntax = syntax; foreach (DicomAttribute item in dataset) { if (item.Tag.Element == 0x0000) { continue; } if (item.IsEmpty) { 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.ExplicitVr) { _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.ExplicitVr) { _writer.Write((byte)item.Tag.VR.Name[0]); _writer.Write((byte)item.Tag.VR.Name[1]); } if (item is DicomAttributeSQ) { DicomAttributeSQ sq = item as DicomAttributeSQ; if (_syntax.ExplicitVr) { _writer.Write((ushort)0x0000); } if (Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence)) { int hl = _syntax.ExplicitVr ? 12 : 8; _writer.Write((uint)sq.CalculateWriteLength(_syntax, options & ~DicomWriteOptions.CalculateGroupLengths) - (uint)hl); } else { _writer.Write((uint)UndefinedLength); } foreach (DicomSequenceItem ids in item.Values as DicomSequenceItem[]) { _writer.Write((ushort)DicomTag.Item.Group); _writer.Write((ushort)DicomTag.Item.Element); if (Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem)) { _writer.Write((uint)ids.CalculateWriteLength(_syntax, options & ~DicomWriteOptions.CalculateGroupLengths)); } else { _writer.Write((uint)UndefinedLength); } Write(this.TransferSyntax, ids, options & ~DicomWriteOptions.CalculateGroupLengths); if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem)) { _writer.Write((ushort)DicomTag.ItemDelimitationItem.Group); _writer.Write((ushort)DicomTag.ItemDelimitationItem.Element); _writer.Write((uint)0x00000000); } } if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence)) { _writer.Write((ushort)DicomTag.SequenceDelimitationItem.Group); _writer.Write((ushort)DicomTag.SequenceDelimitationItem.Element); _writer.Write((uint)0x00000000); } } else if (item is DicomFragmentSequence) { DicomFragmentSequence fs = item as DicomFragmentSequence; if (_syntax.ExplicitVr) { _writer.Write((ushort)0x0000); } _writer.Write((uint)UndefinedLength); _writer.Write((ushort)DicomTag.Item.Group); _writer.Write((ushort)DicomTag.Item.Element); if (Flags.IsSet(options, DicomWriteOptions.WriteFragmentOffsetTable) && fs.HasOffsetTable) { _writer.Write((uint)fs.OffsetTableBuffer.Length); fs.OffsetTableBuffer.CopyTo(_writer); } else { _writer.Write((uint)0x00000000); } foreach (DicomFragment bb in fs.Fragments) { _writer.Write((ushort)DicomTag.Item.Group); _writer.Write((ushort)DicomTag.Item.Element); _writer.Write((uint)bb.Length); bb.GetByteBuffer(_syntax).CopyTo(_writer); } _writer.Write((ushort)DicomTag.SequenceDelimitationItem.Group); _writer.Write((ushort)DicomTag.SequenceDelimitationItem.Element); _writer.Write((uint)0x00000000); } else { DicomAttribute de = item; ByteBuffer theData = de.GetByteBuffer(_syntax, dataset.SpecificCharacterSet); if (_syntax.ExplicitVr) { if (de.Tag.VR.Is16BitLengthField) { // #10890: Can't encode the value length if the length of the data exceeds max value for a 16-bit field if (theData.Length > ushort.MaxValue - 1 /* must be even length so max allowed = 65534 */) { throw new DicomDataException(string.Format( "Value for {0} exceeds maximum stream length allowed for a {1} VR attribute encoded using {2}", de.Tag, de.Tag.VR, _syntax)); } _writer.Write((ushort)theData.Length); } else { _writer.Write((ushort)0x0000); _writer.Write((uint)theData.Length); } } else { _writer.Write((uint)theData.Length); } if (theData.Length > 0) { theData.CopyTo(_writer); } } } return(DicomWriteStatus.Success); }