Exemplo n.º 1
0
        public override DcmItem Clone()
        {
            DcmItemSequence sq = new DcmItemSequence(Tag, StreamPosition, StreamLength, Endian);

            foreach (DcmItemSequenceItem si in SequenceItems)
            {
                sq.AddSequenceItem((DcmItemSequenceItem)si.Clone());
            }
            return(sq);
        }
Exemplo n.º 2
0
        private static void LoadSequence(XElement parent, IList <DcmItem> items, XDicomOptions options)
        {
            foreach (DcmItem item in items)
            {
                if (!Flags.IsSet(options, XDicomOptions.IncludePixelData) && item.Tag == DicomTags.PixelData)
                {
                    continue;
                }

                XElement attr = new XElement("attr");

                attr.SetAttributeValue("tag", item.Tag.Card.ToString("x8"));
                attr.SetAttributeValue("vr", item.VR.VR);

                if (item is DcmItemSequence)
                {
                    DcmItemSequence seq = (DcmItemSequence)item;
                    attr.SetAttributeValue("len", -1);

                    foreach (DcmItemSequenceItem si in seq.SequenceItems)
                    {
                        XElement itm = new XElement("item");
                        LoadSequence(itm, si.Dataset.Elements, options);
                        attr.Add(itm);
                    }
                }
                else if (item is DcmFragmentSequence)
                {
                    DcmFragmentSequence seq = (DcmFragmentSequence)item;
                    attr.SetAttributeValue("len", -1);

                    LoadFragmentOffsetTable(attr, seq);
                    foreach (ByteBuffer fi in seq.Fragments)
                    {
                        LoadFragmentItem(attr, seq.VR, fi);
                    }
                }
                else
                {
                    DcmElement element = (DcmElement)item;
                    attr.SetAttributeValue("len", element.Length);
                    attr.Add(element.GetValueString());
                }

                if (Flags.IsSet(options, XDicomOptions.Comments))
                {
                    parent.Add(new XComment(item.Tag.Entry.Name));
                }
                parent.Add(attr);
            }
        }
Exemplo n.º 3
0
        private static void Save(XElement parent, DcmDataset dataset)
        {
            foreach (XElement attr in parent.Elements("attr"))
            {
                DicomTag tag = DicomTag.Parse(attr.Attribute("tag").Value);
                DicomVR  vr  = DicomVR.Lookup(attr.Attribute("vr").Value);
                int      len = int.Parse(attr.Attribute("len").Value, CultureInfo.InvariantCulture);

                if (vr == DicomVR.SQ)
                {
                    DcmItemSequence seq = new DcmItemSequence(tag);
                    foreach (XElement itm in attr.Elements("item"))
                    {
                        DcmItemSequenceItem item = new DcmItemSequenceItem();
                        Save(itm, item.Dataset);
                        seq.AddSequenceItem(item);
                    }
                    dataset.AddItem(seq);
                }
                else if (len == -1)
                {
                    DcmFragmentSequence seq = new DcmFragmentSequence(tag, vr);
                    bool first = true;
                    foreach (XElement itm in attr.Elements("item"))
                    {
                        if (first)
                        {
                            SaveFragmentOffsetTable(itm, seq);
                            first = false;
                        }
                        else
                        {
                            SaveFragmentItem(itm, seq);
                        }
                    }
                    dataset.AddItem(seq);
                }
                else
                {
                    DcmElement element = DcmElement.Create(tag, vr);
                    element.SetValueString(attr.FirstText());
                    dataset.AddItem(element);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Original Attributes Sequence (0400,0561)
        /// Sequence of Items containing all attributes that were
        /// removed or replaced by other values in the main dataset.
        /// One or more Items may be permitted in this sequence.
        /// </summary>
        /// <param name="originalAttributesSource">
        /// Source of Previous Values (0400,0564)
        /// The source that provided the SOP Instance prior to the
        /// removal or replacement of the  values. For example, this
        /// might be the Institution from which imported SOP Instances
        /// were received.
        /// </param>
        /// <param name="modifyingSystem">
        /// Modifying System (0400,0563)
        /// Identification of the system which removed and/or replaced
        /// the attributes.
        /// </param>
        /// <param name="reasonForModification">
        /// Reason for the Attribute Modification (0400,0565)
        /// Reason for the attribute modification. Defined terms are:
        /// COERCE = Replace values of attributes such as Patient
        ///     Name, ID, Accession Number, for example, during import
        ///     of media from an external institution, or reconciliation
        ///     against a master patient index.
        /// CORRECT = Replace incorrect values, such as Patient
        ///     Name or ID, for example, when incorrect worklist item
        ///     was chosen or operator input error.
        /// </param>
        /// <param name="tagsToModify">
        /// Tags from this dataset to be removed or modified.
        /// </param>
        public void CreateOriginalAttributesSequence(string originalAttributesSource, string modifyingSystem, 
            string reasonForModification, IEnumerable<DicomTag> tagsToModify)
        {
            DcmItemSequenceItem item = new DcmItemSequenceItem();
            item.Dataset.AddElementWithValue(DicomTags.SourceOfPreviousValues, originalAttributesSource);
            item.Dataset.AddElementWithValue(DicomTags.AttributeModificationDateTime, DateTime.Now);
            item.Dataset.AddElementWithValue(DicomTags.ModifyingSystem, modifyingSystem);
            item.Dataset.AddElementWithValue(DicomTags.ReasonForTheAttributeModification, reasonForModification);

            DcmItemSequence sq = new DcmItemSequence(DicomTags.ModifiedAttributesSequence);
            item.Dataset.AddItem(sq);

            DcmItemSequenceItem modified = new DcmItemSequenceItem();
            sq.AddSequenceItem(modified);

            foreach (DicomTag tag in tagsToModify) {
                DcmItem modifiedItem = GetItem(tag);
                if (modifiedItem == null)
                    modified.Dataset.AddItem(modifiedItem.Clone());
            }

            DcmItemSequence oasq = GetSQ(DicomTags.OriginalAttributesSequence);
            if (oasq == null) {
                oasq = new DcmItemSequence(DicomTags.OriginalAttributesSequence);
                AddItem(oasq);
            }
            oasq.AddSequenceItem(item);
        }
Exemplo n.º 5
0
        public void AddReferenceSequenceItem(DicomTag tag, DicomUID classUid, DicomUID instUid)
        {
            DcmItemSequence sq = GetSQ(tag);

            if (sq == null) {
                sq = new DcmItemSequence(tag);
                AddItem(sq);
            }

            DcmItemSequenceItem item = new DcmItemSequenceItem();
            item.Dataset.AddElementWithValue(DicomTags.ReferencedSOPClassUID, classUid);
            item.Dataset.AddElementWithValue(DicomTags.ReferencedSOPInstanceUID, instUid);
            sq.AddSequenceItem(item);
        }
Exemplo n.º 6
0
        protected override void OnReceiveNGetRequest(byte presentationID, ushort messageID,
            DicomUID requestedClass, DicomUID requestedInstance, DicomTag[] attributes)
        {
            if (requestedClass == DicomUID.PrinterSOPClass && requestedInstance == DicomUID.PrinterSOPInstance) {
                DcmDataset ds = new DcmDataset(DicomTransferSyntax.ImplicitVRLittleEndian);
                ds.AddElementWithValue(DicomTags.PrinterStatus, "NORMAL");
                ds.AddElementWithValue(DicomTags.PrinterStatus, "NORMAL");
                ds.AddElementWithValue(DicomTags.PrinterName, _config.PrinterName);
                ds.AddElementWithValue(DicomTags.Manufacturer, "N/A");
                ds.AddElementWithValue(DicomTags.ManufacturersModelName, "N/A");
                ds.AddElementWithValue(DicomTags.DeviceSerialNumber, "N/A");
                ds.AddElementWithValue(DicomTags.SoftwareVersions, "N/A");
                ds.SetDateTime(DicomTags.DateOfLastCalibration, DicomTags.TimeOfLastCalibration, DateTime.Now);

                SendNGetResponse(presentationID, messageID, requestedClass, requestedInstance, ds, DcmStatus.Success);
                return;
            }

            if (requestedClass == DicomUID.PrintJobSOPClass) {
                DcmPrintJob job = null;

                foreach (DcmPrintJob pj in _jobs) {
                    if (pj.SOPInstanceUID == requestedInstance) {
                        job = pj;
                        break;
                    }
                }

                if (job == null) {
                    job = new DcmPrintJob(requestedInstance);
                    job.ExecutionStatus = "DONE";
                    job.CreationDateTime = DateTime.Today;
                    job.PrintPriority = _session.PrintPriority;
                    job.PrinterName = _config.PrinterName;
                    job.Originator = Associate.CallingAE;
                }

                SendNGetResponse(presentationID, messageID, requestedClass, requestedInstance, job.Dataset, DcmStatus.Success);
                return;
            }

            if (requestedClass == DicomUID.PrinterConfigurationRetrievalSOPClass && requestedInstance == DicomUID.PrinterConfigurationRetrievalSOPInstance) {
                DcmDataset ds = new DcmDataset(DicomTransferSyntax.ImplicitVRLittleEndian);
                DcmDataset config = new DcmDataset(DicomTransferSyntax.ImplicitVRLittleEndian);

                DcmItemSequence sq = new DcmItemSequence(DicomTags.PrinterConfigurationSequence);
                sq.AddSequenceItem(config);
                ds.AddItem(sq);

                SendNGetResponse(presentationID, messageID, requestedClass, requestedInstance, ds, DcmStatus.Success);
                return;
            }

            SendAbort(DcmAbortSource.ServiceProvider, DcmAbortReason.NotSpecified);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Read dataset from stream
        /// </summary>
        /// <param name="stopAtTag">End parsing at this tag</param>
        /// <param name="options">DICOM read options</param>
        /// <returns>Status code</returns>
        public DicomReadStatus Read(DicomTag stopAtTag, DicomReadOptions options)
        {
            // Counters:
            //  _remain - bytes remaining in stream
            //  _bytes - estimates bytes to end of dataset
            //  _read - number of bytes read from stream
            try {
                _need = 0;
                _remain = _stream.Length - _stream.Position;

                while (_remain > 0) {
                    DicomReadStatus status = ParseTag(stopAtTag, options);
                    if (status == DicomReadStatus.SuccessEndRead)
                        return DicomReadStatus.Success;
                    if (status != DicomReadStatus.Success)
                        return status;

                    status = ParseVR(options);
                    if (status != DicomReadStatus.Success)
                        return status;

                    status = ParseLength(options);
                    if (status != DicomReadStatus.Success)
                        return status;

                    if (_tag.IsPrivate) {
                        if (_tag.Element != 0x0000 && _tag.Element <= 0x00ff) {
                            // handle UN private creator id
                            if (_vr != DicomVR.LO && Flags.IsSet(options, DicomReadOptions.ForcePrivateCreatorToLO)) {
                                Dicom.Debug.Log.Warn("Converting Private Creator VR from '{0}' to 'LO'", _vr.VR);
                                _vr = DicomVR.LO;
                            }
                        }
                    }

                    if (_vr == DicomVR.UN && _syntax.IsExplicitVR && Flags.IsSet(options, DicomReadOptions.UseDictionaryForExplicitUN)) {
                        _vr = _tag.Entry.DefaultVR;
                    }

                    if (_fragment != null) {
                        status = InsertFragmentItem(options);
                        if (status != DicomReadStatus.Success)
                            return status;
                    }
                    else if (_sqs.Count > 0 &&
                                (_tag == DicomTags.Item ||
                                 _tag == DicomTags.ItemDelimitationItem ||
                                 _tag == DicomTags.SequenceDelimitationItem)) {
                        status = InsertSequenceItem(options);
                        if (status != DicomReadStatus.Success)
                            return status;
                    }
                    else {
                        if (_sqs.Count > 0) {
                            DcmItemSequence sq = _sqs.Peek();
                            if (sq.StreamLength != UndefinedLength) {
                                long end = sq.StreamPosition + 8 + sq.StreamLength;
                                if (_syntax.IsExplicitVR)
                                    end += 2 + 2;
                                if ((_stream.Position - _offset) >= end) {
                                    if (_sds.Count == _sqs.Count)
                                        _sds.Pop();
                                    _sqs.Pop();
                                }
                            }
                        }

                        if (_len == UndefinedLength) {
                            if (_vr == DicomVR.SQ) {
                                DcmItemSequence sq = new DcmItemSequence(_tag, _pos, _len, _endian);
                                InsertDatasetItem(sq, options);
                                _sqs.Push(sq);
                            }
                            else {
                                _fragment = new DcmFragmentSequence(_tag, _vr, _pos, _endian);
                                InsertDatasetItem(_fragment, options);
                            }
                        }
                        else {
                            if (_vr == DicomVR.SQ) {
                                DcmItemSequence sq = new DcmItemSequence(_tag, _pos, _len, _endian);
                                InsertDatasetItem(sq, options);
                                _sqs.Push(sq);
                            }
                            else {
                                if (_len > _remain)
                                    return NeedMoreData(_len);

                                DcmElement elem = DcmElement.Create(_tag, _vr, _pos, _endian, CurrentBuffer(options));
                                _remain -= _len;
                                _read += _len;

                                InsertDatasetItem(elem, options);
                            }
                        }
                    }

                    _tag = null;
                    _vr = null;
                    _len = UndefinedLength;
                }

                return DicomReadStatus.Success;
            }
            catch (EndOfStreamException) {
                // should never happen
                return DicomReadStatus.UnknownError;
            }
        }
Exemplo n.º 8
0
        private void UpdateImageBox(DcmImageBox imageBox, String filename, int index)
        {
            try
            {
                DicomFileFormat ff = new DicomFileFormat();
                ff.Load(filename, DicomReadOptions.DefaultWithoutDeferredLoading);
                if (ff.Dataset != null)
                {
                    ff.Dataset.ChangeTransferSyntax(DicomTransferSyntax.ImplicitVRLittleEndian, null);

                    DcmPixelData pixelData = new DcmPixelData(ff.Dataset);
                    PhotometricInterpretation pi = PhotometricInterpretation.Lookup(pixelData.PhotometricInterpretation);

                    // Grayscale only printer?
                    if (pi.IsColor == true && _supportsColorPrinting == false)
                    {
                        pixelData.Unload();
                        return;
                    }

                    // Color only printer?
                    if (pi.IsColor == false && _supportsGrayscalePrinting == false)
                    {
                        pixelData.Unload();
                        return;
                    }

                    DicomUID imageBoxSOPClassUID = null;
                    DcmItemSequence seq = null;
                    DcmItemSequenceItem item = new DcmItemSequenceItem();
                    pixelData.UpdateDataset(item.Dataset);

                    if (pi.IsColor == true)
                    {
                        imageBoxSOPClassUID = DicomUID.BasicColorImageBoxSOPClass;
                        seq = new DcmItemSequence(DicomTags.BasicColorImageSequence);
                    }
                    else
                    {
                        imageBoxSOPClassUID = DicomUID.BasicGrayscaleImageBoxSOPClass;
                        seq = new DcmItemSequence(DicomTags.BasicGrayscaleImageSequence);
                    }
                    seq.AddSequenceItem(item);
                    imageBox.Dataset.AddItem(seq);

                    pixelData.Unload();

                    imageBox.UpdateImageBox(imageBoxSOPClassUID);
                    imageBox.ImageBoxPosition = (ushort)index;
                }
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 9
0
Arquivo: XDicom.cs Projeto: GMZ/mdcm
        private static void Save(XElement parent, DcmDataset dataset)
        {
            foreach (XElement attr in parent.Elements("attr"))
            {
                DicomTag tag = DicomTag.Parse(attr.Attribute("tag").Value);
                DicomVR vr = DicomVR.Lookup(attr.Attribute("vr").Value);
                int len = int.Parse(attr.Attribute("len").Value, CultureInfo.InvariantCulture);

                if (vr == DicomVR.SQ)
                {
                    DcmItemSequence seq = new DcmItemSequence(tag);
                    foreach (XElement itm in attr.Elements("item"))
                    {
                        DcmItemSequenceItem item = new DcmItemSequenceItem();
                        Save(itm, item.Dataset);
                        seq.AddSequenceItem(item);
                    }
                    dataset.AddItem(seq);
                }
                else if (len == -1)
                {
                    DcmFragmentSequence seq = new DcmFragmentSequence(tag, vr);
                    bool first = true;
                    foreach (XElement itm in attr.Elements("item"))
                    {
                        if (first)
                        {
                            SaveFragmentOffsetTable(itm, seq);
                            first = false;
                        }
                        else
                        {
                            SaveFragmentItem(itm, seq);
                        }
                    }
                    dataset.AddItem(seq);
                }
                else
                {
                    DcmElement element = DcmElement.Create(tag, vr);
                    element.SetValueString(attr.FirstText());
                    dataset.AddItem(element);
                }
            }
        }
Exemplo n.º 10
0
		public override DcmItem Clone() {
			DcmItemSequence sq = new DcmItemSequence(Tag, StreamPosition, StreamLength, Endian);
			foreach (DcmItemSequenceItem si in SequenceItems) {
				sq.AddSequenceItem((DcmItemSequenceItem)si.Clone());
			}
			return sq;
		}