Exemplo n.º 1
0
        /// <summary>
        /// Method to call before performing the actual saving.
        /// </summary>
        protected override void OnSave()
        {
            if (RootDirectoryRecord == null)
            {
                throw new InvalidOperationException("No DICOM files added, cannot save DICOM directory");
            }

            _directoryRecordSequence.Items.Clear();
            var calculator = new DicomWriteLengthCalculator(FileMetaInfo.TransferSyntax, DicomWriteOptions.Default);

            // ensure write length calculator does not include end of sequence item
            //Dataset.Remove(DicomTag.DirectoryRecordSequence);

            //_fileOffset = 128 + calculator.Calculate(FileMetaInfo) + calculator.Calculate(Dataset);

            //Add the offset for the Directory Record sequence tag itself
            //_fileOffset += 4;//sequence element tag
            if (FileMetaInfo.TransferSyntax.IsExplicitVR)
            {
                _fileOffset  = 128 + calculator.Calculate(FileMetaInfo) + calculator.Calculate(Dataset);
                _fileOffset += 2; // vr
                _fileOffset += 2; // padding
                _fileOffset += 4; // length
            }
            else
            {
                _fileOffset = 128 + 4 + calculator.Calculate(FileMetaInfo) + calculator.Calculate(Dataset);

                _fileOffset += 4; //sequence element tag
                _fileOffset += 4; //length
            }

            AddDirectoryRecordsToSequenceItem(RootDirectoryRecord);

            if (RootDirectoryRecord != null)
            {
                CalculateOffsets(calculator);

                SetOffsets(RootDirectoryRecord);

                Dataset.Add <uint>(
                    DicomTag.OffsetOfTheFirstDirectoryRecordOfTheRootDirectoryEntity,
                    RootDirectoryRecord.Offset);

                var lastRoot = RootDirectoryRecord;

                while (lastRoot.NextDirectoryRecord != null)
                {
                    lastRoot = lastRoot.NextDirectoryRecord;
                }

                Dataset.Add <uint>(DicomTag.OffsetOfTheLastDirectoryRecordOfTheRootDirectoryEntity, lastRoot.Offset);
            }
            else
            {
                Dataset.Add <uint>(DicomTag.OffsetOfTheFirstDirectoryRecordOfTheRootDirectoryEntity, 0);
                Dataset.Add <uint>(DicomTag.OffsetOfTheLastDirectoryRecordOfTheRootDirectoryEntity, 0);
            }
        }
Exemplo n.º 2
0
        private void CalculateOffsets(DicomWriteLengthCalculator calculator)
        {
            foreach (var item in Dataset.Get <DicomSequence>(DicomTag.DirectoryRecordSequence))
            {
                var record = item as DicomDirectoryRecord;
                if (record == null)
                {
                    throw new InvalidOperationException("Unexpected type for directory record: " + item.GetType());
                }

                record.Offset = _fileOffset;

                _fileOffset += 4 + 4;                //Sequence item tag;

                _fileOffset += calculator.Calculate(record);

                _fileOffset += 4 + 4;                 // Sequence Item Delimitation Item
            }

            _fileOffset += 4 + 4;             // Sequence Delimitation Item
        }