public DicomWriteOptions(DicomWriteOptions options) { ExplicitLengthSequences = options.ExplicitLengthSequences; ExplicitLengthSequenceItems = options.ExplicitLengthSequenceItems; KeepGroupLengths = options.KeepGroupLengths; LargeObjectSize = options.LargeObjectSize; }
public void WriteOptionsTest(DicomFile sourceFile, DicomWriteOptions options) { bool result = sourceFile.Save(options); Assert.AreEqual(result, true); DicomFile newFile = new DicomFile(sourceFile.Filename); DicomReadOptions readOptions = DicomReadOptions.Default; newFile.Load(readOptions); Assert.AreEqual(sourceFile.DataSet.Equals(newFile.DataSet), true); // update a tag, and make sure it shows they're different newFile.DataSet[DicomTags.PatientsName].Values = "NewPatient^First"; Assert.AreEqual(sourceFile.DataSet.Equals(newFile.DataSet), false); // Now update the original file with the name, and they should be the same again sourceFile.DataSet[DicomTags.PatientsName].Values = "NewPatient^First"; Assert.AreEqual(sourceFile.DataSet.Equals(newFile.DataSet), true); // Return the original string value. sourceFile.DataSet[DicomTags.PatientsName].SetStringValue("Patient^Test"); }
internal override uint CalculateWriteLength(DicomTransferSyntax syntax, DicomWriteOptions options) { uint length = 4 + 4; length += Dataset.CalculateWriteLength(syntax, options & ~DicomWriteOptions.CalculateGroupLengths); if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem)) length += 4 + 4; // Sequence Item Delimitation Item return length; }
public void CreateFileTest() { DicomFile file = new DicomFile("CreateFileTest.dcm"); DicomAttributeCollection dataSet = file.DataSet; SetupMR(dataSet); string studyId = file.DataSet[DicomTags.StudyId].GetString(0, ""); Assert.AreEqual(studyId, "1933"); SetupMetaInfo(file); // Little Endian Tests file.TransferSyntax = TransferSyntax.ExplicitVrLittleEndian; DicomWriteOptions writeOptions = DicomWriteOptions.Default; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequence; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequenceItem; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequence | DicomWriteOptions.ExplicitLengthSequenceItem; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.None; WriteOptionsTest(file, writeOptions); // Big Endian Tests file.Filename = "BigEndianCreateFileTest.dcm"; file.TransferSyntax = TransferSyntax.ExplicitVrBigEndian; writeOptions = DicomWriteOptions.Default; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequence; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequenceItem; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequence | DicomWriteOptions.ExplicitLengthSequenceItem; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.None; WriteOptionsTest(file, writeOptions); }
/// <summary> /// Save to file asynchronously. /// </summary> /// <param name="fileName">Name of file.</param> /// <param name="options">Options to apply during writing.</param> /// <returns>Awaitable <see cref="Task"/>.</returns> public async Task SaveAsync(string fileName, DicomWriteOptions options = null) { PreprocessFileMetaInformation(); File = Setup.ServiceProvider.GetService <IFileReferenceFactory>().Create(fileName); File.Delete(); OnSave(); using var target = new FileByteTarget(File); var writer = new DicomFileWriter(options); await writer.WriteAsync(target, FileMetaInfo, Dataset).ConfigureAwait(false); }
/// <summary> /// Save DICOM file. /// </summary> /// <param name="fileName">File name.</param> /// <param name="options">Options to apply during writing.</param> public void Save(string fileName, DicomWriteOptions options = null) { PreprocessFileMetaInformation(); File = Setup.ServiceProvider.GetService <IFileReferenceFactory>().Create(fileName); File.Delete(); OnSave(); using var target = new FileByteTarget(File); var writer = new DicomFileWriter(options); writer.Write(target, FileMetaInfo, Dataset); }
/// <summary> /// Save to file asynchronously. /// </summary> /// <param name="fileName">Name of file.</param> /// <param name="options">Options to apply during writing.</param> /// <returns>Awaitable <see cref="Task"/>.</returns> public async Task SaveAsync(string fileName, DicomWriteOptions options = null) { this.PreprocessFileMetaInformation(); this.File = IOManager.CreateFileReference(fileName); this.File.Delete(); this.OnSave(); using (var target = new FileByteTarget(this.File)) { var writer = new DicomFileWriter(options); await writer.WriteAsync(target, this.FileMetaInfo, this.Dataset).ConfigureAwait(false); } }
/// <summary> /// Save DICOM file. /// </summary> /// <param name="fileName">File name.</param> /// <param name="options">Options to apply during writing.</param> public void Save(string fileName, DicomWriteOptions options = null) { this.PreprocessFileMetaInformation(); this.File = IOManager.CreateFileReference(fileName); this.File.Delete(); this.OnSave(); using (var target = new FileByteTarget(this.File)) { var writer = new DicomFileWriter(options); writer.Write(target, this.FileMetaInfo, this.Dataset); } }
public void MultiframeCreateFileTest() { DicomFile file = new DicomFile("MultiframeCreateFileTest.dcm"); DicomAttributeCollection dataSet = file.DataSet; SetupMultiframeXA(dataSet, 511, 511, 5); SetupMetaInfo(file); // Little Endian Tests file.TransferSyntax = TransferSyntax.ExplicitVrLittleEndian; DicomWriteOptions writeOptions = DicomWriteOptions.Default; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequence; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequenceItem; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequence | DicomWriteOptions.ExplicitLengthSequenceItem; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.None; WriteOptionsTest(file, writeOptions); // Big Endian Tests file.Filename = "MultiframeBigEndianCreateFileTest.dcm"; file.TransferSyntax = TransferSyntax.ExplicitVrBigEndian; writeOptions = DicomWriteOptions.Default; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequence; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequenceItem; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.ExplicitLengthSequence | DicomWriteOptions.ExplicitLengthSequenceItem; WriteOptionsTest(file, writeOptions); writeOptions = DicomWriteOptions.None; WriteOptionsTest(file, writeOptions); }
/// <summary> /// Save the file as a DICOM Part 10 format file. /// </summary> /// <param name="options">The options to use when saving the file.</param> /// <param name="file">The path of the file to save to.</param> /// <returns></returns> public bool Save(string file, DicomWriteOptions options) { if (file == null) { throw new ArgumentNullException("file"); } Filename = file; using (FileStream fs = File.Create(Filename)) { bool b = Save(fs, options); fs.Flush(); fs.Close(); return(b); } }
public override uint CalculateWriteLength(DicomTransferSyntax syntax, DicomWriteOptions options) { uint length = 0; length += 4; // element tag if (syntax.IsExplicitVR) { length += 2; // vr length += 6; // length } else { length += 4; // length } foreach (DcmItemSequenceItem item in SequenceItems) { length += item.CalculateWriteLength(syntax, options); } if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence)) length += 4 + 4; // Sequence Delimitation Item return length; }
public void Save_PixelDataWrittenInManyChunks_EqualsWhenPixelDataWrittenInOneChunk() { var file = DicomFile.Open(@"Test Data\CT-MONO2-16-ankle"); using (var stream1 = new MemoryStream()) using (var stream2 = new MemoryStream()) { var options1 = new DicomWriteOptions { LargeObjectSize = 1024 }; file.Save(stream1, options1); var options2 = new DicomWriteOptions { LargeObjectSize = 16 * 1024 * 1024 }; file.Save(stream2, options2); Assert.Equal(stream1.ToArray(), stream2.ToArray()); } }
/// <summary> /// Save the file as a DICOM Part 10 format file (as set by the <see cref="Filename"/> property). /// </summary> /// <param name="options">The options to use when saving the file.</param> /// <returns></returns> public bool Save(DicomWriteOptions options) { string path = Path.GetDirectoryName(Filename); if (!string.IsNullOrEmpty(path)) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } using (FileStream fs = File.Create(Filename)) { bool b = Save(fs, options); fs.Flush(); fs.Close(); return(b); } }
public async Task SaveAsync_PixelDataWrittenInManyChunks_EqualsWhenPixelDataWrittenInOneChunk() { var file = DicomFile.Open(@"Test Data\CT-MONO2-16-ankle"); using (var stream1 = new MemoryStream()) using (var stream2 = new MemoryStream()) { var options1 = new DicomWriteOptions { LargeObjectSize = 1024 }; await file.SaveAsync(stream1, options1).ConfigureAwait(false); var options2 = new DicomWriteOptions { LargeObjectSize = 16 * 1024 * 1024 }; await file.SaveAsync(stream2, options2).ConfigureAwait(false); Assert.Equal(stream1.ToArray(), stream2.ToArray()); } }
/// <summary> /// Saves a DICOM file to a MemoryStream /// </summary> /// <param name="options">DICOM write options.</param> /// <returns></returns> public MemoryStream Save(DicomWriteOptions options) { MemoryStream ms = new MemoryStream(); ms.Seek(128, SeekOrigin.Begin); ms.WriteByte((byte)'D'); ms.WriteByte((byte)'I'); ms.WriteByte((byte)'C'); ms.WriteByte((byte)'M'); DicomStreamWriter dsw = new DicomStreamWriter(ms); dsw.Write(_metainfo, options | DicomWriteOptions.CalculateGroupLengths); if (_dataset != null) { dsw.Write(_dataset, options); } return(ms); }
public async Task SaveAsyncToFile_PixelDataWrittenInManyChunks_EqualsWhenPixelDataWrittenInOneChunk() { var file = DicomFile.Open(@"Test Data\CT-MONO2-16-ankle"); var options1 = new DicomWriteOptions { LargeObjectSize = 1024 }; await file.SaveAsync("saveasynctofile1", options1).ConfigureAwait(false); var options2 = new DicomWriteOptions { LargeObjectSize = 16 * 1024 * 1024 }; await file.SaveAsync("saveasynctofile2", options2).ConfigureAwait(false); var bytes1 = File.ReadAllBytes("saveasynctofile1"); var bytes2 = File.ReadAllBytes("saveasynctofile2"); Assert.Equal(bytes1, bytes2); }
/// <summary> /// Calculate the length to write the attribute. /// </summary> /// <param name="syntax">The transfer syntax to calculate the length for.</param> /// <param name="options">The write options to calculate the length for.</param> /// <returns></returns> internal virtual uint CalculateWriteLength(TransferSyntax syntax, DicomWriteOptions options) { uint length = 4; // element tag if (syntax.ExplicitVr) { length += 2; // vr if (Tag.VR.Is16BitLengthField) length += 2; else length += 6; } else { length += 4; // length tag } length += StreamLength; if ((length & 0x00000001) != 0) length++; return length; }
internal override uint CalculateWriteLength(DicomTransferSyntax syntax, DicomWriteOptions options) { uint length = 0; length += 4; // element tag if (syntax.IsExplicitVR) { length += 2; // vr length += 6; // length } else { length += 4; // length } foreach (DcmItemSequenceItem item in SequenceItems) { length += item.CalculateWriteLength(syntax, options); } if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence)) { length += 4 + 4; // Sequence Delimitation Item } return(length); }
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 uint CalculateGroupWriteLength(ushort group, DicomTransferSyntax syntax, DicomWriteOptions options) { uint length = 0; foreach (DcmItem item in _items.Values) { if (item.Tag.Group < group || item.Tag.Element == 0x0000) continue; if (item.Tag.Group > group) return length; length += item.CalculateWriteLength(syntax, options); } return length; }
public uint CalculateWriteLength(DicomTransferSyntax syntax, DicomWriteOptions options) { uint length = 0; ushort group = 0xffff; foreach (DcmItem item in _items.Values) { if (item.Tag.Element == 0x0000) continue; if (item.Tag.Group != group) { group = item.Tag.Group; if (Flags.IsSet(options, DicomWriteOptions.CalculateGroupLengths)) { if (syntax.IsExplicitVR) length += 4 + 2 + 2 + 4; else length += 4 + 4 + 4; } } length += item.CalculateWriteLength(syntax, options); } return length; }
internal override uint CalculateWriteLength(TransferSyntax syntax, DicomWriteOptions options) { uint length = 0; length += 4; // element tag if (syntax.ExplicitVr) { length += 2; // vr length += 6; // length } else { length += 4; // length } // write the offset table (item tag is always present, though content may be zero length) length += 4 + 4; // item tag and length if (Flags.IsSet(options, DicomWriteOptions.WriteFragmentOffsetTable) && _table != null) length += (uint) (_table.Count*4); foreach (DicomFragment fragment in this._fragments) { length += 4; // item tag length += 4; // fragment length length += fragment.Length; // no Item Delimitation Item (fragments are always explicit length) } length += 4 + 4; // Sequence Delimitation Item (fragment sequences are always undefined length) return length; }
public static bool IsSet(DicomWriteOptions options, DicomWriteOptions flag) { return (options & flag) == flag; }
/// <summary> /// Save the file as a DICOM Part 10 format file (as set by the <see cref="Filename"/> property). /// </summary> /// <param name="options">The options to use when saving the file.</param> /// <returns></returns> public bool Save(DicomWriteOptions options) { string path = Path.GetDirectoryName(Filename); if (!string.IsNullOrEmpty(path)) { if (!Directory.Exists(path)) Directory.CreateDirectory(path); } using (FileStream fs = File.Create(Filename)) { bool b = Save(fs, options); fs.Flush(); fs.Close(); return b; } }
/// <summary> /// Save the file as a DICOM Part 10 format file. /// </summary> /// <param name="options">The options to use when saving the file.</param> /// <param name="iStream">The <see cref="Stream"/> to Save the DICOM file to.</param> /// <returns></returns> public bool Save(Stream iStream, DicomWriteOptions options) { if (iStream == null) throw new ArgumentNullException("iStream"); // Original code has seek() here, but there may be use cases where // a user wants to add the file into a stream (that may contain other data) // and the seek would cause the method to not support that. byte[] prefix = new byte[128]; iStream.Write(prefix,0,128); iStream.WriteByte((byte) 'D'); iStream.WriteByte((byte) 'I'); iStream.WriteByte((byte) 'C'); iStream.WriteByte((byte) 'M'); DicomStreamWriter dsw = new DicomStreamWriter(iStream); dsw.Write(TransferSyntax.ExplicitVrLittleEndian, MetaInfo, options | DicomWriteOptions.CalculateGroupLengths); MetaInfoFileLength = iStream.Position; dsw.Write(TransferSyntax, DataSet, options); iStream.Flush(); return true; }
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; }
/// <summary> /// Saves a DICOM file /// </summary> /// <param name="file">Filename</param> /// <param name="options">DICOM write options</param> public void Save(String file, DicomWriteOptions options) { string dir = Path.GetDirectoryName(file); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); using (FileStream fs = File.Create(file)) { fs.Seek(128, SeekOrigin.Begin); fs.WriteByte((byte)'D'); fs.WriteByte((byte)'I'); fs.WriteByte((byte)'C'); fs.WriteByte((byte)'M'); DicomStreamWriter dsw = new DicomStreamWriter(fs); dsw.Write(_metainfo, options | DicomWriteOptions.CalculateGroupLengths); if (_dataset != null) dsw.Write(_dataset, options); fs.Close(); } }
/// <summary> /// Saves a DICOM file in the isolated storage area /// </summary> /// <param name="file">Filename</param> /// <param name="options">DICOM write options</param> /// <param name="useIsoStore">Save in isolated storage</param> public void Save(string file, DicomWriteOptions options, bool useIsoStore = false) { if (useIsoStore) { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { string dir = Path.GetDirectoryName(file); if (dir != null && !store.DirectoryExists(dir)) store.CreateDirectory(dir); var fs = store.CreateFile(file); fs.Seek(128, SeekOrigin.Begin); fs.WriteByte((byte)'D'); fs.WriteByte((byte)'I'); fs.WriteByte((byte)'C'); fs.WriteByte((byte)'M'); DicomStreamWriter dsw = new DicomStreamWriter(fs); dsw.Write(_metainfo, options | DicomWriteOptions.CalculateGroupLengths); if (_dataset != null) dsw.Write(_dataset, options); fs.Close(); } } else { // expand to full path file = Path.GetFullPath(file); string dir = Path.GetDirectoryName(file); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); using (FileStream fs = File.Create(file)) { fs.Seek(128, SeekOrigin.Begin); fs.WriteByte((byte)'D'); fs.WriteByte((byte)'I'); fs.WriteByte((byte)'C'); fs.WriteByte((byte)'M'); DicomStreamWriter dsw = new DicomStreamWriter(fs); dsw.Write(_metainfo, options | DicomWriteOptions.CalculateGroupLengths); if (_dataset != null) dsw.Write(_dataset, options); fs.Close(); } } }
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); }
/// <summary> /// Saves a DICOM file to a MemoryStream /// </summary> /// <param name="options">DICOM write options.</param> /// <returns></returns> public MemoryStream Save(DicomWriteOptions options) { MemoryStream ms = new MemoryStream(); ms.Seek(128, SeekOrigin.Begin); ms.WriteByte((byte)'D'); ms.WriteByte((byte)'I'); ms.WriteByte((byte)'C'); ms.WriteByte((byte)'M'); DicomStreamWriter dsw = new DicomStreamWriter(ms); dsw.Write(_metainfo, options | DicomWriteOptions.CalculateGroupLengths); if (_dataset != null) dsw.Write(_dataset, options); return ms; }
/// <summary> /// Saves a DICOM file /// </summary> /// <param name="file">Filename</param> /// <param name="options">DICOM write options</param> /// <summary> /// Saves a DICOM file to a file /// </summary> /// <param name="file">Filename</param> /// <param name="options">DICOM write options</param> public void Save(String file, DicomWriteOptions options) { // expand to full path file = Path.GetFullPath(file); string dir = Path.GetDirectoryName(file); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); using (FileStream fs = File.Create(file)) { Save(fs, options); fs.Close(); } }
/// <summary> /// Saves a DICOM file to a stream. <br/><i>Note that the caller is expected to dispose of the stream.</i> /// </summary> /// <param name="stream">Stream to place the DICOM file in</param> /// <param name="options">DICOM write options</param> public void Save(Stream stream, DicomWriteOptions options) { stream.Seek(128, SeekOrigin.Begin); stream.WriteByte((byte)'D'); stream.WriteByte((byte)'I'); stream.WriteByte((byte)'C'); stream.WriteByte((byte)'M'); DicomStreamWriter dsw = new DicomStreamWriter(stream); dsw.Write(_metainfo, options | DicomWriteOptions.CalculateGroupLengths); if (_dataset != null) dsw.Write(_dataset, options); }
/// <summary> /// Finds the next directory record of the specified <paramref name="recordType"/>, starting at the specified <paramref name="startIndex"/> /// </summary> /// <param name="recordType">Type of the record.</param> /// <param name="startIndex">The start index.</param> /// <returns></returns> private void CalculateOffsets(TransferSyntax syntax, DicomWriteOptions options) { foreach (DicomSequenceItem sq in (DicomSequenceItem[])_dicomDirFile.DataSet[DicomTags.DirectoryRecordSequence].Values) { DirectoryRecordSequenceItem record = sq as DirectoryRecordSequenceItem; if (record == null) throw new ApplicationException("Unexpected type for directory record: " + sq.GetType()); record.Offset = _fileOffset; _fileOffset += 4 + 4; // Sequence Item Tag _fileOffset += record.CalculateWriteLength(syntax, options & ~DicomWriteOptions.CalculateGroupLengths); if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem)) _fileOffset += 4 + 4; // Sequence Item Delimitation Item } if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence)) _fileOffset += 4 + 4; // Sequence Delimitation Item }
internal override uint CalculateWriteLength(DicomTransferSyntax syntax, DicomWriteOptions options) { uint length = 0; length += 4; // element tag if (syntax.IsExplicitVR) { length += 2; // vr length += 6; // length } else { length += 4; // length } length += 4 + 4; // offset tag if (Flags.IsSet(options, DicomWriteOptions.WriteFragmentOffsetTable) && _table != null) length += (uint)(_table.Count * 4); foreach (ByteBuffer bb in _fragments) { length += 4; // item tag length += 4; // fragment length length += (uint)bb.Length; } return length; }
/// <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; }
public abstract uint CalculateWriteLength(DicomTransferSyntax syntax, DicomWriteOptions options);
/// <summary> /// Save the file as a DICOM Part 10 format file. /// </summary> /// <param name="options">The options to use when saving the file.</param> /// <param name="file">The path of the file to save to.</param> /// <returns></returns> public bool Save(string file, DicomWriteOptions options) { if (file == null) throw new ArgumentNullException("file"); Filename = file; using (FileStream fs = File.Create(Filename)) { bool b = Save(fs, options); fs.Flush(); fs.Close(); return b; } }
internal override uint CalculateWriteLength(TransferSyntax syntax, DicomWriteOptions options) { uint length = 0; length += 4; // element tag if (syntax.ExplicitVr) { length += 2; // vr length += 6; // length } else { length += 4; // length } if (_values != null) { foreach (DicomSequenceItem item in _values) { length += 4 + 4; // Sequence Item Tag length += item.CalculateWriteLength(syntax, options & ~DicomWriteOptions.CalculateGroupLengths); if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem)) length += 4 + 4; // Sequence Item Delimitation Item } if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequence)) length += 4 + 4; // Sequence Delimitation Item } return length; }
public DicomWriteStatus Write(TransferSyntax syntax, DicomAttributeCollection dataset, DicomWriteOptions options) { TransferSyntax = syntax; foreach (DicomAttribute item in dataset) { if (item.IsEmpty) continue; if (item.Tag.Element == 0x0000) continue; if (Flags.IsSet(options, DicomWriteOptions.CalculateGroupLengths) && item.Tag.Group != _group) { _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) { var sq = item as DicomAttributeSQ; if (_syntax.ExplicitVr) _writer.Write((ushort) 0x0000); if (Flags.IsSet(options, DicomWriteOptions.DigitalSignatureMacEncoding)) { // length field is not written when generating digital signature MAC } else 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 (DicomSequenceItem[]) sq.Values) { _writer.Write((ushort) DicomTag.Item.Group); _writer.Write((ushort) DicomTag.Item.Element); if (Flags.IsSet(options, DicomWriteOptions.DigitalSignatureMacEncoding)) { // length field is not written when generating digital signature MAC } else if (Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem)) { _writer.Write((uint) ids.CalculateWriteLength(_syntax, options & ~DicomWriteOptions.CalculateGroupLengths)); } else { _writer.Write((uint) _undefinedLength); } Write(TransferSyntax, ids, options & ~DicomWriteOptions.CalculateGroupLengths); if (!Flags.IsSet(options, DicomWriteOptions.ExplicitLengthSequenceItem) && !Flags.IsSet(options, DicomWriteOptions.DigitalSignatureMacEncoding)) { _writer.Write((ushort) DicomTag.ItemDelimitationItem.Group); _writer.Write((ushort) DicomTag.ItemDelimitationItem.Element); _writer.Write((uint) 0x00000000); } } if (Flags.IsSet(options, DicomWriteOptions.DigitalSignatureMacEncoding)) { _writer.Write((ushort) DicomTag.SequenceDelimitationItem.Group); _writer.Write((ushort) DicomTag.SequenceDelimitationItem.Element); } else 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) { var fs = item as DicomFragmentSequence; if (_syntax.ExplicitVr) _writer.Write((ushort) 0x0000); if (Flags.IsSet(options, DicomWriteOptions.DigitalSignatureMacEncoding)) { // length field is not written when generating digital signature MAC } else { _writer.Write((uint) _undefinedLength); } _writer.Write((ushort) DicomTag.Item.Group); _writer.Write((ushort) DicomTag.Item.Element); if (Flags.IsSet(options, DicomWriteOptions.DigitalSignatureMacEncoding)) { // length field is not written when generating digital signature MAC if (fs.HasOffsetTable) fs.OffsetTableBuffer.CopyTo(_writer); } else 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); if (Flags.IsSet(options, DicomWriteOptions.DigitalSignatureMacEncoding)) { // length field is not written when generating digital signature MAC } else { _writer.Write((uint) bb.Length); } bb.GetByteBuffer(_syntax).CopyTo(_writer); } if (Flags.IsSet(options, DicomWriteOptions.DigitalSignatureMacEncoding)) { _writer.Write((ushort) DicomTag.SequenceDelimitationItem.Group); _writer.Write((ushort) DicomTag.SequenceDelimitationItem.Element); } else { _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; }
private void ReadWriteOptionsTest(DicomFile sourceFile, DicomReadOptions readOptions, DicomWriteOptions options) { var sourceIconImageSq = (DicomAttributeSQ) sourceFile.DataSet[DicomTags.IconImageSequence]; Assert.IsTrue(!sourceIconImageSq.IsEmpty && !sourceIconImageSq.IsNull && sourceIconImageSq.Count == 1, "File doesn't have icon image sequence"); Assert.IsTrue(sourceFile.Save(options), "Failed to write file"); var newFile = new DicomFile(sourceFile.Filename); newFile.Load(readOptions); var resultList = new List<DicomAttributeComparisonResult>(); var result = sourceFile.DataSet.Equals(newFile.DataSet, ref resultList); string resultMessage = resultList.Count > 0 ? resultList.First().ToString() : string.Empty; Assert.IsTrue(result, resultMessage); var newIconImageSq = (DicomAttributeSQ) newFile.DataSet[DicomTags.IconImageSequence]; Assert.IsTrue(!newIconImageSq.IsEmpty && !newIconImageSq.IsNull && newIconImageSq.Count == 1, "New file doesn't have icon image sequence"); // update a tag, and make sure it shows they're different newFile.DataSet[DicomTags.PatientsName].Values = "NewPatient^First"; Assert.IsFalse(sourceFile.DataSet.Equals(newFile.DataSet, ref resultList)); // Now update the original file with the name, and they should be the same again sourceFile.DataSet[DicomTags.PatientsName].Values = "NewPatient^First"; Assert.IsTrue(sourceFile.DataSet.Equals(newFile.DataSet)); // Return the original string value. sourceFile.DataSet[DicomTags.PatientsName].SetStringValue("Patient^Test"); }
/// <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); }
public static bool IsSet(DicomWriteOptions options, DicomWriteOptions flag) { return((options & flag) == flag); }
private void ReadWriteOptionsTest(DicomFile sourceFile, DicomReadOptions readOptions, DicomWriteOptions options) { var sourceIconImageSq = (DicomAttributeSQ)sourceFile.DataSet[DicomTags.IconImageSequence]; Assert.IsTrue(!sourceIconImageSq.IsEmpty && !sourceIconImageSq.IsNull && sourceIconImageSq.Count == 1, "File doesn't have icon image sequence"); Assert.IsTrue(sourceFile.Save(options), "Failed to write file"); var newFile = new DicomFile(sourceFile.Filename); newFile.Load(readOptions); var resultList = new List <DicomAttributeComparisonResult>(); var result = sourceFile.DataSet.Equals(newFile.DataSet, ref resultList); string resultMessage = resultList.Count > 0 ? resultList.First().ToString() : string.Empty; Assert.IsTrue(result, resultMessage); var newIconImageSq = (DicomAttributeSQ)newFile.DataSet[DicomTags.IconImageSequence]; Assert.IsTrue(!newIconImageSq.IsEmpty && !newIconImageSq.IsNull && newIconImageSq.Count == 1, "New file doesn't have icon image sequence"); // update a tag, and make sure it shows they're different newFile.DataSet[DicomTags.PatientsName].Values = "NewPatient^First"; Assert.IsFalse(sourceFile.DataSet.Equals(newFile.DataSet, ref resultList)); // Now update the original file with the name, and they should be the same again sourceFile.DataSet[DicomTags.PatientsName].Values = "NewPatient^First"; Assert.IsTrue(sourceFile.DataSet.Equals(newFile.DataSet)); // Return the original string value. sourceFile.DataSet[DicomTags.PatientsName].SetStringValue("Patient^Test"); }