Example #1
0
        /// <summary>
        /// Returns the DiDataElement with the given ids and removes it from this diFile. Can return null.
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="elementId"></param>
        /// <returns></returns>
        public DiDataElement RemoveElement(uint groupId, uint elementId)
        {
            DiDataElement element = GetElement(groupId, elementId);

            _dataElements.Remove(DiDictonary.ToTag(groupId, elementId));

            return(element);
        }
Example #2
0
        /// <summary>
        /// Fills a QuickInfo objects and skips all information that is not necessary.
        /// </summary>
        /// <returns></returns>
        public QuickInfo Get_quick_info()
        {
            var de = new DiDataElement();
            var qi = new QuickInfo();

            VrFormat  = DiFile.VrExplicit;
            Endianess = DiFile.EndianLittle;

            //        // media stored sop
            //        de = scan_for(0x00020002);
            //        if (de!=null) {
            //            qi._media_stored_sop_class_uid = de.get_value_as_string();
            //        } else {
            //            return null;
            //        }

            // transfer syntax
            de = Scan_for(0x00020010);
            if (de != null)
            {
                var tsUid = de.GetValueAsString();
                VrFormat  = DiDictonary.get_ts_uid_vr_format(tsUid);
                Endianess = DiDictonary.get_ts_uid_endianess(tsUid);
                if (VrFormat == DiFile.EndianUnknown)
                {
                    Debug.Log("DiFileInputStream::get_quick_info Warning: Unknown Transfer Syntax UID \"" + tsUid + "\". Endianess & VR format will be guessed.");
                }
            }

            // image type
            de = Scan_for(0x00080008);
            if (de != null)
            {
                var imageType = de.GetValueAsString();
                var split     = imageType.Split(new [] { "\\\\" }, StringSplitOptions.None);
                if (split.Length > 2 && split[2].Equals("SCOUT"))
                {
                    qi.Scout = true;
                }
                else
                {
                    qi.Scout = false;
                }
            }

            // series uid
            de = Scan_for(0x0020000e);
            if (de != null)
            {
                qi.SeriesUid = de.GetValueAsString();
            }
            else
            {
                return(null);
            }

            // image number
            de             = Scan_for(0x00200013);
            qi.ImageNumber = de?.GetInt() ?? 0;

            return(qi);
        }
Example #3
0
        /// <summary>
        /// Reads the next DiDataElement from a (dicom) input stream.
        /// </summary>
        /// <param name="inputStream">is a DiInputStream - must be open and readable</param>
        public void ReadNext(DiFileStream inputStream)
        {
            bool exp;
            int  vrFormat;

            uint b0 = (uint)inputStream.ReadByte();
            uint b1 = (uint)inputStream.ReadByte();

            _groupId = (b1 << 8) | b0;

            // --- meta group part start ----------------------------

            if (inputStream.BeforeMetaGroup && _groupId == 0x0002)
            {
                // we just entered the meta group
                inputStream.BeforeMetaGroup = false;
                inputStream.MetaGroup       = true;
            }

            if (inputStream.MetaGroup && _groupId != 0x0002)
            {
                // we just left the meta group
                inputStream.MetaGroup = false;
            }

            if (inputStream.BeforeMetaGroup || inputStream.MetaGroup)
            {
                // are we still before or inside meta group?
                vrFormat  = DiFile.VrExplicit;
                Endianess = DiFile.EndianLittle;
            }
            else
            {
                vrFormat  = inputStream.VrFormat;
                Endianess = inputStream.Endianess;
            }

            if (Endianess == DiFile.EndianBig)
            {
                _groupId = (b0 << 8) | b1;
            }

            // --- meta group part end ------------------------------

            _elementId = inputStream.ReadUShort(Endianess);

            b0 = (uint)inputStream.ReadByte();
            b1 = (uint)inputStream.ReadByte();

            _vr = (VRType)((b0 << 8) | b1);

            // check if we are explicit or implicit:
            // b0 and b1 could a) be an explicit VR or b) be the first part of the VL
            exp = (vrFormat == DiFile.VrExplicit) || (vrFormat == DiFile.VrUnknown &&
                                                      (_vr == VRType.AE || _vr == VRType.AS || _vr == VRType.AT ||
                                                       _vr == VRType.CS || _vr == VRType.DA || _vr == VRType.DS ||
                                                       _vr == VRType.DT || _vr == VRType.FD || _vr == VRType.FL ||
                                                       _vr == VRType.IS || _vr == VRType.LO || _vr == VRType.LT ||
                                                       _vr == VRType.PN || _vr == VRType.SH || _vr == VRType.SL ||
                                                       _vr == VRType.SS || _vr == VRType.ST || _vr == VRType.TM ||
                                                       _vr == VRType.UI || _vr == VRType.UL || _vr == VRType.US ||
                                                       _vr == VRType.UT || _vr == VRType.OB || _vr == VRType.OW ||
                                                       _vr == VRType.SQ || _vr == VRType.UN || _vr == VRType.QQ));

            // There are three special SQ related Data Elements that are not ruled by the VR encoding rules
            // conveyed by the Transfer Syntax. They shall be encoded as Implicit VR. These special Data Elements are
            // Item (FFFE,E000), Item Delimitation Item (FFFE,E00D), and Sequence Delimitation Item (FFFE,E0DD).
            // However, the Data Set within the Value Field of the Data Element Item (FFFE,E000) shall be encoded
            // according to the rules conveyed by the Transfer Syntax.
            if (_groupId == 0xfffe && (_elementId == 0xe000 || _elementId == 0xe00d || _elementId == 0xe0dd))
            {
                exp = false;
            }

            if (exp)
            {
                // explicit VR -> get the VR first
                // VL can have 2 or 4 byte ...
                if (_vr == VRType.OB || _vr == VRType.OW || _vr == VRType.SQ || _vr == VRType.UT || _vr == VRType.UN)
                {
                    inputStream.ReadByte(); // skip 2 bytes ...
                    inputStream.ReadByte();
                    _vl = inputStream.ReadInt(Endianess);
                }
                else
                {
                    _vl = inputStream.ReadShort(Endianess);
                }
            }
            else
            {
                // implicit VR -> lookup VR in the DicomDictionary
                _vr = _diDictionary.getVR(GetTag());

                uint b2 = (uint)inputStream.ReadByte(), b3 = (uint)inputStream.ReadByte();

                if (Endianess == DiFile.EndianLittle)
                {
                    _vl = (int)((b3 << 24) + (b2 << 16) + (b1 << 8) + b0);
                }
                else
                {
                    _vl = (int)((b0 << 24) + (b1 << 16) + (b2 << 8) + b3);
                }
            }

            if (_vl == -1)
            {
                _vl = 0;            // _vl can be -1 if VR == SQ
            }
            _values = new byte[_vl];
            inputStream.Read(_values, 0, _values.Length);

            if (Endianess == DiFile.EndianBig)
            {
                // VR's affected by endianess:
                //   2-byte US, SS, OW and each component of AT
                //   4-byte UL, SL, and FL
                //   8 byte FD
                if (_vr == VRType.US || _vr == VRType.SS || _vr == VRType.OW || _vr == VRType.UL || _vr == VRType.SL ||
                    _vr == VRType.FL || _vr == VRType.FD)
                {
                    for (var i = 0; i < _values.Length / 2; i++)
                    {
                        var tmp = _values[i];
                        _values[i] = _values[_values.Length - 1 - i];
                        _values[_values.Length - 1 - i] = tmp;
                    }
                }
            }

            if (DiDictonary.ToTag(_groupId, _elementId) == 0x00020010)
            {
                // check endianess and VR format
                var tsUid = GetValueAsString();
                inputStream.VrFormat  = DiDictonary.get_ts_uid_vr_format(tsUid);
                inputStream.Endianess = DiDictonary.get_ts_uid_endianess(tsUid);
                if (inputStream.VrFormat == DiFile.EndianUnknown)
                {
                    Debug.Log("DiDataElement Unknown Transfer Syntax UID \"" + tsUid +
                              "\". Endianess & VR format will be guessed.");
                }
            }

            try
            {
                _rawInt = GetValueAsInt();
            }
            catch (Exception)
            {
                //nothing to worry about, some elements aren't supposed to be used with Int
            }

            try
            {
                if (_groupId == 0x0028 && (_elementId == 0x1050 || _elementId == 0x1051))
                {
                    var numbers = GetValueAsString().Split('\\');
                    _rawDoubles = new double[numbers.Length];

                    for (var index = 0; index < numbers.Length; index++)
                    {
                        _rawDoubles[index] = double.Parse(numbers[index]);
                    }
                }
                else
                {
                    _rawDoubles[0] = GetValueAsDouble();
                }
            }
            catch (Exception)
            {
                //nothing to worry about, some elements aren't supposed to be used with Double
            }
        }
Example #4
0
 /// <summary>
 /// Returns the DiDataElement with the given ids. Can return null.
 /// </summary>
 /// <param name="groupId"></param>
 /// <param name="elementId"></param>
 /// <returns></returns>
 public DiDataElement GetElement(uint groupId, uint elementId)
 {
     return(_dataElements.GetValue(DiDictonary.ToTag(groupId, elementId)));
 }