private bool IsPrivateSequenceBad(IByteSource source) { source.Mark(); try { var group = source.GetUInt16(); var element = source.GetUInt16(); var tag = new DicomTag(group, element); var length = source.GetUInt32(); group = source.GetUInt16(); element = source.GetUInt16(); tag = new DicomTag(group, element); byte[] bytes = source.GetBytes(2); string vr = Encoding.UTF8.GetString(bytes, 0, bytes.Length); try { DicomVR.Parse(vr); return(!_explicit); } catch { // unable to parse VR if (_explicit) { return(true); } } } finally { source.Rewind(); } return(false); }
public void All_Defined_VRs_Can_Roundtrip() { foreach (var vr in typeof(DicomVR).GetProperties(BindingFlags.Static | BindingFlags.Public) .Where(pi => pi.PropertyType == typeof(DicomVR)) .Select(pi => (DicomVR)pi.GetValue(pi, new object[0]))) { Assert.True(DicomVR.Parse(vr.Code) == vr); } }
/// <summary> /// Initializes a new instance of the <see cref="QueryTag"/> class. /// </summary> /// <remarks>Used for constuctoring from extended query tags.</remarks> /// <param name="entry">The extended query tag store entry.</param> public QueryTag(ExtendedQueryTagStoreEntry entry) { EnsureArg.IsNotNull(entry, nameof(entry)); string fullPath = string.IsNullOrEmpty(entry.PrivateCreator) ? entry.Path : $"{entry.Path}:{entry.PrivateCreator}"; Tag = DicomTag.Parse(fullPath); VR = DicomVR.Parse(entry.VR); Level = entry.Level; ExtendedQueryTagStoreEntry = entry; }
private static DicomVR ParseVRCode(string vrCode, string tagPath) { try { // DicomVR.Parse only accept upper case VR code. return(DicomVR.Parse(vrCode.ToUpper(CultureInfo.InvariantCulture))); } catch (DicomDataException ex) { throw new ExtendedQueryTagEntryValidationException( string.Format(CultureInfo.InvariantCulture, DicomCoreResource.InvalidVRCode, vrCode, tagPath), ex); } }
protected virtual void ReadDicomItem ( JsonTextReader reader, DicomTag tag, DicomDataset dataset, int level ) { var vr = tag.DictionaryEntry.ValueRepresentations.FirstOrDefault( ); while (reader.Read( ) && reader.TokenType == JsonToken.PropertyName) { string propertyValue = (string)reader.Value; switch (propertyValue) { case JsonConstants.VrField: { reader.Read( ); vr = DicomVR.Parse((string)reader.Value); } break; case JsonConstants.ValueField: { ReadDefaultVr(tag, vr, reader, dataset, level); } break; case JsonConstants.ELEMENT_BULKDATA: { ReadBulkData(tag, vr, reader, dataset, level); } break; case JsonConstants.ELEMENT_INLINEBINARY: { ReadInlineBinary(tag, vr, reader, dataset, level); } break; default: { reader.Skip( ); } break; } } }
private void ReadDicomAttribute(DicomDataset ds, XElement element, int level) { XAttribute vrNode; DicomTag tag; DicomDictionaryEntry dicEntry; DicomVR dicomVR; vrNode = element.Attribute(Constants.ATTRIBUTE_VR); tag = DicomTag.Parse(element.Attribute(Constants.ATTRIBUTE_TAG).Value); dicomVR = null; //if ( tag.ToString ("J") == "00020010" ) //{ // ds.InternalTransferSyntax = ReadValue ( element ).FirstOrDefault ( ) ; //} if (vrNode != null && !string.IsNullOrEmpty(vrNode.Value)) { dicomVR = DicomVR.Parse(vrNode.Value); } if (tag.IsPrivate) { tag = ds.GetPrivateTag(tag); if (null != vrNode) { dicomVR = DicomVR.Parse(vrNode.Value); } } if (null == dicomVR) { dicEntry = DicomDictionary.Default[tag]; dicomVR = dicEntry.ValueRepresentations.FirstOrDefault( ); } if (dicomVR == DicomVR.SQ) { ReadSequence(ds, element, tag, level); } else { ReadElement(ds, element, tag, dicomVR, level); } }
private static DicomVR TryParseVr(BsonValue bsonValue) { if (bsonValue == BsonNull.Value) { return(null); } var asBsonDocument = bsonValue as BsonDocument; if (asBsonDocument == null || !asBsonDocument.Contains("vr")) { return(null); } return(DicomVR.Parse(asBsonDocument["vr"].AsString)); }
public void All_Known_VRs_Exist() { foreach (var vr in AllVRs) { Assert.True(DicomVR.Parse(vr).Code == vr); Assert.True(DicomVR.Parse(vr).ToString() == vr); DicomVR parsedVr; Assert.True(DicomVR.TryParse(vr, out parsedVr)); Assert.True(parsedVr.Code == vr); Assert.False(DicomVR.TryParse(vr + "X", out parsedVr)); Assert.False(DicomVR.TryParse(vr + " ", out parsedVr)); Assert.False(DicomVR.TryParse(" " + vr, out parsedVr)); } // "NONE" is a special case, so let's make it so here as well. Assert.True(DicomVR.Parse("NONE") == DicomVR.NONE); }
private static DicomVR ParseVRCode(string vrCode) { if (string.IsNullOrWhiteSpace(vrCode)) { throw new CustomTagEntryValidationException(DicomCoreResource.MissingVRCode); } try { // DicomVR.Parse only accept upper case VR code. return(DicomVR.Parse(vrCode.ToUpper(CultureInfo.InvariantCulture))); } catch (DicomDataException ex) { throw new CustomTagEntryValidationException( string.Format(CultureInfo.InvariantCulture, DicomCoreResource.InvalidVRCode, vrCode), ex); } }
public static AnonymizationDicomTagRule CreateAnonymizationDICOMRule(Dictionary <string, object> config) { if (config == null) { throw new ArgumentNullException("config"); } if (!config.ContainsKey(Constants.TagKey)) { throw new ArgumentException("Missing tag in rule config"); } if (!config.ContainsKey(Constants.MethodKey)) { throw new ArgumentException("Missing method in rule config"); } TagRule tagRule; try { tagRule = JsonConvert.DeserializeObject <TagRule>(config[Constants.TagKey].ToString()); } catch (JsonException innerException) { throw new JsonException($"Failed to parse configuration file", innerException); } var method = config[Constants.MethodKey].ToString(); if (!string.IsNullOrEmpty(tagRule.DicomTagValue)) { var tag = DicomTag.Parse(tagRule.DicomTagValue); return(new AnonymizationDicomTagRule(tag, method, config)); } else if (!string.IsNullOrEmpty(tagRule.DicomTagVR)) { var vr = DicomVR.Parse(tagRule.DicomTagVR.ToString()); return(new AnonymizationDicomTagRule(vr, method, config)); } throw new AnonymizerConfigurationErrorsException("Invaid tag in rule config"); }
public static void FilterByVR(DicomFile dicomFile, string vr) { var dataset = dicomFile.Dataset; string strFilePath = @"tciadicoms-SH.csv"; foreach (var item in dataset) { if (item.ValueRepresentation.Equals(DicomVR.Parse(vr))) { var tmpresult = dataset.GetValues <object>(item.Tag); if (tmpresult.Count() == 0) { continue; } var result = dicomFile.File.ToString() + "," + item.Tag.ToString(); foreach (var i in tmpresult) { result = string.Format("{0},{1}\n", result, i.ToString()); } File.AppendAllText(strFilePath, result); } } }
private void ParseDataset(IByteSource source, object state) { try { _result = DicomReaderResult.Processing; while (!source.IsEOF && !source.HasReachedMilestone() && _result == DicomReaderResult.Processing) { if (_state == ParseState.Tag) { source.Mark(); if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } ushort group = source.GetUInt16(); ushort element = source.GetUInt16(); DicomPrivateCreator creator = null; if (group.IsOdd() && element > 0x00ff) { string pvt = null; uint card = (uint)(group << 16) + (uint)(element >> 8); if (_private.TryGetValue(card, out pvt)) { creator = Dictionary.GetPrivateCreator(pvt); } } _tag = new DicomTag(group, element, creator); if (_stop != null && _tag.CompareTo(_stop) >= 0) { _result = DicomReaderResult.Stopped; return; } _state = ParseState.VR; } while (_state == ParseState.VR) { if (_tag == DicomTag.Item || _tag == DicomTag.ItemDelimitationItem || _tag == DicomTag.SequenceDelimitationItem) { _vr = DicomVR.NONE; _state = ParseState.Length; break; } if (IsExplicitVR) { if (!source.Require(2, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } byte[] bytes = source.GetBytes(2); string vr = Encoding.UTF8.GetString(bytes, 0, bytes.Length); try { _vr = DicomVR.Parse(vr); } catch { // unable to parse VR _vr = DicomVR.UN; } } else { DicomDictionaryEntry entry = Dictionary[_tag]; if (entry != null) { if (entry == DicomDictionary.UnknownTag) { _vr = DicomVR.UN; } else if (entry.ValueRepresentations.Contains(DicomVR.OB) && entry.ValueRepresentations.Contains(DicomVR.OW)) { _vr = DicomVR.OW; // ??? } else { _vr = entry.ValueRepresentations.FirstOrDefault(); } } } if (_vr == null) { _vr = DicomVR.UN; } _state = ParseState.Length; if (_vr == DicomVR.UN) { if (_tag.Element == 0x0000) { // Group Length to UL _vr = DicomVR.UL; break; } else if (IsExplicitVR) { break; } } if (_tag.IsPrivate) { if (_tag.Element != 0x0000 && _tag.Element <= 0x00ff) { _vr = DicomVR.LO; // force private creator to LO } } } while (_state == ParseState.Length) { if (_tag == DicomTag.Item || _tag == DicomTag.ItemDelimitationItem || _tag == DicomTag.SequenceDelimitationItem) { if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt32(); _state = ParseState.Value; break; } if (IsExplicitVR) { if (_vr.Is16bitLength) { if (!source.Require(2, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt16(); } else { if (!source.Require(6, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } source.Skip(2); _length = source.GetUInt32(); } } else { if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt32(); // assume that undefined length in implicit dataset is SQ if (_length == UndefinedLength && _vr == DicomVR.UN) { _vr = DicomVR.SQ; } } _state = ParseState.Value; } if (_state == ParseState.Value) { // check dictionary for VR after reading length to handle 16-bit lengths // check before reading value to handle SQ elements if (_vr == DicomVR.UN && IsExplicitVR) { var entry = Dictionary[_tag]; if (entry != null) { _vr = entry.ValueRepresentations.FirstOrDefault(); } if (_vr == null) { _vr = DicomVR.UN; } } if (_tag == DicomTag.ItemDelimitationItem) { // end of sequence item return; } while (_vr == DicomVR.SQ && _tag.IsPrivate) { if (!IsPrivateSequence(source)) { _vr = DicomVR.UN; break; } if (IsPrivateSequenceBad(source)) { _badPrivateSequence = true; _explicit = !_explicit; } break; } if (_vr == DicomVR.SQ) { // start of sequence _observer.OnBeginSequence(source, _tag, _length); _state = ParseState.Tag; if (_length != UndefinedLength) { _implicit = false; source.PushMilestone(_length); } else { _implicit = true; } PushState(state); ParseItemSequence(source, null); continue; } if (_length == UndefinedLength) { _observer.OnBeginFragmentSequence(source, _tag, _vr); _state = ParseState.Tag; PushState(state); ParseFragmentSequence(source, null); continue; } if (!source.Require(_length, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } IByteBuffer buffer = source.GetBuffer(_length); if (!_vr.IsString) { buffer = EndianByteBuffer.Create(buffer, source.Endian, _vr.UnitSize); } _observer.OnElement(source, _tag, _vr, buffer); // parse private creator value and add to lookup table if (_tag.IsPrivate && _tag.Element != 0x0000 && _tag.Element <= 0x00ff) { var creator = DicomEncoding.Default.GetString(buffer.Data, 0, buffer.Data.Length).TrimEnd((char)DicomVR.LO.PaddingValue); var card = (uint)(_tag.Group << 16) + (uint)(_tag.Element); _private[card] = creator; } ResetState(); } } if (source.HasReachedMilestone()) { // end of explicit length sequence item source.PopMilestone(); return; } if (_result != DicomReaderResult.Processing) { return; } // end of processing _result = DicomReaderResult.Success; } catch (Exception e) { _exception = e; _result = DicomReaderResult.Error; } finally { if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended) { _async.Set(); } } }
public void Unknown_VRs_are_unknown() { Assert.Throws <DicomDataException>(() => DicomVR.Parse("XX")); }
private void ParseDataset(IByteSource source, object state) { try { _result = DicomReaderResult.Processing; while (!source.IsEOF && !source.HasReachedMilestone()) { if (_state == ParseState.Tag) { source.Mark(); if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } ushort group = source.GetUInt16(); ushort element = source.GetUInt16(); DicomPrivateCreator creator = null; if (group.IsOdd() && element > 0x00ff) { string pvt = null; uint card = (uint)(group << 16) + (uint)(element >> 8); if (_private.TryGetValue(card, out pvt)) { creator = Dictionary.GetPrivateCreator(pvt); } } _tag = new DicomTag(group, element, creator); if (_stop != null && _tag.CompareTo(_stop) >= 0) { _result = DicomReaderResult.Stopped; return; } _state = ParseState.VR; } while (_state == ParseState.VR) { if (_tag == DicomTag.Item || _tag == DicomTag.ItemDelimitationItem || _tag == DicomTag.SequenceDelimitationItem) { _vr = DicomVR.NONE; _state = ParseState.Length; break; } if (IsExplicitVR) { if (!source.Require(2, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } byte[] bytes = source.GetBytes(2); string vr = Encoding.ASCII.GetString(bytes); _vr = DicomVR.Parse(vr); } else { DicomDictionaryEntry entry = Dictionary[_tag]; if (entry != null) { if (entry.ValueRepresentations.Contains(DicomVR.OB) && entry.ValueRepresentations.Contains(DicomVR.OW)) { _vr = DicomVR.OW; // ??? } else { _vr = entry.ValueRepresentations.FirstOrDefault(); } } } if (_vr == null) { _vr = DicomVR.UN; } _state = ParseState.Length; if (_vr == DicomVR.UN) { if (_tag.Element == 0x0000) { // Group Length to UL _vr = DicomVR.UL; break; } if (_tag.Group.IsOdd()) { if (_tag.Element <= 0x00ff) { // Private Creator to LO _vr = DicomVR.LO; break; } else if (IsExplicitVR) { DicomDictionaryEntry entry = Dictionary[_tag]; if (entry != null) { _vr = entry.ValueRepresentations.FirstOrDefault(); } if (_vr == null) { _vr = DicomVR.UN; } break; } } } } while (_state == ParseState.Length) { if (_tag == DicomTag.Item || _tag == DicomTag.ItemDelimitationItem || _tag == DicomTag.SequenceDelimitationItem) { if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt32(); _state = ParseState.Value; break; } if (IsExplicitVR) { if (_vr.Is16bitLength) { if (!source.Require(2, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt16(); } else { if (!source.Require(6, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } source.Skip(2); _length = source.GetUInt32(); } } else { if (!source.Require(4, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } _length = source.GetUInt32(); } _state = ParseState.Value; } if (_state == ParseState.Value) { if (_tag == DicomTag.ItemDelimitationItem) { // end of sequence item ParseItemSequence(source, state); return; } if (_vr == DicomVR.SQ) { // start of sequence _observer.OnBeginSequence(source, _tag, _length); _state = ParseState.Tag; if (_length != UndefinedLength) { source.PushMilestone(_length); } PushState(state); ParseItemSequence(source, null); continue; } if (_length == UndefinedLength) { _observer.OnBeginFragmentSequence(source, _tag, _vr); _state = ParseState.Tag; PushState(state); ParseFragmentSequence(source, null); continue; } if (!source.Require(_length, ParseDataset, state)) { _result = DicomReaderResult.Suspended; return; } IByteBuffer buffer = source.GetBuffer(_length); if (!_vr.IsString) { buffer = EndianByteBuffer.Create(buffer, source.Endian, _vr.UnitSize); } _observer.OnElement(source, _tag, _vr, buffer); ResetState(); } } if (source.HasReachedMilestone()) { // end of explicit length sequence item _observer.OnEndSequenceItem(); source.PopMilestone(); ParseItemSequence(source, state); return; } // end of processing _result = DicomReaderResult.Success; } catch (Exception e) { _exception = e; _result = DicomReaderResult.Error; } finally { if (_result != DicomReaderResult.Processing && _result != DicomReaderResult.Suspended) { _async.Set(); } } }
public void Parse_OF_ReturnsVRObject() { var actual = DicomVR.Parse("OF"); Assert.IsType <DicomVR>(actual); }